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.
31 typedef unsigned int number_slot_t;
32 #define BITS_PER_SLOT (sizeof(number_slot_t) * 8)
40 struct number_set read_set;
41 struct number_set write_set;
42 struct number_set signal_set;
44 static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
45 static struct number_set inject_set[SUPPORTED_PERSONALITIES];
46 static struct number_set raw_set[SUPPORTED_PERSONALITIES];
47 static struct number_set trace_set[SUPPORTED_PERSONALITIES];
48 static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
51 number_setbit(const unsigned int i, number_slot_t *const vec)
53 vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT);
57 number_isset(const unsigned int i, const number_slot_t *const vec)
59 return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT));
63 reallocate_number_set(struct number_set *const set, const unsigned int new_nslots)
65 if (new_nslots <= set->nslots)
67 set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec));
68 memset(set->vec + set->nslots, 0,
69 sizeof(*set->vec) * (new_nslots - set->nslots));
70 set->nslots = new_nslots;
74 add_number_to_set(const unsigned int number, struct number_set *const set)
76 reallocate_number_set(set, number / BITS_PER_SLOT + 1);
77 number_setbit(number, set->vec);
81 is_number_in_set(const unsigned int number, const struct number_set *const set)
83 return ((number / BITS_PER_SLOT < set->nslots)
84 && number_isset(number, set->vec)) ^ set->not;
87 typedef int (*string_to_uint_func)(const char *);
90 * Add numbers to SET according to STR specification.
93 qualify_tokens(const char *const str, struct number_set *const set,
94 string_to_uint_func func, const char *const name)
98 memset(set->vec, 0, sizeof(*set->vec) * set->nslots);
102 * Each leading ! character means inversion
103 * of the remaining specification.
108 set->not = !set->not;
112 if (strcmp(s, "none") == 0) {
114 * No numbers are added to the set.
115 * Subsequent is_number_in_set invocations will return set->not.
118 } else if (strcmp(s, "all") == 0) {
120 goto handle_inversion;
124 * Split the string into comma separated tokens.
125 * For each token, find out the corresponding number
126 * by calling FUNC, and add that number to the set.
127 * The absence of tokens or a negative answer
128 * from FUNC is a fatal error.
130 char *copy = xstrdup(s);
131 char *saveptr = NULL;
135 for (token = strtok_r(copy, ",", &saveptr); token;
136 token = strtok_r(NULL, ",", &saveptr)) {
137 number = func(token);
139 error_msg_and_die("invalid %s '%s'", name, token);
142 add_number_to_set(number, set);
148 error_msg_and_die("invalid %s '%s'", name, str);
153 sigstr_to_uint(const char *s)
157 if (*s >= '0' && *s <= '9')
158 return string_to_uint_upto(s, 255);
160 if (strncasecmp(s, "SIG", 3) == 0)
163 for (i = 0; i <= 255; ++i) {
164 const char *name = signame(i);
166 if (strncasecmp(name, "SIG", 3) != 0)
171 if (strcasecmp(name, s) != 0)
181 qualify_syscall_number(const char *s, struct number_set *set)
183 int n = string_to_uint(s);
190 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
191 if ((unsigned) n >= nsyscall_vec[p]) {
194 add_number_to_set(n, &set[p]);
202 lookup_class(const char *s)
204 static const struct {
207 } syscall_class[] = {
208 { "desc", TRACE_DESC },
209 { "file", TRACE_FILE },
210 { "memory", TRACE_MEMORY },
211 { "process", TRACE_PROCESS },
212 { "signal", TRACE_SIGNAL },
213 { "ipc", TRACE_IPC },
214 { "network", TRACE_NETWORK },
215 { "%desc", TRACE_DESC },
216 { "%file", TRACE_FILE },
217 { "%memory", TRACE_MEMORY },
218 { "%process", TRACE_PROCESS },
219 { "%signal", TRACE_SIGNAL },
220 { "%ipc", TRACE_IPC },
221 { "%network", TRACE_NETWORK },
222 { "%sched", TRACE_SCHED },
223 { "%clock", TRACE_CLOCK },
227 for (i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
228 if (strcmp(s, syscall_class[i].name) == 0) {
229 return syscall_class[i].value;
237 qualify_syscall_class(const char *s, struct number_set *set)
239 const unsigned int n = lookup_class(s);
244 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
247 for (i = 0; i < nsyscall_vec[p]; ++i) {
248 if (!sysent_vec[p][i].sys_name
249 || (sysent_vec[p][i].sys_flags & n) != n) {
252 add_number_to_set(i, &set[p]);
260 qualify_syscall_name(const char *s, struct number_set *set)
265 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
268 for (i = 0; i < nsyscall_vec[p]; ++i) {
269 if (!sysent_vec[p][i].sys_name
270 || strcmp(s, sysent_vec[p][i].sys_name)) {
273 add_number_to_set(i, &set[p]);
282 qualify_syscall(const char *token, struct number_set *set)
284 if (*token >= '0' && *token <= '9')
285 return qualify_syscall_number(token, set);
286 return qualify_syscall_class(token, set)
287 || qualify_syscall_name(token, set);
291 * Add syscall numbers to SETs for each supported personality
292 * according to STR specification.
295 qualify_syscall_tokens(const char *const str, struct number_set *const set,
296 const char *const name)
298 /* Clear all sets. */
300 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
302 memset(set[p].vec, 0,
303 sizeof(*set[p].vec) * set[p].nslots);
308 * Each leading ! character means inversion
309 * of the remaining specification.
314 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
315 set[p].not = !set[p].not;
320 if (strcmp(s, "none") == 0) {
322 * No syscall numbers are added to sets.
323 * Subsequent is_number_in_set invocations
324 * will return set[p]->not.
327 } else if (strcmp(s, "all") == 0) {
329 goto handle_inversion;
333 * Split the string into comma separated tokens.
334 * For each token, call qualify_syscall that will take care
335 * if adding appropriate syscall numbers to sets.
336 * The absence of tokens or a negative return code
337 * from qualify_syscall is a fatal error.
339 char *copy = xstrdup(s);
340 char *saveptr = NULL;
344 for (token = strtok_r(copy, ",", &saveptr); token;
345 token = strtok_r(NULL, ",", &saveptr)) {
346 done = qualify_syscall(token, set);
348 error_msg_and_die("invalid %s '%s'", name, token);
355 error_msg_and_die("invalid %s '%s'", name, str);
360 * Returns NULL if STR does not start with PREFIX,
361 * or a pointer to the first char in STR after PREFIX.
364 strip_prefix(const char *prefix, const char *str)
366 size_t len = strlen(prefix);
368 return strncmp(prefix, str, len) ? NULL : str + len;
372 find_errno_by_name(const char *name)
376 for (i = 1; i < nerrnos; ++i) {
377 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
385 parse_inject_token(const char *const token, struct inject_opts *const fopts,
386 const bool fault_tokens_only)
391 if ((val = strip_prefix("when=", token))) {
399 intval = string_to_uint_ex(val, &end, 0xffff, "+");
403 fopts->first = intval;
409 intval = string_to_uint_upto(val, 0xffff);
412 fopts->step = intval;
421 } else if ((val = strip_prefix("error=", token))) {
422 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
424 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
426 intval = find_errno_by_name(val);
429 fopts->rval = -intval;
430 } else if (!fault_tokens_only && (val = strip_prefix("retval=", token))) {
431 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
433 intval = string_to_uint(val);
436 fopts->rval = intval;
437 } else if (!fault_tokens_only && (val = strip_prefix("signal=", token))) {
438 intval = sigstr_to_uint(val);
439 if (intval < 1 || intval > NSIG_BYTES * 8)
441 fopts->signo = intval;
450 parse_inject_expression(const char *const s, char **buf,
451 struct inject_opts *const fopts,
452 const bool fault_tokens_only)
454 char *saveptr = NULL;
459 for (token = strtok_r(*buf, ":", &saveptr); token;
460 token = strtok_r(NULL, ":", &saveptr)) {
463 else if (!parse_inject_token(token, fopts, fault_tokens_only))
476 qualify_read(const char *const str)
478 qualify_tokens(str, &read_set, string_to_uint, "descriptor");
482 qualify_write(const char *const str)
484 qualify_tokens(str, &write_set, string_to_uint, "descriptor");
488 qualify_signals(const char *const str)
490 qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
494 qualify_trace(const char *const str)
496 qualify_syscall_tokens(str, trace_set, "system call");
500 qualify_abbrev(const char *const str)
502 qualify_syscall_tokens(str, abbrev_set, "system call");
506 qualify_verbose(const char *const str)
508 qualify_syscall_tokens(str, verbose_set, "system call");
512 qualify_raw(const char *const str)
514 qualify_syscall_tokens(str, raw_set, "system call");
518 qualify_inject_common(const char *const str,
519 const bool fault_tokens_only,
520 const char *const description)
522 struct inject_opts opts = {
525 .rval = INJECT_OPTS_RVAL_DEFAULT,
529 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
531 error_msg_and_die("invalid %s '%s'", description, str);
534 /* If neither of retval, error, or signal is specified, then ... */
535 if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
536 if (fault_tokens_only) {
537 /* in fault= syntax the default error code is ENOSYS. */
540 /* in inject= syntax this is not allowed. */
541 error_msg_and_die("invalid %s '%s'", description, str);
545 struct number_set tmp_set[SUPPORTED_PERSONALITIES];
546 memset(tmp_set, 0, sizeof(tmp_set));
547 qualify_syscall_tokens(name, tmp_set, description);
552 * Initialize inject_vec accourding to tmp_set.
553 * Merge tmp_set into inject_set.
556 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
557 if (!tmp_set[p].nslots && !tmp_set[p].not) {
561 if (!inject_vec[p]) {
562 inject_vec[p] = xcalloc(nsyscall_vec[p],
563 sizeof(*inject_vec[p]));
567 for (i = 0; i < nsyscall_vec[p]; ++i) {
568 if (is_number_in_set(i, &tmp_set[p])) {
569 add_number_to_set(i, &inject_set[p]);
570 inject_vec[p][i] = opts;
574 free(tmp_set[p].vec);
579 qualify_fault(const char *const str)
581 qualify_inject_common(str, true, "fault argument");
585 qualify_inject(const char *const str)
587 qualify_inject_common(str, false, "inject argument");
590 static const struct qual_options {
592 void (*qualify)(const char *);
594 { "trace", qualify_trace },
595 { "t", qualify_trace },
596 { "abbrev", qualify_abbrev },
597 { "a", qualify_abbrev },
598 { "verbose", qualify_verbose },
599 { "v", qualify_verbose },
600 { "raw", qualify_raw },
601 { "x", qualify_raw },
602 { "signal", qualify_signals },
603 { "signals", qualify_signals },
604 { "s", qualify_signals },
605 { "read", qualify_read },
606 { "reads", qualify_read },
607 { "r", qualify_read },
608 { "write", qualify_write },
609 { "writes", qualify_write },
610 { "w", qualify_write },
611 { "fault", qualify_fault },
612 { "inject", qualify_inject },
616 qualify(const char *str)
618 const struct qual_options *opt = qual_options;
621 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
622 const char *p = qual_options[i].name;
623 unsigned int len = strlen(p);
625 if (strncmp(str, p, len) || str[len] != '=')
628 opt = &qual_options[i];
637 qual_flags(const unsigned int scno)
639 return (is_number_in_set(scno, &trace_set[current_personality])
641 | (is_number_in_set(scno, &abbrev_set[current_personality])
643 | (is_number_in_set(scno, &verbose_set[current_personality])
645 | (is_number_in_set(scno, &raw_set[current_personality])
647 | (is_number_in_set(scno, &inject_set[current_personality])