syslog.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <sys/file.h>
  62. #include <sys/signal.h>
  63. #include <sys/syslog.h>
  64. #include <sys/uio.h>
  65. #include <sys/wait.h>
  66. #include <netdb.h>
  67. #include <string.h>
  68. #include <time.h>
  69. #include <unistd.h>
  70. #include <errno.h>
  71. #include <stdarg.h>
  72. #include <paths.h>
  73. #include <stdio.h>
  74. #include <ctype.h>
  75. #include <signal.h>
  76. #include <bits/uClibc_mutex.h>
  77. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  78. /* !glibc_compat: glibc uses argv[0] by default
  79. * (default: if there was no openlog or if openlog passed NULL),
  80. * not string "syslog"
  81. */
  82. static const char *LogTag = "syslog"; /* string to tag the entry with */
  83. static int LogFile = -1; /* fd for log */
  84. static smalluint connected; /* have done connect */
  85. /* all bits in option argument for openlog fit in 8 bits */
  86. static smalluint LogStat = 0; /* status bits, set by openlog */
  87. /* default facility code if openlog is not called */
  88. /* (this fits in 8 bits even without >> 3 shift, but playing extra safe) */
  89. static smalluint LogFacility = LOG_USER >> 3;
  90. /* bits mask of priorities to be logged (eight prios - 8 bits is enough) */
  91. static smalluint LogMask = 0xff;
  92. /* AF_UNIX address of local logger (we use struct sockaddr
  93. * instead of struct sockaddr_un since "/dev/log" is small enough) */
  94. static const struct sockaddr SyslogAddr = {
  95. .sa_family = AF_UNIX, /* sa_family_t (usually a short) */
  96. .sa_data = _PATH_LOG /* char [14] */
  97. };
  98. static void
  99. closelog_intern(int sig)
  100. {
  101. /* mylock must be held by the caller */
  102. if (LogFile != -1) {
  103. (void) close(LogFile);
  104. }
  105. LogFile = -1;
  106. connected = 0;
  107. if (sig == 0) { /* called from closelog()? - reset to defaults */
  108. LogStat = 0;
  109. LogTag = "syslog";
  110. LogFacility = LOG_USER >> 3;
  111. LogMask = 0xff;
  112. }
  113. }
  114. static void
  115. openlog_intern(const char *ident, int logstat, int logfac)
  116. {
  117. int fd;
  118. int logType = SOCK_DGRAM;
  119. if (ident != NULL)
  120. LogTag = ident;
  121. LogStat = logstat;
  122. /* (we were checking also for logfac != 0, but it breaks
  123. * openlog(xx, LOG_KERN) since LOG_KERN == 0) */
  124. if ((logfac & ~LOG_FACMASK) == 0) /* if we don't have invalid bits */
  125. LogFacility = (unsigned)logfac >> 3;
  126. fd = LogFile;
  127. if (fd == -1) {
  128. retry:
  129. if (logstat & LOG_NDELAY) {
  130. LogFile = fd = socket(AF_UNIX, logType, 0);
  131. if (fd == -1) {
  132. return;
  133. }
  134. fcntl(fd, F_SETFD, FD_CLOEXEC);
  135. /* We don't want to block if e.g. syslogd is SIGSTOPed */
  136. fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
  137. }
  138. }
  139. if (fd != -1 && !connected) {
  140. if (connect(fd, &SyslogAddr, sizeof(SyslogAddr)) != -1) {
  141. connected = 1;
  142. } else {
  143. if (fd != -1) {
  144. close(fd);
  145. LogFile = fd = -1;
  146. }
  147. if (logType == SOCK_DGRAM) {
  148. logType = SOCK_STREAM;
  149. goto retry;
  150. }
  151. }
  152. }
  153. }
  154. /*
  155. * OPENLOG -- open system log
  156. */
  157. void
  158. openlog(const char *ident, int logstat, int logfac)
  159. {
  160. __UCLIBC_MUTEX_LOCK(mylock);
  161. openlog_intern(ident, logstat, logfac);
  162. __UCLIBC_MUTEX_UNLOCK(mylock);
  163. }
  164. /*
  165. * syslog, vsyslog --
  166. * print message on log file; output is intended for syslogd(8).
  167. */
  168. static
  169. #ifndef __USE_BSD
  170. __always_inline
  171. #endif
  172. void
  173. __vsyslog(int pri, const char *fmt, va_list ap)
  174. {
  175. register char *p;
  176. char *last_chr, *head_end, *end, *stdp;
  177. time_t now;
  178. int fd, saved_errno;
  179. int rc;
  180. char tbuf[1024]; /* syslogd is unable to handle longer messages */
  181. /* Just throw out this message if pri has bad bits. */
  182. if ((pri & ~(LOG_PRIMASK|LOG_FACMASK)) != 0)
  183. return;
  184. saved_errno = errno;
  185. __UCLIBC_MUTEX_LOCK(mylock);
  186. /* See if we should just throw out this message according to LogMask. */
  187. if ((LogMask & LOG_MASK(LOG_PRI(pri))) == 0)
  188. goto getout;
  189. if (LogFile < 0 || !connected)
  190. openlog_intern(NULL, LogStat | LOG_NDELAY, (int)LogFacility << 3);
  191. /* Set default facility if none specified. */
  192. if ((pri & LOG_FACMASK) == 0)
  193. pri |= ((int)LogFacility << 3);
  194. /* Build the message. We know the starting part of the message can take
  195. * no longer than 64 characters plus length of the LogTag. So it's
  196. * safe to test only LogTag and use normal sprintf everywhere else.
  197. */
  198. (void)time(&now);
  199. stdp = p = tbuf + sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  200. /*if (LogTag) - always true */ {
  201. if (strlen(LogTag) < sizeof(tbuf) - 64)
  202. p += sprintf(p, "%s", LogTag);
  203. else
  204. p += sprintf(p, "<BUFFER OVERRUN ATTEMPT>");
  205. }
  206. if (LogStat & LOG_PID)
  207. p += sprintf(p, "[%d]", getpid());
  208. /*if (LogTag) - always true */ {
  209. *p++ = ':';
  210. *p++ = ' ';
  211. }
  212. head_end = p;
  213. /* We format the rest of the message. If the buffer becomes full, we mark
  214. * the message as truncated. Note that we require at least 2 free bytes
  215. * in the buffer as we might want to add "\r\n" there.
  216. */
  217. end = tbuf + sizeof(tbuf) - 1;
  218. __set_errno(saved_errno);
  219. p += vsnprintf(p, end - p, fmt, ap);
  220. if (p >= end || p < head_end) { /* Returned -1 in case of error... */
  221. static const char truncate_msg[12] = "[truncated] "; /* no NUL! */
  222. memmove(head_end + sizeof(truncate_msg), head_end,
  223. end - head_end - sizeof(truncate_msg));
  224. memcpy(head_end, truncate_msg, sizeof(truncate_msg));
  225. if (p < head_end) {
  226. while (p < end && *p) {
  227. p++;
  228. }
  229. }
  230. else {
  231. p = end - 1;
  232. }
  233. }
  234. last_chr = p;
  235. /* Output to stderr if requested. */
  236. if (LogStat & LOG_PERROR) {
  237. *last_chr = '\n';
  238. (void)write(STDERR_FILENO, stdp, last_chr - stdp + 1);
  239. }
  240. /* Output the message to the local logger using NUL as a message delimiter. */
  241. p = tbuf;
  242. *last_chr = '\0';
  243. if (LogFile >= 0) {
  244. do {
  245. /* can't just use write, it can result in SIGPIPE */
  246. rc = send(LogFile, p, last_chr + 1 - p, MSG_NOSIGNAL);
  247. if (rc < 0) {
  248. /* I don't think looping forever on EAGAIN is a good idea.
  249. * Imagine that syslogd is SIGSTOPed... */
  250. if (/* (errno != EAGAIN) && */ (errno != EINTR)) {
  251. closelog_intern(1); /* 1: do not reset LogXXX globals to default */
  252. goto write_err;
  253. }
  254. rc = 0;
  255. }
  256. p += rc;
  257. } while (p <= last_chr);
  258. goto getout;
  259. }
  260. write_err:
  261. /*
  262. * Output the message to the console; don't worry about blocking,
  263. * if console blocks everything will. Make sure the error reported
  264. * is the one from the syslogd failure.
  265. */
  266. /* should mode be O_WRONLY | O_NOCTTY? -- Uli */
  267. /* yes, but in Linux "/dev/console" never becomes ctty anyway -- vda */
  268. if ((LogStat & LOG_CONS) &&
  269. (fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY)) >= 0) {
  270. p = strchr(tbuf, '>') + 1;
  271. last_chr[0] = '\r';
  272. last_chr[1] = '\n';
  273. (void)write(fd, p, last_chr - p + 2);
  274. (void)close(fd);
  275. }
  276. getout:
  277. __UCLIBC_MUTEX_UNLOCK(mylock);
  278. }
  279. #ifdef __USE_BSD
  280. strong_alias(__vsyslog,vsyslog)
  281. #endif
  282. void
  283. syslog(int pri, const char *fmt, ...)
  284. {
  285. va_list ap;
  286. va_start(ap, fmt);
  287. __vsyslog(pri, fmt, ap);
  288. va_end(ap);
  289. }
  290. libc_hidden_def(syslog)
  291. /*
  292. * CLOSELOG -- close the system log
  293. */
  294. void
  295. closelog(void)
  296. {
  297. __UCLIBC_MUTEX_LOCK(mylock);
  298. closelog_intern(0); /* 0: reset LogXXX globals to default */
  299. __UCLIBC_MUTEX_UNLOCK(mylock);
  300. }
  301. /* setlogmask -- set the log mask level */
  302. int setlogmask(int pmask)
  303. {
  304. int omask;
  305. omask = LogMask;
  306. if (pmask != 0) {
  307. /* __UCLIBC_MUTEX_LOCK(mylock);*/
  308. LogMask = pmask;
  309. /* __UCLIBC_MUTEX_UNLOCK(mylock);*/
  310. }
  311. return omask;
  312. }