xref: /freebsd/lib/libc/gen/setproctitle.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
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 "namespace.h"
29 #include <sys/param.h>
30 #include <sys/elf_common.h>
31 #include <sys/exec.h>
32 #include <sys/sysctl.h>
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include "un-namespace.h"
39 
40 #include "libc_private.h"
41 
42 /*
43  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
44  * in different locations.
45  * 1: old_ps_strings at the very top of the stack.
46  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
47  * 3: ps_strings at the very top of the stack.
48  * We only support a kernel providing #3 style ps_strings.
49  *
50  * For historical purposes, a definition of the old ps_strings structure
51  * and location is preserved below:
52 struct old_ps_strings {
53 	char	*old_ps_argvstr;
54 	int	old_ps_nargvstr;
55 	char	*old_ps_envstr;
56 	int	old_ps_nenvstr;
57 };
58 #define	OLD_PS_STRINGS ((struct old_ps_strings *) \
59 	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
60  */
61 
62 #include <stdarg.h>
63 
64 #define SPT_BUFSIZE 2048	/* from other parts of sendmail */
65 
66 static char *
67 setproctitle_internal(const char *fmt, va_list ap)
68 {
69 	static struct ps_strings *ps_strings;
70 	static char *buf = NULL;
71 	static char *obuf = NULL;
72 	static char **oargv;
73 	static int oargc = -1;
74 	static char *nargv[2] = { NULL, NULL };
75 	char **nargvp;
76 	int nargc;
77 	int i;
78 	size_t len;
79 	unsigned long ul_ps_strings;
80 
81 	if (buf == NULL) {
82 		buf = malloc(SPT_BUFSIZE);
83 		if (buf == NULL)
84 			return (NULL);
85 		nargv[0] = buf;
86 	}
87 
88 	if (obuf == NULL ) {
89 		obuf = malloc(SPT_BUFSIZE);
90 		if (obuf == NULL)
91 			return (NULL);
92 		*obuf = '\0';
93 	}
94 
95 	if (fmt) {
96 		buf[SPT_BUFSIZE - 1] = '\0';
97 
98 		if (fmt[0] == '-') {
99 			/* skip program name prefix */
100 			fmt++;
101 			len = 0;
102 		} else {
103 			/* print program name heading for grep */
104 			(void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
105 			len = strlen(buf);
106 		}
107 
108 		/* print the argument string */
109 		(void)vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
110 
111 		nargvp = nargv;
112 		nargc = 1;
113 	} else if (*obuf != '\0') {
114 		/* Idea from NetBSD - reset the title on fmt == NULL */
115 		nargvp = oargv;
116 		nargc = oargc;
117 	} else
118 		/* Nothing to restore */
119 		return (NULL);
120 
121 	if (ps_strings == NULL)
122 		(void)_elf_aux_info(AT_PS_STRINGS, &ps_strings,
123 		    sizeof(ps_strings));
124 
125 	if (ps_strings == NULL) {
126 		len = sizeof(ul_ps_strings);
127 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
128 		    0) == -1)
129 			return (NULL);
130 		ps_strings = (struct ps_strings *)ul_ps_strings;
131 	}
132 
133 	if (ps_strings == NULL)
134 		return (NULL);
135 
136 	/*
137 	 * PS_STRINGS points to zeroed memory on a style #2 kernel.
138 	 * Should not happen.
139 	 */
140 	if (ps_strings->ps_argvstr == NULL)
141 		return (NULL);
142 
143 	/* style #3 */
144 	if (oargc == -1) {
145 		/* Record our original args */
146 		oargc = ps_strings->ps_nargvstr;
147 		oargv = ps_strings->ps_argvstr;
148 		for (i = len = 0; i < oargc; i++) {
149 			/*
150 			 * The program may have scribbled into its
151 			 * argv array, e.g., to remove some arguments.
152 			 * If that has happened, break out before
153 			 * trying to call strlen on a NULL pointer.
154 			 */
155 			if (oargv[i] == NULL) {
156 				oargc = i;
157 				break;
158 			}
159 			snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
160 			    len != 0 ? " " : "", oargv[i]);
161 			if (len != 0)
162 				len++;
163 			len += strlen(oargv[i]);
164 			if (len >= SPT_BUFSIZE)
165 				break;
166 		}
167 	}
168 	ps_strings->ps_nargvstr = nargc;
169 	ps_strings->ps_argvstr = nargvp;
170 
171 	return (nargvp[0]);
172 }
173 
174 static int fast_update = 0;
175 
176 void
177 setproctitle_fast(const char *fmt, ...)
178 {
179 	va_list ap;
180 	char *buf;
181 	int oid[4];
182 
183 	va_start(ap, fmt);
184 	buf = setproctitle_internal(fmt, ap);
185 	va_end(ap);
186 
187 	if (buf && !fast_update) {
188 		/* Tell the kernel to start looking in user-space */
189 		oid[0] = CTL_KERN;
190 		oid[1] = KERN_PROC;
191 		oid[2] = KERN_PROC_ARGS;
192 		oid[3] = -1;
193 		sysctl(oid, 4, 0, 0, "", 0);
194 		fast_update = 1;
195 	}
196 }
197 
198 void
199 setproctitle(const char *fmt, ...)
200 {
201 	va_list ap;
202 	char *buf;
203 	int oid[4];
204 
205 	va_start(ap, fmt);
206 	buf = setproctitle_internal(fmt, ap);
207 	va_end(ap);
208 
209 	if (buf != NULL) {
210 		/* Set the title into the kernel cached command line */
211 		oid[0] = CTL_KERN;
212 		oid[1] = KERN_PROC;
213 		oid[2] = KERN_PROC_ARGS;
214 		oid[3] = -1;
215 		sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1);
216 		fast_update = 0;
217 	}
218 }
219