logger.c 4.6 KB

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