Debian Codegraph @

setproctitle.h

raw symbols SCIP
1 /* ==========================================================================
2 * setproctitle.h - Linux/Darwin setproctitle.
3 * --------------------------------------------------------------------------
4 * Copyright (C) 2010 William Ahern
5 * Copyright (C) 2013 Salvatore Sanfilippo
6 * Copyright (C) 2013 Stam He
7 * Copyright (C) 2013 Erik Dubbelboer
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to permit
14 * persons to whom the Software is furnished to do so, subject to the
15 * following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
23 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * ==========================================================================
28 */
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #endif
32
33 #include <stddef.h> // NULL size_t
34 #include <stdarg.h> // va_list va_start va_end
35 #include <stdlib.h> // malloc(3) setenv(3) clearenv(3) setproctitle(3) getprogname(3)
36 #include <stdio.h> // vsnprintf(3) snprintf(3)
37 #include <string.h> // strlen(3) strdup(3) memset(3) memcpy(3)
38 #include <errno.h> /* program_invocation_name program_invocation_short_name */
39 #include <sys/param.h> /* os versions */
40 #include <sys/types.h> /* freebsd setproctitle(3) */
41 #include <unistd.h> /* freebsd setproctitle(3) */
42
43 #if !defined(HAVE_SETPROCTITLE)
44 #if (__NetBSD__ || __FreeBSD__ || __OpenBSD__)
45 #define HAVE_SETPROCTITLE 1
46 #if (__FreeBSD__ && __FreeBSD_version > 1200000)
47 #define HAVE_SETPROCTITLE_FAST 1
48 #else
49 #define HAVE_SETPROCTITLE_FAST 0
50 #endif
51 #else
52 #define HAVE_SETPROCTITLE 0
53 #define HAVE_SETPROCTITLE_FAST 0
54 #endif
55 #endif
56
57
58 #if HAVE_SETPROCTITLE
59 #define HAVE_SETPROCTITLE_REPLACEMENT 0
60 #elif (defined __linux || defined __APPLE__)
61 #define HAVE_SETPROCTITLE_REPLACEMENT 1
62 #else
63 #define HAVE_SETPROCTITLE_REPLACEMENT 0
64 #endif
65
66
67 #if HAVE_SETPROCTITLE_REPLACEMENT
68
69 #ifndef SPT_MAXTITLE
70 #define SPT_MAXTITLE 255
71 #endif
72
73
74 extern char **environ;
75
76
77 static struct {
78 // Original value.
79 const char *arg0;
80
81 // First enviroment variable.
82 char* env0;
83
84 // Title space available.
85 char* base;
86 char* end;
87
88 // Pointer to original nul character within base.
89 char *nul;
90
91 int reset;
92 } SPT;
93
94
95 #ifndef SPT_MIN
96 #define SPT_MIN(a, b) (((a) < (b))? (a) : (b))
97 #endif
98
99
100 static inline size_t spt_min(size_t a, size_t b) {
101 return SPT_MIN(a, b);
102 }
103
104 static char **spt_find_argv_from_env(int argc, char *arg0) {
105 int i;
106 char **buf = NULL;
107 char *ptr;
108 char *limit;
109
110 if (!(buf = (char **)malloc((argc + 1) * sizeof(char *)))) {
111 return NULL;
112 }
113 buf[argc] = NULL;
114
115 // Walk back from environ until you find argc-1 null-terminated strings.
116 // Don't look for argv[0] as it's probably not preceded by 0.
117 ptr = SPT.env0;
118
119 if (!ptr) {
120 // If there is no env there is no space we can use.
121 return NULL;
122 }
123
124 limit = ptr - 8192; // TODO: empiric limit: should use MAX_ARG
125 --ptr;
126 for (i = argc - 1; i >= 1; --i) {
127 if (*ptr) {
128 return NULL;
129 }
130 --ptr;
131 while (*ptr && ptr > limit) { --ptr; }
132 if (ptr <= limit) {
133 return NULL;
134 }
135 buf[i] = (ptr + 1);
136 }
137
138 // The first arg has not a zero in front. But what we have is reliable
139 // enough (modulo its encoding). Check if it is exactly what found.
140 //
141 // The check is known to fail on OS X with locale C if there are
142 // non-ascii characters in the executable path.
143 ptr -= strlen(arg0);
144
145 if (ptr <= limit) {
146 return NULL;
147 }
148 if (strcmp(ptr, arg0)) {
149 return NULL;
150 }
151
152 buf[0] = ptr;
153 return buf;
154 }
155
156
157 static int spt_init1() {
158 // Store a pointer to the first environment variable since go
159 // will overwrite environment.
160 SPT.env0 = environ[0];
161
162 return 2;
163 }
164
165 static int spt_fast_init1() {
166 return 0;
167 }
168
169
170 static void spt_init2(int argc, char *arg0) {
171 char **argv = spt_find_argv_from_env(argc, arg0);
172 char **envp = &SPT.env0;
173 char *base, *end, *nul, *tmp;
174 int i;
175
176 if (!argv) {
177 return;
178 }
179
180 if (!(base = argv[0]))
181 return;
182
183 nul = &base[strlen(base)];
184 end = nul + 1;
185
186 for (i = 0; i < argc || (i >= argc && argv[i]); i++) {
187 if (!argv[i] || argv[i] < end)
188 continue;
189
190 end = argv[i] + strlen(argv[i]) + 1;
191 }
192
193 for (i = 0; envp[i]; i++) {
194 if (envp[i] < end)
195 continue;
196
197 end = envp[i] + strlen(envp[i]) + 1;
198 }
199
200 if (!(SPT.arg0 = strdup(argv[0])))
201 return;
202
203 #if __GLIBC__
204 if (!(tmp = strdup(program_invocation_name)))
205 return;
206
207 program_invocation_name = tmp;
208
209 if (!(tmp = strdup(program_invocation_short_name)))
210 return;
211
212 program_invocation_short_name = tmp;
213 #elif __APPLE__
214 if (!(tmp = strdup(getprogname())))
215 return;
216
217 setprogname(tmp);
218 #endif
219
220 memset(base, 0, end - base);
221
222 SPT.nul = nul;
223 SPT.base = base;
224 SPT.end = end;
225
226 return;
227 }
228
229
230 static void setproctitle(const char *fmt, ...) {
231 char buf[SPT_MAXTITLE + 1]; // Use buffer in case argv[0] is passed.
232 va_list ap;
233 char *nul;
234 int len;
235
236 if (!SPT.base)
237 return;
238
239 if (fmt) {
240 va_start(ap, fmt);
241 len = vsnprintf(buf, sizeof buf, fmt, ap);
242 va_end(ap);
243 } else {
244 len = snprintf(buf, sizeof buf, "%s", SPT.arg0);
245 }
246
247 if (len <= 0) {
248 return;
249 }
250
251 if (!SPT.reset) {
252 memset(SPT.base, 0, SPT.end - SPT.base);
253 SPT.reset = 1;
254 } else {
255 memset(SPT.base, 0, spt_min(sizeof buf, SPT.end - SPT.base));
256 }
257
258 len = spt_min(len, spt_min(sizeof buf, SPT.end - SPT.base) - 1);
259 memcpy(SPT.base, buf, len);
260 nul = &SPT.base[len];
261
262 if (nul < SPT.nul) {
263 memset(nul, ' ', SPT.nul - nul);
264 } else if (nul == SPT.nul && &nul[1] < SPT.end) {
265 *SPT.nul = ' ';
266 *++nul = '\0';
267 }
268 }
269
270
271 #else // HAVE_SETPROCTITLE_REPLACEMENT
272
273 static int spt_init1() {
274 #if HAVE_SETPROCTITLE
275 return 1;
276 #else
277 return 0;
278 #endif
279 }
280
281 static int spt_fast_init1() {
282 #if HAVE_SETPROCTITLE_FAST
283 return 1;
284 #else
285 return 0;
286 #endif
287 }
288
289 static void spt_init2(int argc, char *arg0) {
290 (void)argc;
291 (void)arg0;
292 }
293
294 #endif // HAVE_SETPROCTITLE_REPLACEMENT
295
296
297
298 static void spt_setproctitle(const char *title) {
299 #if HAVE_SETPROCTITLE || HAVE_SETPROCTITLE_REPLACEMENT
300 setproctitle("%s", title);
301 #else
302 (void)title;
303 #endif
304 }
305
306 static void spt_setproctitle_fast(const char *title) {
307 #if HAVE_SETPROCTITLE_FAST
308 setproctitle_fast("%s", title);
309 #else
310 (void)title;
311 #endif
312 }
313