2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #include "config/config.h"
23 #include <dispatch/dispatch.h>
30 #include <mach/clock_types.h>
32 #include "dispatch_test.h"
34 extern char **environ;
37 main(int argc, char *argv[])
39 dispatch_source_t proc;
40 dispatch_source_t sig;
45 fprintf(stderr, "usage: harness [...]\n");
50 // Start the test in a suspended state. Will send SIGCONT once ready.
53 posix_spawnattr_t attr;
54 res = posix_spawnattr_init(&attr);
56 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
60 char** newargv = calloc(argc, sizeof(void*));
61 for (i = 1; i < argc; ++i) {
62 newargv[i-1] = argv[i];
66 res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
72 //fprintf(stderr, "pid = %d\n", pid);
76 // Monitor the test process exit status.
79 dispatch_queue_t main_q = dispatch_get_main_queue();
81 proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q);
84 dispatch_source_set_event_handler(proc, ^{
86 int res2 = waitpid(pid, &status, 0);
88 test_long("Process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
92 dispatch_resume(proc);
95 // Forcibly stop currently running test after 30 second timeout.
98 uint64_t timeout = 30LL * NSEC_PER_SEC;
100 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), main_q, ^{
102 fprintf(stderr, "Terminating unresponsive process (%0.1lfs)\n", (double)timeout/NSEC_PER_SEC);
106 // Control-c forcibly stops currently running test.
109 // Ignore the default signal handler so that dispatch can handle it.
110 signal(SIGINT, SIG_IGN);
112 sig = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, main_q);
115 dispatch_source_set_event_handler(sig, ^{
116 fprintf(stderr, "Terminating process due to signal\n");
119 dispatch_resume(sig);