1*e22febffSanton /* $OpenBSD: pipe.c,v 1.3 2019/12/24 09:37:53 anton Exp $ */ 279596596Santon 379596596Santon /* 479596596Santon * Copyright (c) 2019 Anton Lindqvist <anton@openbsd.org> 579596596Santon * 679596596Santon * Permission to use, copy, modify, and distribute this software for any 779596596Santon * purpose with or without fee is hereby granted, provided that the above 879596596Santon * copyright notice and this permission notice appear in all copies. 979596596Santon * 1079596596Santon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1179596596Santon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1279596596Santon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1379596596Santon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1479596596Santon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1579596596Santon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1679596596Santon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1779596596Santon */ 1879596596Santon 1979596596Santon #include <sys/wait.h> 2079596596Santon 2179596596Santon #include <err.h> 2279596596Santon #include <stdio.h> 2379596596Santon #include <stdlib.h> 2479596596Santon #include <string.h> 2579596596Santon #include <unistd.h> 2679596596Santon 2779596596Santon #include "pipe.h" 2879596596Santon 2979596596Santon static void sighandler(int); 3079596596Santon static __dead void usage(void); 3179596596Santon 3279596596Santon sig_atomic_t gotsigpipe = 0; 3379596596Santon int verbose = 0; 3479596596Santon 3579596596Santon int 3679596596Santon main(int argc, char *argv[]) 3779596596Santon { 3879596596Santon struct { 3979596596Santon const char *t_name; 4079596596Santon int (*t_fn)(void); 4179596596Santon } tests[] = { 42*e22febffSanton { "kqueue-read", test_kqueue_read }, 43*e22febffSanton { "kqueue-read-eof", test_kqueue_read_eof }, 44*e22febffSanton { "kqueue-write", test_kqueue_write }, 45*e22febffSanton { "kqueue-write-eof", test_kqueue_write_eof }, 4679596596Santon { "ping-pong", test_ping_pong }, 4779596596Santon { "run-down-write-big", test_run_down_write_big }, 4879596596Santon { "run-down-write-small", test_run_down_write_small }, 4979596596Santon { "thundering-herd-read-signal", test_thundering_herd_read_signal }, 5079596596Santon { "thundering-herd-read-wakeup", test_thundering_herd_read_wakeup }, 5179596596Santon { "thundering-herd-write-signal", test_thundering_herd_write_signal }, 5279596596Santon { "thundering-herd-write-wakeup", test_thundering_herd_write_wakeup }, 5379596596Santon 5479596596Santon { NULL, NULL }, 5579596596Santon }; 5679596596Santon int ch, i; 5779596596Santon 5879596596Santon while ((ch = getopt(argc, argv, "v")) != -1) { 5979596596Santon switch (ch) { 6079596596Santon case 'v': 6179596596Santon verbose = 1; 6279596596Santon break; 6379596596Santon default: 6479596596Santon usage(); 6579596596Santon } 6679596596Santon } 6779596596Santon argc -= optind; 6879596596Santon argv += optind; 6979596596Santon if (argc != 1) 7079596596Santon usage(); 7179596596Santon 7279596596Santon if (signal(SIGPIPE, sighandler) == SIG_ERR) 7379596596Santon err(1, "signal"); 7479596596Santon 7579596596Santon for (i = 0; tests[i].t_name != NULL; i++) { 7679596596Santon if (strcmp(argv[0], tests[i].t_name)) 7779596596Santon continue; 7879596596Santon 7979596596Santon return tests[i].t_fn(); 8079596596Santon } 8179596596Santon warnx("%s: no such test", argv[0]); 8279596596Santon 8379596596Santon return 1; 8479596596Santon } 8579596596Santon 8679596596Santon int 8779596596Santon xwaitpid(pid_t pid) 8879596596Santon { 8979596596Santon int status; 9079596596Santon 9179596596Santon if (waitpid(pid, &status, 0) == -1) 9279596596Santon err(1, "waitpid"); 9379596596Santon if (WIFEXITED(status)) 9479596596Santon return WEXITSTATUS(status); 9579596596Santon if (WIFSIGNALED(status)) 9679596596Santon return WTERMSIG(status); 9779596596Santon return 0; 9879596596Santon } 9979596596Santon 10079596596Santon static void 10179596596Santon sighandler(int signo) 10279596596Santon { 10379596596Santon 10479596596Santon gotsigpipe = signo; 10579596596Santon } 10679596596Santon 10779596596Santon static __dead void 10879596596Santon usage(void) 10979596596Santon { 11079596596Santon 11179596596Santon fprintf(stderr, "usage: pipe test-case\n"); 11279596596Santon exit(1); 11379596596Santon } 114