xref: /freebsd/lib/libc/gen/setproctitle.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 	va_list ap;
69 	size_t len;
70 	unsigned long ul_ps_strings;
71 	int oid[4];
72 
73 	va_start(ap, fmt);
74 
75 	if (fmt) {
76 		buf[sizeof(buf) - 1] = '\0';
77 
78 		if (fmt[0] == '-') {
79 			/* skip program name prefix */
80 			fmt++;
81 			len = 0;
82 		} else {
83 			/* print program name heading for grep */
84 			(void) snprintf(buf, sizeof(buf), "%s: ", __progname);
85 			len = strlen(buf);
86 		}
87 
88 		/* print the argument string */
89 		(void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
90 
91 		nargvp = nargv;
92 		nargc = 1;
93 		kbuf = buf;
94 	} else if (*obuf != '\0') {
95   		/* Idea from NetBSD - reset the title on fmt == NULL */
96 		nargvp = oargv;
97 		nargc = oargc;
98 		kbuf = obuf;
99 	} else
100 		/* Nothing to restore */
101 		return;
102 
103 	va_end(ap);
104 
105 	/* Set the title into the kernel cached command line */
106 	oid[0] = CTL_KERN;
107 	oid[1] = KERN_PROC;
108 	oid[2] = KERN_PROC_ARGS;
109 	oid[3] = getpid();
110 	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
111 
112 	if (ps_strings == NULL) {
113 		len = sizeof(ul_ps_strings);
114 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
115 		    0) == -1)
116 			ul_ps_strings = PS_STRINGS;
117 		ps_strings = (struct ps_strings *)ul_ps_strings;
118 	}
119 
120 	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
121 	if (ps_strings->ps_argvstr) {
122 		/* style #3 */
123 		if (oargc == -1) {
124 			/* Record our original args */
125 			oargc = ps_strings->ps_nargvstr;
126 			oargv = ps_strings->ps_argvstr;
127 			for (nargc = len = 0; nargc < oargc; nargc++) {
128 				snprintf(obuf + len, sizeof(obuf) - len, "%s%s",
129 				    len ? " " : "", oargv[nargc]);
130 				if (len)
131 					len++;
132 				len += strlen(oargv[nargc]);
133 				if (len >= sizeof(obuf))
134 					break;
135 			}
136 		}
137 		ps_strings->ps_nargvstr = nargc;
138 		ps_strings->ps_argvstr = nargvp;
139 	} else {
140 		/* style #2 - we can only restore our first arg :-( */
141 		if (*obuf == '\0')
142 			strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
143 			    sizeof(obuf) - 1);
144 		OLD_PS_STRINGS->old_ps_nargvstr = 1;
145 		OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
146 	}
147 }
148