logger.c 4.4 KB

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