00001
00002
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <sys/wait.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 #include <fcntl.h>
00010 #include <limits.h>
00011 #include <errno.h>
00012 #include <time.h>
00013 #include <stdio.h>
00014
00015 #define MAX_ERROR 4
00016 #define ITERATIONS 60
00017
00018 #define Fstat(a,b) if (fstat(a,b) != 0) printf("Can't fstat %d\n", a)
00019 #define Time(t) if (time(t) == (time_t)-1) printf("Time error\n")
00020
00021 int errct = 0;
00022 int subtest = 1;
00023 char MaxName[NAME_MAX + 1];
00024 char MaxPath[PATH_MAX];
00025 char ToLongName[NAME_MAX + 2];
00026 char ToLongPath[PATH_MAX + 1];
00027
00028 _PROTOTYPE(void main, (int argc, char *argv[]));
00029 _PROTOTYPE(void test8a, (void));
00030 _PROTOTYPE(void test8b, (void));
00031 _PROTOTYPE(void e, (int number));
00032 _PROTOTYPE(void quit, (void));
00033
00034 void main(argc, argv)
00035 int argc;
00036 char *argv[];
00037 {
00038 int i, m = 0xFFFF;
00039
00040 sync();
00041 if (argc == 2) m = atoi(argv[1]);
00042 printf("Test 8 ");
00043 fflush(stdout);
00044
00045 for (i = 0; i < ITERATIONS; i++) {
00046 if (m & 0001) test8a();
00047 if (m & 0002) test8b();
00048 }
00049 quit();
00050 }
00051
00052 void test8a()
00053 {
00054 int tube[2], t1[2], t2[2], t3[2];
00055 time_t time1, time2;
00056 char buf[128];
00057 struct stat st1, st2;
00058 int stat_loc, flags;
00059
00060 subtest = 1;
00061
00062
00063 if (pipe(tube) != 0) e(1);
00064 if (tube[0] != 3 && tube[1] != 3) e(2);
00065 if (tube[1] != 4 && tube[0] != 4) e(3);
00066 if (tube[1] == tube[0]) e(4);
00067 if (pipe(t1) != 0) e(5);
00068 if (t1[0] != 5 && t1[1] != 5) e(6);
00069 if (t1[1] != 6 && t1[0] != 6) e(7);
00070 if (t1[1] == t1[0]) e(8);
00071 if (close(t1[0]) != 0) e(9);
00072 if (close(tube[0]) != 0) e(10);
00073 if (pipe(t2) != 0) e(11);
00074 if (t2[0] != tube[0] && t2[1] != tube[0]) e(12);
00075 if (t2[1] != t1[0] && t2[0] != t1[0]) e(13);
00076 if (t2[1] == t2[0]) e(14);
00077 if (pipe(t3) != 0) e(15);
00078 if (t3[0] != 7 && t3[1] != 7) e(16);
00079 if (t3[1] != 8 && t3[0] != 8) e(17);
00080 if (t3[1] == t3[0]) e(18);
00081 if (close(tube[1]) != 0) e(19);
00082 if (close(t1[1]) != 0) e(20);
00083 if (close(t2[0]) != 0) e(21);
00084 if (close(t2[1]) != 0) e(22);
00085 if (close(t3[0]) != 0) e(23);
00086 if (close(t3[1]) != 0) e(24);
00087
00088
00089 Time(&time1);
00090 if (pipe(tube) != 0) e(25);
00091 Fstat(tube[0], &st1);
00092 Fstat(tube[1], &st2);
00093 Time(&time2);
00094 if (st1.st_atime < time1) e(26);
00095 if (st1.st_ctime < time1) e(27);
00096 if (st1.st_mtime < time1) e(28);
00097 if (st1.st_atime > time2) e(29);
00098 if (st1.st_ctime > time2) e(30);
00099 if (st1.st_mtime > time2) e(31);
00100 if (st2.st_atime < time1) e(32);
00101 if (st2.st_ctime < time1) e(33);
00102 if (st2.st_mtime < time1) e(34);
00103 if (st2.st_atime > time2) e(35);
00104 if (st2.st_ctime > time2) e(36);
00105 if (st2.st_mtime > time2) e(37);
00106
00107
00108 if ((flags = fcntl(tube[0], F_GETFD)) != 0) e(38);
00109 if ((flags & FD_CLOEXEC) != 0) e(39);
00110 if ((flags = fcntl(tube[0], F_GETFL)) != 0) e(40);
00111 if ((flags & O_RDONLY) != O_RDONLY) e(41);
00112 if ((flags & O_NONBLOCK) != 0) e(42);
00113 if ((flags & O_RDWR) != 0) e(43);
00114 if ((flags & O_WRONLY) != 0) e(44);
00115
00116 if ((flags = fcntl(tube[1], F_GETFD)) != 0) e(45);
00117 if ((flags & FD_CLOEXEC) != 0) e(46);
00118 if ((flags = fcntl(tube[1], F_GETFL)) == -1) e(47);
00119 if ((flags & O_WRONLY) != O_WRONLY) e(48);
00120 if ((flags & O_NONBLOCK) != 0) e(49);
00121 if ((flags & O_RDWR) != 0) e(50);
00122 if ((flags & O_RDONLY) != 0) e(51);
00123
00124
00125 switch (fork()) {
00126 case -1: printf("Can't fork\n"); break;
00127 case 0:
00128 alarm(20);
00129 if (close(tube[0]) != 0) e(52);
00130 if (write(tube[1], "Hello", 6) != 6) e(53);
00131 if (close(tube[1]) != 0) e(54);
00132 exit(0);
00133 default:
00134 if (read(tube[0], buf, sizeof(buf)) != 6) e(55);
00135 if (strncmp(buf, "Hello", 6) != 0) e(56);
00136 wait(&stat_loc);
00137 if (stat_loc != 0) e(57);
00138 }
00139 if (close(tube[0]) != 0) e(58);
00140 if (close(tube[1]) != 0) e(59);
00141 }
00142
00143 void test8b()
00144 {
00145 int tube[2], child2parent[2], parent2child[2];
00146 int i, nchild = 0, nopen = 3, stat_loc;
00147 int fd;
00148 int forkfailed = 0;
00149 char c;
00150
00151 subtest = 2;
00152
00153
00154 while (nopen < OPEN_MAX - 2) {
00155 if (pipe(tube) != 0) {
00156
00157 if (errno != ENFILE) e(1);
00158 sleep(2);
00159 break;
00160 }
00161 nopen += 2;
00162 }
00163
00164 if (nopen < OPEN_MAX - 2) {
00165 if (pipe(tube) != -1) e(2);
00166 switch (errno) {
00167 case EMFILE:
00168 break;
00169 case ENFILE:
00170 switch (fork()) {
00171 case -1:
00172 printf("Can't fork\n");
00173 break;
00174 case 0:
00175 alarm(20);
00176 if (open("/", O_RDONLY) != -1) e(3);
00177 if (errno != ENFILE) e(4);
00178 exit(0);
00179 default:
00180 wait(&stat_loc);
00181 if (stat_loc != 0) e(5);
00182 }
00183 break;
00184 default:
00185 e(6);
00186 }
00187 }
00188
00189
00190 for (i = 3; i < OPEN_MAX; i++) (void) close(i);
00191
00192
00193 if (pipe(child2parent) != 0) e(7);
00194 if (pipe(parent2child) != 0) e(8);
00195 while (!forkfailed && (fd = open("/", O_RDONLY)) != -1) {
00196 close(fd);
00197 switch (fork()) {
00198 case -1:
00199 forkfailed = 1;
00200 break;
00201 case 0:
00202 alarm(60);
00203
00204
00205 while (pipe(tube) != -1);
00206 while (open("/", O_RDONLY) != -1);
00207
00208
00209 if (write(child2parent[1], "*", 1) != 1) e(9);
00210
00211
00212 if (read(parent2child[0], &c, 1) != 1) e(10);
00213 exit(0);
00214 default:
00215
00216
00217 if (read(child2parent[0], &c, 1) != 1) e(11);
00218 nchild++;
00219 break;
00220 }
00221 }
00222
00223 if (!forkfailed) {
00224 if (pipe(tube) != -1) e(12);
00225 if (errno != ENFILE) e(13);
00226 }
00227
00228
00229 while (nchild-- > 0) {
00230 if (write(parent2child[1], "*", 1) != 1) e(14);
00231 wait(&stat_loc);
00232 if (stat_loc != 0) e(15);
00233 }
00234
00235
00236 for (i = 3; i < OPEN_MAX; i++) (void) close(i);
00237 }
00238
00239 void e(n)
00240 int n;
00241 {
00242 int err_num = errno;
00243
00244 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
00245 errno = err_num;
00246 perror("");
00247 if (errct++ > MAX_ERROR) {
00248 printf("Too many errors; test aborted\n");
00249 chdir("..");
00250 system("rm -rf DIR*");
00251 exit(1);
00252 }
00253 errno = 0;
00254 }
00255
00256 void quit()
00257 {
00258 if (errct == 0) {
00259 printf("ok\n");
00260 exit(0);
00261 } else {
00262 printf("%d errors\n", errct);
00263 exit(1);
00264 }
00265 }