]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/syslogd.c
81984746e19213579c7a71610e6f55f19e2c5bc4
[apple/syslog.git] / syslogd.tproj / syslogd.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 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <mach/mach.h>
32 #include <mach/mach_error.h>
33 #include <mach/mach_time.h>
34 #include <servers/bootstrap.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/stat.h>
39 #include <sys/fcntl.h>
40 #include <sys/errno.h>
41 #include <sys/queue.h>
42 #include <sys/time.h>
43 #include <sys/un.h>
44 #include <pthread.h>
45 #include <dirent.h>
46 #include <dlfcn.h>
47 #include <libgen.h>
48 #include <notify.h>
49 #include <notify_keys.h>
50 #include <utmpx.h>
51 #include <asl_private.h>
52 #include <pwd.h>
53
54 #if !TARGET_OS_IPHONE
55 #include <quarantine.h>
56 #endif
57 #include "daemon.h"
58
59 #define SERVICE_NAME "com.apple.system.logger"
60 #define SERVER_STATUS_ERROR -1
61 #define SERVER_STATUS_INACTIVE 0
62 #define SERVER_STATUS_ACTIVE 1
63 #define SERVER_STATUS_ON_DEMAND 2
64
65 #define BILLION 1000000000
66
67 #define NOTIFY_DELAY 1
68
69 extern int _malloc_no_asl_log;
70
71 #if TARGET_IPHONE_SIMULATOR
72 const char *_path_pidfile;
73 const char *_path_syslogd_log;
74 #endif
75
76 /* global */
77 struct global_s global;
78
79 #if !TARGET_IPHONE_SIMULATOR
80 /* Input Modules */
81 int klog_in_init(void);
82 int klog_in_reset(void);
83 int klog_in_close(void);
84 static int activate_klog_in = 1;
85 #endif
86
87 int bsd_in_init(void);
88 int bsd_in_reset(void);
89 int bsd_in_close(void);
90 static int activate_bsd_in = 1;
91
92 #if !TARGET_IPHONE_SIMULATOR
93 int udp_in_init(void);
94 int udp_in_reset(void);
95 int udp_in_close(void);
96 static int activate_udp_in = 1;
97
98 /* Output Modules */
99 int bsd_out_init(void);
100 int bsd_out_reset(void);
101 int bsd_out_close(void);
102 static int activate_bsd_out = 1;
103 #endif
104
105 int asl_action_init(void);
106 int asl_action_reset(void);
107 int asl_action_close(void);
108 static int activate_asl_action = 1;
109
110 #if !TARGET_IPHONE_SIMULATOR
111 /* Interactive Module */
112 int remote_init(void);
113 int remote_reset(void);
114 int remote_close(void);
115 static int remote_enabled = 0;
116 #endif
117
118 extern void database_server();
119
120 static void
121 init_modules()
122 {
123 #if !TARGET_IPHONE_SIMULATOR
124 module_t *m_klog_in, *m_bsd_out, *m_udp_in, *m_remote;
125 #endif
126 module_t *m_asl, *m_bsd_in;
127 int m = 0;
128
129 /* ASL module (configured by /etc/asl.conf) */
130 m_asl = (module_t *)calloc(1, sizeof(module_t));
131 if (m_asl == NULL)
132 {
133 asldebug("alloc failed (init_modules asl_action)\n");
134 exit(1);
135 }
136
137 m_asl->name = "asl_action";
138 m_asl->enabled = activate_asl_action;
139 m_asl->init = asl_action_init;
140 m_asl->reset = asl_action_reset;
141 m_asl->close = asl_action_close;
142
143 if (m_asl->enabled) m_asl->init();
144
145 #if !TARGET_IPHONE_SIMULATOR
146 /* BSD output module (configured by /etc/syslog.conf) */
147 m_bsd_out = (module_t *)calloc(1, sizeof(module_t));
148 if (m_bsd_out == NULL)
149 {
150 asldebug("alloc failed (init_modules bsd_out)\n");
151 exit(1);
152 }
153
154 m_bsd_out->name = "bsd_out";
155 m_bsd_out->enabled = activate_bsd_out;
156 m_bsd_out->init = bsd_out_init;
157 m_bsd_out->reset = bsd_out_reset;
158 m_bsd_out->close = bsd_out_close;
159
160 if (m_bsd_out->enabled)
161 {
162 m_bsd_out->init();
163 global.bsd_out_enabled = 1;
164 }
165
166 /* kernel input module */
167 m_klog_in = (module_t *)calloc(1, sizeof(module_t));
168 if (m_klog_in == NULL)
169 {
170 asldebug("alloc failed (init_modules klog_in)\n");
171 exit(1);
172 }
173
174 m_klog_in->name = "klog_in";
175 m_klog_in->enabled = activate_klog_in;
176 m_klog_in->init = klog_in_init;
177 m_klog_in->reset = klog_in_reset;
178 m_klog_in->close = klog_in_close;
179
180 if (m_klog_in->enabled) m_klog_in->init();
181 #endif
182
183 /* BSD (UNIX domain socket) input module */
184 m_bsd_in = (module_t *)calloc(1, sizeof(module_t));
185 if (m_bsd_in == NULL)
186 {
187 asldebug("alloc failed (init_modules bsd_in)\n");
188 exit(1);
189 }
190
191 m_bsd_in->name = "bsd_in";
192 m_bsd_in->enabled = activate_bsd_in;
193 m_bsd_in->init = bsd_in_init;
194 m_bsd_in->reset = bsd_in_reset;
195 m_bsd_in->close = bsd_in_close;
196
197 if (m_bsd_in->enabled) m_bsd_in->init();
198
199 #if !TARGET_IPHONE_SIMULATOR
200 /* network (syslog protocol) input module */
201 m_udp_in = (module_t *)calloc(1, sizeof(module_t));
202 if (m_udp_in == NULL)
203 {
204 asldebug("alloc failed (init_modules udp_in)\n");
205 exit(1);
206 }
207
208 m_udp_in->name = "udp_in";
209 m_udp_in->enabled = activate_udp_in;
210 m_udp_in->init = udp_in_init;
211 m_udp_in->reset = udp_in_reset;
212 m_udp_in->close = udp_in_close;
213
214 if (m_udp_in->enabled) m_udp_in->init();
215
216 /* remote (iOS support) module */
217 m_remote = (module_t *)calloc(1, sizeof(module_t));
218 if (m_remote == NULL)
219 {
220 asldebug("alloc failed (init_modules remote)\n");
221 exit(1);
222 }
223
224 m_remote->name = "remote";
225 m_remote->enabled = remote_enabled;
226 m_remote->init = remote_init;
227 m_remote->reset = remote_reset;
228 m_remote->close = remote_close;
229
230 if (m_remote->enabled) m_remote->init();
231 #endif /* TARGET_IPHONE_SIMULATOR */
232
233 /* save modules in global.module array */
234 #if TARGET_IPHONE_SIMULATOR
235 global.module_count = 2;
236 #else
237 global.module_count = 6;
238 #endif
239 global.module = (module_t **)calloc(global.module_count, sizeof(module_t *));
240 if (global.module == NULL)
241 {
242 asldebug("alloc failed (init_modules)\n");
243 exit(1);
244 }
245
246 global.module[m++] = m_asl;
247 global.module[m++] = m_bsd_in;
248 #if !TARGET_IPHONE_SIMULATOR
249 global.module[m++] = m_bsd_out;
250 global.module[m++] = m_klog_in;
251 global.module[m++] = m_udp_in;
252 global.module[m++] = m_remote;
253 #endif
254 }
255
256 static void
257 writepid(int *first)
258 {
259 struct stat sb;
260 FILE *fp;
261
262 if (first != NULL)
263 {
264 *first = 1;
265 memset(&sb, 0, sizeof(struct stat));
266 if (stat(_PATH_PIDFILE, &sb) == 0)
267 {
268 if (S_ISREG(sb.st_mode)) *first = 0;
269 }
270 }
271
272 fp = fopen(_PATH_PIDFILE, "w");
273 if (fp != NULL)
274 {
275 fprintf(fp, "%d\n", global.pid);
276 fclose(fp);
277 }
278 }
279
280 void
281 launch_config()
282 {
283 launch_data_t tmp, pdict;
284 kern_return_t status;
285
286 tmp = launch_data_new_string(LAUNCH_KEY_CHECKIN);
287 global.launch_dict = launch_msg(tmp);
288 launch_data_free(tmp);
289
290 if (global.launch_dict == NULL)
291 {
292 asldebug("%d launchd checkin failed\n", global.pid);
293 exit(1);
294 }
295
296 tmp = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_MACHSERVICES);
297 if (tmp == NULL)
298 {
299 asldebug("%d launchd lookup of LAUNCH_JOBKEY_MACHSERVICES failed\n", global.pid);
300 exit(1);
301 }
302
303 pdict = launch_data_dict_lookup(tmp, SERVICE_NAME);
304 if (pdict == NULL)
305 {
306 asldebug("%d launchd lookup of SERVICE_NAME failed\n", global.pid);
307 exit(1);
308 }
309
310 global.server_port = launch_data_get_machport(pdict);
311
312 /* port for receiving MACH_NOTIFY_DEAD_NAME notifications */
313 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &(global.dead_session_port));
314 if (status != KERN_SUCCESS)
315 {
316 asldebug("mach_port_allocate dead_session_port failed: %d", status);
317 exit(1);
318 }
319
320 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &(global.listen_set));
321 if (status != KERN_SUCCESS)
322 {
323 asldebug("mach_port_allocate listen_set failed: %d", status);
324 exit(1);
325 }
326
327 status = mach_port_move_member(mach_task_self(), global.server_port, global.listen_set);
328 if (status != KERN_SUCCESS)
329 {
330 asldebug("mach_port_move_member server_port failed: %d", status);
331 exit(1);
332 }
333
334 status = mach_port_move_member(mach_task_self(), global.dead_session_port, global.listen_set);
335 if (status != KERN_SUCCESS)
336 {
337 asldebug("mach_port_move_member dead_session_port failed (%u)", status);
338 exit(1);
339 }
340 }
341
342 void
343 config_debug(int enable, const char *path)
344 {
345 OSSpinLockLock(&global.lock);
346
347 global.debug = enable;
348 free(global.debug_file);
349 global.debug_file = NULL;
350 if (path != NULL) global.debug_file = strdup(path);
351
352 OSSpinLockUnlock(&global.lock);
353 }
354
355 void
356 config_data_store(int type, uint32_t file_max, uint32_t memory_max, uint32_t str_memory_max)
357 {
358 pthread_mutex_lock(global.db_lock);
359
360 if (global.dbtype & DB_TYPE_FILE)
361 {
362 asl_store_close(global.file_db);
363 global.file_db = NULL;
364 }
365
366 if (global.dbtype & DB_TYPE_MEMORY)
367 {
368 asl_memory_close(global.memory_db);
369 global.memory_db = NULL;
370 }
371
372 global.dbtype = type;
373 global.db_file_max = file_max;
374 global.db_memory_max = memory_max;
375 global.db_memory_str_max = str_memory_max;
376
377 pthread_mutex_unlock(global.db_lock);
378 }
379
380 void
381 write_boot_log(int first)
382 {
383 int mib[2] = {CTL_KERN, KERN_BOOTTIME};
384 size_t len;
385 asl_msg_t *msg;
386 char buf[256];
387 struct utmpx utx;
388
389 if (first == 0)
390 {
391 /* syslogd restart */
392 msg = asl_msg_new(ASL_TYPE_MSG);
393 if (msg == NULL) return;
394
395 asl_msg_set_key_val(msg, ASL_KEY_SENDER, "syslogd");
396 asl_msg_set_key_val(msg, ASL_KEY_FACILITY, "daemon");
397 asl_msg_set_key_val(msg, ASL_KEY_LEVEL, "Notice");
398 asl_msg_set_key_val(msg, ASL_KEY_UID, "0");
399 asl_msg_set_key_val(msg, ASL_KEY_GID, "0");
400 snprintf(buf, sizeof(buf), "%u", global.pid);
401 asl_msg_set_key_val(msg, ASL_KEY_PID, buf);
402 asl_msg_set_key_val(msg, ASL_KEY_MSG, "--- syslogd restarted ---");
403 process_message(msg, SOURCE_INTERNAL);
404 return;
405 }
406
407 bzero(&utx, sizeof(utx));
408 utx.ut_type = BOOT_TIME;
409 utx.ut_pid = 1;
410
411 /* get the boot time */
412 len = sizeof(struct timeval);
413 if (sysctl(mib, 2, &utx.ut_tv, &len, NULL, 0) < 0)
414 {
415 gettimeofday(&utx.ut_tv, NULL);
416 }
417
418 pututxline(&utx);
419
420 msg = asl_msg_new(ASL_TYPE_MSG);
421 if (msg == NULL) return;
422
423 asl_msg_set_key_val(msg, ASL_KEY_SENDER, "bootlog");
424 asl_msg_set_key_val(msg, ASL_KEY_FACILITY, "com.apple.system.utmpx");
425 asl_msg_set_key_val(msg, ASL_KEY_LEVEL, "Notice");
426 asl_msg_set_key_val(msg, ASL_KEY_UID, "0");
427 asl_msg_set_key_val(msg, ASL_KEY_GID, "0");
428 asl_msg_set_key_val(msg, ASL_KEY_PID, "0");
429 snprintf(buf, sizeof(buf), "BOOT_TIME %lu %u", (unsigned long)utx.ut_tv.tv_sec, (unsigned int)utx.ut_tv.tv_usec);
430 asl_msg_set_key_val(msg, ASL_KEY_MSG, buf);
431 asl_msg_set_key_val(msg, "ut_id", "0x00 0x00 0x00 0x00");
432 asl_msg_set_key_val(msg, "ut_pid", "1");
433 asl_msg_set_key_val(msg, "ut_type", "2");
434 snprintf(buf, sizeof(buf), "%lu", (unsigned long)utx.ut_tv.tv_sec);
435 asl_msg_set_key_val(msg, ASL_KEY_TIME, buf);
436 asl_msg_set_key_val(msg, "ut_tv.tv_sec", buf);
437 snprintf(buf, sizeof(buf), "%u", (unsigned int)utx.ut_tv.tv_usec);
438 asl_msg_set_key_val(msg, "ut_tv.tv_usec", buf);
439 snprintf(buf, sizeof(buf), "%u%s", (unsigned int)utx.ut_tv.tv_usec, (utx.ut_tv.tv_usec == 0) ? "" : "000");
440 asl_msg_set_key_val(msg, ASL_KEY_TIME_NSEC, buf);
441
442 process_message(msg, SOURCE_INTERNAL);
443 }
444
445 int
446 main(int argc, const char *argv[])
447 {
448 int32_t i;
449 uint64_t master_val;
450 #if !TARGET_IPHONE_SIMULATOR
451 int network_change_token;
452 #endif
453 int quota_file_token, asl_db_token, master_token;
454 char tstr[32], *notify_key;
455 time_t now;
456 int first_syslogd_start = 1;
457
458 #if TARGET_IPHONE_SIMULATOR
459 const char *sim_log_dir = getenv("SIMULATOR_LOG_ROOT");
460 const char *sim_resource_dir = getenv("SIMULATOR_SHARED_RESOURCES_DIRECTORY");
461 char *p;
462
463 /* assert is evil */
464 assert(sim_log_dir && sim_resource_dir);
465
466 asprintf((char **)&_path_syslogd_log, "%s/syslogd.log", sim_log_dir);
467 asprintf((char **)&_path_pidfile, "%s/var/run/syslog.pid", sim_resource_dir);
468
469 if (_path_syslogd_log == NULL) _path_syslogd_log = "/tmp/syslogd.log";
470 else mkpath_np(sim_log_dir, 0755);
471
472 if (_path_pidfile == NULL)
473 {
474 _path_pidfile = "/tmp/syslog.pid";
475 }
476 else
477 {
478 p = strrchr(_path_pidfile, '/');
479 *p = '\0';
480 mkpath_np(_path_pidfile, 0755);
481 *p = '/';
482 }
483 #endif
484
485 #if TARGET_OS_IPHONE
486 /*
487 * Reset owner, group, and permissions in /var/mobile/Library/Logs
488 * in case something created them incorrectly. syslogd was
489 * guilty of this in the past, creating them with owner root.
490 */
491
492 uid_t __mUserUID = 501;
493 gid_t __mUserGID = 501;
494 struct passwd * pw = getpwnam("mobile");
495
496 if (pw) {
497 __mUserUID = pw->pw_uid;
498 __mUserGID = pw->pw_gid;
499 }
500
501 asl_secure_chown_chmod_dir("/private/var/mobile/Library/Logs", __mUserUID, __mUserGID, 0755);
502 asl_secure_chown_chmod_dir("/private/var/mobile/Library/Logs/CrashReporter", __mUserUID, __mUserGID, 0755);
503 asl_secure_chown_chmod_dir("/private/var/mobile/Library/Logs/CrashReporter/DiagnosticLogs", __mUserUID, __mUserGID, 0755);
504 #endif
505
506 /* Set I/O policy */
507 setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_PASSIVE);
508
509 #if !TARGET_OS_IPHONE
510 /* Set Quarantine */
511 qtn_proc_t qp = qtn_proc_alloc();
512 qtn_proc_set_identifier(qp, "com.apple.syslogd");
513 qtn_proc_set_flags(qp, QTN_FLAG_SANDBOX | QTN_FLAG_HARD);
514 qtn_proc_apply_to_self(qp);
515 qtn_proc_free(qp);
516 #endif
517
518 memset(&global, 0, sizeof(struct global_s));
519
520 global.db_lock = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t));
521 pthread_mutex_init(global.db_lock, NULL);
522
523 /*
524 * Create work queue, but suspend until output modules are initialized.
525 */
526 global.work_queue = dispatch_queue_create("Work Queue", NULL);
527 dispatch_suspend(global.work_queue);
528
529 init_globals();
530
531 #if TARGET_OS_EMBEDDED
532 remote_enabled = 1;
533 activate_bsd_out = 0;
534 #endif
535
536 /* prevent malloc from calling ASL on error */
537 _malloc_no_asl_log = 1;
538
539 /* first pass sets up default configurations */
540 for (i = 1; i < argc; i++)
541 {
542 if (streq(argv[i], "-config"))
543 {
544 if (((i + 1) < argc) && (argv[i+1][0] != '-'))
545 {
546 i++;
547 if (streq(argv[i], "mac"))
548 {
549 global.dbtype = DB_TYPE_FILE;
550 global.db_file_max = 25600000;
551 }
552 else if (streq(argv[i], "appletv"))
553 {
554 global.dbtype = DB_TYPE_FILE;
555 global.db_file_max = 10240000;
556 }
557 else if (streq(argv[i], "iphone"))
558 {
559 #if TARGET_IPHONE_SIMULATOR
560 global.dbtype = DB_TYPE_FILE;
561 global.db_file_max = 25600000;
562 #else
563 global.dbtype = DB_TYPE_MEMORY;
564 remote_enabled = 1;
565 #endif
566 }
567 }
568 }
569 }
570
571 for (i = 1; i < argc; i++)
572 {
573 if (streq(argv[i], "-d"))
574 {
575 global.debug = 1;
576 if (((i+1) < argc) && (argv[i+1][0] != '-')) global.debug_file = strdup(argv[++i]);
577 }
578 else if (streq(argv[i], "-db"))
579 {
580 if (((i + 1) < argc) && (argv[i+1][0] != '-'))
581 {
582 i++;
583 if (streq(argv[i], "file"))
584 {
585 global.dbtype |= DB_TYPE_FILE;
586 if (((i + 1) < argc) && (argv[i+1][0] != '-')) global.db_file_max = atol(argv[++i]);
587 }
588 else if (streq(argv[i], "memory"))
589 {
590 global.dbtype |= DB_TYPE_MEMORY;
591 if (((i + 1) < argc) && (argv[i+1][0] != '-')) global.db_memory_max = atol(argv[++i]);
592 }
593 }
594 }
595 else if (streq(argv[i], "-m"))
596 {
597 if ((i + 1) < argc) global.mark_time = 60 * atoll(argv[++i]);
598 }
599 else if (streq(argv[i], "-utmp_ttl"))
600 {
601 if ((i + 1) < argc) global.utmp_ttl = atol(argv[++i]);
602 }
603 else if (streq(argv[i], "-mps_limit"))
604 {
605 if ((i + 1) < argc) global.mps_limit = atol(argv[++i]);
606 }
607 else if (streq(argv[i], "-dup_delay"))
608 {
609 if ((i + 1) < argc) global.bsd_max_dup_time = atoll(argv[++i]);
610 }
611 #if !TARGET_IPHONE_SIMULATOR
612 else if (streq(argv[i], "-klog_in"))
613 {
614 if ((i + 1) < argc) activate_klog_in = atoi(argv[++i]);
615 }
616 else if (streq(argv[i], "-bsd_in"))
617 {
618 if ((i + 1) < argc) activate_bsd_in = atoi(argv[++i]);
619 }
620 else if (streq(argv[i], "-udp_in"))
621 {
622 if ((i + 1) < argc) activate_udp_in = atoi(argv[++i]);
623 }
624 #endif
625 else if (streq(argv[i], "-launchd_in"))
626 {
627 if ((i + 1) < argc) global.launchd_enabled = atoi(argv[++i]);
628 }
629 #if !TARGET_IPHONE_SIMULATOR
630 else if (streq(argv[i], "-bsd_out"))
631 {
632 if ((i + 1) < argc) activate_bsd_out = atoi(argv[++i]);
633 }
634 else if (streq(argv[i], "-remote"))
635 {
636 if ((i + 1) < argc) remote_enabled = atoi(argv[++i]);
637 }
638 #endif
639 }
640
641 if (global.dbtype == 0)
642 {
643 global.dbtype = DB_TYPE_FILE;
644 global.db_file_max = 25600000;
645 }
646
647 signal(SIGHUP, SIG_IGN);
648
649 memset(tstr, 0, sizeof(tstr));
650 now = time(NULL);
651 ctime_r(&now, tstr);
652 tstr[19] = '\0';
653 asldebug("\n%s syslogd PID %d starting\n", tstr, global.pid);
654
655 writepid(&first_syslogd_start);
656
657 /*
658 * Log UTMPX boot time record
659 */
660 write_boot_log(first_syslogd_start);
661
662 /* default NOTIFY_SYSTEM_MASTER settings */
663 master_val = 0x0;
664 notify_register_plain(NOTIFY_SYSTEM_MASTER, &master_token);
665 notify_set_state(master_token, master_val);
666
667 asldebug("reading launch plist\n");
668 launch_config();
669
670 asldebug("initializing modules\n");
671 init_modules();
672
673 #if !TARGET_IPHONE_SIMULATOR
674 asldebug("setting up network change notification handler\n");
675
676 /* network change notification resets UDP and BSD modules */
677 notify_register_dispatch(kNotifySCNetworkChange, &network_change_token, global.work_queue, ^(int x){
678 if (activate_udp_in != 0) udp_in_reset();
679 if (activate_bsd_out != 0) bsd_out_reset();
680 });
681 #endif
682
683 asldebug("setting up quota notification handler\n");
684
685 notify_key = NULL;
686 asprintf(&notify_key, "%s%s", NOTIFY_PATH_SERVICE, NOQUOTA_FILE_PATH);
687 if (notify_key != NULL)
688 {
689 int status;
690
691 status = notify_register_dispatch(notify_key, &quota_file_token, dispatch_get_main_queue(), ^(int t){
692 struct stat sb;
693 memset(&sb, 0, sizeof(sb));
694 if (stat(NOQUOTA_FILE_PATH, &sb) == 0)
695 {
696 char *str = NULL;
697 asprintf(&str, "[Sender syslogd] [Level 2] [PID %u] [Facility syslog] [Message *** MESSAGE QUOTAS DISABLED FOR ALL PROCESSES ***]", global.pid);
698 internal_log_message(str);
699 free(str);
700 }
701 else
702 {
703 char *str = NULL;
704 asprintf(&str, "[Sender syslogd] [Level 2] [PID %u] [Facility syslog] [Message *** MESSAGE QUOTAS ENABLED ***]", global.pid);
705 internal_log_message(str);
706 free(str);
707 }
708 });
709
710 free(notify_key);
711 }
712
713 /* SIGHUP resets all modules */
714 global.sig_hup_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, (uintptr_t)SIGHUP, 0, dispatch_get_main_queue());
715 dispatch_source_set_event_handler(global.sig_hup_src, ^{
716 dispatch_async(global.work_queue, ^{
717 int i;
718
719 asldebug("SIGHUP reset\n");
720 for (i = 0; i < global.module_count; i++)
721 {
722 if (global.module[i]->enabled != 0) global.module[i]->reset();
723 }
724 });
725 });
726
727 dispatch_resume(global.sig_hup_src);
728
729 /* register for DB notification (posted by dbserver) for performance */
730 notify_register_plain(kNotifyASLDBUpdate, &asl_db_token);
731
732 /* timer for MARK facility */
733 if (global.mark_time > 0)
734 {
735 global.mark_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
736 dispatch_source_set_event_handler(global.mark_timer, ^{
737 asl_mark();
738 });
739 dispatch_source_set_timer(global.mark_timer, dispatch_time(DISPATCH_TIME_NOW, global.mark_time * NSEC_PER_SEC), global.mark_time * NSEC_PER_SEC, 0);
740 dispatch_resume(global.mark_timer);
741 }
742
743 asldebug("starting mach service\n");
744 /*
745 * Start mach server
746 * Parks a thread in database_server. In notifyd, we found that the overhead of
747 * a dispatch source for mach calls was too high, especially on iOS.
748 */
749 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
750 database_server();
751 });
752
753 /* go to work */
754 asldebug("starting work queue\n");
755 dispatch_resume(global.work_queue);
756 dispatch_main();
757
758 /* NOTREACHED */
759 return 0;
760 }