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