3
0

logger.c 5.4 KB

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