2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2016-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "number_set.h"
34 struct number_set *read_set;
35 struct number_set *write_set;
36 struct number_set *signal_set;
38 static struct number_set *abbrev_set;
39 static struct number_set *inject_set;
40 static struct number_set *raw_set;
41 static struct number_set *trace_set;
42 static struct number_set *verbose_set;
45 sigstr_to_uint(const char *s)
49 if (*s >= '0' && *s <= '9')
50 return string_to_uint_upto(s, 255);
52 if (strncasecmp(s, "SIG", 3) == 0)
55 for (i = 0; i <= 255; ++i) {
56 const char *name = signame(i);
58 if (strncasecmp(name, "SIG", 3) != 0)
63 if (strcasecmp(name, s) != 0)
73 find_errno_by_name(const char *name)
77 for (i = 1; i < nerrnos; ++i) {
78 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
86 parse_inject_token(const char *const token, struct inject_opts *const fopts,
87 const bool fault_tokens_only)
92 if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
100 intval = string_to_uint_ex(val, &end, 0xffff, "+");
104 fopts->first = intval;
110 intval = string_to_uint_upto(val, 0xffff);
113 fopts->step = intval;
122 } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
123 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
125 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
127 intval = find_errno_by_name(val);
130 fopts->rval = -intval;
131 } else if (!fault_tokens_only
132 && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
133 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
135 intval = string_to_uint(val);
138 fopts->rval = intval;
139 } else if (!fault_tokens_only
140 && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
141 intval = sigstr_to_uint(val);
142 if (intval < 1 || intval > NSIG_BYTES * 8)
144 fopts->signo = intval;
153 parse_inject_expression(const char *const s, char **buf,
154 struct inject_opts *const fopts,
155 const bool fault_tokens_only)
157 char *saveptr = NULL;
162 for (token = strtok_r(*buf, ":", &saveptr); token;
163 token = strtok_r(NULL, ":", &saveptr)) {
166 else if (!parse_inject_token(token, fopts, fault_tokens_only))
179 qualify_read(const char *const str)
182 read_set = alloc_number_set_array(1);
183 qualify_tokens(str, read_set, string_to_uint, "descriptor");
187 qualify_write(const char *const str)
190 write_set = alloc_number_set_array(1);
191 qualify_tokens(str, write_set, string_to_uint, "descriptor");
195 qualify_signals(const char *const str)
198 signal_set = alloc_number_set_array(1);
199 qualify_tokens(str, signal_set, sigstr_to_uint, "signal");
203 qualify_trace(const char *const str)
206 trace_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
207 qualify_syscall_tokens(str, trace_set, "system call");
211 qualify_abbrev(const char *const str)
214 abbrev_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
215 qualify_syscall_tokens(str, abbrev_set, "system call");
219 qualify_verbose(const char *const str)
222 verbose_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
223 qualify_syscall_tokens(str, verbose_set, "system call");
227 qualify_raw(const char *const str)
230 raw_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
231 qualify_syscall_tokens(str, raw_set, "system call");
235 qualify_inject_common(const char *const str,
236 const bool fault_tokens_only,
237 const char *const description)
239 struct inject_opts opts = {
242 .rval = INJECT_OPTS_RVAL_DEFAULT,
246 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
248 error_msg_and_die("invalid %s '%s'", description, str);
251 /* If neither of retval, error, or signal is specified, then ... */
252 if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
253 if (fault_tokens_only) {
254 /* in fault= syntax the default error code is ENOSYS. */
257 /* in inject= syntax this is not allowed. */
258 error_msg_and_die("invalid %s '%s'", description, str);
262 struct number_set *tmp_set =
263 alloc_number_set_array(SUPPORTED_PERSONALITIES);
264 qualify_syscall_tokens(name, tmp_set, description);
269 * Initialize inject_vec accourding to tmp_set.
270 * Merge tmp_set into inject_set.
273 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
274 if (number_set_array_is_empty(tmp_set, p))
279 alloc_number_set_array(SUPPORTED_PERSONALITIES);
281 if (!inject_vec[p]) {
282 inject_vec[p] = xcalloc(nsyscall_vec[p],
283 sizeof(*inject_vec[p]));
287 for (i = 0; i < nsyscall_vec[p]; ++i) {
288 if (is_number_in_set_array(i, tmp_set, p)) {
289 add_number_to_set_array(i, inject_set, p);
290 inject_vec[p][i] = opts;
295 free_number_set_array(tmp_set, SUPPORTED_PERSONALITIES);
299 qualify_fault(const char *const str)
301 qualify_inject_common(str, true, "fault argument");
305 qualify_inject(const char *const str)
307 qualify_inject_common(str, false, "inject argument");
310 static const struct qual_options {
312 void (*qualify)(const char *);
314 { "trace", qualify_trace },
315 { "t", qualify_trace },
316 { "abbrev", qualify_abbrev },
317 { "a", qualify_abbrev },
318 { "verbose", qualify_verbose },
319 { "v", qualify_verbose },
320 { "raw", qualify_raw },
321 { "x", qualify_raw },
322 { "signal", qualify_signals },
323 { "signals", qualify_signals },
324 { "s", qualify_signals },
325 { "read", qualify_read },
326 { "reads", qualify_read },
327 { "r", qualify_read },
328 { "write", qualify_write },
329 { "writes", qualify_write },
330 { "w", qualify_write },
331 { "fault", qualify_fault },
332 { "inject", qualify_inject },
336 qualify(const char *str)
338 const struct qual_options *opt = qual_options;
341 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
342 const char *name = qual_options[i].name;
343 const size_t len = strlen(name);
344 const char *val = str_strip_prefix_len(str, name, len);
346 if (val == str || *val != '=')
349 opt = &qual_options[i];
357 qual_flags(const unsigned int scno)
359 return (is_number_in_set_array(scno, trace_set, current_personality)
361 | (is_number_in_set_array(scno, abbrev_set, current_personality)
363 | (is_number_in_set_array(scno, verbose_set, current_personality)
365 | (is_number_in_set_array(scno, raw_set, current_personality)
367 | (is_number_in_set_array(scno, inject_set, current_personality)