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