#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <libutil.h>
#include <paths.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct shared_info {
int failed;
char tag[64];
char message[0];
};
static int test = 1;
static void
ok(const char *descr)
{
printf("ok %d - %s\n", test, descr);
test++;
}
static void
fail(const char *descr, const char *fmt, ...)
{
va_list ap;
printf("not ok %d - %s", test, descr);
test++;
if (fmt) {
va_start(ap, fmt);
printf(" # ");
vprintf(fmt, ap);
va_end(ap);
}
printf("\n");
exit(1);
}
#define fail_err(descr) fail((descr), "%s", strerror(errno))
static void
cok(struct shared_info *info, const char *descr)
{
info->failed = 0;
strlcpy(info->tag, descr, sizeof(info->tag));
exit(0);
}
static void
cfail(struct shared_info *info, const char *descr, const char *fmt, ...)
{
va_list ap;
info->failed = 1;
strlcpy(info->tag, descr, sizeof(info->tag));
if (fmt) {
va_start(ap, fmt);
vsprintf(info->message, fmt, ap);
va_end(ap);
}
exit(0);
}
#define cfail_err(info, descr) cfail((info), (descr), "%s", strerror(errno))
static int
highest_fd(void)
{
struct kinfo_file *kif;
int cnt, i, highest;
kif = kinfo_getfile(getpid(), &cnt);
if (kif == NULL)
fail_err("kinfo_getfile");
highest = INT_MIN;
for (i = 0; i < cnt; i++)
if (kif[i].kf_fd > highest)
highest = kif[i].kf_fd;
free(kif);
return (highest);
}
static int
devnull(void)
{
int fd;
fd = open(_PATH_DEVNULL, O_RDONLY);
if (fd < 0)
fail_err("open(\" "_PATH_DEVNULL" \")");
return (fd);
}
int
main(void)
{
struct shared_info *info;
pid_t pid;
int fd, flags, i, start;
printf("1..22\n");
start = devnull();
if (start == -1)
fail("open", "bad descriptor %d", start);
ok("open");
fd = highest_fd();
if (start != fd)
fail("highest_fd", "bad descriptor %d != %d", start, fd);
ok("highest_fd");
closefrom(start + 1);
fd = highest_fd();
if (fd != start)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
for (i = 0; i < 16; i++)
(void)devnull();
fd = highest_fd();
if (fd != start + 16)
fail("open 16", "highest fd %d", fd);
ok("open 16");
closefrom(11);
fd = highest_fd();
if (fd != 10)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
if (close(6) < 0 || close(8) < 0)
fail_err("close2 ");
ok("close 2");
if (close(6) == 0)
fail("close(6)", "did not fail");
if (errno != EBADF)
fail_err("close(6)");
ok("close(6)");
if (close(8) == 0)
fail("close(8)", "did not fail");
if (errno != EBADF)
fail_err("close(8)");
ok("close(8)");
closefrom(4);
fd = highest_fd();
if (fd != 3)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
info = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANON |
MAP_SHARED, -1, 0);
if (info == MAP_FAILED)
fail_err("mmap");
ok("mmap");
pid = fork();
if (pid < 0)
fail_err("fork");
if (pid == 0) {
closefrom(0);
fd = highest_fd();
if (fd >= 0)
cfail(info, "closefrom(0)", "highest fd %d", fd);
cok(info, "closefrom(0)");
}
if (wait(NULL) < 0)
fail_err("wait");
if (info->failed)
fail(info->tag, "%s", info->message);
ok(info->tag);
pid = fork();
if (pid < 0)
fail_err("fork");
if (pid == 0) {
closefrom(-1);
fd = highest_fd();
if (fd >= 0)
cfail(info, "closefrom(-1)", "highest fd %d", fd);
cok(info, "closefrom(-1)");
}
if (wait(NULL) < 0)
fail_err("wait");
if (info->failed)
fail(info->tag, "%s", info->message);
ok(info->tag);
if (dup2(1, 6) < 0)
fail_err("dup2");
fd = highest_fd();
if (fd != 6)
fail("dup2", "highest fd %d", fd);
ok("dup2");
closefrom(4);
fd = highest_fd();
if (fd != 3)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
closefrom(32);
fd = highest_fd();
if (fd != 3)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
for (i = 0; i < 8; i++)
(void)devnull();
fd = highest_fd();
start = fd - 7;
close_range(start + 3, start + 5, 0);
for (i = start + 3; i < start + 6; ++i) {
if (close(i) == 0 || errno != EBADF) {
--i;
break;
}
}
if (i != start + 6)
fail("close_range", "failed to close at %d in %d - %d", i + 1,
start + 3, start + 6);
ok("close_range");
close_range(start + 4, start + 6, 0);
if ((i = highest_fd()) != fd)
fail("close_range", "highest fd %d", i);
ok("close_range");
close_range(start + 3, ~0L, 0);
if ((i = highest_fd()) != start + 2)
fail("close_range", "highest fd %d", i);
ok("close_range");
close_range(start, start + 4, 0);
fd = highest_fd();
if (fd != 3)
fail("close_range", "highest fd %d", fd);
ok("close_range");
pid = fork();
if (pid < 0)
fail_err("fork");
if (pid == 0) {
closefrom(0);
closefrom(0);
cok(info, "closefrom(0)");
}
if (wait(NULL) < 0)
fail_err("wait");
if (info->failed)
fail(info->tag, "%s", info->message);
ok(info->tag);
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)
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)
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)");
for (i = 0; i < 8; i++)
(void)devnull();
fd = highest_fd();
start = fd - 8;
if (close_range(start + 1, start + 4, CLOSE_RANGE_CLOFORK) < 0)
fail_err("close_range(..., CLOSE_RANGE_CLOFORK)");
flags = fcntl(start, F_GETFD);
if (flags < 0)
fail_err("fcntl(.., F_GETFD)");
if ((flags & FD_CLOFORK) != 0)
fail("close_range", "CLOSE_RANGE_CLOFORK 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)
fail_err("fcntl(.., F_GETFD)");
if ((flags & FD_CLOFORK) == 0)
fail("close_range", "CLOSE_RANGE_CLOFORK 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_CLOFORK) != 0)
fail("close_range", "CLOSE_RANGE_CLOFORK 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_CLOFORK)");
return (0);
}