xref: /dragonfly/lib/libc/gen/setproctitle.c (revision 1dedbd3b)
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  */
19 
20 #include "namespace.h"
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <sys/exec.h>
24 #include <sys/sysctl.h>
25 
26 #include <vm/pmap.h>
27 #include <vm/vm.h>
28 #include <vm/vm_param.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 #include "../upmap/upmap.h"
38 
39 /*
40  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
41  * in different locations.
42  * 1: old_ps_strings at the very top of the stack.
43  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
44  * 3: ps_strings at the very top of the stack.
45  * This attempts to support a kernel built in the #2 and #3 era.
46  */
47 
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 #include <stdarg.h>
58 
59 #define SPT_BUFSIZE 2048	/* from other parts of sendmail */
60 
61 void
62 setproctitle(const char *fmt, ...)
63 {
64 	static struct ps_strings *ps_strings;
65 	static char *buf = NULL;
66 	static char *obuf = NULL;
67 	static char **oargv, *kbuf;
68 	static int oargc = -1;
69 	static char *nargv[2] = { NULL, NULL };
70 	char **nargvp;
71 	int nargc;
72 	int i;
73 	va_list ap;
74 	size_t len;
75 	unsigned long ul_ps_strings;
76 	int oid[4];
77 
78 	va_start(ap, fmt);
79 
80 	if (__ukp_spt(fmt, ap) == 0)
81 		return;
82 
83 	if (buf == NULL) {
84 		buf = malloc(SPT_BUFSIZE);
85 		if (buf == NULL)
86 			return;
87 		nargv[0] = buf;
88 	}
89 
90 	if (obuf == NULL ) {
91 		obuf = malloc(SPT_BUFSIZE);
92 		if (obuf == NULL)
93 			return;
94 		*obuf = '\0';
95 	}
96 
97 	if (fmt) {
98 		buf[SPT_BUFSIZE - 1] = '\0';
99 
100 		if (fmt[0] == '-') {
101 			/* skip program name prefix */
102 			fmt++;
103 			len = 0;
104 		} else {
105 			/* print program name heading for grep */
106 			snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
107 			len = strlen(buf);
108 		}
109 
110 		/* print the argument string */
111 		vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
112 
113 		nargvp = nargv;
114 		nargc = 1;
115 		kbuf = buf;
116 	} else if (*obuf != '\0') {
117 		/* Idea from NetBSD - reset the title on fmt == NULL */
118 		nargvp = oargv;
119 		nargc = oargc;
120 		kbuf = obuf;
121 	} else
122 		/* Nothing to restore */
123 		return;
124 
125 	va_end(ap);
126 
127 	/* Set the title into the kernel cached command line */
128 	oid[0] = CTL_KERN;
129 	oid[1] = KERN_PROC;
130 	oid[2] = KERN_PROC_ARGS;
131 	oid[3] = getpid();
132 	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
133 
134 	if (ps_strings == NULL) {
135 		len = sizeof(ul_ps_strings);
136 		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
137 		    0) == -1)
138 			ul_ps_strings = PS_STRINGS;
139 		ps_strings = (struct ps_strings *)ul_ps_strings;
140 	}
141 
142 	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
143 	if (ps_strings->ps_argvstr) {
144 		/* style #3 */
145 		if (oargc == -1) {
146 			/* Record our original args */
147 			oargc = ps_strings->ps_nargvstr;
148 			oargv = ps_strings->ps_argvstr;
149 			for (i = len = 0; i < oargc; i++) {
150 				/*
151 				 * The program may have scribbled into its
152 				 * argv array, e.g., to remove some arguments.
153 				 * If that has happened, break out before
154 				 * trying to call strlen on a NULL pointer.
155 				 */
156 				if (oargv[i] == NULL) {
157 					oargc = i;
158 					break;
159 				}
160 				snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
161 				    len ? " " : "", oargv[i]);
162 				if (len)
163 					len++;
164 				len += strlen(oargv[i]);
165 				if (len >= SPT_BUFSIZE)
166 					break;
167 			}
168 		}
169 		ps_strings->ps_nargvstr = nargc;
170 		ps_strings->ps_argvstr = nargvp;
171 	} else {
172 		/* style #2 - we can only restore our first arg :-( */
173 		if (*obuf == '\0')
174 			strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
175 			    SPT_BUFSIZE - 1);
176 		OLD_PS_STRINGS->old_ps_nargvstr = 1;
177 		OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
178 	}
179 }
180