logger.c 4.9 KB

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