Repository navigation
Expand file tree
/
Copy patht40c.c
More file actions
148 lines (121 loc) · 4.14 KB
/
Copy patht40c.c
File metadata and controls
148 lines (121 loc) · 4.14 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* t40c.c
*
* Test (pseudo) terminal devices
*
* Select works on regular files, (pseudo) terminal devices, streams-based
* files, FIFOs, pipes, and sockets. This test verifies selecting for (pseudo)
* terminal devices.
*
* 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 <sys/wait.h>
#include <string.h>
#define TERMINALW "/dev/ttyp0"
#define TERMINALR "/dev/ptyp0"
#define SENDSTRING "minixrocks"
#define MAX_ERROR 5
int errct = 0, subtest = -1;
void e(int n, char *s) {
printf("Subtest %d, error %d, %s\n", subtest, n, s);
if (errct++ > MAX_ERROR) {
printf("Too many errors; test aborted\n");
exit(errct);
}
}
int do_child(void) {
int fd, retval;
struct timeval tv;
/* Opening master terminal for writing */
if((fd = open(TERMINALW, O_WRONLY)) == -1) {
printf("Error opening %s for writing, signalling parent to quit\n",
TERMINALW);
perror(NULL);
printf("Please make sure that %s is not in use while running this test\n",
TERMINALW);
exit(-1);
}
/* Going to sleep for two seconds to allow the parent proc to get ready */
tv.tv_sec = 2;
tv.tv_usec = 0;
select(0, NULL, NULL, NULL, &tv);
/* Try to write. Doesn't matter how many bytes we actually send. */
retval = write(fd, SENDSTRING, strlen(SENDSTRING));
close(fd);
/* Wait for another second to allow the parent to process incoming data */
tv.tv_usec = 1000000;
retval = select(0,NULL, NULL, NULL, &tv);
exit(0);
}
int do_parent(int child) {
int fd;
fd_set fds_read, fds_write, fds_error;
int retval;
/* Open slave terminal for reading */
if((fd = open(TERMINALR, O_RDONLY)) == -1) {
printf("Error opening %s for reading\n", TERMINALR);
perror(NULL);
printf("Please make sure that %s is not in use while running this test.\n",
TERMINALR);
waitpid(child, &retval, 0);
exit(-1);
}
/* Clear bit masks */
FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
/* Set read bits */
FD_SET(fd, &fds_read);
FD_SET(fd, &fds_write);
/* Test if we can read or write from/to fd. As fd is opened read only we
* cannot actually write, so the select should return immediately with fd
* set in fds_write, but not in fds_read. Note that the child waits two
* seconds before sending data. This gives us the opportunity run this
* sub-test as reading from fd is blocking at this point. */
retval = select(fd+1, &fds_read, &fds_write, &fds_error, NULL);
if(retval != 1) e(1, "incorrect amount of ready file descriptors");
if(FD_ISSET(fd, &fds_read)) e(2, "read should NOT be set");
if(!FD_ISSET(fd, &fds_write)) e(3, "write should be set");
if(FD_ISSET(fd, &fds_error)) e(4, "error should NOT be set");
/* Block until ready; until child wrote stuff */
FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
FD_SET(fd, &fds_read);
retval = select(fd+1, &fds_read, NULL, &fds_error, NULL);
if(retval != 1) e(5, "incorrect amount of ready file descriptors");
if(!FD_ISSET(fd, &fds_read)) e(6, "read should be set");
if(FD_ISSET(fd, &fds_error)) e(7, "error should not be set");
FD_ZERO(&fds_read); FD_ZERO(&fds_error);
FD_SET(fd,&fds_write);
retval = select(fd+1, NULL, &fds_write, NULL, NULL);
/* As it is impossible to write to a read only fd, this select should return
* immediately with fd set in fds_write. */
if(retval != 1) e(8, "incorrect amount or ready file descriptors");
close(fd);
waitpid(child, &retval, 0);
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(-1);
} else if(sscanf(argv[1], "%d", &subtest) != 1) {
printf("Usage: %s subtest_no\n", argv[0]);
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*/
}