blob: 1218d8d2e5dd491b8b7028ed47460325d698fcbb [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 Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersen3843e961999-11-25 07:30:46 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
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 Andersen3843e961999-11-25 07:30:46 +000033#if !defined BB_SYSLOGD
34
35#define SYSLOG_NAMES
36#include <sys/syslog.h>
37
38#else
39/* We have to do this since the header file defines static
Eric Andersenafdde3e2000-12-09 16:41:42 +000040 * structures. Argh.... bad libc, bad, bad...
Eric Andersen3843e961999-11-25 07:30:46 +000041 */
42#include <sys/syslog.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000043
Eric Andersen3843e961999-11-25 07:30:46 +000044typedef struct _code {
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 char *c_name;
46 int c_val;
Eric Andersen3843e961999-11-25 07:30:46 +000047} CODE;
48extern CODE prioritynames[];
49extern CODE facilitynames[];
50#endif
51
Eric Andersen3843e961999-11-25 07:30:46 +000052/* 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.
Eric Andersen4e573f42000-11-14 23:29:24 +000056 *
57 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000058 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000059static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000060{
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 if (isdigit(*name))
64 return (atoi(name));
65 for (c = codetab; c->c_name; c++) {
66 if (!strcasecmp(name, c->c_name)) {
67 return (c->c_val);
68 }
Eric Andersen3843e961999-11-25 07:30:46 +000069 }
Eric Andersen3843e961999-11-25 07:30:46 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000072}
73
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.
Eric Andersen4e573f42000-11-14 23:29:24 +000078 *
79 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000080 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000081static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000082{
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 char *save;
84 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 for (save = s; *s && *s != '.'; ++s);
87 if (*s) {
88 *s = '\0';
89 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000090 if (fac < 0)
Matt Kraaidd19c692001-01-31 19:00:21 +000091 error_msg_and_die("unknown facility name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 *s++ = '.';
93 } else {
94 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000095 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000097 if (lev < 0)
Matt Kraaidd19c692001-01-31 19:00:21 +000098 error_msg_and_die("unknown priority name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +0000100}
101
102
103extern int logger_main(int argc, char **argv)
104{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 int pri = LOG_USER | LOG_NOTICE;
106 int option = 0;
Matt Kraai1944f542001-01-02 18:13:58 +0000107 int c, i, len, opt;
Eric Andersenb6106152000-06-19 17:25:40 +0000108 char *message=NULL, buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +0000109
Matt Kraai1944f542001-01-02 18:13:58 +0000110 /* Fill out the name string early (may be overwritten later) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000112
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 /* Parse any options */
Matt Kraai1944f542001-01-02 18:13:58 +0000114 while ((opt = getopt(argc, argv, "p:st:")) > 0) {
115 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 case 's':
117 option |= LOG_PERROR;
118 break;
119 case 'p':
Matt Kraai1944f542001-01-02 18:13:58 +0000120 pri = pencode(optarg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 break;
122 case 't':
Matt Kraai1944f542001-01-02 18:13:58 +0000123 strncpy(name, optarg, sizeof(name));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 break;
125 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000126 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 }
Eric Andersen3843e961999-11-25 07:30:46 +0000128 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129
Matt Kraai1944f542001-01-02 18:13:58 +0000130 if (optind == argc) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 /* read from stdin */
Matt Kraai1944f542001-01-02 18:13:58 +0000132 i = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
134 buf[i++] = c;
135 }
Eric Andersen5e8b3ea2001-01-03 00:06:46 +0000136 buf[i++] = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 message = buf;
Eric Andersenbefda6e1999-11-25 08:06:22 +0000138 } else {
Matt Kraai1944f542001-01-02 18:13:58 +0000139 len = 1; /* for the '\0' */
140 message=xcalloc(1, 1);
141 for (i = optind; i < argc; i++) {
142 len += strlen(argv[i]);
143 len += 1; /* for the space between the args */
144 message = xrealloc(message, len);
145 strcat(message, argv[i]);
146 strcat(message, " ");
Eric Andersen9cff4fb2000-12-08 19:35:51 +0000147 }
Matt Kraai1944f542001-01-02 18:13:58 +0000148 message[strlen(message)-1] = '\0';
Eric Andersenbefda6e1999-11-25 08:06:22 +0000149 }
Eric Andersen3843e961999-11-25 07:30:46 +0000150
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 openlog(name, option, (pri | LOG_FACMASK));
Matt Kraai7b5c16e2000-12-07 16:22:04 +0000152 syslog(pri, "%s", message);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 closelog();
Matt Kraai31804132000-10-25 16:40:21 +0000154 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000155}
Eric Andersen4e573f42000-11-14 23:29:24 +0000156
157
158/*-
159 * Copyright (c) 1983, 1993
160 * The Regents of the University of California. All rights reserved.
161 *
162 * This is the original license statement for the decode and pencode functions.
163 *
164 * Redistribution and use in source and binary forms, with or without
165 * modification, are permitted provided that the following conditions
166 * are met:
167 * 1. Redistributions of source code must retain the above copyright
168 * notice, this list of conditions and the following disclaimer.
169 * 2. Redistributions in binary form must reproduce the above copyright
170 * notice, this list of conditions and the following disclaimer in the
171 * documentation and/or other materials provided with the distribution.
172 *
173 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
174 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
175 *
176 * 4. Neither the name of the University nor the names of its contributors
177 * may be used to endorse or promote products derived from this software
178 * without specific prior written permission.
179 *
180 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
184 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
185 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
186 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
187 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
188 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
189 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
190 * SUCH DAMAGE.
191 */
192
193
194