add Berkeley specific header, written for compatibility
[unix-history] / usr / src / lib / libc / gen / syslog.c
CommitLineData
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)
9e31603f 19static char sccsid[] = "@(#)syslog.c 5.16 (Berkeley) %G%";
c5282f4a
MK
20#endif /* LIBC_SCCS and not lint */
21
b97c7a10
SL
22/*
23 * SYSLOG -- print message on log file
24 *
25 * This routine looks a lot like printf, except that it
26 * outputs to the log file instead of the standard output.
bc51229a
RC
27 * Also:
28 * adds a timestamp,
29 * prints the module name in front of the message,
30 * has some other formatting types (or will sometime),
31 * adds a newline on the end of the message.
b97c7a10 32 *
bc51229a 33 * The output of this routine is intended to be read by /etc/syslogd.
4d707151
RC
34 *
35 * Author: Eric Allman
36 * Modified to use UNIX domain IPC by Ralph Campbell
b97c7a10 37 */
bc51229a 38
b97c7a10
SL
39#include <sys/types.h>
40#include <sys/socket.h>
bc51229a 41#include <sys/file.h>
a52c86c2
MK
42#include <sys/signal.h>
43#include <sys/syslog.h>
b97c7a10 44#include <netdb.h>
a52c86c2 45#include <strings.h>
b97c7a10 46
bc51229a
RC
47#define MAXLINE 1024 /* max message size */
48#define NULL 0 /* manifest */
49
a05c1f2b 50#define IMPORTANT LOG_ERR
5cd7a5ab 51
bc51229a
RC
52static char logname[] = "/dev/log";
53static char ctty[] = "/dev/console";
b97c7a10 54
bc51229a 55static int LogFile = -1; /* fd for log */
12d303b4 56static int connected; /* have done connect */
bc51229a 57static int LogStat = 0; /* status bits, set by openlog() */
a05c1f2b
EA
58static char *LogTag = "syslog"; /* string to tag the entry with */
59static int LogMask = 0xff; /* mask of priorities to be logged */
60static int LogFacility = LOG_USER; /* default facility code */
b97c7a10 61
a05c1f2b 62static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
b97c7a10
SL
63
64extern int errno, sys_nerr;
65extern char *sys_errlist[];
66
67syslog(pri, fmt, p0, p1, p2, p3, p4)
68 int pri;
69 char *fmt;
70{
bc51229a
RC
71 char buf[MAXLINE + 1], outline[MAXLINE + 1];
72 register char *b, *f, *o;
73 register int c;
74 long now;
4d707151 75 int pid, olderrno = errno;
b97c7a10 76
b97c7a10 77 /* see if we should just throw out this message */
12d303b4
MK
78 if ((unsigned) LOG_FAC(pri) >= LOG_NFACILITIES ||
79 LOG_MASK(LOG_PRI(pri)) == 0 ||
17d49c2f 80 (pri &~ (LOG_PRIMASK|LOG_FACMASK)) != 0)
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 */
bc51229a 90 o = outline;
06eef7c4 91 (void)sprintf(o, "<%d>", pri);
4d707151 92 o += strlen(o);
a05c1f2b 93 time(&now);
06eef7c4 94 (void)sprintf(o, "%.15s ", ctime(&now) + 4);
a05c1f2b 95 o += strlen(o);
bc51229a
RC
96 if (LogTag) {
97 strcpy(o, LogTag);
98 o += strlen(o);
99 }
100 if (LogStat & LOG_PID) {
06eef7c4 101 (void)sprintf(o, "[%d]", getpid());
bc51229a
RC
102 o += strlen(o);
103 }
a05c1f2b
EA
104 if (LogTag) {
105 strcpy(o, ": ");
106 o += 2;
107 }
b97c7a10 108
bc51229a
RC
109 b = buf;
110 f = fmt;
111 while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
112 if (c != '%') {
113 *b++ = c;
114 continue;
b97c7a10 115 }
bc51229a
RC
116 if ((c = *f++) != 'm') {
117 *b++ = '%';
118 *b++ = c;
119 continue;
b97c7a10 120 }
4d707151 121 if ((unsigned)olderrno > sys_nerr)
06eef7c4 122 (void)sprintf(b, "error %d", olderrno);
b97c7a10 123 else
4d707151 124 strcpy(b, sys_errlist[olderrno]);
bc51229a 125 b += strlen(b);
b97c7a10 126 }
bc51229a
RC
127 *b++ = '\n';
128 *b = '\0';
06eef7c4 129 (void)sprintf(o, buf, p0, p1, p2, p3, p4);
bc51229a
RC
130 c = strlen(outline);
131 if (c > MAXLINE)
132 c = MAXLINE;
a05c1f2b
EA
133
134 /* output the message to the local logger */
12d303b4 135 if (send(LogFile, outline, c, 0) >= 0)
bc51229a 136 return;
c229b84c 137 if (!(LogStat & LOG_CONS))
bc51229a 138 return;
a05c1f2b
EA
139
140 /* output the message to the console */
fe98dde4 141 pid = vfork();
bc51229a
RC
142 if (pid == -1)
143 return;
144 if (pid == 0) {
ad1bd1a6
MK
145 int fd;
146
ba519163 147 signal(SIGALRM, SIG_DFL);
1b43787d 148 sigsetmask(sigblock(0L) & ~sigmask(SIGALRM));
ba519163 149 alarm(5);
ad1bd1a6 150 fd = open(ctty, O_WRONLY);
ba519163 151 alarm(0);
5cd7a5ab 152 strcat(o, "\r");
a52c86c2 153 o = index(outline, '>') + 1;
ad1bd1a6
MK
154 write(fd, o, c + 1 - (o - outline));
155 close(fd);
156 _exit(0);
bc51229a 157 }
c229b84c
MK
158 if (!(LogStat & LOG_NOWAIT))
159 while ((c = wait((int *)0)) > 0 && c != pid)
160 ;
b97c7a10
SL
161}
162
163/*
164 * OPENLOG -- open system log
165 */
a05c1f2b
EA
166
167openlog(ident, logstat, logfac)
b97c7a10 168 char *ident;
a05c1f2b 169 int logstat, logfac;
b97c7a10 170{
a05c1f2b
EA
171 if (ident != NULL)
172 LogTag = ident;
b97c7a10 173 LogStat = logstat;
c5282f4a
MK
174 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
175 LogFacility = logfac;
12d303b4
MK
176 if (LogFile == -1) {
177 SyslogAddr.sa_family = AF_UNIX;
178 strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
179 if (LogStat & LOG_NDELAY) {
180 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
181 fcntl(LogFile, F_SETFD, 1);
182 }
5cd7a5ab 183 }
12d303b4
MK
184 if (LogFile != -1 && !connected &&
185 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
186 connected = 1;
b97c7a10
SL
187}
188
189/*
190 * CLOSELOG -- close the system log
191 */
fadfb2f6 192
b97c7a10
SL
193closelog()
194{
195
196 (void) close(LogFile);
197 LogFile = -1;
12d303b4 198 connected = 0;
b97c7a10 199}
bc51229a
RC
200
201/*
202 * SETLOGMASK -- set the log mask level
203 */
df714f23
RC
204setlogmask(pmask)
205 int pmask;
bc51229a 206{
df714f23 207 int omask;
bc51229a 208
df714f23
RC
209 omask = LogMask;
210 if (pmask != 0)
211 LogMask = pmask;
212 return (omask);
bc51229a 213}