home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / daemons / syslog_f.z / syslog_f / libsyslog / RCS / syslog.c,v
Encoding:
Text File  |  1993-03-17  |  6.8 KB  |  306 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 1.2
  9. date    93.03.17.20.18.32;    author negaard;    state Exp;
  10. branches;
  11. next    1.1;
  12.  
  13. 1.1
  14. date    93.03.17.20.17.54;    author negaard;    state Exp;
  15. branches;
  16. next    ;
  17.  
  18.  
  19. desc
  20. @Send a message to the syslog daemon.
  21. @
  22.  
  23.  
  24. 1.2
  25. log
  26. @Add preprocessor directive, SYSLOG_USE_FIFO, to log messages to a named
  27. pipe instead of using a unix domain socket.
  28. @
  29. text
  30. @/*
  31.  * Copyright (c) 1983, 1988 Regents of the University of California.
  32.  * All rights reserved.
  33.  *
  34.  * Redistribution and use in source and binary forms, with or without
  35.  * modification, are permitted provided that the following conditions
  36.  * are met:
  37.  * 1. Redistributions of source code must retain the above copyright
  38.  *    notice, this list of conditions and the following disclaimer.
  39.  * 2. Redistributions in binary form must reproduce the above copyright
  40.  *    notice, this list of conditions and the following disclaimer in the
  41.  *    documentation and/or other materials provided with the distribution.
  42.  * 3. All advertising materials mentioning features or use of this software
  43.  *    must display the following acknowledgement:
  44.  *    This product includes software developed by the University of
  45.  *    California, Berkeley and its contributors.
  46.  * 4. Neither the name of the University nor the names of its contributors
  47.  *    may be used to endorse or promote products derived from this software
  48.  *    without specific prior written permission.
  49.  *
  50.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  51.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  54.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60.  * SUCH DAMAGE.
  61.  */
  62.  
  63. #if defined(LIBC_SCCS) && !defined(lint)
  64. static char sccsid[] = "@@(#)syslog.c    5.34 (Berkeley) 6/26/91";
  65. #endif /* LIBC_SCCS and not lint */
  66.  
  67. #include <sys/types.h>
  68. #include <sys/socket.h>
  69. #include <sys/file.h>
  70. #include <sys/syslog.h>
  71. #include <sys/uio.h>
  72. #include <sys/errno.h>
  73. #include <netdb.h>
  74. #include <string.h>
  75. #if __STDC__
  76. #include <stdarg.h>
  77. #else
  78. #include <varargs.h>
  79. #endif
  80. #include <time.h>
  81. #include <unistd.h>
  82. #include <paths.h>
  83. #include <stdio.h>
  84.  
  85. static int    LogFile = -1;        /* fd for log */
  86. static int    connected;        /* have done connect */
  87. static int    LogStat = 0;        /* status bits, set by openlog() */
  88. static const char *LogTag = "syslog";    /* string to tag the entry with */
  89. static int    LogFacility = LOG_USER;    /* default facility code */
  90. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  91.  
  92. /*
  93.  * syslog, vsyslog --
  94.  *    print message on log file; output is intended for syslogd(8).
  95.  */
  96. void
  97. #if __STDC__
  98. syslog(int pri, const char *fmt, ...)
  99. #else
  100. syslog(pri, fmt, va_alist)
  101.     int pri;
  102.     char *fmt;
  103.     va_dcl
  104. #endif
  105. {
  106.     va_list ap;
  107.  
  108. #if __STDC__
  109.     va_start(ap, fmt);
  110. #else
  111.     va_start(ap);
  112. #endif
  113.     vsyslog(pri, fmt, ap);
  114.     va_end(ap);
  115. }
  116.  
  117. void
  118. vsyslog(pri, fmt, ap)
  119.     int pri;
  120.     register const char *fmt;
  121.     va_list ap;
  122. {
  123.     register int cnt;
  124.     register char *p;
  125.     time_t now, time();
  126.     int fd, saved_errno;
  127.     char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
  128.  
  129.     /* check for invalid bits or no priority set */
  130.     if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)) ||
  131.         !(LOG_MASK(pri) & LogMask))
  132.         return;
  133.  
  134.     saved_errno = errno;
  135.  
  136.     /* set default facility if none specified */
  137.     if ((pri & LOG_FACMASK) == 0)
  138.         pri |= LogFacility;
  139.  
  140.     /* build the message */
  141.     (void)time(&now);
  142.     (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  143.     for (p = tbuf; *p; ++p);
  144.     if (LogStat & LOG_PERROR)
  145.         stdp = p;
  146.     if (LogTag) {
  147.         (void)strcpy(p, LogTag);
  148.         for (; *p; ++p);
  149.     }
  150.     if (LogStat & LOG_PID) {
  151.         (void)sprintf(p, "[%d]", getpid());
  152.         for (; *p; ++p);
  153.     }
  154.     if (LogTag) {
  155.         *p++ = ':';
  156.         *p++ = ' ';
  157.     }
  158.  
  159.     /* substitute error message for %m */
  160.     {
  161.         register char ch, *t1, *t2;
  162.         char *strerror();
  163.  
  164.         for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  165.             if (ch == '%' && fmt[1] == 'm') {
  166.                 ++fmt;
  167.                 for (t2 = strerror(saved_errno);
  168.                     *t1 = *t2++; ++t1);
  169.             }
  170.             else
  171.                 *t1++ = ch;
  172.         *t1 = '\0';
  173.     }
  174.  
  175.     (void)vsprintf(p, fmt_cpy, ap);
  176.  
  177.     cnt = strlen(tbuf);
  178.  
  179.     /* output to stderr if requested */
  180.     if (LogStat & LOG_PERROR) {
  181.         struct iovec iov[2];
  182.         register struct iovec *v = iov;
  183.  
  184.         v->iov_base = stdp;
  185.         v->iov_len = cnt - (stdp - tbuf);
  186.         ++v;
  187.         v->iov_base = "\n";
  188.         v->iov_len = 1;
  189.         (void)writev(STDERR_FILENO, iov, 2);
  190.     }
  191.  
  192.     /* get connected, output the message to the local logger */
  193.     if (!connected)
  194.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  195. #ifdef SYSLOG_USE_FIFO
  196.     if (write(LogFile, tbuf, cnt) >= 0)
  197.         return;
  198. #else
  199.     if (send(LogFile, tbuf, cnt, 0) >= 0)
  200.         return;
  201. #endif
  202.  
  203.     /* see if should attempt the console */
  204.     if (!(LogStat&LOG_CONS))
  205.         return;
  206.  
  207.     /*
  208.      * Output the message to the console; don't worry about blocking,
  209.      * if console blocks everything will.  Make sure the error reported
  210.      * is the one from the syslogd failure.
  211.      */
  212.     if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  213.         (void)strcat(tbuf, "\r\n");
  214.         cnt += 2;
  215.         p = index(tbuf, '>') + 1;
  216.         (void)write(fd, p, cnt - (p - tbuf));
  217.         (void)close(fd);
  218.     }
  219. }
  220.  
  221. #ifndef SYSLOG_USE_FIFO
  222. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  223. #endif
  224.  
  225. void
  226. openlog(ident, logstat, logfac)
  227.     const char *ident;
  228.     int logstat, logfac;
  229. {
  230.     if (ident != NULL)
  231.         LogTag = ident;
  232.     LogStat = logstat;
  233.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  234.         LogFacility = logfac;
  235.  
  236.     if (LogFile == -1) {
  237. #ifndef SYSLOG_USE_FIFO
  238.         SyslogAddr.sa_family = AF_UNIX;
  239.         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  240.             sizeof(SyslogAddr.sa_data));
  241. #endif
  242.         if (LogStat & LOG_NDELAY) {
  243. #ifdef SYSLOG_USE_FIFO
  244.             /* Opening FIFO with O_RDWR guarantees no
  245.              * blocking according to kernel fs comments
  246.              */
  247.             if ((LogFile = open(_PATH_LOG, O_WRONLY|O_NONBLOCK, 0)) == -1)
  248.                 return;
  249.             else
  250.                 connected = 1;
  251. #else
  252.             if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
  253.                 return;
  254. #endif
  255.             (void)fcntl(LogFile, F_SETFD, 1);
  256.         }
  257.     }
  258. #ifndef SYSLOG_USE_FIFO
  259.     if (LogFile != -1 && !connected)
  260.         if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
  261.             (void)close(LogFile);
  262.             LogFile = -1;
  263.         } else
  264.             connected = 1;
  265. #endif
  266. }
  267.  
  268. void
  269. closelog()
  270. {
  271.     (void)close(LogFile);
  272.     LogFile = -1;
  273.     connected = 0;
  274. }
  275.  
  276. /* setlogmask -- set the log mask level */
  277. setlogmask(pmask)
  278.     int pmask;
  279. {
  280.     int omask;
  281.  
  282.     omask = LogMask;
  283.     if (pmask != 0)
  284.         LogMask = pmask;
  285.     return (omask);
  286. }
  287. @
  288.  
  289.  
  290. 1.1
  291. log
  292. @Initial revision
  293. @
  294. text
  295. @d166 4
  296. d172 1
  297. d192 1
  298. d194 1
  299. d208 1
  300. d212 1
  301. d214 9
  302. d225 1
  303. d229 1
  304. d236 1
  305. @
  306.