blob: 090750173188810b52f8eb347528d1aab237a969 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000011#ifndef CONFIG_SYSLOGD
Eric Andersen3843e961999-11-25 07:30:46 +000012#define SYSLOG_NAMES
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000013#define SYSLOG_NAMES_CONST
14#include <syslog.h>
Eric Andersen3843e961999-11-25 07:30:46 +000015#else
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000016/* brokenness alert. Everybody except dietlibc get's this wrong by neither
17 * providing a typedef nor an extern for facilitynames and prioritynames
18 * in syslog.h.
19 */
20# include <syslog.h>
21# ifndef __dietlibc__
22/* We have to do this since the header file does neither provide a sane type
23 * for this structure nor extern definitions. Argh.... bad libc, bad, bad...
24 */
25typedef struct _code {
26 char *c_name; /* FIXME: this should be const char *const c_name ! */
27 int c_val;
28} CODE;
29# ifdef __UCLIBC__
30extern const CODE prioritynames[];
31extern const CODE facilitynames[];
32# else
33extern CODE prioritynames[];
34extern CODE facilitynames[];
Eric Andersen8d79ce82001-07-22 23:00:15 +000035# endif
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000036# 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 */
Denis Vlasenkod2023282007-11-23 23:39:01 +000046static int decode(char *name, const CODE *codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000047{
Denis Vlasenkod2023282007-11-23 23:39:01 +000048 const CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 if (isdigit(*name))
Denis Vlasenko13858992006-10-08 12:49:22 +000051 return atoi(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 for (c = codetab; c->c_name; c++) {
53 if (!strcasecmp(name, c->c_name)) {
Denis Vlasenko13858992006-10-08 12:49:22 +000054 return c->c_val;
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 }
Eric Andersen3843e961999-11-25 07:30:46 +000056 }
Eric Andersen3843e961999-11-25 07:30:46 +000057
Denis Vlasenko13858992006-10-08 12:49:22 +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
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000073 for (save = s; *s && *s != '.'; ++s)
74 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 if (*s) {
76 *s = '\0';
77 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000078 if (fac < 0)
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000079 bb_error_msg_and_die("unknown %s name: %s", "facility", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 *s++ = '.';
81 } else {
82 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000083 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000085 if (lev < 0)
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000086 bb_error_msg_and_die("unknown %s name: %s", "priority", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000088}
89
90
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000091int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000092int logger_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +000093{
Denis Vlasenko4df81352007-01-12 21:01:05 +000094 char *str_p, *str_t;
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000095 int i = 0;
Denis Vlasenko4df81352007-01-12 21:01:05 +000096 char name[80];
Eric Andersen3843e961999-11-25 07:30:46 +000097
Matt Kraai1944f542001-01-02 18:13:58 +000098 /* Fill out the name string early (may be overwritten later) */
Denis Vlasenko3734b942007-07-27 11:20:10 +000099 bb_getpwuid(name, sizeof(name), geteuid());
Denis Vlasenko4df81352007-01-12 21:01:05 +0000100 str_t = name;
Eric Andersen3843e961999-11-25 07:30:46 +0000101
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 /* Parse any options */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000103 getopt32(argv, "p:st:", &str_p, &str_t);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000104
105 if (option_mask32 & 0x2) /* -s */
106 i |= LOG_PERROR;
Denis Vlasenko4df81352007-01-12 21:01:05 +0000107 //if (option_mask32 & 0x4) /* -t */
108 openlog(str_t, i, 0);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000109 i = LOG_USER | LOG_NOTICE;
110 if (option_mask32 & 0x1) /* -p */
Denis Vlasenko4df81352007-01-12 21:01:05 +0000111 i = pencode(str_p);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000112
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000113 argc -= optind;
114 argv += optind;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000115 if (!argc) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000116#define strbuf bb_common_bufsiz1
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000117 while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000118 if (strbuf[0]
119 && NOT_LONE_CHAR(strbuf, '\n')
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000120 ) {
121 /* Neither "" nor "\n" */
Denis Vlasenko74324c82007-06-04 10:16:52 +0000122 syslog(i, "%s", strbuf);
Eric Andersen20aab262001-07-19 22:28:02 +0000123 }
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000124 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000125 } else {
Glenn L McGrath907a2402002-11-10 21:33:28 +0000126 char *message = NULL;
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000127 int len = 0;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000128 int pos = 0;
129 do {
130 len += strlen(*argv) + 1;
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000131 message = xrealloc(message, len + 1);
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000132 sprintf(message + pos, " %s", *argv),
133 pos = len;
134 } while (*++argv);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000135 syslog(i, "%s", message + 1); /* skip leading " " */
Eric Andersenbefda6e1999-11-25 08:06:22 +0000136 }
Eric Andersen3843e961999-11-25 07:30:46 +0000137
Eric Andersen20aab262001-07-19 22:28:02 +0000138 closelog();
Matt Kraai31804132000-10-25 16:40:21 +0000139 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000140}
Eric Andersen4e573f42000-11-14 23:29:24 +0000141
142
143/*-
144 * Copyright (c) 1983, 1993
145 * The Regents of the University of California. All rights reserved.
146 *
147 * This is the original license statement for the decode and pencode functions.
148 *
149 * Redistribution and use in source and binary forms, with or without
150 * modification, are permitted provided that the following conditions
151 * are met:
152 * 1. Redistributions of source code must retain the above copyright
153 * notice, this list of conditions and the following disclaimer.
154 * 2. Redistributions in binary form must reproduce the above copyright
155 * notice, this list of conditions and the following disclaimer in the
156 * documentation and/or other materials provided with the distribution.
157 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
159 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000160 *
161 * 4. Neither the name of the University nor the names of its contributors
162 * may be used to endorse or promote products derived from this software
163 * without specific prior written permission.
164 *
165 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
166 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
168 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
171 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
172 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
173 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
174 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
175 * SUCH DAMAGE.
176 */