Module Name: src Committed By: kamil Date: Tue Apr 10 00:40:35 UTC 2018
Modified Files: src/tests/lib/libc/sys: t_ptrace_wait.c Log Message: ATF: ptrace: Merge code in fork2 and vfork2 tests with (v)fork1 ones Reduce code duplication, use the same function body with conditional switches. Sponsored by <The NetBSD Foundation> To generate a diff of this commit: cvs rdiff -u -r1.28 -r1.29 src/tests/lib/libc/sys/t_ptrace_wait.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libc/sys/t_ptrace_wait.c diff -u src/tests/lib/libc/sys/t_ptrace_wait.c:1.28 src/tests/lib/libc/sys/t_ptrace_wait.c:1.29 --- src/tests/lib/libc/sys/t_ptrace_wait.c:1.28 Tue Apr 10 00:09:31 2018 +++ src/tests/lib/libc/sys/t_ptrace_wait.c Tue Apr 10 00:40:35 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.c,v 1.29 2018/04/10 00:40:35 kamil Exp $ */ /*- * Copyright (c) 2016 The NetBSD Foundation, Inc. @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 kamil Exp $"); +__RCSID("$NetBSD: t_ptrace_wait.c,v 1.29 2018/04/10 00:40:35 kamil Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -1321,9 +1321,8 @@ ATF_TC_BODY(eventmask6, tc) TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); } -#if defined(TWAIT_HAVE_PID) static void -fork_test(bool regular) +fork_test(bool regular, bool track_event) { const int exitval = 5; const int exitval2 = 15; @@ -1332,12 +1331,14 @@ fork_test(bool regular) #if defined(TWAIT_HAVE_STATUS) int status; #endif +#if defined(TWAIT_HAVE_PID) ptrace_state_t state; const int slen = sizeof(state); +#endif ptrace_event_t event; const int elen = sizeof(event); - if (!regular) { + if (!regular && track_event) { atf_tc_expect_fail("PR kern/51630"); } @@ -1370,64 +1371,76 @@ fork_test(bool regular) validate_status_stopped(status, sigval); - DPRINTF("Enable PTRACE_%sFORK in EVENT_MASK for the child %d\n", - regular ? "" : "V", child); - event.pe_set_event = regular ? PTRACE_FORK : PTRACE_VFORK; + DPRINTF("%s PTRACE_%sFORK in EVENT_MASK for the child %d\n", + track_event ? "Enable" : "Disable", regular ? "" : "V", child); + event.pe_set_event = track_event ? (regular ? PTRACE_FORK : PTRACE_VFORK) : 0; SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); DPRINTF("Before resuming the child process where it left off and " "without signal to be sent\n"); - DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, " - "pe_report_event=PTRACE_%sFORK, state.pe_other_pid=child2) and " - "for child2 (TRAP_CHLD, pe_report_event=PTRACE_%sFORK, " - "state.pe_other_pid=child)\n", child, regular ? "" : "V", - regular ? "" : "V"); + if (track_event) { + DPRINTF("We expect two SIGTRAP events, for child %d " + "(TRAP_CHLD, pe_report_event=PTRACE_%sFORK, " + "state.pe_other_pid=child2) and for child2 " + "(TRAP_CHLD, pe_report_event=PTRACE_%sFORK, " + "state.pe_other_pid=child)\n", child, + regular ? "" : "V", regular ? "" : "V"); + } SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); - DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_stopped(status, SIGTRAP); - - SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); - ATF_REQUIRE_EQ(state.pe_report_event, - regular ? PTRACE_FORK : PTRACE_VFORK); - - child2 = state.pe_other_pid; - DPRINTF("Reported PTRACE_%sFORK event with forkee %d\n", - regular ? "" : "V", child2); - - DPRINTF("Before calling %s() for the forkee %d of the child %d\n", - TWAIT_FNAME, child2, child); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), - child2); - - validate_status_stopped(status, SIGTRAP); - - SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); - ATF_REQUIRE_EQ(state.pe_report_event, - regular ? PTRACE_FORK : PTRACE_VFORK); - ATF_REQUIRE_EQ(state.pe_other_pid, child); - - DPRINTF("Before resuming the forkee process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); +#if defined(TWAIT_HAVE_PID) + if (track_event) { + DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, + child); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), + child); - DPRINTF("Before resuming the child process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); + validate_status_stopped(status, SIGTRAP); - DPRINTF("Before calling %s() for the forkee - expected exited\n", - TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), - child2); + SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, + slen) != -1); + ATF_REQUIRE_EQ(state.pe_report_event, + regular ? PTRACE_FORK : PTRACE_VFORK); + + child2 = state.pe_other_pid; + DPRINTF("Reported PTRACE_%sFORK event with forkee %d\n", + regular ? "" : "V", child2); + + DPRINTF("Before calling %s() for the forkee %d of the child " + "%d\n", TWAIT_FNAME, child2, child); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), + child2); - validate_status_exited(status, exitval2); + validate_status_stopped(status, SIGTRAP); - DPRINTF("Before calling %s() for the forkee - expected no process\n", - TWAIT_FNAME); - TWAIT_REQUIRE_FAILURE(ECHILD, - wpid = TWAIT_GENERIC(child2, &status, 0)); + SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, + slen) != -1); + ATF_REQUIRE_EQ(state.pe_report_event, + regular ? PTRACE_FORK : PTRACE_VFORK); + ATF_REQUIRE_EQ(state.pe_other_pid, child); + + DPRINTF("Before resuming the forkee process where it left off " + "and without signal to be sent\n"); + SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) + != -1); + + DPRINTF("Before resuming the child process where it left off " + "and without signal to be sent\n"); + SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); + + DPRINTF("Before calling %s() for the forkee - expected exited" + "\n", TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), + child2); + + validate_status_exited(status, exitval2); + + DPRINTF("Before calling %s() for the forkee - expected no " + "process\n", TWAIT_FNAME); + TWAIT_REQUIRE_FAILURE(ECHILD, + wpid = TWAIT_GENERIC(child2, &status, 0)); + } +#endif DPRINTF("Before calling %s() for the child - expected stopped " "SIGCHLD\n", TWAIT_FNAME); @@ -1450,6 +1463,7 @@ fork_test(bool regular) TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); } +#if defined(TWAIT_HAVE_PID) ATF_TC(fork1); ATF_TC_HEAD(fork1, tc) { @@ -1461,7 +1475,7 @@ ATF_TC_HEAD(fork1, tc) ATF_TC_BODY(fork1, tc) { - fork_test(true); + fork_test(true, true); } #endif @@ -1475,72 +1489,8 @@ ATF_TC_HEAD(fork2, tc) ATF_TC_BODY(fork2, tc) { - const int exitval = 5; - const int exitval2 = 15; - const int sigval = SIGSTOP; - pid_t child, child2, wpid; -#if defined(TWAIT_HAVE_STATUS) - int status; -#endif - ptrace_event_t event; - const int elen = sizeof(event); - - DPRINTF("Before forking process PID=%d\n", getpid()); - SYSCALL_REQUIRE((child = fork()) != -1); - if (child == 0) { - DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); - FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); - - DPRINTF("Before raising %s from child\n", strsignal(sigval)); - FORKEE_ASSERT(raise(sigval) == 0); - - FORKEE_ASSERT((child2 = fork()) != -1); - - if (child2 == 0) - _exit(exitval2); - - FORKEE_REQUIRE_SUCCESS - (wpid = TWAIT_GENERIC(child2, &status, 0), child2); - - forkee_status_exited(status, exitval2); - - DPRINTF("Before exiting of the child process\n"); - _exit(exitval); - } - DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); - - DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_stopped(status, sigval); - - DPRINTF("Set empty EVENT_MASK for the child %d\n", child); - event.pe_set_event = 0; - SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); - DPRINTF("Before resuming the child process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); - - DPRINTF("Before calling %s() for the child - expected stopped " - "SIGCHLD\n", TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_stopped(status, SIGCHLD); - - DPRINTF("Before resuming the child process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); - - DPRINTF("Before calling %s() for the child - expected exited\n", - TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_exited(status, exitval); - - DPRINTF("Before calling %s() for the child - expected no process\n", - TWAIT_FNAME); - TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); + fork_test(true, false); } #if defined(TWAIT_HAVE_PID) @@ -1555,7 +1505,7 @@ ATF_TC_HEAD(vfork1, tc) ATF_TC_BODY(vfork1, tc) { - fork_test(false); + fork_test(false, true); } #endif @@ -1569,72 +1519,8 @@ ATF_TC_HEAD(vfork2, tc) ATF_TC_BODY(vfork2, tc) { - const int exitval = 5; - const int exitval2 = 15; - const int sigval = SIGSTOP; - pid_t child, child2, wpid; -#if defined(TWAIT_HAVE_STATUS) - int status; -#endif - ptrace_event_t event; - const int elen = sizeof(event); - - DPRINTF("Before forking process PID=%d\n", getpid()); - SYSCALL_REQUIRE((child = fork()) != -1); - if (child == 0) { - DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); - FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); - DPRINTF("Before raising %s from child\n", strsignal(sigval)); - FORKEE_ASSERT(raise(sigval) == 0); - - FORKEE_ASSERT((child2 = vfork()) != -1); - - if (child2 == 0) - _exit(exitval2); - - FORKEE_REQUIRE_SUCCESS - (wpid = TWAIT_GENERIC(child2, &status, 0), child2); - - forkee_status_exited(status, exitval2); - - DPRINTF("Before exiting of the child process\n"); - _exit(exitval); - } - DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); - - DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_stopped(status, sigval); - - DPRINTF("Set empty EVENT_MASK for the child %d\n", child); - event.pe_set_event = 0; - SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); - - DPRINTF("Before resuming the child process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); - - DPRINTF("Before calling %s() for the child - expected stopped " - "SIGCHLD\n", TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_stopped(status, SIGCHLD); - - DPRINTF("Before resuming the child process where it left off and " - "without signal to be sent\n"); - SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); - - DPRINTF("Before calling %s() for the child - expected exited\n", - TWAIT_FNAME); - TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - - validate_status_exited(status, exitval); - - DPRINTF("Before calling %s() for the child - expected no process\n", - TWAIT_FNAME); - TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); + fork_test(false, false); } ATF_TC(vforkdone1);