xref: /onnv-gate/usr/src/lib/libc/port/gen/syslog.c (revision 7632:91aa3d8541b5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53235Sraf  * Common Development and Distribution License (the "License").
63235Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213235Sraf 
220Sstevel@tonic-gate /*
236812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * SYSLOG -- print message on log file
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * This routine looks a lot like printf, except that it
450Sstevel@tonic-gate  * outputs to the log file instead of the standard output.
460Sstevel@tonic-gate  * Also:
470Sstevel@tonic-gate  *	adds a timestamp,
480Sstevel@tonic-gate  *	prints the module name in front of the message,
490Sstevel@tonic-gate  *	has some other formatting types (or will sometime),
500Sstevel@tonic-gate  *	adds a newline on the end of the message.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * The output of this routine is intended to be read by /etc/syslogd.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
556812Sraf #pragma weak _syslog = syslog
560Sstevel@tonic-gate 
576812Sraf #include "lint.h"
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <sys/types32.h>
600Sstevel@tonic-gate #include <sys/mman.h>
610Sstevel@tonic-gate #include <sys/stropts.h>
620Sstevel@tonic-gate #include <sys/strlog.h>
630Sstevel@tonic-gate #include <sys/log.h>		/* for LOG_MAXPS */
640Sstevel@tonic-gate #include <stdlib.h>
650Sstevel@tonic-gate #include <procfs.h>
660Sstevel@tonic-gate #include <syslog.h>
670Sstevel@tonic-gate #include <signal.h>
680Sstevel@tonic-gate #include <fcntl.h>
690Sstevel@tonic-gate #include <string.h>
700Sstevel@tonic-gate #include <stdarg.h>
710Sstevel@tonic-gate #include <unistd.h>
720Sstevel@tonic-gate #include <wait.h>
730Sstevel@tonic-gate #include <stdio.h>
740Sstevel@tonic-gate #include <string.h>
750Sstevel@tonic-gate #include <errno.h>
760Sstevel@tonic-gate #include <thread.h>
770Sstevel@tonic-gate #include <synch.h>
780Sstevel@tonic-gate #include <sys/door.h>
790Sstevel@tonic-gate #include <sys/stat.h>
800Sstevel@tonic-gate #include <stropts.h>
813235Sraf #include <sys/fork.h>
820Sstevel@tonic-gate #include <sys/wait.h>
830Sstevel@tonic-gate #include "libc.h"
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	MAXLINE		1024		/* max message size (but see below) */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	PRIMASK(p)	(1 << ((p) & LOG_PRIMASK))
880Sstevel@tonic-gate #define	PRIFAC(p)	(((p) & LOG_FACMASK) >> 3)
890Sstevel@tonic-gate #define	IMPORTANT 	LOG_ERR
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #ifndef FALSE
920Sstevel@tonic-gate #define	FALSE 	0
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #ifndef TRUE
960Sstevel@tonic-gate #define	TRUE	1
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	logname		"/dev/conslog"
1000Sstevel@tonic-gate #define	ctty		"/dev/syscon"
1010Sstevel@tonic-gate #define	sysmsg		"/dev/sysmsg"
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	DOORFILE	"/var/run/syslog_door"
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static struct __syslog {
1060Sstevel@tonic-gate 	int	_LogFile;
1070Sstevel@tonic-gate 	int	_LogStat;
1080Sstevel@tonic-gate 	const char *_LogTag;
1090Sstevel@tonic-gate 	int	_LogMask;
1100Sstevel@tonic-gate 	char	*_SyslogHost;
1110Sstevel@tonic-gate 	int	_LogFacility;
1120Sstevel@tonic-gate 	int	_LogFileInvalid;
1130Sstevel@tonic-gate 	int	_OpenLogCalled;
1140Sstevel@tonic-gate 	dev_t   _LogDev;
1150Sstevel@tonic-gate 	char	_ProcName[PRFNSZ + 1];
1160Sstevel@tonic-gate } __syslog = {
1170Sstevel@tonic-gate 	-1,		/* fd for log */
1180Sstevel@tonic-gate 	0,		/* status bits, set by openlog() */
1190Sstevel@tonic-gate 	"syslog",	/* string to tag the entry with */
1200Sstevel@tonic-gate 	0xff,		/* mask of priorities to be logged */
1210Sstevel@tonic-gate 	NULL,
1220Sstevel@tonic-gate 	LOG_USER,	/* default facility code */
1230Sstevel@tonic-gate 	FALSE,		/* check for validity of fd for log */
1240Sstevel@tonic-gate 	0,		/* openlog has not yet been called */
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	LogFile (__syslog._LogFile)
1280Sstevel@tonic-gate #define	LogStat (__syslog._LogStat)
1290Sstevel@tonic-gate #define	LogTag (__syslog._LogTag)
1300Sstevel@tonic-gate #define	LogMask (__syslog._LogMask)
1310Sstevel@tonic-gate #define	SyslogHost (__syslog._SyslogHost)
1320Sstevel@tonic-gate #define	LogFacility (__syslog._LogFacility)
1330Sstevel@tonic-gate #define	LogFileInvalid (__syslog._LogFileInvalid)
1340Sstevel@tonic-gate #define	OpenLogCalled (__syslog._OpenLogCalled)
1350Sstevel@tonic-gate #define	LogDev (__syslog._LogDev)
1360Sstevel@tonic-gate #define	ProcName (__syslog._ProcName)
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static int syslogd_ok(void);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Regrettably, there are several instances inside libc where
1420Sstevel@tonic-gate  * syslog() is called from the bottom of a deep call stack
1430Sstevel@tonic-gate  * and a critical lock was acquired near the top of the stack.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * Because syslog() uses stdio (and it is called from within stdio)
1460Sstevel@tonic-gate  * it runs the danger of deadlocking, perhaps with an interposed
1470Sstevel@tonic-gate  * malloc() when fork() is occurring concurrently, perhaps with
1480Sstevel@tonic-gate  * some other lock within libc.
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * The only fix for this problem is to restructure libc not to do
1510Sstevel@tonic-gate  * this thing and always to call syslog() with no locks held.
1520Sstevel@tonic-gate  * This restructuring will require a substantial effort.
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * Meanwhile, we just hope that on the rare occasion that syslog()
1550Sstevel@tonic-gate  * is called from within libc (such occurrences should "never happen")
1560Sstevel@tonic-gate  * that we don't get caught in a race condition deadlock.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate void
syslog(int pri,const char * fmt,...)1590Sstevel@tonic-gate syslog(int pri, const char *fmt, ...)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	va_list ap;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	va_start(ap, fmt);
1640Sstevel@tonic-gate 	vsyslog(pri, fmt, ap);
1650Sstevel@tonic-gate 	va_end(ap);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate void
vsyslog(int pri,const char * fmt,va_list ap)1700Sstevel@tonic-gate vsyslog(int pri, const char *fmt, va_list ap)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	char *b, *f, *o;
1730Sstevel@tonic-gate 	char c;
1740Sstevel@tonic-gate 	int clen;
1750Sstevel@tonic-gate 	char buf[MAXLINE + 2];
1760Sstevel@tonic-gate 	char outline[MAXLINE + 256];  /* pad to allow date, system name... */
1770Sstevel@tonic-gate 	time_t now;
1780Sstevel@tonic-gate 	pid_t pid;
1790Sstevel@tonic-gate 	struct log_ctl hdr;
1800Sstevel@tonic-gate 	struct strbuf dat;
1810Sstevel@tonic-gate 	struct strbuf ctl;
1820Sstevel@tonic-gate 	char timestr[26];	/* hardwired value 26 due to Posix */
1830Sstevel@tonic-gate 	size_t taglen;
1840Sstevel@tonic-gate 	int olderrno = errno;
1850Sstevel@tonic-gate 	struct stat statbuff;
1860Sstevel@tonic-gate 	int procfd;
1870Sstevel@tonic-gate 	char procfile[32];
1880Sstevel@tonic-gate 	psinfo_t p;
1890Sstevel@tonic-gate 	int showpid;
1900Sstevel@tonic-gate 	uint32_t msgid;
1910Sstevel@tonic-gate 	char *msgid_start, *msgid_end;
1923235Sraf 	int nowait;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Maximum tag length is 256 (the pad in outline) minus the size of the
1960Sstevel@tonic-gate  * other things that can go in the pad.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate #define	MAX_TAG		230
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/* see if we should just throw out this message */
2010Sstevel@tonic-gate 	if (pri < 0 || PRIFAC(pri) >= LOG_NFACILITIES ||
2020Sstevel@tonic-gate 	    (PRIMASK(pri) & LogMask) == 0)
2030Sstevel@tonic-gate 		return;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (LogFileInvalid)
2060Sstevel@tonic-gate 		return;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * if openlog() has not been called by the application,
2100Sstevel@tonic-gate 	 * try to get the name of the application and set it
2110Sstevel@tonic-gate 	 * as the ident string for messages. If unable to get
2120Sstevel@tonic-gate 	 * it for any reason, fall back to using the default
2130Sstevel@tonic-gate 	 * of syslog. If we succeed in getting the name, also
2140Sstevel@tonic-gate 	 * turn on LOG_PID, to provide greater detail.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	showpid = 0;
2170Sstevel@tonic-gate 	if (OpenLogCalled == 0) {
218*7632SNick.Todd@Sun.COM 		(void) sprintf(procfile, "/proc/%d/psinfo", (int)getpid());
2190Sstevel@tonic-gate 		if ((procfd = open(procfile, O_RDONLY)) >= 0) {
2200Sstevel@tonic-gate 			if (read(procfd, &p, sizeof (psinfo_t)) >= 0) {
2210Sstevel@tonic-gate 				(void) strncpy(ProcName, p.pr_fname, PRFNSZ);
2220Sstevel@tonic-gate 				LogTag = (const char *) &ProcName;
2230Sstevel@tonic-gate 				showpid = LOG_PID;
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 			(void) close(procfd);
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	if (LogFile < 0)
2290Sstevel@tonic-gate 		openlog(LogTag, LogStat|LOG_NDELAY|showpid, 0);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if ((fstat(LogFile, &statbuff) != 0) ||
2320Sstevel@tonic-gate 	    (!S_ISCHR(statbuff.st_mode)) || (statbuff.st_rdev != LogDev)) {
2330Sstevel@tonic-gate 		LogFileInvalid = TRUE;
2340Sstevel@tonic-gate 		return;
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* set default facility if none specified */
2380Sstevel@tonic-gate 	if ((pri & LOG_FACMASK) == 0)
2390Sstevel@tonic-gate 		pri |= LogFacility;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* build the header */
2420Sstevel@tonic-gate 	hdr.pri = pri;
2430Sstevel@tonic-gate 	hdr.flags = SL_CONSOLE;
2440Sstevel@tonic-gate 	hdr.level = 0;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* build the message */
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * To avoid potential security problems, bounds checking is done
2490Sstevel@tonic-gate 	 * on outline and buf.
2500Sstevel@tonic-gate 	 * The following code presumes that the header information will
2510Sstevel@tonic-gate 	 * fit in 250-odd bytes, as was accounted for in the buffer size
2520Sstevel@tonic-gate 	 * allocation.  This is dependent on the assumption that the LogTag
2530Sstevel@tonic-gate 	 * and the string returned by sprintf() for getpid() will return
2540Sstevel@tonic-gate 	 * be less than 230-odd characters combined.
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	o = outline;
2570Sstevel@tonic-gate 	(void) time(&now);
2580Sstevel@tonic-gate 	(void) sprintf(o, "%.15s ", ctime_r(&now, timestr, 26) + 4);
2590Sstevel@tonic-gate 	o += strlen(o);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (LogTag) {
2620Sstevel@tonic-gate 		taglen = strlen(LogTag) < MAX_TAG ? strlen(LogTag) : MAX_TAG;
2630Sstevel@tonic-gate 		(void) strncpy(o, LogTag, taglen);
2640Sstevel@tonic-gate 		o[taglen] = '\0';
2650Sstevel@tonic-gate 		o += strlen(o);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	if (LogStat & LOG_PID) {
268*7632SNick.Todd@Sun.COM 		(void) sprintf(o, "[%d]", (int)getpid());
2690Sstevel@tonic-gate 		o += strlen(o);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	if (LogTag) {
2720Sstevel@tonic-gate 		(void) strcpy(o, ": ");
2730Sstevel@tonic-gate 		o += 2;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	STRLOG_MAKE_MSGID(fmt, msgid);
2770Sstevel@tonic-gate 	(void) sprintf(o, "[ID %u FACILITY_AND_PRIORITY] ", msgid);
2780Sstevel@tonic-gate 	o += strlen(o);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	b = buf;
2810Sstevel@tonic-gate 	f = (char *)fmt;
2820Sstevel@tonic-gate 	while ((c = *f++) != '\0' && b < &buf[MAXLINE]) {
2830Sstevel@tonic-gate 		char *errmsg;
2840Sstevel@tonic-gate 		if (c != '%') {
2850Sstevel@tonic-gate 			*b++ = c;
2860Sstevel@tonic-gate 			continue;
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 		if ((c = *f++) != 'm') {
2890Sstevel@tonic-gate 			*b++ = '%';
2900Sstevel@tonic-gate 			*b++ = c;
2910Sstevel@tonic-gate 			continue;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		if ((errmsg = strerror(olderrno)) == NULL)
2940Sstevel@tonic-gate 			(void) snprintf(b, &buf[MAXLINE] - b, "error %d",
2950Sstevel@tonic-gate 			    olderrno);
2960Sstevel@tonic-gate 		else {
2970Sstevel@tonic-gate 			while (*errmsg != '\0' && b < &buf[MAXLINE]) {
2980Sstevel@tonic-gate 				if (*errmsg == '%') {
2990Sstevel@tonic-gate 					(void) strcpy(b, "%%");
3000Sstevel@tonic-gate 					b += 2;
3010Sstevel@tonic-gate 				}
3020Sstevel@tonic-gate 				else
3030Sstevel@tonic-gate 					*b++ = *errmsg;
3040Sstevel@tonic-gate 				errmsg++;
3050Sstevel@tonic-gate 			}
3060Sstevel@tonic-gate 			*b = '\0';
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		b += strlen(b);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	if (b > buf && *(b-1) != '\n')	/* ensure at least one newline */
3110Sstevel@tonic-gate 		*b++ = '\n';
3120Sstevel@tonic-gate 	*b = '\0';
3130Sstevel@tonic-gate 	/* LINTED variable format specifier */
3140Sstevel@tonic-gate 	(void) vsnprintf(o, &outline[sizeof (outline)] - o, buf, ap);
3150Sstevel@tonic-gate 	clen  = (int)strlen(outline) + 1;	/* add one for NULL byte */
3160Sstevel@tonic-gate 	if (clen > MAXLINE) {
3170Sstevel@tonic-gate 		clen = MAXLINE;
3180Sstevel@tonic-gate 		outline[MAXLINE-1] = '\0';
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * 1136432 points out that the underlying log driver actually
3230Sstevel@tonic-gate 	 * refuses to accept (ERANGE) messages longer than LOG_MAXPS
3240Sstevel@tonic-gate 	 * bytes.  So it really doesn't make much sense to putmsg a
3250Sstevel@tonic-gate 	 * longer message..
3260Sstevel@tonic-gate 	 */
3270Sstevel@tonic-gate 	if (clen > LOG_MAXPS) {
3280Sstevel@tonic-gate 		clen = LOG_MAXPS;
3290Sstevel@tonic-gate 		outline[LOG_MAXPS-1] = '\0';
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/* set up the strbufs */
3330Sstevel@tonic-gate 	ctl.maxlen = sizeof (struct log_ctl);
3340Sstevel@tonic-gate 	ctl.len = sizeof (struct log_ctl);
3350Sstevel@tonic-gate 	ctl.buf = (caddr_t)&hdr;
3360Sstevel@tonic-gate 	dat.maxlen = sizeof (outline);
3370Sstevel@tonic-gate 	dat.len = clen;
3380Sstevel@tonic-gate 	dat.buf = outline;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/* output the message to the local logger */
3410Sstevel@tonic-gate 	if ((putmsg(LogFile, &ctl, &dat, 0) >= 0) && syslogd_ok())
3420Sstevel@tonic-gate 		return;
3430Sstevel@tonic-gate 	if (!(LogStat & LOG_CONS))
3440Sstevel@tonic-gate 		return;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/*
3470Sstevel@tonic-gate 	 * Output the message to the console directly.  To reduce visual
3480Sstevel@tonic-gate 	 * clutter, we strip out the message ID.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if ((msgid_start = strstr(outline, "[ID ")) != NULL &&
3510Sstevel@tonic-gate 	    (msgid_end = strstr(msgid_start, "] ")) != NULL)
3520Sstevel@tonic-gate 		(void) strcpy(msgid_start, msgid_end + 2);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	clen = strlen(outline) + 1;
3550Sstevel@tonic-gate 
3563235Sraf 	nowait = (LogStat & LOG_NOWAIT);
3573235Sraf 	pid = forkx(nowait? 0 : (FORK_NOSIGCHLD | FORK_WAITPID));
3583235Sraf 	if (pid == -1)
3590Sstevel@tonic-gate 		return;
3603235Sraf 
3610Sstevel@tonic-gate 	if (pid == 0) {
3623235Sraf 		sigset_t sigs;
3630Sstevel@tonic-gate 		int fd;
3640Sstevel@tonic-gate 
3653235Sraf 		(void) sigset(SIGALRM, SIG_DFL);
3663235Sraf 		(void) sigemptyset(&sigs);
3673235Sraf 		(void) sigaddset(&sigs, SIGALRM);
3683235Sraf 		(void) sigprocmask(SIG_UNBLOCK, &sigs, NULL);
3690Sstevel@tonic-gate 		(void) alarm(5);
3700Sstevel@tonic-gate 		if (((fd = open(sysmsg, O_WRONLY)) >= 0) ||
3710Sstevel@tonic-gate 		    (fd = open(ctty, O_WRONLY)) >= 0) {
3720Sstevel@tonic-gate 			(void) alarm(0);
3730Sstevel@tonic-gate 			outline[clen - 1] = '\r';
3740Sstevel@tonic-gate 			(void) write(fd, outline, clen);
3750Sstevel@tonic-gate 			(void) close(fd);
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		_exit(0);
3780Sstevel@tonic-gate 	}
3793235Sraf 	if (!nowait)
3803235Sraf 		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
3813235Sraf 			continue;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate  * Use a door call to syslogd to see if it's alive.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate static int
syslogd_ok(void)3880Sstevel@tonic-gate syslogd_ok(void)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	int d;
3910Sstevel@tonic-gate 	int s;
3920Sstevel@tonic-gate 	door_arg_t darg;
3930Sstevel@tonic-gate 	door_info_t info;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if ((d = open(DOORFILE, O_RDONLY)) < 0)
3960Sstevel@tonic-gate 		return (0);
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * see if our pid matches the pid of the door server.
3990Sstevel@tonic-gate 	 * If so, syslogd has called syslog(), probably as
4000Sstevel@tonic-gate 	 * a result of some name service library error, and
4010Sstevel@tonic-gate 	 * we don't want to let syslog continue and possibly
4020Sstevel@tonic-gate 	 * fork here.
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	info.di_target = 0;
4050Sstevel@tonic-gate 	if (__door_info(d, &info) < 0 || info.di_target == getpid()) {
4060Sstevel@tonic-gate 		(void) close(d);
4070Sstevel@tonic-gate 		return (0);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	darg.data_ptr = NULL;
4100Sstevel@tonic-gate 	darg.data_size = 0;
4110Sstevel@tonic-gate 	darg.desc_ptr = NULL;
4120Sstevel@tonic-gate 	darg.desc_num = 0;
4130Sstevel@tonic-gate 	darg.rbuf = NULL;
4140Sstevel@tonic-gate 	darg.rsize = 0;
4150Sstevel@tonic-gate 	s = __door_call(d, &darg);
4160Sstevel@tonic-gate 	(void) close(d);
4170Sstevel@tonic-gate 	if (s < 0)
4180Sstevel@tonic-gate 		return (0);		/* failure - syslogd dead */
4190Sstevel@tonic-gate 	else
4200Sstevel@tonic-gate 		return (1);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate  * OPENLOG -- open system log
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate void
openlog(const char * ident,int logstat,int logfac)4280Sstevel@tonic-gate openlog(const char *ident, int logstat, int logfac)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	struct	stat	statbuff;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	OpenLogCalled = 1;
4330Sstevel@tonic-gate 	if (ident != NULL)
4340Sstevel@tonic-gate 		LogTag = ident;
4350Sstevel@tonic-gate 	LogStat = logstat;
4360Sstevel@tonic-gate 	if (logfac != 0)
4370Sstevel@tonic-gate 		LogFacility = logfac & LOG_FACMASK;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * if the fstat(2) fails or the st_rdev has changed
4410Sstevel@tonic-gate 	 * then we must open the file
4420Sstevel@tonic-gate 	 */
4430Sstevel@tonic-gate 	if ((fstat(LogFile, &statbuff) == 0) &&
4440Sstevel@tonic-gate 	    (S_ISCHR(statbuff.st_mode)) && (statbuff.st_rdev == LogDev))
4450Sstevel@tonic-gate 		return;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	if (LogStat & LOG_NDELAY) {
4480Sstevel@tonic-gate 		LogFile = open(logname, O_WRONLY);
4490Sstevel@tonic-gate 		(void) fcntl(LogFile, F_SETFD, 1);
4500Sstevel@tonic-gate 		(void) fstat(LogFile, &statbuff);
4510Sstevel@tonic-gate 		LogDev = statbuff.st_rdev;
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * CLOSELOG -- close the system log
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate void
closelog(void)4600Sstevel@tonic-gate closelog(void)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	struct	stat	statbuff;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	OpenLogCalled = 0;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* if the LogFile is invalid it can not be closed */
4670Sstevel@tonic-gate 	if (LogFileInvalid)
4680Sstevel@tonic-gate 		return;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * if the fstat(2) fails or the st_rdev has changed
4720Sstevel@tonic-gate 	 * then we can not close the file
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	if ((fstat(LogFile, &statbuff) == 0) && (statbuff.st_rdev == LogDev)) {
4750Sstevel@tonic-gate 		(void) close(LogFile);
4760Sstevel@tonic-gate 		LogFile = -1;
4770Sstevel@tonic-gate 		LogStat = 0;
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * SETLOGMASK -- set the log mask level
4830Sstevel@tonic-gate  */
4840Sstevel@tonic-gate int
setlogmask(int pmask)4850Sstevel@tonic-gate setlogmask(int pmask)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	int omask = 0;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	omask = LogMask;
4900Sstevel@tonic-gate 	if (pmask != 0)
4910Sstevel@tonic-gate 		LogMask = pmask;
4920Sstevel@tonic-gate 	return (omask);
4930Sstevel@tonic-gate }
494