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