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 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 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 | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | "logger [OPTION]... [MESSAGE]\n\n" |
| 52 | "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n" |
| 53 | "Options:\n" |
| 54 | "\t-s\tLog to stderr as well as the system log.\n" |
| 55 | "\t-t\tLog using the specified tag (defaults to user name).\n" |
| 56 | |
| 57 | "\t-p\tEnter the message with the specified priority.\n" |
| 58 | "\t\tThis may be numerical or a ``facility.level'' pair.\n"; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | /* Decode a symbolic name to a numeric value |
| 62 | * this function is based on code |
| 63 | * Copyright (c) 1983, 1993 |
| 64 | * The Regents of the University of California. All rights reserved. |
| 65 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | static int decode(char *name, CODE * codetab) |
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 | CODE *c; |
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 | if (isdigit(*name)) |
| 71 | return (atoi(name)); |
| 72 | for (c = codetab; c->c_name; c++) { |
| 73 | if (!strcasecmp(name, c->c_name)) { |
| 74 | return (c->c_val); |
| 75 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 76 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 77 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | return (-1); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* Decode a symbolic name to a numeric value |
| 82 | * this function is based on code |
| 83 | * Copyright (c) 1983, 1993 |
| 84 | * The Regents of the University of California. All rights reserved. |
| 85 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | static int pencode(char *s) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 87 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | char *save; |
| 89 | int lev, fac = LOG_USER; |
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 | for (save = s; *s && *s != '.'; ++s); |
| 92 | if (*s) { |
| 93 | *s = '\0'; |
| 94 | fac = decode(save, facilitynames); |
| 95 | if (fac < 0) { |
| 96 | fprintf(stderr, "unknown facility name: %s\n", save); |
| 97 | exit(FALSE); |
| 98 | } |
| 99 | *s++ = '.'; |
| 100 | } else { |
| 101 | s = save; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 102 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | lev = decode(s, prioritynames); |
| 104 | if (lev < 0) { |
| 105 | fprintf(stderr, "unknown priority name: %s\n", save); |
| 106 | exit(FALSE); |
| 107 | } |
| 108 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | extern int logger_main(int argc, char **argv) |
| 113 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 114 | int pri = LOG_USER | LOG_NOTICE; |
| 115 | int option = 0; |
| 116 | int fromStdinFlag = FALSE; |
| 117 | int stopLookingAtMeLikeThat = FALSE; |
| 118 | char *message, buf[1024], name[128]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 119 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | /* Fill out the name string early (may be overwritten later */ |
| 121 | my_getpwuid(name, geteuid()); |
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 | /* Parse any options */ |
| 124 | while (--argc > 0 && **(++argv) == '-') { |
| 125 | if (*((*argv) + 1) == '\0') { |
| 126 | fromStdinFlag = TRUE; |
| 127 | } |
| 128 | stopLookingAtMeLikeThat = FALSE; |
| 129 | while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) { |
| 130 | switch (**argv) { |
| 131 | case 's': |
| 132 | option |= LOG_PERROR; |
| 133 | break; |
| 134 | case 'p': |
| 135 | if (--argc == 0) { |
| 136 | usage(logger_usage); |
| 137 | } |
| 138 | pri = pencode(*(++argv)); |
| 139 | stopLookingAtMeLikeThat = TRUE; |
| 140 | break; |
| 141 | case 't': |
| 142 | if (--argc == 0) { |
| 143 | usage(logger_usage); |
| 144 | } |
| 145 | strncpy(name, *(++argv), sizeof(name)); |
| 146 | stopLookingAtMeLikeThat = TRUE; |
| 147 | break; |
| 148 | default: |
| 149 | usage(logger_usage); |
| 150 | } |
| 151 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 152 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 153 | |
| 154 | if (fromStdinFlag == TRUE) { |
| 155 | /* read from stdin */ |
| 156 | int c, i = 0; |
| 157 | |
| 158 | while ((c = getc(stdin)) != EOF && i < sizeof(buf)) { |
| 159 | buf[i++] = c; |
| 160 | } |
| 161 | message = buf; |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 162 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 163 | if (argc >= 1) { |
| 164 | message = *argv; |
| 165 | } else { |
| 166 | fprintf(stderr, "No message\n"); |
| 167 | exit(FALSE); |
| 168 | } |
Eric Andersen | befda6e | 1999-11-25 08:06:22 +0000 | [diff] [blame] | 169 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 170 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | openlog(name, option, (pri | LOG_FACMASK)); |
| 172 | syslog(pri, message); |
| 173 | closelog(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 174 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 176 | } |