2009-07-14 11:43:33 +02:00
|
|
|
/* t40b.c
|
|
|
|
*
|
|
|
|
* Test regular files
|
|
|
|
*
|
|
|
|
* Select works on regular files, (pseudo) terminal devices, streams-based
|
|
|
|
* files, FIFOs, pipes, and sockets. This test verifies selecting for regular
|
|
|
|
* file descriptors. "File descriptors associated with regular files shall
|
|
|
|
* always select true for ready to read, ready to write, and error conditions"
|
|
|
|
* - Open Group. Although we set a timeout, the select should return
|
|
|
|
* immediately.
|
|
|
|
*
|
|
|
|
* This test is part of a bigger select test. It expects as argument which sub-
|
|
|
|
* test it is.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2013-04-16 18:04:46 +02:00
|
|
|
#include "common.h"
|
|
|
|
|
2011-04-21 15:18:00 +02:00
|
|
|
#define FILE1 "selecttestb-1"
|
2009-07-14 11:43:33 +02:00
|
|
|
#define FILES 2
|
|
|
|
#define TIME 3
|
|
|
|
|
|
|
|
#define MAX_ERROR 10
|
|
|
|
|
|
|
|
char errorbuf[1000];
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int fd1, fd2, retval;
|
|
|
|
fd_set fds_read, fds_write, fds_error;
|
|
|
|
struct timeval tv;
|
|
|
|
time_t start, end;
|
|
|
|
|
|
|
|
/* Get subtest number */
|
|
|
|
if(argc != 2) {
|
|
|
|
printf("Usage: %s subtest_no\n", argv[0]);
|
|
|
|
exit(-1);
|
|
|
|
} else if(sscanf(argv[1], "%d", &subtest) != 1) {
|
|
|
|
printf("Usage: %s subtest_no\n", argv[0]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set timeout */
|
|
|
|
tv.tv_sec = TIME;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
/* Open a file for writing */
|
|
|
|
if((fd1 = open(FILE1, O_WRONLY|O_CREAT, 0644)) == -1) {
|
|
|
|
snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for writing",
|
|
|
|
FILE1);
|
2013-04-16 18:04:46 +02:00
|
|
|
em(1, errorbuf);
|
2009-07-14 11:43:33 +02:00
|
|
|
perror(NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the same file for reading */
|
|
|
|
if((fd2 = open(FILE1, O_RDONLY)) == -1) {
|
|
|
|
snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for reading",
|
|
|
|
FILE1);
|
2013-04-16 18:04:46 +02:00
|
|
|
em(2, errorbuf);
|
2009-07-14 11:43:33 +02:00
|
|
|
perror(NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear file descriptor bit masks */
|
|
|
|
FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
|
|
|
|
|
|
|
|
/* Fill bit mask */
|
|
|
|
FD_SET(fd1, &fds_write);
|
|
|
|
FD_SET(fd2, &fds_read);
|
|
|
|
FD_SET(fd1, &fds_error);
|
|
|
|
FD_SET(fd2, &fds_error);
|
|
|
|
|
|
|
|
/* Do the select and time how long it takes */
|
|
|
|
start = time(NULL);
|
|
|
|
retval = select(fd2+1, &fds_read, &fds_write, &fds_error, &tv);
|
|
|
|
end = time(NULL);
|
|
|
|
|
|
|
|
/* Correct amount of ready file descriptors? 1 read + 1 write + 2 errors */
|
|
|
|
if(retval != 4) {
|
2013-04-16 18:04:46 +02:00
|
|
|
em(3, "four fds should be set");
|
2009-07-14 11:43:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test resulting bit masks */
|
2013-04-16 18:04:46 +02:00
|
|
|
if(!FD_ISSET(fd1, &fds_write)) em(4, "write should be set");
|
|
|
|
if(!FD_ISSET(fd2, &fds_read)) em(5, "read should be set");
|
|
|
|
if(!FD_ISSET(fd1, &fds_error)) em(6, "error should be set");
|
|
|
|
if(!FD_ISSET(fd2, &fds_error)) em(7, "error should be set");
|
2009-07-14 11:43:33 +02:00
|
|
|
|
|
|
|
/* Was it instantaneous? */
|
|
|
|
if(end-start != TIME - TIME) {
|
|
|
|
snprintf(errorbuf,sizeof(errorbuf),"time spent blocking is not %d, but %ld",
|
|
|
|
TIME - TIME, (long int) (end-start));
|
2013-04-16 18:04:46 +02:00
|
|
|
em(8, errorbuf);
|
2009-07-14 11:43:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for read to become ready on O_WRONLY. This should fail immediately. */
|
|
|
|
FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
|
|
|
|
FD_SET(fd1, &fds_read);
|
|
|
|
FD_SET(fd1, &fds_error);
|
|
|
|
FD_SET(fd2, &fds_error);
|
|
|
|
tv.tv_sec = TIME;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
retval = select(fd2+1, &fds_read, NULL, &fds_error, &tv);
|
|
|
|
|
|
|
|
/* Correct amount of ready file descriptors? 1 read + 2 error */
|
2013-04-16 18:04:46 +02:00
|
|
|
if(retval != 3) em(9, "incorrect amount of ready file descriptors");
|
|
|
|
if(!FD_ISSET(fd1, &fds_read)) em(10, "read should be set");
|
|
|
|
if(!FD_ISSET(fd1, &fds_error)) em(11, "error should be set");
|
|
|
|
if(!FD_ISSET(fd2, &fds_error)) em(12, "error should be set");
|
2009-07-14 11:43:33 +02:00
|
|
|
|
|
|
|
/* Try again as above, bit this time with O_RDONLY in the write set */
|
|
|
|
FD_ZERO(&fds_error);
|
|
|
|
FD_SET(fd2, &fds_write);
|
|
|
|
FD_SET(fd1, &fds_error);
|
|
|
|
FD_SET(fd2, &fds_error);
|
|
|
|
tv.tv_sec = TIME;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
retval = select(fd2+1, NULL, &fds_write, &fds_error, &tv);
|
|
|
|
|
|
|
|
/* Correct amount of ready file descriptors? 1 write + 2 errors */
|
2013-04-16 18:04:46 +02:00
|
|
|
if(retval != 3) em(13, "incorrect amount of ready file descriptors");
|
|
|
|
if(!FD_ISSET(fd2, &fds_write)) em(14, "write should be set");
|
|
|
|
if(!FD_ISSET(fd1, &fds_error)) em(15, "error should be set");
|
|
|
|
if(!FD_ISSET(fd2, &fds_error)) em(16, "error should be set");
|
2009-07-14 11:43:33 +02:00
|
|
|
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
|
|
|
unlink(FILE1);
|
|
|
|
|
|
|
|
exit(errct);
|
|
|
|
}
|