Fix up copyright msgs. Bump version to 0.49 in preparation for
[oweals/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,2001 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 "busybox.h"
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #if !defined BB_SYSLOGD
34
35 #define SYSLOG_NAMES
36 #include <sys/syslog.h>
37
38 #else
39 /* We have to do this since the header file defines static
40  * structures.  Argh.... bad libc, bad, bad...
41  */
42 #include <sys/syslog.h>
43 typedef struct _code {
44         char *c_name;
45         int c_val;
46 } CODE;
47 extern CODE prioritynames[];
48 extern CODE facilitynames[];
49 #endif
50
51 /* Decode a symbolic name to a numeric value 
52  * this function is based on code
53  * Copyright (c) 1983, 1993
54  * The Regents of the University of California.  All rights reserved.
55  *  
56  * Original copyright notice is retained at the end of this file.
57  */
58 static int decode(char *name, CODE * codetab)
59 {
60         CODE *c;
61
62         if (isdigit(*name))
63                 return (atoi(name));
64         for (c = codetab; c->c_name; c++) {
65                 if (!strcasecmp(name, c->c_name)) {
66                         return (c->c_val);
67                 }
68         }
69
70         return (-1);
71 }
72
73 /* Decode a symbolic name to a numeric value 
74  * this function is based on code
75  * Copyright (c) 1983, 1993
76  * The Regents of the University of California.  All rights reserved.
77  *
78  * Original copyright notice is retained at the end of this file.
79  */
80 static int pencode(char *s)
81 {
82         char *save;
83         int lev, fac = LOG_USER;
84
85         for (save = s; *s && *s != '.'; ++s);
86         if (*s) {
87                 *s = '\0';
88                 fac = decode(save, facilitynames);
89                 if (fac < 0)
90                         error_msg_and_die("unknown facility name: %s\n", save);
91                 *s++ = '.';
92         } else {
93                 s = save;
94         }
95         lev = decode(s, prioritynames);
96         if (lev < 0)
97                 error_msg_and_die("unknown priority name: %s\n", save);
98         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
99 }
100
101
102 extern int logger_main(int argc, char **argv)
103 {
104         int pri = LOG_USER | LOG_NOTICE;
105         int option = 0;
106         int c, i, len, opt;
107         char *message=NULL, buf[1024], name[128];
108
109         /* Fill out the name string early (may be overwritten later) */
110         my_getpwuid(name, geteuid());
111
112         /* Parse any options */
113         while ((opt = getopt(argc, argv, "p:st:")) > 0) {
114                 switch (opt) {
115                         case 's':
116                                 option |= LOG_PERROR;
117                                 break;
118                         case 'p':
119                                 pri = pencode(optarg);
120                                 break;
121                         case 't':
122                                 strncpy(name, optarg, sizeof(name));
123                                 break;
124                         default:
125                                 usage(logger_usage);
126                 }
127         }
128
129         if (optind == argc) {
130                 /* read from stdin */
131                 i = 0;
132                 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
133                         buf[i++] = c;
134                 }
135                 buf[i++] = '\0';
136                 message = buf;
137         } else {
138                 len = 1; /* for the '\0' */
139                 message=xcalloc(1, 1);
140                 for (i = optind; i < argc; i++) {
141                         len += strlen(argv[i]);
142                         len += 1;  /* for the space between the args */
143                         message = xrealloc(message, len);
144                         strcat(message, argv[i]);
145                         strcat(message, " ");
146                 }
147                 message[strlen(message)-1] = '\0';
148         }
149
150         openlog(name, option, (pri | LOG_FACMASK));
151         syslog(pri, "%s", message);
152         closelog();
153         return EXIT_SUCCESS;
154 }
155
156
157 /*-
158  * Copyright (c) 1983, 1993
159  *      The Regents of the University of California.  All rights reserved.
160  *
161  * This is the original license statement for the decode and pencode functions.
162  *
163  * Redistribution and use in source and binary forms, with or without
164  * modification, are permitted provided that the following conditions
165  * are met:
166  * 1. Redistributions of source code must retain the above copyright
167  *    notice, this list of conditions and the following disclaimer.
168  * 2. Redistributions in binary form must reproduce the above copyright
169  *    notice, this list of conditions and the following disclaimer in the
170  *    documentation and/or other materials provided with the distribution.
171  *
172  * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 
173  *              ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 
174  *
175  * 4. Neither the name of the University nor the names of its contributors
176  *    may be used to endorse or promote products derived from this software
177  *    without specific prior written permission.
178  *
179  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
180  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
183  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
184  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
185  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
186  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
187  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
188  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
189  * SUCH DAMAGE.
190  */
191
192
193