summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/regress/lib/libc/sys
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/regress/lib/libc/sys')
-rw-r--r--src/regress/lib/libc/sys/Makefile152
-rw-r--r--src/regress/lib/libc/sys/README71
-rw-r--r--src/regress/lib/libc/sys/atf-c.c128
-rw-r--r--src/regress/lib/libc/sys/atf-c.h105
-rw-r--r--src/regress/lib/libc/sys/h_macros.h88
-rw-r--r--src/regress/lib/libc/sys/macros.h76
-rw-r--r--src/regress/lib/libc/sys/t_access.c213
-rw-r--r--src/regress/lib/libc/sys/t_bind.c81
-rw-r--r--src/regress/lib/libc/sys/t_chroot.c318
-rw-r--r--src/regress/lib/libc/sys/t_clock_gettime.c210
-rw-r--r--src/regress/lib/libc/sys/t_connect.c135
-rw-r--r--src/regress/lib/libc/sys/t_dup.c407
-rw-r--r--src/regress/lib/libc/sys/t_fork.c383
-rw-r--r--src/regress/lib/libc/sys/t_fsync.c125
-rw-r--r--src/regress/lib/libc/sys/t_getgroups.c176
-rw-r--r--src/regress/lib/libc/sys/t_getitimer.c215
-rw-r--r--src/regress/lib/libc/sys/t_getlogin.c238
-rw-r--r--src/regress/lib/libc/sys/t_getpid.c135
-rw-r--r--src/regress/lib/libc/sys/t_getrusage.c286
-rw-r--r--src/regress/lib/libc/sys/t_getsid.c120
-rw-r--r--src/regress/lib/libc/sys/t_getsockname.c85
-rw-r--r--src/regress/lib/libc/sys/t_gettimeofday.c108
-rw-r--r--src/regress/lib/libc/sys/t_kevent.c232
-rw-r--r--src/regress/lib/libc/sys/t_kill.c313
-rw-r--r--src/regress/lib/libc/sys/t_link.c230
-rw-r--r--src/regress/lib/libc/sys/t_listen.c139
-rw-r--r--src/regress/lib/libc/sys/t_minherit.c200
-rw-r--r--src/regress/lib/libc/sys/t_mkdir.c208
-rw-r--r--src/regress/lib/libc/sys/t_mkfifo.c306
-rw-r--r--src/regress/lib/libc/sys/t_mknod.c196
-rw-r--r--src/regress/lib/libc/sys/t_mlock.c329
-rw-r--r--src/regress/lib/libc/sys/t_mmap.c590
-rw-r--r--src/regress/lib/libc/sys/t_msgctl.c402
-rw-r--r--src/regress/lib/libc/sys/t_msgget.c337
-rw-r--r--src/regress/lib/libc/sys/t_msgrcv.c385
-rw-r--r--src/regress/lib/libc/sys/t_msgsnd.c381
-rw-r--r--src/regress/lib/libc/sys/t_msync.c225
-rw-r--r--src/regress/lib/libc/sys/t_pipe.c162
-rw-r--r--src/regress/lib/libc/sys/t_pipe2.c199
-rw-r--r--src/regress/lib/libc/sys/t_poll.c247
-rw-r--r--src/regress/lib/libc/sys/t_pollts.c203
-rw-r--r--src/regress/lib/libc/sys/t_ppoll.c34
-rw-r--r--src/regress/lib/libc/sys/t_ptrace.c232
-rw-r--r--src/regress/lib/libc/sys/t_recvmmsg.c189
-rw-r--r--src/regress/lib/libc/sys/t_revoke.c198
-rw-r--r--src/regress/lib/libc/sys/t_select.c227
-rw-r--r--src/regress/lib/libc/sys/t_sendmmsg.c216
-rw-r--r--src/regress/lib/libc/sys/t_sendrecv.c189
-rw-r--r--src/regress/lib/libc/sys/t_setrlimit.c556
-rw-r--r--src/regress/lib/libc/sys/t_setuid.c123
-rw-r--r--src/regress/lib/libc/sys/t_sigaction.c151
-rw-r--r--src/regress/lib/libc/sys/t_sigaltstack.c99
-rw-r--r--src/regress/lib/libc/sys/t_socketpair.c138
-rw-r--r--src/regress/lib/libc/sys/t_stat.c420
-rw-r--r--src/regress/lib/libc/sys/t_truncate.c180
-rw-r--r--src/regress/lib/libc/sys/t_umask.c207
-rw-r--r--src/regress/lib/libc/sys/t_unlink.c159
-rw-r--r--src/regress/lib/libc/sys/t_vfork.c31
-rw-r--r--src/regress/lib/libc/sys/t_wait_noproc.c355
-rw-r--r--src/regress/lib/libc/sys/t_wait_noproc_wnohang.c31
-rw-r--r--src/regress/lib/libc/sys/t_waitid.c272
-rw-r--r--src/regress/lib/libc/sys/t_write.c282
62 files changed, 0 insertions, 13428 deletions
diff --git a/src/regress/lib/libc/sys/Makefile b/src/regress/lib/libc/sys/Makefile
deleted file mode 100644
index 5ff237d4e7..0000000000
--- a/src/regress/lib/libc/sys/Makefile
+++ /dev/null
@@ -1,152 +0,0 @@
1# $OpenBSD: Makefile,v 1.18 2023/10/31 08:00:33 claudio Exp $
2
3# Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de>
4# Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18# Each test program in PROGS may define several numbered subtests.
19# In a first step compile all programs and extract their parameters.
20# For each PROG define new regression subtests based on the test number.
21
22.if defined(NUMBERS)
23REGRESS_TARGETS = ${NUMBERS:C/(.*)/run-${PROG}-\1 cleanup-${PROG}-\1/}
24.else
25REGRESS_TARGETS = ${PROGS:S/^/run-/}
26.endif
27
28PROGS =
29PROGS += t_access
30PROGS += t_bind
31PROGS += t_chroot t_clock_gettime
32PROGS += t_connect
33PROGS += t_dup
34PROGS += t_fork
35PROGS += t_fsync
36PROGS += t_getgroups t_getitimer t_getlogin t_getpid t_getrusage
37PROGS += t_getsid t_getsockname t_gettimeofday
38PROGS += t_kevent
39PROGS += t_kill
40PROGS += t_link t_listen
41PROGS += t_minherit
42PROGS += t_mkdir
43PROGS += t_mkfifo
44PROGS += t_mknod
45PROGS += t_mlock
46PROGS += t_mmap
47PROGS += t_msgctl
48PROGS += t_msgget
49PROGS += t_msgrcv
50PROGS += t_msgsnd
51PROGS += t_msync
52PROGS += t_pipe
53PROGS += t_pipe2
54PROGS += t_poll
55PROGS += t_ppoll
56PROGS += t_ptrace
57PROGS += t_recvmmsg
58PROGS += t_revoke
59PROGS += t_select
60PROGS += t_sendmmsg
61PROGS += t_sendrecv
62PROGS += t_setrlimit
63PROGS += t_setuid
64PROGS += t_sigaction
65PROGS += t_sigaltstack
66PROGS += t_socketpair
67PROGS += t_stat
68#PROGS += t_syscall Deleted: test of gcc1.x syscall pad
69PROGS += t_truncate
70PROGS += t_umask t_unlink
71PROGS += t_vfork
72PROGS += t_waitid
73PROGS += t_wait_noproc
74PROGS += t_wait_noproc_wnohang
75PROGS += t_write
76
77# failing tests
78REGRESS_EXPECTED_FAILURES =
79REGRESS_EXPECTED_FAILURES += run-t_mlock-4
80REGRESS_EXPECTED_FAILURES += run-t_msgrcv-3
81
82. for t in run-t_fork-{3,4,5}
83${t}:
84 # Only works with a controlling tty
85 @echo DISABLED
86. endfor
87
88run-t_vfork-2:
89 # SIGSTOP with vfork is masked before exec(3)/exit(3)
90 # see NetBSD: kern_sig.c,v 1.345
91 @echo DISABLED
92
93. for p in ${PROGS}
94SRCS_$p = $p.c atf-c.c
95. endfor
96
97CFLAGS += -std=gnu99
98
99LDADD_t_getpid = -lpthread
100
101run-t_truncate: setup-t_truncate
102setup-t_truncate:
103 ${SUDO} touch truncate_test.root_owned
104 ${SUDO} chown root:wheel truncate_test.root_owned
105
106run-t_chroot: clean-dir
107run-t_ptrace: clean-dir
108clean-dir:
109 ${SUDO} rm -rf dir
110
111CLEANFILES = access dummy mmap truncate_test.root_owned
112
113.for p in ${PROGS}
114run-$p: $p
115 ulimit -c unlimited && \
116 ntests="`./$p -n`" && \
117 echo "1..$$ntests" && \
118 tnumbers="`jot -ns' ' - 1 $$ntests`" && \
119 ${.MAKE} -C ${.CURDIR} PROG=$p NUMBERS="$$tnumbers" regress
120.endfor
121
122.if defined(NUMBERS)
123CUR_USER != id -g
124
125. for n in ${NUMBERS}
126DESCR_$n != eval `./${PROG} -i $n` && echo $$DESCR
127REQ_USER_$n != eval `./${PROG} -i $n` && echo $$REQ_USER
128
129. if ${REQ_USER_$n} == "root"
130REGRESS_ROOT_TARGETS += run-${PROG}-$n
131. endif
132
133run-${PROG}-$n:
134 @echo "$n ${DESCR_$n}"
135. if ${REQ_USER_$n} == "root"
136 ${SUDO} ./${PROG} -r $n
137. elif ${REQ_USER_$n} == "unprivileged" && ${CUR_USER} == 0
138 ${SUDO} su ${BUILDUSER} -c exec ./${PROG} -r $n
139. elif ${REQ_USER_$n} == "unprivileged" || ${REQ_USER_$n} == ""
140 ./${PROG} -r $n
141. else
142 # bad REQ_USER: ${REQ_USER_$n}
143 false
144. endif
145
146cleanup-${PROG}-$n:
147 -./${PROG} -c $n
148
149. endfor
150.endif
151
152.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/sys/README b/src/regress/lib/libc/sys/README
deleted file mode 100644
index 526832e5a7..0000000000
--- a/src/regress/lib/libc/sys/README
+++ /dev/null
@@ -1,71 +0,0 @@
1Regression tests for system calls ported from NetBSD.
2
3Reimplement ATF with many hacks to adjust the tests as little as possible.
4
5Tests passing without source file adjustments:
6t_access t_getsockname t_msgctl t_sigaltstack
7t_bind t_gettimeofday t_msgsnd t_socketpair
8t_conect t_kill t_msync t_truncate
9t_getgroups t_link t_pipe t_umask
10t_getitimer t_listen t_ppoll t_write
11t_getlogin t_minherit t_sendrecv
12t_getpid t_mkdir t_setuid
13t_getsid t_mkfifo t_sigaction
14
15Tests passing after adjustments:
16t_chroot - fchroot is not implemented
17t_clock_gettime - requires sysctlbyname
18t_dup - OpenBSD dup3 is similar to Linux dup3
19t_fork - add reallocarr function, remove clone(2) tests
20t_fsync - replace mkstemp
21t_getrusage - no expected fail, PR kern/30115 is NetBSD, work more
22t_kevent - no EVFILT_USER, DRVCTLDEV, passing kqueue forbidden
23t_mknod - remove tests for unsupported file types
24t_msgget - remove msgget_limit test
25t_poll - remove pollts_* tests
26t_ptrace - change EPERM -> EINVAL for PT_ATTACH of a parent
27t_revoke - remove basic tests, revoke only on ttys supported
28t_select - remove sigset_t struct as it is int on OpenBSD
29t_setrlimit - remove unsupported resource parameters and lwp
30t_syscall - add __syscall prototype
31t_vfork - SIGSTOP is masked before exec(3)/exit(3)
32t_wait_noproc - waitid and wait6 are not implemented
33t_wait_noproc_wnohang - waitid and wait6 are not implemented
34
35Failing tests:
36t_mlock - wrong errno, succeeds where not expected, POSIX imprecise
37t_mmap - ENOTBLK on test NetBSD is skipping, remove mmap_va0 test
38t_msgrcv - msgrcv(id, &r, 3 - 1, 0x41, 004000) != -1
39t_pipe2 - closefrom(4) == -1, remove F_GETNOSIGPIPE and nosigpipe test
40t_stat - invalid GID with doas
41t_unlink - wrong errno according to POSIX
42
43Excluded tests:
44t_clock_nanosleep - not available
45t_clone - not available
46t_futex_ops - no lwp
47t_futex_robust - no lwp
48t_getcontext - not available, removed in POSIX.1-2008
49t_getrandom - not available
50t_issetugid - works as iplemented
51t_lwp_create - not available
52t_lwp_ctl - not available
53t_mincore - removed
54t_mprotect - no exec_prot_support and no return_one in libc
55t_nanosleep - not available
56t_pollts - not available
57t_posix_fadvise - optional POSIX Advisory Information
58t_posix_fallocate - optional POSIX Advisory Information
59t_ptrace_sigchld -
60t_ptrace_wait -
61t_ptrace_wait3 -
62t_ptrace_wait4 -
63t_ptrace_wait6 - not implemented
64t_ptrace_waitid -
65t_ptrace_waitpid -
66t_sigqueue - not implemented, added in POSIX.1-2004
67t_sigtimedwait - not implemented, added in POSIX.1-2004
68t_swapcontext - not available, removed in POSIX.1-2008
69t_timer_create - not implemented, added in POSIX.1-2004
70t_ucontext - not available, removed in POSIX.1-2008
71t_wait - wait6 is not available, not POSIX
diff --git a/src/regress/lib/libc/sys/atf-c.c b/src/regress/lib/libc/sys/atf-c.c
deleted file mode 100644
index e550a9b753..0000000000
--- a/src/regress/lib/libc/sys/atf-c.c
+++ /dev/null
@@ -1,128 +0,0 @@
1/* $OpenBSD: atf-c.c,v 1.2 2019/11/22 15:59:53 bluhm Exp $ */
2/*
3 * Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/wait.h>
19
20#include <err.h>
21#include <errno.h>
22#include <limits.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <pwd.h>
27#include <unistd.h>
28
29#include "atf-c.h"
30
31void usage(void);
32
33int cleanup;
34int count;
35int inspect;
36int run;
37int test;
38
39int
40main(int argc, char *argv[])
41{
42 int ch, test;
43 const char *errstr, *num;
44
45 while ((ch = getopt(argc, argv, "c:i:nr:")) != -1) {
46 switch(ch) {
47 case 'c':
48 cleanup = 1;
49 num = optarg;
50 break;
51 case 'i':
52 inspect = 1;
53 num = optarg;
54 break;
55 case 'n':
56 count = 1;
57 break;
58 case 'r':
59 run = 1;
60 num = optarg;
61 break;
62 default:
63 usage();
64 }
65 }
66 argc -= optind;
67 argv += optind;
68
69 if (cleanup + count + inspect + run > 1)
70 usage();
71
72 if (cleanup || inspect || run) {
73 test = strtonum(num, 1, INT_MAX, &errstr);
74 if (errstr != NULL)
75 errx(1, "test # is %s: %s", errstr, argv[1]);
76 }
77 if (count)
78 printf("%d\n", atf_test(0, 0));
79 else if (cleanup)
80 ATF_CLEANUP(test);
81 else if (run)
82 ATF_RUN(test);
83 else if (inspect)
84 ATF_INSPECT(test);
85 else
86 usage();
87
88 return 0;
89}
90
91void
92usage(void)
93{
94 fprintf(stderr, "usage: %s [-n] [-c|i|r test#]\n", getprogname());
95 exit(1);
96}
97
98void
99atf_require(int exp, int expected_errno, const char *expstr, const char *src,
100 const int lineno, char *fmt, ...)
101{
102 va_list args;
103 if (!(exp)) {
104 fprintf(stderr, "\n%s:%d: ", src, lineno);
105 if (fmt != NULL) {
106 va_start(args, fmt);
107 vfprintf(stderr, fmt, args);
108 va_end(args);
109 } else {
110 fprintf(stderr, "'%s' evaluated to false\n", expstr);
111 }
112 exit(1);
113 } else if (expected_errno >= 0 && errno != expected_errno) {
114 fprintf(stderr, "\n%s:%d: ", src, lineno);
115 fprintf(stderr, "expected errno %d but got %d instead\n",
116 expected_errno, errno);
117 exit(1);
118 }
119 return;
120}
121
122void
123atf_tc_fail(char *fmt, ...)
124{
125 va_list args;
126 va_start(args, fmt);
127 verrx(1, fmt, args);
128}
diff --git a/src/regress/lib/libc/sys/atf-c.h b/src/regress/lib/libc/sys/atf-c.h
deleted file mode 100644
index 6670abac0a..0000000000
--- a/src/regress/lib/libc/sys/atf-c.h
+++ /dev/null
@@ -1,105 +0,0 @@
1/* $OpenBSD: atf-c.h,v 1.5 2022/09/11 20:51:44 mbuhl Exp $ */
2/*
3 * Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#if !defined(ATF_C_H)
19#define ATF_C_H
20
21#include <pwd.h>
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25
26
27int atf_test(int, int);
28void atf_require(int, int, const char *, const char *, const int, char *, ...);
29void atf_tc_fail(char *, ...)
30 __attribute__((__noreturn__, __format__ (printf, 1, 2)));
31
32#define ATF_INSPECT_TEST 1
33#define ATF_RUN_TEST 2
34#define ATF_CLEANUP_TEST 3
35
36#define ATF_TC_FUNCTIONS(fn) \
37void atf_head_##fn(void); \
38void atf_body_##fn(void); \
39void atf_cleanup_##fn(void);
40
41#define ATF_TC(fn) \
42ATF_TC_FUNCTIONS(fn) \
43void atf_cleanup_##fn(void) { return; }
44
45#define ATF_TC_WITH_CLEANUP(fn) \
46ATF_TC_FUNCTIONS(fn)
47
48#define ATF_TC_HEAD(fn, tc) void atf_head_##fn(void)
49#define ATF_TC_BODY(fn, tc) void atf_body_##fn(void)
50#define ATF_TC_CLEANUP(fn, tc) void atf_cleanup_##fn(void)
51
52#define ATF_TP_ADD_TCS(tp) int atf_test(int tst, int what)
53#define ATF_TP_ADD_TC(tp, fn) tst--; \
54 if (tst == 0) { \
55 if (what == ATF_INSPECT_TEST) \
56 atf_head_##fn(); \
57 else if (what == ATF_RUN_TEST) \
58 atf_body_##fn(); \
59 else if (what == ATF_CLEANUP_TEST) \
60 atf_cleanup_##fn(); \
61 return 0; \
62 }
63
64#define atf_no_error() (-tst)
65
66#define ATF_INSPECT(i) atf_test(i, ATF_INSPECT_TEST)
67#define ATF_RUN(i) atf_test(i, ATF_RUN_TEST)
68#define ATF_CLEANUP(i) atf_test(i, ATF_CLEANUP_TEST)
69
70#define atf_tc_set_md_var(tc, attr, fmt, ...) \
71 if (strcmp(attr, "descr") == 0) \
72 printf("DESCR=\"" fmt "\"\n", ##__VA_ARGS__); \
73 else if (strcmp(attr, "require.user") == 0) \
74 printf("REQ_USER=" fmt "\n", ##__VA_ARGS__);
75
76#define ATF_CHECK ATF_REQUIRE
77#define ATF_CHECK_MSG ATF_REQUIRE_MSG
78#define ATF_CHECK_EQ ATF_REQUIRE_EQ
79#define ATF_CHECK_EQ_MSG ATF_REQUIRE_EQ_MSG
80#define ATF_CHECK_ERRNO ATF_REQUIRE_ERRNO
81#define ATF_CHECK_STREQ ATF_REQUIRE_STREQ
82
83#define atf_req(exp, err, msg, ...) \
84 atf_require(exp, err, #exp, __FILE__, __LINE__, msg, ##__VA_ARGS__)
85#define ATF_REQUIRE(exp) atf_req(exp, -1, NULL)
86#define ATF_REQUIRE_ERRNO(no, exp) atf_req(exp, no, NULL)
87#define ATF_REQUIRE_MSG(exp, fmt, ...) atf_req(exp, -1, fmt, ##__VA_ARGS__)
88#define ATF_REQUIRE_EQ(a, b) atf_req((a) == (b), -1, NULL)
89#define ATF_REQUIRE_EQ_MSG(a, b, fmt, ...) \
90 atf_req((a) == (b), -1, fmt, ##__VA_ARGS__)
91#define ATF_REQUIRE_STREQ(x, y) \
92 ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
93
94#define atf_tc_fail_nonfatal(fmt, ...) atf_tc_fail(fmt, ##__VA_ARGS__)
95#define atf_tc_expect_fail(fmt, ...) \
96 atf_tc_fail(fmt "\nEXPECTED_FAIL", ##__VA_ARGS__)
97#define atf_tc_skip(fmt, ...) \
98 atf_tc_fail(fmt "\nSKIPPING", ##__VA_ARGS__)
99#define atf_tc_pass() exit(0)
100
101#define atf_tc_get_config_var(a, b) "."
102
103#define atf_utils_fork() fork()
104
105#endif /* !defined(ATF_C_H) */
diff --git a/src/regress/lib/libc/sys/h_macros.h b/src/regress/lib/libc/sys/h_macros.h
deleted file mode 100644
index 22f7ed83f4..0000000000
--- a/src/regress/lib/libc/sys/h_macros.h
+++ /dev/null
@@ -1,88 +0,0 @@
1/* $OpenBSD: h_macros.h,v 1.2 2021/09/02 12:40:44 mbuhl Exp $ */
2/* $NetBSD: h_macros.h,v 1.13 2016/08/20 15:49:08 christos Exp $ */
3
4/*-
5 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef SRC_TESTS_H_MACROS_H_
31#define SRC_TESTS_H_MACROS_H_
32
33#include <sys/types.h>
34#include <errno.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "atf-c.h"
41
42#define REQUIRE_LIBC(x, v) \
43 ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
44
45#define CHECK_LIBC(x, v) \
46 ATF_CHECK_MSG((x) != (v), "%s: %s", #x, strerror(errno))
47
48#define RL(x) REQUIRE_LIBC(x, -1)
49#define RLF(x, fmt, arg) \
50 ATF_CHECK_MSG((x) != -1, "%s [" fmt "]: %s", #x, arg, strerror(errno))
51#define RZ(x) \
52do { \
53 int RZ_rv = x; \
54 ATF_REQUIRE_MSG(RZ_rv == 0, "%s: %s", #x, strerror(RZ_rv)); \
55} while (/*CONSTCOND*/0)
56
57__dead static __inline __attribute__((__format__(__printf__,1,2))) void
58atf_tc_fail_errno(const char *fmt, ...)
59{
60 va_list ap;
61 char buf[1024];
62 int sverrno = errno;
63
64 va_start(ap, fmt);
65 vsnprintf(buf, sizeof(buf), fmt, ap);
66 va_end(ap);
67
68 strlcat(buf, ": ", sizeof(buf));
69 strlcat(buf, strerror(sverrno), sizeof(buf));
70
71 atf_tc_fail("%s", buf);
72}
73
74static __inline void
75tests_makegarbage(void *space, size_t len)
76{
77 uint16_t *sb = space;
78 uint16_t randval;
79
80 while (len >= sizeof(randval)) {
81 *sb++ = (uint16_t)random();
82 len -= sizeof(*sb);
83 }
84 randval = (uint16_t)random();
85 memcpy(sb, &randval, len);
86}
87
88#endif
diff --git a/src/regress/lib/libc/sys/macros.h b/src/regress/lib/libc/sys/macros.h
deleted file mode 100644
index cb0789eb71..0000000000
--- a/src/regress/lib/libc/sys/macros.h
+++ /dev/null
@@ -1,76 +0,0 @@
1/* $OpenBSD: macros.h,v 1.5 2021/12/13 16:56:48 deraadt Exp $ */
2/* Public domain - Moritz Buhl */
3
4#include <sys/socket.h>
5#include <sys/stdint.h>
6#include <sys/sysctl.h>
7#include <sys/types.h>
8
9#include <stdbool.h>
10#include <string.h>
11#include <stdio.h>
12
13#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
14
15#define __arraycount(_a) nitems(_a)
16#define __unreachable() atf_tc_fail("unreachable")
17#define __UNCONST(a) (a)
18
19/* t_chroot.c */
20#define fchroot(fd) 0
21
22/* t_clock_gettime.c */
23int sysctlbyname(char *, void *, size_t *, void *, size_t);
24
25int
26sysctlbyname(char* s, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
27{
28 int mib[3], miblen;
29
30 mib[0] = CTL_KERN;
31 if (strcmp(s, "kern.timecounter.hardware") == 0) {
32 mib[1] = KERN_TIMECOUNTER;
33 mib[2] = KERN_TIMECOUNTER_HARDWARE;
34 miblen = 3;
35 } else if (strcmp(s, "kern.timecounter.choice") == 0) {
36 mib[1] = KERN_TIMECOUNTER;
37 mib[2] = KERN_TIMECOUNTER_CHOICE;
38 miblen = 3;
39 } else if (strcmp(s, "kern.securelevel") == 0) {
40 mib[1] = KERN_SECURELVL;
41 miblen = 2;
42 } else {
43 fprintf(stderr, "%s(): mib '%s' not supported\n", __func__, s);
44 return -42;
45 }
46
47 return sysctl(mib, miblen, oldp, oldlenp, newp, newlen);
48}
49
50/* t_connect.c */
51#define IPPORT_RESERVEDMAX 1023
52
53/* t_fork.c */
54#define kinfo_proc2 kinfo_proc
55#define KERN_PROC2 KERN_PROC
56#define reallocarr(pp, n, s) ((*pp = reallocarray(*pp, n, s)), *pp == NULL)
57#define LSSTOP SSTOP
58
59/* t_mlock.c */
60#define MAP_WIRED __MAP_NOREPLACE
61
62/* t_pipe2.c */
63#define O_NOSIGPIPE 0
64
65/* t_poll.c */
66#define pollts(a, b, c, e) 0
67
68/* t_sendrecv.c */
69#define SO_RERROR SO_DEBUG
70
71/* t_write.c */
72#define _PATH_DEVZERO "/dev/zero"
73
74/* t_wait_noproc.c */
75#define ___STRING(x) #x
76#define __BIT(n) (1 << (n))
diff --git a/src/regress/lib/libc/sys/t_access.c b/src/regress/lib/libc/sys/t_access.c
deleted file mode 100644
index 531b17c46e..0000000000
--- a/src/regress/lib/libc/sys/t_access.c
+++ /dev/null
@@ -1,213 +0,0 @@
1/* $OpenBSD: t_access.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_access.c,v 1.3 2019/07/16 17:29:18 martin Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include "atf-c.h"
36
37#include <sys/stat.h>
38
39#include <errno.h>
40#include <fcntl.h>
41#include <limits.h>
42#include <stdint.h>
43#include <stdlib.h>
44#include <unistd.h>
45
46static const char path[] = "access";
47static const int mode[4] = { R_OK, W_OK, X_OK, F_OK };
48
49ATF_TC_WITH_CLEANUP(access_access);
50ATF_TC_HEAD(access_access, tc)
51{
52 atf_tc_set_md_var(tc, "descr", "Test access(2) for EACCES");
53 atf_tc_set_md_var(tc, "require.user", "unprivileged");
54}
55
56ATF_TC_BODY(access_access, tc)
57{
58 const int perm[3] = { 0200, 0400, 0000 };
59 size_t i;
60 int fd;
61
62 fd = open(path, O_RDONLY | O_CREAT, 0600);
63
64 if (fd < 0)
65 return;
66
67 for (i = 0; i < __arraycount(mode) - 1; i++) {
68
69 ATF_REQUIRE(fchmod(fd, perm[i]) == 0);
70
71 errno = 0;
72
73 ATF_REQUIRE(access(path, mode[i]) != 0);
74 ATF_REQUIRE(errno == EACCES);
75 }
76
77 ATF_REQUIRE(close(fd) == 0);
78}
79
80ATF_TC_CLEANUP(access_access, tc)
81{
82 (void)unlink(path);
83}
84
85ATF_TC(access_fault);
86ATF_TC_HEAD(access_fault, tc)
87{
88 atf_tc_set_md_var(tc, "descr", "Test access(2) for EFAULT");
89}
90
91ATF_TC_BODY(access_fault, tc)
92{
93 size_t i;
94
95 for (i = 0; i < __arraycount(mode); i++) {
96
97 errno = 0;
98
99 ATF_REQUIRE(access(NULL, mode[i]) != 0);
100 ATF_REQUIRE(errno == EFAULT);
101
102 errno = 0;
103
104 ATF_REQUIRE(access((char *)-1, mode[i]) != 0);
105 ATF_REQUIRE(errno == EFAULT);
106 }
107}
108
109ATF_TC(access_inval);
110ATF_TC_HEAD(access_inval, tc)
111{
112 atf_tc_set_md_var(tc, "descr", "Test access(2) for EINVAL");
113}
114
115ATF_TC_BODY(access_inval, tc)
116{
117
118 errno = 0;
119
120 ATF_REQUIRE(access("/usr", -1) != 0);
121 ATF_REQUIRE(errno == EINVAL);
122}
123
124ATF_TC(access_notdir);
125ATF_TC_HEAD(access_notdir, tc)
126{
127 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOTDIR");
128}
129
130ATF_TC_BODY(access_notdir, tc)
131{
132 size_t i;
133
134 for (i = 0; i < __arraycount(mode); i++) {
135
136 errno = 0;
137
138 /*
139 * IEEE Std 1003.1-2008 about ENOTDIR:
140 *
141 * "A component of the path prefix is not a directory,
142 * or the path argument contains at least one non-<slash>
143 * character and ends with one or more trailing <slash>
144 * characters and the last pathname component names an
145 * existing file that is neither a directory nor a symbolic
146 * link to a directory."
147 */
148 ATF_REQUIRE(access("/etc/passwd//", mode[i]) != 0);
149 ATF_REQUIRE(errno == ENOTDIR);
150 }
151}
152
153ATF_TC(access_notexist);
154ATF_TC_HEAD(access_notexist, tc)
155{
156 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOENT");
157}
158
159ATF_TC_BODY(access_notexist, tc)
160{
161 size_t i;
162
163 for (i = 0; i < __arraycount(mode); i++) {
164
165 errno = 0;
166
167 ATF_REQUIRE(access("", mode[i]) != 0);
168 ATF_REQUIRE(errno == ENOENT);
169 }
170}
171
172ATF_TC(access_toolong);
173ATF_TC_HEAD(access_toolong, tc)
174{
175 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENAMETOOLONG");
176}
177
178ATF_TC_BODY(access_toolong, tc)
179{
180 char *buf;
181 size_t i;
182
183 buf = malloc(PATH_MAX);
184
185 if (buf == NULL)
186 return;
187
188 for (i = 0; i < PATH_MAX; i++)
189 buf[i] = 'x';
190
191 for (i = 0; i < __arraycount(mode); i++) {
192
193 errno = 0;
194
195 ATF_REQUIRE(access(buf, mode[i]) != 0);
196 ATF_REQUIRE(errno == ENAMETOOLONG);
197 }
198
199 free(buf);
200}
201
202ATF_TP_ADD_TCS(tp)
203{
204
205 ATF_TP_ADD_TC(tp, access_access);
206 ATF_TP_ADD_TC(tp, access_fault);
207 ATF_TP_ADD_TC(tp, access_inval);
208 ATF_TP_ADD_TC(tp, access_notdir);
209 ATF_TP_ADD_TC(tp, access_notexist);
210 ATF_TP_ADD_TC(tp, access_toolong);
211
212 return atf_no_error();
213}
diff --git a/src/regress/lib/libc/sys/t_bind.c b/src/regress/lib/libc/sys/t_bind.c
deleted file mode 100644
index 7989c7c229..0000000000
--- a/src/regress/lib/libc/sys/t_bind.c
+++ /dev/null
@@ -1,81 +0,0 @@
1/* $OpenBSD: t_bind.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_bind.c,v 1.3 2015/04/05 23:28:10 rtr Exp $ */
3/*
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <errno.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <unistd.h>
37
38#include <sys/socket.h>
39#include <arpa/inet.h>
40#include <netinet/in.h>
41
42#include "atf-c.h"
43
44ATF_TC(bind_foreign_family);
45
46ATF_TC_HEAD(bind_foreign_family, tc)
47{
48 atf_tc_set_md_var(tc, "descr", "Checks that binding a socket "
49 "with a different address family fails");
50}
51
52ATF_TC_BODY(bind_foreign_family, tc)
53{
54 struct sockaddr_in addr;
55
56 /* addr.sin_family = AF_UNSPEC = 0 */
57 memset(&addr, 0, sizeof(addr));
58
59 /*
60 * it is not necessary to initialize sin_{addr,port} since
61 * those structure members shall not be accessed if bind
62 * fails correctly.
63 */
64
65 int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
66 ATF_REQUIRE(sock != -1);
67
68 /* should fail but currently doesn't */
69 ATF_REQUIRE(-1 == bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
70 ATF_REQUIRE(EAFNOSUPPORT == errno);
71
72 close(sock);
73}
74
75ATF_TP_ADD_TCS(tp)
76{
77
78 ATF_TP_ADD_TC(tp, bind_foreign_family);
79
80 return atf_no_error();
81}
diff --git a/src/regress/lib/libc/sys/t_chroot.c b/src/regress/lib/libc/sys/t_chroot.c
deleted file mode 100644
index fb65bacebd..0000000000
--- a/src/regress/lib/libc/sys/t_chroot.c
+++ /dev/null
@@ -1,318 +0,0 @@
1/* $OpenBSD: t_chroot.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_chroot.c,v 1.2 2017/01/10 22:36:29 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36#include <sys/stat.h>
37
38#include "atf-c.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <limits.h>
42#include <pwd.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47ATF_TC(chroot_basic);
48ATF_TC_HEAD(chroot_basic, tc)
49{
50 atf_tc_set_md_var(tc, "descr", "A basic test of chroot(2)");
51 atf_tc_set_md_var(tc, "require.user", "root");
52}
53
54ATF_TC_BODY(chroot_basic, tc)
55{
56 char buf[PATH_MAX];
57 int fd, sta;
58 pid_t pid;
59
60 (void)memset(buf, '\0', sizeof(buf));
61 (void)getcwd(buf, sizeof(buf));
62 (void)strlcat(buf, "/dir", sizeof(buf));
63
64 ATF_REQUIRE(mkdir(buf, 0500) == 0);
65 ATF_REQUIRE(chdir(buf) == 0);
66
67 pid = fork();
68 ATF_REQUIRE(pid >= 0);
69
70 if (pid == 0) {
71
72 if (chroot(buf) != 0)
73 _exit(EXIT_FAILURE);
74
75 errno = 0;
76
77 if (chroot("/root") != -1)
78 _exit(EXIT_FAILURE);
79
80 if (errno != ENOENT)
81 _exit(EXIT_FAILURE);
82
83 fd = open("file", O_RDONLY | O_CREAT, 0600);
84
85 if (fd < 0)
86 _exit(EXIT_FAILURE);
87
88 if (close(fd) != 0)
89 _exit(EXIT_FAILURE);
90
91 _exit(EXIT_SUCCESS);
92 }
93
94 (void)wait(&sta);
95
96 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
97 atf_tc_fail("chroot(2) failed");
98
99 (void)chdir("/");
100 (void)strlcat(buf, "/file", sizeof(buf));
101
102 fd = open(buf, O_RDONLY);
103
104 if (fd < 0)
105 atf_tc_fail("chroot(2) did not change the root directory");
106
107 ATF_REQUIRE(close(fd) == 0);
108 ATF_REQUIRE(unlink(buf) == 0);
109}
110
111ATF_TC(chroot_err);
112ATF_TC_HEAD(chroot_err, tc)
113{
114 atf_tc_set_md_var(tc, "descr", "Test error conditions of chroot(2)");
115 atf_tc_set_md_var(tc, "require.user", "root");
116}
117
118ATF_TC_BODY(chroot_err, tc)
119{
120 char buf[PATH_MAX + 1];
121
122 (void)memset(buf, 'x', sizeof(buf));
123
124 errno = 0;
125 ATF_REQUIRE_ERRNO(ENAMETOOLONG, chroot(buf) == -1);
126
127 errno = 0;
128 ATF_REQUIRE_ERRNO(EFAULT, chroot((void *)-1) == -1);
129
130 errno = 0;
131 ATF_REQUIRE_ERRNO(ENOENT, chroot("/a/b/c/d/e/f/g/h/i/j") == -1);
132}
133
134ATF_TC(chroot_perm);
135ATF_TC_HEAD(chroot_perm, tc)
136{
137 atf_tc_set_md_var(tc, "descr", "Test permissions with chroot(2)");
138 atf_tc_set_md_var(tc, "require.user", "unprivileged");
139}
140
141ATF_TC_BODY(chroot_perm, tc)
142{
143 static char buf[LINE_MAX];
144 pid_t pid;
145 int sta;
146
147 (void)memset(buf, '\0', sizeof(buf));
148 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
149
150 pid = fork();
151 ATF_REQUIRE(pid >= 0);
152
153 if (pid == 0) {
154
155 errno = 0;
156
157 if (chroot(buf) != -1)
158 _exit(EXIT_FAILURE);
159
160 if (errno != EPERM)
161 _exit(EXIT_FAILURE);
162
163 _exit(EXIT_SUCCESS);
164 }
165
166 (void)wait(&sta);
167
168 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
169 atf_tc_fail("chroot(2) succeeded as unprivileged user");
170}
171
172ATF_TC(fchroot_basic);
173ATF_TC_HEAD(fchroot_basic, tc)
174{
175 atf_tc_set_md_var(tc, "descr", "A basic test of fchroot(2)");
176 atf_tc_set_md_var(tc, "require.user", "root");
177}
178
179ATF_TC_BODY(fchroot_basic, tc)
180{
181 char buf[PATH_MAX];
182 int fd, sta;
183 pid_t pid;
184
185 (void)memset(buf, '\0', sizeof(buf));
186 (void)getcwd(buf, sizeof(buf));
187 (void)strlcat(buf, "/dir", sizeof(buf));
188
189 ATF_REQUIRE(mkdir(buf, 0500) == 0);
190 ATF_REQUIRE(chdir(buf) == 0);
191
192 fd = open(buf, O_RDONLY);
193 ATF_REQUIRE(fd >= 0);
194
195 pid = fork();
196 ATF_REQUIRE(pid >= 0);
197
198 if (pid == 0) {
199
200 if (fchroot(fd) != 0)
201 _exit(EXIT_FAILURE);
202
203 if (close(fd) != 0)
204 _exit(EXIT_FAILURE);
205
206 fd = open("file", O_RDONLY | O_CREAT, 0600);
207
208 if (fd < 0)
209 _exit(EXIT_FAILURE);
210
211 if (close(fd) != 0)
212 _exit(EXIT_FAILURE);
213
214 _exit(EXIT_SUCCESS);
215 }
216
217 (void)wait(&sta);
218
219 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
220 atf_tc_fail("fchroot(2) failed");
221
222 (void)chdir("/");
223 (void)strlcat(buf, "/file", sizeof(buf));
224
225 fd = open(buf, O_RDONLY);
226
227 if (fd < 0)
228 atf_tc_fail("fchroot(2) did not change the root directory");
229
230 ATF_REQUIRE(close(fd) == 0);
231 ATF_REQUIRE(unlink(buf) == 0);
232}
233
234ATF_TC(fchroot_err);
235ATF_TC_HEAD(fchroot_err, tc)
236{
237 atf_tc_set_md_var(tc, "descr", "Test error conditions of fchroot(2)");
238 atf_tc_set_md_var(tc, "require.user", "root");
239}
240
241ATF_TC_BODY(fchroot_err, tc)
242{
243 int fd;
244
245 fd = open("/etc/passwd", O_RDONLY);
246 ATF_REQUIRE(fd >= 0);
247
248 errno = 0;
249 ATF_REQUIRE_ERRNO(EBADF, fchroot(-1) == -1);
250
251 errno = 0;
252 ATF_REQUIRE_ERRNO(ENOTDIR, fchroot(fd) == -1);
253
254 ATF_REQUIRE(close(fd) == 0);
255}
256
257ATF_TC(fchroot_perm);
258ATF_TC_HEAD(fchroot_perm, tc)
259{
260 atf_tc_set_md_var(tc, "descr", "Test permissions with fchroot(2)");
261 atf_tc_set_md_var(tc, "require.user", "root");
262}
263
264ATF_TC_BODY(fchroot_perm, tc)
265{
266 static char buf[LINE_MAX];
267 struct passwd *pw;
268 int fd, sta;
269 pid_t pid;
270
271 (void)memset(buf, '\0', sizeof(buf));
272 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
273
274 pw = getpwnam("nobody");
275 fd = open(buf, O_RDONLY);
276
277 ATF_REQUIRE(fd >= 0);
278 ATF_REQUIRE(pw != NULL);
279
280 pid = fork();
281 ATF_REQUIRE(pid >= 0);
282
283 if (pid == 0) {
284
285 (void)setuid(pw->pw_uid);
286
287 errno = 0;
288
289 if (fchroot(fd) != -1)
290 _exit(EXIT_FAILURE);
291
292 if (errno != EPERM)
293 _exit(EXIT_FAILURE);
294
295 _exit(EXIT_SUCCESS);
296 }
297
298 (void)wait(&sta);
299
300 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
301 atf_tc_fail("fchroot(2) succeeded as unprivileged user");
302}
303
304ATF_TP_ADD_TCS(tp)
305{
306
307 ATF_TP_ADD_TC(tp, chroot_basic);
308 ATF_TP_ADD_TC(tp, chroot_err);
309 ATF_TP_ADD_TC(tp, chroot_perm);
310#ifndef __OpenBSD__
311 /* fchroot(2) not available */
312 ATF_TP_ADD_TC(tp, fchroot_basic);
313 ATF_TP_ADD_TC(tp, fchroot_err);
314 ATF_TP_ADD_TC(tp, fchroot_perm);
315#endif
316
317 return atf_no_error();
318}
diff --git a/src/regress/lib/libc/sys/t_clock_gettime.c b/src/regress/lib/libc/sys/t_clock_gettime.c
deleted file mode 100644
index 11105d2240..0000000000
--- a/src/regress/lib/libc/sys/t_clock_gettime.c
+++ /dev/null
@@ -1,210 +0,0 @@
1/* $OpenBSD: t_clock_gettime.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_clock_gettime.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
3
4/*-
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Frank Kardel.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*-
34 * Copyright (c) 2006 Frank Kardel
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
57 */
58
59#include "macros.h"
60
61#include <sys/sysctl.h>
62
63
64#include "atf-c.h"
65#include <errno.h>
66#include <limits.h>
67#include <stdio.h>
68#include <stdint.h>
69#include <stdlib.h>
70#include <string.h>
71#include <time.h>
72#include <unistd.h>
73
74#include "h_macros.h"
75
76#define MINPOSDIFF 15000000 /* 15 ms for now */
77#define TIMEOUT 5
78
79#define TC_HARDWARE "kern.timecounter.hardware"
80#define TC_CHOICE "kern.timecounter.choice"
81
82static void
83check_timecounter(void)
84{
85 struct timespec tsa, tsb, tsl, res;
86 long long mindiff = INTMAX_MAX;
87 time_t endlimit;
88
89#define CL(x) \
90 do { \
91 if ((x) != -1) \
92 break; \
93 atf_tc_fail_nonfatal("%s: %s", #x, strerror(errno)); \
94 return; \
95 } while (0)
96
97 CL(clock_gettime(CLOCK_REALTIME, &tsa));
98 tsl = tsa;
99
100 CL(time(&endlimit));
101 endlimit += TIMEOUT + 1;
102
103 while ((time_t)tsa.tv_sec < endlimit) {
104 long long diff;
105
106 CL(clock_gettime(CLOCK_REALTIME, &tsb));
107 diff = 1000000000LL * (tsb.tv_sec - tsa.tv_sec)
108 + tsb.tv_nsec - tsa.tv_nsec;
109
110 if (diff > 0 && mindiff > diff)
111 mindiff = diff;
112
113 if (diff < 0 || diff > MINPOSDIFF) {
114 long long elapsed;
115 (void)printf("%stime TSA: 0x%jx.%08jx, TSB: 0x%jx.%08jx, "
116 "diff = %lld nsec, ", (diff < 0) ? "BAD " : "",
117 (uintmax_t)tsa.tv_sec, (uintmax_t)tsa.tv_nsec,
118 (uintmax_t)tsb.tv_sec, (uintmax_t)tsb.tv_nsec, diff);
119
120 elapsed = 1000000000LL * (tsb.tv_sec - tsl.tv_sec)
121 + tsb.tv_nsec - tsl.tv_nsec;
122
123
124 (void)printf("%lld nsec\n", elapsed);
125 tsl = tsb;
126
127 ATF_CHECK(diff >= 0);
128 if (diff < 0)
129 return;
130 }
131
132 tsa.tv_sec = tsb.tv_sec;
133 tsa.tv_nsec = tsb.tv_nsec;
134 }
135
136 if (clock_getres(CLOCK_REALTIME, &res) == 0) {
137 long long r = res.tv_sec * 1000000000 + res.tv_nsec;
138
139 (void)printf("Claimed resolution: %lld nsec (%f Hz) or "
140 "better\n", r, 1.0 / r * 1e9);
141 (void)printf("Observed minimum non zero delta: %lld "
142 "nsec\n", mindiff);
143 }
144
145#undef CL
146}
147
148ATF_TC(clock_gettime_real);
149ATF_TC_HEAD(clock_gettime_real, tc)
150{
151 atf_tc_set_md_var(tc, "require.user", "root");
152 atf_tc_set_md_var(tc, "descr",
153 "Checks the monotonicity of the CLOCK_REALTIME implementation");
154 atf_tc_set_md_var(tc, "timeout", "300");
155}
156
157ATF_TC_BODY(clock_gettime_real, tc)
158{
159 char name[128], cbuf[512], ctrbuf[10240];
160 size_t cbufsiz = sizeof(cbuf);
161 size_t ctrbufsiz = sizeof(ctrbuf);
162 const char *p;
163 char *save;
164 int quality, n;
165
166 if (sysctlbyname(TC_HARDWARE, cbuf, &cbufsiz, NULL, 0) != 0) {
167 (void)printf("\nChecking legacy time implementation "
168 "for %d seconds\n", TIMEOUT);
169 check_timecounter();
170 return;
171 /* NOTREACHED */
172 }
173 (void)printf("%s = %s\n", TC_HARDWARE, cbuf);
174 REQUIRE_LIBC(save = strdup(cbuf), NULL);
175
176 RL(sysctlbyname(TC_CHOICE, ctrbuf, &ctrbufsiz, NULL, 0));
177 (void)printf("%s = %s\n", TC_CHOICE, ctrbuf);
178
179 for (p = ctrbuf, n = 0; sscanf(p, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n",
180 name, &quality, &n) == 2; p += n) {
181 struct timespec ts;
182 int ret;
183
184 if (quality < 0)
185 continue;
186
187 (void)printf("\nChecking %s for %d seconds\n", name, TIMEOUT);
188 CHECK_LIBC(ret = sysctlbyname(TC_HARDWARE, NULL, 0,
189 name, strlen(name)), -1);
190 if (ret == -1)
191 continue;
192
193 /* wait a bit to select new counter in clockinterrupt */
194 ts.tv_sec = 0;
195 ts.tv_nsec = 100000000;
196 (void)nanosleep(&ts, NULL);
197
198 check_timecounter();
199 }
200
201 RL(sysctlbyname(TC_HARDWARE, NULL, 0, save, strlen(save)));
202}
203
204ATF_TP_ADD_TCS(tp)
205{
206
207 ATF_TP_ADD_TC(tp, clock_gettime_real);
208
209 return atf_no_error();
210}
diff --git a/src/regress/lib/libc/sys/t_connect.c b/src/regress/lib/libc/sys/t_connect.c
deleted file mode 100644
index c1730a40c7..0000000000
--- a/src/regress/lib/libc/sys/t_connect.c
+++ /dev/null
@@ -1,135 +0,0 @@
1/* $OpenBSD: t_connect.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $ */
2/* $NetBSD: t_connect.c,v 1.3 2017/01/13 20:09:48 christos Exp $ */
3/*
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "macros.h"
30
31#include <sys/socket.h>
32#include <err.h>
33#include <errno.h>
34#include <string.h>
35#include <unistd.h>
36
37#include <arpa/inet.h>
38#include <netinet/in.h>
39
40#include "atf-c.h"
41
42ATF_TC(connect_low_port);
43ATF_TC_HEAD(connect_low_port, tc)
44{
45 atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
46 "works");
47 atf_tc_set_md_var(tc, "require.user", "root");
48}
49ATF_TC_BODY(connect_low_port, tc)
50{
51 struct sockaddr_in sin, sinlist;
52 int sd, val, slist;
53 socklen_t slen;
54
55 slist = socket(AF_INET, SOCK_STREAM, 0);
56 sd = socket(AF_INET, SOCK_STREAM, 0);
57
58 ATF_REQUIRE(sd > 0);
59 ATF_REQUIRE(slist > 0);
60
61 /* bind listening socket */
62 memset(&sinlist, 0, sizeof(sinlist));
63 sinlist.sin_family = AF_INET;
64 sinlist.sin_port = htons(31522);
65 sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
66
67 ATF_REQUIRE_EQ(bind(slist,
68 (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
69 ATF_REQUIRE_EQ(listen(slist, 1), 0);
70
71 val = IP_PORTRANGE_LOW;
72 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
73 sizeof(val)) == -1)
74 atf_tc_fail("setsockopt failed: %s", strerror(errno));
75
76 memset(&sin, 0, sizeof(sin));
77
78 sin.sin_port = htons(31522);
79 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
80 sin.sin_family = AF_INET;
81
82 if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
83 int serrno = errno;
84 atf_tc_fail("connect failed: %s%s",
85 strerror(serrno),
86 serrno != EACCES ? "" :
87 " (see http://mail-index.netbsd.org/"
88 "source-changes/2007/12/16/0011.html)");
89 }
90
91 slen = sizeof(sin);
92 ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
93 ATF_REQUIRE_EQ(slen, sizeof(sin));
94 ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
95
96 close(sd);
97 close(slist);
98}
99
100ATF_TC(connect_foreign_family);
101ATF_TC_HEAD(connect_foreign_family, tc)
102{
103 atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket "
104 "with a different address family fails");
105}
106ATF_TC_BODY(connect_foreign_family, tc)
107{
108 struct sockaddr_in addr;
109
110 /* addr.sin_family = AF_UNSPEC = 0 */
111 memset(&addr, 0, sizeof(addr));
112
113 /*
114 * it is not necessary to initialize sin_{addr,port} since
115 * those structure members shall not be accessed if connect
116 * fails correctly.
117 */
118
119 int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
120 ATF_REQUIRE(sock != -1);
121
122 ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)));
123 ATF_REQUIRE(EAFNOSUPPORT == errno);
124
125 close(sock);
126}
127
128ATF_TP_ADD_TCS(tp)
129{
130
131 ATF_TP_ADD_TC(tp, connect_low_port);
132 ATF_TP_ADD_TC(tp, connect_foreign_family);
133
134 return atf_no_error();
135}
diff --git a/src/regress/lib/libc/sys/t_dup.c b/src/regress/lib/libc/sys/t_dup.c
deleted file mode 100644
index b461a5f7c8..0000000000
--- a/src/regress/lib/libc/sys/t_dup.c
+++ /dev/null
@@ -1,407 +0,0 @@
1/* $OpenBSD: t_dup.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_dup.c,v 1.9 2017/01/13 20:31:53 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/resource.h>
36#include <sys/stat.h>
37#include <sys/wait.h>
38
39#include "atf-c.h"
40#include <errno.h>
41#include <fcntl.h>
42#include <limits.h>
43#include <stdbool.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <sysexits.h>
49
50static char path[] = "dup";
51static void check_mode(bool, bool, bool);
52
53static void
54check_mode(bool _dup, bool _dup2, bool _dup3)
55{
56 int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR };
57 int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
58 struct stat st, st1;
59 int fd, fd1, fd2;
60 size_t i, j;
61
62 /*
63 * Check that a duplicated descriptor
64 * retains the mode of the original file.
65 */
66 for (i = 0; i < __arraycount(mode); i++) {
67
68 for (j = 0; j < __arraycount(perm); j++) {
69
70 fd1 = open(path, mode[i] | O_CREAT, perm[j]);
71 fd2 = open("/etc/passwd", O_RDONLY);
72
73 ATF_REQUIRE(fd1 >= 0);
74 ATF_REQUIRE(fd2 >= 0);
75
76 if (_dup != false)
77 fd = dup(fd1);
78 else if (_dup2 != false)
79 fd = dup2(fd1, fd2);
80 else if (_dup3 != false)
81 fd = dup3(fd1, fd2, O_CLOEXEC);
82 else {
83 fd = -1;
84 }
85
86 ATF_REQUIRE(fd >= 0);
87
88 (void)memset(&st, 0, sizeof(struct stat));
89 (void)memset(&st1, 0, sizeof(struct stat));
90
91 ATF_REQUIRE(fstat(fd, &st) == 0);
92 ATF_REQUIRE(fstat(fd1, &st1) == 0);
93
94 if (st.st_mode != st1.st_mode)
95 atf_tc_fail("invalid mode");
96
97 (void)close(fd);
98 (void)close(fd1);
99 (void)close(fd2);
100 (void)unlink(path);
101 }
102 }
103}
104
105ATF_TC(dup2_basic);
106ATF_TC_HEAD(dup2_basic, tc)
107{
108 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
109}
110
111ATF_TC_BODY(dup2_basic, tc)
112{
113 int fd, fd1, fd2;
114
115 fd1 = open("/etc/passwd", O_RDONLY);
116 fd2 = open("/etc/passwd", O_RDONLY);
117
118 ATF_REQUIRE(fd1 >= 0);
119 ATF_REQUIRE(fd2 >= 0);
120
121 fd = dup2(fd1, fd2);
122 ATF_REQUIRE(fd >= 0);
123
124 if (fd != fd2)
125 atf_tc_fail("invalid descriptor");
126
127 (void)close(fd);
128 (void)close(fd1);
129
130 ATF_REQUIRE(close(fd2) != 0);
131}
132
133ATF_TC(dup2_err);
134ATF_TC_HEAD(dup2_err, tc)
135{
136 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
137}
138
139ATF_TC_BODY(dup2_err, tc)
140{
141 int fd;
142
143 fd = open("/etc/passwd", O_RDONLY);
144 ATF_REQUIRE(fd >= 0);
145
146 errno = 0;
147 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
148
149 errno = 0;
150 ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
151
152 errno = 0;
153 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
154
155 /*
156 * Note that this should not fail with EINVAL.
157 */
158 ATF_REQUIRE(dup2(fd, fd) != -1);
159
160 (void)close(fd);
161}
162
163ATF_TC(dup2_max);
164ATF_TC_HEAD(dup2_max, tc)
165{
166 atf_tc_set_md_var(tc, "descr", "Test dup2(2) against limits");
167}
168
169ATF_TC_BODY(dup2_max, tc)
170{
171 struct rlimit res;
172
173 (void)memset(&res, 0, sizeof(struct rlimit));
174 (void)getrlimit(RLIMIT_NOFILE, &res);
175
176 errno = 0;
177 ATF_REQUIRE_ERRNO(EBADF, dup2(STDERR_FILENO, res.rlim_cur + 1) == -1);
178}
179
180ATF_TC_WITH_CLEANUP(dup2_mode);
181ATF_TC_HEAD(dup2_mode, tc)
182{
183 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
184}
185
186ATF_TC_BODY(dup2_mode, tc)
187{
188 check_mode(false, true, false);
189}
190
191ATF_TC_CLEANUP(dup2_mode, tc)
192{
193 (void)unlink(path);
194}
195
196
197ATF_TC(dup3_err);
198ATF_TC_HEAD(dup3_err, tc)
199{
200 atf_tc_set_md_var(tc, "descr",
201 "Test error conditions of dup3(2) (PR lib/45148)");
202}
203
204ATF_TC_BODY(dup3_err, tc)
205{
206 int fd;
207
208 fd = open("/etc/passwd", O_RDONLY);
209 ATF_REQUIRE(fd >= 0);
210
211 errno = 0;
212#ifdef __OpenBSD__
213 ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) == -1);
214#else
215 ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
216#endif
217
218 errno = 0;
219#ifdef __OpenBSD__
220 ATF_REQUIRE_ERRNO(EINVAL, dup3(-1, -1, O_CLOEXEC) == -1);
221#else
222 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
223#endif
224
225 errno = 0;
226 ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
227
228 errno = 0;
229 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
230
231 errno = 0;
232 ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1);
233
234 (void)close(fd);
235}
236
237ATF_TC(dup3_max);
238ATF_TC_HEAD(dup3_max, tc)
239{
240 atf_tc_set_md_var(tc, "descr", "Test dup3(2) against limits");
241}
242
243ATF_TC_BODY(dup3_max, tc)
244{
245 struct rlimit res;
246
247 (void)memset(&res, 0, sizeof(struct rlimit));
248 (void)getrlimit(RLIMIT_NOFILE, &res);
249
250 errno = 0;
251 ATF_REQUIRE_ERRNO(EBADF, dup3(STDERR_FILENO,
252 res.rlim_cur + 1, O_CLOEXEC) == -1);
253}
254
255ATF_TC_WITH_CLEANUP(dup3_mode);
256ATF_TC_HEAD(dup3_mode, tc)
257{
258 atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
259}
260
261ATF_TC_BODY(dup3_mode, tc)
262{
263 check_mode(false, false, true);
264}
265
266ATF_TC_CLEANUP(dup3_mode, tc)
267{
268 (void)unlink(path);
269}
270
271ATF_TC(dup_err);
272ATF_TC_HEAD(dup_err, tc)
273{
274 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
275}
276
277ATF_TC_BODY(dup_err, tc)
278{
279
280 errno = 0;
281 ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
282}
283
284ATF_TC_WITH_CLEANUP(dup_max);
285ATF_TC_HEAD(dup_max, tc)
286{
287 atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
288}
289
290ATF_TC_BODY(dup_max, tc)
291{
292 struct rlimit res;
293 int *buf, fd, sta;
294 size_t i, n;
295 pid_t pid;
296
297 pid = fork();
298 ATF_REQUIRE(pid >= 0);
299
300 if (pid == 0) {
301
302 /*
303 * Open a temporary file until the
304 * maximum number of open files is
305 * reached. Ater that dup(2) family
306 * should fail with EMFILE.
307 */
308#ifdef __OpenBSD__
309 (void)closefrom(STDERR_FILENO + 1);
310#else
311 (void)closefrom(0);
312#endif
313 (void)memset(&res, 0, sizeof(struct rlimit));
314
315 n = 10;
316 res.rlim_cur = res.rlim_max = n;
317 if (setrlimit(RLIMIT_NOFILE, &res) != 0)
318 _exit(EX_OSERR);
319
320 buf = calloc(n, sizeof(int));
321
322 if (buf == NULL)
323 _exit(EX_OSERR);
324
325#ifdef __OpenBSD__
326 buf[0] = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
327#else
328 buf[0] = mkstemp(path);
329#endif
330
331 if (buf[0] < 0)
332 _exit(EX_OSERR);
333
334#ifdef __OpenBSD__
335 for (i = 1; i < n - (STDERR_FILENO + 1); i++) {
336#else
337 for (i = 1; i < n; i++) {
338#endif
339
340 buf[i] = open(path, O_RDONLY);
341
342 if (buf[i] < 0)
343 _exit(EX_OSERR);
344 }
345
346 errno = 0;
347 fd = dup(buf[0]);
348
349 if (fd != -1 || errno != EMFILE)
350 _exit(EX_DATAERR);
351
352 _exit(EXIT_SUCCESS);
353 }
354
355 (void)wait(&sta);
356
357 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
358
359 if (WEXITSTATUS(sta) == EX_OSERR)
360 atf_tc_fail("system call error");
361
362 if (WEXITSTATUS(sta) == EX_DATAERR)
363 atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
364
365 atf_tc_fail("unknown error");
366 }
367
368 (void)unlink(path);
369}
370
371ATF_TC_CLEANUP(dup_max, tc)
372{
373 (void)unlink(path);
374}
375
376ATF_TC_WITH_CLEANUP(dup_mode);
377ATF_TC_HEAD(dup_mode, tc)
378{
379 atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
380}
381
382ATF_TC_BODY(dup_mode, tc)
383{
384 check_mode(true, false, false);
385}
386
387ATF_TC_CLEANUP(dup_mode, tc)
388{
389 (void)unlink(path);
390}
391
392ATF_TP_ADD_TCS(tp)
393{
394
395 ATF_TP_ADD_TC(tp, dup2_basic);
396 ATF_TP_ADD_TC(tp, dup2_err);
397 ATF_TP_ADD_TC(tp, dup2_max);
398 ATF_TP_ADD_TC(tp, dup2_mode);
399 ATF_TP_ADD_TC(tp, dup3_err);
400 ATF_TP_ADD_TC(tp, dup3_max);
401 ATF_TP_ADD_TC(tp, dup3_mode);
402 ATF_TP_ADD_TC(tp, dup_err);
403 ATF_TP_ADD_TC(tp, dup_max);
404 ATF_TP_ADD_TC(tp, dup_mode);
405
406 return atf_no_error();
407}
diff --git a/src/regress/lib/libc/sys/t_fork.c b/src/regress/lib/libc/sys/t_fork.c
deleted file mode 100644
index b55b557824..0000000000
--- a/src/regress/lib/libc/sys/t_fork.c
+++ /dev/null
@@ -1,383 +0,0 @@
1/* $OpenBSD: t_fork.c,v 1.5 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_fork.c,v 1.4 2019/04/06 15:41:54 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2018, 2019 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "macros.h"
30
31#include <sys/types.h>
32#include <sys/signal.h>
33#ifdef __OpenBSD__
34#include <sys/proc.h>
35#endif
36#include <sys/sysctl.h>
37#include <sys/wait.h>
38#include <sched.h>
39#include <signal.h>
40#include <stdbool.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <err.h>
44#include <errno.h>
45
46#include "atf-c.h"
47
48#ifdef VFORK
49#define FORK vfork
50#else
51#define FORK fork
52#endif
53
54/*
55 * A child process cannot call atf functions and expect them to magically
56 * work like in the parent.
57 * The printf(3) messaging from a child will not work out of the box as well
58 * without estabilishing a communication protocol with its parent. To not
59 * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
60 * wrapped with ASSERT_EQ()/ASSERT_NEQ() as that is guaranteed to work.
61 */
62#define ASSERT_EQ(x, y) \
63do { \
64 uintmax_t vx = (x); \
65 uintmax_t vy = (y); \
66 int ret = vx == vy; \
67 if (!ret) \
68 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: " \
69 "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__, \
70 #x, vx, #y, vy); \
71} while (/*CONSTCOND*/0)
72
73#define ASSERT_NEQ(x, y) \
74do { \
75 uintmax_t vx = (x); \
76 uintmax_t vy = (y); \
77 int ret = vx != vy; \
78 if (!ret) \
79 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: " \
80 "%s(%ju) != %s(%ju)", __FILE__, __LINE__, __func__, \
81 #x, vx, #y, vy); \
82} while (/*CONSTCOND*/0)
83
84static pid_t
85await_stopped_child(pid_t process)
86{
87 struct kinfo_proc2 *p = NULL;
88 size_t i, len;
89 pid_t child = -1;
90
91 int name[] = {
92 [0] = CTL_KERN,
93 [1] = KERN_PROC2,
94 [2] = KERN_PROC_ALL,
95 [3] = 0,
96 [4] = sizeof(struct kinfo_proc2),
97 [5] = 0
98 };
99
100 const size_t namelen = __arraycount(name);
101
102 /* Await the process becoming a zombie */
103 while(1) {
104 name[5] = 0;
105
106 ASSERT_EQ(sysctl(name, namelen, 0, &len, NULL, 0), 0);
107
108 ASSERT_EQ(reallocarr(&p, len, sizeof(struct kinfo_proc2)), 0);
109
110 name[5] = len;
111
112 ASSERT_EQ(sysctl(name, namelen, p, &len, NULL, 0), 0);
113
114 for (i = 0; i < len/sizeof(struct kinfo_proc2); i++) {
115 if (p[i].p_pid == getpid())
116 continue;
117 if (p[i].p_ppid != process)
118 continue;
119 if (p[i].p_stat != LSSTOP)
120 continue;
121 child = p[i].p_pid;
122 break;
123 }
124
125 if (child != -1)
126 break;
127
128 ASSERT_EQ(usleep(1000), 0);
129 }
130
131 /* Free the buffer */
132 ASSERT_EQ(reallocarr(&p, 0, sizeof(struct kinfo_proc2)), 0);
133
134 return child;
135}
136
137static void
138raise_raw(int sig)
139{
140 int rv, status;
141 pid_t child, parent, watcher, wpid;
142 int expect_core = (sig == SIGABRT) ? 1 : 0;
143
144 /*
145 * Spawn a dedicated thread to watch for a stopped child and emit
146 * the SIGKILL signal to it.
147 *
148 * This is required in vfork(2)ing parent and optional in fork(2).
149 *
150 * vfork(2) might clobber watcher, this means that it's safer and
151 * simpler to reparent this process to initproc and forget about it.
152 */
153 if (sig == SIGSTOP
154#ifndef VFORK
155 || (sig == SIGTSTP || sig == SIGTTIN || sig == SIGTTOU)
156#endif
157 ) {
158
159 parent = getpid();
160
161 watcher = fork();
162 ATF_REQUIRE(watcher != 1);
163 if (watcher == 0) {
164 /* Double fork(2) trick to reparent to initproc */
165 watcher = fork();
166 ASSERT_NEQ(watcher, -1);
167 if (watcher != 0)
168 _exit(0);
169
170 child = await_stopped_child(parent);
171
172 errno = 0;
173 rv = kill(child, SIGKILL);
174 ASSERT_EQ(rv, 0);
175 ASSERT_EQ(errno, 0);
176
177 /* This exit value will be collected by initproc */
178 _exit(0);
179 }
180
181 wpid = waitpid(watcher, &status, 0);
182
183 ATF_REQUIRE_EQ(wpid, watcher);
184
185 ATF_REQUIRE(WIFEXITED(status));
186 ATF_REQUIRE(!WIFCONTINUED(status));
187 ATF_REQUIRE(!WIFSIGNALED(status));
188 ATF_REQUIRE(!WIFSTOPPED(status));
189 ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
190 }
191
192 child = FORK();
193 ATF_REQUIRE(child != 1);
194 if (child == 0) {
195 rv = raise(sig);
196 ASSERT_EQ(rv, 0);
197 _exit(0);
198 }
199 wpid = waitpid(child, &status, 0);
200
201 ATF_REQUIRE_EQ(wpid, child);
202
203 switch (sig) {
204 case SIGKILL:
205 case SIGABRT:
206 case SIGHUP:
207 ATF_REQUIRE(!WIFEXITED(status));
208 ATF_REQUIRE(!WIFCONTINUED(status));
209 ATF_REQUIRE(WIFSIGNALED(status));
210 ATF_REQUIRE(!WIFSTOPPED(status));
211 ATF_REQUIRE_EQ(WTERMSIG(status), sig);
212 ATF_REQUIRE_EQ(!!WCOREDUMP(status), expect_core);
213 break;
214#ifdef VFORK
215 case SIGTSTP:
216 case SIGTTIN:
217 case SIGTTOU:
218#endif
219 case SIGCONT:
220 ATF_REQUIRE(WIFEXITED(status));
221 ATF_REQUIRE(!WIFCONTINUED(status));
222 ATF_REQUIRE(!WIFSIGNALED(status));
223 ATF_REQUIRE(!WIFSTOPPED(status));
224 ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
225 break;
226#ifndef VFORK
227 case SIGTSTP:
228 case SIGTTIN:
229 case SIGTTOU:
230#endif
231 case SIGSTOP:
232 ATF_REQUIRE(!WIFEXITED(status));
233 ATF_REQUIRE(!WIFCONTINUED(status));
234 ATF_REQUIRE(WIFSIGNALED(status));
235 ATF_REQUIRE(!WIFSTOPPED(status));
236 ATF_REQUIRE_EQ(WTERMSIG(status), SIGKILL);
237 ATF_REQUIRE_EQ(!!WCOREDUMP(status), 0);
238 }
239}
240
241#define RAISE(test, sig) \
242ATF_TC(test); \
243ATF_TC_HEAD(test, tc) \
244{ \
245 \
246 atf_tc_set_md_var(tc, "descr", \
247 "raise " #sig " in a child"); \
248} \
249 \
250ATF_TC_BODY(test, tc) \
251{ \
252 \
253 raise_raw(sig); \
254}
255
256RAISE(raise1, SIGKILL) /* non-maskable */
257RAISE(raise2, SIGSTOP) /* non-maskable */
258RAISE(raise3, SIGTSTP) /* ignored in vfork(2) */
259RAISE(raise4, SIGTTIN) /* ignored in vfork(2) */
260RAISE(raise5, SIGTTOU) /* ignored in vfork(2) */
261RAISE(raise6, SIGABRT) /* regular abort trap */
262RAISE(raise7, SIGHUP) /* hangup */
263RAISE(raise8, SIGCONT) /* continued? */
264
265/// ----------------------------------------------------------------------------
266
267static int
268clone_func(void *arg __unused)
269{
270
271 return 0;
272}
273
274static void
275nested_raw(const char *fn, volatile int flags)
276{
277 int status;
278 pid_t child, child2, wpid;
279 const size_t stack_size = 1024 * 1024;
280 void *stack, *stack_base;
281
282 stack = malloc(stack_size);
283 ATF_REQUIRE(stack != NULL);
284
285#ifdef __MACHINE_STACK_GROWS_UP
286 stack_base = stack;
287#else
288 stack_base = (char *)stack + stack_size;
289#endif
290
291 flags |= SIGCHLD;
292
293 child = FORK();
294 ATF_REQUIRE(child != 1);
295 if (child == 0) {
296 if (strcmp(fn, "fork") == 0)
297 child2 = fork();
298 else if (strcmp(fn, "vfork") == 0)
299 child2 = vfork();
300#ifndef __OpenBSD__
301 else if (strcmp(fn, "clone") == 0)
302 child2 = __clone(clone_func, stack_base, flags, NULL);
303#endif
304 else
305 __unreachable();
306
307 ASSERT_NEQ(child2, -1);
308
309 if ((strcmp(fn, "fork") == 0) || (strcmp(fn, "vfork") == 0)) {
310 if (child2 == 0)
311 _exit(0);
312 }
313
314 wpid = waitpid(child2, &status, 0);
315 ASSERT_EQ(child2, wpid);
316 ASSERT_EQ(!!WIFEXITED(status), true);
317 ASSERT_EQ(!!WIFCONTINUED(status), false);
318 ASSERT_EQ(!!WIFSIGNALED(status), false);
319 ASSERT_EQ(!!WIFSTOPPED(status), false);
320 ASSERT_EQ(WEXITSTATUS(status), 0);
321
322 _exit(0);
323 }
324 wpid = waitpid(child, &status, 0);
325
326 ATF_REQUIRE_EQ(wpid, child);
327 ATF_REQUIRE_EQ(!!WIFEXITED(status), true);
328 ATF_REQUIRE_EQ(!!WIFCONTINUED(status), false);
329 ATF_REQUIRE_EQ(!!WIFSIGNALED(status), false);
330 ATF_REQUIRE_EQ(!!WIFSTOPPED(status), false);
331 ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
332}
333
334#define NESTED(test, fn, flags) \
335ATF_TC(test); \
336ATF_TC_HEAD(test, tc) \
337{ \
338 \
339 atf_tc_set_md_var(tc, "descr", \
340 "Test nested " #fn " in a child"); \
341} \
342 \
343ATF_TC_BODY(test, tc) \
344{ \
345 \
346 nested_raw(#fn, flags); \
347}
348
349NESTED(nested_fork, fork, 0)
350NESTED(nested_vfork, vfork, 0)
351#ifndef __OpenBSD__
352NESTED(nested_clone, clone, 0)
353NESTED(nested_clone_vm, clone, CLONE_VM)
354NESTED(nested_clone_fs, clone, CLONE_FS)
355NESTED(nested_clone_files, clone, CLONE_FILES)
356//NESTED(nested_clone_sighand, clone, CLONE_SIGHAND) // XXX
357NESTED(nested_clone_vfork, clone, CLONE_VFORK)
358#endif
359
360ATF_TP_ADD_TCS(tp)
361{
362 ATF_TP_ADD_TC(tp, raise1);
363 ATF_TP_ADD_TC(tp, raise2);
364 ATF_TP_ADD_TC(tp, raise3);
365 ATF_TP_ADD_TC(tp, raise4);
366 ATF_TP_ADD_TC(tp, raise5);
367 ATF_TP_ADD_TC(tp, raise6);
368 ATF_TP_ADD_TC(tp, raise7);
369 ATF_TP_ADD_TC(tp, raise8);
370
371 ATF_TP_ADD_TC(tp, nested_fork);
372 ATF_TP_ADD_TC(tp, nested_vfork);
373#ifndef __OpenBSD__
374 ATF_TP_ADD_TC(tp, nested_clone);
375 ATF_TP_ADD_TC(tp, nested_clone_vm);
376 ATF_TP_ADD_TC(tp, nested_clone_fs);
377 ATF_TP_ADD_TC(tp, nested_clone_files);
378// ATF_TP_ADD_TC(tp, nested_clone_sighand); // XXX
379 ATF_TP_ADD_TC(tp, nested_clone_vfork);
380#endif
381
382 return atf_no_error();
383}
diff --git a/src/regress/lib/libc/sys/t_fsync.c b/src/regress/lib/libc/sys/t_fsync.c
deleted file mode 100644
index 66653df633..0000000000
--- a/src/regress/lib/libc/sys/t_fsync.c
+++ /dev/null
@@ -1,125 +0,0 @@
1/* $OpenBSD: t_fsync.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_fsync.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42#include "atf-c.h"
43
44ATF_TC(fsync_err);
45ATF_TC_HEAD(fsync_err, tc)
46{
47 atf_tc_set_md_var(tc, "descr",
48 "Test error conditions of fsync(2) (PR kern/30)");
49}
50
51ATF_TC_BODY(fsync_err, tc)
52{
53 int i, fd[2];
54
55 /*
56 * The fsync(2) call should fail with EBADF
57 * when the 'fd' is not a valid descriptor.
58 */
59 for (i = 1; i < 1024; i = i + 128) {
60
61 errno = 0;
62
63 ATF_REQUIRE(fsync(-i) == -1);
64 ATF_REQUIRE(errno == EBADF);
65 }
66
67 /*
68 * On the other hand, EINVAL should follow
69 * if the operation is not possible with
70 * the file descriptor.
71 */
72 ATF_REQUIRE(pipe(fd) == 0);
73
74 errno = 0;
75
76 ATF_REQUIRE(fsync(fd[0]) == -1);
77 ATF_REQUIRE(errno == EINVAL);
78
79 errno = 0;
80
81 ATF_REQUIRE(fsync(fd[1]) == -1);
82 ATF_REQUIRE(errno == EINVAL);
83
84 ATF_REQUIRE(close(fd[0]) == 0);
85 ATF_REQUIRE(close(fd[1]) == 0);
86}
87
88ATF_TC(fsync_sync);
89ATF_TC_HEAD(fsync_sync, tc)
90{
91 atf_tc_set_md_var(tc, "descr", "A basic test of fsync(2)");
92}
93
94ATF_TC_BODY(fsync_sync, tc)
95{
96 char buf[128];
97 int fd, i;
98
99 for (i = 0; i < 10; i++) {
100
101 (void)snprintf(buf, sizeof(buf), "t_fsync-%d", i);
102
103#ifdef __OpenBSD__
104 fd = open(buf, O_CREAT|O_EXCL|O_RDWR, 0600);
105#else
106 fd = mkstemp(buf);
107#endif
108
109 ATF_REQUIRE(fd != -1);
110 ATF_REQUIRE(write(fd, "0", 1) == 1);
111 ATF_REQUIRE(fsync(fd) == 0);
112
113 ATF_REQUIRE(unlink(buf) == 0);
114 ATF_REQUIRE(close(fd) == 0);
115 }
116}
117
118ATF_TP_ADD_TCS(tp)
119{
120
121 ATF_TP_ADD_TC(tp, fsync_err);
122 ATF_TP_ADD_TC(tp, fsync_sync);
123
124 return atf_no_error();
125}
diff --git a/src/regress/lib/libc/sys/t_getgroups.c b/src/regress/lib/libc/sys/t_getgroups.c
deleted file mode 100644
index a17c9c6778..0000000000
--- a/src/regress/lib/libc/sys/t_getgroups.c
+++ /dev/null
@@ -1,176 +0,0 @@
1/* $OpenBSD: t_getgroups.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <limits.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44ATF_TC(getgroups_err);
45ATF_TC_HEAD(getgroups_err, tc)
46{
47 atf_tc_set_md_var(tc, "descr", "Test errors in getgroups(2)");
48}
49
50ATF_TC_BODY(getgroups_err, tc)
51{
52 gid_t gidset[NGROUPS_MAX];
53
54 errno = 0;
55
56#if __OpenBSD__
57 ATF_REQUIRE(getgroups(NGROUPS_MAX, (gid_t *)-1) == -1);
58#else
59 ATF_REQUIRE(getgroups(10, (gid_t *)-1) == -1);
60#endif
61 ATF_REQUIRE(errno == EFAULT);
62
63 errno = 0;
64
65 ATF_REQUIRE(getgroups(-1, gidset) == -1);
66 ATF_REQUIRE(errno == EINVAL);
67}
68
69ATF_TC(getgroups_getgid);
70ATF_TC_HEAD(getgroups_getgid, tc)
71{
72 atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)");
73}
74
75ATF_TC_BODY(getgroups_getgid, tc)
76{
77 gid_t gidset[NGROUPS_MAX];
78 gid_t gid = getgid();
79 int i, n;
80
81 /*
82 * Check that getgid(2) is found from
83 * the GIDs returned by getgroups(2).
84 */
85 n = getgroups(NGROUPS_MAX, gidset);
86
87 for (i = 0; i < n; i++) {
88
89 if (gidset[i] == gid)
90 return;
91 }
92
93 atf_tc_fail("getgid(2) not found from getgroups(2)");
94}
95
96ATF_TC(getgroups_setgid);
97ATF_TC_HEAD(getgroups_setgid, tc)
98{
99 atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)");
100 atf_tc_set_md_var(tc, "require.user", "root");
101}
102
103ATF_TC_BODY(getgroups_setgid, tc)
104{
105 gid_t gidset[NGROUPS_MAX];
106 int i, n, rv, sta;
107 pid_t pid;
108
109 /*
110 * Check that we can setgid(2)
111 * to the returned group IDs.
112 */
113 n = getgroups(NGROUPS_MAX, gidset);
114 ATF_REQUIRE(n >= 0);
115
116 for (i = 0; i < n; i++) {
117
118 pid = fork();
119 ATF_REQUIRE(pid >= 0);
120
121 if (pid == 0) {
122
123 rv = setgid(gidset[i]);
124
125 if (rv != 0)
126 _exit(EXIT_FAILURE);
127
128 _exit(EXIT_SUCCESS);
129 }
130
131 (void)wait(&sta);
132
133 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
134 atf_tc_fail("getgroups(2) is inconsistent");
135 }
136}
137
138ATF_TC(getgroups_zero);
139ATF_TC_HEAD(getgroups_zero, tc)
140{
141 atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param");
142}
143
144ATF_TC_BODY(getgroups_zero, tc)
145{
146 const gid_t val = 123456789;
147 gid_t gidset[NGROUPS_MAX];
148 size_t i;
149
150 /*
151 * If the first parameter is zero, the number
152 * of groups should be returned but the supplied
153 * buffer should remain intact.
154 */
155 for (i = 0; i < __arraycount(gidset); i++)
156 gidset[i] = val;
157
158 ATF_REQUIRE(getgroups(0, gidset) >= 0);
159
160 for (i = 0; i < __arraycount(gidset); i++) {
161
162 if (gidset[i] != val)
163 atf_tc_fail("getgroups(2) modified the buffer");
164 }
165}
166
167ATF_TP_ADD_TCS(tp)
168{
169
170 ATF_TP_ADD_TC(tp, getgroups_err);
171 ATF_TP_ADD_TC(tp, getgroups_getgid);
172 ATF_TP_ADD_TC(tp, getgroups_setgid);
173 ATF_TP_ADD_TC(tp, getgroups_zero);
174
175 return atf_no_error();
176}
diff --git a/src/regress/lib/libc/sys/t_getitimer.c b/src/regress/lib/libc/sys/t_getitimer.c
deleted file mode 100644
index b18bc75cd0..0000000000
--- a/src/regress/lib/libc/sys/t_getitimer.c
+++ /dev/null
@@ -1,215 +0,0 @@
1/* $OpenBSD: t_getitimer.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getitimer.c,v 1.3 2019/07/13 12:44:02 gson Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/time.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <limits.h>
40#include <signal.h>
41#include <string.h>
42#include <unistd.h>
43
44static bool fail;
45static void sighandler(int);
46
47static void
48sighandler(int signo)
49{
50
51 if (signo == SIGALRM || signo == SIGVTALRM)
52 fail = false;
53}
54
55ATF_TC(getitimer_empty);
56ATF_TC_HEAD(getitimer_empty, tc)
57{
58 atf_tc_set_md_var(tc, "descr", "getitimer(2) before setitimer(2)");
59}
60
61ATF_TC_BODY(getitimer_empty, tc)
62{
63 struct itimerval it;
64
65 /*
66 * Verify that the passed structure remains
67 * empty after calling getitimer(2) but before
68 * actually arming the timer with setitimer(2).
69 */
70 (void)memset(&it, 0, sizeof(struct itimerval));
71
72 ATF_REQUIRE(getitimer(ITIMER_REAL, &it) == 0);
73
74 if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
75 goto fail;
76
77 ATF_REQUIRE(getitimer(ITIMER_VIRTUAL, &it) == 0);
78
79 if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
80 goto fail;
81
82 ATF_REQUIRE(getitimer(ITIMER_PROF, &it) == 0);
83
84 if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
85 goto fail;
86
87 return;
88
89fail:
90 atf_tc_fail("getitimer(2) modfied the timer before it was armed");
91}
92
93ATF_TC(getitimer_err);
94ATF_TC_HEAD(getitimer_err, tc)
95{
96 atf_tc_set_md_var(tc, "descr", "Test errors from getitimer(2)");
97}
98
99ATF_TC_BODY(getitimer_err, tc)
100{
101 struct itimerval it;
102
103 errno = 0;
104 ATF_REQUIRE_ERRNO(EINVAL, getitimer(-1, &it) == -1);
105
106 errno = 0;
107 ATF_REQUIRE_ERRNO(EINVAL, getitimer(INT_MAX, &it) == -1);
108
109 errno = 0;
110 ATF_REQUIRE_ERRNO(EFAULT, getitimer(ITIMER_REAL, (void *)-1) == -1);
111}
112
113ATF_TC(setitimer_basic);
114ATF_TC_HEAD(setitimer_basic, tc)
115{
116 atf_tc_set_md_var(tc, "descr", "A basic test of setitimer(2)");
117}
118
119ATF_TC_BODY(setitimer_basic, tc)
120{
121 struct itimerval it;
122
123 it.it_value.tv_sec = 0;
124 it.it_value.tv_usec = 100;
125
126 it.it_interval.tv_sec = 0;
127 it.it_interval.tv_usec = 0;
128
129 fail = true;
130
131 ATF_REQUIRE(signal(SIGALRM, sighandler) != SIG_ERR);
132 ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);
133
134 /*
135 * Although the interaction between
136 * setitimer(2) and sleep(3) can be
137 * unspecified, it is assumed that one
138 * second suspension will be enough for
139 * the timer to fire.
140 */
141 (void)sleep(1);
142
143 if (fail != false)
144 atf_tc_fail("timer did not fire");
145}
146
147ATF_TC(setitimer_err);
148ATF_TC_HEAD(setitimer_err, tc)
149{
150 atf_tc_set_md_var(tc, "descr", "Test errors from setitimer(2)"
151 " (PR standards/44927)");
152}
153
154ATF_TC_BODY(setitimer_err, tc)
155{
156 struct itimerval it, ot;
157
158 errno = 0;
159 ATF_REQUIRE_ERRNO(EINVAL, setitimer(-1, &it, &ot) == -1);
160
161 errno = 0;
162 ATF_REQUIRE_ERRNO(EINVAL, setitimer(INT_MAX, &it, &ot) == -1);
163
164 errno = 0;
165 ATF_REQUIRE_ERRNO(EFAULT, setitimer(ITIMER_REAL,(void*)-1, &ot) == -1);
166}
167
168ATF_TC(setitimer_old);
169ATF_TC_HEAD(setitimer_old, tc)
170{
171 atf_tc_set_md_var(tc, "descr", "Test old values from setitimer(2)");
172}
173
174ATF_TC_BODY(setitimer_old, tc)
175{
176 struct itimerval it, ot;
177
178 /*
179 * Make two calls; the second one should store the old
180 * timer value which should be the same as that set in
181 * the first call, or slightly less due to time passing
182 * between the two calls.
183 */
184 it.it_value.tv_sec = 4;
185 it.it_value.tv_usec = 999999;
186
187 it.it_interval.tv_sec = 0;
188 it.it_interval.tv_usec = 0;
189
190 ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
191
192 it.it_value.tv_sec = 2;
193 it.it_value.tv_usec = 1;
194
195 it.it_interval.tv_sec = 0;
196 it.it_interval.tv_usec = 0;
197
198 ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
199
200 /* Check seconds only as microseconds may have decremented */
201 if (ot.it_value.tv_sec != 4)
202 atf_tc_fail("setitimer(2) did not store old values");
203}
204
205ATF_TP_ADD_TCS(tp)
206{
207
208 ATF_TP_ADD_TC(tp, getitimer_empty);
209 ATF_TP_ADD_TC(tp, getitimer_err);
210 ATF_TP_ADD_TC(tp, setitimer_basic);
211 ATF_TP_ADD_TC(tp, setitimer_err);
212 ATF_TP_ADD_TC(tp, setitimer_old);
213
214 return atf_no_error();
215}
diff --git a/src/regress/lib/libc/sys/t_getlogin.c b/src/regress/lib/libc/sys/t_getlogin.c
deleted file mode 100644
index b4f06fdee6..0000000000
--- a/src/regress/lib/libc/sys/t_getlogin.c
+++ /dev/null
@@ -1,238 +0,0 @@
1/* $OpenBSD: t_getlogin.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <stdlib.h>
40#include <string.h>
41#include <limits.h>
42#include <unistd.h>
43
44ATF_TC(getlogin_r_err);
45ATF_TC_HEAD(getlogin_r_err, tc)
46{
47 atf_tc_set_md_var(tc, "descr", "Test errors from getlogin_r(2)");
48}
49
50ATF_TC_BODY(getlogin_r_err, tc)
51{
52 char small[0];
53
54 ATF_REQUIRE(getlogin_r(small, sizeof(small)) == ERANGE);
55}
56
57ATF_TC(getlogin_same);
58ATF_TC_HEAD(getlogin_same, tc)
59{
60 atf_tc_set_md_var(tc, "descr", "getlogin(2) vs. getlogin_r(2)");
61}
62
63ATF_TC_BODY(getlogin_same, tc)
64{
65 char buf[LOGIN_NAME_MAX];
66 char *str;
67
68 str = getlogin();
69
70 if (str == NULL)
71 return;
72
73 ATF_REQUIRE(getlogin_r(buf, sizeof(buf)) == 0);
74
75 if (strcmp(str, buf) != 0)
76 atf_tc_fail("getlogin(2) and getlogin_r(2) differ");
77}
78
79ATF_TC(setlogin_basic);
80ATF_TC_HEAD(setlogin_basic, tc)
81{
82 atf_tc_set_md_var(tc, "descr", "Test that setlogin(2) works");
83 atf_tc_set_md_var(tc, "require.user", "root");
84}
85
86ATF_TC_BODY(setlogin_basic, tc)
87{
88 char *name;
89 pid_t pid;
90 int sta;
91
92 pid = fork();
93 ATF_REQUIRE(pid >= 0);
94
95 if (pid == 0) {
96
97 (void)setsid();
98
99 if (setlogin("foobar") != 0)
100 _exit(EXIT_FAILURE);
101
102 name = getlogin();
103
104 if (name == NULL)
105 _exit(EXIT_FAILURE);
106
107 if (strcmp(name, "foobar") != 0)
108 _exit(EXIT_FAILURE);
109
110 _exit(EXIT_SUCCESS);
111 }
112
113 (void)wait(&sta);
114
115 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
116 atf_tc_fail("setlogin(2) failed to set login name");
117}
118
119ATF_TC(setlogin_err);
120ATF_TC_HEAD(setlogin_err, tc)
121{
122 atf_tc_set_md_var(tc, "descr", "Test errors from setlogin(2)");
123 atf_tc_set_md_var(tc, "require.user", "root");
124}
125
126ATF_TC_BODY(setlogin_err, tc)
127{
128 char buf[LOGIN_NAME_MAX + 1];
129 char *name;
130 pid_t pid;
131 int sta;
132
133 pid = fork();
134 ATF_REQUIRE(pid >= 0);
135
136 (void)memset(buf, 'x', sizeof(buf));
137
138 if (pid == 0) {
139
140 (void)setsid();
141
142 errno = 0;
143
144 if (setlogin(buf) != -1)
145 _exit(EINVAL);
146
147 if (errno != EINVAL)
148 _exit(EINVAL);
149
150 errno = 0;
151
152 if (setlogin((void *)-1) != -1)
153 _exit(EFAULT);
154
155 if (errno != EFAULT)
156 _exit(EFAULT);
157
158 name = getlogin();
159
160 if (name == NULL)
161 _exit(EXIT_FAILURE);
162
163 if (strcmp(name, "foobar") == 0)
164 _exit(EXIT_FAILURE);
165
166 _exit(EXIT_SUCCESS);
167 }
168
169 (void)wait(&sta);
170
171 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
172
173 if (WEXITSTATUS(sta) == EFAULT)
174 atf_tc_fail("expected EFAULT, but the call succeeded");
175
176 if (WEXITSTATUS(sta) == EINVAL)
177 atf_tc_fail("expected EINVAL, but the call succeeded");
178
179 atf_tc_fail("setlogin(2) failed, but login name was set");
180 }
181}
182
183ATF_TC(setlogin_perm);
184ATF_TC_HEAD(setlogin_perm, tc)
185{
186 atf_tc_set_md_var(tc, "descr", "Test setlogin(2) as normal user");
187 atf_tc_set_md_var(tc, "require.user", "unprivileged");
188}
189
190ATF_TC_BODY(setlogin_perm, tc)
191{
192 char *name;
193 pid_t pid;
194 int sta;
195
196 pid = fork();
197 ATF_REQUIRE(pid >= 0);
198
199 if (pid == 0) {
200
201 (void)setsid();
202
203 errno = 0;
204
205 if (setlogin("foobar") != -1)
206 _exit(EXIT_FAILURE);
207
208 if (errno != EPERM)
209 _exit(EXIT_FAILURE);
210
211 name = getlogin();
212
213 if (name == NULL)
214 _exit(EXIT_FAILURE);
215
216 if (strcmp(name, "foobar") == 0)
217 _exit(EXIT_FAILURE);
218
219 _exit(EXIT_SUCCESS);
220 }
221
222 (void)wait(&sta);
223
224 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
225 atf_tc_fail("login name was set as an unprivileged user");
226}
227
228ATF_TP_ADD_TCS(tp)
229{
230
231 ATF_TP_ADD_TC(tp, getlogin_r_err);
232 ATF_TP_ADD_TC(tp, getlogin_same);
233 ATF_TP_ADD_TC(tp, setlogin_basic);
234 ATF_TP_ADD_TC(tp, setlogin_err);
235 ATF_TP_ADD_TC(tp, setlogin_perm);
236
237 return atf_no_error();
238}
diff --git a/src/regress/lib/libc/sys/t_getpid.c b/src/regress/lib/libc/sys/t_getpid.c
deleted file mode 100644
index db9f8c8746..0000000000
--- a/src/regress/lib/libc/sys/t_getpid.c
+++ /dev/null
@@ -1,135 +0,0 @@
1/* $OpenBSD: t_getpid.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getpid.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include <stdlib.h>
38#include <pthread.h>
39#include <unistd.h>
40
41#include "atf-c.h"
42
43static int maxiter = 10;
44static void *threadfunc(void *);
45
46static void *
47threadfunc(void *arg)
48{
49 *(pid_t *)arg = getpid();
50
51 return NULL;
52}
53
54ATF_TC(getpid_process);
55ATF_TC_HEAD(getpid_process, tc)
56{
57 atf_tc_set_md_var(tc, "descr", "Test getpid(2) with processes");
58}
59
60ATF_TC_BODY(getpid_process, tc)
61{
62 pid_t ppid, fpid, cpid, tpid, wpid;
63 int i, sta;
64
65 for (i = 0; i < maxiter; i++) {
66
67 tpid = getpid();
68 fpid = fork();
69
70 ATF_REQUIRE(fpid >= 0);
71
72 if (fpid == 0) {
73
74 cpid = getpid();
75 ppid = getppid();
76
77 if (tpid != ppid)
78 _exit(EXIT_FAILURE);
79
80 if (cpid == ppid)
81 _exit(EXIT_FAILURE);
82
83 if (tpid == fpid)
84 _exit(EXIT_FAILURE);
85
86 _exit(EXIT_SUCCESS);
87 }
88
89 wpid = wait(&sta);
90
91 if (wpid != fpid)
92 atf_tc_fail("PID mismatch");
93
94 ATF_REQUIRE(WIFEXITED(sta) != 0);
95
96 if (WEXITSTATUS(sta) != EXIT_SUCCESS)
97 atf_tc_fail("PID mismatch");
98 }
99}
100
101ATF_TC(getpid_thread);
102ATF_TC_HEAD(getpid_thread, tc)
103{
104 atf_tc_set_md_var(tc, "descr", "Test getpid(2) with threads");
105}
106
107ATF_TC_BODY(getpid_thread, tc)
108{
109 pid_t pid, tpid;
110 pthread_t tid;
111 int i, rv;
112
113 for (i = 0; i < maxiter; i++) {
114
115 pid = getpid();
116
117 rv = pthread_create(&tid, NULL, threadfunc, &tpid);
118 ATF_REQUIRE(rv == 0);
119
120 rv = pthread_join(tid, NULL);
121 ATF_REQUIRE(rv == 0);
122
123 if (pid != tpid)
124 atf_tc_fail("Unequal PIDs");
125 }
126}
127
128ATF_TP_ADD_TCS(tp)
129{
130
131 ATF_TP_ADD_TC(tp, getpid_process);
132 ATF_TP_ADD_TC(tp, getpid_thread);
133
134 return atf_no_error();
135}
diff --git a/src/regress/lib/libc/sys/t_getrusage.c b/src/regress/lib/libc/sys/t_getrusage.c
deleted file mode 100644
index 1a9e3d139c..0000000000
--- a/src/regress/lib/libc/sys/t_getrusage.c
+++ /dev/null
@@ -1,286 +0,0 @@
1/* $OpenBSD: t_getrusage.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getrusage.c,v 1.8 2018/05/09 08:45:03 mrg Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/resource.h>
36#include <sys/time.h>
37
38#include "atf-c.h"
39#include <stdio.h>
40#include <errno.h>
41#include <limits.h>
42#include <signal.h>
43#include <stdint.h>
44#include <stdlib.h>
45#include <string.h>
46#include <fcntl.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49
50static void work(void);
51static void sighandler(int);
52
53static const size_t maxiter = 2000;
54
55static void
56sighandler(int signo __unused)
57{
58 /* Nothing. */
59}
60
61static void
62work(void)
63{
64 size_t n = UINT16_MAX * 10;
65
66 while (n > 0) {
67#ifdef __or1k__
68 asm volatile("l.nop"); /* Do something. */
69#elif defined(__ia64__)
70 asm volatile("nop 0"); /* Do something. */
71#else
72 asm volatile("nop"); /* Do something. */
73#endif
74 n--;
75 }
76}
77
78ATF_TC(getrusage_err);
79ATF_TC_HEAD(getrusage_err, tc)
80{
81 atf_tc_set_md_var(tc, "descr", "Test error conditions");
82}
83
84ATF_TC_BODY(getrusage_err, tc)
85{
86 struct rusage ru;
87
88 errno = 0;
89
90 ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0);
91 ATF_REQUIRE(errno == EINVAL);
92
93 errno = 0;
94
95 ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0);
96 ATF_REQUIRE(errno == EFAULT);
97}
98
99ATF_TC(getrusage_sig);
100ATF_TC_HEAD(getrusage_sig, tc)
101{
102 atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)");
103}
104
105ATF_TC_BODY(getrusage_sig, tc)
106{
107 struct rusage ru;
108 const long n = 5;
109 int i;
110
111 /*
112 * Test that signals are recorded.
113 */
114 ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);
115
116 for (i = 0; i < n; i++)
117 ATF_REQUIRE(raise(SIGUSR1) == 0);
118
119 (void)memset(&ru, 0, sizeof(struct rusage));
120 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
121
122 if (n != ru.ru_nsignals)
123 atf_tc_fail("getrusage(2) did not record signals");
124}
125
126ATF_TC(getrusage_maxrss);
127ATF_TC_HEAD(getrusage_maxrss, tc)
128{
129 atf_tc_set_md_var(tc, "descr", "Test maxrss growing with getrusage(2)");
130}
131
132ATF_TC_BODY(getrusage_maxrss, tc)
133{
134 struct rusage ru;
135 long maxrss;
136 int i, fd;
137
138#define DUMP_FILE "dump"
139
140 fd = open(DUMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0222);
141 ATF_REQUIRE(fd != -1);
142
143 (void)memset(&ru, 0, sizeof(struct rusage));
144 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
145 maxrss = ru.ru_maxrss;
146
147#define CHUNK (1024 * 1024)
148 for (i = 0; i < 40; i++) {
149 void *p = malloc(CHUNK);
150 memset(p, 0, CHUNK);
151 write(fd, p, CHUNK);
152 }
153 close(fd);
154 unlink(DUMP_FILE);
155
156 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
157 ATF_REQUIRE_MSG(maxrss < ru.ru_maxrss,
158 "maxrss: %ld, ru.ru_maxrss: %ld", maxrss, ru.ru_maxrss);
159}
160
161ATF_TC(getrusage_msgsnd);
162ATF_TC_HEAD(getrusage_msgsnd, tc)
163{
164 atf_tc_set_md_var(tc, "descr", "Test send growing with getrusage(2)");
165}
166
167ATF_TC_BODY(getrusage_msgsnd, tc)
168{
169 struct rusage ru;
170 long msgsnd;
171 int s, i;
172 struct sockaddr_in sin;
173
174 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
175 msgsnd = ru.ru_msgsnd;
176
177 s = socket(AF_INET, SOCK_DGRAM, 0);
178 ATF_REQUIRE(s >= 0);
179 memset(&sin, 0, sizeof(sin));
180 sin.sin_family = AF_INET;
181 sin.sin_len = sizeof(sin);
182 sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
183 sin.sin_port = htons(3333);
184
185 for (i = 0; i < 10; i++)
186 ATF_REQUIRE(sendto(s, &sin, sizeof(sin), 0, (void *)&sin,
187 (socklen_t)sizeof(sin)) != -1);
188
189 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
190 ATF_REQUIRE(msgsnd + 10 == ru.ru_msgsnd);
191 close(s);
192}
193
194ATF_TC(getrusage_utime_back);
195ATF_TC_HEAD(getrusage_utime_back, tc)
196{
197 atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)");
198}
199
200ATF_TC_BODY(getrusage_utime_back, tc)
201{
202 struct rusage ru1, ru2;
203 size_t i;
204
205 /*
206 * Test that two consecutive calls are sane.
207 */
208#ifndef __OpenBSD__
209 atf_tc_expect_fail("PR kern/30115");
210#endif
211
212 for (i = 0; i < maxiter; i++) {
213
214 (void)memset(&ru1, 0, sizeof(struct rusage));
215 (void)memset(&ru2, 0, sizeof(struct rusage));
216
217 work();
218
219 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0);
220
221 work();
222
223 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0);
224
225 if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0)
226 atf_tc_fail("user time went backwards");
227 }
228
229#ifndef __OpenBSD__
230 atf_tc_fail("anticipated error did not occur");
231#endif
232}
233
234ATF_TC(getrusage_utime_zero);
235ATF_TC_HEAD(getrusage_utime_zero, tc)
236{
237 atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)");
238}
239
240ATF_TC_BODY(getrusage_utime_zero, tc)
241{
242 struct rusage ru;
243 size_t i;
244
245 /*
246 * Test that getrusage(2) does not return
247 * zero user time for the calling process.
248 *
249 * See also (duplicate) PR port-amd64/41734.
250 */
251#ifndef __OpenBSD__
252 atf_tc_expect_fail("PR kern/30115");
253#endif
254
255 for (i = 0; i < maxiter; i++) {
256
257 work();
258#ifdef __OpenBSD__
259 }
260#endif
261
262 (void)memset(&ru, 0, sizeof(struct rusage));
263
264 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
265
266 if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0)
267 atf_tc_fail("zero user time from getrusage(2)");
268#ifndef __OpenBSD__
269 }
270
271 atf_tc_fail("anticipated error did not occur");
272#endif
273}
274
275ATF_TP_ADD_TCS(tp)
276{
277
278 ATF_TP_ADD_TC(tp, getrusage_err);
279 ATF_TP_ADD_TC(tp, getrusage_sig);
280 ATF_TP_ADD_TC(tp, getrusage_maxrss);
281 ATF_TP_ADD_TC(tp, getrusage_msgsnd);
282 ATF_TP_ADD_TC(tp, getrusage_utime_back);
283 ATF_TP_ADD_TC(tp, getrusage_utime_zero);
284
285 return atf_no_error();
286}
diff --git a/src/regress/lib/libc/sys/t_getsid.c b/src/regress/lib/libc/sys/t_getsid.c
deleted file mode 100644
index 9bd82cd2ba..0000000000
--- a/src/regress/lib/libc/sys/t_getsid.c
+++ /dev/null
@@ -1,120 +0,0 @@
1/* $OpenBSD: t_getsid.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_getsid.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include <errno.h>
38#include <stdlib.h>
39#include <unistd.h>
40
41#include "atf-c.h"
42
43ATF_TC(getsid_current);
44ATF_TC_HEAD(getsid_current, tc)
45{
46 atf_tc_set_md_var(tc, "descr", "Test getsid(0)");
47}
48
49ATF_TC_BODY(getsid_current, tc)
50{
51 pid_t sid;
52
53 sid = getsid(0);
54 ATF_REQUIRE(sid != -1);
55
56 if (sid != getsid(getpid()))
57 atf_tc_fail("getsid(0) did not match the calling process");
58}
59
60ATF_TC(getsid_err);
61ATF_TC_HEAD(getsid_err, tc)
62{
63 atf_tc_set_md_var(tc, "descr", "Test error conditions in getsid(2)");
64}
65
66ATF_TC_BODY(getsid_err, tc)
67{
68
69 errno = 0;
70
71 ATF_REQUIRE(getsid(-1) == -1);
72 ATF_REQUIRE(errno == ESRCH);
73}
74
75ATF_TC(getsid_process);
76ATF_TC_HEAD(getsid_process, tc)
77{
78 atf_tc_set_md_var(tc, "descr", "Test getsid(2) with processes");
79}
80
81ATF_TC_BODY(getsid_process, tc)
82{
83 pid_t csid, pid, ppid, sid;
84 int sta;
85
86 sid = getsid(0);
87 pid = fork();
88
89 ATF_REQUIRE(pid >= 0);
90 ATF_REQUIRE(sid != -1);
91
92 if (pid == 0) {
93
94 csid = getsid(0);
95 ppid = getppid();
96
97 if (sid != csid)
98 _exit(EXIT_FAILURE);
99
100 if (getsid(ppid) != csid)
101 _exit(EXIT_FAILURE);
102
103 _exit(EXIT_SUCCESS);
104 }
105
106 (void)wait(&sta);
107
108 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
109 atf_tc_fail("invalid session ID");
110}
111
112ATF_TP_ADD_TCS(tp)
113{
114
115 ATF_TP_ADD_TC(tp, getsid_current);
116 ATF_TP_ADD_TC(tp, getsid_err);
117 ATF_TP_ADD_TC(tp, getsid_process);
118
119 return atf_no_error();
120}
diff --git a/src/regress/lib/libc/sys/t_getsockname.c b/src/regress/lib/libc/sys/t_getsockname.c
deleted file mode 100644
index 8b0accd7a0..0000000000
--- a/src/regress/lib/libc/sys/t_getsockname.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* $OpenBSD: t_getsockname.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_getsockname.c,v 1.1 2016/07/30 11:03:54 njoly Exp $ */
3/*
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/socket.h>
33#include <sys/un.h>
34
35#include <string.h>
36#include <unistd.h>
37
38#include "atf-c.h"
39
40ATF_TC(getsockname_unix);
41
42ATF_TC_HEAD(getsockname_unix, tc)
43{
44 atf_tc_set_md_var(tc, "descr", "Checks getsockname with UNIX domain");
45}
46
47ATF_TC_BODY(getsockname_unix, tc)
48{
49 const char *path = "sock.unix";
50 int sd;
51 socklen_t len;
52 struct sockaddr_un sun;
53
54 sd = socket(AF_UNIX, SOCK_STREAM, 0);
55 ATF_REQUIRE(sd != -1);
56
57 len = sizeof(sun);
58 memset(&sun, 0, sizeof(sun));
59 ATF_REQUIRE(getsockname(sd, (struct sockaddr *)&sun, &len) != -1);
60 ATF_CHECK(sun.sun_family == AF_UNIX);
61 ATF_CHECK(strcmp(sun.sun_path, "") == 0);
62
63 len = sizeof(sun);
64 memset(&sun, 0, sizeof(sun));
65 sun.sun_family = AF_UNIX;
66 strcpy(sun.sun_path, path);
67 ATF_REQUIRE(bind(sd, (struct sockaddr *)&sun, len) != -1);
68
69 len = sizeof(sun);
70 memset(&sun, 0, sizeof(sun));
71 ATF_REQUIRE(getsockname(sd, (struct sockaddr *)&sun, &len) != -1);
72 ATF_CHECK(sun.sun_family == AF_UNIX);
73 ATF_CHECK(strcmp(sun.sun_path, path) == 0);
74
75 ATF_REQUIRE(close(sd) != -1);
76 ATF_REQUIRE(unlink(path) != -1);
77}
78
79ATF_TP_ADD_TCS(tp)
80{
81
82 ATF_TP_ADD_TC(tp, getsockname_unix);
83
84 return atf_no_error();
85}
diff --git a/src/regress/lib/libc/sys/t_gettimeofday.c b/src/regress/lib/libc/sys/t_gettimeofday.c
deleted file mode 100644
index 7a7d319be1..0000000000
--- a/src/regress/lib/libc/sys/t_gettimeofday.c
+++ /dev/null
@@ -1,108 +0,0 @@
1/* $OpenBSD: t_gettimeofday.c,v 1.5 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_gettimeofday.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/time.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <signal.h>
40#include <string.h>
41
42#ifdef __OpenBSD__
43static void sighandler(int);
44
45static void
46sighandler(int signo)
47{
48 _exit(0);
49}
50#endif
51
52ATF_TC(gettimeofday_err);
53ATF_TC_HEAD(gettimeofday_err, tc)
54{
55 atf_tc_set_md_var(tc, "descr", "Test errors from gettimeofday(2)");
56}
57
58ATF_TC_BODY(gettimeofday_err, tc)
59{
60
61#ifdef __OpenBSD__
62 /*
63 * With userland timecounters we will generate SIGSEGV instead
64 * of failing with errno set to EFAULT. POSIX explicitly
65 * allows this behaviour.
66 */
67 ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
68 /* On sparc64 dereferencing -1 causes SIGBUS */
69 ATF_REQUIRE(signal(SIGBUS, sighandler) != SIG_ERR);
70#endif
71 errno = 0;
72
73 ATF_REQUIRE_ERRNO(EFAULT, gettimeofday((void *)-1, NULL) != 0);
74}
75
76ATF_TC(gettimeofday_mono);
77ATF_TC_HEAD(gettimeofday_mono, tc)
78{
79 atf_tc_set_md_var(tc, "descr", "Test monotonicity of gettimeofday(2)");
80}
81
82ATF_TC_BODY(gettimeofday_mono, tc)
83{
84 static const size_t maxiter = 100;
85 struct timeval tv1, tv2;
86 size_t i;
87
88 for (i = 0; i < maxiter; i++) {
89
90 (void)memset(&tv1, 0, sizeof(struct timeval));
91 (void)memset(&tv2, 0, sizeof(struct timeval));
92
93 ATF_REQUIRE(gettimeofday(&tv1, NULL) == 0);
94 ATF_REQUIRE(gettimeofday(&tv2, NULL) == 0);
95
96 if (timercmp(&tv2, &tv1, <) != 0)
97 atf_tc_fail("time went backwards");
98 }
99}
100
101ATF_TP_ADD_TCS(tp)
102{
103
104 ATF_TP_ADD_TC(tp, gettimeofday_err);
105 ATF_TP_ADD_TC(tp, gettimeofday_mono);
106
107 return atf_no_error();
108}
diff --git a/src/regress/lib/libc/sys/t_kevent.c b/src/regress/lib/libc/sys/t_kevent.c
deleted file mode 100644
index 003d7b8a5d..0000000000
--- a/src/regress/lib/libc/sys/t_kevent.c
+++ /dev/null
@@ -1,232 +0,0 @@
1/* $OpenBSD: t_kevent.c,v 1.3 2023/04/19 12:58:15 jsg Exp $ */
2/* $NetBSD: t_kevent.c,v 1.9 2020/10/31 01:08:32 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundatiom
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32#include "macros.h"
33
34#include <sys/types.h>
35#include <sys/event.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <time.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <err.h>
46#ifndef __OpenBSD__
47#include <sys/drvctlio.h>
48#endif
49#include <sys/time.h>
50#include <sys/socket.h>
51#include <sys/wait.h>
52
53ATF_TC(kevent_zerotimer);
54ATF_TC_HEAD(kevent_zerotimer, tc)
55{
56 atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
57 "does not crash the system (PR lib/45618)");
58}
59
60ATF_TC_BODY(kevent_zerotimer, tc)
61{
62 struct kevent ev;
63 int kq;
64
65 ATF_REQUIRE((kq = kqueue()) != -1);
66 EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
67 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
68 ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
69}
70
71ATF_TC(kqueue_desc_passing);
72ATF_TC_HEAD(kqueue_desc_passing, tc)
73{
74 atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
75 "another process does not crash the kernel (PR 46463)");
76}
77
78ATF_TC_BODY(kqueue_desc_passing, tc)
79{
80 pid_t child;
81 int s[2], storage, status, kq;
82 struct cmsghdr *msg;
83 struct iovec iov;
84 struct msghdr m;
85 struct kevent ev;
86
87 ATF_REQUIRE((kq = kqueue()) != -1);
88
89 // atf_tc_skip("crashes kernel (PR kern/46463)");
90
91 ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
92 msg = malloc(CMSG_SPACE(sizeof(int)));
93 m.msg_iov = &iov;
94 m.msg_iovlen = 1;
95 m.msg_name = NULL;
96 m.msg_namelen = 0;
97 m.msg_control = msg;
98 m.msg_controllen = CMSG_SPACE(sizeof(int));
99
100 child = fork();
101 if (child == 0) {
102#ifdef __OpenBSD__
103 sleep(1);
104 exit(0);
105#endif
106 close(s[0]);
107
108 iov.iov_base = &storage;
109 iov.iov_len = sizeof(int);
110 m.msg_iov = &iov;
111 m.msg_iovlen = 1;
112
113 if (recvmsg(s[1], &m, 0) == -1)
114 err(1, "child: could not recvmsg");
115
116 kq = *(int *)CMSG_DATA(msg);
117 printf("child (pid %d): received kq fd %d\n", getpid(), kq);
118 exit(0);
119 }
120
121 close(s[1]);
122
123 iov.iov_base = &storage;
124 iov.iov_len = sizeof(int);
125
126 msg->cmsg_level = SOL_SOCKET;
127 msg->cmsg_type = SCM_RIGHTS;
128 msg->cmsg_len = CMSG_LEN(sizeof(int));
129
130 *(int *)CMSG_DATA(msg) = kq;
131
132 EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
133 ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
134
135 printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
136 if (sendmsg(s[0], &m, 0) == -1) {
137#ifdef __OpenBSD__
138 ATF_REQUIRE_EQ_MSG(errno, EINVAL, "errno is %d", errno);
139#else
140 ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
141 atf_tc_skip("PR kern/46523");
142#endif
143 }
144
145 close(kq);
146
147 waitpid(child, &status, 0);
148 ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
149}
150
151#ifndef __OpenBSD__
152ATF_TC(kqueue_unsupported_fd);
153ATF_TC_HEAD(kqueue_unsupported_fd, tc)
154{
155 atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
156 " type is not supported does not crash the kernel");
157}
158
159ATF_TC_BODY(kqueue_unsupported_fd, tc)
160{
161 /* mqueue and semaphore use fnullop_kqueue also */
162 int fd, kq;
163 struct kevent ev;
164
165 fd = open(DRVCTLDEV, O_RDONLY);
166 if (fd == -1) {
167 switch (errno) {
168 case ENOENT:
169 case ENXIO:
170 atf_tc_skip("no " DRVCTLDEV " available for testing");
171 break;
172 }
173 }
174 ATF_REQUIRE(fd != -1);
175 ATF_REQUIRE((kq = kqueue()) != -1);
176
177 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
178 NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
179 NOTE_RENAME|NOTE_REVOKE, 0, 0);
180
181 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
182 ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
183
184 (void)close(fd);
185 (void)close(kq);
186}
187
188ATF_TC(kqueue_EVFILT_USER);
189ATF_TC_HEAD(kqueue_EVFILT_USER, tc)
190{
191 atf_tc_set_md_var(tc, "descr", "Checks usability of EVFILT_USER");
192}
193
194ATF_TC_BODY(kqueue_EVFILT_USER, tc)
195{
196 /* mqueue and semaphore use fnullop_kqueue also */
197 int kq;
198 struct kevent ev, rev;
199
200 ATF_REQUIRE((kq = kqueue()) != -1);
201
202 EV_SET(&ev, 666, EVFILT_USER, EV_ADD | EV_ENABLE, 0, 0, 0);
203 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
204 EV_SET(&ev, 666, EVFILT_USER, 0, NOTE_FFCOPY | NOTE_TRIGGER | 8, 0, 0);
205 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
206 const struct timespec timeout = {
207 .tv_sec = 1,
208 .tv_nsec = 0,
209 };
210
211 ATF_REQUIRE(kevent(kq, NULL, 0, &rev, 1, &timeout) == 1);
212 ATF_REQUIRE(rev.ident == 666);
213 ATF_REQUIRE(rev.filter == EVFILT_USER);
214 ATF_REQUIRE(rev.fflags == 8);
215 (void)close(kq);
216}
217#endif
218
219
220
221ATF_TP_ADD_TCS(tp)
222{
223
224 ATF_TP_ADD_TC(tp, kevent_zerotimer);
225 ATF_TP_ADD_TC(tp, kqueue_desc_passing);
226#ifndef __OpenBSD__
227 ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
228 ATF_TP_ADD_TC(tp, kqueue_EVFILT_USER);
229#endif
230
231 return atf_no_error();
232}
diff --git a/src/regress/lib/libc/sys/t_kill.c b/src/regress/lib/libc/sys/t_kill.c
deleted file mode 100644
index 0c55dfa41b..0000000000
--- a/src/regress/lib/libc/sys/t_kill.c
+++ /dev/null
@@ -1,313 +0,0 @@
1/* $OpenBSD: t_kill.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include <errno.h>
38#include <limits.h>
39#include <pwd.h>
40#include <signal.h>
41#include <stdlib.h>
42#include <unistd.h>
43
44#include "atf-c.h"
45
46ATF_TC(kill_basic);
47ATF_TC_HEAD(kill_basic, tc)
48{
49 atf_tc_set_md_var(tc, "descr", "Test that kill(2) works");
50}
51
52ATF_TC_BODY(kill_basic, tc)
53{
54 const int sig[] = { SIGHUP, SIGINT, SIGKILL, SIGTERM };
55 pid_t pid;
56 size_t i;
57 int sta;
58
59 for (i = 0; i < __arraycount(sig); i++) {
60
61 pid = fork();
62 ATF_REQUIRE(pid >= 0);
63
64 switch (pid) {
65
66 case 0:
67 pause();
68 break;
69
70 default:
71 ATF_REQUIRE(kill(pid, sig[i]) == 0);
72 }
73
74 (void)wait(&sta);
75
76 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != sig[i])
77 atf_tc_fail("kill(2) failed to kill child");
78 }
79}
80
81ATF_TC(kill_err);
82ATF_TC_HEAD(kill_err, tc)
83{
84 atf_tc_set_md_var(tc, "descr", "Test error conditions of kill(2)");
85}
86
87ATF_TC_BODY(kill_err, tc)
88{
89 int rv, sta;
90 pid_t pid;
91
92 pid = fork();
93 ATF_REQUIRE(pid >= 0);
94
95 if (pid == 0) {
96
97 errno = 0;
98 rv = kill(getpid(), -1);
99
100 if (rv == 0 || errno != EINVAL)
101 _exit(EINVAL);
102
103 errno = 0;
104 rv = kill(INT_MAX, SIGUSR1);
105
106 if (rv == 0 || errno != ESRCH)
107 _exit(ESRCH);
108
109 _exit(EXIT_SUCCESS);
110 }
111
112 (void)wait(&sta);
113
114 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
115
116 if (WEXITSTATUS(sta) == EINVAL)
117 atf_tc_fail("expected EINVAL, but kill(2) succeeded");
118
119 if (WEXITSTATUS(sta) == ESRCH)
120 atf_tc_fail("expected ESRCH, but kill(2) succeeded");
121
122 atf_tc_fail("unknown error from kill(2)");
123 }
124}
125
126ATF_TC(kill_perm);
127ATF_TC_HEAD(kill_perm, tc)
128{
129 atf_tc_set_md_var(tc, "descr", "Test kill(2) permissions");
130 atf_tc_set_md_var(tc, "require.user", "root");
131}
132
133ATF_TC_BODY(kill_perm, tc)
134{
135 struct passwd *pw;
136 pid_t cpid, ppid;
137 uid_t cuid = 0;
138 uid_t puid = 0;
139 int sta;
140
141 /*
142 * Test that kill(2) fails when called
143 * for a PID owned by another user.
144 */
145 pw = getpwnam("operator");
146
147 if (pw != NULL)
148 cuid = pw->pw_uid;
149
150 pw = getpwnam("nobody");
151
152 if (pw != NULL)
153 puid = pw->pw_uid;
154
155 if (cuid == 0 || puid == 0 || cuid == puid)
156 atf_tc_fail("getpwnam(3) failed");
157
158 ppid = fork();
159
160 if (ppid < 0)
161 _exit(EXIT_FAILURE);
162
163 if (ppid == 0) {
164
165 cpid = fork();
166
167 if (cpid < 0)
168 _exit(EXIT_FAILURE);
169
170 if (cpid == 0) {
171
172 if (setuid(cuid) < 0)
173 _exit(EXIT_FAILURE);
174 else {
175 (void)sleep(1);
176 }
177
178 _exit(EXIT_SUCCESS);
179 }
180
181 /*
182 * Try to kill the child after having
183 * set the real and effective UID.
184 */
185 if (setuid(puid) != 0)
186 _exit(EXIT_FAILURE);
187
188 errno = 0;
189
190 if (kill(cpid, SIGKILL) == 0)
191 _exit(EPERM);
192
193 if (errno != EPERM)
194 _exit(EPERM);
195
196 (void)waitpid(cpid, &sta, 0);
197
198 _exit(EXIT_SUCCESS);
199 }
200
201 (void)waitpid(ppid, &sta, 0);
202
203 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) == EPERM)
204 atf_tc_fail("killed a process of another user");
205
206 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
207 atf_tc_fail("unknown error from kill(2)");
208}
209
210ATF_TC(kill_pgrp_neg);
211ATF_TC_HEAD(kill_pgrp_neg, tc)
212{
213 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #2");
214}
215
216ATF_TC_BODY(kill_pgrp_neg, tc)
217{
218 const int maxiter = 3;
219 pid_t cpid, ppid;
220 int i, sta;
221
222 ppid = fork();
223 ATF_REQUIRE(ppid >= 0);
224
225 if (ppid == 0) {
226
227 ATF_REQUIRE(setpgid(0, 0) == 0);
228
229 for (i = 0; i < maxiter; i++) {
230
231 cpid = fork();
232 ATF_REQUIRE(cpid >= 0);
233
234 if (cpid == 0)
235 pause();
236 }
237
238 /*
239 * Test the variant of killpg(3); if the process number
240 * is negative but not -1, the signal should be sent to
241 * all processes whose process group ID is equal to the
242 * absolute value of the process number.
243 */
244 ATF_REQUIRE(kill(-getpgrp(), SIGKILL) == 0);
245
246 (void)sleep(1);
247
248 _exit(EXIT_SUCCESS);
249 }
250
251 (void)waitpid(ppid, &sta, 0);
252
253 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
254 atf_tc_fail("failed to kill(2) a process group");
255}
256
257ATF_TC(kill_pgrp_zero);
258ATF_TC_HEAD(kill_pgrp_zero, tc)
259{
260 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #1");
261}
262
263ATF_TC_BODY(kill_pgrp_zero, tc)
264{
265 const int maxiter = 3;
266 pid_t cpid, ppid;
267 int i, sta;
268
269 ppid = fork();
270 ATF_REQUIRE(ppid >= 0);
271
272 if (ppid == 0) {
273
274 ATF_REQUIRE(setpgid(0, 0) == 0);
275
276 for (i = 0; i < maxiter; i++) {
277
278 cpid = fork();
279 ATF_REQUIRE(cpid >= 0);
280
281 if (cpid == 0)
282 pause();
283 }
284
285 /*
286 * If the supplied process number is zero,
287 * the signal should be sent to all processes
288 * under the current process group.
289 */
290 ATF_REQUIRE(kill(0, SIGKILL) == 0);
291
292 (void)sleep(1);
293
294 _exit(EXIT_SUCCESS);
295 }
296
297 (void)waitpid(ppid, &sta, 0);
298
299 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
300 atf_tc_fail("failed to kill(2) a process group");
301}
302
303ATF_TP_ADD_TCS(tp)
304{
305
306 ATF_TP_ADD_TC(tp, kill_basic);
307 ATF_TP_ADD_TC(tp, kill_err);
308 ATF_TP_ADD_TC(tp, kill_perm);
309 ATF_TP_ADD_TC(tp, kill_pgrp_neg);
310 ATF_TP_ADD_TC(tp, kill_pgrp_zero);
311
312 return atf_no_error();
313}
diff --git a/src/regress/lib/libc/sys/t_link.c b/src/regress/lib/libc/sys/t_link.c
deleted file mode 100644
index 12dbd8b595..0000000000
--- a/src/regress/lib/libc/sys/t_link.c
+++ /dev/null
@@ -1,230 +0,0 @@
1/* $OpenBSD: t_link.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_link.c,v 1.3 2017/01/13 20:42:36 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <stdio.h>
42#include <string.h>
43#include <unistd.h>
44
45static const char *getpath(void);
46static char path[] = "link";
47static const char *pathl;
48
49static const char *
50getpath(void)
51{
52 static char buf[LINE_MAX];
53
54 (void)memset(buf, '\0', sizeof(buf));
55
56 if (getcwd(buf, sizeof(buf)) == NULL)
57 return NULL;
58
59 (void)strlcat(buf, path, sizeof(buf));
60 (void)strlcat(buf, ".link", sizeof(buf));
61
62 return buf;
63}
64
65ATF_TC_WITH_CLEANUP(link_count);
66ATF_TC_HEAD(link_count, tc)
67{
68 atf_tc_set_md_var(tc, "descr", "link(2) counts are incremented?");
69}
70
71ATF_TC_BODY(link_count, tc)
72{
73 struct stat sa, sb;
74 int fd;
75
76 (void)memset(&sa, 0, sizeof(struct stat));
77 (void)memset(&sb, 0, sizeof(struct stat));
78
79 pathl = getpath();
80 fd = open(path, O_RDWR | O_CREAT, 0600);
81
82 ATF_REQUIRE(fd >= 0);
83 ATF_REQUIRE(pathl != NULL);
84
85 ATF_REQUIRE(stat(path, &sa) == 0);
86 ATF_REQUIRE(link(path, pathl) == 0);
87 ATF_REQUIRE(stat(path, &sb) == 0);
88
89 if (sa.st_nlink != sb.st_nlink - 1)
90 atf_tc_fail("incorrect link(2) count");
91
92 ATF_REQUIRE(close(fd) == 0);
93 ATF_REQUIRE(unlink(path) == 0);
94 ATF_REQUIRE(unlink(pathl) == 0);
95}
96
97ATF_TC_CLEANUP(link_count, tc)
98{
99 (void)unlink(path);
100 (void)unlink(pathl);
101}
102
103ATF_TC_WITH_CLEANUP(link_err);
104ATF_TC_HEAD(link_err, tc)
105{
106 atf_tc_set_md_var(tc, "descr", "Test error conditions of link(2)");
107}
108
109ATF_TC_BODY(link_err, tc)
110{
111 char buf[PATH_MAX + 1];
112 int fd;
113
114 (void)memset(buf, 'x', sizeof(buf));
115
116 pathl = getpath();
117 fd = open(path, O_RDWR | O_CREAT, 0600);
118
119 ATF_REQUIRE(fd >= 0);
120 ATF_REQUIRE(pathl != NULL);
121
122 errno = 0;
123 ATF_REQUIRE(link(path, pathl) == 0);
124 ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1);
125
126 errno = 0;
127 ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1);
128
129 errno = 0;
130 ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1);
131
132 errno = 0;
133 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1);
134
135 errno = 0;
136 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1);
137
138 errno = 0;
139 ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1);
140
141 errno = 0;
142 ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1);
143
144 ATF_REQUIRE(close(fd) == 0);
145 ATF_REQUIRE(unlink(path) == 0);
146 ATF_REQUIRE(unlink(pathl) == 0);
147}
148
149ATF_TC_CLEANUP(link_err, tc)
150{
151 (void)unlink(path);
152 (void)unlink(pathl);
153}
154
155ATF_TC(link_perm);
156ATF_TC_HEAD(link_perm, tc)
157{
158 atf_tc_set_md_var(tc, "descr", "Test permissions with link(2)");
159 atf_tc_set_md_var(tc, "require.user", "unprivileged");
160}
161
162ATF_TC_BODY(link_perm, tc)
163{
164 int rv;
165
166 errno = 0;
167 rv = link("/root", "/root.link");
168 ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
169 "link to a directory did not fail with EPERM or EACCESS; link() "
170 "returned %d, errno %d", rv, errno);
171
172 errno = 0;
173 ATF_REQUIRE_ERRNO(EACCES,
174 link("/root/.profile", "/root/.profile.link") == -1);
175}
176
177ATF_TC_WITH_CLEANUP(link_stat);
178ATF_TC_HEAD(link_stat, tc)
179{
180 atf_tc_set_md_var(tc, "descr", "Check stat(2) of a linked file");
181}
182
183ATF_TC_BODY(link_stat, tc)
184{
185 struct stat sa, sb;
186 int fd;
187
188 (void)memset(&sa, 0, sizeof(struct stat));
189 (void)memset(&sb, 0, sizeof(struct stat));
190
191 pathl = getpath();
192 fd = open(path, O_RDWR | O_CREAT, 0600);
193
194 ATF_REQUIRE(fd >= 0);
195 ATF_REQUIRE(pathl != NULL);
196
197 ATF_REQUIRE(link(path, pathl) == 0);
198 ATF_REQUIRE(stat(path, &sa) == 0);
199 ATF_REQUIRE(lstat(pathl, &sb) == 0);
200
201 if (sa.st_uid != sb.st_uid)
202 atf_tc_fail("unequal UIDs");
203
204 if (sa.st_mode != sb.st_mode)
205 atf_tc_fail("unequal modes");
206
207 if (sa.st_ino != sb.st_ino)
208 atf_tc_fail("unequal inodes");
209
210 ATF_REQUIRE(close(fd) == 0);
211 ATF_REQUIRE(unlink(path) == 0);
212 ATF_REQUIRE(unlink(pathl) == 0);
213}
214
215ATF_TC_CLEANUP(link_stat, tc)
216{
217 (void)unlink(path);
218 (void)unlink(pathl);
219}
220
221ATF_TP_ADD_TCS(tp)
222{
223
224 ATF_TP_ADD_TC(tp, link_count);
225 ATF_TP_ADD_TC(tp, link_err);
226 ATF_TP_ADD_TC(tp, link_perm);
227 ATF_TP_ADD_TC(tp, link_stat);
228
229 return atf_no_error();
230}
diff --git a/src/regress/lib/libc/sys/t_listen.c b/src/regress/lib/libc/sys/t_listen.c
deleted file mode 100644
index da5b287cdf..0000000000
--- a/src/regress/lib/libc/sys/t_listen.c
+++ /dev/null
@@ -1,139 +0,0 @@
1/* $OpenBSD: t_listen.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_listen.c,v 1.6 2019/07/09 16:24:01 maya Exp $ */
3/*
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/socket.h>
33#include "atf-c.h"
34#include <err.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <arpa/inet.h>
41#include <netinet/in.h>
42
43static const char *path = "listen";
44
45ATF_TC_WITH_CLEANUP(listen_err);
46ATF_TC_HEAD(listen_err, tc)
47{
48 atf_tc_set_md_var(tc, "descr",
49 "Checks errors from listen(2) (PR standards/46150)");
50}
51
52ATF_TC_BODY(listen_err, tc)
53{
54 static const size_t siz = sizeof(struct sockaddr_in);
55 struct sockaddr_in sina, sinb;
56 int fda, fdb, fdc;
57
58 (void)memset(&sina, 0, sizeof(struct sockaddr_in));
59 (void)memset(&sinb, 0, sizeof(struct sockaddr_in));
60
61 sina.sin_family = AF_INET;
62 sina.sin_port = htons(31522);
63 sina.sin_addr.s_addr = inet_addr("127.0.0.1");
64
65 sinb.sin_family = AF_INET;
66 sinb.sin_port = htons(31522);
67 sinb.sin_addr.s_addr = inet_addr("127.0.0.1");
68
69 fda = socket(AF_INET, SOCK_STREAM, 0);
70 fdb = socket(AF_INET, SOCK_STREAM, 0);
71 fdc = open("listen", O_RDWR | O_CREAT, 0600);
72
73 ATF_REQUIRE(fda >= 0 && fdb >= 0 && fdc >= 0);
74 ATF_REQUIRE_ERRNO(ENOTSOCK, listen(fdc, 1) == -1);
75
76 (void)close(fdc);
77 (void)unlink(path);
78
79 ATF_REQUIRE(bind(fda, (struct sockaddr *)&sina, siz) == 0);
80 ATF_REQUIRE(listen(fda, 1) == 0);
81
82 /*
83 * According to IEEE Std 1003.1-2008: if the socket is
84 * already connected, the call should fail with EINVAL.
85 */
86 ATF_REQUIRE(connect(fdb, (struct sockaddr *)&sinb, siz) == 0);
87 ATF_REQUIRE_ERRNO(EINVAL, listen(fdb, 1) == -1);
88
89 (void)close(fda);
90 (void)close(fdb);
91
92 ATF_REQUIRE_ERRNO(EBADF, connect(fdb,
93 (struct sockaddr *)&sinb, siz) == -1);
94}
95
96ATF_TC_CLEANUP(listen_err, tc)
97{
98 (void)unlink(path);
99}
100
101ATF_TC(listen_low_port);
102ATF_TC_HEAD(listen_low_port, tc)
103{
104 atf_tc_set_md_var(tc, "descr", "Does low-port allocation work?");
105 atf_tc_set_md_var(tc, "require.user", "root");
106}
107
108ATF_TC_BODY(listen_low_port, tc)
109{
110 int sd, val;
111
112 sd = socket(AF_INET, SOCK_STREAM, 0);
113 ATF_REQUIRE_MSG(sd != -1, "socket failed: %s", strerror(errno));
114
115 val = IP_PORTRANGE_LOW;
116 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
117 sizeof(val)) == -1)
118 atf_tc_fail("setsockopt failed: %s", strerror(errno));
119
120 if (listen(sd, 5) == -1) {
121 int serrno = errno;
122 atf_tc_fail("listen failed: %s%s",
123 strerror(serrno),
124 serrno != EACCES ? "" :
125 " (see http://mail-index.netbsd.org/"
126 "source-changes/2007/12/16/0011.html)");
127 }
128
129 close(sd);
130}
131
132ATF_TP_ADD_TCS(tp)
133{
134
135 ATF_TP_ADD_TC(tp, listen_err);
136 ATF_TP_ADD_TC(tp, listen_low_port);
137
138 return atf_no_error();
139}
diff --git a/src/regress/lib/libc/sys/t_minherit.c b/src/regress/lib/libc/sys/t_minherit.c
deleted file mode 100644
index 578092352c..0000000000
--- a/src/regress/lib/libc/sys/t_minherit.c
+++ /dev/null
@@ -1,200 +0,0 @@
1/* $OpenBSD: t_minherit.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_minherit.c,v 1.1 2014/07/18 12:34:52 christos Exp $ */
3
4/*-
5 * Copyright (c) 2014 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32#include "macros.h"
33
34#include <sys/mman.h>
35#include <sys/sysctl.h>
36#include <sys/wait.h>
37
38#include <errno.h>
39#include <fcntl.h>
40#include <stdlib.h>
41#include <string.h>
42#include <signal.h>
43#include <unistd.h>
44
45#include "atf-c.h"
46
47static long page;
48
49static void *
50makemap(int v, int f) {
51 void *map = mmap(NULL, page, PROT_READ|PROT_WRITE,
52 MAP_SHARED|MAP_ANON, -1, 0);
53 ATF_REQUIRE(map != MAP_FAILED);
54 memset(map, v, page);
55 if (f != 666)
56 ATF_REQUIRE(minherit(map, page, f) == 0);
57 else
58 ATF_REQUIRE(minherit(map, page, f) == -1);
59 return map;
60}
61
62ATF_TC(minherit_copy);
63ATF_TC_HEAD(minherit_copy, tc)
64{
65 atf_tc_set_md_var(tc, "descr",
66 "Test for MAP_INHERIT_COPY from minherit(2)");
67}
68
69ATF_TC_BODY(minherit_copy, tc)
70{
71 void *map1 = makemap(1, MAP_INHERIT_COPY);
72 void *map2 = makemap(1, MAP_INHERIT_COPY);
73 switch (fork()) {
74 default:
75 ATF_REQUIRE(wait(NULL) != -1);
76 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
77 break;
78 case -1:
79 ATF_REQUIRE(0);
80 break;
81 case 0:
82 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
83 memset(map1, 0, page);
84 exit(0);
85 }
86}
87
88ATF_TC(minherit_share);
89ATF_TC_HEAD(minherit_share, tc)
90{
91 atf_tc_set_md_var(tc, "descr",
92 "Test for MAP_INHERIT_SHARE from minherit(2)");
93}
94
95ATF_TC_BODY(minherit_share, tc)
96{
97 void *map1 = makemap(1, MAP_INHERIT_SHARE);
98 void *map2 = makemap(1, MAP_INHERIT_SHARE);
99
100 switch (fork()) {
101 default:
102 ATF_REQUIRE(wait(NULL) != -1);
103 memset(map2, 0, page);
104 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
105 break;
106 case -1:
107 ATF_REQUIRE(0);
108 break;
109 case 0:
110 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
111 memset(map1, 0, page);
112 exit(0);
113 }
114}
115
116static void
117segv(int n) {
118 _exit(n);
119}
120
121ATF_TC(minherit_none);
122ATF_TC_HEAD(minherit_none, tc)
123{
124 atf_tc_set_md_var(tc, "descr",
125 "Test for MAP_INHERIT_NONE from minherit(2)");
126}
127
128ATF_TC_BODY(minherit_none, tc)
129{
130 void *map1 = makemap(0, MAP_INHERIT_NONE);
131 int status;
132
133 switch (fork()) {
134 default:
135 ATF_REQUIRE(wait(&status) != -1);
136 ATF_REQUIRE(WEXITSTATUS(status) == SIGSEGV);
137 break;
138 case -1:
139 ATF_REQUIRE(0);
140 break;
141 case 0:
142 ATF_REQUIRE(signal(SIGSEGV, segv) != SIG_ERR);
143 memset(map1, 0, page);
144 exit(0);
145 }
146}
147
148ATF_TC(minherit_zero);
149ATF_TC_HEAD(minherit_zero, tc)
150{
151 atf_tc_set_md_var(tc, "descr",
152 "Test for MAP_INHERIT_ZERO from minherit(2)");
153}
154
155ATF_TC_BODY(minherit_zero, tc)
156{
157 void *map1 = makemap(1, MAP_INHERIT_ZERO);
158 void *map2 = makemap(0, MAP_INHERIT_SHARE);
159
160 switch (fork()) {
161 default:
162 ATF_REQUIRE(wait(NULL) != -1);
163 memset(map2, 1, page);
164 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
165 break;
166 case -1:
167 ATF_REQUIRE(0);
168 break;
169 case 0:
170 ATF_REQUIRE(memcmp(map1, map2, page) == 0);
171 memset(map1, 2, page);
172 exit(0);
173 }
174}
175
176ATF_TC(minherit_bad);
177ATF_TC_HEAD(minherit_bad, tc)
178{
179 atf_tc_set_md_var(tc, "descr",
180 "Test for bad minherit(2)");
181}
182
183ATF_TC_BODY(minherit_bad, tc)
184{
185 (void)makemap(0, 666);
186}
187
188ATF_TP_ADD_TCS(tp)
189{
190 page = sysconf(_SC_PAGESIZE);
191 ATF_REQUIRE(page >= 0);
192
193 ATF_TP_ADD_TC(tp, minherit_copy);
194 ATF_TP_ADD_TC(tp, minherit_share);
195 ATF_TP_ADD_TC(tp, minherit_none);
196 ATF_TP_ADD_TC(tp, minherit_zero);
197 ATF_TP_ADD_TC(tp, minherit_bad);
198
199 return atf_no_error();
200}
diff --git a/src/regress/lib/libc/sys/t_mkdir.c b/src/regress/lib/libc/sys/t_mkdir.c
deleted file mode 100644
index 5052bd3988..0000000000
--- a/src/regress/lib/libc/sys/t_mkdir.c
+++ /dev/null
@@ -1,208 +0,0 @@
1/* $OpenBSD: t_mkdir.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_mkdir.c,v 1.2 2011/10/15 07:38:31 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2008, 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe and Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <limits.h>
42#include <pwd.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48ATF_TC(mkdir_err);
49ATF_TC_HEAD(mkdir_err, tc)
50{
51 atf_tc_set_md_var(tc, "descr", "Checks errors from mkdir(2)");
52}
53
54ATF_TC_BODY(mkdir_err, tc)
55{
56 char buf[PATH_MAX + 1];
57 int fd;
58
59 (void)memset(buf, 'x', sizeof(buf));
60
61 fd = open("/etc", O_RDONLY);
62
63 if (fd >= 0) {
64
65 (void)close(fd);
66
67 errno = 0;
68 ATF_REQUIRE_ERRNO(EEXIST, mkdir("/etc", 0500) == -1);
69 }
70
71 errno = 0;
72 ATF_REQUIRE_ERRNO(EFAULT, mkdir((void *)-1, 0500) == -1);
73
74 errno = 0;
75 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkdir(buf, 0500) == -1);
76
77 errno = 0;
78 ATF_REQUIRE_ERRNO(ENOENT, mkdir("/a/b/c/d/e/f/g/h/i/j/k", 0500) == -1);
79}
80
81ATF_TC_WITH_CLEANUP(mkdir_perm);
82ATF_TC_HEAD(mkdir_perm, tc)
83{
84 atf_tc_set_md_var(tc, "descr", "Checks permissions with mkdir(2)");
85 atf_tc_set_md_var(tc, "require.user", "unprivileged");
86}
87
88ATF_TC_BODY(mkdir_perm, tc)
89{
90 errno = 0;
91 ATF_REQUIRE_ERRNO(EACCES, mkdir("/usr/__nonexistent__", 0500) == -1);
92}
93
94ATF_TC_CLEANUP(mkdir_perm, tc)
95{
96 (void)rmdir("/usr/__nonexistent__");
97}
98
99ATF_TC_WITH_CLEANUP(mkdir_mode);
100ATF_TC_HEAD(mkdir_mode, tc)
101{
102 atf_tc_set_md_var(tc, "descr", "Test that UIDs and GIDs are right "
103 "for a directory created with mkdir(2)");
104 atf_tc_set_md_var(tc, "require.user", "root");
105}
106
107ATF_TC_BODY(mkdir_mode, tc)
108{
109 static const char *path = "/tmp/mkdir";
110 struct stat st_a, st_b;
111 struct passwd *pw;
112 pid_t pid;
113 int sta;
114
115 (void)memset(&st_a, 0, sizeof(struct stat));
116 (void)memset(&st_b, 0, sizeof(struct stat));
117
118 pw = getpwnam("nobody");
119
120 ATF_REQUIRE(pw != NULL);
121 ATF_REQUIRE(stat("/tmp", &st_a) == 0);
122
123 pid = fork();
124 ATF_REQUIRE(pid >= 0);
125
126 if (pid == 0) {
127
128 if (setuid(pw->pw_uid) != 0)
129 _exit(EXIT_FAILURE);
130
131 if (mkdir(path, 0500) != 0)
132 _exit(EXIT_FAILURE);
133
134 _exit(EXIT_SUCCESS);
135 }
136
137 (void)sleep(1);
138 (void)wait(&sta);
139
140 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
141 atf_tc_fail("failed to create '%s'", path);
142
143 ATF_REQUIRE(stat(path, &st_b) == 0);
144 ATF_REQUIRE(rmdir(path) == 0);
145
146 /*
147 * The directory's owner ID should be set to the
148 * effective UID, whereas the group ID should be
149 * set to that of the parent directory.
150 */
151 if (st_b.st_uid != pw->pw_uid)
152 atf_tc_fail("invalid UID for '%s'", path);
153
154 if (st_b.st_gid != st_a.st_gid)
155 atf_tc_fail("GID did not follow the parent directory");
156}
157
158ATF_TC_CLEANUP(mkdir_mode, tc)
159{
160 (void)rmdir("/tmp/mkdir");
161}
162
163ATF_TC(mkdir_trail);
164ATF_TC_HEAD(mkdir_trail, tc)
165{
166 atf_tc_set_md_var(tc, "descr", "Checks mkdir(2) for trailing slashes");
167}
168
169ATF_TC_BODY(mkdir_trail, tc)
170{
171 const char *tests[] = {
172 /*
173 * IEEE 1003.1 second ed. 2.2.2.78:
174 *
175 * If the pathname refers to a directory, it may also have
176 * one or more trailing slashes. Multiple successive slashes
177 * are considered to be the same as one slash.
178 */
179 "dir1/",
180 "dir2//",
181
182 NULL,
183 };
184
185 const char **test;
186
187 for (test = &tests[0]; *test != NULL; ++test) {
188
189 (void)printf("Checking \"%s\"\n", *test);
190 (void)rmdir(*test);
191
192 ATF_REQUIRE(mkdir(*test, 0777) == 0);
193 ATF_REQUIRE(rename(*test, "foo") == 0);
194 ATF_REQUIRE(rename("foo/", *test) == 0);
195 ATF_REQUIRE(rmdir(*test) == 0);
196 }
197}
198
199ATF_TP_ADD_TCS(tp)
200{
201
202 ATF_TP_ADD_TC(tp, mkdir_err);
203 ATF_TP_ADD_TC(tp, mkdir_perm);
204 ATF_TP_ADD_TC(tp, mkdir_mode);
205 ATF_TP_ADD_TC(tp, mkdir_trail);
206
207 return atf_no_error();
208}
diff --git a/src/regress/lib/libc/sys/t_mkfifo.c b/src/regress/lib/libc/sys/t_mkfifo.c
deleted file mode 100644
index 13cd2d1922..0000000000
--- a/src/regress/lib/libc/sys/t_mkfifo.c
+++ /dev/null
@@ -1,306 +0,0 @@
1/* $OpenBSD: t_mkfifo.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_mkfifo.c,v 1.3 2019/06/20 03:31:54 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <limits.h>
42#include <stdlib.h>
43#include <signal.h>
44#include <string.h>
45#include <unistd.h>
46
47static const char path[] = "fifo";
48static void support(void);
49
50static void
51support(void)
52{
53
54 errno = 0;
55
56 if (mkfifo(path, 0600) == 0) {
57 ATF_REQUIRE(unlink(path) == 0);
58 return;
59 }
60
61 if (errno == EOPNOTSUPP)
62 atf_tc_skip("the kernel does not support FIFOs");
63 else {
64 atf_tc_fail("mkfifo(2) failed");
65 }
66}
67
68ATF_TC_WITH_CLEANUP(mkfifo_block);
69ATF_TC_HEAD(mkfifo_block, tc)
70{
71 atf_tc_set_md_var(tc, "descr", "Test that FIFOs block");
72}
73
74ATF_TC_BODY(mkfifo_block, tc)
75{
76 int sta, fd = -1;
77 pid_t pid;
78
79 support();
80
81 ATF_REQUIRE(mkfifo(path, 0600) == 0);
82
83 pid = fork();
84 ATF_REQUIRE(pid >= 0);
85
86 if (pid == 0) {
87
88 /*
89 * If we open the FIFO as read-only (write-only),
90 * the call should block until another process
91 * opens the FIFO for writing (reading).
92 */
93 fd = open(path, O_RDONLY);
94
95 _exit(EXIT_FAILURE); /* NOTREACHED */
96 }
97
98 (void)sleep(1);
99
100 ATF_REQUIRE(kill(pid, SIGKILL) == 0);
101
102 (void)wait(&sta);
103
104 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
105 atf_tc_fail("FIFO did not block");
106
107 (void)close(fd);
108 (void)unlink(path);
109}
110
111ATF_TC_CLEANUP(mkfifo_block, tc)
112{
113 (void)unlink(path);
114}
115
116ATF_TC_WITH_CLEANUP(mkfifo_err);
117ATF_TC_HEAD(mkfifo_err, tc)
118{
119 atf_tc_set_md_var(tc, "descr", "Test erros from mkfifo(2)");
120}
121
122ATF_TC_BODY(mkfifo_err, tc)
123{
124 char buf[PATH_MAX + 1];
125
126 support();
127
128 (void)memset(buf, 'x', sizeof(buf));
129 ATF_REQUIRE(mkfifo(path, 0600) == 0);
130
131 errno = 0;
132 ATF_REQUIRE_ERRNO(EFAULT, mkfifo((char *)-1, 0600) == -1);
133
134 errno = 0;
135 ATF_REQUIRE_ERRNO(EEXIST, mkfifo("/etc/passwd", 0600) == -1);
136
137 errno = 0;
138 ATF_REQUIRE_ERRNO(EEXIST, mkfifo(path, 0600) == -1);
139
140 errno = 0;
141 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkfifo(buf, 0600) == -1);
142
143 errno = 0;
144 ATF_REQUIRE_ERRNO(ENOENT, mkfifo("/a/b/c/d/e/f/g", 0600) == -1);
145
146 ATF_REQUIRE(unlink(path) == 0);
147}
148
149ATF_TC_CLEANUP(mkfifo_err, tc)
150{
151 (void)unlink(path);
152}
153
154ATF_TC_WITH_CLEANUP(mkfifo_nonblock);
155ATF_TC_HEAD(mkfifo_nonblock, tc)
156{
157 atf_tc_set_md_var(tc, "descr", "Test O_NONBLOCK with FIFOs");
158}
159
160ATF_TC_BODY(mkfifo_nonblock, tc)
161{
162 int fd, sta;
163 pid_t pid;
164
165 support();
166
167 fd = -1;
168 ATF_REQUIRE(mkfifo(path, 0600) == 0);
169
170 pid = fork();
171 ATF_REQUIRE(pid >= 0);
172
173 if (pid == 0) {
174
175 /*
176 * If we open the FIFO as O_NONBLOCK, the O_RDONLY
177 * call should return immediately, whereas the call
178 * for write-only should fail with ENXIO.
179 */
180 fd = open(path, O_RDONLY | O_NONBLOCK);
181
182 if (fd >= 0)
183 _exit(EXIT_SUCCESS);
184
185 (void)pause(); /* NOTREACHED */
186 }
187
188 (void)sleep(1);
189
190 errno = 0;
191 ATF_REQUIRE_ERRNO(ENXIO, open(path, O_WRONLY | O_NONBLOCK) == -1);
192
193 (void)kill(pid, SIGKILL);
194 (void)wait(&sta);
195
196 if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
197 atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
198
199 (void)close(fd);
200 (void)unlink(path);
201}
202
203ATF_TC_CLEANUP(mkfifo_nonblock, tc)
204{
205 (void)unlink(path);
206}
207
208ATF_TC_WITH_CLEANUP(mkfifo_perm);
209ATF_TC_HEAD(mkfifo_perm, tc)
210{
211 atf_tc_set_md_var(tc, "descr", "Test permissions with mkfifo(2)");
212 atf_tc_set_md_var(tc, "require.user", "unprivileged");
213}
214
215ATF_TC_BODY(mkfifo_perm, tc)
216{
217
218 support();
219
220 errno = 0;
221 ATF_REQUIRE_ERRNO(EACCES, mkfifo("/root/fifo", 0600) == -1);
222
223 ATF_REQUIRE(mkfifo(path, 0600) == 0);
224
225 /*
226 * For some reason this fails with EFTYPE...
227 */
228 errno = 0;
229 ATF_REQUIRE_ERRNO(EFTYPE, chmod(path, 1777) == -1);
230
231 ATF_REQUIRE(unlink(path) == 0);
232}
233
234ATF_TC_CLEANUP(mkfifo_perm, tc)
235{
236 (void)unlink(path);
237}
238
239ATF_TC_WITH_CLEANUP(mkfifo_stat);
240ATF_TC_HEAD(mkfifo_stat, tc)
241{
242 atf_tc_set_md_var(tc, "descr", "Test mkfifo(2) with stat");
243}
244
245ATF_TC_BODY(mkfifo_stat, tc)
246{
247 struct stat st;
248
249 support();
250
251 (void)memset(&st, 0, sizeof(struct stat));
252
253 ATF_REQUIRE(mkfifo(path, 0600) == 0);
254 ATF_REQUIRE(stat(path, &st) == 0);
255
256 if (S_ISFIFO(st.st_mode) == 0)
257 atf_tc_fail("invalid mode from mkfifo(2)");
258
259 ATF_REQUIRE(unlink(path) == 0);
260}
261
262ATF_TC_CLEANUP(mkfifo_stat, tc)
263{
264 (void)unlink(path);
265}
266
267ATF_TC_WITH_CLEANUP(mknod_s_ififo);
268ATF_TC_HEAD(mknod_s_ififo, tc)
269{
270 atf_tc_set_md_var(tc, "descr", "Test mknod(2) with S_IFIFO");
271}
272
273ATF_TC_BODY(mknod_s_ififo, tc)
274{
275 struct stat st;
276
277 support();
278
279 (void)memset(&st, 0, sizeof(struct stat));
280
281 ATF_REQUIRE(mknod(path, S_IFIFO | 0600, 0) == 0);
282 ATF_REQUIRE(stat(path, &st) == 0);
283
284 if (S_ISFIFO(st.st_mode) == 0)
285 atf_tc_fail("invalid mode from mknod(2) with S_IFIFO");
286
287 ATF_REQUIRE(unlink(path) == 0);
288}
289
290ATF_TC_CLEANUP(mknod_s_ififo, tc)
291{
292 (void)unlink(path);
293}
294
295ATF_TP_ADD_TCS(tp)
296{
297
298 ATF_TP_ADD_TC(tp, mkfifo_block);
299 ATF_TP_ADD_TC(tp, mkfifo_err);
300 ATF_TP_ADD_TC(tp, mkfifo_nonblock);
301 ATF_TP_ADD_TC(tp, mkfifo_perm);
302 ATF_TP_ADD_TC(tp, mkfifo_stat);
303 ATF_TP_ADD_TC(tp, mknod_s_ififo);
304
305 return atf_no_error();
306}
diff --git a/src/regress/lib/libc/sys/t_mknod.c b/src/regress/lib/libc/sys/t_mknod.c
deleted file mode 100644
index 0c8b54f85f..0000000000
--- a/src/regress/lib/libc/sys/t_mknod.c
+++ /dev/null
@@ -1,196 +0,0 @@
1/* $OpenBSD: t_mknod.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <paths.h>
42#include <stdio.h>
43#include <string.h>
44#include <unistd.h>
45
46static char path[] = "node";
47
48ATF_TC_WITH_CLEANUP(mknod_err);
49ATF_TC_HEAD(mknod_err, tc)
50{
51 atf_tc_set_md_var(tc, "descr",
52 "Test error conditions of mknod(2) (PR kern/45111)");
53 atf_tc_set_md_var(tc, "require.user", "root");
54}
55
56ATF_TC_BODY(mknod_err, tc)
57{
58 char buf[PATH_MAX + 1];
59
60 (void)memset(buf, 'x', sizeof(buf));
61
62 errno = 0;
63 ATF_REQUIRE_ERRNO(EINVAL, mknod(path, S_IFCHR, -1) == -1);
64
65 errno = 0;
66 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mknod(buf, S_IFCHR, 0) == -1);
67
68 errno = 0;
69 ATF_REQUIRE_ERRNO(EFAULT, mknod((char *)-1, S_IFCHR, 0) == -1);
70
71 errno = 0;
72 ATF_REQUIRE_ERRNO(ENOENT, mknod("/a/b/c/d/e/f/g", S_IFCHR, 0) == -1);
73}
74
75ATF_TC_CLEANUP(mknod_err, tc)
76{
77 (void)unlink(path);
78}
79
80ATF_TC_WITH_CLEANUP(mknod_exist);
81ATF_TC_HEAD(mknod_exist, tc)
82{
83 atf_tc_set_md_var(tc, "descr", "Test EEXIST from mknod(2)");
84 atf_tc_set_md_var(tc, "require.user", "root");
85}
86
87ATF_TC_BODY(mknod_exist, tc)
88{
89 int fd;
90
91 fd = open("/etc/passwd", O_RDONLY);
92
93 if (fd >= 0) {
94
95 (void)close(fd);
96
97 errno = 0;
98 ATF_REQUIRE_ERRNO(EEXIST,
99 mknod("/etc/passwd", S_IFCHR, 0) == -1);
100 }
101
102 ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
103
104 errno = 0;
105 ATF_REQUIRE_ERRNO(EEXIST, mknod(path, S_IFCHR, 0) == -1);
106
107 ATF_REQUIRE(unlink(path) == 0);
108}
109
110ATF_TC_CLEANUP(mknod_exist, tc)
111{
112 (void)unlink(path);
113}
114
115ATF_TC_WITH_CLEANUP(mknod_perm);
116ATF_TC_HEAD(mknod_perm, tc)
117{
118 atf_tc_set_md_var(tc, "descr", "Test permissions of mknod(2)");
119 atf_tc_set_md_var(tc, "require.user", "unprivileged");
120}
121
122ATF_TC_BODY(mknod_perm, tc)
123{
124
125 errno = 0;
126 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFCHR, 0) == -1);
127
128 errno = 0;
129 ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFBLK, 0) == -1);
130}
131
132ATF_TC_CLEANUP(mknod_perm, tc)
133{
134 (void)unlink(path);
135}
136
137ATF_TC_WITH_CLEANUP(mknod_stat);
138ATF_TC_HEAD(mknod_stat, tc)
139{
140 atf_tc_set_md_var(tc, "descr", "A basic test of mknod(2)");
141 atf_tc_set_md_var(tc, "require.user", "root");
142}
143
144ATF_TC_BODY(mknod_stat, tc)
145{
146 struct stat st;
147
148 (void)memset(&st, 0, sizeof(struct stat));
149
150 ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
151 ATF_REQUIRE(stat(path, &st) == 0);
152
153 if (S_ISCHR(st.st_mode) == 0)
154 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFCHR)");
155
156 ATF_REQUIRE(unlink(path) == 0);
157
158 (void)memset(&st, 0, sizeof(struct stat));
159
160 ATF_REQUIRE(mknod(path, S_IFBLK, 0) == 0);
161 ATF_REQUIRE(stat(path, &st) == 0);
162
163 if (S_ISBLK(st.st_mode) == 0)
164 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFBLK)");
165
166 ATF_REQUIRE(unlink(path) == 0);
167
168 (void)memset(&st, 0, sizeof(struct stat));
169
170#ifndef __OpenBSD__
171 /* OpenBSD only supports FIFO and device special files */
172 ATF_REQUIRE(mknod(path, S_IFREG, 0) == 0);
173 ATF_REQUIRE(stat(path, &st) == 0);
174
175 if (S_ISREG(st.st_mode) == 0)
176 atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFREG)");
177
178 ATF_REQUIRE(unlink(path) == 0);
179#endif
180}
181
182ATF_TC_CLEANUP(mknod_stat, tc)
183{
184 (void)unlink(path);
185}
186
187ATF_TP_ADD_TCS(tp)
188{
189
190 ATF_TP_ADD_TC(tp, mknod_err);
191 ATF_TP_ADD_TC(tp, mknod_exist);
192 ATF_TP_ADD_TC(tp, mknod_perm);
193 ATF_TP_ADD_TC(tp, mknod_stat);
194
195 return atf_no_error();
196}
diff --git a/src/regress/lib/libc/sys/t_mlock.c b/src/regress/lib/libc/sys/t_mlock.c
deleted file mode 100644
index 865bd9e36b..0000000000
--- a/src/regress/lib/libc/sys/t_mlock.c
+++ /dev/null
@@ -1,329 +0,0 @@
1/* $OpenBSD: t_mlock.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_mlock.c,v 1.8 2020/01/24 08:45:16 skrll Exp $ */
3
4/*-
5 * Copyright (c) 2012 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/mman.h>
36#include <sys/resource.h>
37#include <sys/sysctl.h>
38#include <sys/wait.h>
39
40#include <errno.h>
41#include "atf-c.h"
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48static long page = 0;
49
50ATF_TC(mlock_clip);
51ATF_TC_HEAD(mlock_clip, tc)
52{
53 atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
54 "clips if the clip address is within the entry (PR kern/44788)");
55}
56
57ATF_TC_BODY(mlock_clip, tc)
58{
59 void *buf;
60 int err1, err2;
61
62 buf = malloc(page);
63 ATF_REQUIRE(buf != NULL);
64 fprintf(stderr, "mlock_clip: buf = %p (page=%ld)\n", buf, page);
65
66 if (page < 1024)
67 atf_tc_skip("page size too small");
68
69 for (size_t i = page; i >= 1; i = i - 1024) {
70 err1 = mlock(buf, page - i);
71 if (err1 != 0)
72 fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
73 " mlock(%p, %ld): %s\n", page, i, buf, page - i,
74 strerror(errno));
75 err2 = munlock(buf, page - i);
76 if (err2 != 0)
77 fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
78 " munlock(%p, %ld): %s (mlock %s)\n", page, i,
79 buf, page - i, strerror(errno), err1?"failed":"ok");
80 }
81
82 free(buf);
83}
84
85ATF_TC(mlock_err);
86ATF_TC_HEAD(mlock_err, tc)
87{
88 atf_tc_set_md_var(tc, "descr",
89 "Test error conditions in mlock(2) and munlock(2)");
90}
91
92ATF_TC_BODY(mlock_err, tc)
93{
94 void *invalid_ptr;
95 void *buf;
96 int mlock_err, munlock_err;
97
98 /*
99 * Any bad address must return ENOMEM (for lock & unlock)
100 */
101 errno = 0;
102 ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
103
104 errno = 0;
105 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
106
107 errno = 0;
108#ifdef __OpenBSD__
109 ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
110#else
111 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
112#endif
113
114 errno = 0;
115 ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
116
117 errno = 0;
118 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
119
120 errno = 0;
121#ifdef __OpenBSD__
122 ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
123#else
124 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
125#endif
126
127 buf = malloc(page);
128 ATF_REQUIRE(buf != NULL);
129 fprintf(stderr, "mlock_err: buf = %p (page=%ld)\n", buf, page);
130
131 /*
132 * unlocking memory that is not locked is an error...
133 */
134
135#ifndef __OpenBSD__
136 errno = 0;
137 ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
138#endif
139
140 /*
141 * These are permitted to fail (EINVAL) but do not on NetBSD
142 */
143 mlock_err = mlock((void *)(((uintptr_t)buf) + page/3), page/5);
144 if (mlock_err != 0)
145 fprintf(stderr, "mlock_err: mlock(%p, %ld): %d [%d] %s\n",
146 (void *)(((uintptr_t)buf) + page/3), page/5, mlock_err,
147 errno, strerror(errno));
148 ATF_REQUIRE(mlock_err == 0);
149 munlock_err= munlock((void *)(((uintptr_t)buf) + page/3), page/5);
150 if (munlock_err != 0)
151 fprintf(stderr, "mlock_err: munlock(%p, %ld): %d [%d] %s\n",
152 (void *)(((uintptr_t)buf) + page/3), page/5, munlock_err,
153 errno, strerror(errno));
154 ATF_REQUIRE(munlock_err == 0);
155
156 (void)free(buf);
157
158 /*
159 * Try to create a pointer to an unmapped page - first after current
160 * brk will likely do.
161 */
162 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
163 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
164
165 errno = 0;
166 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
167
168 errno = 0;
169 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
170}
171
172ATF_TC(mlock_limits);
173ATF_TC_HEAD(mlock_limits, tc)
174{
175 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
176}
177
178ATF_TC_BODY(mlock_limits, tc)
179{
180 struct rlimit res;
181 void *buf;
182 pid_t pid;
183 int sta;
184
185 buf = malloc(page);
186 ATF_REQUIRE(buf != NULL);
187 fprintf(stderr, "mlock_limits: buf = %p (page=%ld)\n", buf, page);
188
189 pid = fork();
190 ATF_REQUIRE(pid >= 0);
191
192 if (pid == 0) {
193
194 for (ssize_t i = page; i >= 2; i -= 100) {
195
196 res.rlim_cur = i - 1;
197 res.rlim_max = i - 1;
198
199 (void)fprintf(stderr, "trying to lock %zu bytes "
200 "with %zu byte limit\n", i, (size_t)res.rlim_cur);
201
202 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
203 _exit(EXIT_FAILURE);
204
205 errno = 0;
206
207 if ((sta = mlock(buf, i)) != -1 || errno != EAGAIN) {
208 fprintf(stderr, "mlock(%p, %zu): %d [%d] %s\n",
209 buf, i, sta, errno, strerror(errno));
210 (void)munlock(buf, i);
211 _exit(EXIT_FAILURE);
212 }
213 }
214
215 _exit(EXIT_SUCCESS);
216 }
217
218 (void)wait(&sta);
219
220 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
221 atf_tc_fail("mlock(2) locked beyond system limits");
222
223 free(buf);
224}
225
226ATF_TC(mlock_mmap);
227ATF_TC_HEAD(mlock_mmap, tc)
228{
229 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
230}
231
232ATF_TC_BODY(mlock_mmap, tc)
233{
234#ifdef __OpenBSD__
235 static const int flags = MAP_ANON | MAP_PRIVATE;
236#else
237 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
238#endif
239 void *buf;
240
241 /*
242 * Make a wired RW mapping and check that mlock(2)
243 * does not fail for the (already locked) mapping.
244 */
245 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
246
247 if (buf == MAP_FAILED)
248 fprintf(stderr,
249 "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
250 " [%d] %s\n", page, PROT_READ | PROT_WRITE, flags, errno,
251 strerror(errno));
252
253 ATF_REQUIRE(buf != MAP_FAILED);
254
255 fprintf(stderr, "mlock_mmap: buf=%p, page=%ld\n", buf, page);
256
257 ATF_REQUIRE(mlock(buf, page) == 0);
258 ATF_REQUIRE(munlock(buf, page) == 0);
259 ATF_REQUIRE(munmap(buf, page) == 0);
260 ATF_REQUIRE(munlock(buf, page) != 0);
261
262 fprintf(stderr, "mlock_mmap: first test succeeded\n");
263
264 /*
265 * But it should be impossible to mlock(2) a PROT_NONE mapping.
266 */
267 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
268
269 if (buf == MAP_FAILED)
270 fprintf(stderr,
271 "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
272 " [%d] %s\n", page, PROT_NONE, flags, errno,
273 strerror(errno));
274
275 ATF_REQUIRE(buf != MAP_FAILED);
276 ATF_REQUIRE(mlock(buf, page) != 0);
277 ATF_REQUIRE(munmap(buf, page) == 0);
278
279 fprintf(stderr, "mlock_mmap: second test succeeded\n");
280}
281
282ATF_TC(mlock_nested);
283ATF_TC_HEAD(mlock_nested, tc)
284{
285 atf_tc_set_md_var(tc, "descr",
286 "Test that consecutive mlock(2) calls succeed");
287}
288
289ATF_TC_BODY(mlock_nested, tc)
290{
291 const size_t maxiter = 100;
292 void *buf;
293 int err;
294
295 buf = malloc(page);
296 ATF_REQUIRE(buf != NULL);
297 fprintf(stderr, "mlock_nested: buf = %p (page=%ld)\n", buf, page);
298
299 for (size_t i = 0; i < maxiter; i++) {
300 err = mlock(buf, page);
301 if (err != 0)
302 fprintf(stderr,
303 "mlock_nested: i=%zu (of %zu) mlock(%p, %ld): %d [%d] %s\n",
304 i, maxiter, buf, page, err, errno, strerror(errno));
305 ATF_REQUIRE(err == 0);
306 }
307
308 err = munlock(buf, page);
309 if (err != 0)
310 fprintf(stderr, "mlock_nested: munlock(%p, %ld): %d [%d] %s\n",
311 buf, page, err, errno, strerror(errno));
312 ATF_REQUIRE(err == 0);
313 free(buf);
314}
315
316ATF_TP_ADD_TCS(tp)
317{
318
319 page = sysconf(_SC_PAGESIZE);
320 ATF_REQUIRE(page >= 0);
321
322 ATF_TP_ADD_TC(tp, mlock_clip);
323 ATF_TP_ADD_TC(tp, mlock_err);
324 ATF_TP_ADD_TC(tp, mlock_limits);
325 ATF_TP_ADD_TC(tp, mlock_mmap);
326 ATF_TP_ADD_TC(tp, mlock_nested);
327
328 return atf_no_error();
329}
diff --git a/src/regress/lib/libc/sys/t_mmap.c b/src/regress/lib/libc/sys/t_mmap.c
deleted file mode 100644
index 59c74296e3..0000000000
--- a/src/regress/lib/libc/sys/t_mmap.c
+++ /dev/null
@@ -1,590 +0,0 @@
1/* $OpenBSD: t_mmap.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_mmap.c,v 1.14 2020/06/26 07:50:11 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*-
34 * Copyright (c)2004 YAMAMOTO Takashi,
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59#include "macros.h"
60
61#include <sys/disklabel.h>
62#include <sys/mman.h>
63#include <sys/stat.h>
64#include <sys/socket.h>
65#include <sys/sysctl.h>
66#include <sys/wait.h>
67
68#include "atf-c.h"
69#include <errno.h>
70#include <fcntl.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <limits.h>
75#include <string.h>
76#include <unistd.h>
77#include <paths.h>
78
79static long page = 0;
80static char path[] = "mmap";
81static void map_check(void *, int);
82static void map_sighandler(int);
83static void testloan(void *, void *, char, int);
84
85#define BUFSIZE (32 * 1024) /* enough size to trigger sosend_loan */
86
87static void
88map_check(void *map, int flag)
89{
90
91 if (flag != 0) {
92 ATF_REQUIRE(map == MAP_FAILED);
93 return;
94 }
95
96 ATF_REQUIRE(map != MAP_FAILED);
97 ATF_REQUIRE(munmap(map, page) == 0);
98}
99
100void
101testloan(void *vp, void *vp2, char pat, int docheck)
102{
103 char buf[BUFSIZE];
104 char backup[BUFSIZE];
105 ssize_t nwritten;
106 ssize_t nread;
107 int fds[2];
108 int val;
109
110 val = BUFSIZE;
111
112 if (docheck != 0)
113 (void)memcpy(backup, vp, BUFSIZE);
114
115 if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds) != 0)
116 atf_tc_fail("socketpair() failed");
117
118 val = BUFSIZE;
119
120 if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) != 0)
121 atf_tc_fail("setsockopt() failed, SO_RCVBUF");
122
123 val = BUFSIZE;
124
125 if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) != 0)
126 atf_tc_fail("setsockopt() failed, SO_SNDBUF");
127
128 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) != 0)
129 atf_tc_fail("fcntl() failed");
130
131 nwritten = write(fds[0], (char *)vp + page, BUFSIZE - page);
132
133 if (nwritten == -1)
134 atf_tc_fail("write() failed");
135
136 /* Break loan. */
137 (void)memset(vp2, pat, BUFSIZE);
138
139 nread = read(fds[1], buf + page, BUFSIZE - page);
140
141 if (nread == -1)
142 atf_tc_fail("read() failed");
143
144 if (nread != nwritten)
145 atf_tc_fail("too short read");
146
147 if (docheck != 0 && memcmp(backup, buf + page, nread) != 0)
148 atf_tc_fail("data mismatch");
149
150 ATF_REQUIRE(close(fds[0]) == 0);
151 ATF_REQUIRE(close(fds[1]) == 0);
152}
153
154static void
155map_sighandler(int signo)
156{
157 _exit(signo);
158}
159
160ATF_TC(mmap_block);
161ATF_TC_HEAD(mmap_block, tc)
162{
163 atf_tc_set_md_var(tc, "descr", "Test mmap(2) with a block device");
164 atf_tc_set_md_var(tc, "require.user", "root");
165}
166
167ATF_TC_BODY(mmap_block, tc)
168{
169 static const int mib[] = { CTL_HW, HW_DISKNAMES };
170 static const unsigned int miblen = __arraycount(mib);
171 char *map, *dk, *drives, dev[PATH_MAX];
172 size_t len;
173 int fd = -1;
174
175#ifndef __OpenBSD__
176 /* works for us */
177 atf_tc_skip("The test case causes a panic " \
178 "(PR kern/38889, PR kern/46592)");
179#endif
180
181 ATF_REQUIRE(sysctl(mib, miblen, NULL, &len, NULL, 0) == 0);
182 drives = malloc(len);
183 ATF_REQUIRE(drives != NULL);
184 ATF_REQUIRE(sysctl(mib, miblen, drives, &len, NULL, 0) == 0);
185#ifdef __OpenBSD__
186 /* devices separated by comma, disk uid by colon */
187 for (dk = strtok(drives, ",:"); dk != NULL; dk = strtok(NULL, ",:")) {
188#else
189 for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
190#endif
191 if (strncmp(dk, "dk", 2) == 0)
192 snprintf(dev, sizeof(dev), _PATH_DEV "%s", dk);
193 else
194 snprintf(dev, sizeof(dev), _PATH_DEV "%s%c", dk,
195 'a' + RAW_PART);
196 fprintf(stderr, "trying: %s\n", dev);
197
198 if ((fd = open(dev, O_RDONLY)) >= 0) {
199 (void)fprintf(stderr, "using %s\n", dev);
200 break;
201 } else
202 (void)fprintf(stderr, "%s: %s\n", dev, strerror(errno));
203 }
204 free(drives);
205
206 if (fd < 0)
207 atf_tc_skip("failed to find suitable block device");
208
209 map = mmap(NULL, 4096, PROT_READ, MAP_FILE, fd, 0);
210 ATF_REQUIRE_MSG(map != MAP_FAILED, "mmap: %s", strerror(errno));
211
212 (void)fprintf(stderr, "first byte %x\n", *map);
213 ATF_REQUIRE(close(fd) == 0);
214 (void)fprintf(stderr, "first byte %x\n", *map);
215
216 ATF_REQUIRE(munmap(map, 4096) == 0);
217}
218
219ATF_TC(mmap_err);
220ATF_TC_HEAD(mmap_err, tc)
221{
222 atf_tc_set_md_var(tc, "descr", "Test error conditions of mmap(2)");
223}
224
225ATF_TC_BODY(mmap_err, tc)
226{
227 size_t addr = SIZE_MAX;
228 void *map;
229
230 errno = 0;
231 map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, -1, 0);
232
233 ATF_REQUIRE(map == MAP_FAILED);
234 ATF_REQUIRE(errno == EBADF);
235
236 errno = 0;
237 map = mmap(&addr, page, PROT_READ, MAP_FIXED|MAP_PRIVATE, -1, 0);
238
239 ATF_REQUIRE(map == MAP_FAILED);
240 ATF_REQUIRE(errno == EINVAL);
241
242 errno = 0;
243 map = mmap(NULL, page, PROT_READ, MAP_ANON|MAP_PRIVATE, INT_MAX, 0);
244
245 ATF_REQUIRE(map == MAP_FAILED);
246 ATF_REQUIRE(errno == EINVAL);
247}
248
249ATF_TC_WITH_CLEANUP(mmap_loan);
250ATF_TC_HEAD(mmap_loan, tc)
251{
252 atf_tc_set_md_var(tc, "descr", "Test uvm page loanout with mmap(2)");
253}
254
255ATF_TC_BODY(mmap_loan, tc)
256{
257 char buf[BUFSIZE];
258 char *vp, *vp2;
259 int fd;
260
261 fd = open(path, O_RDWR | O_CREAT, 0600);
262 ATF_REQUIRE(fd >= 0);
263
264 (void)memset(buf, 'x', sizeof(buf));
265 (void)write(fd, buf, sizeof(buf));
266
267 vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
268 MAP_FILE | MAP_PRIVATE, fd, 0);
269
270 ATF_REQUIRE(vp != MAP_FAILED);
271
272 vp2 = vp;
273
274 testloan(vp, vp2, 'A', 0);
275 testloan(vp, vp2, 'B', 1);
276
277 ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
278
279 vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
280 MAP_FILE | MAP_SHARED, fd, 0);
281
282 vp2 = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
283 MAP_FILE | MAP_SHARED, fd, 0);
284
285 ATF_REQUIRE(vp != MAP_FAILED);
286 ATF_REQUIRE(vp2 != MAP_FAILED);
287
288 testloan(vp, vp2, 'E', 1);
289
290 ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
291 ATF_REQUIRE(munmap(vp2, BUFSIZE) == 0);
292}
293
294ATF_TC_CLEANUP(mmap_loan, tc)
295{
296 (void)unlink(path);
297}
298
299ATF_TC_WITH_CLEANUP(mmap_prot_1);
300ATF_TC_HEAD(mmap_prot_1, tc)
301{
302 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #1");
303}
304
305ATF_TC_BODY(mmap_prot_1, tc)
306{
307 void *map;
308 int fd;
309
310 /*
311 * Open a file write-only and try to
312 * map it read-only. This should fail.
313 */
314 fd = open(path, O_WRONLY | O_CREAT, 0700);
315
316 if (fd < 0)
317 return;
318
319 ATF_REQUIRE(write(fd, "XXX", 3) == 3);
320
321 map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
322 map_check(map, 1);
323
324 map = mmap(NULL, 3, PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);
325 map_check(map, 0);
326
327 ATF_REQUIRE(close(fd) == 0);
328}
329
330ATF_TC_CLEANUP(mmap_prot_1, tc)
331{
332 (void)unlink(path);
333}
334
335ATF_TC(mmap_prot_2);
336ATF_TC_HEAD(mmap_prot_2, tc)
337{
338 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #2");
339}
340
341ATF_TC_BODY(mmap_prot_2, tc)
342{
343 char buf[2];
344 void *map;
345 pid_t pid;
346 int sta;
347
348 /*
349 * Make a PROT_NONE mapping and try to access it.
350 * If we catch a SIGSEGV, all works as expected.
351 */
352 map = mmap(NULL, page, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
353 ATF_REQUIRE(map != MAP_FAILED);
354
355 pid = fork();
356 ATF_REQUIRE(pid >= 0);
357
358 if (pid == 0) {
359 ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
360 ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
361 }
362
363 (void)wait(&sta);
364
365 ATF_REQUIRE(WIFEXITED(sta) != 0);
366 ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
367 ATF_REQUIRE(munmap(map, page) == 0);
368}
369
370ATF_TC_WITH_CLEANUP(mmap_prot_3);
371ATF_TC_HEAD(mmap_prot_3, tc)
372{
373 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #3");
374}
375
376ATF_TC_BODY(mmap_prot_3, tc)
377{
378 char buf[2];
379 int fd, sta;
380 void *map;
381 pid_t pid;
382
383 /*
384 * Open a file, change the permissions
385 * to read-only, and try to map it as
386 * PROT_NONE. This should succeed, but
387 * the access should generate SIGSEGV.
388 */
389 fd = open(path, O_RDWR | O_CREAT, 0700);
390
391 if (fd < 0)
392 return;
393
394 ATF_REQUIRE(write(fd, "XXX", 3) == 3);
395 ATF_REQUIRE(close(fd) == 0);
396 ATF_REQUIRE(chmod(path, 0444) == 0);
397
398 fd = open(path, O_RDONLY);
399 ATF_REQUIRE(fd != -1);
400
401 map = mmap(NULL, 3, PROT_NONE, MAP_FILE | MAP_SHARED, fd, 0);
402 ATF_REQUIRE(map != MAP_FAILED);
403
404 pid = fork();
405
406 ATF_REQUIRE(pid >= 0);
407
408 if (pid == 0) {
409 ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
410 ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
411 }
412
413 (void)wait(&sta);
414
415 ATF_REQUIRE(WIFEXITED(sta) != 0);
416 ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
417 ATF_REQUIRE(munmap(map, 3) == 0);
418}
419
420ATF_TC_CLEANUP(mmap_prot_3, tc)
421{
422 (void)unlink(path);
423}
424
425ATF_TC_WITH_CLEANUP(mmap_truncate);
426ATF_TC_HEAD(mmap_truncate, tc)
427{
428 atf_tc_set_md_var(tc, "descr", "Test mmap(2) and ftruncate(2)");
429}
430
431ATF_TC_BODY(mmap_truncate, tc)
432{
433 char *map;
434 long i;
435 int fd;
436
437 fd = open(path, O_RDWR | O_CREAT, 0700);
438
439 if (fd < 0)
440 return;
441
442 /*
443 * See that ftruncate(2) works
444 * while the file is mapped.
445 */
446 ATF_REQUIRE(ftruncate(fd, page) == 0);
447
448 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
449 fd, 0);
450 ATF_REQUIRE(map != MAP_FAILED);
451
452 for (i = 0; i < page; i++)
453 map[i] = 'x';
454
455 ATF_REQUIRE(ftruncate(fd, 0) == 0);
456 ATF_REQUIRE(ftruncate(fd, page / 8) == 0);
457 ATF_REQUIRE(ftruncate(fd, page / 4) == 0);
458 ATF_REQUIRE(ftruncate(fd, page / 2) == 0);
459 ATF_REQUIRE(ftruncate(fd, page / 12) == 0);
460 ATF_REQUIRE(ftruncate(fd, page / 64) == 0);
461
462 (void)munmap(map, page);
463 ATF_REQUIRE(close(fd) == 0);
464}
465
466ATF_TC_CLEANUP(mmap_truncate, tc)
467{
468 (void)unlink(path);
469}
470
471ATF_TC_WITH_CLEANUP(mmap_truncate_signal);
472ATF_TC_HEAD(mmap_truncate_signal, tc)
473{
474 atf_tc_set_md_var(tc, "descr",
475 "Test mmap(2) ftruncate(2) causing signal");
476}
477
478ATF_TC_BODY(mmap_truncate_signal, tc)
479{
480 char *map;
481 long i;
482 int fd, sta;
483 pid_t pid;
484
485 fd = open(path, O_RDWR | O_CREAT, 0700);
486
487 if (fd < 0)
488 return;
489
490 ATF_REQUIRE(write(fd, "foo\n", 5) == 5);
491
492 map = mmap(NULL, page, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
493 ATF_REQUIRE(map != MAP_FAILED);
494
495 sta = 0;
496 for (i = 0; i < 5; i++)
497 sta += map[i];
498 ATF_REQUIRE(sta == 334);
499
500 ATF_REQUIRE(ftruncate(fd, 0) == 0);
501 pid = fork();
502 ATF_REQUIRE(pid >= 0);
503
504 if (pid == 0) {
505 ATF_REQUIRE(signal(SIGBUS, map_sighandler) != SIG_ERR);
506 ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
507 sta = 0;
508 for (i = 0; i < page; i++)
509 sta += map[i];
510 /* child never will get this far, but the compiler will
511 not know, so better use the values calculated to
512 prevent the access to be optimized out */
513 ATF_REQUIRE(i == 0);
514 ATF_REQUIRE(sta == 0);
515 (void)munmap(map, page);
516 (void)close(fd);
517 return;
518 }
519
520 (void)wait(&sta);
521
522 ATF_REQUIRE(WIFEXITED(sta) != 0);
523 if (WEXITSTATUS(sta) == SIGSEGV)
524 atf_tc_fail("child process got SIGSEGV instead of SIGBUS");
525 ATF_REQUIRE(WEXITSTATUS(sta) == SIGBUS);
526 ATF_REQUIRE(munmap(map, page) == 0);
527 ATF_REQUIRE(close(fd) == 0);
528}
529
530ATF_TC_CLEANUP(mmap_truncate_signal, tc)
531{
532 (void)unlink(path);
533}
534
535ATF_TC(mmap_va0);
536ATF_TC_HEAD(mmap_va0, tc)
537{
538 atf_tc_set_md_var(tc, "descr", "Test mmap(2) and vm.user_va0_disable");
539}
540
541ATF_TC_BODY(mmap_va0, tc)
542{
543 int flags = MAP_ANON | MAP_FIXED | MAP_PRIVATE;
544 size_t len = sizeof(int);
545 void *map;
546 int val;
547
548 /*
549 * Make an anonymous fixed mapping at zero address. If the address
550 * is restricted as noted in security(7), the syscall should fail.
551 */
552 if (sysctlbyname("vm.user_va0_disable", &val, &len, NULL, 0) != 0)
553 atf_tc_fail("failed to read vm.user_va0_disable");
554
555 map = mmap(NULL, page, PROT_EXEC, flags, -1, 0);
556 map_check(map, val);
557
558 map = mmap(NULL, page, PROT_READ, flags, -1, 0);
559 map_check(map, val);
560
561 map = mmap(NULL, page, PROT_WRITE, flags, -1, 0);
562 map_check(map, val);
563
564 map = mmap(NULL, page, PROT_READ|PROT_WRITE, flags, -1, 0);
565 map_check(map, val);
566
567 map = mmap(NULL, page, PROT_EXEC|PROT_READ|PROT_WRITE, flags, -1, 0);
568 map_check(map, val);
569}
570
571ATF_TP_ADD_TCS(tp)
572{
573 page = sysconf(_SC_PAGESIZE);
574 ATF_REQUIRE(page >= 0);
575
576 ATF_TP_ADD_TC(tp, mmap_block);
577 ATF_TP_ADD_TC(tp, mmap_err);
578 ATF_TP_ADD_TC(tp, mmap_loan);
579 ATF_TP_ADD_TC(tp, mmap_prot_1);
580 ATF_TP_ADD_TC(tp, mmap_prot_2);
581 ATF_TP_ADD_TC(tp, mmap_prot_3);
582 ATF_TP_ADD_TC(tp, mmap_truncate);
583 ATF_TP_ADD_TC(tp, mmap_truncate_signal);
584#ifndef __OpenBSD__
585 /* sysctl vm.user_va0_disable not available */
586 ATF_TP_ADD_TC(tp, mmap_va0);
587#endif
588
589 return atf_no_error();
590}
diff --git a/src/regress/lib/libc/sys/t_msgctl.c b/src/regress/lib/libc/sys/t_msgctl.c
deleted file mode 100644
index 91c0eaaeaf..0000000000
--- a/src/regress/lib/libc/sys/t_msgctl.c
+++ /dev/null
@@ -1,402 +0,0 @@
1/* $OpenBSD: t_msgctl.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_msgctl.c,v 1.7 2017/10/07 17:15:44 kre Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/msg.h>
36#include <sys/stat.h>
37#include <sys/sysctl.h>
38#include <sys/wait.h>
39
40#include "atf-c.h"
41#include <errno.h>
42#include <limits.h>
43#include <pwd.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <sysexits.h>
49#include <time.h>
50#include <unistd.h>
51
52#define MSG_KEY 12345689
53#define MSG_MTYPE_1 0x41
54
55struct msg {
56 long mtype;
57 char buf[3];
58};
59
60static void clean(void);
61
62static void
63clean(void)
64{
65 int id;
66
67 if ((id = msgget(MSG_KEY, 0)) != -1)
68 (void)msgctl(id, IPC_RMID, 0);
69}
70
71ATF_TC_WITH_CLEANUP(msgctl_err);
72ATF_TC_HEAD(msgctl_err, tc)
73{
74 atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
75}
76
77ATF_TC_BODY(msgctl_err, tc)
78{
79 const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
80 struct msqid_ds msgds;
81 size_t i;
82 int id;
83
84 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
85
86 id = msgget(MSG_KEY, IPC_CREAT | 0600);
87 ATF_REQUIRE(id != -1);
88
89 errno = 0;
90 ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);
91
92 errno = 0;
93 ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);
94
95 for (i = 0; i < __arraycount(cmd); i++) {
96 errno = 0;
97 ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
98 }
99
100 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
101}
102
103ATF_TC_CLEANUP(msgctl_err, tc)
104{
105 clean();
106}
107
108ATF_TC_WITH_CLEANUP(msgctl_perm);
109ATF_TC_HEAD(msgctl_perm, tc)
110{
111 atf_tc_set_md_var(tc, "descr", "Test permissions with msgctl(2)");
112 atf_tc_set_md_var(tc, "require.user", "root");
113}
114
115ATF_TC_BODY(msgctl_perm, tc)
116{
117 struct msqid_ds msgds;
118 struct passwd *pw;
119 pid_t pid;
120 int sta;
121 int id;
122
123 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
124
125 pw = getpwnam("nobody");
126 id = msgget(MSG_KEY, IPC_CREAT | 0600);
127
128 ATF_REQUIRE(id != -1);
129 ATF_REQUIRE(pw != NULL);
130 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
131
132 pid = fork();
133 ATF_REQUIRE(pid >= 0);
134
135 if (pid == 0) {
136
137 if (setuid(pw->pw_uid) != 0)
138 _exit(EX_OSERR);
139
140 msgds.msg_perm.uid = getuid();
141 msgds.msg_perm.gid = getgid();
142
143 errno = 0;
144
145 if (msgctl(id, IPC_SET, &msgds) == 0)
146 _exit(EXIT_FAILURE);
147
148 if (errno != EPERM)
149 _exit(EXIT_FAILURE);
150
151 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
152
153 if (msgctl(id, IPC_STAT, &msgds) != 0)
154 _exit(EX_OSERR);
155
156 msgds.msg_qbytes = 1;
157
158 if (msgctl(id, IPC_SET, &msgds) == 0)
159 _exit(EXIT_FAILURE);
160
161 if (errno != EPERM)
162 _exit(EXIT_FAILURE);
163
164 _exit(EXIT_SUCCESS);
165 }
166
167 (void)wait(&sta);
168
169 if (WIFEXITED(sta) == 0) {
170
171 if (WEXITSTATUS(sta) == EX_OSERR)
172 atf_tc_fail("system call failed");
173
174 if (WEXITSTATUS(sta) == EXIT_FAILURE)
175 atf_tc_fail("UID %u manipulated root's "
176 "message queue", pw->pw_uid);
177 }
178
179 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
180}
181
182ATF_TC_CLEANUP(msgctl_perm, tc)
183{
184 clean();
185}
186
187ATF_TC_WITH_CLEANUP(msgctl_pid);
188ATF_TC_HEAD(msgctl_pid, tc)
189{
190 atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
191}
192
193ATF_TC_BODY(msgctl_pid, tc)
194{
195 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
196 struct msqid_ds msgds;
197 int id, sta;
198 pid_t pid;
199
200 id = msgget(MSG_KEY, IPC_CREAT | 0600);
201 ATF_REQUIRE(id != -1);
202
203 pid = fork();
204 ATF_REQUIRE(pid >= 0);
205
206 if (pid == 0) {
207
208 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
209
210 _exit(EXIT_SUCCESS);
211 }
212
213 (void)sleep(1);
214 (void)wait(&sta);
215 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
216
217 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
218
219 if (pid != msgds.msg_lspid)
220 atf_tc_fail("the PID of last msgsnd(2) was not updated");
221
222 pid = fork();
223 ATF_REQUIRE(pid >= 0);
224
225 if (pid == 0) {
226
227 (void)msgrcv(id, &msg,
228 sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
229
230 _exit(EXIT_SUCCESS);
231 }
232
233 (void)sleep(1);
234 (void)wait(&sta);
235 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
236
237 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
238
239 if (pid != msgds.msg_lrpid)
240 atf_tc_fail("the PID of last msgrcv(2) was not updated");
241
242 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
243}
244
245ATF_TC_CLEANUP(msgctl_pid, tc)
246{
247 clean();
248}
249
250ATF_TC_WITH_CLEANUP(msgctl_set);
251ATF_TC_HEAD(msgctl_set, tc)
252{
253 atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
254 atf_tc_set_md_var(tc, "require.user", "root");
255}
256
257ATF_TC_BODY(msgctl_set, tc)
258{
259 struct msqid_ds msgds;
260 struct passwd *pw;
261 int id;
262
263 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
264
265 pw = getpwnam("nobody");
266 id = msgget(MSG_KEY, IPC_CREAT | 0600);
267
268 ATF_REQUIRE(id != -1);
269 ATF_REQUIRE(pw != NULL);
270 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
271
272 msgds.msg_perm.uid = pw->pw_uid;
273
274 if (msgctl(id, IPC_SET, &msgds) != 0)
275 atf_tc_fail("root failed to change the UID of message queue");
276
277 msgds.msg_perm.uid = getuid();
278 msgds.msg_perm.gid = pw->pw_gid;
279
280 if (msgctl(id, IPC_SET, &msgds) != 0)
281 atf_tc_fail("root failed to change the GID of message queue");
282
283 /*
284 * Note: setting the qbytes to zero fails even as root.
285 */
286 msgds.msg_qbytes = 1;
287 msgds.msg_perm.gid = getgid();
288
289 if (msgctl(id, IPC_SET, &msgds) != 0)
290 atf_tc_fail("root failed to change qbytes of message queue");
291
292 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
293}
294
295ATF_TC_CLEANUP(msgctl_set, tc)
296{
297 clean();
298}
299
300ATF_TC_WITH_CLEANUP(msgctl_time);
301ATF_TC_HEAD(msgctl_time, tc)
302{
303 atf_tc_set_md_var(tc, "descr", "Test that access times are updated");
304}
305
306ATF_TC_BODY(msgctl_time, tc)
307{
308 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
309 struct msqid_ds msgds;
310 time_t t;
311 int id;
312
313 id = msgget(MSG_KEY, IPC_CREAT | 0600);
314 ATF_REQUIRE(id != -1);
315
316 t = time(NULL);
317
318 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
319 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
320 (void)msgctl(id, IPC_STAT, &msgds);
321
322 if (llabs(t - msgds.msg_stime) > 1)
323 atf_tc_fail("time of last msgsnd(2) was not updated");
324
325 if (msgds.msg_rtime != 0)
326 atf_tc_fail("time of last msgrcv(2) was updated incorrectly");
327
328 t = time(NULL);
329
330 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
331 (void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
332 (void)msgctl(id, IPC_STAT, &msgds);
333
334 if (llabs(t - msgds.msg_rtime) > 1)
335 atf_tc_fail("time of last msgrcv(2) was not updated");
336
337 /*
338 * Note: this is non-zero even after the memset(3).
339 */
340 if (msgds.msg_stime == 0)
341 atf_tc_fail("time of last msgsnd(2) was updated incorrectly");
342
343 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
344}
345
346ATF_TC_CLEANUP(msgctl_time, tc)
347{
348 clean();
349}
350
351static volatile int sig_caught;
352
353static void
354sigsys_handler(int signum)
355{
356
357 sig_caught = signum;
358}
359
360static int
361no_kernel_sysvmsg(void)
362{
363 int id;
364 void (*osig)(int);
365
366 sig_caught = 0;
367 osig = signal(SIGSYS, sigsys_handler);
368 id = msgget(MSG_KEY, IPC_CREAT | 0600);
369 if (sig_caught || id == -1)
370 return 1;
371
372 (void)msgctl(id, IPC_RMID, 0);
373 (void)signal(SIGSYS, osig);
374
375 return 0;
376}
377
378ATF_TC(msgctl_query);
379ATF_TC_HEAD(msgctl_query, tc)
380{
381 atf_tc_set_md_var(tc, "descr", "Skip msgctl_* tests - no SYSVMSG");
382}
383ATF_TC_BODY(msgctl_query, tc)
384{
385 atf_tc_skip("No SYSVMSG in kernel");
386}
387
388ATF_TP_ADD_TCS(tp)
389{
390
391 if (no_kernel_sysvmsg()) {
392 ATF_TP_ADD_TC(tp, msgctl_query);
393 } else {
394 ATF_TP_ADD_TC(tp, msgctl_err);
395 ATF_TP_ADD_TC(tp, msgctl_perm);
396 ATF_TP_ADD_TC(tp, msgctl_pid);
397 ATF_TP_ADD_TC(tp, msgctl_set);
398 ATF_TP_ADD_TC(tp, msgctl_time);
399 }
400
401 return atf_no_error();
402}
diff --git a/src/regress/lib/libc/sys/t_msgget.c b/src/regress/lib/libc/sys/t_msgget.c
deleted file mode 100644
index f4d3013b6f..0000000000
--- a/src/regress/lib/libc/sys/t_msgget.c
+++ /dev/null
@@ -1,337 +0,0 @@
1/* $OpenBSD: t_msgget.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_msgget.c,v 1.3 2017/10/08 08:31:05 kre Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/msg.h>
36#include <sys/stat.h>
37#include <sys/sysctl.h>
38#include <sys/wait.h>
39
40#include "atf-c.h"
41#include <errno.h>
42#include <pwd.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <string.h>
47#include <sysexits.h>
48#include <time.h>
49#include <unistd.h>
50
51#define MSG_KEY 12345689
52
53static void clean(void);
54
55static void
56clean(void)
57{
58 int id;
59
60 if ((id = msgget(MSG_KEY, 0)) != -1)
61 (void)msgctl(id, IPC_RMID, 0);
62}
63
64ATF_TC_WITH_CLEANUP(msgget_excl);
65ATF_TC_HEAD(msgget_excl, tc)
66{
67 atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL");
68}
69
70ATF_TC_BODY(msgget_excl, tc)
71{
72 int id;
73
74 /*
75 * Create a message queue and re-open it with
76 * O_CREAT and IPC_EXCL set. This should fail.
77 */
78 id = msgget(MSG_KEY, IPC_CREAT | 0600);
79
80 if (id < 0)
81 atf_tc_fail("failed to create message queue");
82
83 errno = 0;
84
85 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1)
86 atf_tc_fail("msgget(2) failed for IPC_EXCL");
87
88 ATF_REQUIRE(errno == EEXIST);
89
90 /*
91 * However, the same call should succeed
92 * when IPC_EXCL is not set in the flags.
93 */
94 id = msgget(MSG_KEY, IPC_CREAT | 0600);
95
96 if (id < 0)
97 atf_tc_fail("msgget(2) failed to re-open");
98
99 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
100}
101
102ATF_TC_CLEANUP(msgget_excl, tc)
103{
104 clean();
105}
106
107ATF_TC_WITH_CLEANUP(msgget_exit);
108ATF_TC_HEAD(msgget_exit, tc)
109{
110 atf_tc_set_md_var(tc, "descr",
111 "Test that XSI message queues are "
112 "not removed when the process exits");
113}
114
115ATF_TC_BODY(msgget_exit, tc)
116{
117 int id, sta;
118 pid_t pid;
119
120 pid = fork();
121 ATF_REQUIRE(pid >= 0);
122
123 if (pid == 0) {
124
125 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1)
126 _exit(EXIT_FAILURE);
127
128 _exit(EXIT_SUCCESS);
129 }
130
131 (void)wait(&sta);
132
133 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
134 atf_tc_fail("failed to create message queue");
135
136 id = msgget(MSG_KEY, 0);
137
138 if (id == -1)
139 atf_tc_fail("message queue was removed on process exit");
140
141 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
142}
143
144ATF_TC_CLEANUP(msgget_exit, tc)
145{
146 clean();
147}
148
149ATF_TC_WITH_CLEANUP(msgget_init);
150ATF_TC_HEAD(msgget_init, tc)
151{
152 atf_tc_set_md_var(tc, "descr",
153 "Test that msgget(2) initializes data structures properly");
154}
155
156ATF_TC_BODY(msgget_init, tc)
157{
158 const uid_t uid = geteuid();
159 const gid_t gid = getegid();
160 struct msqid_ds msgds;
161 time_t t;
162 int id;
163
164 (void)memset(&msgds, 0x9, sizeof(struct msqid_ds));
165
166 t = time(NULL);
167 id = msgget(MSG_KEY, IPC_CREAT | 0600);
168
169 ATF_REQUIRE(id !=-1);
170 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
171
172 ATF_CHECK(msgds.msg_qnum == 0);
173 ATF_CHECK(msgds.msg_lspid == 0);
174 ATF_CHECK(msgds.msg_lrpid == 0);
175 ATF_CHECK(msgds.msg_rtime == 0);
176 ATF_CHECK(msgds.msg_stime == 0);
177 ATF_CHECK(msgds.msg_perm.uid == uid);
178 ATF_CHECK(msgds.msg_perm.gid == gid);
179 ATF_CHECK(msgds.msg_perm.cuid == uid);
180 ATF_CHECK(msgds.msg_perm.cgid == gid);
181 ATF_CHECK(msgds.msg_perm.mode == 0600);
182
183 if (llabs(t - msgds.msg_ctime) > 5)
184 atf_tc_fail("msgget(2) initialized current time incorrectly");
185
186 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
187}
188
189ATF_TC_CLEANUP(msgget_init, tc)
190{
191 clean();
192}
193
194ATF_TC(msgget_limit);
195ATF_TC_HEAD(msgget_limit, tc)
196{
197 atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits");
198}
199
200ATF_TC_BODY(msgget_limit, tc)
201{
202 size_t len = sizeof(int);
203 bool fail = false;
204 int i, lim = 0;
205 int *buf;
206
207 if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0)
208 atf_tc_skip("failed to read kern.ipc.msgmni sysctl");
209
210 buf = calloc(lim + 1, sizeof(*buf));
211 ATF_REQUIRE(buf != NULL);
212
213 for (i = 0; i < lim; i++) {
214
215 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
216
217 (void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]);
218
219 /*
220 * This test only works when there are zero existing
221 * message queues. Thus, bypass the unit test when
222 * this precondition is not met, for reason or another.
223 */
224 if (buf[i] == -1)
225 goto out;
226 }
227
228 i++;
229 errno = 0;
230
231 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
232
233 if (buf[i] != -1 || errno != ENOSPC)
234 fail = true;
235
236out: /* Remember to clean-up. */
237 for (i = 0; i < lim; i++)
238 (void)msgctl(buf[i], IPC_RMID, 0);
239
240 free(buf);
241
242 if (fail != false)
243 atf_tc_fail("msgget(2) opened more than %d queues", lim);
244}
245
246ATF_TC_WITH_CLEANUP(msgget_mode);
247ATF_TC_HEAD(msgget_mode, tc)
248{
249 atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)");
250 atf_tc_set_md_var(tc, "require.user", "root");
251}
252
253ATF_TC_BODY(msgget_mode, tc)
254{
255 static const mode_t mode[] = {
256 S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP,
257 S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
258 };
259
260 struct msqid_ds msgds;
261 size_t i;
262 int id;
263
264 for (i = 0; i < __arraycount(mode); i++) {
265
266 (void)fprintf(stderr, "testing mode %d\n", mode[i]);
267 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
268
269 id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]);
270
271 ATF_REQUIRE(id != -1);
272 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
273 ATF_REQUIRE(msgds.msg_perm.mode == mode[i]);
274 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
275 }
276}
277
278ATF_TC_CLEANUP(msgget_mode, tc)
279{
280 clean();
281}
282
283static volatile int sig_caught;
284
285static void
286sigsys_handler(int signum)
287{
288
289 sig_caught = signum;
290}
291
292static int
293no_kernel_sysvmsg(void)
294{
295 int id;
296 void (*osig)(int);
297
298 sig_caught = 0;
299 osig = signal(SIGSYS, sigsys_handler);
300 id = msgget(MSG_KEY, IPC_CREAT | 0600);
301 if (sig_caught || id == -1)
302 return 1;
303
304 (void)msgctl(id, IPC_RMID, 0);
305 (void)signal(SIGSYS, osig);
306
307 return 0;
308}
309
310ATF_TC(msgget_query);
311ATF_TC_HEAD(msgget_query, tc)
312{
313 atf_tc_set_md_var(tc, "descr", "Skip msgget_* tests - no SYSVMSG");
314}
315ATF_TC_BODY(msgget_query, tc)
316{
317 atf_tc_skip("No SYSVMSG in kernel");
318}
319
320ATF_TP_ADD_TCS(tp)
321{
322
323 if (no_kernel_sysvmsg()) {
324 ATF_TP_ADD_TC(tp, msgget_query);
325 } else {
326 ATF_TP_ADD_TC(tp, msgget_excl);
327 ATF_TP_ADD_TC(tp, msgget_exit);
328 ATF_TP_ADD_TC(tp, msgget_init);
329#ifndef __OpenBSD__
330 /* sysctl kern.ipc.msgmni not available */
331 ATF_TP_ADD_TC(tp, msgget_limit);
332#endif
333 ATF_TP_ADD_TC(tp, msgget_mode);
334 }
335
336 return atf_no_error();
337}
diff --git a/src/regress/lib/libc/sys/t_msgrcv.c b/src/regress/lib/libc/sys/t_msgrcv.c
deleted file mode 100644
index 2e73b3d5ba..0000000000
--- a/src/regress/lib/libc/sys/t_msgrcv.c
+++ /dev/null
@@ -1,385 +0,0 @@
1/* $OpenBSD: t_msgrcv.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_msgrcv.c,v 1.5 2017/10/08 08:31:05 kre Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/msg.h>
36#include <sys/stat.h>
37#include <sys/sysctl.h>
38#include <sys/wait.h>
39
40#include "atf-c.h"
41#include <errno.h>
42#include <limits.h>
43#include <pwd.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <sysexits.h>
49#include <time.h>
50#include <unistd.h>
51
52#define MSG_KEY 1234
53#define MSG_MTYPE_1 0x41
54#define MSG_MTYPE_2 0x42
55#define MSG_MTYPE_3 0x43
56#define MSG_LEN 3
57
58struct msg {
59 long mtype;
60 char buf[MSG_LEN];
61};
62
63static void clean(void);
64
65static void
66clean(void)
67{
68 int id;
69
70 if ((id = msgget(MSG_KEY, 0)) != -1)
71 (void)msgctl(id, IPC_RMID, 0);
72}
73
74ATF_TC_WITH_CLEANUP(msgrcv_basic);
75ATF_TC_HEAD(msgrcv_basic, tc)
76{
77 atf_tc_set_md_var(tc, "descr", "A basic test of msgrcv(2)");
78}
79
80ATF_TC_BODY(msgrcv_basic, tc)
81{
82 struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
83 struct msg msg2 = { MSG_MTYPE_1, { 'x', 'y', 'z' } };
84 int id;
85
86 id = msgget(MSG_KEY, IPC_CREAT | 0600);
87 ATF_REQUIRE(id != -1);
88
89 (void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
90 (void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT);
91
92 ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
93 ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
94 ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
95
96 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
97}
98
99ATF_TC_CLEANUP(msgrcv_basic, tc)
100{
101 clean();
102}
103
104ATF_TC_WITH_CLEANUP(msgrcv_block);
105ATF_TC_HEAD(msgrcv_block, tc)
106{
107 atf_tc_set_md_var(tc, "descr", "Test that msgrcv(2) blocks");
108}
109
110ATF_TC_BODY(msgrcv_block, tc)
111{
112 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
113 int id, sta;
114 pid_t pid;
115
116 id = msgget(MSG_KEY, IPC_CREAT | 0600);
117 ATF_REQUIRE(id != -1);
118
119 pid = fork();
120 ATF_REQUIRE(pid >= 0);
121
122 if (pid == 0) {
123
124 if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1, 0) < 0)
125 _exit(EXIT_FAILURE);
126
127 _exit(EXIT_SUCCESS);
128 }
129
130 /*
131 * Below msgsnd(2) should unblock the child,
132 * and hence kill(2) should fail with ESRCH.
133 */
134 (void)sleep(1);
135 (void)msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT);
136 (void)sleep(1);
137 (void)kill(pid, SIGKILL);
138 (void)wait(&sta);
139
140 if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
141 atf_tc_fail("msgrcv(2) did not block");
142
143 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
144}
145
146ATF_TC_CLEANUP(msgrcv_block, tc)
147{
148 clean();
149}
150
151ATF_TC_WITH_CLEANUP(msgrcv_err);
152ATF_TC_HEAD(msgrcv_err, tc)
153{
154 atf_tc_set_md_var(tc, "descr", "Test errors from msgrcv(2)");
155}
156
157ATF_TC_BODY(msgrcv_err, tc)
158{
159 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
160 int id, r = 0;
161
162 id = msgget(MSG_KEY, IPC_CREAT | 0600);
163 ATF_REQUIRE(id != -1);
164
165 errno = 0;
166
167 ATF_REQUIRE_ERRNO(ENOMSG, msgrcv(id, &msg,
168 MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
169
170 ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
171
172 errno = 0;
173
174 ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1,
175 MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
176
177 errno = 0;
178
179 ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
180 MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
181
182 errno = 0;
183
184 ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
185 SSIZE_MAX, MSG_MTYPE_1, IPC_NOWAIT) == -1);
186
187 ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
188
189 errno = 0;
190
191 ATF_REQUIRE_ERRNO(E2BIG, msgrcv(id, &r,
192 MSG_LEN - 1, MSG_MTYPE_1, IPC_NOWAIT) == -1);
193
194 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
195}
196
197ATF_TC_CLEANUP(msgrcv_err, tc)
198{
199 clean();
200}
201
202
203ATF_TC_WITH_CLEANUP(msgrcv_mtype);
204ATF_TC_HEAD(msgrcv_mtype, tc)
205{
206 atf_tc_set_md_var(tc, "descr", "Test message types with msgrcv(2)");
207}
208
209ATF_TC_BODY(msgrcv_mtype, tc)
210{
211 struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
212 struct msg msg2 = { MSG_MTYPE_3, { 'x', 'y', 'z' } };
213 int id;
214
215 id = msgget(MSG_KEY, IPC_CREAT | 0600);
216 ATF_REQUIRE(id != -1);
217
218 (void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
219 (void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_2, IPC_NOWAIT);
220
221 ATF_CHECK(msg1.buf[0] != msg2.buf[0]); /* Different mtype. */
222 ATF_CHECK(msg1.buf[1] != msg2.buf[1]);
223 ATF_CHECK(msg1.buf[2] != msg2.buf[2]);
224
225 (void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT);
226
227 ATF_CHECK(msg1.buf[0] == msg2.buf[0]); /* Same mtype. */
228 ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
229 ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
230
231 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
232}
233
234ATF_TC_CLEANUP(msgrcv_mtype, tc)
235{
236 clean();
237}
238
239ATF_TC_WITH_CLEANUP(msgrcv_nonblock);
240ATF_TC_HEAD(msgrcv_nonblock, tc)
241{
242 atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with IPC_NOWAIT");
243 atf_tc_set_md_var(tc, "timeout", "10");
244}
245
246ATF_TC_BODY(msgrcv_nonblock, tc)
247{
248 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
249 const ssize_t n = 10;
250 int id, sta;
251 ssize_t i;
252 pid_t pid;
253
254 id = msgget(MSG_KEY, IPC_CREAT | 0600);
255 ATF_REQUIRE(id != -1);
256
257 for (i = 0; i < n; i++) {
258
259 ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
260 }
261
262 pid = fork();
263 ATF_REQUIRE(pid >= 0);
264
265 if (pid == 0) {
266
267 while (i != 0) {
268
269 if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1,
270 IPC_NOWAIT) == -1)
271 _exit(EXIT_FAILURE);
272
273 i--;
274 }
275
276 _exit(EXIT_SUCCESS);
277 }
278
279 (void)sleep(2);
280 (void)kill(pid, SIGKILL);
281 (void)wait(&sta);
282
283 if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
284 atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT");
285
286 if (WIFEXITED(sta) == 0 && WEXITSTATUS(sta) != EXIT_SUCCESS)
287 atf_tc_fail("msgrcv(2) failed");
288
289 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
290}
291
292ATF_TC_CLEANUP(msgrcv_nonblock, tc)
293{
294 clean();
295}
296
297ATF_TC_WITH_CLEANUP(msgrcv_truncate);
298ATF_TC_HEAD(msgrcv_truncate, tc)
299{
300 atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with MSG_NOERROR");
301}
302
303ATF_TC_BODY(msgrcv_truncate, tc)
304{
305#define MSG_SMALLLEN 2
306 struct msgsmall {
307 long mtype;
308 char buf[MSG_SMALLLEN];
309 };
310
311 struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
312 struct msgsmall msg2 = { MSG_MTYPE_1, { 'x', 'y' } };
313 int id;
314
315 id = msgget(MSG_KEY, IPC_CREAT | 0600);
316 ATF_REQUIRE(id != -1);
317
318 (void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
319 (void)msgrcv(id, &msg2, MSG_SMALLLEN,
320 MSG_MTYPE_1, IPC_NOWAIT | MSG_NOERROR);
321
322 ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
323 ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
324
325 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
326}
327
328ATF_TC_CLEANUP(msgrcv_truncate, tc)
329{
330 clean();
331}
332
333static volatile int sig_caught;
334
335static void
336sigsys_handler(int signum)
337{
338
339 sig_caught = signum;
340}
341
342static int
343no_kernel_sysvmsg(void)
344{
345 int id;
346 void (*osig)(int);
347
348 sig_caught = 0;
349 osig = signal(SIGSYS, sigsys_handler);
350 id = msgget(MSG_KEY, IPC_CREAT | 0600);
351 if (sig_caught || id == -1)
352 return 1;
353
354 (void)msgctl(id, IPC_RMID, 0);
355 (void)signal(SIGSYS, osig);
356
357 return 0;
358}
359
360ATF_TC(msgrcv_query);
361ATF_TC_HEAD(msgrcv_query, tc)
362{
363 atf_tc_set_md_var(tc, "descr", "Skip msgrcv_* tests - no SYSVMSG");
364}
365ATF_TC_BODY(msgrcv_query, tc)
366{
367 atf_tc_skip("No SYSVMSG in kernel");
368}
369
370ATF_TP_ADD_TCS(tp)
371{
372
373 if (no_kernel_sysvmsg()) {
374 ATF_TP_ADD_TC(tp, msgrcv_query);
375 } else {
376 ATF_TP_ADD_TC(tp, msgrcv_basic);
377 ATF_TP_ADD_TC(tp, msgrcv_block);
378 ATF_TP_ADD_TC(tp, msgrcv_err);
379 ATF_TP_ADD_TC(tp, msgrcv_mtype);
380 ATF_TP_ADD_TC(tp, msgrcv_nonblock);
381 ATF_TP_ADD_TC(tp, msgrcv_truncate);
382 }
383
384 return atf_no_error();
385}
diff --git a/src/regress/lib/libc/sys/t_msgsnd.c b/src/regress/lib/libc/sys/t_msgsnd.c
deleted file mode 100644
index 4ea2aa485e..0000000000
--- a/src/regress/lib/libc/sys/t_msgsnd.c
+++ /dev/null
@@ -1,381 +0,0 @@
1/* $OpenBSD: t_msgsnd.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_msgsnd.c,v 1.4 2017/10/08 08:31:05 kre Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/msg.h>
36#include <sys/stat.h>
37#include <sys/sysctl.h>
38#include <sys/wait.h>
39
40#include "atf-c.h"
41#include <errno.h>
42#include <limits.h>
43#include <pwd.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <sysexits.h>
49#include <time.h>
50#include <unistd.h>
51
52#define MSG_KEY 1234
53#define MSG_MTYPE_1 0x41
54#define MSG_MTYPE_2 0x42
55#define MSG_MTYPE_3 0x43
56
57struct msg {
58 long mtype;
59 char buf[3];
60};
61
62static void clean(void);
63
64static void
65clean(void)
66{
67 int id;
68
69 if ((id = msgget(MSG_KEY, 0)) != -1)
70 (void)msgctl(id, IPC_RMID, 0);
71}
72
73ATF_TC_WITH_CLEANUP(msgsnd_block);
74ATF_TC_HEAD(msgsnd_block, tc)
75{
76 atf_tc_set_md_var(tc, "descr", "Test that msgsnd(2) blocks");
77 atf_tc_set_md_var(tc, "timeout", "10");
78}
79
80ATF_TC_BODY(msgsnd_block, tc)
81{
82 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
83 int id, sta;
84 pid_t pid;
85
86 id = msgget(MSG_KEY, IPC_CREAT | 0600);
87 ATF_REQUIRE(id != -1);
88
89 pid = fork();
90 ATF_REQUIRE(pid >= 0);
91
92 if (pid == 0) {
93
94 /*
95 * Enqueue messages until some limit (e.g. the maximum
96 * number of messages in the queue or the maximum number
97 * of bytes in the queue) is reached. After this the call
98 * should block when the IPC_NOWAIT is not set.
99 */
100 for (;;) {
101
102 if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0)
103 _exit(EXIT_FAILURE);
104 }
105 }
106
107 (void)sleep(2);
108 (void)kill(pid, SIGKILL);
109 (void)wait(&sta);
110
111 if (WIFEXITED(sta) != 0 || WIFSIGNALED(sta) == 0)
112 atf_tc_fail("msgsnd(2) did not block");
113
114 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
115}
116
117ATF_TC_CLEANUP(msgsnd_block, tc)
118{
119 clean();
120}
121
122ATF_TC_WITH_CLEANUP(msgsnd_count);
123ATF_TC_HEAD(msgsnd_count, tc)
124{
125 atf_tc_set_md_var(tc, "descr",
126 "Test that msgsnd(2) increments the amount of "
127 "message in the queue, as given by msgctl(2)");
128 atf_tc_set_md_var(tc, "timeout", "10");
129}
130
131ATF_TC_BODY(msgsnd_count, tc)
132{
133 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
134 struct msqid_ds ds;
135 size_t i = 0;
136 int id, rv;
137
138 id = msgget(MSG_KEY, IPC_CREAT | 0600);
139 ATF_REQUIRE(id != -1);
140
141 for (;;) {
142
143 errno = 0;
144 rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
145
146 if (rv == 0) {
147 i++;
148 continue;
149 }
150
151 if (rv == -1 && errno == EAGAIN)
152 break;
153
154 atf_tc_fail("failed to enqueue a message");
155 }
156
157 (void)memset(&ds, 0, sizeof(struct msqid_ds));
158 (void)msgctl(id, IPC_STAT, &ds);
159
160 if (ds.msg_qnum != i)
161 atf_tc_fail("incorrect message count");
162
163 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
164}
165
166ATF_TC_CLEANUP(msgsnd_count, tc)
167{
168 clean();
169}
170
171ATF_TC_WITH_CLEANUP(msgsnd_err);
172ATF_TC_HEAD(msgsnd_err, tc)
173{
174 atf_tc_set_md_var(tc, "descr", "Test errors from msgsnd(2)");
175}
176
177ATF_TC_BODY(msgsnd_err, tc)
178{
179 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
180 int id;
181
182 id = msgget(MSG_KEY, IPC_CREAT | 0600);
183 ATF_REQUIRE(id != -1);
184
185 errno = 0;
186
187 ATF_REQUIRE_ERRNO(EFAULT, msgsnd(id, (void *)-1,
188 sizeof(struct msg), IPC_NOWAIT) == -1);
189
190 errno = 0;
191
192 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
193 sizeof(struct msg), IPC_NOWAIT) == -1);
194
195 errno = 0;
196
197 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
198 SSIZE_MAX, IPC_NOWAIT) == -1);
199
200 errno = 0;
201 msg.mtype = 0;
202
203 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(id, &msg,
204 sizeof(struct msg), IPC_NOWAIT) == -1);
205
206 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
207}
208
209ATF_TC_CLEANUP(msgsnd_err, tc)
210{
211 clean();
212}
213
214ATF_TC_WITH_CLEANUP(msgsnd_nonblock);
215ATF_TC_HEAD(msgsnd_nonblock, tc)
216{
217 atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) with IPC_NOWAIT");
218 atf_tc_set_md_var(tc, "timeout", "10");
219}
220
221ATF_TC_BODY(msgsnd_nonblock, tc)
222{
223 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
224 int id, rv, sta;
225 pid_t pid;
226
227 id = msgget(MSG_KEY, IPC_CREAT | 0600);
228 ATF_REQUIRE(id != -1);
229
230 pid = fork();
231 ATF_REQUIRE(pid >= 0);
232
233 if (pid == 0) {
234
235 for (;;) {
236
237 errno = 0;
238 rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
239
240 if (rv == -1 && errno == EAGAIN)
241 _exit(EXIT_SUCCESS);
242 }
243 }
244
245 (void)sleep(2);
246 (void)kill(pid, SIGKILL);
247 (void)wait(&sta);
248
249 if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
250 atf_tc_fail("msgsnd(2) blocked with IPC_NOWAIT");
251
252 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
253}
254
255ATF_TC_CLEANUP(msgsnd_nonblock, tc)
256{
257 clean();
258}
259
260ATF_TC_WITH_CLEANUP(msgsnd_perm);
261ATF_TC_HEAD(msgsnd_perm, tc)
262{
263 atf_tc_set_md_var(tc, "descr", "Test permissions with msgsnd(2)");
264 atf_tc_set_md_var(tc, "require.user", "root");
265}
266
267ATF_TC_BODY(msgsnd_perm, tc)
268{
269 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
270 struct passwd *pw;
271 int id, sta;
272 pid_t pid;
273 uid_t uid;
274
275 pw = getpwnam("nobody");
276 id = msgget(MSG_KEY, IPC_CREAT | 0600);
277
278 ATF_REQUIRE(id != -1);
279 ATF_REQUIRE(pw != NULL);
280
281 uid = pw->pw_uid;
282 ATF_REQUIRE(uid != 0);
283
284 pid = fork();
285 ATF_REQUIRE(pid >= 0);
286
287 if (pid == 0) {
288
289 /*
290 * Try to enqueue a message to the queue
291 * created by root as RW for owner only.
292 */
293 if (setuid(uid) != 0)
294 _exit(EX_OSERR);
295
296 id = msgget(MSG_KEY, 0);
297
298 if (id == -1)
299 _exit(EX_OSERR);
300
301 errno = 0;
302
303 if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0)
304 _exit(EXIT_FAILURE);
305
306 if (errno != EACCES)
307 _exit(EXIT_FAILURE);
308
309 _exit(EXIT_SUCCESS);
310 }
311
312 (void)wait(&sta);
313
314 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
315
316 if (errno == EX_OSERR)
317 atf_tc_fail("system call failed");
318
319 atf_tc_fail("UID %u enqueued message to root's queue", uid);
320 }
321
322 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
323}
324
325ATF_TC_CLEANUP(msgsnd_perm, tc)
326{
327 clean();
328}
329
330static volatile int sig_caught;
331
332static void
333sigsys_handler(int signum)
334{
335
336 sig_caught = signum;
337}
338
339static int
340no_kernel_sysvmsg(void)
341{
342 int id;
343 void (*osig)(int);
344
345 sig_caught = 0;
346 osig = signal(SIGSYS, sigsys_handler);
347 id = msgget(MSG_KEY, IPC_CREAT | 0600);
348 if (sig_caught || id == -1)
349 return 1;
350
351 (void)msgctl(id, IPC_RMID, 0);
352 (void)signal(SIGSYS, osig);
353
354 return 0;
355}
356
357ATF_TC(msgsnd_query);
358ATF_TC_HEAD(msgsnd_query, tc)
359{
360 atf_tc_set_md_var(tc, "descr", "Skip msgsnd_* tests - no SYSVMSG");
361}
362ATF_TC_BODY(msgsnd_query, tc)
363{
364 atf_tc_skip("No SYSVMSG in kernel");
365}
366
367ATF_TP_ADD_TCS(tp)
368{
369
370 if (no_kernel_sysvmsg()) {
371 ATF_TP_ADD_TC(tp, msgsnd_query);
372 } else {
373 ATF_TP_ADD_TC(tp, msgsnd_block);
374 ATF_TP_ADD_TC(tp, msgsnd_count);
375 ATF_TP_ADD_TC(tp, msgsnd_err);
376 ATF_TP_ADD_TC(tp, msgsnd_nonblock);
377 ATF_TP_ADD_TC(tp, msgsnd_perm);
378 }
379
380 return atf_no_error();
381}
diff --git a/src/regress/lib/libc/sys/t_msync.c b/src/regress/lib/libc/sys/t_msync.c
deleted file mode 100644
index acce0f664e..0000000000
--- a/src/regress/lib/libc/sys/t_msync.c
+++ /dev/null
@@ -1,225 +0,0 @@
1/* $OpenBSD: t_msync.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_msync.c,v 1.3 2017/01/14 20:52:42 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/mman.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45static long page = 0;
46static const off_t off = 512;
47static const char path[] = "msync";
48
49static const char *msync_sync(const char *, int);
50
51static const char *
52msync_sync(const char *garbage, int flags)
53{
54 char *buf, *map = MAP_FAILED;
55 const char *str = NULL;
56 size_t len;
57 int fd, rv;
58
59 /*
60 * Create a temporary file, write
61 * one page to it, and map the file.
62 */
63 buf = malloc(page);
64
65 if (buf == NULL)
66 return NULL;
67
68 memset(buf, 'x', page);
69
70 fd = open(path, O_RDWR | O_CREAT, 0700);
71
72 if (fd < 0) {
73 free(buf);
74 return "failed to open";
75 }
76
77 ATF_REQUIRE_MSG(write(fd, buf, page) != -1, "write(2) failed: %s",
78 strerror(errno));
79
80 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
81 fd, 0);
82
83 if (map == MAP_FAILED) {
84 str = "failed to map";
85 goto out;
86 }
87
88 /*
89 * Seek to an arbitrary offset and
90 * write garbage to this position.
91 */
92 if (lseek(fd, off, SEEK_SET) != off) {
93 str = "failed to seek";
94 goto out;
95 }
96
97 len = strlen(garbage);
98 rv = write(fd, garbage, len);
99
100 if (rv != (ssize_t)len) {
101 str = "failed to write garbage";
102 goto out;
103 }
104
105 /*
106 * Synchronize the mapping and verify
107 * that garbage is at the given offset.
108 */
109 if (msync(map, page, flags) != 0) {
110 str = "failed to msync";
111 goto out;
112 }
113
114 if (memcmp(map + off, garbage, len) != 0) {
115 str = "msync did not synchronize";
116 goto out;
117 }
118
119out:
120 free(buf);
121
122 (void)close(fd);
123 (void)unlink(path);
124
125 if (map != MAP_FAILED)
126 (void)munmap(map, page);
127
128 return str;
129}
130
131ATF_TC(msync_async);
132ATF_TC_HEAD(msync_async, tc)
133{
134 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_ASYNC");
135}
136
137ATF_TC_BODY(msync_async, tc)
138{
139 const char *str;
140
141 str = msync_sync("garbage", MS_ASYNC);
142
143 if (str != NULL)
144 atf_tc_fail("%s", str);
145}
146
147ATF_TC(msync_err);
148ATF_TC_HEAD(msync_err, tc)
149{
150 atf_tc_set_md_var(tc, "descr", "Test error conditions in msync(2)");
151}
152
153ATF_TC_BODY(msync_err, tc)
154{
155
156 char *map = MAP_FAILED;
157
158 /*
159 * Test that invalid flags error out.
160 */
161 ATF_REQUIRE(msync_sync("error", -1) != NULL);
162 ATF_REQUIRE(msync_sync("error", INT_MAX) != NULL);
163
164 errno = 0;
165
166 /*
167 * Map a page and then unmap to get an unmapped address.
168 */
169 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
170 -1, 0);
171 ATF_REQUIRE(map != MAP_FAILED);
172
173 (void)munmap(map, page);
174
175 ATF_REQUIRE(msync(map, page, MS_SYNC) != 0);
176 ATF_REQUIRE(errno == EFAULT);
177}
178
179ATF_TC(msync_invalidate);
180ATF_TC_HEAD(msync_invalidate, tc)
181{
182 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_INVALIDATE");
183}
184
185ATF_TC_BODY(msync_invalidate, tc)
186{
187 const char *str;
188
189 str = msync_sync("garbage", MS_INVALIDATE);
190
191 if (str != NULL)
192 atf_tc_fail("%s", str);
193}
194
195ATF_TC(msync_sync);
196ATF_TC_HEAD(msync_sync, tc)
197{
198 atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_SYNC");
199}
200
201ATF_TC_BODY(msync_sync, tc)
202{
203 const char *str;
204
205 str = msync_sync("garbage", MS_SYNC);
206
207 if (str != NULL)
208 atf_tc_fail("%s", str);
209}
210
211ATF_TP_ADD_TCS(tp)
212{
213
214 page = sysconf(_SC_PAGESIZE);
215
216 ATF_REQUIRE(page >= 0);
217 ATF_REQUIRE(page > off);
218
219 ATF_TP_ADD_TC(tp, msync_async);
220 ATF_TP_ADD_TC(tp, msync_err);
221 ATF_TP_ADD_TC(tp, msync_invalidate);
222 ATF_TP_ADD_TC(tp, msync_sync);
223
224 return atf_no_error();
225}
diff --git a/src/regress/lib/libc/sys/t_pipe.c b/src/regress/lib/libc/sys/t_pipe.c
deleted file mode 100644
index 7640cc2944..0000000000
--- a/src/regress/lib/libc/sys/t_pipe.c
+++ /dev/null
@@ -1,162 +0,0 @@
1/* $OpenBSD: t_pipe.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_pipe.c,v 1.7 2020/06/26 07:50:11 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/types.h>
33#include <sys/wait.h>
34
35#include <errno.h>
36#include <fcntl.h>
37#include <poll.h>
38#include <sched.h>
39#include <signal.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43
44#include "atf-c.h"
45
46#include "h_macros.h"
47
48static pid_t pid;
49static int nsiginfo = 0;
50
51/*
52 * This is used for both parent and child. Handle parent's SIGALRM,
53 * the childs SIGINFO doesn't need anything.
54 */
55static void
56sighand(int sig)
57{
58 if (sig == SIGALRM) {
59 kill(pid, SIGINFO);
60 }
61 if (sig == SIGINFO) {
62 nsiginfo++;
63 }
64}
65
66ATF_TC(pipe_restart);
67ATF_TC_HEAD(pipe_restart, tc)
68{
69 atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe "
70 "works correctly after being interrupted and restarted "
71 "(PR kern/14087)");
72}
73
74ATF_TC_BODY(pipe_restart, tc)
75{
76 int pp[2], st;
77 ssize_t sz, todo, done;
78 char *f;
79 sigset_t asigset, osigset, emptysigset;
80
81 /* Initialise signal masks */
82 RL(sigemptyset(&emptysigset));
83 RL(sigemptyset(&asigset));
84 RL(sigaddset(&asigset, SIGINFO));
85
86 /* Register signal handlers for both read and writer */
87 REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR);
88 REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR);
89
90 todo = 2 * 1024 * 1024;
91 REQUIRE_LIBC(f = malloc(todo), NULL);
92
93 RL(pipe(pp));
94
95 RL(pid = fork());
96 if (pid == 0) {
97 /* child */
98 RL(close(pp[1]));
99
100 /* Do initial write. This should succeed, make
101 * the other side do partial write and wait for us to pick
102 * rest up.
103 */
104 RL(done = read(pp[0], f, 128 * 1024));
105
106 /* Wait until parent is alarmed and awakens us */
107 RL(sigprocmask(SIG_BLOCK, &asigset, &osigset));
108 while (nsiginfo == 0) {
109 if (sigsuspend(&emptysigset) != -1 || errno != EINTR)
110 atf_tc_fail("sigsuspend(&emptysigset): %s",
111 strerror(errno));
112 }
113 RL(sigprocmask(SIG_SETMASK, &osigset, NULL));
114
115 /* Read all what parent wants to give us */
116 while((sz = read(pp[0], f, 1024 * 1024)) > 0)
117 done += sz;
118
119 /*
120 * Exit with 1 if number of bytes read doesn't match
121 * number of expected bytes
122 */
123 printf("Read: %#zx\n", (size_t)done);
124 printf("Expected: %#zx\n", (size_t)todo);
125
126 exit(done != todo);
127
128 /* NOTREACHED */
129 } else {
130 RL(close(pp[0]));
131
132 /*
133 * Arrange for alarm after two seconds. Since we have
134 * handler setup for SIGARLM, the write(2) call should
135 * be restarted internally by kernel.
136 */
137 (void)alarm(2);
138
139 /* We write exactly 'todo' bytes. The very first write(2)
140 * should partially succeed, block and eventually
141 * be restarted by kernel
142 */
143 while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
144 todo -= sz;
145
146 /* Close the pipe, so that child would stop reading */
147 RL(close(pp[1]));
148
149 /* And pickup child's exit status */
150 RL(waitpid(pid, &st, 0));
151
152 ATF_REQUIRE_EQ(WEXITSTATUS(st), 0);
153 }
154 free(f);
155}
156
157ATF_TP_ADD_TCS(tp)
158{
159 ATF_TP_ADD_TC(tp, pipe_restart);
160
161 return atf_no_error();
162}
diff --git a/src/regress/lib/libc/sys/t_pipe2.c b/src/regress/lib/libc/sys/t_pipe2.c
deleted file mode 100644
index b88d933c9a..0000000000
--- a/src/regress/lib/libc/sys/t_pipe2.c
+++ /dev/null
@@ -1,199 +0,0 @@
1/* $OpenBSD: t_pipe2.c,v 1.4 2023/10/31 07:56:44 claudio Exp $ */
2/* $NetBSD: t_pipe2.c,v 1.9 2017/01/13 21:19:45 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include "macros.h"
41
42#include "atf-c.h"
43#include <fcntl.h>
44#include <unistd.h>
45#include <stdlib.h>
46#include <errno.h>
47#include <sys/resource.h>
48
49static void
50run(int flags)
51{
52 int fd[2], i;
53
54 while ((i = open("/", O_RDONLY)) < 3)
55 ATF_REQUIRE(i != -1);
56
57 ATF_REQUIRE_MSG(closefrom(3) != -1, "closefrom failed: %s",
58 strerror(errno));
59
60 ATF_REQUIRE(pipe2(fd, flags) == 0);
61
62 ATF_REQUIRE(fd[0] == 3);
63 ATF_REQUIRE(fd[1] == 4);
64
65 if (flags & O_CLOEXEC) {
66 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0);
67 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0);
68 } else {
69 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0);
70 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0);
71 }
72
73 if (flags & O_NONBLOCK) {
74 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0);
75 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0);
76 } else {
77 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0);
78 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0);
79 }
80
81#ifndef __OpenBSD__
82 /* F_GETNOSIGPIPE not available */
83 if (flags & O_NOSIGPIPE) {
84 ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) != 0);
85 ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) != 0);
86 } else {
87 ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) == 0);
88 ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) == 0);
89 }
90#endif
91
92 ATF_REQUIRE(close(fd[0]) != -1);
93 ATF_REQUIRE(close(fd[1]) != -1);
94}
95
96ATF_TC(pipe2_basic);
97ATF_TC_HEAD(pipe2_basic, tc)
98{
99 atf_tc_set_md_var(tc, "descr", "A basic test of pipe2(2)");
100}
101
102ATF_TC_BODY(pipe2_basic, tc)
103{
104 run(0);
105}
106
107ATF_TC(pipe2_consume);
108ATF_TC_HEAD(pipe2_consume, tc)
109{
110 atf_tc_set_md_var(tc, "descr", "Test that consuming file descriptors "
111 "with pipe2(2) does not crash the system (PR kern/46457)");
112}
113
114ATF_TC_BODY(pipe2_consume, tc)
115{
116 struct rlimit rl;
117 int err, filedes[2];
118 int old;
119
120 (void)closefrom(4);
121
122 err = getrlimit(RLIMIT_NOFILE, &rl);
123 ATF_REQUIRE(err == 0);
124 /*
125 * The heart of this test is to run against the number of open
126 * file descriptor limit in the middle of a pipe2() call - i.e.
127 * before the call only a single descriptor may be openend.
128 */
129 old = rl.rlim_cur;
130 rl.rlim_cur = 4;
131 err = setrlimit(RLIMIT_NOFILE, &rl);
132 ATF_REQUIRE(err == 0);
133
134 err = pipe2(filedes, O_CLOEXEC);
135 ATF_REQUIRE(err == -1);
136 rl.rlim_cur = old;
137 err = setrlimit(RLIMIT_NOFILE, &rl);
138}
139
140ATF_TC(pipe2_nonblock);
141ATF_TC_HEAD(pipe2_nonblock, tc)
142{
143 atf_tc_set_md_var(tc, "descr", "A non-blocking test of pipe2(2)");
144}
145
146ATF_TC_BODY(pipe2_nonblock, tc)
147{
148 run(O_NONBLOCK);
149}
150
151ATF_TC(pipe2_cloexec);
152ATF_TC_HEAD(pipe2_cloexec, tc)
153{
154 atf_tc_set_md_var(tc, "descr", "A close-on-exec test of pipe2(2)");
155}
156
157ATF_TC_BODY(pipe2_cloexec, tc)
158{
159 run(O_CLOEXEC);
160}
161
162ATF_TC(pipe2_nosigpipe);
163ATF_TC_HEAD(pipe2_nosigpipe, tc)
164{
165 atf_tc_set_md_var(tc, "descr", "A no sigpipe test of pipe2(2)");
166}
167
168ATF_TC_BODY(pipe2_nosigpipe, tc)
169{
170 run(O_NOSIGPIPE);
171}
172
173ATF_TC(pipe2_einval);
174ATF_TC_HEAD(pipe2_einval, tc)
175{
176 atf_tc_set_md_var(tc, "descr", "A error check of pipe2(2)");
177}
178
179ATF_TC_BODY(pipe2_einval, tc)
180{
181 int fd[2];
182 ATF_REQUIRE_ERRNO(EINVAL, pipe2(fd, O_ASYNC) == -1);
183}
184
185ATF_TP_ADD_TCS(tp)
186{
187
188 ATF_TP_ADD_TC(tp, pipe2_basic);
189 ATF_TP_ADD_TC(tp, pipe2_consume);
190 ATF_TP_ADD_TC(tp, pipe2_nonblock);
191 ATF_TP_ADD_TC(tp, pipe2_cloexec);
192#ifndef __OpenBSD__
193 /* O_NOSIGPIPE not available */
194 ATF_TP_ADD_TC(tp, pipe2_nosigpipe);
195#endif
196 ATF_TP_ADD_TC(tp, pipe2_einval);
197
198 return atf_no_error();
199}
diff --git a/src/regress/lib/libc/sys/t_poll.c b/src/regress/lib/libc/sys/t_poll.c
deleted file mode 100644
index 345b6c7ad0..0000000000
--- a/src/regress/lib/libc/sys/t_poll.c
+++ /dev/null
@@ -1,247 +0,0 @@
1/* $OpenBSD: t_poll.c,v 1.3 2022/05/28 18:39:39 mbuhl Exp $ */
2/* $NetBSD: t_poll.c,v 1.4 2020/07/17 15:34:16 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Matthias Scheler.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/time.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <paths.h>
42#include <poll.h>
43#include <stdio.h>
44#include <signal.h>
45#include <unistd.h>
46
47static int desc;
48
49static void
50child1(void)
51{
52 struct pollfd pfd;
53
54 pfd.fd = desc;
55 pfd.events = POLLIN | POLLHUP | POLLOUT;
56
57 (void)poll(&pfd, 1, 2000);
58 (void)printf("child1 exit\n");
59}
60
61static void
62child2(void)
63{
64 struct pollfd pfd;
65
66 pfd.fd = desc;
67 pfd.events = POLLIN | POLLHUP | POLLOUT;
68
69 (void)sleep(1);
70 (void)poll(&pfd, 1, INFTIM);
71 (void)printf("child2 exit\n");
72}
73
74static void
75child3(void)
76{
77 struct pollfd pfd;
78
79 (void)sleep(5);
80
81 pfd.fd = desc;
82 pfd.events = POLLIN | POLLHUP | POLLOUT;
83
84 (void)poll(&pfd, 1, INFTIM);
85 (void)printf("child3 exit\n");
86}
87
88ATF_TC(3way);
89ATF_TC_HEAD(3way, tc)
90{
91 atf_tc_set_md_var(tc, "timeout", "15");
92 atf_tc_set_md_var(tc, "descr",
93 "Check for 3-way collision for descriptor. First child comes "
94 "and polls on descriptor, second child comes and polls, first "
95 "child times out and exits, third child comes and polls. When "
96 "the wakeup event happens, the two remaining children should "
97 "both be awaken. (kern/17517)");
98}
99
100ATF_TC_BODY(3way, tc)
101{
102 int pf[2];
103 int status, i;
104 pid_t pid;
105
106 pipe(pf);
107 desc = pf[0];
108
109 pid = fork();
110 ATF_REQUIRE(pid >= 0);
111
112 if (pid == 0) {
113 (void)close(pf[1]);
114 child1();
115 _exit(0);
116 /* NOTREACHED */
117 }
118
119 pid = fork();
120 ATF_REQUIRE(pid >= 0);
121
122 if (pid == 0) {
123 (void)close(pf[1]);
124 child2();
125 _exit(0);
126 /* NOTREACHED */
127 }
128
129 pid = fork();
130 ATF_REQUIRE( pid >= 0);
131
132 if (pid == 0) {
133 (void)close(pf[1]);
134 child3();
135 _exit(0);
136 /* NOTREACHED */
137 }
138
139 (void)sleep(10);
140
141 (void)printf("parent write\n");
142
143 ATF_REQUIRE(write(pf[1], "konec\n", 6) == 6);
144
145 for(i = 0; i < 3; ++i)
146 (void)wait(&status);
147
148 (void)printf("parent terminated\n");
149}
150
151ATF_TC(basic);
152ATF_TC_HEAD(basic, tc)
153{
154 atf_tc_set_md_var(tc, "timeout", "10");
155 atf_tc_set_md_var(tc, "descr",
156 "Basis functionality test for poll(2)");
157}
158
159ATF_TC_BODY(basic, tc)
160{
161 int fds[2];
162 struct pollfd pfds[2];
163 int ret;
164
165 ATF_REQUIRE_EQ(pipe(fds), 0);
166
167 pfds[0].fd = fds[0];
168 pfds[0].events = POLLIN;
169 pfds[1].fd = fds[1];
170 pfds[1].events = POLLOUT;
171
172 /*
173 * Check that we get a timeout waiting for data on the read end
174 * of our pipe.
175 */
176 pfds[0].revents = -1;
177 pfds[1].revents = -1;
178 ret = poll(&pfds[0], 1, 1);
179 ATF_REQUIRE_EQ_MSG(ret, 0, "got: %d", ret);
180 ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
181 ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
182
183 /* Check that the write end of the pipe as reported as ready. */
184 pfds[0].revents = -1;
185 pfds[1].revents = -1;
186 ret = poll(&pfds[1], 1, 1);
187 ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
188 ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
189 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
190 pfds[1].revents);
191
192 /* Check that only the write end of the pipe as reported as ready. */
193 pfds[0].revents = -1;
194 pfds[1].revents = -1;
195 ret = poll(pfds, 2, 1);
196 ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
197 ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
198 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
199 pfds[1].revents);
200
201 /* Write data to our pipe. */
202 ATF_REQUIRE_EQ(write(fds[1], "", 1), 1);
203
204 /* Check that both ends of our pipe are reported as ready. */
205 pfds[0].revents = -1;
206 pfds[1].revents = -1;
207 ret = poll(pfds, 2, 1);
208 ATF_REQUIRE_EQ_MSG(ret, 2, "got: %d", ret);
209 ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
210 pfds[0].revents);
211 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
212 pfds[1].revents);
213
214 ATF_REQUIRE_EQ(close(fds[0]), 0);
215 ATF_REQUIRE_EQ(close(fds[1]), 0);
216}
217
218ATF_TC(err);
219ATF_TC_HEAD(err, tc)
220{
221 atf_tc_set_md_var(tc, "descr", "Check errors from poll(2)");
222}
223
224ATF_TC_BODY(err, tc)
225{
226 struct pollfd pfd;
227 int fd = 0;
228
229 pfd.fd = fd;
230 pfd.events = POLLIN;
231
232 errno = 0;
233 ATF_REQUIRE_ERRNO(EFAULT, poll((struct pollfd *)-1, 1, -1) == -1);
234
235 errno = 0;
236 ATF_REQUIRE_ERRNO(EINVAL, poll(&pfd, 1, -2) == -1);
237}
238
239ATF_TP_ADD_TCS(tp)
240{
241
242 ATF_TP_ADD_TC(tp, 3way);
243 ATF_TP_ADD_TC(tp, basic);
244 ATF_TP_ADD_TC(tp, err);
245
246 return atf_no_error();
247}
diff --git a/src/regress/lib/libc/sys/t_pollts.c b/src/regress/lib/libc/sys/t_pollts.c
deleted file mode 100644
index 966b8c7721..0000000000
--- a/src/regress/lib/libc/sys/t_pollts.c
+++ /dev/null
@@ -1,203 +0,0 @@
1/* $OpenBSD: t_pollts.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $ */
2/* $NetBSD: t_pollts.c,v 1.1 2020/07/17 15:34:17 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2011, 2020 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Matthias Scheler.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32#include "macros.h"
33
34#include <sys/time.h>
35#include <sys/wait.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <paths.h>
41#include <poll.h>
42#include <stdio.h>
43#include <signal.h>
44#include <unistd.h>
45
46#ifndef POLLTS
47#define POLLTS pollts
48#endif
49
50ATF_TC(basic);
51ATF_TC_HEAD(basic, tc)
52{
53 atf_tc_set_md_var(tc, "timeout", "10");
54 atf_tc_set_md_var(tc, "descr",
55 "Basis functionality test for ppoll(2)/pollts(2)");
56}
57
58ATF_TC_BODY(basic, tc)
59{
60 int fds[2];
61 struct pollfd pfds[2];
62 struct timespec timeout;
63 int ret;
64
65 ATF_REQUIRE_EQ(pipe(fds), 0);
66
67 pfds[0].fd = fds[0];
68 pfds[0].events = POLLIN;
69 pfds[1].fd = fds[1];
70 pfds[1].events = POLLOUT;
71
72 /* Use a timeout of 1 second. */
73 timeout.tv_sec = 1;
74 timeout.tv_nsec = 0;
75
76 /*
77 * Check that we get a timeout waiting for data on the read end
78 * of our pipe.
79 */
80 pfds[0].revents = -1;
81 pfds[1].revents = -1;
82 ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfds[0], 1, &timeout, NULL), 0,
83 "got: %d", ret);
84 ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
85 ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
86
87 /* Check that the write end of the pipe as reported as ready. */
88 pfds[0].revents = -1;
89 pfds[1].revents = -1;
90 ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfds[1], 1, &timeout, NULL), 1,
91 "got: %d", ret);
92 ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
93 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
94 pfds[1].revents);
95
96 /* Check that only the write end of the pipe as reported as ready. */
97 pfds[0].revents = -1;
98 pfds[1].revents = -1;
99 ATF_REQUIRE_EQ_MSG(ret = POLLTS(pfds, 2, &timeout, NULL), 1,
100 "got: %d", ret);
101 ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
102 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
103 pfds[1].revents);
104
105 /* Write data to our pipe. */
106 ATF_REQUIRE_EQ(write(fds[1], "", 1), 1);
107
108 /* Check that both ends of our pipe are reported as ready. */
109 pfds[0].revents = -1;
110 pfds[1].revents = -1;
111 ATF_REQUIRE_EQ_MSG(ret = POLLTS(pfds, 2, &timeout, NULL), 2,
112 "got: %d", ret);
113 ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
114 pfds[0].revents);
115 ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
116 pfds[1].revents);
117
118 ATF_REQUIRE_EQ(close(fds[0]), 0);
119 ATF_REQUIRE_EQ(close(fds[1]), 0);
120}
121
122ATF_TC(err);
123ATF_TC_HEAD(err, tc)
124{
125 atf_tc_set_md_var(tc, "descr", "Check errors from ppoll(2)/pollts(2)");
126}
127
128ATF_TC_BODY(err, tc)
129{
130 struct timespec timeout;
131 struct pollfd pfd;
132 int fd = 0;
133
134 pfd.fd = fd;
135 pfd.events = POLLIN;
136
137 timeout.tv_sec = 1;
138 timeout.tv_nsec = 0;
139
140 errno = 0;
141 ATF_REQUIRE_ERRNO(EFAULT, POLLTS((void *)-1, 1, &timeout, NULL) == -1);
142
143 timeout.tv_sec = -1;
144 timeout.tv_nsec = -1;
145
146 errno = 0;
147 ATF_REQUIRE_ERRNO(EINVAL, POLLTS(&pfd, 1, &timeout, NULL) == -1);
148}
149
150ATF_TC(sigmask);
151ATF_TC_HEAD(sigmask, tc)
152{
153 atf_tc_set_md_var(tc, "timeout", "10");
154 atf_tc_set_md_var(tc, "descr",
155 "Check that ppoll(2)/pollts(2) restores the signal mask (PR kern/44986)");
156}
157
158ATF_TC_BODY(sigmask, tc)
159{
160 int fd;
161 struct pollfd pfd;
162 struct timespec timeout;
163 sigset_t mask;
164 int ret;
165
166 fd = open(_PATH_DEVNULL, O_RDONLY);
167 ATF_REQUIRE(fd >= 0);
168
169 pfd.fd = fd;
170 pfd.events = POLLIN;
171
172 /* Use a timeout of 1 second. */
173 timeout.tv_sec = 1;
174 timeout.tv_nsec = 0;
175
176 /* Unblock all signals. */
177 ATF_REQUIRE_EQ(sigfillset(&mask), 0);
178 ATF_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &mask, NULL), 0);
179
180 /*
181 * Check that ppoll(2)/pollts(2) immediately returns. We block *all*
182 * signals during ppoll(2)/pollts(2).
183 */
184 ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfd, 1, &timeout, &mask), 1,
185 "got: %d", ret);
186
187 /* Check that signals are now longer blocked. */
188 ATF_REQUIRE_EQ(sigprocmask(SIG_SETMASK, NULL, &mask), 0);
189 ATF_REQUIRE_EQ_MSG(sigismember(&mask, SIGUSR1), 0,
190 "signal mask was changed.");
191
192 ATF_REQUIRE_EQ(close(fd), 0);
193}
194
195ATF_TP_ADD_TCS(tp)
196{
197
198 ATF_TP_ADD_TC(tp, basic);
199 ATF_TP_ADD_TC(tp, err);
200 ATF_TP_ADD_TC(tp, sigmask);
201
202 return atf_no_error();
203}
diff --git a/src/regress/lib/libc/sys/t_ppoll.c b/src/regress/lib/libc/sys/t_ppoll.c
deleted file mode 100644
index c5daef1f5f..0000000000
--- a/src/regress/lib/libc/sys/t_ppoll.c
+++ /dev/null
@@ -1,34 +0,0 @@
1/* $OpenBSD: t_ppoll.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $ */
2/* $NetBSD: t_ppoll.c,v 1.1 2020/07/17 15:34:17 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2011, 2020 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Matthias Scheler.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#define POLLTS ppoll
34#include "t_pollts.c"
diff --git a/src/regress/lib/libc/sys/t_ptrace.c b/src/regress/lib/libc/sys/t_ptrace.c
deleted file mode 100644
index 4b87acfdd2..0000000000
--- a/src/regress/lib/libc/sys/t_ptrace.c
+++ /dev/null
@@ -1,232 +0,0 @@
1/* $OpenBSD: t_ptrace.c,v 1.5 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_ptrace.c,v 1.4 2018/05/14 12:44:40 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2016 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/types.h>
33#include <sys/ptrace.h>
34#include <sys/stat.h>
35#include <sys/sysctl.h>
36#include <err.h>
37#include <errno.h>
38#include <limits.h>
39#include <unistd.h>
40
41#include "atf-c.h"
42
43#include "h_macros.h"
44
45/*
46 * A child process cannot call atf functions and expect them to magically
47 * work like in the parent.
48 * The printf(3) messaging from a child will not work out of the box as well
49 * without estabilishing a communication protocol with its parent. To not
50 * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
51 * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
52 */
53#define FORKEE_ASSERTX(x) \
54do { \
55 int ret = (x); \
56 if (!ret) \
57 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
58 __FILE__, __LINE__, __func__, #x); \
59} while (0)
60
61#define FORKEE_ASSERT(x) \
62do { \
63 int ret = (x); \
64 if (!ret) \
65 err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
66 __FILE__, __LINE__, __func__, #x); \
67} while (0)
68
69ATF_TC(attach_pid0);
70ATF_TC_HEAD(attach_pid0, tc)
71{
72 atf_tc_set_md_var(tc, "descr",
73 "Assert that a debugger cannot attach to PID 0");
74}
75
76ATF_TC_BODY(attach_pid0, tc)
77{
78 errno = 0;
79 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 0, NULL, 0) == -1);
80}
81
82ATF_TC(attach_pid1);
83ATF_TC_HEAD(attach_pid1, tc)
84{
85 atf_tc_set_md_var(tc, "descr",
86 "Assert that a debugger cannot attach to PID 1 (as non-root)");
87
88 atf_tc_set_md_var(tc, "require.user", "unprivileged");
89}
90
91ATF_TC_BODY(attach_pid1, tc)
92{
93 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
94}
95
96ATF_TC(attach_pid1_securelevel);
97ATF_TC_HEAD(attach_pid1_securelevel, tc)
98{
99 atf_tc_set_md_var(tc, "descr",
100 "Assert that a debugger cannot attach to PID 1 with "
101 "securelevel >= 0 (as root)");
102
103 atf_tc_set_md_var(tc, "require.user", "root");
104}
105
106ATF_TC_BODY(attach_pid1_securelevel, tc)
107{
108 int level;
109 size_t len = sizeof(level);
110
111 ATF_REQUIRE(sysctlbyname("kern.securelevel", &level, &len, NULL, 0)
112 != -1);
113
114 if (level < 0) {
115 atf_tc_skip("Test must be run with securelevel >= 0");
116 }
117
118 ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
119}
120
121ATF_TC(attach_self);
122ATF_TC_HEAD(attach_self, tc)
123{
124 atf_tc_set_md_var(tc, "descr",
125 "Assert that a debugger cannot attach to self (as it's nonsense)");
126}
127
128ATF_TC_BODY(attach_self, tc)
129{
130 ATF_REQUIRE_ERRNO(EINVAL, ptrace(PT_ATTACH, getpid(), NULL, 0) == -1);
131}
132
133ATF_TC(attach_chroot);
134ATF_TC_HEAD(attach_chroot, tc)
135{
136 atf_tc_set_md_var(tc, "descr",
137 "Assert that a debugger cannot trace another process unless the "
138 "process's root directory is at or below the tracing process's "
139 "root");
140
141 atf_tc_set_md_var(tc, "require.user", "root");
142}
143
144ATF_TC_BODY(attach_chroot, tc)
145{
146 char buf[PATH_MAX];
147 pid_t child;
148 int fds_toparent[2], fds_fromparent[2];
149 int rv;
150 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
151
152 (void)memset(buf, '\0', sizeof(buf));
153 ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
154 (void)strlcat(buf, "/dir", sizeof(buf));
155
156 ATF_REQUIRE(mkdir(buf, 0500) == 0);
157 ATF_REQUIRE(chdir(buf) == 0);
158
159 ATF_REQUIRE(pipe(fds_toparent) == 0);
160 ATF_REQUIRE(pipe(fds_fromparent) == 0);
161 child = atf_utils_fork();
162 if (child == 0) {
163 FORKEE_ASSERT(close(fds_toparent[0]) == 0);
164 FORKEE_ASSERT(close(fds_fromparent[1]) == 0);
165
166 FORKEE_ASSERT(chroot(buf) == 0);
167
168 rv = write(fds_toparent[1], &msg, sizeof(msg));
169 FORKEE_ASSERTX(rv == sizeof(msg));
170
171#ifdef __OpenBSD__
172 ATF_REQUIRE_ERRNO(EINVAL,
173#else
174 ATF_REQUIRE_ERRNO(EPERM,
175#endif
176 ptrace(PT_ATTACH, getppid(), NULL, 0) == -1);
177
178 rv = read(fds_fromparent[0], &msg, sizeof(msg));
179 FORKEE_ASSERTX(rv == sizeof(msg));
180
181 _exit(0);
182 }
183 ATF_REQUIRE(close(fds_toparent[1]) == 0);
184 ATF_REQUIRE(close(fds_fromparent[0]) == 0);
185
186 printf("Waiting for chrooting of the child PID %d\n", child);
187 rv = read(fds_toparent[0], &msg, sizeof(msg));
188 ATF_REQUIRE(rv == sizeof(msg));
189
190 printf("Child is ready, it will try to PT_ATTACH to parent\n");
191 rv = write(fds_fromparent[1], &msg, sizeof(msg));
192 ATF_REQUIRE(rv == sizeof(msg));
193
194 printf("fds_fromparent is no longer needed - close it\n");
195 ATF_REQUIRE(close(fds_fromparent[1]) == 0);
196
197 printf("fds_toparent is no longer needed - close it\n");
198 ATF_REQUIRE(close(fds_toparent[0]) == 0);
199}
200
201ATF_TC(traceme_twice);
202ATF_TC_HEAD(traceme_twice, tc)
203{
204 atf_tc_set_md_var(tc, "descr",
205 "Assert that a process cannot mark its parent a debugger twice");
206}
207
208ATF_TC_BODY(traceme_twice, tc)
209{
210
211 printf("Mark the parent process (PID %d) a debugger of PID %d\n",
212 getppid(), getpid());
213 ATF_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) == 0);
214
215 printf("Mark the parent process (PID %d) a debugger of PID %d again\n",
216 getppid(), getpid());
217 ATF_REQUIRE_ERRNO(EBUSY, ptrace(PT_TRACE_ME, 0, NULL, 0) == -1);
218}
219
220ATF_TP_ADD_TCS(tp)
221{
222 setvbuf(stdout, NULL, _IONBF, 0);
223 setvbuf(stderr, NULL, _IONBF, 0);
224 ATF_TP_ADD_TC(tp, attach_pid0);
225 ATF_TP_ADD_TC(tp, attach_pid1);
226 ATF_TP_ADD_TC(tp, attach_pid1_securelevel);
227 ATF_TP_ADD_TC(tp, attach_self);
228 ATF_TP_ADD_TC(tp, attach_chroot);
229 ATF_TP_ADD_TC(tp, traceme_twice);
230
231 return atf_no_error();
232}
diff --git a/src/regress/lib/libc/sys/t_recvmmsg.c b/src/regress/lib/libc/sys/t_recvmmsg.c
deleted file mode 100644
index 793692dac1..0000000000
--- a/src/regress/lib/libc/sys/t_recvmmsg.c
+++ /dev/null
@@ -1,189 +0,0 @@
1/* $OpenBSD: t_recvmmsg.c,v 1.2 2022/09/11 20:51:44 mbuhl Exp $ */
2/* $NetBSD: t_recvmmsg.c,v 1.4 2018/08/21 10:39:21 christos Exp $ */
3
4/*-
5 * Copyright (c) 2012 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jared McNeill and Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39#include <sys/cdefs.h>
40
41#include "atf-c.h"
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <sys/wait.h>
45
46#include <string.h>
47#include <time.h>
48#include <stdint.h>
49#include <errno.h>
50#include <signal.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <unistd.h>
54#include <sched.h>
55
56#define BUFSIZE 65536
57#define NPKTS 50
58
59#define min(a, b) ((a) < (b) ? (a) : (b))
60static int debug;
61static volatile sig_atomic_t rdied;
62
63static void
64handle_sigchld(__unused int pid)
65{
66
67 rdied = 1;
68}
69
70ATF_TC(recvmmsg_basic);
71ATF_TC_HEAD(recvmmsg_basic, tc)
72{
73 atf_tc_set_md_var(tc, "descr", "A basic test of recvmmsg(2)");
74}
75
76ATF_TC_BODY(recvmmsg_basic, tc)
77{
78 int fd[2], error, i, cnt;
79 uint8_t *buf;
80 struct mmsghdr *mmsghdr;
81 struct iovec *iov;
82 unsigned int mmsgcnt, n;
83 int status;
84 off_t off;
85 uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
86 struct sigaction sa;
87 ssize_t overf = 0;
88
89 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
90 ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
91
92 buf = malloc(BUFSIZE);
93 ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
94
95 mmsgcnt = BUFSIZE / sizeof(DGRAM);
96 mmsghdr = malloc(sizeof(*mmsghdr) * mmsgcnt);
97 ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
98 iov = malloc(sizeof(*iov) * mmsgcnt);
99 ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
100
101 for (off = 0, n = 0; n < mmsgcnt; n++) {
102 iov[n].iov_base = buf + off;
103 iov[n].iov_len = sizeof(DGRAM);
104 off += iov[n].iov_len;
105 mmsghdr[n].msg_hdr.msg_iov = &iov[n];
106 mmsghdr[n].msg_hdr.msg_iovlen = 1;
107 mmsghdr[n].msg_hdr.msg_name = NULL;
108 mmsghdr[n].msg_hdr.msg_namelen = 0;
109 }
110
111 memset(&sa, 0, sizeof(sa));
112 sa.sa_flags = SA_RESTART;
113 sa.sa_handler = &handle_sigchld;
114 sigemptyset(&sa.sa_mask);
115 error = sigaction(SIGCHLD, &sa, 0);
116 ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
117 strerror(errno));
118
119 switch (fork()) {
120 case -1:
121 ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
122 break;
123
124 case 0:
125 n = NPKTS;
126 if (debug)
127 printf("waiting for %u messages (max %u per syscall)\n", n,
128 mmsgcnt);
129 while (n > 0) {
130 struct timespec ts = { 1, 0 };
131 cnt = recvmmsg(fd[1], mmsghdr, min(mmsgcnt, n),
132 MSG_WAITALL, &ts);
133 if (cnt == -1 && errno == ENOBUFS) {
134 overf++;
135 if (debug)
136 printf("receive buffer overflowed"
137 " (%zu)\n",overf);
138 continue;
139 }
140 ATF_REQUIRE_MSG(cnt != -1, "recvmmsg failed (%s)",
141 strerror(errno));
142 ATF_REQUIRE_MSG(cnt != 0, "recvmmsg timeout");
143 if (debug)
144 printf("recvmmsg: got %u messages\n", cnt);
145 for (i = 0; i < cnt; i++) {
146 ATF_CHECK_EQ_MSG(mmsghdr[i].msg_len,
147 sizeof(DGRAM), "packet length");
148 ATF_CHECK_EQ_MSG(
149 ((uint8_t *)iov[i].iov_base)[0],
150 NPKTS - n + i, "packet contents");
151 }
152 n -= cnt;
153 }
154 if (debug)
155 printf("done!\n");
156 exit(0);
157 /*NOTREACHED*/
158 default:
159 sched_yield();
160
161 for (n = 0; n < NPKTS; n++) {
162 if (debug)
163 printf("sending packet %u/%u...\n", (n+1),
164 NPKTS);
165 do {
166 if (rdied)
167 break;
168 DGRAM[0] = n;
169 error = send(fd[0], DGRAM, sizeof(DGRAM), 0);
170 } while (error == -1 && errno == ENOBUFS);
171 ATF_REQUIRE_MSG(error != -1, "send failed (%s)",
172 strerror(errno));
173 }
174 error = wait(&status);
175 ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
176 strerror(errno));
177 ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
178 "receiver died");
179 break;
180 }
181}
182
183ATF_TP_ADD_TCS(tp)
184{
185
186 ATF_TP_ADD_TC(tp, recvmmsg_basic);
187
188 return atf_no_error();
189}
diff --git a/src/regress/lib/libc/sys/t_revoke.c b/src/regress/lib/libc/sys/t_revoke.c
deleted file mode 100644
index d330b8832a..0000000000
--- a/src/regress/lib/libc/sys/t_revoke.c
+++ /dev/null
@@ -1,198 +0,0 @@
1/* $OpenBSD: t_revoke.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_revoke.c,v 1.2 2017/01/13 21:15:57 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/resource.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <fcntl.h>
40#include <errno.h>
41#include <pwd.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47static const char path[] = "revoke";
48
49ATF_TC_WITH_CLEANUP(revoke_basic);
50ATF_TC_HEAD(revoke_basic, tc)
51{
52 atf_tc_set_md_var(tc, "descr", "A basic test of revoke(2)");
53}
54
55ATF_TC_BODY(revoke_basic, tc)
56{
57 struct rlimit res;
58 char tmp[10];
59 size_t i, n;
60 int *buf;
61
62 (void)memset(&res, 0, sizeof(struct rlimit));
63 (void)getrlimit(RLIMIT_NOFILE, &res);
64
65 if ((n = res.rlim_cur / 10) == 0)
66 n = 10;
67
68 buf = calloc(n, sizeof(int));
69 ATF_REQUIRE(buf != NULL);
70
71 buf[0] = open(path, O_RDWR | O_CREAT, 0600);
72 ATF_REQUIRE(buf[0] >= 0);
73
74 for (i = 1; i < n; i++) {
75 buf[i] = open(path, O_RDWR);
76 ATF_REQUIRE(buf[i] >= 0);
77 }
78
79 ATF_REQUIRE(revoke(path) == 0);
80
81 for (i = 0; i < n; i++) {
82
83 ATF_REQUIRE(read(buf[i], tmp, sizeof(tmp)) == -1);
84
85 (void)close(buf[i]);
86 }
87
88 free(buf);
89
90 (void)unlink(path);
91}
92
93ATF_TC_CLEANUP(revoke_basic, tc)
94{
95 (void)unlink(path);
96}
97
98ATF_TC(revoke_err);
99ATF_TC_HEAD(revoke_err, tc)
100{
101 atf_tc_set_md_var(tc, "descr", "Test errors from revoke(2)");
102 atf_tc_set_md_var(tc, "require.user", "unprivileged");
103}
104
105ATF_TC_BODY(revoke_err, tc)
106{
107 char buf[1024 + 1]; /* XXX: From the manual page... */
108
109 (void)memset(buf, 'x', sizeof(buf));
110
111 errno = 0;
112 ATF_REQUIRE_ERRNO(EFAULT, revoke((char *)-1) == -1);
113
114 errno = 0;
115 ATF_REQUIRE_ERRNO(ENAMETOOLONG, revoke(buf) == -1);
116
117 errno = 0;
118#ifdef __OpenBSD__
119 ATF_REQUIRE_ERRNO(ENOTTY, revoke("/etc/passwd") == -1);
120#else
121 ATF_REQUIRE_ERRNO(EPERM, revoke("/etc/passwd") == -1);
122#endif
123
124 errno = 0;
125 ATF_REQUIRE_ERRNO(ENOENT, revoke("/etc/xxx/yyy") == -1);
126}
127
128ATF_TC_WITH_CLEANUP(revoke_perm);
129ATF_TC_HEAD(revoke_perm, tc)
130{
131 atf_tc_set_md_var(tc, "descr", "Test permissions revoke(2)");
132 atf_tc_set_md_var(tc, "require.user", "root");
133}
134
135ATF_TC_BODY(revoke_perm, tc)
136{
137 struct passwd *pw;
138 int fd, sta;
139 pid_t pid;
140
141 pw = getpwnam("nobody");
142 fd = open(path, O_RDWR | O_CREAT, 0600);
143
144 ATF_REQUIRE(fd >= 0);
145 ATF_REQUIRE(pw != NULL);
146 ATF_REQUIRE(revoke(path) == 0);
147
148 pid = fork();
149 ATF_REQUIRE(pid >= 0);
150
151 if (pid == 0) {
152
153 if (setuid(pw->pw_uid) != 0)
154 _exit(EXIT_FAILURE);
155
156 errno = 0;
157
158 if (revoke(path) == 0)
159 _exit(EXIT_FAILURE);
160
161 if (errno != EACCES)
162 _exit(EXIT_FAILURE);
163
164 if (close(fd) != 0)
165 _exit(EXIT_FAILURE);
166
167 _exit(EXIT_SUCCESS);
168 }
169
170 (void)wait(&sta);
171
172 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
173 atf_tc_fail("revoke(2) did not obey permissions");
174
175 (void)close(fd);
176 ATF_REQUIRE(unlink(path) == 0);
177}
178
179ATF_TC_CLEANUP(revoke_perm, tc)
180{
181 (void)unlink(path);
182}
183
184ATF_TP_ADD_TCS(tp)
185{
186
187#ifndef __OpenBSD__
188 /* OpenBSD supports revoke only on ttys */
189 ATF_TP_ADD_TC(tp, revoke_basic);
190#endif
191 ATF_TP_ADD_TC(tp, revoke_err);
192#ifndef __OpenBSD__
193 /* OpenBSD supports revoke only on ttys */
194 ATF_TP_ADD_TC(tp, revoke_perm);
195#endif
196
197 return atf_no_error();
198}
diff --git a/src/regress/lib/libc/sys/t_select.c b/src/regress/lib/libc/sys/t_select.c
deleted file mode 100644
index b10fe87495..0000000000
--- a/src/regress/lib/libc/sys/t_select.c
+++ /dev/null
@@ -1,227 +0,0 @@
1/* $OpenBSD: t_select.c,v 1.2 2020/11/09 23:18:51 bluhm Exp $ */
2/* $NetBSD: t_select.c,v 1.4 2017/01/13 21:18:33 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundatiom
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <assert.h>
36#include <sys/types.h>
37#include <sys/select.h>
38#include <sys/wait.h>
39#include <err.h>
40#include <stdio.h>
41#include <string.h>
42#include <signal.h>
43#include <stdlib.h>
44#include <unistd.h>
45#include <errno.h>
46#include <fcntl.h>
47
48#include "atf-c.h"
49
50static sig_atomic_t keep_going = 1;
51
52static void
53sig_handler(int signum __unused)
54{
55 keep_going = 0;
56}
57
58static void
59sigchld(int signum __unused)
60{
61}
62
63static char
64xtoa(uint8_t n)
65{
66 static const char xarray[] = "0123456789abcdef";
67 assert(n < sizeof(xarray));
68 return xarray[n];
69}
70
71static const char *
72prmask(const sigset_t *m, char *buf, size_t len)
73{
74 size_t j = 2;
75 assert(len >= 3 + sizeof(*m));
76 buf[0] = '0';
77 buf[1] = 'x';
78#define N(p, a) (((p) >> ((a) * 4)) & 0xf)
79#ifdef __OpenBSD__
80 /* On NetBSD sigset_t is a struct */
81 uint32_t p = (*m);
82 for (size_t k = sizeof(p); k > 0; k--)
83 buf[j++] = xtoa(N(p, k - 1));
84#else
85 for (size_t i = __arraycount(m->__bits); i > 0; i--) {
86 uint32_t p = m->__bits[i - 1];
87 for (size_t k = sizeof(p); k > 0; k--)
88 buf[j++] = xtoa(N(p, k - 1));
89 }
90#endif
91 buf[j] = '\0';
92 return buf;
93}
94
95static __dead void
96child(const struct timespec *ts)
97{
98 struct sigaction sa;
99 sigset_t set, oset, nset;
100 char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3];
101 int fd;
102
103 memset(&sa, 0, sizeof(sa));
104 sa.sa_handler = sig_handler;
105 if ((fd = open("/dev/null", O_RDONLY)) == -1)
106 err(1, "open");
107
108 if (sigaction(SIGTERM, &sa, NULL) == -1)
109 err(1, "sigaction");
110
111 sigfillset(&set);
112 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
113 err(1, "sigprocmask");
114
115 if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
116 err(1, "sigprocmask");
117
118 sigemptyset(&set);
119
120 for (;;) {
121 fd_set rset;
122 FD_ZERO(&rset);
123 FD_SET(fd, &rset);
124 if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) {
125 if(errno == EINTR) {
126 if (!keep_going)
127 break;
128 }
129 }
130 if (ts)
131 break;
132 }
133 if (sigprocmask(SIG_BLOCK, NULL, &nset) == -1)
134 err(1, "sigprocmask");
135 if (memcmp(&oset, &nset, sizeof(oset)) != 0)
136 atf_tc_fail("pselect() masks don't match "
137 "after timeout %s != %s",
138 prmask(&nset, nbuf, sizeof(nbuf)),
139 prmask(&oset, obuf, sizeof(obuf)));
140 _exit(0);
141}
142
143ATF_TC(pselect_sigmask);
144ATF_TC_HEAD(pselect_sigmask, tc)
145{
146 atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
147 "setting when a signal is received (PR lib/43625)");
148}
149
150ATF_TC_BODY(pselect_sigmask, tc)
151{
152 pid_t pid;
153 int status;
154
155 signal(SIGCHLD, sigchld);
156
157 switch (pid = fork()) {
158 case 0:
159 child(NULL);
160 /*NOTREACHED*/
161 case -1:
162 err(1, "fork");
163 default:
164 sleep(1);
165 if (kill(pid, SIGTERM) == -1)
166 err(1, "kill");
167 sleep(1);
168 switch (waitpid(pid, &status, WNOHANG)) {
169 case -1:
170 err(1, "wait");
171 case 0:
172 if (kill(pid, SIGKILL) == -1)
173 err(1, "kill");
174 atf_tc_fail("pselect() did not receive signal");
175 break;
176 default:
177 break;
178 }
179 }
180}
181
182ATF_TC(pselect_timeout);
183ATF_TC_HEAD(pselect_timeout, tc)
184{
185
186 atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
187 "setting when a timeout occurs");
188}
189
190ATF_TC_BODY(pselect_timeout, tc)
191{
192 pid_t pid;
193 int status;
194 static const struct timespec zero = { 0, 0 };
195
196 signal(SIGCHLD, sigchld);
197
198 switch (pid = fork()) {
199 case 0:
200 child(&zero);
201 break;
202 case -1:
203 err(1, "fork");
204 default:
205 sleep(1);
206 switch (waitpid(pid, &status, WNOHANG)) {
207 case -1:
208 err(1, "wait");
209 case 0:
210 if (kill(pid, SIGKILL) == -1)
211 err(1, "kill");
212 atf_tc_fail("pselect() did not receive signal");
213 break;
214 default:
215 break;
216 }
217 }
218}
219
220ATF_TP_ADD_TCS(tp)
221{
222
223 ATF_TP_ADD_TC(tp, pselect_sigmask);
224 ATF_TP_ADD_TC(tp, pselect_timeout);
225
226 return atf_no_error();
227}
diff --git a/src/regress/lib/libc/sys/t_sendmmsg.c b/src/regress/lib/libc/sys/t_sendmmsg.c
deleted file mode 100644
index 46a2e9bf5c..0000000000
--- a/src/regress/lib/libc/sys/t_sendmmsg.c
+++ /dev/null
@@ -1,216 +0,0 @@
1/* $OpenBSD: t_sendmmsg.c,v 1.3 2023/10/27 07:33:06 anton Exp $ */
2/* $NetBSD: t_sendmmsg.c,v 1.3 2019/03/16 21:46:43 christos Exp $ */
3
4/*-
5 * Copyright (c) 2018 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32#include <sys/cdefs.h>
33
34#include "atf-c.h"
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <sys/ioctl.h>
38#include <sys/wait.h>
39
40#include <string.h>
41#include <time.h>
42#include <stdint.h>
43#include <errno.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <unistd.h>
48#include <sched.h>
49
50#define BUFSIZE 65536
51
52#define min(a, b) ((a) < (b) ? (a) : (b))
53static int debug;
54static volatile sig_atomic_t rdied;
55
56static void
57handle_sigchld(__unused int pid)
58{
59
60 rdied = 1;
61}
62
63ATF_TC(sendmmsg_basic);
64ATF_TC_HEAD(sendmmsg_basic, tc)
65{
66 atf_tc_set_md_var(tc, "descr", "A basic test of sendmmsg(2)");
67}
68
69static void
70setsock(int fd, int type)
71{
72 int buflen = BUFSIZE;
73 socklen_t socklen = sizeof(buflen);
74
75 ATF_REQUIRE_MSG(setsockopt(fd, SOL_SOCKET, type,
76 &buflen, socklen) != -1, "%s (%s)",
77 type == SO_RCVBUF ? "rcv" : "snd", strerror(errno));
78}
79
80ATF_TC_BODY(sendmmsg_basic, tc)
81{
82 int fd[2], error, cnt;
83 uint8_t *buf;
84 struct mmsghdr *mmsghdr;
85 struct iovec *iov;
86 unsigned int mmsgcnt, n;
87 int status;
88 off_t off;
89 uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
90 uint8_t rgram[sizeof(DGRAM)];
91 struct sigaction sa;
92 ssize_t overf = 0;
93
94 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
95 ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
96
97 buf = malloc(BUFSIZE);
98 ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
99
100 setsock(fd[1], SO_SNDBUF);
101// setsock(fd[0], SO_RCVBUF);
102
103 mmsgcnt = BUFSIZE / sizeof(DGRAM);
104 mmsghdr = calloc(mmsgcnt, sizeof(*mmsghdr));
105 ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
106 iov = malloc(sizeof(*iov) * mmsgcnt);
107 ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
108
109 for (off = 0, n = 0; n < mmsgcnt; n++) {
110 iov[n].iov_base = buf + off;
111 memcpy(iov[n].iov_base, DGRAM, sizeof(DGRAM));
112 *(buf + off) = n;
113 iov[n].iov_len = sizeof(DGRAM);
114 off += iov[n].iov_len;
115 mmsghdr[n].msg_hdr.msg_iov = &iov[n];
116 mmsghdr[n].msg_hdr.msg_iovlen = 1;
117 mmsghdr[n].msg_hdr.msg_name = NULL;
118 mmsghdr[n].msg_hdr.msg_namelen = 0;
119 }
120
121 memset(&sa, 0, sizeof(sa));
122 sa.sa_flags = SA_RESTART;
123 sa.sa_handler = &handle_sigchld;
124 sigemptyset(&sa.sa_mask);
125 error = sigaction(SIGCHLD, &sa, 0);
126 ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
127 strerror(errno));
128
129 switch (fork()) {
130 case -1:
131 ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
132 break;
133 case 0:
134 sched_yield();
135 if (debug)
136 printf("sending %u messages (max %u per syscall)\n", n,
137 mmsgcnt);
138 for (n = 0; n < mmsgcnt;) {
139 if (debug)
140 printf("sending packet %u/%u...\n", n,
141 mmsgcnt);
142#ifdef __OpenBSD__
143 int npkt = min(1024, mmsgcnt - n);
144#else
145 // XXX: ENOBUFS bug, on the receive side!!!
146 // in npkt = min(mmsgsize, mmsgcnt - n);
147 int npkt = min(3, mmsgcnt - n), a;
148 do {
149 a = 0;
150 ATF_REQUIRE(ioctl(fd[1], FIONSPACE, &a) != -1);
151 printf("1 %d\n", a);
152 ATF_REQUIRE(ioctl(fd[0], FIONSPACE, &a) != -1);
153 printf("0 %d\n", a);
154 } while ((size_t)a < sizeof(DGRAM));
155#endif
156 cnt = sendmmsg(fd[1], mmsghdr + n, npkt, 0);
157 if (cnt == -1 && errno == ENOBUFS) {
158 overf++;
159 if (debug)
160 printf("send buffer overflowed"
161 " (%zu)\n",overf);
162 if (overf > 100)
163 exit(1);
164 sched_yield();
165 sched_yield();
166 sched_yield();
167 continue;
168 }
169 ATF_REQUIRE_MSG(cnt != -1, "sendmmsg %u failed (%s)",
170 n, strerror(errno));
171 if (debug)
172 printf("sendmmsg: sent %u messages\n", cnt);
173 n += cnt;
174 sched_yield();
175 sched_yield();
176 sched_yield();
177 }
178 if (debug)
179 printf("done!\n");
180 exit(0);
181 /*NOTREACHED*/
182 default:
183 for (n = 0; n < mmsgcnt; n++) {
184 if (debug)
185 printf("receiving packet %u/%u...\n", n,
186 mmsgcnt);
187 do {
188 if (rdied)
189 break;
190 cnt = recv(fd[0], rgram, sizeof(rgram), 0);
191 ATF_REQUIRE_MSG(cnt != -1 || errno != ENOBUFS,
192 "recv failed (%s)", strerror(errno));
193 ATF_CHECK_EQ_MSG(cnt, sizeof(rgram),
194 "packet length");
195 ATF_CHECK_EQ_MSG(rgram[0], n,
196 "number %u != %u", rgram[0], n);
197 ATF_REQUIRE_MSG(memcmp(rgram + 1, DGRAM + 1,
198 sizeof(rgram) - 1) == 0, "bad data");
199 } while (cnt == -1 && errno == ENOBUFS);
200 }
201 error = wait(&status);
202 ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
203 strerror(errno));
204 ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
205 "receiver died, status %d", status);
206 break;
207 }
208}
209
210ATF_TP_ADD_TCS(tp)
211{
212
213 ATF_TP_ADD_TC(tp, sendmmsg_basic);
214
215 return atf_no_error();
216}
diff --git a/src/regress/lib/libc/sys/t_sendrecv.c b/src/regress/lib/libc/sys/t_sendrecv.c
deleted file mode 100644
index 178d9338b8..0000000000
--- a/src/regress/lib/libc/sys/t_sendrecv.c
+++ /dev/null
@@ -1,189 +0,0 @@
1/* $OpenBSD: t_sendrecv.c,v 1.4 2024/07/30 13:28:27 claudio Exp $ */
2/* $NetBSD: t_sendrecv.c,v 1.8 2021/03/28 17:30:01 christos Exp $ */
3
4/*-
5 * Copyright (c) 2018 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include "atf-c.h"
36#include <sys/types.h>
37#include <sys/socket.h>
38
39#include <string.h>
40#include <stdint.h>
41#include <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <sched.h>
45#include <unistd.h>
46#include <signal.h>
47
48
49#define COUNT 100
50
51union packet {
52 uint8_t buf[1316];
53 uintmax_t seq;
54};
55
56static volatile sig_atomic_t rdied;
57
58static void
59handle_sigchld(__unused int pid)
60{
61
62 rdied = 1;
63}
64
65static void
66sender(int sd)
67{
68 union packet p;
69 ssize_t n;
70 p.seq = 0;
71 for (size_t i = 0; i < COUNT; i++) {
72 for (; (n = send(sd, &p, sizeof(p), 0)) == sizeof(p);
73 p.seq++)
74 continue;
75// printf(">>%zd %d %ju\n", n, errno, p.seq);
76 ATF_REQUIRE_MSG(errno == ENOBUFS, "send %s", strerror(errno));
77 }
78 close(sd);
79// printf("sender done\n");
80}
81
82static void
83receiver(int sd)
84{
85 union packet p;
86 ssize_t n;
87 uintmax_t seq = 0;
88
89 for (size_t i = 0; i < COUNT; i++) {
90 if (rdied)
91 return;
92 while ((n = recv(sd, &p, sizeof(p), 0), sizeof(p))
93 == sizeof(p))
94 {
95 if (rdied)
96 return;
97 if (p.seq != seq)
98 printf("%ju != %ju\n", p.seq, seq);
99 if (seq % 10 == 0)
100 usleep(100);
101 seq = p.seq + 1;
102 }
103// printf("<<%zd %d %ju\n", n, errno, seq);
104 if (n == 0)
105 return;
106 ATF_REQUIRE_EQ(n, -1);
107 ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno));
108 }
109 close(sd);
110}
111
112static void
113sendrecv(int rerror)
114{
115 int fd[2], sd[2], error;
116 char c = 0;
117 struct sigaction sa;
118
119 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd);
120 ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
121 error = pipe(fd);
122 ATF_REQUIRE_MSG(error != -1, "pipe failed (%s)", strerror(errno));
123
124 for (size_t i = 0; i < __arraycount(sd); i++) {
125 error = setsockopt(sd[i], SOL_SOCKET, SO_RERROR, &rerror,
126 sizeof(rerror));
127 ATF_REQUIRE_MSG(error != -1,
128 "setsockopt(SO_RERROR) failed (%s)", strerror(errno));
129 }
130
131 memset(&sa, 0, sizeof(sa));
132 sa.sa_flags = 0;
133 sa.sa_handler = &handle_sigchld;
134 sigemptyset(&sa.sa_mask);
135 error = sigaction(SIGCHLD, &sa, 0);
136 ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
137 strerror(errno));
138
139 switch (fork()) {
140 case -1:
141 ATF_REQUIRE_MSG(errno == 0,
142 "fork failed (%s)", strerror(errno));
143 __unreachable();
144 /*NOTREACHED*/
145 case 0:
146 read(fd[1], &c, sizeof(c));
147 sender(sd[0]);
148 close(sd[0]);
149 exit(EXIT_SUCCESS);
150 /*NOTREACHED*/
151 default:
152 write(fd[0], &c, sizeof(c));
153 receiver(sd[1]);
154 return;
155 }
156}
157
158ATF_TC(sendrecv_basic);
159
160ATF_TC_HEAD(sendrecv_basic, tc)
161{
162 atf_tc_set_md_var(tc, "descr", "A basic test of send/recv(2)");
163}
164
165ATF_TC_BODY(sendrecv_basic, tc)
166{
167 sendrecv(0);
168}
169
170ATF_TC(sendrecv_rerror);
171
172ATF_TC_HEAD(sendrecv_rerror, tc)
173{
174 atf_tc_set_md_var(tc, "descr", "Test send/recv(2) with receiver error");
175}
176
177ATF_TC_BODY(sendrecv_rerror, tc)
178{
179 sendrecv(1);
180}
181
182ATF_TP_ADD_TCS(tp)
183{
184
185 ATF_TP_ADD_TC(tp, sendrecv_basic);
186 ATF_TP_ADD_TC(tp, sendrecv_rerror);
187
188 return atf_no_error();
189}
diff --git a/src/regress/lib/libc/sys/t_setrlimit.c b/src/regress/lib/libc/sys/t_setrlimit.c
deleted file mode 100644
index c9c64d42f0..0000000000
--- a/src/regress/lib/libc/sys/t_setrlimit.c
+++ /dev/null
@@ -1,556 +0,0 @@
1/* $OpenBSD: t_setrlimit.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_setrlimit.c,v 1.7 2020/10/13 06:58:57 rin Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32#include "macros.h"
33
34#include <sys/resource.h>
35#include <sys/mman.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <limits.h>
42#ifndef __OpenBSD__
43#include <lwp.h>
44#endif
45#include <signal.h>
46#include <stdint.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#ifndef __OpenBSD__
51#include <ucontext.h>
52#endif
53#include <unistd.h>
54
55static void sighandler(int);
56static const char path[] = "setrlimit";
57
58static const int rlimit[] = {
59#ifndef __OpenBSD__
60 RLIMIT_AS,
61#endif
62 RLIMIT_CORE,
63 RLIMIT_CPU,
64 RLIMIT_DATA,
65 RLIMIT_FSIZE,
66 RLIMIT_MEMLOCK,
67 RLIMIT_NOFILE,
68 RLIMIT_NPROC,
69 RLIMIT_RSS,
70#ifndef __OpenBSD__
71 RLIMIT_SBSIZE,
72#endif
73 RLIMIT_STACK
74};
75
76ATF_TC(setrlimit_basic);
77ATF_TC_HEAD(setrlimit_basic, tc)
78{
79 atf_tc_set_md_var(tc, "descr", "A basic soft limit test");
80}
81
82ATF_TC_BODY(setrlimit_basic, tc)
83{
84 struct rlimit res;
85 int *buf, lim;
86 size_t i;
87
88 buf = calloc(__arraycount(rlimit), sizeof(int));
89
90 if (buf == NULL)
91 atf_tc_fail("initialization failed");
92
93 for (i = lim = 0; i < __arraycount(rlimit); i++) {
94
95 (void)memset(&res, 0, sizeof(struct rlimit));
96
97 if (getrlimit(rlimit[i], &res) != 0)
98 continue;
99
100 if (res.rlim_cur == RLIM_INFINITY || res.rlim_cur == 0)
101 continue;
102
103 if (res.rlim_cur == res.rlim_max) /* An unprivileged run. */
104 continue;
105
106 buf[i] = res.rlim_cur;
107 res.rlim_cur = res.rlim_cur - 1;
108
109 if (setrlimit(rlimit[i], &res) != 0) {
110 lim = rlimit[i];
111 goto out;
112 }
113 }
114
115out:
116 for (i = 0; i < __arraycount(rlimit); i++) {
117
118 (void)memset(&res, 0, sizeof(struct rlimit));
119
120 if (buf[i] == 0)
121 continue;
122
123 if (getrlimit(rlimit[i], &res) != 0)
124 continue;
125
126 res.rlim_cur = buf[i];
127
128 (void)setrlimit(rlimit[i], &res);
129 }
130
131 if (lim != 0)
132 atf_tc_fail("failed to set limit (%d)", lim);
133 free(buf);
134}
135
136ATF_TC(setrlimit_current);
137ATF_TC_HEAD(setrlimit_current, tc)
138{
139 atf_tc_set_md_var(tc, "descr", "setrlimit(3) with current limits");
140}
141
142ATF_TC_BODY(setrlimit_current, tc)
143{
144 struct rlimit res;
145 size_t i;
146
147 for (i = 0; i < __arraycount(rlimit); i++) {
148
149 (void)memset(&res, 0, sizeof(struct rlimit));
150
151 ATF_REQUIRE(getrlimit(rlimit[i], &res) == 0);
152 ATF_REQUIRE(setrlimit(rlimit[i], &res) == 0);
153 }
154}
155
156ATF_TC(setrlimit_err);
157ATF_TC_HEAD(setrlimit_err, tc)
158{
159 atf_tc_set_md_var(tc, "descr", "Test error conditions");
160}
161
162ATF_TC_BODY(setrlimit_err, tc)
163{
164 struct rlimit res;
165 size_t i;
166
167 for (i = 0; i < __arraycount(rlimit); i++) {
168
169 errno = 0;
170
171 ATF_REQUIRE(getrlimit(rlimit[i], (void *)0) != 0);
172 ATF_REQUIRE(errno == EFAULT);
173 }
174
175 errno = 0;
176
177 ATF_REQUIRE(getrlimit(INT_MAX, &res) != 0);
178 ATF_REQUIRE(errno == EINVAL);
179}
180
181ATF_TC_WITH_CLEANUP(setrlimit_fsize);
182ATF_TC_HEAD(setrlimit_fsize, tc)
183{
184 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_FSIZE");
185}
186
187ATF_TC_BODY(setrlimit_fsize, tc)
188{
189 struct rlimit res;
190 int fd, sta;
191 pid_t pid;
192
193 fd = open(path, O_RDWR | O_CREAT, 0700);
194
195 if (fd < 0)
196 atf_tc_fail("initialization failed");
197
198 pid = fork();
199 ATF_REQUIRE(pid >= 0);
200
201 if (pid == 0) {
202
203 res.rlim_cur = 2;
204 res.rlim_max = 2;
205
206 if (setrlimit(RLIMIT_FSIZE, &res) != 0)
207 _exit(EXIT_FAILURE);
208
209 if (signal(SIGXFSZ, sighandler) == SIG_ERR)
210 _exit(EXIT_FAILURE);
211
212 /*
213 * The third call should generate a SIGXFSZ.
214 */
215 (void)write(fd, "X", 1);
216 (void)write(fd, "X", 1);
217 (void)write(fd, "X", 1);
218
219 _exit(EXIT_FAILURE);
220 }
221
222 (void)close(fd);
223 (void)wait(&sta);
224 (void)unlink(path);
225
226 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
227 atf_tc_fail("RLIMIT_FSIZE not enforced");
228}
229
230ATF_TC_CLEANUP(setrlimit_fsize, tc)
231{
232 (void)unlink(path);
233}
234
235static void
236sighandler(int signo)
237{
238
239 if (signo != SIGXFSZ)
240 _exit(EXIT_FAILURE);
241
242 _exit(EXIT_SUCCESS);
243}
244
245ATF_TC(setrlimit_memlock);
246ATF_TC_HEAD(setrlimit_memlock, tc)
247{
248 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_MEMLOCK");
249}
250
251ATF_TC_BODY(setrlimit_memlock, tc)
252{
253 struct rlimit res;
254 void *buf;
255 long page;
256 pid_t pid;
257 int sta;
258
259 page = sysconf(_SC_PAGESIZE);
260 ATF_REQUIRE(page >= 0);
261
262 buf = malloc(page);
263 pid = fork();
264
265 if (buf == NULL || pid < 0)
266 atf_tc_fail("initialization failed");
267
268 if (pid == 0) {
269
270 /*
271 * Try to lock a page while
272 * RLIMIT_MEMLOCK is zero.
273 */
274 if (mlock(buf, page) != 0)
275 _exit(EXIT_FAILURE);
276
277 if (munlock(buf, page) != 0)
278 _exit(EXIT_FAILURE);
279
280 res.rlim_cur = 0;
281 res.rlim_max = 0;
282
283 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
284 _exit(EXIT_FAILURE);
285
286 if (mlock(buf, page) != 0)
287 _exit(EXIT_SUCCESS);
288
289 (void)munlock(buf, page);
290
291 _exit(EXIT_FAILURE);
292 }
293
294 free(buf);
295
296 (void)wait(&sta);
297
298 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
299 atf_tc_fail("RLIMIT_MEMLOCK not enforced");
300}
301
302ATF_TC(setrlimit_nofile_1);
303ATF_TC_HEAD(setrlimit_nofile_1, tc)
304{
305 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_NOFILE, #1");
306}
307
308ATF_TC_BODY(setrlimit_nofile_1, tc)
309{
310 struct rlimit res;
311 int fd, i, rv, sta;
312 pid_t pid;
313
314 res.rlim_cur = 0;
315 res.rlim_max = 0;
316
317 pid = fork();
318 ATF_REQUIRE(pid >= 0);
319
320 if (pid == 0) {
321
322 /*
323 * Close all descriptors, set RLIMIT_NOFILE
324 * to zero, and try to open a random file.
325 * This should fail with EMFILE.
326 */
327 for (i = 0; i < 1024; i++)
328 (void)close(i);
329
330 rv = setrlimit(RLIMIT_NOFILE, &res);
331
332 if (rv != 0)
333 _exit(EXIT_FAILURE);
334
335 errno = 0;
336 fd = open("/etc/passwd", O_RDONLY);
337
338 if (fd >= 0 || errno != EMFILE)
339 _exit(EXIT_FAILURE);
340
341 _exit(EXIT_SUCCESS);
342 }
343
344 (void)wait(&sta);
345
346 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
347 atf_tc_fail("RLIMIT_NOFILE not enforced");
348}
349
350ATF_TC(setrlimit_nofile_2);
351ATF_TC_HEAD(setrlimit_nofile_2, tc)
352{
353 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_NOFILE, #2");
354}
355
356ATF_TC_BODY(setrlimit_nofile_2, tc)
357{
358 static const rlim_t lim = 12;
359 struct rlimit res;
360 int fd, i, rv, sta;
361 pid_t pid;
362
363 /*
364 * See that an arbitrary limit on
365 * open files is being enforced.
366 */
367 res.rlim_cur = lim;
368 res.rlim_max = lim;
369
370 pid = fork();
371 ATF_REQUIRE(pid >= 0);
372
373 if (pid == 0) {
374
375 for (i = 0; i < 1024; i++)
376 (void)close(i);
377
378 rv = setrlimit(RLIMIT_NOFILE, &res);
379
380 if (rv != 0)
381 _exit(EXIT_FAILURE);
382
383 for (i = 0; i < (int)lim; i++) {
384
385 fd = open("/etc/passwd", O_RDONLY);
386
387 if (fd < 0)
388 _exit(EXIT_FAILURE);
389 }
390
391 /*
392 * After the limit has been reached,
393 * EMFILE should again follow.
394 */
395 fd = open("/etc/passwd", O_RDONLY);
396
397 if (fd >= 0 || errno != EMFILE)
398 _exit(EXIT_FAILURE);
399
400 _exit(EXIT_SUCCESS);
401 }
402
403 (void)wait(&sta);
404
405 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
406 atf_tc_fail("RLIMIT_NOFILE not enforced");
407}
408
409ATF_TC(setrlimit_nproc);
410ATF_TC_HEAD(setrlimit_nproc, tc)
411{
412 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_NPROC");
413 atf_tc_set_md_var(tc, "require.user", "unprivileged");
414}
415
416ATF_TC_BODY(setrlimit_nproc, tc)
417{
418 struct rlimit res;
419 pid_t pid, cpid;
420 int sta;
421
422 pid = fork();
423 ATF_REQUIRE(pid >= 0);
424
425 if (pid == 0) {
426
427 /*
428 * Set RLIMIT_NPROC to zero and try to fork.
429 */
430 res.rlim_cur = 0;
431 res.rlim_max = 0;
432
433 if (setrlimit(RLIMIT_NPROC, &res) != 0)
434 _exit(EXIT_FAILURE);
435
436 cpid = fork();
437
438 if (cpid < 0)
439 _exit(EXIT_SUCCESS);
440
441 _exit(EXIT_FAILURE);
442 }
443
444 (void)waitpid(pid, &sta, 0);
445
446 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
447 atf_tc_fail("RLIMIT_NPROC not enforced");
448}
449
450#ifndef __OpenBSD__
451
452ATF_TC(setrlimit_nthr);
453ATF_TC_HEAD(setrlimit_nthr, tc)
454{
455 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_NTHR");
456 atf_tc_set_md_var(tc, "require.user", "unprivileged");
457}
458
459static void
460func(lwpid_t *id)
461{
462 printf("thread %d\n", *id);
463 fflush(stdout);
464 _lwp_exit();
465}
466
467ATF_TC_BODY(setrlimit_nthr, tc)
468{
469 struct rlimit res;
470 lwpid_t lwpid;
471 ucontext_t c;
472
473 /*
474 * Set RLIMIT_NTHR to zero and try to create a thread.
475 */
476 res.rlim_cur = 0;
477 res.rlim_max = 0;
478 ATF_REQUIRE(setrlimit(RLIMIT_NTHR, &res) == 0);
479 ATF_REQUIRE(getcontext(&c) == 0);
480 c.uc_link = NULL;
481 sigemptyset(&c.uc_sigmask);
482 c.uc_stack.ss_flags = 0;
483 c.uc_stack.ss_size = 4096;
484 ATF_REQUIRE((c.uc_stack.ss_sp = malloc(c.uc_stack.ss_size)) != NULL);
485 makecontext(&c, func, 1, &lwpid);
486 ATF_CHECK_ERRNO(EAGAIN, _lwp_create(&c, 0, &lwpid) == -1);
487}
488#endif
489
490ATF_TC(setrlimit_perm);
491ATF_TC_HEAD(setrlimit_perm, tc)
492{
493 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2) for EPERM");
494 atf_tc_set_md_var(tc, "require.user", "unprivileged");
495}
496
497ATF_TC_BODY(setrlimit_perm, tc)
498{
499 struct rlimit res;
500 size_t i;
501
502 /*
503 * Try to raise the maximum limits as an user.
504 */
505 for (i = 0; i < __arraycount(rlimit); i++) {
506
507 ATF_REQUIRE(getrlimit(rlimit[i], &res) == 0);
508
509 if (res.rlim_max == UINT64_MAX) /* Overflow. */
510 continue;
511
512 errno = 0;
513 res.rlim_max = res.rlim_max + 1;
514
515 ATF_CHECK_ERRNO(EPERM, setrlimit(rlimit[i], &res) != 0);
516 }
517}
518
519ATF_TC(setrlimit_stack);
520ATF_TC_HEAD(setrlimit_stack, tc)
521{
522 atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_STACK");
523 atf_tc_set_md_var(tc, "require.user", "unprivileged");
524}
525
526ATF_TC_BODY(setrlimit_stack, tc)
527{
528 struct rlimit res;
529
530 /* Ensure soft limit is not bigger than hard limit */
531 res.rlim_cur = res.rlim_max = 6 * 1024 * 1024;
532 ATF_REQUIRE(setrlimit(RLIMIT_STACK, &res) == 0);
533 ATF_REQUIRE(getrlimit(RLIMIT_STACK, &res) == 0);
534 ATF_CHECK(res.rlim_cur <= res.rlim_max);
535
536}
537
538ATF_TP_ADD_TCS(tp)
539{
540
541 ATF_TP_ADD_TC(tp, setrlimit_basic);
542 ATF_TP_ADD_TC(tp, setrlimit_current);
543 ATF_TP_ADD_TC(tp, setrlimit_err);
544 ATF_TP_ADD_TC(tp, setrlimit_fsize);
545 ATF_TP_ADD_TC(tp, setrlimit_memlock);
546 ATF_TP_ADD_TC(tp, setrlimit_nofile_1);
547 ATF_TP_ADD_TC(tp, setrlimit_nofile_2);
548 ATF_TP_ADD_TC(tp, setrlimit_nproc);
549 ATF_TP_ADD_TC(tp, setrlimit_perm);
550#ifndef __OpenBSD__
551 ATF_TP_ADD_TC(tp, setrlimit_nthr);
552#endif
553 ATF_TP_ADD_TC(tp, setrlimit_stack);
554
555 return atf_no_error();
556}
diff --git a/src/regress/lib/libc/sys/t_setuid.c b/src/regress/lib/libc/sys/t_setuid.c
deleted file mode 100644
index 6924041390..0000000000
--- a/src/regress/lib/libc/sys/t_setuid.c
+++ /dev/null
@@ -1,123 +0,0 @@
1/* $OpenBSD: t_setuid.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_setuid.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <pwd.h>
40#include <stdlib.h>
41#include <unistd.h>
42
43ATF_TC(setuid_perm);
44ATF_TC_HEAD(setuid_perm, tc)
45{
46 atf_tc_set_md_var(tc, "descr", "Test setuid(0) as normal user");
47 atf_tc_set_md_var(tc, "require.user", "unprivileged");
48}
49
50ATF_TC_BODY(setuid_perm, tc)
51{
52 errno = 0;
53
54 ATF_REQUIRE(setuid(0) == -1);
55 ATF_REQUIRE(errno == EPERM);
56}
57
58ATF_TC(setuid_real);
59ATF_TC_HEAD(setuid_real, tc)
60{
61 atf_tc_set_md_var(tc, "descr", "Test setuid(2) with real UID");
62}
63
64ATF_TC_BODY(setuid_real, tc)
65{
66 uid_t uid = getuid();
67
68 ATF_REQUIRE(setuid(uid) == 0);
69
70 ATF_REQUIRE(getuid() == uid);
71 ATF_REQUIRE(geteuid() == uid);
72}
73
74ATF_TC(setuid_root);
75ATF_TC_HEAD(setuid_root, tc)
76{
77 atf_tc_set_md_var(tc, "descr", "A basic test of setuid(2)");
78 atf_tc_set_md_var(tc, "require.user", "root");
79}
80
81ATF_TC_BODY(setuid_root, tc)
82{
83 struct passwd *pw;
84 int rv, sta;
85 pid_t pid;
86 uid_t uid;
87
88 while ((pw = getpwent()) != NULL) {
89
90 pid = fork();
91 ATF_REQUIRE(pid >= 0);
92
93 if (pid == 0) {
94
95 rv = setuid(pw->pw_uid);
96
97 if (rv != 0)
98 _exit(EXIT_FAILURE);
99
100 uid = getuid();
101
102 if (uid != pw->pw_uid)
103 _exit(EXIT_FAILURE);
104
105 _exit(EXIT_SUCCESS);
106 }
107
108 (void)wait(&sta);
109
110 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
111 atf_tc_fail("failed to change UID to %u", pw->pw_uid);
112 }
113}
114
115ATF_TP_ADD_TCS(tp)
116{
117
118 ATF_TP_ADD_TC(tp, setuid_perm);
119 ATF_TP_ADD_TC(tp, setuid_real);
120 ATF_TP_ADD_TC(tp, setuid_root);
121
122 return atf_no_error();
123}
diff --git a/src/regress/lib/libc/sys/t_sigaction.c b/src/regress/lib/libc/sys/t_sigaction.c
deleted file mode 100644
index 4254e1fece..0000000000
--- a/src/regress/lib/libc/sys/t_sigaction.c
+++ /dev/null
@@ -1,151 +0,0 @@
1/* $OpenBSD: t_sigaction.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_sigaction.c,v 1.5 2017/01/13 21:30:41 christos Exp $ */
3
4/*-
5 * Copyright (c) 2010 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/wait.h>
33
34#include <signal.h>
35#include <stdbool.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39
40#include "atf-c.h"
41
42#include "h_macros.h"
43
44static bool handler_called = false;
45
46static void
47handler(int signo __unused)
48{
49 handler_called = true;
50}
51
52static void
53sa_resethand_child(const int flags)
54{
55 struct sigaction sa;
56
57 sa.sa_flags = flags;
58 sa.sa_handler = &handler;
59 sigemptyset(&sa.sa_mask);
60
61 sigaction(SIGUSR1, &sa, NULL);
62 kill(getpid(), SIGUSR1);
63 exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
64}
65
66static void
67wait_and_check_child(const pid_t pid, const char *fail_message)
68{
69 int status;
70
71 (void)waitpid(pid, &status, 0);
72
73 if (WIFEXITED(status))
74 ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
75 else
76 atf_tc_fail("%s; raw exit status was %d", fail_message, status);
77}
78
79static void
80catch(int sig __unused)
81{
82 return;
83}
84
85ATF_TC(sigaction_basic);
86ATF_TC_HEAD(sigaction_basic, tc)
87{
88
89 atf_tc_set_md_var(tc, "descr", "Checks for correct I&D cache"
90 "synchronization after copying out the trampoline code.");
91}
92
93ATF_TC_BODY(sigaction_basic, tc)
94{
95 static struct sigaction sa;
96
97 sa.sa_handler = catch;
98
99 sigaction(SIGUSR1, &sa, 0);
100 kill(getpid(), SIGUSR1);
101 atf_tc_pass();
102}
103
104ATF_TC(sigaction_noflags);
105ATF_TC_HEAD(sigaction_noflags, tc)
106{
107 atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
108 "sigaction(2) but without any flags");
109}
110
111ATF_TC_BODY(sigaction_noflags, tc)
112{
113 const pid_t pid = fork();
114 if (pid == -1)
115 atf_tc_fail_errno("fork(2) failed");
116 else if (pid == 0)
117 sa_resethand_child(0);
118 else
119 wait_and_check_child(pid, "Child process did not exit cleanly;"
120 " it failed to process the signal");
121}
122
123ATF_TC(sigaction_resethand);
124ATF_TC_HEAD(sigaction_resethand, tc)
125{
126 atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
127}
128
129ATF_TC_BODY(sigaction_resethand, tc)
130{
131 const pid_t pid = fork();
132 if (pid == -1)
133 atf_tc_fail_errno("fork(2) failed");
134 else if (pid == 0)
135 sa_resethand_child(SA_RESETHAND);
136 else {
137 wait_and_check_child(pid, "Child process did not exit cleanly;"
138 " it either failed to process the signal or SA_RESETHAND"
139 " is broken");
140 }
141}
142
143ATF_TP_ADD_TCS(tp)
144{
145
146 ATF_TP_ADD_TC(tp, sigaction_basic);
147 ATF_TP_ADD_TC(tp, sigaction_noflags);
148 ATF_TP_ADD_TC(tp, sigaction_resethand);
149
150 return atf_no_error();
151}
diff --git a/src/regress/lib/libc/sys/t_sigaltstack.c b/src/regress/lib/libc/sys/t_sigaltstack.c
deleted file mode 100644
index b02063bf2d..0000000000
--- a/src/regress/lib/libc/sys/t_sigaltstack.c
+++ /dev/null
@@ -1,99 +0,0 @@
1/* $OpenBSD: t_sigaltstack.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $ */
3
4/*-
5 * Copyright (c) 2020 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "macros.h"
30
31#include <signal.h>
32#include <stdbool.h>
33
34#include "atf-c.h"
35
36#include "h_macros.h"
37
38static stack_t sigstk;
39static bool handler_called;
40static bool handler_use_altstack;
41
42static void
43handler(int signo __unused)
44{
45 char sp[128];
46
47 handler_called = true;
48
49 /* checking if the stack pointer is within the range of altstack */
50 if ((char *)sigstk.ss_sp <= sp &&
51 ((char *)sigstk.ss_sp + sigstk.ss_size) > sp)
52 handler_use_altstack = true;
53 else
54 handler_use_altstack = false;
55}
56
57ATF_TC(sigaltstack_onstack);
58ATF_TC_HEAD(sigaltstack_onstack, tc)
59{
60 atf_tc_set_md_var(tc, "descr",
61 "Checks for using signal stack with SA_ONSTACK");
62}
63
64ATF_TC_BODY(sigaltstack_onstack, tc)
65{
66 struct sigaction sa;
67 int i;
68
69 /* set a signal handler use alternative stack */
70 memset(&sigstk, 0, sizeof(sigstk));
71 sigstk.ss_sp = malloc(SIGSTKSZ);
72 ATF_REQUIRE(sigstk.ss_sp != NULL);
73 sigstk.ss_size = SIGSTKSZ;
74 sigstk.ss_flags = 0;
75 ATF_REQUIRE(sigaltstack(&sigstk, 0) == 0);
76
77 sigemptyset(&sa.sa_mask);
78 sa.sa_handler = handler;
79 sa.sa_flags = SA_ONSTACK;
80 sigaction(SIGUSR1, &sa, NULL);
81
82 /* test several times */
83 for (i = 1; i <= 5; i++) {
84 handler_called = false;
85 kill(getpid(), SIGUSR1);
86
87 if (!handler_called)
88 atf_tc_fail("signal handler wasn't called (count=%d)", i);
89 if (!handler_use_altstack)
90 atf_tc_fail("alternative stack wasn't used (count=%d)", i);
91 }
92}
93
94ATF_TP_ADD_TCS(tp)
95{
96 ATF_TP_ADD_TC(tp, sigaltstack_onstack);
97
98 return atf_no_error();
99}
diff --git a/src/regress/lib/libc/sys/t_socketpair.c b/src/regress/lib/libc/sys/t_socketpair.c
deleted file mode 100644
index e24a166161..0000000000
--- a/src/regress/lib/libc/sys/t_socketpair.c
+++ /dev/null
@@ -1,138 +0,0 @@
1/* $OpenBSD: t_socketpair.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_socketpair.c,v 1.2 2017/01/13 20:04:52 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include "macros.h"
41
42#include "atf-c.h"
43#include <fcntl.h>
44#include <unistd.h>
45#include <stdlib.h>
46#include <sys/socket.h>
47#include <sys/un.h>
48#include <errno.h>
49
50static void
51connected(int fd)
52{
53 struct sockaddr_un addr;
54 socklen_t len = (socklen_t)sizeof(addr);
55 ATF_REQUIRE(getpeername(fd, (struct sockaddr*)(void *)&addr,
56 &len) == 0);
57}
58
59static void
60run(int flags)
61{
62 int fd[2], i;
63
64 while ((i = open("/", O_RDONLY)) < 3)
65 ATF_REQUIRE(i != -1);
66
67 ATF_REQUIRE(closefrom(3) != -1);
68
69 ATF_REQUIRE(socketpair(AF_UNIX, SOCK_DGRAM | flags, 0, fd) == 0);
70
71 ATF_REQUIRE(fd[0] == 3);
72 ATF_REQUIRE(fd[1] == 4);
73
74 connected(fd[0]);
75 connected(fd[1]);
76
77 if (flags & SOCK_CLOEXEC) {
78 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0);
79 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0);
80 } else {
81 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0);
82 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0);
83 }
84
85 if (flags & SOCK_NONBLOCK) {
86 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0);
87 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0);
88 } else {
89 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0);
90 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0);
91 }
92
93 ATF_REQUIRE(close(fd[0]) != -1);
94 ATF_REQUIRE(close(fd[1]) != -1);
95}
96
97ATF_TC(socketpair_basic);
98ATF_TC_HEAD(socketpair_basic, tc)
99{
100 atf_tc_set_md_var(tc, "descr", "A basic test of socketpair(2)");
101}
102
103ATF_TC_BODY(socketpair_basic, tc)
104{
105 run(0);
106}
107
108ATF_TC(socketpair_nonblock);
109ATF_TC_HEAD(socketpair_nonblock, tc)
110{
111 atf_tc_set_md_var(tc, "descr", "A non-blocking test of socketpair(2)");
112}
113
114ATF_TC_BODY(socketpair_nonblock, tc)
115{
116 run(SOCK_NONBLOCK);
117}
118
119ATF_TC(socketpair_cloexec);
120ATF_TC_HEAD(socketpair_cloexec, tc)
121{
122 atf_tc_set_md_var(tc, "descr", "A close-on-exec of socketpair(2)");
123}
124
125ATF_TC_BODY(socketpair_cloexec, tc)
126{
127 run(SOCK_CLOEXEC);
128}
129
130ATF_TP_ADD_TCS(tp)
131{
132
133 ATF_TP_ADD_TC(tp, socketpair_basic);
134 ATF_TP_ADD_TC(tp, socketpair_nonblock);
135 ATF_TP_ADD_TC(tp, socketpair_cloexec);
136
137 return atf_no_error();
138}
diff --git a/src/regress/lib/libc/sys/t_stat.c b/src/regress/lib/libc/sys/t_stat.c
deleted file mode 100644
index ae88e1559a..0000000000
--- a/src/regress/lib/libc/sys/t_stat.c
+++ /dev/null
@@ -1,420 +0,0 @@
1/* $OpenBSD: t_stat.c,v 1.5 2023/10/31 07:57:59 claudio Exp $ */
2/* $NetBSD: t_stat.c,v 1.6 2019/07/16 17:29:18 martin Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38
39#include <arpa/inet.h>
40#include <netinet/in.h>
41
42#include "atf-c.h"
43#include <errno.h>
44#include <fcntl.h>
45#include <fts.h>
46#include <limits.h>
47#include <string.h>
48#include <unistd.h>
49
50#include <stdio.h>
51
52static const char *path = "stat";
53
54ATF_TC_WITH_CLEANUP(stat_chflags);
55ATF_TC_HEAD(stat_chflags, tc)
56{
57 atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
58}
59
60ATF_TC_BODY(stat_chflags, tc)
61{
62 struct stat sa, sb;
63 int fd;
64
65 (void)memset(&sa, 0, sizeof(struct stat));
66 (void)memset(&sb, 0, sizeof(struct stat));
67
68 fd = open(path, O_RDONLY | O_CREAT, 0600);
69
70 ATF_REQUIRE(fd != -1);
71 ATF_REQUIRE(stat(path, &sa) == 0);
72 ATF_REQUIRE(chflags(path, UF_NODUMP) == 0);
73 ATF_REQUIRE(stat(path, &sb) == 0);
74
75 if (sa.st_flags == sb.st_flags)
76 atf_tc_fail("stat(2) did not detect chflags(2)");
77
78 ATF_REQUIRE(close(fd) == 0);
79 ATF_REQUIRE(unlink(path) == 0);
80}
81
82ATF_TC_CLEANUP(stat_chflags, tc)
83{
84 (void)unlink(path);
85}
86
87ATF_TC(stat_dir);
88ATF_TC_HEAD(stat_dir, tc)
89{
90 atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
91}
92
93ATF_TC_BODY(stat_dir, tc)
94{
95 const short depth = 2;
96 struct stat sa, sb;
97 char *argv[2];
98 FTSENT *ftse;
99 FTS *fts;
100 int ops;
101
102 argv[1] = NULL;
103 argv[0] = __UNCONST("/");
104
105 ops = FTS_NOCHDIR;
106 ops |= FTS_PHYSICAL;
107
108 fts = fts_open(argv, ops, NULL);
109 ATF_REQUIRE(fts != NULL);
110
111 while ((ftse = fts_read(fts)) != NULL) {
112
113 if (ftse->fts_level < 1)
114 continue;
115
116 if (ftse->fts_level > depth) {
117 (void)fts_set(fts, ftse, FTS_SKIP);
118 continue;
119 }
120
121 switch(ftse->fts_info) {
122
123 case FTS_DP:
124
125 (void)memset(&sa, 0, sizeof(struct stat));
126 (void)memset(&sb, 0, sizeof(struct stat));
127
128 ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0);
129 ATF_REQUIRE(chdir(ftse->fts_path) == 0);
130 ATF_REQUIRE(stat(".", &sb) == 0);
131
132 /*
133 * The previous two stat(2) calls
134 * should be for the same directory.
135 */
136 if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
137 atf_tc_fail("inconsistent stat(2)");
138
139 /*
140 * Check that fts(3)'s stat(2)
141 * call equals the manual one.
142 */
143 if (sb.st_ino != ftse->fts_statp->st_ino)
144 atf_tc_fail("stat(2) and fts(3) differ");
145
146 break;
147
148 default:
149 break;
150 }
151 }
152
153 (void)fts_close(fts);
154}
155
156ATF_TC(stat_err);
157ATF_TC_HEAD(stat_err, tc)
158{
159 atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
160}
161
162ATF_TC_BODY(stat_err, tc)
163{
164 char buf[NAME_MAX + 1];
165 struct stat st;
166
167 (void)memset(buf, 'x', sizeof(buf));
168
169 errno = 0;
170 ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
171
172 errno = 0;
173 ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
174
175 errno = 0;
176 ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
177
178 errno = 0;
179 ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
180
181 errno = 0;
182 ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
183
184 errno = 0;
185 ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
186
187 errno = 0;
188 ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
189
190 errno = 0;
191 ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
192
193 errno = 0;
194 ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
195}
196
197ATF_TC_WITH_CLEANUP(stat_mtime);
198ATF_TC_HEAD(stat_mtime, tc)
199{
200 atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
201}
202
203ATF_TC_BODY(stat_mtime, tc)
204{
205 struct stat sa, sb;
206 int fd[3];
207 size_t i;
208
209 for (i = 0; i < __arraycount(fd); i++) {
210
211 (void)memset(&sa, 0, sizeof(struct stat));
212 (void)memset(&sb, 0, sizeof(struct stat));
213
214 fd[i] = open(path, O_WRONLY | O_CREAT, 0600);
215
216 ATF_REQUIRE(fd[i] != -1);
217 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
218 ATF_REQUIRE(stat(path, &sa) == 0);
219
220 (void)sleep(1);
221
222 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
223 ATF_REQUIRE(stat(path, &sb) == 0);
224
225 ATF_REQUIRE(close(fd[i]) == 0);
226 ATF_REQUIRE(unlink(path) == 0);
227
228 if (sa.st_mtime == sb.st_mtime)
229 atf_tc_fail("mtimes did not change");
230 }
231}
232
233ATF_TC_CLEANUP(stat_mtime, tc)
234{
235 (void)unlink(path);
236}
237
238ATF_TC_WITH_CLEANUP(stat_perm);
239ATF_TC_HEAD(stat_perm, tc)
240{
241 atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
242 atf_tc_set_md_var(tc, "require.user", "root");
243}
244
245ATF_TC_BODY(stat_perm, tc)
246{
247 struct stat sa, sb;
248 gid_t gid;
249 uid_t uid;
250 int fd;
251
252 (void)memset(&sa, 0, sizeof(struct stat));
253 (void)memset(&sb, 0, sizeof(struct stat));
254
255 uid = getuid();
256 gid = getgid();
257
258 fd = open(path, O_RDONLY | O_CREAT, 0600);
259
260 ATF_REQUIRE(fd != -1);
261 ATF_REQUIRE(fstat(fd, &sa) == 0);
262 ATF_REQUIRE(stat(path, &sb) == 0);
263
264 if (sa.st_gid != sb.st_gid)
265 atf_tc_fail("invalid GID");
266
267 if (uid != sa.st_uid || sa.st_uid != sb.st_uid)
268 atf_tc_fail("invalid UID");
269
270 ATF_REQUIRE(close(fd) == 0);
271 ATF_REQUIRE(unlink(path) == 0);
272}
273
274ATF_TC_CLEANUP(stat_perm, tc)
275{
276 (void)unlink(path);
277}
278
279ATF_TC_WITH_CLEANUP(stat_size);
280ATF_TC_HEAD(stat_size, tc)
281{
282 atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)");
283}
284
285ATF_TC_BODY(stat_size, tc)
286{
287 struct stat sa, sb, sc;
288 const size_t n = 10;
289 size_t i;
290 int fd;
291
292 fd = open(path, O_WRONLY | O_CREAT, 0600);
293 ATF_REQUIRE(fd >= 0);
294
295 for (i = 0; i < n; i++) {
296
297 (void)memset(&sa, 0, sizeof(struct stat));
298 (void)memset(&sb, 0, sizeof(struct stat));
299 (void)memset(&sc, 0, sizeof(struct stat));
300
301 ATF_REQUIRE(fstat(fd, &sa) == 0);
302 ATF_REQUIRE(write(fd, "X", 1) == 1);
303 ATF_REQUIRE(fstat(fd, &sb) == 0);
304 ATF_REQUIRE(stat(path, &sc) == 0);
305
306 if (sa.st_size + 1 != sb.st_size)
307 atf_tc_fail("invalid file size");
308
309 if (sb.st_size != sc.st_size)
310 atf_tc_fail("stat(2) and fstat(2) mismatch");
311 }
312
313 ATF_REQUIRE(close(fd) == 0);
314 ATF_REQUIRE(unlink(path) == 0);
315}
316
317ATF_TC_CLEANUP(stat_size, tc)
318{
319 (void)unlink(path);
320}
321
322ATF_TC(stat_socket);
323ATF_TC_HEAD(stat_socket, tc)
324{
325 atf_tc_set_md_var(tc, "descr", "Test fstat(2) with "
326 "a socket (PR kern/46077)");
327}
328
329ATF_TC_BODY(stat_socket, tc)
330{
331 struct sockaddr_in addr;
332 struct stat st;
333 uint32_t iaddr;
334 int fd, flags;
335
336 (void)memset(&st, 0, sizeof(struct stat));
337 (void)memset(&addr, 0, sizeof(struct sockaddr_in));
338
339 fd = socket(AF_INET, SOCK_STREAM, 0);
340 ATF_REQUIRE(fd >= 0);
341
342 flags = fcntl(fd, F_GETFL);
343
344 ATF_REQUIRE(flags != -1);
345 ATF_REQUIRE(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1);
346 ATF_REQUIRE(inet_pton(AF_INET, "127.0.0.1", &iaddr) == 1);
347
348 addr.sin_port = htons(42);
349 addr.sin_family = AF_INET;
350 addr.sin_addr.s_addr = iaddr;
351
352 errno = 0;
353
354 ATF_REQUIRE_ERRNO(EINPROGRESS,
355 connect(fd, (struct sockaddr *)&addr,
356 sizeof(struct sockaddr_in)) == -1);
357
358 errno = 0;
359
360 if (fstat(fd, &st) != 0 || errno != 0)
361 atf_tc_fail("fstat(2) failed for a EINPROGRESS socket");
362
363 (void)close(fd);
364}
365
366ATF_TC_WITH_CLEANUP(stat_symlink);
367ATF_TC_HEAD(stat_symlink, tc)
368{
369 atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)");
370}
371
372ATF_TC_BODY(stat_symlink, tc)
373{
374 const char *pathlink = "pathlink";
375 struct stat sa, sb;
376 int fd;
377
378 (void)memset(&sa, 0, sizeof(struct stat));
379 (void)memset(&sb, 0, sizeof(struct stat));
380
381 fd = open(path, O_WRONLY | O_CREAT, 0600);
382
383 ATF_REQUIRE(fd >= 0);
384 ATF_REQUIRE(symlink(path, pathlink) == 0);
385 ATF_REQUIRE(stat(pathlink, &sa) == 0);
386 ATF_REQUIRE(lstat(pathlink, &sb) == 0);
387
388 if (S_ISLNK(sa.st_mode) != 0)
389 atf_tc_fail("stat(2) detected symbolic link");
390
391 if (S_ISLNK(sb.st_mode) == 0)
392 atf_tc_fail("lstat(2) did not detect symbolic link");
393
394 if (sa.st_mode == sb.st_mode)
395 atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
396
397 (void)close(fd);
398 ATF_REQUIRE(unlink(path) == 0);
399 ATF_REQUIRE(unlink(pathlink) == 0);
400}
401
402ATF_TC_CLEANUP(stat_symlink, tc)
403{
404 (void)unlink(path);
405}
406
407ATF_TP_ADD_TCS(tp)
408{
409
410 ATF_TP_ADD_TC(tp, stat_chflags);
411 ATF_TP_ADD_TC(tp, stat_dir);
412 ATF_TP_ADD_TC(tp, stat_err);
413 ATF_TP_ADD_TC(tp, stat_mtime);
414 ATF_TP_ADD_TC(tp, stat_perm);
415 ATF_TP_ADD_TC(tp, stat_size);
416 ATF_TP_ADD_TC(tp, stat_socket);
417 ATF_TP_ADD_TC(tp, stat_symlink);
418
419 return atf_no_error();
420}
diff --git a/src/regress/lib/libc/sys/t_truncate.c b/src/regress/lib/libc/sys/t_truncate.c
deleted file mode 100644
index f42494f8db..0000000000
--- a/src/regress/lib/libc/sys/t_truncate.c
+++ /dev/null
@@ -1,180 +0,0 @@
1/* $OpenBSD: t_truncate.c,v 1.4 2022/05/24 05:14:30 anton Exp $ */
2/* $NetBSD: t_truncate.c,v 1.3 2017/01/13 20:03:51 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <stdio.h>
42#include <string.h>
43#include <unistd.h>
44
45static const char path[] = "truncate";
46static const size_t sizes[] = { 8, 16, 512, 1024, 2048, 4094, 3000, 30 };
47
48ATF_TC_WITH_CLEANUP(ftruncate_basic);
49ATF_TC_HEAD(ftruncate_basic, tc)
50{
51 atf_tc_set_md_var(tc, "descr", "A basic test of ftruncate(2)");
52}
53
54ATF_TC_BODY(ftruncate_basic, tc)
55{
56 struct stat st;
57 size_t i;
58 int fd;
59
60 fd = open(path, O_RDWR | O_CREAT, 0600);
61 ATF_REQUIRE(fd >= 0);
62
63 for (i = 0; i < __arraycount(sizes); i++) {
64
65 (void)memset(&st, 0, sizeof(struct stat));
66
67 ATF_REQUIRE(ftruncate(fd, sizes[i]) == 0);
68 ATF_REQUIRE(fstat(fd, &st) == 0);
69
70 (void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]);
71
72 if (sizes[i] != (size_t)st.st_size)
73 atf_tc_fail("ftruncate(2) did not truncate");
74 }
75
76 (void)close(fd);
77 (void)unlink(path);
78}
79
80ATF_TC_CLEANUP(ftruncate_basic, tc)
81{
82 (void)unlink(path);
83}
84
85ATF_TC(ftruncate_err);
86ATF_TC_HEAD(ftruncate_err, tc)
87{
88 atf_tc_set_md_var(tc, "descr", "Test errors from ftruncate(2)");
89 atf_tc_set_md_var(tc, "require.user", "unprivileged");
90}
91
92ATF_TC_BODY(ftruncate_err, tc)
93{
94 int fd;
95
96 fd = open("/etc/passwd", O_RDONLY);
97 ATF_REQUIRE(fd >= 0);
98
99 errno = 0;
100 ATF_REQUIRE_ERRNO(EBADF, ftruncate(-1, 999) == -1);
101
102 errno = 0;
103 ATF_REQUIRE_ERRNO(EINVAL, ftruncate(fd, 999) == -1);
104
105 (void)close(fd);
106}
107
108ATF_TC_WITH_CLEANUP(truncate_basic);
109ATF_TC_HEAD(truncate_basic, tc)
110{
111 atf_tc_set_md_var(tc, "descr", "A basic test of truncate(2)");
112}
113
114ATF_TC_BODY(truncate_basic, tc)
115{
116 struct stat st;
117 size_t i;
118 int fd;
119
120 fd = open(path, O_RDWR | O_CREAT, 0600);
121 ATF_REQUIRE(fd >= 0);
122
123 for (i = 0; i < __arraycount(sizes); i++) {
124
125 (void)memset(&st, 0, sizeof(struct stat));
126
127 ATF_REQUIRE(truncate(path, sizes[i]) == 0);
128 ATF_REQUIRE(fstat(fd, &st) == 0);
129
130 (void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]);
131
132 if (sizes[i] != (size_t)st.st_size)
133 atf_tc_fail("truncate(2) did not truncate");
134 }
135
136 (void)close(fd);
137 (void)unlink(path);
138}
139
140ATF_TC_CLEANUP(truncate_basic, tc)
141{
142 (void)unlink(path);
143}
144
145ATF_TC(truncate_err);
146ATF_TC_HEAD(truncate_err, tc)
147{
148 atf_tc_set_md_var(tc, "descr", "Test errors from truncate(2)");
149 atf_tc_set_md_var(tc, "require.user", "unprivileged");
150}
151
152ATF_TC_BODY(truncate_err, tc)
153{
154 char buf[PATH_MAX];
155
156 errno = 0;
157 ATF_REQUIRE_ERRNO(EFAULT, truncate((void *)-1, 999) == -1);
158
159 errno = 0;
160 ATF_REQUIRE_ERRNO(EISDIR, truncate("/tmp", 999) == -1);
161
162 errno = 0;
163 ATF_REQUIRE_ERRNO(ENOENT, truncate("/a/b/c/d/e/f/g", 999) == -1);
164
165 errno = 0;
166 snprintf(buf, sizeof(buf), "%s/truncate_test.root_owned",
167 atf_tc_get_config_var(tc, "srcdir"));
168 ATF_REQUIRE_ERRNO(EACCES, truncate(buf, 999) == -1);
169}
170
171ATF_TP_ADD_TCS(tp)
172{
173
174 ATF_TP_ADD_TC(tp, ftruncate_basic);
175 ATF_TP_ADD_TC(tp, ftruncate_err);
176 ATF_TP_ADD_TC(tp, truncate_basic);
177 ATF_TP_ADD_TC(tp, truncate_err);
178
179 return atf_no_error();
180}
diff --git a/src/regress/lib/libc/sys/t_umask.c b/src/regress/lib/libc/sys/t_umask.c
deleted file mode 100644
index 53837b654e..0000000000
--- a/src/regress/lib/libc/sys/t_umask.c
+++ /dev/null
@@ -1,207 +0,0 @@
1/* $OpenBSD: t_umask.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_umask.c,v 1.2 2017/01/13 19:34:19 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36#include <sys/wait.h>
37
38#include "atf-c.h"
39#include <fcntl.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44static const char path[] = "umask";
45static const mode_t mask[] = {
46 S_IRWXU,
47 S_IRUSR,
48 S_IWUSR,
49 S_IXUSR,
50 S_IRWXG,
51 S_IRGRP,
52 S_IWGRP,
53 S_IXGRP,
54 S_IRWXO,
55 S_IROTH,
56 S_IWOTH,
57 S_IXOTH
58};
59
60ATF_TC_WITH_CLEANUP(umask_fork);
61ATF_TC_HEAD(umask_fork, tc)
62{
63 atf_tc_set_md_var(tc, "descr", "Check that umask(2) is inherited");
64}
65
66ATF_TC_BODY(umask_fork, tc)
67{
68 mode_t mode;
69 pid_t pid;
70 size_t i;
71 int sta;
72
73 for (i = 0; i < __arraycount(mask) - 1; i++) {
74
75 (void)umask(mask[i] | mask[i + 1]);
76
77 pid = fork();
78
79 if (pid < 0)
80 continue;
81
82 if (pid == 0) {
83
84 mode = umask(mask[i]);
85
86 if (mode != (mask[i] | mask[i + 1]))
87 _exit(EXIT_FAILURE);
88
89 _exit(EXIT_SUCCESS);
90 }
91
92 (void)wait(&sta);
93
94 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
95 goto fail;
96 }
97
98 return;
99
100fail:
101 (void)umask(S_IWGRP | S_IWOTH);
102
103 atf_tc_fail("umask(2) was not inherited");
104}
105
106ATF_TC_CLEANUP(umask_fork, tc)
107{
108 (void)umask(S_IWGRP | S_IWOTH);
109}
110
111ATF_TC_WITH_CLEANUP(umask_open);
112ATF_TC_HEAD(umask_open, tc)
113{
114 atf_tc_set_md_var(tc, "descr", "A basic test of open(2) and umask(2)");
115}
116
117ATF_TC_BODY(umask_open, tc)
118{
119 const char *str = NULL;
120 struct stat st;
121 size_t i;
122 int fd;
123
124 for (i = 0; i < __arraycount(mask); i++) {
125
126 (void)umask(mask[i]);
127
128 fd = open(path, O_RDWR | O_CREAT, 0777);
129
130 if (fd < 0)
131 continue;
132
133 (void)close(fd);
134 (void)memset(&st, 0, sizeof(struct stat));
135
136 if (stat(path, &st) != 0) {
137 str = "failed to stat(2)";
138 goto out;
139 }
140
141 if ((st.st_mode & mask[i]) != 0) {
142 str = "invalid umask(2)";
143 goto out;
144 }
145
146 if (unlink(path) != 0) {
147 str = "failed to unlink(2)";
148 goto out;
149 }
150
151 }
152
153out:
154 (void)umask(S_IWGRP | S_IWOTH);
155
156 if (str != NULL)
157 atf_tc_fail("%s", str);
158}
159
160ATF_TC_CLEANUP(umask_open, tc)
161{
162 (void)umask(S_IWGRP | S_IWOTH);
163 (void)unlink(path);
164}
165
166ATF_TC_WITH_CLEANUP(umask_previous);
167ATF_TC_HEAD(umask_previous, tc)
168{
169 atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)");
170}
171
172ATF_TC_BODY(umask_previous, tc)
173{
174 mode_t mode;
175 size_t i;
176
177 for (i = 0; i < __arraycount(mask); i++) {
178
179 mode = umask(mask[i]);
180 mode = umask(mask[i]);
181
182 if (mode != mask[i])
183 goto fail;
184 }
185
186 return;
187
188fail:
189 (void)umask(S_IWGRP | S_IWOTH);
190
191 atf_tc_fail("umask(2) did not return the previous mask");
192}
193
194ATF_TC_CLEANUP(umask_previous, tc)
195{
196 (void)umask(S_IWGRP | S_IWOTH);
197}
198
199ATF_TP_ADD_TCS(tp)
200{
201
202 ATF_TP_ADD_TC(tp, umask_fork);
203 ATF_TP_ADD_TC(tp, umask_open);
204 ATF_TP_ADD_TC(tp, umask_previous);
205
206 return atf_no_error();
207}
diff --git a/src/regress/lib/libc/sys/t_unlink.c b/src/regress/lib/libc/sys/t_unlink.c
deleted file mode 100644
index be1529b6ad..0000000000
--- a/src/regress/lib/libc/sys/t_unlink.c
+++ /dev/null
@@ -1,159 +0,0 @@
1/* $OpenBSD: t_unlink.c,v 1.3 2023/10/31 07:59:52 claudio Exp $ */
2/* $NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/stat.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <string.h>
42#include <unistd.h>
43
44static char path[] = "unlink";
45
46ATF_TC_WITH_CLEANUP(unlink_basic);
47ATF_TC_HEAD(unlink_basic, tc)
48{
49 atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)");
50}
51
52ATF_TC_BODY(unlink_basic, tc)
53{
54 const size_t n = 512;
55 size_t i;
56 int fd;
57
58 for (i = 0; i < n; i++) {
59
60 fd = open(path, O_RDWR | O_CREAT, 0666);
61
62 ATF_REQUIRE(fd != -1);
63 ATF_REQUIRE(close(fd) == 0);
64 ATF_REQUIRE(unlink(path) == 0);
65
66 errno = 0;
67 ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
68 }
69}
70
71ATF_TC_CLEANUP(unlink_basic, tc)
72{
73 (void)unlink(path);
74}
75
76ATF_TC_WITH_CLEANUP(unlink_err);
77ATF_TC_HEAD(unlink_err, tc)
78{
79 atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)");
80}
81
82ATF_TC_BODY(unlink_err, tc)
83{
84 char buf[PATH_MAX + 1];
85
86 (void)memset(buf, 'x', sizeof(buf));
87
88 errno = 0;
89 ATF_REQUIRE(unlink("/") == -1);
90
91 errno = 0;
92 ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1);
93
94 errno = 0;
95 ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1);
96}
97
98ATF_TC_CLEANUP(unlink_err, tc)
99{
100 (void)unlink(path);
101}
102
103ATF_TC_WITH_CLEANUP(unlink_fifo);
104ATF_TC_HEAD(unlink_fifo, tc)
105{
106 atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO");
107}
108
109ATF_TC_BODY(unlink_fifo, tc)
110{
111
112 ATF_REQUIRE(mkfifo(path, 0666) == 0);
113 ATF_REQUIRE(unlink(path) == 0);
114
115 errno = 0;
116 ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
117}
118
119ATF_TC_CLEANUP(unlink_fifo, tc)
120{
121 (void)unlink(path);
122}
123
124ATF_TC_WITH_CLEANUP(unlink_perm);
125ATF_TC_HEAD(unlink_perm, tc)
126{
127 atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)");
128 atf_tc_set_md_var(tc, "require.user", "unprivileged");
129}
130
131ATF_TC_BODY(unlink_perm, tc)
132{
133 int rv;
134
135 errno = 0;
136 rv = unlink("/etc");
137 ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
138 "unlinking a directory did not fail with EPERM or EACCESS; "
139 "unlink() returned %d, errno %d", rv, errno);
140
141 errno = 0;
142 ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1);
143}
144
145ATF_TC_CLEANUP(unlink_perm, tc)
146{
147 (void)unlink(path);
148}
149
150ATF_TP_ADD_TCS(tp)
151{
152
153 ATF_TP_ADD_TC(tp, unlink_basic);
154 ATF_TP_ADD_TC(tp, unlink_err);
155 ATF_TP_ADD_TC(tp, unlink_fifo);
156 ATF_TP_ADD_TC(tp, unlink_perm);
157
158 return atf_no_error();
159}
diff --git a/src/regress/lib/libc/sys/t_vfork.c b/src/regress/lib/libc/sys/t_vfork.c
deleted file mode 100644
index a7c6f82e8a..0000000000
--- a/src/regress/lib/libc/sys/t_vfork.c
+++ /dev/null
@@ -1,31 +0,0 @@
1/* $OpenBSD: t_vfork.c,v 1.1 2021/09/02 15:28:41 mbuhl Exp $ */
2/* $NetBSD: t_vfork.c,v 1.1 2018/05/18 06:39:58 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2018 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define VFORK
31#include "t_fork.c"
diff --git a/src/regress/lib/libc/sys/t_wait_noproc.c b/src/regress/lib/libc/sys/t_wait_noproc.c
deleted file mode 100644
index 8d408041c5..0000000000
--- a/src/regress/lib/libc/sys/t_wait_noproc.c
+++ /dev/null
@@ -1,355 +0,0 @@
1/* $OpenBSD: t_wait_noproc.c,v 1.4 2022/12/19 22:44:54 guenther Exp $ */
2/* $NetBSD: t_wait_noproc.c,v 1.5 2016/11/09 17:50:19 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2016 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/wait.h>
33#include <sys/resource.h>
34
35#include <errno.h>
36#include <stdio.h>
37
38#include "atf-c.h"
39
40#ifndef TWAIT_OPTION
41#define TWAIT_OPTION 0
42#endif
43
44#if TWAIT_OPTION == 0
45ATF_TC(wait);
46ATF_TC_HEAD(wait, tc)
47{
48 atf_tc_set_md_var(tc, "descr",
49 "Test that wait(2) returns ECHILD for no child");
50}
51
52ATF_TC_BODY(wait, tc)
53{
54 ATF_REQUIRE_ERRNO(ECHILD, wait(NULL) == -1);
55}
56#endif
57
58ATF_TC(waitpid);
59ATF_TC_HEAD(waitpid, tc)
60{
61 atf_tc_set_md_var(tc, "descr",
62 "Test that waitpid(2) returns ECHILD for WAIT_ANY and option %s",
63 ___STRING(TWAIT_OPTION));
64}
65
66ATF_TC_BODY(waitpid, tc)
67{
68 ATF_REQUIRE_ERRNO(ECHILD, waitpid(WAIT_ANY, NULL, TWAIT_OPTION) == -1);
69}
70
71ATF_TC(waitid);
72ATF_TC_HEAD(waitid, tc)
73{
74 atf_tc_set_md_var(tc, "descr",
75 "Test that waitid(2) returns ECHILD for P_ALL and option %s",
76 ___STRING(TWAIT_OPTION));
77}
78
79ATF_TC_BODY(waitid, tc)
80{
81 ATF_REQUIRE_ERRNO(ECHILD,
82 waitid(P_ALL, 0, NULL,
83 WEXITED | TWAIT_OPTION) == -1);
84}
85
86ATF_TC(wait3);
87ATF_TC_HEAD(wait3, tc)
88{
89 atf_tc_set_md_var(tc, "descr",
90 "Test that wait3(2) returns ECHILD for no child");
91}
92
93ATF_TC_BODY(wait3, tc)
94{
95 ATF_REQUIRE_ERRNO(ECHILD, wait3(NULL, TWAIT_OPTION, NULL) == -1);
96}
97
98ATF_TC(wait4);
99ATF_TC_HEAD(wait4, tc)
100{
101 atf_tc_set_md_var(tc, "descr",
102 "Test that wait4(2) returns ECHILD for WAIT_ANY and option %s",
103 ___STRING(TWAIT_OPTION));
104}
105
106ATF_TC_BODY(wait4, tc)
107{
108 ATF_REQUIRE_ERRNO(ECHILD,
109 wait4(WAIT_ANY, NULL, TWAIT_OPTION, NULL) == -1);
110}
111
112#ifndef __OpenBSD__
113 * Adjusted for OpenBSD, not available
114 * ATF_TC(wait6);
115 * ATF_TC_HEAD(wait6, tc)
116 * {
117 * atf_tc_set_md_var(tc, "descr",
118 * "Test that wait6(2) returns ECHILD for P_ALL and option %s",
119 * ___STRING(TWAIT_OPTION));
120 * }
121 *
122 * ATF_TC_BODY(wait6, tc)
123 * {
124 * ATF_REQUIRE_ERRNO(ECHILD,
125 * wait6(P_ALL, 0, NULL,
126 * WTRAPPED | WEXITED | TWAIT_OPTION, NULL, NULL) == -1);
127 * }
128 */
129#endif
130
131/*
132 * Generator of valid combinations of options
133 * Usage: i = 0; while ((o = get_options_wait6(i++)) != -1) {}
134 */
135static int
136get_options6(size_t pos)
137{
138 int rv = 0;
139 size_t n;
140 /*
141 * waitid(2) must specify at least one of WEXITED, WUNTRACED,
142 * WSTOPPED, WTRAPPED or WCONTINUED. Single option WNOWAIT
143 * isn't valid.
144 */
145 const int matrix[] = {
146 WNOWAIT, // First in order to blacklist it easily
147 WEXITED,
148#ifndef __OpenBSD__
149 WUNTRACED,
150#endif
151 WSTOPPED, // SUS compatibility, equal to WUNTRACED
152 WTRAPPED,
153 WCONTINUED
154 };
155
156 const size_t M = (1 << __arraycount(matrix)) - 1;
157 /* Skip empty and sole WNOWAIT option */
158 pos+=2;
159
160 if (pos > M)
161 return -1;
162
163 for (n = 0; n < __arraycount(matrix); n++) {
164 if (pos & __BIT(n))
165 rv |= matrix[n];
166 }
167
168 return rv;
169}
170
171/*
172 * Generator of valid combinations of options
173 * Usage: i = 0; while ((o = get_options_wait4(i++)) != -1) {}
174 */
175static int
176get_options4(size_t pos)
177{
178 int rv = 0;
179 size_t n;
180
181 const int special[] = {
182 0,
183#ifndef __OpenBSD__
184 WALLSIG,
185 WALTSIG,
186 __WALL, // Linux compatibility, equal to WALLSIG
187 __WCLONE // Linux compatibility, equal to WALTSIG
188#endif
189 };
190
191 const int matrix[] = {
192#ifndef __OpenBSD__
193 WNOWAIT,
194 WEXITED,
195#endif
196 WUNTRACED,
197#ifndef __OpenBSD__
198 WSTOPPED, // SUS compatibility, equal to WUNTRACED
199 WTRAPPED,
200#endif
201 WCONTINUED
202 };
203
204 const size_t M = (1 << __arraycount(special)) - 1;
205
206 if (pos < __arraycount(special))
207 return special[pos];
208
209 pos -= __arraycount(special);
210
211 ++pos; /* Don't start with empty mask */
212
213 if (pos > M)
214 return -1;
215
216 for (n = 0; n < __arraycount(special); n++) {
217 if (pos & __BIT(n))
218 rv |= matrix[n];
219 }
220
221 return rv;
222}
223
224ATF_TC(waitpid_options);
225ATF_TC_HEAD(waitpid_options, tc)
226{
227 atf_tc_set_md_var(tc, "descr",
228 "Test that waitpid(2) returns ECHILD for WAIT_ANY and valid "
229 "combination of options with%s WNOHANG",
230 TWAIT_OPTION == 0 ? "out" : "");
231}
232
233ATF_TC_BODY(waitpid_options, tc)
234{
235 size_t i = 0;
236 int o;
237
238 while((o = get_options4(i++)) != -1) {
239 printf("Testing waitpid(2) with options %x\n", o);
240
241 ATF_REQUIRE_ERRNO(ECHILD,
242 waitpid(WAIT_ANY, NULL, o | TWAIT_OPTION) == -1);
243 }
244}
245
246ATF_TC(waitid_options);
247ATF_TC_HEAD(waitid_options, tc)
248{
249 atf_tc_set_md_var(tc, "descr",
250 "Test that waitid(2) returns ECHILD for P_ALL and valid "
251 "combination of options with%s WNOHANG",
252 TWAIT_OPTION == 0 ? "out" : "");
253}
254
255ATF_TC_BODY(waitid_options, tc)
256{
257 size_t i = 0;
258 int o;
259
260 while((o = get_options6(i++)) != -1) {
261 printf("Testing waitid(2) with options %x\n", o);
262
263 ATF_REQUIRE_ERRNO(ECHILD,
264 waitid(P_ALL, 0, NULL, o | TWAIT_OPTION) == -1);
265 }
266}
267
268ATF_TC(wait3_options);
269ATF_TC_HEAD(wait3_options, tc)
270{
271 atf_tc_set_md_var(tc, "descr",
272 "Test that wait3(2) returns ECHILD for no child");
273}
274
275ATF_TC_BODY(wait3_options, tc)
276{
277 size_t i = 0;
278 int o;
279
280 while((o = get_options4(i++)) != -1) {
281 printf("Testing wait3(2) with options %x\n", o);
282
283 ATF_REQUIRE_ERRNO(ECHILD,
284 wait3(NULL, o | TWAIT_OPTION, NULL) == -1);
285 }
286}
287
288ATF_TC(wait4_options);
289ATF_TC_HEAD(wait4_options, tc)
290{
291 atf_tc_set_md_var(tc, "descr",
292 "Test that wait4(2) returns ECHILD for WAIT_ANY and option %s",
293 ___STRING(TWAIT_OPTION));
294}
295
296ATF_TC_BODY(wait4_options, tc)
297{
298 size_t i = 0;
299 int o;
300
301 while((o = get_options4(i++)) != -1) {
302 printf("Testing wait4(2) with options %x\n", o);
303
304 ATF_REQUIRE_ERRNO(ECHILD,
305 wait4(WAIT_ANY, NULL, o | TWAIT_OPTION, NULL) == -1);
306 }
307}
308
309#ifndef __OpenBSD__
310ATF_TC(wait6_options);
311ATF_TC_HEAD(wait6_options, tc)
312{
313 atf_tc_set_md_var(tc, "descr",
314 "Test that wait6(2) returns ECHILD for P_ALL and option %s",
315 ___STRING(TWAIT_OPTION));
316}
317
318ATF_TC_BODY(wait6_options, tc)
319{
320 size_t i = 0;
321 int o;
322
323 while((o = get_options6(i++)) != -1) {
324 printf("Testing wait6(2) with options %x\n", o);
325
326 ATF_REQUIRE_ERRNO(ECHILD,
327 wait6(P_ALL, 0, NULL, o | TWAIT_OPTION, NULL, NULL) == -1);
328 }
329}
330#endif
331
332ATF_TP_ADD_TCS(tp)
333{
334
335#if TWAIT_OPTION == 0
336 ATF_TP_ADD_TC(tp, wait);
337#endif
338 ATF_TP_ADD_TC(tp, waitpid);
339 ATF_TP_ADD_TC(tp, waitid);
340 ATF_TP_ADD_TC(tp, wait3);
341 ATF_TP_ADD_TC(tp, wait4);
342#ifndef __OpenBSD__
343 ATF_TP_ADD_TC(tp, wait6);
344#endif
345
346 ATF_TP_ADD_TC(tp, waitpid_options);
347 ATF_TP_ADD_TC(tp, waitid_options);
348 ATF_TP_ADD_TC(tp, wait3_options);
349 ATF_TP_ADD_TC(tp, wait4_options);
350#ifndef __OpenBSD__
351 ATF_TP_ADD_TC(tp, wait6_options);
352#endif
353
354 return atf_no_error();
355}
diff --git a/src/regress/lib/libc/sys/t_wait_noproc_wnohang.c b/src/regress/lib/libc/sys/t_wait_noproc_wnohang.c
deleted file mode 100644
index d1dcb98164..0000000000
--- a/src/regress/lib/libc/sys/t_wait_noproc_wnohang.c
+++ /dev/null
@@ -1,31 +0,0 @@
1/* $OpenBSD: t_wait_noproc_wnohang.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $ */
2/* $NetBSD: t_wait_noproc_wnohang.c,v 1.1 2016/11/06 15:03:30 kamil Exp $ */
3
4/*-
5 * Copyright (c) 2016 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define TWAIT_OPTION WNOHANG
31#include "t_wait_noproc.c"
diff --git a/src/regress/lib/libc/sys/t_waitid.c b/src/regress/lib/libc/sys/t_waitid.c
deleted file mode 100644
index ecb16fabad..0000000000
--- a/src/regress/lib/libc/sys/t_waitid.c
+++ /dev/null
@@ -1,272 +0,0 @@
1/* $OpenBSD: t_waitid.c,v 1.1 2022/10/26 23:18:02 kettenis Exp $ */
2/* $NetBSD: t_wait.c,v 1.10 2021/07/17 14:03:35 martin Exp $ */
3
4/*-
5 * Copyright (c) 2016 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36#include <sys/resource.h>
37
38#include <errno.h>
39#include <inttypes.h>
40#include <limits.h>
41#include <pwd.h>
42#include <signal.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46
47#include "atf-c.h"
48
49ATF_TC(waitid_invalid);
50ATF_TC_HEAD(waitid_invalid, tc)
51{
52 atf_tc_set_md_var(tc, "descr",
53 "Test that waitid(2) returns EINVAL with 0 options");
54}
55
56ATF_TC_BODY(waitid_invalid, tc)
57{
58 siginfo_t si;
59 ATF_REQUIRE(waitid(P_ALL, 0, &si, 0) == -1
60 && errno == EINVAL);
61}
62
63ATF_TC(waitid_exited);
64ATF_TC_HEAD(waitid_exited, tc)
65{
66 atf_tc_set_md_var(tc, "descr",
67 "Test that waitid(2) handled exiting process and code");
68}
69
70ATF_TC_BODY(waitid_exited, tc)
71{
72 siginfo_t si;
73 pid_t pid;
74
75 switch (pid = fork()) {
76 case 0:
77 exit(0x5a5a5a5a);
78 /*NOTREACHED*/
79 case -1:
80 ATF_REQUIRE(pid > 0);
81 __unreachable();
82 default:
83 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
84 ATF_REQUIRE(si.si_status == 0x5a5a5a5a);
85 ATF_REQUIRE(si.si_pid == pid);
86 ATF_REQUIRE(si.si_uid == getuid());
87 ATF_REQUIRE(si.si_code == CLD_EXITED);
88 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
89 (uintmax_t)si.si_utime);
90 break;
91 }
92}
93
94ATF_TC(waitid_terminated);
95ATF_TC_HEAD(waitid_terminated, tc)
96{
97 atf_tc_set_md_var(tc, "descr",
98 "Test that waitid(2) handled terminated process and code");
99}
100
101ATF_TC_BODY(waitid_terminated, tc)
102{
103 siginfo_t si;
104 pid_t pid;
105
106 switch (pid = fork()) {
107 case 0:
108 sleep(100);
109 /*FALLTHROUGH*/
110 case -1:
111 ATF_REQUIRE(pid > 0);
112 __unreachable();
113 default:
114 ATF_REQUIRE(kill(pid, SIGTERM) == 0);
115 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
116 ATF_REQUIRE(si.si_status == SIGTERM);
117 ATF_REQUIRE(si.si_pid == pid);
118 ATF_REQUIRE(si.si_uid == getuid());
119 ATF_REQUIRE(si.si_code == CLD_KILLED);
120 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
121 (uintmax_t)si.si_utime);
122 break;
123 }
124}
125
126ATF_TC(waitid_coredumped);
127ATF_TC_HEAD(waitid_coredumped, tc)
128{
129 atf_tc_set_md_var(tc, "descr",
130 "Test that waitid(2) handled coredumped process and code");
131}
132
133ATF_TC_BODY(waitid_coredumped, tc)
134{
135 siginfo_t si;
136 pid_t pid;
137 static const struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
138
139 switch (pid = fork()) {
140 case 0:
141 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
142 *(char *)8 = 0;
143 /*FALLTHROUGH*/
144 case -1:
145 ATF_REQUIRE(pid > 0);
146 __unreachable();
147 default:
148 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
149 ATF_REQUIRE(si.si_status == SIGSEGV);
150 ATF_REQUIRE(si.si_pid == pid);
151 ATF_REQUIRE(si.si_uid == getuid());
152 ATF_REQUIRE(si.si_code == CLD_DUMPED);
153 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
154 (uintmax_t)si.si_utime);
155 break;
156 }
157}
158
159ATF_TC(waitid_stop_and_go);
160ATF_TC_HEAD(waitid_stop_and_go, tc)
161{
162 atf_tc_set_md_var(tc, "descr",
163 "Test that waitid(2) handled stopped/continued process and code");
164}
165
166ATF_TC_BODY(waitid_stop_and_go, tc)
167{
168 siginfo_t si;
169 pid_t pid;
170 static const struct rlimit rl = { 0, 0 };
171
172 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
173 switch (pid = fork()) {
174 case 0:
175 sleep(100);
176 /*FALLTHROUGH*/
177 case -1:
178 ATF_REQUIRE(pid > 0);
179 __unreachable();
180 default:
181 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
182 ATF_REQUIRE(waitid(P_PID, pid, &si, WSTOPPED) == 0);
183 ATF_REQUIRE(si.si_status == SIGSTOP);
184 ATF_REQUIRE(si.si_pid == pid);
185 ATF_REQUIRE(si.si_uid == getuid());
186 ATF_REQUIRE(si.si_code == CLD_STOPPED);
187 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
188 (uintmax_t)si.si_utime);
189
190 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
191 ATF_REQUIRE(waitid(P_PID, pid, &si, WCONTINUED) == 0);
192 ATF_REQUIRE(si.si_status == SIGCONT);
193 ATF_REQUIRE(si.si_pid == pid);
194 ATF_REQUIRE(si.si_uid == getuid());
195 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
196 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
197 (uintmax_t)si.si_utime);
198
199 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
200 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
201 ATF_REQUIRE(si.si_status == SIGQUIT);
202 ATF_REQUIRE(si.si_pid == pid);
203 ATF_REQUIRE(si.si_uid == getuid());
204 ATF_REQUIRE(si.si_code == CLD_KILLED);
205 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
206 (uintmax_t)si.si_utime);
207 break;
208 }
209}
210
211ATF_TC(waitid_stopgo_loop);
212ATF_TC_HEAD(waitid_stopgo_loop, tc)
213{
214 atf_tc_set_md_var(tc, "descr",
215 "Test that waitid(2) handled stopped/continued process loop");
216}
217
218ATF_TC_BODY(waitid_stopgo_loop, tc)
219{
220 siginfo_t si;
221 pid_t pid;
222 static const struct rlimit rl = { 0, 0 };
223 size_t N = 100;
224
225 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
226 switch (pid = fork()) {
227 case 0:
228 sleep(100);
229 /*FALLTHROUGH*/
230 case -1:
231 ATF_REQUIRE(pid > 0);
232 __unreachable();
233 }
234
235 printf("Before loop of SIGSTOP/SIGCONT sequence %zu times\n", N);
236 while (N --> 0) {
237 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
238 ATF_REQUIRE(waitid(P_PID, pid, &si, WSTOPPED) == 0);
239 ATF_REQUIRE(si.si_status == SIGSTOP);
240 ATF_REQUIRE(si.si_pid == pid);
241 ATF_REQUIRE(si.si_uid == getuid());
242 ATF_REQUIRE(si.si_code == CLD_STOPPED);
243
244 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
245 ATF_REQUIRE(waitid(P_PID, pid, &si, WCONTINUED) == 0);
246 ATF_REQUIRE(si.si_status == SIGCONT);
247 ATF_REQUIRE(si.si_pid == pid);
248 ATF_REQUIRE(si.si_uid == getuid());
249 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
250 }
251 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
252 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
253 ATF_REQUIRE(si.si_status == SIGQUIT);
254 ATF_REQUIRE(si.si_pid == pid);
255 ATF_REQUIRE(si.si_uid == getuid());
256 ATF_REQUIRE(si.si_code == CLD_KILLED);
257 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
258 (uintmax_t)si.si_utime);
259}
260
261ATF_TP_ADD_TCS(tp)
262{
263
264 ATF_TP_ADD_TC(tp, waitid_invalid);
265 ATF_TP_ADD_TC(tp, waitid_exited);
266 ATF_TP_ADD_TC(tp, waitid_terminated);
267 ATF_TP_ADD_TC(tp, waitid_coredumped);
268 ATF_TP_ADD_TC(tp, waitid_stop_and_go);
269 ATF_TP_ADD_TC(tp, waitid_stopgo_loop);
270
271 return atf_no_error();
272}
diff --git a/src/regress/lib/libc/sys/t_write.c b/src/regress/lib/libc/sys/t_write.c
deleted file mode 100644
index 8d01ebfba8..0000000000
--- a/src/regress/lib/libc/sys/t_write.c
+++ /dev/null
@@ -1,282 +0,0 @@
1/* $OpenBSD: t_write.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_write.c,v 1.7 2019/07/16 17:29:18 martin Exp $ */
3
4/*-
5 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/uio.h>
33#include <sys/mman.h>
34
35#include "atf-c.h"
36#include <errno.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <limits.h>
40#include <stdio.h>
41#include <stdint.h>
42#include <string.h>
43#include <unistd.h>
44#include <paths.h>
45
46static void sighandler(int);
47
48static bool fail = false;
49static const char *path = "write";
50
51static void
52sighandler(int signo __unused)
53{
54 fail = false;
55}
56
57ATF_TC_WITH_CLEANUP(write_err);
58ATF_TC_HEAD(write_err, tc)
59{
60 atf_tc_set_md_var(tc, "descr", "Checks errors from write(2)");
61}
62
63ATF_TC_BODY(write_err, tc)
64{
65 char rbuf[3] = { 'a', 'b', 'c' };
66 char wbuf[3] = { 'x', 'y', 'z' };
67 int fd;
68
69 errno = 0;
70 ATF_REQUIRE_ERRNO(EBADF, write(-1, wbuf, sizeof(wbuf)) == -1);
71
72 fd = open(path, O_RDWR | O_CREAT, 0600);
73
74 if (fd >= 0) {
75
76 errno = 0;
77 ATF_REQUIRE_ERRNO(0, write(fd, wbuf, 3) == 3);
78
79 errno = 0;
80 ATF_REQUIRE_ERRNO(EINVAL, write(fd, wbuf, SIZE_MAX) == -1);
81
82 errno = 0;
83 ATF_REQUIRE_ERRNO(EFAULT, write(fd, (void *)-1, 1) == -1);
84
85 /*
86 * Check that the above bogus write(2)
87 * calls did not corrupt the file.
88 */
89 ATF_REQUIRE(lseek(fd, 0, SEEK_SET) == 0);
90 ATF_REQUIRE(read(fd, rbuf, 3) == 3);
91 ATF_REQUIRE(memcmp(rbuf, wbuf, 3) == 0);
92
93 (void)close(fd);
94 (void)unlink(path);
95 }
96}
97
98ATF_TC_CLEANUP(write_err, tc)
99{
100 (void)unlink(path);
101}
102
103ATF_TC(write_pipe);
104ATF_TC_HEAD(write_pipe, tc)
105{
106 atf_tc_set_md_var(tc, "descr", "Checks for EPIPE from write(2)");
107}
108
109ATF_TC_BODY(write_pipe, tc)
110{
111 int fds[2];
112
113 ATF_REQUIRE(pipe(fds) == 0);
114 ATF_REQUIRE(signal(SIGPIPE, sighandler) == 0);
115
116 ATF_REQUIRE(write(fds[1], "x", 1) != -1);
117 ATF_REQUIRE(close(fds[0]) == 0);
118
119 errno = 0;
120 fail = true;
121
122 if (write(fds[1], "x", 1) != -1 || errno != EPIPE)
123 atf_tc_fail_nonfatal("expected EPIPE but write(2) succeeded");
124
125 ATF_REQUIRE(close(fds[1]) == 0);
126
127 if (fail != false)
128 atf_tc_fail_nonfatal("SIGPIPE was not raised");
129}
130
131ATF_TC_WITH_CLEANUP(write_pos);
132ATF_TC_HEAD(write_pos, tc)
133{
134 atf_tc_set_md_var(tc, "descr", "Checks that write(2) "
135 "updates the file position");
136}
137
138ATF_TC_BODY(write_pos, tc)
139{
140 const size_t n = 123;
141 size_t i;
142 int fd;
143
144 fd = open(path, O_RDWR | O_CREAT, 0600);
145 ATF_REQUIRE(fd >= 0);
146
147 for (i = 0; i < n; i++) {
148 ATF_REQUIRE(write(fd, "x", 1) == 1);
149 ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == (off_t)(i + 1));
150 }
151
152 ATF_REQUIRE(close(fd) == 0);
153 ATF_REQUIRE(unlink(path) == 0);
154}
155
156ATF_TC_CLEANUP(write_pos, tc)
157{
158 (void)unlink(path);
159}
160
161ATF_TC_WITH_CLEANUP(write_ret);
162ATF_TC_HEAD(write_ret, tc)
163{
164 atf_tc_set_md_var(tc, "descr", "Checks return values from write(2)");
165}
166
167ATF_TC_BODY(write_ret, tc)
168{
169 const size_t n = 99;
170 char buf[123];
171 size_t i, j;
172 int fd;
173
174 fd = open(path, O_WRONLY | O_CREAT, 0600);
175 ATF_REQUIRE(fd >= 0);
176
177 (void)memset(buf, 'x', sizeof(buf));
178
179 for (i = j = 0; i < n; i++)
180 j += write(fd, buf, sizeof(buf));
181
182 if (j != n * 123)
183 atf_tc_fail("inconsistent return values from write(2)");
184
185 (void)close(fd);
186 (void)unlink(path);
187}
188
189ATF_TC_CLEANUP(write_ret, tc)
190{
191 (void)unlink(path);
192}
193
194ATF_TC(writev_iovmax);
195ATF_TC_HEAD(writev_iovmax, tc)
196{
197 atf_tc_set_md_var(tc, "timeout", "10");
198 atf_tc_set_md_var(tc, "descr",
199 "Checks that file descriptor is properly FILE_UNUSE()d "
200 "when iovcnt is greater than IOV_MAX");
201}
202
203ATF_TC_BODY(writev_iovmax, tc)
204{
205 ssize_t retval;
206
207 (void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");
208
209 errno = 0;
210 retval = writev(2, NULL, IOV_MAX + 1);
211
212 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
213 ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
214}
215
216ATF_TC(write_fault);
217
218ATF_TC_HEAD(write_fault, tc)
219{
220 atf_tc_set_md_var(tc, "descr",
221 "Check that writing to non-permitted space returns EFAULT");
222}
223
224#define SIZE 8192
225
226ATF_TC_BODY(write_fault, tc)
227{
228 int fd[2];
229 ATF_REQUIRE(pipe(fd) != -1);
230 // Can't use /dev/null cause it doesn't access the buffer.
231
232 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
233 ATF_REQUIRE(map != MAP_FAILED);
234
235 ssize_t retval = write(fd[1], map, SIZE);
236
237 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
238 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
239
240 munmap(map, SIZE);
241 close(fd[0]);
242 close(fd[1]);
243}
244
245ATF_TC(read_fault);
246
247ATF_TC_HEAD(read_fault, tc)
248{
249 atf_tc_set_md_var(tc, "descr",
250 "Check that reading from non-permitted space returns EFAULT");
251}
252
253ATF_TC_BODY(read_fault, tc)
254{
255 int fd = open(_PATH_DEVZERO, O_RDONLY);
256 ATF_REQUIRE(fd != -1);
257
258 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
259 ATF_REQUIRE(map != MAP_FAILED);
260
261 ssize_t retval = read(fd, map, SIZE);
262
263 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
264 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
265
266 munmap(map, SIZE);
267 close(fd);
268}
269
270ATF_TP_ADD_TCS(tp)
271{
272
273 ATF_TP_ADD_TC(tp, write_err);
274 ATF_TP_ADD_TC(tp, write_pipe);
275 ATF_TP_ADD_TC(tp, write_pos);
276 ATF_TP_ADD_TC(tp, write_ret);
277 ATF_TP_ADD_TC(tp, writev_iovmax);
278 ATF_TP_ADD_TC(tp, write_fault);
279 ATF_TP_ADD_TC(tp, read_fault);
280
281 return atf_no_error();
282}