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 * We only support a kernel providing #3 style ps_strings.
46 *
47 * For historical purposes, a definition of the old ps_strings structure
48 * and location is preserved below:
49 struct old_ps_strings {
50 char *old_ps_argvstr;
51 int old_ps_nargvstr;
52 char *old_ps_envstr;
53 int old_ps_nenvstr;
54 };
55 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
56 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
57 */
58
59 #include <stdarg.h>
60
61 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */
62
63 void
setproctitle(const char * fmt,...)64 setproctitle(const char *fmt, ...)
65 {
66 static struct ps_strings *ps_strings;
67 static char *buf = NULL;
68 static char *obuf = NULL;
69 static char **oargv, *kbuf;
70 static int oargc = -1;
71 static char *nargv[2] = { NULL, NULL };
72 char **nargvp;
73 int nargc;
74 int i;
75 va_list ap;
76 size_t len;
77 unsigned long ul_ps_strings;
78 int oid[4];
79
80 if (buf == NULL) {
81 buf = malloc(SPT_BUFSIZE);
82 if (buf == NULL)
83 return;
84 nargv[0] = buf;
85 }
86
87 if (obuf == NULL ) {
88 obuf = malloc(SPT_BUFSIZE);
89 if (obuf == NULL)
90 return;
91 *obuf = '\0';
92 }
93
94 va_start(ap, fmt);
95
96 if (fmt) {
97 buf[SPT_BUFSIZE - 1] = '\0';
98
99 if (fmt[0] == '-') {
100 /* skip program name prefix */
101 fmt++;
102 len = 0;
103 } else {
104 /* print program name heading for grep */
105 (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
106 len = strlen(buf);
107 }
108
109 /* print the argument string */
110 (void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
111
112 nargvp = nargv;
113 nargc = 1;
114 kbuf = buf;
115 } else if (*obuf != '\0') {
116 /* Idea from NetBSD - reset the title on fmt == NULL */
117 nargvp = oargv;
118 nargc = oargc;
119 kbuf = obuf;
120 } else
121 /* Nothing to restore */
122 return;
123
124 va_end(ap);
125
126 /* Set the title into the kernel cached command line */
127 oid[0] = CTL_KERN;
128 oid[1] = KERN_PROC;
129 oid[2] = KERN_PROC_ARGS;
130 oid[3] = getpid();
131 sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
132
133 if (ps_strings == NULL) {
134 len = sizeof(ul_ps_strings);
135 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
136 0) == -1)
137 ul_ps_strings = PS_STRINGS;
138 ps_strings = (struct ps_strings *)ul_ps_strings;
139 }
140
141 /*
142 * PS_STRINGS points to zeroed memory on a style #2 kernel.
143 * Should not happen.
144 */
145 if (ps_strings->ps_argvstr == NULL)
146 return;
147
148 /* style #3 */
149 if (oargc == -1) {
150 /* Record our original args */
151 oargc = ps_strings->ps_nargvstr;
152 oargv = ps_strings->ps_argvstr;
153 for (i = len = 0; i < oargc; i++) {
154 /*
155 * The program may have scribbled into its
156 * argv array, e.g., to remove some arguments.
157 * If that has happened, break out before
158 * trying to call strlen on a NULL pointer.
159 */
160 if (oargv[i] == NULL) {
161 oargc = i;
162 break;
163 }
164 snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
165 len != 0 ? " " : "", oargv[i]);
166 if (len != 0)
167 len++;
168 len += strlen(oargv[i]);
169 if (len >= SPT_BUFSIZE)
170 break;
171 }
172 }
173 ps_strings->ps_nargvstr = nargc;
174 ps_strings->ps_argvstr = nargvp;
175 }
176