blob: 15a4bfb822025942935098fe0da833b22128ad9d [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen3843e961999-11-25 07:30:46 +00006 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen3843e961999-11-25 07:30:46 +00008 */
9
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Eric Andersen3843e961999-11-25 07:30:46 +000011#include <stdio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000012#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000013#include <sys/types.h>
Eric Andersen3843e961999-11-25 07:30:46 +000014#include <fcntl.h>
Eric Andersen3843e961999-11-25 07:30:46 +000015#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000016#include <string.h>
17#include <stdlib.h>
Eric Andersen3843e961999-11-25 07:30:46 +000018
Eric Andersenbdfd0d72001-10-24 05:00:29 +000019#if !defined CONFIG_SYSLOGD
Eric Andersen3843e961999-11-25 07:30:46 +000020
21#define SYSLOG_NAMES
22#include <sys/syslog.h>
23
24#else
Eric Andersen3843e961999-11-25 07:30:46 +000025#include <sys/syslog.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000026# ifndef __dietlibc__
27 /* We have to do this since the header file defines static
28 * structures. Argh.... bad libc, bad, bad...
29 */
30 typedef struct _code {
31 char *c_name;
32 int c_val;
33 } CODE;
34 extern CODE prioritynames[];
35 extern CODE facilitynames[];
36# endif
Eric Andersen3843e961999-11-25 07:30:46 +000037#endif
38
Eric Andersenc7bda1c2004-03-15 08:29:22 +000039/* Decode a symbolic name to a numeric value
Eric Andersen3843e961999-11-25 07:30:46 +000040 * this function is based on code
41 * Copyright (c) 1983, 1993
42 * The Regents of the University of California. All rights reserved.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043 *
Eric Andersen4e573f42000-11-14 23:29:24 +000044 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000045 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000046static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000047{
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 if (isdigit(*name))
51 return (atoi(name));
52 for (c = codetab; c->c_name; c++) {
53 if (!strcasecmp(name, c->c_name)) {
54 return (c->c_val);
55 }
Eric Andersen3843e961999-11-25 07:30:46 +000056 }
Eric Andersen3843e961999-11-25 07:30:46 +000057
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000059}
60
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061/* Decode a symbolic name to a numeric value
Eric Andersen3843e961999-11-25 07:30:46 +000062 * this function is based on code
63 * Copyright (c) 1983, 1993
64 * The Regents of the University of California. All rights reserved.
Eric Andersen4e573f42000-11-14 23:29:24 +000065 *
66 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000067 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000068static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000069{
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 char *save;
71 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 for (save = s; *s && *s != '.'; ++s);
74 if (*s) {
75 *s = '\0';
76 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000077 if (fac < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_error_msg_and_die("unknown facility name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 *s++ = '.';
80 } else {
81 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000082 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000084 if (lev < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 bb_error_msg_and_die("unknown priority name: %s", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000087}
88
89
Rob Landleydfba7412006-03-06 20:47:33 +000090int logger_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +000091{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000092 unsigned opt;
Denis Vlasenkocb1ba862006-09-22 08:44:58 +000093 char *opt_p, *opt_t;
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 int pri = LOG_USER | LOG_NOTICE;
95 int option = 0;
Denis Vlasenkocb1ba862006-09-22 08:44:58 +000096 int c, i;
Glenn L McGrath907a2402002-11-10 21:33:28 +000097 char buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +000098
Matt Kraai1944f542001-01-02 18:13:58 +000099 /* Fill out the name string early (may be overwritten later) */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000100 bb_getpwuid(name, geteuid(), sizeof(name));
Eric Andersen3843e961999-11-25 07:30:46 +0000101
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 /* Parse any options */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000103 opt = getopt32(argc, argv, "p:st:", &opt_p, &opt_t);
Denis Vlasenkocb1ba862006-09-22 08:44:58 +0000104 if (opt & 0x1) pri = pencode(opt_p); // -p
105 if (opt & 0x2) option |= LOG_PERROR; // -s
106 if (opt & 0x4) safe_strncpy(name, opt_t, sizeof(name)); // -t
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107
Peter Kjellerstedtc089ccd2005-04-06 10:56:57 +0000108 openlog(name, option, 0);
Matt Kraai1944f542001-01-02 18:13:58 +0000109 if (optind == argc) {
Eric Andersen20aab262001-07-19 22:28:02 +0000110 do {
111 /* read from stdin */
112 i = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113 while ((c = getc(stdin)) != EOF && c != '\n' &&
Eric Andersen20aab262001-07-19 22:28:02 +0000114 i < (sizeof(buf)-1)) {
115 buf[i++] = c;
116 }
117 if (i > 0) {
118 buf[i++] = '\0';
119 syslog(pri, "%s", buf);
120 }
121 } while (c != EOF);
Eric Andersenbefda6e1999-11-25 08:06:22 +0000122 } else {
Glenn L McGrath907a2402002-11-10 21:33:28 +0000123 char *message = NULL;
124 int len = argc - optind; /* for the space between the args
125 and '\0' */
126 opt = len;
127 argv += optind;
128 for (i = 0; i < opt; i++) {
129 len += strlen(*argv);
Matt Kraai1944f542001-01-02 18:13:58 +0000130 message = xrealloc(message, len);
Glenn L McGrath907a2402002-11-10 21:33:28 +0000131 if(!i)
Denis Vlasenko9cac5212006-09-09 12:24:19 +0000132 message[0] = '\0';
Peter Kjellerstedtc089ccd2005-04-06 10:56:57 +0000133 else
134 strcat(message, " ");
Glenn L McGrath907a2402002-11-10 21:33:28 +0000135 strcat(message, *argv);
136 argv++;
Eric Andersen9cff4fb2000-12-08 19:35:51 +0000137 }
Eric Andersen20aab262001-07-19 22:28:02 +0000138 syslog(pri, "%s", message);
Eric Andersenbefda6e1999-11-25 08:06:22 +0000139 }
Eric Andersen3843e961999-11-25 07:30:46 +0000140
Eric Andersen20aab262001-07-19 22:28:02 +0000141 closelog();
Matt Kraai31804132000-10-25 16:40:21 +0000142 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000143}
Eric Andersen4e573f42000-11-14 23:29:24 +0000144
145
146/*-
147 * Copyright (c) 1983, 1993
148 * The Regents of the University of California. All rights reserved.
149 *
150 * This is the original license statement for the decode and pencode functions.
151 *
152 * Redistribution and use in source and binary forms, with or without
153 * modification, are permitted provided that the following conditions
154 * are met:
155 * 1. Redistributions of source code must retain the above copyright
156 * notice, this list of conditions and the following disclaimer.
157 * 2. Redistributions in binary form must reproduce the above copyright
158 * notice, this list of conditions and the following disclaimer in the
159 * documentation and/or other materials provided with the distribution.
160 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000161 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
162 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000163 *
164 * 4. Neither the name of the University nor the names of its contributors
165 * may be used to endorse or promote products derived from this software
166 * without specific prior written permission.
167 *
168 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
169 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
172 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
173 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
174 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
175 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
176 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
177 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
178 * SUCH DAMAGE.
179 */
180
181
182