xref: /freebsd/lib/libc/gen/setproctitle.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1995 Peter Wemm
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "namespace.h"
32 #include <sys/param.h>
33 #include <sys/elf_common.h>
34 #include <sys/exec.h>
35 #include <sys/sysctl.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include "un-namespace.h"
42 
43 #include "libc_private.h"
44 
45 /*
46  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
47  * in different locations.
48  * 1: old_ps_strings at the very top of the stack.
49  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
50  * 3: ps_strings at the very top of the stack.
51  * We only support a kernel providing #3 style ps_strings.
52  *
53  * For historical purposes, a definition of the old ps_strings structure
54  * and location is preserved below:
55 struct old_ps_strings {
56 	char	*old_ps_argvstr;
57 	int	old_ps_nargvstr;
58 	char	*old_ps_envstr;
59 	int	old_ps_nenvstr;
60 };
61 #define	OLD_PS_STRINGS ((struct old_ps_strings *) \
62 	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
63  */
64 
65 #include <stdarg.h>
66 
67 #define SPT_BUFSIZE 2048	/* from other parts of sendmail */
68 
69 static char *
70 setproctitle_internal(const char *fmt, va_list ap)
71 {
72 	static struct ps_strings *ps_strings;
73 	static char *buf = NULL;
74 	static char *obuf = NULL;
75 	static char **oargv;
76 	static int oargc = -1;
77 	static char *nargv[2] = { NULL, NULL };
78 	char **nargvp;
79 	int nargc;
80 	int i;
81 	size_t len;
82 	unsigned long ul_ps_strings;
83 
84 	if (buf == NULL) {
85 		buf = malloc(SPT_BUFSIZE);
86 		if (buf == NULL)
87 			return (NULL);
88 		nargv[0] = buf;
89 	}
90 
91 	if (obuf == NULL ) {
92 		obuf = malloc(SPT_BUFSIZE);
93 		if (obuf == NULL)
94 			return (NULL);
95 		*obuf = '\0';
96 	}
97 
98 	if (fmt) {
99 		buf[SPT_BUFSIZE - 1] = '\0';
100 
101 		if (fmt[0] == '-') {
102 			/* skip program name prefix */
103 			fmt++;
104 			len = 0;
105 		} else {
106 			/* print program name heading for grep */
107 			(void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
108 			len = strlen(buf);
109 		}
110 
111 		/* print the argument string */
112 		(void)vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
113 
114 		nargvp = nargv;
115 		nargc = 1;
116 	} else if (*obuf != '\0') {
117 		/* Idea from NetBSD - reset the title on fmt == NULL */
118 		nargvp = oargv;
119 		nargc = oargc;
120 	} else
121 		/* Nothing to restore */
122 		return (NULL);
123 
124 	if (ps_strings == NULL)
125 		(void)_elf_aux_info(AT_PS_STRINGS, &ps_strings,
126 		    sizeof(ps_strings));
127 
128 	if (ps_strings == NULL) {
129 		len = sizeof(ul_ps_strings);
130 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
131 		    0) == -1)
132 			return (NULL);
133 		ps_strings = (struct ps_strings *)ul_ps_strings;
134 	}
135 
136 	if (ps_strings == NULL)
137 		return (NULL);
138 
139 	/*
140 	 * PS_STRINGS points to zeroed memory on a style #2 kernel.
141 	 * Should not happen.
142 	 */
143 	if (ps_strings->ps_argvstr == NULL)
144 		return (NULL);
145 
146 	/* style #3 */
147 	if (oargc == -1) {
148 		/* Record our original args */
149 		oargc = ps_strings->ps_nargvstr;
150 		oargv = ps_strings->ps_argvstr;
151 		for (i = len = 0; i < oargc; i++) {
152 			/*
153 			 * The program may have scribbled into its
154 			 * argv array, e.g., to remove some arguments.
155 			 * If that has happened, break out before
156 			 * trying to call strlen on a NULL pointer.
157 			 */
158 			if (oargv[i] == NULL) {
159 				oargc = i;
160 				break;
161 			}
162 			snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
163 			    len != 0 ? " " : "", oargv[i]);
164 			if (len != 0)
165 				len++;
166 			len += strlen(oargv[i]);
167 			if (len >= SPT_BUFSIZE)
168 				break;
169 		}
170 	}
171 	ps_strings->ps_nargvstr = nargc;
172 	ps_strings->ps_argvstr = nargvp;
173 
174 	return (nargvp[0]);
175 }
176 
177 static int fast_update = 0;
178 
179 void
180 setproctitle_fast(const char *fmt, ...)
181 {
182 	va_list ap;
183 	char *buf;
184 	int oid[4];
185 
186 	va_start(ap, fmt);
187 	buf = setproctitle_internal(fmt, ap);
188 	va_end(ap);
189 
190 	if (buf && !fast_update) {
191 		/* Tell the kernel to start looking in user-space */
192 		oid[0] = CTL_KERN;
193 		oid[1] = KERN_PROC;
194 		oid[2] = KERN_PROC_ARGS;
195 		oid[3] = -1;
196 		sysctl(oid, 4, 0, 0, "", 0);
197 		fast_update = 1;
198 	}
199 }
200 
201 void
202 setproctitle(const char *fmt, ...)
203 {
204 	va_list ap;
205 	char *buf;
206 	int oid[4];
207 
208 	va_start(ap, fmt);
209 	buf = setproctitle_internal(fmt, ap);
210 	va_end(ap);
211 
212 	if (buf != NULL) {
213 		/* Set the title into the kernel cached command line */
214 		oid[0] = CTL_KERN;
215 		oid[1] = KERN_PROC;
216 		oid[2] = KERN_PROC_ARGS;
217 		oid[3] = -1;
218 		sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1);
219 		fast_update = 0;
220 	}
221 }
222