[BACK]Return to syslog.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/syslog.c, Revision 1.6

1.6     ! cgd         1: /*     $NetBSD$        */
        !             2:
1.1       cgd         3: /*
1.6     ! cgd         4:  * Copyright (c) 1983, 1988, 1993
        !             5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #if defined(LIBC_SCCS) && !defined(lint)
1.6     ! cgd        37: #if 0
        !            38: static char sccsid[] = "@(#)syslog.c   8.4 (Berkeley) 3/18/94";
        !            39: #else
        !            40: static char rcsid[] = "$NetBSD";
        !            41: #endif
1.1       cgd        42: #endif /* LIBC_SCCS and not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/socket.h>
                     46: #include <sys/syslog.h>
                     47: #include <sys/uio.h>
                     48: #include <netdb.h>
1.2       glass      49:
                     50: #include <errno.h>
                     51: #include <fcntl.h>
                     52: #include <paths.h>
                     53: #include <stdio.h>
1.1       cgd        54: #include <string.h>
1.2       glass      55: #include <time.h>
                     56: #include <unistd.h>
                     57:
1.1       cgd        58: #if __STDC__
                     59: #include <stdarg.h>
                     60: #else
                     61: #include <varargs.h>
                     62: #endif
                     63:
                     64: static int     LogFile = -1;           /* fd for log */
                     65: static int     connected;              /* have done connect */
                     66: static int     LogStat = 0;            /* status bits, set by openlog() */
1.6     ! cgd        67: static const char *LogTag = NULL;      /* string to tag the entry with */
1.1       cgd        68: static int     LogFacility = LOG_USER; /* default facility code */
                     69: static int     LogMask = 0xff;         /* mask of priorities to be logged */
1.6     ! cgd        70: extern char    *__progname;            /* Program name, from crt0. */
1.1       cgd        71:
                     72: /*
                     73:  * syslog, vsyslog --
                     74:  *     print message on log file; output is intended for syslogd(8).
                     75:  */
                     76: void
                     77: #if __STDC__
                     78: syslog(int pri, const char *fmt, ...)
                     79: #else
                     80: syslog(pri, fmt, va_alist)
                     81:        int pri;
                     82:        char *fmt;
                     83:        va_dcl
                     84: #endif
                     85: {
                     86:        va_list ap;
                     87:
                     88: #if __STDC__
                     89:        va_start(ap, fmt);
                     90: #else
                     91:        va_start(ap);
                     92: #endif
                     93:        vsyslog(pri, fmt, ap);
                     94:        va_end(ap);
                     95: }
                     96:
                     97: void
                     98: vsyslog(pri, fmt, ap)
                     99:        int pri;
                    100:        register const char *fmt;
                    101:        va_list ap;
                    102: {
                    103:        register int cnt;
1.6     ! cgd       104:        register char ch, *p, *t;
1.2       glass     105:        time_t now;
1.1       cgd       106:        int fd, saved_errno;
1.2       glass     107:        char *stdp, tbuf[2048], fmt_cpy[1024];
                    108:
                    109: #define        INTERNALLOG     LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
                    110:        /* Check for invalid bits. */
                    111:        if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
                    112:                syslog(INTERNALLOG,
                    113:                    "syslog: unknown facility/priority: %x", pri);
                    114:                pri &= LOG_PRIMASK|LOG_FACMASK;
                    115:        }
1.1       cgd       116:
1.2       glass     117:        /* Check priority against setlogmask values. */
                    118:        if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
1.1       cgd       119:                return;
                    120:
                    121:        saved_errno = errno;
                    122:
1.6     ! cgd       123:        /* Set default facility if none specified. */
1.1       cgd       124:        if ((pri & LOG_FACMASK) == 0)
                    125:                pri |= LogFacility;
                    126:
1.6     ! cgd       127:        /* Build the message. */
1.1       cgd       128:        (void)time(&now);
1.6     ! cgd       129:        p = tbuf + sprintf(tbuf, "<%d>", pri);
        !           130:        p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
        !           131:            localtime(&now));
1.1       cgd       132:        if (LogStat & LOG_PERROR)
                    133:                stdp = p;
1.6     ! cgd       134:        if (LogTag == NULL)
        !           135:                LogTag = __progname;
        !           136:        if (LogTag != NULL)
        !           137:                p += sprintf(p, "%s", LogTag);
        !           138:        if (LogStat & LOG_PID)
        !           139:                p += sprintf(p, "[%d]", getpid());
        !           140:        if (LogTag != NULL) {
1.1       cgd       141:                *p++ = ':';
                    142:                *p++ = ' ';
                    143:        }
                    144:
1.6     ! cgd       145:        /* Substitute error message for %m. */
        !           146:        for (t = fmt_cpy; ch = *fmt; ++fmt)
        !           147:                if (ch == '%' && fmt[1] == 'm') {
        !           148:                        ++fmt;
        !           149:                        t += sprintf(t, "%s", strerror(saved_errno));
        !           150:                } else
        !           151:                        *t++ = ch;
        !           152:        *t = '\0';
1.1       cgd       153:
1.6     ! cgd       154:        p += vsprintf(p, fmt_cpy, ap);
        !           155:        cnt = p - tbuf;
1.1       cgd       156:
1.6     ! cgd       157:        /* Output to stderr if requested. */
1.1       cgd       158:        if (LogStat & LOG_PERROR) {
                    159:                struct iovec iov[2];
                    160:                register struct iovec *v = iov;
                    161:
                    162:                v->iov_base = stdp;
                    163:                v->iov_len = cnt - (stdp - tbuf);
                    164:                ++v;
                    165:                v->iov_base = "\n";
                    166:                v->iov_len = 1;
                    167:                (void)writev(STDERR_FILENO, iov, 2);
                    168:        }
                    169:
1.6     ! cgd       170:        /* Get connected, output the message to the local logger. */
1.1       cgd       171:        if (!connected)
                    172:                openlog(LogTag, LogStat | LOG_NDELAY, 0);
                    173:        if (send(LogFile, tbuf, cnt, 0) >= 0)
                    174:                return;
                    175:
                    176:        /*
                    177:         * Output the message to the console; don't worry about blocking,
                    178:         * if console blocks everything will.  Make sure the error reported
                    179:         * is the one from the syslogd failure.
                    180:         */
1.6     ! cgd       181:        if (LogStat & LOG_CONS &&
        !           182:            (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
1.1       cgd       183:                (void)strcat(tbuf, "\r\n");
                    184:                cnt += 2;
1.5       jtc       185:                p = strchr(tbuf, '>') + 1;
1.1       cgd       186:                (void)write(fd, p, cnt - (p - tbuf));
                    187:                (void)close(fd);
                    188:        }
                    189: }
                    190:
                    191: static struct sockaddr SyslogAddr;     /* AF_UNIX address of local logger */
                    192:
                    193: void
                    194: openlog(ident, logstat, logfac)
                    195:        const char *ident;
                    196:        int logstat, logfac;
                    197: {
                    198:        if (ident != NULL)
                    199:                LogTag = ident;
                    200:        LogStat = logstat;
                    201:        if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
                    202:                LogFacility = logfac;
                    203:
                    204:        if (LogFile == -1) {
                    205:                SyslogAddr.sa_family = AF_UNIX;
                    206:                (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
                    207:                    sizeof(SyslogAddr.sa_data));
                    208:                if (LogStat & LOG_NDELAY) {
                    209:                        if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
                    210:                                return;
                    211:                        (void)fcntl(LogFile, F_SETFD, 1);
                    212:                }
                    213:        }
                    214:        if (LogFile != -1 && !connected)
                    215:                if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
                    216:                        (void)close(LogFile);
                    217:                        LogFile = -1;
                    218:                } else
                    219:                        connected = 1;
                    220: }
                    221:
                    222: void
                    223: closelog()
                    224: {
                    225:        (void)close(LogFile);
                    226:        LogFile = -1;
                    227:        connected = 0;
                    228: }
                    229:
                    230: /* setlogmask -- set the log mask level */
1.2       glass     231: int
1.1       cgd       232: setlogmask(pmask)
                    233:        int pmask;
                    234: {
                    235:        int omask;
                    236:
                    237:        omask = LogMask;
                    238:        if (pmask != 0)
                    239:                LogMask = pmask;
                    240:        return (omask);
                    241: }

CVSweb <webmaster@jp.NetBSD.org>