]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/bsd_out.c
7da14d8b54b76f335448ddf3a50ac3c9e76246f0
2 * Copyright (c) 2004 Apple Computer, 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
;
58 extern uint64_t bsd_flush_time
;
59 extern uint64_t bsd_max_dup_time
;
67 struct sockaddr
*addr
;
74 TAILQ_ENTRY(config_rule
) entries
;
77 static TAILQ_HEAD(cr
, config_rule
) bsd_out_rule
;
79 extern uint32_t string_hash(const char *s
, uint32_t inlen
);
82 static int _parse_config_file(const char *);
88 _parse_config_file(_PATH_SYSLOG_CONF
);
92 _insertString(char *s
, char **l
, uint32_t x
)
96 if (s
== NULL
) return l
;
99 l
= (char **)malloc(2 * sizeof(char *));
100 if (l
== NULL
) return NULL
;
113 for (i
= 0; l
[i
] != NULL
; i
++);
114 len
= i
+ 1; /* count the NULL on the end of the list too! */
116 l
= (char **)reallocf(l
, (len
+ 1) * sizeof(char *));
117 if (l
== NULL
) return NULL
;
119 if ((x
>= (len
- 1)) || (x
== IndexNull
))
121 l
[len
- 1] = strdup(s
);
122 if (l
[len
- 1] == NULL
)
132 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
134 if (l
[x
] == NULL
) return NULL
;
140 _explode(char *s
, char *delim
)
146 if (s
== NULL
) return NULL
;
151 for (i
= 0; ((p
[i
] != '\0') && (strchr(delim
, p
[i
]) == NULL
)); i
++);
154 if (t
== NULL
) return NULL
;
156 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
158 l
= _insertString(t
, l
, IndexNull
);
161 if (p
[i
] == '\0') return l
;
162 if (p
[i
+ 1] == '\0') l
= _insertString("", l
, IndexNull
);
174 if (l
== NULL
) return;
175 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
180 _level_for_name(const char *name
)
182 if (name
== NULL
) return -1;
184 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
185 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
186 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
187 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
188 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
189 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
190 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
191 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
192 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
193 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
194 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
195 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
198 if (!strcasecmp(name
, "none")) return -2;
204 _syslog_dst_open(struct config_rule
*r
)
208 struct addrinfo hints
, *gai
, *ai
;
210 if (r
== NULL
) return -1;
211 if (r
->fd
!= -1) return 0;
213 if (r
->dst
[0] == '/')
215 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
218 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
222 r
->type
= DST_TYPE_FILE
;
223 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
228 if (r
->dst
[0] == '!')
230 r
->type
= DST_TYPE_NOTE
;
235 if (r
->dst
[0] == '@')
237 node
= strdup(r
->dst
+ 1);
238 if (node
== NULL
) return -1;
241 serv
= strrchr(node
, ':');
242 if (serv
!= NULL
) *serv
++ = '\0';
243 else serv
= "syslog";
245 memset(&hints
, 0, sizeof(hints
));
246 hints
.ai_family
= PF_UNSPEC
;
247 hints
.ai_socktype
= SOCK_DGRAM
;
248 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
252 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
256 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
258 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
259 if (r
->fd
< 0) continue;
261 r
->addr
= (struct sockaddr
*)calloc(1, ai
->ai_addrlen
);
262 if (r
->addr
== NULL
) return -1;
264 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
273 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
277 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
281 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
285 r
->type
= DST_TYPE_SOCK
;
290 if (strcmp(r
->dst
, "*") == 0)
292 r
->type
= DST_TYPE_WALL
;
297 /* Can't deal with dst! */
298 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
303 _syslog_dst_close(struct config_rule
*r
)
305 if (r
== NULL
) return;
312 if (r
->fd
>= 0) close(r
->fd
);
319 if (r
->fd
>= 0) close(r
->fd
);
321 if (r
->addr
!= NULL
) free(r
->addr
);
340 char **semi
, **comma
;
341 int i
, j
, n
, lasts
, lastc
, pri
;
342 struct config_rule
*out
;
344 if (s
== NULL
) return -1;
345 while ((*s
== ' ') || (*s
== '\t')) s
++;
346 if (*s
== '#') return -1;
348 semi
= _explode(s
, "; \t");
350 if (semi
== NULL
) return -1;
351 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
352 if (out
== NULL
) return -1;
357 for (i
= 0; semi
[i
] != NULL
; i
++)
359 if (semi
[i
][0] == '\0') continue;
364 out
->dst
= strdup(semi
[lasts
]);
365 if (out
->dst
== NULL
) return -1;
367 for (i
= 0; i
< lasts
; i
++)
369 if (semi
[i
][0] == '\0') continue;
370 comma
= _explode(semi
[i
], ",.");
372 for (j
= 0; comma
[j
] != NULL
; j
++)
374 if (comma
[j
][0] == '\0') continue;
378 for (j
= 0; j
< lastc
; j
++)
380 if (comma
[j
][0] == '\0') continue;
381 pri
= _level_for_name(comma
[lastc
]);
382 if (pri
== -1) continue;
386 out
->facility
= (char **)calloc(1, sizeof(char *));
387 out
->pri
= (int *)calloc(1, sizeof(int));
391 out
->facility
= (char **)reallocf(out
->facility
, (out
->count
+ 1) * sizeof(char *));
392 out
->pri
= (int *)reallocf(out
->pri
, (out
->count
+ 1) * sizeof(int));
395 if (out
->facility
== NULL
) return -1;
396 if (out
->pri
== NULL
) return -1;
398 out
->facility
[out
->count
] = strdup(comma
[j
]);
399 if (out
->facility
[out
->count
] == NULL
) return -1;
401 out
->pri
[out
->count
] = pri
;
410 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
416 bsd_log_string(const char *msg
)
418 uint32_t i
, len
, outlen
;
422 if (msg
== NULL
) return NULL
;
425 while ((len
> 0) && (msg
[len
- 1] == '\n')) len
--;
427 if (len
== 0) return NULL
;
430 for (i
= 0; i
< len
; i
++)
433 if (isascii(c
) && iscntrl(c
) && (c
!= '\t')) outlen
++;
436 out
= malloc(outlen
);
437 if (out
== NULL
) return NULL
;
441 for (i
= 0; i
< len
; i
++)
445 if (isascii(c
) && iscntrl(c
))
474 _syslog_send_repeat_msg(struct config_rule
*r
)
476 char vt
[16], *p
, *msg
;
480 if (r
== NULL
) return -1;
481 if (r
->type
!= DST_TYPE_FILE
) return 0;
482 if (r
->last_count
== 0) return 0;
486 if (p
== NULL
) return -1;
492 asprintf(&msg
, "%s: --- last message repeated %u time%s ---\n", vt
, r
->last_count
, (r
->last_count
== 1) ? "" : "s");
493 if (msg
== NULL
) return -1;
496 status
= write(r
->fd
, msg
, len
);
497 if ((status
< 0) || (status
< len
))
499 asldebug("%s: error writing repeat message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
501 /* Try re-opening the file (once) and write again */
503 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
506 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
511 status
= write(r
->fd
, msg
, len
);
512 if ((status
< 0) || (status
< len
))
514 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
525 _syslog_send(asl_msg_t
*msg
, struct config_rule
*r
, char **out
, char **fwd
, time_t now
)
527 char *so
, *sf
, *vt
, *p
, *outmsg
;
528 const char *vtime
, *vhost
, *vident
, *vpid
, *vmsg
, *vlevel
, *vfacility
, *vrefproc
, *vrefpid
;
531 int pf
, fc
, status
, is_dup
, do_write
;
535 if (out
== NULL
) return -1;
536 if (fwd
== NULL
) return -1;
537 if (r
== NULL
) return -1;
539 if (r
->type
== DST_TYPE_NOTE
)
541 notify_post(r
->dst
+1);
549 /* Build output string if it hasn't been built by a previous rule-match */
552 vtime
= asl_get(msg
, ASL_KEY_TIME
);
555 tick
= asl_parse_time(vtime
);
556 if (tick
!= (time_t)-1)
560 if (vt
== NULL
) return -1;
565 else if (strlen(vtime
) < 24)
568 if (vt
== NULL
) return -1;
573 if (vt
== NULL
) return -1;
575 memcpy(vt
, vtime
+4, 15);
585 if (vt
== NULL
) return -1;
591 vhost
= asl_get(msg
, ASL_KEY_HOST
);
592 if (vhost
== NULL
) vhost
= "localhost";
594 vident
= asl_get(msg
, ASL_KEY_SENDER
);
595 if ((vident
!= NULL
) && (!strcmp(vident
, "Unknown"))) vident
= NULL
;
597 vpid
= asl_get(msg
, ASL_KEY_PID
);
598 if ((vpid
!= NULL
) && (!strcmp(vpid
, "-1"))) vpid
= NULL
;
600 if ((vpid
!= NULL
) && (vident
== NULL
)) vident
= "Unknown";
602 vrefproc
= asl_get(msg
, ASL_KEY_REF_PROC
);
603 vrefpid
= asl_get(msg
, ASL_KEY_REF_PID
);
605 vmsg
= asl_get(msg
, ASL_KEY_MSG
);
606 if (vmsg
!= NULL
) outmsg
= bsd_log_string(vmsg
);
610 if (vt
!= NULL
) n
+= (strlen(vt
) + 1);
613 if (vhost
!= NULL
) n
+= (strlen(vhost
) + 1);
616 if (vident
!= NULL
) n
+= strlen(vident
);
619 if (vpid
!= NULL
) n
+= (strlen(vpid
) + 2);
622 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
)) n
+= 2;
625 if (vrefproc
!= NULL
) n
+= strlen(vrefproc
);
628 if (vrefpid
!= NULL
) n
+= (strlen(vrefpid
) + 2);
631 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
)) n
+= 1;
637 if (outmsg
!= NULL
) n
+= strlen(outmsg
);
639 if (n
== 0) return -1;
645 if (so
== NULL
) return -1;
670 if ((vrefproc
!= NULL
) || (vrefpid
!= NULL
))
674 if (vrefproc
!= NULL
) strcat(so
, vrefproc
);
700 /* check if message is a duplicate of the last message, and inside the dup time window */
702 if ((bsd_max_dup_time
> 0) && (*out
!= NULL
) && (r
->last_msg
!= NULL
))
704 msg_hash
= string_hash(*out
+ 16, strlen(*out
+ 16));
705 if ((r
->last_hash
== msg_hash
) && (!strcmp(r
->last_msg
, *out
+ 16)))
707 if ((now
- r
->last_time
) < bsd_max_dup_time
) is_dup
= 1;
711 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
714 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
715 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
717 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
718 if (fc
> 0) pf
|= fc
;
721 asprintf(&sf
, "<%d>%s", pf
, *out
);
722 if (sf
== NULL
) return -1;
728 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
729 else outlen
= strlen(*out
);
733 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
736 * If current message is NOT a duplicate and r->last_count > 0
737 * we need to write a "last message was repeated N times" log entry
739 if ((r
->type
== DST_TYPE_FILE
) && (is_dup
== 0) && (r
->last_count
> 0)) _syslog_send_repeat_msg(r
);
744 * Special case for kernel messages.
745 * Don't write kernel messages to /dev/console.
746 * The kernel printf routine already sends them to /dev/console
747 * so writing them here would cause duplicates.
749 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
750 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) do_write
= 0;
751 if ((do_write
== 1) && (r
->type
== DST_TYPE_FILE
) && (is_dup
== 1)) do_write
= 0;
753 if (do_write
== 0) status
= outlen
;
754 else status
= write(r
->fd
, *out
, outlen
);
756 if ((status
< 0) || (status
< outlen
))
758 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
760 /* Try re-opening the file (once) and write again */
762 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
| O_NOCTTY
, 0644);
765 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
769 status
= write(r
->fd
, *out
, outlen
);
770 if ((status
< 0) || (status
< outlen
))
772 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
776 else if ((r
->type
== DST_TYPE_SOCK
) && (r
->addr
!= NULL
))
778 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
779 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
781 else if (r
->type
== DST_TYPE_WALL
)
783 pw
= popen(_PATH_WALL
, "w");
786 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
794 _syslog_dst_close(r
);
802 if (r
->last_msg
!= NULL
) free(r
->last_msg
);
805 if (*out
!= NULL
) r
->last_msg
= strdup(*out
+ 16);
807 r
->last_hash
= msg_hash
;
816 _syslog_rule_match(asl_msg_t
*msg
, struct config_rule
*r
)
818 uint32_t i
, test
, f
, pri
;
821 if (msg
== NULL
) return 0;
822 if (r
== NULL
) return 0;
823 if (r
->count
== 0) return 0;
827 for (i
= 0; i
< r
->count
; i
++)
829 if (r
->pri
[i
] == -1) continue;
831 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
832 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
835 if (strcmp(r
->facility
[i
], "*") == 0) f
= 1;
838 val
= asl_get(msg
, ASL_KEY_FACILITY
);
839 if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0)) f
= 1;
842 if (f
== 0) continue;
844 /* Turn off matching facility with priority "none" */
851 val
= asl_get(msg
, ASL_KEY_LEVEL
);
852 if (val
== NULL
) continue;
855 if (pri
< 0) continue;
857 if (pri
<= r
->pri
[i
]) test
= 1;
864 bsd_out_sendmsg(asl_msg_t
*msg
, const char *outid
)
866 struct config_rule
*r
;
877 if (msg
== NULL
) return -1;
885 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
887 if (_syslog_rule_match(msg
, r
) == 1) _syslog_send(msg
, r
, &out
, &fwd
, tick
);
888 if ((r
->type
== DST_TYPE_FILE
) && (r
->last_count
> 0))
890 delta
= tick
- r
->last_time
;
891 if (delta
< bsd_max_dup_time
)
893 delta
= bsd_max_dup_time
- delta
;
894 if (bsd_flush_time
== 0) bsd_flush_time
= delta
;
895 else if (delta
< bsd_flush_time
) bsd_flush_time
= delta
;
900 if (out
!= NULL
) free(out
);
901 if (fwd
!= NULL
) free(fwd
);
907 bsd_flush_duplicates()
909 struct config_rule
*r
;
916 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
918 if (r
->type
!= DST_TYPE_FILE
) continue;
920 if (r
->last_count
> 0)
922 delta
= tick
- r
->last_time
;
923 if (delta
< bsd_max_dup_time
)
925 delta
= bsd_max_dup_time
- delta
;
926 if (bsd_flush_time
== 0) bsd_flush_time
= delta
;
927 else if (delta
< bsd_flush_time
) bsd_flush_time
= delta
;
932 _syslog_send_repeat_msg(r
);
933 _syslog_dst_close(r
);
942 _parse_config_file(const char *confname
)
947 cf
= fopen(confname
, "r");
948 if (cf
== NULL
) return 1;
950 while (NULL
!= (line
= get_line_from_file(cf
)))
964 asldebug("%s: init\n", MY_ID
);
966 TAILQ_INIT(&bsd_out_rule
);
968 query
= asl_new(ASL_TYPE_QUERY
);
969 aslevent_addmatch(query
, MY_ID
);
970 aslevent_addoutput(bsd_out_sendmsg
, MY_ID
);
972 _parse_config_file(_PATH_SYSLOG_CONF
);
986 struct config_rule
*r
, *n
;
990 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
992 n
= r
->entries
.tqe_next
;
994 if (r
->dst
!= NULL
) free(r
->dst
);
995 if (r
->fd
> 0) close(r
->fd
);
996 if (r
->addr
!= NULL
) free(r
->addr
);
997 if (r
->last_msg
!= NULL
) free(r
->last_msg
);
998 if (r
->facility
!= NULL
)
1000 for (i
= 0; i
< r
->count
; i
++)
1002 if (r
->facility
[i
] != NULL
) free(r
->facility
[i
]);
1006 if (r
->pri
!= NULL
) free(r
->pri
);
1008 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);