syslog.c 9.0 KB

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