3
0

logger.c 5.5 KB

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