2 * Copyright (c) 2004-2011 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 <sys/types.h>
26 #include <sys/socket.h>
41 #define MY_ID "bsd_out"
43 #define _PATH_WALL "/usr/bin/wall"
44 #define ASL_KEY_FACILITY "Facility"
45 #define FACILITY_KERNEL "kern"
46 #define _PATH_CONSOLE "/dev/console"
48 #define DST_TYPE_NONE 0
49 #define DST_TYPE_FILE 1
50 #define DST_TYPE_CONS 2
51 #define DST_TYPE_SOCK 3
52 #define DST_TYPE_WALL 4
53 #define DST_TYPE_NOTE 5
55 #define CLOSE_ON_IDLE_SEC 300
57 static dispatch_queue_t bsd_out_queue
;
58 static dispatch_source_t bsd_idle_timer
;
66 struct sockaddr
*addr
;
68 uint32_t *fac_prefix_len
;
73 dispatch_source_t dup_timer
;
75 TAILQ_ENTRY(config_rule
) entries
;
78 static TAILQ_HEAD(cr
, config_rule
) bsd_out_rule
;
80 extern uint32_t asl_core_string_hash(const char *s
, uint32_t inlen
);
83 _level_for_name(const char *name
)
85 if (name
== NULL
) return -1;
87 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
88 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
89 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
90 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
91 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
92 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
93 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
94 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
95 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
96 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
97 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
98 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
101 if (!strcasecmp(name
, "none")) return -2;
107 _syslog_dst_open(struct config_rule
*r
)
111 struct addrinfo hints
, *gai
, *ai
;
113 if (r
== NULL
) return -1;
114 if (r
->fd
!= -1) return 0;
116 if (r
->dst
[0] == '/')
118 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
121 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
125 r
->type
= DST_TYPE_FILE
;
126 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
131 if (r
->dst
[0] == '!')
133 r
->type
= DST_TYPE_NOTE
;
138 if (r
->dst
[0] == '@')
140 node
= strdup(r
->dst
+ 1);
141 if (node
== NULL
) return -1;
144 serv
= strrchr(node
, ':');
145 if (serv
!= NULL
) *serv
++ = '\0';
146 else serv
= "syslog";
148 memset(&hints
, 0, sizeof(hints
));
149 hints
.ai_family
= PF_UNSPEC
;
150 hints
.ai_socktype
= SOCK_DGRAM
;
151 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
155 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
159 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
161 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
162 if (r
->fd
< 0) continue;
164 r
->addr
= (struct sockaddr
*)malloc(ai
->ai_addrlen
);
165 if (r
->addr
== NULL
) return -1;
167 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
176 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
182 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
186 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
192 r
->type
= DST_TYPE_SOCK
;
197 if (strcmp(r
->dst
, "*") == 0)
199 r
->type
= DST_TYPE_WALL
;
204 /* Can't deal with dst! */
205 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
210 _syslog_dst_close(struct config_rule
*r
)
212 if (r
== NULL
) return;
225 if (r
->fd
>= 0) close(r
->fd
);
232 if (r
->fd
>= 0) close(r
->fd
);
249 _clean_facility_name(char *s
)
254 if (s
== NULL
) return NULL
;
256 if (len
== 0) return NULL
;
260 if ((*s
== '\'') || (*s
== '"'))
264 if (p
[len
- 1] == *s
) len
--;
267 out
= calloc(1, len
+ 1);
268 if (out
== NULL
) return NULL
;
277 char **semi
, **comma
, *star
;
278 int i
, j
, n
, lasts
, lastc
, pri
;
279 struct config_rule
*out
;
281 if (s
== NULL
) return -1;
282 while ((*s
== ' ') || (*s
== '\t')) s
++;
283 if (*s
== '#') return -1;
285 semi
= explode(s
, "; \t");
287 if (semi
== NULL
) return -1;
288 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
289 if (out
== NULL
) return -1;
294 for (i
= 0; semi
[i
] != NULL
; i
++)
296 if (semi
[i
][0] == '\0') continue;
301 out
->dst
= strdup(semi
[lasts
]);
302 if (out
->dst
== NULL
) return -1;
304 for (i
= 0; i
< lasts
; i
++)
306 if (semi
[i
][0] == '\0') continue;
307 comma
= explode(semi
[i
], ",.");
309 for (j
= 0; comma
[j
] != NULL
; j
++)
311 if (comma
[j
][0] == '\0') continue;
315 for (j
= 0; j
< lastc
; j
++)
317 if (comma
[j
][0] == '\0') continue;
318 pri
= _level_for_name(comma
[lastc
]);
319 if (pri
== -1) continue;
323 out
->facility
= (char **)calloc(1, sizeof(char *));
324 out
->fac_prefix_len
= (uint32_t *)calloc(1, sizeof(uint32_t));
325 out
->pri
= (int *)calloc(1, sizeof(int));
329 out
->facility
= (char **)reallocf(out
->facility
, (out
->count
+ 1) * sizeof(char *));
330 out
->fac_prefix_len
= (uint32_t *)reallocf(out
->fac_prefix_len
, (out
->count
+ 1) * sizeof(uint32_t));
331 out
->pri
= (int *)reallocf(out
->pri
, (out
->count
+ 1) * sizeof(int));
334 if (out
->facility
== NULL
) return -1;
335 if (out
->fac_prefix_len
== NULL
) return -1;
336 if (out
->pri
== NULL
) return -1;
338 out
->facility
[out
->count
] = _clean_facility_name(comma
[j
]);
339 if (out
->facility
[out
->count
] == NULL
) return -1;
341 out
->fac_prefix_len
[out
->count
] = 0;
342 star
= strchr(out
->facility
[out
->count
], '*');
343 if (star
!= NULL
) out
->fac_prefix_len
[out
->count
] = (uint32_t)(star
- out
->facility
[out
->count
]);
345 out
->pri
[out
->count
] = pri
;
354 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
360 _bsd_send_repeat_msg(struct config_rule
*r
)
366 if (r
== NULL
) return -1;
367 if (r
->type
!= DST_TYPE_FILE
) return 0;
368 if (r
->last_count
== 0) return 0;
371 dispatch_suspend(r
->dup_timer
);
374 memset(vt
, 0, sizeof(vt
));
379 asprintf(&msg
, "%s: --- last message repeated %u time%s ---\n", vt
+ 4, r
->last_count
, (r
->last_count
== 1) ? "" : "s");
381 if (msg
== NULL
) return -1;
384 status
= write(r
->fd
, msg
, len
);
385 if ((status
< 0) || (status
< len
))
387 asldebug("%s: error writing repeat message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
389 /* Try re-opening the file (once) and write again */
391 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
394 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
399 status
= write(r
->fd
, msg
, len
);
400 if ((status
< 0) || (status
< len
))
402 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
413 _bsd_send(aslmsg msg
, struct config_rule
*r
, char **out
, char **fwd
, time_t now
)
416 const char *vlevel
, *vfacility
;
418 int pf
, fc
, status
, is_dup
, do_write
;
419 uint32_t msg_hash
, n
;
421 if (out
== NULL
) return -1;
422 if (fwd
== NULL
) return -1;
423 if (r
== NULL
) return -1;
427 if (r
->type
== DST_TYPE_NOTE
)
429 notify_post(r
->dst
+1);
436 /* Build output string if it hasn't been built by a previous rule-match */
439 *out
= asl_format_message((asl_msg_t
*)msg
, ASL_MSG_FMT_BSD
, ASL_TIME_FMT_LCL
, ASL_ENCODE_SAFE
, &n
);
440 if (*out
== NULL
) return -1;
443 /* check if message is a duplicate of the last message, and inside the dup time window */
445 if ((global
.bsd_max_dup_time
> 0) && (*out
!= NULL
) && (r
->last_msg
!= NULL
))
447 msg_hash
= asl_core_string_hash(*out
+ 16, strlen(*out
+ 16));
448 if ((r
->last_hash
== msg_hash
) && (!strcmp(r
->last_msg
, *out
+ 16)))
450 if ((now
- r
->last_time
) < global
.bsd_max_dup_time
) is_dup
= 1;
454 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
457 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
458 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
460 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
461 if (fc
> 0) pf
|= fc
;
464 asprintf(&sf
, "<%d>%s", pf
, *out
);
465 if (sf
== NULL
) return -1;
470 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
471 else outlen
= strlen(*out
);
473 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
476 * If current message is NOT a duplicate and r->last_count > 0
477 * we need to write a "last message was repeated N times" log entry
479 if ((r
->type
== DST_TYPE_FILE
) && (is_dup
== 0) && (r
->last_count
> 0)) _bsd_send_repeat_msg(r
);
484 * Special case for kernel messages.
485 * Don't write kernel messages to /dev/console.
486 * The kernel printf routine already sends them to /dev/console
487 * so writing them here would cause duplicates.
489 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
490 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) do_write
= 0;
491 if ((do_write
== 1) && (r
->type
== DST_TYPE_FILE
) && (is_dup
== 1))
495 if (r
->dup_timer
== NULL
)
497 /* create a timer to flush dups on this file */
498 r
->dup_timer
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, 0, bsd_out_queue
);
499 dispatch_source_set_event_handler(r
->dup_timer
, ^{ _bsd_send_repeat_msg(r
); });
502 if (r
->last_count
== 0)
504 /* start the timer */
505 dispatch_source_set_timer(r
->dup_timer
, dispatch_time(DISPATCH_TIME_NOW
, NSEC_PER_SEC
* global
.bsd_max_dup_time
), DISPATCH_TIME_FOREVER
, 0);
506 dispatch_resume(r
->dup_timer
);
510 if (do_write
== 0) status
= outlen
;
511 else status
= write(r
->fd
, *out
, outlen
);
513 if ((status
< 0) || (status
< outlen
))
515 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
517 /* Try re-opening the file (once) and write again */
519 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
522 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
526 status
= write(r
->fd
, *out
, outlen
);
527 if ((status
< 0) || (status
< outlen
))
529 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
533 else if ((r
->type
== DST_TYPE_SOCK
) && (r
->addr
!= NULL
))
535 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
536 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
538 else if (r
->type
== DST_TYPE_WALL
)
540 #ifndef CONFIG_IPHONE
541 FILE *pw
= popen(_PATH_WALL
, "w");
544 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
548 fprintf(pw
, "%s", *out
);
562 if (*out
!= NULL
) r
->last_msg
= strdup(*out
+ 16);
564 r
->last_hash
= msg_hash
;
573 _bsd_rule_match(aslmsg msg
, struct config_rule
*r
)
579 if (msg
== NULL
) return 0;
580 if (r
== NULL
) return 0;
581 if (r
->count
== 0) return 0;
585 for (i
= 0; i
< r
->count
; i
++)
587 if (r
->pri
[i
] == -1) continue;
589 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
590 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
593 val
= asl_get(msg
, ASL_KEY_FACILITY
);
595 if (strcmp(r
->facility
[i
], "*") == 0)
599 else if ((r
->fac_prefix_len
[i
] > 0) && (strncasecmp(r
->facility
[i
], val
, r
->fac_prefix_len
[i
]) == 0))
603 else if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0))
608 if (f
== 0) continue;
610 /* Turn off matching facility with priority "none" */
617 val
= asl_get(msg
, ASL_KEY_LEVEL
);
618 if (val
== NULL
) continue;
621 if (pri
< 0) continue;
623 if (pri
<= r
->pri
[i
]) test
= 1;
630 _bsd_match_and_send(aslmsg msg
)
632 struct config_rule
*r
;
636 if (msg
== NULL
) return -1;
643 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
645 if (_bsd_rule_match(msg
, r
) == 1) _bsd_send(msg
, r
, &out
, &fwd
, now
);
655 bsd_out_message(aslmsg msg
)
657 if (msg
== NULL
) return;
659 asl_msg_retain((asl_msg_t
*)msg
);
661 dispatch_async(bsd_out_queue
, ^{
662 _bsd_match_and_send(msg
);
663 asl_msg_release((asl_msg_t
*)msg
);
668 _bsd_close_idle_files()
671 struct config_rule
*r
;
676 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
678 /* only applies to files */
679 if (r
->type
!= DST_TYPE_FILE
) continue;
682 * If the last message repeat count is non-zero, a _bsd_flush_duplicates()
683 * call will occur within 30 seconds. Don't bother closing the file.
685 if (r
->last_count
> 0) continue;
687 delta
= now
- r
->last_time
;
688 if (delta
> CLOSE_ON_IDLE_SEC
) _syslog_dst_close(r
);
693 _parse_config_file(const char *confname
)
698 cf
= fopen(confname
, "r");
699 if (cf
== NULL
) return 1;
701 while (NULL
!= (line
= get_line_from_file(cf
)))
715 static dispatch_once_t once
;
717 asldebug("%s: init\n", MY_ID
);
719 TAILQ_INIT(&bsd_out_rule
);
720 _parse_config_file(_PATH_SYSLOG_CONF
);
722 dispatch_once(&once
, ^{
723 bsd_out_queue
= dispatch_queue_create("BSD Out Queue", NULL
);
725 /* start a timer to close idle files */
726 bsd_idle_timer
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, 0, bsd_out_queue
);
727 dispatch_source_set_event_handler(bsd_idle_timer
, ^{ _bsd_close_idle_files(); });
728 dispatch_source_set_timer(bsd_idle_timer
, dispatch_time(DISPATCH_TIME_NOW
, NSEC_PER_SEC
* CLOSE_ON_IDLE_SEC
), NSEC_PER_SEC
* CLOSE_ON_IDLE_SEC
, 0);
729 dispatch_resume(bsd_idle_timer
);
736 _bsd_out_close_internal(void)
738 struct config_rule
*r
, *n
;
742 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
744 n
= r
->entries
.tqe_next
;
746 if (r
->dup_timer
!= NULL
)
748 if (r
->last_count
> 0) _bsd_send_repeat_msg(r
);
749 dispatch_source_cancel(r
->dup_timer
);
750 dispatch_resume(r
->dup_timer
);
751 dispatch_release(r
->dup_timer
);
757 free(r
->fac_prefix_len
);
760 if (r
->fd
>= 0) close(r
->fd
);
762 if (r
->facility
!= NULL
)
764 for (i
= 0; i
< r
->count
; i
++)
765 free(r
->facility
[i
]);
771 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);
781 dispatch_async(bsd_out_queue
, ^{
782 _bsd_out_close_internal();
791 dispatch_async(bsd_out_queue
, ^{
792 _bsd_out_close_internal();