xref: /freebsd/lib/libc/gen/setproctitle.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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  * $FreeBSD$
18  */
19 
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/exec.h>
23 #include <sys/sysctl.h>
24 
25 #include <vm/vm.h>
26 #include <vm/vm_param.h>
27 #include <vm/pmap.h>
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 /*
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  * This attempts to support a kernel built in the #2 and #3 era.
41  */
42 
43 struct old_ps_strings {
44 	char	*old_ps_argvstr;
45 	int	old_ps_nargvstr;
46 	char	*old_ps_envstr;
47 	int	old_ps_nenvstr;
48 };
49 #define	OLD_PS_STRINGS ((struct old_ps_strings *) \
50 	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
51 
52 #include <stdarg.h>
53 
54 #define SPT_BUFSIZE 2048	/* from other parts of sendmail */
55 extern char * __progname;	/* is this defined in a .h anywhere? */
56 
57 void
58 setproctitle(const char *fmt, ...)
59 {
60 	static struct ps_strings *ps_strings;
61 	static char buf[SPT_BUFSIZE];
62 	static char obuf[SPT_BUFSIZE];
63 	static char **oargv, *kbuf;
64 	static int oargc = -1;
65 	static char *nargv[2] = { buf, NULL };
66 	char **nargvp;
67 	int nargc;
68 	int i;
69 	va_list ap;
70 	size_t len;
71 	unsigned long ul_ps_strings;
72 	int oid[4];
73 
74 	va_start(ap, fmt);
75 
76 	if (fmt) {
77 		buf[sizeof(buf) - 1] = '\0';
78 
79 		if (fmt[0] == '-') {
80 			/* skip program name prefix */
81 			fmt++;
82 			len = 0;
83 		} else {
84 			/* print program name heading for grep */
85 			(void) snprintf(buf, sizeof(buf), "%s: ", __progname);
86 			len = strlen(buf);
87 		}
88 
89 		/* print the argument string */
90 		(void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
91 
92 		nargvp = nargv;
93 		nargc = 1;
94 		kbuf = buf;
95 	} else if (*obuf != '\0') {
96   		/* Idea from NetBSD - reset the title on fmt == NULL */
97 		nargvp = oargv;
98 		nargc = oargc;
99 		kbuf = obuf;
100 	} else
101 		/* Nothing to restore */
102 		return;
103 
104 	va_end(ap);
105 
106 	/* Set the title into the kernel cached command line */
107 	oid[0] = CTL_KERN;
108 	oid[1] = KERN_PROC;
109 	oid[2] = KERN_PROC_ARGS;
110 	oid[3] = getpid();
111 	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
112 
113 	if (ps_strings == NULL) {
114 		len = sizeof(ul_ps_strings);
115 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
116 		    0) == -1)
117 			ul_ps_strings = PS_STRINGS;
118 		ps_strings = (struct ps_strings *)ul_ps_strings;
119 	}
120 
121 	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
122 	if (ps_strings->ps_argvstr) {
123 		/* style #3 */
124 		if (oargc == -1) {
125 			/* Record our original args */
126 			oargc = ps_strings->ps_nargvstr;
127 			oargv = ps_strings->ps_argvstr;
128 			for (i = len = 0; i < oargc; i++) {
129 				/*
130 				 * The program may have scribbled into its
131 				 * argv array, e.g., to remove some arguments.
132 				 * If that has happened, break out before
133 				 * trying to call strlen on a NULL pointer.
134 				 */
135 				if (oargv[i] == NULL) {
136 					oargc = i;
137 					break;
138 				}
139 				snprintf(obuf + len, sizeof(obuf) - len, "%s%s",
140 				    len ? " " : "", oargv[i]);
141 				if (len)
142 					len++;
143 				len += strlen(oargv[i]);
144 				if (len >= sizeof(obuf))
145 					break;
146 			}
147 		}
148 		ps_strings->ps_nargvstr = nargc;
149 		ps_strings->ps_argvstr = nargvp;
150 	} else {
151 		/* style #2 - we can only restore our first arg :-( */
152 		if (*obuf == '\0')
153 			strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
154 			    sizeof(obuf) - 1);
155 		OLD_PS_STRINGS->old_ps_nargvstr = 1;
156 		OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
157 	}
158 }
159