1 /* vi: set sw=4 ts=4: */
3 * Mini logger implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #if !defined CONFIG_SYSLOGD
15 #include <sys/syslog.h>
18 #include <sys/syslog.h>
20 /* We have to do this since the header file defines static
21 * structures. Argh.... bad libc, bad, bad...
23 typedef struct _code {
27 extern CODE prioritynames[];
28 extern CODE facilitynames[];
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.
37 * Original copyright notice is retained at the end of this file.
39 static int decode(char *name, CODE * codetab)
45 for (c = codetab; c->c_name; c++) {
46 if (!strcasecmp(name, c->c_name)) {
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.
59 * Original copyright notice is retained at the end of this file.
61 static int pencode(char *s)
64 int lev, fac = LOG_USER;
66 for (save = s; *s && *s != '.'; ++s)
70 fac = decode(save, facilitynames);
72 bb_error_msg_and_die("unknown %s name: %s", "facility", save);
77 lev = decode(s, prioritynames);
79 bb_error_msg_and_die("unknown %s name: %s", "priority", save);
80 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
84 int logger_main(int argc, char **argv)
88 RESERVE_CONFIG_BUFFER(name, 80);
90 /* Fill out the name string early (may be overwritten later) */
91 bb_getpwuid(name, geteuid(), sizeof(name));
93 /* Parse any options */
94 getopt32(argc, argv, "p:st:", &opt_p, &opt_t);
96 if (option_mask32 & 0x2) /* -s */
98 if (option_mask32 & 0x4) /* -t */
99 safe_strncpy(name, opt_t, sizeof(name));
101 if (ENABLE_FEATURE_CLEAN_UP)
102 RELEASE_CONFIG_BUFFER(name);
103 i = LOG_USER | LOG_NOTICE;
104 if (option_mask32 & 0x1) /* -p */
110 while (fgets(bb_common_bufsiz1, BUFSIZ, stdin)) {
111 if (bb_common_bufsiz1[0]
112 && NOT_LONE_CHAR(bb_common_bufsiz1, '\n')
114 /* Neither "" nor "\n" */
115 syslog(i, "%s", bb_common_bufsiz1);
119 char *message = NULL;
120 int len = 1; /* for NUL */
123 len += strlen(*argv) + 1;
124 message = xrealloc(message, len);
125 sprintf(message + pos, " %s", *argv),
128 syslog(i, "%s", message + 1); /* skip leading " " */
137 * Copyright (c) 1983, 1993
138 * The Regents of the University of California. All rights reserved.
140 * This is the original license statement for the decode and pencode functions.
142 * Redistribution and use in source and binary forms, with or without
143 * modification, are permitted provided that the following conditions
145 * 1. Redistributions of source code must retain the above copyright
146 * notice, this list of conditions and the following disclaimer.
147 * 2. Redistributions in binary form must reproduce the above copyright
148 * notice, this list of conditions and the following disclaimer in the
149 * documentation and/or other materials provided with the distribution.
151 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
152 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
154 * 4. Neither the name of the University nor the names of its contributors
155 * may be used to endorse or promote products derived from this software
156 * without specific prior written permission.
158 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
159 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
161 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
162 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
163 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
164 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
165 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
166 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
167 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF