4 #include <sys/socket.h>
19 ack_mach_port(launch_data_t o
, const char *name
, void *context
__attribute__((unused
)))
21 mach_port_t p
= launch_data_get_machport(o
);
23 mach_port_deallocate(mach_task_self(), p
);
25 syslog(LOG_NOTICE
, "Ignoring Mach service: %s", name
);
31 struct timespec timeout
= { 60, 0 };
32 struct sockaddr_storage ss
;
33 socklen_t slen
= sizeof(ss
);
35 launch_data_t tmp
, resp
, msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
39 openlog(getprogname(), LOG_PERROR
|LOG_PID
|LOG_CONS
, LOG_DAEMON
);
41 if (-1 == (kq
= kqueue())) {
42 syslog(LOG_ERR
, "kqueue(): %m");
46 if ((resp
= launch_msg(msg
)) == NULL
) {
47 syslog(LOG_ERR
, "launch_msg(\"" LAUNCH_KEY_CHECKIN
"\") IPC failure: %m");
51 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(resp
)) {
52 errno
= launch_data_get_errno(resp
);
54 syslog(LOG_ERR
, "Check-in failed. Did you forget to set ServiceIPC == true in your plist?");
56 syslog(LOG_ERR
, "Check-in failed: %m");
60 tmp
= launch_data_dict_lookup(resp
, LAUNCH_JOBKEY_TIMEOUT
);
62 timeout
.tv_sec
= launch_data_get_integer(tmp
);
64 tmp
= launch_data_dict_lookup(resp
, LAUNCH_JOBKEY_MACHSERVICES
);
66 launch_data_dict_iterate(tmp
, ack_mach_port
, NULL
);
69 tmp
= launch_data_dict_lookup(resp
, LAUNCH_JOBKEY_SOCKETS
);
71 syslog(LOG_ERR
, "No sockets found to answer requests on!");
75 if (launch_data_dict_get_count(tmp
) > 1) {
76 syslog(LOG_WARNING
, "Some sockets will be ignored!");
79 tmp
= launch_data_dict_lookup(tmp
, "SampleListeners");
81 syslog(LOG_ERR
, "No known sockets found to answer requests on!");
85 for (i
= 0; i
< launch_data_array_get_count(tmp
); i
++) {
86 launch_data_t tmpi
= launch_data_array_get_index(tmp
, i
);
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");
95 launch_data_free(msg
);
96 launch_data_free(resp
);
102 if ((r
= kevent(kq
, NULL
, 0, &kev
, 1, &timeout
)) == -1) {
103 syslog(LOG_ERR
, "kevent(): %m");
109 if ((r
= accept(kev
.ident
, (struct sockaddr
*)&ss
, &slen
)) == -1) {
110 syslog(LOG_ERR
, "accept(): %m");
111 continue; /* this isn't fatal */
117 fprintf(c
, "hello world!\n");