source: MondoRescue/branches/3.3/mindi-busybox/sysklogd/logger.c

Last change on this file was 3621, checked in by Bruno Cornec, 7 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

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