syslog.c 9.2 KB

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