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