Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/file/closefrom_test.c
Show First 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | |||||
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, i, start; | ||||
printf("1..15\n"); | printf("1..19\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"); | ||||
/* Make sure highest_fd() works. */ | /* Make sure highest_fd() works. */ | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | main(void) | ||||
ok("closefrom"); | ok("closefrom"); | ||||
/* Do a closefrom() beyond our highest open fd. */ | /* Do a closefrom() beyond our highest open fd. */ | ||||
closefrom(32); | closefrom(32); | ||||
fd = highest_fd(); | fd = highest_fd(); | ||||
if (fd != 3) | if (fd != 3) | ||||
fail("closefrom", "highest fd %d", fd); | fail("closefrom", "highest fd %d", fd); | ||||
ok("closefrom"); | ok("closefrom"); | ||||
/* Chew up another 8 fd */ | |||||
for (i = 0; i < 8; i++) | |||||
(void)devnull(); | |||||
fd = highest_fd(); | |||||
start = fd - 7; | |||||
/* close_range() a hole in the middle */ | |||||
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 from the middle of the hole */ | |||||
close_range(start + 4, start + 6, 0); | |||||
if ((i = highest_fd()) != fd) | |||||
fail("close_range", "highest fd %d", i); | |||||
ok("close_range"); | |||||
/* close_range to the end; effectively closefrom(2) */ | |||||
close_range(start + 3, ~0L, 0); | |||||
if ((i = highest_fd()) != start + 2) | |||||
fail("close_range", "highest fd %d", i); | |||||
ok("close_range"); | |||||
/* Now close the rest */ | |||||
close_range(start, start + 4, 0); | |||||
fd = highest_fd(); | |||||
if (fd != 3) | |||||
fail("close_range", "highest fd %d", fd); | |||||
ok("close_range"); | |||||
return (0); | return (0); | ||||
} | } |