3
0

logger.c 4.6 KB

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