blob: a9e0afcc8380e638e004947422ef7390e89cd771 [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 *
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 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
50static const char logger_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 "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 Andersen3843e961999-11-25 07:30:46 +000059
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 Andersene49d5ec2000-02-08 19:58:47 +000066static int decode(char *name, CODE * codetab)
Eric Andersen3843e961999-11-25 07:30:46 +000067{
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 CODE *c;
Eric Andersen3843e961999-11-25 07:30:46 +000069
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 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 Andersen3843e961999-11-25 07:30:46 +000076 }
Eric Andersen3843e961999-11-25 07:30:46 +000077
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 return (-1);
Eric Andersen3843e961999-11-25 07:30:46 +000079}
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 Andersene49d5ec2000-02-08 19:58:47 +000086static int pencode(char *s)
Eric Andersen3843e961999-11-25 07:30:46 +000087{
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 char *save;
89 int lev, fac = LOG_USER;
Eric Andersen3843e961999-11-25 07:30:46 +000090
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 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 Andersen3843e961999-11-25 07:30:46 +0000102 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 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 Andersen3843e961999-11-25 07:30:46 +0000109}
110
111
112extern int logger_main(int argc, char **argv)
113{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 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 Andersen3843e961999-11-25 07:30:46 +0000119
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 /* Fill out the name string early (may be overwritten later */
121 my_getpwuid(name, geteuid());
Eric Andersen3843e961999-11-25 07:30:46 +0000122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 /* 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 Andersen3843e961999-11-25 07:30:46 +0000152 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153
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 Andersenbefda6e1999-11-25 08:06:22 +0000162 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 if (argc >= 1) {
164 message = *argv;
165 } else {
166 fprintf(stderr, "No message\n");
167 exit(FALSE);
168 }
Eric Andersenbefda6e1999-11-25 08:06:22 +0000169 }
Eric Andersen3843e961999-11-25 07:30:46 +0000170
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 openlog(name, option, (pri | LOG_FACMASK));
172 syslog(pri, message);
173 closelog();
Eric Andersen3843e961999-11-25 07:30:46 +0000174
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000176}