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