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