syslog.c 10 KB

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