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.
13 #include <sys/types.h>
19 #if !defined CONFIG_SYSLOGD
22 #include <sys/syslog.h>
25 #include <sys/syslog.h>
27 /* We have to do this since the header file defines static
28 * structures. Argh.... bad libc, bad, bad...
30 typedef struct _code {
34 extern CODE prioritynames[];
35 extern CODE facilitynames[];
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.
44 * Original copyright notice is retained at the end of this file.
46 static int decode(char *name, CODE * codetab)
52 for (c = codetab; c->c_name; c++) {
53 if (!strcasecmp(name, c->c_name)) {
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.
66 * Original copyright notice is retained at the end of this file.
68 static int pencode(char *s)
71 int lev, fac = LOG_USER;
73 for (save = s; *s && *s != '.'; ++s);
76 fac = decode(save, facilitynames);
78 bb_error_msg_and_die("unknown facility name: %s", save);
83 lev = decode(s, prioritynames);
85 bb_error_msg_and_die("unknown priority name: %s", save);
86 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
90 int logger_main(int argc, char **argv)
92 int pri = LOG_USER | LOG_NOTICE;
95 char buf[1024], name[128];
97 /* Fill out the name string early (may be overwritten later) */
98 bb_getpwuid(name, geteuid(), sizeof(name));
100 /* Parse any options */
101 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
104 option |= LOG_PERROR;
107 pri = pencode(optarg);
110 safe_strncpy(name, optarg, sizeof(name));
117 openlog(name, option, 0);
118 if (optind == argc) {
120 /* read from stdin */
122 while ((c = getc(stdin)) != EOF && c != '\n' &&
123 i < (sizeof(buf)-1)) {
128 syslog(pri, "%s", buf);
132 char *message = NULL;
133 int len = argc - optind; /* for the space between the args
137 for (i = 0; i < opt; i++) {
138 len += strlen(*argv);
139 message = xrealloc(message, len);
143 strcat(message, " ");
144 strcat(message, *argv);
147 syslog(pri, "%s", message);
156 * Copyright (c) 1983, 1993
157 * The Regents of the University of California. All rights reserved.
159 * This is the original license statement for the decode and pencode functions.
161 * Redistribution and use in source and binary forms, with or without
162 * modification, are permitted provided that the following conditions
164 * 1. Redistributions of source code must retain the above copyright
165 * notice, this list of conditions and the following disclaimer.
166 * 2. Redistributions in binary form must reproduce the above copyright
167 * notice, this list of conditions and the following disclaimer in the
168 * documentation and/or other materials provided with the distribution.
170 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
171 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
173 * 4. Neither the name of the University nor the names of its contributors
174 * may be used to endorse or promote products derived from this software
175 * without specific prior written permission.
177 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
181 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
182 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
183 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
184 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
185 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
186 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF