xref: /dragonfly/lib/libc/gen/setproctitle.c (revision 279dd846)
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.18 2003/07/01 09:45:35 alfred Exp $
18  * $DragonFly: src/lib/libc/gen/setproctitle.c,v 1.5 2005/11/13 00:07:42 swildner Exp $
19  */
20 
21 #include "namespace.h"
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/exec.h>
25 #include <sys/sysctl.h>
26 
27 #include <vm/vm.h>
28 #include <vm/vm_param.h>
29 #include <vm/pmap.h>
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include "un-namespace.h"
36 
37 #include "libc_private.h"
38 #include "../upmap/upmap.h"
39 
40 /*
41  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
42  * in different locations.
43  * 1: old_ps_strings at the very top of the stack.
44  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
45  * 3: ps_strings at the very top of the stack.
46  * This attempts to support a kernel built in the #2 and #3 era.
47  */
48 
49 struct old_ps_strings {
50 	char	*old_ps_argvstr;
51 	int	old_ps_nargvstr;
52 	char	*old_ps_envstr;
53 	int	old_ps_nenvstr;
54 };
55 #define	OLD_PS_STRINGS ((struct old_ps_strings *) \
56 	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
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 	va_start(ap, fmt);
80 
81 	if (__ukp_spt(fmt, ap) == 0)
82 		return;
83 
84 	if (buf == NULL) {
85 		buf = malloc(SPT_BUFSIZE);
86 		if (buf == NULL)
87 			return;
88 		nargv[0] = buf;
89 	}
90 
91 	if (obuf == NULL ) {
92 		obuf = malloc(SPT_BUFSIZE);
93 		if (obuf == NULL)
94 			return;
95 		*obuf = '\0';
96 	}
97 
98 	if (fmt) {
99 		buf[SPT_BUFSIZE - 1] = '\0';
100 
101 		if (fmt[0] == '-') {
102 			/* skip program name prefix */
103 			fmt++;
104 			len = 0;
105 		} else {
106 			/* print program name heading for grep */
107 			snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
108 			len = strlen(buf);
109 		}
110 
111 		/* print the argument string */
112 		vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
113 
114 		nargvp = nargv;
115 		nargc = 1;
116 		kbuf = buf;
117 	} else if (*obuf != '\0') {
118   		/* Idea from NetBSD - reset the title on fmt == NULL */
119 		nargvp = oargv;
120 		nargc = oargc;
121 		kbuf = obuf;
122 	} else
123 		/* Nothing to restore */
124 		return;
125 
126 	va_end(ap);
127 
128 	/* Set the title into the kernel cached command line */
129 	oid[0] = CTL_KERN;
130 	oid[1] = KERN_PROC;
131 	oid[2] = KERN_PROC_ARGS;
132 	oid[3] = getpid();
133 	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
134 
135 	if (ps_strings == NULL) {
136 		len = sizeof(ul_ps_strings);
137 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
138 		    0) == -1)
139 			ul_ps_strings = PS_STRINGS;
140 		ps_strings = (struct ps_strings *)ul_ps_strings;
141 	}
142 
143 	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
144 	if (ps_strings->ps_argvstr) {
145 		/* style #3 */
146 		if (oargc == -1) {
147 			/* Record our original args */
148 			oargc = ps_strings->ps_nargvstr;
149 			oargv = ps_strings->ps_argvstr;
150 			for (i = len = 0; i < oargc; i++) {
151 				/*
152 				 * The program may have scribbled into its
153 				 * argv array, e.g., to remove some arguments.
154 				 * If that has happened, break out before
155 				 * trying to call strlen on a NULL pointer.
156 				 */
157 				if (oargv[i] == NULL) {
158 					oargc = i;
159 					break;
160 				}
161 				snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
162 				    len ? " " : "", oargv[i]);
163 				if (len)
164 					len++;
165 				len += strlen(oargv[i]);
166 				if (len >= SPT_BUFSIZE)
167 					break;
168 			}
169 		}
170 		ps_strings->ps_nargvstr = nargc;
171 		ps_strings->ps_argvstr = nargvp;
172 	} else {
173 		/* style #2 - we can only restore our first arg :-( */
174 		if (*obuf == '\0')
175 			strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
176 			    SPT_BUFSIZE - 1);
177 		OLD_PS_STRINGS->old_ps_nargvstr = 1;
178 		OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
179 	}
180 }
181