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) |
c5980113 | 9 | static char sccsid[] = "@(#)syslog.c 5.33 (Berkeley) %G%"; |
c5282f4a MK |
10 | #endif /* LIBC_SCCS and not lint */ |
11 | ||
b97c7a10 SL |
12 | #include <sys/types.h> |
13 | #include <sys/socket.h> | |
bc51229a | 14 | #include <sys/file.h> |
a52c86c2 | 15 | #include <sys/syslog.h> |
84f78f79 | 16 | #include <sys/uio.h> |
8c095963 | 17 | #include <sys/errno.h> |
b97c7a10 | 18 | #include <netdb.h> |
6ebcb998 | 19 | #include <string.h> |
c5980113 DS |
20 | #if __STDC__ |
21 | #include <stdarg.h> | |
22 | #else | |
137a7cf5 | 23 | #include <varargs.h> |
c5980113 DS |
24 | #endif |
25 | #include <time.h> | |
26 | #include <unistd.h> | |
bea3da5c | 27 | #include <paths.h> |
61ed6dd0 | 28 | #include <stdio.h> |
b97c7a10 | 29 | |
bc51229a | 30 | static int LogFile = -1; /* fd for log */ |
12d303b4 | 31 | static int connected; /* have done connect */ |
61ed6dd0 | 32 | static int LogStat = 0; /* status bits, set by openlog() */ |
c5980113 | 33 | static const char *LogTag = "syslog"; /* string to tag the entry with */ |
a05c1f2b | 34 | static int LogFacility = LOG_USER; /* default facility code */ |
b97c7a10 | 35 | |
8c095963 KB |
36 | /* |
37 | * syslog, vsyslog -- | |
38 | * print message on log file; output is intended for syslogd(8). | |
39 | */ | |
c5980113 DS |
40 | void |
41 | #if __STDC__ | |
42 | syslog(int pri, const char *fmt, ...) | |
43 | #else | |
44 | syslog(pri, fmt, va_alist) | |
45 | int pri; | |
b97c7a10 | 46 | char *fmt; |
c5980113 DS |
47 | va_dcl |
48 | #endif | |
137a7cf5 | 49 | { |
c5980113 DS |
50 | va_list ap; |
51 | ||
52 | #if __STDC__ | |
53 | va_start(ap, fmt); | |
54 | #else | |
55 | va_start(ap); | |
56 | #endif | |
57 | vsyslog(pri, fmt, ap); | |
58 | va_end(ap); | |
137a7cf5 KB |
59 | } |
60 | ||
c5980113 | 61 | void |
137a7cf5 KB |
62 | vsyslog(pri, fmt, ap) |
63 | int pri; | |
c5980113 | 64 | register const char *fmt; |
137a7cf5 | 65 | va_list ap; |
b97c7a10 | 66 | { |
61ed6dd0 KB |
67 | register int cnt; |
68 | register char *p; | |
69 | time_t now, time(); | |
27071b79 | 70 | int fd, saved_errno; |
84f78f79 | 71 | char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); |
56772c3d | 72 | |
09e4fb27 | 73 | /* check for invalid bits or no priority set */ |
4eabad57 KB |
74 | if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) |
75 | return; | |
b97c7a10 | 76 | |
09e4fb27 | 77 | saved_errno = errno; |
a05c1f2b EA |
78 | |
79 | /* set default facility if none specified */ | |
80 | if ((pri & LOG_FACMASK) == 0) | |
81 | pri |= LogFacility; | |
82 | ||
83 | /* build the message */ | |
61ed6dd0 KB |
84 | (void)time(&now); |
85 | (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); | |
86 | for (p = tbuf; *p; ++p); | |
84f78f79 KB |
87 | if (LogStat & LOG_PERROR) |
88 | stdp = p; | |
bc51229a | 89 | if (LogTag) { |
61ed6dd0 KB |
90 | (void)strcpy(p, LogTag); |
91 | for (; *p; ++p); | |
bc51229a RC |
92 | } |
93 | if (LogStat & LOG_PID) { | |
61ed6dd0 KB |
94 | (void)sprintf(p, "[%d]", getpid()); |
95 | for (; *p; ++p); | |
bc51229a | 96 | } |
a05c1f2b | 97 | if (LogTag) { |
61ed6dd0 KB |
98 | *p++ = ':'; |
99 | *p++ = ' '; | |
a05c1f2b | 100 | } |
b97c7a10 | 101 | |
56772c3d KB |
102 | /* substitute error message for %m */ |
103 | { | |
104 | register char ch, *t1, *t2; | |
105 | char *strerror(); | |
106 | ||
107 | for (t1 = fmt_cpy; ch = *fmt; ++fmt) | |
108 | if (ch == '%' && fmt[1] == 'm') { | |
109 | ++fmt; | |
110 | for (t2 = strerror(saved_errno); | |
111 | *t1 = *t2++; ++t1); | |
112 | } | |
113 | else | |
114 | *t1++ = ch; | |
c58afa87 | 115 | *t1 = '\0'; |
56772c3d KB |
116 | } |
117 | ||
118 | (void)vsprintf(p, fmt_cpy, ap); | |
a05c1f2b | 119 | |
84f78f79 KB |
120 | cnt = strlen(tbuf); |
121 | ||
122 | /* output to stderr if requested */ | |
123 | if (LogStat & LOG_PERROR) { | |
124 | struct iovec iov[2]; | |
125 | register struct iovec *v = iov; | |
126 | ||
127 | v->iov_base = stdp; | |
128 | v->iov_len = cnt - (stdp - tbuf); | |
129 | ++v; | |
130 | v->iov_base = "\n"; | |
131 | v->iov_len = 1; | |
90793b84 | 132 | (void)writev(STDERR_FILENO, iov, 2); |
84f78f79 KB |
133 | } |
134 | ||
8c095963 | 135 | /* get connected, output the message to the local logger */ |
c5980113 DS |
136 | if (!connected) |
137 | openlog(LogTag, LogStat | LOG_NDELAY, 0); | |
138 | if (send(LogFile, tbuf, cnt, 0) >= 0) | |
4eabad57 | 139 | return; |
8c095963 KB |
140 | |
141 | /* see if should attempt the console */ | |
142 | if (!(LogStat&LOG_CONS)) | |
4eabad57 | 143 | return; |
a05c1f2b | 144 | |
27071b79 | 145 | /* |
8c095963 KB |
146 | * Output the message to the console; don't worry about blocking, |
147 | * if console blocks everything will. Make sure the error reported | |
148 | * is the one from the syslogd failure. | |
27071b79 | 149 | */ |
8c095963 KB |
150 | if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { |
151 | (void)strcat(tbuf, "\r\n"); | |
152 | cnt += 2; | |
153 | p = index(tbuf, '>') + 1; | |
154 | (void)write(fd, p, cnt - (p - tbuf)); | |
155 | (void)close(fd); | |
4eabad57 | 156 | } |
b97c7a10 SL |
157 | } |
158 | ||
61ed6dd0 | 159 | static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ |
8c095963 | 160 | |
c5980113 | 161 | void |
a05c1f2b | 162 | openlog(ident, logstat, logfac) |
c5980113 | 163 | const 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; | |
8c095963 | 171 | |
12d303b4 MK |
172 | if (LogFile == -1) { |
173 | SyslogAddr.sa_family = AF_UNIX; | |
8c095963 | 174 | (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, |
bea3da5c | 175 | sizeof(SyslogAddr.sa_data)); |
12d303b4 | 176 | if (LogStat & LOG_NDELAY) { |
8c095963 | 177 | if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) |
4eabad57 | 178 | return; |
8c095963 | 179 | (void)fcntl(LogFile, F_SETFD, 1); |
12d303b4 | 180 | } |
5cd7a5ab | 181 | } |
8c095963 KB |
182 | if (LogFile != -1 && !connected) |
183 | if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { | |
8c095963 | 184 | (void)close(LogFile); |
8c095963 | 185 | LogFile = -1; |
8c095963 KB |
186 | } else |
187 | connected = 1; | |
b97c7a10 SL |
188 | } |
189 | ||
c5980113 | 190 | void |
b97c7a10 SL |
191 | closelog() |
192 | { | |
4eabad57 | 193 | (void)close(LogFile); |
b97c7a10 | 194 | LogFile = -1; |
12d303b4 | 195 | connected = 0; |
b97c7a10 | 196 | } |
bc51229a | 197 | |
61ed6dd0 | 198 | static int LogMask = 0xff; /* mask of priorities to be logged */ |
8c095963 KB |
199 | |
200 | /* setlogmask -- set the log mask level */ | |
df714f23 RC |
201 | setlogmask(pmask) |
202 | int pmask; | |
bc51229a | 203 | { |
df714f23 | 204 | int omask; |
bc51229a | 205 | |
df714f23 RC |
206 | omask = LogMask; |
207 | if (pmask != 0) | |
208 | LogMask = pmask; | |
209 | return (omask); | |
bc51229a | 210 | } |