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