1/*
2 * Copyright (c) 1999-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * Copyright (c) 1993
26 *	The Regents of the University of California.  All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 *    notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 *    notice, this list of conditions and the following disclaimer in the
35 *    documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 *    must display the following acknowledgement:
38 *	This product includes software developed by the University of
39 *	California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 *    may be used to endorse or promote products derived from this software
42 *    without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#include <sys/syslog.h>
58#include <stdlib.h>
59#include <string.h>
60#include <stdint.h>
61#include <pthread.h>
62#include <dispatch/dispatch.h>
63#include <asl.h>
64#include "asl_private.h"
65
66#ifdef __STDC__
67#include <stdarg.h>
68#else
69#include <varargs.h>
70#endif
71
72#define	LOG_NO_NOTIFY	0x1000
73extern const char *asl_syslog_faciliy_num_to_name(int n);
74
75#ifdef BUILDING_VARIANT
76__private_extern__ pthread_mutex_t _sl_lock;
77__private_extern__ aslclient _sl_asl;
78__private_extern__ char *_sl_ident;
79__private_extern__ int _sl_fac;
80__private_extern__ int _sl_opts;
81__private_extern__ int _sl_mask;
82#else /* !BUILDING_VARIANT */
83__private_extern__ pthread_mutex_t _sl_lock = PTHREAD_MUTEX_INITIALIZER;
84__private_extern__ aslclient _sl_asl = NULL;
85__private_extern__ char *_sl_ident = NULL;
86__private_extern__ int _sl_fac = 0;
87__private_extern__ int _sl_opts = 0;
88__private_extern__ int _sl_mask = 0;
89#endif /* BUILDING_VARIANT */
90
91/*
92 * syslog, vsyslog --
93 *	print message on log file; output is intended for syslogd(8).
94 */
95void
96#ifdef __STDC__
97syslog(int pri, const char *fmt, ...)
98#else
99syslog(pri, fmt, va_alist)
100	int pri;
101	char *fmt;
102	va_dcl
103#endif
104{
105	va_list ap;
106
107#ifdef __STDC__
108	va_start(ap, fmt);
109#else
110	va_start(ap);
111#endif
112	vsyslog(pri, fmt, ap);
113	va_end(ap);
114}
115
116void
117vsyslog(int pri, const char *fmt, va_list ap)
118{
119	int fac;
120	aslmsg facmsg;
121	const char *facility;
122
123	facmsg = NULL;
124	fac = pri & LOG_FACMASK;
125	if (fac != 0)
126	{
127		facility = asl_syslog_faciliy_num_to_name(fac);
128		if (facility != NULL)
129		{
130			facmsg = asl_new(ASL_TYPE_MSG);
131			asl_set(facmsg, ASL_KEY_FACILITY, facility);
132		}
133	}
134
135	pthread_mutex_lock(&_sl_lock);
136
137	/* open syslog ASL client if required */
138	if (_sl_asl == NULL)
139	{
140		_sl_asl = asl_open(NULL, NULL, ASL_OPT_SYSLOG_LEGACY);
141		_sl_mask = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG);
142		asl_set_filter(_sl_asl, _sl_mask);
143	}
144
145	asl_vlog(_sl_asl, facmsg, LOG_PRI(pri), fmt, ap);
146
147	pthread_mutex_unlock(&_sl_lock);
148
149	if (facmsg != NULL) asl_free(facmsg);
150}
151
152#ifndef BUILDING_VARIANT
153
154void
155openlog(const char *ident, int opts, int logfac)
156{
157	const char *facility;
158	uint32_t asl_opts;
159
160	pthread_mutex_lock(&_sl_lock);
161
162	/* close existing aslclient */
163	asl_close(_sl_asl);
164	_sl_asl = NULL;
165
166	free(_sl_ident);
167	_sl_ident = NULL;
168
169	/* open with specified parameters */
170
171	if (ident != NULL) _sl_ident = strdup(ident);
172	/* NB we allow the strdup to fail silently */
173
174	_sl_fac = logfac;
175	facility = asl_syslog_faciliy_num_to_name(_sl_fac);
176
177	_sl_opts = opts;
178	asl_opts = ASL_OPT_SYSLOG_LEGACY;
179
180	if (_sl_opts & LOG_NO_NOTIFY) asl_opts |= ASL_OPT_NO_REMOTE;
181	if (_sl_opts & LOG_PERROR) asl_opts |= ASL_OPT_STDERR;
182
183	_sl_mask = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG);
184
185	_sl_asl = asl_open(_sl_ident, facility, asl_opts);
186	asl_set_filter(_sl_asl, _sl_mask);
187
188	pthread_mutex_unlock(&_sl_lock);
189}
190
191void
192closelog()
193{
194	pthread_mutex_lock(&_sl_lock);
195
196	if (_sl_asl != NULL) asl_close(_sl_asl);
197	_sl_asl = NULL;
198
199	if (_sl_ident != NULL) free(_sl_ident);
200	_sl_ident = NULL;
201
202	pthread_mutex_unlock(&_sl_lock);
203}
204
205/* setlogmask -- set the log mask level */
206int
207setlogmask(int mask)
208{
209	int oldmask;
210
211	if (mask == 0) return _sl_mask;
212
213	pthread_mutex_lock(&_sl_lock);
214
215	_sl_mask = mask;
216	oldmask = asl_set_filter(_sl_asl, mask);
217
218	pthread_mutex_unlock(&_sl_lock);
219
220	return oldmask;
221}
222
223#endif /* !BUILDING_VARIANT */
224