blob: 1b55bf589ca276ed32aed47aa97a4fe0d47849c6 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini logger implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersen3843e961999-11-25 07:30:46 +00007 *
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.
12 *
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.
17 *
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
21 *
22 */
23
Eric Andersen3843e961999-11-25 07:30:46 +000024#include <stdio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000025#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000026#include <sys/types.h>
Eric Andersen3843e961999-11-25 07:30:46 +000027#include <fcntl.h>
Eric Andersen3843e961999-11-25 07:30:46 +000028#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
30#include <stdlib.h>
Eric Andersen3843e961999-11-25 07:30:46 +000031
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersenbdfd0d72001-10-24 05:00:29 +000033#if !defined CONFIG_SYSLOGD
Eric Andersen3843e961999-11-25 07:30:46 +000034
35#define SYSLOG_NAMES
36#include <sys/syslog.h>
37
38#else
Eric Andersen3843e961999-11-25 07:30:46 +000039#include <sys/syslog.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000040# ifndef __dietlibc__
41 /* We have to do this since the header file defines static
42 * structures. Argh.... bad libc, bad, bad...
43 */
44 typedef struct _code {
45 char *c_name;
46 int c_val;
47 } CODE;
48 extern CODE prioritynames[];
49 extern CODE facilitynames[];
50# endif
Eric Andersen3843e961999-11-25 07:30:46 +000051#endif
52
Eric Andersen3843e961999-11-25 07:30:46 +000053/* 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.
Eric Andersen4e573f42000-11-14 23:29:24 +000057 *
58 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000059 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000060static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000061{
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000063
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 if (isdigit(*name))
65 return (atoi(name));
66 for (c = codetab; c->c_name; c++) {
67 if (!strcasecmp(name, c->c_name)) {
68 return (c->c_val);
69 }
Eric Andersen3843e961999-11-25 07:30:46 +000070 }
Eric Andersen3843e961999-11-25 07:30:46 +000071
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000073}
74
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.
Eric Andersen4e573f42000-11-14 23:29:24 +000079 *
80 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000081 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000082static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000083{
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 char *save;
85 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000086
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 for (save = s; *s && *s != '.'; ++s);
88 if (*s) {
89 *s = '\0';
90 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000091 if (fac < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_error_msg_and_die("unknown facility name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 *s++ = '.';
94 } else {
95 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000096 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000098 if (lev < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_error_msg_and_die("unknown priority name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +0000101}
102
103
104extern int logger_main(int argc, char **argv)
105{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 int pri = LOG_USER | LOG_NOTICE;
107 int option = 0;
Glenn L McGrath907a2402002-11-10 21:33:28 +0000108 int c, i, opt;
109 char buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +0000110
Matt Kraai1944f542001-01-02 18:13:58 +0000111 /* Fill out the name string early (may be overwritten later) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000113
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 /* Parse any options */
Matt Kraai1944f542001-01-02 18:13:58 +0000115 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
116 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 case 's':
118 option |= LOG_PERROR;
119 break;
120 case 'p':
Matt Kraai1944f542001-01-02 18:13:58 +0000121 pri = pencode(optarg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 break;
123 case 't':
Eric Andersen02e6ba92002-09-30 20:39:56 +0000124 safe_strncpy(name, optarg, sizeof(name));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 break;
126 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 }
Eric Andersen3843e961999-11-25 07:30:46 +0000129 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130
Eric Andersen20aab262001-07-19 22:28:02 +0000131 openlog(name, option, (pri | LOG_FACMASK));
Matt Kraai1944f542001-01-02 18:13:58 +0000132 if (optind == argc) {
Eric Andersen20aab262001-07-19 22:28:02 +0000133 do {
134 /* read from stdin */
135 i = 0;
136 while ((c = getc(stdin)) != EOF && c != '\n' &&
137 i < (sizeof(buf)-1)) {
138 buf[i++] = c;
139 }
140 if (i > 0) {
141 buf[i++] = '\0';
142 syslog(pri, "%s", buf);
143 }
144 } while (c != EOF);
Eric Andersenbefda6e1999-11-25 08:06:22 +0000145 } else {
Glenn L McGrath907a2402002-11-10 21:33:28 +0000146 char *message = NULL;
147 int len = argc - optind; /* for the space between the args
148 and '\0' */
149 opt = len;
150 argv += optind;
151 for (i = 0; i < opt; i++) {
152 len += strlen(*argv);
Matt Kraai1944f542001-01-02 18:13:58 +0000153 message = xrealloc(message, len);
Glenn L McGrath907a2402002-11-10 21:33:28 +0000154 if(!i)
155 message[0] = 0;
156 else
Matt Kraai1944f542001-01-02 18:13:58 +0000157 strcat(message, " ");
Glenn L McGrath907a2402002-11-10 21:33:28 +0000158 strcat(message, *argv);
159 argv++;
Eric Andersen9cff4fb2000-12-08 19:35:51 +0000160 }
Eric Andersen20aab262001-07-19 22:28:02 +0000161 syslog(pri, "%s", message);
Eric Andersenbefda6e1999-11-25 08:06:22 +0000162 }
Eric Andersen3843e961999-11-25 07:30:46 +0000163
Eric Andersen20aab262001-07-19 22:28:02 +0000164 closelog();
Matt Kraai31804132000-10-25 16:40:21 +0000165 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000166}
Eric Andersen4e573f42000-11-14 23:29:24 +0000167
168
169/*-
170 * Copyright (c) 1983, 1993
171 * The Regents of the University of California. All rights reserved.
172 *
173 * This is the original license statement for the decode and pencode functions.
174 *
175 * Redistribution and use in source and binary forms, with or without
176 * modification, are permitted provided that the following conditions
177 * are met:
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.
183 *
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>
186 *
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.
190 *
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
201 * SUCH DAMAGE.
202 */
203
204
205