Browse Source

tests: check -DD and -DDD options

* tests/strace-D.test: Check -DD and -DDD.
* tests/strace-DD.test: New test.
* tests/strace-DDD.test: Likewise.
* tests/strace-DD.expected: New file.
* tests/strace-DDD.expected: Likewise.
* tests/tracer_ppid_pgid_sid.c: Likewise.
* tests/Makefile.am (check_PROGRAMS): Add tracer_ppid_pgid_sid.
(MISC_TESTS): Add strace-DD.test and strace-DDD.test.
(EXTRA_DIST): Add strace-DD.expected and strace-DDD.expected.
Dmitry V. Levin 1 month ago
parent
commit
626e8d694d

+ 1
- 0
tests/.gitignore View File

@@ -635,6 +635,7 @@ timer_xettime
635 635
 timerfd_xettime
636 636
 times
637 637
 times-fail
638
+tracer_ppid_pgid_sid
638 639
 truncate
639 640
 truncate64
640 641
 ugetrlimit

+ 5
- 0
tests/Makefile.am View File

@@ -176,6 +176,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
176 176
 	status-unfinished-threads \
177 177
 	syslog-success \
178 178
 	threads-execve \
179
+	tracer_ppid_pgid_sid \
179 180
 	unblock_reset_raise \
180 181
 	unix-pair-send-recv \
181 182
 	unix-pair-sendto-recvfrom \
@@ -364,6 +365,8 @@ MISC_TESTS = \
364 365
 	status-unfinished-threads.test \
365 366
 	strace-C.test \
366 367
 	strace-D.test \
368
+	strace-DD.test \
369
+	strace-DDD.test \
367 370
 	strace-E.test \
368 371
 	strace-S.test \
369 372
 	strace-T.test \
@@ -457,6 +460,8 @@ EXTRA_DIST = \
457 460
 	status-detached.expected \
458 461
 	strace-C.expected \
459 462
 	strace-D.expected \
463
+	strace-DD.expected \
464
+	strace-DDD.expected \
460 465
 	strace-E.expected \
461 466
 	strace-T.expected \
462 467
 	strace-ff.expected \

+ 19
- 11
tests/strace-D.test View File

@@ -1,4 +1,4 @@
1
-#!/bin/sh
1
+#!/bin/sh -efu
2 2
 #
3 3
 # Check -D option.
4 4
 #
@@ -35,7 +35,7 @@ set -- sed -n -E '/^(P|Tracer)Pid:/p' "$status_file"
35 35
 	dump_log_and_fail_with "$*: unexpected output"
36 36
 
37 37
 # !-D: PPid > 0, TracerPid > 0, PPid == TracerPid
38
-run_strace -a14 -q -enone "$@" > "$OUT"
38
+run_strace -q -enone "$@" > "$OUT"
39 39
 [ "$(get_parent_pid < "$OUT")" -gt 0 ] &&
40 40
 [ "$(get_tracer_pid < "$OUT")" -gt 0 ] &&
41 41
 [ "$(get_parent_pid < "$OUT")" = "$(get_tracer_pid < "$OUT")" ] || {
@@ -44,14 +44,22 @@ run_strace -a14 -q -enone "$@" > "$OUT"
44 44
 }
45 45
 match_diff
46 46
 
47
-# -D: PPid > 0, TracerPid > 0, PPid != TracerPid
48
-run_strace -a14 -D -q -enone "$@" > "$OUT"
49
-[ "$(get_parent_pid < "$OUT")" -gt 0 ] &&
50
-[ "$(get_tracer_pid < "$OUT")" -gt 0 ] &&
51
-[ "$(get_parent_pid < "$OUT")" != "$(get_tracer_pid < "$OUT")" ] || {
52
-	cat < "$OUT" > "$LOG"
53
-	dump_log_and_fail_with "$STRACE $args: unexpected output"
47
+test_parent_tracer_pid()
48
+{
49
+	local d
50
+	d="$1"; shift
51
+
52
+	# -D/-DD/-DDD: PPid > 0, TracerPid > 0, PPid != TracerPid
53
+	run_strace $d -q -enone "$@" > "$OUT"
54
+	[ "$(get_parent_pid < "$OUT")" -gt 0 ] &&
55
+	[ "$(get_tracer_pid < "$OUT")" -gt 0 ] &&
56
+	[ "$(get_parent_pid < "$OUT")" != "$(get_tracer_pid < "$OUT")" ] || {
57
+		cat < "$OUT" > "$LOG"
58
+		dump_log_and_fail_with "$STRACE $args: unexpected output"
59
+	}
60
+	match_diff
54 61
 }
55
-match_diff
56 62
 
57
-exit 0
63
+test_parent_tracer_pid -D "$@"
64
+test_parent_tracer_pid -DD "$@"
65
+test_parent_tracer_pid -DDD "$@"

+ 1
- 0
tests/strace-DD.expected View File

@@ -0,0 +1 @@
1
+nanosleep({tv_sec=2, tv_nsec=0}, NULL)  = ? ERESTART_RESTARTBLOCK (Interrupted by signal)

+ 37
- 0
tests/strace-DD.test View File

@@ -0,0 +1,37 @@
1
+#!/bin/sh -efu
2
+#
3
+# Check -DD option.
4
+#
5
+# Copyright (c) 2019 Dmitry V. Levin <ldv@altlinux.org>
6
+# All rights reserved.
7
+#
8
+# SPDX-License-Identifier: GPL-2.0-or-later
9
+
10
+. "${srcdir=.}/init.sh"
11
+
12
+[ "$STRACE" = "$STRACE_EXE" ] ||
13
+	skip_ 'Not applicable: $STRACE != $STRACE_EXE'
14
+
15
+check_prog timeout
16
+
17
+test_D()
18
+{
19
+	local exp args
20
+	exp="$1"; shift
21
+
22
+	set -- -e signal=none -e trace=nanosleep "$@" ../sleep 2
23
+	args="$*"
24
+	set -- timeout -s XCPU 1 $STRACE -o "$LOG" "$@"
25
+	> "$LOG"
26
+	"$@" && rc=0 || rc=$?
27
+	[ "$rc" = 124 ] ||
28
+		dump_log_and_fail_with \
29
+			"$* failed with unexpected exit code $rc"
30
+	$SLEEP_A_BIT
31
+	match_diff "$LOG" "$exp"
32
+}
33
+
34
+printf %s 'nanosleep({tv_sec=2, tv_nsec=0}, ' > "$EXP"
35
+test_D "$EXP" -D
36
+test_D "$srcdir/$NAME.expected" -DD
37
+test_D "$srcdir/$NAME.expected" -DDD

+ 1
- 0
tests/strace-DDD.expected View File

@@ -0,0 +1 @@
1
++++ exited with 0 +++

+ 68
- 0
tests/strace-DDD.test View File

@@ -0,0 +1,68 @@
1
+#!/bin/sh -efu
2
+#
3
+# Check -DDD option.
4
+#
5
+# Copyright (c) 2019 Dmitry V. Levin <ldv@altlinux.org>
6
+# All rights reserved.
7
+#
8
+# SPDX-License-Identifier: GPL-2.0-or-later
9
+
10
+. "${srcdir=.}/init.sh"
11
+
12
+check_prog sed
13
+
14
+status_file=/proc/self/status
15
+[ -f "$status_file" ] ||
16
+	framework_skip_ "$status_file is not available"
17
+
18
+stat_file=/proc/self/stat
19
+[ -f "$stat_file" ] ||
20
+	framework_skip_ "$stat_file is not available"
21
+
22
+set -- -enone -esignal=none ../tracer_ppid_pgid_sid
23
+run_strace "$@" > "$OUT"
24
+read -r ppid pgid sid < "$OUT" &&
25
+[ "$ppid" -gt 1 ] &&
26
+[ "$pgid" -gt 0 ] &&
27
+[ "$sid" -gt 0 ] || {
28
+	cat < "$OUT" > "$LOG"
29
+	dump_log_and_fail_with "$STRACE $args: unexpected output"
30
+}
31
+match_diff
32
+
33
+pgid0="$pgid"
34
+sid0="$sid"
35
+
36
+run_strace -D "$@" > "$OUT"
37
+read -r ppid pgid sid < "$OUT" &&
38
+[ "$ppid" -eq 1 ] &&
39
+[ "$pgid" = "$pgid0" ] &&
40
+[ "$sid" = "$sid0" ] || {
41
+	cat < "$OUT" > "$LOG"
42
+	dump_log_and_fail_with "$STRACE $args: unexpected output"
43
+}
44
+match_diff
45
+
46
+run_strace -DD "$@" > "$OUT"
47
+read -r ppid pgid sid < "$OUT" &&
48
+[ "$ppid" -eq 1 ] &&
49
+[ "$pgid" -gt 1 ] &&
50
+[ "$pgid" != "$pgid0" ] &&
51
+[ "$pgid" != "$sid" ] &&
52
+[ "$sid" = "$sid0" ] || {
53
+	cat < "$OUT" > "$LOG"
54
+	dump_log_and_fail_with "$STRACE $args: unexpected output"
55
+}
56
+match_diff
57
+
58
+run_strace -DDD "$@" > "$OUT"
59
+read -r ppid pgid sid < "$OUT" &&
60
+[ "$ppid" -eq 1 ] &&
61
+[ "$pgid" -gt 1 ] &&
62
+[ "$pgid" != "$pgid0" ] &&
63
+[ "$sid" = "$pgid" ] &&
64
+[ "$sid" != "$sid0" ] || {
65
+	cat < "$OUT" > "$LOG"
66
+	dump_log_and_fail_with "$STRACE $args: unexpected output"
67
+}
68
+match_diff

+ 92
- 0
tests/tracer_ppid_pgid_sid.c View File

@@ -0,0 +1,92 @@
1
+/*
2
+ * Helper program for strace-DDD.test
3
+ *
4
+ * Copyright (c) 2019 Dmitry V. Levin <ldv@altlinux.org>
5
+ * All rights reserved.
6
+ *
7
+ * SPDX-License-Identifier: GPL-2.0-or-later
8
+ */
9
+
10
+#include "tests.h"
11
+#include <ctype.h>
12
+#include <stdio.h>
13
+#include <stdlib.h>
14
+#include <string.h>
15
+#include <unistd.h>
16
+
17
+static int
18
+fetch_tracer_pid(const char *str)
19
+{
20
+	for (; isspace(*str); ++str)
21
+		;
22
+	return atoi(str);
23
+}
24
+
25
+static int
26
+get_tracer_pid(void)
27
+{
28
+	static const char status[] = "/proc/self/status";
29
+	FILE *fp = fopen(status, "r");
30
+	if (!fp)
31
+		perror_msg_and_fail("fopen: %s", status);
32
+
33
+	static const char prefix[] = "TracerPid:";
34
+	const size_t prefix_len = sizeof(prefix) - 1;
35
+	const char *str = NULL;
36
+	char *line = NULL;
37
+	size_t n = 0;
38
+
39
+	while (getline(&line, &n, fp) > 0) {
40
+		if (strncmp(line, prefix, prefix_len) == 0) {
41
+			str = line + prefix_len;
42
+			break;
43
+		}
44
+	}
45
+	if (!str && !line)
46
+		perror_msg_and_fail("getline");
47
+
48
+	int pid = str ? fetch_tracer_pid(str) : 0;
49
+	free(line);
50
+	fclose(fp);
51
+
52
+	return pid;
53
+}
54
+
55
+static void
56
+get_ppid_pgid_sid(int pid, int *ppid, int *pgid, int *sid)
57
+{
58
+	char *stat;
59
+	if (asprintf(&stat, "/proc/%d/stat", pid) < 0)
60
+		perror_msg_and_fail("asprintf");
61
+
62
+	FILE *fp = fopen(stat, "r");
63
+	if (!fp)
64
+		perror_msg_and_fail("fopen: %s", stat);
65
+	char buf[4096];
66
+	if (!fgets(buf, sizeof(buf), fp))
67
+		perror_msg_and_fail("fgets: %s", stat);
68
+
69
+	fclose(fp);
70
+
71
+	const char *p = strrchr(buf, ')');
72
+	if (!p)
73
+		error_msg_and_fail("%s: parenthesis not found", stat);
74
+	++p;
75
+
76
+	if (sscanf(p, " %*c %d %d %d", ppid, pgid, sid) != 3)
77
+		error_msg_and_fail("%s: sscanf failed", stat);
78
+}
79
+
80
+int
81
+main(void)
82
+{
83
+	int tracer_pid = get_tracer_pid();
84
+	if (tracer_pid < 0)
85
+		error_msg_and_fail("tracer_pid = %d", tracer_pid);
86
+
87
+	int ppid = 0, pgid = 0, sid = 0;
88
+	get_ppid_pgid_sid(tracer_pid, &ppid, &pgid, &sid);
89
+	printf("%d %d %d\n", ppid, pgid, sid);
90
+
91
+	return 0;
92
+}

Loading…
Cancel
Save