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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 30 | |
| 31 | #if !defined BB_SYSLOGD |
| 32 | |
| 33 | #define SYSLOG_NAMES |
| 34 | #include <sys/syslog.h> |
| 35 | |
| 36 | #else |
| 37 | /* We have to do this since the header file defines static |
| 38 | * structues. Argh.... bad libc, bad, bad... |
| 39 | */ |
| 40 | #include <sys/syslog.h> |
| 41 | typedef struct _code { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | char *c_name; |
| 43 | int c_val; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 44 | } CODE; |
| 45 | extern CODE prioritynames[]; |
| 46 | extern CODE facilitynames[]; |
| 47 | #endif |
| 48 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 49 | /* Decode a symbolic name to a numeric value |
| 50 | * this function is based on code |
| 51 | * Copyright (c) 1983, 1993 |
| 52 | * The Regents of the University of California. All rights reserved. |
| 53 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | static int decode(char *name, CODE * codetab) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 55 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | CODE *c; |
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 | if (isdigit(*name)) |
| 59 | return (atoi(name)); |
| 60 | for (c = codetab; c->c_name; c++) { |
| 61 | if (!strcasecmp(name, c->c_name)) { |
| 62 | return (c->c_val); |
| 63 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 64 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 65 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | return (-1); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* Decode a symbolic name to a numeric value |
| 70 | * this function is based on code |
| 71 | * Copyright (c) 1983, 1993 |
| 72 | * The Regents of the University of California. All rights reserved. |
| 73 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 75 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | char *save; |
| 77 | int lev, fac = LOG_USER; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 78 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | for (save = s; *s && *s != '.'; ++s); |
| 80 | if (*s) { |
| 81 | *s = '\0'; |
| 82 | fac = decode(save, facilitynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 83 | if (fac < 0) |
| 84 | fatalError("unknown facility name: %s\n", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | *s++ = '.'; |
| 86 | } else { |
| 87 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 88 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | lev = decode(s, prioritynames); |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 90 | if (lev < 0) |
| 91 | fatalError("unknown priority name: %s\n", save); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | extern int logger_main(int argc, char **argv) |
| 97 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | int pri = LOG_USER | LOG_NOTICE; |
| 99 | int option = 0; |
| 100 | int fromStdinFlag = FALSE; |
| 101 | int stopLookingAtMeLikeThat = FALSE; |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 102 | char *message=NULL, buf[1024], name[128]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | /* Fill out the name string early (may be overwritten later */ |
| 105 | my_getpwuid(name, geteuid()); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 106 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | /* Parse any options */ |
| 108 | while (--argc > 0 && **(++argv) == '-') { |
| 109 | if (*((*argv) + 1) == '\0') { |
| 110 | fromStdinFlag = TRUE; |
| 111 | } |
| 112 | stopLookingAtMeLikeThat = FALSE; |
| 113 | while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) { |
| 114 | switch (**argv) { |
| 115 | case 's': |
| 116 | option |= LOG_PERROR; |
| 117 | break; |
| 118 | case 'p': |
| 119 | if (--argc == 0) { |
| 120 | usage(logger_usage); |
| 121 | } |
| 122 | pri = pencode(*(++argv)); |
| 123 | stopLookingAtMeLikeThat = TRUE; |
| 124 | break; |
| 125 | case 't': |
| 126 | if (--argc == 0) { |
| 127 | usage(logger_usage); |
| 128 | } |
| 129 | strncpy(name, *(++argv), sizeof(name)); |
| 130 | stopLookingAtMeLikeThat = TRUE; |
| 131 | break; |
| 132 | default: |
| 133 | usage(logger_usage); |
| 134 | } |
| 135 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 136 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 137 | |
| 138 | if (fromStdinFlag == TRUE) { |
| 139 | /* read from stdin */ |
Pavel Roskin | df4532b | 2000-07-14 17:24:58 +0000 | [diff] [blame] | 140 | int c; |
| 141 | unsigned int i = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | |
| 143 | while ((c = getc(stdin)) != EOF && i < sizeof(buf)) { |
| 144 | buf[i++] = c; |
| 145 | } |
| 146 | message = buf; |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 147 | } else { |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 148 | if (argc >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | message = *argv; |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 150 | else |
| 151 | fatalError("No message\n"); |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 153 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | openlog(name, option, (pri | LOG_FACMASK)); |
| 155 | syslog(pri, message); |
| 156 | closelog(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 157 | |
Matt Kraai | 3180413 | 2000-10-25 16:40:21 +0000 | [diff] [blame] | 158 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 159 | } |