1#!/bin/sh
2#
3# Check strace options syntax.
4#
5# Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6# Copyright (c) 2016-2018 The strace developers.
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. The name of the author may not be used to endorse or promote products
18#    derived from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31. "${srcdir=.}/syntax.sh"
32
33check_e "Invalid process id: '0'" -p 0
34check_e "Invalid process id: '-42'" -p -42
35check_e "Invalid process id: '$$.'" -p $$.
36check_e "Invalid process id: 'a'" -p 1,a
37check_e "Syscall 'chdir' for -b isn't supported" -b chdir
38check_e "Syscall 'chdir' for -b isn't supported" -b execve -b chdir
39
40check_e_using_grep 'exec: File *name too long' "$(printf '%4096s' ' ')"
41
42ff_name="$(printf '%4084s' ' ')"
43check_e_using_grep "$ff_name: File *name too long" -ff -o "$ff_name" true
44
45check_h 'must have PROG [ARGS] or -p PID'
46check_h 'PROG [ARGS] must be specified with -D' -D -p $$
47check_h '-c and -C are mutually exclusive' -c -C true
48check_h '-c and -C are mutually exclusive' -C -c true
49check_h '(-c or -C) and -ff are mutually exclusive' -c -ff true
50check_h '(-c or -C) and -ff are mutually exclusive' -C -ff true
51check_h '-w must be given with (-c or -C)' -w true
52check_h 'piping the output and -ff are mutually exclusive' -o '|' -ff true
53check_h 'piping the output and -ff are mutually exclusive' -o '!' -ff true
54check_h "invalid -a argument: '-42'" -a -42
55check_h "invalid -O argument: '-42'" -O -42
56check_h "invalid -s argument: '-42'" -s -42
57check_h "invalid -s argument: '1073741824'" -s 1073741824
58check_h "invalid -I argument: '5'" -I 5
59
60../zeroargc "$STRACE_EXE" /bin/true 2> "$LOG" &&
61	dump_log_and_fail_with \
62		'zeroargc strace failed to handle the error properly'
63cat > "$EXP" << __EOF__
64$STRACE_EXE: must have PROG [ARGS] or -p PID
65Try '$STRACE_EXE -h' for more information.
66__EOF__
67diff -u -- "$EXP" "$LOG" > /dev/null || {
68	cat > "$EXP" <<- '__EOF__'
69	strace: must have PROG [ARGS] or -p PID
70	Try 'strace -h' for more information.
71	__EOF__
72	match_diff "$LOG" "$EXP" \
73		"zeroargc $STRACE $args output mismatch"
74}
75
76if [ -n "${UID-}" ]; then
77	if [ "${UID-}" = 0 ]; then
78		umsg="Cannot find user ':nosuchuser:'"
79	else
80		umsg='You must be root to use the -u option'
81	fi
82
83	check_e "$umsg" -u :nosuchuser: true
84
85	for c in i r t T y; do
86		check_e "-$c has no effect with -c
87$STRACE_EXE: $umsg" -u :nosuchuser: -c -$c true
88	done
89		check_e "-i has no effect with -c
90$STRACE_EXE: -r has no effect with -c
91$STRACE_EXE: -t has no effect with -c
92$STRACE_EXE: -T has no effect with -c
93$STRACE_EXE: -y has no effect with -c
94$STRACE_EXE: $umsg" -u :nosuchuser: -cirtTy true
95
96	check_e "-tt has no effect with -r
97$STRACE_EXE: $umsg" -u :nosuchuser: -r -tt true
98fi
99
100args='-p 2147483647'
101$STRACE $args 2> "$LOG" &&
102	dump_log_and_fail_with \
103		"strace $args failed to handle the error properly"
104
105for cmd in PTRACE_SEIZE PTRACE_ATTACH; do
106	cat > "$EXP" << __EOF__
107$STRACE_EXE: attach: ptrace($cmd, 2147483647): No such process
108__EOF__
109	diff -- "$EXP" "$LOG" ||
110		continue
111	args=
112	break
113done
114
115[ -z "$args" ] ||
116	dump_log_and_fail_with \
117		"strace $args failed to print expected diagnostics"
118