syslog.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 __FORCE_GLIBC
  34. #define _GNU_SOURCE
  35. #include <features.h>
  36. /*
  37. * SYSLOG -- print message on log file
  38. *
  39. * This routine looks a lot like printf, except that it outputs to the
  40. * log file instead of the standard output. Also:
  41. * adds a timestamp,
  42. * prints the module name in front of the message,
  43. * has some other formatting types (or will sometime),
  44. * adds a newline on the end of the message.
  45. *
  46. * The output of this routine is intended to be read by syslogd(8).
  47. *
  48. * Author: Eric Allman
  49. * Modified to use UNIX domain IPC by Ralph Campbell
  50. * Patched March 12, 1996 by A. Ian Vogelesang <vogelesang@hdshq.com>
  51. * - to correct the handling of message & format string truncation,
  52. * - to visibly tag truncated records to facilitate
  53. * investigation of such Bad Things with grep, and,
  54. * - to correct the handling of case where "write"
  55. * returns after writing only part of the message.
  56. * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997
  57. * - better buffer overrun checks.
  58. * - special handling of "%m" removed as we use GNU sprintf which handles
  59. * it automatically.
  60. * - Major code cleanup.
  61. */
  62. #include <sys/types.h>
  63. #include <sys/socket.h>
  64. #include <sys/file.h>
  65. #include <sys/signal.h>
  66. #include <sys/syslog.h>
  67. #include <sys/uio.h>
  68. #include <sys/wait.h>
  69. #include <netdb.h>
  70. #include <string.h>
  71. #include <time.h>
  72. #include <unistd.h>
  73. #include <errno.h>
  74. #include <stdarg.h>
  75. #include <paths.h>
  76. #include <stdio.h>
  77. #include <ctype.h>
  78. #include <signal.h>
  79. #ifdef __UCLIBC_HAS_THREADS__
  80. #include <pthread.h>
  81. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  82. # define LOCK pthread_mutex_lock(&mylock)
  83. # define UNLOCK pthread_mutex_unlock(&mylock);
  84. #else
  85. # define LOCK
  86. # define UNLOCK
  87. #endif
  88. static int LogFile = -1; /* fd for log */
  89. static int connected; /* have done connect */
  90. static int LogStat = 0; /* status bits, set by openlog() */
  91. static const char *LogTag = "syslog"; /* string to tag the entry with */
  92. static int LogFacility = LOG_USER; /* default facility code */
  93. static int LogMask = 0xff; /* mask of priorities to be logged */
  94. static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
  95. static void closelog_intern( int );
  96. void syslog( int, const char *, ...);
  97. void vsyslog( int, const char *, va_list );
  98. void openlog( const char *, int, int );
  99. void closelog( void );
  100. int setlogmask(int pmask);
  101. static void
  102. closelog_intern(int to_default)
  103. {
  104. LOCK;
  105. (void) close(LogFile);
  106. LogFile = -1;
  107. connected = 0;
  108. if (to_default)
  109. {
  110. LogStat = 0;
  111. LogTag = "syslog";
  112. LogFacility = LOG_USER;
  113. LogMask = 0xff;
  114. }
  115. UNLOCK;
  116. }
  117. static void
  118. sigpipe_handler (int sig)
  119. {
  120. closelog_intern (0);
  121. }
  122. /*
  123. * syslog, vsyslog --
  124. * print message on log file; output is intended for syslogd(8).
  125. */
  126. void
  127. syslog(int pri, const char *fmt, ...)
  128. {
  129. va_list ap;
  130. va_start(ap, fmt);
  131. vsyslog(pri, fmt, ap);
  132. va_end(ap);
  133. }
  134. void
  135. vsyslog( int pri, const char *fmt, va_list ap )
  136. {
  137. register char *p;
  138. char *last_chr, *head_end, *end, *stdp;
  139. time_t now;
  140. int fd, saved_errno;
  141. int rc;
  142. char tbuf[1024]; /* syslogd is unable to handle longer messages */
  143. struct sigaction action, oldaction;
  144. int sigpipe;
  145. memset (&action, 0, sizeof (action));
  146. action.sa_handler = sigpipe_handler;
  147. sigemptyset (&action.sa_mask);
  148. sigpipe = sigaction (SIGPIPE, &action, &oldaction);
  149. saved_errno = errno;
  150. LOCK;
  151. /* See if we should just throw out this message. */
  152. if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  153. goto getout;
  154. if (LogFile < 0 || !connected)
  155. openlog(LogTag, LogStat | LOG_NDELAY, 0);
  156. /* Set default facility if none specified. */
  157. if ((pri & LOG_FACMASK) == 0)
  158. pri |= LogFacility;
  159. /* Build the message. We know the starting part of the message can take
  160. * no longer than 64 characters plus length of the LogTag. So it's
  161. * safe to test only LogTag and use normal sprintf everywhere else.
  162. */
  163. (void)time(&now);
  164. stdp = p = tbuf + sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  165. if (LogTag) {
  166. if (strlen(LogTag) < sizeof(tbuf) - 64)
  167. p += sprintf(p, "%s", LogTag);
  168. else
  169. p += sprintf(p, "<BUFFER OVERRUN ATTEMPT>");
  170. }
  171. if (LogStat & LOG_PID)
  172. p += sprintf(p, "[%d]", getpid());
  173. if (LogTag) {
  174. *p++ = ':';
  175. *p++ = ' ';
  176. }
  177. head_end = p;
  178. /* We format the rest of the message. If the buffer becomes full, we mark
  179. * the message as truncated. Note that we require at least 2 free bytes
  180. * in the buffer as we might want to add "\r\n" there.
  181. */
  182. end = tbuf + sizeof(tbuf) - 1;
  183. __set_errno(saved_errno);
  184. p += vsnprintf(p, end - p, fmt, ap);
  185. if (p >= end || p < head_end) { /* Returned -1 in case of error... */
  186. static const char truncate_msg[12] = "[truncated] ";
  187. memmove(head_end + sizeof(truncate_msg), head_end,
  188. end - head_end - sizeof(truncate_msg));
  189. memcpy(head_end, truncate_msg, sizeof(truncate_msg));
  190. p = end - 1;
  191. }
  192. last_chr = p;
  193. /* Output to stderr if requested. */
  194. if (LogStat & LOG_PERROR) {
  195. *last_chr = '\n';
  196. (void)write(STDERR_FILENO, stdp, last_chr - stdp + 1);
  197. }
  198. /* Output the message to the local logger using NUL as a message delimiter. */
  199. p = tbuf;
  200. *last_chr = 0;
  201. do {
  202. rc = write(LogFile, p, last_chr + 1 - p);
  203. if (rc < 0) {
  204. if ((errno==EAGAIN) || (errno==EINTR))
  205. rc=0;
  206. else {
  207. closelog_intern(0);
  208. break;
  209. }
  210. }
  211. p+=rc;
  212. } while (p <= last_chr);
  213. if (rc >= 0)
  214. goto getout;
  215. /*
  216. * Output the message to the console; don't worry about blocking,
  217. * if console blocks everything will. Make sure the error reported
  218. * is the one from the syslogd failure.
  219. */
  220. /* should mode be `O_WRONLY | O_NOCTTY' ? -- Uli */
  221. if (LogStat & LOG_CONS &&
  222. (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  223. p = index(tbuf, '>') + 1;
  224. last_chr[0] = '\r';
  225. last_chr[1] = '\n';
  226. (void)write(fd, p, last_chr - p + 2);
  227. (void)close(fd);
  228. }
  229. getout:
  230. UNLOCK;
  231. if (sigpipe == 0)
  232. sigaction (SIGPIPE, &oldaction,
  233. (struct sigaction *) NULL);
  234. }
  235. /*
  236. * OPENLOG -- open system log
  237. */
  238. void
  239. openlog( const char *ident, int logstat, int logfac )
  240. {
  241. int logType = SOCK_DGRAM;
  242. LOCK;
  243. if (ident != NULL)
  244. LogTag = ident;
  245. LogStat = logstat;
  246. if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  247. LogFacility = logfac;
  248. if (LogFile == -1) {
  249. SyslogAddr.sa_family = AF_UNIX;
  250. (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  251. sizeof(SyslogAddr.sa_data));
  252. retry:
  253. if (LogStat & LOG_NDELAY) {
  254. if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
  255. UNLOCK;
  256. return;
  257. }
  258. /* fcntl(LogFile, F_SETFD, 1); */
  259. }
  260. }
  261. if (LogFile != -1 && !connected &&
  262. #if 0
  263. connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family)+
  264. strlen(SyslogAddr.sa_data)) != -1
  265. #else
  266. connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  267. sizeof(SyslogAddr.sa_data) +
  268. strlen(SyslogAddr.sa_data)) != -1
  269. #endif
  270. )
  271. {
  272. connected = 1;
  273. } else if (logType == SOCK_DGRAM) {
  274. logType = SOCK_STREAM;
  275. goto retry;
  276. }
  277. UNLOCK;
  278. }
  279. /*
  280. * CLOSELOG -- close the system log
  281. */
  282. void
  283. closelog( void )
  284. {
  285. closelog_intern(1);
  286. }
  287. /* setlogmask -- set the log mask level */
  288. int setlogmask(int pmask)
  289. {
  290. int omask;
  291. omask = LogMask;
  292. LOCK;
  293. if (pmask != 0)
  294. LogMask = pmask;
  295. UNLOCK;
  296. return (omask);
  297. }