blob: 5a7027731481c90598dabdd36ba257eb81912d18 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen3843e961999-11-25 07:30:46 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define logger_trivial_usage
11//usage: "[OPTIONS] [MESSAGE]"
12//usage:#define logger_full_usage "\n\n"
13//usage: "Write MESSAGE (or stdin) to syslog\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage: "\n -s Log to stderr as well as the system log"
15//usage: "\n -t TAG Log using the specified tag (defaults to user name)"
16//usage: "\n -p PRIO Priority (numeric or facility.level pair)"
17//usage:
18//usage:#define logger_example_usage
19//usage: "$ logger \"hello\"\n"
20
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000021/*
22 * Done in syslogd_and_logger.c:
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Eric Andersen3843e961999-11-25 07:30:46 +000024#define SYSLOG_NAMES
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000025#define SYSLOG_NAMES_CONST
26#include <syslog.h>
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000027*/
Eric Andersen3843e961999-11-25 07:30:46 +000028
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029/* Decode a symbolic name to a numeric value
Eric Andersen3843e961999-11-25 07:30:46 +000030 * this function is based on code
31 * Copyright (c) 1983, 1993
32 * The Regents of the University of California. All rights reserved.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033 *
Eric Andersen4e573f42000-11-14 23:29:24 +000034 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000035 */
Denis Vlasenkod2023282007-11-23 23:39:01 +000036static int decode(char *name, const CODE *codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000037{
Denis Vlasenkod2023282007-11-23 23:39:01 +000038 const CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000039
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 if (isdigit(*name))
Denis Vlasenko13858992006-10-08 12:49:22 +000041 return atoi(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 for (c = codetab; c->c_name; c++) {
43 if (!strcasecmp(name, c->c_name)) {
Denis Vlasenko13858992006-10-08 12:49:22 +000044 return c->c_val;
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 }
Eric Andersen3843e961999-11-25 07:30:46 +000046 }
Eric Andersen3843e961999-11-25 07:30:46 +000047
Denis Vlasenko13858992006-10-08 12:49:22 +000048 return -1;
Eric Andersen3843e961999-11-25 07:30:46 +000049}
50
Eric Andersenc7bda1c2004-03-15 08:29:22 +000051/* Decode a symbolic name to a numeric value
Eric Andersen3843e961999-11-25 07:30:46 +000052 * this function is based on code
53 * Copyright (c) 1983, 1993
54 * The Regents of the University of California. All rights reserved.
Eric Andersen4e573f42000-11-14 23:29:24 +000055 *
56 * Original copyright notice is retained at the end of this file.
Eric Andersen3843e961999-11-25 07:30:46 +000057 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000058static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000059{
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 char *save;
61 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000062
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000063 for (save = s; *s && *s != '.'; ++s)
64 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 if (*s) {
66 *s = '\0';
67 fac = decode(save, facilitynames);
Matt Kraai31804132000-10-25 16:40:21 +000068 if (fac < 0)
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000069 bb_error_msg_and_die("unknown %s name: %s", "facility", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 *s++ = '.';
71 } else {
72 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000073 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 lev = decode(s, prioritynames);
Matt Kraai31804132000-10-25 16:40:21 +000075 if (lev < 0)
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000076 bb_error_msg_and_die("unknown %s name: %s", "priority", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000078}
79
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000080#define strbuf bb_common_bufsiz1
Eric Andersen3843e961999-11-25 07:30:46 +000081
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000082int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010083int logger_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +000084{
Denis Vlasenko4df81352007-01-12 21:01:05 +000085 char *str_p, *str_t;
Denis Vlasenko0c68a872008-12-02 22:56:59 +000086 int opt;
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000087 int i = 0;
Eric Andersen3843e961999-11-25 07:30:46 +000088
Matt Kraai1944f542001-01-02 18:13:58 +000089 /* Fill out the name string early (may be overwritten later) */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000090 str_t = uid2uname_utoa(geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +000091
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 /* Parse any options */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000093 opt = getopt32(argv, "p:st:", &str_p, &str_t);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000094
Denis Vlasenko0c68a872008-12-02 22:56:59 +000095 if (opt & 0x2) /* -s */
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000096 i |= LOG_PERROR;
Denis Vlasenko0c68a872008-12-02 22:56:59 +000097 //if (opt & 0x4) /* -t */
Denis Vlasenko4df81352007-01-12 21:01:05 +000098 openlog(str_t, i, 0);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +000099 i = LOG_USER | LOG_NOTICE;
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000100 if (opt & 0x1) /* -p */
Denis Vlasenko4df81352007-01-12 21:01:05 +0000101 i = pencode(str_p);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000102
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000103 argv += optind;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100104 if (!argv[0]) {
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000105 while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000106 if (strbuf[0]
107 && NOT_LONE_CHAR(strbuf, '\n')
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000108 ) {
109 /* Neither "" nor "\n" */
Denis Vlasenko74324c82007-06-04 10:16:52 +0000110 syslog(i, "%s", strbuf);
Eric Andersen20aab262001-07-19 22:28:02 +0000111 }
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000112 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000113 } else {
Glenn L McGrath907a2402002-11-10 21:33:28 +0000114 char *message = NULL;
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000115 int len = 0;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000116 int pos = 0;
117 do {
118 len += strlen(*argv) + 1;
Denis Vlasenkoebeaea02007-10-02 09:57:41 +0000119 message = xrealloc(message, len + 1);
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000120 sprintf(message + pos, " %s", *argv),
121 pos = len;
122 } while (*++argv);
Bernhard Reutner-Fischer0f486632007-01-09 17:37:32 +0000123 syslog(i, "%s", message + 1); /* skip leading " " */
Eric Andersenbefda6e1999-11-25 08:06:22 +0000124 }
Eric Andersen3843e961999-11-25 07:30:46 +0000125
Eric Andersen20aab262001-07-19 22:28:02 +0000126 closelog();
Matt Kraai31804132000-10-25 16:40:21 +0000127 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000128}
Eric Andersen4e573f42000-11-14 23:29:24 +0000129
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000130/* Clean up. Needed because we are included from syslogd_and_logger.c */
131#undef strbuf
Eric Andersen4e573f42000-11-14 23:29:24 +0000132
133/*-
134 * Copyright (c) 1983, 1993
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200135 * The Regents of the University of California. All rights reserved.
Eric Andersen4e573f42000-11-14 23:29:24 +0000136 *
137 * This is the original license statement for the decode and pencode functions.
138 *
139 * Redistribution and use in source and binary forms, with or without
140 * modification, are permitted provided that the following conditions
141 * are met:
142 * 1. Redistributions of source code must retain the above copyright
143 * notice, this list of conditions and the following disclaimer.
144 * 2. Redistributions in binary form must reproduce the above copyright
145 * notice, this list of conditions and the following disclaimer in the
146 * documentation and/or other materials provided with the distribution.
147 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200148 * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
149 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
Eric Andersen4e573f42000-11-14 23:29:24 +0000150 *
151 * 4. Neither the name of the University nor the names of its contributors
152 * may be used to endorse or promote products derived from this software
153 * without specific prior written permission.
154 *
155 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
156 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
157 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
158 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
159 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
160 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
161 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
162 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
163 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
164 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
165 * SUCH DAMAGE.
166 */