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