dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / dyld_process_info_notify.dtest / target.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <dlfcn.h>
5 #include <libgen.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <mach/mach.h>
9 #include <sys/param.h>
10 #include <dispatch/dispatch.h>
11
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
15
16 // This process will clean itself up in the event its parent dies
17
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, ^{
22 exit(0);
23 });
24 dispatch_resume(parentDeathSource);
25
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, ^{
30 exit(0);
31 });
32 dispatch_resume(exitSignalSource);
33
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);
40 dlclose(h);
41 }
42 kill(getppid(), SIGUSR2);
43 });
44 dispatch_resume(signalSource);
45
46 // Message our parent to let them know our signal handlers are ready
47 kill(getppid(), SIGUSR1);
48 dispatch_main();
49 }
50