2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1995 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include "namespace.h"
29 #include <sys/param.h>
30 #include <sys/elf_common.h>
32 #include <sys/sysctl.h>
38 #include "un-namespace.h"
40 #include "libc_private.h"
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.
50 * For historical purposes, a definition of the old ps_strings structure
51 * and location is preserved below:
52 struct old_ps_strings {
58 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
59 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
64 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */
67 setproctitle_internal(const char *fmt, va_list ap)
69 static struct ps_strings *ps_strings;
70 static char *buf = NULL;
71 static char *obuf = NULL;
73 static int oargc = -1;
74 static char *nargv[2] = { NULL, NULL };
79 unsigned long ul_ps_strings;
82 buf = malloc(SPT_BUFSIZE);
89 obuf = malloc(SPT_BUFSIZE);
96 buf[SPT_BUFSIZE - 1] = '\0';
99 /* skip program name prefix */
103 /* print program name heading for grep */
104 (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
108 /* print the argument string */
109 (void)vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
113 } else if (*obuf != '\0') {
114 /* Idea from NetBSD - reset the title on fmt == NULL */
118 /* Nothing to restore */
121 if (ps_strings == NULL)
122 (void)_elf_aux_info(AT_PS_STRINGS, &ps_strings,
125 if (ps_strings == NULL) {
126 len = sizeof(ul_ps_strings);
127 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
130 ps_strings = (struct ps_strings *)ul_ps_strings;
133 if (ps_strings == NULL)
137 * PS_STRINGS points to zeroed memory on a style #2 kernel.
140 if (ps_strings->ps_argvstr == NULL)
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++) {
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.
155 if (oargv[i] == NULL) {
159 snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
160 len != 0 ? " " : "", oargv[i]);
163 len += strlen(oargv[i]);
164 if (len >= SPT_BUFSIZE)
168 ps_strings->ps_nargvstr = nargc;
169 ps_strings->ps_argvstr = nargvp;
174 static int fast_update = 0;
177 setproctitle_fast(const char *fmt, ...)
184 buf = setproctitle_internal(fmt, ap);
187 if (buf && !fast_update) {
188 /* Tell the kernel to start looking in user-space */
191 oid[2] = KERN_PROC_ARGS;
193 sysctl(oid, 4, 0, 0, "", 0);
199 setproctitle(const char *fmt, ...)
206 buf = setproctitle_internal(fmt, ap);
210 /* Set the title into the kernel cached command line */
213 oid[2] = KERN_PROC_ARGS;
215 sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1);