]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/bsd_out.c
6e7bd2affaffea576724b399120f5d2056a1e395
2 * Copyright (c) 2004-2008 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>
40 #define MY_ID "bsd_out"
42 #define _PATH_WALL "/usr/bin/wall"
43 #define ASL_KEY_FACILITY "Facility"
44 #define FACILITY_KERNEL "kern"
45 #define _PATH_CONSOLE "/dev/console"
46 #define IndexNull ((uint32_t)-1)
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 static asl_msg_t
*query
= NULL
;
64 struct sockaddr
*addr
;
71 TAILQ_ENTRY(config_rule
) entries
;
74 static TAILQ_HEAD(cr
, config_rule
) bsd_out_rule
;
76 extern uint32_t asl_core_string_hash(const char *s
, uint32_t inlen
);
79 static int _parse_config_file(const char *);
85 _parse_config_file(_PATH_SYSLOG_CONF
);
89 _insertString(char *s
, char **l
, uint32_t x
)
93 if (s
== NULL
) return l
;
96 l
= (char **)malloc(2 * sizeof(char *));
97 if (l
== NULL
) return NULL
;
110 for (i
= 0; l
[i
] != NULL
; i
++);
111 len
= i
+ 1; /* count the NULL on the end of the list too! */
113 l
= (char **)reallocf(l
, (len
+ 1) * sizeof(char *));
114 if (l
== NULL
) return NULL
;
116 if ((x
>= (len
- 1)) || (x
== IndexNull
))
118 l
[len
- 1] = strdup(s
);
119 if (l
[len
- 1] == NULL
)
129 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
131 if (l
[x
] == NULL
) return NULL
;
137 _explode(char *s
, char *delim
)
143 if (s
== NULL
) return NULL
;
148 for (i
= 0; ((p
[i
] != '\0') && (strchr(delim
, p
[i
]) == NULL
)); i
++);
151 if (t
== NULL
) return NULL
;
153 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
155 l
= _insertString(t
, l
, IndexNull
);
158 if (p
[i
] == '\0') return l
;
159 if (p
[i
+ 1] == '\0') l
= _insertString("", l
, IndexNull
);
171 if (l
== NULL
) return;
172 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
177 _level_for_name(const char *name
)
179 if (name
== NULL
) return -1;
181 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
182 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
183 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
184 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
185 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
186 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
187 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
188 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
189 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
190 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
191 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
192 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
195 if (!strcasecmp(name
, "none")) return -2;
201 _syslog_dst_open(struct config_rule
*r
)
205 struct addrinfo hints
, *gai
, *ai
;
207 if (r
== NULL
) return -1;
208 if (r
->fd
!= -1) return 0;
210 if (r
->dst
[0] == '/')
212 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
215 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
219 r
->type
= DST_TYPE_FILE
;
220 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
225 if (r
->dst
[0] == '!')
227 r
->type
= DST_TYPE_NOTE
;
232 if (r
->dst
[0] == '@')
234 node
= strdup(r
->dst
+ 1);
235 if (node
== NULL
) return -1;
238 serv
= strrchr(node
, ':');
239 if (serv
!= NULL
) *serv
++ = '\0';
240 else serv
= "syslog";
242 memset(&hints
, 0, sizeof(hints
));
243 hints
.ai_family
= PF_UNSPEC
;
244 hints
.ai_socktype
= SOCK_DGRAM
;
245 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
249 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
253 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
255 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
256 if (r
->fd
< 0) continue;
258 r
->addr
= (struct sockaddr
*)calloc(1, ai
->ai_addrlen
);
259 if (r
->addr
== NULL
) return -1;
261 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
270 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
274 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
278 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
282 r
->type
= DST_TYPE_SOCK
;
287 if (strcmp(r
->dst
, "*") == 0)
289 r
->type
= DST_TYPE_WALL
;
294 /* Can't deal with dst! */
295 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
300 _syslog_dst_close(struct config_rule
*r
)
302 if (r
== NULL
) return;
309 if (r
->fd
>= 0) close(r
->fd
);
316 if (r
->fd
>= 0) close(r
->fd
);
318 if (r
->addr
!= NULL
) free(r
->addr
);
337 char **semi
, **comma
;
338 int i
, j
, n
, lasts
, lastc
, pri
;
339 struct config_rule
*out
;
341 if (s
== NULL
) return -1;
342 while ((*s
== ' ') || (*s
== '\t')) s
++;
343 if (*s
== '#') return -1;
345 semi
= _explode(s
, "; \t");
347 if (semi
== NULL
) return -1;
348 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
349 if (out
== NULL
) return -1;
354 for (i
= 0; semi
[i
] != NULL
; i
++)
356 if (semi
[i
][0] == '\0') continue;
361 out
->dst
= strdup(semi
[lasts
]);
362 if (out
->dst
== NULL
) return -1;
364 for (i
= 0; i
< lasts
; i
++)
366 if (semi
[i
][0] == '\0') continue;
367 comma
= _explode(semi
[i
], ",.");
369 for (j
= 0; comma
[j
] != NULL
; j
++)
371 if (comma
[j
][0] == '\0') continue;
375 for (j
= 0; j
< lastc
; j
++)
377 if (comma
[j
][0] == '\0') continue;
378 pri
= _level_for_name(comma
[lastc
]);
379 if (pri
== -1) continue;
383 out
->facility
= (char **)calloc(1, sizeof(char *));
384 out
->pri
= (int *)calloc(1, sizeof(int));
388 out
->facility
= (char **)reallocf(out
->facility
, (out
->count
+ 1) * sizeof(char *));
389 out
->pri
= (int *)reallocf(out
->pri
, (out
->count
+ 1) * sizeof(int));
392 if (out
->facility
== NULL
) return -1;
393 if (out
->pri
== NULL
) return -1;
395 out
->facility
[out
->count
] = strdup(comma
[j
]);
396 if (out
->facility
[out
->count
] == NULL
) return -1;
398 out
->pri
[out
->count
] = pri
;
407 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
413 bsd_log_string(const char *msg
)
415 uint32_t i
, len
, outlen
;
419 if (msg
== NULL
) return NULL
;
422 while ((len
> 0) && (msg
[len
- 1] == '\n')) len
--;
424 if (len
== 0) return NULL
;
427 for (i
= 0; i
< len
; i
++)
430 if (isascii(c
) && iscntrl(c
) && (c
!= '\t')) outlen
++;
433 out
= malloc(outlen
);
434 if (out
== NULL
) return NULL
;
438 for (i
= 0; i
< len
; i
++)
442 if (isascii(c
) && iscntrl(c
))
471 _syslog_send_repeat_msg(struct config_rule
*r
)
473 char vt
[16], *p
, *msg
;
477 if (r
== NULL
) return -1;
478 if (r
->type
!= DST_TYPE_FILE
) return 0;
479 if (r
->last_count
== 0) return 0;
483 if (p
== NULL
) return -1;
489 asprintf(&msg
, "%s: --- last message repeated %u time%s ---\n", vt
, r
->last_count
, (r
->last_count
== 1) ? "" : "s");
490 if (msg
== NULL
) return -1;
493 status
= write(r
->fd
, msg
, len
);
494 if ((status
< 0) || (status
< len
))
496 asldebug("%s: error writing repeat message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
498 /* Try re-opening the file (once) and write again */
500 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
503 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
508 status
= write(r
->fd
, msg
, len
);
509 if ((status
< 0) || (status
< len
))
511 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
522 _syslog_send(asl_msg_t
*msg
, struct config_rule
*r
, char **out
, char **fwd
, time_t now
)
524 char *so
, *sf
, *vt
, *p
, *outmsg
;
525 const char *vtime
, *vhost
, *vident
, *vpid
, *vmsg
, *vlevel
, *vfacility
, *vrefproc
, *vrefpid
;
528 int pf
, fc
, status
, is_dup
, do_write
;
532 if (out
== NULL
) return -1;
533 if (fwd
== NULL
) return -1;
534 if (r
== NULL
) return -1;
536 if (r
->type
== DST_TYPE_NOTE
)
538 notify_post(r
->dst
+1);
546 /* Build output string if it hasn't been built by a previous rule-match */
549 vtime
= asl_get(msg
, ASL_KEY_TIME
);
552 tick
= asl_parse_time(vtime
);
553 if (tick
!= (time_t)-1)
557 if (vt
== NULL
) return -1;
562 else if (strlen(vtime
) < 24)
565 if (vt
== NULL
) return -1;
570 if (vt
== NULL
) return -1;
572 memcpy(vt
, vtime
+4, 15);
582 if (vt
== NULL
) return -1;
588 vhost
= asl_get(msg
, ASL_KEY_HOST
);
589 if (vhost
== NULL
) vhost
= "localhost";
591 vident
= asl_get(msg
, ASL_KEY_SENDER
);
592 if ((vident
!= NULL
) && (!strcmp(vident
, "Unknown"))) vident
= NULL
;
594 vpid
= asl_get(msg
, ASL_KEY_PID
);
595 if ((vpid
!= NULL
) && (!strcmp(vpid
, "-1"))) vpid
= NULL
;
597 if ((vpid
!= NULL
) && (vident
== NULL
)) vident
= "Unknown";
599 vrefproc
= asl_get(msg
, ASL_KEY_REF_PROC
);
600 vrefpid
= asl_get(msg
, ASL_KEY_REF_PID
);
602 vmsg
= asl_get(msg
, ASL_KEY_MSG
);
603 if (vmsg
!= NULL
) outmsg
= bsd_log_string(vmsg
);
607 if (vt
!= NULL
) n
+= (strlen(vt
) + 1);
610 if (vhost
!= NULL
) n
+= (strlen(vhost
) + 1);
613 if (vident
!= NULL
) n
+= strlen(vident
);
616 if (vpid
!= NULL
) n
+= (strlen(vpid
) + 2);
619 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
)) n
+= 2;
622 if (vrefproc
!= NULL
) n
+= strlen(vrefproc
);
625 if (vrefpid
!= NULL
) n
+= (strlen(vrefpid
) + 2);
628 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
)) n
+= 1;
634 if (outmsg
!= NULL
) n
+= strlen(outmsg
);
636 if (n
== 0) return -1;
642 if (so
== NULL
) return -1;
667 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
))
671 if (vrefproc
!= NULL
) strcat(so
, vrefproc
);
697 /* check if message is a duplicate of the last message, and inside the dup time window */
699 if ((global
.bsd_max_dup_time
> 0) && (*out
!= NULL
) && (r
->last_msg
!= NULL
))
701 msg_hash
= asl_core_string_hash(*out
+ 16, strlen(*out
+ 16));
702 if ((r
->last_hash
== msg_hash
) && (!strcmp(r
->last_msg
, *out
+ 16)))
704 if ((now
- r
->last_time
) < global
.bsd_max_dup_time
) is_dup
= 1;
708 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
711 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
712 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
714 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
715 if (fc
> 0) pf
|= fc
;
718 asprintf(&sf
, "<%d>%s", pf
, *out
);
719 if (sf
== NULL
) return -1;
725 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
726 else outlen
= strlen(*out
);
730 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
733 * If current message is NOT a duplicate and r->last_count > 0
734 * we need to write a "last message was repeated N times" log entry
736 if ((r
->type
== DST_TYPE_FILE
) && (is_dup
== 0) && (r
->last_count
> 0)) _syslog_send_repeat_msg(r
);
741 * Special case for kernel messages.
742 * Don't write kernel messages to /dev/console.
743 * The kernel printf routine already sends them to /dev/console
744 * so writing them here would cause duplicates.
746 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
747 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) do_write
= 0;
748 if ((do_write
== 1) && (r
->type
== DST_TYPE_FILE
) && (is_dup
== 1)) do_write
= 0;
750 if (do_write
== 0) status
= outlen
;
751 else status
= write(r
->fd
, *out
, outlen
);
753 if ((status
< 0) || (status
< outlen
))
755 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
757 /* Try re-opening the file (once) and write again */
759 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
762 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
766 status
= write(r
->fd
, *out
, outlen
);
767 if ((status
< 0) || (status
< outlen
))
769 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
773 else if ((r
->type
== DST_TYPE_SOCK
) && (r
->addr
!= NULL
))
775 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
776 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
778 else if (r
->type
== DST_TYPE_WALL
)
780 pw
= popen(_PATH_WALL
, "w");
783 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
791 _syslog_dst_close(r
);
799 if (r
->last_msg
!= NULL
) free(r
->last_msg
);
802 if (*out
!= NULL
) r
->last_msg
= strdup(*out
+ 16);
804 r
->last_hash
= msg_hash
;
813 _syslog_rule_match(asl_msg_t
*msg
, struct config_rule
*r
)
815 uint32_t i
, test
, f
, pri
;
818 if (msg
== NULL
) return 0;
819 if (r
== NULL
) return 0;
820 if (r
->count
== 0) return 0;
824 for (i
= 0; i
< r
->count
; i
++)
826 if (r
->pri
[i
] == -1) continue;
828 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
829 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
832 if (strcmp(r
->facility
[i
], "*") == 0) f
= 1;
835 val
= asl_get(msg
, ASL_KEY_FACILITY
);
836 if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0)) f
= 1;
839 if (f
== 0) continue;
841 /* Turn off matching facility with priority "none" */
848 val
= asl_get(msg
, ASL_KEY_LEVEL
);
849 if (val
== NULL
) continue;
852 if (pri
< 0) continue;
854 if (pri
<= r
->pri
[i
]) test
= 1;
861 bsd_out_sendmsg(asl_msg_t
*msg
, const char *outid
)
863 struct config_rule
*r
;
874 if (msg
== NULL
) return -1;
880 global
.bsd_flush_time
= 0;
882 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
884 if (_syslog_rule_match(msg
, r
) == 1) _syslog_send(msg
, r
, &out
, &fwd
, tick
);
885 if ((r
->type
== DST_TYPE_FILE
) && (r
->last_count
> 0))
887 delta
= tick
- r
->last_time
;
888 if (delta
< global
.bsd_max_dup_time
)
890 delta
= global
.bsd_max_dup_time
- delta
;
891 if (global
.bsd_flush_time
== 0) global
.bsd_flush_time
= delta
;
892 else if (delta
< global
.bsd_flush_time
) global
.bsd_flush_time
= delta
;
897 if (out
!= NULL
) free(out
);
898 if (fwd
!= NULL
) free(fwd
);
904 bsd_flush_duplicates(time_t now
)
906 struct config_rule
*r
;
909 global
.bsd_flush_time
= 0;
911 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
913 if (r
->type
!= DST_TYPE_FILE
) continue;
915 if (r
->last_count
> 0)
917 delta
= now
- r
->last_time
;
918 if (delta
< global
.bsd_max_dup_time
)
920 delta
= global
.bsd_max_dup_time
- delta
;
921 if (global
.bsd_flush_time
== 0) global
.bsd_flush_time
= delta
;
922 else if (delta
< global
.bsd_flush_time
) global
.bsd_flush_time
= delta
;
927 _syslog_send_repeat_msg(r
);
928 _syslog_dst_close(r
);
937 _parse_config_file(const char *confname
)
942 cf
= fopen(confname
, "r");
943 if (cf
== NULL
) return 1;
945 while (NULL
!= (line
= get_line_from_file(cf
)))
959 asldebug("%s: init\n", MY_ID
);
961 TAILQ_INIT(&bsd_out_rule
);
963 query
= asl_new(ASL_TYPE_QUERY
);
964 aslevent_addmatch(query
, MY_ID
);
965 aslevent_addoutput(bsd_out_sendmsg
, MY_ID
);
967 _parse_config_file(_PATH_SYSLOG_CONF
);
981 struct config_rule
*r
, *n
;
985 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
987 n
= r
->entries
.tqe_next
;
989 if (r
->dst
!= NULL
) free(r
->dst
);
990 if (r
->fd
> 0) close(r
->fd
);
991 if (r
->addr
!= NULL
) free(r
->addr
);
992 if (r
->last_msg
!= NULL
) free(r
->last_msg
);
993 if (r
->facility
!= NULL
)
995 for (i
= 0; i
< r
->count
; i
++)
997 if (r
->facility
[i
] != NULL
) free(r
->facility
[i
]);
1001 if (r
->pri
!= NULL
) free(r
->pri
);
1003 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);