00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <sys/wait.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <string.h>
00021 #include <fcntl.h>
00022 #include <limits.h>
00023 #include <errno.h>
00024 #include <time.h>
00025 #include <stdio.h>
00026
00027 #define MAX_ERROR 4
00028 #define ITERATIONS 10
00029
00030 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
00031 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
00032 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
00033
00034 #define IS_CLOEXEC(fd) ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == FD_CLOEXEC)
00035 #define SET_CLOEXEC(fd) fcntl(fd, F_SETFD, FD_CLOEXEC)
00036
00037 int errct = 0;
00038 int subtest = 1;
00039 int superuser;
00040 char MaxName[NAME_MAX + 1];
00041 char MaxPath[PATH_MAX];
00042 char ToLongName[NAME_MAX + 2];
00043 char ToLongPath[PATH_MAX + 1];
00044
00045 _PROTOTYPE(void main, (int argc, char *argv[]));
00046 _PROTOTYPE(void test29a, (void));
00047 _PROTOTYPE(void test29b, (void));
00048 _PROTOTYPE(void test29c, (void));
00049 _PROTOTYPE(void e, (int number));
00050 _PROTOTYPE(void quit, (void));
00051
00052 void main(argc, argv)
00053 int argc;
00054 char *argv[];
00055 {
00056 int i, m = 0xFFFF;
00057
00058 sync();
00059 if (argc == 2) m = atoi(argv[1]);
00060 printf("Test 29 ");
00061 fflush(stdout);
00062 System("rm -rf DIR_29; mkdir DIR_29");
00063 Chdir("DIR_29");
00064 superuser = (geteuid() == 0);
00065
00066 for (i = 0; i < ITERATIONS; i++) {
00067 if (m & 0001) test29a();
00068 if (m & 0002) test29b();
00069 if (m & 0004) test29c();
00070 }
00071 quit();
00072 }
00073
00074 void test29a()
00075 {
00076 int fd1, fd2, fd3, fd4, fd5;
00077 struct flock flock;
00078
00079 subtest = 1;
00080
00081
00082 if ((fd1 = dup(0)) != 3) e(1);
00083 if ((fd2 = dup(0)) != 4) e(2);
00084 if ((fd3 = dup(0)) != 5) e(3);
00085 if ((fd4 = dup(0)) != 6) e(4);
00086 if ((fd5 = dup(0)) != 7) e(5);
00087 if (close(fd2) != 0) e(6);
00088 if (close(fd4) != 0) e(7);
00089 if ((fd2 = dup(0)) != 4) e(8);
00090 if ((fd4 = dup(0)) != 6) e(9);
00091 if (close(fd1) != 0) e(10);
00092 if (close(fd3) != 0) e(11);
00093 if (close(fd5) != 0) e(12);
00094 if ((fd1 = dup(0)) != 3) e(13);
00095 if ((fd3 = dup(0)) != 5) e(14);
00096 if ((fd5 = dup(0)) != 7) e(15);
00097 if (close(fd1) != 0) e(16);
00098 if (close(fd2) != 0) e(17);
00099 if (close(fd3) != 0) e(18);
00100 if (close(fd4) != 0) e(19);
00101 if (close(fd5) != 0) e(20);
00102
00103
00104 if ((fd1 = dup(0)) != 3) e(21);
00105 if (SET_CLOEXEC(fd1) == -1) e(22);
00106 if (!IS_CLOEXEC(fd1)) e(23);
00107 if ((fd2 = dup(fd1)) != 4) e(24);
00108 if ((fd3 = dup(fd2)) != 5) e(25);
00109 if (IS_CLOEXEC(fd2)) e(26);
00110 if (IS_CLOEXEC(fd3)) e(27);
00111 if (SET_CLOEXEC(fd2) == -1) e(28);
00112 if (!IS_CLOEXEC(fd2)) e(29);
00113 if (IS_CLOEXEC(fd3)) e(30);
00114 if (close(fd1) != 0) e(31);
00115 if (close(fd2) != 0) e(32);
00116 if (close(fd3) != 0) e(33);
00117
00118
00119 System("echo 'Hallo' > file");
00120 if ((fd1 = open("file", O_RDWR)) != 3) e(34);
00121 flock.l_whence = SEEK_SET;
00122 flock.l_start = 0;
00123 flock.l_len = 10;
00124 flock.l_type = F_WRLCK;
00125 if (fcntl(fd1, F_SETLK, &flock) == -1) e(35);
00126 if (fcntl(fd1, F_SETLK, &flock) == -1) e(36);
00127 if ((fd2 = dup(fd1)) != 4) e(37);
00128 if (fcntl(fd1, F_SETLK, &flock) == -1) e(38);
00129 if (fcntl(fd1, F_GETLK, &flock) == -1) e(39);
00130 #if 0
00131 if (flock.l_type != F_WRLCK) e(40);
00132 if (flock.l_pid != getpid()) e(41);
00133 #endif
00134 flock.l_type = F_WRLCK;
00135 if (fcntl(fd2, F_GETLK, &flock) == -1) e(42);
00136 #if 0
00137 if (flock.l_type != F_WRLCK) e(43);
00138 if (flock.l_pid != getpid()) e(44);
00139 #endif
00140 if (close(fd1) != 0) e(45);
00141 if (close(fd2) != 0) e(46);
00142
00143 System("rm -rf ../DIR_29/*");
00144 }
00145
00146 void test29b()
00147 {
00148 int fd;
00149 char buf[32];
00150
00151 subtest = 2;
00152
00153
00154 System("echo 'Hallo!' > file");
00155
00156
00157 if ((fd = open("file", O_RDONLY)) != 3) e(1);
00158 if (read(fd, buf, 2) != 2) e(2);
00159 if (strncmp(buf, "Ha", 2) != 0) e(3);
00160 if (dup2(fd, fd) != fd) e(4);
00161 if (read(fd, buf, 2) != 2) e(5);
00162 if (strncmp(buf, "ll", 2) != 0) e(6);
00163 if (dup2(fd, fd) != fd) e(7);
00164 if (read(fd, buf, 2) != 2) e(8);
00165 if (strncmp(buf, "o!", 2) != 0) e(9);
00166 if (close(fd) != 0) e(10);
00167
00168
00169 if ((fd = open("file", O_RDONLY)) != 3) e(11);
00170 if (read(fd, buf, 2) != 2) e(12);
00171 if (strncmp(buf, "Ha", 2) != 0) e(13);
00172 if (dup2(OPEN_MAX + 3, fd) != -1) e(14);
00173 if (errno != EBADF) e(15);
00174 if (read(fd, buf, 2) != 2) e(16);
00175 if (strncmp(buf, "ll", 2) != 0) e(17);
00176 if (dup2(-4, fd) != -1) e(18);
00177 if (errno != EBADF) e(19);
00178 if (read(fd, buf, 2) != 2) e(20);
00179 if (strncmp(buf, "o!", 2) != 0) e(21);
00180 if (close(fd) != 0) e(22);
00181
00182 System("rm -rf ../DIR_29/*");
00183 }
00184
00185 void test29c()
00186 {
00187 int i;
00188
00189 subtest = 3;
00190
00191
00192 for (i = -OPEN_MAX; i < OPEN_MAX * 2; i++) {
00193
00194
00195 if (i >= 0 && i < 3) continue;
00196
00197
00198 if (dup(i) != -1) e(1);
00199 if (errno != EBADF) e(2);
00200
00201
00202 if (i == OPEN_MAX) {
00203 if (dup2(0, i) != -1) e(3);
00204 if (errno != EINVAL) e(4);
00205 }
00206
00207
00208 if (i < 0 || i > OPEN_MAX) {
00209 if (dup2(0, i) != -1) e(5);
00210 if (errno != EBADF) e(6);
00211 }
00212 }
00213
00214 System("rm -rf ../DIR_29/*");
00215 }
00216
00217 void e(n)
00218 int n;
00219 {
00220 int err_num = errno;
00221
00222 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
00223 errno = err_num;
00224 perror("");
00225 if (errct++ > MAX_ERROR) {
00226 printf("Too many errors; test aborted\n");
00227 chdir("..");
00228 system("rm -rf DIR*");
00229 exit(1);
00230 }
00231 errno = 0;
00232 }
00233
00234 void quit()
00235 {
00236 Chdir("..");
00237 System("rm -rf DIR_29");
00238
00239 if (errct == 0) {
00240 printf("ok\n");
00241 exit(0);
00242 } else {
00243 printf("%d errors\n", errct);
00244 exit(1);
00245 }
00246 }