blob: fa1cf6d15e2c023f33e553ae00387adcf13253de [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini logger implementation for busybox
4 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 by Lineo, inc.
Eric Andersen3843e961999-11-25 07:30:46 +00006 * 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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersen3843e961999-11-25 07:30:46 +000025#include <stdio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000026#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000027#include <sys/types.h>
Eric Andersen3843e961999-11-25 07:30:46 +000028#include <fcntl.h>
Eric Andersen3843e961999-11-25 07:30:46 +000029#include <ctype.h>
Eric Andersen3843e961999-11-25 07:30:46 +000030
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>
41typedef struct _code {
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 char *c_name;
43 int c_val;
Eric Andersen3843e961999-11-25 07:30:46 +000044} CODE;
45extern CODE prioritynames[];
46extern CODE facilitynames[];
47#endif
48
Eric Andersen3843e961999-11-25 07:30:46 +000049/* 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 Andersene49d5ec2000-02-08 19:58:47 +000054static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000055{
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000057
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 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 Andersen3843e961999-11-25 07:30:46 +000064 }
Eric Andersen3843e961999-11-25 07:30:46 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000067}
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 Andersene49d5ec2000-02-08 19:58:47 +000074static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000075{
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 char *save;
77 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000078
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 for (save = s; *s && *s != '.'; ++s);
80 if (*s) {
81 *s = '\0';
82 fac = decode(save, facilitynames);
83 if (fac < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000084 errorMsg("unknown facility name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 exit(FALSE);
86 }
87 *s++ = '.';
88 } else {
89 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000090 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 lev = decode(s, prioritynames);
92 if (lev < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000093 errorMsg("unknown priority name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 exit(FALSE);
95 }
96 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000097}
98
99
100extern int logger_main(int argc, char **argv)
101{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 int pri = LOG_USER | LOG_NOTICE;
103 int option = 0;
104 int fromStdinFlag = FALSE;
105 int stopLookingAtMeLikeThat = FALSE;
Eric Andersenb6106152000-06-19 17:25:40 +0000106 char *message=NULL, buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +0000107
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 /* Fill out the name string early (may be overwritten later */
109 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000110
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 /* Parse any options */
112 while (--argc > 0 && **(++argv) == '-') {
113 if (*((*argv) + 1) == '\0') {
114 fromStdinFlag = TRUE;
115 }
116 stopLookingAtMeLikeThat = FALSE;
117 while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) {
118 switch (**argv) {
119 case 's':
120 option |= LOG_PERROR;
121 break;
122 case 'p':
123 if (--argc == 0) {
124 usage(logger_usage);
125 }
126 pri = pencode(*(++argv));
127 stopLookingAtMeLikeThat = TRUE;
128 break;
129 case 't':
130 if (--argc == 0) {
131 usage(logger_usage);
132 }
133 strncpy(name, *(++argv), sizeof(name));
134 stopLookingAtMeLikeThat = TRUE;
135 break;
136 default:
137 usage(logger_usage);
138 }
139 }
Eric Andersen3843e961999-11-25 07:30:46 +0000140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141
142 if (fromStdinFlag == TRUE) {
143 /* read from stdin */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000144 int c;
145 unsigned int i = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146
147 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
148 buf[i++] = c;
149 }
150 message = buf;
Eric Andersenbefda6e1999-11-25 08:06:22 +0000151 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (argc >= 1) {
153 message = *argv;
154 } else {
Matt Kraaid537a952000-07-14 01:51:25 +0000155 errorMsg("No message\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 exit(FALSE);
157 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000158 }
Eric Andersen3843e961999-11-25 07:30:46 +0000159
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 openlog(name, option, (pri | LOG_FACMASK));
161 syslog(pri, message);
162 closelog();
Eric Andersen3843e961999-11-25 07:30:46 +0000163
Eric Andersenb6106152000-06-19 17:25:40 +0000164 return(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000165}