syslog.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 1983, 1988, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #define time __time
  34. #define ctime __ctime
  35. #define sigaction __sigaction_internal
  36. #define __FORCE_GLIBC
  37. #define _GNU_SOURCE
  38. #include <features.h>
  39. /*
  40. * SYSLOG -- print message on log file
  41. *
  42. * This routine looks a lot like printf, except that it outputs to the
  43. * log file instead of the standard output. Also:
  44. * adds a timestamp,
  45. * prints the module name in front of the message,
  46. * has some other formatting types (or will sometime),
  47. * adds a newline on the end of the message.
  48. *
  49. * The output of this routine is intended to be read by syslogd(8).
  50. *
  51. * Author: Eric Allman
  52. * Modified to use UNIX domain IPC by Ralph Campbell
  53. * Patched March 12, 1996 by A. Ian Vogelesang <vogelesang@hdshq.com>
  54. * - to correct the handling of message & format string truncation,
  55. * - to visibly tag truncated records to facilitate
  56. * investigation of such Bad Things with grep, and,
  57. * - to correct the handling of case where "write"
  58. * returns after writing only part of the message.
  59. * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997
  60. * - better buffer overrun checks.
  61. * - special handling of "%m" removed as we use GNU sprintf which handles
  62. * it automatically.
  63. * - Major code cleanup.
  64. */
  65. #include <sys/types.h>
  66. #include <sys/socket.h>
  67. #include <sys/file.h>
  68. #include <sys/signal.h>
  69. #include <sys/syslog.h>
  70. #include <sys/uio.h>
  71. #include <sys/wait.h>
  72. #include <netdb.h>
  73. #include <string.h>
  74. #include <time.h>
  75. #include <unistd.h>
  76. #include <errno.h>
  77. #include <stdarg.h>
  78. #include <paths.h>
  79. #include <stdio.h>
  80. #include <ctype.h>
  81. #include <signal.h>
  82. #ifdef __UCLIBC_HAS_THREADS__
  83. #include <pthread.h>
  84. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  85. # define LOCK __pthread_mutex_lock(&mylock)
  86. # define UNLOCK __pthread_mutex_unlock(&mylock);
  87. #else
  88. # define LOCK
  89. # define UNLOCK
  90. #endif
  91. static int LogFile = -1; /* fd for log */
  92. static int connected; /* have done connect */
  93. static int LogStat = 0; /* status bits, set by openlog() */
  94. static const char *LogTag = "syslog"; /* string to tag the entry with */
  95. static int LogFacility = LOG_USER; /* default facility code */
  96. static int LogMask = 0xff; /* mask of priorities to be logged */
  97. static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
  98. static void closelog_intern( int );
  99. void syslog( int, const char *, ...);
  100. void vsyslog( int, const char *, va_list );
  101. void openlog( const char *, int, int );
  102. void closelog( void );
  103. int setlogmask(int pmask);
  104. static void
  105. closelog_intern(int to_default)
  106. {
  107. LOCK;
  108. if (LogFile != -1) {
  109. (void) __close(LogFile);
  110. }
  111. LogFile = -1;
  112. connected = 0;
  113. if (to_default)
  114. {
  115. LogStat = 0;
  116. LogTag = "syslog";
  117. LogFacility = LOG_USER;
  118. LogMask = 0xff;
  119. }
  120. UNLOCK;
  121. }
  122. static void
  123. sigpipe_handler (int sig)
  124. {
  125. closelog_intern (0);
  126. }
  127. /*
  128. * syslog, vsyslog --
  129. * print message on log file; output is intended for syslogd(8).
  130. */
  131. void attribute_hidden
  132. __vsyslog( int pri, const char *fmt, va_list ap )
  133. {
  134. register char *p;
  135. char *last_chr, *head_end, *end, *stdp;
  136. time_t now;
  137. int fd, saved_errno;
  138. int rc;
  139. char tbuf[1024]; /* syslogd is unable to handle longer messages */
  140. struct sigaction action, oldaction;
  141. int sigpipe;
  142. __memset (&action, 0, sizeof (action));
  143. action.sa_handler = sigpipe_handler;
  144. sigemptyset (&action.sa_mask);
  145. sigpipe = sigaction (SIGPIPE, &action, &oldaction);
  146. saved_errno = errno;
  147. LOCK;
  148. /* See if we should just throw out this message. */
  149. if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  150. goto getout;
  151. if (LogFile < 0 || !connected)
  152. openlog(LogTag, LogStat | LOG_NDELAY, 0);
  153. /* Set default facility if none specified. */
  154. if ((pri & LOG_FACMASK) == 0)
  155. pri |= LogFacility;
  156. /* Build the message. We know the starting part of the message can take
  157. * no longer than 64 characters plus length of the LogTag. So it's
  158. * safe to test only LogTag and use normal sprintf everywhere else.
  159. */
  160. (void)time(&now);
  161. stdp = p = tbuf + sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  162. if (LogTag) {
  163. if (__strlen(LogTag) < sizeof(tbuf) - 64)
  164. p += sprintf(p, "%s", LogTag);
  165. else
  166. p += sprintf(p, "<BUFFER OVERRUN ATTEMPT>");
  167. }
  168. if (LogStat & LOG_PID)
  169. p += sprintf(p, "[%d]", __getpid());
  170. if (LogTag) {
  171. *p++ = ':';
  172. *p++ = ' ';
  173. }
  174. head_end = p;
  175. /* We format the rest of the message. If the buffer becomes full, we mark
  176. * the message as truncated. Note that we require at least 2 free bytes
  177. * in the buffer as we might want to add "\r\n" there.
  178. */
  179. end = tbuf + sizeof(tbuf) - 1;
  180. __set_errno(saved_errno);
  181. p += vsnprintf(p, end - p, fmt, ap);
  182. if (p >= end || p < head_end) { /* Returned -1 in case of error... */
  183. static const char truncate_msg[12] = "[truncated] ";
  184. __memmove(head_end + sizeof(truncate_msg), head_end,
  185. end - head_end - sizeof(truncate_msg));
  186. __memcpy(head_end, truncate_msg, sizeof(truncate_msg));
  187. if (p < head_end) {
  188. while (p < end && *p) {
  189. p++;
  190. }
  191. }
  192. else {
  193. p = end - 1;
  194. }
  195. }
  196. last_chr = p;
  197. /* Output to stderr if requested. */
  198. if (LogStat & LOG_PERROR) {
  199. *last_chr = '\n';
  200. (void)__write(STDERR_FILENO, stdp, last_chr - stdp + 1);
  201. }
  202. /* Output the message to the local logger using NUL as a message delimiter. */
  203. p = tbuf;
  204. *last_chr = 0;
  205. do {
  206. rc = __write(LogFile, p, last_chr + 1 - p);
  207. if (rc < 0) {
  208. if ((errno==EAGAIN) || (errno==EINTR))
  209. rc=0;
  210. else {
  211. closelog_intern(0);
  212. break;
  213. }
  214. }
  215. p+=rc;
  216. } while (p <= last_chr);
  217. if (rc >= 0)
  218. goto getout;
  219. /*
  220. * Output the message to the console; don't worry about blocking,
  221. * if console blocks everything will. Make sure the error reported
  222. * is the one from the syslogd failure.
  223. */
  224. /* should mode be `O_WRONLY | O_NOCTTY' ? -- Uli */
  225. if (LogStat & LOG_CONS &&
  226. (fd = __open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  227. p = __strchr(tbuf, '>') + 1;
  228. last_chr[0] = '\r';
  229. last_chr[1] = '\n';
  230. (void)__write(fd, p, last_chr - p + 2);
  231. (void)__close(fd);
  232. }
  233. getout:
  234. UNLOCK;
  235. if (sigpipe == 0)
  236. sigaction (SIGPIPE, &oldaction,
  237. (struct sigaction *) NULL);
  238. }
  239. strong_alias(__vsyslog,vsyslog)
  240. void attribute_hidden
  241. __syslog(int pri, const char *fmt, ...)
  242. {
  243. va_list ap;
  244. va_start(ap, fmt);
  245. __vsyslog(pri, fmt, ap);
  246. va_end(ap);
  247. }
  248. strong_alias(__syslog,syslog)
  249. /*
  250. * OPENLOG -- open system log
  251. */
  252. void attribute_hidden
  253. __openlog( const char *ident, int logstat, int logfac )
  254. {
  255. int logType = SOCK_DGRAM;
  256. LOCK;
  257. if (ident != NULL)
  258. LogTag = ident;
  259. LogStat = logstat;
  260. if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  261. LogFacility = logfac;
  262. if (LogFile == -1) {
  263. SyslogAddr.sa_family = AF_UNIX;
  264. (void)__strncpy(SyslogAddr.sa_data, _PATH_LOG,
  265. sizeof(SyslogAddr.sa_data));
  266. retry:
  267. if (LogStat & LOG_NDELAY) {
  268. if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
  269. UNLOCK;
  270. return;
  271. }
  272. /* fcntl(LogFile, F_SETFD, 1); */
  273. }
  274. }
  275. if (LogFile != -1 && !connected) {
  276. if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  277. sizeof(SyslogAddr.sa_data) + __strlen(SyslogAddr.sa_data)) != -1)
  278. {
  279. connected = 1;
  280. } else if (logType == SOCK_DGRAM) {
  281. logType = SOCK_STREAM;
  282. if (LogFile != -1) {
  283. __close(LogFile);
  284. LogFile = -1;
  285. }
  286. goto retry;
  287. } else {
  288. if (LogFile != -1) {
  289. __close(LogFile);
  290. LogFile = -1;
  291. }
  292. }
  293. }
  294. UNLOCK;
  295. }
  296. strong_alias(__openlog,openlog)
  297. /*
  298. * CLOSELOG -- close the system log
  299. */
  300. void attribute_hidden
  301. __closelog( void )
  302. {
  303. closelog_intern(1);
  304. }
  305. strong_alias(__closelog,closelog)
  306. /* setlogmask -- set the log mask level */
  307. int setlogmask(int pmask)
  308. {
  309. int omask;
  310. omask = LogMask;
  311. LOCK;
  312. if (pmask != 0)
  313. LogMask = pmask;
  314. UNLOCK;
  315. return (omask);
  316. }