syslog.c 9.5 KB

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