1*4b171f7dSanton /* $OpenBSD: pipe.c,v 1.5 2021/10/22 05:03:04 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;
33f82cc820Santon int infinity = 0;
3479596596Santon int verbose = 0;
3579596596Santon
3679596596Santon int
main(int argc,char * argv[])3779596596Santon main(int argc, char *argv[])
3879596596Santon {
3979596596Santon struct {
4079596596Santon const char *t_name;
4179596596Santon int (*t_fn)(void);
4279596596Santon } tests[] = {
43f82cc820Santon { "close-race", test_close_race },
44e22febffSanton { "kqueue-read", test_kqueue_read },
45e22febffSanton { "kqueue-read-eof", test_kqueue_read_eof },
46e22febffSanton { "kqueue-write", test_kqueue_write },
47e22febffSanton { "kqueue-write-eof", test_kqueue_write_eof },
4879596596Santon { "ping-pong", test_ping_pong },
4979596596Santon { "run-down-write-big", test_run_down_write_big },
5079596596Santon { "run-down-write-small", test_run_down_write_small },
51*4b171f7dSanton { "select-hup", test_select_hup },
5279596596Santon { "thundering-herd-read-signal", test_thundering_herd_read_signal },
5379596596Santon { "thundering-herd-read-wakeup", test_thundering_herd_read_wakeup },
5479596596Santon { "thundering-herd-write-signal", test_thundering_herd_write_signal },
5579596596Santon { "thundering-herd-write-wakeup", test_thundering_herd_write_wakeup },
5679596596Santon
5779596596Santon { NULL, NULL },
5879596596Santon };
5979596596Santon int ch, i;
6079596596Santon
61f82cc820Santon while ((ch = getopt(argc, argv, "iv")) != -1) {
6279596596Santon switch (ch) {
63f82cc820Santon case 'i':
64f82cc820Santon infinity = 1;
65f82cc820Santon break;
6679596596Santon case 'v':
6779596596Santon verbose = 1;
6879596596Santon break;
6979596596Santon default:
7079596596Santon usage();
7179596596Santon }
7279596596Santon }
7379596596Santon argc -= optind;
7479596596Santon argv += optind;
7579596596Santon if (argc != 1)
7679596596Santon usage();
7779596596Santon
7879596596Santon if (signal(SIGPIPE, sighandler) == SIG_ERR)
7979596596Santon err(1, "signal");
8079596596Santon
8179596596Santon for (i = 0; tests[i].t_name != NULL; i++) {
8279596596Santon if (strcmp(argv[0], tests[i].t_name))
8379596596Santon continue;
8479596596Santon
8579596596Santon return tests[i].t_fn();
8679596596Santon }
8779596596Santon warnx("%s: no such test", argv[0]);
8879596596Santon
8979596596Santon return 1;
9079596596Santon }
9179596596Santon
9279596596Santon int
xwaitpid(pid_t pid)9379596596Santon xwaitpid(pid_t pid)
9479596596Santon {
9579596596Santon int status;
9679596596Santon
9779596596Santon if (waitpid(pid, &status, 0) == -1)
9879596596Santon err(1, "waitpid");
9979596596Santon if (WIFEXITED(status))
10079596596Santon return WEXITSTATUS(status);
10179596596Santon if (WIFSIGNALED(status))
10279596596Santon return WTERMSIG(status);
10379596596Santon return 0;
10479596596Santon }
10579596596Santon
10679596596Santon static void
sighandler(int signo)10779596596Santon sighandler(int signo)
10879596596Santon {
10979596596Santon
11079596596Santon gotsigpipe = signo;
11179596596Santon }
11279596596Santon
11379596596Santon static __dead void
usage(void)11479596596Santon usage(void)
11579596596Santon {
11679596596Santon
117f82cc820Santon fprintf(stderr, "usage: pipe [-iv] test-case\n");
11879596596Santon exit(1);
11979596596Santon }
120