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 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 10 | /* |
| 11 | * Done in syslogd_and_logger.c: |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 13 | #define SYSLOG_NAMES |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 14 | #define SYSLOG_NAMES_CONST |
| 15 | #include <syslog.h> |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 16 | */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 17 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 19 | * this function is based on code |
| 20 | * Copyright (c) 1983, 1993 |
| 21 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 23 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 24 | */ |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 25 | static int decode(char *name, const CODE *codetab) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | d202328 | 2007-11-23 23:39:01 +0000 | [diff] [blame] | 27 | const CODE *c; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 28 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | if (isdigit(*name)) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 30 | return atoi(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | for (c = codetab; c->c_name; c++) { |
| 32 | if (!strcasecmp(name, c->c_name)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 33 | return c->c_val; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 35 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 37 | return -1; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 40 | /* Decode a symbolic name to a numeric value |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 41 | * this function is based on code |
| 42 | * Copyright (c) 1983, 1993 |
| 43 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 46 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 48 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | char *save; |
| 50 | int lev, fac = LOG_USER; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 51 | |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 52 | for (save = s; *s && *s != '.'; ++s) |
| 53 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | if (*s) { |
| 55 | *s = '\0'; |
| 56 | fac = decode(save, facilitynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 57 | if (fac < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 58 | bb_error_msg_and_die("unknown %s name: %s", "facility", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | *s++ = '.'; |
| 60 | } else { |
| 61 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 62 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | lev = decode(s, prioritynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 64 | if (lev < 0) |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 65 | bb_error_msg_and_die("unknown %s name: %s", "priority", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 69 | #define strbuf bb_common_bufsiz1 |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 71 | int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 72 | int logger_main(int argc, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 73 | { |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 74 | char *str_p, *str_t; |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 75 | int i = 0; |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 76 | char name[80]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 77 | |
Matt Kraai | 1944f54 | 2001-01-02 18:13:58 +0000 | [diff] [blame] | 78 | /* Fill out the name string early (may be overwritten later) */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 79 | bb_getpwuid(name, sizeof(name), geteuid()); |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 80 | str_t = name; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | /* Parse any options */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 83 | getopt32(argv, "p:st:", &str_p, &str_t); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 84 | |
| 85 | if (option_mask32 & 0x2) /* -s */ |
| 86 | i |= LOG_PERROR; |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 87 | //if (option_mask32 & 0x4) /* -t */ |
| 88 | openlog(str_t, i, 0); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 89 | i = LOG_USER | LOG_NOTICE; |
| 90 | if (option_mask32 & 0x1) /* -p */ |
Denis Vlasenko | 4df8135 | 2007-01-12 21:01:05 +0000 | [diff] [blame] | 91 | i = pencode(str_p); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 93 | argc -= optind; |
| 94 | argv += optind; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 95 | if (!argc) { |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 96 | while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 97 | if (strbuf[0] |
| 98 | && NOT_LONE_CHAR(strbuf, '\n') |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 99 | ) { |
| 100 | /* Neither "" nor "\n" */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 101 | syslog(i, "%s", strbuf); |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 102 | } |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 104 | } else { |
Glenn L McGrath | 907a240 | 2002-11-10 21:33:28 +0000 | [diff] [blame] | 105 | char *message = NULL; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 106 | int len = 0; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 107 | int pos = 0; |
| 108 | do { |
| 109 | len += strlen(*argv) + 1; |
Denis Vlasenko | ebeaea0 | 2007-10-02 09:57:41 +0000 | [diff] [blame] | 110 | message = xrealloc(message, len + 1); |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 111 | sprintf(message + pos, " %s", *argv), |
| 112 | pos = len; |
| 113 | } while (*++argv); |
Bernhard Reutner-Fischer | 0f48663 | 2007-01-09 17:37:32 +0000 | [diff] [blame] | 114 | syslog(i, "%s", message + 1); /* skip leading " " */ |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 115 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 116 | |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 117 | closelog(); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 118 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 119 | } |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 121 | /* Clean up. Needed because we are included from syslogd_and_logger.c */ |
| 122 | #undef strbuf |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 123 | |
| 124 | /*- |
| 125 | * Copyright (c) 1983, 1993 |
| 126 | * The Regents of the University of California. All rights reserved. |
| 127 | * |
| 128 | * This is the original license statement for the decode and pencode functions. |
| 129 | * |
| 130 | * Redistribution and use in source and binary forms, with or without |
| 131 | * modification, are permitted provided that the following conditions |
| 132 | * are met: |
| 133 | * 1. Redistributions of source code must retain the above copyright |
| 134 | * notice, this list of conditions and the following disclaimer. |
| 135 | * 2. Redistributions in binary form must reproduce the above copyright |
| 136 | * notice, this list of conditions and the following disclaimer in the |
| 137 | * documentation and/or other materials provided with the distribution. |
| 138 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 139 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 140 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 141 | * |
| 142 | * 4. Neither the name of the University nor the names of its contributors |
| 143 | * may be used to endorse or promote products derived from this software |
| 144 | * without specific prior written permission. |
| 145 | * |
| 146 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 147 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 148 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 149 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 150 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 151 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 152 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 153 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 154 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 155 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 156 | * SUCH DAMAGE. |
| 157 | */ |