syslog.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #if defined(LIBC_SCCS) && !defined(lint)
  34. static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
  35. #endif /* LIBC_SCCS and not lint */
  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. #if 0
  68. #include "syslog.h"
  69. #include "pathnames.h"
  70. #endif
  71. #include <sys/uio.h>
  72. #include <sys/wait.h>
  73. #include <netdb.h>
  74. #include <string.h>
  75. #include <time.h>
  76. #include <unistd.h>
  77. #include <errno.h>
  78. #include <stdarg.h>
  79. #include <paths.h>
  80. #include <stdio.h>
  81. #include <ctype.h>
  82. #include <signal.h>
  83. #if defined(_REENTRENT) || defined(_THREAD_SAFE)
  84. # include <pthread.h>
  85. extern int __writev( int, const struct iovec *, size_t);
  86. /* We need to initialize the mutex. For this we use a method provided
  87. by pthread function 'pthread_once'. For this we need a once block. */
  88. static pthread_once__t _once_block = pthread_once_init;
  89. /* This is the mutex which protects the global environment of simultaneous
  90. modifications. */
  91. static pthread_mutex_t _syslog_mutex;
  92. static void
  93. DEFUN_VOID(_init_syslog_mutex)
  94. {
  95. pthread_mutex_init(&_syslog_mutex, pthread_mutexattr_default);
  96. }
  97. # define LOCK() \
  98. do { pthread_once(&_once_block, _init_syslog_mutex);
  99. pthread_mutex_lock(&_syslog_mutex); } while (0)
  100. # define UNLOCK() pthread_mutex_unlock(&_syslog_mutex)
  101. #else /* !_REENTRENT && !_THREAD_SAFE */
  102. # define LOCK()
  103. # define UNLOCK()
  104. #endif /* _REENTRENT || _THREAD_SAFE */
  105. static int LogFile = -1; /* fd for log */
  106. static int connected; /* have done connect */
  107. static int LogStat = 0; /* status bits, set by openlog() */
  108. static const char *LogTag = "syslog"; /* string to tag the entry with */
  109. static int LogFacility = LOG_USER; /* default facility code */
  110. static int LogMask = 0xff; /* mask of priorities to be logged */
  111. static void closelog_intern( int );
  112. void syslog( int, const char *, ...);
  113. void vsyslog( int, const char *, va_list );
  114. void openlog( const char *, int, int );
  115. void closelog( void );
  116. int setlogmask( int );
  117. static void
  118. closelog_intern(int to_default)
  119. {
  120. LOCK();
  121. (void) close(LogFile);
  122. LogFile = -1;
  123. connected = 0;
  124. if (to_default)
  125. {
  126. LogStat = 0;
  127. LogTag = "syslog";
  128. LogFacility = LOG_USER;
  129. LogMask = 0xff;
  130. }
  131. UNLOCK();
  132. }
  133. static void
  134. sigpipe_handler (int sig)
  135. {
  136. closelog_intern (0);
  137. }
  138. /*
  139. * syslog, vsyslog --
  140. * print message on log file; output is intended for syslogd(8).
  141. */
  142. void
  143. syslog(int pri, const char *fmt, ...)
  144. {
  145. va_list ap;
  146. va_start(ap, fmt);
  147. vsyslog(pri, fmt, ap);
  148. va_end(ap);
  149. }
  150. void
  151. vsyslog( int pri, const char *fmt, va_list ap )
  152. {
  153. register char *p;
  154. char *last_chr, *head_end, *end, *stdp;
  155. time_t now;
  156. int fd, saved_errno;
  157. int rc;
  158. char tbuf[1024]; /* syslogd is unable to handle longer messages */
  159. struct sigaction action, oldaction;
  160. int sigpipe;
  161. memset (&action, 0, sizeof (action));
  162. action.sa_handler = sigpipe_handler;
  163. sigemptyset (&action.sa_mask);
  164. sigpipe = sigaction (SIGPIPE, &action, &oldaction);
  165. saved_errno = errno;
  166. LOCK();
  167. /* See if we should just throw out this message. */
  168. if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  169. goto getout;
  170. if (LogFile < 0 || !connected)
  171. openlog(LogTag, LogStat | LOG_NDELAY, 0);
  172. /* Set default facility if none specified. */
  173. if ((pri & LOG_FACMASK) == 0)
  174. pri |= LogFacility;
  175. /* Build the message. We know the starting part of the message can take
  176. * no longer than 64 characters plus length of the LogTag. So it's
  177. * safe to test only LogTag and use normal sprintf everywhere else.
  178. */
  179. (void)time(&now);
  180. stdp = p = tbuf + sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  181. if (LogTag) {
  182. if (strlen(LogTag) < sizeof(tbuf) - 64)
  183. p += sprintf(p, "%s", LogTag);
  184. else
  185. p += sprintf(p, "<BUFFER OVERRUN ATTEMPT>");
  186. }
  187. if (LogStat & LOG_PID)
  188. p += sprintf(p, "[%d]", getpid());
  189. if (LogTag) {
  190. *p++ = ':';
  191. *p++ = ' ';
  192. }
  193. head_end = p;
  194. /* We format the rest of the message. If the buffer becomes full, we mark
  195. * the message as truncated. Note that we require at least 2 free bytes
  196. * in the buffer as we might want to add "\r\n" there.
  197. */
  198. end = tbuf + sizeof(tbuf) - 1;
  199. errno = saved_errno;
  200. p += vsnprintf(p, end - p, fmt, ap);
  201. if (p >= end || p < head_end) { /* Returned -1 in case of error... */
  202. static char truncate_msg[12] = "[truncated] ";
  203. memmove(head_end + sizeof(truncate_msg), head_end,
  204. end - head_end - sizeof(truncate_msg));
  205. memcpy(head_end, truncate_msg, sizeof(truncate_msg));
  206. p = end - 1;
  207. }
  208. last_chr = p;
  209. /* Output to stderr if requested. */
  210. if (LogStat & LOG_PERROR) {
  211. *last_chr = '\n';
  212. (void)write(STDERR_FILENO, stdp, last_chr - stdp + 1);
  213. }
  214. /* Output the message to the local logger using NUL as a message delimiter. */
  215. p = tbuf;
  216. *last_chr = 0;
  217. do {
  218. rc = write(LogFile, p, last_chr + 1 - p);
  219. if (rc < 0) {
  220. if ((errno==EAGAIN) || (errno==EINTR))
  221. rc=0;
  222. else {
  223. closelog_intern(0);
  224. break;
  225. }
  226. }
  227. p+=rc;
  228. } while (p <= last_chr);
  229. if (rc >= 0)
  230. goto getout;
  231. /*
  232. * Output the message to the console; don't worry about blocking,
  233. * if console blocks everything will. Make sure the error reported
  234. * is the one from the syslogd failure.
  235. */
  236. /* should mode be `O_WRONLY | O_NOCTTY' ? -- Uli */
  237. if (LogStat & LOG_CONS &&
  238. (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  239. p = index(tbuf, '>') + 1;
  240. last_chr[0] = '\r';
  241. last_chr[1] = '\n';
  242. (void)write(fd, p, last_chr - p + 2);
  243. (void)close(fd);
  244. }
  245. getout:
  246. UNLOCK();
  247. if (sigpipe == 0)
  248. sigaction (SIGPIPE, &oldaction,
  249. (struct sigaction *) NULL);
  250. }
  251. static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
  252. /*
  253. * OPENLOG -- open system log
  254. */
  255. void
  256. openlog( const char *ident, int logstat, int logfac )
  257. {
  258. LOCK();
  259. if (ident != NULL)
  260. LogTag = ident;
  261. LogStat = logstat;
  262. if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  263. LogFacility = logfac;
  264. if (LogFile == -1) {
  265. SyslogAddr.sa_family = AF_UNIX;
  266. (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  267. sizeof(SyslogAddr.sa_data));
  268. if (LogStat & LOG_NDELAY) {
  269. if ((LogFile = socket(AF_UNIX, SOCK_STREAM, 0)) == -1){
  270. UNLOCK();
  271. return;
  272. }
  273. /* fcntl(LogFile, F_SETFD, 1); */
  274. }
  275. }
  276. if (LogFile != -1 && !connected &&
  277. #if 0
  278. connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family)+
  279. strlen(SyslogAddr.sa_data)) != -1)
  280. #else
  281. connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  282. sizeof(SyslogAddr.sa_data) +
  283. strlen(SyslogAddr.sa_data)) != -1)
  284. #endif
  285. connected = 1;
  286. UNLOCK();
  287. }
  288. /*
  289. * CLOSELOG -- close the system log
  290. */
  291. void
  292. closelog( void )
  293. {
  294. closelog_intern(1);
  295. }
  296. /*
  297. * SETLOGMASK -- set the log mask level
  298. */
  299. int
  300. setlogmask( int pmask )
  301. {
  302. int omask;
  303. omask = LogMask;
  304. LOCK();
  305. if (pmask != 0)
  306. LogMask = pmask;
  307. UNLOCK();
  308. return (omask);
  309. }