1 /* 2 * Copyright (c) 1995 Peter Wemm <peter@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, is permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * Peter Wemm. 16 */ 17 18 #include <sys/cdefs.h> 19 __FBSDID("$FreeBSD$"); 20 21 #include "namespace.h" 22 #include <sys/types.h> 23 #include <sys/param.h> 24 #include <sys/exec.h> 25 #include <sys/sysctl.h> 26 27 #include <vm/vm.h> 28 #include <vm/vm_param.h> 29 #include <vm/pmap.h> 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <stdlib.h> 34 #include <unistd.h> 35 #include "un-namespace.h" 36 37 #include "libc_private.h" 38 39 /* 40 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and 41 * in different locations. 42 * 1: old_ps_strings at the very top of the stack. 43 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. 44 * 3: ps_strings at the very top of the stack. 45 * This attempts to support a kernel built in the #2 and #3 era. 46 */ 47 48 struct old_ps_strings { 49 char *old_ps_argvstr; 50 int old_ps_nargvstr; 51 char *old_ps_envstr; 52 int old_ps_nenvstr; 53 }; 54 #define OLD_PS_STRINGS ((struct old_ps_strings *) \ 55 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) 56 57 #include <stdarg.h> 58 59 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */ 60 61 void 62 setproctitle(const char *fmt, ...) 63 { 64 static struct ps_strings *ps_strings; 65 static char buf[SPT_BUFSIZE]; 66 static char obuf[SPT_BUFSIZE]; 67 static char **oargv, *kbuf; 68 static int oargc = -1; 69 static char *nargv[2] = { buf, NULL }; 70 char **nargvp; 71 int nargc; 72 int i; 73 va_list ap; 74 size_t len; 75 unsigned long ul_ps_strings; 76 int oid[4]; 77 78 va_start(ap, fmt); 79 80 if (fmt) { 81 buf[sizeof(buf) - 1] = '\0'; 82 83 if (fmt[0] == '-') { 84 /* skip program name prefix */ 85 fmt++; 86 len = 0; 87 } else { 88 /* print program name heading for grep */ 89 (void)snprintf(buf, sizeof(buf), "%s: ", _getprogname()); 90 len = strlen(buf); 91 } 92 93 /* print the argument string */ 94 (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); 95 96 nargvp = nargv; 97 nargc = 1; 98 kbuf = buf; 99 } else if (*obuf != '\0') { 100 /* Idea from NetBSD - reset the title on fmt == NULL */ 101 nargvp = oargv; 102 nargc = oargc; 103 kbuf = obuf; 104 } else 105 /* Nothing to restore */ 106 return; 107 108 va_end(ap); 109 110 /* Set the title into the kernel cached command line */ 111 oid[0] = CTL_KERN; 112 oid[1] = KERN_PROC; 113 oid[2] = KERN_PROC_ARGS; 114 oid[3] = getpid(); 115 sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); 116 117 if (ps_strings == NULL) { 118 len = sizeof(ul_ps_strings); 119 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 120 0) == -1) 121 ul_ps_strings = PS_STRINGS; 122 ps_strings = (struct ps_strings *)ul_ps_strings; 123 } 124 125 /* PS_STRINGS points to zeroed memory on a style #2 kernel */ 126 if (ps_strings->ps_argvstr) { 127 /* style #3 */ 128 if (oargc == -1) { 129 /* Record our original args */ 130 oargc = ps_strings->ps_nargvstr; 131 oargv = ps_strings->ps_argvstr; 132 for (i = len = 0; i < oargc; i++) { 133 /* 134 * The program may have scribbled into its 135 * argv array, e.g., to remove some arguments. 136 * If that has happened, break out before 137 * trying to call strlen on a NULL pointer. 138 */ 139 if (oargv[i] == NULL) { 140 oargc = i; 141 break; 142 } 143 snprintf(obuf + len, sizeof(obuf) - len, "%s%s", 144 len ? " " : "", oargv[i]); 145 if (len) 146 len++; 147 len += strlen(oargv[i]); 148 if (len >= sizeof(obuf)) 149 break; 150 } 151 } 152 ps_strings->ps_nargvstr = nargc; 153 ps_strings->ps_argvstr = nargvp; 154 } else { 155 /* style #2 - we can only restore our first arg :-( */ 156 if (*obuf == '\0') 157 strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr, 158 sizeof(obuf) - 1); 159 OLD_PS_STRINGS->old_ps_nargvstr = 1; 160 OLD_PS_STRINGS->old_ps_argvstr = nargvp[0]; 161 } 162 } 163