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) |
56772c3d | 19 | static char sccsid[] = "@(#)syslog.c 5.19 (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> | |
b97c7a10 | 43 | #include <netdb.h> |
a52c86c2 | 44 | #include <strings.h> |
137a7cf5 | 45 | #include <varargs.h> |
61ed6dd0 | 46 | #include <stdio.h> |
b97c7a10 | 47 | |
61ed6dd0 KB |
48 | #define LOGNAME "/dev/log" |
49 | #define CONSOLE "/dev/console" | |
b97c7a10 | 50 | |
bc51229a | 51 | static int LogFile = -1; /* fd for log */ |
12d303b4 | 52 | static int connected; /* have done connect */ |
61ed6dd0 | 53 | static int LogStat = 0; /* status bits, set by openlog() */ |
a05c1f2b | 54 | static char *LogTag = "syslog"; /* string to tag the entry with */ |
a05c1f2b | 55 | static int LogFacility = LOG_USER; /* default facility code */ |
b97c7a10 | 56 | |
61ed6dd0 KB |
57 | syslog(pri, fmt, args) |
58 | int pri, args; | |
b97c7a10 | 59 | char *fmt; |
137a7cf5 KB |
60 | { |
61 | vsyslog(pri, fmt, &args); | |
62 | } | |
63 | ||
64 | vsyslog(pri, fmt, ap) | |
65 | int pri; | |
56772c3d | 66 | register char *fmt; |
137a7cf5 | 67 | va_list ap; |
b97c7a10 | 68 | { |
56772c3d | 69 | extern int errno; |
61ed6dd0 KB |
70 | register int cnt; |
71 | register char *p; | |
72 | time_t now, time(); | |
56772c3d KB |
73 | int pid, saved_errno; |
74 | char tbuf[2048], fmt_cpy[1024], *ctime(); | |
75 | ||
76 | saved_errno = errno; | |
b97c7a10 | 77 | |
b97c7a10 | 78 | /* see if we should just throw out this message */ |
61ed6dd0 KB |
79 | if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || |
80 | !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) | |
b97c7a10 | 81 | return; |
12d303b4 | 82 | if (LogFile < 0 || !connected) |
fadfb2f6 | 83 | openlog(LogTag, LogStat | LOG_NDELAY, 0); |
a05c1f2b EA |
84 | |
85 | /* set default facility if none specified */ | |
86 | if ((pri & LOG_FACMASK) == 0) | |
87 | pri |= LogFacility; | |
88 | ||
89 | /* build the message */ | |
61ed6dd0 KB |
90 | (void)time(&now); |
91 | (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); | |
92 | for (p = tbuf; *p; ++p); | |
bc51229a | 93 | if (LogTag) { |
61ed6dd0 KB |
94 | (void)strcpy(p, LogTag); |
95 | for (; *p; ++p); | |
bc51229a RC |
96 | } |
97 | if (LogStat & LOG_PID) { | |
61ed6dd0 KB |
98 | (void)sprintf(p, "[%d]", getpid()); |
99 | for (; *p; ++p); | |
bc51229a | 100 | } |
a05c1f2b | 101 | if (LogTag) { |
61ed6dd0 KB |
102 | *p++ = ':'; |
103 | *p++ = ' '; | |
a05c1f2b | 104 | } |
b97c7a10 | 105 | |
56772c3d KB |
106 | /* substitute error message for %m */ |
107 | { | |
108 | register char ch, *t1, *t2; | |
109 | char *strerror(); | |
110 | ||
111 | for (t1 = fmt_cpy; ch = *fmt; ++fmt) | |
112 | if (ch == '%' && fmt[1] == 'm') { | |
113 | ++fmt; | |
114 | for (t2 = strerror(saved_errno); | |
115 | *t1 = *t2++; ++t1); | |
116 | } | |
117 | else | |
118 | *t1++ = ch; | |
119 | } | |
120 | ||
121 | (void)vsprintf(p, fmt_cpy, ap); | |
a05c1f2b EA |
122 | |
123 | /* output the message to the local logger */ | |
61ed6dd0 KB |
124 | if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 || |
125 | !(LogStat&LOG_CONS)) | |
bc51229a | 126 | return; |
a05c1f2b EA |
127 | |
128 | /* output the message to the console */ | |
fe98dde4 | 129 | pid = vfork(); |
bc51229a RC |
130 | if (pid == -1) |
131 | return; | |
132 | if (pid == 0) { | |
ad1bd1a6 | 133 | int fd; |
61ed6dd0 KB |
134 | long sigsetmask(); |
135 | ||
136 | (void)signal(SIGALRM, SIG_DFL); | |
137 | sigsetmask((long)~sigmask(SIGALRM)); | |
138 | (void)alarm((u_int)5); | |
139 | if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) | |
140 | return; | |
141 | (void)alarm((u_int)0); | |
142 | (void)strcat(tbuf, "\r"); | |
143 | p = index(tbuf, '>') + 1; | |
144 | (void)write(fd, p, cnt + 1 - (p - tbuf)); | |
145 | (void)close(fd); | |
ad1bd1a6 | 146 | _exit(0); |
bc51229a | 147 | } |
c229b84c | 148 | if (!(LogStat & LOG_NOWAIT)) |
61ed6dd0 | 149 | while ((cnt = wait((int *)0)) > 0 && cnt != pid); |
b97c7a10 SL |
150 | } |
151 | ||
61ed6dd0 | 152 | static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ |
b97c7a10 SL |
153 | /* |
154 | * OPENLOG -- open system log | |
155 | */ | |
a05c1f2b | 156 | openlog(ident, logstat, logfac) |
b97c7a10 | 157 | char *ident; |
a05c1f2b | 158 | int logstat, logfac; |
b97c7a10 | 159 | { |
a05c1f2b EA |
160 | if (ident != NULL) |
161 | LogTag = ident; | |
b97c7a10 | 162 | LogStat = logstat; |
c5282f4a MK |
163 | if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) |
164 | LogFacility = logfac; | |
12d303b4 MK |
165 | if (LogFile == -1) { |
166 | SyslogAddr.sa_family = AF_UNIX; | |
61ed6dd0 | 167 | strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); |
12d303b4 MK |
168 | if (LogStat & LOG_NDELAY) { |
169 | LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); | |
170 | fcntl(LogFile, F_SETFD, 1); | |
171 | } | |
5cd7a5ab | 172 | } |
12d303b4 MK |
173 | if (LogFile != -1 && !connected && |
174 | connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) | |
175 | connected = 1; | |
b97c7a10 SL |
176 | } |
177 | ||
178 | /* | |
179 | * CLOSELOG -- close the system log | |
180 | */ | |
181 | closelog() | |
182 | { | |
b97c7a10 SL |
183 | (void) close(LogFile); |
184 | LogFile = -1; | |
12d303b4 | 185 | connected = 0; |
b97c7a10 | 186 | } |
bc51229a | 187 | |
61ed6dd0 | 188 | static int LogMask = 0xff; /* mask of priorities to be logged */ |
bc51229a RC |
189 | /* |
190 | * SETLOGMASK -- set the log mask level | |
191 | */ | |
df714f23 RC |
192 | setlogmask(pmask) |
193 | int pmask; | |
bc51229a | 194 | { |
df714f23 | 195 | int omask; |
bc51229a | 196 | |
df714f23 RC |
197 | omask = LogMask; |
198 | if (pmask != 0) | |
199 | LogMask = pmask; | |
200 | return (omask); | |
bc51229a | 201 | } |