# 2.11BSD#431 - /usr/src/lib/libc/gen/syslog.c

##### Functions

    closelog          X
    openlog           X
    setlogmask        X
    syslog            X
    vsyslog           X

##### Macros

    INTERNALLOG       X
    STDERR_FILENO     X

       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.3 (2.11BSD) 1995/07/15";
      36: #endif /* LIBC_SCCS and not lint */
      37:
      38: #include <sys/types.h>
      39: #include <sys/socket.h>
      40: #include <syslog.h>
      41: #include <sys/uio.h>
      42: #include <netdb.h>
      43:
      44: #include <errno.h>
      45: #include <fcntl.h>
      46: #include <paths.h>
      47: #include <stdio.h>
      48: #include <string.h>
      49: #include <time.h>
      50:
      51: #include <varargs.h>
      52:
      53: #define STDERR_FILENO   2
      54:
      55: static  int LogFile = -1;       /* fd for log */
      56: static  char    connected;      /* have done connect */
      57: #ifdef  pdp11
      58: static  char    ToFile = 0;     /* set if logfile is used */
      59: #endif
      60: static  int LogStat = 0;        /* status bits, set by openlog() */
      61: static  char    *LogTag = NULL;     /* string to tag the entry with */
      62: static  int LogFacility = LOG_USER; /* default facility code */
      63: static  int LogMask = 0xff;     /* mask of priorities to be logged */
      64: #ifdef  pdp11
      65: static  char    logfile[] = "/usr/adm/messages";
      66: #endif
      67:
      68: extern  char    *__progname;        /* Program name, from crt0. */
      69: extern  int errno;          /* error number */
      70:
      71: /*
      72:  * syslog, vsyslog --
      73:  *	print message on log file; output is intended for syslogd(8).
      74:  */
      75: void
      76: syslog(pri, fmt, va_alist)
      77:     int pri;
      78:     char *fmt;
      79:     va_dcl
      80: {
      81:     va_list ap;
      82:
      83:     va_start(ap);
      84:     vsyslog(pri, fmt, ap);
      85:     va_end(ap);
      86: }
      87:
      88: void
      89: vsyslog(pri, fmt, ap)
      90:     int pri;
      91:     register char *fmt;
      92:     va_list ap;
      93: {
      94:     int cnt;
      95:     char ch;
      96:     register char *p, *t;
      97:     time_t now;
      98:     int fd, saved_errno;
      99:     char *stdp, tbuf[640], fmt_cpy[512];
     100:     pid_t   pid;
     101:
     102: #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
     103:     /* Check for invalid bits. */
     104:     if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
     105:         syslog(INTERNALLOG,
     106:             "syslog: bad fac/pri: %x", pri);
     107:         pri &= LOG_PRIMASK|LOG_FACMASK;
     108:     }
     109:
     110:     /* Check priority against setlogmask values. */
     111:     if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
     112:         return;
     113:
     114:     saved_errno = errno;
     115:
     116:     /* Set default facility if none specified. */
     117:     if ((pri & LOG_FACMASK) == 0)
     118:         pri |= LogFacility;
     119:
     120:     /* Build the message. */
     121:     (void)time(&now);
     122:     p = tbuf + sprintf(tbuf, "<%d>", pri);
     123:     p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
     124:         localtime(&now));
     125:     if (LogStat & LOG_PERROR)
     126:         stdp = p;
     127:     if (LogTag == NULL)
     128:         LogTag = __progname;
     129:     if (LogTag != NULL)
     130:         p += sprintf(p, "%s", LogTag);
     131:     if (LogStat & LOG_PID)
     132:         p += sprintf(p, "[%d]", getpid());
     133:     if (LogTag != NULL) {
     134:         *p++ = ':';
     135:         *p++ = ' ';
     136:     }
     137:
     138:     /* Substitute error message for %m. */
     139:     for (t = fmt_cpy; ch = *fmt; ++fmt)
     140:         if (ch == '%' && fmt[1] == 'm') {
     141:             ++fmt;
     142:             t += sprintf(t, "%s", strerror(saved_errno));
     143:         } else
     144:             *t++ = ch;
     145:     *t = '\0';
     146:
     147:     p += vsprintf(p, fmt_cpy, ap);
     148:     cnt = p - tbuf;
     149:
     150:     /* Output to stderr if requested. */
     151:     if (LogStat & LOG_PERROR) {
     152:         struct iovec iov[2];
     153:         register struct iovec *v = iov;
     154:
     155:         v->iov_base = stdp;
     156:         v->iov_len = cnt - (stdp - tbuf);
     157:         ++v;
     158:         v->iov_base = "\n";
     159:         v->iov_len = 1;
     160:         (void)writev(STDERR_FILENO, iov, 2);
     161:     }
     162:
     163:     /* Get connected, output the message to the local logger. */
     164:     if (!connected)
     165:         openlog(LogTag, LogStat | LOG_NDELAY, 0);
     166: #ifdef  pdp11
     167:     if (ToFile) {
     168:         if (write(LogFile, tbuf, cnt) == cnt)
     169:             return;
     170:     }
     171:     else
     172: #endif
     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: 	 *
     181: 	 * 2.11BSD has to do a more complicated dance because we do not
     182: 	 * want to acquire a controlling terminal (bad news for 'init'!).
     183: 	 * Until either the tty driver is ported from 4.4 or O_NOCTTY
     184: 	 * is implemented we have to fork and let the child do the open of
     185: 	 * the console.
     186: 	 */
     187:     if (LogStat & LOG_CONS) {
     188:         pid = vfork();
     189:         if (pid == -1)
     190:             return;
     191:         if (pid == 0) {
     192:             fd = open(_PATH_CONSOLE, O_WRONLY, 0);
     193:             (void)strcat(tbuf, "\r\n");
     194:             cnt += 2;
     195:             p = index(tbuf, '>') + 1;
     196:             (void)write(fd, p, cnt - (p - tbuf));
     197:             (void)close(fd);
     198:             _exit(0);
     199:         }
     200:         while (waitpid(pid, NULL, NULL) == -1 && (errno == EINTR))
     201:             ;
     202:     }
     203: }
     204:
     205: static struct sockaddr SyslogAddr;  /* AF_UNIX address of local logger */
     206:
     207: void
     208: openlog(ident, logstat, logfac)
     209:     char *ident;
     210:     int logstat;
     211:     register int logfac;
     212: {
     213:     if (ident != NULL)
     214:         LogTag = ident;
     215:     LogStat = logstat;
     216:     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
     217:         LogFacility = logfac;
     218:
     219:     if (LogFile == -1) {
     220:         SyslogAddr.sa_family = AF_UNIX;
     221:         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
     222:             sizeof(SyslogAddr.sa_data));
     223:         if (LogStat & LOG_NDELAY) {
     224:             LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
     225: #ifdef  pdp11
     226:             if (LogFile == -1) {
     227:                 LogFile = open(logfile, O_WRONLY|O_APPEND);
     228:                 ToFile = 1;
     229:                 connected = 1;
     230:             }
     231:             else
     232:                 ToFile = 0;
     233: #endif
     234:             if (LogFile == -1)
     235:                 return;
     236:             (void)fcntl(LogFile, F_SETFD, 1);
     237:         }
     238:     }
     239:     if (LogFile != -1 && !connected)
     240:         if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
     241:             (void)close(LogFile);
     242:             LogFile = -1;
     243:         } else
     244:             connected = 1;
     245: }
     246:
     247: void
     248: closelog()
     249: {
     250:     (void)close(LogFile);
     251:     LogFile = -1;
     252:     connected = 0;
     253: }
     254:
     255: /* setlogmask -- set the log mask level */
     256: int
     257: setlogmask(pmask)
     258:     register int pmask;
     259: {
     260:     register int omask;
     261:
     262:     omask = LogMask;
     263:     if (pmask != 0)
     264:         LogMask = pmask;
     265:     return (omask);
     266: }

#### Defined functions

closelog defined in line 247; used 8 times

- in /usr/src/bin/su.c line 108
- in /usr/src/libexec/getty/main.c line 146
- in /usr/src/libexec/popper/popper.c line 73
- in /usr/src/sbin/init/init.c line 539, 685, 738
- in /usr/src/sbin/newfs/newfs.c line 256
- in /usr/src/usr.sbin/cron/do_command.c line 150

openlog defined in line 207; used 46 times

- in line 165
- in /usr/src/bin/login/login.c line 181
- in /usr/src/bin/su.c line 47
- in /usr/src/libexec/acctd/acctd.c line 462, 476
- in /usr/src/libexec/comsat/comsat.c line 71
- in /usr/src/libexec/fingerd/fingerd.c line 75
- in /usr/src/libexec/ftpd/ftpd.c line 145
- in /usr/src/libexec/getty/main.c line 120
- in /usr/src/libexec/identd/src/identd.c line 763-765(2)
- in /usr/src/libexec/identd/src/itest.c line 95-97(2)
- in /usr/src/libexec/popper/pop_init.c line 54-56(2)
- in /usr/src/libexec/rlogind/rlogind.c line 87
- in /usr/src/libexec/rshd/rshd.c line 73
- in /usr/src/libexec/talkd/talkd.c line 53
- in /usr/src/libexec/telnetd/telnetd.c line 155
- in /usr/src/libexec/tftpd/tftpd.c line 80
- in /usr/src/new/nntp/xmit/nntpxmit.c line 191-193(2)
- in /usr/src/new/notes/src/newsinput.c line 56
- in /usr/src/new/notes/src/newsoutput.c line 80
- in /usr/src/sbin/init/init.c line 118
- in /usr/src/sbin/newfs/newfs.c line 254
- in /usr/src/sbin/reboot/reboot.c line 100
- in /usr/src/sbin/routed/main.c line 45
- in /usr/src/sbin/shutdown/shutdown.c line 116
- in /usr/src/ucb/logger.c line 95
- in /usr/src/usr.lib/libident/ident-tester.c line 70-72(2)
- in /usr/src/usr.sbin/cron/misc.c line 508
- in /usr/src/usr.sbin/inetd/inetd.c line 251
- in /usr/src/usr.sbin/lpr/lpc.c line 44
- in /usr/src/usr.sbin/lpr/lpd.c line 97
- in /usr/src/usr.sbin/lpr/lpq.c line 66
- in /usr/src/usr.sbin/lpr/lpr.c line 99
- in /usr/src/usr.sbin/lpr/lprm.c line 53
- in /usr/src/usr.sbin/ntp/ntpd.c line 275, 281
- in /usr/src/usr.sbin/rwhod/rwhod.c line 110
- in /usr/src/usr.sbin/sendmail.MX/src/main.c line 236
- in /usr/src/usr.sbin/sendmail/src/main.c line 231
- in /usr/src/usr.sbin/timed/timed.c line 103
- in /usr/src/usr.sbin/timed/timedc.c line 39

setlogmask defined in line 256; used 7 times

- in /usr/src/sbin/routed/main.c line 46, 71-76(2)
- in /usr/src/usr.sbin/ntp/ntpd.c line 284-287(2), 1265, 1280

syslog defined in line 75; used 483 times

- in line 105
- in /usr/src/bin/date.c line 135
- in /usr/src/bin/login/login.c line 232-236(2), 407-413(3), 561-564(2)
- in /usr/src/bin/su.c line 99, 107
- in /usr/src/libexec/comsat/comsat.c line 73-77(2), 130
- in /usr/src/libexec/fingerd/fingerd.c line 102
- in /usr/src/libexec/ftpd/ftpcmd.y line 783, 823, 838
- in /usr/src/libexec/ftpd/ftpd.c line 148-153(2), 221, 227-231(2), 255, 364, 377, 434, 482, 491, 993-994(2), 1008-1009(2), 1141
- in /usr/src/libexec/getty/main.c line 145
- in /usr/src/libexec/identd/src/error.h line 18, 27, 35
- in /usr/src/libexec/identd/src/identd.c line 218, 769
- in /usr/src/libexec/identd/src/kernel/2.11bsd.c line 138, 144
- in /usr/src/libexec/identd/src/parse.c line 99, 108, 120, 188, 197, 224, 241, 267, 291, 317, 336, 354, 373, 385, 404, 411-414(2), 420, 433, 439, 453, 464, 519, 528-531(2), 542, 558, 580-584(2), 591
- in /usr/src/libexec/popper/pop_log.c line 49
- in /usr/src/libexec/rlogind/rlogind.c line 103, 111-116(2), 200, 220-224(2)
- in /usr/src/libexec/rshd/rshd.c line 89, 98, 104-109(2), 150, 169-173(2), 182, 193, 207-216(3), 244, 257, 268, 368, 384
- in /usr/src/libexec/talkd/announce.c line 49
- in /usr/src/libexec/talkd/print.c line 39, 59
- in /usr/src/libexec/talkd/process.c line 43, 50, 57
- in /usr/src/libexec/talkd/table.c line 139, 175
- in /usr/src/libexec/talkd/talkd.c line 55-59(2), 72, 81
- in /usr/src/libexec/tcpd/fix_options.c line 43-47(2)
- in /usr/src/libexec/tcpd/hosts_access.c line 159
- in /usr/src/libexec/tcpd/miscd.c line 113-116(2)
- in /usr/src/libexec/tcpd/myvsyslog.c line 30
- in /usr/src/libexec/tcpd/options.c line 187, 375
- in /usr/src/libexec/tcpd/refuse.c line 28
- in /usr/src/libexec/tcpd/tcpd.c line 123-125(2)
- in /usr/src/libexec/telnetd/telnetd.c line 163, 189-192(2), 810, 1250
- in /usr/src/libexec/tftpd/tftpd.c line 82, 89, 136, 148-156(3), 326, 335, 389, 398, 486
- in /usr/src/new/nntp/xmit/nntpxmit.c line 1100
- in /usr/src/new/notes/src/bnewshead.c line 248
- in /usr/src/new/notes/src/recsio.c line 116
- in /usr/src/sbin/init/init.c line 222, 241, 267-272(2), 450, 538, 572, 684, 737, 752, 760, 770-778(3)
- in /usr/src/sbin/reboot/reboot.c line 101
- in /usr/src/sbin/routed/input.c line 35, 96, 133, 152, 160
- in /usr/src/sbin/routed/main.c line 180-184(2), 190
- in /usr/src/sbin/routed/startup.c line 40, 46, 57, 77, 84, 90-94(2), 198
- in /usr/src/sbin/routed/tables.c line 183, 215
- in /usr/src/sbin/shutdown/shutdown.c line 244
- in /usr/src/ucb/logger.c line 109, 115
- in /usr/src/usr.lib/libident/ident-tester.c line 88, 113, 144, 157, 174, 191, 204, 216-220(2)
- in /usr/src/usr.sbin/cron/misc.c line 512
- in /usr/src/usr.sbin/inetd/inetd.c line 264, 273, 319, 336, 370, 385, 425, 435, 443, 452, 481, 503, 510, 555, 614, 622-624(2), 632, 685, 782, 788, 804, 853, 909
- in /usr/src/usr.sbin/lpr/lpd.c line 101, 107, 117, 139, 151, 163-168(2), 174, 190, 202, 235, 285-288(2), 382
- in /usr/src/usr.sbin/lpr/printjob.c line 74, 91, 98, 104, 114, 121, 128, 146, 164, 172, 182, 192, 233, 469, 506, 542, 563, 579, 592, 598, 609, 737, 757, 777, 981, 987, 1014-1017(2), 1063, 1072, 1125, 1160, 1182, 1224-1228(2), 1236, 1244, 1251, 1257, 1263, 1279
- in /usr/src/usr.sbin/lpr/recvjob.c line 53, 70, 315
- in /usr/src/usr.sbin/ntp/ntp_adjust.c line 166, 294
- in /usr/src/usr.sbin/ntp/ntp_proto.c line 213, 228, 257, 285, 432, 489, 498, 710, 729, 754, 985, 1195-1200(2)
- in /usr/src/usr.sbin/ntp/ntp_sock.c line 71, 119, 128, 140, 176, 185, 198, 209, 217, 226, 243, 275, 290, 297, 312, 327, 338
- in /usr/src/usr.sbin/ntp/ntpd.c line 290-291(2), 303, 313, 403, 599, 665, 691, 735, 811-821(3), 829, 862-877(4), 889, 925, 981, 993, 1005, 1015-1020(2), 1041, 1063, 1176, 1189, 1225, 1250, 1267, 1277, 1289, 1328
- in /usr/src/usr.sbin/ntp/read_psti.c line 94, 102, 111, 132, 139, 183, 191, 199, 212, 222, 269, 285, 306, 344-349(2)
- in /usr/src/usr.sbin/rwhod/rwhod.c line 115, 127-141(4), 156-160(2), 169, 180, 293, 313, 331, 354, 384, 396, 406
- in /usr/src/usr.sbin/sendmail.MX/aux/bbn_daemon.c line 134
- in /usr/src/usr.sbin/sendmail.MX/src/alias.c line 255, 262, 281, 540
- in /usr/src/usr.sbin/sendmail.MX/src/daemon.c line 122, 247
- in /usr/src/usr.sbin/sendmail.MX/src/deliver.c line 1093
- in /usr/src/usr.sbin/sendmail.MX/src/envelope.c line 97, 351, 476
- in /usr/src/usr.sbin/sendmail.MX/src/err.c line 76
- in /usr/src/usr.sbin/sendmail.MX/src/extract.c line 90
- in /usr/src/usr.sbin/sendmail.MX/src/headers.c line 327, 397
- in /usr/src/usr.sbin/sendmail.MX/src/main.c line 750, 1052
- in /usr/src/usr.sbin/sendmail.MX/src/queue.c line 247, 324, 615, 628, 999, 1033
- in /usr/src/usr.sbin/sendmail.MX/src/savemail.c line 333
- in /usr/src/usr.sbin/sendmail.MX/src/usersmtp.c line 422
- in /usr/src/usr.sbin/sendmail.MX/src/util.c line 575, 581, 943
- in /usr/src/usr.sbin/sendmail/aux/4.1a_daemon.c line 72, 110
- in /usr/src/usr.sbin/sendmail/aux/bbn_daemon.c line 134
- in /usr/src/usr.sbin/sendmail/src/alias.c line 252, 259, 278, 548
- in /usr/src/usr.sbin/sendmail/src/daemon.c line 122, 247
- in /usr/src/usr.sbin/sendmail/src/deliver.c line 1038
- in /usr/src/usr.sbin/sendmail/src/envelope.c line 91, 345, 470
- in /usr/src/usr.sbin/sendmail/src/err.c line 67
- in /usr/src/usr.sbin/sendmail/src/extract.c line 90
- in /usr/src/usr.sbin/sendmail/src/headers.c line 321, 391
- in /usr/src/usr.sbin/sendmail/src/main.c line 754, 1056
- in /usr/src/usr.sbin/sendmail/src/queue.c line 236, 313, 602, 615, 971, 988, 1022
- in /usr/src/usr.sbin/sendmail/src/savemail.c line 327
- in /usr/src/usr.sbin/sendmail/src/usersmtp.c line 418
- in /usr/src/usr.sbin/sendmail/src/util.c line 575, 581, 943
- in /usr/src/usr.sbin/timed/acksend.c line 59
- in /usr/src/usr.sbin/timed/candidate.c line 33, 47, 94
- in /usr/src/usr.sbin/timed/correct.c line 114, 120
- in /usr/src/usr.sbin/timed/master.c line 63, 87, 117, 125, 134, 150, 189, 217, 255, 311, 377, 421, 487, 526
- in /usr/src/usr.sbin/timed/readmsg.c line 155, 288, 323, 363, 377, 390
- in /usr/src/usr.sbin/timed/slave.c line 52, 64, 73, 138, 208, 225, 243, 278, 294-298(2), 311, 361, 370, 400, 424, 478, 493, 518
- in /usr/src/usr.sbin/timed/timed.c line 177, 185-197(4), 216, 228, 238, 253, 267, 275, 282, 317, 337, 384, 489, 603

vsyslog defined in line 88; used 5 times

- in line 84
- in /usr/src/libexec/acctd/acctd.c line 464, 478
- in /usr/src/libexec/fingerd/fingerd.c line 185
- in /usr/src/sbin/newfs/newfs.c line 255

#### Defined variables

LogFacility defined in line 62; used 2 times

- in line 118, 217

LogFile defined in line 55; used 14 times

- in line 168-173(2), 219-227(4), 234-242(6), 250-251(2)

LogMask defined in line 63; used 3 times

- in line 111, 262-264(2)

LogStat defined in line 60; used 7 times

- in line 125, 131, 151, 165, 187, 215, 223

LogTag defined in line 61; used 7 times

- in line 127-133(5), 165, 214

SyslogAddr defined in line 205; used 5 times

- in line 220-222(3), 240(2)

ToFile defined in line 58; used 3 times

- in line 167, 228-232(2)

connected defined in line 56; used 5 times

- in line 164, 229, 239-244(2), 252

logfile defined in line 65; used 1 times

- in line 227

sccsid defined in line 35; never used

#### Defined macros

INTERNALLOG defined in line 102; used 1 times

- in line 105

STDERR_FILENO defined in line 53; used 1 times

- in line 160

Last modified: 1995-07-16 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 4160 |