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