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 | |
| 24 | #include "internal.h" |
| 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> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 31 | |
| 32 | #if !defined BB_SYSLOGD |
| 33 | |
| 34 | #define SYSLOG_NAMES |
| 35 | #include <sys/syslog.h> |
| 36 | |
| 37 | #else |
| 38 | /* We have to do this since the header file defines static |
| 39 | * structues. Argh.... bad libc, bad, bad... |
| 40 | */ |
| 41 | #include <sys/syslog.h> |
| 42 | typedef struct _code { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | char *c_name; |
| 44 | int c_val; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 45 | } CODE; |
| 46 | extern CODE prioritynames[]; |
| 47 | extern CODE facilitynames[]; |
| 48 | #endif |
| 49 | |
| 50 | static const char logger_usage[] = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame^] | 51 | "logger [OPTION]... [MESSAGE]\n" |
| 52 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 53 | "\nWrite MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | "Options:\n" |
| 55 | "\t-s\tLog to stderr as well as the system log.\n" |
| 56 | "\t-t\tLog using the specified tag (defaults to user name).\n" |
| 57 | |
| 58 | "\t-p\tEnter the message with the specified priority.\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame^] | 59 | "\t\tThis may be numerical or a ``facility.level'' pair.\n" |
| 60 | #endif |
| 61 | ; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | /* Decode a symbolic name to a numeric value |
| 65 | * this function is based on code |
| 66 | * Copyright (c) 1983, 1993 |
| 67 | * The Regents of the University of California. All rights reserved. |
| 68 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | static int decode(char *name, CODE * codetab) |
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 | CODE *c; |
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 | if (isdigit(*name)) |
| 74 | return (atoi(name)); |
| 75 | for (c = codetab; c->c_name; c++) { |
| 76 | if (!strcasecmp(name, c->c_name)) { |
| 77 | return (c->c_val); |
| 78 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 79 | } |
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 | return (-1); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Decode a symbolic name to a numeric value |
| 85 | * this function is based on code |
| 86 | * Copyright (c) 1983, 1993 |
| 87 | * The Regents of the University of California. All rights reserved. |
| 88 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 90 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | char *save; |
| 92 | int lev, fac = LOG_USER; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 93 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | for (save = s; *s && *s != '.'; ++s); |
| 95 | if (*s) { |
| 96 | *s = '\0'; |
| 97 | fac = decode(save, facilitynames); |
| 98 | if (fac < 0) { |
| 99 | fprintf(stderr, "unknown facility name: %s\n", save); |
| 100 | exit(FALSE); |
| 101 | } |
| 102 | *s++ = '.'; |
| 103 | } else { |
| 104 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 105 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | lev = decode(s, prioritynames); |
| 107 | if (lev < 0) { |
| 108 | fprintf(stderr, "unknown priority name: %s\n", save); |
| 109 | exit(FALSE); |
| 110 | } |
| 111 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | extern int logger_main(int argc, char **argv) |
| 116 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | int pri = LOG_USER | LOG_NOTICE; |
| 118 | int option = 0; |
| 119 | int fromStdinFlag = FALSE; |
| 120 | int stopLookingAtMeLikeThat = FALSE; |
| 121 | char *message, buf[1024], name[128]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | /* Fill out the name string early (may be overwritten later */ |
| 124 | my_getpwuid(name, geteuid()); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 125 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | /* Parse any options */ |
| 127 | while (--argc > 0 && **(++argv) == '-') { |
| 128 | if (*((*argv) + 1) == '\0') { |
| 129 | fromStdinFlag = TRUE; |
| 130 | } |
| 131 | stopLookingAtMeLikeThat = FALSE; |
| 132 | while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) { |
| 133 | switch (**argv) { |
| 134 | case 's': |
| 135 | option |= LOG_PERROR; |
| 136 | break; |
| 137 | case 'p': |
| 138 | if (--argc == 0) { |
| 139 | usage(logger_usage); |
| 140 | } |
| 141 | pri = pencode(*(++argv)); |
| 142 | stopLookingAtMeLikeThat = TRUE; |
| 143 | break; |
| 144 | case 't': |
| 145 | if (--argc == 0) { |
| 146 | usage(logger_usage); |
| 147 | } |
| 148 | strncpy(name, *(++argv), sizeof(name)); |
| 149 | stopLookingAtMeLikeThat = TRUE; |
| 150 | break; |
| 151 | default: |
| 152 | usage(logger_usage); |
| 153 | } |
| 154 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 155 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | |
| 157 | if (fromStdinFlag == TRUE) { |
| 158 | /* read from stdin */ |
| 159 | int c, i = 0; |
| 160 | |
| 161 | while ((c = getc(stdin)) != EOF && i < sizeof(buf)) { |
| 162 | buf[i++] = c; |
| 163 | } |
| 164 | message = buf; |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 165 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 166 | if (argc >= 1) { |
| 167 | message = *argv; |
| 168 | } else { |
| 169 | fprintf(stderr, "No message\n"); |
| 170 | exit(FALSE); |
| 171 | } |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 172 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 173 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 174 | openlog(name, option, (pri | LOG_FACMASK)); |
| 175 | syslog(pri, message); |
| 176 | closelog(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 177 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 178 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 179 | } |