]> git.saurik.com Git - apple/launchd.git/blame - launchd/testing/spawn_via_launchd.c
launchd-152.tar.gz
[apple/launchd.git] / launchd / testing / spawn_via_launchd.c
CommitLineData
ed34e3c3
A
1#include <mach/mach.h>
2#include <servers/bootstrap.h>
3#include <sys/types.h>
4#include <sys/time.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdbool.h>
8#include <unistd.h>
9#include <errno.h>
10#include <string.h>
11#include <signal.h>
12#include <assert.h>
13#include <launch.h>
14#include <launch_priv.h>
15
16int
17main(int argc, const char *const *argv)
18{
19 struct timeval tvs, tve, tvd;
20 mach_port_t mpo = MACH_PORT_NULL;
21 int wstatus;
22 pid_t p;
23
24 struct spawn_via_launchd_attr attrs;
25 memset(&attrs, 0, sizeof(attrs));
26 attrs.spawn_flags = SPAWN_VIA_LAUNCHD_STOPPED; // | SPAWN_VIA_LAUNCHD_FORCE_PPC;
27 attrs.spawn_observer_port = &mpo;
28#if 0
29 const char *const env[] = { "FOO=bar", "ROCK=roll", NULL };
30 attrs.spawn_path = "/bin/booger";
31 attrs.spawn_chdir = "/Users/me";
32 attrs.spawn_env = env;
33#endif
34
35 gettimeofday(&tvs, NULL);
36 p = spawn_via_launchd("com.example.booger", argv + 1, &attrs);
37
38 if (p == -1) {
39 fprintf(stderr, "spawn_via_launchd(): %s\n", strerror(errno));
40 exit(EXIT_FAILURE);
41 }
42
43 kill(p, SIGCONT);
44 gettimeofday(&tve, NULL);
45 timersub(&tve, &tvs, &tvd);
46 fprintf(stdout, "p == %d mpo = 0x%x in %ld usec\n", p, mpo, tvd.tv_sec * 1000000 + tvd.tv_usec);
47
48 assert(mpm_wait(mpo, &wstatus) == 0);
49
50 fprintf(stdout, "wstatus == %d\n", wstatus);
51
52 exit(EXIT_SUCCESS);
53}