1 /* vi: set sw=4 ts=4: */
3 * Mini logger implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <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
27 #include <sys/types.h>
31 #if !defined BB_SYSLOGD
34 #include <sys/syslog.h>
37 /* We have to do this since the header file defines static
38 * structures. Argh.... bad libc, bad, bad...
40 #include <sys/syslog.h>
41 typedef struct _code {
45 extern CODE prioritynames[];
46 extern CODE facilitynames[];
49 /* Decode a symbolic name to a numeric value
50 * this function is based on code
51 * Copyright (c) 1983, 1993
52 * The Regents of the University of California. All rights reserved.
54 * Original copyright notice is retained at the end of this file.
56 static int decode(char *name, CODE * codetab)
62 for (c = codetab; c->c_name; c++) {
63 if (!strcasecmp(name, c->c_name)) {
71 /* Decode a symbolic name to a numeric value
72 * this function is based on code
73 * Copyright (c) 1983, 1993
74 * The Regents of the University of California. All rights reserved.
76 * Original copyright notice is retained at the end of this file.
78 static int pencode(char *s)
81 int lev, fac = LOG_USER;
83 for (save = s; *s && *s != '.'; ++s);
86 fac = decode(save, facilitynames);
88 error_msg_and_die("unknown facility name: %s\n", save);
93 lev = decode(s, prioritynames);
95 error_msg_and_die("unknown priority name: %s\n", save);
96 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
100 extern int logger_main(int argc, char **argv)
102 int pri = LOG_USER | LOG_NOTICE;
105 char *message=NULL, buf[1024], name[128];
107 /* Fill out the name string early (may be overwritten later) */
108 my_getpwuid(name, geteuid());
110 /* Parse any options */
111 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
114 option |= LOG_PERROR;
117 pri = pencode(optarg);
120 strncpy(name, optarg, sizeof(name));
127 if (optind == argc) {
128 /* read from stdin */
130 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
135 len = 1; /* for the '\0' */
136 message=xcalloc(1, 1);
137 for (i = optind; i < argc; i++) {
138 len += strlen(argv[i]);
139 len += 1; /* for the space between the args */
140 message = xrealloc(message, len);
141 strcat(message, argv[i]);
142 strcat(message, " ");
144 message[strlen(message)-1] = '\0';
147 openlog(name, option, (pri | LOG_FACMASK));
148 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