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