2 * Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, is permitted provided that the following conditions
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
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include "namespace.h"
22 #include <sys/param.h>
24 #include <sys/sysctl.h>
30 #include "un-namespace.h"
32 #include "libc_private.h"
35 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
36 * in different locations.
37 * 1: old_ps_strings at the very top of the stack.
38 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
39 * 3: ps_strings at the very top of the stack.
40 * We only support a kernel providing #3 style ps_strings.
42 * For historical purposes, a definition of the old ps_strings structure
43 * and location is preserved below:
44 struct old_ps_strings {
50 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
51 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
56 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */
59 setproctitle(const char *fmt, ...)
61 static struct ps_strings *ps_strings;
62 static char *buf = NULL;
63 static char *obuf = NULL;
64 static char **oargv, *kbuf;
65 static int oargc = -1;
66 static char *nargv[2] = { NULL, NULL };
72 unsigned long ul_ps_strings;
76 buf = malloc(SPT_BUFSIZE);
83 obuf = malloc(SPT_BUFSIZE);
92 buf[SPT_BUFSIZE - 1] = '\0';
95 /* skip program name prefix */
99 /* print program name heading for grep */
100 (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
104 /* print the argument string */
105 (void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
110 } else if (*obuf != '\0') {
111 /* Idea from NetBSD - reset the title on fmt == NULL */
116 /* Nothing to restore */
121 /* Set the title into the kernel cached command line */
124 oid[2] = KERN_PROC_ARGS;
126 sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
128 if (ps_strings == NULL) {
129 len = sizeof(ul_ps_strings);
130 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
133 ps_strings = (struct ps_strings *)ul_ps_strings;
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;