xref: /openbsd/lib/libc/gen/syslog.c (revision 898184e3)
1 /*	$OpenBSD: syslog.c,v 1.31 2011/05/30 18:48:33 martynas Exp $ */
2 /*
3  * Copyright (c) 1983, 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/uio.h>
35 #include <sys/un.h>
36 #include <netdb.h>
37 
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <stdarg.h>
46 
47 static struct syslog_data sdata = SYSLOG_DATA_INIT;
48 
49 void	__vsyslog_r(int pri, struct syslog_data *, size_t (*)(char *, size_t),
50     const char *, va_list);
51 
52 static size_t
53 gettime(char *buf, size_t maxsize)
54 {
55 	time_t	now;
56 
57 	(void)time(&now);
58 	return (strftime(buf, maxsize, "%h %e %T ", localtime(&now)));
59 }
60 
61 
62 /*
63  * syslog, vsyslog --
64  *	print message on log file; output is intended for syslogd(8).
65  */
66 /* PRINTFLIKE2 */
67 void
68 syslog(int pri, const char *fmt, ...)
69 {
70 	va_list ap;
71 
72 	va_start(ap, fmt);
73 	vsyslog(pri, fmt, ap);
74 	va_end(ap);
75 }
76 
77 void
78 vsyslog(int pri, const char *fmt, va_list ap)
79 {
80 	__vsyslog_r(pri, &sdata, &gettime, fmt, ap);
81 }
82 
83 void
84 openlog(const char *ident, int logstat, int logfac)
85 {
86 	openlog_r(ident, logstat, logfac, &sdata);
87 }
88 
89 void
90 closelog(void)
91 {
92 	closelog_r(&sdata);
93 }
94 
95 /* setlogmask -- set the log mask level */
96 int
97 setlogmask(int pmask)
98 {
99 	return setlogmask_r(pmask, &sdata);
100 }
101 
102 /* setlogmask -- set the log mask level */
103 int
104 setlogmask_r(int pmask, struct syslog_data *data)
105 {
106 	int omask;
107 
108 	omask = data->log_mask;
109 	if (pmask != 0)
110 		data->log_mask = pmask;
111 	return (omask);
112 }
113