]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/bsd_out.c
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
;
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 *));
91 if (l
== NULL
) return NULL
;
104 for (i
= 0; l
[i
] != NULL
; i
++);
105 len
= i
+ 1; /* count the NULL on the end of the list too! */
107 l
= (char **)reallocf(l
, (len
+ 1) * sizeof(char *));
108 if (l
== NULL
) return NULL
;
110 if ((x
>= (len
- 1)) || (x
== IndexNull
))
112 l
[len
- 1] = strdup(s
);
113 if (l
[len
- 1] == NULL
)
123 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
125 if (l
[x
] == NULL
) return NULL
;
131 _explode(char *s
, char *delim
)
137 if (s
== NULL
) return NULL
;
142 for (i
= 0; ((p
[i
] != '\0') && (strchr(delim
, p
[i
]) == NULL
)); i
++);
145 if (t
== NULL
) return NULL
;
147 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
149 l
= _insertString(t
, l
, IndexNull
);
152 if (p
[i
] == '\0') return l
;
153 if (p
[i
+ 1] == '\0') l
= _insertString("", l
, IndexNull
);
165 if (l
== NULL
) return;
166 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
171 _level_for_name(const char *name
)
173 if (name
== NULL
) return -1;
175 if (!strcasecmp(name
, "emerg")) return ASL_LEVEL_EMERG
;
176 if (!strcasecmp(name
, "panic")) return ASL_LEVEL_EMERG
;
177 if (!strcasecmp(name
, "alert")) return ASL_LEVEL_ALERT
;
178 if (!strcasecmp(name
, "crit")) return ASL_LEVEL_CRIT
;
179 if (!strcasecmp(name
, "err")) return ASL_LEVEL_ERR
;
180 if (!strcasecmp(name
, "error")) return ASL_LEVEL_ERR
;
181 if (!strcasecmp(name
, "warn")) return ASL_LEVEL_WARNING
;
182 if (!strcasecmp(name
, "warning")) return ASL_LEVEL_WARNING
;
183 if (!strcasecmp(name
, "notice")) return ASL_LEVEL_NOTICE
;
184 if (!strcasecmp(name
, "info")) return ASL_LEVEL_INFO
;
185 if (!strcasecmp(name
, "debug")) return ASL_LEVEL_DEBUG
;
186 if (!strcmp(name
, "*")) return ASL_LEVEL_DEBUG
;
189 if (!strcasecmp(name
, "none")) return -2;
195 _syslog_dst_open(struct config_rule
*r
)
199 struct addrinfo hints
, *gai
, *ai
;
201 if (r
== NULL
) return -1;
205 if (r
->dst
[0] == '/')
207 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
210 asldebug("%s: open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
214 r
->type
= DST_TYPE_FILE
;
215 if (!strcmp(r
->dst
, _PATH_CONSOLE
)) r
->type
= DST_TYPE_CONS
;
220 if (r
->dst
[0] == '!')
222 r
->type
= DST_TYPE_NOTE
;
226 if (r
->dst
[0] == '@')
228 node
= strdup(r
->dst
+ 1);
229 if (node
== NULL
) return -1;
232 serv
= strrchr(node
, ':');
233 if (serv
!= NULL
) *serv
++ = '\0';
234 else serv
= "syslog";
236 memset(&hints
, 0, sizeof(hints
));
237 hints
.ai_family
= PF_UNSPEC
;
238 hints
.ai_socktype
= SOCK_DGRAM
;
239 i
= getaddrinfo(node
, serv
, &hints
, &gai
);
243 asldebug("%s: getaddrinfo failed for node %s service %s: (%s)\n", MY_ID
, node
, serv
, gai_strerror(i
));
247 for (ai
= gai
; ai
!= NULL
; ai
= ai
->ai_next
)
249 r
->fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
250 if (r
->fd
< 0) continue;
252 r
->addr
= (struct sockaddr
*)calloc(1, ai
->ai_addrlen
);
253 if (r
->addr
== NULL
) return -1;
255 memcpy(r
->addr
, ai
->ai_addr
, ai
->ai_addrlen
);
264 asldebug("%s: connection failed for %s\n", MY_ID
, (r
->dst
) + 1);
268 if (fcntl(r
->fd
, F_SETFL
, O_NONBLOCK
) < 0)
272 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, r
->fd
, strerror(errno
));
276 r
->type
= DST_TYPE_SOCK
;
281 if (strcmp(r
->dst
, "*") == 0)
283 r
->type
= DST_TYPE_WALL
;
287 /* Can't deal with dst! */
288 asldebug("%s: unsupported / unknown output name: %s\n", MY_ID
, r
->dst
);
295 char **semi
, **comma
;
296 int i
, j
, n
, lasts
, lastc
, pri
;
297 struct config_rule
*out
;
299 if (s
== NULL
) return -1;
300 while ((*s
== ' ') || (*s
== '\t')) s
++;
301 if (*s
== '#') return -1;
303 semi
= _explode(s
, "; \t");
305 if (semi
== NULL
) return -1;
306 out
= (struct config_rule
*)calloc(1, sizeof(struct config_rule
));
307 if (out
== NULL
) return -1;
311 for (i
= 0; semi
[i
] != NULL
; i
++)
313 if (semi
[i
][0] == '\0') continue;
318 out
->dst
= strdup(semi
[lasts
]);
319 if (out
->dst
== NULL
) return -1;
321 _syslog_dst_open(out
);
323 for (i
= 0; i
< lasts
; i
++)
325 if (semi
[i
][0] == '\0') continue;
326 comma
= _explode(semi
[i
], ",.");
328 for (j
= 0; comma
[j
] != NULL
; j
++)
330 if (comma
[j
][0] == '\0') continue;
334 for (j
= 0; j
< lastc
; j
++)
336 if (comma
[j
][0] == '\0') continue;
337 pri
= _level_for_name(comma
[lastc
]);
338 if (pri
== -1) continue;
342 out
->facility
= (char **)calloc(1, sizeof(char *));
343 out
->pri
= (int *)calloc(1, sizeof(int));
347 out
->facility
= (char **)reallocf(out
->facility
, (out
->count
+ 1) * sizeof(char *));
348 out
->pri
= (int *)reallocf(out
->pri
, (out
->count
+ 1) * sizeof(int));
351 if (out
->facility
== NULL
) return -1;
352 if (out
->pri
== NULL
) return -1;
354 out
->facility
[out
->count
] = strdup(comma
[j
]);
355 if (out
->facility
[out
->count
] == NULL
) return -1;
357 out
->pri
[out
->count
] = pri
;
366 TAILQ_INSERT_TAIL(&bsd_out_rule
, out
, entries
);
372 bsd_log_string(const char *msg
)
374 uint32_t i
, len
, outlen
;
378 if (msg
== NULL
) return NULL
;
383 for (i
= 0; i
< len
; i
++)
386 if (isascii(c
) && iscntrl(c
) && (c
!= '\t')) outlen
++;
389 out
= malloc(outlen
);
390 if (out
== NULL
) return NULL
;
394 for (i
= 0; i
< len
; i
++)
398 if (isascii(c
) && iscntrl(c
))
427 _syslog_send(asl_msg_t
*msg
, struct config_rule
*r
, char **out
, char **fwd
)
429 char *so
, *sf
, *vt
, *p
, *outmsg
;
430 const char *vtime
, *vhost
, *vident
, *vpid
, *vmsg
, *vlevel
, *vfacility
;
436 if (out
== NULL
) return -1;
437 if (fwd
== NULL
) return -1;
439 if (r
->type
== DST_TYPE_NOTE
)
441 notify_post(r
->dst
+1);
448 /* Build output string if it hasn't been built by a previous rule-match */
451 vtime
= asl_get(msg
, ASL_KEY_TIME
);
454 tick
= asl_parse_time(vtime
);
455 if (tick
!= (time_t)-1)
459 if (vt
== NULL
) return -1;
465 else if (strlen(vtime
) < 24)
468 if (vt
== NULL
) return -1;
473 if (vt
== NULL
) return -1;
475 memcpy(vt
, vtime
+4, 15);
484 if (vt
== NULL
) return -1;
490 vhost
= asl_get(msg
, ASL_KEY_HOST
);
491 if (vhost
== NULL
) vhost
= "localhost";
493 vident
= asl_get(msg
, ASL_KEY_SENDER
);
494 if ((vident
!= NULL
) && (!strcmp(vident
, "Unknown"))) vident
= NULL
;
496 vpid
= asl_get(msg
, ASL_KEY_PID
);
497 if ((vpid
!= NULL
) && (!strcmp(vpid
, "-1"))) vpid
= NULL
;
499 if ((vpid
!= NULL
) && (vident
== NULL
)) vident
= "Unknown";
501 vmsg
= asl_get(msg
, ASL_KEY_MSG
);
502 if (vmsg
!= NULL
) outmsg
= bsd_log_string(vmsg
);
505 if (vt
!= NULL
) n
+= (strlen(vt
) + 1);
506 if (vhost
!= NULL
) n
+= (strlen(vhost
) + 1);
507 if (vident
!= NULL
) n
+= strlen(vident
);
509 if (vpid
!= NULL
) n
+= (strlen(vpid
) + 2);
511 if (outmsg
!= NULL
) n
+= strlen(outmsg
);
513 if (n
== 0) return -1;
517 if (so
== NULL
) return -1;
555 if ((*fwd
== NULL
) && (r
->type
== DST_TYPE_SOCK
))
558 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
559 if (vlevel
!= NULL
) pf
= atoi(vlevel
);
561 fc
= asl_syslog_faciliy_name_to_num(asl_get(msg
, ASL_KEY_FACILITY
));
562 if (fc
> 0) pf
|= fc
;
565 asprintf(&sf
, "<%d>%s", pf
, *out
);
566 if (sf
== NULL
) return -1;
571 if (r
->type
== DST_TYPE_SOCK
) outlen
= strlen(*fwd
);
572 else outlen
= strlen(*out
);
574 if ((r
->type
== DST_TYPE_FILE
) || (r
->type
== DST_TYPE_CONS
))
577 * Special case for kernel messages.
578 * Don't write kernel messages to /dev/console.
579 * The kernel printf routine already sends them to /dev/console
580 * so writing them here would cause duplicates.
582 vfacility
= asl_get(msg
, ASL_KEY_FACILITY
);
583 if ((vfacility
!= NULL
) && (!strcmp(vfacility
, FACILITY_KERNEL
)) && (r
->type
== DST_TYPE_CONS
)) return 0;
585 status
= write(r
->fd
, *out
, outlen
);
586 if ((status
< 0) || (status
< outlen
))
588 asldebug("%s: error writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
590 /* Try re-opening the file (once) and write again */
592 r
->fd
= open(r
->dst
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
595 asldebug("%s: re-open failed for file: %s (%s)\n", MY_ID
, r
->dst
, strerror(errno
));
599 status
= write(r
->fd
, *out
, outlen
);
600 if ((status
< 0) || (status
< outlen
))
602 asldebug("%s: error re-writing message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
606 else if (r
->type
== DST_TYPE_SOCK
)
608 status
= sendto(r
->fd
, *fwd
, outlen
, 0, r
->addr
, r
->addr
->sa_len
);
609 if (status
< 0) asldebug("%s: error sending message (%s): %s\n", MY_ID
, r
->dst
, strerror(errno
));
611 else if (r
->type
== DST_TYPE_WALL
)
613 pw
= popen(_PATH_WALL
, "w");
616 asldebug("%s: error sending wall message: %s\n", MY_ID
, strerror(errno
));
628 _syslog_rule_match(asl_msg_t
*msg
, struct config_rule
*r
)
630 uint32_t i
, test
, f
, pri
;
633 if (msg
== NULL
) return 0;
634 if (r
== NULL
) return 0;
635 if (r
->count
== 0) return 0;
639 for (i
= 0; i
< r
->count
; i
++)
641 if (r
->pri
[i
] == -1) continue;
643 if ((test
== 1) && (r
->pri
[i
] >= 0)) continue;
644 if ((test
== 0) && (r
->pri
[i
] == -2)) continue;
647 if (strcmp(r
->facility
[i
], "*") == 0) f
= 1;
650 val
= asl_get(msg
, ASL_KEY_FACILITY
);
651 if ((val
!= NULL
) && (strcasecmp(r
->facility
[i
], val
) == 0)) f
= 1;
654 if (f
== 0) continue;
656 /* Turn off matching facility with priority "none" */
663 val
= asl_get(msg
, ASL_KEY_LEVEL
);
664 if (val
== NULL
) continue;
667 if (pri
< 0) continue;
669 if (pri
<= r
->pri
[i
]) test
= 1;
676 bsd_out_sendmsg(asl_msg_t
*msg
, const char *outid
)
678 struct config_rule
*r
;
687 if (msg
== NULL
) return -1;
692 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
694 if (_syslog_rule_match(msg
, r
) == 1) _syslog_send(msg
, r
, &out
, &fwd
);
697 if (out
!= NULL
) free(out
);
698 if (fwd
!= NULL
) free(fwd
);
704 _parse_config_file(const char *confname
)
709 cf
= fopen(confname
, "r");
710 if (cf
== NULL
) return 1;
712 while (NULL
!= (line
= get_line_from_file(cf
)))
726 asldebug("%s: init\n", MY_ID
);
728 TAILQ_INIT(&bsd_out_rule
);
730 query
= asl_new(ASL_TYPE_QUERY
);
731 aslevent_addmatch(query
, MY_ID
);
732 aslevent_addoutput(bsd_out_sendmsg
, MY_ID
);
734 _parse_config_file(_PATH_SYSLOG_CONF
);
748 struct config_rule
*r
, *n
;
752 for (r
= bsd_out_rule
.tqh_first
; r
!= NULL
; r
= n
)
754 n
= r
->entries
.tqe_next
;
756 if (r
->dst
!= NULL
) free(r
->dst
);
757 if (r
->fd
> 0) close(r
->fd
);
758 if (r
->addr
!= NULL
) free(r
->addr
);
759 if (r
->facility
!= NULL
)
761 for (i
= 0; i
< r
->count
; i
++)
763 if (r
->facility
[i
] != NULL
) free(r
->facility
[i
]);
767 if (r
->pri
!= NULL
) free(r
->pri
);
769 TAILQ_REMOVE(&bsd_out_rule
, r
, entries
);