Debian Codegraph @

staprun/common.c

raw
1 /* -*- linux-c -*-
2 *
3 * common.c - staprun suid/user common code
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 *
10 * Copyright (C) 2007-2021 Red Hat Inc.
11 */
12
13 #include "staprun.h"
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <sys/utsname.h>
17 #include <assert.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include "../git_version.h"
24 #include "../version.h"
25
26 /* variables needed by parse_args() */
27 int verbose;
28 int suppress_warnings;
29 int target_pid;
30 int target_namespaces_pid;
31 int target_mnt_ns_fd = -1;
32 int orig_mnt_ns_fd = -1;
33 unsigned int buffer_size;
34 unsigned int reader_timeout_ms;
35 char *target_cmd;
36 char *outfile_name;
37 int rename_mod;
38 int attach_mod;
39 int delete_mod;
40 int load_only;
41 int need_uprobes;
42 const char *uprobes_path = NULL;
43 int daemon_mode;
44 off_t fsize_max;
45 int fnum_max;
46 int remote_id;
47 const char *remote_uri;
48 int read_stdin;
49 int relay_basedir_fd;
50 int color_errors;
51 color_modes color_mode;
52 int monitor;
53 int monitor_interval;
54
55 /* module variables */
56 char *modname = NULL;
57 char *modpath = "";
58 char *modoptions[MAXMODOPTIONS];
59
60 int control_channel = -1; /* NB: fd==0 possible */
61
62 static char path_buf[PATH_MAX];
63 static char *get_abspath(char *path)
64 {
65 int len;
66 if (path[0] == '/')
67 return path;
68
69 len = strlen(getcwd(path_buf, PATH_MAX));
70 if (len + 2 + strlen(path) >= PATH_MAX)
71 return NULL;
72 path_buf[len] = '/';
73 /* Note that this strcpy() call is OK, since we checked
74 * the length earlier to make sure the string would fit. */
75 strcpy(&path_buf[len + 1], path);
76 return path_buf;
77 }
78
79 int make_outfile_name(char *buf, int max, int fnum, int cpu, time_t t, int bulk)
80 {
81 int len;
82 if (PATH_MAX < max)
83 max = PATH_MAX;
84 len = stap_strfloctime(buf, max, outfile_name, t);
85 if (len < 0) {
86 err(_("Invalid FILE name format\n"));
87 return -1;
88 }
89 /* special case: for testing we sometimes want to write to /dev/null */
90 if (strcmp(outfile_name, "/dev/null") == 0) {
91 /* This strcpy() call is OK since we know that the
92 * buffer is at least PATH_MAX bytes long at this
93 * point. */
94 strcpy(buf, "/dev/null");
95 } else {
96 if (bulk) {
97 if (snprintf_chk(&buf[len], max - len, "_cpu%d.%d",
98 cpu, fnum))
99 return -1;
100 } else {
101 /* stream mode */
102 if (snprintf_chk(&buf[len], max - len, ".%d", fnum))
103 return -1;
104 }
105 }
106 return 0;
107 }
108
109 void parse_args(int argc, char **argv)
110 {
111 int c;
112 char *s;
113
114 /* Initialize option variables. */
115 verbose = 0;
116 suppress_warnings = 0;
117 target_pid = 0;
118 target_namespaces_pid = 0;
119 buffer_size = 0;
120 reader_timeout_ms = 200;
121 target_cmd = NULL;
122 outfile_name = NULL;
123 rename_mod = 0;
124 attach_mod = 0;
125 delete_mod = 0;
126 read_stdin = 0;
127 load_only = 0;
128 need_uprobes = 0;
129 daemon_mode = 0;
130 fsize_max = 0;
131 fnum_max = 0;
132 monitor = 0;
133 monitor_interval = 1;
134 remote_id = -1;
135 remote_uri = NULL;
136 relay_basedir_fd = -1;
137 color_mode = color_auto;
138 color_errors = isatty(STDERR_FILENO)
139 && strcmp(getenv("TERM") ?: "notdumb", "dumb");
140
141 while ((c = getopt(argc, argv, "ALu::vihb:t:dc:o:x:N:S:DwRr:VT:C:M:"
142 #ifdef HAVE_OPENAT
143 "F:"
144 #endif
145 )) != EOF) {
146 switch (c) {
147 case 'u':
148 need_uprobes = 1;
149 if (optarg)
150 uprobes_path = strdup (optarg);
151 break;
152 case 'v':
153 verbose++;
154 break;
155 case 'w':
156 suppress_warnings=1;
157 break;
158 case 'b':
159 buffer_size = (unsigned)atoi(optarg);
160 if (buffer_size < 1 || buffer_size > 4095) {
161 err(_("Invalid buffer size '%d' (should be 1-4095).\n"), buffer_size);
162 usage(argv[0],1);
163 }
164 break;
165 case 't':
166 case 'x':
167 target_pid = atoi(optarg);
168 break;
169 case 'N':
170 target_namespaces_pid = atoi(optarg);
171 // error out if it's obviously and invalid pid
172 if (target_namespaces_pid < 1) {
173 err(_("Invalid target namespaces pid %d (should be > 0).\n"), target_namespaces_pid);
174 usage(argv[0],1);
175 }
176 break;
177 case 'd':
178 /* delete module */
179 delete_mod = 1;
180 break;
181 case 'c':
182 target_cmd = optarg;
183 break;
184 case 'o':
185 outfile_name = optarg;
186 break;
187 case 'i':
188 read_stdin = 1;
189 break;
190 case 'R':
191 rename_mod = 1;
192 break;
193 case 'A':
194 attach_mod = 1;
195 break;
196 case 'L':
197 load_only = 1;
198 break;
199 case 'D':
200 daemon_mode = 1;
201 break;
202 case 'F':
203 relay_basedir_fd = atoi(optarg);
204 if (relay_basedir_fd < 0) {
205 err(_("Invalid file descriptor option '%s'.\n"), optarg);
206 usage(argv[0],1);
207 }
208 break;
209 case 'S':
210 assert(optarg != 0); // optarg can't be NULL (or getopt would choke)
211 fsize_max = strtoul(optarg, &s, 10);
212 fsize_max <<= 20;
213 if (s[0] == ',')
214 fnum_max = (int)strtoul(&s[1], &s, 10);
215 if (s[0] != '\0') {
216 err(_("Invalid file size option '%s'.\n"), optarg);
217 usage(argv[0],1);
218 }
219 break;
220 case 'r':
221 assert(optarg != 0); // optarg can't be NULL (or getopt would choke)
222 /* parse ID:URL */
223 remote_id = strtoul(optarg, &s, 10);
224 if (s[0] == ':')
225 remote_uri = strdup (& s[1]);
226
227 if (remote_id < 0 || remote_uri == 0 || remote_uri[0] == '\0') {
228 err(_("Cannot process remote id option '%s'.\n"), optarg);
229 usage(argv[0],1);
230 }
231 break;
232 case 'V':
233 /* PRERELEASE */
234 printf(_("Systemtap module loader/runner (version %s, %s)\n"
235 "Copyright (C) 2005-2025 Red Hat, Inc. and others\n"
236 "This is free software; see the source for copying conditions.\n"),
237 VERSION, STAP_EXTENDED_VERSION);
238 _exit(0);
239 break;
240 case 'h':
241 usage(argv[0],0);
242 break;
243 case 'T':
244 reader_timeout_ms = (unsigned)atoi(optarg);
245 if (reader_timeout_ms < 1) {
246 err(_("Invalid reader timeout value '%d' (should be >= 1).\n"), reader_timeout_ms);
247 usage(argv[0],1);
248 }
249 break;
250 case 'C':
251 assert(optarg != 0); // optarg can't be NULL (or getopt would choke)
252 if (!strcmp(optarg, "never"))
253 color_mode = color_never;
254 else if (!strcmp(optarg, "auto"))
255 color_mode = color_auto;
256 else if (!strcmp(optarg, "always"))
257 color_mode = color_always;
258 else {
259 err(_("Invalid option '%s' for -C."), optarg);
260 usage(argv[0],1);
261 }
262 color_errors = color_mode == color_always
263 || (color_mode == color_auto && isatty(STDERR_FILENO)
264 && strcmp(getenv("TERM") ?: "notdumb", "dumb"));
265 break;
266 case 'M':
267 monitor = 1;
268 monitor_interval = atoi(optarg);
269 if (monitor_interval < 1) {
270 err(_("Invalid monitor interval\n"));
271 }
272 break;
273 default:
274 usage(argv[0],1);
275 }
276 }
277 if (outfile_name) {
278 char tmp[PATH_MAX];
279 int ret;
280 outfile_name = get_abspath(outfile_name);
281 if (outfile_name == NULL) {
282 err(_("File name is too long.\n"));
283 usage(argv[0],1);
284 }
285 ret = stap_strfloctime(tmp, PATH_MAX - 21,
286 /* = _cpuNNNNNN.SSSSSSSSSS */
287 outfile_name, time(NULL));
288 if (ret < 0) {
289 err(_("Filename format is invalid or too long.\n"));
290 usage(argv[0],1);
291 }
292 }
293 if (attach_mod && load_only) {
294 err(_("You can't specify the '-A' and '-L' options together.\n"));
295 usage(argv[0],1);
296 }
297
298 if (attach_mod && buffer_size) {
299 err(_("You can't specify the '-A' and '-b' options together. The '-b'\n"
300 "buffer size option only has an effect when the module is inserted.\n"));
301 usage(argv[0],1);
302 }
303
304 if (attach_mod && target_cmd) {
305 err(_("You can't specify the '-A' and '-c' options together. The '-c cmd'\n"
306 "option used to start a command only has an effect when the module\n"
307 "is inserted.\n"));
308 usage(argv[0],1);
309 }
310
311 if (attach_mod && target_pid) {
312 err(_("You can't specify the '-A' and '-x' options together. The '-x pid'\n"
313 "option only has an effect when the module is inserted.\n"));
314 usage(argv[0],1);
315 }
316
317 if (target_cmd && target_pid) {
318 err(_("You can't specify the '-c' and '-x' options together.\n"));
319 usage(argv[0],1);
320 }
321
322 if (daemon_mode && load_only) {
323 err(_("You can't specify the '-D' and '-L' options together.\n"));
324 usage(argv[0],1);
325 }
326 if (daemon_mode && delete_mod) {
327 err(_("You can't specify the '-D' and '-d' options together.\n"));
328 usage(argv[0],1);
329 }
330 if (daemon_mode && target_cmd) {
331 err(_("You can't specify the '-D' and '-c' options together.\n"));
332 usage(argv[0],1);
333 }
334 if (daemon_mode && outfile_name == NULL) {
335 err(_("You have to specify output FILE with '-D' option.\n"));
336 usage(argv[0],1);
337 }
338 if (outfile_name == NULL && fsize_max != 0) {
339 err(_("You have to specify output FILE with '-S' option.\n"));
340 usage(argv[0],1);
341 }
342 }
343
344 void usage(char *prog, int rc)
345 {
346 printf(_("\n%s [-v] [-w] [-V] [-h] [-u] [-c cmd ] [-x pid] [-u user] [-A|-L|-d] [-C WHEN]\n"
347 "\t[-b bufsize] [-R] [-r N:URI] [-o FILE [-D] [-S size[,N]]] MODULE [module-options]\n"), prog);
348 printf(_("-v Increase verbosity.\n"
349 "-V Print version number and exit.\n"
350 "-h Print this help text and exit.\n"
351 "-w Suppress warnings.\n"
352 "-u Load uprobes.ko\n"
353 "-c cmd Command \'cmd\' will be run and staprun will\n"
354 " exit when it does. The '_stp_target' variable\n"
355 " will contain the pid for the command.\n"
356 "-x pid Sets the '_stp_target' variable to pid.\n"
357 "-N pid Sets the '_stp_namespaces_pid' variable to pid.\n"
358 "-o FILE Send output to FILE. This supports strftime(3)\n"
359 " formats for FILE.\n"
360 "-b buffer size The systemtap module specifies a buffer size.\n"
361 " Setting one here will override that value. The\n"
362 " value should be an integer between 1 and 4095 \n"
363 " which be assumed to be the buffer size in MB.\n"
364 " That value will be per-cpu in bulk mode.\n"
365 "-L Load module and start probes, then detach.\n"
366 "-A Attach to loaded systemtap module.\n"
367 "-C WHEN Enable colored errors. WHEN must be either 'auto',\n"
368 " 'never', or 'always'. Set to 'auto' by default.\n"
369 #ifdef HAVE_MONITOR_LIBS
370 "-M INTERVAL Enable monitor mode.\n"
371 #endif
372 "-d Delete a module. Only detached or unused modules\n"
373 " the user has permission to access will be deleted. Use \"*\"\n"
374 " (quoted) to delete all unused modules.\n"
375 "-R Have staprun create a new name for the module before\n"
376 " inserting it. This allows the same module to be inserted\n"
377 " more than once.\n"
378 "-r N:URI Pass N:URI data to tapset functions remote_id()/remote_uri().\n"
379 "-D Run in background. This requires '-o' option.\n"
380 "-S size[,N] Switches output file to next file when the size\n"
381 " of file reaches the specified size. The value\n"
382 " should be an integer greater than 1 which is\n"
383 " assumed to be the maximum file size in MB.\n"
384 " When the number of output files reaches N, it\n"
385 " switches to the first output file. You can omit\n"
386 " the second argument.\n"
387 "-T timeout Specifies upper limit on amount of time reader thread\n"
388 " will wait for new full trace buffer. Value should be an\n"
389 " integer >= 1, which is timeout value in ms. Default 200ms.\n"
390 #ifdef HAVE_OPENAT
391 "-F fd Specifies file descriptor for module relay directory\n"
392 #endif
393 "\n"
394 "MODULE can be either a module name or a module path. If a\n"
395 "module name is used, it is searched in the following directory:\n"));
396 {
397 struct utsname utsbuf;
398 int rc = uname (& utsbuf);
399 if (! rc)
400 printf("/lib/modules/%s/systemtap\n", utsbuf.release);
401 else
402 printf("/lib/modules/`uname -r`/systemtap\n");
403 }
404 exit(rc);
405 }
406
407 /*
408 * parse_modpath. Here's how this code interprets the global modpath:
409 *
410 * (1) If modpath contains a '/', it is assumed to be an absolute or
411 * relative file path to a module (such as "../foo.ko" or
412 * "/tmp/stapXYZ/stap_foo.ko").
413 *
414 * (2) If modpath doesn't contain a '/' and ends in '.ko', it is a file
415 * path to a module in the current directory (such as "foo.ko").
416 *
417 * (3) If modpath doesn't contain a '/' and doesn't end in '.ko', then
418 * it is a module name and the full pathname of the module is
419 * '/lib/modules/`uname -r`/systemtap/PATH.ko'. For instance, if
420 * modpath was "foo", the full module pathname would be
421 * '/lib/modules/`uname -r`/systemtap/foo.ko'.
422 */
423 void parse_modpath(const char *inpath)
424 {
425 const char *mptr = strrchr(inpath, '/');
426 char *ptr;
427
428 dbug(3, "inpath=%s\n", inpath);
429
430 /* If we couldn't find a '/', ... */
431 if (mptr == NULL) {
432 size_t plen = strlen(inpath);
433
434 /* If the path ends with the '.ko' file extension,
435 * then we've got a module in the current
436 * directory. */
437 if (plen > 3 && strcmp(&inpath[plen - 3], ".ko") == 0) {
438 mptr = inpath;
439 modpath = strdup(inpath);
440 if (!modpath) {
441 err(_("Memory allocation failed. Exiting.\n"));
442 exit(1);
443 }
444 } else {
445 /* If we didn't find the '.ko' file extension, then
446 * we've just got a module name, not a module path.
447 * Look for the module in /lib/modules/`uname
448 * -r`/systemtap. */
449
450 struct utsname utsbuf;
451 int len;
452 #define MODULE_PATH "/lib/modules/%s/systemtap/%s.ko"
453
454 /* First, we need to figure out what the
455 * kernel version. */
456 if (uname(&utsbuf) != 0) {
457 perr(_("Unable to determine kernel version, uname failed"));
458 exit(-1);
459 }
460
461 /* Build the module path, which will look like
462 * '/lib/modules/KVER/systemtap/{path}.ko'. */
463 len = sizeof(MODULE_PATH) + sizeof(utsbuf.release) + strlen(inpath);
464 modpath = malloc(len);
465 if (!modpath) {
466 err(_("Memory allocation failed. Exiting.\n"));
467 exit(1);
468 }
469
470 if (snprintf_chk(modpath, len, MODULE_PATH, utsbuf.release, inpath))
471 exit(-1);
472
473 dbug(2, "modpath=\"%s\"\n", modpath);
474
475 mptr = strrchr(modpath, '/');
476 mptr++;
477 }
478 } else {
479 /* We found a '/', so the module name starts with the next
480 * character. */
481 mptr++;
482
483 modpath = strdup(inpath);
484 if (!modpath) {
485 err(_("Memory allocation failed. Exiting.\n"));
486 exit(1);
487 }
488 }
489
490 modname = strdup(mptr);
491 if (!modname) {
492 err(_("Memory allocation failed. Exiting.\n"));
493 exit(1);
494 }
495
496 ptr = strrchr(modname, '.');
497 if (ptr)
498 *ptr = '\0';
499
500 /* We've finally got a real modname. Make sure it isn't too
501 * long. If it is too long, init_module() will appear to
502 * work, but the module can't be removed (because you end up
503 * with control characters in the module name). */
504 if (strlen(modname) > MODULE_NAME_LEN) {
505 err(_("Module name ('%s') is too long.\n"), modname);
506 exit(1);
507 }
508 }
509
510 #define ERR_MSG "\nUNEXPECTED FATAL ERROR in staprun. Please file a bug report.\n"
511 static void fatal_handler (int signum)
512 {
513 int rc;
514 char *str = strsignal(signum);
515 rc = write (STDERR_FILENO, ERR_MSG, sizeof(ERR_MSG));
516 fprintf(stderr, "%s\n", str);
517 (void) rc; /* notused */
518 _exit(1);
519 }
520
521 void setup_signals(void)
522 {
523 sigset_t s;
524 struct sigaction a;
525
526 /* blocking all signals while we set things up */
527 sigfillset(&s);
528 #ifdef SINGLE_THREADED
529 sigprocmask(SIG_SETMASK, &s, NULL);
530 #else
531 pthread_sigmask(SIG_SETMASK, &s, NULL);
532 #endif
533 /* set some of them to be ignored */
534 memset(&a, 0, sizeof(a));
535 sigfillset(&a.sa_mask);
536 a.sa_handler = SIG_IGN;
537 sigaction(SIGPIPE, &a, NULL);
538 sigaction(SIGUSR2, &a, NULL);
539
540 /* for serious errors, handle them in fatal_handler */
541 a.sa_handler = fatal_handler;
542 sigaction(SIGBUS, &a, NULL);
543 sigaction(SIGFPE, &a, NULL);
544 sigaction(SIGILL, &a, NULL);
545 sigaction(SIGSEGV, &a, NULL);
546 sigaction(SIGXCPU, &a, NULL);
547 sigaction(SIGXFSZ, &a, NULL);
548
549 /* unblock all signals */
550 sigemptyset(&s);
551
552 #ifdef SINGLE_THREADED
553 sigprocmask(SIG_SETMASK, &s, NULL);
554 #else
555 pthread_sigmask(SIG_SETMASK, &s, NULL);
556 #endif
557 }
558
559 /*
560 * set FD_CLOEXEC for any file descriptor
561 */
562 int set_clexec(int fd)
563 {
564 int val;
565 if ((val = fcntl(fd, F_GETFD, 0)) < 0)
566 goto err;
567
568 if ((val = fcntl(fd, F_SETFD, val | FD_CLOEXEC)) < 0)
569 goto err;
570
571 return 0;
572 err:
573 perr("fcntl failed");
574 close(fd);
575 return -1;
576 }
577
578 /*
579 * In multithreaded programs, using fcntl() to set F_CLOEXEC
580 * (close-on-exec) on a file descriptor is subject to a race condition
581 * that another thread may call fork() and exec() between the time the
582 * file is opened and fcntl() is called.
583 *
584 * So, when possible (since Linux 2.6.23), we use the O_CLOEXEC flag
585 * when calling open() and openat() to avoid this race condition. When
586 * O_CLOEXEC isn't available, the best we can do is call fcntl()
587 * immediately after open() is called.
588 *
589 * The same logic applies to openat() and pipe().
590 */
591
592 int open_cloexec(const char *pathname, int flags, mode_t mode)
593 {
594 #ifdef O_CLOEXEC
595 return open(pathname, flags | O_CLOEXEC, mode);
596 #else
597 int fd = open(pathname, flags, mode);
598 if (fd >= 0) {
599 if (set_clexec(fd) < 0) {
600 close(fd);
601 fd = -1;
602 }
603 }
604 return fd;
605 #endif
606 }
607
608 #ifdef HAVE_OPENAT
609 int openat_cloexec(int dirfd, const char *pathname, int flags, mode_t mode)
610 {
611 #ifdef O_CLOEXEC
612 return openat(dirfd, pathname, flags | O_CLOEXEC, mode);
613 #else
614 int fd = openat(dirfd, pathname, flags, mode);
615 if (fd >= 0) {
616 if (set_clexec(fd) < 0) {
617 close(fd);
618 fd = -1;
619 }
620 }
621 return fd;
622 #endif
623 }
624 #endif
625
626 int pipe_cloexec(int pipefd[2])
627 {
628 #ifdef O_CLOEXEC
629 return pipe2(pipefd, O_CLOEXEC);
630 #else
631 int rc = pipe(pipefd);
632 if (rc == 0) {
633 if (set_clexec(pipefd[0]) < 0 || set_clexec(pipefd[1] < 0)) {
634 goto err;
635 }
636 }
637 return rc;
638
639 err:
640 close(pipefd[0]);
641 close(pipefd[1]);
642 pipefd[0] = -1;
643 pipefd[1] = -1;
644 return -1;
645 #endif
646 }
647
648 /**
649 * send_request - send request to kernel over control channel
650 * @type: the relay-app command id
651 * @data: pointer to the data to be sent
652 * @len: length of the data to be sent
653 *
654 * Returns 0 on success, non-zero otherwise.
655 */
656 int send_request(int type, void *data, int len)
657 {
658 char buf[1024];
659 int rc = 0;
660
661 PROBE3(stapio, send__ctlmsg, type, data, len);
662 /* Before doing memcpy, make sure 'buf' is big enough. */
663 if ((len + sizeof(type)) > (int)sizeof(buf)) {
664 _err(_("exceeded maximum send_request size.\n"));
665 return -1;
666 }
667 memcpy(buf, &type, sizeof (type));
668 memcpy(&buf[sizeof (type)], data, len);
669
670 errno = 0;
671 assert (control_channel >= 0);
672 rc = write (control_channel, buf, len + sizeof (type));
673 if (rc < 0) return rc;
674 /* A bug in the transport layer of older modules causes them to return sizeof (type) fewer
675 bytes written than actual. This is fixed in newer modules. So accept both. */
676 return (rc != len && rc != len + (int)sizeof (type));
677 }
678
679 #include <stdarg.h>
680
681 static int use_syslog = 0;
682
683 __attribute__ ((format (printf, 1, 2)))
684 void eprintf(const char *fmt, ...)
685 {
686 va_list va;
687 va_start(va, fmt);
688 if (use_syslog)
689 vsyslog(LOG_ERR, fmt, va);
690 else
691 vfprintf(stderr, fmt, va);
692 va_end(va);
693 }
694
695 void switch_syslog(const char *name)
696 {
697 openlog(name, LOG_PID, LOG_DAEMON);
698 use_syslog = 1;
699 color_errors = 0;
700 }
701
702 /* Parse SYSTEMTAP_COLORS and returns the SGR parameter(s) for the given
703 type. The env var SYSTEMTAP_COLORS must be in the following format:
704 'key1=val1:key2=val2:' etc... where valid keys are 'error', 'warning',
705 'source', 'caret', 'token' and valid values constitute SGR parameter(s).
706 For example, the default setting would be:
707 'error=01;31:warning=00;33:source=00;34:caret=01:token=01'
708 */
709 char *parse_stap_color(const char *type)
710 {
711 const char *key, *col, *eq;
712 int n = strlen(type);
713 int done = 0;
714
715 key = getenv("SYSTEMTAP_COLORS");
716 if (key == NULL)
717 key = "error=01;31:warning=00;33:source=00;34:caret=01:token=01";
718 else if (*key == '\0')
719 return NULL; // disable colors if set but empty
720
721 while (!done) {
722 if (!(col = strchr(key, ':'))) {
723 col = strchr(key, '\0');
724 done = 1;
725 }
726 if (!((eq = strchr(key, '=')) && eq < col))
727 return NULL; /* invalid syntax: no = in range */
728 if (!(key < eq && eq < col-1))
729 return NULL; /* invalid syntax: key or val empty */
730 if (strspn(eq+1, "0123456789;") < (size_t)(col-eq-1))
731 return NULL; /* invalid syntax: invalid char in val */
732 if (eq-key == n && !strncmp(key, type, n))
733 return strndup(eq+1, col-eq-1);
734 if (!done) key = col+1; /* advance to next key */
735 }
736
737 return NULL; /* key not found */
738 }