]>
git.saurik.com Git - apple/launchd.git/blob - launchd/doc/sampled.c
3 #include <sys/socket.h>
19 struct timespec timeout
= { 60, 0 };
20 struct sockaddr_storage ss
;
21 socklen_t slen
= sizeof(ss
);
23 launch_data_t tmp
, resp
, msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
27 openlog(getprogname(), LOG_PERROR
|LOG_PID
|LOG_CONS
, LOG_DAEMON
);
29 if (-1 == (kq
= kqueue())) {
30 syslog(LOG_ERR
, "kqueue(): %m");
34 if ((resp
= launch_msg(msg
)) == NULL
) {
35 syslog(LOG_ERR
, "launch_msg(\"" LAUNCH_KEY_CHECKIN
"\") IPC failure: %m");
39 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(resp
)) {
40 errno
= launch_data_get_errno(resp
);
41 syslog(LOG_ERR
, "Check-in failed: %m");
45 tmp
= launch_data_dict_lookup(resp
, LAUNCH_JOBKEY_TIMEOUT
);
47 timeout
.tv_sec
= launch_data_get_integer(tmp
);
49 tmp
= launch_data_dict_lookup(resp
, LAUNCH_JOBKEY_SOCKETS
);
51 syslog(LOG_ERR
, "No sockets found to answer requests on!");
55 if (launch_data_dict_get_count(tmp
) > 1) {
56 syslog(LOG_WARNING
, "Some sockets will be ignored!");
59 tmp
= launch_data_dict_lookup(tmp
, "SampleListeners");
61 syslog(LOG_ERR
, "No known sockets found to answer requests on!");
65 for (i
= 0; i
< launch_data_array_get_count(tmp
); i
++) {
66 launch_data_t tmpi
= launch_data_array_get_index(tmp
, i
);
68 EV_SET(&kev
, launch_data_get_fd(tmpi
), EVFILT_READ
, EV_ADD
, 0, 0, NULL
);
69 if (kevent(kq
, &kev
, 1, NULL
, 0, NULL
) == -1) {
70 syslog(LOG_DEBUG
, "kevent(): %m");
75 launch_data_free(msg
);
76 launch_data_free(resp
);
82 if ((r
= kevent(kq
, NULL
, 0, &kev
, 1, &timeout
)) == -1) {
83 syslog(LOG_ERR
, "kevent(): %m");
89 if ((r
= accept(kev
.ident
, (struct sockaddr
*)&ss
, &slen
)) == -1) {
90 syslog(LOG_ERR
, "accept(): %m");
91 continue; /* this isn't fatal */
97 fprintf(c
, "hello world!\n");