Tue Nov 15 19:30:28 2016 UTC ()
Add new test fork1 in t_ptrace_wait{4,6,id,pid}

Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK set to
PTRACE_FORK.

In this test tracee calls fork(2) and this event is noted by tracer, both
for forker and forkee with PT_GET_PROCESS_STATE reporting pe_report_event
equal to PTRACE_FORK and pe_other_pid as forkee for forker and forker for
forkee.

The fork(2) event in the current implementation stops forker and forkee
with the SIGTRAP signal.

Exited forkee stops forker with the SIGCHLD signal.

Sponsored by <The NetBSD Foundation>.


(kamil)
diff -r1.15 -r1.16 src/tests/kernel/t_ptrace_wait.c

cvs diff -r1.15 -r1.16 src/tests/kernel/Attic/t_ptrace_wait.c (expand / switch to unified diff)

--- src/tests/kernel/Attic/t_ptrace_wait.c 2016/11/15 02:53:32 1.15
+++ src/tests/kernel/Attic/t_ptrace_wait.c 2016/11/15 19:30:28 1.16
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_ptrace_wait.c,v 1.15 2016/11/15 02:53:32 kamil Exp $ */ 1/* $NetBSD: t_ptrace_wait.c,v 1.16 2016/11/15 19:30:28 kamil Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc. 4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.15 2016/11/15 02:53:32 kamil Exp $"); 30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.16 2016/11/15 19:30:28 kamil Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/types.h> 33#include <sys/types.h>
34#include <sys/ptrace.h> 34#include <sys/ptrace.h>
35#include <sys/resource.h> 35#include <sys/resource.h>
36#include <sys/stat.h> 36#include <sys/stat.h>
37#include <sys/sysctl.h> 37#include <sys/sysctl.h>
38#include <sys/wait.h> 38#include <sys/wait.h>
39#include <err.h> 39#include <err.h>
40#include <errno.h> 40#include <errno.h>
41#include <signal.h> 41#include <signal.h>
42#include <stdint.h> 42#include <stdint.h>
43#include <stdio.h> 43#include <stdio.h>
@@ -1588,40 +1588,166 @@ ATF_TC_BODY(eventmask2, tc) @@ -1588,40 +1588,166 @@ ATF_TC_BODY(eventmask2, tc)
1588 "without signal to be sent\n"); 1588 "without signal to be sent\n");
1589 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1589 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1590 1590
1591 printf("Before calling %s() for the child\n", TWAIT_FNAME); 1591 printf("Before calling %s() for the child\n", TWAIT_FNAME);
1592 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1592 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1593 1593
1594 validate_status_exited(status, exitval); 1594 validate_status_exited(status, exitval);
1595 1595
1596 printf("Before calling %s() for the child\n", TWAIT_FNAME); 1596 printf("Before calling %s() for the child\n", TWAIT_FNAME);
1597 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1597 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1598} 1598}
1599 1599
1600#if defined(TWAIT_HAVE_PID) 1600#if defined(TWAIT_HAVE_PID)
 1601ATF_TC(fork1);
 1602ATF_TC_HEAD(fork1, tc)
 1603{
 1604 atf_tc_set_md_var(tc, "descr",
 1605 "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
 1606 "set to PTRACE_FORK");
 1607}
 1608
 1609ATF_TC_BODY(fork1, tc)
 1610{
 1611 const int exitval = 5;
 1612 const int exitval2 = 15;
 1613 const int sigval = SIGSTOP;
 1614 pid_t child, child2, wpid;
 1615#if defined(TWAIT_HAVE_STATUS)
 1616 int status;
 1617#endif
 1618 ptrace_state_t state;
 1619 const int slen = sizeof(state);
 1620 ptrace_event_t event;
 1621 const int elen = sizeof(event);
 1622
 1623 printf("Before forking process PID=%d\n", getpid());
 1624 child = atf_utils_fork();
 1625 if (child == 0) {
 1626 printf("Before calling PT_TRACE_ME from child %d\n", getpid());
 1627 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
 1628
 1629 printf("Before raising %s from child\n", strsignal(sigval));
 1630 FORKEE_ASSERT(raise(sigval) == 0);
 1631
 1632 FORKEE_ASSERT((child2 = fork()) != 1);
 1633
 1634 if (child2 == 0)
 1635 _exit(exitval2);
 1636
 1637 FORKEE_REQUIRE_SUCCESS
 1638 (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
 1639
 1640 forkee_status_exited(status, exitval2);
 1641
 1642 printf("Before exiting of the child process\n");
 1643 _exit(exitval);
 1644 }
 1645 printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
 1646
 1647 printf("Before calling %s() for the child\n", TWAIT_FNAME);
 1648 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 1649
 1650 validate_status_stopped(status, sigval);
 1651
 1652 printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
 1653 event.pe_set_event = PTRACE_FORK;
 1654 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
 1655
 1656 printf("Before resuming the child process where it left off and "
 1657 "without signal to be sent\n");
 1658 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 1659
 1660 printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
 1661 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 1662
 1663 validate_status_stopped(status, SIGTRAP);
 1664
 1665 ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
 1666 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
 1667
 1668 child2 = state.pe_other_pid;
 1669 printf("Reported PTRACE_FORK event with forkee %d\n", child2);
 1670
 1671 printf("Before calling %s() for the forkee %d of the child %d\n",
 1672 TWAIT_FNAME, child2, child);
 1673 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
 1674 child2);
 1675
 1676 validate_status_stopped(status, SIGTRAP);
 1677
 1678 ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
 1679 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
 1680 ATF_REQUIRE_EQ(state.pe_other_pid, child);
 1681
 1682 printf("Before resuming the forkee process where it left off and "
 1683 "without signal to be sent\n");
 1684 ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
 1685
 1686 printf("Before resuming the child process where it left off and "
 1687 "without signal to be sent\n");
 1688 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 1689
 1690 printf("Before calling %s() for the forkee - expected exited\n",
 1691 TWAIT_FNAME);
 1692 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
 1693 child2);
 1694
 1695 validate_status_exited(status, exitval2);
 1696
 1697 printf("Before calling %s() for the forkee - expected no process\n",
 1698 TWAIT_FNAME);
 1699 TWAIT_REQUIRE_FAILURE(ECHILD,
 1700 wpid = TWAIT_GENERIC(child2, &status, 0));
 1701
 1702 printf("Before calling %s() for the child - expected stopped "
 1703 "SIGCHLD\n", TWAIT_FNAME);
 1704 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 1705
 1706 validate_status_stopped(status, SIGCHLD);
 1707
 1708 printf("Before resuming the child process where it left off and "
 1709 "without signal to be sent\n");
 1710 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 1711
 1712 printf("Before calling %s() for the child - expected exited\n",
 1713 TWAIT_FNAME);
 1714 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 1715
 1716 validate_status_exited(status, exitval);
 1717
 1718 printf("Before calling %s() for the child - expected no process\n",
 1719 TWAIT_FNAME);
 1720 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
 1721}
 1722#endif
 1723
 1724#if defined(TWAIT_HAVE_PID)
1601#define ATF_TP_ADD_TC_HAVE_PID(a,b) ATF_TP_ADD_TC(a,b) 1725#define ATF_TP_ADD_TC_HAVE_PID(a,b) ATF_TP_ADD_TC(a,b)
1602#else 1726#else
1603#define ATF_TP_ADD_TC_HAVE_PID(a,b) 1727#define ATF_TP_ADD_TC_HAVE_PID(a,b)
1604#endif 1728#endif
1605 1729
1606ATF_TP_ADD_TCS(tp) 1730ATF_TP_ADD_TCS(tp)
1607{ 1731{
1608 setvbuf(stdout, NULL, _IONBF, 0); 1732 setvbuf(stdout, NULL, _IONBF, 0);
1609 setvbuf(stderr, NULL, _IONBF, 0); 1733 setvbuf(stderr, NULL, _IONBF, 0);
1610 ATF_TP_ADD_TC(tp, traceme1); 1734 ATF_TP_ADD_TC(tp, traceme1);
1611 ATF_TP_ADD_TC(tp, traceme2); 1735 ATF_TP_ADD_TC(tp, traceme2);
1612 ATF_TP_ADD_TC(tp, traceme3); 1736 ATF_TP_ADD_TC(tp, traceme3);
1613 ATF_TP_ADD_TC(tp, traceme4); 1737 ATF_TP_ADD_TC(tp, traceme4);
1614 1738
1615 ATF_TP_ADD_TC_HAVE_PID(tp, attach1); 1739 ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
1616 ATF_TP_ADD_TC_HAVE_PID(tp, attach2); 1740 ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
1617 ATF_TP_ADD_TC(tp, attach3); 1741 ATF_TP_ADD_TC(tp, attach3);
1618 ATF_TP_ADD_TC(tp, attach4); 1742 ATF_TP_ADD_TC(tp, attach4);
1619 ATF_TP_ADD_TC_HAVE_PID(tp, attach5); 1743 ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
1620 ATF_TP_ADD_TC_HAVE_PID(tp, attach6); 1744 ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
1621 ATF_TP_ADD_TC_HAVE_PID(tp, attach7); 1745 ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
1622 1746
1623 ATF_TP_ADD_TC(tp, eventmask1); 1747 ATF_TP_ADD_TC(tp, eventmask1);
1624 ATF_TP_ADD_TC(tp, eventmask2); 1748 ATF_TP_ADD_TC(tp, eventmask2);
1625 1749
 1750 ATF_TP_ADD_TC_HAVE_PID(tp, fork1);
 1751
1626 return atf_no_error(); 1752 return atf_no_error();
1627} 1753}