Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini logger implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 9 | //config:config LOGGER |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "logger (6.3 kb)" |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select FEATURE_SYSLOG |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: The logger utility allows you to send arbitrary text |
| 15 | //config: messages to the system log (i.e. the 'syslogd' utility) so |
| 16 | //config: they can be logged. This is generally used to help locate |
| 17 | //config: problems that occur within programs and scripts. |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 18 | |
| 19 | //applet:IF_LOGGER(APPLET(logger, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_LOGGER) += syslogd_and_logger.o |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define logger_trivial_usage |
Denys Vlasenko | 84d5edd | 2020-12-13 22:34:05 +0100 | [diff] [blame] | 24 | //usage: "[-s] [-t TAG] [-p PRIO] [MESSAGE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define logger_full_usage "\n\n" |
| 26 | //usage: "Write MESSAGE (or stdin) to syslog\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | //usage: "\n -s Log to stderr as well as the system log" |
| 28 | //usage: "\n -t TAG Log using the specified tag (defaults to user name)" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 29 | //usage: "\n -p PRIO Priority (number or FACILITY.LEVEL pair)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: |
| 31 | //usage:#define logger_example_usage |
| 32 | //usage: "$ logger \"hello\"\n" |
| 33 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 34 | /* |
| 35 | * Done in syslogd_and_logger.c: |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 36 | #include "libbb.h" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 37 | #define SYSLOG_NAMES |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 38 | #define SYSLOG_NAMES_CONST |
| 39 | #include <syslog.h> |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 40 | */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 41 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 42 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 43 | * this function is based on code |
| 44 | * Copyright (c) 1983, 1993 |
| 45 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 46 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 47 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 48 | */ |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 49 | static int decode(char *name, const CODE *codetab) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 50 | { |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 51 | const CODE *c; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 52 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | if (isdigit(*name)) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 54 | return atoi(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | for (c = codetab; c->c_name; c++) { |
| 56 | if (!strcasecmp(name, c->c_name)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 57 | return c->c_val; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 59 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 61 | return -1; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 64 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 65 | * this function is based on code |
| 66 | * Copyright (c) 1983, 1993 |
| 67 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 68 | * |
| 69 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 70 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 72 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | char *save; |
| 74 | int lev, fac = LOG_USER; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 75 | |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 76 | for (save = s; *s && *s != '.'; ++s) |
| 77 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | if (*s) { |
| 79 | *s = '\0'; |
Denys Vlasenko | cf686ae | 2017-08-16 15:05:36 +0200 | [diff] [blame] | 80 | fac = decode(save, bb_facilitynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 81 | if (fac < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 82 | bb_error_msg_and_die("unknown %s name: %s", "facility", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | *s++ = '.'; |
| 84 | } else { |
| 85 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 86 | } |
Denys Vlasenko | cf686ae | 2017-08-16 15:05:36 +0200 | [diff] [blame] | 87 | lev = decode(s, bb_prioritynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 88 | if (lev < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 89 | bb_error_msg_and_die("unknown %s name: %s", "priority", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 93 | #define strbuf bb_common_bufsiz1 |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 95 | int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 96 | int logger_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 98 | char *str_p, *str_t; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 99 | int opt; |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 100 | int i = 0; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 101 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 102 | setup_common_bufsiz(); |
| 103 | |
Matt Kraai | 1944f54 | 2001-01-02 18:13:58 +0000 | [diff] [blame] | 104 | /* Fill out the name string early (may be overwritten later) */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 105 | str_t = uid2uname_utoa(geteuid()); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 106 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | /* Parse any options */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 108 | opt = getopt32(argv, "p:st:", &str_p, &str_t); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 110 | if (opt & 0x2) /* -s */ |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 111 | i |= LOG_PERROR; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 112 | //if (opt & 0x4) /* -t */ |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 113 | openlog(str_t, i, 0); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 114 | i = LOG_USER | LOG_NOTICE; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 115 | if (opt & 0x1) /* -p */ |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 116 | i = pencode(str_p); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 118 | argv += optind; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 119 | if (!argv[0]) { |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 120 | while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 121 | if (strbuf[0] |
| 122 | && NOT_LONE_CHAR(strbuf, '\n') |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 123 | ) { |
| 124 | /* Neither "" nor "\n" */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 125 | syslog(i, "%s", strbuf); |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 126 | } |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 127 | } |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 128 | } else { |
Glenn L McGrath | 907a240 | 2002-11-10 21:33:28 +0000 | [diff] [blame] | 129 | char *message = NULL; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 130 | int len = 0; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 131 | int pos = 0; |
| 132 | do { |
| 133 | len += strlen(*argv) + 1; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 134 | message = xrealloc(message, len + 1); |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 135 | sprintf(message + pos, " %s", *argv), |
| 136 | pos = len; |
| 137 | } while (*++argv); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 138 | syslog(i, "%s", message + 1); /* skip leading " " */ |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 139 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 140 | |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 141 | closelog(); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 142 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 143 | } |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 145 | /* Clean up. Needed because we are included from syslogd_and_logger.c */ |
| 146 | #undef strbuf |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 147 | |
| 148 | /*- |
| 149 | * Copyright (c) 1983, 1993 |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 150 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 151 | * |
| 152 | * This is the original license statement for the decode and pencode functions. |
| 153 | * |
| 154 | * Redistribution and use in source and binary forms, with or without |
| 155 | * modification, are permitted provided that the following conditions |
| 156 | * are met: |
| 157 | * 1. Redistributions of source code must retain the above copyright |
| 158 | * notice, this list of conditions and the following disclaimer. |
| 159 | * 2. Redistributions in binary form must reproduce the above copyright |
| 160 | * notice, this list of conditions and the following disclaimer in the |
| 161 | * documentation and/or other materials provided with the distribution. |
| 162 | * |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 163 | * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 164 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 165 | * |
| 166 | * 4. Neither the name of the University nor the names of its contributors |
| 167 | * may be used to endorse or promote products derived from this software |
| 168 | * without specific prior written permission. |
| 169 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 170 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 171 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 172 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 173 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 174 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 175 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 176 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 177 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 178 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 179 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 180 | * SUCH DAMAGE. |
| 181 | */ |