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