]> git.saurik.com Git - apple/launchd.git/blob - launchd/doc/sampled.c
launchd-152.tar.gz
[apple/launchd.git] / launchd / doc / sampled.c
1 #include <mach/mach.h>
2 #include <sys/types.h>
3 #include <sys/event.h>
4 #include <sys/socket.h>
5 #include <sys/time.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <syslog.h>
14 #include <libgen.h>
15
16 #include "launch.h"
17
18 static void
19 ack_mach_port(launch_data_t o, const char *name, void *context __attribute__((unused)))
20 {
21 mach_port_t p = launch_data_get_machport(o);
22
23 mach_port_deallocate(mach_task_self(), p);
24
25 syslog(LOG_NOTICE, "Ignoring Mach service: %s", name);
26 }
27
28 int
29 main(void)
30 {
31 struct timespec timeout = { 60, 0 };
32 struct sockaddr_storage ss;
33 socklen_t slen = sizeof(ss);
34 struct kevent kev;
35 launch_data_t tmp, resp, msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
36 size_t i;
37 int kq;
38
39 openlog(getprogname(), LOG_PERROR|LOG_PID|LOG_CONS, LOG_DAEMON);
40
41 if (-1 == (kq = kqueue())) {
42 syslog(LOG_ERR, "kqueue(): %m");
43 exit(EXIT_FAILURE);
44 }
45
46 if ((resp = launch_msg(msg)) == NULL) {
47 syslog(LOG_ERR, "launch_msg(\"" LAUNCH_KEY_CHECKIN "\") IPC failure: %m");
48 exit(EXIT_FAILURE);
49 }
50
51 if (LAUNCH_DATA_ERRNO == launch_data_get_type(resp)) {
52 errno = launch_data_get_errno(resp);
53 if (errno == EACCES)
54 syslog(LOG_ERR, "Check-in failed. Did you forget to set ServiceIPC == true in your plist?");
55 else
56 syslog(LOG_ERR, "Check-in failed: %m");
57 exit(EXIT_FAILURE);
58 }
59
60 tmp = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_TIMEOUT);
61 if (tmp)
62 timeout.tv_sec = launch_data_get_integer(tmp);
63
64 tmp = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_MACHSERVICES);
65 if (tmp) {
66 launch_data_dict_iterate(tmp, ack_mach_port, NULL);
67 }
68
69 tmp = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_SOCKETS);
70 if (NULL == tmp) {
71 syslog(LOG_ERR, "No sockets found to answer requests on!");
72 exit(EXIT_FAILURE);
73 }
74
75 if (launch_data_dict_get_count(tmp) > 1) {
76 syslog(LOG_WARNING, "Some sockets will be ignored!");
77 }
78
79 tmp = launch_data_dict_lookup(tmp, "SampleListeners");
80 if (NULL == tmp) {
81 syslog(LOG_ERR, "No known sockets found to answer requests on!");
82 exit(EXIT_FAILURE);
83 }
84
85 for (i = 0; i < launch_data_array_get_count(tmp); i++) {
86 launch_data_t tmpi = launch_data_array_get_index(tmp, i);
87
88 EV_SET(&kev, launch_data_get_fd(tmpi), EVFILT_READ, EV_ADD, 0, 0, NULL);
89 if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) {
90 syslog(LOG_DEBUG, "kevent(): %m");
91 exit(EXIT_FAILURE);
92 }
93 }
94
95 launch_data_free(msg);
96 launch_data_free(resp);
97
98 for (;;) {
99 FILE *c;
100 int r;
101
102 if ((r = kevent(kq, NULL, 0, &kev, 1, &timeout)) == -1) {
103 syslog(LOG_ERR, "kevent(): %m");
104 exit(EXIT_FAILURE);
105 } else if (r == 0) {
106 exit(EXIT_SUCCESS);
107 }
108
109 if ((r = accept(kev.ident, (struct sockaddr *)&ss, &slen)) == -1) {
110 syslog(LOG_ERR, "accept(): %m");
111 continue; /* this isn't fatal */
112 }
113
114 c = fdopen(r, "r+");
115
116 if (c) {
117 fprintf(c, "hello world!\n");
118 fclose(c);
119 } else {
120 close(r);
121 }
122 }
123 }