Upates to include copyright 2000 to everything
[platform/upstream/busybox.git] / sysklogd / logger.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini logger implementation for busybox
4  *
5  * Copyright (C) 1999,2000 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>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <ctype.h>
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 {
43         char *c_name;
44         int c_val;
45 } CODE;
46 extern CODE prioritynames[];
47 extern CODE facilitynames[];
48 #endif
49
50 static const char logger_usage[] =
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";
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  */
66 static int decode(char *name, CODE * codetab)
67 {
68         CODE *c;
69
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                 }
76         }
77
78         return (-1);
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  */
86 static int pencode(char *s)
87 {
88         char *save;
89         int lev, fac = LOG_USER;
90
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;
102         }
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));
109 }
110
111
112 extern int logger_main(int argc, char **argv)
113 {
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];
119
120         /* Fill out the name string early (may be overwritten later */
121         my_getpwuid(name, geteuid());
122
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                 }
152         }
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;
162         } else {
163                 if (argc >= 1) {
164                         message = *argv;
165                 } else {
166                         fprintf(stderr, "No message\n");
167                         exit(FALSE);
168                 }
169         }
170
171         openlog(name, option, (pri | LOG_FACMASK));
172         syslog(pri, message);
173         closelog();
174
175         exit(TRUE);
176 }