1/* vi: set sw=4 ts=4: */
2/*
3 * Mini logger implementation for busybox
4 *
5 * Copyright (C) 1999,2000,2001 by Lineo, inc.
6 * 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
24#include <stdio.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <fcntl.h>
28#include <ctype.h>
29#include <string.h>
30#include <stdlib.h>
31
32#include "busybox.h"
33#if !defined BB_SYSLOGD
34
35#define SYSLOG_NAMES
36#include <sys/syslog.h>
37
38#else
39#include <sys/syslog.h>
40#  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
51#endif
52
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.
57 *
58 * Original copyright notice is retained at the end of this file.
59 */
60static int decode(char *name, CODE * codetab)
61{
62	CODE *c;
63
64	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		}
70	}
71
72	return (-1);
73}
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.
79 *
80 * Original copyright notice is retained at the end of this file.
81 */
82static int pencode(char *s)
83{
84	char *save;
85	int lev, fac = LOG_USER;
86
87	for (save = s; *s && *s != '.'; ++s);
88	if (*s) {
89		*s = '\0';
90		fac = decode(save, facilitynames);
91		if (fac < 0)
92			error_msg_and_die("unknown facility name: %s", save);
93		*s++ = '.';
94	} else {
95		s = save;
96	}
97	lev = decode(s, prioritynames);
98	if (lev < 0)
99		error_msg_and_die("unknown priority name: %s", save);
100	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
101}
102
103
104extern int logger_main(int argc, char **argv)
105{
106	int pri = LOG_USER | LOG_NOTICE;
107	int option = 0;
108	int c, i, len, opt;
109	char *message=NULL, buf[1024], name[128];
110
111	/* Fill out the name string early (may be overwritten later) */
112	my_getpwuid(name, geteuid());
113
114	/* Parse any options */
115	while ((opt = getopt(argc, argv, "p:st:")) > 0) {
116		switch (opt) {
117			case 's':
118				option |= LOG_PERROR;
119				break;
120			case 'p':
121				pri = pencode(optarg);
122				break;
123			case 't':
124				strncpy(name, optarg, sizeof(name));
125				break;
126			default:
127				show_usage();
128		}
129	}
130
131	openlog(name, option, (pri | LOG_FACMASK));
132	if (optind == argc) {
133		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);
145	} else {
146		len = 1; /* for the '\0' */
147		message=xcalloc(1, 1);
148		for (i = optind; i < argc; i++) {
149			len += strlen(argv[i]);
150			len += 1;  /* for the space between the args */
151			message = xrealloc(message, len);
152			strcat(message, argv[i]);
153			strcat(message, " ");
154		}
155		message[strlen(message)-1] = '\0';
156		syslog(pri, "%s", message);
157	}
158
159	closelog();
160	return EXIT_SUCCESS;
161}
162
163
164/*-
165 * Copyright (c) 1983, 1993
166 *	The Regents of the University of California.  All rights reserved.
167 *
168 * This is the original license statement for the decode and pencode functions.
169 *
170 * Redistribution and use in source and binary forms, with or without
171 * modification, are permitted provided that the following conditions
172 * are met:
173 * 1. Redistributions of source code must retain the above copyright
174 *    notice, this list of conditions and the following disclaimer.
175 * 2. Redistributions in binary form must reproduce the above copyright
176 *    notice, this list of conditions and the following disclaimer in the
177 *    documentation and/or other materials provided with the distribution.
178 *
179 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
180 *		ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
181 *
182 * 4. Neither the name of the University nor the names of its contributors
183 *    may be used to endorse or promote products derived from this software
184 *    without specific prior written permission.
185 *
186 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
187 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
190 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
192 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
193 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
194 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
195 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
196 * SUCH DAMAGE.
197 */
198
199
200
201