[Midnightbsd-cvs] src [11973] trunk/tests/sys/kern: add kern tests
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sat Jul 28 16:50:25 EDT 2018
Revision: 11973
http://svnweb.midnightbsd.org/src/?rev=11973
Author: laffer1
Date: 2018-07-28 16:50:25 -0400 (Sat, 28 Jul 2018)
Log Message:
-----------
add kern tests
Added Paths:
-----------
trunk/tests/sys/kern/
trunk/tests/sys/kern/Makefile
trunk/tests/sys/kern/acct/
trunk/tests/sys/kern/acct/Makefile
trunk/tests/sys/kern/acct/acct_test.c
trunk/tests/sys/kern/execve/
trunk/tests/sys/kern/execve/Makefile
trunk/tests/sys/kern/execve/bad_interp_len
trunk/tests/sys/kern/execve/dev_null_script
trunk/tests/sys/kern/execve/execve_helper.c
trunk/tests/sys/kern/execve/execve_test.sh
trunk/tests/sys/kern/execve/good_aout.c
trunk/tests/sys/kern/execve/good_script
trunk/tests/sys/kern/execve/non_exist_shell
trunk/tests/sys/kern/execve/script_arg
trunk/tests/sys/kern/execve/script_arg_nospace
trunk/tests/sys/kern/kern_descrip_test.c
trunk/tests/sys/kern/pipe/
trunk/tests/sys/kern/pipe/Makefile
trunk/tests/sys/kern/pipe/big_pipe_test.c
trunk/tests/sys/kern/pipe/pipe_fstat_bug_test.c
trunk/tests/sys/kern/pipe/pipe_ino_test.c
trunk/tests/sys/kern/pipe/pipe_overcommit1_test.c
trunk/tests/sys/kern/pipe/pipe_overcommit2_test.c
trunk/tests/sys/kern/pipe/pipe_reverse2_test.c
trunk/tests/sys/kern/pipe/pipe_reverse_test.c
trunk/tests/sys/kern/pipe/pipe_wraparound_test.c
trunk/tests/sys/kern/ptrace_test.c
trunk/tests/sys/kern/reaper.c
trunk/tests/sys/kern/unix_passfd_test.c
trunk/tests/sys/kern/unix_seqpacket_test.c
trunk/tests/sys/kern/waitpid_nohang.c
Added: trunk/tests/sys/kern/Makefile
===================================================================
--- trunk/tests/sys/kern/Makefile (rev 0)
+++ trunk/tests/sys/kern/Makefile 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,37 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/tests/sys/kern/Makefile 315949 2017-03-25 13:33:23Z badger $
+
+SRCTOP= ${.CURDIR:H:H:H}
+OBJTOP= ${.OBJDIR:H:H:H}
+TESTSRC= ${SRCTOP}/contrib/netbsd-tests/kernel
+
+TESTSDIR= ${TESTSBASE}/sys/kern
+
+ATF_TESTS_C+= kern_descrip_test
+ATF_TESTS_C+= ptrace_test
+TEST_METADATA.ptrace_test+= timeout="15"
+ATF_TESTS_C+= reaper
+ATF_TESTS_C+= unix_seqpacket_test
+ATF_TESTS_C+= unix_passfd_test
+TEST_METADATA.unix_seqpacket_test+= timeout="15"
+ATF_TESTS_C+= waitpid_nohang
+
+LDADD.ptrace_test+= -lpthread
+LDADD.unix_seqpacket_test+= -lpthread
+
+NETBSD_ATF_TESTS_C+= lockf_test
+NETBSD_ATF_TESTS_C+= mqueue_test
+
+CFLAGS.mqueue_test+= -I${SRCTOP}/tests
+DPADD.mqueue_test+= ${LIBRT}
+LDADD.mqueue_test+= -lrt
+
+WARNS?= 5
+
+TESTS_SUBDIRS+= acct
+TESTS_SUBDIRS+= execve
+TESTS_SUBDIRS+= pipe
+
+.include <netbsd-tests.test.mk>
+
+.include <bsd.test.mk>
Property changes on: trunk/tests/sys/kern/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/acct/Makefile
===================================================================
--- trunk/tests/sys/kern/acct/Makefile (rev 0)
+++ trunk/tests/sys/kern/acct/Makefile 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,21 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/tests/sys/kern/acct/Makefile 313214 2017-02-04 16:02:49Z ngie $
+
+TESTSDIR= ${TESTSBASE}/sys/kern/acct
+
+ATF_TESTS_C= acct_test
+
+CFLAGS+= -I${.OBJDIR}
+
+CLEANFILES+= convert.c convert.c.tmp
+
+DPSRCS.acct_test= convert.c
+acct_test.o: convert.c
+
+convert.c: ${.CURDIR:H:H:H:H}/sys/kern/kern_acct.c
+ sed -n -e 's/log(/syslog(/g' \
+ -e 's/exp/exponent/g' \
+ -e '/FLOAT_CONVERSION_START/,/FLOAT_CONVERSION_END/p' ${.ALLSRC} >${.TARGET}.tmp
+ mv ${.TARGET}.tmp ${.TARGET}
+
+.include <bsd.test.mk>
Property changes on: trunk/tests/sys/kern/acct/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/acct/acct_test.c
===================================================================
--- trunk/tests/sys/kern/acct/acct_test.c (rev 0)
+++ trunk/tests/sys/kern/acct/acct_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,241 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2011 Giorgos Keramidas. All rights reserved.
+ * Copyright (c) 2007 Diomidis Spinellis. All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/acct/acct_test.c 305228 2016-09-01 19:08:07Z ngie $");
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <syslog.h>
+#include <time.h>
+
+#include <atf-c.h>
+
+#define KASSERT(val, msg) assert(val)
+
+typedef u_int32_t comp_t;
+
+#define AHZ 1000000
+
+#include "convert.c"
+
+union cf {
+ comp_t c;
+ float f;
+};
+
+static void
+check_result(const char *name, float expected, union cf v)
+{
+ double eps;
+
+ eps = fabs(expected - v.f) / expected;
+ ATF_CHECK(eps <= FLT_EPSILON);
+ if (eps > FLT_EPSILON) {
+ printf("Error in %s\n", name);
+ printf("Got 0x%08x %12g\n", v.c, v.f);
+ v.f = expected;
+ printf("Expected 0x%08x %12g (%.15lg)\n", v.c, v.f, expected);
+ printf("Epsilon=%lg, rather than %g\n", eps, FLT_EPSILON);
+ }
+}
+
+/*
+ * Test case for encoding {0 sec, 0 usec} within a reasonable epsilon.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_zero);
+ATF_TC_BODY(encode_tv_zero, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ v.c = encode_timeval(tv);
+ ATF_CHECK(fabs(v.f - 0.0) < FLT_EPSILON);
+}
+
+/*
+ * Test case for encoding a random long number.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_long);
+ATF_TC_BODY(encode_long, tc)
+{
+ union cf v;
+ long l;
+
+ l = random();
+ v.c = encode_long(l);
+ check_result(atf_tc_get_ident(tc), l, v);
+}
+
+/*
+ * Test case for encoding a small number of seconds {1 sec, 0 usec}.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_only_sec);
+ATF_TC_BODY(encode_tv_only_sec, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+}
+
+/*
+ * Test case for encoding a small number of usec {0 sec, 1 usec}.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_only_usec);
+ATF_TC_BODY(encode_tv_only_usec, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 1;
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+}
+
+/*
+ * Test case for encoding a large number of usec {1 sec, 999.999 usec}.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_many_usec);
+ATF_TC_BODY(encode_tv_many_usec, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 999999L;
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+}
+
+/*
+ * Test case for encoding a huge number of usec {1 sec, 1.000.000 usec} that
+ * overflows the usec counter and should show up as an increase in timeval's
+ * seconds instead.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_usec_overflow);
+ATF_TC_BODY(encode_tv_usec_overflow, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 1000000L;
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+}
+
+/*
+ * Test case for encoding a very large number of seconds, one that is very
+ * near to the limit of 32-bit signed values. With a usec value of 999.999
+ * microseconds this should result in the largest value we can represent with
+ * a timeval struct.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_upper_limit);
+ATF_TC_BODY(encode_tv_upper_limit, tc)
+{
+ union cf v;
+ struct timeval tv;
+
+ tv.tv_sec = 2147483647L;
+ tv.tv_usec = 999999L;
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+}
+
+/*
+ * Test case for encoding a million random timeval objects, and checking that
+ * the conversion does not diverge too much from the expected values.
+ */
+
+ATF_TC_WITHOUT_HEAD(encode_tv_random_million);
+ATF_TC_BODY(encode_tv_random_million, tc)
+{
+ union cf v;
+ struct timeval tv;
+ long k;
+
+#ifdef __LP64__
+ atf_tc_expect_fail("the testcase violates FLT_EPSILON on 64-bit "
+ "platforms, e.g. amd64");
+#endif
+
+ ATF_REQUIRE_MSG(unsetenv("TZ") == 0, "unsetting TZ failed; errno=%d", errno);
+
+ for (k = 1; k < 1000000L; k++) {
+ tv.tv_sec = random();
+ tv.tv_usec = (random() % 1000000L);
+ v.c = encode_timeval(tv);
+ check_result(atf_tc_get_ident(tc),
+ (float)tv.tv_sec * AHZ + tv.tv_usec, v);
+ }
+}
+
+/* ---------------------------------------------------------------------
+ * Main.
+ * --------------------------------------------------------------------- */
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, encode_long);
+ ATF_TP_ADD_TC(tp, encode_tv_zero);
+ ATF_TP_ADD_TC(tp, encode_tv_only_sec);
+ ATF_TP_ADD_TC(tp, encode_tv_only_usec);
+ ATF_TP_ADD_TC(tp, encode_tv_many_usec);
+ ATF_TP_ADD_TC(tp, encode_tv_usec_overflow);
+ ATF_TP_ADD_TC(tp, encode_tv_upper_limit);
+ ATF_TP_ADD_TC(tp, encode_tv_random_million);
+
+ return atf_no_error();
+}
Property changes on: trunk/tests/sys/kern/acct/acct_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/Makefile
===================================================================
--- trunk/tests/sys/kern/execve/Makefile (rev 0)
+++ trunk/tests/sys/kern/execve/Makefile 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,40 @@
+# $MidnightBSD$
+# $MidnightBSD$
+
+TESTSDIR= ${TESTSBASE}/sys/kern/execve
+
+BINDIR= ${TESTSDIR}
+
+MAN=
+
+ATF_TESTS_SH+= execve_test
+
+PROGS+= good_aout
+PROGS+= execve_helper
+
+LDFLAGS.goodaout+= -static
+
+CLEANFILES+= empty
+CLEANFILES+= sparse_aout
+CLEANFILES+= trunc_aout
+
+SCRIPTS+= bad_interp_len
+SCRIPTS+= dev_null_script
+SCRIPTS+= empty
+SCRIPTS+= good_script
+SCRIPTS+= non_exist_shell
+SCRIPTS+= script_arg
+SCRIPTS+= script_arg_nospace
+SCRIPTS+= sparse_aout
+SCRIPTS+= trunc_aout
+
+empty:
+ @touch $@
+
+sparse_aout:
+ @truncate -s 20480 $@
+
+trunc_aout:
+ @truncate -s 16 $@
+
+.include <bsd.test.mk>
Property changes on: trunk/tests/sys/kern/execve/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/bad_interp_len
===================================================================
--- trunk/tests/sys/kern/execve/bad_interp_len (rev 0)
+++ trunk/tests/sys/kern/execve/bad_interp_len 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#! 456789012345678 0123456789012345 789012345678 012345678901234 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
+# $MidnightBSD$
+
+echo succeeded
Property changes on: trunk/tests/sys/kern/execve/bad_interp_len
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/dev_null_script
===================================================================
--- trunk/tests/sys/kern/execve/dev_null_script (rev 0)
+++ trunk/tests/sys/kern/execve/dev_null_script 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#! /dev/null
+# $MidnightBSD$
+
+echo succeeded
Property changes on: trunk/tests/sys/kern/execve/dev_null_script
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/execve_helper.c
===================================================================
--- trunk/tests/sys/kern/execve/execve_helper.c (rev 0)
+++ trunk/tests/sys/kern/execve/execve_helper.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,55 @@
+/* $MidnightBSD$ */
+/* $NetBSD: doexec.c,v 1.8 2003/07/26 19:38:48 salo Exp $ */
+
+/*
+ * Copyright (c) 1993 Christopher G. Demetriou
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed for the
+ * NetBSD Project. See http://www.NetBSD.org/ for
+ * information about NetBSD.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $MidnightBSD$
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s <progname>\n", argv[0]);
+ exit(2);
+ }
+
+ execve(argv[1], &argv[1], NULL);
+ err(1, "execve failed");
+}
Property changes on: trunk/tests/sys/kern/execve/execve_helper.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/execve_test.sh
===================================================================
--- trunk/tests/sys/kern/execve/execve_test.sh (rev 0)
+++ trunk/tests/sys/kern/execve/execve_test.sh 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,115 @@
+
+bad_interp_len_head()
+{
+ atf_set "descr" "Bad interpreter length"
+}
+bad_interp_len_body()
+{
+ atf_check -s exit:1 -e 'match:No such file or directory' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper bad_interp_len"
+}
+
+empty_head()
+{
+ atf_set "descr" "Empty file"
+}
+empty_body()
+{
+ atf_check -s exit:1 -e 'match:Exec format error' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper empty"
+}
+
+good_aout_head()
+{
+ atf_set "descr" "Good a.out"
+}
+good_aout_body()
+{
+ atf_check -s exit:0 -e empty -o 'match:succeeded' \
+ -x "cd $(atf_get_srcdir) && ./execve_helper ./good_aout"
+}
+
+good_script_head()
+{
+ atf_set "descr" "Good script"
+}
+good_script_body()
+{
+ atf_check -s exit:0 -e empty -o 'match:succeeded' \
+ -x "cd $(atf_get_srcdir) && ./execve_helper good_script"
+}
+
+non_exist_head()
+{
+ atf_set "descr" "Non-existent file"
+}
+non_exist_body()
+{
+ atf_check -s exit:1 -e 'match:No such file or directory' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper non_exist"
+}
+
+non_exist_shell_head()
+{
+ atf_set "descr" "Non-existent shell"
+}
+non_exist_shell_body()
+{
+ atf_check -s exit:1 -e 'match:No such file or directory' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper non_exist_shell"
+}
+
+script_arg_head()
+{
+ atf_set "descr" "-x in the shebang"
+}
+script_arg_body()
+{
+ atf_check -s exit:0 -e 'match:\+ echo succeeded' -o 'match:succeeded' \
+ -x "cd $(atf_get_srcdir) && ./execve_helper script_arg"
+}
+
+script_arg_nospace_head()
+{
+ atf_set "descr" '-x in the shebang; no space between #! and /bin/sh'
+}
+script_arg_nospace_body()
+{
+ atf_check -s exit:0 -e 'match:\+ echo succeeded' -o 'match:succeeded' \
+ -x "cd $(atf_get_srcdir) && ./execve_helper script_arg_nospace"
+}
+
+sparse_aout_head()
+{
+ atf_set "descr" 'Sparse file'
+}
+sparse_aout_body()
+{
+ atf_check -s exit:1 -e 'match:Exec format error' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper sparse_aout"
+}
+
+trunc_aout_head()
+{
+ atf_set "descr" 'Truncated file'
+}
+trunc_aout_body()
+{
+ atf_check -s exit:1 -e 'match:Exec format error' -o empty \
+ -x "cd $(atf_get_srcdir) && ./execve_helper trunc_aout"
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case bad_interp_len
+ atf_add_test_case empty
+ atf_add_test_case good_aout
+ atf_add_test_case good_script
+ atf_add_test_case non_exist
+ atf_add_test_case non_exist_shell
+ atf_add_test_case script_arg
+ atf_add_test_case script_arg_nospace
+ atf_add_test_case sparse_aout
+ atf_add_test_case trunc_aout
+
+}
Property changes on: trunk/tests/sys/kern/execve/execve_test.sh
___________________________________________________________________
Added: mnbsd:nokeywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/good_aout.c
===================================================================
--- trunk/tests/sys/kern/execve/good_aout.c (rev 0)
+++ trunk/tests/sys/kern/execve/good_aout.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,46 @@
+/* $MidnightBSD$ */
+/* $NetBSD: goodaout.c,v 1.8 2003/07/26 19:38:49 salo Exp $ */
+
+/*-
+ * Copyright (c) 1993 Christopher G. Demetriou
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed for the
+ * NetBSD Project. See http://www.NetBSD.org/ for
+ * information about NetBSD.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $MidnightBSD$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+ printf("succeeded\n");
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/execve/good_aout.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/good_script
===================================================================
--- trunk/tests/sys/kern/execve/good_script (rev 0)
+++ trunk/tests/sys/kern/execve/good_script 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#!/bin/sh
+# $MidnightBSD$
+
+echo succeeded
Property changes on: trunk/tests/sys/kern/execve/good_script
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/non_exist_shell
===================================================================
--- trunk/tests/sys/kern/execve/non_exist_shell (rev 0)
+++ trunk/tests/sys/kern/execve/non_exist_shell 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#! /foo/bar/baz
+# $MidnightBSD$
+
+echo foo
Property changes on: trunk/tests/sys/kern/execve/non_exist_shell
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/script_arg
===================================================================
--- trunk/tests/sys/kern/execve/script_arg (rev 0)
+++ trunk/tests/sys/kern/execve/script_arg 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#! /bin/sh -x
+# $MidnightBSD$
+
+echo succeeded
Property changes on: trunk/tests/sys/kern/execve/script_arg
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/execve/script_arg_nospace
===================================================================
--- trunk/tests/sys/kern/execve/script_arg_nospace (rev 0)
+++ trunk/tests/sys/kern/execve/script_arg_nospace 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,4 @@
+#!/bin/sh -x
+# $MidnightBSD$
+
+echo succeeded
Property changes on: trunk/tests/sys/kern/execve/script_arg_nospace
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/tests/sys/kern/kern_descrip_test.c
===================================================================
--- trunk/tests/sys/kern/kern_descrip_test.c (rev 0)
+++ trunk/tests/sys/kern/kern_descrip_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,192 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2014 EMC Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/kern_descrip_test.c 301805 2016-06-10 18:07:35Z ngie $");
+
+#include <sys/param.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/limits.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+#include <atf-c.h>
+
+static volatile sig_atomic_t done;
+
+#define AFILE "afile"
+#define EXPANDBY 1000
+#define PARALLEL 4
+#define RENDEZVOUS "rendezvous"
+#define VALUE "value"
+
+ATF_TC_WITHOUT_HEAD(dup2__simple);
+ATF_TC_BODY(dup2__simple, tc)
+{
+ int fd1, fd2;
+ struct stat sb1, sb2;
+
+ ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
+ fd2 = 27;
+ ATF_REQUIRE(dup2(fd1, fd2) != -1);
+ ATF_REQUIRE(fstat(fd1, &sb1) != -1);
+ ATF_REQUIRE(fstat(fd2, &sb2) != -1);
+ ATF_REQUIRE(bcmp(&sb1, &sb2, sizeof(sb1)) == 0);
+}
+
+ATF_TC(dup2__ebadf_when_2nd_arg_out_of_range);
+ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Regression test for r234131");
+}
+
+ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc)
+{
+ int fd1, fd2, ret;
+
+ ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
+ fd2 = INT_MAX;
+ ret = dup2(fd1, fd2);
+ ATF_CHECK_EQ(-1, ret);
+ ATF_CHECK_EQ(EBADF, errno);
+}
+
+static void
+handler(int s __unused)
+{
+ done++;
+}
+
+static void
+openfiles2(size_t n)
+{
+ size_t i;
+ int r;
+
+ errno = 0;
+ for (i = 0; i < n; i++)
+ ATF_REQUIRE((r = open(AFILE, O_RDONLY)) != -1);
+ kill(getppid(), SIGUSR1);
+
+ for (;;) {
+ if (access(RENDEZVOUS, R_OK) != 0)
+ break;
+ usleep(1000);
+ }
+ _exit(0);
+}
+
+static void
+openfiles(size_t n)
+{
+ int i, fd;
+
+ signal(SIGUSR1, handler);
+ ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1);
+ close(fd);
+ ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1);
+ close(fd);
+ done = 0;
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ openfiles2(n / PARALLEL);
+ while (done != PARALLEL)
+ usleep(1000);
+ unlink(RENDEZVOUS);
+ usleep(40000);
+}
+
+ATF_TC_WITH_CLEANUP(kern_maxfiles__increase);
+ATF_TC_HEAD(kern_maxfiles__increase, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
+ atf_tc_set_md_var(tc, "descr",
+ "Check kern.maxfiles expansion");
+}
+
+ATF_TC_BODY(kern_maxfiles__increase, tc)
+{
+ size_t oldlen;
+ int maxfiles, oldmaxfiles, current;
+ char buf[80];
+
+ oldlen = sizeof(maxfiles);
+ if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1)
+ atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
+ strerror(errno));
+ if (sysctlbyname("kern.openfiles", ¤t, &oldlen, NULL, 0) == -1)
+ atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles",
+ strerror(errno));
+
+ oldmaxfiles = maxfiles;
+
+ /* Store old kern.maxfiles in a symlink for cleanup */
+ snprintf(buf, sizeof(buf), "%d", oldmaxfiles);
+ if (symlink(buf, VALUE) == 1)
+ atf_tc_fail("symlink(%s, %s): %s", buf, VALUE,
+ strerror(errno));
+
+ maxfiles += EXPANDBY;
+ if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1)
+ atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
+ strerror(errno));
+
+ openfiles(oldmaxfiles - current + 1);
+ (void)unlink(VALUE);
+}
+
+ATF_TC_CLEANUP(kern_maxfiles__increase, tc)
+{
+ size_t oldlen;
+ int n, oldmaxfiles;
+ char buf[80];
+
+ if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) {
+ buf[MIN((size_t)n, sizeof(buf) - 1)] = '\0';
+ if (sscanf(buf, "%d", &oldmaxfiles) == 1) {
+ oldlen = sizeof(oldmaxfiles);
+ (void) sysctlbyname("kern.maxfiles", NULL, 0,
+ &oldmaxfiles, oldlen);
+ }
+ }
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, dup2__simple);
+ ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range);
+ ATF_TP_ADD_TC(tp, kern_maxfiles__increase);
+
+ return (atf_no_error());
+}
Property changes on: trunk/tests/sys/kern/kern_descrip_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/Makefile
===================================================================
--- trunk/tests/sys/kern/pipe/Makefile (rev 0)
+++ trunk/tests/sys/kern/pipe/Makefile 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,17 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/tests/sys/kern/pipe/Makefile 290914 2015-11-16 05:38:40Z ngie $
+
+TESTSDIR= ${TESTSBASE}/sys/kern/pipe
+
+PLAIN_TESTS_C+= big_pipe_test
+PLAIN_TESTS_C+= pipe_fstat_bug_test
+PLAIN_TESTS_C+= pipe_ino_test
+PLAIN_TESTS_C+= pipe_overcommit1_test
+PLAIN_TESTS_C+= pipe_overcommit2_test
+PLAIN_TESTS_C+= pipe_reverse2_test
+PLAIN_TESTS_C+= pipe_reverse_test
+PLAIN_TESTS_C+= pipe_wraparound_test
+
+WARNS?= 6
+
+.include <bsd.test.mk>
Property changes on: trunk/tests/sys/kern/pipe/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/big_pipe_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/big_pipe_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/big_pipe_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,89 @@
+/* $MidnightBSD$ */
+#include <sys/select.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define BIG_PIPE_SIZE 64*1024 /* From sys/pipe.h */
+
+/*
+ * Test for the non-blocking big pipe bug (write(2) returning
+ * EAGAIN while select(2) returns the descriptor as ready for write).
+ *
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/big_pipe_test.c 290914 2015-11-16 05:38:40Z ngie $
+ */
+
+static void
+write_frame(int fd, char *buf, unsigned long buflen)
+{
+ fd_set wfd;
+ int i;
+
+ while (buflen) {
+ FD_ZERO(&wfd);
+ FD_SET(fd, &wfd);
+ i = select(fd+1, NULL, &wfd, NULL, NULL);
+ if (i < 0)
+ err(1, "select failed");
+ if (i != 1) {
+ errx(1, "select returned unexpected value %d\n", i);
+ exit(1);
+ }
+ i = write(fd, buf, buflen);
+ if (i < 0) {
+ if (errno != EAGAIN)
+ warn("write failed");
+ exit(1);
+ }
+ buf += i;
+ buflen -= i;
+ }
+}
+
+int
+main(void)
+{
+ /* any value over PIPE_SIZE should do */
+ char buf[BIG_PIPE_SIZE];
+ int i, flags, fd[2];
+
+ if (pipe(fd) < 0)
+ errx(1, "pipe failed");
+
+ flags = fcntl(fd[1], F_GETFL);
+ if (flags == -1 || fcntl(fd[1], F_SETFL, flags|O_NONBLOCK) == -1) {
+ printf("fcntl failed: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ switch (fork()) {
+ case -1:
+ err(1, "fork failed: %s\n", strerror(errno));
+ break;
+ case 0:
+ close(fd[1]);
+ for (;;) {
+ /* Any small size should do */
+ i = read(fd[0], buf, 256);
+ if (i == 0)
+ break;
+ if (i < 0)
+ err(1, "read");
+ }
+ exit(0);
+ default:
+ break;
+ }
+
+ close(fd[0]);
+ memset(buf, 0, sizeof buf);
+ for (i = 0; i < 1000; i++)
+ write_frame(fd[1], buf, sizeof buf);
+
+ printf("ok\n");
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/big_pipe_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_fstat_bug_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_fstat_bug_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_fstat_bug_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,157 @@
+/* $MidnightBSD$ */
+/*
+Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+*/
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/event.h>
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_fstat_bug_test.c 309662 2016-12-07 00:57:15Z ngie $
+ * The goal of this program is to see if fstat reports the correct
+ * data count for a pipe. Prior to revision 1.172 of sys_pipe.c,
+ * 0 would be returned once the pipe entered direct write mode.
+ *
+ * Linux (2.6) always returns zero, so it's not a valuable platform
+ * for comparison.
+ */
+
+int
+main(void)
+{
+ char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";
+ int desc[2], ipc_coord[2];
+ struct kevent event, ke;
+ ssize_t error;
+ int successes = 0;
+ struct stat status;
+ pid_t new_pid;
+ int kq;
+
+ error = pipe(desc);
+ if (error == -1)
+ err(1, "Couldn't allocate data pipe");
+
+ error = pipe(ipc_coord);
+ if (error == -1)
+ err(1, "Couldn't allocate IPC coordination pipe");
+
+ new_pid = fork();
+ assert(new_pid != -1);
+
+ close(new_pid == 0 ? desc[0] : desc[1]);
+
+#define SYNC_R(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: waiting for synchronization", __LINE__); \
+ if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
+ errno = _error; \
+ } while(0)
+
+#define SYNC_W(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: sending synchronization", __LINE__); \
+ if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
+ errno = _error; \
+ } while(0)
+
+#define WRITE(s) do { \
+ ssize_t _size; \
+ if ((_size = write(desc[1], &buffer, s)) != s) \
+ warn("short write; wrote %zd, expected %d", _size, s); \
+ } while(0)
+
+ if (new_pid == 0) {
+
+ SYNC_R(0, go);
+ WRITE(145);
+ SYNC_W(0, go2);
+
+ SYNC_R(0, go);
+ WRITE(2048);
+ SYNC_W(0, go2);
+
+ SYNC_R(0, go);
+ WRITE(4096);
+ SYNC_W(0, go2);
+
+ SYNC_R(0, go);
+ WRITE(8191);
+ SYNC_W(0, go2);
+
+ SYNC_R(0, go);
+ SYNC_W(0, go2); /* XXX: why is this required? */
+ WRITE(8192);
+ SYNC_W(0, go2);
+
+ close(ipc_coord[0]);
+ close(ipc_coord[1]);
+
+ _exit(0);
+ }
+
+ kq = kqueue();
+ if (kq == -1)
+ _exit(1);
+
+ EV_SET(&ke, desc[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
+
+ /* Attach event to the kqueue. */
+ if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)
+ _exit(2);
+
+ while (successes < 5) {
+ SYNC_W(1, go);
+ SYNC_R(1, go2);
+
+ /* Ensure data is available to read */
+ if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)
+ _exit(3);
+
+ fstat(desc[0], &status);
+ error = read(desc[0], &buffer2, sizeof(buffer2));
+
+ if (status.st_size != error)
+ err(1, "FAILURE: stat size %jd read size %zd",
+ (intmax_t)status.st_size, error);
+ if (error > 0) {
+ printf("SUCCESS at stat size %jd read size %zd\n",
+ (intmax_t)status.st_size, error);
+ successes++;
+ }
+ }
+
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_fstat_bug_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_ino_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_ino_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_ino_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,66 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2011 Giovanni Trematerra <giovanni.trematerra at gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_ino_test.c 290914 2015-11-16 05:38:40Z ngie $
+ * Test conformance to stat(2) SUSv4 description:
+ * "For all other file types defined in this volume of POSIX.1-2008, the
+ * structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atim,
+ * st_ctim, and st_mtim shall have meaningful values ...".
+ * Check that st_dev and st_ino are meaningful.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int pipefd[2];
+ struct stat st1, st2;
+
+ if (pipe(pipefd) == -1)
+ err(1, "FAIL: pipe");
+
+ if (fstat(pipefd[0], &st1) == -1)
+ err(1, "FAIL: fstat st1");
+ if (fstat(pipefd[1], &st2) == -1)
+ err(1, "FAIL: fstat st2");
+ if (st1.st_dev != st2.st_dev || st1.st_dev == 0 || st2.st_dev == 0)
+ errx(1, "FAIL: wrong dev number %d %d", st1.st_dev, st2.st_dev);
+ if (st1.st_ino == st2.st_ino)
+ errx(1, "FAIL: inode numbers are equal: %d", st1.st_ino);
+
+ close(pipefd[0]);
+ close(pipefd[1]);
+ printf("PASS\n");
+
+ return (0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_ino_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_overcommit1_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_overcommit1_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_overcommit1_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,52 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (C) 2005 Michael J. Silbersack <silby at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice(s), this list of conditions and the following disclaimer as
+ * the first lines of this file unmodified other than the possible
+ * addition of one or more copyright notices.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice(s), this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_overcommit1_test.c 293144 2016-01-04 07:09:29Z ngie $
+ * This program just allocates as many pipes as it can to ensure
+ * that using up all pipe memory doesn't cause a panic.
+ */
+
+int
+main(void)
+{
+ int pipes[10000];
+ unsigned int i;
+
+ for (i = 0; i < nitems(pipes); i++)
+ (void)pipe(&pipes[i]);
+ printf("PASS\n");
+
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_overcommit1_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_overcommit2_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_overcommit2_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_overcommit2_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,84 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (C) 2005 Michael J. Silbersack <silby at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice(s), this list of conditions and the following disclaimer as
+ * the first lines of this file unmodified other than the possible
+ * addition of one or more copyright notices.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice(s), this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_overcommit2_test.c 290914 2015-11-16 05:38:40Z ngie $
+ * This program tests how sys_pipe.c handles the case where there
+ * is ample memory to allocate a pipe, but the file descriptor
+ * limit for that user has been exceeded.
+ */
+
+int
+main(void)
+{
+ char template[] = "pipe.XXXXXXXXXX";
+ int lastfd, pipes[10000], returnval;
+ unsigned int i;
+
+ lastfd = -1;
+
+ if (mkstemp(template) == -1)
+ err(1, "mkstemp failed");
+
+ for (i = 0; i < nitems(pipes); i++) {
+ returnval = open(template, O_RDONLY);
+ if (returnval == -1 && (errno == ENFILE || errno == EMFILE))
+ break; /* All descriptors exhausted. */
+ else
+ lastfd = returnval;
+ }
+
+ /* First falloc failure case in sys_pipe.c:pipe() */
+ for (i = 0; i < 1000; i++) {
+ returnval = pipe(&pipes[i]);
+ }
+
+ /*
+ * Free just one FD so that the second falloc failure
+ * case will occur.
+ */
+ close(lastfd);
+
+ for (i = 0; i < 1000; i++) {
+ returnval = pipe(&pipes[i]);
+ }
+ printf("PASS\n");
+
+ unlink(template);
+
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_overcommit2_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_reverse2_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_reverse2_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_reverse2_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,68 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2010 Jilles Tjoelker
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_reverse2_test.c 290914 2015-11-16 05:38:40Z ngie $
+ */
+
+#include <sys/select.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/*
+ * Check that pipes can be selected for writing in the reverse direction.
+ */
+int
+main(void)
+{
+ int pip[2];
+ fd_set set;
+ int n;
+
+ if (pipe(pip) == -1)
+ err(1, "FAIL: pipe");
+
+ FD_ZERO(&set);
+ FD_SET(pip[0], &set);
+ n = select(pip[1] + 1, NULL, &set, NULL, &(struct timeval){ 0, 0 });
+ if (n != 1)
+ errx(1, "FAIL: select initial reverse direction");
+
+ n = write(pip[0], "x", 1);
+ if (n != 1)
+ err(1, "FAIL: write reverse direction");
+
+ FD_ZERO(&set);
+ FD_SET(pip[0], &set);
+ n = select(pip[1] + 1, NULL, &set, NULL, &(struct timeval){ 0, 0 });
+ if (n != 1)
+ errx(1, "FAIL: select reverse direction after write");
+
+ printf("PASS\n");
+
+ return (0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_reverse2_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_reverse_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_reverse_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_reverse_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,150 @@
+/* $MidnightBSD$ */
+/*
+Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+*/
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_reverse_test.c 290914 2015-11-16 05:38:40Z ngie $
+ * This program simply tests writing through the reverse direction of
+ * a pipe. Nothing too fancy, it's only needed because most pipe-using
+ * programs never touch the reverse direction (it doesn't exist on
+ * Linux.)
+ */
+
+int
+main(void)
+{
+ char buffer[65535], buffer2[65535], go[] = "go", go2[] = "go2";
+ int desc[2], ipc_coord[2];
+ size_t i;
+ ssize_t total;
+ int buggy, error;
+ pid_t new_pid;
+
+ buggy = 0;
+ total = 0;
+
+ error = pipe(desc);
+ if (error == -1)
+ err(1, "Couldn't allocate data pipe");
+
+ error = pipe(ipc_coord);
+ if (error == -1)
+ err(1, "Couldn't allocate IPC coordination pipe");
+
+ buffer[0] = 'A';
+
+ for (i = 1; i < (int)sizeof(buffer); i++) {
+ buffer[i] = buffer[i - 1] + 1;
+ if (buffer[i] > 'Z')
+ buffer[i] = 'A';
+ }
+
+ new_pid = fork();
+ assert(new_pid != -1);
+
+#define SYNC_R(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: waiting for synchronization", __LINE__); \
+ if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
+ errno = _error; \
+ } while(0)
+
+#define SYNC_W(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: sending synchronization", __LINE__); \
+ if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
+ errno = _error; \
+ } while(0)
+
+#define WRITE(s) do { \
+ ssize_t _size; \
+ if ((_size = write(desc[1], &buffer[total], s)) != s) \
+ warn("short write; wrote %zd, expected %d", _size, s); \
+ total += _size; \
+ } while(0)
+
+ if (new_pid == 0) {
+ SYNC_R(0, go);
+ for (i = 0; i < 8; i++)
+ WRITE(4096);
+
+ SYNC_W(0, go2);
+ SYNC_R(0, go);
+
+ for (i = 0; i < 2; i++)
+ WRITE(4096);
+
+ SYNC_W(0, go2);
+
+ _exit(0);
+ }
+
+ SYNC_W(1, go);
+ SYNC_R(1, go2);
+
+ error = read(desc[0], &buffer2, 8 * 4096);
+ total += error;
+ printf("Read %d bytes\n", error);
+
+ SYNC_W(1, go);
+ SYNC_R(1, go2);
+
+ error = read(desc[0], &buffer2[total], 2 * 4096);
+ total += error;
+ printf("Read %d bytes, done\n", error);
+
+ if (memcmp(buffer, buffer2, total) != 0) {
+ for (i = 0; i < (size_t)total; i++) {
+ if (buffer[i] != buffer2[i]) {
+ buggy = 1;
+ printf("Location %zu input: %hhx "
+ "output: %hhx\n",
+ i, buffer[i], buffer2[i]);
+ }
+ }
+ }
+
+ waitpid(new_pid, NULL, 0);
+
+ if ((buggy == 1) || (total != 10 * 4096))
+ errx(1, "FAILED");
+ else
+ printf("SUCCESS\n");
+
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_reverse_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/pipe/pipe_wraparound_test.c
===================================================================
--- trunk/tests/sys/kern/pipe/pipe_wraparound_test.c (rev 0)
+++ trunk/tests/sys/kern/pipe/pipe_wraparound_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,141 @@
+/* $MidnightBSD$ */
+/*
+Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+*/
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * $FreeBSD: stable/10/tests/sys/kern/pipe/pipe_wraparound_test.c 290914 2015-11-16 05:38:40Z ngie $
+ * This program tests to make sure that wraparound writes and reads
+ * are working, assuming that 16K socket buffers are used. In order
+ * to really stress the pipe code with this test, kernel modifications
+ * nay be necessary.
+ */
+
+int main (void)
+{
+ char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";
+ int desc[2], ipc_coord[2];
+ ssize_t error, total;
+ int buggy, i;
+ pid_t new_pid;
+
+ buggy = 0;
+ total = 0;
+
+ error = pipe(desc);
+ if (error == -1)
+ err(1, "Couldn't allocate data pipe");
+
+ error = pipe(ipc_coord);
+ if (error == -1)
+ err(1, "Couldn't allocate IPC coordination pipe");
+
+ buffer[0] = 'A';
+
+ for (i = 1; i < (int)sizeof(buffer); i++) {
+ buffer[i] = buffer[i - 1] + 1;
+ if (buffer[i] > 'Z')
+ buffer[i] = 'A';
+ }
+
+ new_pid = fork();
+ assert(new_pid != -1);
+
+#define SYNC_R(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: waiting for synchronization", __LINE__); \
+ if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
+ errno = _error; \
+ } while(0)
+
+#define SYNC_W(i, _buf) do { \
+ int _error = errno; \
+ warnx("%d: sending synchronization", __LINE__); \
+ if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
+ err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
+ errno = _error; \
+ } while(0)
+
+#define WRITE(s) do { \
+ ssize_t _size; \
+ if ((_size = write(desc[1], &buffer[total], s)) != s) \
+ warn("short write; wrote %zd, expected %d", _size, s); \
+ total += _size; \
+ } while(0)
+
+ if (new_pid == 0) {
+ WRITE(4096);
+ WRITE(4096);
+ WRITE(4000);
+ SYNC_W(0, go2);
+
+ SYNC_R(0, go);
+ WRITE(3000);
+ WRITE(3000);
+ SYNC_W(0, go2);
+
+ _exit(0);
+ }
+
+ SYNC_R(1, go2);
+ error = read(desc[0], &buffer2, 8192);
+ total += error;
+ printf("Read %zd bytes\n", error);
+ SYNC_W(1, go);
+ SYNC_R(1, go2);
+ error = read(desc[0], &buffer2[total], 16384);
+ total += error;
+ printf("Read %zd bytes, done\n", error);
+
+ if (memcmp(buffer, buffer2, total) != 0) {
+ for (i = 0; i < total; i++) {
+ if (buffer[i] != buffer2[i]) {
+ buggy = 1;
+ printf("Location %d input: %hhx output: %hhx\n",
+ i, buffer[i], buffer2[i]);
+ }
+ }
+ }
+
+ waitpid(new_pid, NULL, 0);
+
+ if (buggy)
+ errx(1, "FAILURE");
+
+ printf("SUCCESS\n");
+
+ exit(0);
+}
Property changes on: trunk/tests/sys/kern/pipe/pipe_wraparound_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/ptrace_test.c
===================================================================
--- trunk/tests/sys/kern/ptrace_test.c (rev 0)
+++ trunk/tests/sys/kern/ptrace_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,3167 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2015 John Baldwin <jhb at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/ptrace_test.c 328384 2018-01-25 00:08:13Z jhb $");
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/procctl.h>
+#include <sys/ptrace.h>
+#include <sys/queue.h>
+#include <sys/runq.h>
+#include <sys/syscall.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <machine/cpufunc.h>
+#include <pthread.h>
+#include <sched.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <atf-c.h>
+
+/*
+ * A variant of ATF_REQUIRE that is suitable for use in child
+ * processes. This only works if the parent process is tripped up by
+ * the early exit and fails some requirement itself.
+ */
+#define CHILD_REQUIRE(exp) do { \
+ if (!(exp)) \
+ child_fail_require(__FILE__, __LINE__, \
+ #exp " not met"); \
+ } while (0)
+
+static __dead2 void
+child_fail_require(const char *file, int line, const char *str)
+{
+ char buf[128];
+
+ snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
+ write(2, buf, strlen(buf));
+ _exit(32);
+}
+
+static void
+trace_me(void)
+{
+
+ /* Attach the parent process as a tracer of this process. */
+ CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ /* Trigger a stop. */
+ raise(SIGSTOP);
+}
+
+static void
+attach_child(pid_t pid)
+{
+ pid_t wpid;
+ int status;
+
+ ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
+
+ wpid = waitpid(pid, &status, 0);
+ ATF_REQUIRE(wpid == pid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+}
+
+static void
+wait_for_zombie(pid_t pid)
+{
+
+ /*
+ * Wait for a process to exit. This is kind of gross, but
+ * there is not a better way.
+ *
+ * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
+ * with ESRCH. After that change, a valid struct kinfo_proc
+ * is returned for zombies with ki_stat set to SZOMB.
+ */
+ for (;;) {
+ struct kinfo_proc kp;
+ size_t len;
+ int mib[4];
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_PID;
+ mib[3] = pid;
+ len = sizeof(kp);
+ if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
+ ATF_REQUIRE(errno == ESRCH);
+ break;
+ }
+ if (kp.ki_stat == SZOMB)
+ break;
+ usleep(5000);
+ }
+}
+
+/*
+ * Verify that a parent debugger process "sees" the exit of a debugged
+ * process exactly once when attached via PT_TRACE_ME.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
+ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
+{
+ pid_t child, wpid;
+ int status;
+
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ /* Child process. */
+ trace_me();
+
+ _exit(1);
+ }
+
+ /* Parent process. */
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(child, &status, 0);
+ ATF_REQUIRE(wpid == child);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
+
+ /* The second wait() should report the exit status. */
+ wpid = waitpid(child, &status, 0);
+ ATF_REQUIRE(wpid == child);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ /* The child should no longer exist. */
+ wpid = waitpid(child, &status, 0);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a parent debugger process "sees" the exit of a debugged
+ * process exactly once when attached via PT_ATTACH.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
+ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
+{
+ pid_t child, wpid;
+ int cpipe[2], status;
+ char c;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ /* Child process. */
+ close(cpipe[0]);
+
+ /* Wait for the parent to attach. */
+ CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
+
+ _exit(1);
+ }
+ close(cpipe[1]);
+
+ /* Parent process. */
+
+ /* Attach to the child process. */
+ attach_child(child);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
+
+ /* Signal the child to exit. */
+ close(cpipe[0]);
+
+ /* The second wait() should report the exit status. */
+ wpid = waitpid(child, &status, 0);
+ ATF_REQUIRE(wpid == child);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ /* The child should no longer exist. */
+ wpid = waitpid(child, &status, 0);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a parent process "sees" the exit of a debugged process only
+ * after the debugger has seen it.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
+ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
+{
+ pid_t child, debugger, wpid;
+ int cpipe[2], dpipe[2], status;
+ char c;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((child = fork()) != -1);
+
+ if (child == 0) {
+ /* Child process. */
+ close(cpipe[0]);
+
+ /* Wait for parent to be ready. */
+ CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
+
+ _exit(1);
+ }
+ close(cpipe[1]);
+
+ ATF_REQUIRE(pipe(dpipe) == 0);
+ ATF_REQUIRE((debugger = fork()) != -1);
+
+ if (debugger == 0) {
+ /* Debugger process. */
+ close(dpipe[0]);
+
+ CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
+
+ wpid = waitpid(child, &status, 0);
+ CHILD_REQUIRE(wpid == child);
+ CHILD_REQUIRE(WIFSTOPPED(status));
+ CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
+
+ /* Signal parent that debugger is attached. */
+ CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
+
+ /* Wait for parent's failed wait. */
+ CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
+
+ wpid = waitpid(child, &status, 0);
+ CHILD_REQUIRE(wpid == child);
+ CHILD_REQUIRE(WIFEXITED(status));
+ CHILD_REQUIRE(WEXITSTATUS(status) == 1);
+
+ _exit(0);
+ }
+ close(dpipe[1]);
+
+ /* Parent process. */
+
+ /* Wait for the debugger to attach to the child. */
+ ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
+
+ /* Release the child. */
+ ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
+ ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
+ close(cpipe[0]);
+
+ wait_for_zombie(child);
+
+ /*
+ * This wait should return a pid of 0 to indicate no status to
+ * report. The parent should see the child as non-exited
+ * until the debugger sees the exit.
+ */
+ wpid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(wpid == 0);
+
+ /* Signal the debugger to wait for the child. */
+ close(dpipe[0]);
+
+ /* Wait for the debugger. */
+ wpid = waitpid(debugger, &status, 0);
+ ATF_REQUIRE(wpid == debugger);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ /* The child process should now be ready. */
+ wpid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(wpid == child);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+}
+
+/*
+ * Verify that a parent process "sees" the exit of a debugged process
+ * only after a non-direct-child debugger has seen it. In particular,
+ * various wait() calls in the parent must avoid failing with ESRCH by
+ * checking the parent's orphan list for the debugee.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
+ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
+{
+ pid_t child, debugger, fpid, wpid;
+ int cpipe[2], dpipe[2], status;
+ char c;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((child = fork()) != -1);
+
+ if (child == 0) {
+ /* Child process. */
+ close(cpipe[0]);
+
+ /* Wait for parent to be ready. */
+ CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
+
+ _exit(1);
+ }
+ close(cpipe[1]);
+
+ ATF_REQUIRE(pipe(dpipe) == 0);
+ ATF_REQUIRE((debugger = fork()) != -1);
+
+ if (debugger == 0) {
+ /* Debugger parent. */
+
+ /*
+ * Fork again and drop the debugger parent so that the
+ * debugger is not a child of the main parent.
+ */
+ CHILD_REQUIRE((fpid = fork()) != -1);
+ if (fpid != 0)
+ _exit(2);
+
+ /* Debugger process. */
+ close(dpipe[0]);
+
+ CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
+
+ wpid = waitpid(child, &status, 0);
+ CHILD_REQUIRE(wpid == child);
+ CHILD_REQUIRE(WIFSTOPPED(status));
+ CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
+
+ /* Signal parent that debugger is attached. */
+ CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
+
+ /* Wait for parent's failed wait. */
+ CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
+
+ wpid = waitpid(child, &status, 0);
+ CHILD_REQUIRE(wpid == child);
+ CHILD_REQUIRE(WIFEXITED(status));
+ CHILD_REQUIRE(WEXITSTATUS(status) == 1);
+
+ _exit(0);
+ }
+ close(dpipe[1]);
+
+ /* Parent process. */
+
+ /* Wait for the debugger parent process to exit. */
+ wpid = waitpid(debugger, &status, 0);
+ ATF_REQUIRE(wpid == debugger);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ /* A WNOHANG wait here should see the non-exited child. */
+ wpid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(wpid == 0);
+
+ /* Wait for the debugger to attach to the child. */
+ ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
+
+ /* Release the child. */
+ ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
+ ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
+ close(cpipe[0]);
+
+ wait_for_zombie(child);
+
+ /*
+ * This wait should return a pid of 0 to indicate no status to
+ * report. The parent should see the child as non-exited
+ * until the debugger sees the exit.
+ */
+ wpid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(wpid == 0);
+
+ /* Signal the debugger to wait for the child. */
+ ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
+
+ /* Wait for the debugger. */
+ ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
+ close(dpipe[0]);
+
+ /* The child process should now be ready. */
+ wpid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(wpid == child);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+}
+
+/*
+ * The parent process should always act the same regardless of how the
+ * debugger is attached to it.
+ */
+static __dead2 void
+follow_fork_parent(bool use_vfork)
+{
+ pid_t fpid, wpid;
+ int status;
+
+ if (use_vfork)
+ CHILD_REQUIRE((fpid = vfork()) != -1);
+ else
+ CHILD_REQUIRE((fpid = fork()) != -1);
+
+ if (fpid == 0)
+ /* Child */
+ _exit(2);
+
+ wpid = waitpid(fpid, &status, 0);
+ CHILD_REQUIRE(wpid == fpid);
+ CHILD_REQUIRE(WIFEXITED(status));
+ CHILD_REQUIRE(WEXITSTATUS(status) == 2);
+
+ _exit(1);
+}
+
+/*
+ * Helper routine for follow fork tests. This waits for two stops
+ * that report both "sides" of a fork. It returns the pid of the new
+ * child process.
+ */
+static pid_t
+handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
+{
+ struct ptrace_lwpinfo pl;
+ bool fork_reported[2];
+ pid_t child, wpid;
+ int i, status;
+
+ fork_reported[0] = false;
+ fork_reported[1] = false;
+ child = -1;
+
+ /*
+ * Each process should report a fork event. The parent should
+ * report a PL_FLAG_FORKED event, and the child should report
+ * a PL_FLAG_CHILD event.
+ */
+ for (i = 0; i < 2; i++) {
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid > 0);
+ ATF_REQUIRE(WIFSTOPPED(status));
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
+ 0);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
+ (PL_FLAG_FORKED | PL_FLAG_CHILD));
+ if (pl.pl_flags & PL_FLAG_CHILD) {
+ ATF_REQUIRE(wpid != parent);
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+ ATF_REQUIRE(!fork_reported[1]);
+ if (child == -1)
+ child = wpid;
+ else
+ ATF_REQUIRE(child == wpid);
+ if (ppl != NULL)
+ ppl[1] = pl;
+ fork_reported[1] = true;
+ } else {
+ ATF_REQUIRE(wpid == parent);
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+ ATF_REQUIRE(!fork_reported[0]);
+ if (child == -1)
+ child = pl.pl_child_pid;
+ else
+ ATF_REQUIRE(child == pl.pl_child_pid);
+ if (ppl != NULL)
+ ppl[0] = pl;
+ fork_reported[0] = true;
+ }
+ }
+
+ return (child);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork and
+ * that the traced parent sees the exit of the child after the debugger
+ * when both processes remain attached to the debugger.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
+ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The child can't exit until the grandchild reports status, so the
+ * grandchild should report its exit first to the debugger.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork
+ * and that the traced parent sees the exit of the child when the new
+ * child process is detached after it reports its fork.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
+ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * Should not see any status from the grandchild now, only the
+ * child.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork
+ * and that the traced parent sees the exit of the child when the
+ * traced parent is detached after the fork.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
+ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The child can't exit until the grandchild reports status, so the
+ * grandchild should report its exit first to the debugger.
+ *
+ * Even though the child process is detached, it is still a
+ * child of the debugger, so it will still report it's exit
+ * after the grandchild.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void
+attach_fork_parent(int cpipe[2])
+{
+ pid_t fpid;
+
+ close(cpipe[0]);
+
+ /* Double-fork to disassociate from the debugger. */
+ CHILD_REQUIRE((fpid = fork()) != -1);
+ if (fpid != 0)
+ _exit(3);
+
+ /* Send the pid of the disassociated child to the debugger. */
+ fpid = getpid();
+ CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
+
+ /* Wait for the debugger to attach. */
+ CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork and
+ * that the traced parent sees the exit of the child after the debugger
+ * when both processes remain attached to the debugger. In this test
+ * the parent that forks is not a direct child of the debugger.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
+ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int cpipe[2], status;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ attach_fork_parent(cpipe);
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ close(cpipe[1]);
+
+ /* Wait for the direct child to exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 3);
+
+ /* Read the pid of the fork parent. */
+ ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
+ sizeof(children[0]));
+
+ /* Attach to the fork parent. */
+ attach_child(children[0]);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the fork parent ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Signal the fork parent to continue. */
+ close(cpipe[0]);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The fork parent can't exit until the child reports status,
+ * so the child should report its exit first to the debugger.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork
+ * and that the traced parent sees the exit of the child when the new
+ * child process is detached after it reports its fork. In this test
+ * the parent that forks is not a direct child of the debugger.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
+ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int cpipe[2], status;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ attach_fork_parent(cpipe);
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ close(cpipe[1]);
+
+ /* Wait for the direct child to exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 3);
+
+ /* Read the pid of the fork parent. */
+ ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
+ sizeof(children[0]));
+
+ /* Attach to the fork parent. */
+ attach_child(children[0]);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the fork parent ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Signal the fork parent to continue. */
+ close(cpipe[0]);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * Should not see any status from the child now, only the fork
+ * parent.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a new child process is stopped after a followed fork
+ * and that the traced parent sees the exit of the child when the
+ * traced parent is detached after the fork. In this test the parent
+ * that forks is not a direct child of the debugger.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
+ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
+{
+ pid_t children[2], fpid, wpid;
+ int cpipe[2], status;
+
+ ATF_REQUIRE(pipe(cpipe) == 0);
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ attach_fork_parent(cpipe);
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ close(cpipe[1]);
+
+ /* Wait for the direct child to exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 3);
+
+ /* Read the pid of the fork parent. */
+ ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
+ sizeof(children[0]));
+
+ /* Attach to the fork parent. */
+ attach_child(children[0]);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the fork parent ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Signal the fork parent to continue. */
+ close(cpipe[0]);
+
+ children[1] = handle_fork_events(children[0], NULL);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * Should not see any status from the fork parent now, only
+ * the child.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
+ * child process created via fork() reports the correct value.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
+ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
+{
+ struct ptrace_lwpinfo pl[2];
+ pid_t children[2], fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(false);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Wait for both halves of the fork event to get reported. */
+ children[1] = handle_fork_events(children[0], pl);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
+ ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
+ ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
+ ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
+ ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The child can't exit until the grandchild reports status, so the
+ * grandchild should report its exit first to the debugger.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
+ * child process created via vfork() reports the correct value.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
+ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
+{
+ struct ptrace_lwpinfo pl[2];
+ pid_t children[2], fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(true);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Wait for both halves of the fork event to get reported. */
+ children[1] = handle_fork_events(children[0], pl);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
+ ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
+ ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
+ ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
+ ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The child can't exit until the grandchild reports status, so the
+ * grandchild should report its exit first to the debugger.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void *
+simple_thread(void *arg __unused)
+{
+
+ pthread_exit(NULL);
+}
+
+static __dead2 void
+simple_thread_main(void)
+{
+ pthread_t thread;
+
+ CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
+ CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
+ exit(1);
+}
+
+/*
+ * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
+ * thread reports the correct value.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
+ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ lwpid_t mainlwp;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ simple_thread_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ mainlwp = pl.pl_lwpid;
+
+ /*
+ * Continue the child ignoring the SIGSTOP and tracing all
+ * system call exits.
+ */
+ ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
+
+ /*
+ * Wait for the new thread to arrive. pthread_create() might
+ * invoke any number of system calls. For now we just wait
+ * for the new thread to arrive and make sure it reports a
+ * valid system call code. If ptrace grows thread event
+ * reporting then this test can be made more precise.
+ */
+ for (;;) {
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
+ ATF_REQUIRE(pl.pl_syscall_code != 0);
+ if (pl.pl_lwpid != mainlwp)
+ /* New thread seen. */
+ break;
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ }
+
+ /* Wait for the child to exit. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ for (;;) {
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFEXITED(status))
+ break;
+
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ }
+
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that the expected LWP events are reported for a child thread.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
+ATF_TC_BODY(ptrace__lwp_events, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ lwpid_t lwps[2];
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ simple_thread_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ lwps[0] = pl.pl_lwpid;
+
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The first event should be for the child thread's birth. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
+ (PL_FLAG_BORN | PL_FLAG_SCX));
+ ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
+ lwps[1] = pl.pl_lwpid;
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next event should be for the child thread's death. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
+ (PL_FLAG_EXITED | PL_FLAG_SCE));
+ ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void *
+exec_thread(void *arg __unused)
+{
+
+ execl("/usr/bin/true", "true", NULL);
+ exit(127);
+}
+
+static __dead2 void
+exec_thread_main(void)
+{
+ pthread_t thread;
+
+ CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
+ for (;;)
+ sleep(60);
+ exit(1);
+}
+
+/*
+ * Verify that the expected LWP events are reported for a multithreaded
+ * process that calls execve(2).
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
+ATF_TC_BODY(ptrace__lwp_events_exec, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ lwpid_t lwps[2];
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exec_thread_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ lwps[0] = pl.pl_lwpid;
+
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The first event should be for the child thread's birth. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
+ (PL_FLAG_BORN | PL_FLAG_SCX));
+ ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
+ lwps[1] = pl.pl_lwpid;
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /*
+ * The next event should be for the main thread's death due to
+ * single threading from execve().
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
+ (PL_FLAG_EXITED));
+ ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next event should be for the child process's exec. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
+ (PL_FLAG_EXEC | PL_FLAG_SCX));
+ ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void
+handler(int sig __unused)
+{
+}
+
+static void
+signal_main(void)
+{
+
+ signal(SIGINFO, handler);
+ raise(SIGINFO);
+ exit(0);
+}
+
+/*
+ * Verify that the expected ptrace event is reported for a signal.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
+ATF_TC_BODY(ptrace__siginfo, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ signal_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next event should be for the SIGINFO. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
+ ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
+ ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that the expected ptrace events are reported for PTRACE_EXEC.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
+ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
+{
+ pid_t fpid, wpid;
+ int events, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exec_thread(NULL);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ events = 0;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Should get one event at exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
+ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int events, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exec_thread(NULL);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ events = PTRACE_EXEC;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next event should be for the child process's exec. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
+ (PL_FLAG_EXEC | PL_FLAG_SCX));
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
+ATF_TC_BODY(ptrace__event_mask, tc)
+{
+ pid_t fpid, wpid;
+ int events, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exit(0);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+ ATF_REQUIRE(events & PTRACE_FORK);
+ ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+ ATF_REQUIRE(!(events & PTRACE_FORK));
+
+ /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+ ATF_REQUIRE(events & PTRACE_LWP);
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+ ATF_REQUIRE(!(events & PTRACE_LWP));
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Should get one event at exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that the expected ptrace events are reported for PTRACE_VFORK.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
+ATF_TC_BODY(ptrace__ptrace_vfork, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int events, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(true);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+ events |= PTRACE_VFORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
+ sizeof(events)) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
+
+ /* The next event should report the end of the vfork. */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
+ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
+{
+ struct ptrace_lwpinfo pl[2];
+ pid_t children[2], fpid, wpid;
+ int events, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ follow_fork_parent(true);
+ }
+
+ /* Parent process. */
+ children[0] = fpid;
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(children[0], &status, 0);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
+ sizeof(events)) == 0);
+ events |= PTRACE_FORK | PTRACE_VFORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
+ sizeof(events)) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ /* Wait for both halves of the fork event to get reported. */
+ children[1] = handle_fork_events(children[0], pl);
+ ATF_REQUIRE(children[1] > 0);
+
+ ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
+
+ /*
+ * The child can't exit until the grandchild reports status, so the
+ * grandchild should report its exit first to the debugger.
+ */
+ wpid = waitpid(children[1], &status, 0);
+ ATF_REQUIRE(wpid == children[1]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+
+ /*
+ * The child should report it's vfork() completion before it
+ * exits.
+ */
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
+ -1);
+ ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == children[0]);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * XXX: There's nothing inherently platform specific about this test, however a
+ * userspace visible breakpoint() is a prerequisite.
+ */
+ #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
+/*
+ * Verify that no more events are reported after PT_KILL except for the
+ * process exit when stopped due to a breakpoint trap.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
+ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
+{
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ breakpoint();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report hitting the breakpoint. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ /* Kill the child process. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+#endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
+
+/*
+ * Verify that no more events are reported after PT_KILL except for the
+ * process exit when stopped inside of a system call.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
+ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report a system call entry for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+
+ /* Kill the child process. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that no more events are reported after PT_KILL except for the
+ * process exit when killing a multithreaded process.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
+ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ lwpid_t main_lwp;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ simple_thread_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ main_lwp = pl.pl_lwpid;
+
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The first event should be for the child thread's birth. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
+ (PL_FLAG_BORN | PL_FLAG_SCX));
+ ATF_REQUIRE(pl.pl_lwpid != main_lwp);
+
+ /* Kill the child process. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void *
+mask_usr1_thread(void *arg)
+{
+ pthread_barrier_t *pbarrier;
+ sigset_t sigmask;
+
+ pbarrier = (pthread_barrier_t*)arg;
+
+ sigemptyset(&sigmask);
+ sigaddset(&sigmask, SIGUSR1);
+ CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
+
+ /* Sync up with other thread after sigmask updated. */
+ pthread_barrier_wait(pbarrier);
+
+ for (;;)
+ sleep(60);
+
+ return (NULL);
+}
+
+/*
+ * Verify that the SIGKILL from PT_KILL takes priority over other signals
+ * and prevents spurious stops due to those other signals.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
+ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
+{
+ pid_t fpid, wpid;
+ int status;
+ cpuset_t setmask;
+ pthread_t t;
+ pthread_barrier_t barrier;
+ struct sched_param sched_param;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ /* Bind to one CPU so only one thread at a time will run. */
+ CPU_ZERO(&setmask);
+ CPU_SET(0, &setmask);
+ cpusetid_t setid;
+ CHILD_REQUIRE(cpuset(&setid) == 0);
+ CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
+ CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
+
+ CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
+
+ CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
+ (void*)&barrier) == 0);
+
+ /*
+ * Give the main thread higher priority. The test always
+ * assumes that, if both threads are able to run, the main
+ * thread runs first.
+ */
+ sched_param.sched_priority =
+ (sched_get_priority_max(SCHED_FIFO) +
+ sched_get_priority_min(SCHED_FIFO)) / 2;
+ CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
+ SCHED_FIFO, &sched_param) == 0);
+ sched_param.sched_priority -= RQ_PPQ;
+ CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
+ &sched_param) == 0);
+
+ sigset_t sigmask;
+ sigemptyset(&sigmask);
+ sigaddset(&sigmask, SIGUSR2);
+ CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
+
+ /* Sync up with other thread after sigmask updated. */
+ pthread_barrier_wait(&barrier);
+
+ trace_me();
+
+ for (;;)
+ sleep(60);
+
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Send a signal that only the second thread can handle. */
+ ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
+
+ /* The second wait() should report the SIGUSR2. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
+
+ /* Send a signal that only the first thread can handle. */
+ ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
+
+ /* Replace the SIGUSR2 with a kill. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that the SIGKILL from PT_KILL takes priority over other stop events
+ * and prevents spurious stops caused by those events.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
+ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
+{
+ pid_t fpid, wpid;
+ int status;
+ cpuset_t setmask;
+ pthread_t t;
+ pthread_barrier_t barrier;
+ lwpid_t main_lwp;
+ struct ptrace_lwpinfo pl;
+ struct sched_param sched_param;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+
+ /* Bind to one CPU so only one thread at a time will run. */
+ CPU_ZERO(&setmask);
+ CPU_SET(0, &setmask);
+ cpusetid_t setid;
+ CHILD_REQUIRE(cpuset(&setid) == 0);
+ CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
+ CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
+
+ CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
+
+ CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
+ (void*)&barrier) == 0);
+
+ /*
+ * Give the main thread higher priority. The test always
+ * assumes that, if both threads are able to run, the main
+ * thread runs first.
+ */
+ sched_param.sched_priority =
+ (sched_get_priority_max(SCHED_FIFO) +
+ sched_get_priority_min(SCHED_FIFO)) / 2;
+ CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
+ SCHED_FIFO, &sched_param) == 0);
+ sched_param.sched_priority -= RQ_PPQ;
+ CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
+ &sched_param) == 0);
+
+ sigset_t sigmask;
+ sigemptyset(&sigmask);
+ sigaddset(&sigmask, SIGUSR2);
+ CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
+
+ /* Sync up with other thread after sigmask updated. */
+ pthread_barrier_wait(&barrier);
+
+ /* Sync up with the test before doing the getpid(). */
+ raise(SIGSTOP);
+
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ main_lwp = pl.pl_lwpid;
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /*
+ * Continue until child is done with setup, which is indicated with
+ * SIGSTOP. Ignore system calls in the meantime.
+ */
+ for (;;) {
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ if (WSTOPSIG(status) == SIGTRAP) {
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
+ } else {
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+ break;
+ }
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+ }
+
+ /* Proceed, allowing main thread to hit syscall entry for getpid(). */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_lwpid == main_lwp);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+ /* Prevent the main thread from hitting its syscall exit for now. */
+ ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
+
+ /*
+ * Proceed, allowing second thread to hit syscall exit for
+ * pthread_barrier_wait().
+ */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_lwpid != main_lwp);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
+
+ /* Send a signal that only the second thread can handle. */
+ ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
+
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next wait() should report the SIGUSR2. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
+
+ /* Allow the main thread to try to finish its system call. */
+ ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
+
+ /*
+ * At this point, the main thread is in the middle of a system call and
+ * has been resumed. The second thread has taken a SIGUSR2 which will
+ * be replaced with a SIGKILL below. The main thread will get to run
+ * first. It should notice the kill request (even though the signal
+ * replacement occurred in the other thread) and exit accordingly. It
+ * should not stop for the system call exit event.
+ */
+
+ /* Replace the SIGUSR2 with a kill. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL (not a syscall exit). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void
+sigusr1_handler(int sig)
+{
+
+ CHILD_REQUIRE(sig == SIGUSR1);
+ _exit(2);
+}
+
+/*
+ * Verify that even if the signal queue is full for a child process,
+ * a PT_KILL will kill the process.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
+ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
+{
+ pid_t fpid, wpid;
+ int status;
+ int max_pending_per_proc;
+ size_t len;
+ int i;
+
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ len = sizeof(max_pending_per_proc);
+ ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
+ &max_pending_per_proc, &len, NULL, 0) == 0);
+
+ /* Fill the signal queue. */
+ for (i = 0; i < max_pending_per_proc; ++i)
+ ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
+
+ /* Kill the child process. */
+ ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
+
+ /* The last wait() should report the SIGKILL. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that when stopped at a system call entry, a signal can be
+ * requested with PT_CONTINUE which will be delivered once the system
+ * call is complete.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report a system call entry for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+
+ /* Continue the child process with a signal. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ for (;;) {
+ /*
+ * The last wait() should report exit 2, i.e., a normal _exit
+ * from the signal handler. In the meantime, catch and proceed
+ * past any syscall stops.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ } else {
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+ break;
+ }
+ }
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void
+sigusr1_counting_handler(int sig)
+{
+ static int counter = 0;
+
+ CHILD_REQUIRE(sig == SIGUSR1);
+ counter++;
+ if (counter == 2)
+ _exit(2);
+}
+
+/*
+ * Verify that, when continuing from a stop at system call entry and exit,
+ * a signal can be requested from both stops, and both will be delivered when
+ * the system call is complete.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report a system call entry for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+
+ /* Continue the child process with a signal. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ /* The third wait() should report a system call exit for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
+
+ /* Continue the child process with a signal. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ for (;;) {
+ /*
+ * The last wait() should report exit 2, i.e., a normal _exit
+ * from the signal handler. In the meantime, catch and proceed
+ * past any syscall stops.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ } else {
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+ break;
+ }
+ }
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that even if the signal queue is full for a child process,
+ * a PT_CONTINUE with a signal will not result in loss of that signal.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
+{
+ pid_t fpid, wpid;
+ int status;
+ int max_pending_per_proc;
+ size_t len;
+ int i;
+
+ ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ len = sizeof(max_pending_per_proc);
+ ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
+ &max_pending_per_proc, &len, NULL, 0) == 0);
+
+ /* Fill the signal queue. */
+ for (i = 0; i < max_pending_per_proc; ++i)
+ ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
+
+ /* Continue with signal. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ for (;;) {
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFSTOPPED(status)) {
+ ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ } else {
+ /*
+ * The last wait() should report normal _exit from the
+ * SIGUSR1 handler.
+ */
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+ break;
+ }
+ }
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that, after stopping due to a signal, that signal can be
+ * replaced with another signal.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
+ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ sleep(20);
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Send a signal without ptrace. */
+ ATF_REQUIRE(kill(fpid, SIGINT) == 0);
+
+ /* The second wait() should report a SIGINT was received. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
+ ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
+
+ /* Continue the child process with a different signal. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
+
+ /*
+ * The last wait() should report having died due to the new
+ * signal, SIGTERM.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+/*
+ * Verify that a signal can be passed through to the child even when there
+ * was no true signal originally. Such cases arise when a SIGTRAP is
+ * invented for e.g, system call stops.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report a system call entry for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+
+ /* Continue the child process with a SIGTRAP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
+
+ for (;;) {
+ /*
+ * The last wait() should report exit due to SIGTRAP. In the
+ * meantime, catch and proceed past any syscall stops.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ } else {
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
+ break;
+ }
+ }
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+
+}
+
+/*
+ * A mixed bag PT_CONTINUE with signal test.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status;
+
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ getpid();
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP and tracing system calls. */
+ ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
+
+ /* The second wait() should report a system call entry for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
+
+ /* Continue with the first SIGUSR1. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ /* The next wait() should report a system call exit for getpid(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
+
+ /* Send an ABRT without ptrace. */
+ ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
+
+ /* Continue normally. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next wait() should report the SIGABRT. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
+ ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
+
+ /* Continue, replacing the SIGABRT with another SIGUSR1. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ for (;;) {
+ /*
+ * The last wait() should report exit 2, i.e., a normal _exit
+ * from the signal handler. In the meantime, catch and proceed
+ * past any syscall stops.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ } else {
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 2);
+ break;
+ }
+ }
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+
+}
+
+/*
+ * Verify a signal delivered by ptrace is noticed by kevent(2).
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
+{
+ pid_t fpid, wpid;
+ int status, kq, nevents;
+ struct kevent kev;
+
+ ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ CHILD_REQUIRE((kq = kqueue()) > 0);
+ EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
+ CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
+
+ trace_me();
+
+ for (;;) {
+ nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
+ if (nevents == -1 && errno == EINTR)
+ continue;
+ CHILD_REQUIRE(nevents > 0);
+ CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
+ CHILD_REQUIRE(kev.ident == SIGUSR1);
+ break;
+ }
+
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue with the SIGUSR1. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ /*
+ * The last wait() should report normal exit with code 1.
+ */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static sem_t sigusr1_sem;
+
+static void
+sigusr1_sempost_handler(int sig __unused)
+{
+
+ CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
+}
+
+static void *
+signal_thread(void *arg)
+{
+ int err;
+ sigset_t sigmask;
+
+ pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
+
+ /* Wait for this thread to receive a SIGUSR1. */
+ do {
+ err = sem_wait(&sigusr1_sem);
+ CHILD_REQUIRE(err == 0 || errno == EINTR);
+ } while (err != 0 && errno == EINTR);
+
+ /* Free our companion thread from the barrier. */
+ pthread_barrier_wait(pbarrier);
+
+ /*
+ * Swap ignore duties; the next SIGUSR1 should go to the
+ * other thread.
+ */
+ CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
+ CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
+ CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
+
+ /* Sync up threads after swapping signal masks. */
+ pthread_barrier_wait(pbarrier);
+
+ /* Wait until our companion has received its SIGUSR1. */
+ pthread_barrier_wait(pbarrier);
+
+ return (NULL);
+}
+
+/*
+ * Verify that if ptrace stops due to a signal but continues with
+ * a different signal that the new signal is routed to a thread
+ * that can accept it, and that that thread is awakened by the signal
+ * in a timely manner.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
+ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
+{
+ pid_t fpid, wpid;
+ int status, err;
+ pthread_t t;
+ sigset_t sigmask;
+ pthread_barrier_t barrier;
+
+ ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
+ ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
+ ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
+
+ /* The other thread should receive the first SIGUSR1. */
+ CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
+ CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
+ CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
+
+ trace_me();
+
+ /* Wait until other thread has received its SIGUSR1. */
+ pthread_barrier_wait(&barrier);
+
+ /*
+ * Swap ignore duties; the next SIGUSR1 should go to this
+ * thread.
+ */
+ CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
+
+ /* Sync up threads after swapping signal masks. */
+ pthread_barrier_wait(&barrier);
+
+ /*
+ * Sync up with test code; we're ready for the next SIGUSR1
+ * now.
+ */
+ raise(SIGSTOP);
+
+ /* Wait for this thread to receive a SIGUSR1. */
+ do {
+ err = sem_wait(&sigusr1_sem);
+ CHILD_REQUIRE(err == 0 || errno == EINTR);
+ } while (err != 0 && errno == EINTR);
+
+ /* Free the other thread from the barrier. */
+ pthread_barrier_wait(&barrier);
+
+ CHILD_REQUIRE(pthread_join(t, NULL) == 0);
+
+ exit(1);
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /*
+ * Send a signal without ptrace that either thread will accept (USR2,
+ * in this case).
+ */
+ ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
+
+ /* The second wait() should report a SIGUSR2 was received. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
+
+ /* Continue the child, changing the signal to USR1. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ /* The next wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
+
+ /* The next wait() should report a SIGUSR2 was received. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
+
+ /* Continue the child, changing the signal to USR1. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
+
+ /* The last wait() should report normal exit with code 1. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+static void *
+raise_sigstop_thread(void *arg __unused)
+{
+
+ raise(SIGSTOP);
+ return NULL;
+}
+
+static void *
+sleep_thread(void *arg __unused)
+{
+
+ sleep(60);
+ return NULL;
+}
+
+static void
+terminate_with_pending_sigstop(bool sigstop_from_main_thread)
+{
+ pid_t fpid, wpid;
+ int status, i;
+ cpuset_t setmask;
+ cpusetid_t setid;
+ pthread_t t;
+
+ /*
+ * Become the reaper for this process tree. We need to be able to check
+ * that both child and grandchild have died.
+ */
+ ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
+
+ fpid = fork();
+ ATF_REQUIRE(fpid >= 0);
+ if (fpid == 0) {
+ fpid = fork();
+ CHILD_REQUIRE(fpid >= 0);
+ if (fpid == 0) {
+ trace_me();
+
+ /* Pin to CPU 0 to serialize thread execution. */
+ CPU_ZERO(&setmask);
+ CPU_SET(0, &setmask);
+ CHILD_REQUIRE(cpuset(&setid) == 0);
+ CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
+ CPU_WHICH_CPUSET, setid,
+ sizeof(setmask), &setmask) == 0);
+
+ if (sigstop_from_main_thread) {
+ /*
+ * We expect the SIGKILL sent when our parent
+ * dies to be delivered to the new thread.
+ * Raise the SIGSTOP in this thread so the
+ * threads compete.
+ */
+ CHILD_REQUIRE(pthread_create(&t, NULL,
+ sleep_thread, NULL) == 0);
+ raise(SIGSTOP);
+ } else {
+ /*
+ * We expect the SIGKILL to be delivered to
+ * this thread. After creating the new thread,
+ * just get off the CPU so the other thread can
+ * raise the SIGSTOP.
+ */
+ CHILD_REQUIRE(pthread_create(&t, NULL,
+ raise_sigstop_thread, NULL) == 0);
+ sleep(60);
+ }
+
+ exit(0);
+ }
+ /* First stop is trace_me() immediately after fork. */
+ wpid = waitpid(fpid, &status, 0);
+ CHILD_REQUIRE(wpid == fpid);
+ CHILD_REQUIRE(WIFSTOPPED(status));
+ CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Second stop is from the raise(SIGSTOP). */
+ wpid = waitpid(fpid, &status, 0);
+ CHILD_REQUIRE(wpid == fpid);
+ CHILD_REQUIRE(WIFSTOPPED(status));
+ CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /*
+ * Terminate tracing process without detaching. Our child
+ * should be killed.
+ */
+ exit(0);
+ }
+
+ /*
+ * We should get a normal exit from our immediate child and a SIGKILL
+ * exit from our grandchild. The latter case is the interesting one.
+ * Our grandchild should not have stopped due to the SIGSTOP that was
+ * left dangling when its parent died.
+ */
+ for (i = 0; i < 2; ++i) {
+ wpid = wait(&status);
+ if (wpid == fpid)
+ ATF_REQUIRE(WIFEXITED(status) &&
+ WEXITSTATUS(status) == 0);
+ else
+ ATF_REQUIRE(WIFSIGNALED(status) &&
+ WTERMSIG(status) == SIGKILL);
+ }
+}
+
+/*
+ * These two tests ensure that if the tracing process exits without detaching
+ * just after the child received a SIGSTOP, the child is cleanly killed and
+ * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
+ * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
+ * different threads, the SIGKILL must win. There are two variants of this
+ * test, designed to catch the case where the SIGKILL is delivered to the
+ * younger thread (the first test) and the case where the SIGKILL is delivered
+ * to the older thread (the second test). This behavior has changed in the
+ * past, so make no assumption.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop1);
+ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
+{
+
+ terminate_with_pending_sigstop(true);
+}
+ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop2);
+ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
+{
+
+ terminate_with_pending_sigstop(false);
+}
+
+/*
+ * Verify that after ptrace() discards a SIGKILL signal, the event mask
+ * is not modified.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
+ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ int status, event_mask, new_event_mask;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ raise(SIGSTOP);
+ exit(0);
+ }
+
+ /* The first wait() should report the stop from trace_me(). */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Set several unobtrusive event bits. */
+ event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
+ sizeof(event_mask)) == 0);
+
+ /* Send a SIGKILL without using ptrace. */
+ ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next stop should be due to the SIGKILL. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
+ ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
+
+ /* Continue the child ignoring the SIGKILL. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The next wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ /* Check the current event mask. It should not have changed. */
+ new_event_mask = 0;
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
+ sizeof(new_event_mask)) == 0);
+ ATF_REQUIRE(event_mask == new_event_mask);
+
+ /* Continue the child to let it exit. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+
+#if defined(__amd64__) || defined(__i386__)
+/*
+ * Only x86 both define breakpoint() and have a PC after breakpoint so
+ * that restarting doesn't retrigger the breakpoint.
+ */
+static void *
+continue_thread(void *arg __unused)
+{
+ breakpoint();
+ return (NULL);
+}
+
+static __dead2 void
+continue_thread_main(void)
+{
+ pthread_t threads[2];
+
+ CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
+ NULL) == 0);
+ CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
+ NULL) == 0);
+ CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
+ CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
+ exit(1);
+}
+
+/*
+ * Ensure that PT_CONTINUE clears the status of the thread that
+ * triggered the stop even if a different thread's LWP was passed to
+ * PT_CONTINUE.
+ */
+ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
+ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
+{
+ struct ptrace_lwpinfo pl;
+ pid_t fpid, wpid;
+ lwpid_t lwps[2];
+ bool hit_break[2];
+ int i, j, status;
+
+ ATF_REQUIRE((fpid = fork()) != -1);
+ if (fpid == 0) {
+ trace_me();
+ continue_thread_main();
+ }
+
+ /* The first wait() should report the stop from SIGSTOP. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+
+ ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
+
+ /* Continue the child ignoring the SIGSTOP. */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* One of the new threads should report it's birth. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
+ (PL_FLAG_BORN | PL_FLAG_SCX));
+ lwps[0] = pl.pl_lwpid;
+
+ /*
+ * Suspend this thread to ensure both threads are alive before
+ * hitting the breakpoint.
+ */
+ ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* Second thread should report it's birth. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
+ (PL_FLAG_BORN | PL_FLAG_SCX));
+ ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
+ lwps[1] = pl.pl_lwpid;
+
+ /* Resume both threads waiting for breakpoint events. */
+ hit_break[0] = hit_break[1] = false;
+ ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+
+ /* One thread should report a breakpoint. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
+ ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
+ ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
+ pl.pl_siginfo.si_code == TRAP_BRKPT);
+ if (pl.pl_lwpid == lwps[0])
+ i = 0;
+ else
+ i = 1;
+ hit_break[i] = true;
+
+ /*
+ * Resume both threads but pass the other thread's LWPID to
+ * PT_CONTINUE.
+ */
+ ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
+
+ /*
+ * Will now get two thread exit events and one more breakpoint
+ * event.
+ */
+ for (j = 0; j < 3; j++) {
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(wpid == fpid);
+ ATF_REQUIRE(WIFSTOPPED(status));
+ ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
+ sizeof(pl)) != -1);
+
+ if (pl.pl_lwpid == lwps[0])
+ i = 0;
+ else
+ i = 1;
+
+ ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
+ if (pl.pl_flags & PL_FLAG_EXITED) {
+ ATF_REQUIRE_MSG(hit_break[i],
+ "exited thread did not report breakpoint");
+ lwps[i] = 0;
+ } else {
+ ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
+ ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
+ pl.pl_siginfo.si_code == TRAP_BRKPT);
+ ATF_REQUIRE_MSG(!hit_break[i],
+ "double breakpoint event");
+ hit_break[i] = true;
+ }
+
+ ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
+ }
+
+ /* Both threads should have exited. */
+ ATF_REQUIRE(lwps[0] == 0);
+ ATF_REQUIRE(lwps[1] == 0);
+
+ /* The last event should be for the child process's exit. */
+ wpid = waitpid(fpid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+
+ wpid = wait(&status);
+ ATF_REQUIRE(wpid == -1);
+ ATF_REQUIRE(errno == ECHILD);
+}
+#endif
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
+ ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
+ ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
+ ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
+ ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
+ ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
+ ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
+ ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
+ ATF_TP_ADD_TC(tp,
+ ptrace__follow_fork_child_detached_unrelated_debugger);
+ ATF_TP_ADD_TC(tp,
+ ptrace__follow_fork_parent_detached_unrelated_debugger);
+ ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
+ ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
+ ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
+ ATF_TP_ADD_TC(tp, ptrace__lwp_events);
+ ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
+ ATF_TP_ADD_TC(tp, ptrace__siginfo);
+ ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
+ ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
+ ATF_TP_ADD_TC(tp, ptrace__event_mask);
+ ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
+ ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
+#if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
+#endif
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
+ ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
+ ATF_TP_ADD_TC(tp,
+ ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
+ ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
+ ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
+ ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
+#if defined(__amd64__) || defined(__i386__)
+ ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
+#endif
+
+ return (atf_no_error());
+}
Property changes on: trunk/tests/sys/kern/ptrace_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/reaper.c
===================================================================
--- trunk/tests/sys/kern/reaper.c (rev 0)
+++ trunk/tests/sys/kern/reaper.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,759 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2016 Jilles Tjoelker
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/reaper.c 313303 2017-02-05 21:31:40Z jilles $");
+
+#include <sys/procctl.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+
+static void
+dummy_sighandler(int sig __unused, siginfo_t *info __unused, void *ctx __unused)
+{
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_wait_child_first);
+ATF_TC_BODY(reaper_wait_child_first, tc)
+{
+ pid_t parent, child, grandchild, pid;
+ int status, r;
+ int pip[2];
+
+ /* Be paranoid. */
+ pid = waitpid(-1, NULL, WNOHANG);
+ ATF_REQUIRE(pid == -1 && errno == ECHILD);
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = pipe(pip);
+ ATF_REQUIRE_EQ(0, r);
+
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ if (close(pip[1]) != 0)
+ _exit(100);
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(101);
+ else if (grandchild == 0) {
+ if (read(pip[0], &(uint8_t){ 0 }, 1) != 0)
+ _exit(102);
+ if (getppid() != parent)
+ _exit(103);
+ _exit(2);
+ } else
+ _exit(3);
+ }
+
+ pid = waitpid(child, &status, 0);
+ ATF_REQUIRE_EQ(child, pid);
+ r = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+ ATF_CHECK_EQ(3, r);
+
+ r = close(pip[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ pid = waitpid(-1, &status, 0);
+ ATF_REQUIRE(pid > 0 && pid != child);
+ r = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+ ATF_CHECK_EQ(2, r);
+
+ r = close(pip[0]);
+ ATF_REQUIRE_EQ(0, r);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_wait_grandchild_first);
+ATF_TC_BODY(reaper_wait_grandchild_first, tc)
+{
+ pid_t parent, child, grandchild, pid;
+ int status, r;
+
+ /* Be paranoid. */
+ pid = waitpid(-1, NULL, WNOHANG);
+ ATF_REQUIRE(pid == -1 && errno == ECHILD);
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(101);
+ else if (grandchild == 0)
+ _exit(2);
+ else {
+ if (waitid(P_PID, grandchild, NULL,
+ WNOWAIT | WEXITED) != 0)
+ _exit(102);
+ _exit(3);
+ }
+ }
+
+ pid = waitpid(child, &status, 0);
+ ATF_REQUIRE_EQ(child, pid);
+ r = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+ ATF_CHECK_EQ(3, r);
+
+ pid = waitpid(-1, &status, 0);
+ ATF_REQUIRE(pid > 0 && pid != child);
+ r = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+ ATF_CHECK_EQ(2, r);
+}
+
+ATF_TC(reaper_sigchld_child_first);
+ATF_TC_HEAD(reaper_sigchld_child_first, tc)
+{
+ atf_tc_set_md_var(tc, "timeout", "2");
+}
+ATF_TC_BODY(reaper_sigchld_child_first, tc)
+{
+ struct sigaction act;
+ sigset_t mask;
+ siginfo_t info;
+ pid_t parent, child, grandchild, pid;
+ int r;
+ int pip[2];
+
+ /* Be paranoid. */
+ pid = waitpid(-1, NULL, WNOHANG);
+ ATF_REQUIRE(pid == -1 && errno == ECHILD);
+
+ act.sa_sigaction = dummy_sighandler;
+ act.sa_flags = SA_SIGINFO | SA_RESTART;
+ r = sigemptyset(&act.sa_mask);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigaction(SIGCHLD, &act, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = sigemptyset(&mask);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigaddset(&mask, SIGCHLD);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigprocmask(SIG_BLOCK, &mask, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = pipe(pip);
+ ATF_REQUIRE_EQ(0, r);
+
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ if (close(pip[1]) != 0)
+ _exit(100);
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(101);
+ else if (grandchild == 0) {
+ if (read(pip[0], &(uint8_t){ 0 }, 1) != 0)
+ _exit(102);
+ if (getppid() != parent)
+ _exit(103);
+ _exit(2);
+ } else
+ _exit(3);
+ }
+
+ r = sigwaitinfo(&mask, &info);
+ ATF_REQUIRE_EQ(SIGCHLD, r);
+ ATF_CHECK_EQ(SIGCHLD, info.si_signo);
+ ATF_CHECK_EQ(CLD_EXITED, info.si_code);
+ ATF_CHECK_EQ(3, info.si_status);
+ ATF_CHECK_EQ(child, info.si_pid);
+
+ pid = waitpid(child, NULL, 0);
+ ATF_REQUIRE_EQ(child, pid);
+
+ r = close(pip[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = sigwaitinfo(&mask, &info);
+ ATF_REQUIRE_EQ(SIGCHLD, r);
+ ATF_CHECK_EQ(SIGCHLD, info.si_signo);
+ ATF_CHECK_EQ(CLD_EXITED, info.si_code);
+ ATF_CHECK_EQ(2, info.si_status);
+ grandchild = info.si_pid;
+ ATF_REQUIRE(grandchild > 0);
+ ATF_REQUIRE(grandchild != parent);
+ ATF_REQUIRE(grandchild != child);
+
+ pid = waitpid(-1, NULL, 0);
+ ATF_REQUIRE_EQ(grandchild, pid);
+
+ r = close(pip[0]);
+ ATF_REQUIRE_EQ(0, r);
+}
+
+ATF_TC(reaper_sigchld_grandchild_first);
+ATF_TC_HEAD(reaper_sigchld_grandchild_first, tc)
+{
+ atf_tc_set_md_var(tc, "timeout", "2");
+}
+ATF_TC_BODY(reaper_sigchld_grandchild_first, tc)
+{
+ struct sigaction act;
+ sigset_t mask;
+ siginfo_t info;
+ pid_t parent, child, grandchild, pid;
+ int r;
+
+ /* Be paranoid. */
+ pid = waitpid(-1, NULL, WNOHANG);
+ ATF_REQUIRE(pid == -1 && errno == ECHILD);
+
+ act.sa_sigaction = dummy_sighandler;
+ act.sa_flags = SA_SIGINFO | SA_RESTART;
+ r = sigemptyset(&act.sa_mask);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigaction(SIGCHLD, &act, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = sigemptyset(&mask);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigaddset(&mask, SIGCHLD);
+ ATF_REQUIRE_EQ(0, r);
+ r = sigprocmask(SIG_BLOCK, &mask, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(101);
+ else if (grandchild == 0)
+ _exit(2);
+ else {
+ if (waitid(P_PID, grandchild, NULL,
+ WNOWAIT | WEXITED) != 0)
+ _exit(102);
+ _exit(3);
+ }
+ }
+
+ pid = waitpid(child, NULL, 0);
+ ATF_REQUIRE_EQ(child, pid);
+
+ r = sigwaitinfo(&mask, &info);
+ ATF_REQUIRE_EQ(SIGCHLD, r);
+ ATF_CHECK_EQ(SIGCHLD, info.si_signo);
+ ATF_CHECK_EQ(CLD_EXITED, info.si_code);
+ ATF_CHECK_EQ(2, info.si_status);
+ grandchild = info.si_pid;
+ ATF_REQUIRE(grandchild > 0);
+ ATF_REQUIRE(grandchild != parent);
+ ATF_REQUIRE(grandchild != child);
+
+ pid = waitpid(-1, NULL, 0);
+ ATF_REQUIRE_EQ(grandchild, pid);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_status);
+ATF_TC_BODY(reaper_status, tc)
+{
+ struct procctl_reaper_status st;
+ ssize_t sr;
+ pid_t parent, child, pid;
+ int r, status;
+ int pip[2];
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_STATUS, &st);
+ ATF_REQUIRE_EQ(0, r);
+ ATF_CHECK_EQ(0, st.rs_flags & REAPER_STATUS_OWNED);
+ ATF_CHECK(st.rs_children > 0);
+ ATF_CHECK(st.rs_descendants > 0);
+ ATF_CHECK(st.rs_descendants >= st.rs_children);
+ ATF_CHECK(st.rs_reaper != parent);
+ ATF_CHECK(st.rs_reaper > 0);
+
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = procctl(P_PID, parent, PROC_REAP_STATUS, &st);
+ ATF_REQUIRE_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_STATUS_OWNED,
+ st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT));
+ ATF_CHECK_EQ(0, st.rs_children);
+ ATF_CHECK_EQ(0, st.rs_descendants);
+ ATF_CHECK(st.rs_reaper == parent);
+ ATF_CHECK_EQ(-1, st.rs_pid);
+
+ r = pipe(pip);
+ ATF_REQUIRE_EQ(0, r);
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ if (close(pip[0]) != 0)
+ _exit(100);
+ if (procctl(P_PID, parent, PROC_REAP_STATUS, &st) != 0)
+ _exit(101);
+ if (write(pip[1], &st, sizeof(st)) != (ssize_t)sizeof(st))
+ _exit(102);
+ if (procctl(P_PID, getpid(), PROC_REAP_STATUS, &st) != 0)
+ _exit(103);
+ if (write(pip[1], &st, sizeof(st)) != (ssize_t)sizeof(st))
+ _exit(104);
+ _exit(0);
+ }
+ r = close(pip[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ sr = read(pip[0], &st, sizeof(st));
+ ATF_REQUIRE_EQ((ssize_t)sizeof(st), sr);
+ ATF_CHECK_EQ(REAPER_STATUS_OWNED,
+ st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT));
+ ATF_CHECK_EQ(1, st.rs_children);
+ ATF_CHECK_EQ(1, st.rs_descendants);
+ ATF_CHECK(st.rs_reaper == parent);
+ ATF_CHECK_EQ(child, st.rs_pid);
+ sr = read(pip[0], &st, sizeof(st));
+ ATF_REQUIRE_EQ((ssize_t)sizeof(st), sr);
+ ATF_CHECK_EQ(0,
+ st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT));
+ ATF_CHECK_EQ(1, st.rs_children);
+ ATF_CHECK_EQ(1, st.rs_descendants);
+ ATF_CHECK(st.rs_reaper == parent);
+ ATF_CHECK_EQ(child, st.rs_pid);
+
+ r = close(pip[0]);
+ ATF_REQUIRE_EQ(0, r);
+ pid = waitpid(child, &status, 0);
+ ATF_REQUIRE_EQ(child, pid);
+ ATF_CHECK_EQ(0, status);
+
+ r = procctl(P_PID, parent, PROC_REAP_STATUS, &st);
+ ATF_REQUIRE_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_STATUS_OWNED,
+ st.rs_flags & (REAPER_STATUS_OWNED | REAPER_STATUS_REALINIT));
+ ATF_CHECK_EQ(0, st.rs_children);
+ ATF_CHECK_EQ(0, st.rs_descendants);
+ ATF_CHECK(st.rs_reaper == parent);
+ ATF_CHECK_EQ(-1, st.rs_pid);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_getpids);
+ATF_TC_BODY(reaper_getpids, tc)
+{
+ struct procctl_reaper_pidinfo info[10];
+ ssize_t sr;
+ pid_t parent, child, grandchild, pid;
+ int r, status, childidx;
+ int pipa[2], pipb[2];
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(0, info[0].pi_flags & REAPER_PIDINFO_VALID);
+
+ r = pipe(pipa);
+ ATF_REQUIRE_EQ(0, r);
+ r = pipe(pipb);
+ ATF_REQUIRE_EQ(0, r);
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ if (close(pipa[1]) != 0)
+ _exit(100);
+ if (close(pipb[0]) != 0)
+ _exit(100);
+ if (read(pipa[0], &(uint8_t){ 0 }, 1) != 1)
+ _exit(101);
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(102);
+ if (grandchild == 0) {
+ if (write(pipb[1], &(uint8_t){ 0 }, 1) != 1)
+ _exit(103);
+ if (read(pipa[0], &(uint8_t){ 0 }, 1) != 1)
+ _exit(104);
+ _exit(0);
+ }
+ for (;;)
+ pause();
+ }
+ r = close(pipa[0]);
+ ATF_REQUIRE_EQ(0, r);
+ r = close(pipb[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_PIDINFO_VALID | REAPER_PIDINFO_CHILD,
+ info[0].pi_flags & (REAPER_PIDINFO_VALID | REAPER_PIDINFO_CHILD));
+ ATF_CHECK_EQ(child, info[0].pi_pid);
+ ATF_CHECK_EQ(child, info[0].pi_subtree);
+ ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID);
+
+ sr = write(pipa[1], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+ sr = read(pipb[0], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_PIDINFO_VALID,
+ info[0].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(REAPER_PIDINFO_VALID,
+ info[1].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(0, info[2].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(child, info[0].pi_subtree);
+ ATF_CHECK_EQ(child, info[1].pi_subtree);
+ childidx = info[1].pi_pid == child ? 1 : 0;
+ ATF_CHECK_EQ(REAPER_PIDINFO_CHILD,
+ info[childidx].pi_flags & REAPER_PIDINFO_CHILD);
+ ATF_CHECK_EQ(0, info[childidx ^ 1].pi_flags & REAPER_PIDINFO_CHILD);
+ ATF_CHECK(info[childidx].pi_pid == child);
+ grandchild = info[childidx ^ 1].pi_pid;
+ ATF_CHECK(grandchild > 0);
+ ATF_CHECK(grandchild != child);
+ ATF_CHECK(grandchild != parent);
+
+ r = kill(child, SIGTERM);
+ ATF_REQUIRE_EQ(0, r);
+
+ pid = waitpid(child, &status, 0);
+ ATF_REQUIRE_EQ(child, pid);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_PIDINFO_VALID,
+ info[0].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(child, info[0].pi_subtree);
+ ATF_CHECK_EQ(REAPER_PIDINFO_CHILD,
+ info[0].pi_flags & REAPER_PIDINFO_CHILD);
+ ATF_CHECK_EQ(grandchild, info[0].pi_pid);
+
+ sr = write(pipa[1], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(REAPER_PIDINFO_VALID,
+ info[0].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(0, info[1].pi_flags & REAPER_PIDINFO_VALID);
+ ATF_CHECK_EQ(child, info[0].pi_subtree);
+ ATF_CHECK_EQ(REAPER_PIDINFO_CHILD,
+ info[0].pi_flags & REAPER_PIDINFO_CHILD);
+ ATF_CHECK_EQ(grandchild, info[0].pi_pid);
+
+ pid = waitpid(grandchild, &status, 0);
+ ATF_REQUIRE_EQ(grandchild, pid);
+ ATF_CHECK_EQ(0, status);
+
+ memset(info, '\0', sizeof(info));
+ r = procctl(P_PID, parent, PROC_REAP_GETPIDS,
+ &(struct procctl_reaper_pids){
+ .rp_count = sizeof(info) / sizeof(info[0]),
+ .rp_pids = info
+ });
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(0, info[0].pi_flags & REAPER_PIDINFO_VALID);
+
+ r = close(pipa[1]);
+ ATF_REQUIRE_EQ(0, r);
+ r = close(pipb[0]);
+ ATF_REQUIRE_EQ(0, r);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_kill_badsig);
+ATF_TC_BODY(reaper_kill_badsig, tc)
+{
+ struct procctl_reaper_kill params;
+ pid_t parent;
+ int r;
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ params.rk_sig = -1;
+ params.rk_flags = 0;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_CHECK(r == -1 && errno == EINVAL);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_kill_sigzero);
+ATF_TC_BODY(reaper_kill_sigzero, tc)
+{
+ struct procctl_reaper_kill params;
+ pid_t parent;
+ int r;
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ params.rk_sig = 0;
+ params.rk_flags = 0;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_CHECK(r == -1 && errno == EINVAL);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_kill_empty);
+ATF_TC_BODY(reaper_kill_empty, tc)
+{
+ struct procctl_reaper_kill params;
+ pid_t parent;
+ int r;
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ params.rk_sig = SIGTERM;
+ params.rk_flags = 0;
+ params.rk_killed = 77;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_CHECK(r == -1 && errno == ESRCH);
+ ATF_CHECK_EQ(0, params.rk_killed);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_kill_normal);
+ATF_TC_BODY(reaper_kill_normal, tc)
+{
+ struct procctl_reaper_kill params;
+ ssize_t sr;
+ pid_t parent, child, grandchild, pid;
+ int r, status;
+ int pip[2];
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = pipe(pip);
+ ATF_REQUIRE_EQ(0, r);
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ if (close(pip[0]) != 0)
+ _exit(100);
+ grandchild = fork();
+ if (grandchild == -1)
+ _exit(101);
+ if (grandchild == 0) {
+ if (write(pip[1], &(uint8_t){ 0 }, 1) != 1)
+ _exit(102);
+ for (;;)
+ pause();
+ }
+ for (;;)
+ pause();
+ }
+ r = close(pip[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ sr = read(pip[0], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+
+ params.rk_sig = SIGTERM;
+ params.rk_flags = 0;
+ params.rk_killed = 77;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(2, params.rk_killed);
+
+ pid = waitpid(child, &status, 0);
+ ATF_REQUIRE_EQ(child, pid);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
+
+ pid = waitpid(-1, &status, 0);
+ ATF_REQUIRE(pid > 0);
+ ATF_CHECK(pid != parent);
+ ATF_CHECK(pid != child);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
+
+ r = close(pip[0]);
+ ATF_REQUIRE_EQ(0, r);
+}
+
+ATF_TC_WITHOUT_HEAD(reaper_kill_subtree);
+ATF_TC_BODY(reaper_kill_subtree, tc)
+{
+ struct procctl_reaper_kill params;
+ ssize_t sr;
+ pid_t parent, child1, child2, grandchild1, grandchild2, pid;
+ int r, status;
+ int pip[2];
+
+ parent = getpid();
+ r = procctl(P_PID, parent, PROC_REAP_ACQUIRE, NULL);
+ ATF_REQUIRE_EQ(0, r);
+
+ r = pipe(pip);
+ ATF_REQUIRE_EQ(0, r);
+ child1 = fork();
+ ATF_REQUIRE(child1 != -1);
+ if (child1 == 0) {
+ if (close(pip[0]) != 0)
+ _exit(100);
+ grandchild1 = fork();
+ if (grandchild1 == -1)
+ _exit(101);
+ if (grandchild1 == 0) {
+ if (write(pip[1], &(uint8_t){ 0 }, 1) != 1)
+ _exit(102);
+ for (;;)
+ pause();
+ }
+ for (;;)
+ pause();
+ }
+ child2 = fork();
+ ATF_REQUIRE(child2 != -1);
+ if (child2 == 0) {
+ if (close(pip[0]) != 0)
+ _exit(100);
+ grandchild2 = fork();
+ if (grandchild2 == -1)
+ _exit(101);
+ if (grandchild2 == 0) {
+ if (write(pip[1], &(uint8_t){ 0 }, 1) != 1)
+ _exit(102);
+ for (;;)
+ pause();
+ }
+ for (;;)
+ pause();
+ }
+ r = close(pip[1]);
+ ATF_REQUIRE_EQ(0, r);
+
+ sr = read(pip[0], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+ sr = read(pip[0], &(uint8_t){ 0 }, 1);
+ ATF_REQUIRE_EQ(1, sr);
+
+ params.rk_sig = SIGUSR1;
+ params.rk_flags = REAPER_KILL_SUBTREE;
+ params.rk_subtree = child1;
+ params.rk_killed = 77;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_REQUIRE_EQ(0, r);
+ ATF_REQUIRE_EQ(2, params.rk_killed);
+ ATF_CHECK_EQ(-1, params.rk_fpid);
+
+ pid = waitpid(child1, &status, 0);
+ ATF_REQUIRE_EQ(child1, pid);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1);
+
+ pid = waitpid(-1, &status, 0);
+ ATF_REQUIRE(pid > 0);
+ ATF_CHECK(pid != parent);
+ ATF_CHECK(pid != child1);
+ ATF_CHECK(pid != child2);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1);
+
+ params.rk_sig = SIGUSR2;
+ params.rk_flags = REAPER_KILL_SUBTREE;
+ params.rk_subtree = child2;
+ params.rk_killed = 77;
+ r = procctl(P_PID, parent, PROC_REAP_KILL, ¶ms);
+ ATF_REQUIRE_EQ(0, r);
+ ATF_REQUIRE_EQ(2, params.rk_killed);
+ ATF_CHECK_EQ(-1, params.rk_fpid);
+
+ pid = waitpid(child2, &status, 0);
+ ATF_REQUIRE_EQ(child2, pid);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR2);
+
+ pid = waitpid(-1, &status, 0);
+ ATF_REQUIRE(pid > 0);
+ ATF_CHECK(pid != parent);
+ ATF_CHECK(pid != child1);
+ ATF_CHECK(pid != child2);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR2);
+
+ r = close(pip[0]);
+ ATF_REQUIRE_EQ(0, r);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, reaper_wait_child_first);
+ ATF_TP_ADD_TC(tp, reaper_wait_grandchild_first);
+ ATF_TP_ADD_TC(tp, reaper_sigchld_child_first);
+ ATF_TP_ADD_TC(tp, reaper_sigchld_grandchild_first);
+ ATF_TP_ADD_TC(tp, reaper_status);
+ ATF_TP_ADD_TC(tp, reaper_getpids);
+ ATF_TP_ADD_TC(tp, reaper_kill_badsig);
+ ATF_TP_ADD_TC(tp, reaper_kill_sigzero);
+ ATF_TP_ADD_TC(tp, reaper_kill_empty);
+ ATF_TP_ADD_TC(tp, reaper_kill_normal);
+ ATF_TP_ADD_TC(tp, reaper_kill_subtree);
+ return (atf_no_error());
+}
Property changes on: trunk/tests/sys/kern/reaper.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/unix_passfd_test.c
===================================================================
--- trunk/tests/sys/kern/unix_passfd_test.c (rev 0)
+++ trunk/tests/sys/kern/unix_passfd_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,397 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2005 Robert N. M. Watson
+ * Copyright (c) 2015 Mark Johnston
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/tests/sys/kern/unix_passfd_test.c 293272 2016-01-06 20:29:15Z ngie $
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/un.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+/*
+ * UNIX domain sockets allow file descriptors to be passed via "ancillary
+ * data", or control messages. This regression test is intended to exercise
+ * this facility, both performing some basic tests that it operates, and also
+ * causing some kernel edge cases to execute, such as garbage collection when
+ * there are cyclic file descriptor references. Right now we test only with
+ * stream sockets, but ideally we'd also test with datagram sockets.
+ */
+
+static void
+domainsocketpair(int *fdp)
+{
+
+ ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
+ "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
+}
+
+static void
+closesocketpair(int *fdp)
+{
+
+ close(fdp[0]);
+ close(fdp[1]);
+}
+
+static void
+devnull(int *fdp)
+{
+ int fd;
+
+ fd = open("/dev/null", O_RDONLY);
+ ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
+ *fdp = fd;
+}
+
+static void
+tempfile(int *fdp)
+{
+ char path[PATH_MAX];
+ int fd;
+
+ snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
+ getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
+ fd = mkstemp(path);
+ ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
+ (void)unlink(path);
+ *fdp = fd;
+}
+
+static void
+dofstat(int fd, struct stat *sb)
+{
+
+ ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
+ "fstat failed: %s", strerror(errno));
+}
+
+static void
+samefile(struct stat *sb1, struct stat *sb2)
+{
+
+ ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
+ ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
+}
+
+static void
+sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
+{
+ struct iovec iovec;
+ char message[CMSG_SPACE(sizeof(int))];
+ struct cmsghdr *cmsghdr;
+ struct msghdr msghdr;
+ ssize_t len;
+
+ bzero(&msghdr, sizeof(msghdr));
+ bzero(&message, sizeof(message));
+
+ msghdr.msg_control = message;
+ msghdr.msg_controllen = sizeof(message);
+
+ iovec.iov_base = payload;
+ iovec.iov_len = paylen;
+
+ msghdr.msg_iov = &iovec;
+ msghdr.msg_iovlen = 1;
+
+ cmsghdr = (struct cmsghdr *)(void*)message;
+ cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
+ cmsghdr->cmsg_level = SOL_SOCKET;
+ cmsghdr->cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(cmsghdr), &send_fd, sizeof(int));
+
+ len = sendmsg(sockfd, &msghdr, 0);
+ ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
+ ATF_REQUIRE_MSG((size_t)len == paylen,
+ "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
+ strerror(errno));
+}
+
+static void
+sendfd(int sockfd, int send_fd)
+{
+ char ch = 0;
+
+ return (sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)));
+}
+
+static void
+recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen)
+{
+ struct cmsghdr *cmsghdr;
+ char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
+ struct msghdr msghdr;
+ struct iovec iovec;
+ ssize_t len;
+
+ bzero(&msghdr, sizeof(msghdr));
+
+ msghdr.msg_control = message;
+ msghdr.msg_controllen = sizeof(message);
+
+ iovec.iov_base = buf;
+ iovec.iov_len = buflen;
+
+ msghdr.msg_iov = &iovec;
+ msghdr.msg_iovlen = 1;
+
+ len = recvmsg(sockfd, &msghdr, 0);
+ ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
+ ATF_REQUIRE_MSG((size_t)len == buflen,
+ "recvmsg: %zd bytes received; expected %zd", len, buflen);
+
+ cmsghdr = CMSG_FIRSTHDR(&msghdr);
+ ATF_REQUIRE_MSG(cmsghdr != NULL,
+ "recvmsg: did not receive control message");
+ *recv_fd = -1;
+ for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
+ if (cmsghdr->cmsg_level == SOL_SOCKET &&
+ cmsghdr->cmsg_type == SCM_RIGHTS &&
+ cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
+ memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
+ ATF_REQUIRE(*recv_fd != -1);
+ }
+ }
+ ATF_REQUIRE_MSG(*recv_fd != -1,
+ "recvmsg: did not receive single-fd message");
+}
+
+static void
+recvfd(int sockfd, int *recv_fd)
+{
+ char ch = 0;
+
+ return (recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)));
+}
+
+/*
+ * Put a temporary file into a UNIX domain socket, then take it out and make
+ * sure it's the same file. First time around, don't close the reference
+ * after sending.
+ */
+ATF_TC_WITHOUT_HEAD(simple_send_fd);
+ATF_TC_BODY(simple_send_fd, tc)
+{
+ struct stat getfd_stat, putfd_stat;
+ int fd[2], getfd, putfd;
+
+ domainsocketpair(fd);
+ tempfile(&putfd);
+ dofstat(putfd, &putfd_stat);
+ sendfd(fd[0], putfd);
+ recvfd(fd[1], &getfd);
+ dofstat(getfd, &getfd_stat);
+ samefile(&putfd_stat, &getfd_stat);
+ close(putfd);
+ close(getfd);
+ closesocketpair(fd);
+}
+
+/*
+ * Same as simple_send_fd, only close the file reference after sending, so that
+ * the only reference is the descriptor in the UNIX domain socket buffer.
+ */
+ATF_TC_WITHOUT_HEAD(send_and_close);
+ATF_TC_BODY(send_and_close, tc)
+{
+ struct stat getfd_stat, putfd_stat;
+ int fd[2], getfd, putfd;
+
+ domainsocketpair(fd);
+ tempfile(&putfd);
+ dofstat(putfd, &putfd_stat);
+ sendfd(fd[0], putfd);
+ close(putfd);
+ recvfd(fd[1], &getfd);
+ dofstat(getfd, &getfd_stat);
+ samefile(&putfd_stat, &getfd_stat);
+ close(getfd);
+ closesocketpair(fd);
+}
+
+/*
+ * Put a temporary file into a UNIX domain socket, then close both endpoints
+ * causing garbage collection to kick off.
+ */
+ATF_TC_WITHOUT_HEAD(send_and_cancel);
+ATF_TC_BODY(send_and_cancel, tc)
+{
+ int fd[2], putfd;
+
+ domainsocketpair(fd);
+ tempfile(&putfd);
+ sendfd(fd[0], putfd);
+ close(putfd);
+ closesocketpair(fd);
+}
+
+/*
+ * Send two files. Then receive them. Make sure they are returned in the
+ * right order, and both get there.
+ */
+ATF_TC_WITHOUT_HEAD(two_files);
+ATF_TC_BODY(two_files, tc)
+{
+ struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
+ int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
+
+ domainsocketpair(fd);
+ tempfile(&putfd_1);
+ tempfile(&putfd_2);
+ dofstat(putfd_1, &putfd_1_stat);
+ dofstat(putfd_2, &putfd_2_stat);
+ sendfd(fd[0], putfd_1);
+ sendfd(fd[0], putfd_2);
+ close(putfd_1);
+ close(putfd_2);
+ recvfd(fd[1], &getfd_1);
+ recvfd(fd[1], &getfd_2);
+ dofstat(getfd_1, &getfd_1_stat);
+ dofstat(getfd_2, &getfd_2_stat);
+ samefile(&putfd_1_stat, &getfd_1_stat);
+ samefile(&putfd_2_stat, &getfd_2_stat);
+ close(getfd_1);
+ close(getfd_2);
+ closesocketpair(fd);
+}
+
+/*
+ * Big bundling test. Send an endpoint of the UNIX domain socket over itself,
+ * closing the door behind it.
+ */
+ATF_TC_WITHOUT_HEAD(bundle);
+ATF_TC_BODY(bundle, tc)
+{
+ int fd[2], getfd;
+
+ domainsocketpair(fd);
+
+ sendfd(fd[0], fd[0]);
+ close(fd[0]);
+ recvfd(fd[1], &getfd);
+ close(getfd);
+ close(fd[1]);
+}
+
+/*
+ * Big bundling test part two: Send an endpoint of the UNIX domain socket over
+ * itself, close the door behind it, and never remove it from the other end.
+ */
+ATF_TC_WITHOUT_HEAD(bundle_cancel);
+ATF_TC_BODY(bundle_cancel, tc)
+{
+ int fd[2];
+
+ domainsocketpair(fd);
+ sendfd(fd[0], fd[0]);
+ sendfd(fd[1], fd[0]);
+ closesocketpair(fd);
+}
+
+/*
+ * Test for PR 151758: Send an character device over the UNIX domain socket
+ * and then close both sockets to orphan the device.
+ */
+ATF_TC_WITHOUT_HEAD(devfs_orphan);
+ATF_TC_BODY(devfs_orphan, tc)
+{
+ int fd[2], putfd;
+
+ domainsocketpair(fd);
+ devnull(&putfd);
+ sendfd(fd[0], putfd);
+ close(putfd);
+ closesocketpair(fd);
+}
+
+#define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace"
+
+/*
+ * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
+ * control message to the data. Sender sends large payload.
+ * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
+ * receives truncated data.
+ */
+ATF_TC_WITHOUT_HEAD(rights_creds_payload);
+ATF_TC_BODY(rights_creds_payload, tc)
+{
+ const int on = 1;
+ u_long sendspace;
+ size_t len;
+ void *buf;
+ int fd[2], getfd, putfd, rc;
+
+ atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
+ "are present with large data");
+
+ len = sizeof(sendspace);
+ rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
+ &len, NULL, 0);
+ ATF_REQUIRE_MSG(rc != -1,
+ "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
+
+ buf = calloc(1, sendspace);
+ ATF_REQUIRE(buf != NULL);
+
+ domainsocketpair(fd);
+ rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
+ ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
+ strerror(errno));
+ tempfile(&putfd);
+ sendfd_payload(fd[0], putfd, buf, sendspace);
+ recvfd_payload(fd[1], &getfd, buf, sendspace);
+ close(putfd);
+ close(getfd);
+ closesocketpair(fd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, simple_send_fd);
+ ATF_TP_ADD_TC(tp, send_and_close);
+ ATF_TP_ADD_TC(tp, send_and_cancel);
+ ATF_TP_ADD_TC(tp, two_files);
+ ATF_TP_ADD_TC(tp, bundle);
+ ATF_TP_ADD_TC(tp, bundle_cancel);
+ ATF_TP_ADD_TC(tp, devfs_orphan);
+ ATF_TP_ADD_TC(tp, rights_creds_payload);
+
+ return (atf_no_error());
+}
Property changes on: trunk/tests/sys/kern/unix_passfd_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/unix_seqpacket_test.c
===================================================================
--- trunk/tests/sys/kern/unix_seqpacket_test.c (rev 0)
+++ trunk/tests/sys/kern/unix_seqpacket_test.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,1142 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2014 Spectra Logic Corporation. All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/unix_seqpacket_test.c 317834 2017-05-05 16:24:35Z brooks $");
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <stdio.h>
+
+#include <atf-c.h>
+
+/*
+ * Helper functions
+ */
+
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
+static void
+do_socketpair(int *sv)
+{
+ int s;
+
+ s = socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sv);
+ ATF_REQUIRE_EQ(0, s);
+ ATF_REQUIRE(sv[0] >= 0);
+ ATF_REQUIRE(sv[1] >= 0);
+ ATF_REQUIRE(sv[0] != sv[1]);
+}
+
+static void
+do_socketpair_nonblocking(int *sv)
+{
+ int s;
+
+ s = socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sv);
+ ATF_REQUIRE_EQ(0, s);
+ ATF_REQUIRE(sv[0] >= 0);
+ ATF_REQUIRE(sv[1] >= 0);
+ ATF_REQUIRE(sv[0] != sv[1]);
+ ATF_REQUIRE(-1 != fcntl(sv[0], F_SETFL, O_NONBLOCK));
+ ATF_REQUIRE(-1 != fcntl(sv[1], F_SETFL, O_NONBLOCK));
+}
+
+/*
+ * Returns a pair of sockets made the hard way: bind, listen, connect & accept
+ * @return const char* The path to the socket
+ */
+static const char*
+mk_pair_of_sockets(int *sv)
+{
+ struct sockaddr_un sun;
+ /* ATF's isolation mechanisms will guarantee uniqueness of this file */
+ const char *path = "sock";
+ int s, err, s2, s1;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+
+ bzero(&sun, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
+ err = bind(s, (struct sockaddr *)&sun, sizeof(sun));
+ err = listen(s, -1);
+ ATF_CHECK_EQ(0, err);
+
+ /* Create the other socket */
+ s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s2 >= 0);
+ err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+ if (err != 0) {
+ perror("connect");
+ atf_tc_fail("connect(2) failed");
+ }
+
+ /* Accept it */
+ s1 = accept(s, NULL, NULL);
+ if (s1 == -1) {
+ perror("accept");
+ atf_tc_fail("accept(2) failed");
+ }
+
+ sv[0] = s1;
+ sv[1] = s2;
+
+ close(s);
+
+ return (path);
+}
+
+static volatile sig_atomic_t got_sigpipe = 0;
+static void
+shutdown_send_sigpipe_handler(int __unused x)
+{
+ got_sigpipe = 1;
+}
+
+/*
+ * Parameterized test function bodies
+ */
+static void
+test_eagain(int sndbufsize, int rcvbufsize)
+{
+ int i;
+ int sv[2];
+ const size_t totalsize = (sndbufsize + rcvbufsize) * 2;
+ const size_t pktsize = MIN(sndbufsize, rcvbufsize) / 4;
+ const int numpkts = totalsize / pktsize;
+ char sndbuf[pktsize];
+ ssize_t ssize;
+
+ /* setup the socket pair */
+ do_socketpair_nonblocking(sv);
+ /* Setup the buffers */
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ bzero(sndbuf, pktsize);
+ /* Send data until we get EAGAIN */
+ for(i=0; i < numpkts; i++) {
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ if (ssize == -1) {
+ if (errno == EAGAIN) {
+ close(sv[0]);
+ close(sv[1]);
+ atf_tc_pass();
+ }
+ else {
+ perror("send");
+ atf_tc_fail("send returned < 0 but not EAGAIN");
+ }
+ }
+ }
+ atf_tc_fail("Never got EAGAIN");
+}
+
+static void
+test_sendrecv_symmetric_buffers(int bufsize, int blocking) {
+ int s;
+ int sv[2];
+ const ssize_t pktsize = bufsize / 2;
+ char sndbuf[pktsize];
+ char recv_buf[pktsize];
+ ssize_t ssize, rsize;
+
+ /* setup the socket pair */
+ if (blocking)
+ do_socketpair(sv);
+ else
+ do_socketpair_nonblocking(sv);
+
+ /* Setup the buffers */
+ s = setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
+ ATF_REQUIRE_EQ(0, s);
+ s = setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
+ ATF_REQUIRE_EQ(0, s);
+
+ /* Fill the send buffer */
+ bzero(sndbuf, pktsize);
+
+ /* send and receive the packet */
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(pktsize, ssize, "expected %zd=send(...) but got %zd",
+ pktsize, ssize);
+
+ rsize = recv(sv[1], recv_buf, pktsize, MSG_WAITALL);
+ if (rsize < 0) {
+ perror("recv");
+ atf_tc_fail("recv returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(pktsize, rsize, "expected %zd=send(...) but got %zd",
+ pktsize, rsize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+static void
+test_pipe_simulator(int sndbufsize, int rcvbufsize)
+{
+ int num_sent, num_received;
+ int sv[2];
+ const ssize_t pktsize = MIN(sndbufsize, rcvbufsize) / 4;
+ int numpkts;
+ char sndbuf[pktsize];
+ char rcvbuf[pktsize];
+ char comparebuf[pktsize];
+ ssize_t ssize, rsize;
+ bool currently_sending = true;
+
+ /* setup the socket pair */
+ do_socketpair_nonblocking(sv);
+ /* Setup the buffers */
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ /* Send a total amount of data comfortably greater than the buffers */
+ numpkts = MAX(sndbufsize, rcvbufsize) * 8 / pktsize;
+ for (num_sent=0, num_received=0;
+ num_sent < numpkts || num_received < numpkts; ) {
+ if (currently_sending && num_sent < numpkts) {
+ /* The simulated sending process */
+ /* fill the buffer */
+ memset(sndbuf, num_sent, pktsize);
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ if (ssize < 0) {
+ /*
+ * XXX: This is bug-compatible with the kernel.
+ * The kernel returns EMSGSIZE when it should
+ * return EAGAIN
+ */
+ if (errno == EAGAIN || errno == EMSGSIZE)
+ currently_sending = false;
+ else {
+ perror("send");
+ atf_tc_fail("send failed");
+ }
+ } else {
+ ATF_CHECK_EQ_MSG(pktsize, ssize,
+ "expected %zd=send(...) but got %zd",
+ pktsize, ssize);
+ num_sent++;
+ }
+ } else {
+ /* The simulated receiving process */
+ rsize = recv(sv[1], rcvbuf, pktsize, MSG_WAITALL);
+ if (rsize < 0) {
+ if (errno == EAGAIN) {
+ currently_sending = true;
+ ATF_REQUIRE_MSG(num_sent < numpkts,
+ "Packets were lost!");
+ }
+ else {
+ perror("recv");
+ atf_tc_fail("recv failed");
+ }
+ } else {
+ ATF_CHECK_EQ_MSG(pktsize, rsize,
+ "expected %zd=recv(...) but got %zd",
+ pktsize, rsize);
+ memset(comparebuf, num_received, pktsize);
+ ATF_CHECK_EQ_MSG(0, memcmp(comparebuf, rcvbuf,
+ pktsize),
+ "Received data miscompare");
+ num_received++;
+ }
+ }
+ }
+ close(sv[0]);
+ close(sv[1]);
+}
+
+typedef struct {
+ ssize_t pktsize;
+ int numpkts;
+ int so;
+} test_pipe_thread_data_t;
+
+static void*
+test_pipe_writer(void* args)
+{
+ test_pipe_thread_data_t* td = args;
+ char sndbuf[td->pktsize];
+ ssize_t ssize;
+ int i;
+
+ for(i=0; i < td->numpkts; i++) {
+ memset(sndbuf, i, td->pktsize);
+ ssize = send(td->so, sndbuf, td->pktsize, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(td->pktsize, ssize,
+ "expected %zd=send(...) but got %zd",
+ td->pktsize, ssize);
+ }
+ return (0);
+}
+
+static void*
+test_pipe_reader(void* args)
+{
+ test_pipe_thread_data_t* td = args;
+ char rcvbuf[td->pktsize];
+ char comparebuf[td->pktsize];
+ ssize_t rsize;
+ int i, d;
+
+ for(i=0; i < td->numpkts; i++) {
+ memset(comparebuf, i, td->pktsize);
+ rsize = recv(td->so, rcvbuf, td->pktsize, MSG_WAITALL);
+ if (rsize < 0) {
+ perror("recv");
+ atf_tc_fail("recv returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(td->pktsize, rsize,
+ "expected %zd=send(...) but got %zd",
+ td->pktsize, rsize);
+ d = memcmp(comparebuf, rcvbuf, td->pktsize);
+ ATF_CHECK_EQ_MSG(0, d,
+ "Received data miscompare on packet %d", i);
+ }
+ return (0);
+}
+
+
+static void
+test_pipe(int sndbufsize, int rcvbufsize)
+{
+ test_pipe_thread_data_t writer_data, reader_data;
+ pthread_t writer, reader;
+ int sv[2];
+ const size_t pktsize = MIN(sndbufsize, rcvbufsize) / 4;
+ int numpkts;
+
+ /* setup the socket pair */
+ do_socketpair(sv);
+ /* Setup the buffers */
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ /* Send a total amount of data comfortably greater than the buffers */
+ numpkts = MAX(sndbufsize, rcvbufsize) * 8 / pktsize;
+
+ /* Start the child threads */
+ writer_data.pktsize = pktsize;
+ writer_data.numpkts = numpkts;
+ writer_data.so = sv[0];
+ reader_data.pktsize = pktsize;
+ reader_data.numpkts = numpkts;
+ reader_data.so = sv[1];
+ ATF_REQUIRE_EQ(0, pthread_create(&writer, NULL, test_pipe_writer,
+ (void*)&writer_data));
+ /*
+ * Give the writer time to start writing, and hopefully block, before
+ * starting the reader. This increases the likelihood of the test case
+ * failing due to PR kern/185812
+ */
+ usleep(1000);
+ ATF_REQUIRE_EQ(0, pthread_create(&reader, NULL, test_pipe_reader,
+ (void*)&reader_data));
+
+ /* Join the children */
+ ATF_REQUIRE_EQ(0, pthread_join(writer, NULL));
+ ATF_REQUIRE_EQ(0, pthread_join(reader, NULL));
+ close(sv[0]);
+ close(sv[1]);
+}
+
+
+/*
+ * Test Cases
+ */
+
+/* Create a SEQPACKET socket */
+ATF_TC_WITHOUT_HEAD(create_socket);
+ATF_TC_BODY(create_socket, tc)
+{
+ int s;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+ close(s);
+}
+
+/* Create SEQPACKET sockets using socketpair(2) */
+ATF_TC_WITHOUT_HEAD(create_socketpair);
+ATF_TC_BODY(create_socketpair, tc)
+{
+ int sv[2];
+ int s;
+
+ s = socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sv);
+ ATF_CHECK_EQ(0, s);
+ ATF_CHECK(sv[0] >= 0);
+ ATF_CHECK(sv[1] >= 0);
+ ATF_CHECK(sv[0] != sv[1]);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/* Call listen(2) without first calling bind(2). It should fail */
+ATF_TC_WITHOUT_HEAD(listen_unbound);
+ATF_TC_BODY(listen_unbound, tc)
+{
+ int s, r;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s > 0);
+ r = listen(s, -1);
+ /* expect listen to fail since we haven't called bind(2) */
+ ATF_CHECK(r != 0);
+ close(s);
+}
+
+/* Bind the socket to a file */
+ATF_TC_WITHOUT_HEAD(bind);
+ATF_TC_BODY(bind, tc)
+{
+ struct sockaddr_un sun;
+ /* ATF's isolation mechanisms will guarantee uniqueness of this file */
+ const char *path = "sock";
+ int s, r;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+
+ bzero(&sun, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
+ r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
+ ATF_CHECK_EQ(0, r);
+ close(s);
+}
+
+/* listen(2) a socket that is already bound(2) should succeed */
+ATF_TC_WITHOUT_HEAD(listen_bound);
+ATF_TC_BODY(listen_bound, tc)
+{
+ struct sockaddr_un sun;
+ /* ATF's isolation mechanisms will guarantee uniqueness of this file */
+ const char *path = "sock";
+ int s, r, l;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+
+ bzero(&sun, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
+ r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
+ l = listen(s, -1);
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(0, l);
+ close(s);
+}
+
+/* connect(2) can make a connection */
+ATF_TC_WITHOUT_HEAD(connect);
+ATF_TC_BODY(connect, tc)
+{
+ struct sockaddr_un sun;
+ /* ATF's isolation mechanisms will guarantee uniqueness of this file */
+ const char *path = "sock";
+ int s, r, err, l, s2;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+
+ bzero(&sun, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
+ r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
+ l = listen(s, -1);
+ ATF_CHECK_EQ(0, r);
+ ATF_CHECK_EQ(0, l);
+
+ /* Create the other socket */
+ s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s2 >= 0);
+ err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+ if (err != 0) {
+ perror("connect");
+ atf_tc_fail("connect(2) failed");
+ }
+ close(s);
+ close(s2);
+}
+
+/* accept(2) can receive a connection */
+ATF_TC_WITHOUT_HEAD(accept);
+ATF_TC_BODY(accept, tc)
+{
+ int sv[2];
+
+ mk_pair_of_sockets(sv);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+
+/* Set O_NONBLOCK on the socket */
+ATF_TC_WITHOUT_HEAD(fcntl_nonblock);
+ATF_TC_BODY(fcntl_nonblock, tc)
+{
+ int s;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+ if (fcntl(s, F_SETFL, O_NONBLOCK) == -1) {
+ perror("fcntl");
+ atf_tc_fail("fcntl failed");
+ }
+ close(s);
+}
+
+/* Resize the send and receive buffers */
+ATF_TC_WITHOUT_HEAD(resize_buffers);
+ATF_TC_BODY(resize_buffers, tc)
+{
+ int s;
+ int sndbuf = 12345;
+ int rcvbuf = 23456;
+ int xs, xr;
+ socklen_t sl = sizeof(xs);
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+
+ printf(" Socket Buffer Sizes\n");
+ printf(" | SNDBUF | RCVBUF |\n");
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_SNDBUF, &xs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_RCVBUF, &xr, &sl));
+ printf("Default | %7d | %7d |\n", xs, xr);
+
+ if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) != 0){
+ perror("setsockopt");
+ atf_tc_fail("setsockopt(SO_SNDBUF) failed");
+ }
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_SNDBUF, &xs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_RCVBUF, &xr, &sl));
+ printf("After changing SNDBUF | %7d | %7d |\n", xs, xr);
+
+ if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) != 0){
+ perror("setsockopt");
+ atf_tc_fail("setsockopt(SO_RCVBUF) failed");
+ }
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_SNDBUF, &xs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(s, SOL_SOCKET, SO_RCVBUF, &xr, &sl));
+ printf("After changing RCVBUF | %7d | %7d |\n", xs, xr);
+ close(s);
+}
+
+/*
+ * Resize the send and receive buffers of a connected socketpair
+ * Print some useful debugging info too
+ */
+ATF_TC_WITHOUT_HEAD(resize_connected_buffers);
+ATF_TC_BODY(resize_connected_buffers, tc)
+{
+ int sv[2];
+ int sndbuf = 12345;
+ int rcvbuf = 23456;
+ int err;
+ int ls, lr, rs, rr;
+ socklen_t sl = sizeof(ls);
+
+ /* setup the socket pair */
+ do_socketpair(sv);
+
+ printf(" Socket Buffer Sizes\n");
+ printf(" | Left Socket | Right Socket |\n");
+ printf(" | SNDBUF | RCVBUF | SNDBUF | RCVBUF |\n");
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &ls, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &lr, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &rs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rr, &sl));
+ printf("Default | %7d | %7d | %7d | %7d |\n",
+ ls, lr, rs, rr);
+
+ /* Update one side's send buffer */
+ err = setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
+ if (err != 0){
+ perror("setsockopt");
+ atf_tc_fail("setsockopt(SO_SNDBUF) failed");
+ }
+
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &ls, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &lr, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &rs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rr, &sl));
+ printf("After changing Left's SNDBUF | %7d | %7d | %7d | %7d |\n",
+ ls, lr, rs, rr);
+
+ /* Update the same side's receive buffer */
+ err = setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
+ if (err != 0){
+ perror("setsockopt");
+ atf_tc_fail("setsockopt(SO_RCVBUF) failed");
+ }
+
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &ls, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &lr, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &rs, &sl));
+ ATF_CHECK_EQ(0, getsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rr, &sl));
+ printf("After changing Left's RCVBUF | %7d | %7d | %7d | %7d |\n",
+ ls, lr, rs, rr);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+
+/* send(2) and recv(2) a single short record */
+ATF_TC_WITHOUT_HEAD(send_recv);
+ATF_TC_BODY(send_recv, tc)
+{
+ int sv[2];
+ const int bufsize = 64;
+ const char *data = "data";
+ char recv_buf[bufsize];
+ ssize_t datalen;
+ ssize_t ssize, rsize;
+
+ /* setup the socket pair */
+ do_socketpair(sv);
+
+ /* send and receive a small packet */
+ datalen = strlen(data) + 1; /* +1 for the null */
+ ssize = send(sv[0], data, datalen, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(datalen, ssize, "expected %zd=send(...) but got %zd",
+ datalen, ssize);
+
+ rsize = recv(sv[1], recv_buf, bufsize, MSG_WAITALL);
+ ATF_CHECK_EQ(datalen, rsize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/* sendto(2) and recvfrom(2) a single short record
+ * According to The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
+ * Edition, sendto(2) is exactly the same as send(2) on a connection-mode socket
+ *
+ * According to the same spec, not all protocols are required to provide the
+ * source addres in recvfrom(2).
+ */
+ATF_TC_WITHOUT_HEAD(sendto_recvfrom);
+ATF_TC_BODY(sendto_recvfrom, tc)
+{
+ const char* path;
+ struct sockaddr_storage from;
+ int sv[2];
+ const int bufsize = 64;
+ const char *data = "data";
+ char recv_buf[bufsize];
+ ssize_t datalen;
+ ssize_t ssize, rsize;
+ socklen_t fromlen;
+
+ /* setup the socket pair */
+ path = mk_pair_of_sockets(sv);
+
+ /* send and receive a small packet */
+ datalen = strlen(data) + 1; /* +1 for the null */
+ ssize = sendto(sv[0], data, datalen, MSG_EOR, NULL, 0);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(datalen, ssize, "expected %zd=send(...) but got %zd",
+ datalen, ssize);
+
+ fromlen = sizeof(from);
+ rsize = recvfrom(sv[1], recv_buf, bufsize, MSG_WAITALL,
+ (struct sockaddr*)&from, &fromlen);
+ if (ssize < 0) {
+ perror("recvfrom");
+ atf_tc_fail("recvfrom returned < 0");
+ }
+ ATF_CHECK_EQ(datalen, rsize);
+
+ /*
+ * FreeBSD does not currently provide the source address for SEQ_PACKET
+ * AF_UNIX sockets, and POSIX does not require it, so these two checks
+ * are disabled. If FreeBSD gains that feature in the future, then
+ * these checks may be reenabled
+ */
+ /* ATF_CHECK_EQ(PF_LOCAL, from.ss_family); */
+ /* ATF_CHECK_STREQ(path, ((struct sockaddr_un*)&from)->sun_path); */
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/*
+ * send(2) and recv(2) a single short record with sockets created the
+ * traditional way, involving bind, listen, connect, and accept
+ */
+ATF_TC_WITHOUT_HEAD(send_recv_with_connect);
+ATF_TC_BODY(send_recv_with_connect, tc)
+{
+ int sv[2];
+ const int bufsize = 64;
+ const char *data = "data";
+ char recv_buf[bufsize];
+ ssize_t datalen;
+ ssize_t ssize, rsize;
+
+ mk_pair_of_sockets(sv);
+
+ /* send and receive a small packet */
+ datalen = strlen(data) + 1; /* +1 for the null */
+ ssize = send(sv[0], data, datalen, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(datalen, ssize, "expected %zd=send(...) but got %zd",
+ datalen, ssize);
+
+ rsize = recv(sv[1], recv_buf, bufsize, MSG_WAITALL);
+ ATF_CHECK_EQ(datalen, rsize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/* send(2) should fail on a shutdown socket */
+ATF_TC_WITHOUT_HEAD(shutdown_send);
+ATF_TC_BODY(shutdown_send, tc)
+{
+ int s;
+ const char data[] = "data";
+ ssize_t ssize;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+ ATF_CHECK_EQ(0, shutdown(s, SHUT_RDWR));
+ /* USE MSG_NOSIGNAL so we don't get SIGPIPE */
+ ssize = send(s, data, sizeof(data), MSG_EOR | MSG_NOSIGNAL);
+ ATF_CHECK_EQ(EPIPE, errno);
+ ATF_CHECK_EQ(-1, ssize);
+ close(s);
+}
+
+/* send(2) should cause SIGPIPE on a shutdown socket */
+ATF_TC_WITHOUT_HEAD(shutdown_send_sigpipe);
+ATF_TC_BODY(shutdown_send_sigpipe, tc)
+{
+ int s;
+ const char data[] = "data";
+ ssize_t ssize;
+
+ s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+ ATF_REQUIRE(s >= 0);
+ ATF_CHECK_EQ(0, shutdown(s, SHUT_RDWR));
+ ATF_REQUIRE(SIG_ERR != signal(SIGPIPE, shutdown_send_sigpipe_handler));
+ ssize = send(s, data, sizeof(data), MSG_EOR);
+ ATF_CHECK_EQ(1, got_sigpipe);
+ close(s);
+}
+
+/* nonblocking send(2) and recv(2) a single short record */
+ATF_TC_WITHOUT_HEAD(send_recv_nonblocking);
+ATF_TC_BODY(send_recv_nonblocking, tc)
+{
+ int sv[2];
+ const int bufsize = 64;
+ const char *data = "data";
+ char recv_buf[bufsize];
+ ssize_t datalen;
+ ssize_t ssize, rsize;
+
+ /* setup the socket pair */
+ do_socketpair_nonblocking(sv);
+
+ /* Verify that there is nothing to receive */
+ rsize = recv(sv[1], recv_buf, bufsize, MSG_WAITALL);
+ ATF_CHECK_EQ(EAGAIN, errno);
+ ATF_CHECK_EQ(-1, rsize);
+
+ /* send and receive a small packet */
+ datalen = strlen(data) + 1; /* +1 for the null */
+ ssize = send(sv[0], data, datalen, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(datalen, ssize, "expected %zd=send(...) but got %zd",
+ datalen, ssize);
+
+ rsize = recv(sv[1], recv_buf, bufsize, MSG_WAITALL);
+ ATF_CHECK_EQ(datalen, rsize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/*
+ * We should get EMSGSIZE if we try to send a message larger than the socket
+ * buffer, with blocking sockets
+ */
+ATF_TC_WITHOUT_HEAD(emsgsize);
+ATF_TC_BODY(emsgsize, tc)
+{
+ int sv[2];
+ const int sndbufsize = 8192;
+ const int rcvbufsize = 8192;
+ const size_t pktsize = (sndbufsize + rcvbufsize) * 2;
+ char sndbuf[pktsize];
+ ssize_t ssize;
+
+ /* setup the socket pair */
+ do_socketpair(sv);
+ /* Setup the buffers */
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ ATF_CHECK_EQ(EMSGSIZE, errno);
+ ATF_CHECK_EQ(-1, ssize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/*
+ * We should get EMSGSIZE if we try to send a message larger than the socket
+ * buffer, with nonblocking sockets
+ */
+ATF_TC_WITHOUT_HEAD(emsgsize_nonblocking);
+ATF_TC_BODY(emsgsize_nonblocking, tc)
+{
+ int sv[2];
+ const int sndbufsize = 8192;
+ const int rcvbufsize = 8192;
+ const size_t pktsize = (sndbufsize + rcvbufsize) * 2;
+ char sndbuf[pktsize];
+ ssize_t ssize;
+
+ /* setup the socket pair */
+ do_socketpair_nonblocking(sv);
+ /* Setup the buffers */
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ ATF_CHECK_EQ(EMSGSIZE, errno);
+ ATF_CHECK_EQ(-1, ssize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+
+/*
+ * We should get EAGAIN if we try to send a message larger than the socket
+ * buffer, with nonblocking sockets. Test with several different sockbuf sizes
+ */
+ATF_TC_WITHOUT_HEAD(eagain_8k_8k);
+ATF_TC_BODY(eagain_8k_8k, tc)
+{
+ test_eagain(8192, 8192);
+}
+ATF_TC_WITHOUT_HEAD(eagain_8k_128k);
+ATF_TC_BODY(eagain_8k_128k, tc)
+{
+ test_eagain(8192, 131072);
+}
+ATF_TC_WITHOUT_HEAD(eagain_128k_8k);
+ATF_TC_BODY(eagain_128k_8k, tc)
+{
+ test_eagain(131072, 8192);
+}
+ATF_TC_WITHOUT_HEAD(eagain_128k_128k);
+ATF_TC_BODY(eagain_128k_128k, tc)
+{
+ test_eagain(131072, 131072);
+}
+
+
+/*
+ * nonblocking send(2) and recv(2) of several records, which should collectively
+ * fill up the send buffer but not the receive buffer
+ */
+ATF_TC_WITHOUT_HEAD(rcvbuf_oversized);
+ATF_TC_BODY(rcvbuf_oversized, tc)
+{
+ int i;
+ int sv[2];
+ const ssize_t pktsize = 1024;
+ const int sndbufsize = 8192;
+ const int rcvbufsize = 131072;
+ const size_t geometric_mean_bufsize = 32768;
+ const int numpkts = geometric_mean_bufsize / pktsize;
+ char sndbuf[pktsize];
+ char recv_buf[pktsize];
+ ssize_t ssize, rsize;
+
+ /* setup the socket pair */
+ do_socketpair_nonblocking(sv);
+ ATF_REQUIRE_EQ(0, setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbufsize,
+ sizeof(sndbufsize)));
+ ATF_REQUIRE_EQ(0, setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
+ sizeof(rcvbufsize)));
+
+ /*
+ * Send and receive packets that are collectively greater than the send
+ * buffer, but less than the receive buffer
+ */
+ for (i=0; i < numpkts; i++) {
+ /* Fill the buffer */
+ memset(sndbuf, i, pktsize);
+
+ /* send the packet */
+ ssize = send(sv[0], sndbuf, pktsize, MSG_EOR);
+ if (ssize < 0) {
+ perror("send");
+ atf_tc_fail("send returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(pktsize, ssize,
+ "expected %zd=send(...) but got %zd", pktsize, ssize);
+
+ /* Receive it */
+
+ rsize = recv(sv[1], recv_buf, pktsize, MSG_WAITALL);
+ if (rsize < 0) {
+ perror("recv");
+ atf_tc_fail("recv returned < 0");
+ }
+ ATF_CHECK_EQ_MSG(pktsize, rsize,
+ "expected %zd=send(...) but got %zd", pktsize, rsize);
+
+ /* Verify the contents */
+ ATF_CHECK_EQ_MSG(0, memcmp(sndbuf, recv_buf, pktsize),
+ "Received data miscompare");
+ }
+
+ /* Trying to receive again should return EAGAIN */
+ rsize = recv(sv[1], recv_buf, pktsize, MSG_WAITALL);
+ ATF_CHECK_EQ(EAGAIN, errno);
+ ATF_CHECK_EQ(-1, rsize);
+ close(sv[0]);
+ close(sv[1]);
+}
+
+/*
+ * Simulate the behavior of a blocking pipe. The sender will send until his
+ * buffer fills up, then we'll simulate a scheduler switch that will allow the
+ * receiver to read until his buffer empties. Repeat the process until the
+ * transfer is complete.
+ * Repeat the test with multiple send and receive buffer sizes
+ */
+ATF_TC_WITHOUT_HEAD(pipe_simulator_8k_8k);
+ATF_TC_BODY(pipe_simulator_8k_8k, tc)
+{
+ test_pipe_simulator(8192, 8192);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_simulator_8k_128k);
+ATF_TC_BODY(pipe_simulator_8k_128k, tc)
+{
+ test_pipe_simulator(8192, 131072);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_simulator_128k_8k);
+ATF_TC_BODY(pipe_simulator_128k_8k, tc)
+{
+ test_pipe_simulator(131072, 8192);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_simulator_128k_128k);
+ATF_TC_BODY(pipe_simulator_128k_128k, tc)
+{
+ test_pipe_simulator(131072, 131072);
+}
+
+/*
+ * Test blocking I/O by passing data between two threads. The total amount of
+ * data will be >> buffer size to force blocking. Repeat the test with multiple
+ * send and receive buffer sizes
+ */
+ATF_TC_WITHOUT_HEAD(pipe_8k_8k);
+ATF_TC_BODY(pipe_8k_8k, tc)
+{
+ test_pipe(8192, 8192);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_8k_128k);
+ATF_TC_BODY(pipe_8k_128k, tc)
+{
+ test_pipe(8192, 131072);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_128k_8k);
+ATF_TC_BODY(pipe_128k_8k, tc)
+{
+ test_pipe(131072, 8192);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_128k_128k);
+ATF_TC_BODY(pipe_128k_128k, tc)
+{
+ test_pipe(131072, 131072);
+}
+
+
+/*
+ * Test single-packet I/O with and without blocking, with symmetric buffers of
+ * various sizes
+ */
+ATF_TC_WITHOUT_HEAD(sendrecv_8k);
+ATF_TC_BODY(sendrecv_8k, tc)
+{
+ test_sendrecv_symmetric_buffers(8 * 1024, true);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_16k);
+ATF_TC_BODY(sendrecv_16k, tc)
+{
+ test_sendrecv_symmetric_buffers(16 * 1024, true);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_32k);
+ATF_TC_BODY(sendrecv_32k, tc)
+{
+ test_sendrecv_symmetric_buffers(32 * 1024, true);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_64k);
+ATF_TC_BODY(sendrecv_64k, tc)
+{
+ test_sendrecv_symmetric_buffers(64 * 1024, true);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_128k);
+ATF_TC_BODY(sendrecv_128k, tc)
+{
+ test_sendrecv_symmetric_buffers(128 * 1024, true);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_8k_nonblocking);
+ATF_TC_BODY(sendrecv_8k_nonblocking, tc)
+{
+ test_sendrecv_symmetric_buffers(8 * 1024, false);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_16k_nonblocking);
+ATF_TC_BODY(sendrecv_16k_nonblocking, tc)
+{
+ test_sendrecv_symmetric_buffers(16 * 1024, false);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_32k_nonblocking);
+ATF_TC_BODY(sendrecv_32k_nonblocking, tc)
+{
+ test_sendrecv_symmetric_buffers(32 * 1024, false);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_64k_nonblocking);
+ATF_TC_BODY(sendrecv_64k_nonblocking, tc)
+{
+ test_sendrecv_symmetric_buffers(64 * 1024, false);
+}
+ATF_TC_WITHOUT_HEAD(sendrecv_128k_nonblocking);
+ATF_TC_BODY(sendrecv_128k_nonblocking, tc)
+{
+ test_sendrecv_symmetric_buffers(128 * 1024, false);
+}
+
+
+/*
+ * Main.
+ */
+
+ATF_TP_ADD_TCS(tp)
+{
+ /* Basic creation and connection tests */
+ ATF_TP_ADD_TC(tp, create_socket);
+ ATF_TP_ADD_TC(tp, create_socketpair);
+ ATF_TP_ADD_TC(tp, listen_unbound);
+ ATF_TP_ADD_TC(tp, bind);
+ ATF_TP_ADD_TC(tp, listen_bound);
+ ATF_TP_ADD_TC(tp, connect);
+ ATF_TP_ADD_TC(tp, accept);
+ ATF_TP_ADD_TC(tp, fcntl_nonblock);
+ ATF_TP_ADD_TC(tp, resize_buffers);
+ ATF_TP_ADD_TC(tp, resize_connected_buffers);
+
+ /* Unthreaded I/O tests */
+ ATF_TP_ADD_TC(tp, send_recv);
+ ATF_TP_ADD_TC(tp, send_recv_nonblocking);
+ ATF_TP_ADD_TC(tp, send_recv_with_connect);
+ ATF_TP_ADD_TC(tp, sendto_recvfrom);
+ ATF_TP_ADD_TC(tp, shutdown_send);
+ ATF_TP_ADD_TC(tp, shutdown_send_sigpipe);
+ ATF_TP_ADD_TC(tp, emsgsize);
+ ATF_TP_ADD_TC(tp, emsgsize_nonblocking);
+ ATF_TP_ADD_TC(tp, eagain_8k_8k);
+ ATF_TP_ADD_TC(tp, eagain_8k_128k);
+ ATF_TP_ADD_TC(tp, eagain_128k_8k);
+ ATF_TP_ADD_TC(tp, eagain_128k_128k);
+ ATF_TP_ADD_TC(tp, sendrecv_8k);
+ ATF_TP_ADD_TC(tp, sendrecv_16k);
+ ATF_TP_ADD_TC(tp, sendrecv_32k);
+ ATF_TP_ADD_TC(tp, sendrecv_64k);
+ ATF_TP_ADD_TC(tp, sendrecv_128k);
+ ATF_TP_ADD_TC(tp, sendrecv_8k_nonblocking);
+ ATF_TP_ADD_TC(tp, sendrecv_16k_nonblocking);
+ ATF_TP_ADD_TC(tp, sendrecv_32k_nonblocking);
+ ATF_TP_ADD_TC(tp, sendrecv_64k_nonblocking);
+ ATF_TP_ADD_TC(tp, sendrecv_128k_nonblocking);
+ ATF_TP_ADD_TC(tp, rcvbuf_oversized);
+ ATF_TP_ADD_TC(tp, pipe_simulator_8k_8k);
+ ATF_TP_ADD_TC(tp, pipe_simulator_8k_128k);
+ ATF_TP_ADD_TC(tp, pipe_simulator_128k_8k);
+ ATF_TP_ADD_TC(tp, pipe_simulator_128k_128k);
+
+ /* Threaded I/O tests with blocking sockets */
+ ATF_TP_ADD_TC(tp, pipe_8k_8k);
+ ATF_TP_ADD_TC(tp, pipe_8k_128k);
+ ATF_TP_ADD_TC(tp, pipe_128k_8k);
+ ATF_TP_ADD_TC(tp, pipe_128k_128k);
+
+ return atf_no_error();
+}
Property changes on: trunk/tests/sys/kern/unix_seqpacket_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/tests/sys/kern/waitpid_nohang.c
===================================================================
--- trunk/tests/sys/kern/waitpid_nohang.c (rev 0)
+++ trunk/tests/sys/kern/waitpid_nohang.c 2018-07-28 20:50:25 UTC (rev 11973)
@@ -0,0 +1,71 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2016 Jilles Tjoelker
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/tests/sys/kern/waitpid_nohang.c 307065 2016-10-11 18:51:03Z jilles $");
+
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <signal.h>
+#include <unistd.h>
+
+ATF_TC_WITHOUT_HEAD(waitpid_nohang);
+ATF_TC_BODY(waitpid_nohang, tc)
+{
+ pid_t child, pid;
+ int status, r;
+
+ child = fork();
+ ATF_REQUIRE(child != -1);
+ if (child == 0) {
+ sleep(10);
+ _exit(1);
+ }
+
+ status = 42;
+ pid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(pid == 0);
+ ATF_CHECK(status == 42);
+
+ r = kill(child, SIGTERM);
+ ATF_REQUIRE(r == 0);
+ r = waitid(P_PID, child, NULL, WEXITED | WNOWAIT);
+ ATF_REQUIRE(r == 0);
+
+ status = -1;
+ pid = waitpid(child, &status, WNOHANG);
+ ATF_REQUIRE(pid == child);
+ ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, waitpid_nohang);
+ return (atf_no_error());
+}
Property changes on: trunk/tests/sys/kern/waitpid_nohang.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
More information about the Midnightbsd-cvs
mailing list