| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef _GNU_SOURCE |
| 30 |
#define _GNU_SOURCE |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <stddef.h> |
| 34 |
#include <stdarg.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <string.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <sys/param.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <unistd.h> |
| 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 |
|
| 79 |
const char *arg0; |
| 80 |
|
| 81 |
|
| 82 |
char* env0; |
| 83 |
|
| 84 |
|
| 85 |
char* base; |
| 86 |
char* end; |
| 87 |
|
| 88 |
|
| 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 |
|
| 116 |
|
| 117 |
ptr = SPT.env0; |
| 118 |
|
| 119 |
if (!ptr) { |
| 120 |
|
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
limit = ptr - 8192; |
| 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 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 159 |
|
| 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]; |
| 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 |
| 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 |
| 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 |
|