syslogd,klogd: better help text
[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 source tree.
8  */
9
10 //usage:#define logger_trivial_usage
11 //usage:       "[OPTIONS] [MESSAGE]"
12 //usage:#define logger_full_usage "\n\n"
13 //usage:       "Write MESSAGE (or stdin) to syslog\n"
14 //usage:     "\nOptions:"
15 //usage:     "\n        -s      Log to stderr as well as the system log"
16 //usage:     "\n        -t TAG  Log using the specified tag (defaults to user name)"
17 //usage:     "\n        -p PRIO Priority (numeric or facility.level pair)"
18 //usage:
19 //usage:#define logger_example_usage
20 //usage:       "$ logger \"hello\"\n"
21
22 /*
23  * Done in syslogd_and_logger.c:
24 #include "libbb.h"
25 #define SYSLOG_NAMES
26 #define SYSLOG_NAMES_CONST
27 #include <syslog.h>
28 */
29
30 /* Decode a symbolic name to a numeric value
31  * this function is based on code
32  * Copyright (c) 1983, 1993
33  * The Regents of the University of California.  All rights reserved.
34  *
35  * Original copyright notice is retained at the end of this file.
36  */
37 static int decode(char *name, const CODE *codetab)
38 {
39         const CODE *c;
40
41         if (isdigit(*name))
42                 return atoi(name);
43         for (c = codetab; c->c_name; c++) {
44                 if (!strcasecmp(name, c->c_name)) {
45                         return c->c_val;
46                 }
47         }
48
49         return -1;
50 }
51
52 /* Decode a symbolic name to a numeric value
53  * this function is based on code
54  * Copyright (c) 1983, 1993
55  * The Regents of the University of California.  All rights reserved.
56  *
57  * Original copyright notice is retained at the end of this file.
58  */
59 static int pencode(char *s)
60 {
61         char *save;
62         int lev, fac = LOG_USER;
63
64         for (save = s; *s && *s != '.'; ++s)
65                 ;
66         if (*s) {
67                 *s = '\0';
68                 fac = decode(save, facilitynames);
69                 if (fac < 0)
70                         bb_error_msg_and_die("unknown %s name: %s", "facility", save);
71                 *s++ = '.';
72         } else {
73                 s = save;
74         }
75         lev = decode(s, prioritynames);
76         if (lev < 0)
77                 bb_error_msg_and_die("unknown %s name: %s", "priority", save);
78         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
79 }
80
81 #define strbuf bb_common_bufsiz1
82
83 int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84 int logger_main(int argc UNUSED_PARAM, char **argv)
85 {
86         char *str_p, *str_t;
87         int opt;
88         int i = 0;
89
90         /* Fill out the name string early (may be overwritten later) */
91         str_t = uid2uname_utoa(geteuid());
92
93         /* Parse any options */
94         opt = getopt32(argv, "p:st:", &str_p, &str_t);
95
96         if (opt & 0x2) /* -s */
97                 i |= LOG_PERROR;
98         //if (opt & 0x4) /* -t */
99         openlog(str_t, i, 0);
100         i = LOG_USER | LOG_NOTICE;
101         if (opt & 0x1) /* -p */
102                 i = pencode(str_p);
103
104         argv += optind;
105         if (!argv[0]) {
106                 while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
107                         if (strbuf[0]
108                          && NOT_LONE_CHAR(strbuf, '\n')
109                         ) {
110                                 /* Neither "" nor "\n" */
111                                 syslog(i, "%s", strbuf);
112                         }
113                 }
114         } else {
115                 char *message = NULL;
116                 int len = 0;
117                 int pos = 0;
118                 do {
119                         len += strlen(*argv) + 1;
120                         message = xrealloc(message, len + 1);
121                         sprintf(message + pos, " %s", *argv),
122                         pos = len;
123                 } while (*++argv);
124                 syslog(i, "%s", message + 1); /* skip leading " " */
125         }
126
127         closelog();
128         return EXIT_SUCCESS;
129 }
130
131 /* Clean up. Needed because we are included from syslogd_and_logger.c */
132 #undef strbuf
133
134 /*-
135  * Copyright (c) 1983, 1993
136  * The Regents of the University of California.  All rights reserved.
137  *
138  * This is the original license statement for the decode and pencode functions.
139  *
140  * Redistribution and use in source and binary forms, with or without
141  * modification, are permitted provided that the following conditions
142  * are met:
143  * 1. Redistributions of source code must retain the above copyright
144  *    notice, this list of conditions and the following disclaimer.
145  * 2. Redistributions in binary form must reproduce the above copyright
146  *    notice, this list of conditions and the following disclaimer in the
147  *    documentation and/or other materials provided with the distribution.
148  *
149  * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
150  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
151  *
152  * 4. Neither the name of the University nor the names of its contributors
153  *    may be used to endorse or promote products derived from this software
154  *    without specific prior written permission.
155  *
156  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
157  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
159  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
160  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
161  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
162  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
163  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
164  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
165  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
166  * SUCH DAMAGE.
167  */