Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
c5282f4a MK |
2 | * Copyright (c) 1983, 1988 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
269a7923 | 5 | * %sccs.include.redist.c% |
bb0cfa24 DF |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
3b180a43 | 9 | static char sccsid[] = "@(#)syslog.c 5.28 (Berkeley) %G%"; |
c5282f4a MK |
10 | #endif /* LIBC_SCCS and not lint */ |
11 | ||
b97c7a10 SL |
12 | /* |
13 | * SYSLOG -- print message on log file | |
14 | * | |
61ed6dd0 KB |
15 | * This routine looks a lot like printf, except that it outputs to the |
16 | * log file instead of the standard output. Also: | |
bc51229a RC |
17 | * adds a timestamp, |
18 | * prints the module name in front of the message, | |
19 | * has some other formatting types (or will sometime), | |
20 | * adds a newline on the end of the message. | |
b97c7a10 | 21 | * |
61ed6dd0 | 22 | * The output of this routine is intended to be read by syslogd(8). |
4d707151 RC |
23 | * |
24 | * Author: Eric Allman | |
25 | * Modified to use UNIX domain IPC by Ralph Campbell | |
b97c7a10 | 26 | */ |
bc51229a | 27 | |
b97c7a10 SL |
28 | #include <sys/types.h> |
29 | #include <sys/socket.h> | |
bc51229a | 30 | #include <sys/file.h> |
a52c86c2 MK |
31 | #include <sys/signal.h> |
32 | #include <sys/syslog.h> | |
84f78f79 | 33 | #include <sys/uio.h> |
bea3da5c | 34 | #include <sys/wait.h> |
b97c7a10 | 35 | #include <netdb.h> |
6ebcb998 | 36 | #include <string.h> |
137a7cf5 | 37 | #include <varargs.h> |
bea3da5c | 38 | #include <paths.h> |
61ed6dd0 | 39 | #include <stdio.h> |
b97c7a10 | 40 | |
bea3da5c | 41 | #define _PATH_LOGNAME "/dev/log" |
b97c7a10 | 42 | |
bc51229a | 43 | static int LogFile = -1; /* fd for log */ |
12d303b4 | 44 | static int connected; /* have done connect */ |
61ed6dd0 | 45 | static int LogStat = 0; /* status bits, set by openlog() */ |
a05c1f2b | 46 | static char *LogTag = "syslog"; /* string to tag the entry with */ |
a05c1f2b | 47 | static int LogFacility = LOG_USER; /* default facility code */ |
b97c7a10 | 48 | |
61ed6dd0 KB |
49 | syslog(pri, fmt, args) |
50 | int pri, args; | |
b97c7a10 | 51 | char *fmt; |
137a7cf5 KB |
52 | { |
53 | vsyslog(pri, fmt, &args); | |
54 | } | |
55 | ||
56 | vsyslog(pri, fmt, ap) | |
57 | int pri; | |
56772c3d | 58 | register char *fmt; |
137a7cf5 | 59 | va_list ap; |
b97c7a10 | 60 | { |
56772c3d | 61 | extern int errno; |
61ed6dd0 KB |
62 | register int cnt; |
63 | register char *p; | |
64 | time_t now, time(); | |
27071b79 | 65 | int fd, saved_errno; |
84f78f79 | 66 | char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); |
56772c3d KB |
67 | |
68 | saved_errno = errno; | |
b97c7a10 | 69 | |
b97c7a10 | 70 | /* see if we should just throw out this message */ |
f55e3a27 | 71 | if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) |
b97c7a10 | 72 | return; |
12d303b4 | 73 | if (LogFile < 0 || !connected) |
fadfb2f6 | 74 | openlog(LogTag, LogStat | LOG_NDELAY, 0); |
a05c1f2b EA |
75 | |
76 | /* set default facility if none specified */ | |
77 | if ((pri & LOG_FACMASK) == 0) | |
78 | pri |= LogFacility; | |
79 | ||
80 | /* build the message */ | |
61ed6dd0 KB |
81 | (void)time(&now); |
82 | (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); | |
83 | for (p = tbuf; *p; ++p); | |
84f78f79 KB |
84 | if (LogStat & LOG_PERROR) |
85 | stdp = p; | |
bc51229a | 86 | if (LogTag) { |
61ed6dd0 KB |
87 | (void)strcpy(p, LogTag); |
88 | for (; *p; ++p); | |
bc51229a RC |
89 | } |
90 | if (LogStat & LOG_PID) { | |
61ed6dd0 KB |
91 | (void)sprintf(p, "[%d]", getpid()); |
92 | for (; *p; ++p); | |
bc51229a | 93 | } |
a05c1f2b | 94 | if (LogTag) { |
61ed6dd0 KB |
95 | *p++ = ':'; |
96 | *p++ = ' '; | |
a05c1f2b | 97 | } |
b97c7a10 | 98 | |
56772c3d KB |
99 | /* substitute error message for %m */ |
100 | { | |
101 | register char ch, *t1, *t2; | |
102 | char *strerror(); | |
103 | ||
104 | for (t1 = fmt_cpy; ch = *fmt; ++fmt) | |
105 | if (ch == '%' && fmt[1] == 'm') { | |
106 | ++fmt; | |
107 | for (t2 = strerror(saved_errno); | |
108 | *t1 = *t2++; ++t1); | |
109 | } | |
110 | else | |
111 | *t1++ = ch; | |
c58afa87 | 112 | *t1 = '\0'; |
56772c3d KB |
113 | } |
114 | ||
115 | (void)vsprintf(p, fmt_cpy, ap); | |
a05c1f2b | 116 | |
84f78f79 KB |
117 | cnt = strlen(tbuf); |
118 | ||
119 | /* output to stderr if requested */ | |
120 | if (LogStat & LOG_PERROR) { | |
121 | struct iovec iov[2]; | |
122 | register struct iovec *v = iov; | |
123 | ||
124 | v->iov_base = stdp; | |
125 | v->iov_len = cnt - (stdp - tbuf); | |
126 | ++v; | |
127 | v->iov_base = "\n"; | |
128 | v->iov_len = 1; | |
129 | (void)writev(2, iov, 2); | |
130 | } | |
131 | ||
a05c1f2b | 132 | /* output the message to the local logger */ |
bea3da5c | 133 | if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS)) |
bc51229a | 134 | return; |
a05c1f2b | 135 | |
27071b79 KB |
136 | /* |
137 | * output the message to the console; don't worry about | |
138 | * blocking, if console blocks everything will. | |
139 | */ | |
140 | if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) | |
bc51229a | 141 | return; |
4fe1b7b4 | 142 | (void)strcat(tbuf, "\r\n"); |
3b180a43 | 143 | cnt += 2; |
27071b79 | 144 | p = index(tbuf, '>') + 1; |
3b180a43 | 145 | (void)write(fd, p, cnt - (p - tbuf)); |
27071b79 | 146 | (void)close(fd); |
b97c7a10 SL |
147 | } |
148 | ||
61ed6dd0 | 149 | static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ |
b97c7a10 SL |
150 | /* |
151 | * OPENLOG -- open system log | |
152 | */ | |
a05c1f2b | 153 | openlog(ident, logstat, logfac) |
b97c7a10 | 154 | char *ident; |
a05c1f2b | 155 | int logstat, logfac; |
b97c7a10 | 156 | { |
a05c1f2b EA |
157 | if (ident != NULL) |
158 | LogTag = ident; | |
b97c7a10 | 159 | LogStat = logstat; |
c5282f4a MK |
160 | if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) |
161 | LogFacility = logfac; | |
12d303b4 MK |
162 | if (LogFile == -1) { |
163 | SyslogAddr.sa_family = AF_UNIX; | |
bea3da5c KB |
164 | strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, |
165 | sizeof(SyslogAddr.sa_data)); | |
12d303b4 MK |
166 | if (LogStat & LOG_NDELAY) { |
167 | LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); | |
168 | fcntl(LogFile, F_SETFD, 1); | |
169 | } | |
5cd7a5ab | 170 | } |
12d303b4 MK |
171 | if (LogFile != -1 && !connected && |
172 | connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) | |
173 | connected = 1; | |
b97c7a10 SL |
174 | } |
175 | ||
176 | /* | |
177 | * CLOSELOG -- close the system log | |
178 | */ | |
179 | closelog() | |
180 | { | |
b97c7a10 SL |
181 | (void) close(LogFile); |
182 | LogFile = -1; | |
12d303b4 | 183 | connected = 0; |
b97c7a10 | 184 | } |
bc51229a | 185 | |
61ed6dd0 | 186 | static int LogMask = 0xff; /* mask of priorities to be logged */ |
bc51229a RC |
187 | /* |
188 | * SETLOGMASK -- set the log mask level | |
189 | */ | |
df714f23 RC |
190 | setlogmask(pmask) |
191 | int pmask; | |
bc51229a | 192 | { |
df714f23 | 193 | int omask; |
bc51229a | 194 | |
df714f23 RC |
195 | omask = LogMask; |
196 | if (pmask != 0) | |
197 | LogMask = pmask; | |
198 | return (omask); | |
bc51229a | 199 | } |