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