logger.c 4.5 KB

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