source: MondoRescue/branches/2.2.9/mindi-busybox/sysklogd/logger.c@ 2887

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