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