2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 typedef unsigned int number_slot_t;
33 #define BITS_PER_SLOT (sizeof(number_slot_t) * 8)
41 struct number_set read_set;
42 struct number_set write_set;
43 struct number_set signal_set;
45 static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
46 static struct number_set inject_set[SUPPORTED_PERSONALITIES];
47 static struct number_set raw_set[SUPPORTED_PERSONALITIES];
48 static struct number_set trace_set[SUPPORTED_PERSONALITIES];
49 static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
52 number_setbit(const unsigned int i, number_slot_t *const vec)
54 vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT);
58 number_isset(const unsigned int i, const number_slot_t *const vec)
60 return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT));
64 reallocate_number_set(struct number_set *const set, const unsigned int new_nslots)
66 if (new_nslots <= set->nslots)
68 set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec));
69 memset(set->vec + set->nslots, 0,
70 sizeof(*set->vec) * (new_nslots - set->nslots));
71 set->nslots = new_nslots;
75 add_number_to_set(const unsigned int number, struct number_set *const set)
77 reallocate_number_set(set, number / BITS_PER_SLOT + 1);
78 number_setbit(number, set->vec);
82 is_number_in_set(const unsigned int number, const struct number_set *const set)
84 return ((number / BITS_PER_SLOT < set->nslots)
85 && number_isset(number, set->vec)) ^ set->not;
88 typedef int (*string_to_uint_func)(const char *);
91 * Add numbers to SET according to STR specification.
94 qualify_tokens(const char *const str, struct number_set *const set,
95 string_to_uint_func func, const char *const name)
99 memset(set->vec, 0, sizeof(*set->vec) * set->nslots);
103 * Each leading ! character means inversion
104 * of the remaining specification.
109 set->not = !set->not;
113 if (strcmp(s, "none") == 0) {
115 * No numbers are added to the set.
116 * Subsequent is_number_in_set invocations will return set->not.
119 } else if (strcmp(s, "all") == 0) {
121 goto handle_inversion;
125 * Split the string into comma separated tokens.
126 * For each token, find out the corresponding number
127 * by calling FUNC, and add that number to the set.
128 * The absence of tokens or a negative answer
129 * from FUNC is a fatal error.
131 char *copy = xstrdup(s);
132 char *saveptr = NULL;
136 for (token = strtok_r(copy, ",", &saveptr); token;
137 token = strtok_r(NULL, ",", &saveptr)) {
138 number = func(token);
140 error_msg_and_die("invalid %s '%s'", name, token);
143 add_number_to_set(number, set);
149 error_msg_and_die("invalid %s '%s'", name, str);
154 sigstr_to_uint(const char *s)
158 if (*s >= '0' && *s <= '9')
159 return string_to_uint_upto(s, 255);
161 if (strncasecmp(s, "SIG", 3) == 0)
164 for (i = 0; i <= 255; ++i) {
165 const char *name = signame(i);
167 if (strncasecmp(name, "SIG", 3) != 0)
172 if (strcasecmp(name, s) != 0)
182 qualify_syscall_number(const char *s, struct number_set *set)
184 int n = string_to_uint(s);
191 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
192 if ((unsigned) n >= nsyscall_vec[p]) {
195 add_number_to_set(n, &set[p]);
203 regerror_msg_and_die(int errcode, const regex_t *preg,
204 const char *str, const char *pattern)
208 regerror(errcode, preg, buf, sizeof(buf));
209 error_msg_and_die("%s: %s: %s", str, pattern, buf);
213 qualify_syscall_regex(const char *s, struct number_set *set)
218 if ((rc = regcomp(&preg, s, REG_EXTENDED | REG_NOSUB)) != 0)
219 regerror_msg_and_die(rc, &preg, "regcomp", s);
223 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
226 for (i = 0; i < nsyscall_vec[p]; ++i) {
227 if (!sysent_vec[p][i].sys_name)
229 rc = regexec(&preg, sysent_vec[p][i].sys_name,
231 if (rc == REG_NOMATCH)
234 regerror_msg_and_die(rc, &preg, "regexec", s);
235 add_number_to_set(i, &set[p]);
245 lookup_class(const char *s)
247 static const struct {
250 } syscall_class[] = {
251 { "desc", TRACE_DESC },
252 { "file", TRACE_FILE },
253 { "memory", TRACE_MEMORY },
254 { "process", TRACE_PROCESS },
255 { "signal", TRACE_SIGNAL },
256 { "ipc", TRACE_IPC },
257 { "network", TRACE_NETWORK },
258 { "%desc", TRACE_DESC },
259 { "%file", TRACE_FILE },
260 { "%memory", TRACE_MEMORY },
261 { "%process", TRACE_PROCESS },
262 { "%signal", TRACE_SIGNAL },
263 { "%ipc", TRACE_IPC },
264 { "%network", TRACE_NETWORK },
265 { "%statfs", TRACE_STATFS },
266 { "%fstatfs", TRACE_FSTATFS },
270 for (i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
271 if (strcmp(s, syscall_class[i].name) == 0) {
272 return syscall_class[i].value;
280 qualify_syscall_class(const char *s, struct number_set *set)
282 const unsigned int n = lookup_class(s);
287 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
290 for (i = 0; i < nsyscall_vec[p]; ++i) {
291 if (!sysent_vec[p][i].sys_name
292 || (sysent_vec[p][i].sys_flags & n) != n) {
295 add_number_to_set(i, &set[p]);
303 qualify_syscall_name(const char *s, struct number_set *set)
308 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
311 for (i = 0; i < nsyscall_vec[p]; ++i) {
312 if (!sysent_vec[p][i].sys_name
313 || strcmp(s, sysent_vec[p][i].sys_name)) {
316 add_number_to_set(i, &set[p]);
325 qualify_syscall(const char *token, struct number_set *set)
327 if (*token >= '0' && *token <= '9')
328 return qualify_syscall_number(token, set);
330 return qualify_syscall_regex(token + 1, set);
331 return qualify_syscall_class(token, set)
332 || qualify_syscall_name(token, set);
336 * Add syscall numbers to SETs for each supported personality
337 * according to STR specification.
340 qualify_syscall_tokens(const char *const str, struct number_set *const set,
341 const char *const name)
343 /* Clear all sets. */
345 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
347 memset(set[p].vec, 0,
348 sizeof(*set[p].vec) * set[p].nslots);
353 * Each leading ! character means inversion
354 * of the remaining specification.
359 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
360 set[p].not = !set[p].not;
365 if (strcmp(s, "none") == 0) {
367 * No syscall numbers are added to sets.
368 * Subsequent is_number_in_set invocations
369 * will return set[p]->not.
372 } else if (strcmp(s, "all") == 0) {
374 goto handle_inversion;
378 * Split the string into comma separated tokens.
379 * For each token, call qualify_syscall that will take care
380 * if adding appropriate syscall numbers to sets.
381 * The absence of tokens or a negative return code
382 * from qualify_syscall is a fatal error.
384 char *copy = xstrdup(s);
385 char *saveptr = NULL;
389 for (token = strtok_r(copy, ",", &saveptr); token;
390 token = strtok_r(NULL, ",", &saveptr)) {
391 done = qualify_syscall(token, set);
393 error_msg_and_die("invalid %s '%s'", name, token);
400 error_msg_and_die("invalid %s '%s'", name, str);
405 * Returns NULL if STR does not start with PREFIX,
406 * or a pointer to the first char in STR after PREFIX.
409 strip_prefix(const char *prefix, const char *str)
411 size_t len = strlen(prefix);
413 return strncmp(prefix, str, len) ? NULL : str + len;
417 find_errno_by_name(const char *name)
421 for (i = 1; i < nerrnos; ++i) {
422 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
430 parse_inject_token(const char *const token, struct inject_opts *const fopts,
431 const bool fault_tokens_only)
436 if ((val = strip_prefix("when=", token))) {
444 intval = string_to_uint_ex(val, &end, 0xffff, "+");
448 fopts->first = intval;
454 intval = string_to_uint_upto(val, 0xffff);
457 fopts->step = intval;
466 } else if ((val = strip_prefix("error=", token))) {
467 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
469 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
471 intval = find_errno_by_name(val);
474 fopts->rval = -intval;
475 } else if (!fault_tokens_only && (val = strip_prefix("retval=", token))) {
476 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
478 intval = string_to_uint(val);
481 fopts->rval = intval;
482 } else if (!fault_tokens_only && (val = strip_prefix("signal=", token))) {
483 intval = sigstr_to_uint(val);
484 if (intval < 1 || intval > NSIG_BYTES * 8)
486 fopts->signo = intval;
495 parse_inject_expression(const char *const s, char **buf,
496 struct inject_opts *const fopts,
497 const bool fault_tokens_only)
499 char *saveptr = NULL;
504 for (token = strtok_r(*buf, ":", &saveptr); token;
505 token = strtok_r(NULL, ":", &saveptr)) {
508 else if (!parse_inject_token(token, fopts, fault_tokens_only))
521 qualify_read(const char *const str)
523 qualify_tokens(str, &read_set, string_to_uint, "descriptor");
527 qualify_write(const char *const str)
529 qualify_tokens(str, &write_set, string_to_uint, "descriptor");
533 qualify_signals(const char *const str)
535 qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
539 qualify_trace(const char *const str)
541 qualify_syscall_tokens(str, trace_set, "system call");
545 qualify_abbrev(const char *const str)
547 qualify_syscall_tokens(str, abbrev_set, "system call");
551 qualify_verbose(const char *const str)
553 qualify_syscall_tokens(str, verbose_set, "system call");
557 qualify_raw(const char *const str)
559 qualify_syscall_tokens(str, raw_set, "system call");
563 qualify_inject_common(const char *const str,
564 const bool fault_tokens_only,
565 const char *const description)
567 struct inject_opts opts = {
570 .rval = INJECT_OPTS_RVAL_DEFAULT,
574 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
576 error_msg_and_die("invalid %s '%s'", description, str);
579 /* If neither of retval, error, or signal is specified, then ... */
580 if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
581 if (fault_tokens_only) {
582 /* in fault= syntax the default error code is ENOSYS. */
585 /* in inject= syntax this is not allowed. */
586 error_msg_and_die("invalid %s '%s'", description, str);
590 struct number_set tmp_set[SUPPORTED_PERSONALITIES];
591 memset(tmp_set, 0, sizeof(tmp_set));
592 qualify_syscall_tokens(name, tmp_set, description);
597 * Initialize inject_vec accourding to tmp_set.
598 * Merge tmp_set into inject_set.
601 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
602 if (!tmp_set[p].nslots && !tmp_set[p].not) {
606 if (!inject_vec[p]) {
607 inject_vec[p] = xcalloc(nsyscall_vec[p],
608 sizeof(*inject_vec[p]));
612 for (i = 0; i < nsyscall_vec[p]; ++i) {
613 if (is_number_in_set(i, &tmp_set[p])) {
614 add_number_to_set(i, &inject_set[p]);
615 inject_vec[p][i] = opts;
619 free(tmp_set[p].vec);
624 qualify_fault(const char *const str)
626 qualify_inject_common(str, true, "fault argument");
630 qualify_inject(const char *const str)
632 qualify_inject_common(str, false, "inject argument");
635 static const struct qual_options {
637 void (*qualify)(const char *);
639 { "trace", qualify_trace },
640 { "t", qualify_trace },
641 { "abbrev", qualify_abbrev },
642 { "a", qualify_abbrev },
643 { "verbose", qualify_verbose },
644 { "v", qualify_verbose },
645 { "raw", qualify_raw },
646 { "x", qualify_raw },
647 { "signal", qualify_signals },
648 { "signals", qualify_signals },
649 { "s", qualify_signals },
650 { "read", qualify_read },
651 { "reads", qualify_read },
652 { "r", qualify_read },
653 { "write", qualify_write },
654 { "writes", qualify_write },
655 { "w", qualify_write },
656 { "fault", qualify_fault },
657 { "inject", qualify_inject },
661 qualify(const char *str)
663 const struct qual_options *opt = qual_options;
666 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
667 const char *p = qual_options[i].name;
668 unsigned int len = strlen(p);
670 if (strncmp(str, p, len) || str[len] != '=')
673 opt = &qual_options[i];
682 qual_flags(const unsigned int scno)
684 return (is_number_in_set(scno, &trace_set[current_personality])
686 | (is_number_in_set(scno, &abbrev_set[current_personality])
688 | (is_number_in_set(scno, &verbose_set[current_personality])
690 | (is_number_in_set(scno, &raw_set[current_personality])
692 | (is_number_in_set(scno, &inject_set[current_personality])