3
0

logger.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "libbb.h"
  10. #ifndef CONFIG_SYSLOGD
  11. #define SYSLOG_NAMES
  12. #define SYSLOG_NAMES_CONST
  13. #include <syslog.h>
  14. #else
  15. /* brokenness alert. Everybody except dietlibc get's this wrong by neither
  16. * providing a typedef nor an extern for facilitynames and prioritynames
  17. * in syslog.h.
  18. */
  19. # include <syslog.h>
  20. # ifndef __dietlibc__
  21. /* We have to do this since the header file does neither provide a sane type
  22. * for this structure nor extern definitions. Argh.... bad libc, bad, bad...
  23. */
  24. typedef struct _code {
  25. char *c_name; /* FIXME: this should be const char *const c_name ! */
  26. int c_val;
  27. } CODE;
  28. # ifdef __UCLIBC__
  29. extern const CODE prioritynames[];
  30. extern const CODE facilitynames[];
  31. # else
  32. extern CODE prioritynames[];
  33. extern CODE facilitynames[];
  34. # endif
  35. # endif
  36. #endif
  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, 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, 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. int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int logger_main(int argc, char **argv)
  85. {
  86. char *str_p, *str_t;
  87. int i = 0;
  88. char name[80];
  89. /* Fill out the name string early (may be overwritten later) */
  90. bb_getpwuid(name, sizeof(name), geteuid());
  91. str_t = name;
  92. /* Parse any options */
  93. getopt32(argv, "p:st:", &str_p, &str_t);
  94. if (option_mask32 & 0x2) /* -s */
  95. i |= LOG_PERROR;
  96. //if (option_mask32 & 0x4) /* -t */
  97. openlog(str_t, i, 0);
  98. i = LOG_USER | LOG_NOTICE;
  99. if (option_mask32 & 0x1) /* -p */
  100. i = pencode(str_p);
  101. argc -= optind;
  102. argv += optind;
  103. if (!argc) {
  104. #define strbuf bb_common_bufsiz1
  105. while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
  106. if (strbuf[0]
  107. && NOT_LONE_CHAR(strbuf, '\n')
  108. ) {
  109. /* Neither "" nor "\n" */
  110. syslog(i, "%s", strbuf);
  111. }
  112. }
  113. } else {
  114. char *message = NULL;
  115. int len = 0;
  116. int pos = 0;
  117. do {
  118. len += strlen(*argv) + 1;
  119. message = xrealloc(message, len + 1);
  120. sprintf(message + pos, " %s", *argv),
  121. pos = len;
  122. } while (*++argv);
  123. syslog(i, "%s", message + 1); /* skip leading " " */
  124. }
  125. closelog();
  126. return EXIT_SUCCESS;
  127. }
  128. /*-
  129. * Copyright (c) 1983, 1993
  130. * The Regents of the University of California. All rights reserved.
  131. *
  132. * This is the original license statement for the decode and pencode functions.
  133. *
  134. * Redistribution and use in source and binary forms, with or without
  135. * modification, are permitted provided that the following conditions
  136. * are met:
  137. * 1. Redistributions of source code must retain the above copyright
  138. * notice, this list of conditions and the following disclaimer.
  139. * 2. Redistributions in binary form must reproduce the above copyright
  140. * notice, this list of conditions and the following disclaimer in the
  141. * documentation and/or other materials provided with the distribution.
  142. *
  143. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  144. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  145. *
  146. * 4. Neither the name of the University nor the names of its contributors
  147. * may be used to endorse or promote products derived from this software
  148. * without specific prior written permission.
  149. *
  150. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  151. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  152. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  153. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  154. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  155. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  156. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  157. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  158. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  159. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  160. * SUCH DAMAGE.
  161. */