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