home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / misc / syslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  6.7 KB  |  264 lines

  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. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)syslog.c    8.4 (Berkeley) 3/18/94";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. /*
  39.  * SYSLOG -- print message on log file
  40.  *
  41.  * This routine looks a lot like printf, except that it outputs to the
  42.  * log file instead of the standard output.  Also:
  43.  *    adds a timestamp,
  44.  *    prints the module name in front of the message,
  45.  *    has some other formatting types (or will sometime),
  46.  *    adds a newline on the end of the message.
  47.  *
  48.  * The output of this routine is intended to be read by syslogd(8).
  49.  *
  50.  * Author: Eric Allman
  51.  * Modified to use UNIX domain IPC by Ralph Campbell
  52.  */
  53.  
  54. #include <sys/types.h>
  55. #include <sys/socket.h>
  56. #include <sys/file.h>
  57. #include <sys/signal.h>
  58. #include <sys/syslog.h>
  59. #if 0
  60. #include "syslog.h"
  61. #include "pathnames.h"
  62. #endif
  63.  
  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.  
  75. static int    LogFile = -1;        /* fd for log */
  76. static int    connected;        /* have done connect */
  77. static int    LogStat = 0;        /* status bits, set by openlog() */
  78. static const char *LogTag = "syslog";    /* string to tag the entry with */
  79. static int    LogFacility = LOG_USER;    /* default facility code */
  80.  
  81. /*
  82.  * syslog, vsyslog --
  83.  *     print message on log file; output is intended for syslogd(8).
  84.  */
  85. void
  86. syslog(int pri, const char *fmt, ...)
  87. {
  88.     va_list ap;
  89.  
  90.     va_start(ap, fmt);
  91.     vsyslog(pri, fmt, ap);
  92.     va_end(ap);
  93. }
  94.  
  95. void
  96. vsyslog(pri, fmt, ap)
  97.     int pri;
  98.     const char *fmt;
  99.     va_list ap;
  100. {
  101.     register int cnt;
  102.     register char *p;
  103.     time_t now;
  104.     int fd, saved_errno;
  105.     char tbuf[2048], fmt_cpy[1024], *stdp;
  106.  
  107.     saved_errno = errno;
  108.  
  109.     /* See if we should just throw out this message. */
  110.     if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  111.         return;
  112.     if (LogFile < 0 || !connected)
  113.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  114.  
  115.     /* Set default facility if none specified. */
  116.     if ((pri & LOG_FACMASK) == 0)
  117.         pri |= LogFacility;
  118.  
  119.     /* Build the message. */
  120.     (void)time(&now);
  121.     (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  122.     for (p = tbuf; *p; ++p);
  123.     if (LogStat & LOG_PERROR)
  124.         stdp = p;
  125.     if (LogTag) {
  126.         (void)strcpy(p, LogTag);
  127.         for (; *p; ++p);
  128.     }
  129.     if (LogStat & LOG_PID) {
  130.         (void)sprintf(p, "[%d]", getpid());
  131.         for (; *p; ++p);
  132.     }
  133.     if (LogTag) {
  134.         *p++ = ':';
  135.         *p++ = ' ';
  136.     }
  137.  
  138.     /* Substitute error message for %m. */
  139.     {
  140.         register char ch, *t1, *t2;
  141.         char *strerror();
  142.  
  143.         for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  144.             if (ch == '%' && fmt[1] == 'm') {
  145.                 ++fmt;
  146.                 t1 += sprintf(t1, "%s", strerror(saved_errno));
  147.             }
  148.             else
  149.                 *t1++ = ch;
  150.         *t1 = '\0';
  151.     }
  152.  
  153.     p += vsprintf(p, fmt_cpy, ap);
  154.     cnt = p - tbuf;
  155.  
  156.     /* Output to stderr if requested. */
  157.     if (LogStat & LOG_PERROR) {
  158.         struct iovec iov[2];
  159.         register struct iovec *v = iov;
  160.  
  161.         v->iov_base = stdp;
  162.         v->iov_len = cnt - (stdp - tbuf);
  163.         ++v;
  164.         v->iov_base = "\n";
  165.         v->iov_len = 1;
  166.         (void)writev(STDERR_FILENO, iov, 2);
  167.     }
  168.  
  169.     /* Output the message to the local logger. */
  170. #if 1
  171.     /* Use NUL as a message delimiter. */
  172.     if (write(LogFile, tbuf, cnt + 1) >= 1)
  173.     {
  174.         return;
  175.     }
  176.     else
  177.     {
  178.         /* If the write fails, we try to reconect it next
  179.          * time. */
  180.         closelog ();
  181.     }
  182. #else
  183.     if (write(LogFile, tbuf, cnt) >= 0)
  184.         return;
  185. #endif
  186.     /*
  187.      * Output the message to the console; don't worry about blocking,
  188.      * if console blocks everything will.  Make sure the error reported
  189.      * is the one from the syslogd failure.
  190.      */
  191.     /* should mode be `O_WRONLY | O_NOCTTY' ? -- Uli */
  192.     if (LogStat & LOG_CONS &&
  193.         (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  194.         (void)strcat(tbuf, "\r\n");
  195.         cnt += 2;
  196.         p = index(tbuf, '>') + 1;
  197.         (void)write(fd, p, cnt - (p - tbuf));
  198.         (void)close(fd);
  199.     }
  200. }
  201.  
  202. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  203. /*
  204.  * OPENLOG -- open system log
  205.  */
  206. void
  207. openlog(ident, logstat, logfac)
  208.     const char *ident;
  209.     int logstat, logfac;
  210. {
  211.     if (ident != NULL)
  212.         LogTag = ident;
  213.     LogStat = logstat;
  214.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  215.         LogFacility = logfac;
  216.     if (LogFile == -1) {
  217.         SyslogAddr.sa_family = AF_UNIX;
  218.         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  219.             sizeof(SyslogAddr.sa_data));
  220.         if (LogStat & LOG_NDELAY) {
  221.             if ((LogFile = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
  222.                 return;
  223. /*            fcntl(LogFile, F_SETFD, 1); */
  224.         }
  225.     }
  226.     if (LogFile != -1 && !connected &&
  227. #if 0
  228.         connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family)+
  229.             strlen(SyslogAddr.sa_data)) != -1)
  230. #else
  231.         connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
  232.             sizeof(SyslogAddr.sa_data) +
  233.             strlen(SyslogAddr.sa_data)) != -1)
  234. #endif
  235.         connected = 1;
  236. }
  237.  
  238. /*
  239.  * CLOSELOG -- close the system log
  240.  */
  241. void
  242. closelog()
  243. {
  244.     (void) close(LogFile);
  245.     LogFile = -1;
  246.     connected = 0;
  247. }
  248.  
  249. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  250. /*
  251.  * SETLOGMASK -- set the log mask level
  252.  */
  253. int
  254. setlogmask(pmask)
  255.     int pmask;
  256. {
  257.     int omask;
  258.  
  259.     omask = LogMask;
  260.     if (pmask != 0)
  261.         LogMask = pmask;
  262.     return (omask);
  263. }
  264.