xref: /onnv-gate/usr/src/cmd/svc/startd/log.c (revision 2258)
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
51958Slianep  * Common Development and Distribution License (the "License").
61958Slianep  * 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  */
210Sstevel@tonic-gate /*
221958Slianep  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * log.c - debugging and logging functions
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * Logging destinations
320Sstevel@tonic-gate  *   svc.startd(1M) supports three logging destinations:  the system log, a
330Sstevel@tonic-gate  *   daemon-specific log (in the /var/svc/log hierarchy by default), and to the
341958Slianep  *   standard output (redirected to the /var/svc/log/svc.startd.log file by
351958Slianep  *   default).  Any or all of these destinations may be used to
360Sstevel@tonic-gate  *   communicate a specific message; the audiences for each destination differ.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *   Generic messages associated with svc.startd(1M) are made by the
390Sstevel@tonic-gate  *   log_framework() and log_error() functions.  For these messages, svc.startd
400Sstevel@tonic-gate  *   logs under its own name and under the LOG_DAEMON facility when issuing
410Sstevel@tonic-gate  *   events to the system log.  By design, severities below LOG_NOTICE are never
420Sstevel@tonic-gate  *   issued to the system log.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *   Messages associated with a specific service instance are logged using the
450Sstevel@tonic-gate  *   log_instance() or log_instance_fmri() functions.  These messages are always
460Sstevel@tonic-gate  *   sent to the appropriate per-instance log file.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *   In the case of verbose or debug boot, the log_transition() function
490Sstevel@tonic-gate  *   displays messages regarding instance transitions to the system console,
500Sstevel@tonic-gate  *   until the expected login services are available.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  *   Finally, log_console() displays messages to the system consoles and
530Sstevel@tonic-gate  *   the master restarter log file.  This is used when booting to a milestone
540Sstevel@tonic-gate  *   other than 'all'.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * Logging detail
570Sstevel@tonic-gate  *   The constants for severity from <syslog.h> are reused, with a specific
580Sstevel@tonic-gate  *   convention here.  (It is worth noting that the #define values for the LOG_
590Sstevel@tonic-gate  *   levels are such that more important severities have lower values.)  The
600Sstevel@tonic-gate  *   severity determines the importance of the event, and its addressibility by
610Sstevel@tonic-gate  *   the administrator.  Each severity level's use is defined below, along with
620Sstevel@tonic-gate  *   an illustrative example.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  *   LOG_EMERG		Not used presently.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  *   LOG_ALERT		An unrecoverable operation requiring external
670Sstevel@tonic-gate  *			intervention has occurred.   Includes an inability to
680Sstevel@tonic-gate  *			write to the smf(5) repository (due to svc.configd(1M)
690Sstevel@tonic-gate  *			absence, due to permissions failures, etc.).  Message
700Sstevel@tonic-gate  *			should identify component at fault.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  *   LOG_CRIT		An unrecoverable operation internal to svc.startd(1M)
730Sstevel@tonic-gate  *			has occurred.  Failure should be recoverable by restart
740Sstevel@tonic-gate  *			of svc.startd(1M).
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *   LOG_ERR		An smf(5) event requiring administrative intervention
770Sstevel@tonic-gate  *			has occurred.  Includes instance being moved to the
780Sstevel@tonic-gate  *			maintenance state.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *   LOG_WARNING	A potentially destabilizing smf(5) event not requiring
810Sstevel@tonic-gate  *			administrative intervention has occurred.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  *   LOG_NOTICE		A noteworthy smf(5) event has occurred.  Includes
840Sstevel@tonic-gate  *			individual instance failures.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *   LOG_INFO		A noteworthy operation internal to svc.startd(1M) has
870Sstevel@tonic-gate  *			occurred.  Includes recoverable failures or otherwise
880Sstevel@tonic-gate  *			unexpected outcomes.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *   LOG_DEBUG		An internal operation only of interest to a
910Sstevel@tonic-gate  *			svc.startd(1M) developer has occurred.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *  Logging configuration
94*2258Srm88369  *    The preferred approach is to set the logging property values
950Sstevel@tonic-gate  *    in the options property group of the svc.startd default instance.  The
960Sstevel@tonic-gate  *    valid values are "quiet", "verbose", and "debug".  "quiet" is the default;
970Sstevel@tonic-gate  *    "verbose" and "debug" allow LOG_INFO and LOG_DEBUG logging requests to
98*2258Srm88369  *    reach the svc.startd.log file, respectively.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #include <sys/stat.h>
1020Sstevel@tonic-gate #include <sys/statvfs.h>
1030Sstevel@tonic-gate #include <sys/time.h>
1040Sstevel@tonic-gate #include <sys/types.h>
1050Sstevel@tonic-gate #include <assert.h>
1060Sstevel@tonic-gate #include <errno.h>
1070Sstevel@tonic-gate #include <fcntl.h>
1080Sstevel@tonic-gate #include <kstat.h>
1090Sstevel@tonic-gate #include <libgen.h>
1100Sstevel@tonic-gate #include <libintl.h>
1110Sstevel@tonic-gate #include <libuutil.h>
1120Sstevel@tonic-gate #include <locale.h>
1130Sstevel@tonic-gate #include <malloc.h>
1140Sstevel@tonic-gate #include <pthread.h>
1150Sstevel@tonic-gate #include <stdarg.h>
1160Sstevel@tonic-gate #include <stdio.h>
1170Sstevel@tonic-gate #include <strings.h>
1180Sstevel@tonic-gate #include <syslog.h>
1190Sstevel@tonic-gate #include <unistd.h>
1200Sstevel@tonic-gate #include <zone.h>
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #include "startd.h"
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	LOGBUF_SZ	(60 * 80)			/* 60 lines */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static FILE *logfile = NULL;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #ifndef NDEBUG
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * This is a circular buffer for all (even those not emitted externally)
1320Sstevel@tonic-gate  * logging messages.  To read it properly you should start after the first
1330Sstevel@tonic-gate  * null, go until the second, and then go back to the beginning until the
1340Sstevel@tonic-gate  * first null.  Or use ::startd_log in mdb.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate /* LINTED unused */
1370Sstevel@tonic-gate static const size_t logbuf_sz = LOGBUF_SZ;		/* For mdb */
1380Sstevel@tonic-gate static char logbuf[LOGBUF_SZ] = "";
1390Sstevel@tonic-gate static pthread_mutex_t logbuf_mutex = PTHREAD_MUTEX_INITIALIZER;
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void
1430Sstevel@tonic-gate xstrftime_poststart(char *buf, size_t bufsize, struct timeval *time)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	long sec, usec;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	sec = time->tv_sec - st->st_start_time.tv_sec;
1480Sstevel@tonic-gate 	usec = time->tv_usec - st->st_start_time.tv_usec;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (usec < 0) {
1510Sstevel@tonic-gate 		sec -= 1;
1520Sstevel@tonic-gate 		usec += 1000000;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "start + %d.%02ds", sec, usec / 10000);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static void
1590Sstevel@tonic-gate vlog_prefix(int severity, const char *prefix, const char *format, va_list args)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	char buf[512], *cp;
1620Sstevel@tonic-gate 	char timebuf[LOG_DATE_SIZE];
1630Sstevel@tonic-gate 	struct timeval now;
1640Sstevel@tonic-gate 	struct tm ltime;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #ifdef NDEBUG
1670Sstevel@tonic-gate 	if (severity > st->st_log_level_min)
1680Sstevel@tonic-gate 		return;
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (gettimeofday(&now, NULL) != 0)
1720Sstevel@tonic-gate 		(void) fprintf(stderr, "gettimeofday(3C) failed: %s\n",
1730Sstevel@tonic-gate 		    strerror(errno));
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (st->st_log_timezone_known)
1760Sstevel@tonic-gate 		(void) strftime(timebuf, sizeof (timebuf), "%b %e %T",
1770Sstevel@tonic-gate 		    localtime_r(&now.tv_sec, &ltime));
1780Sstevel@tonic-gate 	else
1790Sstevel@tonic-gate 		xstrftime_poststart(timebuf, sizeof (timebuf), &now);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s/%d%s: ", timebuf, pthread_self(),
1820Sstevel@tonic-gate 	    prefix);
1830Sstevel@tonic-gate 	cp = strchr(buf, '\0');
1840Sstevel@tonic-gate 	(void) vsnprintf(cp, sizeof (buf) - (cp - buf), format, args);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #ifndef NDEBUG
1870Sstevel@tonic-gate 	/* Copy into logbuf. */
1880Sstevel@tonic-gate 	(void) pthread_mutex_lock(&logbuf_mutex);
1890Sstevel@tonic-gate 	if (strlen(logbuf) + strlen(buf) + 1 <= sizeof (logbuf))
1900Sstevel@tonic-gate 		(void) strcat(logbuf, buf);
1910Sstevel@tonic-gate 	else
1920Sstevel@tonic-gate 		(void) strlcpy(logbuf, buf, sizeof (logbuf));
1930Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&logbuf_mutex);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (severity > st->st_log_level_min)
1960Sstevel@tonic-gate 		return;
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate 
1991958Slianep 	if (st->st_log_flags & STARTD_LOG_FILE && logfile) {
2000Sstevel@tonic-gate 		(void) fputs(buf, logfile);
2011958Slianep 		(void) fflush(logfile);
2021958Slianep 	}
2030Sstevel@tonic-gate 	if (st->st_log_flags & STARTD_LOG_TERMINAL)
2040Sstevel@tonic-gate 		(void) fputs(buf, stdout);
2051958Slianep 	if (st->st_log_flags & STARTD_LOG_SYSLOG && st->st_log_timezone_known)
2060Sstevel@tonic-gate 		vsyslog(severity, format, args);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*PRINTFLIKE2*/
2100Sstevel@tonic-gate void
2110Sstevel@tonic-gate log_error(int severity, const char *format, ...)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	va_list args;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	va_start(args, format);
2160Sstevel@tonic-gate 	vlog_prefix(severity, " ERROR", format, args);
2170Sstevel@tonic-gate 	va_end(args);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*PRINTFLIKE2*/
2210Sstevel@tonic-gate void
2220Sstevel@tonic-gate log_framework(int severity, const char *format, ...)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	va_list args;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	va_start(args, format);
2270Sstevel@tonic-gate 	vlog_prefix(severity, "", format, args);
2280Sstevel@tonic-gate 	va_end(args);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * void log_preexec()
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * log_preexec() should be invoked prior to any exec(2) calls, to prevent the
2350Sstevel@tonic-gate  * logfile and syslogd file descriptors from being leaked to child processes.
2360Sstevel@tonic-gate  * Why openlog(3C) lacks a close-on-exec option is a minor mystery.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate void
2390Sstevel@tonic-gate log_preexec()
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	closelog();
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * void setlog()
2460Sstevel@tonic-gate  *   Close file descriptors and redirect output.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate void
2490Sstevel@tonic-gate setlog(const char *logstem)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int fd;
2520Sstevel@tonic-gate 	char logfile[PATH_MAX];
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	closefrom(0);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	(void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, logstem);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	(void) umask(fmask);
2610Sstevel@tonic-gate 	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,
2620Sstevel@tonic-gate 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
2630Sstevel@tonic-gate 	(void) umask(dmask);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	if (fd == -1)
2660Sstevel@tonic-gate 		return;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	(void) dup2(fd, 1);
2690Sstevel@tonic-gate 	(void) dup2(fd, 2);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if (fd != 1 && fd != 2)
2720Sstevel@tonic-gate 		startd_close(fd);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static int
2760Sstevel@tonic-gate log_dir_writeable(const char *path)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	int fd;
2790Sstevel@tonic-gate 	struct statvfs svb;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY, 0644)) == -1)
2820Sstevel@tonic-gate 		return (-1);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (fstatvfs(fd, &svb) == -1)
2850Sstevel@tonic-gate 		return (-1);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (svb.f_flag & ST_RDONLY) {
2880Sstevel@tonic-gate 		(void) close(fd);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		fd = -1;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	return (fd);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static void
2970Sstevel@tonic-gate vlog_instance(const char *fmri, const char *logstem, boolean_t canlog,
2980Sstevel@tonic-gate     const char *format, va_list args)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	char logfile[PATH_MAX];
3010Sstevel@tonic-gate 	char *message;
3020Sstevel@tonic-gate 	char omessage[1024];
3030Sstevel@tonic-gate 	int fd, err;
3040Sstevel@tonic-gate 	char timebuf[LOG_DATE_SIZE];
3050Sstevel@tonic-gate 	struct tm ltime;
3060Sstevel@tonic-gate 	struct timeval now;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	(void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix,
3090Sstevel@tonic-gate 	    logstem);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	(void) umask(fmask);
3120Sstevel@tonic-gate 	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,
3130Sstevel@tonic-gate 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
3140Sstevel@tonic-gate 	err = errno;
3150Sstevel@tonic-gate 	(void) umask(dmask);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (fd == -1) {
3180Sstevel@tonic-gate 		if (canlog)
3190Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: open(%s) "
3200Sstevel@tonic-gate 			    "failed with %s.\n", fmri, logfile, strerror(err));
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		return;
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	(void) vsnprintf(omessage, sizeof (omessage), format, args);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (gettimeofday(&now, NULL) != 0)
3280Sstevel@tonic-gate 		(void) fprintf(stderr, "gettimeofday(3C) failed: %s\n",
3290Sstevel@tonic-gate 		    strerror(errno));
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (st->st_log_timezone_known)
3320Sstevel@tonic-gate 		(void) strftime(timebuf, sizeof (timebuf), "%b %e %T",
3330Sstevel@tonic-gate 		    localtime_r(&now.tv_sec, &ltime));
3340Sstevel@tonic-gate 	else
3350Sstevel@tonic-gate 		xstrftime_poststart(timebuf, sizeof (timebuf), &now);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	message = uu_msprintf("[ %s %s ]\n", timebuf, omessage);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (message == NULL) {
3400Sstevel@tonic-gate 		if (canlog)
3410Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: %s.\n",
3420Sstevel@tonic-gate 			    fmri, uu_strerror(uu_error()));
3430Sstevel@tonic-gate 	} else {
3440Sstevel@tonic-gate 		if (write(fd, message, strlen(message)) < 0 && canlog)
3450Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: write(%d) "
3460Sstevel@tonic-gate 			    "failed with %s.\n", fmri, fd,
3470Sstevel@tonic-gate 			    strerror(errno));
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		uu_free(message);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (close(fd) != 0 && canlog)
3530Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "close(%d) failed: %s.\n", fd,
3540Sstevel@tonic-gate 		    strerror(errno));
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * void log_instance(const restarter_inst_t *, boolean_t, const char *, ...)
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  * The log_instance() format is "[ month day time message ]".  (The
3610Sstevel@tonic-gate  * brackets distinguish svc.startd messages from method output.)  We avoid
3620Sstevel@tonic-gate  * calling log_*() functions on error when canlog is not set, since we may
3630Sstevel@tonic-gate  * be called from a child process.
3640Sstevel@tonic-gate  *
3650Sstevel@tonic-gate  * When adding new calls to this function, consider: If this is called before
3660Sstevel@tonic-gate  * any instances have started, then it should be called with canlog clear,
3670Sstevel@tonic-gate  * lest we spew errors to the console when booted on the miniroot.
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate /*PRINTFLIKE3*/
3700Sstevel@tonic-gate void
3710Sstevel@tonic-gate log_instance(const restarter_inst_t *inst, boolean_t canlog,
3720Sstevel@tonic-gate     const char *format, ...)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	va_list args;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	va_start(args, format);
3770Sstevel@tonic-gate 	vlog_instance(inst->ri_i.i_fmri, inst->ri_logstem, canlog, format,
3780Sstevel@tonic-gate 	    args);
3790Sstevel@tonic-gate 	va_end(args);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * void log_instance_fmri(const char *, const char *,boolean_t, const char *,
3840Sstevel@tonic-gate  *    ...)
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * The log_instance_fmri() format is "[ month day time message ]".  (The
3870Sstevel@tonic-gate  * brackets distinguish svc.startd messages from method output.)  We avoid
3880Sstevel@tonic-gate  * calling log_*() functions on error when canlog is not set, since we may
3890Sstevel@tonic-gate  * be called from a child process.
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * For new calls to this function, see the warning in log_instance()'s
3920Sstevel@tonic-gate  * comment.
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate /*PRINTFLIKE4*/
3950Sstevel@tonic-gate void
3960Sstevel@tonic-gate log_instance_fmri(const char *fmri, const char *logstem, boolean_t canlog,
3970Sstevel@tonic-gate     const char *format, ...)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	va_list args;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	va_start(args, format);
4020Sstevel@tonic-gate 	vlog_instance(fmri, logstem, canlog, format, args);
4030Sstevel@tonic-gate 	va_end(args);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * void log_transition(const restarter_inst_t *, start_outcome_t)
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * The log_transition() format is
4100Sstevel@tonic-gate  *
4110Sstevel@tonic-gate  *   [ _service_fmri_ _participle_ (_common_name_) ]
4120Sstevel@tonic-gate  *
4130Sstevel@tonic-gate  * Again, brackets separate messages from specific service instance output to
4140Sstevel@tonic-gate  * the console.
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate void
4170Sstevel@tonic-gate log_transition(const restarter_inst_t *inst, start_outcome_t outcome)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	char *message;
4200Sstevel@tonic-gate 	char omessage[1024];
4210Sstevel@tonic-gate 	char *action;
4220Sstevel@tonic-gate 	int severity;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (outcome == START_REQUESTED) {
4250Sstevel@tonic-gate 		char *cname = NULL;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		cname = inst->ri_common_name;
4280Sstevel@tonic-gate 		if (cname == NULL)
4290Sstevel@tonic-gate 			cname = inst->ri_C_common_name;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (!(st->st_boot_flags & STARTD_BOOT_VERBOSE))
4320Sstevel@tonic-gate 			return;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		if (inst->ri_start_index > 1)
4350Sstevel@tonic-gate 			return;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		if (cname)
4380Sstevel@tonic-gate 			(void) snprintf(omessage, sizeof (omessage), " (%s)",
4390Sstevel@tonic-gate 			cname);
4400Sstevel@tonic-gate 		else
4410Sstevel@tonic-gate 			*omessage = '\0';
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		action = gettext("starting");
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		message = uu_msprintf("[ %s %s%s ]\n",
4460Sstevel@tonic-gate 		    inst->ri_i.i_fmri + strlen("svc:/"), action,
4470Sstevel@tonic-gate 		    omessage);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		severity = LOG_INFO;
4500Sstevel@tonic-gate 	} else {
4510Sstevel@tonic-gate 		switch (outcome) {
4521958Slianep 		case MAINT_REQUESTED:
4531958Slianep 			action = gettext("transitioned to maintenance by "
4541958Slianep 			    "request (see 'svcs -xv' for details)");
4551958Slianep 			break;
4560Sstevel@tonic-gate 		case START_FAILED_REPEATEDLY:
4571958Slianep 			action = gettext("failed repeatedly: transitioned to "
4581958Slianep 			    "maintenance (see 'svcs -xv' for details)");
4590Sstevel@tonic-gate 			break;
4600Sstevel@tonic-gate 		case START_FAILED_CONFIGURATION:
4611958Slianep 			action = gettext("misconfigured: transitioned to "
4621958Slianep 			    "maintenance (see 'svcs -xv' for details)");
4630Sstevel@tonic-gate 			break;
4640Sstevel@tonic-gate 		case START_FAILED_FATAL:
4651958Slianep 			action = gettext("failed fatally: transitioned to "
4661958Slianep 			    "maintenance (see 'svcs -xv' for details)");
4670Sstevel@tonic-gate 			break;
4680Sstevel@tonic-gate 		case START_FAILED_TIMEOUT_FATAL:
4691958Slianep 			action = gettext("timed out: transitioned to "
4701958Slianep 			    "maintenance (see 'svcs -xv' for details)");
4710Sstevel@tonic-gate 			break;
4720Sstevel@tonic-gate 		case START_FAILED_OTHER:
4731958Slianep 			action = gettext("failed: transitioned to "
4741958Slianep 			    "maintenance (see 'svcs -xv' for details)");
4750Sstevel@tonic-gate 			break;
4760Sstevel@tonic-gate 		case START_REQUESTED:
4770Sstevel@tonic-gate 			assert(outcome != START_REQUESTED);
4780Sstevel@tonic-gate 			/*FALLTHROUGH*/
4790Sstevel@tonic-gate 		default:
4800Sstevel@tonic-gate 			action = gettext("outcome unknown?");
4810Sstevel@tonic-gate 		}
4820Sstevel@tonic-gate 
4831958Slianep 		message = uu_msprintf("[ %s %s ]\n",
4841958Slianep 		    inst->ri_i.i_fmri + strlen("svc:/"), action);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		severity = LOG_ERR;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (message == NULL) {
4910Sstevel@tonic-gate 		log_error(LOG_NOTICE,
4920Sstevel@tonic-gate 		    "Could not log boot message for %s: %s.\n",
4930Sstevel@tonic-gate 		    inst->ri_i.i_fmri, uu_strerror(uu_error()));
4940Sstevel@tonic-gate 	} else {
4951958Slianep 		/*
4961958Slianep 		 * All significant errors should to go to syslog to
4971958Slianep 		 * communicate appropriate information even for systems
4981958Slianep 		 * without a console connected during boot.  Send the
4991958Slianep 		 * message to stderr only if the severity is lower than
5001958Slianep 		 * (indicated by >) LOG_ERR.
5011958Slianep 		 */
5021958Slianep 		if (!st->st_log_login_reached && severity > LOG_ERR) {
5030Sstevel@tonic-gate 			/*LINTED*/
5040Sstevel@tonic-gate 			if (fprintf(stderr, message) < 0)
5050Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not log for %s: "
5060Sstevel@tonic-gate 				    "fprintf() failed with %s.\n",
5070Sstevel@tonic-gate 				    inst->ri_i.i_fmri, strerror(errno));
5080Sstevel@tonic-gate 		} else {
5090Sstevel@tonic-gate 			log_framework(severity, "%s %s\n",
5100Sstevel@tonic-gate 			    inst->ri_i.i_fmri + strlen("svc:/"), action);
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		uu_free(message);
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * log_console - log a message to the consoles and to syslog
5190Sstevel@tonic-gate  *
5200Sstevel@tonic-gate  * This logs a message as-is to the console (and auxiliary consoles),
5210Sstevel@tonic-gate  * as well as to the master restarter log.
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate /*PRINTFLIKE2*/
5240Sstevel@tonic-gate void
5250Sstevel@tonic-gate log_console(int severity, const char *format, ...)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate 	va_list args;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	va_start(args, format);
5300Sstevel@tonic-gate 	vlog_prefix(severity, "", format, args);
5310Sstevel@tonic-gate 	va_end(args);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	va_start(args, format);
5340Sstevel@tonic-gate 	(void) vfprintf(stderr, format, args);
5350Sstevel@tonic-gate 	va_end(args);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * void log_init()
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * Set up the log files, if necessary, for the current invocation.  This
5420Sstevel@tonic-gate  * function should be called before any other functions in this file.  Set the
5430Sstevel@tonic-gate  * syslog(3C) logging mask such that severities of the importance of
5440Sstevel@tonic-gate  * LOG_NOTICE and above are passed through, but lower severity messages are
5450Sstevel@tonic-gate  * masked out.
5460Sstevel@tonic-gate  *
5470Sstevel@tonic-gate  * It may be called multiple times to change the logging configuration due to
5480Sstevel@tonic-gate  * administrative request.
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate void
5510Sstevel@tonic-gate log_init()
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	int dirfd, logfd;
5540Sstevel@tonic-gate 	char *dir;
5550Sstevel@tonic-gate 	struct stat sb;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (st->st_start_time.tv_sec == 0) {
5580Sstevel@tonic-gate 		if (getzoneid() != GLOBAL_ZONEID) {
5590Sstevel@tonic-gate 			st->st_start_time.tv_sec = time(NULL);
5600Sstevel@tonic-gate 		} else {
5610Sstevel@tonic-gate 			/*
5620Sstevel@tonic-gate 			 * We need to special-case the BOOT_TIME utmp entry, and
5630Sstevel@tonic-gate 			 * drag that value out of the kernel if it's there.
5640Sstevel@tonic-gate 			 */
5650Sstevel@tonic-gate 			kstat_ctl_t *kc;
5660Sstevel@tonic-gate 			kstat_t *ks;
5670Sstevel@tonic-gate 			kstat_named_t *boot;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			if (((kc = kstat_open()) != 0) &&
5700Sstevel@tonic-gate 			    ((ks = kstat_lookup(kc, "unix", 0, "system_misc"))
5710Sstevel@tonic-gate 			    != NULL) &&
5720Sstevel@tonic-gate 			    (kstat_read(kc, ks, NULL) != -1) &&
5730Sstevel@tonic-gate 			    ((boot = kstat_data_lookup(ks, "boot_time")) !=
5740Sstevel@tonic-gate 			    NULL)) {
5750Sstevel@tonic-gate 				/*
5760Sstevel@tonic-gate 				 * If we're here, then we've successfully found
5770Sstevel@tonic-gate 				 * the boot_time kstat... use its value.
5780Sstevel@tonic-gate 				 */
5790Sstevel@tonic-gate 				st->st_start_time.tv_sec = boot->value.ul;
5800Sstevel@tonic-gate 			} else {
5810Sstevel@tonic-gate 				st->st_start_time.tv_sec = time(NULL);
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 			if (kc)
5850Sstevel@tonic-gate 				(void) kstat_close(kc);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * Establish our timezone if the appropriate directory is available.
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if (!st->st_log_timezone_known && stat(FS_TIMEZONE_DIR, &sb) == 0) {
5930Sstevel@tonic-gate 		tzset();
5940Sstevel@tonic-gate 		st->st_log_timezone_known = 1;
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * Establish our locale if the appropriate directory is available.  Set
5990Sstevel@tonic-gate 	 * the locale string from the environment so we can extract template
6000Sstevel@tonic-gate 	 * information correctly, if the locale directories aren't yet
6010Sstevel@tonic-gate 	 * available.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if (st->st_locale != NULL)
6040Sstevel@tonic-gate 		free(st->st_locale);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if ((st->st_locale = getenv("LC_ALL")) == NULL)
6070Sstevel@tonic-gate 		if ((st->st_locale = getenv("LC_MESSAGES")) == NULL)
6080Sstevel@tonic-gate 			st->st_locale = getenv("LANG");
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (!st->st_log_locale_known && stat(FS_LOCALE_DIR, &sb) == 0) {
6110Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
6120Sstevel@tonic-gate 		st->st_locale = setlocale(LC_MESSAGES, "");
6130Sstevel@tonic-gate 		if (st->st_locale)
6140Sstevel@tonic-gate 			st->st_log_locale_known = 1;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		(void) textdomain(TEXT_DOMAIN);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (st->st_locale) {
6200Sstevel@tonic-gate 		st->st_locale = safe_strdup(st->st_locale);
6210Sstevel@tonic-gate 		xstr_sanitize(st->st_locale);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (logfile) {
6250Sstevel@tonic-gate 		(void) fclose(logfile);
6260Sstevel@tonic-gate 		logfile = NULL;
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * Set syslog(3C) behaviour in all cases.
6310Sstevel@tonic-gate 	 */
6320Sstevel@tonic-gate 	closelog();
6330Sstevel@tonic-gate 	openlog("svc.startd", LOG_PID | LOG_CONS, LOG_DAEMON);
6340Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((dirfd = log_dir_writeable(LOG_PREFIX_NORMAL)) == -1) {
6370Sstevel@tonic-gate 		if ((dirfd = log_dir_writeable(LOG_PREFIX_EARLY)) == -1)
6380Sstevel@tonic-gate 			return;
6390Sstevel@tonic-gate 		else
6400Sstevel@tonic-gate 			dir = LOG_PREFIX_EARLY;
6410Sstevel@tonic-gate 	} else {
6420Sstevel@tonic-gate 		dir = LOG_PREFIX_NORMAL;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	st->st_log_prefix = dir;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	(void) umask(fmask);
6480Sstevel@tonic-gate 	if ((logfd = openat(dirfd, STARTD_DEFAULT_LOG, O_CREAT | O_RDWR,
6490Sstevel@tonic-gate 	    0644)) == -1) {
6500Sstevel@tonic-gate 		(void) close(dirfd);
6510Sstevel@tonic-gate 		(void) umask(dmask);
6520Sstevel@tonic-gate 		return;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	(void) close(dirfd);
6560Sstevel@tonic-gate 	(void) umask(dmask);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if ((logfile = fdopen(logfd, "a")) == NULL)
6590Sstevel@tonic-gate 		if (errno != EROFS)
6600Sstevel@tonic-gate 			log_error(LOG_WARNING, "can't open logfile %s/%s",
6610Sstevel@tonic-gate 			    dir, STARTD_DEFAULT_LOG);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (logfile &&
6640Sstevel@tonic-gate 	    fcntl(fileno(logfile), F_SETFD, FD_CLOEXEC) == -1)
6650Sstevel@tonic-gate 		log_error(LOG_WARNING,
6660Sstevel@tonic-gate 		    "couldn't mark logfile close-on-exec: %s\n",
6670Sstevel@tonic-gate 		    strerror(errno));
6680Sstevel@tonic-gate }
669