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