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 | */ |
| 9 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 10 | //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 Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | //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 Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Done in syslogd_and_logger.c: |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 24 | #define SYSLOG_NAMES |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 25 | #define SYSLOG_NAMES_CONST |
| 26 | #include <syslog.h> |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 27 | */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 28 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 29 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 30 | * this function is based on code |
| 31 | * Copyright (c) 1983, 1993 |
| 32 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 33 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 34 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 35 | */ |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 36 | static int decode(char *name, const CODE *codetab) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 37 | { |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 38 | const CODE *c; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 39 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | if (isdigit(*name)) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 41 | return atoi(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | for (c = codetab; c->c_name; c++) { |
| 43 | if (!strcasecmp(name, c->c_name)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 44 | return c->c_val; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 46 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 48 | return -1; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 51 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 52 | * this function is based on code |
| 53 | * Copyright (c) 1983, 1993 |
| 54 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 55 | * |
| 56 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 57 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 59 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | char *save; |
| 61 | int lev, fac = LOG_USER; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 62 | |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 63 | for (save = s; *s && *s != '.'; ++s) |
| 64 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | if (*s) { |
| 66 | *s = '\0'; |
| 67 | fac = decode(save, facilitynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 68 | if (fac < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 69 | bb_error_msg_and_die("unknown %s name: %s", "facility", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | *s++ = '.'; |
| 71 | } else { |
| 72 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 73 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | lev = decode(s, prioritynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 75 | if (lev < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 76 | bb_error_msg_and_die("unknown %s name: %s", "priority", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 80 | #define strbuf bb_common_bufsiz1 |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 82 | int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 83 | int logger_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 85 | char *str_p, *str_t; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 86 | int opt; |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 87 | int i = 0; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 88 | |
Matt Kraai | 1944f54 | 2001-01-02 18:13:58 +0000 | [diff] [blame] | 89 | /* Fill out the name string early (may be overwritten later) */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 90 | str_t = uid2uname_utoa(geteuid()); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 91 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | /* Parse any options */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 93 | opt = getopt32(argv, "p:st:", &str_p, &str_t); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 95 | if (opt & 0x2) /* -s */ |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 96 | i |= LOG_PERROR; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 97 | //if (opt & 0x4) /* -t */ |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 98 | openlog(str_t, i, 0); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 99 | i = LOG_USER | LOG_NOTICE; |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 100 | if (opt & 0x1) /* -p */ |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 101 | i = pencode(str_p); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 103 | argv += optind; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 104 | if (!argv[0]) { |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 105 | while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 106 | if (strbuf[0] |
| 107 | && NOT_LONE_CHAR(strbuf, '\n') |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 108 | ) { |
| 109 | /* Neither "" nor "\n" */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 110 | syslog(i, "%s", strbuf); |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 111 | } |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 112 | } |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 113 | } else { |
Glenn L McGrath | 907a240 | 2002-11-10 21:33:28 +0000 | [diff] [blame] | 114 | char *message = NULL; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 115 | int len = 0; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 116 | int pos = 0; |
| 117 | do { |
| 118 | len += strlen(*argv) + 1; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 119 | message = xrealloc(message, len + 1); |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 120 | sprintf(message + pos, " %s", *argv), |
| 121 | pos = len; |
| 122 | } while (*++argv); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 123 | syslog(i, "%s", message + 1); /* skip leading " " */ |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 124 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 125 | |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 126 | closelog(); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 127 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 128 | } |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 130 | /* Clean up. Needed because we are included from syslogd_and_logger.c */ |
| 131 | #undef strbuf |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 132 | |
| 133 | /*- |
| 134 | * Copyright (c) 1983, 1993 |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 135 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 136 | * |
| 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 Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 148 | * 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 Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 150 | * |
| 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 | */ |