1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * PPPoE Server-mode daemon log file support.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  * All rights reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
34*7c478bd9Sstevel@tonic-gate #include <alloca.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <syslog.h>
39*7c478bd9Sstevel@tonic-gate #include <assert.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "common.h"
43*7c478bd9Sstevel@tonic-gate #include "logging.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* Not all functions are used by all applications.  Let lint know this. */
46*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate const char *prog_name = "none";	/* Subsystem name for syslog */
49*7c478bd9Sstevel@tonic-gate int log_level;			/* Higher number for more detail. */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static int curlogfd = -1;	/* Current log file */
52*7c478bd9Sstevel@tonic-gate static const char *curfname;	/* Name of current log file */
53*7c478bd9Sstevel@tonic-gate static const char *stderr_name = "stderr";
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	SMALLSTR	254	/* Don't allocate for most strings. */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate  * Returns -1 on error (with errno set), 0 on blocked write (file
59*7c478bd9Sstevel@tonic-gate  * system full), or N (buffer length) on success.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate static int
dowrite(int fd,const void * buf,int len)62*7c478bd9Sstevel@tonic-gate dowrite(int fd, const void *buf, int len)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	int retv;
65*7c478bd9Sstevel@tonic-gate 	const uint8_t *bp = (uint8_t *)buf;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	while (len > 0) {
68*7c478bd9Sstevel@tonic-gate 		retv = write(fd, bp, len);
69*7c478bd9Sstevel@tonic-gate 		if (retv == 0) {
70*7c478bd9Sstevel@tonic-gate 			break;
71*7c478bd9Sstevel@tonic-gate 		}
72*7c478bd9Sstevel@tonic-gate 		if (retv == -1) {
73*7c478bd9Sstevel@tonic-gate 			if (errno != EINTR)
74*7c478bd9Sstevel@tonic-gate 				break;
75*7c478bd9Sstevel@tonic-gate 		} else {
76*7c478bd9Sstevel@tonic-gate 			bp += retv;
77*7c478bd9Sstevel@tonic-gate 			len -= retv;
78*7c478bd9Sstevel@tonic-gate 		}
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	if (len <= 0)
81*7c478bd9Sstevel@tonic-gate 		return (bp - (uint8_t *)buf);
82*7c478bd9Sstevel@tonic-gate 	return (retv);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /* A close that avoids closing stderr */
86*7c478bd9Sstevel@tonic-gate static int
doclose(void)87*7c478bd9Sstevel@tonic-gate doclose(void)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	int	retval = 0;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if (curlogfd == -1)
92*7c478bd9Sstevel@tonic-gate 		return (0);
93*7c478bd9Sstevel@tonic-gate 	if ((curlogfd != STDERR_FILENO) || (curfname != stderr_name))
94*7c478bd9Sstevel@tonic-gate 		retval = close(curlogfd);
95*7c478bd9Sstevel@tonic-gate 	curlogfd = -1;
96*7c478bd9Sstevel@tonic-gate 	return (retval);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Log levels are 0 for no messages, 1 for errors, 2 for warnings, 3
101*7c478bd9Sstevel@tonic-gate  * for informational messages, and 4 for debugging messages.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate static void
vlogat(int loglev,const char * fmt,va_list args)104*7c478bd9Sstevel@tonic-gate vlogat(int loglev, const char *fmt, va_list args)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	char timbuf[64];
107*7c478bd9Sstevel@tonic-gate 	char regbuf[SMALLSTR+2];
108*7c478bd9Sstevel@tonic-gate 	char *ostr;
109*7c478bd9Sstevel@tonic-gate 	int timlen;
110*7c478bd9Sstevel@tonic-gate 	int slen;
111*7c478bd9Sstevel@tonic-gate 	char *nstr;
112*7c478bd9Sstevel@tonic-gate 	int err1, err2;
113*7c478bd9Sstevel@tonic-gate 	int sloglev;
114*7c478bd9Sstevel@tonic-gate 	int retv;
115*7c478bd9Sstevel@tonic-gate 	va_list args2;
116*7c478bd9Sstevel@tonic-gate 	static int xlate_loglev[] = {
117*7c478bd9Sstevel@tonic-gate 		LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG
118*7c478bd9Sstevel@tonic-gate 	};
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (loglev >= log_level)
121*7c478bd9Sstevel@tonic-gate 		return;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	timbuf[0] = '\0';
124*7c478bd9Sstevel@tonic-gate 	timlen = 0;
125*7c478bd9Sstevel@tonic-gate 	if (curlogfd >= 0) {
126*7c478bd9Sstevel@tonic-gate 		time_t now = time(NULL);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 		/*
129*7c478bd9Sstevel@tonic-gate 		 * Form a time/date string for file (non-syslog) logging.
130*7c478bd9Sstevel@tonic-gate 		 * Caution: string broken in two so that SCCS doesn't mangle
131*7c478bd9Sstevel@tonic-gate 		 * the %-T-% sequence.
132*7c478bd9Sstevel@tonic-gate 		 */
133*7c478bd9Sstevel@tonic-gate 		timlen = strftime(timbuf, sizeof (timbuf), "%Y/%m/%d %T"
134*7c478bd9Sstevel@tonic-gate 		    "%Z: ", localtime(&now));
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/* Try formatting once into the small buffer. */
138*7c478bd9Sstevel@tonic-gate 	va_copy(args2, args);
139*7c478bd9Sstevel@tonic-gate 	slen = vsnprintf(regbuf, SMALLSTR, fmt, args);
140*7c478bd9Sstevel@tonic-gate 	if (slen < SMALLSTR) {
141*7c478bd9Sstevel@tonic-gate 		ostr = regbuf;
142*7c478bd9Sstevel@tonic-gate 	} else {
143*7c478bd9Sstevel@tonic-gate 		/*
144*7c478bd9Sstevel@tonic-gate 		 * Length returned by vsnprintf doesn't include null,
145*7c478bd9Sstevel@tonic-gate 		 * and may also be missing a terminating \n.
146*7c478bd9Sstevel@tonic-gate 		 */
147*7c478bd9Sstevel@tonic-gate 		ostr = alloca(slen + 2);
148*7c478bd9Sstevel@tonic-gate 		slen = vsnprintf(ostr, slen + 1, fmt, args2);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/* Don't bother logging empty lines. */
152*7c478bd9Sstevel@tonic-gate 	if (slen <= 0)
153*7c478bd9Sstevel@tonic-gate 		return;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	/* Tack on a \n if needed. */
156*7c478bd9Sstevel@tonic-gate 	if (ostr[slen - 1] != '\n') {
157*7c478bd9Sstevel@tonic-gate 		ostr[slen++] = '\n';
158*7c478bd9Sstevel@tonic-gate 		ostr[slen] = '\0';
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/* Translate our log levels into syslog standard values */
162*7c478bd9Sstevel@tonic-gate 	assert(loglev >= 0 && loglev < Dim(xlate_loglev));
163*7c478bd9Sstevel@tonic-gate 	sloglev = xlate_loglev[loglev];
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* Log each line separately */
166*7c478bd9Sstevel@tonic-gate 	for (; *ostr != '\0'; ostr = nstr + 1) {
167*7c478bd9Sstevel@tonic-gate 		nstr = strchr(ostr, '\n');
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		/* Ignore zero-length lines. */
170*7c478bd9Sstevel@tonic-gate 		if (nstr == ostr)
171*7c478bd9Sstevel@tonic-gate 			continue;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		slen = nstr - ostr + 1;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 		/*
176*7c478bd9Sstevel@tonic-gate 		 * If we're supposed to be logging to a file, then try
177*7c478bd9Sstevel@tonic-gate 		 * that first.  Ditch the file and revert to syslog if
178*7c478bd9Sstevel@tonic-gate 		 * any errors occur.
179*7c478bd9Sstevel@tonic-gate 		 */
180*7c478bd9Sstevel@tonic-gate 		if (curlogfd >= 0) {
181*7c478bd9Sstevel@tonic-gate 			if ((retv = dowrite(curlogfd, timbuf, timlen)) > 0)
182*7c478bd9Sstevel@tonic-gate 				retv = dowrite(curlogfd, ostr, slen);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 			/*
185*7c478bd9Sstevel@tonic-gate 			 * If we've successfully logged this line,
186*7c478bd9Sstevel@tonic-gate 			 * then go do the next one.
187*7c478bd9Sstevel@tonic-gate 			 */
188*7c478bd9Sstevel@tonic-gate 			if (retv > 0)
189*7c478bd9Sstevel@tonic-gate 				continue;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 			/* Save errno (if any) and close log file */
192*7c478bd9Sstevel@tonic-gate 			err1 = errno;
193*7c478bd9Sstevel@tonic-gate 			if (doclose() == -1)
194*7c478bd9Sstevel@tonic-gate 				err2 = errno;
195*7c478bd9Sstevel@tonic-gate 			else
196*7c478bd9Sstevel@tonic-gate 				err2 = 0;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 			/*
199*7c478bd9Sstevel@tonic-gate 			 * Recursion is safe here because we cleared
200*7c478bd9Sstevel@tonic-gate 			 * out curlogfd above.
201*7c478bd9Sstevel@tonic-gate 			 */
202*7c478bd9Sstevel@tonic-gate 			if (retv == -1)
203*7c478bd9Sstevel@tonic-gate 				logerr("write log %s: %s", curfname,
204*7c478bd9Sstevel@tonic-gate 				    mystrerror(err1));
205*7c478bd9Sstevel@tonic-gate 			else
206*7c478bd9Sstevel@tonic-gate 				logerr("cannot write %s", curfname);
207*7c478bd9Sstevel@tonic-gate 			if (err2 == 0)
208*7c478bd9Sstevel@tonic-gate 				logdbg("closed log %s", curfname);
209*7c478bd9Sstevel@tonic-gate 			else
210*7c478bd9Sstevel@tonic-gate 				logerr("closing log %s: %s", curfname,
211*7c478bd9Sstevel@tonic-gate 				    mystrerror(err2));
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 		syslog(sloglev, "%.*s", slen, ostr);
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate /* Log at debug level */
218*7c478bd9Sstevel@tonic-gate void
logdbg(const char * fmt,...)219*7c478bd9Sstevel@tonic-gate logdbg(const char *fmt, ...)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	va_list args;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
224*7c478bd9Sstevel@tonic-gate 	vlogat(LOGLVL_DBG, fmt, args);
225*7c478bd9Sstevel@tonic-gate 	va_end(args);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate /* Log informational messages */
229*7c478bd9Sstevel@tonic-gate void
loginfo(const char * fmt,...)230*7c478bd9Sstevel@tonic-gate loginfo(const char *fmt, ...)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	va_list args;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
235*7c478bd9Sstevel@tonic-gate 	vlogat(LOGLVL_INFO, fmt, args);
236*7c478bd9Sstevel@tonic-gate 	va_end(args);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate /* Log warning messages */
240*7c478bd9Sstevel@tonic-gate void
logwarn(const char * fmt,...)241*7c478bd9Sstevel@tonic-gate logwarn(const char *fmt, ...)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	va_list args;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
246*7c478bd9Sstevel@tonic-gate 	vlogat(LOGLVL_WARN, fmt, args);
247*7c478bd9Sstevel@tonic-gate 	va_end(args);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /* Log error messages */
251*7c478bd9Sstevel@tonic-gate void
logerr(const char * fmt,...)252*7c478bd9Sstevel@tonic-gate logerr(const char *fmt, ...)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	va_list args;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
257*7c478bd9Sstevel@tonic-gate 	vlogat(LOGLVL_ERR, fmt, args);
258*7c478bd9Sstevel@tonic-gate 	va_end(args);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate /* Log a strerror message */
262*7c478bd9Sstevel@tonic-gate void
logstrerror(const char * emsg)263*7c478bd9Sstevel@tonic-gate logstrerror(const char *emsg)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	logerr("%s: %s\n", emsg, mystrerror(errno));
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate void
log_to_stderr(int dbglvl)269*7c478bd9Sstevel@tonic-gate log_to_stderr(int dbglvl)
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	log_level = dbglvl;
272*7c478bd9Sstevel@tonic-gate 	if (curlogfd >= 0)
273*7c478bd9Sstevel@tonic-gate 		close_log_files();
274*7c478bd9Sstevel@tonic-gate 	curlogfd = STDERR_FILENO;
275*7c478bd9Sstevel@tonic-gate 	curfname = stderr_name;
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * Set indicated log file and debug level.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate void
log_for_service(const char * fname,int dbglvl)282*7c478bd9Sstevel@tonic-gate log_for_service(const char *fname, int dbglvl)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	int err1, err2;
285*7c478bd9Sstevel@tonic-gate 	boolean_t closed;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	log_level = dbglvl;
288*7c478bd9Sstevel@tonic-gate 	if (fname != NULL &&
289*7c478bd9Sstevel@tonic-gate 	    (*fname == '\0' || strcasecmp(fname, "syslog") == 0))
290*7c478bd9Sstevel@tonic-gate 		fname = NULL;
291*7c478bd9Sstevel@tonic-gate 	if (fname == NULL && curfname == NULL)
292*7c478bd9Sstevel@tonic-gate 		return;
293*7c478bd9Sstevel@tonic-gate 	err1 = err2 = 0;
294*7c478bd9Sstevel@tonic-gate 	closed = B_FALSE;
295*7c478bd9Sstevel@tonic-gate 	if (curlogfd >= 0) {
296*7c478bd9Sstevel@tonic-gate 		if (fname == curfname ||
297*7c478bd9Sstevel@tonic-gate 		    (fname != NULL && strcmp(fname, curfname) == 0)) {
298*7c478bd9Sstevel@tonic-gate 			curfname = fname;
299*7c478bd9Sstevel@tonic-gate 			return;
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 		if (doclose() == -1)
302*7c478bd9Sstevel@tonic-gate 			err1 = errno;
303*7c478bd9Sstevel@tonic-gate 		closed = B_TRUE;
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 	if (fname != NULL) {
306*7c478bd9Sstevel@tonic-gate 		curlogfd = open(fname, O_WRONLY|O_APPEND|O_CREAT, 0600);
307*7c478bd9Sstevel@tonic-gate 		if (curlogfd == -1)
308*7c478bd9Sstevel@tonic-gate 			err2 = errno;
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	if (closed) {
311*7c478bd9Sstevel@tonic-gate 		if (err1 == 0)
312*7c478bd9Sstevel@tonic-gate 			logdbg("closed log %s", curfname);
313*7c478bd9Sstevel@tonic-gate 		else
314*7c478bd9Sstevel@tonic-gate 			logerr("closing log %s: %s", curfname,
315*7c478bd9Sstevel@tonic-gate 			    mystrerror(err1));
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 	if (fname != NULL) {
318*7c478bd9Sstevel@tonic-gate 		if (err2 == 0)
319*7c478bd9Sstevel@tonic-gate 			logdbg("opened log %s", fname);
320*7c478bd9Sstevel@tonic-gate 		else
321*7c478bd9Sstevel@tonic-gate 			logerr("opening log %s: %s", fname, mystrerror(err2));
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 	curfname = fname;
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate /*
327*7c478bd9Sstevel@tonic-gate  * Close any open log file.  This is used for SIGHUP (to support log
328*7c478bd9Sstevel@tonic-gate  * file rotation) and when execing.
329*7c478bd9Sstevel@tonic-gate  */
330*7c478bd9Sstevel@tonic-gate void
close_log_files(void)331*7c478bd9Sstevel@tonic-gate close_log_files(void)
332*7c478bd9Sstevel@tonic-gate {
333*7c478bd9Sstevel@tonic-gate 	int err = 0;
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (curlogfd >= 0) {
336*7c478bd9Sstevel@tonic-gate 		if (doclose() == -1)
337*7c478bd9Sstevel@tonic-gate 			err = errno;
338*7c478bd9Sstevel@tonic-gate 		if (err == 0)
339*7c478bd9Sstevel@tonic-gate 			logdbg("closed log %s", curfname);
340*7c478bd9Sstevel@tonic-gate 		else
341*7c478bd9Sstevel@tonic-gate 			logerr("closing log %s: %s", curfname,
342*7c478bd9Sstevel@tonic-gate 			    mystrerror(err));
343*7c478bd9Sstevel@tonic-gate 	}
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate /*
347*7c478bd9Sstevel@tonic-gate  * Reopen syslog connection; in case it was closed.
348*7c478bd9Sstevel@tonic-gate  */
349*7c478bd9Sstevel@tonic-gate void
reopen_log(void)350*7c478bd9Sstevel@tonic-gate reopen_log(void)
351*7c478bd9Sstevel@tonic-gate {
352*7c478bd9Sstevel@tonic-gate 	openlog(prog_name, LOG_PID | LOG_NDELAY | LOG_NOWAIT, LOG_DAEMON);
353*7c478bd9Sstevel@tonic-gate 	/* I control the log level */
354*7c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_DEBUG));
355*7c478bd9Sstevel@tonic-gate }
356