1 #include <dispatch/dispatch.h>
8 #include <mach/clock_types.h>
10 #include "dispatch_test.h"
12 extern char **environ
;
15 main(int argc
, char *argv
[])
17 dispatch_source_t tmp_ds
;
22 fprintf(stderr
, "usage: harness [...]\n");
26 posix_spawnattr_t attr
;
27 res
= posix_spawnattr_init(&attr
);
29 res
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_START_SUSPENDED
);
33 char** newargv
= calloc(argc
, sizeof(void*));
34 for (i
= 1; i
< argc
; ++i
) {
35 newargv
[i
-1] = argv
[i
];
39 res
= posix_spawnp(&pid
, newargv
[0], NULL
, &attr
, newargv
, environ
);
45 //fprintf(stderr, "pid = %d\n", pid);
48 dispatch_queue_t main_q
= dispatch_get_main_queue();
50 tmp_ds
= dispatch_source_proc_create(pid
, DISPATCH_PROC_EXIT
, NULL
, main_q
,
51 ^(dispatch_event_t ev
__attribute__((unused
))) {
53 int res2
= waitpid(pid
, &status
, 0);
55 //int passed = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
56 test_long("Process exited", WEXITSTATUS(status
) | WTERMSIG(status
), 0);
61 uint64_t timeout
= 30LL * NSEC_PER_SEC
;
63 tmp_ds
= dispatch_source_timer_create(DISPATCH_TIMER_ONESHOT
, timeout
, 0, NULL
, main_q
,
64 ^(dispatch_event_t ev
__attribute__((unused
))) {
66 fprintf(stderr
, "Terminating unresponsive process (%0.1lfs)\n", (double)timeout
/NSEC_PER_SEC
);
70 signal(SIGINT
, SIG_IGN
);
71 tmp_ds
= dispatch_source_signal_create(SIGINT
, NULL
, main_q
,
72 ^(dispatch_event_t ev
__attribute__((unused
))) {
73 fprintf(stderr
, "Terminating process due to signal\n");