Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/file/closefrom_test.c
Show First 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | devnull(void) | ||||
return (fd); | return (fd); | ||||
} | } | ||||
int | int | ||||
main(void) | main(void) | ||||
{ | { | ||||
struct shared_info *info; | struct shared_info *info; | ||||
pid_t pid; | pid_t pid; | ||||
int fd, i, start; | int fd, flags, i, start; | ||||
printf("1..20\n"); | printf("1..20\n"); | ||||
/* We better start up with fd's 0, 1, and 2 open. */ | /* We better start up with fd's 0, 1, and 2 open. */ | ||||
start = devnull(); | start = devnull(); | ||||
if (start == -1) | if (start == -1) | ||||
fail("open", "bad descriptor %d", start); | fail("open", "bad descriptor %d", start); | ||||
ok("open"); | ok("open"); | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | if (pid == 0) { | ||||
closefrom(0); | closefrom(0); | ||||
cok(info, "closefrom(0)"); | cok(info, "closefrom(0)"); | ||||
} | } | ||||
if (wait(NULL) < 0) | if (wait(NULL) < 0) | ||||
fail_err("wait"); | fail_err("wait"); | ||||
if (info->failed) | if (info->failed) | ||||
fail(info->tag, "%s", info->message); | fail(info->tag, "%s", info->message); | ||||
ok(info->tag); | ok(info->tag); | ||||
/* test CLOSE_RANGE_CLOEXEC */ | |||||
for (i = 0; i < 8; i++) | |||||
(void)devnull(); | |||||
fd = highest_fd(); | |||||
start = fd - 8; | |||||
if (close_range(start + 1, start + 4, CLOSE_RANGE_CLOEXEC) < 0) | |||||
markj: I'd check for an error from close_range(), though I see that the pre-existing tests do not for… | |||||
fail_err("close_range(..., CLOSE_RANGE_CLOEXEC)"); | |||||
flags = fcntl(start, F_GETFD); | |||||
if (flags < 0) | |||||
fail_err("fcntl(.., F_GETFD)"); | |||||
if ((flags & FD_CLOEXEC) != 0) | |||||
fail("close_range", "CLOSE_RANGE_CLOEXEC set close-on-exec " | |||||
"when it should not have on fd %d", start); | |||||
for (i = start + 1; i <= start + 4; i++) { | |||||
flags = fcntl(i, F_GETFD); | |||||
if (flags < 0) | |||||
Not Done Inline ActionsThis test will pass if fcntl() fails. It'd be more rigourous to check for failure before testing FD_CLOEXEC. markj: This test will pass if fcntl() fails. It'd be more rigourous to check for failure before… | |||||
fail_err("fcntl(.., F_GETFD)"); | |||||
if ((flags & FD_CLOEXEC) == 0) | |||||
fail("close_range", "CLOSE_RANGE_CLOEXEC did not set " | |||||
"close-on-exec on fd %d", i); | |||||
} | |||||
for (; i < start + 8; i++) { | |||||
flags = fcntl(i, F_GETFD); | |||||
if (flags < 0) | |||||
fail_err("fcntl(.., F_GETFD)"); | |||||
if ((flags & FD_CLOEXEC) != 0) | |||||
fail("close_range", "CLOSE_RANGE_CLOEXEC set close-on-exec " | |||||
"when it should not have on fd %d", i); | |||||
} | |||||
if (close_range(start, start + 8, 0) < 0) | |||||
fail_err("close_range"); | |||||
ok("close_range(..., CLOSE_RANGE_CLOEXEC)"); | |||||
return (0); | return (0); | ||||
} | } |
I'd check for an error from close_range(), though I see that the pre-existing tests do not for some reason.