📄 test25.c
字号:
int fd1, fd2;
char buf[BUF_SIZE];
struct stat st;
int stat_loc;
subtest = 3;
System("rm -rf ../DIR_25/*");
/* Fifo file test here. */
if (mkfifo("fifo", 0777) != 0) e(1);
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20); /* Give child 20 seconds to live. */
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(2);
if (read(fd1, buf, BUF_SIZE) != 23) e(3);
if (strncmp(buf, "1 2 3 testing testing\n", 23) != 0) e(4);
if (close(fd1) != 0) e(5);
exit(0);
default:
if ((fd1 = open("fifo", O_WRONLY)) != 3) e(6);
if (write(fd1, "1 2 3 testing testing\n", 23) != 23) e(7);
if (close(fd1) != 0) e(8);
if (wait(&stat_loc) == -1) e(9);
if (stat_loc != 0) e(10); /* The alarm went off? */
}
/* Try opening for writing with O_NONBLOCK. */
fd1 = open("fifo", O_WRONLY | O_NONBLOCK);
if (fd1 != -1) e(11);
if (errno != ENXIO) e(12);
close(fd1);
/* Try opening for writing with O_NONBLOCK and O_CREAT. */
fd1 = open("fifo", O_WRONLY | O_CREAT | O_NONBLOCK, 0777);
if (fd1 != -1) e(13);
if (errno != ENXIO) e(14);
close(fd1);
/* Both the NONBLOCK and the EXCLusive give raise to error. */
fd1 = open("fifo", O_WRONLY | O_CREAT | O_EXCL | O_NONBLOCK, 0777);
if (fd1 != -1) e(15);
if (errno != EEXIST && errno != ENXIO) e(16);
close(fd1); /* Just in case. */
/* Try opening for reading with O_NONBLOCK. */
fd1 = open("fifo", O_RDONLY | O_NONBLOCK);
if (fd1 != 3) e(17);
if (close(fd1) != 0) e(18);
/* Nopt runs out of memory. ;-< We just cut out some valid code */
/* FIFO's should always append. (They have no file position.) */
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20); /* Give child 20 seconds to live. */
if ((fd1 = open("fifo", O_WRONLY)) != 3) e(19);
if ((fd2 = open("fifo", O_WRONLY)) != 4) e(20);
if (write(fd1, "I did see Elvis.\n", 18) != 18) e(21);
if (write(fd2, "I DID.\n", 8) != 8) e(22);
if (close(fd2) != 0) e(23);
if (close(fd1) != 0) e(24);
exit(0);
default:
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(25);
if (read(fd1, buf, 18) != 18) e(26);
if (strncmp(buf, "I did see Elvis.\n", 18) != 0) e(27);
if (read(fd1, buf, BUF_SIZE) != 8) e(28);
if (strncmp(buf, "I DID.\n", 8) != 0) e(29);
if (close(fd1) != 0) e(30);
if (wait(&stat_loc) == -1) e(31);
if (stat_loc != 0) e(32); /* The alarm went off? */
}
/* O_TRUNC should have no effect on FIFO files. */
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20); /* Give child 20 seconds to live. */
if ((fd1 = open("fifo", O_WRONLY)) != 3) e(33);
if (write(fd1, "I did see Elvis.\n", 18) != 18) e(34);
if ((fd2 = open("fifo", O_WRONLY | O_TRUNC)) != 4) e(35);
if (write(fd2, "I DID.\n", 8) != 8) e(36);
if (close(fd2) != 0) e(37);
if (close(fd1) != 0) e(38);
exit(0);
default:
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(39);
if (read(fd1, buf, 18) != 18) e(40);
if (strncmp(buf, "I did see Elvis.\n", 18) != 0) e(41);
if (read(fd1, buf, BUF_SIZE) != 8) e(42);
if (strncmp(buf, "I DID.\n", 8) != 0) e(43);
if (close(fd1) != 0) e(44);
if (wait(&stat_loc) == -1) e(45);
if (stat_loc != 0) e(46); /* The alarm went off? */
}
/* Closing the last fd should flush all data to the bitbucket. */
System("rm -rf /tmp/sema.25");
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20); /* Give child 20 seconds to live. */
if ((fd1 = open("fifo", O_WRONLY)) != 3) e(47);
if (write(fd1, "I did see Elvis.\n", 18) != 18) e(48);
Creat("/tmp/sema.25");
sleep(1); /* give parent a chance to open */
if (close(fd1) != 0) e(49);
exit(0);
default:
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(50);
/* Make `sure' write has closed. */
while (stat("/tmp/sema.25", &st) != 0) sleep(1);
if (close(fd1) != 0) e(51);
if ((fd1 = open("fifo", O_RDONLY | O_NONBLOCK)) != 3) e(52);
if (read(fd1, buf, BUF_SIZE) != 18) e(53);
if (close(fd1) != 0) e(54);
if (wait(&stat_loc) == -1) e(55);
if (stat_loc != 0) e(56); /* The alarm went off? */
}
/* Let's try one too many. */
System("rm -rf /tmp/sema.25");
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20); /* Give child 20 seconds to live. */
if ((fd1 = open("fifo", O_WRONLY)) != 3) e(57);
if (write(fd1, "I did see Elvis.\n", 18) != 18) e(58);
/* Keep open till second reader is opened. */
while (stat("/tmp/sema.25", &st) != 0) sleep(1);
if (close(fd1) != 0) e(59);
exit(0);
default:
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(60);
if (read(fd1, buf, 2) != 2) e(61);
if (strncmp(buf, "I ", 2) != 0) e(62);
if (close(fd1) != 0) e(63);
if ((fd1 = open("fifo", O_RDONLY)) != 3) e(64);
/* Signal second reader is open. */
Creat("/tmp/sema.25");
if (read(fd1, buf, 4) != 4) e(65);
if (strncmp(buf, "did ", 4) != 0) e(66);
if ((fd2 = open("fifo", O_RDONLY)) != 4) e(67);
if (read(fd2, buf, BUF_SIZE) != 12) e(68);
if (strncmp(buf, "see Elvis.\n", 12) != 0) e(69);
if (close(fd2) != 0) e(70);
if (close(fd1) != 0) e(71);
if (wait(&stat_loc) == -1) e(72);
if (stat_loc != 0) e(73); /* The alarm went off? */
}
System("rm -rf fifo /tmp/sema.25");
/* O_TRUNC should have no effect on directroys. */
System("mkdir dir; touch dir/f1 dir/f2 dir/f3");
if ((fd1 = open("dir", O_WRONLY | O_TRUNC)) != -1) e(74);
if (errno != EISDIR) e(75);
close(fd1);
/* Opening a directory for reading should be possible. */
if ((fd1 = open("dir", O_RDONLY)) != 3) e(76);
if (close(fd1) != 0) e(77);
if (unlink("dir/f1") != 0) e(78); /* Should still be there. */
if (unlink("dir/f2") != 0) e(79);
if (unlink("dir/f3") != 0) e(80);
if (rmdir("dir") != 0) e(81);
if (!superuser) {
/* Test if O_CREAT is not usable to open files with the wrong mode */
(void) umask(0200); /* nono has no */
System("touch nono"); /* write bit */
(void) umask(0000);
fd1 = open("nono", O_RDWR | O_CREAT, 0777); /* try to open */
if (fd1 != -1) e(82);
if (errno != EACCES) e(83); /* but no access */
}
}
void test25d()
{
int fd;
subtest = 4;
System("rm -rf ../DIR_25/*");
/* Test maximal file name length. */
if ((fd = open(MaxName, O_RDWR | O_CREAT, 0777)) != 3) e(1);
if (close(fd) != 0) e(2);
MaxPath[strlen(MaxPath) - 2] = '/';
MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
if ((fd = open(MaxPath, O_RDWR | O_CREAT, 0777)) != 3) e(3);
if (close(fd) != 0) e(4);
MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
}
void test25e()
{
int fd;
char *noread = "noread"; /* Name for unreadable file. */
char *nowrite = "nowrite"; /* Same for unwritable. */
int stat_loc;
subtest = 5;
System("rm -rf ../DIR_25/*");
mkdir("bar", 0777); /* make bar */
/* Check if no access on part of path generates the correct error. */
System("chmod 677 bar"); /* rw-rwxrwx */
if (open("bar/nono", O_RDWR | O_CREAT, 0666) != -1) e(1);
if (errno != EACCES) e(2);
/* Ditto for no write permission. */
System("chmod 577 bar"); /* r-xrwxrwx */
if (open("bar/nono", O_RDWR | O_CREAT, 0666) != -1) e(3);
if (errno != EACCES) e(4);
/* Clean up bar. */
System("rm -rf bar");
/* Improper flags set on existing file. */
System("touch noread; chmod 377 noread"); /* noread */
if (open(noread, O_RDONLY) != -1) e(5);
if (open(noread, O_RDWR) != -1) e(6);
if (open(noread, O_RDWR | O_CREAT, 0777) != -1) e(7);
if (open(noread, O_RDWR | O_CREAT | O_TRUNC, 0777) != -1) e(8);
if ((fd = open(noread, O_WRONLY)) != 3) e(9);
if (close(fd) != 0) e(10);
System("touch nowrite; chmod 577 nowrite"); /* nowrite */
if (open(nowrite, O_WRONLY) != -1) e(11);
if (open(nowrite, O_RDWR) != -1) e(12);
if (open(nowrite, O_RDWR | O_CREAT, 0777) != -1) e(13);
if (open(nowrite, O_RDWR | O_CREAT | O_TRUNC, 0777) != -1) e(14);
if ((fd = open(nowrite, O_RDONLY)) != 3) e(15);
if (close(fd) != 0) e(16);
if (superuser) {
/* If we can make a file ownd by some one else, test access again. */
System("chmod 733 noread");
System("chown bin noread");
System("chgrp system noread");
System("chmod 755 nowrite");
System("chown bin nowrite");
System("chgrp system nowrite");
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
setuid(1);
setgid(1); /* become daemon */
if (open(noread, O_RDONLY) != -1) e(17);
if (open(noread, O_RDWR) != -1) e(18);
if (open(noread, O_RDWR | O_CREAT, 0777) != -1) e(19);
fd = open(noread, O_RDWR | O_CREAT | O_TRUNC, 0777);
if (fd != -1) e(20);
if ((fd = open(noread, O_WRONLY)) != 3) e(21);
if (close(fd) != 0) e(22);
if (open(nowrite, O_WRONLY) != -1) e(23);
if (open(nowrite, O_RDWR) != -1) e(24);
if (open(nowrite, O_RDWR | O_CREAT, 0777) != -1) e(25);
fd = open(nowrite, O_RDWR | O_CREAT | O_TRUNC, 0777);
if (fd != -1) e(26);
if ((fd = open(nowrite, O_RDONLY)) != 3) e(27);
if (close(fd) != 0) e(28);
exit(0);
default:
if (wait(&stat_loc) == -1) e(29);
}
}
/* Clean up the noread and nowrite files. */
System("rm -rf noread nowrite");
/* Test the O_EXCL flag. */
System("echo > exists");
if (open("exists", O_RDWR | O_CREAT | O_EXCL, 0777) != -1) e(30);
if (errno != EEXIST) e(31);
if (open("exists", O_RDONLY | O_CREAT | O_EXCL, 0777) != -1) e(32);
if (errno != EEXIST) e(33);
if (open("exists", O_WRONLY | O_CREAT | O_EXCL, 0777) != -1) e(34);
if (errno != EEXIST) e(35);
fd = open("exists", O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0777);
if (fd != -1) e(36);
if (errno != EEXIST) e(37);
fd = open("exists", O_RDONLY | O_CREAT | O_EXCL | O_TRUNC, 0777);
if (fd != -1) e(38);
if (errno != EEXIST) e(39);
fd = open("exists", O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0777);
if (fd != -1) e(40);
if (errno != EEXIST) e(41);
/* Test ToLongName and ToLongPath */
if ((fd = open(ToLongName, O_RDWR | O_CREAT, 0777)) != 3) e(45);
if (close(fd) != 0) e(46);
ToLongPath[PATH_MAX - 2] = '/';
ToLongPath[PATH_MAX - 1] = 'a';
if ((fd = open(ToLongPath, O_RDWR | O_CREAT, 0777)) != -1) e(47);
if (errno != ENAMETOOLONG) e(48);
if (close(fd) != -1) e(49);
ToLongPath[PATH_MAX - 1] = '/';
}
void makelongnames()
{
register int i;
memset(MaxName, 'a', NAME_MAX);
MaxName[NAME_MAX] = '\0';
for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
MaxPath[i++] = '.';
MaxPath[i] = '/';
}
MaxPath[PATH_MAX - 1] = '\0';
strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
strcpy(ToLongPath, MaxPath);
ToLongName[NAME_MAX] = 'a';
ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
ToLongPath[PATH_MAX - 1] = '/';
ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
}
void e(n)
int n;
{
int err_num = errno; /* Save in case printf clobbers it. */
printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
errno = err_num;
perror("");
if (errct++ > MAX_ERROR) {
printf("Too many errors; test aborted\n");
chdir("..");
system("rm -rf DIR*");
exit(1);
}
errno = 0;
}
void quit()
{
Chdir("..");
System("rm -rf DIR_25");
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -