blob: 669a2d6010091551641ce11afa0b311524c0908e [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersen3843e961999-11-25 07:30:46 +000025#include <stdio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000026#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000027#include <sys/types.h>
Eric Andersen3843e961999-11-25 07:30:46 +000028#include <fcntl.h>
Eric Andersen3843e961999-11-25 07:30:46 +000029#include <ctype.h>
Eric Andersen3843e961999-11-25 07:30:46 +000030
31#if !defined BB_SYSLOGD
32
33#define SYSLOG_NAMES
34#include <sys/syslog.h>
35
36#else
37/* We have to do this since the header file defines static
Eric Andersenafdde3e2000-12-09 16:41:42 +000038 * structures. Argh.... bad libc, bad, bad...
Eric Andersen3843e961999-11-25 07:30:46 +000039 */
40#include <sys/syslog.h>
41typedef struct _code {
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 char *c_name;
43 int c_val;
Eric Andersen3843e961999-11-25 07:30:46 +000044} CODE;
45extern CODE prioritynames[];
46extern CODE facilitynames[];
47#endif
48
Eric Andersen3843e961999-11-25 07:30:46 +000049/* 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.
Eric Andersen4e573f42000-11-14 23:29:24 +000053 *
54 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000055 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000056static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000057{
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 if (isdigit(*name))
61 return (atoi(name));
62 for (c = codetab; c->c_name; c++) {
63 if (!strcasecmp(name, c->c_name)) {
64 return (c->c_val);
65 }
Eric Andersen3843e961999-11-25 07:30:46 +000066 }
Eric Andersen3843e961999-11-25 07:30:46 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000069}
70
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.
Eric Andersen4e573f42000-11-14 23:29:24 +000075 *
76 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000077 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000078static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000079{
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 char *save;
81 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000082
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 for (save = s; *s && *s != '.'; ++s);
84 if (*s) {
85 *s = '\0';
86 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000087 if (fac < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +000088 error_msg_and_die("unknown facility name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 *s++ = '.';
90 } else {
91 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000092 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000094 if (lev < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +000095 error_msg_and_die("unknown priority name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000097}
98
99
100extern int logger_main(int argc, char **argv)
101{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 int pri = LOG_USER | LOG_NOTICE;
103 int option = 0;
104 int fromStdinFlag = FALSE;
105 int stopLookingAtMeLikeThat = FALSE;
Eric Andersenb6106152000-06-19 17:25:40 +0000106 char *message=NULL, buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +0000107
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 /* Fill out the name string early (may be overwritten later */
109 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000110
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 /* Parse any options */
112 while (--argc > 0 && **(++argv) == '-') {
113 if (*((*argv) + 1) == '\0') {
114 fromStdinFlag = TRUE;
115 }
116 stopLookingAtMeLikeThat = FALSE;
117 while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) {
118 switch (**argv) {
119 case 's':
120 option |= LOG_PERROR;
121 break;
122 case 'p':
123 if (--argc == 0) {
124 usage(logger_usage);
125 }
126 pri = pencode(*(++argv));
127 stopLookingAtMeLikeThat = TRUE;
128 break;
129 case 't':
130 if (--argc == 0) {
131 usage(logger_usage);
132 }
133 strncpy(name, *(++argv), sizeof(name));
134 stopLookingAtMeLikeThat = TRUE;
135 break;
136 default:
137 usage(logger_usage);
138 }
139 }
Eric Andersen3843e961999-11-25 07:30:46 +0000140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141
142 if (fromStdinFlag == TRUE) {
143 /* read from stdin */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000144 int c;
145 unsigned int i = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146
147 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
148 buf[i++] = c;
149 }
150 message = buf;
Eric Andersenbefda6e1999-11-25 08:06:22 +0000151 } else {
Eric Andersen9cff4fb2000-12-08 19:35:51 +0000152 if (argc >= 1) {
153 int len = 1; /* for the '\0' */
154 for (; *argv != NULL; argv++) {
155 len += strlen(*argv);
156 len += 1; /* for the space between the args */
157 message = xrealloc(message, len);
158 strcat(message, *argv);
159 strcat(message, " ");
160 }
161 message[strlen(message)-1] = '\0';
162 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000163 error_msg_and_die("No message\n");
Eric Andersen9cff4fb2000-12-08 19:35:51 +0000164 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000165 }
Eric Andersen3843e961999-11-25 07:30:46 +0000166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 openlog(name, option, (pri | LOG_FACMASK));
Matt Kraai7b5c16e2000-12-07 16:22:04 +0000168 syslog(pri, "%s", message);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 closelog();
Eric Andersen3843e961999-11-25 07:30:46 +0000170
Matt Kraai31804132000-10-25 16:40:21 +0000171 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000172}
Eric Andersen4e573f42000-11-14 23:29:24 +0000173
174
175/*-
176 * Copyright (c) 1983, 1993
177 * The Regents of the University of California. All rights reserved.
178 *
179 * This is the original license statement for the decode and pencode functions.
180 *
181 * Redistribution and use in source and binary forms, with or without
182 * modification, are permitted provided that the following conditions
183 * are met:
184 * 1. Redistributions of source code must retain the above copyright
185 * notice, this list of conditions and the following disclaimer.
186 * 2. Redistributions in binary form must reproduce the above copyright
187 * notice, this list of conditions and the following disclaimer in the
188 * documentation and/or other materials provided with the distribution.
189 *
190 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
191 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
192 *
193 * 4. Neither the name of the University nor the names of its contributors
194 * may be used to endorse or promote products derived from this software
195 * without specific prior written permission.
196 *
197 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
198 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
201 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
204 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
205 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
206 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
207 * SUCH DAMAGE.
208 */
209
210
211