]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/remote.c
a050abadfc42c761c5c0548012db917802be42c0
[apple/syslog.git] / syslogd.tproj / remote.c
1 /*
2 * Copyright (c) 2004-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <TargetConditionals.h>
25
26 #if TARGET_IPHONE_SIMULATOR
27 struct _not_empty;
28 #else
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <arpa/inet.h>
36 #include <sys/un.h>
37 #include <sys/uio.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <netdb.h>
45 #include <pthread.h>
46 #include <notify.h>
47 #include "daemon.h"
48
49 #define forever for(;;)
50
51 #define MY_ID "remote"
52 #define MAXLINE 4096
53 #define LOCKDOWN_PATH "/var/run/lockdown"
54 #define SYSLOG_SOCK_PATH "/var/run/lockdown/syslog.sock"
55 #define ASL_REMOTE_PORT 203
56
57 #define PRINT_STD 0
58 #define PRINT_RAW 1
59
60 #define WATCH_OFF 0
61 #define WATCH_LOCKDOWN_START 1
62 #define WATCH_RUN 2
63
64 #define SESSION_FLAGS_LOCKDOWN 0x00000001
65
66 #define MAXSOCK 1
67
68 static int rfd4 = -1;
69 static int rfd6 = -1;
70 static int rfdl = -1;
71
72 static dispatch_source_t in_src_local;
73 static dispatch_source_t in_src_tcp;
74 static dispatch_queue_t in_queue;
75
76 #ifdef NSS32
77 typedef uint32_t notify_state_t;
78 extern int notify_set_state(int, notify_state_t);
79 #else
80 typedef uint64_t notify_state_t;
81 #endif
82
83 extern size_t asl_memory_size(asl_memory_t *s);
84 extern uint32_t db_query(asl_msg_list_t *query, asl_msg_list_t **res, uint64_t startid, int count, uint32_t duration, int direction, uint64_t *lastid, int32_t ruid, int32_t rgid, int raccess);
85
86 extern void add_lockdown_session(int fd);
87 extern void remove_lockdown_session(int fd);
88
89 #define SESSION_WRITE(f,x) if (write(f, x, strlen(x)) < 0) goto exit_session
90
91 typedef struct
92 {
93 int sock;
94 uint32_t flags;
95 } session_args_t;
96
97 uint32_t
98 remote_db_size(uint32_t sel)
99 {
100 if (sel == DB_TYPE_FILE) return global.db_file_max;
101 if (sel == DB_TYPE_MEMORY) return global.db_memory_max;
102 return 0;
103 }
104
105 uint32_t
106 remote_db_set_size(uint32_t sel, uint32_t size)
107 {
108 if (sel == DB_TYPE_FILE) global.db_file_max = size;
109 if (sel == DB_TYPE_MEMORY) global.db_memory_max = size;
110 return 0;
111 }
112
113 asl_msg_t *
114 remote_db_stats(uint32_t sel)
115 {
116 asl_msg_t *m;
117 m = NULL;
118
119 if (sel == DB_TYPE_FILE) asl_store_statistics(global.file_db, &m);
120 if (sel == DB_TYPE_MEMORY) asl_memory_statistics(global.memory_db, &m);
121 return m;
122 }
123
124 void
125 session(void *x)
126 {
127 int i, s, wfd, status, pfmt, watch, wtoken, nfd, do_prompt;
128 asl_msg_list_t *res;
129 asl_msg_list_t ql;
130 uint32_t outlen;
131 asl_msg_t *stats;
132 asl_msg_t *query;
133 asl_msg_t *qlq[1];
134 char str[1024], *p, *qs, *out;
135 ssize_t len;
136 fd_set readfds, errfds;
137 uint64_t low_id, high_id;
138 uint32_t dbselect, flags;
139 session_args_t *sp;
140
141 if (x == NULL) pthread_exit(NULL);
142
143 sp = (session_args_t *)x;
144 s = sp->sock;
145 flags = sp->flags;
146 free(x);
147
148 asldebug("%s %d: starting interactive session for %ssocket %d\n", MY_ID, s, (flags & SESSION_FLAGS_LOCKDOWN) ? "lockdown " : "", s);
149
150 do_prompt = 1;
151 watch = WATCH_OFF;
152 wfd = -1;
153 wtoken = -1;
154
155 dbselect = 0;
156 if (global.dbtype & DB_TYPE_MEMORY) dbselect = DB_TYPE_MEMORY;
157 else if (global.dbtype & DB_TYPE_FILE) dbselect = DB_TYPE_FILE;
158
159 low_id = 0;
160 high_id = 0;
161
162 pfmt = PRINT_STD;
163 query = NULL;
164 memset(&ql, 0, sizeof(asl_msg_list_t));
165
166 if (flags & SESSION_FLAGS_LOCKDOWN) sleep(1);
167
168 snprintf(str, sizeof(str), "\n========================\nASL is here to serve you\n");
169 if (write(s, str, strlen(str)) < 0)
170 {
171 close(s);
172 pthread_exit(NULL);
173 return;
174 }
175
176 if (flags & SESSION_FLAGS_LOCKDOWN)
177 {
178 snprintf(str, sizeof(str), "> ");
179 SESSION_WRITE(s, str);
180 }
181
182 forever
183 {
184 if (((flags & SESSION_FLAGS_LOCKDOWN) == 0) && (do_prompt > 0))
185 {
186 snprintf(str, sizeof(str), "> ");
187 SESSION_WRITE(s, str);
188 }
189
190 do_prompt = 1;
191
192 memset(str, 0, sizeof(str));
193
194 FD_ZERO(&readfds);
195 FD_SET(s, &readfds);
196 FD_ZERO(&errfds);
197 FD_SET(s, &errfds);
198 nfd = s;
199
200 if (wfd != -1)
201 {
202 FD_SET(wfd, &readfds);
203 if (wfd > nfd) nfd = wfd;
204 }
205
206 status = select(nfd + 1, &readfds, NULL, &errfds, NULL);
207 if (status == 0) continue;
208 if (status < 0)
209 {
210 asldebug("%s %d: select %d %s\n", MY_ID, s, errno, strerror(errno));
211 goto exit_session;
212 }
213
214 if (FD_ISSET(s, &errfds))
215 {
216 asldebug("%s %d: error on socket %d\n", MY_ID, s, s);
217 goto exit_session;
218 }
219
220 if ((wfd != -1) && (FD_ISSET(wfd, &readfds)))
221 {
222 (void)read(wfd, &i, sizeof(int));
223 }
224
225 if (FD_ISSET(s, &errfds))
226 {
227 asldebug("%s %d: socket %d reported error\n", MY_ID, s, s);
228 goto exit_session;
229 }
230
231 if (FD_ISSET(s, &readfds))
232 {
233 len = read(s, str, sizeof(str) - 1);
234 if (len <= 0)
235 {
236 asldebug("%s %d: read error on socket %d: %d %s\n", MY_ID, s, s, errno, strerror(errno));
237 goto exit_session;
238 }
239
240 while ((len > 1) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
241 {
242 str[len - 1] = '\0';
243 len--;
244 }
245
246 if ((!strcmp(str, "q")) || (!strcmp(str, "quit")) || (!strcmp(str, "exit")))
247 {
248 snprintf(str, sizeof(str), "Goodbye\n");
249 write(s, str, strlen(str));
250 close(s);
251 s = -1;
252 break;
253 }
254
255 if ((!strcmp(str, "?")) || (!strcmp(str, "help")))
256 {
257 snprintf(str, sizeof(str), "Commands\n");
258 SESSION_WRITE(s, str);
259 snprintf(str, sizeof(str), " quit exit session\n");
260 SESSION_WRITE(s, str);
261 snprintf(str, sizeof(str), " select [val] get [set] current database\n");
262 SESSION_WRITE(s, str);
263 snprintf(str, sizeof(str), " val must be \"file\" or \"mem\"\n");
264 SESSION_WRITE(s, str);
265 snprintf(str, sizeof(str), " file [on/off] enable / disable file store\n");
266 SESSION_WRITE(s, str);
267 snprintf(str, sizeof(str), " memory [on/off] enable / disable memory store\n");
268 SESSION_WRITE(s, str);
269 snprintf(str, sizeof(str), " stats database statistics\n");
270 SESSION_WRITE(s, str);
271 snprintf(str, sizeof(str), " flush flush database\n");
272 SESSION_WRITE(s, str);
273 snprintf(str, sizeof(str), " dbsize [val] get [set] database size (# of records)\n");
274 SESSION_WRITE(s, str);
275 snprintf(str, sizeof(str), " watch print new messages as they arrive\n");
276 SESSION_WRITE(s, str);
277 snprintf(str, sizeof(str), " stop stop watching for new messages\n");
278 SESSION_WRITE(s, str);
279 snprintf(str, sizeof(str), " raw use raw format for printing messages\n");
280 SESSION_WRITE(s, str);
281 snprintf(str, sizeof(str), " std use standard format for printing messages\n");
282 SESSION_WRITE(s, str);
283 snprintf(str, sizeof(str), " * show all log messages\n");
284 SESSION_WRITE(s, str);
285 snprintf(str, sizeof(str), " * key val equality search for messages (single key/value pair)\n");
286 SESSION_WRITE(s, str);
287 snprintf(str, sizeof(str), " * op key val search for matching messages (single key/value pair)\n");
288 SESSION_WRITE(s, str);
289 snprintf(str, sizeof(str), " * [op key val] ... search for matching messages (multiple key/value pairs)\n");
290 SESSION_WRITE(s, str);
291 snprintf(str, sizeof(str), " operators: = < > ! (not equal) T (key exists) R (regex)\n");
292 SESSION_WRITE(s, str);
293 snprintf(str, sizeof(str), " modifiers (must follow operator):\n");
294 SESSION_WRITE(s, str);
295 snprintf(str, sizeof(str), " C=casefold N=numeric S=substring A=prefix Z=suffix\n");
296 SESSION_WRITE(s, str);
297 snprintf(str, sizeof(str), "\n");
298 SESSION_WRITE(s, str);
299 continue;
300 }
301 else if (!strcmp(str, "stats"))
302 {
303 stats = remote_db_stats(dbselect);
304 out = asl_format_message((asl_msg_t *)stats, ASL_MSG_FMT_RAW, ASL_TIME_FMT_SEC, ASL_ENCODE_NONE, &outlen);
305 write(s, out, outlen);
306 free(out);
307 asl_msg_release(stats);
308 continue;
309 }
310 else if (!strcmp(str, "flush"))
311 {}
312 else if (!strncmp(str, "select", 6))
313 {
314 p = str + 6;
315 while ((*p == ' ') || (*p == '\t')) p++;
316 if (*p == '\0')
317 {
318 if (dbselect == 0) snprintf(str, sizeof(str), "no store\n");
319 else if (dbselect == DB_TYPE_FILE) snprintf(str, sizeof(str), "file store\n");
320 else if (dbselect == DB_TYPE_MEMORY) snprintf(str, sizeof(str), "memory store\n");
321 SESSION_WRITE(s, str);
322 continue;
323 }
324
325 if (!strncmp(p, "file", 4))
326 {
327 if ((global.dbtype & DB_TYPE_FILE) == 0)
328 {
329 snprintf(str, sizeof(str), "file database is not enabled\n");
330 SESSION_WRITE(s, str);
331 continue;
332 }
333
334 dbselect = DB_TYPE_FILE;
335 }
336 else if (!strncmp(p, "mem", 3))
337 {
338 if ((global.dbtype & DB_TYPE_MEMORY) == 0)
339 {
340 snprintf(str, sizeof(str), "memory database is not enabled\n");
341 SESSION_WRITE(s, str);
342 continue;
343 }
344
345 dbselect = DB_TYPE_MEMORY;
346 }
347 else
348 {
349 snprintf(str, sizeof(str), "unknown database type\n");
350 SESSION_WRITE(s, str);
351 continue;
352 }
353
354 snprintf(str, sizeof(str), "OK\n");
355 SESSION_WRITE(s, str);
356 continue;
357 }
358 else if (!strncmp(str, "file", 4))
359 {
360 p = str + 4;
361 while ((*p == ' ') || (*p == '\t')) p++;
362 if (*p == '\0')
363 {
364 snprintf(str, sizeof(str), "file database is %senabled\n", (global.dbtype & DB_TYPE_FILE) ? "" : "not ");
365 SESSION_WRITE(s, str);
366 if ((global.dbtype & DB_TYPE_FILE) != 0) dbselect = DB_TYPE_FILE;
367 continue;
368 }
369
370 if (!strcmp(p, "on")) global.dbtype |= DB_TYPE_FILE;
371 else if (!strcmp(p, "off")) global.dbtype &= ~ DB_TYPE_FILE;
372
373 snprintf(str, sizeof(str), "OK\n");
374 SESSION_WRITE(s, str);
375 continue;
376 }
377 else if (!strncmp(str, "memory", 6))
378 {
379 p = str + 6;
380 while ((*p == ' ') || (*p == '\t')) p++;
381 if (*p == '\0')
382 {
383 snprintf(str, sizeof(str), "memory database is %senabled\n", (global.dbtype & DB_TYPE_MEMORY) ? "" : "not ");
384 SESSION_WRITE(s, str);
385 if ((global.dbtype & DB_TYPE_MEMORY) != 0) dbselect = DB_TYPE_MEMORY;
386 continue;
387 }
388
389 if (!strcmp(p, "on")) global.dbtype |= DB_TYPE_MEMORY;
390 else if (!strcmp(p, "off")) global.dbtype &= ~ DB_TYPE_MEMORY;
391
392 snprintf(str, sizeof(str), "OK\n");
393 SESSION_WRITE(s, str);
394 continue;
395 }
396 else if (!strncmp(str, "dbsize", 6))
397 {
398 if (dbselect == 0)
399 {
400 snprintf(str, sizeof(str), "no store\n");
401 SESSION_WRITE(s, str);
402 continue;
403 }
404
405 p = str + 6;
406 while ((*p == ' ') || (*p == '\t')) p++;
407 if (*p == '\0')
408 {
409 snprintf(str, sizeof(str), "DB size %u\n", remote_db_size(dbselect));
410 SESSION_WRITE(s, str);
411 continue;
412 }
413
414 i = atoi(p);
415 remote_db_set_size(dbselect, i);
416
417 snprintf(str, sizeof(str), "OK\n");
418 SESSION_WRITE(s, str);
419 continue;
420 }
421 else if (!strcmp(str, "stop"))
422 {
423 if (watch != WATCH_OFF)
424 {
425 watch = WATCH_OFF;
426 notify_cancel(wtoken);
427 wfd = -1;
428 wtoken = -1;
429
430 low_id = 0;
431 high_id = 0;
432
433 if (query != NULL) free(query);
434 query = NULL;
435
436 snprintf(str, sizeof(str), "OK\n");
437 SESSION_WRITE(s, str);
438 continue;
439 }
440
441 snprintf(str, sizeof(str), "not watching!\n");
442 SESSION_WRITE(s, str);
443 continue;
444 }
445 else if (!strcmp(str, "raw"))
446 {
447 pfmt = PRINT_RAW;
448 continue;
449 }
450 else if (!strcmp(str, "std"))
451 {
452 pfmt = PRINT_STD;
453 continue;
454 }
455 else if (!strcmp(str, "watch"))
456 {
457 if (((flags & SESSION_FLAGS_LOCKDOWN) == 0) && (watch != WATCH_OFF))
458 {
459 snprintf(str, sizeof(str), "already watching!\n");
460 SESSION_WRITE(s, str);
461 continue;
462 }
463
464 if (flags & SESSION_FLAGS_LOCKDOWN)
465 {
466 /*
467 * If this session is PurpleConsole or Xcode watching for log messages,
468 * we pass through the bottom of the loop (below) once to pick up
469 * existing messages already in memory. After that, dbserver will
470 * send new messages in send_to_direct_watchers(). We wait until
471 * the initial messages are sent before adding the connection to
472 * global.lockdown_session_fds to allow this query to complete before
473 * dbserver starts sending. To prevent a race between this query and
474 * when messages are sent by send_to_direct_watchers, we suspend the
475 * work queue and resume it when lockdown_session_fds has been updated.
476 */
477 watch = WATCH_LOCKDOWN_START;
478 dispatch_suspend(global.work_queue);
479 }
480 else
481 {
482 status = notify_register_file_descriptor(kNotifyASLDBUpdate, &wfd, 0, &wtoken);
483 if (status != 0)
484 {
485 snprintf(str, sizeof(str), "notify_register_file_descriptor failed: %d\n", status);
486 SESSION_WRITE(s, str);
487 continue;
488 }
489
490 watch = WATCH_RUN;
491 }
492
493 snprintf(str, sizeof(str), "OK\n");
494 SESSION_WRITE(s, str);
495 do_prompt = 2;
496 }
497 else if ((str[0] == '*') || (str[0] == 'T') || (str[0] == '=') || (str[0] == '!') || (str[0] == '<') || (str[0] == '>'))
498 {
499 memset(&ql, 0, sizeof(asl_msg_list_t));
500 if (query != NULL) free(query);
501 query = NULL;
502
503 p = str;
504 if (*p == '*') p++;
505 while ((*p == ' ') || (*p == '\t')) p++;
506
507 if (*p == '\0')
508 {
509 /* NULL query */
510 }
511 else if (*p == '[')
512 {
513 qs = NULL;
514 asprintf(&qs, "Q %s", p);
515 query = asl_msg_from_string(qs);
516 free(qs);
517 }
518 else if ((*p == 'T') || (*p == '=') || (*p == '!') || (*p == '<') || (*p == '>') || (*p == 'R'))
519 {
520 qs = NULL;
521 asprintf(&qs, "Q [%s]", p);
522 query = asl_msg_from_string(qs);
523 free(qs);
524 }
525 else
526 {
527 qs = NULL;
528 asprintf(&qs, "Q [= %s]", p);
529 query = asl_msg_from_string(qs);
530 free(qs);
531 }
532 }
533 else
534 {
535 snprintf(str, sizeof(str), "unrecognized command\n");
536 SESSION_WRITE(s, str);
537 snprintf(str, sizeof(str), "enter \"help\" for help\n");
538 SESSION_WRITE(s, str);
539 continue;
540 }
541 }
542
543 if ((flags & SESSION_FLAGS_LOCKDOWN) && (watch == WATCH_RUN)) continue;
544
545 /* Bottom of the loop: do a database query and print the results */
546
547 if (query != NULL)
548 {
549 ql.count = 1;
550 qlq[0] = query;
551 ql.msg = qlq;
552 }
553
554 if (watch == WATCH_OFF) low_id = 0;
555
556 res = NULL;
557 high_id = 0;
558 (void)db_query(&ql, &res, low_id, 0, 0, 0, &high_id, 0, 0, 0);
559
560 if ((watch == WATCH_RUN) && (high_id >= low_id)) low_id = high_id + 1;
561
562 if (res == NULL)
563 {
564 if (watch == WATCH_OFF)
565 {
566 snprintf(str, sizeof(str), "-nil-\n");
567 SESSION_WRITE(s, str);
568 }
569 else
570 {
571 if (do_prompt != 2) do_prompt = 0;
572 }
573 }
574 else if (pfmt == PRINT_RAW)
575 {
576 if (watch == WATCH_RUN)
577 {
578 snprintf(str, sizeof(str), "\n");
579 SESSION_WRITE(s, str);
580 }
581
582 outlen = 0;
583 out = asl_msg_list_to_string(res, &outlen);
584 write(s, out, outlen);
585 free(out);
586
587 snprintf(str, sizeof(str), "\n");
588 SESSION_WRITE(s, str);
589 }
590 else
591 {
592 if ((watch == WATCH_RUN) || (watch == WATCH_LOCKDOWN_START))
593 {
594 snprintf(str, sizeof(str), "\n");
595 SESSION_WRITE(s, str);
596 }
597
598 snprintf(str, sizeof(str), "\n");
599 for (i = 0; i < res->count; i++)
600 {
601 int wstatus;
602
603 out = asl_format_message(res->msg[i], ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_ENCODE_SAFE, &outlen);
604
605 do
606 {
607 int n = 0;
608
609 errno = 0;
610 wstatus = write(s, out, outlen);
611 if (wstatus < 0)
612 {
613 asldebug("%s %d: %d/%d write data failed: %d %s\n", MY_ID, s, i, res->count, errno, strerror(errno));
614 if (errno == EAGAIN)
615 {
616 n++;
617 if (n > 10) break;
618 usleep(10000);
619 }
620 else
621 {
622 goto exit_session;
623 }
624 }
625 } while (errno == EAGAIN);
626
627 free(out);
628 if (global.remote_delay_time > 0) usleep(global.remote_delay_time);
629 }
630 }
631
632 asl_msg_list_release(res);
633
634 if (watch == WATCH_LOCKDOWN_START)
635 {
636 add_lockdown_session(s);
637 watch = WATCH_RUN;
638 dispatch_resume(global.work_queue);
639 }
640 }
641
642 exit_session:
643
644 asldebug("%s %d: terminating session for %ssocket %d\n", MY_ID, s, (flags & SESSION_FLAGS_LOCKDOWN) ? "lockdown " : "", s);
645
646 if (s >= 0)
647 {
648 if (flags & SESSION_FLAGS_LOCKDOWN) remove_lockdown_session(s);
649 close(s);
650 }
651
652 if (watch == WATCH_LOCKDOWN_START) dispatch_resume(global.work_queue);
653 if (wtoken >= 0) notify_cancel(wtoken);
654 if (query != NULL) asl_msg_release(query);
655 pthread_exit(NULL);
656 }
657
658 asl_msg_t *
659 remote_acceptmsg(int fd, int tcp)
660 {
661 socklen_t fromlen;
662 int s, flags, status, v;
663 pthread_attr_t attr;
664 pthread_t t;
665 struct sockaddr_storage from;
666 session_args_t *sp;
667
668 fromlen = sizeof(struct sockaddr_un);
669 if (tcp == 1) fromlen = sizeof(struct sockaddr_storage);
670
671 memset(&from, 0, sizeof(from));
672
673 s = accept(fd, (struct sockaddr *)&from, &fromlen);
674 if (s == -1)
675 {
676 asldebug("%s: accept: %s\n", MY_ID, strerror(errno));
677 return NULL;
678 }
679
680 flags = fcntl(s, F_GETFL, 0);
681 flags &= ~ O_NONBLOCK;
682 status = fcntl(s, F_SETFL, flags);
683 if (status < 0)
684 {
685 asldebug("%s: fcntl: %s\n", MY_ID, strerror(errno));
686 close(s);
687 return NULL;
688 }
689
690 v = 1;
691 setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &v, sizeof(v));
692
693 if (tcp == 1)
694 {
695 flags = 1;
696 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(int));
697 }
698
699 sp = (session_args_t *)calloc(1, sizeof(session_args_t));
700 if (sp == NULL)
701 {
702 asldebug("%s: malloc: %s\n", MY_ID, strerror(errno));
703 close(s);
704 return NULL;
705 }
706
707 sp->sock = s;
708 if (tcp == 0) sp->flags |= SESSION_FLAGS_LOCKDOWN;
709
710 pthread_attr_init(&attr);
711 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
712 pthread_create(&t, &attr, (void *(*)(void *))session, (void *)sp);
713 pthread_attr_destroy(&attr);
714
715 return NULL;
716 }
717
718 asl_msg_t *
719 remote_acceptmsg_local(int fd)
720 {
721 return remote_acceptmsg(fd, 0);
722 }
723
724 asl_msg_t *
725 remote_acceptmsg_tcp(int fd)
726 {
727 return remote_acceptmsg(fd, 1);
728 }
729
730 int
731 remote_init_lockdown(void)
732 {
733 int status, reuse, fd;
734 struct sockaddr_un local;
735
736 fd = socket(AF_UNIX, SOCK_STREAM, 0);
737 if (fd < 0)
738 {
739 asldebug("%s: socket: %s\n", MY_ID, strerror(errno));
740 return -1;
741 }
742
743 reuse = 1;
744 status = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(int));
745 if (status < 0)
746 {
747 asldebug("%s: setsockopt: %s\n", MY_ID, strerror(errno));
748 close(fd);
749 return -1;
750 }
751
752 /* make sure the lockdown directory exists */
753 mkdir(LOCKDOWN_PATH, 0777);
754
755 memset(&local, 0, sizeof(local));
756 local.sun_family = AF_UNIX;
757 strlcpy(local.sun_path, SYSLOG_SOCK_PATH, sizeof(local.sun_path));
758 unlink(local.sun_path);
759
760 status = bind(fd, (struct sockaddr *)&local, sizeof(local.sun_family) + sizeof(local.sun_path));
761
762 if (status < 0)
763 {
764 asldebug("%s: bind: %s\n", MY_ID, strerror(errno));
765 close(fd);
766 return -1;
767 }
768
769 status = fcntl(fd, F_SETFL, O_NONBLOCK);
770 if (status < 0)
771 {
772 asldebug("%s: fcntl: %s\n", MY_ID, strerror(errno));
773 close(fd);
774 return -1;
775 }
776
777 status = listen(fd, 5);
778 if (status < 0)
779 {
780 asldebug("%s: listen: %s\n", MY_ID, strerror(errno));
781 close(fd);
782 return -1;
783 }
784
785 chmod(SYSLOG_SOCK_PATH, 0666);
786
787 in_src_local = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, in_queue);
788 dispatch_source_set_event_handler(in_src_local, ^{ remote_acceptmsg_local(fd); });
789 dispatch_resume(in_src_local);
790
791 return fd;
792 }
793
794 int
795 remote_init_tcp(int family)
796 {
797 int status, reuse, fd;
798 struct sockaddr_in a4;
799 struct sockaddr_in6 a6;
800 struct sockaddr *s;
801 socklen_t len;
802
803 fd = socket(family, SOCK_STREAM, 0);
804 if (fd < 0)
805 {
806 asldebug("%s: socket: %s\n", MY_ID, strerror(errno));
807 return -1;
808 }
809
810 reuse = 1;
811 status = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(int));
812 if (status < 0)
813 {
814 asldebug("%s: setsockopt: %s\n", MY_ID, strerror(errno));
815 close(fd);
816 return -1;
817 }
818
819 memset(&(a4.sin_addr), 0, sizeof(struct in_addr));
820 a4.sin_family = AF_INET;
821 a4.sin_port = htons(ASL_REMOTE_PORT);
822
823 memset(&(a6.sin6_addr), 0, sizeof(struct in6_addr));
824 a6.sin6_family = AF_INET6;
825 a6.sin6_port = htons(ASL_REMOTE_PORT);
826
827 s = (struct sockaddr *)&a4;
828 len = sizeof(struct sockaddr_in);
829
830 if (family == AF_INET6)
831 {
832 s = (struct sockaddr *)&a6;
833 len = sizeof(struct sockaddr_in6);
834 }
835
836 status = bind(fd, s, len);
837 if (status < 0)
838 {
839 asldebug("%s: bind: %s\n", MY_ID, strerror(errno));
840 close(fd);
841 return -1;
842 }
843
844 status = fcntl(fd, F_SETFL, O_NONBLOCK);
845 if (status < 0)
846 {
847 asldebug("%s: fcntl: %s\n", MY_ID, strerror(errno));
848 close(fd);
849 return -1;
850 }
851
852 status = listen(fd, 5);
853 if (status < 0)
854 {
855 asldebug("%s: listen: %s\n", MY_ID, strerror(errno));
856 close(fd);
857 return -1;
858 }
859
860 in_src_tcp = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, in_queue);
861 dispatch_source_set_event_handler(in_src_tcp, ^{ remote_acceptmsg_tcp(fd); });
862 dispatch_resume(in_src_tcp);
863
864 return fd;
865 }
866
867 int
868 remote_init(void)
869 {
870 static dispatch_once_t once;
871
872 dispatch_once(&once, ^{
873 in_queue = dispatch_queue_create(MY_ID, NULL);
874 });
875
876 asldebug("%s: init\n", MY_ID);
877
878 #ifdef LOCKDOWN
879 rfdl = remote_init_lockdown();
880 #endif
881
882 #ifdef REMOTE_IPV4
883 rfd4 = remote_init_tcp(AF_INET);
884 #endif
885
886 #ifdef REMOTE_IPV6
887 rfd6 = remote_init_tcp(AF_INET6);
888 #endif
889
890 return 0;
891 }
892
893 int
894 remote_close(void)
895 {
896 if (rfdl >= 0)
897 {
898 close(rfdl);
899 }
900
901 rfdl = -1;
902
903 if (rfd4 >= 0)
904 {
905 close(rfd4);
906 }
907
908 rfd4 = -1;
909
910 if (rfd6 >= 0)
911 {
912 close(rfd6);
913 }
914
915 rfd6 = -1;
916
917 return 0;
918 }
919
920 int
921 remote_reset(void)
922 {
923 return 0;
924
925 remote_close();
926 return remote_init();
927 }
928
929 #endif /* !TARGET_IPHONE_SIMULATOR */