blob: 0e1ce8dce7e439539d2d1e966d6033f3bdcae042 [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
24#include "internal.h"
25#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>
28#include <sys/stat.h>
29#include <fcntl.h>
Eric Andersen3843e961999-11-25 07:30:46 +000030#include <ctype.h>
Eric Andersen3843e961999-11-25 07:30:46 +000031
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>
42typedef struct _code {
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 char *c_name;
44 int c_val;
Eric Andersen3843e961999-11-25 07:30:46 +000045} CODE;
46extern CODE prioritynames[];
47extern CODE facilitynames[];
48#endif
49
Eric Andersen3843e961999-11-25 07:30:46 +000050/* Decode a symbolic name to a numeric value
51 * this function is based on code
52 * Copyright (c) 1983, 1993
53 * The Regents of the University of California. All rights reserved.
54 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000055static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000056{
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 if (isdigit(*name))
60 return (atoi(name));
61 for (c = codetab; c->c_name; c++) {
62 if (!strcasecmp(name, c->c_name)) {
63 return (c->c_val);
64 }
Eric Andersen3843e961999-11-25 07:30:46 +000065 }
Eric Andersen3843e961999-11-25 07:30:46 +000066
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000068}
69
70/* Decode a symbolic name to a numeric value
71 * this function is based on code
72 * Copyright (c) 1983, 1993
73 * The Regents of the University of California. All rights reserved.
74 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000075static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000076{
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 char *save;
78 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000079
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 for (save = s; *s && *s != '.'; ++s);
81 if (*s) {
82 *s = '\0';
83 fac = decode(save, facilitynames);
84 if (fac < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000085 errorMsg("unknown facility name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 exit(FALSE);
87 }
88 *s++ = '.';
89 } else {
90 s = save;
Eric Andersen3843e961999-11-25 07:30:46 +000091 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 lev = decode(s, prioritynames);
93 if (lev < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000094 errorMsg("unknown priority name: %s\n", save);
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 exit(FALSE);
96 }
97 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
Eric Andersen3843e961999-11-25 07:30:46 +000098}
99
100
101extern int logger_main(int argc, char **argv)
102{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 int pri = LOG_USER | LOG_NOTICE;
104 int option = 0;
105 int fromStdinFlag = FALSE;
106 int stopLookingAtMeLikeThat = FALSE;
Eric Andersenb6106152000-06-19 17:25:40 +0000107 char *message=NULL, buf[1024], name[128];
Eric Andersen3843e961999-11-25 07:30:46 +0000108
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 /* Fill out the name string early (may be overwritten later */
110 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000111
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 /* Parse any options */
113 while (--argc > 0 && **(++argv) == '-') {
114 if (*((*argv) + 1) == '\0') {
115 fromStdinFlag = TRUE;
116 }
117 stopLookingAtMeLikeThat = FALSE;
118 while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) {
119 switch (**argv) {
120 case 's':
121 option |= LOG_PERROR;
122 break;
123 case 'p':
124 if (--argc == 0) {
125 usage(logger_usage);
126 }
127 pri = pencode(*(++argv));
128 stopLookingAtMeLikeThat = TRUE;
129 break;
130 case 't':
131 if (--argc == 0) {
132 usage(logger_usage);
133 }
134 strncpy(name, *(++argv), sizeof(name));
135 stopLookingAtMeLikeThat = TRUE;
136 break;
137 default:
138 usage(logger_usage);
139 }
140 }
Eric Andersen3843e961999-11-25 07:30:46 +0000141 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142
143 if (fromStdinFlag == TRUE) {
144 /* read from stdin */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000145 int c;
146 unsigned int i = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147
148 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
149 buf[i++] = c;
150 }
151 message = buf;
Eric Andersenbefda6e1999-11-25 08:06:22 +0000152 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 if (argc >= 1) {
154 message = *argv;
155 } else {
Matt Kraaid537a952000-07-14 01:51:25 +0000156 errorMsg("No message\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 exit(FALSE);
158 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000159 }
Eric Andersen3843e961999-11-25 07:30:46 +0000160
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 openlog(name, option, (pri | LOG_FACMASK));
162 syslog(pri, message);
163 closelog();
Eric Andersen3843e961999-11-25 07:30:46 +0000164
Eric Andersenb6106152000-06-19 17:25:40 +0000165 return(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000166}