test67: test opening files with O_CLOEXEC

Change-Id: If71e61f830c6d3b5154368e189baa4f70bd73850
This commit is contained in:
Thomas Veerman 2013-02-26 11:30:44 +00:00
parent a5a4f7183f
commit c3f77a1f88
4 changed files with 199 additions and 3 deletions

View File

@ -23,11 +23,11 @@ OBJS.test57=test57loop.o
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
61 62 64 65 66
61 62 64 65 66 67
PROG+= test$(t)
.endfor
PROG+= t10a t11a t11b t40a t40b t40c t40d t40e t40f t60a t60b
PROG+= t10a t11a t11b t40a t40b t40c t40d t40e t40f t60a t60b t67a t67b
.include <bsd.own.mk>
@ -53,5 +53,5 @@ clean: .PHONY .MAKE
$(MAKE) -C select clean
rm -rf *.o *.s *.bak test? test?? t10a t11a t11b \
t40a t40b t40c t40d t40e t40f \
t60a t60b \
t60a t60b t67a t67b \
DIR*

33
test/t67a.c Normal file
View File

@ -0,0 +1,33 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int fd, fd_parent;
char buf[1];
if (argc != 2) {
return 1;
}
fd_parent = atoi(argv[1]);
/* If we open a new file, the fd we obtain should be fd_parent + 1 */
fd = open("open_plusplus_fd", O_CREAT|O_RDWR);
if (fd != fd_parent + 1) {
return 2;
}
/* Also, writing to fd_parent should succeed */
if (write(fd_parent, buf, sizeof(buf)) <= 0) {
return 3;
}
return 0;
}

35
test/t67b.c Normal file
View File

@ -0,0 +1,35 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int fd, fd_parent;
char buf[1];
if (argc != 2) {
return 1;
}
fd_parent = atoi(argv[1]);
/* Writing to fd_parent should fail as it has to be closed at this
* point */
if (write(fd_parent, buf, sizeof(buf)) != -1) {
return 2;
}
/* If we open a new file, the fd we obtain should be identical to
* fd_parent */
fd = open("open_identical_fd", O_CREAT|O_RDWR);
if (fd != fd_parent) {
return 3;
}
return 0;
}

128
test/test67.c Normal file
View File

@ -0,0 +1,128 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_ERROR 5
#include "common.c"
static int fd = 0;
void copy_subtests(void);
void test_open_cloexec(void);
void test_open_fork(void);
void
copy_subtests()
{
char *subtests[] = { "t67a", "t67b" };
char copy_cmd[8 + PATH_MAX + 1];
int i, no_tests;
no_tests = sizeof(subtests) / sizeof(char *);
for (i = 0; i < no_tests; i++) {
snprintf(copy_cmd, 8 + PATH_MAX, "cp ../%s .", subtests[i]);
system(copy_cmd);
}
}
void
test_open_cloexec()
{
int flags;
pid_t pid;
/* Let's create a file with O_CLOEXEC turned on */
fd = open("file", O_RDWR|O_CREAT|O_CLOEXEC);
if (fd < 0) e(1);
/* Now verify through fcntl the flag is indeed set */
flags = fcntl(fd, F_GETFD);
if (flags < 0) e(2);
if (!(flags & FD_CLOEXEC)) e(3);
/* Fork a child and let child exec a test program that verifies
* fd is not a valid file */
pid = fork();
if (pid == -1) e(4);
else if (pid == 0) {
/* We're the child */
char fd_buf[2];
/* Verify again O_CLOEXEC is on */
flags = fcntl(fd, F_GETFD);
if (flags < 0) e(5);
if (!(flags & FD_CLOEXEC)) e(6);
snprintf(fd_buf, sizeof(fd_buf), "%d", fd);
execl("./t67b", "t67b", fd_buf, NULL);
/* Should not reach this */
exit(1);
} else {
/* We're the parent */
int result;
if (waitpid(pid, &result, 0) == -1) e(7);
if (WEXITSTATUS(result) != 0) e(8);
}
close(fd);
}
void
test_open_fork()
{
int flags;
pid_t pid;
/* Let's create a file with O_CLOEXEC NOT turned on */
fd = open("file", O_RDWR|O_CREAT);
if (fd < 0) e(1);
/* Now verify through fcntl the flag is indeed not set */
flags = fcntl(fd, F_GETFD);
if (flags < 0) e(2);
if (flags & FD_CLOEXEC) e(3);
/* Fork a child and let child exec a test program that verifies
* fd is a valid file */
pid = fork();
if (pid == -1) e(4);
else if (pid == 0) {
/* We're the child */
char fd_buf[2];
/* Verify again O_CLOEXEC is off */
flags = fcntl(fd, F_GETFD);
if (flags < 0) e(5);
if (flags & FD_CLOEXEC) e(6);
snprintf(fd_buf, sizeof(fd_buf), "%d", fd);
execl("./t67a", "t67a", fd_buf, NULL);
/* Should not reach this */
exit(1);
} else {
/* We're the parent */
int result = 0;
if (waitpid(pid, &result, 0) == -1) e(7);
if (WEXITSTATUS(result) != 0) e(8);
}
close(fd);
}
int
main(int argc, char *argv[])
{
start(67);
copy_subtests();
test_open_fork();
test_open_cloexec();
quit();
return(-1); /* Unreachable */
}