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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
32 #if !defined CONFIG_SYSLOGD
35 #include <sys/syslog.h>
38 #include <sys/syslog.h>
40 /* We have to do this since the header file defines static
41 * structures. Argh.... bad libc, bad, bad...
43 typedef struct _code {
47 extern CODE prioritynames[];
48 extern CODE facilitynames[];
52 /* Decode a symbolic name to a numeric value
53 * this function is based on code
54 * Copyright (c) 1983, 1993
55 * The Regents of the University of California. All rights reserved.
57 * Original copyright notice is retained at the end of this file.
59 static int decode(char *name, CODE * codetab)
65 for (c = codetab; c->c_name; c++) {
66 if (!strcasecmp(name, c->c_name)) {
74 /* Decode a symbolic name to a numeric value
75 * this function is based on code
76 * Copyright (c) 1983, 1993
77 * The Regents of the University of California. All rights reserved.
79 * Original copyright notice is retained at the end of this file.
81 static int pencode(char *s)
84 int lev, fac = LOG_USER;
86 for (save = s; *s && *s != '.'; ++s);
89 fac = decode(save, facilitynames);
91 bb_error_msg_and_die("unknown facility name: %s", save);
96 lev = decode(s, prioritynames);
98 bb_error_msg_and_die("unknown priority name: %s", save);
99 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
103 extern int logger_main(int argc, char **argv)
105 int pri = LOG_USER | LOG_NOTICE;
108 char buf[1024], name[128];
110 /* Fill out the name string early (may be overwritten later) */
111 my_getpwuid(name, geteuid());
113 /* Parse any options */
114 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
117 option |= LOG_PERROR;
120 pri = pencode(optarg);
123 safe_strncpy(name, optarg, sizeof(name));
130 openlog(name, option, (pri | LOG_FACMASK));
131 if (optind == argc) {
133 /* read from stdin */
135 while ((c = getc(stdin)) != EOF && c != '\n' &&
136 i < (sizeof(buf)-1)) {
141 syslog(pri, "%s", buf);
145 char *message = NULL;
146 int len = argc - optind; /* for the space between the args
150 for (i = 0; i < opt; i++) {
151 len += strlen(*argv);
152 message = xrealloc(message, len);
156 strcat(message, " ");
157 strcat(message, *argv);
160 syslog(pri, "%s", message);
169 * Copyright (c) 1983, 1993
170 * The Regents of the University of California. All rights reserved.
172 * This is the original license statement for the decode and pencode functions.
174 * Redistribution and use in source and binary forms, with or without
175 * modification, are permitted provided that the following conditions
177 * 1. Redistributions of source code must retain the above copyright
178 * notice, this list of conditions and the following disclaimer.
179 * 2. Redistributions in binary form must reproduce the above copyright
180 * notice, this list of conditions and the following disclaimer in the
181 * documentation and/or other materials provided with the distribution.
183 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
184 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
186 * 4. Neither the name of the University nor the names of its contributors
187 * may be used to endorse or promote products derived from this software
188 * without specific prior written permission.
190 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
194 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
196 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
197 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
198 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF