logger.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, 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. argc -= optind;
  81. argv += optind;
  82. if (!argc) {
  83. while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
  84. if (strbuf[0]
  85. && NOT_LONE_CHAR(strbuf, '\n')
  86. ) {
  87. /* Neither "" nor "\n" */
  88. syslog(i, "%s", strbuf);
  89. }
  90. }
  91. } else {
  92. char *message = NULL;
  93. int len = 0;
  94. int pos = 0;
  95. do {
  96. len += strlen(*argv) + 1;
  97. message = xrealloc(message, len + 1);
  98. sprintf(message + pos, " %s", *argv),
  99. pos = len;
  100. } while (*++argv);
  101. syslog(i, "%s", message + 1); /* skip leading " " */
  102. }
  103. closelog();
  104. return EXIT_SUCCESS;
  105. }
  106. /* Clean up. Needed because we are included from syslogd_and_logger.c */
  107. #undef strbuf
  108. /*-
  109. * Copyright (c) 1983, 1993
  110. * The Regents of the University of California. All rights reserved.
  111. *
  112. * This is the original license statement for the decode and pencode functions.
  113. *
  114. * Redistribution and use in source and binary forms, with or without
  115. * modification, are permitted provided that the following conditions
  116. * are met:
  117. * 1. Redistributions of source code must retain the above copyright
  118. * notice, this list of conditions and the following disclaimer.
  119. * 2. Redistributions in binary form must reproduce the above copyright
  120. * notice, this list of conditions and the following disclaimer in the
  121. * documentation and/or other materials provided with the distribution.
  122. *
  123. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  124. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  125. *
  126. * 4. Neither the name of the University nor the names of its contributors
  127. * may be used to endorse or promote products derived from this software
  128. * without specific prior written permission.
  129. *
  130. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  131. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  132. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  133. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  134. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  135. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  136. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  137. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  138. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  139. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  140. * SUCH DAMAGE.
  141. */