2 * Copyright (c) 2004-2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <TargetConditionals.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>
39 #include <sys/fcntl.h>
40 #include <sys/errno.h>
41 #include <sys/queue.h>
49 #include <notify_keys.h>
51 #include <asl_private.h>
54 #include <quarantine.h>
58 #define SERVICE_NAME "com.apple.system.logger"
59 #define SERVER_STATUS_ERROR -1
60 #define SERVER_STATUS_INACTIVE 0
61 #define SERVER_STATUS_ACTIVE 1
62 #define SERVER_STATUS_ON_DEMAND 2
64 #define BILLION 1000000000
66 #define NOTIFY_DELAY 1
68 extern int _malloc_no_asl_log
;
70 #if TARGET_IPHONE_SIMULATOR
71 const char *_path_pidfile
;
72 const char *_path_syslogd_log
;
76 struct global_s global
;
78 #if !TARGET_IPHONE_SIMULATOR
80 int klog_in_init(void);
81 int klog_in_reset(void);
82 int klog_in_close(void);
83 static int activate_klog_in
= 1;
86 int bsd_in_init(void);
87 int bsd_in_reset(void);
88 int bsd_in_close(void);
89 static int activate_bsd_in
= 1;
91 #if !TARGET_IPHONE_SIMULATOR
92 int udp_in_init(void);
93 int udp_in_reset(void);
94 int udp_in_close(void);
95 static int activate_udp_in
= 1;
98 int bsd_out_init(void);
99 int bsd_out_reset(void);
100 int bsd_out_close(void);
101 static int activate_bsd_out
= 1;
104 int asl_action_init(void);
105 int asl_action_reset(void);
106 int asl_action_close(void);
107 static int activate_asl_action
= 1;
109 #if !TARGET_IPHONE_SIMULATOR
110 /* Interactive Module */
111 int remote_init(void);
112 int remote_reset(void);
113 int remote_close(void);
114 static int remote_enabled
= 0;
117 extern void database_server();
122 #if !TARGET_IPHONE_SIMULATOR
123 module_t
*m_klog_in
, *m_bsd_out
, *m_udp_in
, *m_remote
;
125 module_t
*m_asl
, *m_bsd_in
;
128 /* ASL module (configured by /etc/asl.conf) */
129 m_asl
= (module_t
*)calloc(1, sizeof(module_t
));
132 asldebug("alloc failed (init_modules asl_action)\n");
136 m_asl
->name
= "asl_action";
137 m_asl
->enabled
= activate_asl_action
;
138 m_asl
->init
= asl_action_init
;
139 m_asl
->reset
= asl_action_reset
;
140 m_asl
->close
= asl_action_close
;
142 if (m_asl
->enabled
) m_asl
->init();
144 #if !TARGET_IPHONE_SIMULATOR
145 /* BSD output module (configured by /etc/syslog.conf) */
146 m_bsd_out
= (module_t
*)calloc(1, sizeof(module_t
));
147 if (m_bsd_out
== NULL
)
149 asldebug("alloc failed (init_modules bsd_out)\n");
153 m_bsd_out
->name
= "bsd_out";
154 m_bsd_out
->enabled
= activate_bsd_out
;
155 m_bsd_out
->init
= bsd_out_init
;
156 m_bsd_out
->reset
= bsd_out_reset
;
157 m_bsd_out
->close
= bsd_out_close
;
159 if (m_bsd_out
->enabled
)
162 global
.bsd_out_enabled
= 1;
165 /* kernel input module */
166 m_klog_in
= (module_t
*)calloc(1, sizeof(module_t
));
167 if (m_klog_in
== NULL
)
169 asldebug("alloc failed (init_modules klog_in)\n");
173 m_klog_in
->name
= "klog_in";
174 m_klog_in
->enabled
= activate_klog_in
;
175 m_klog_in
->init
= klog_in_init
;
176 m_klog_in
->reset
= klog_in_reset
;
177 m_klog_in
->close
= klog_in_close
;
179 if (m_klog_in
->enabled
) m_klog_in
->init();
182 /* BSD (UNIX domain socket) input module */
183 m_bsd_in
= (module_t
*)calloc(1, sizeof(module_t
));
184 if (m_bsd_in
== NULL
)
186 asldebug("alloc failed (init_modules bsd_in)\n");
190 m_bsd_in
->name
= "bsd_in";
191 m_bsd_in
->enabled
= activate_bsd_in
;
192 m_bsd_in
->init
= bsd_in_init
;
193 m_bsd_in
->reset
= bsd_in_reset
;
194 m_bsd_in
->close
= bsd_in_close
;
196 if (m_bsd_in
->enabled
) m_bsd_in
->init();
198 #if !TARGET_IPHONE_SIMULATOR
199 /* network (syslog protocol) input module */
200 m_udp_in
= (module_t
*)calloc(1, sizeof(module_t
));
201 if (m_udp_in
== NULL
)
203 asldebug("alloc failed (init_modules udp_in)\n");
207 m_udp_in
->name
= "udp_in";
208 m_udp_in
->enabled
= activate_udp_in
;
209 m_udp_in
->init
= udp_in_init
;
210 m_udp_in
->reset
= udp_in_reset
;
211 m_udp_in
->close
= udp_in_close
;
213 if (m_udp_in
->enabled
) m_udp_in
->init();
215 /* remote (iOS support) module */
216 m_remote
= (module_t
*)calloc(1, sizeof(module_t
));
217 if (m_remote
== NULL
)
219 asldebug("alloc failed (init_modules remote)\n");
223 m_remote
->name
= "remote";
224 m_remote
->enabled
= remote_enabled
;
225 m_remote
->init
= remote_init
;
226 m_remote
->reset
= remote_reset
;
227 m_remote
->close
= remote_close
;
229 if (m_remote
->enabled
) m_remote
->init();
230 #endif /* TARGET_IPHONE_SIMULATOR */
232 /* save modules in global.module array */
233 #if TARGET_IPHONE_SIMULATOR
234 global
.module_count
= 2;
236 global
.module_count
= 6;
238 global
.module = (module_t
**)calloc(global
.module_count
, sizeof(module_t
*));
239 if (global
.module == NULL
)
241 asldebug("alloc failed (init_modules)\n");
245 global
.module[m
++] = m_asl
;
246 global
.module[m
++] = m_bsd_in
;
247 #if !TARGET_IPHONE_SIMULATOR
248 global
.module[m
++] = m_bsd_out
;
249 global
.module[m
++] = m_klog_in
;
250 global
.module[m
++] = m_udp_in
;
251 global
.module[m
++] = m_remote
;
264 memset(&sb
, 0, sizeof(struct stat
));
265 if (stat(_PATH_PIDFILE
, &sb
) == 0)
267 if (S_ISREG(sb
.st_mode
)) *first
= 0;
271 fp
= fopen(_PATH_PIDFILE
, "w");
274 fprintf(fp
, "%d\n", global
.pid
);
282 launch_data_t tmp
, pdict
;
283 kern_return_t status
;
285 tmp
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
286 global
.launch_dict
= launch_msg(tmp
);
287 launch_data_free(tmp
);
289 if (global
.launch_dict
== NULL
)
291 asldebug("%d launchd checkin failed\n", global
.pid
);
295 tmp
= launch_data_dict_lookup(global
.launch_dict
, LAUNCH_JOBKEY_MACHSERVICES
);
298 asldebug("%d launchd lookup of LAUNCH_JOBKEY_MACHSERVICES failed\n", global
.pid
);
302 pdict
= launch_data_dict_lookup(tmp
, SERVICE_NAME
);
305 asldebug("%d launchd lookup of SERVICE_NAME failed\n", global
.pid
);
309 global
.server_port
= launch_data_get_machport(pdict
);
311 /* port for receiving MACH_NOTIFY_DEAD_NAME notifications */
312 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &(global
.dead_session_port
));
313 if (status
!= KERN_SUCCESS
)
315 asldebug("mach_port_allocate dead_session_port failed: %d", status
);
319 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET
, &(global
.listen_set
));
320 if (status
!= KERN_SUCCESS
)
322 asldebug("mach_port_allocate listen_set failed: %d", status
);
326 status
= mach_port_move_member(mach_task_self(), global
.server_port
, global
.listen_set
);
327 if (status
!= KERN_SUCCESS
)
329 asldebug("mach_port_move_member server_port failed: %d", status
);
333 status
= mach_port_move_member(mach_task_self(), global
.dead_session_port
, global
.listen_set
);
334 if (status
!= KERN_SUCCESS
)
336 asldebug("mach_port_move_member dead_session_port failed (%u)", status
);
342 config_debug(int enable
, const char *path
)
344 OSSpinLockLock(&global
.lock
);
346 global
.debug
= enable
;
347 free(global
.debug_file
);
348 global
.debug_file
= NULL
;
349 if (path
!= NULL
) global
.debug_file
= strdup(path
);
351 OSSpinLockUnlock(&global
.lock
);
355 config_data_store(int type
, uint32_t file_max
, uint32_t memory_max
, uint32_t str_memory_max
)
357 pthread_mutex_lock(global
.db_lock
);
359 if (global
.dbtype
& DB_TYPE_FILE
)
361 asl_store_close(global
.file_db
);
362 global
.file_db
= NULL
;
365 if (global
.dbtype
& DB_TYPE_MEMORY
)
367 asl_memory_close(global
.memory_db
);
368 global
.memory_db
= NULL
;
371 global
.dbtype
= type
;
372 global
.db_file_max
= file_max
;
373 global
.db_memory_max
= memory_max
;
374 global
.db_memory_str_max
= str_memory_max
;
376 pthread_mutex_unlock(global
.db_lock
);
380 write_boot_log(int first
)
382 int mib
[2] = {CTL_KERN
, KERN_BOOTTIME
};
390 /* syslogd restart */
391 msg
= asl_msg_new(ASL_TYPE_MSG
);
392 if (msg
== NULL
) return;
394 asl_msg_set_key_val(msg
, ASL_KEY_SENDER
, "syslogd");
395 asl_msg_set_key_val(msg
, ASL_KEY_FACILITY
, "daemon");
396 asl_msg_set_key_val(msg
, ASL_KEY_LEVEL
, "Notice");
397 asl_msg_set_key_val(msg
, ASL_KEY_UID
, "0");
398 asl_msg_set_key_val(msg
, ASL_KEY_GID
, "0");
399 snprintf(buf
, sizeof(buf
), "%u", global
.pid
);
400 asl_msg_set_key_val(msg
, ASL_KEY_PID
, buf
);
401 asl_msg_set_key_val(msg
, ASL_KEY_MSG
, "--- syslogd restarted ---");
402 process_message(msg
, SOURCE_INTERNAL
);
406 bzero(&utx
, sizeof(utx
));
407 utx
.ut_type
= BOOT_TIME
;
410 /* get the boot time */
411 len
= sizeof(struct timeval
);
412 if (sysctl(mib
, 2, &utx
.ut_tv
, &len
, NULL
, 0) < 0)
414 gettimeofday(&utx
.ut_tv
, NULL
);
419 msg
= asl_msg_new(ASL_TYPE_MSG
);
420 if (msg
== NULL
) return;
422 asl_msg_set_key_val(msg
, ASL_KEY_SENDER
, "bootlog");
423 asl_msg_set_key_val(msg
, ASL_KEY_FACILITY
, "com.apple.system.utmpx");
424 asl_msg_set_key_val(msg
, ASL_KEY_LEVEL
, "Notice");
425 asl_msg_set_key_val(msg
, ASL_KEY_UID
, "0");
426 asl_msg_set_key_val(msg
, ASL_KEY_GID
, "0");
427 asl_msg_set_key_val(msg
, ASL_KEY_PID
, "0");
428 snprintf(buf
, sizeof(buf
), "BOOT_TIME %lu %u", (unsigned long)utx
.ut_tv
.tv_sec
, (unsigned int)utx
.ut_tv
.tv_usec
);
429 asl_msg_set_key_val(msg
, ASL_KEY_MSG
, buf
);
430 asl_msg_set_key_val(msg
, "ut_id", "0x00 0x00 0x00 0x00");
431 asl_msg_set_key_val(msg
, "ut_pid", "1");
432 asl_msg_set_key_val(msg
, "ut_type", "2");
433 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)utx
.ut_tv
.tv_sec
);
434 asl_msg_set_key_val(msg
, ASL_KEY_TIME
, buf
);
435 asl_msg_set_key_val(msg
, "ut_tv.tv_sec", buf
);
436 snprintf(buf
, sizeof(buf
), "%u", (unsigned int)utx
.ut_tv
.tv_usec
);
437 asl_msg_set_key_val(msg
, "ut_tv.tv_usec", buf
);
438 snprintf(buf
, sizeof(buf
), "%u%s", (unsigned int)utx
.ut_tv
.tv_usec
, (utx
.ut_tv
.tv_usec
== 0) ? "" : "000");
439 asl_msg_set_key_val(msg
, ASL_KEY_TIME_NSEC
, buf
);
441 process_message(msg
, SOURCE_INTERNAL
);
445 main(int argc
, const char *argv
[])
449 #if !TARGET_IPHONE_SIMULATOR
450 int network_change_token
;
452 int quota_file_token
, asl_db_token
, master_token
;
453 char tstr
[32], *notify_key
;
455 int first_syslogd_start
= 1;
457 #if TARGET_IPHONE_SIMULATOR
458 const char *sim_log_dir
= getenv("SIMULATOR_LOG_ROOT");
459 const char *sim_resource_dir
= getenv("SIMULATOR_SHARED_RESOURCES_DIRECTORY");
463 assert(sim_log_dir
&& sim_resource_dir
);
465 asprintf((char **)&_path_syslogd_log
, "%s/syslogd.log", sim_log_dir
);
466 asprintf((char **)&_path_pidfile
, "%s/var/run/syslog.pid", sim_resource_dir
);
468 if (_path_syslogd_log
== NULL
) _path_syslogd_log
= "/tmp/syslogd.log";
469 else mkpath_np(sim_log_dir
, 0755);
471 if (_path_pidfile
== NULL
)
473 _path_pidfile
= "/tmp/syslog.pid";
477 p
= strrchr(_path_pidfile
, '/');
479 mkpath_np(_path_pidfile
, 0755);
485 setiopolicy_np(IOPOL_TYPE_DISK
, IOPOL_SCOPE_PROCESS
, IOPOL_PASSIVE
);
487 #if !TARGET_OS_IPHONE
489 qtn_proc_t qp
= qtn_proc_alloc();
490 qtn_proc_set_identifier(qp
, "com.apple.syslogd");
491 qtn_proc_set_flags(qp
, QTN_FLAG_SANDBOX
| QTN_FLAG_HARD
);
492 qtn_proc_apply_to_self(qp
);
496 memset(&global
, 0, sizeof(struct global_s
));
498 global
.db_lock
= (pthread_mutex_t
*)calloc(1, sizeof(pthread_mutex_t
));
499 pthread_mutex_init(global
.db_lock
, NULL
);
502 * Create work queue, but suspend until output modules are initialized.
504 global
.work_queue
= dispatch_queue_create("Work Queue", NULL
);
505 dispatch_suspend(global
.work_queue
);
509 #if TARGET_OS_EMBEDDED
511 activate_bsd_out
= 0;
514 /* prevent malloc from calling ASL on error */
515 _malloc_no_asl_log
= 1;
517 /* first pass sets up default configurations */
518 for (i
= 1; i
< argc
; i
++)
520 if (streq(argv
[i
], "-config"))
522 if (((i
+ 1) < argc
) && (argv
[i
+1][0] != '-'))
525 if (streq(argv
[i
], "mac"))
527 global
.dbtype
= DB_TYPE_FILE
;
528 global
.db_file_max
= 25600000;
530 else if (streq(argv
[i
], "appletv"))
532 global
.dbtype
= DB_TYPE_FILE
;
533 global
.db_file_max
= 10240000;
535 else if (streq(argv
[i
], "iphone"))
537 #if TARGET_IPHONE_SIMULATOR
538 global
.dbtype
= DB_TYPE_FILE
;
539 global
.db_file_max
= 25600000;
541 global
.dbtype
= DB_TYPE_MEMORY
;
549 for (i
= 1; i
< argc
; i
++)
551 if (streq(argv
[i
], "-d"))
554 if (((i
+1) < argc
) && (argv
[i
+1][0] != '-')) global
.debug_file
= strdup(argv
[++i
]);
556 else if (streq(argv
[i
], "-db"))
558 if (((i
+ 1) < argc
) && (argv
[i
+1][0] != '-'))
561 if (streq(argv
[i
], "file"))
563 global
.dbtype
|= DB_TYPE_FILE
;
564 if (((i
+ 1) < argc
) && (argv
[i
+1][0] != '-')) global
.db_file_max
= atol(argv
[++i
]);
566 else if (streq(argv
[i
], "memory"))
568 global
.dbtype
|= DB_TYPE_MEMORY
;
569 if (((i
+ 1) < argc
) && (argv
[i
+1][0] != '-')) global
.db_memory_max
= atol(argv
[++i
]);
573 else if (streq(argv
[i
], "-m"))
575 if ((i
+ 1) < argc
) global
.mark_time
= 60 * atoll(argv
[++i
]);
577 else if (streq(argv
[i
], "-utmp_ttl"))
579 if ((i
+ 1) < argc
) global
.utmp_ttl
= atol(argv
[++i
]);
581 else if (streq(argv
[i
], "-mps_limit"))
583 if ((i
+ 1) < argc
) global
.mps_limit
= atol(argv
[++i
]);
585 else if (streq(argv
[i
], "-dup_delay"))
587 if ((i
+ 1) < argc
) global
.bsd_max_dup_time
= atoll(argv
[++i
]);
589 #if !TARGET_IPHONE_SIMULATOR
590 else if (streq(argv
[i
], "-klog_in"))
592 if ((i
+ 1) < argc
) activate_klog_in
= atoi(argv
[++i
]);
594 else if (streq(argv
[i
], "-bsd_in"))
596 if ((i
+ 1) < argc
) activate_bsd_in
= atoi(argv
[++i
]);
598 else if (streq(argv
[i
], "-udp_in"))
600 if ((i
+ 1) < argc
) activate_udp_in
= atoi(argv
[++i
]);
603 else if (streq(argv
[i
], "-launchd_in"))
605 if ((i
+ 1) < argc
) global
.launchd_enabled
= atoi(argv
[++i
]);
607 #if !TARGET_IPHONE_SIMULATOR
608 else if (streq(argv
[i
], "-bsd_out"))
610 if ((i
+ 1) < argc
) activate_bsd_out
= atoi(argv
[++i
]);
612 else if (streq(argv
[i
], "-remote"))
614 if ((i
+ 1) < argc
) remote_enabled
= atoi(argv
[++i
]);
619 if (global
.dbtype
== 0)
621 global
.dbtype
= DB_TYPE_FILE
;
622 global
.db_file_max
= 25600000;
625 signal(SIGHUP
, SIG_IGN
);
627 memset(tstr
, 0, sizeof(tstr
));
631 asldebug("\n%s syslogd PID %d starting\n", tstr
, global
.pid
);
633 writepid(&first_syslogd_start
);
636 * Log UTMPX boot time record
638 write_boot_log(first_syslogd_start
);
640 /* default NOTIFY_SYSTEM_MASTER settings */
642 notify_register_plain(NOTIFY_SYSTEM_MASTER
, &master_token
);
643 notify_set_state(master_token
, master_val
);
645 asldebug("reading launch plist\n");
648 asldebug("initializing modules\n");
651 #if !TARGET_IPHONE_SIMULATOR
652 asldebug("setting up network change notification handler\n");
654 /* network change notification resets UDP and BSD modules */
655 notify_register_dispatch(kNotifySCNetworkChange
, &network_change_token
, global
.work_queue
, ^(int x
){
656 if (activate_udp_in
!= 0) udp_in_reset();
657 if (activate_bsd_out
!= 0) bsd_out_reset();
661 asldebug("setting up quota notification handler\n");
664 asprintf(¬ify_key
, "%s%s", NOTIFY_PATH_SERVICE
, NOQUOTA_FILE_PATH
);
665 if (notify_key
!= NULL
)
669 status
= notify_register_dispatch(notify_key
, "a_file_token
, dispatch_get_main_queue(), ^(int t
){
671 memset(&sb
, 0, sizeof(sb
));
672 if (stat(NOQUOTA_FILE_PATH
, &sb
) == 0)
675 asprintf(&str
, "[Sender syslogd] [Level 2] [PID %u] [Facility syslog] [Message *** MESSAGE QUOTAS DISABLED FOR ALL PROCESSES ***]", global
.pid
);
676 internal_log_message(str
);
682 asprintf(&str
, "[Sender syslogd] [Level 2] [PID %u] [Facility syslog] [Message *** MESSAGE QUOTAS ENABLED ***]", global
.pid
);
683 internal_log_message(str
);
691 /* SIGHUP resets all modules */
692 global
.sig_hup_src
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, (uintptr_t)SIGHUP
, 0, dispatch_get_main_queue());
693 dispatch_source_set_event_handler(global
.sig_hup_src
, ^{
694 dispatch_async(global
.work_queue
, ^{
697 asldebug("SIGHUP reset\n");
698 for (i
= 0; i
< global
.module_count
; i
++)
700 if (global
.module[i
]->enabled
!= 0) global
.module[i
]->reset();
705 dispatch_resume(global
.sig_hup_src
);
707 /* register for DB notification (posted by dbserver) for performance */
708 notify_register_plain(kNotifyASLDBUpdate
, &asl_db_token
);
710 /* timer for MARK facility */
711 if (global
.mark_time
> 0)
713 global
.mark_timer
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, 0, dispatch_get_main_queue());
714 dispatch_source_set_event_handler(global
.mark_timer
, ^{
717 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);
718 dispatch_resume(global
.mark_timer
);
721 asldebug("starting mach service\n");
724 * Parks a thread in database_server. In notifyd, we found that the overhead of
725 * a dispatch source for mach calls was too high, especially on iOS.
727 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^{
732 asldebug("starting work queue\n");
733 dispatch_resume(global
.work_queue
);