10000 Adapt oclo tests to FreeBSD · freebsd/freebsd-src@6102aa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6102aa1

Browse files
Adapt oclo tests to FreeBSD
1 parent b73fdc7 commit 6102aa1

File tree

4 files changed

+129
-80
lines changed

4 files changed

+129
-80
lines changed

tests/oclo/Makefile

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
1-
#
2-
# This file and its contents are supplied under the terms of the
3-
# Common Development and Distribution License ("CDDL"), version 1.0.
4-
# You may only use this file in accordance with the terms of version
5-
# 1.0 of the CDDL.
6-
#
7-
# A full copy of the text of the CDDL should have accompanied this
8-
# source. A copy of the CDDL is also available via the Internet at
9-
# http://www.illumos.org/license/CDDL.
10-
#
1+
PROGS= oclo oclo_errors ocloexec_verify
112

12-
#
13-
# Copyright 2025 Oxide Computer Company
14-
#
3+
_ALLOW_ABSOLUTE_OBJ_PATH=1
154

16-
PROGS = \
17-
oclo \
18-
oclo_errors \
19-
ocloexec_verify
5+
SRCS.oclo= oclo.c
6+
SRCS.oclo+= /usr/src/crypto/openssh/openbsd-compat/recallocarray.c
7+
CFLAGS.oclo+= -I/usr/src/crypto/openssh
8+
SRCS.oclo_errors= oclo_errors.c
9+
LDADD.ocloexec_verify+= -lutil
10+
SRCS.ocloexec_verify= ocloexec_verify.c
2011

21-
ROOTOPTDIR = $(ROOT)/opt/os-tests/tests
22-
ROOTOPTOCLO = $(ROOTOPTDIR)/oclo
23-
ROOTOPTPROGS = $(PROGS:%=$(ROOTOPTOCLO)/%)
12+
MAN=
2413

25-
include $(SRC)/cmd/Makefile.cmd
26-
include $(SRC)/cmd/Makefile.cmd.64
27-
include $(SRC)/cmd/Makefile.ctf
14+
test: ${PROGS}
15+
${.CURDIR}/oclo
2816

29-
CSTD = $(CSTD_GNU17)
30-
CTF_MODE = link
31-
CPPFLAGS += -D_XOPEN_SOURCE=800 -D__EXTENSIONS__
32-
33-
oclo := LDLIBS += -lsocket
34-
oclo_errors := LDLIBS += -lsocket
35-
36-
.KEEP_STATE:
37-
38-
all: $(PROGS)
39-
40-
install: $(ROOTOPTPROGS)
41-
42-
clean:
43-
$(RM) *.o
44-
45-
$(ROOTOPTPROGS): $(PROGS) $(ROOTOPTOCLO)
46-
47-
$(ROOTOPTDIR):
48-
$(INS.dir)
49-
50-
$(ROOTOPTOCLO): $(ROOTOPTDIR)
51-
$(INS.dir)
52-
53-
$(ROOTOPTOCLO)/%: %
54-
$(INS.file)
55-
56-
clobber: clean
57-
$(RM) $(PROGS)
58-
59-
FRC:
17+
.include <bsd.progs.mk>

tests/oclo/oclo.c

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,63 @@
4141
* program that will verify everything is correctly honored after an exec.
4242
*/
4343

44+
#include <sys/param.h>
45+
46+
#include <netinet/in.h>
47+
#include <sys/socket.h>
48+
49+
#include <stdio.h>
50+
#include <stdint.h>
4451
#include <stdlib.h>
4552
#include <unistd.h>
4653
#include <stdbool.h>
4754
#include <err.h>
48-
#include <sys/types.h>
4955
#include <sys/stat.h>
5056
#include <fcntl.h>
51-
#include <sys/sysmacros.h>
52-
#include <sys/fork.h>
53-
#include <wait.h>
57+
#include <sys/wait.h>
5458
#include <errno.h>
5559
#include <string.h>
5660
#include <limits.h>
5761
#include <libgen.h>
58-
#include <sys/socket.h>
62+
#include <fcntl.h>
63+
#include <signal.h>
64+
65+
void *recallocarray(void *, size_t, size_t, size_t);
66+
67+
#define strerrorname_np(e) (sys_errlist[e])
68+
69+
#ifndef F_DUP3FD
70+
#define F_DUP3FD 777
71+
#endif
72+
73+
#ifndef FORK_NOSIGCHLD
74+
#define FORK_NOSIGCHLD 0x01
75+
#endif
76+
77+
#ifndef FORK_WAITPID
78+
#define FORK_WAITPID 0x02
79+
#endif
80+
81+
pid_t
82+
forkx(int flags)
83+
{
84+
int rfork_flags = RFPROC | RFFDG;
85+
pid_t pid;
86+
87+
if ((flags & ~(FORK_NOSIGCHLD | FORK_WAITPID)) != 0) {
88+
errno = EINVAL;
89+
return (-1);
90+
}
91+
92+
if ((flags & FORK_WAITPID) == 0)
93+
rfork_flags |= RFNOWAIT;
94+
95+
pid = rfork(rfork_flags);
96+
if (pid == -1)
97+
return (-1);
98+
99+
return (pid);
100+
}
59101

60102
/*
61103
* Verification program name.
@@ -253,6 +295,25 @@ oclo_fdup_common(const clo_create_t *c, int targ_flags, int cmd)
253295
{
254296
int dup, fd;
255297

298+
if (cmd == F_DUP3FD) {
299+
switch (targ_flags & (O_CLOEXEC | O_CLOFORK)) {
300+
case O_CLOEXEC | O_CLOFORK:
301+
cmd = F_DUP2FD_CLOBOTH;
302+
break;
303+
case O_CLOEXEC:
304+
cmd = F_DUP2FD_CLOEXEC;
305+
break;
306+
case O_CLOFORK:
307+
cmd = F_DUP2FD_CLOFORK;
308+
break;
309+
case 0:
310+
cmd = F_DUP2FD;
311+
break;
312+
default:
313+
errx(1, "Invalid cmd: %d", cmd);
314+
}
315+
}
316+
256317
fd = oclo_file(c);
257318
oclo_record(c, fd, c->clo_flags, "base");
258319
switch (cmd) {
@@ -264,11 +325,14 @@ oclo_fdup_common(const clo_create_t *c, int targ_flags, int cmd)
264325
case F_DUP2FD:
265326
case F_DUP2FD_CLOEXEC:
266327
case F_DUP2FD_CLOFORK:
328+
case F_DUP2FD_CLOBOTH:
267329
dup = fcntl(fd, cmd, fd + 1);
268330
break;
331+
#if 0
269332
case F_DUP3FD:
270333
dup = fcntl(fd, cmd, fd + 1, targ_flags);
271334
break;
335+
#endif
272336
default:
273337
errx(EXIT_FAILURE, "TEST FAILURE: %s: internal error: "
274338
"unexpected fcntl cmd: 0x%x", c->clo_desc, cmd);
@@ -327,13 +391,13 @@ oclo_fdup3fd_none(const clo_create_t *c)
327391
static void
328392
oclo_fdup3fd_exec(const clo_create_t *c)
329393
{
330-
oclo_fdup_common(c, FD_CLOEXEC, F_DUP3FD);
394+
oclo_fdup_common(c, FD_CLOEXEC, F_DUPFD_CLOEXEC);
331395
}
332396

333397
static void
334398
oclo_fdup3fd_fork(const clo_create_t *c)
335399
{
336-
oclo_fdup_common(c, FD_CLOFORK, F_DUP3FD);
400+
oclo_fdup_common(c, FD_CLOFORK, F_DUPFD_CLOFORK);
337401
}
338402

339403
static void
@@ -600,7 +664,7 @@ oclo_rights_common(const clo_create_t *c, int targ_flags)
600664

601665
if (msg.msg_controllen < CMSG_SPACE(sizeof (int))) {
602666
errx(EXIT_FAILURE, "TEST FAILED: %s: found insufficient "
603-
"message control length: expected at least 0x%x, found "
667+
"message control length: expected at least 0x%lx, found "
604668
"0x%x", c->clo_desc, CMSG_SPACE(sizeof (int)),
605669
msg.msg_controllen);
606670
}
@@ -855,6 +919,7 @@ static const clo_create_t oclo_create[] = { {
855919
.clo_flags = FD_CLOEXEC | FD_CLOFORK,
856920
.clo_func = oclo_fdup3fd_exec
857921
}, {
922+
#if 0
858923
.clo_desc = "fcntl(F_DUP3FD) none->FD_CLOFORK|FD_CLOEXEC",
859924
.clo_flags = 0,
860925
.clo_func = oclo_fdup3fd_both
@@ -872,6 +937,7 @@ static const clo_create_t oclo_create[] = { {
872937
.clo_flags = FD_CLOEXEC | FD_CLOFORK,
873938
.clo_func = oclo_fdup3fd_both
874939
}, {
940+
#endif
875941
.clo_desc = "fcntl(F_DUP3FD) none->FD_CLOFORK",
876942
.clo_flags = 0,
877943
.clo_func = oclo_fdup3fd_fork
@@ -1208,15 +1274,15 @@ oclo_exec(void)
12081274
char dir[PATH_MAX], file[PATH_MAX];
12091275
char **argv;
12101276

1211-
ret = readlink("/proc/self/path/a.out", dir, sizeof (dir));
1277+
ret = readlink("/proc/curproc/exe", dir, sizeof (dir));
12121278
if (ret < 0) {
12131279
err(EXIT_FAILURE, "TEST FAILED: failed to read our a.out path "
12141280
"from /proc");
12151281
} else if (ret == 0) {
1216-
errx(EXIT_FAILURE, "TEST FAILED: reading /proc/self/path/a.out "
1282+
errx(EXIT_FAILURE, "TEST FAILED: reading /proc/curproc/exe "
12171283
"returned 0 bytes");
12181284
} else if (ret == sizeof (dir)) {
1219-
errx(EXIT_FAILURE, "TEST FAILED: Using /proc/self/path/a.out "
1285+
errx(EXIT_FAILURE, "TEST FAILED: Using /proc/curproc/exe "
12201286
"requires truncation");
12211287
}
12221288

@@ -1262,7 +1328,7 @@ main(void)
12621328
* Treat failure during this set up phase as a hard failure. There's no
12631329
* reason to continue if we can't successfully create the FDs we expect.
12641330
*/
1265-
for (size_t i = 0; i < ARRAY_SIZE(oclo_create); i++) {
1331+
for (size_t i = 0; i < nitems(oclo_create); i++) {
12661332
oclo_create[i].clo_func(&oclo_create[i]);
12671333
}
12681334

tests/oclo/oclo_errors.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@
2424
* o accept4()
2525
*/
2626

27+
#include <netinet/in.h>
28+
#include <sys/socket.h>
29+
#include <stdint.h>
2730
#include <stdlib.h>
2831
#include <err.h>
2932
#include <stdio.h>
3033
#include <unistd.h>
31-
#include <sys/stdbool.h>
34+
#include <stdbool.h>
3235
#include <errno.h>
3336
#include <string.h>
3437
#include <fcntl.h>
3538
#include <limits.h>
36-
#include <sys/socket.h>
39+
40+
#define strerrorname_np(e) (sys_errlist[e])
3741

3842
static bool
3943
oclo_check(const char *desc, const char *act, int ret, int e)
@@ -60,12 +64,14 @@ oclo_dup3(const char *desc, int flags)
6064
return (oclo_check(desc, "duplicated", fd, errno));
6165
}
6266

67+
#ifdef F_DUP3FD
6368
static bool
6469
oclo_dup3fd(const char *desc, int flags)
6570
{
6671
int fd = fcntl(STDERR_FILENO, F_DUP3FD, 23, flags);
6772
return (oclo_check(desc, "duplicated", fd, errno));
6873
}
74+
#endif
6975

7076

7177
static bool
@@ -139,6 +145,7 @@ main(void)
139145
ret = EXIT_FAILURE;
140146
}
141147

148+
#ifdef FDUP3FD
142149
if (!oclo_dup3fd("fcntl(FDUP3FD): 0x7777", 0x7777)) {
143150
ret = EXIT_FAILURE;
144151
}
@@ -151,6 +158,7 @@ main(void)
151158
if (!oclo_dup3fd("fcntl(FDUP3FD): INT_MAX", INT_MAX)) {
152159
ret = EXIT_FAILURE;
153160
}
161+
#endif
154162

155163

156164
if (!oclo_pipe2("pipe2(): O_RDWR", O_RDWR)) {

tests/oclo/ocloexec_verify.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,35 @@
2323
*/
2424

2525
#include <err.h>
26+
#include <errno.h>
27+
#include <stdio.h>
2628
#include <stdlib.h>
2729
#include <unistd.h>
2830
#include <fcntl.h>
2931
#include <stdbool.h>
3032
#include <errno.h>
3133
#include <string.h>
34+
#include <sys/user.h>
35+
#include <libutil.h>
36+
37+
#define strerrorname_np(e) (sys_errlist[e])
3238

3339
static int
34-
verify_fdwalk_cb(void *arg, int fd)
40+
getmaxfd(void)
3541
{
36-
int *max = arg;
37-
*max = fd;
38-
return (0);
42+
struct kinfo_file *files;
43+
int i, cnt, max;
44+
45+
if ((files = kinfo_getfile(getpid(), &cnt)) == NULL)
46+
err(1, "kinfo_getfile");
47+
48+
max = -1;
49+
for (i = 0; i < cnt; i++)
50+
if (files[i].kf_fd > max)
51+
max = files[i].kf_fd;
52+
53+
free(files);
54+
return (max);
3955
}
4056

4157
static bool
@@ -88,7 +104,7 @@ verify_flags(int fd, int exp_flags)
88104
int
89105
main(int argc, char *argv[])
90106
{
91-
int maxfd = STDIN_FILENO;
107+
int maxfd;
92108
int ret = EXIT_SUCCESS;
93109

94110
/*
@@ -97,24 +113,25 @@ main(int argc, char *argv[])
97113
* program name, which we want to skip. Note, the last fd may not exist
98114
* because it was marked for close, hence the use of '>' below.
99115
*/
100-
(void) fdwalk(verify_fdwalk_cb, &maxfd);
116+
maxfd = getmaxfd();
101117
if (maxfd - 3 > argc - 1) {
102118
errx(EXIT_FAILURE, "TEST FAILED: found more fds %d than "
103119
"arguments %d", maxfd - 3, argc - 1);
104120
}
105121

106122
for (int i = 1; i < argc; i++) {
107-
const char *errstr;
123+
char *endptr;
108124
int targ_fd = i + STDERR_FILENO;
109-
long long targ_flags = strtonumx(argv[i], 0,
110-
FD_CLOEXEC | FD_CLOFORK, &errstr, 0);
125+
errno = 0;
126+
long long val = strtoll(argv[i], &endptr, 0);
111127

112-
if (errstr != NULL) {
128+
if (errno != 0 || *endptr != '\0' ||
129+
(val < 0 || val > (FD_CLOEXEC | FD_CLOFORK))) {
113130
errx(EXIT_FAILURE, "TEST FAILED: failed to parse "
114-
"argument %d: %s is %s", i, argv[i], errstr);
131+
"argument %d: %s", i, argv[i]);
115132
}
116133

117-
if (!verify_flags(targ_fd, (int)targ_flags))
134+
if (!verify_flags(targ_fd, (int)val))
118135
ret = EXIT_FAILURE;
119136
}
120137

0 commit comments

Comments
 (0)
0