10 #include <dispatch/dispatch.h>
12 // The process starts, then sends its parent a SIGUSR1 to indiicate it is ready
13 // At that point it waits for SIGUSR1, and when it recieves one it loads and unloads libfoo.dylib 3 times
14 // The process remains running until it recieves a SIGTERM
16 // This process will clean itself up in the event its parent dies
18 int main(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]) {
19 // Setup parent death handler
20 dispatch_source_t parentDeathSource
= dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC
, getppid(), DISPATCH_PROC_EXIT
, dispatch_get_main_queue());
21 dispatch_source_set_event_handler(parentDeathSource
, ^{
24 dispatch_resume(parentDeathSource
);
26 // Setup SIGTERM handler
27 signal(SIGTERM
, SIG_IGN
);
28 dispatch_source_t exitSignalSource
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, SIGTERM
, 0, dispatch_get_main_queue());
29 dispatch_source_set_event_handler(exitSignalSource
, ^{
32 dispatch_resume(exitSignalSource
);
34 // Setup SIGUSR1 handler
35 signal(SIGUSR1
, SIG_IGN
);
36 dispatch_source_t signalSource
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, SIGUSR1
, 0, dispatch_get_main_queue());
37 dispatch_source_set_event_handler(signalSource
, ^{
38 for (int i
=0; i
< 3; ++i
) {
39 void* h
= dlopen(RUN_DIR
"/libfoo.dylib", 0);
42 kill(getppid(), SIGUSR2
);
44 dispatch_resume(signalSource
);
46 // Message our parent to let them know our signal handlers are ready
47 kill(getppid(), SIGUSR1
);