3
0

logger.c 4.9 KB

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