FFFF minix/minix/tests/t40d.c at master · ccttctt/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
423 lines (355 loc) · 13.4 KB

t40d.c

File metadata and controls

423 lines (355 loc) · 13.4 KB
/* Clear sets and set up bit masks */
FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
FD_SET(fd_np2, &fds_read);
FD_SET(fd_np2, &fds_write);
tv.tv_sec = DO_TIMEOUT;
tv.tv_usec = 0;
/* Select for reading from an fd opened O_WRONLY. This should return
* immediately as it is not a meaningful operation [1] and is therefore not
* blocking. The select should return two file descriptors are ready (the
* failing read and valid write). */
retval = select(fd_np2+1, &fds_read, &fds_write, &fds_error, &tv);
/* Did we receive an error? */
if(retval <= 0) {
snprintf(errbuf, sizeof(errbuf),
"two fds should be set%s", (retval == 0 ? " (TIMEOUT)" : ""));
em(7, errbuf);
}
/* Make sure read bit is set (and just 1 bit) */
FD_ZERO(&fds_compare_read); FD_SET(fd_np2, &fds_compare_read);
if(!compare_fds(fd_np2+1, &fds_compare_read, &fds_read))
em(8, "read should be set");
/* Write bit should be set (and just 1 bit) */
FD_ZERO(&fds_compare_write); FD_SET(fd_np2, &fds_compare_write);
if(!compare_fds(fd_np2+1, &fds_compare_write, &fds_write))
em(9, "write should be set");
if(!empty_fds(fd_np2+1, &fds_error))
em(10, "Error should NOT be set");
FD_ZERO(&fds_read); FD_ZERO(&fds_write);
FD_SET(fd_np2, &fds_write);
tv.tv_sec = DO_TIMEOUT;
tv.tv_usec = 0;
retval = select(fd_np2+1, &fds_read, &fds_write, NULL, &tv);
/* Correct amount of ready file descriptors? Just 1 write */
if(retval != 1) {
snprintf(errbuf, sizeof(errbuf),
"one fd should be set%s", (retval == 0 ? " (TIMEOUT)" : ""));
em(11, errbuf);
}
if(!empty_fds(fd_np2+1, &fds_read)) em(12, "read should NOT be set");
/* Anonymous pipe */
/* Check if we can write to the pipe */
FD_ZERO(&fds_read); FD_ZERO(&fds_write);
FD_SET(fd_ap[1], &fds_write);
tv.tv_sec = DO_TIMEOUT;
tv.tv_usec = 0;
retval = select(fd_ap[1]+1, NULL, &fds_write, NULL, &tv);
/* Correct amount of ready file descriptors? Just 1 write */
if(retval != 1) {
snprintf(errbuf, sizeof(errbuf),
"one fd should be set%s", (retval == 0 ? " (TIMEOUT)" : ""));
em(13, errbuf);
}
/* Make sure write bit is set (and just 1 bit) */
FD_ZERO(&fds_compare_write); FD_SET(fd_ap[1], &fds_compare_write);
if(!compare_fds(fd_ap[1]+1, &fds_compare_write, &fds_write))
em(14, "write should be set");
/* Intentionally test reading from pipe and letting it time out. */
FD_SET(fd_ap[0], &fds_read);
tv.tv_sec = 1;
tv.tv_usec = 0;
start = time(NULL);
retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
end = time(NULL);
/* Did we time out? */
if(retval != 0) em(15, "we should have timed out");
/* Did it take us approximately 1 second? */
if((int) (end - start) != 1) {
snprintf(errbuf, sizeof(errbuf),
"time out is not 1 second (instead, it is %ld)",
(long int) (end - start));
em(16, errbuf);
}
/* Do another read, but this time we expect incoming data from child. */
FD_ZERO(&fds_read);
FD_SET(fd_ap[0], &fds_read);
tv.tv_sec = DO_TIMEOUT;
tv.tv_usec = 0;
retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
/* Correct amount of ready file descriptors? Just 1 read. */
if(retval != 1) em(17, "one fd should be set");
/* Is the read bit set? And just 1 bit. */
FD_ZERO(&fds_compare_read); FD_SET(fd_ap[0], &fds_compare_read);
if(!compare_fds(fd_ap[0]+1, &fds_compare_read, &fds_read))
em(18, "read should be set.");
/* By convention fd_ap[0] is meant to be used for reading from the pipe and
* fd_ap[1] is meant for writing, where fd_ap is a an anonymous pipe.
* However, it is unspecified what happens when fd_ap[0] is used for writing
* and fd_ap[1] for reading. (It is unsupported on Minix.) As such, it is not
* necessary to make test cases for wrong pipe file descriptors using select.
*/
waitpid(child, &retval, 0);
unlink(NAMEDPIPE2);
unlink(NAMEDPIPE1);
exit(errct);
}
int main(int argc, char **argv) {
int forkres;
/* Get subtest number */
if(argc != 2) {
printf("Usage: %s subtest_no\n", argv[0]);
exit(-2);
} else if(sscanf(argv[1], "%d", &subtest) != 1) {
printf("Usage: %s subtest_no\n", argv[0]);
exit(-2);
}
/* Set up anonymous pipe */
if(pipe(fd_ap) < 0) {
perror("Could not create anonymous pipe");
exit(-1);
}
/* Create named pipe2. It is unlinked by do_parent. */
if(mkfifo(NAMEDPIPE1, 0600) < 0) {
printf("Could not create named pipe %s", NAMEDPIPE1);
perror(NULL);
exit(-1);
}
if(mkfifo(NAMEDPIPE2, 0600) < 0) {
printf("Could not create named pipe %s", NAMEDPIPE2);
perror(NULL);
exit(-1);
}
forkres = fork();
if(forkres == 0) do_child();
else if(forkres > 0) do_parent(forkres);
else { /* Fork failed */
perror("Unable to fork");
exit(-1);
}
exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/
}
0