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 60
57 static asl_msg_t
*query
= NULL
;
58 static int reset
= RESET_NONE
;
59 static pthread_mutex_t reset_lock
= PTHREAD_MUTEX_INITIALIZER
;
67 struct sockaddr
*addr
;
69 uint32_t *fac_prefix_len
;
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 int bsd_out_network_reset(void);
84 static int _parse_config_file(const char *);
89 pthread_mutex_lock(&reset_lock
);
90 if (reset
== RESET_NONE
)
92 pthread_mutex_unlock(&reset_lock
);
96 if (reset
== RESET_CONFIG
)
99 _parse_config_file(_PATH_SYSLOG_CONF
);
101 else if (reset
== RESET_NETWORK
)
103 bsd_out_network_reset();
108 pthread_mutex_unlock(&reset_lock
);
112 _level_for_name(const char *name
)
114 if (name
== NULL
) return -1;
116 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
117 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
118 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
119 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
120 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
121 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
122 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
123 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
124 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
125 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
126 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
127 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
130 if (!strcasecmp(name
, "none")) return -2;
136 _syslog_dst_open(struct config_rule
*r
)
140 struct addrinfo hints
, *gai
, *ai
;
142 if (r
== NULL
) return -1;
143 if (r
->fd
!= -1) return 0;
145 if (r
->dst
[0] == '/')
147 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
150 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
154 r
->type
= DST_TYPE_FILE
;
155 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
160 if (r
->dst
[0] == '!')
162 r
->type
= DST_TYPE_NOTE
;
167 if (r
->dst
[0] == '@')
169 node
= strdup(r
->dst
+ 1);
170 if (node
== NULL
) return -1;
173 serv
= strrchr(node
, ':');
174 if (serv
!= NULL
) *serv
++ = '\0';
175 else serv
= "syslog";
177 memset(&hints
, 0, sizeof(hints
));
178 hints
.ai_family
= PF_UNSPEC
;
179 hints
.ai_socktype
= SOCK_DGRAM
;
180 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
184 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
188 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
190 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
191 if (r
->fd
< 0) continue;
193 r
->addr
= (struct sockaddr
*)malloc(ai
->ai_addrlen
);
194 if (r
->addr
== NULL
) return -1;
196 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
205 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
211 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
215 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
221 r
->type
= DST_TYPE_SOCK
;
226 if (strcmp(r
->dst
, "*") == 0)
228 r
->type
= DST_TYPE_WALL
;
233 /* Can't deal with dst! */
234 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
239 _syslog_dst_close(struct config_rule
*r
)
241 if (r
== NULL
) return;
254 if (r
->fd
>= 0) close(r
->fd
);
261 if (r
->fd
>= 0) close(r
->fd
);
278 _clean_facility_name(char *s
)
283 if (s
== NULL
) return NULL
;
285 if (len
== 0) return NULL
;
289 if ((*s
== '\'') || (*s
== '"'))
293 if (p
[len
- 1] == *s
) len
--;
296 out
= calloc(1, len
+ 1);
297 if (out
== NULL
) return NULL
;
306 char **semi
, **comma
, *star
;
307 int i
, j
, n
, lasts
, lastc
, pri
;
308 struct config_rule
*out
;
310 if (s
== NULL
) return -1;
311 while ((*s
== ' ') || (*s
== '\t')) s
++;
312 if (*s
== '#') return -1;
314 semi
= explode(s
, "; \t");
316 if (semi
== NULL
) return -1;
317 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
318 if (out
== NULL
) return -1;
323 for (i
= 0; semi
[i
] != NULL
; i
++)
325 if (semi
[i
][0] == '\0') continue;
330 out
->dst
= strdup(semi
[lasts
]);
331 if (out
->dst
== NULL
) return -1;
333 for (i
= 0; i
< lasts
; i
++)
335 if (semi
[i
][0] == '\0') continue;
336 comma
= explode(semi
[i
], ",.");
338 for (j
= 0; comma
[j
] != NULL
; j
++)
340 if (comma
[j
][0] == '\0') continue;
344 for (j
= 0; j
< lastc
; j
++)
346 if (comma
[j
][0] == '\0') continue;
347 pri
= _level_for_name(comma
[lastc
]);
348 if (pri
== -1) continue;
352 out
->facility
= (char **)calloc(1, sizeof(char *));
353 out
->fac_prefix_len
= (uint32_t *)calloc(1, sizeof(uint32_t));
354 out
->pri
= (int *)calloc(1, sizeof(int));
358 out
->facility
= (char **)reallocf(out
->facility
, (out
->count
+ 1) * sizeof(char *));
359 out
->fac_prefix_len
= (uint32_t *)reallocf(out
->fac_prefix_len
, (out
->count
+ 1) * sizeof(uint32_t));
360 out
->pri
= (int *)reallocf(out
->pri
, (out
->count
+ 1) * sizeof(int));
363 if (out
->facility
== NULL
) return -1;
364 if (out
->fac_prefix_len
== NULL
) return -1;
365 if (out
->pri
== NULL
) return -1;
367 out
->facility
[out
->count
] = _clean_facility_name(comma
[j
]);
368 if (out
->facility
[out
->count
] == NULL
) return -1;
370 out
->fac_prefix_len
[out
->count
] = 0;
371 star
= strchr(out
->facility
[out
->count
], '*');
372 if (star
!= NULL
) out
->fac_prefix_len
[out
->count
] = (uint32_t)(star
- out
->facility
[out
->count
]);
374 out
->pri
[out
->count
] = pri
;
383 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
389 _syslog_send_repeat_msg(struct config_rule
*r
)
395 if (r
== NULL
) return -1;
396 if (r
->type
!= DST_TYPE_FILE
) return 0;
397 if (r
->last_count
== 0) return 0;
400 memset(vt
, 0, sizeof(vt
));
405 asprintf(&msg
, "%s: --- last message repeated %u time%s ---\n", vt
+ 4, r
->last_count
, (r
->last_count
== 1) ? "" : "s");
406 if (msg
== NULL
) return -1;
409 status
= write(r
->fd
, msg
, len
);
410 if ((status
< 0) || (status
< len
))
412 asldebug("%s: error writing repeat message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
414 /* Try re-opening the file (once) and write again */
416 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
419 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
424 status
= write(r
->fd
, msg
, len
);
425 if ((status
< 0) || (status
< len
))
427 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
438 _syslog_send(aslmsg msg
, struct config_rule
*r
, char **out
, char **fwd
, time_t now
)
441 const char *vlevel
, *vfacility
;
443 int pf
, fc
, status
, is_dup
, do_write
;
444 uint32_t msg_hash
, n
;
446 if (out
== NULL
) return -1;
447 if (fwd
== NULL
) return -1;
448 if (r
== NULL
) return -1;
452 if (r
->type
== DST_TYPE_NOTE
)
454 notify_post(r
->dst
+1);
461 /* Build output string if it hasn't been built by a previous rule-match */
464 *out
= asl_format_message((asl_msg_t
*)msg
, ASL_MSG_FMT_BSD
, ASL_TIME_FMT_LCL
, ASL_ENCODE_SAFE
, &n
);
465 if (*out
== NULL
) return -1;
468 /* check if message is a duplicate of the last message, and inside the dup time window */
470 if ((global
.bsd_max_dup_time
> 0) && (*out
!= NULL
) && (r
->last_msg
!= NULL
))
472 msg_hash
= asl_core_string_hash(*out
+ 16, strlen(*out
+ 16));
473 if ((r
->last_hash
== msg_hash
) && (!strcmp(r
->last_msg
, *out
+ 16)))
475 if ((now
- r
->last_time
) < global
.bsd_max_dup_time
) is_dup
= 1;
479 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
482 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
483 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
485 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
486 if (fc
> 0) pf
|= fc
;
489 asprintf(&sf
, "<%d>%s", pf
, *out
);
490 if (sf
== NULL
) return -1;
496 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
497 else outlen
= strlen(*out
);
499 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
502 * If current message is NOT a duplicate and r->last_count > 0
503 * we need to write a "last message was repeated N times" log entry
505 if ((r
->type
== DST_TYPE_FILE
) && (is_dup
== 0) && (r
->last_count
> 0)) _syslog_send_repeat_msg(r
);
510 * Special case for kernel messages.
511 * Don't write kernel messages to /dev/console.
512 * The kernel printf routine already sends them to /dev/console
513 * so writing them here would cause duplicates.
515 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
516 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) do_write
= 0;
517 if ((do_write
== 1) && (r
->type
== DST_TYPE_FILE
) && (is_dup
== 1)) do_write
= 0;
519 if (do_write
== 0) status
= outlen
;
520 else status
= write(r
->fd
, *out
, outlen
);
522 if ((status
< 0) || (status
< outlen
))
524 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
526 /* Try re-opening the file (once) and write again */
528 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
531 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
535 status
= write(r
->fd
, *out
, outlen
);
536 if ((status
< 0) || (status
< outlen
))
538 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
542 else if ((r
->type
== DST_TYPE_SOCK
) && (r
->addr
!= NULL
))
544 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
545 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
547 else if (r
->type
== DST_TYPE_WALL
)
549 #ifndef CONFIG_IPHONE
550 FILE *pw
= popen(_PATH_WALL
, "w");
553 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
557 fprintf(pw
, "%s", *out
);
571 if (*out
!= NULL
) r
->last_msg
= strdup(*out
+ 16);
573 r
->last_hash
= msg_hash
;
582 _syslog_rule_match(aslmsg msg
, struct config_rule
*r
)
584 uint32_t i
, test
, f
, pri
;
587 if (msg
== NULL
) return 0;
588 if (r
== NULL
) return 0;
589 if (r
->count
== 0) return 0;
593 for (i
= 0; i
< r
->count
; i
++)
595 if (r
->pri
[i
] == -1) continue;
597 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
598 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
601 val
= asl_get(msg
, ASL_KEY_FACILITY
);
603 if (strcmp(r
->facility
[i
], "*") == 0)
607 else if ((r
->fac_prefix_len
[i
] > 0) && (strncasecmp(r
->facility
[i
], val
, r
->fac_prefix_len
[i
]) == 0))
611 else if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0))
616 if (f
== 0) continue;
618 /* Turn off matching facility with priority "none" */
625 val
= asl_get(msg
, ASL_KEY_LEVEL
);
626 if (val
== NULL
) continue;
629 if (pri
< 0) continue;
631 if (pri
<= r
->pri
[i
]) test
= 1;
638 bsd_out_sendmsg(aslmsg msg
, const char *outid
)
640 struct config_rule
*r
;
645 if (reset
!= RESET_NONE
) _do_reset();
647 if (msg
== NULL
) return -1;
653 global
.bsd_flush_time
= 0;
655 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
657 if (_syslog_rule_match(msg
, r
) == 1) _syslog_send(msg
, r
, &out
, &fwd
, tick
);
658 if ((r
->type
== DST_TYPE_FILE
) && (r
->last_count
> 0))
660 delta
= tick
- r
->last_time
;
661 if (delta
< global
.bsd_max_dup_time
)
663 delta
= global
.bsd_max_dup_time
- delta
;
664 if (global
.bsd_flush_time
== 0) global
.bsd_flush_time
= delta
;
665 else if (delta
< global
.bsd_flush_time
) global
.bsd_flush_time
= delta
;
677 bsd_close_idle_files(time_t now
)
679 struct config_rule
*r
;
682 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
684 /* only applies to files */
685 if (r
->type
!= DST_TYPE_FILE
) continue;
688 * If the last message repeat count is non-zero, a bsd_flush_duplicates()
689 * call will occur within 30 seconds. Don't bother closing the file.
691 if (r
->last_count
> 0) continue;
693 delta
= now
- r
->last_time
;
694 if (delta
> CLOSE_ON_IDLE_SEC
) _syslog_dst_close(r
);
699 bsd_flush_duplicates(time_t now
)
701 struct config_rule
*r
;
704 global
.bsd_flush_time
= 0;
706 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
708 if (r
->type
!= DST_TYPE_FILE
) continue;
710 if (r
->last_count
> 0)
712 delta
= now
- r
->last_time
;
713 if (delta
< global
.bsd_max_dup_time
)
715 delta
= global
.bsd_max_dup_time
- delta
;
716 if (global
.bsd_flush_time
== 0) global
.bsd_flush_time
= delta
;
717 else if (delta
< global
.bsd_flush_time
) global
.bsd_flush_time
= delta
;
722 _syslog_send_repeat_msg(r
);
731 _parse_config_file(const char *confname
)
736 cf
= fopen(confname
, "r");
737 if (cf
== NULL
) return 1;
739 while (NULL
!= (line
= get_line_from_file(cf
)))
753 asldebug("%s: init\n", MY_ID
);
755 TAILQ_INIT(&bsd_out_rule
);
757 query
= asl_msg_new(ASL_TYPE_QUERY
);
758 aslevent_addmatch(query
, MY_ID
);
759 aslevent_addoutput(bsd_out_sendmsg
, MY_ID
);
761 _parse_config_file(_PATH_SYSLOG_CONF
);
768 reset
= global
.reset
;
775 struct config_rule
*r
, *n
;
779 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
781 n
= r
->entries
.tqe_next
;
786 free(r
->fac_prefix_len
);
789 if (r
->fd
>= 0) close(r
->fd
);
791 if (r
->facility
!= NULL
)
793 for (i
= 0; i
< r
->count
; i
++)
794 free(r
->facility
[i
]);
799 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);
807 bsd_out_network_reset(void)
809 struct config_rule
*r
;
811 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
813 if (r
->type
== DST_TYPE_SOCK
)