xref: /titanic_52/usr/src/cmd/nscd/nscd_log.c (revision 18bdb8a7484e018149ac9a2766c97bdea9752c87)
1cb5caa98Sdjl /*
2cb5caa98Sdjl  * CDDL HEADER START
3cb5caa98Sdjl  *
4cb5caa98Sdjl  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
7cb5caa98Sdjl  *
8cb5caa98Sdjl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9cb5caa98Sdjl  * or http://www.opensolaris.org/os/licensing.
10cb5caa98Sdjl  * See the License for the specific language governing permissions
11cb5caa98Sdjl  * and limitations under the License.
12cb5caa98Sdjl  *
13cb5caa98Sdjl  * When distributing Covered Code, include this CDDL HEADER in each
14cb5caa98Sdjl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15cb5caa98Sdjl  * If applicable, add the following below this CDDL HEADER, with the
16cb5caa98Sdjl  * fields enclosed by brackets "[]" replaced with your own identifying
17cb5caa98Sdjl  * information: Portions Copyright [yyyy] [name of copyright owner]
18cb5caa98Sdjl  *
19cb5caa98Sdjl  * CDDL HEADER END
20cb5caa98Sdjl  */
21cb5caa98Sdjl /*
22*18bdb8a7Smichen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23cb5caa98Sdjl  * Use is subject to license terms.
24cb5caa98Sdjl  */
25cb5caa98Sdjl 
26cb5caa98Sdjl #pragma ident	"%Z%%M%	%I%	%E% SMI"
27cb5caa98Sdjl 
28cb5caa98Sdjl #include <stdlib.h>
29cb5caa98Sdjl #include <locale.h>
30cb5caa98Sdjl #include <limits.h>
31cb5caa98Sdjl #include <fcntl.h>
32cb5caa98Sdjl #include <sys/stat.h>
33cb5caa98Sdjl #include <sys/varargs.h>
34cb5caa98Sdjl #include <synch.h>
35cb5caa98Sdjl #include <thread.h>
36cb5caa98Sdjl #include <string.h>
37cb5caa98Sdjl #include <unistd.h>
38cb5caa98Sdjl #include "nscd_log.h"
39cb5caa98Sdjl #include "nscd_config.h"
40cb5caa98Sdjl #include "nscd_switch.h"
41cb5caa98Sdjl #include "cache.h"
42cb5caa98Sdjl 
43cb5caa98Sdjl /*
44cb5caa98Sdjl  * old nscd debug levels
45cb5caa98Sdjl  */
46cb5caa98Sdjl #define	DBG_OFF		0
47cb5caa98Sdjl #define	DBG_CANT_FIND	2
48cb5caa98Sdjl #define	DBG_NETLOOKUPS	4
49cb5caa98Sdjl #define	DBG_ALL		6
50cb5caa98Sdjl 
51cb5caa98Sdjl /* max. chars in a nscd log entry */
52cb5caa98Sdjl #define	LOGBUFLEN	1024
53cb5caa98Sdjl 
54cb5caa98Sdjl /* configuration for the nscd log component */
55cb5caa98Sdjl int		_nscd_log_comp = 0x0;
56cb5caa98Sdjl int		_nscd_log_level = 0x0;
57*18bdb8a7Smichen static char	_nscd_logfile[PATH_MAX] = { 0 };
58*18bdb8a7Smichen 
59*18bdb8a7Smichen #define	NSCD_DEBUG_NONE		'0'
60*18bdb8a7Smichen #define	NSCD_DEBUG_OPEN		'1'
61*18bdb8a7Smichen #define	NSCD_DEBUG_CLOSE	'2'
62*18bdb8a7Smichen 
63*18bdb8a7Smichen static char	_nscd_debug = NSCD_DEBUG_NONE;
64*18bdb8a7Smichen static char	_nscd_logfile_d[PATH_MAX] = { 0 };
65*18bdb8a7Smichen static char	_nscd_logfile_s[PATH_MAX] = { 0 };
66cb5caa98Sdjl 
67cb5caa98Sdjl /* statistics data */
68cb5caa98Sdjl static nscd_cfg_stat_global_log_t logstats = {
69cb5caa98Sdjl 	NSCD_CFG_STAT_GROUP_INFO_GLOBAL_LOG, 0 };
70cb5caa98Sdjl 
71cb5caa98Sdjl /* if no log file specified, log entry goes to stderr */
72cb5caa98Sdjl int _logfd = 2;
73cb5caa98Sdjl 
74*18bdb8a7Smichen 
75cb5caa98Sdjl /* close old log file and open a new one */
76cb5caa98Sdjl static nscd_rc_t
77cb5caa98Sdjl _nscd_set_lf(
78cb5caa98Sdjl 	char	*lf)
79cb5caa98Sdjl {
80cb5caa98Sdjl 	int	newlogfd;
81cb5caa98Sdjl 	char	*me = "_nscd_set_lf";
82cb5caa98Sdjl 
83cb5caa98Sdjl 	/*
84cb5caa98Sdjl 	 *  don't try and open the log file /dev/null
85cb5caa98Sdjl 	 */
86cb5caa98Sdjl 	if (lf == NULL || *lf == 0) {
87cb5caa98Sdjl 		/* ignore empty log file specs */
88cb5caa98Sdjl 		return (NSCD_SUCCESS);
89cb5caa98Sdjl 	} else if (strcmp(lf, "/dev/null") == 0) {
90*18bdb8a7Smichen 		(void) strlcpy(_nscd_logfile, lf, PATH_MAX);
91cb5caa98Sdjl 		if (_logfd >= 0)
92cb5caa98Sdjl 			(void) close(_logfd);
93cb5caa98Sdjl 		_logfd = -1;
94cb5caa98Sdjl 		return (NSCD_SUCCESS);
95cb5caa98Sdjl 	} else if (strcmp(lf, "stderr") == 0) {
96*18bdb8a7Smichen 		(void) strlcpy(_nscd_logfile, lf, PATH_MAX);
97cb5caa98Sdjl 		if (_logfd != -1 && _logfd != 2)
98cb5caa98Sdjl 			(void) close(_logfd);
99cb5caa98Sdjl 		_logfd = 2;
100cb5caa98Sdjl 		return (NSCD_SUCCESS);
101cb5caa98Sdjl 	} else {
102cb5caa98Sdjl 
103cb5caa98Sdjl 		/*
104cb5caa98Sdjl 		 * In order to open this file securely, we'll try a few tricks
105cb5caa98Sdjl 		 */
106cb5caa98Sdjl 
107cb5caa98Sdjl 		if ((newlogfd = open(lf, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
108cb5caa98Sdjl 			/*
109cb5caa98Sdjl 			 * File already exists... now we need to get cute
110cb5caa98Sdjl 			 * since opening a file in a world-writeable directory
111cb5caa98Sdjl 			 * safely is hard = it could be a hard link or a
112cb5caa98Sdjl 			 * symbolic link to a system file.
113cb5caa98Sdjl 			 */
114cb5caa98Sdjl 			struct stat before;
115cb5caa98Sdjl 
116cb5caa98Sdjl 			if (lstat(lf, &before) < 0) {
117*18bdb8a7Smichen 				if (_nscd_debug == NSCD_DEBUG_NONE)
118cb5caa98Sdjl 					_nscd_logit(me, "Cannot open new "
119*18bdb8a7Smichen 					    "logfile \"%s\": %sn",
120*18bdb8a7Smichen 					    lf, strerror(errno));
121cb5caa98Sdjl 				return (NSCD_CFG_FILE_OPEN_ERROR);
122cb5caa98Sdjl 			}
123cb5caa98Sdjl 
124cb5caa98Sdjl 			if (S_ISREG(before.st_mode) && /* no symbolic links */
125cb5caa98Sdjl 			    (before.st_nlink == 1) && /* no hard links */
126cb5caa98Sdjl 			    (before.st_uid == 0)) {   /* owned by root */
127cb5caa98Sdjl 				if ((newlogfd =
128cb5caa98Sdjl 				    open(lf, O_APPEND|O_WRONLY, 0644)) < 0) {
129*18bdb8a7Smichen 					if (_nscd_debug == NSCD_DEBUG_NONE)
130*18bdb8a7Smichen 						_nscd_logit(me,
131*18bdb8a7Smichen 						    "Cannot open new "\
132cb5caa98Sdjl 						    "logfile \"%s\": %s\n", lf,
133cb5caa98Sdjl 						    strerror(errno));
134cb5caa98Sdjl 					return (NSCD_CFG_FILE_OPEN_ERROR);
135cb5caa98Sdjl 				}
136cb5caa98Sdjl 			} else {
137*18bdb8a7Smichen 				if (_nscd_debug == NSCD_DEBUG_NONE)
138cb5caa98Sdjl 					_nscd_logit(me, "Cannot use specified "
139cb5caa98Sdjl 					    "logfile \"%s\": "\
140*18bdb8a7Smichen 					    "file is/has links or isn't "
141*18bdb8a7Smichen 					    "owned by root\n", lf);
142cb5caa98Sdjl 				return (NSCD_CFG_FILE_OPEN_ERROR);
143cb5caa98Sdjl 			}
144cb5caa98Sdjl 		}
145cb5caa98Sdjl 
146cb5caa98Sdjl 		(void) close(_logfd);
147*18bdb8a7Smichen 		(void) strlcpy(_nscd_logfile, lf, PATH_MAX);
148cb5caa98Sdjl 		_logfd = newlogfd;
149*18bdb8a7Smichen 		if (_nscd_debug == NSCD_DEBUG_NONE)
150cb5caa98Sdjl 			_nscd_logit(me, "Start of new logfile %s\n", lf);
151cb5caa98Sdjl 	}
152cb5caa98Sdjl 	return (NSCD_SUCCESS);
153cb5caa98Sdjl }
154cb5caa98Sdjl 
155cb5caa98Sdjl 
156cb5caa98Sdjl /* log an entry to the configured nscd log file */
157cb5caa98Sdjl void
158cb5caa98Sdjl _nscd_logit(
159cb5caa98Sdjl 	char		*funcname,
160cb5caa98Sdjl 	char		*format,
161cb5caa98Sdjl 	...)
162cb5caa98Sdjl {
163cb5caa98Sdjl 	static mutex_t	loglock = DEFAULTMUTEX;
164cb5caa98Sdjl 	struct timeval	tv;
165cb5caa98Sdjl 	char		tid_buf[32];
166cb5caa98Sdjl 	char		pid_buf[32];
167cb5caa98Sdjl 	char		buffer[LOGBUFLEN];
168cb5caa98Sdjl 	int		safechars, offset;
169cb5caa98Sdjl 	va_list		ap;
170cb5caa98Sdjl 
171cb5caa98Sdjl 	if (_logfd < 0)
172cb5caa98Sdjl 		return;
173cb5caa98Sdjl 
174*18bdb8a7Smichen 	if (_nscd_debug == NSCD_DEBUG_OPEN) {
175*18bdb8a7Smichen 		(void) mutex_lock(&loglock);
176*18bdb8a7Smichen 		if (_nscd_debug == NSCD_DEBUG_OPEN &&
177*18bdb8a7Smichen 		    *_nscd_logfile_d != '\0' &&
178*18bdb8a7Smichen 		    (strcmp(_nscd_logfile, "/dev/null") == 0 ||
179*18bdb8a7Smichen 		    strcmp(_nscd_logfile, "stderr") == 0)) {
180*18bdb8a7Smichen 			(void) strlcpy(_nscd_logfile_s,
181*18bdb8a7Smichen 			    _nscd_logfile, PATH_MAX);
182*18bdb8a7Smichen 			(void) _nscd_set_lf(_nscd_logfile_d);
183*18bdb8a7Smichen 		}
184*18bdb8a7Smichen 		_nscd_debug = NSCD_DEBUG_NONE;
185*18bdb8a7Smichen 		(void) mutex_unlock(&loglock);
186*18bdb8a7Smichen 	} else if (_nscd_debug == NSCD_DEBUG_CLOSE) {
187*18bdb8a7Smichen 		(void) mutex_lock(&loglock);
188*18bdb8a7Smichen 		if (_nscd_debug == NSCD_DEBUG_CLOSE)
189*18bdb8a7Smichen 			(void) _nscd_set_lf(_nscd_logfile_s);
190*18bdb8a7Smichen 		_nscd_debug = NSCD_DEBUG_NONE;
191*18bdb8a7Smichen 		(void) mutex_unlock(&loglock);
192*18bdb8a7Smichen 	}
193*18bdb8a7Smichen 
194cb5caa98Sdjl 	va_start(ap, format);
195cb5caa98Sdjl 
196cb5caa98Sdjl 	if (gettimeofday(&tv, NULL) != 0 ||
197cb5caa98Sdjl 	    ctime_r(&tv.tv_sec, buffer, LOGBUFLEN) == NULL) {
198cb5caa98Sdjl 		(void) snprintf(buffer, LOGBUFLEN,
199cb5caa98Sdjl 		    "<time conversion failed>\t");
200cb5caa98Sdjl 	} else {
201cb5caa98Sdjl 		(void) sprintf(tid_buf, "--%d", thr_self());
202cb5caa98Sdjl 		(void) sprintf(pid_buf, "--%ld", getpid());
203cb5caa98Sdjl 		/*
204cb5caa98Sdjl 		 * ctime_r() includes some stuff we don't want;
205cb5caa98Sdjl 		 * adjust length to overwrite " YYYY\n" and
206cb5caa98Sdjl 		 * include tid string length.
207cb5caa98Sdjl 		 */
208cb5caa98Sdjl 		offset = strlen(buffer) - 6;
209cb5caa98Sdjl 		safechars = LOGBUFLEN - (offset - 1);
210cb5caa98Sdjl 		(void) snprintf(buffer + offset,
211cb5caa98Sdjl 		    safechars, ".%.4ld%s%s\t%s:\n\t\t",
212cb5caa98Sdjl 		    tv.tv_usec/100, tid_buf, pid_buf,
213cb5caa98Sdjl 		    funcname);
214cb5caa98Sdjl 	}
215cb5caa98Sdjl 	offset = strlen(buffer);
216cb5caa98Sdjl 	safechars = LOGBUFLEN - (offset - 1);
217cb5caa98Sdjl 	/*LINTED: E_SEC_PRINTF_VAR_FMT*/
218cb5caa98Sdjl 	if (vsnprintf(buffer + offset, safechars, format, ap) >
219cb5caa98Sdjl 	    safechars) {
220cb5caa98Sdjl 		(void) strncat(buffer, "...\n", LOGBUFLEN);
221cb5caa98Sdjl 	}
222cb5caa98Sdjl 
223cb5caa98Sdjl 	(void) mutex_lock(&loglock);
224cb5caa98Sdjl 	(void) write(_logfd, buffer, strlen(buffer));
225cb5caa98Sdjl 	logstats.entries_logged++;
226cb5caa98Sdjl 	(void) mutex_unlock(&loglock);
227cb5caa98Sdjl 
228cb5caa98Sdjl 	va_end(ap);
229cb5caa98Sdjl }
230cb5caa98Sdjl 
231*18bdb8a7Smichen /*
232*18bdb8a7Smichen  * Map old nscd debug level (0 -10) to log level:
233*18bdb8a7Smichen  *      -- >= 6: DBG_ALL 		--> NSCD_LOG_LEVEL_ALL
234*18bdb8a7Smichen  *      -- >= 4: DBG_DBG_NETLOOKUPS 	--> NSCD_LOG_LEVEL_CANT_FIND
235*18bdb8a7Smichen  *      -- >= 2: DBG_CANT_FIND 		--> NSCD_LOG_LEVEL_CANT_FIND
236*18bdb8a7Smichen  *      -- >= 0: DBG_OFF 		--> NSCD_LOG_LEVEL_NONE
237*18bdb8a7Smichen  */
238*18bdb8a7Smichen static int
239*18bdb8a7Smichen debug_to_log_level(
240*18bdb8a7Smichen 	int	level)
241*18bdb8a7Smichen {
242*18bdb8a7Smichen 	if (level >= 0 && level <= 10) {
243*18bdb8a7Smichen 		if (level >= DBG_ALL)
244*18bdb8a7Smichen 			return (NSCD_LOG_LEVEL_ALL);
245*18bdb8a7Smichen 		else if (level >= DBG_NETLOOKUPS)
246*18bdb8a7Smichen 			return (NSCD_LOG_LEVEL_CANT_FIND);
247*18bdb8a7Smichen 		else if (level >= DBG_CANT_FIND)
248*18bdb8a7Smichen 			return (NSCD_LOG_LEVEL_CANT_FIND);
249*18bdb8a7Smichen 		else if (level >= DBG_OFF)
250*18bdb8a7Smichen 			return (NSCD_LOG_LEVEL_NONE);
251*18bdb8a7Smichen 	}
252*18bdb8a7Smichen 	return (level);
253*18bdb8a7Smichen }
254cb5caa98Sdjl 
255cb5caa98Sdjl /* ARGSUSED */
256cb5caa98Sdjl nscd_rc_t
257cb5caa98Sdjl _nscd_cfg_log_notify(
258cb5caa98Sdjl 	void				*data,
259cb5caa98Sdjl 	struct nscd_cfg_param_desc	*pdesc,
260cb5caa98Sdjl 	nscd_cfg_id_t			*nswdb,
261cb5caa98Sdjl 	nscd_cfg_flag_t			dflag,
262cb5caa98Sdjl 	nscd_cfg_error_t		**errorp,
263cb5caa98Sdjl 	void				*cookie)
264cb5caa98Sdjl {
265cb5caa98Sdjl 
266cb5caa98Sdjl 	nscd_cfg_global_log_t		*logcfg;
267cb5caa98Sdjl 	int				off;
268cb5caa98Sdjl 
269cb5caa98Sdjl 	/*
270cb5caa98Sdjl 	 * At init time, the whole group of config params are received.
271cb5caa98Sdjl 	 * At update time, group or individual parameter value could
272cb5caa98Sdjl 	 * be received.
273cb5caa98Sdjl 	 */
274cb5caa98Sdjl 
275cb5caa98Sdjl 	if (_nscd_cfg_flag_is_set(dflag, NSCD_CFG_DFLAG_GROUP)) {
276cb5caa98Sdjl 
277cb5caa98Sdjl 		logcfg = (nscd_cfg_global_log_t *)data;
278cb5caa98Sdjl 
279cb5caa98Sdjl 		_nscd_log_comp = logcfg->debug_comp;
280cb5caa98Sdjl 		_nscd_log_level = logcfg->debug_level;
281cb5caa98Sdjl 
282cb5caa98Sdjl 		/*
283cb5caa98Sdjl 		 * logcfg->logfile should have been opened
284cb5caa98Sdjl 		 * by _nscd_cfg_log_verify()
285cb5caa98Sdjl 		 */
286cb5caa98Sdjl 
287cb5caa98Sdjl 		return (NSCD_SUCCESS);
288cb5caa98Sdjl 	}
289cb5caa98Sdjl 
290cb5caa98Sdjl 	/*
291cb5caa98Sdjl 	 * individual config parameter
292cb5caa98Sdjl 	 */
293cb5caa98Sdjl 	off = offsetof(nscd_cfg_global_log_t, debug_comp);
294cb5caa98Sdjl 	if (pdesc->p_offset == off) {
295cb5caa98Sdjl 		_nscd_log_comp = *(nscd_cfg_bitmap_t *)data;
296cb5caa98Sdjl 		return (NSCD_SUCCESS);
297cb5caa98Sdjl 	}
298cb5caa98Sdjl 
299cb5caa98Sdjl 	off = offsetof(nscd_cfg_global_log_t, debug_level);
300cb5caa98Sdjl 	if (pdesc->p_offset == off)
301cb5caa98Sdjl 		_nscd_log_level = *(nscd_cfg_bitmap_t *)data;
302cb5caa98Sdjl 
303cb5caa98Sdjl 	/*
304cb5caa98Sdjl 	 * logcfg->logfile should have been opened
305cb5caa98Sdjl 	 * by _nscd_cfg_log_verify()
306cb5caa98Sdjl 	 */
307cb5caa98Sdjl 
308cb5caa98Sdjl 	return (NSCD_SUCCESS);
309cb5caa98Sdjl }
310cb5caa98Sdjl 
311cb5caa98Sdjl /* ARGSUSED */
312cb5caa98Sdjl nscd_rc_t
313cb5caa98Sdjl _nscd_cfg_log_verify(
314cb5caa98Sdjl 	void				*data,
315cb5caa98Sdjl 	struct	nscd_cfg_param_desc	*pdesc,
316cb5caa98Sdjl 	nscd_cfg_id_t			*nswdb,
317cb5caa98Sdjl 	nscd_cfg_flag_t			dflag,
318cb5caa98Sdjl 	nscd_cfg_error_t		**errorp,
319cb5caa98Sdjl 	void				**cookie)
320cb5caa98Sdjl {
321cb5caa98Sdjl 	nscd_cfg_global_log_t		*logcfg;
322cb5caa98Sdjl 	nscd_cfg_bitmap_t		bt;
323cb5caa98Sdjl 	int				off;
324cb5caa98Sdjl 
325cb5caa98Sdjl 	/*
326cb5caa98Sdjl 	 * There is no switch db specific config params
327cb5caa98Sdjl 	 * for the nscd log component. It is a bug if
328cb5caa98Sdjl 	 * the input param description is global.
329cb5caa98Sdjl 	 */
330cb5caa98Sdjl 	if (_nscd_cfg_flag_is_not_set(pdesc->pflag, NSCD_CFG_PFLAG_GLOBAL))
331cb5caa98Sdjl 		return (NSCD_CFG_PARAM_DESC_ERROR);
332cb5caa98Sdjl 
333cb5caa98Sdjl 	/*
334cb5caa98Sdjl 	 * At init time, the whole group of config params are received.
335cb5caa98Sdjl 	 * At update time, group or individual parameter value could
336cb5caa98Sdjl 	 * be received.
337cb5caa98Sdjl 	 */
338cb5caa98Sdjl 
339cb5caa98Sdjl 	if (_nscd_cfg_flag_is_set(dflag, NSCD_CFG_DFLAG_GROUP)) {
340cb5caa98Sdjl 
341cb5caa98Sdjl 		logcfg = (nscd_cfg_global_log_t *)data;
342cb5caa98Sdjl 
343cb5caa98Sdjl 		if (_nscd_cfg_bitmap_valid(logcfg->debug_comp,
344cb5caa98Sdjl 		    NSCD_LOG_ALL) == 0)
345cb5caa98Sdjl 			return (NSCD_CFG_SYNTAX_ERROR);
346cb5caa98Sdjl 
347cb5caa98Sdjl 		if (_nscd_cfg_bitmap_valid(logcfg->debug_level,
348cb5caa98Sdjl 		    NSCD_LOG_LEVEL_ALL) == 0)
349cb5caa98Sdjl 			return (NSCD_CFG_SYNTAX_ERROR);
350cb5caa98Sdjl 
351cb5caa98Sdjl 		if (logcfg->logfile != NULL)
352cb5caa98Sdjl 			return (_nscd_set_lf(logcfg->logfile));
353cb5caa98Sdjl 
354cb5caa98Sdjl 		return (NSCD_SUCCESS);
355cb5caa98Sdjl 	}
356cb5caa98Sdjl 
357cb5caa98Sdjl 	/*
358cb5caa98Sdjl 	 * individual config parameter
359cb5caa98Sdjl 	 */
360cb5caa98Sdjl 
361cb5caa98Sdjl 	off = offsetof(nscd_cfg_global_log_t, debug_comp);
362cb5caa98Sdjl 	if (pdesc->p_offset == off) {
363cb5caa98Sdjl 
364cb5caa98Sdjl 		bt = *(nscd_cfg_bitmap_t *)data;
365cb5caa98Sdjl 		if (_nscd_cfg_bitmap_valid(bt, NSCD_LOG_ALL) == 0)
366cb5caa98Sdjl 			return (NSCD_CFG_SYNTAX_ERROR);
367cb5caa98Sdjl 
368cb5caa98Sdjl 		return (NSCD_SUCCESS);
369cb5caa98Sdjl 	}
370cb5caa98Sdjl 
371cb5caa98Sdjl 	off = offsetof(nscd_cfg_global_log_t, debug_level);
372cb5caa98Sdjl 	if (pdesc->p_offset == off) {
373cb5caa98Sdjl 
374cb5caa98Sdjl 		bt = *(nscd_cfg_bitmap_t *)data;
375cb5caa98Sdjl 		if (_nscd_cfg_bitmap_valid(bt, NSCD_LOG_LEVEL_ALL) == 0)
376cb5caa98Sdjl 			return (NSCD_CFG_SYNTAX_ERROR);
377cb5caa98Sdjl 
378cb5caa98Sdjl 		return (NSCD_SUCCESS);
379cb5caa98Sdjl 	}
380cb5caa98Sdjl 
381cb5caa98Sdjl 	off = offsetof(nscd_cfg_global_log_t, logfile);
382cb5caa98Sdjl 	if (pdesc->p_offset == off) {
383cb5caa98Sdjl 		if (data != NULL)
384cb5caa98Sdjl 			return (_nscd_set_lf((char *)data));
385cb5caa98Sdjl 		else
386cb5caa98Sdjl 			return (NSCD_SUCCESS);
387cb5caa98Sdjl 	}
388cb5caa98Sdjl 
389cb5caa98Sdjl 	return (NSCD_CFG_PARAM_DESC_ERROR);
390cb5caa98Sdjl }
391cb5caa98Sdjl 
392cb5caa98Sdjl /* ARGSUSED */
393cb5caa98Sdjl nscd_rc_t
394cb5caa98Sdjl _nscd_cfg_log_get_stat(
395cb5caa98Sdjl 	void				**stat,
396cb5caa98Sdjl 	struct nscd_cfg_stat_desc	*sdesc,
397cb5caa98Sdjl 	nscd_cfg_id_t			*nswdb,
398cb5caa98Sdjl 	nscd_cfg_flag_t			*dflag,
399cb5caa98Sdjl 	void				(**free_stat)(void *stat),
400cb5caa98Sdjl 	nscd_cfg_error_t		**errorp)
401cb5caa98Sdjl {
402cb5caa98Sdjl 
403cb5caa98Sdjl 	*(nscd_cfg_stat_global_log_t **)stat = &logstats;
404cb5caa98Sdjl 
405cb5caa98Sdjl 	/* indicate the statistics are static, i.e., do not free */
406cb5caa98Sdjl 	*dflag = _nscd_cfg_flag_set(*dflag, NSCD_CFG_DFLAG_STATIC_DATA);
407cb5caa98Sdjl 
408cb5caa98Sdjl 	return (NSCD_SUCCESS);
409cb5caa98Sdjl }
410cb5caa98Sdjl 
411cb5caa98Sdjl /*
412cb5caa98Sdjl  * set the name of the current log file and make it current.
413cb5caa98Sdjl  */
414cb5caa98Sdjl nscd_rc_t
415cb5caa98Sdjl _nscd_set_log_file(
416cb5caa98Sdjl 	char			*name)
417cb5caa98Sdjl {
418cb5caa98Sdjl 	nscd_rc_t		rc;
419cb5caa98Sdjl 	nscd_cfg_handle_t	*h;
420cb5caa98Sdjl 
421cb5caa98Sdjl 	rc = _nscd_cfg_get_handle("logfile", NULL, &h, NULL);
422cb5caa98Sdjl 	if (rc != NSCD_SUCCESS)
423cb5caa98Sdjl 		return (rc);
424cb5caa98Sdjl 
425cb5caa98Sdjl 	rc = _nscd_cfg_set(h, name, NULL);
426cb5caa98Sdjl 	_nscd_cfg_free_handle(h);
427cb5caa98Sdjl 	if (rc != NSCD_SUCCESS)
428cb5caa98Sdjl 		exit(rc);
429cb5caa98Sdjl 
430cb5caa98Sdjl 	return (NSCD_SUCCESS);
431cb5caa98Sdjl }
432cb5caa98Sdjl 
433*18bdb8a7Smichen /* Set debug level to the new one and make it current */
434cb5caa98Sdjl nscd_rc_t
435cb5caa98Sdjl _nscd_set_debug_level(
436cb5caa98Sdjl 	int			level)
437cb5caa98Sdjl {
438cb5caa98Sdjl 	nscd_rc_t		rc;
439cb5caa98Sdjl 	nscd_cfg_handle_t	*h;
440*18bdb8a7Smichen 	int			l = 0;
441*18bdb8a7Smichen 	int			c = -1;
442cb5caa98Sdjl 
443*18bdb8a7Smichen 	/* old nscd debug level is 1 to 10, map it to log_level and log_comp */
444*18bdb8a7Smichen 	if (level >= 0 && level <= 10) {
445*18bdb8a7Smichen 		l = debug_to_log_level(level);
446*18bdb8a7Smichen 		c = NSCD_LOG_CACHE;
447*18bdb8a7Smichen 	} else
448*18bdb8a7Smichen 		l = level;
449*18bdb8a7Smichen 
450*18bdb8a7Smichen 	if (level < 0)
451*18bdb8a7Smichen 		c = -1 * level / 1000000;
452*18bdb8a7Smichen 
453*18bdb8a7Smichen 	if (c != -1) {
454cb5caa98Sdjl 		rc = _nscd_cfg_get_handle("debug-components", NULL, &h, NULL);
455cb5caa98Sdjl 		if (rc != NSCD_SUCCESS)
456cb5caa98Sdjl 			return (rc);
457cb5caa98Sdjl 
458cb5caa98Sdjl 		rc = _nscd_cfg_set(h, &c, NULL);
459cb5caa98Sdjl 		_nscd_cfg_free_handle(h);
460cb5caa98Sdjl 		if (rc != NSCD_SUCCESS)
461cb5caa98Sdjl 			exit(rc);
462*18bdb8a7Smichen 	}
463cb5caa98Sdjl 
464cb5caa98Sdjl 	rc = _nscd_cfg_get_handle("debug-level", NULL, &h, NULL);
465cb5caa98Sdjl 	if (rc != NSCD_SUCCESS)
466cb5caa98Sdjl 		return (rc);
467cb5caa98Sdjl 
468cb5caa98Sdjl 	if (level < 0)
469*18bdb8a7Smichen 		l = -1 * level % 1000000;
470cb5caa98Sdjl 
471cb5caa98Sdjl 	rc = _nscd_cfg_set(h, &l, NULL);
472cb5caa98Sdjl 	_nscd_cfg_free_handle(h);
473cb5caa98Sdjl 	if (rc != NSCD_SUCCESS)
474cb5caa98Sdjl 		exit(rc);
475cb5caa98Sdjl 
476cb5caa98Sdjl 	return (NSCD_SUCCESS);
477cb5caa98Sdjl }
478*18bdb8a7Smichen 
479*18bdb8a7Smichen void
480*18bdb8a7Smichen _nscd_get_log_info(
481*18bdb8a7Smichen 	char	*level,
482*18bdb8a7Smichen 	int	llen,
483*18bdb8a7Smichen 	char	*file,
484*18bdb8a7Smichen 	int	flen)
485*18bdb8a7Smichen {
486*18bdb8a7Smichen 	if (_nscd_log_level != 0)
487*18bdb8a7Smichen 		(void) snprintf(level, llen, "%d", _nscd_log_level);
488*18bdb8a7Smichen 	if (*_nscd_logfile != '\0')
489*18bdb8a7Smichen 		(void) strlcpy(file, _nscd_logfile, flen);
490*18bdb8a7Smichen }
491