|
|
1.1 ! root 1: /* ! 2: * test of named pipes ! 3: */ ! 4: #include <fcntl.h> ! 5: #include <signal.h> ! 6: ! 7: int alarmed; ! 8: int myalrm(); ! 9: ! 10: char * pname = "PIPE"; ! 11: int fd; ! 12: char buf[80]; ! 13: int nread; ! 14: #define PLIMIT 64000 ! 15: ! 16: main(argc, argv) ! 17: int argc; ! 18: char ** argv; ! 19: { ! 20: /* ! 21: * Get name of pipe to play with - default to "PIPE". ! 22: */ ! 23: if (argc > 1) { ! 24: pname = argv[1]; ! 25: } ! 26: printf("Using pipe %s\n", pname); ! 27: if (signal(SIGALRM, myalrm) == SIG_ERR) ! 28: perror("pipetest: set alarm sig"); ! 29: ! 30: test1(); ! 31: test2(); ! 32: test3(); ! 33: test4(); ! 34: test5(); ! 35: test6(); ! 36: test7(); ! 37: test8(); ! 38: ! 39: printf("Done.\n"); ! 40: } ! 41: ! 42: test1() ! 43: { ! 44: /* ! 45: * Open read-only test; no process has pipe open for write. ! 46: */ ! 47: printf("\nTEST #1\n"); ! 48: printf("Open O_RDONLY, pipe not open elsewhere.\n"); ! 49: alarmed = 0; ! 50: alarm(2); ! 51: if ((fd = open(pname, O_RDONLY, 0)) != -1) { ! 52: printf("TEST #1 failed. Unexpected successful open.\n"); ! 53: close(fd); ! 54: } else { ! 55: perror("test#1: open"); ! 56: if (alarmed) ! 57: printf("TEST #1 passed. Open timed out.\n"); ! 58: else ! 59: printf("TEST #1 failed. Open did not time out.\n"); ! 60: } ! 61: alarm(0); ! 62: } ! 63: ! 64: test2() ! 65: { ! 66: /* ! 67: * Open read-only with no delay; no process has pipe open for write. ! 68: */ ! 69: printf("\nTEST #2\n"); ! 70: printf("Open O_RDONLY|O_NDELAY, pipe not open elsewhere.\n"); ! 71: alarmed = 0; ! 72: alarm(2); ! 73: if ((fd = open(pname, O_RDONLY | O_NDELAY, 0)) != -1) { ! 74: printf("TEST #2 passed. Open succeeded.\n"); ! 75: close(fd); ! 76: } else { ! 77: perror("test#2: open"); ! 78: if (alarmed) ! 79: printf("TEST #2 failed. Open timed out.\n"); ! 80: else ! 81: printf("TEST #2 failed. Open did not time out.\n"); ! 82: } ! 83: alarm(0); ! 84: } ! 85: ! 86: test3() ! 87: { ! 88: /* ! 89: * Open write-only test; no process has pipe open for read. ! 90: */ ! 91: printf("\nTEST #3\n"); ! 92: printf("Open O_WRONLY, pipe not open elsewhere.\n"); ! 93: alarmed = 0; ! 94: if (signal(SIGALRM, myalrm) == SIG_ERR) ! 95: perror("test#3: set alarm sig"); ! 96: alarm(2); ! 97: if ((fd = open(pname, O_WRONLY, 0)) != -1) { ! 98: printf("TEST #3 failed. Unexpected successful open.\n"); ! 99: close(fd); ! 100: } else { ! 101: perror("test#3: open"); ! 102: if (alarmed) ! 103: printf("TEST #3 passed. Open timed out.\n"); ! 104: else ! 105: printf("TEST #3 failed. Open did not time out.\n"); ! 106: } ! 107: alarm(0); ! 108: } ! 109: ! 110: test4() ! 111: { ! 112: /* ! 113: * Open write-only with no delay; no process has pipe open for read. ! 114: */ ! 115: printf("\nTEST #4\n"); ! 116: printf("Open O_WRONLY|O_NDELAY, pipe not open elsewhere.\n"); ! 117: alarmed = 0; ! 118: alarm(2); ! 119: if ((fd = open(pname, O_RDONLY | O_NDELAY, 0)) != -1) { ! 120: printf("TEST #4 passed. Open succeeded.\n"); ! 121: close(fd); ! 122: } else { ! 123: perror("test#4: open"); ! 124: if (alarmed) ! 125: printf("TEST #4 failed. Open timed out.\n"); ! 126: else ! 127: printf("TEST #4 failed. Open did not time out.\n"); ! 128: } ! 129: alarm(0); ! 130: } ! 131: ! 132: test5() ! 133: { ! 134: /* ! 135: * Read pipe, no data. ! 136: */ ! 137: printf("\nTEST #5\n"); ! 138: printf("Read pipe, no data.\n"); ! 139: if ((fd = open(pname, O_RDWR, 0)) == -1) { ! 140: perror("test#5: open O_RDWR"); ! 141: printf("TEST #5 failed. Open failed.\n"); ! 142: } else { ! 143: alarmed = 0; ! 144: alarm(2); ! 145: nread = read(fd, buf, 80); ! 146: if (nread < 0) { ! 147: perror("test#5: read"); ! 148: if (alarmed) ! 149: printf("TEST #5 passed. Read timed out.\n"); ! 150: else ! 151: printf( ! 152: "TEST #5 failed. Read did not time out.\n"); ! 153: } else { ! 154: printf( ! 155: "TEST #5 failed. Read returned %d.\n", nread); ! 156: } ! 157: close(fd); ! 158: } ! 159: alarm(0); ! 160: } ! 161: ! 162: test6() ! 163: { ! 164: /* ! 165: * Partial read. ! 166: */ ! 167: printf("\nTEST #6\n"); ! 168: printf("Read pipe, partial data.\n"); ! 169: if ((fd = open(pname, O_RDWR, 0)) == -1) { ! 170: perror("test#6: open O_RDWR"); ! 171: printf("TEST #6 failed. Open failed.\n"); ! 172: } else { ! 173: alarmed = 0; ! 174: alarm(2); ! 175: write(fd, "Hello", 5); ! 176: nread = read(fd, buf, 80); ! 177: if (nread < 0) { ! 178: perror("test#6: read"); ! 179: if (alarmed) ! 180: printf("TEST #6 failed. Read timed out.\n"); ! 181: else ! 182: printf( ! 183: "TEST #6 failed. Read did not time out.\n"); ! 184: } else { ! 185: printf( ! 186: "TEST #6 passed. Read returned %d.\n", nread); ! 187: } ! 188: close(fd); ! 189: } ! 190: alarm(0); ! 191: ! 192: } ! 193: ! 194: test7() ! 195: { ! 196: int wr_ct, nwrit; ! 197: /* ! 198: * Write pipe until no room. ! 199: */ ! 200: printf("\nTEST #7\n"); ! 201: printf("Write pipe until no room.\n"); ! 202: if ((fd = open(pname, O_RDWR, 0)) == -1) { ! 203: perror("test#7: open O_RDWR"); ! 204: printf("TEST #7 failed. Open failed.\n"); ! 205: } else { ! 206: alarmed = 0; ! 207: for (wr_ct = 0; wr_ct < PLIMIT; wr_ct += 80) { ! 208: alarm(2); ! 209: nwrit = write(fd, buf, 80); ! 210: if (nwrit > 0) ! 211: wr_ct += nwrit; ! 212: else if (nwrit == 0) { ! 213: printf("TEST #7 failed. Write failed.\n"); ! 214: break; ! 215: } else { ! 216: perror("test#7: write"); ! 217: if (alarmed) ! 218: printf("TEST #7 passed. Write timed out.\n"); ! 219: else ! 220: printf("TEST #7 failed. Write did not time out.\n"); ! 221: break; ! 222: } ! 223: } ! 224: alarm(0); ! 225: printf("%d bytes written\n", wr_ct); ! 226: close(fd); ! 227: } ! 228: } ! 229: ! 230: test8() ! 231: { ! 232: int wr_ct, nwrit; ! 233: /* ! 234: * Write pipe until no room. ! 235: */ ! 236: printf("\nTEST #8\n"); ! 237: printf("Write pipe until no room, no delay.\n"); ! 238: if ((fd = open(pname, O_RDWR|O_NDELAY, 0)) == -1) { ! 239: perror("test#8: open O_RDWR|O_NDELAY"); ! 240: printf("TEST #8 failed. Open failed.\n"); ! 241: } else { ! 242: alarmed = 0; ! 243: for (wr_ct = 0; wr_ct < PLIMIT; wr_ct += 80) { ! 244: alarm(2); ! 245: nwrit = write(fd, buf, 80); ! 246: if (nwrit > 0) ! 247: wr_ct += nwrit; ! 248: else if (nwrit == 0) { ! 249: printf("TEST #8 Passed. Write failed.\n"); ! 250: break; ! 251: } else { ! 252: perror("test#8: write"); ! 253: if (alarmed) ! 254: printf("TEST #8 failed. Write timed out.\n"); ! 255: else ! 256: printf("TEST #8 failed. Write did not time out.\n"); ! 257: break; ! 258: } ! 259: } ! 260: alarm(0); ! 261: printf("%d bytes written\n", wr_ct); ! 262: close(fd); ! 263: } ! 264: } ! 265: ! 266: int ! 267: myalrm() ! 268: { ! 269: if (signal(SIGALRM, myalrm) == SIG_ERR) ! 270: perror("set alarm sig"); ! 271: alarmed = 1; ! 272: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.