📄 test7.c
字号:
/* POSIX test program (7). Author: Andy Tanenbaum */
/* The following POSIX calls are tested:
*
* pipe(), mkfifo(), fcntl()
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define ITERATIONS 4
#define MAX_ERROR 4
#define ITEMS 32
#define READ 10
#define WRITE 20
#define UNLOCK 30
#define U 70
#define L 80
char buf[ITEMS] = {0,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9};
int subtest, errct, xfd;
int whence = SEEK_SET, func_code = F_SETLK;
extern char **environ;
_PROTOTYPE(int main, (int argc, char *argv []));
_PROTOTYPE(void test7a, (void));
_PROTOTYPE(void test7b, (void));
_PROTOTYPE(void test7c, (void));
_PROTOTYPE(void test7d, (void));
_PROTOTYPE(void test7e, (void));
_PROTOTYPE(void test7f, (void));
_PROTOTYPE(void test7g, (void));
_PROTOTYPE(void test7h, (void));
_PROTOTYPE(void test7i, (void));
_PROTOTYPE(void test7j, (void));
_PROTOTYPE(void cloexec_test, (void));
_PROTOTYPE(int set, (int how, int first, int last));
_PROTOTYPE(int locked, (int b));
_PROTOTYPE(void e, (int n));
_PROTOTYPE(void sigfunc, (int s));
_PROTOTYPE(void quit, (void));
int main(argc, argv)
int argc;
char *argv[];
{
int i, m = 0xFFFF;
sync();
if (argc == 2) m = atoi(argv[1]);
if (m == 0) cloexec_test(); /* important; do not remove this! */
printf("Test 7 ");
fflush(stdout);
system("rm -rf DIR_07; mkdir DIR_07");
chdir("DIR_07");
for (i = 0; i < ITERATIONS; i++) {
if (m & 00001) test7a();
if (m & 00002) test7b();
if (m & 00004) test7c();
if (m & 00010) test7d();
if (m & 00020) test7e();
if (m & 00040) test7f();
if (m & 00100) test7g();
if (m & 00200) test7h();
if (m & 00400) test7i();
if (m & 01000) test7j();
}
quit();
return(-1); /* impossible */
}
void test7a()
{
/* Test pipe(). */
int i, fd[2], ect;
char buf2[ITEMS+1];
/* Create a pipe, write on it, and read it back. */
subtest = 1;
if (pipe(fd) != 0) e(1);
if (write(fd[1], buf, ITEMS) != ITEMS) e(2);
buf2[0] = 0;
if (read(fd[0], buf2, ITEMS+1) != ITEMS) e(3);
ect = 0;
for (i = 0; i < ITEMS; i++) if (buf[i] != buf2[i]) ect++;
if (ect != 0) e(4);
if (close(fd[0]) != 0) e(5);
if (close(fd[1]) != 0) e(6);
/* Error test. Keep opening pipes until it fails. Check error code. */
errno = 0;
while (1) {
if (pipe(fd) < 0) break;
}
if (errno != EMFILE) e(7);
/* Close all the pipes. */
for (i = 3; i < OPEN_MAX; i++) close(i);
}
void test7b()
{
/* Test mkfifo(). */
int fdr, fdw, status;
char buf2[ITEMS+1];
/* Create a fifo, write on it, and read it back. */
subtest = 2;
if (mkfifo("T7.b", 0777) != 0) e(1);
if (fork()) {
/* Parent writes on the fifo. */
if ( (fdw = open("T7.b", O_WRONLY)) < 0) e(2);
if (write(fdw, buf, ITEMS) != ITEMS) e(3);
wait(&status);
if (close(fdw) != 0) e(4);
} else {
/* Child reads from the fifo. */
if ( (fdr = open("T7.b", O_RDONLY)) < 0) e(5);
if (read(fdr, buf2, ITEMS+1) != ITEMS) e(6);
if (strcmp(buf, buf2) != 0) e(7);
if (close(fdr) != 0) e(8);
exit(0);
}
/* Check some error conditions. */
if (mkfifo("T7.b", 0777) != -1) e(9);
errno = 0;
if (mkfifo("a/b/c", 0777) != -1) e(10);
if (errno != ENOENT) e(11);
errno = 0;
if (mkfifo("", 0777) != -1) e(12);
if (errno != ENOENT) e(13);
errno = 0;
if (mkfifo("T7.b/x", 0777) != -1) e(14);
if (errno != ENOTDIR) e(15);
if (unlink("T7.b") != 0) e(16);
/* Now check fifos and the O_NONBLOCK flag. */
if (mkfifo("T7.b", 0600) != 0) e(17);
errno = 0;
if (open("T7.b", O_WRONLY | O_NONBLOCK) != -1) e(18);
if (errno != ENXIO) e(19);
if ( (fdr = open("T7.b", O_RDONLY | O_NONBLOCK)) < 0) e(20);
if (fork()) {
/* Parent reads from fdr. */
wait(&status); /* but first make sure writer has already run*/
if ( ( (status>>8) & 0377) != 77) e(21);
if (read(fdr, buf2, ITEMS+1) != ITEMS) e(22);
if (strcmp(buf, buf2) != 0) e(23);
if (close(fdr) != 0) e(24);
} else {
/* Child opens the fifo for writing and writes to it. */
if ( (fdw = open("T7.b", O_WRONLY | O_NONBLOCK)) < 0) e(25);
if (write(fdw, buf, ITEMS) != ITEMS) e(26);
if (close(fdw) != 0) e(27);
exit(77);
}
if (unlink("T7.b") != 0) e(28);
}
void test7c()
{
/* Test fcntl(). */
int fd, m, s, newfd, newfd2;
struct stat stat1, stat2, stat3;
subtest = 3;
errno = -100;
if ( (fd = creat("T7.c", 0777)) < 0) e(1);
/* Turn the per-file-descriptor flags on and off. */
if (fcntl(fd, F_GETFD) != 0) e(2); /* FD_CLOEXEC is initially off */
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(3);/* turn it on */
if (fcntl(fd, F_GETFD) != FD_CLOEXEC) e(4); /* should be on now */
if (fcntl(fd, F_SETFD, 0) != 0) e(5); /* turn it off */
if (fcntl(fd, F_GETFD) != 0) e(6); /* should be off now */
/* Turn the open-file-description flags on and off. Start with O_APPEND. */
m = O_WRONLY;
if (fcntl(fd, F_GETFL) != m) e(7); /* O_APPEND, O_NONBLOCK are off */
if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(8); /* turn on O_APPEND */
if (fcntl(fd, F_GETFL) != (O_APPEND | m)) e(9);/* should be on now */
if (fcntl(fd, F_SETFL, 0) != 0) e(10); /* turn it off */
if (fcntl(fd, F_GETFL) != m) e(11); /* should be off now */
/* Turn the open-file-description flags on and off. Now try O_NONBLOCK. */
if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) e(12); /* turn on O_NONBLOCK */
if (fcntl(fd, F_GETFL) != (O_NONBLOCK | m)) e(13); /* should be on now */
if (fcntl(fd, F_SETFL, 0) != 0) e(14); /* turn it off */
if (fcntl(fd, F_GETFL) != m) e(15); /* should be off now */
/* Now both at once. */
if (fcntl(fd, F_SETFL, O_APPEND|O_NONBLOCK) != 0) e(16);
if (fcntl(fd, F_GETFL) != (O_NONBLOCK | O_APPEND | m)) e(17);
if (fcntl(fd, F_SETFL, 0) != 0) e(18);
if (fcntl(fd, F_GETFL) != m) e(19);
/* Now test F_DUPFD. */
if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(20); /* 0-4 open */
if ( (newfd2 = fcntl(fd, F_DUPFD, 0)) != 5) e(21); /* 0-5 open */
if (close(newfd) != 0) e(22); /* 0-3, 5 open */
if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(23); /* 0-5 open */
if (close(newfd) != 0) e(24); /* 0-3, 5 open */
if ( (newfd = fcntl(fd, F_DUPFD, 5)) != 6) e(25); /* 0-3, 5, 6 open */
if (close(newfd2) != 0) e(26); /* 0-3, 6 open */
/* O_APPEND should be inherited, but FD_CLOEXEC should be cleared. Check. */
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(26);/* turn FD_CLOEXEC on */
if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(27); /* turn O_APPEND on */
if ( (newfd2 = fcntl(fd, F_DUPFD, 10)) != 10) e(28); /* 0-3, 6, 10 open */
if (fcntl(newfd2, F_GETFD) != 0) e(29); /* FD_CLOEXEC must be 0 */
if (fcntl(newfd2, F_GETFL) != (O_APPEND | m)) e(30); /* O_APPEND set */
if (fcntl(fd, F_SETFD, 0) != 0) e(31);/* turn FD_CLOEXEC off */
/* Check if newfd and newfd2 are the same inode. */
if (fstat(fd, &stat1) != 0) e(32);
if (fstat(fd, &stat2) != 0) e(33);
if (fstat(fd, &stat3) != 0) e(34);
if (stat1.st_dev != stat2.st_dev) e(35);
if (stat1.st_dev != stat3.st_dev) e(36);
if (stat1.st_ino != stat2.st_ino) e(37);
if (stat1.st_ino != stat3.st_ino) e(38);
/* Now check on the FD_CLOEXEC flag. Set it for fd (3) and newfd2 (10) */
if (fd != 3 || newfd2 != 10 || newfd != 6) e(39);
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(40); /* close 3 on exec */
if (fcntl(newfd2, F_SETFD, FD_CLOEXEC) != 0) e(41); /* close 10 on exec */
if (fcntl(newfd, F_SETFD, 0) != 0) e(42); /* don't close 6 */
if (fork()) {
wait(&s); /* parent just waits */
if (WEXITSTATUS(s) != 0) e(43);
} else {
execle("../test7", "test7", "0", (char *) 0, environ);
exit(1); /* the impossible never happens, right? */
}
/* Finally, close all the files. */
if (fcntl(fd, F_SETFD, 0) != 0) e(44); /* FD_CLOEXEC off */
if (fcntl(newfd2, F_SETFD, 0) != 0) e(45); /* FD_CLOEXEC off */
if (close(fd) != 0) e(46);
if (close(newfd) != 0) e(47);
if (close(newfd2) != 0) e(48);
}
void test7d()
{
/* Test file locking. */
subtest = 4;
if ( (xfd = creat("T7.d", 0777)) != 3) e(1);
close(xfd);
if ( (xfd = open("T7.d", O_RDWR)) < 0) e(2);
if (write(xfd, buf, ITEMS) != ITEMS) e(3);
if (set(WRITE, 0, 3) != 0) e(4);
if (set(WRITE, 5, 9) != 0) e(5);
if (set(UNLOCK, 0, 3) != 0) e(6);
if (set(UNLOCK, 4, 9) != 0) e(7);
if (set(READ, 1, 4) != 0) e(8);
if (set(READ, 4, 7) != 0) e(9);
if (set(UNLOCK, 4, 7) != 0) e(10);
if (set(UNLOCK, 1, 4) != 0) e(11);
if (set(WRITE, 0, 3) != 0) e(12);
if (set(WRITE, 5, 7) != 0) e(13);
if (set(WRITE, 9 ,10) != 0) e(14);
if (set(UNLOCK, 0, 4) != 0) e(15);
if (set(UNLOCK, 0, 7) != 0) e(16);
if (set(UNLOCK, 0, 2000) != 0) e(17);
if (set(WRITE, 0, 3) != 0) e(18);
if (set(WRITE, 5, 7) != 0) e(19);
if (set(WRITE, 9 ,10) != 0) e(20);
if (set(UNLOCK, 0, 100) != 0) e(21);
if (set(WRITE, 0, 9) != 0) e(22);
if (set(UNLOCK, 8, 9) != 0) e(23);
if (set(UNLOCK, 0, 2) != 0) e(24);
if (set(UNLOCK, 5, 5) != 0) e(25);
if (set(UNLOCK, 4, 6) != 0) e(26);
if (set(UNLOCK, 3, 3) != 0) e(27);
if (set(UNLOCK, 7, 7) != 0) e(28);
if (set(WRITE, 0, 10) != 0) e(29);
if (set(UNLOCK, 0, 1000) != 0) e(30);
/* Up until now, all locks have been disjoint. Now try conflicts. */
if (set(WRITE, 0, 4) != 0) e(31);
if (set(WRITE, 4, 7) != 0) e(32); /* owner may lock same byte twice */
if (set(WRITE, 5, 10) != 0) e(33);
if (set(UNLOCK, 0, 11) != 0) e(34);
/* File is now unlocked. Length 0 means whole file. */
if (set(WRITE, 2, 1) != 0) e(35); /* this locks whole file */
if (set(WRITE, 9,10) != 0) e(36); /* a process can relock its file */
if (set(WRITE, 3, 3) != 0) e(37);
if (set(UNLOCK, 0, -1) != 0) e(38); /* file is now unlocked. */
/* Test F_GETLK. */
if (set(WRITE, 2, 3) != 0) e(39);
if (locked(1) != U) e(40);
if (locked(2) != L) e(41);
if (locked(3) != L) e(42);
if (locked(4) != U) e(43);
if (set(UNLOCK, 2, 3) != 0) e(44);
if (locked(2) != U) e(45);
if (locked(3) != U) e(46);
close(xfd);
}
void test7e()
{
/* Test to see if SETLKW blocks as it should. */
int pid, s;
subtest = 5;
if ( (xfd = creat("T7.e", 0777)) != 3) e(1);
if (close(xfd) != 0) e(2);
if ( (xfd = open("T7.e", O_RDWR)) < 0) e(3);
if (write(xfd, buf, ITEMS) != ITEMS) e(4);
if (set(WRITE, 0, 3) != 0) e(5);
if ( (pid = fork()) ) {
/* Parent waits until child has started before signaling it. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -