1*1b191cb5SApple OSS Distributions #include <string.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <stdlib.h>
4*1b191cb5SApple OSS Distributions #include <sys/reason.h>
5*1b191cb5SApple OSS Distributions #include <dispatch/dispatch.h>
6*1b191cb5SApple OSS Distributions #include <dispatch/private.h>
7*1b191cb5SApple OSS Distributions
8*1b191cb5SApple OSS Distributions #define TEST_REASON_CODE 4
9*1b191cb5SApple OSS Distributions
10*1b191cb5SApple OSS Distributions #define countof(x) (sizeof(x) / sizeof(x[0]))
11*1b191cb5SApple OSS Distributions
12*1b191cb5SApple OSS Distributions static bool
_should_spin(char * mode)13*1b191cb5SApple OSS Distributions _should_spin(char *mode)
14*1b191cb5SApple OSS Distributions {
15*1b191cb5SApple OSS Distributions // These tests are signaled by the parent
16*1b191cb5SApple OSS Distributions char *spin_modes[] = {
17*1b191cb5SApple OSS Distributions "spin",
18*1b191cb5SApple OSS Distributions "reason",
19*1b191cb5SApple OSS Distributions "clean",
20*1b191cb5SApple OSS Distributions "dirty",
21*1b191cb5SApple OSS Distributions };
22*1b191cb5SApple OSS Distributions for (size_t i = 0; i < countof(spin_modes); i++) {
23*1b191cb5SApple OSS Distributions if (strcmp(mode, spin_modes[i]) == 0) {
24*1b191cb5SApple OSS Distributions return true;
25*1b191cb5SApple OSS Distributions }
26*1b191cb5SApple OSS Distributions }
27*1b191cb5SApple OSS Distributions return false;
28*1b191cb5SApple OSS Distributions }
29*1b191cb5SApple OSS Distributions
30*1b191cb5SApple OSS Distributions int
main(int argc,char * argv[])31*1b191cb5SApple OSS Distributions main(int argc, char *argv[])
32*1b191cb5SApple OSS Distributions {
33*1b191cb5SApple OSS Distributions if (argc != 2) {
34*1b191cb5SApple OSS Distributions printf("Missing arguments\n");
35*1b191cb5SApple OSS Distributions exit(1);
36*1b191cb5SApple OSS Distributions }
37*1b191cb5SApple OSS Distributions
38*1b191cb5SApple OSS Distributions if (strcmp(argv[1], "crash") == 0) {
39*1b191cb5SApple OSS Distributions abort_with_reason(OS_REASON_TEST, TEST_REASON_CODE, "Test forcing crash", OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT);
40*1b191cb5SApple OSS Distributions } else if (strcmp(argv[1], "success") == 0) {
41*1b191cb5SApple OSS Distributions exit(0);
42*1b191cb5SApple OSS Distributions } else if (strcmp(argv[1], "exit") == 0) {
43*1b191cb5SApple OSS Distributions exit(2);
44*1b191cb5SApple OSS Distributions } else if (strcmp(argv[1], "wait") == 0) {
45*1b191cb5SApple OSS Distributions signal(SIGUSR1, SIG_IGN);
46*1b191cb5SApple OSS Distributions dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, NULL);
47*1b191cb5SApple OSS Distributions dispatch_source_set_event_handler(source, ^{
48*1b191cb5SApple OSS Distributions abort_with_reason(OS_REASON_TEST, TEST_REASON_CODE, "Test forcing crash", OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT);
49*1b191cb5SApple OSS Distributions });
50*1b191cb5SApple OSS Distributions dispatch_activate(source);
51*1b191cb5SApple OSS Distributions } else if (_should_spin(argv[1])) {
52*1b191cb5SApple OSS Distributions while (1) {
53*1b191cb5SApple OSS Distributions // Do nothing until the parent kills us
54*1b191cb5SApple OSS Distributions continue;
55*1b191cb5SApple OSS Distributions }
56*1b191cb5SApple OSS Distributions } else {
57*1b191cb5SApple OSS Distributions printf("Unknown argument: %s\n", argv[1]);
58*1b191cb5SApple OSS Distributions exit(1);
59*1b191cb5SApple OSS Distributions }
60*1b191cb5SApple OSS Distributions dispatch_main();
61*1b191cb5SApple OSS Distributions }
62