- note cosmetic glitch in tail
[oweals/busybox.git] / sysklogd / logger.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini logger implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "busybox.h"
11
12 #if !defined CONFIG_SYSLOGD
13
14 #define SYSLOG_NAMES
15 #include <sys/syslog.h>
16
17 #else
18 #include <sys/syslog.h>
19 #  ifndef __dietlibc__
20         /* We have to do this since the header file defines static
21          * structures.  Argh.... bad libc, bad, bad...
22          */
23         typedef struct _code {
24                 char *c_name;
25                 int c_val;
26         } CODE;
27         extern CODE prioritynames[];
28         extern CODE facilitynames[];
29 #  endif
30 #endif
31
32 /* Decode a symbolic name to a numeric value
33  * this function is based on code
34  * Copyright (c) 1983, 1993
35  * The Regents of the University of California.  All rights reserved.
36  *
37  * Original copyright notice is retained at the end of this file.
38  */
39 static int decode(char *name, CODE * codetab)
40 {
41         CODE *c;
42
43         if (isdigit(*name))
44                 return atoi(name);
45         for (c = codetab; c->c_name; c++) {
46                 if (!strcasecmp(name, c->c_name)) {
47                         return c->c_val;
48                 }
49         }
50
51         return -1;
52 }
53
54 /* Decode a symbolic name to a numeric value
55  * this function is based on code
56  * Copyright (c) 1983, 1993
57  * The Regents of the University of California.  All rights reserved.
58  *
59  * Original copyright notice is retained at the end of this file.
60  */
61 static int pencode(char *s)
62 {
63         char *save;
64         int lev, fac = LOG_USER;
65
66         for (save = s; *s && *s != '.'; ++s)
67                 ;
68         if (*s) {
69                 *s = '\0';
70                 fac = decode(save, facilitynames);
71                 if (fac < 0)
72                         bb_error_msg_and_die("unknown %s name: %s", "facility", save);
73                 *s++ = '.';
74         } else {
75                 s = save;
76         }
77         lev = decode(s, prioritynames);
78         if (lev < 0)
79                 bb_error_msg_and_die("unknown %s name: %s", "priority", save);
80         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
81 }
82
83
84 int logger_main(int argc, char **argv);
85 int logger_main(int argc, char **argv)
86 {
87         char *str_p, *str_t;
88         int i = 0;
89         char name[80];
90
91         /* Fill out the name string early (may be overwritten later) */
92         bb_getpwuid(name, geteuid(), sizeof(name));
93         str_t = name;
94
95         /* Parse any options */
96         getopt32(argc, argv, "p:st:", &str_p, &str_t);
97
98         if (option_mask32 & 0x2) /* -s */
99                 i |= LOG_PERROR;
100         //if (option_mask32 & 0x4) /* -t */
101         openlog(str_t, i, 0);
102         i = LOG_USER | LOG_NOTICE;
103         if (option_mask32 & 0x1) /* -p */
104                 i = pencode(str_p);
105
106         argc -= optind;
107         argv += optind;
108         if (!argc) {
109                 while (fgets(bb_common_bufsiz1, BUFSIZ, stdin)) {
110                         if (bb_common_bufsiz1[0]
111                          && NOT_LONE_CHAR(bb_common_bufsiz1, '\n')
112                         ) {
113                                 /* Neither "" nor "\n" */
114                                 syslog(i, "%s", bb_common_bufsiz1);
115                         }
116                 }
117         } else {
118                 char *message = NULL;
119                 int len = 1; /* for NUL */
120                 int pos = 0;
121                 do {
122                         len += strlen(*argv) + 1;
123                         message = xrealloc(message, len);
124                         sprintf(message + pos, " %s", *argv),
125                         pos = len;
126                 } while (*++argv);
127                 syslog(i, "%s", message + 1); /* skip leading " " */
128         }
129
130         closelog();
131         return EXIT_SUCCESS;
132 }
133
134
135 /*-
136  * Copyright (c) 1983, 1993
137  *      The Regents of the University of California.  All rights reserved.
138  *
139  * This is the original license statement for the decode and pencode functions.
140  *
141  * Redistribution and use in source and binary forms, with or without
142  * modification, are permitted provided that the following conditions
143  * are met:
144  * 1. Redistributions of source code must retain the above copyright
145  *    notice, this list of conditions and the following disclaimer.
146  * 2. Redistributions in binary form must reproduce the above copyright
147  *    notice, this list of conditions and the following disclaimer in the
148  *    documentation and/or other materials provided with the distribution.
149  *
150  * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
151  *              ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
152  *
153  * 4. Neither the name of the University nor the names of its contributors
154  *    may be used to endorse or promote products derived from this software
155  *    without specific prior written permission.
156  *
157  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
158  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
159  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
160  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
161  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
162  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
163  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
164  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
165  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
166  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
167  * SUCH DAMAGE.
168  */