1 /* vi: set sw=4 ts=4: */
3 * Mini logger implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
33 #if !defined CONFIG_SYSLOGD
36 #include <sys/syslog.h>
39 #include <sys/syslog.h>
41 /* We have to do this since the header file defines static
42 * structures. Argh.... bad libc, bad, bad...
44 typedef struct _code {
48 extern CODE prioritynames[];
49 extern CODE facilitynames[];
53 /* Decode a symbolic name to a numeric value
54 * this function is based on code
55 * Copyright (c) 1983, 1993
56 * The Regents of the University of California. All rights reserved.
58 * Original copyright notice is retained at the end of this file.
60 static int decode(char *name, CODE * codetab)
66 for (c = codetab; c->c_name; c++) {
67 if (!strcasecmp(name, c->c_name)) {
75 /* Decode a symbolic name to a numeric value
76 * this function is based on code
77 * Copyright (c) 1983, 1993
78 * The Regents of the University of California. All rights reserved.
80 * Original copyright notice is retained at the end of this file.
82 static int pencode(char *s)
85 int lev, fac = LOG_USER;
87 for (save = s; *s && *s != '.'; ++s);
90 fac = decode(save, facilitynames);
92 error_msg_and_die("unknown facility name: %s", save);
97 lev = decode(s, prioritynames);
99 error_msg_and_die("unknown priority name: %s", save);
100 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
104 extern int logger_main(int argc, char **argv)
106 int pri = LOG_USER | LOG_NOTICE;
109 char buf[1024], name[128];
111 /* Fill out the name string early (may be overwritten later) */
112 my_getpwuid(name, geteuid());
114 /* Parse any options */
115 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
118 option |= LOG_PERROR;
121 pri = pencode(optarg);
124 safe_strncpy(name, optarg, sizeof(name));
131 openlog(name, option, (pri | LOG_FACMASK));
132 if (optind == argc) {
134 /* read from stdin */
136 while ((c = getc(stdin)) != EOF && c != '\n' &&
137 i < (sizeof(buf)-1)) {
142 syslog(pri, "%s", buf);
146 char *message = NULL;
147 int len = argc - optind; /* for the space between the args
151 for (i = 0; i < opt; i++) {
152 len += strlen(*argv);
153 message = xrealloc(message, len);
157 strcat(message, " ");
158 strcat(message, *argv);
161 syslog(pri, "%s", message);
170 * Copyright (c) 1983, 1993
171 * The Regents of the University of California. All rights reserved.
173 * This is the original license statement for the decode and pencode functions.
175 * Redistribution and use in source and binary forms, with or without
176 * modification, are permitted provided that the following conditions
178 * 1. Redistributions of source code must retain the above copyright
179 * notice, this list of conditions and the following disclaimer.
180 * 2. Redistributions in binary form must reproduce the above copyright
181 * notice, this list of conditions and the following disclaimer in the
182 * documentation and/or other materials provided with the distribution.
184 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
185 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
187 * 4. Neither the name of the University nor the names of its contributors
188 * may be used to endorse or promote products derived from this software
189 * without specific prior written permission.
191 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
195 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
197 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
198 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
200 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF