xref: /freebsd/lib/libc/gen/setproctitle.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*
2  * Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, is permitted provided that the following conditions
7  * are met:
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
15  *    Peter Wemm.
16  */
17 
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
20 
21 #include "namespace.h"
22 #include <sys/param.h>
23 #include <sys/exec.h>
24 #include <sys/sysctl.h>
25 
26 #include <vm/vm.h>
27 #include <vm/vm_param.h>
28 #include <vm/pmap.h>
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include "un-namespace.h"
35 
36 #include "libc_private.h"
37 
38 /*
39  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
40  * in different locations.
41  * 1: old_ps_strings at the very top of the stack.
42  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
43  * 3: ps_strings at the very top of the stack.
44  * We only support a kernel providing #3 style ps_strings.
45  *
46  * For historical purposes, a definition of the old ps_strings structure
47  * and location is preserved below:
48 struct old_ps_strings {
49 	char	*old_ps_argvstr;
50 	int	old_ps_nargvstr;
51 	char	*old_ps_envstr;
52 	int	old_ps_nenvstr;
53 };
54 #define	OLD_PS_STRINGS ((struct old_ps_strings *) \
55 	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
56  */
57 
58 #include <stdarg.h>
59 
60 #define SPT_BUFSIZE 2048	/* from other parts of sendmail */
61 
62 void
63 setproctitle(const char *fmt, ...)
64 {
65 	static struct ps_strings *ps_strings;
66 	static char *buf = NULL;
67 	static char *obuf = NULL;
68 	static char **oargv, *kbuf;
69 	static int oargc = -1;
70 	static char *nargv[2] = { NULL, NULL };
71 	char **nargvp;
72 	int nargc;
73 	int i;
74 	va_list ap;
75 	size_t len;
76 	unsigned long ul_ps_strings;
77 	int oid[4];
78 
79 	if (buf == NULL) {
80 		buf = malloc(SPT_BUFSIZE);
81 		if (buf == NULL)
82 			return;
83 		nargv[0] = buf;
84 	}
85 
86 	if (obuf == NULL ) {
87 		obuf = malloc(SPT_BUFSIZE);
88 		if (obuf == NULL)
89 			return;
90 		*obuf = '\0';
91 	}
92 
93 	va_start(ap, fmt);
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 		kbuf = buf;
114 	} else if (*obuf != '\0') {
115   		/* Idea from NetBSD - reset the title on fmt == NULL */
116 		nargvp = oargv;
117 		nargc = oargc;
118 		kbuf = obuf;
119 	} else
120 		/* Nothing to restore */
121 		return;
122 
123 	va_end(ap);
124 
125 	/* Set the title into the kernel cached command line */
126 	oid[0] = CTL_KERN;
127 	oid[1] = KERN_PROC;
128 	oid[2] = KERN_PROC_ARGS;
129 	oid[3] = getpid();
130 	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
131 
132 	if (ps_strings == NULL) {
133 		len = sizeof(ul_ps_strings);
134 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
135 		    0) == -1)
136 			return;
137 		ps_strings = (struct ps_strings *)ul_ps_strings;
138 	}
139 
140 	/*
141 	 * PS_STRINGS points to zeroed memory on a style #2 kernel.
142 	 * Should not happen.
143 	 */
144 	if (ps_strings->ps_argvstr == NULL)
145 		return;
146 
147 	/* style #3 */
148 	if (oargc == -1) {
149 		/* Record our original args */
150 		oargc = ps_strings->ps_nargvstr;
151 		oargv = ps_strings->ps_argvstr;
152 		for (i = len = 0; i < oargc; i++) {
153 			/*
154 			 * The program may have scribbled into its
155 			 * argv array, e.g., to remove some arguments.
156 			 * If that has happened, break out before
157 			 * trying to call strlen on a NULL pointer.
158 			 */
159 			if (oargv[i] == NULL) {
160 				oargc = i;
161 				break;
162 			}
163 			snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
164 			    len != 0 ? " " : "", oargv[i]);
165 			if (len != 0)
166 				len++;
167 			len += strlen(oargv[i]);
168 			if (len >= SPT_BUFSIZE)
169 				break;
170 		}
171 	}
172 	ps_strings->ps_nargvstr = nargc;
173 	ps_strings->ps_argvstr = nargvp;
174 }
175