]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/bsd_out.c
a24fdaf9578de8ec75df5b16e65b10a753bf4d68
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
27 #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
;
67 TAILQ_ENTRY(config_rule
) entries
;
70 static TAILQ_HEAD(cr
, config_rule
) bsd_out_rule
;
73 static int _parse_config_file(const char *);
79 _parse_config_file(_PATH_SYSLOG_CONF
);
83 _insertString(char *s
, char **l
, uint32_t x
)
87 if (s
== NULL
) return l
;
90 l
= (char **)malloc(2 * sizeof(char *));
96 for (i
= 0; l
[i
] != NULL
; i
++);
97 len
= i
+ 1; /* count the NULL on the end of the list too! */
99 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
101 if ((x
>= (len
- 1)) || (x
== IndexNull
))
103 l
[len
- 1] = strdup(s
);
108 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
114 _explode(char *s
, char *delim
)
120 if (s
== NULL
) return NULL
;
125 for (i
= 0; ((p
[i
] != '\0') && (strchr(delim
, p
[i
]) == NULL
)); i
++);
128 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
130 l
= _insertString(t
, l
, IndexNull
);
133 if (p
[i
] == '\0') return l
;
134 if (p
[i
+ 1] == '\0') l
= _insertString("", l
, IndexNull
);
146 if (l
== NULL
) return;
147 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
152 _level_for_name(const char *name
)
154 if (name
== NULL
) return -1;
156 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
157 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
158 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
159 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
160 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
161 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
162 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
163 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
164 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
165 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
166 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
167 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
170 if (!strcasecmp(name
, "none")) return -2;
176 _syslog_dst_open(struct config_rule
*r
)
180 struct addrinfo hints
, *gai
, *ai
;
182 if (r
== NULL
) return -1;
186 if (r
->dst
[0] == '/')
188 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
191 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
195 r
->type
= DST_TYPE_FILE
;
196 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
201 if (r
->dst
[0] == '!')
203 r
->type
= DST_TYPE_NOTE
;
207 if (r
->dst
[0] == '@')
209 node
= strdup(r
->dst
+ 1);
210 if (node
== NULL
) return -1;
213 serv
= strrchr(node
, ':');
214 if (serv
!= NULL
) *serv
++ = '\0';
215 else serv
= "syslog";
217 memset(&hints
, 0, sizeof(hints
));
218 hints
.ai_family
= PF_UNSPEC
;
219 hints
.ai_socktype
= SOCK_DGRAM
;
220 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
224 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
228 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
230 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
231 if (r
->fd
< 0) continue;
233 r
->addr
= (struct sockaddr
*)calloc(1, ai
->ai_addrlen
);
234 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
243 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
247 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
251 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
255 r
->type
= DST_TYPE_SOCK
;
260 if (strcmp(r
->dst
, "*") == 0)
262 r
->type
= DST_TYPE_WALL
;
266 /* Can't deal with dst! */
267 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
274 char **semi
, **comma
;
275 int i
, j
, n
, lasts
, lastc
, pri
;
276 struct config_rule
*out
;
278 if (s
== NULL
) return -1;
279 while ((*s
== ' ') || (*s
== '\t')) s
++;
280 if (*s
== '#') return -1;
282 semi
= _explode(s
, "; \t");
284 if (semi
== NULL
) return -1;
285 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
286 if (out
== NULL
) return -1;
290 for (i
= 0; semi
[i
] != NULL
; i
++)
292 if (semi
[i
][0] == '\0') continue;
297 out
->dst
= strdup(semi
[lasts
]);
298 _syslog_dst_open(out
);
300 for (i
= 0; i
< lasts
; i
++)
302 if (semi
[i
][0] == '\0') continue;
303 comma
= _explode(semi
[i
], ",.");
305 for (j
= 0; comma
[j
] != NULL
; j
++)
307 if (comma
[j
][0] == '\0') continue;
311 for (j
= 0; j
< lastc
; j
++)
313 if (comma
[j
][0] == '\0') continue;
314 pri
= _level_for_name(comma
[lastc
]);
315 if (pri
== -1) continue;
319 out
->facility
= (char **)calloc(1, sizeof(char *));
320 out
->pri
= (int *)calloc(1, sizeof(int));
324 out
->facility
= (char **)realloc(out
->facility
, (out
->count
+ 1) * sizeof(char *));
325 out
->pri
= (int *)realloc(out
->pri
, (out
->count
+ 1) * sizeof(int));
327 out
->facility
[out
->count
] = strdup(comma
[j
]);
328 out
->pri
[out
->count
] = pri
;
337 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
343 _syslog_send(asl_msg_t
*msg
, struct config_rule
*r
, char **out
, char **fwd
)
345 char *so
, *sf
, *vt
, *p
;
346 const char *vtime
, *vhost
, *vident
, *vpid
, *vmsg
, *vlevel
, *vfacility
;
352 if (out
== NULL
) return -1;
353 if (fwd
== NULL
) return -1;
355 if (r
->type
== DST_TYPE_NOTE
)
357 notify_post(r
->dst
+1);
363 /* Build output string if it hasn't been built by a previous rule-match */
366 vtime
= asl_get(msg
, ASL_KEY_TIME
);
369 tick
= asl_parse_time(vtime
);
370 if (tick
!= (time_t)-1)
374 if (vt
== NULL
) return -1;
379 else if (strlen(vtime
) < 24) vt
= strdup(vtime
);
383 if (vt
== NULL
) return -1;
384 memcpy(vt
, vtime
+4, 15);
393 if (vt
== NULL
) return -1;
398 vhost
= asl_get(msg
, ASL_KEY_HOST
);
399 if (vhost
== NULL
) vhost
= "localhost";
401 vident
= asl_get(msg
, ASL_KEY_SENDER
);
402 if ((vident
!= NULL
) && (!strcmp(vident
, "Unknown"))) vident
= NULL
;
404 vpid
= asl_get(msg
, ASL_KEY_PID
);
405 if ((vpid
!= NULL
) && (!strcmp(vpid
, "-1"))) vpid
= NULL
;
407 if ((vpid
!= NULL
) && (vident
== NULL
)) vident
= "Unknown";
409 vmsg
= asl_get(msg
, ASL_KEY_MSG
);
412 if (vt
!= NULL
) n
+= (strlen(vt
) + 1);
413 if (vhost
!= NULL
) n
+= (strlen(vhost
) + 1);
414 if (vident
!= NULL
) n
+= strlen(vident
);
416 if (vpid
!= NULL
) n
+= (strlen(vpid
) + 2);
417 if (vmsg
!= NULL
) n
+= strlen(vmsg
);
419 if (n
== 0) return -1;
423 if (so
== NULL
) return -1;
460 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
463 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
464 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
466 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
467 if (fc
> 0) pf
|= fc
;
470 asprintf(&sf
, "<%d>%s", pf
, *out
);
471 if (sf
== NULL
) return -1;
476 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
477 else outlen
= strlen(*out
);
479 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
482 * Special case for kernel messages.
483 * Don't write kernel messages to /dev/console.
484 * The kernel printf routine already sends them to /dev/console
485 * so writing them here would cause duplicates.
487 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
488 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) return 0;
490 status
= write(r
->fd
, *out
, outlen
);
491 if ((status
< 0) || (status
< outlen
))
493 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
495 /* Try re-opening the file (once) and write again */
497 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
500 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
504 status
= write(r
->fd
, *out
, outlen
);
505 if ((status
< 0) || (status
< outlen
))
507 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
511 else if (r
->type
== DST_TYPE_SOCK
)
513 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
514 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
516 else if (r
->type
== DST_TYPE_WALL
)
518 pw
= popen(_PATH_WALL
, "w");
521 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
533 _syslog_rule_match(asl_msg_t
*msg
, struct config_rule
*r
)
535 uint32_t i
, test
, f
, pri
;
538 if (msg
== NULL
) return 0;
539 if (r
== NULL
) return 0;
540 if (r
->count
== 0) return 0;
544 for (i
= 0; i
< r
->count
; i
++)
546 if (r
->pri
[i
] == -1) continue;
548 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
549 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
552 if (strcmp(r
->facility
[i
], "*") == 0) f
= 1;
555 val
= asl_get(msg
, ASL_KEY_FACILITY
);
556 if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0)) f
= 1;
559 if (f
== 0) continue;
561 /* Turn off matching facility with priority "none" */
568 val
= asl_get(msg
, ASL_KEY_LEVEL
);
569 if (val
== NULL
) continue;
572 if (pri
< 0) continue;
574 if (pri
<= r
->pri
[i
]) test
= 1;
581 bsd_out_sendmsg(asl_msg_t
*msg
, const char *outid
)
583 struct config_rule
*r
;
592 if (msg
== NULL
) return -1;
597 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
599 if (_syslog_rule_match(msg
, r
) == 1) _syslog_send(msg
, r
, &out
, &fwd
);
602 if (out
!= NULL
) free(out
);
603 if (fwd
!= NULL
) free(fwd
);
609 _parse_config_file(const char *confname
)
614 cf
= fopen(confname
, "r");
615 if (cf
== NULL
) return 1;
617 while (NULL
!= (line
= get_line_from_file(cf
)))
631 asldebug("%s: init\n", MY_ID
);
633 TAILQ_INIT(&bsd_out_rule
);
635 query
= asl_new(ASL_TYPE_QUERY
);
636 aslevent_addmatch(query
, MY_ID
);
637 aslevent_addoutput(bsd_out_sendmsg
, MY_ID
);
639 _parse_config_file(_PATH_SYSLOG_CONF
);
653 struct config_rule
*r
, *n
;
657 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
659 n
= r
->entries
.tqe_next
;
661 if (r
->dst
!= NULL
) free(r
->dst
);
662 if (r
->fd
> 0) close(r
->fd
);
663 if (r
->addr
!= NULL
) free(r
->addr
);
664 if (r
->facility
!= NULL
)
666 for (i
= 0; i
< r
->count
; i
++)
668 if (r
->facility
[i
] != NULL
) free(r
->facility
[i
]);
672 if (r
->pri
!= NULL
) free(r
->pri
);
674 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);