xref: /onnv-gate/usr/src/cmd/svc/startd/log.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * log.c - debugging and logging functions
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Logging destinations
33*0Sstevel@tonic-gate  *   svc.startd(1M) supports three logging destinations:  the system log, a
34*0Sstevel@tonic-gate  *   daemon-specific log (in the /var/svc/log hierarchy by default), and to the
35*0Sstevel@tonic-gate  *   standard output.  Any or all of these destinations may be used to
36*0Sstevel@tonic-gate  *   communicate a specific message; the audiences for each destination differ.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  *   Generic messages associated with svc.startd(1M) are made by the
39*0Sstevel@tonic-gate  *   log_framework() and log_error() functions.  For these messages, svc.startd
40*0Sstevel@tonic-gate  *   logs under its own name and under the LOG_DAEMON facility when issuing
41*0Sstevel@tonic-gate  *   events to the system log.  By design, severities below LOG_NOTICE are never
42*0Sstevel@tonic-gate  *   issued to the system log.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  *   Messages associated with a specific service instance are logged using the
45*0Sstevel@tonic-gate  *   log_instance() or log_instance_fmri() functions.  These messages are always
46*0Sstevel@tonic-gate  *   sent to the appropriate per-instance log file.
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  *   In the case of verbose or debug boot, the log_transition() function
49*0Sstevel@tonic-gate  *   displays messages regarding instance transitions to the system console,
50*0Sstevel@tonic-gate  *   until the expected login services are available.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  *   Finally, log_console() displays messages to the system consoles and
53*0Sstevel@tonic-gate  *   the master restarter log file.  This is used when booting to a milestone
54*0Sstevel@tonic-gate  *   other than 'all'.
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * Logging detail
57*0Sstevel@tonic-gate  *   The constants for severity from <syslog.h> are reused, with a specific
58*0Sstevel@tonic-gate  *   convention here.  (It is worth noting that the #define values for the LOG_
59*0Sstevel@tonic-gate  *   levels are such that more important severities have lower values.)  The
60*0Sstevel@tonic-gate  *   severity determines the importance of the event, and its addressibility by
61*0Sstevel@tonic-gate  *   the administrator.  Each severity level's use is defined below, along with
62*0Sstevel@tonic-gate  *   an illustrative example.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  *   LOG_EMERG		Not used presently.
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  *   LOG_ALERT		An unrecoverable operation requiring external
67*0Sstevel@tonic-gate  *			intervention has occurred.   Includes an inability to
68*0Sstevel@tonic-gate  *			write to the smf(5) repository (due to svc.configd(1M)
69*0Sstevel@tonic-gate  *			absence, due to permissions failures, etc.).  Message
70*0Sstevel@tonic-gate  *			should identify component at fault.
71*0Sstevel@tonic-gate  *
72*0Sstevel@tonic-gate  *   LOG_CRIT		An unrecoverable operation internal to svc.startd(1M)
73*0Sstevel@tonic-gate  *			has occurred.  Failure should be recoverable by restart
74*0Sstevel@tonic-gate  *			of svc.startd(1M).
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  *   LOG_ERR		An smf(5) event requiring administrative intervention
77*0Sstevel@tonic-gate  *			has occurred.  Includes instance being moved to the
78*0Sstevel@tonic-gate  *			maintenance state.
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  *   LOG_WARNING	A potentially destabilizing smf(5) event not requiring
81*0Sstevel@tonic-gate  *			administrative intervention has occurred.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  *   LOG_NOTICE		A noteworthy smf(5) event has occurred.  Includes
84*0Sstevel@tonic-gate  *			individual instance failures.
85*0Sstevel@tonic-gate  *
86*0Sstevel@tonic-gate  *   LOG_INFO		A noteworthy operation internal to svc.startd(1M) has
87*0Sstevel@tonic-gate  *			occurred.  Includes recoverable failures or otherwise
88*0Sstevel@tonic-gate  *			unexpected outcomes.
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  *   LOG_DEBUG		An internal operation only of interest to a
91*0Sstevel@tonic-gate  *			svc.startd(1M) developer has occurred.
92*0Sstevel@tonic-gate  *
93*0Sstevel@tonic-gate  *  Logging configuration
94*0Sstevel@tonic-gate  *    While the logging output can be configured using the -d and -v flags at
95*0Sstevel@tonic-gate  *    invocation, the preferred approach is to set the logging property values
96*0Sstevel@tonic-gate  *    in the options property group of the svc.startd default instance.  The
97*0Sstevel@tonic-gate  *    valid values are "quiet", "verbose", and "debug".  "quiet" is the default;
98*0Sstevel@tonic-gate  *    "verbose" and "debug" allow LOG_INFO and LOG_DEBUG logging requests to
99*0Sstevel@tonic-gate  *    reach the daemon-specific log, respectively.
100*0Sstevel@tonic-gate  */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate #include <sys/stat.h>
103*0Sstevel@tonic-gate #include <sys/statvfs.h>
104*0Sstevel@tonic-gate #include <sys/time.h>
105*0Sstevel@tonic-gate #include <sys/types.h>
106*0Sstevel@tonic-gate #include <assert.h>
107*0Sstevel@tonic-gate #include <errno.h>
108*0Sstevel@tonic-gate #include <fcntl.h>
109*0Sstevel@tonic-gate #include <kstat.h>
110*0Sstevel@tonic-gate #include <libgen.h>
111*0Sstevel@tonic-gate #include <libintl.h>
112*0Sstevel@tonic-gate #include <libuutil.h>
113*0Sstevel@tonic-gate #include <locale.h>
114*0Sstevel@tonic-gate #include <malloc.h>
115*0Sstevel@tonic-gate #include <pthread.h>
116*0Sstevel@tonic-gate #include <stdarg.h>
117*0Sstevel@tonic-gate #include <stdio.h>
118*0Sstevel@tonic-gate #include <strings.h>
119*0Sstevel@tonic-gate #include <syslog.h>
120*0Sstevel@tonic-gate #include <unistd.h>
121*0Sstevel@tonic-gate #include <zone.h>
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #include "startd.h"
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate #define	LOGBUF_SZ	(60 * 80)			/* 60 lines */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static FILE *logfile = NULL;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #ifndef NDEBUG
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate  * This is a circular buffer for all (even those not emitted externally)
133*0Sstevel@tonic-gate  * logging messages.  To read it properly you should start after the first
134*0Sstevel@tonic-gate  * null, go until the second, and then go back to the beginning until the
135*0Sstevel@tonic-gate  * first null.  Or use ::startd_log in mdb.
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate /* LINTED unused */
138*0Sstevel@tonic-gate static const size_t logbuf_sz = LOGBUF_SZ;		/* For mdb */
139*0Sstevel@tonic-gate static char logbuf[LOGBUF_SZ] = "";
140*0Sstevel@tonic-gate static pthread_mutex_t logbuf_mutex = PTHREAD_MUTEX_INITIALIZER;
141*0Sstevel@tonic-gate #endif
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate static void
144*0Sstevel@tonic-gate xstrftime_poststart(char *buf, size_t bufsize, struct timeval *time)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	long sec, usec;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	sec = time->tv_sec - st->st_start_time.tv_sec;
149*0Sstevel@tonic-gate 	usec = time->tv_usec - st->st_start_time.tv_usec;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (usec < 0) {
152*0Sstevel@tonic-gate 		sec -= 1;
153*0Sstevel@tonic-gate 		usec += 1000000;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "start + %d.%02ds", sec, usec / 10000);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate static void
160*0Sstevel@tonic-gate vlog_prefix(int severity, const char *prefix, const char *format, va_list args)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	char buf[512], *cp;
163*0Sstevel@tonic-gate 	char timebuf[LOG_DATE_SIZE];
164*0Sstevel@tonic-gate 	struct timeval now;
165*0Sstevel@tonic-gate 	struct tm ltime;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate #ifdef NDEBUG
168*0Sstevel@tonic-gate 	if (severity > st->st_log_level_min)
169*0Sstevel@tonic-gate 		return;
170*0Sstevel@tonic-gate #endif
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (gettimeofday(&now, NULL) != 0)
173*0Sstevel@tonic-gate 		(void) fprintf(stderr, "gettimeofday(3C) failed: %s\n",
174*0Sstevel@tonic-gate 		    strerror(errno));
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (st->st_log_timezone_known)
177*0Sstevel@tonic-gate 		(void) strftime(timebuf, sizeof (timebuf), "%b %e %T",
178*0Sstevel@tonic-gate 		    localtime_r(&now.tv_sec, &ltime));
179*0Sstevel@tonic-gate 	else
180*0Sstevel@tonic-gate 		xstrftime_poststart(timebuf, sizeof (timebuf), &now);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s/%d%s: ", timebuf, pthread_self(),
183*0Sstevel@tonic-gate 	    prefix);
184*0Sstevel@tonic-gate 	cp = strchr(buf, '\0');
185*0Sstevel@tonic-gate 	(void) vsnprintf(cp, sizeof (buf) - (cp - buf), format, args);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate #ifndef NDEBUG
188*0Sstevel@tonic-gate 	/* Copy into logbuf. */
189*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&logbuf_mutex);
190*0Sstevel@tonic-gate 	if (strlen(logbuf) + strlen(buf) + 1 <= sizeof (logbuf))
191*0Sstevel@tonic-gate 		(void) strcat(logbuf, buf);
192*0Sstevel@tonic-gate 	else
193*0Sstevel@tonic-gate 		(void) strlcpy(logbuf, buf, sizeof (logbuf));
194*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&logbuf_mutex);
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (severity > st->st_log_level_min)
197*0Sstevel@tonic-gate 		return;
198*0Sstevel@tonic-gate #endif
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (st->st_log_flags & STARTD_LOG_FILE && logfile)
201*0Sstevel@tonic-gate 		(void) fputs(buf, logfile);
202*0Sstevel@tonic-gate 	if (st->st_log_flags & STARTD_LOG_TERMINAL)
203*0Sstevel@tonic-gate 		(void) fputs(buf, stdout);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (st->st_log_timezone_known)
206*0Sstevel@tonic-gate 		vsyslog(severity, format, args);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (st->st_log_flags & STARTD_LOG_FILE && logfile)
209*0Sstevel@tonic-gate 		(void) fflush(logfile);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*PRINTFLIKE2*/
213*0Sstevel@tonic-gate void
214*0Sstevel@tonic-gate log_error(int severity, const char *format, ...)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	va_list args;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	va_start(args, format);
219*0Sstevel@tonic-gate 	vlog_prefix(severity, " ERROR", format, args);
220*0Sstevel@tonic-gate 	va_end(args);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /*PRINTFLIKE2*/
224*0Sstevel@tonic-gate void
225*0Sstevel@tonic-gate log_framework(int severity, const char *format, ...)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	va_list args;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	va_start(args, format);
230*0Sstevel@tonic-gate 	vlog_prefix(severity, "", format, args);
231*0Sstevel@tonic-gate 	va_end(args);
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate  * void log_preexec()
236*0Sstevel@tonic-gate  *
237*0Sstevel@tonic-gate  * log_preexec() should be invoked prior to any exec(2) calls, to prevent the
238*0Sstevel@tonic-gate  * logfile and syslogd file descriptors from being leaked to child processes.
239*0Sstevel@tonic-gate  * Why openlog(3C) lacks a close-on-exec option is a minor mystery.
240*0Sstevel@tonic-gate  */
241*0Sstevel@tonic-gate void
242*0Sstevel@tonic-gate log_preexec()
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate 	closelog();
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * void setlog()
249*0Sstevel@tonic-gate  *   Close file descriptors and redirect output.
250*0Sstevel@tonic-gate  */
251*0Sstevel@tonic-gate void
252*0Sstevel@tonic-gate setlog(const char *logstem)
253*0Sstevel@tonic-gate {
254*0Sstevel@tonic-gate 	int fd;
255*0Sstevel@tonic-gate 	char logfile[PATH_MAX];
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	closefrom(0);
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	(void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, logstem);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	(void) umask(fmask);
264*0Sstevel@tonic-gate 	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,
265*0Sstevel@tonic-gate 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
266*0Sstevel@tonic-gate 	(void) umask(dmask);
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	if (fd == -1)
269*0Sstevel@tonic-gate 		return;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	(void) dup2(fd, 1);
272*0Sstevel@tonic-gate 	(void) dup2(fd, 2);
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	if (fd != 1 && fd != 2)
275*0Sstevel@tonic-gate 		startd_close(fd);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate static int
279*0Sstevel@tonic-gate log_dir_writeable(const char *path)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	int fd;
282*0Sstevel@tonic-gate 	struct statvfs svb;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY, 0644)) == -1)
285*0Sstevel@tonic-gate 		return (-1);
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (fstatvfs(fd, &svb) == -1)
288*0Sstevel@tonic-gate 		return (-1);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (svb.f_flag & ST_RDONLY) {
291*0Sstevel@tonic-gate 		(void) close(fd);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		fd = -1;
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	return (fd);
297*0Sstevel@tonic-gate }
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate static void
300*0Sstevel@tonic-gate vlog_instance(const char *fmri, const char *logstem, boolean_t canlog,
301*0Sstevel@tonic-gate     const char *format, va_list args)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate 	char logfile[PATH_MAX];
304*0Sstevel@tonic-gate 	char *message;
305*0Sstevel@tonic-gate 	char omessage[1024];
306*0Sstevel@tonic-gate 	int fd, err;
307*0Sstevel@tonic-gate 	char timebuf[LOG_DATE_SIZE];
308*0Sstevel@tonic-gate 	struct tm ltime;
309*0Sstevel@tonic-gate 	struct timeval now;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	(void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix,
312*0Sstevel@tonic-gate 	    logstem);
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	(void) umask(fmask);
315*0Sstevel@tonic-gate 	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,
316*0Sstevel@tonic-gate 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
317*0Sstevel@tonic-gate 	err = errno;
318*0Sstevel@tonic-gate 	(void) umask(dmask);
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	if (fd == -1) {
321*0Sstevel@tonic-gate 		if (canlog)
322*0Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: open(%s) "
323*0Sstevel@tonic-gate 			    "failed with %s.\n", fmri, logfile, strerror(err));
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 		return;
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	(void) vsnprintf(omessage, sizeof (omessage), format, args);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (gettimeofday(&now, NULL) != 0)
331*0Sstevel@tonic-gate 		(void) fprintf(stderr, "gettimeofday(3C) failed: %s\n",
332*0Sstevel@tonic-gate 		    strerror(errno));
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	if (st->st_log_timezone_known)
335*0Sstevel@tonic-gate 		(void) strftime(timebuf, sizeof (timebuf), "%b %e %T",
336*0Sstevel@tonic-gate 		    localtime_r(&now.tv_sec, &ltime));
337*0Sstevel@tonic-gate 	else
338*0Sstevel@tonic-gate 		xstrftime_poststart(timebuf, sizeof (timebuf), &now);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	message = uu_msprintf("[ %s %s ]\n", timebuf, omessage);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if (message == NULL) {
343*0Sstevel@tonic-gate 		if (canlog)
344*0Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: %s.\n",
345*0Sstevel@tonic-gate 			    fmri, uu_strerror(uu_error()));
346*0Sstevel@tonic-gate 	} else {
347*0Sstevel@tonic-gate 		if (write(fd, message, strlen(message)) < 0 && canlog)
348*0Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not log for %s: write(%d) "
349*0Sstevel@tonic-gate 			    "failed with %s.\n", fmri, fd,
350*0Sstevel@tonic-gate 			    strerror(errno));
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 		uu_free(message);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	if (close(fd) != 0 && canlog)
356*0Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "close(%d) failed: %s.\n", fd,
357*0Sstevel@tonic-gate 		    strerror(errno));
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate /*
361*0Sstevel@tonic-gate  * void log_instance(const restarter_inst_t *, boolean_t, const char *, ...)
362*0Sstevel@tonic-gate  *
363*0Sstevel@tonic-gate  * The log_instance() format is "[ month day time message ]".  (The
364*0Sstevel@tonic-gate  * brackets distinguish svc.startd messages from method output.)  We avoid
365*0Sstevel@tonic-gate  * calling log_*() functions on error when canlog is not set, since we may
366*0Sstevel@tonic-gate  * be called from a child process.
367*0Sstevel@tonic-gate  *
368*0Sstevel@tonic-gate  * When adding new calls to this function, consider: If this is called before
369*0Sstevel@tonic-gate  * any instances have started, then it should be called with canlog clear,
370*0Sstevel@tonic-gate  * lest we spew errors to the console when booted on the miniroot.
371*0Sstevel@tonic-gate  */
372*0Sstevel@tonic-gate /*PRINTFLIKE3*/
373*0Sstevel@tonic-gate void
374*0Sstevel@tonic-gate log_instance(const restarter_inst_t *inst, boolean_t canlog,
375*0Sstevel@tonic-gate     const char *format, ...)
376*0Sstevel@tonic-gate {
377*0Sstevel@tonic-gate 	va_list args;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	va_start(args, format);
380*0Sstevel@tonic-gate 	vlog_instance(inst->ri_i.i_fmri, inst->ri_logstem, canlog, format,
381*0Sstevel@tonic-gate 	    args);
382*0Sstevel@tonic-gate 	va_end(args);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /*
386*0Sstevel@tonic-gate  * void log_instance_fmri(const char *, const char *,boolean_t, const char *,
387*0Sstevel@tonic-gate  *    ...)
388*0Sstevel@tonic-gate  *
389*0Sstevel@tonic-gate  * The log_instance_fmri() format is "[ month day time message ]".  (The
390*0Sstevel@tonic-gate  * brackets distinguish svc.startd messages from method output.)  We avoid
391*0Sstevel@tonic-gate  * calling log_*() functions on error when canlog is not set, since we may
392*0Sstevel@tonic-gate  * be called from a child process.
393*0Sstevel@tonic-gate  *
394*0Sstevel@tonic-gate  * For new calls to this function, see the warning in log_instance()'s
395*0Sstevel@tonic-gate  * comment.
396*0Sstevel@tonic-gate  */
397*0Sstevel@tonic-gate /*PRINTFLIKE4*/
398*0Sstevel@tonic-gate void
399*0Sstevel@tonic-gate log_instance_fmri(const char *fmri, const char *logstem, boolean_t canlog,
400*0Sstevel@tonic-gate     const char *format, ...)
401*0Sstevel@tonic-gate {
402*0Sstevel@tonic-gate 	va_list args;
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	va_start(args, format);
405*0Sstevel@tonic-gate 	vlog_instance(fmri, logstem, canlog, format, args);
406*0Sstevel@tonic-gate 	va_end(args);
407*0Sstevel@tonic-gate }
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate /*
410*0Sstevel@tonic-gate  * void log_transition(const restarter_inst_t *, start_outcome_t)
411*0Sstevel@tonic-gate  *
412*0Sstevel@tonic-gate  * The log_transition() format is
413*0Sstevel@tonic-gate  *
414*0Sstevel@tonic-gate  *   [ _service_fmri_ _participle_ (_common_name_) ]
415*0Sstevel@tonic-gate  *
416*0Sstevel@tonic-gate  * Again, brackets separate messages from specific service instance output to
417*0Sstevel@tonic-gate  * the console.
418*0Sstevel@tonic-gate  */
419*0Sstevel@tonic-gate void
420*0Sstevel@tonic-gate log_transition(const restarter_inst_t *inst, start_outcome_t outcome)
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	char *message;
423*0Sstevel@tonic-gate 	char omessage[1024];
424*0Sstevel@tonic-gate 	char *action;
425*0Sstevel@tonic-gate 	int severity;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	if (outcome == START_REQUESTED) {
428*0Sstevel@tonic-gate 		char *cname = NULL;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 		cname = inst->ri_common_name;
431*0Sstevel@tonic-gate 		if (cname == NULL)
432*0Sstevel@tonic-gate 			cname = inst->ri_C_common_name;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 		if (!(st->st_boot_flags & STARTD_BOOT_VERBOSE))
435*0Sstevel@tonic-gate 			return;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 		if (inst->ri_start_index > 1)
438*0Sstevel@tonic-gate 			return;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 		if (cname)
441*0Sstevel@tonic-gate 			(void) snprintf(omessage, sizeof (omessage), " (%s)",
442*0Sstevel@tonic-gate 			cname);
443*0Sstevel@tonic-gate 		else
444*0Sstevel@tonic-gate 			*omessage = '\0';
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 		action = gettext("starting");
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 		message = uu_msprintf("[ %s %s%s ]\n",
449*0Sstevel@tonic-gate 		    inst->ri_i.i_fmri + strlen("svc:/"), action,
450*0Sstevel@tonic-gate 		    omessage);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 		severity = LOG_INFO;
453*0Sstevel@tonic-gate 	} else {
454*0Sstevel@tonic-gate 		switch (outcome) {
455*0Sstevel@tonic-gate 		case START_FAILED_REPEATEDLY:
456*0Sstevel@tonic-gate 			action = gettext("failed repeatedly");
457*0Sstevel@tonic-gate 			break;
458*0Sstevel@tonic-gate 		case START_FAILED_CONFIGURATION:
459*0Sstevel@tonic-gate 			action = gettext("misconfigured");
460*0Sstevel@tonic-gate 			break;
461*0Sstevel@tonic-gate 		case START_FAILED_FATAL:
462*0Sstevel@tonic-gate 			action = gettext("failed fatally");
463*0Sstevel@tonic-gate 			break;
464*0Sstevel@tonic-gate 		case START_FAILED_TIMEOUT_FATAL:
465*0Sstevel@tonic-gate 			action = gettext("timed out, fault threshold reached");
466*0Sstevel@tonic-gate 			break;
467*0Sstevel@tonic-gate 		case START_FAILED_OTHER:
468*0Sstevel@tonic-gate 			action = gettext("failed");
469*0Sstevel@tonic-gate 			break;
470*0Sstevel@tonic-gate 		case START_REQUESTED:
471*0Sstevel@tonic-gate 			assert(outcome != START_REQUESTED);
472*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
473*0Sstevel@tonic-gate 		default:
474*0Sstevel@tonic-gate 			action = gettext("outcome unknown?");
475*0Sstevel@tonic-gate 		}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		message = uu_msprintf("[ %s %s %s ]\n",
478*0Sstevel@tonic-gate 		    inst->ri_i.i_fmri + strlen("svc:/"), action,
479*0Sstevel@tonic-gate 		    gettext("(see 'svcs -x' for details)"));
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		severity = LOG_ERR;
482*0Sstevel@tonic-gate 	}
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	if (message == NULL) {
486*0Sstevel@tonic-gate 		log_error(LOG_NOTICE,
487*0Sstevel@tonic-gate 		    "Could not log boot message for %s: %s.\n",
488*0Sstevel@tonic-gate 		    inst->ri_i.i_fmri, uu_strerror(uu_error()));
489*0Sstevel@tonic-gate 	} else {
490*0Sstevel@tonic-gate 		if (!st->st_log_login_reached) {
491*0Sstevel@tonic-gate 			/*LINTED*/
492*0Sstevel@tonic-gate 			if (fprintf(stderr, message) < 0)
493*0Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not log for %s: "
494*0Sstevel@tonic-gate 				    "fprintf() failed with %s.\n",
495*0Sstevel@tonic-gate 				    inst->ri_i.i_fmri, strerror(errno));
496*0Sstevel@tonic-gate 		} else {
497*0Sstevel@tonic-gate 			log_framework(severity, "%s %s\n",
498*0Sstevel@tonic-gate 			    inst->ri_i.i_fmri + strlen("svc:/"), action);
499*0Sstevel@tonic-gate 		}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		uu_free(message);
502*0Sstevel@tonic-gate 	}
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate /*
506*0Sstevel@tonic-gate  * log_console - log a message to the consoles and to syslog
507*0Sstevel@tonic-gate  *
508*0Sstevel@tonic-gate  * This logs a message as-is to the console (and auxiliary consoles),
509*0Sstevel@tonic-gate  * as well as to the master restarter log.
510*0Sstevel@tonic-gate  */
511*0Sstevel@tonic-gate /*PRINTFLIKE2*/
512*0Sstevel@tonic-gate void
513*0Sstevel@tonic-gate log_console(int severity, const char *format, ...)
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	va_list args;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	va_start(args, format);
518*0Sstevel@tonic-gate 	vlog_prefix(severity, "", format, args);
519*0Sstevel@tonic-gate 	va_end(args);
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	va_start(args, format);
522*0Sstevel@tonic-gate 	(void) vfprintf(stderr, format, args);
523*0Sstevel@tonic-gate 	va_end(args);
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate /*
527*0Sstevel@tonic-gate  * void log_init()
528*0Sstevel@tonic-gate  *
529*0Sstevel@tonic-gate  * Set up the log files, if necessary, for the current invocation.  This
530*0Sstevel@tonic-gate  * function should be called before any other functions in this file.  Set the
531*0Sstevel@tonic-gate  * syslog(3C) logging mask such that severities of the importance of
532*0Sstevel@tonic-gate  * LOG_NOTICE and above are passed through, but lower severity messages are
533*0Sstevel@tonic-gate  * masked out.
534*0Sstevel@tonic-gate  *
535*0Sstevel@tonic-gate  * It may be called multiple times to change the logging configuration due to
536*0Sstevel@tonic-gate  * administrative request.
537*0Sstevel@tonic-gate  */
538*0Sstevel@tonic-gate void
539*0Sstevel@tonic-gate log_init()
540*0Sstevel@tonic-gate {
541*0Sstevel@tonic-gate 	int dirfd, logfd;
542*0Sstevel@tonic-gate 	char *dir;
543*0Sstevel@tonic-gate 	struct stat sb;
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 	if (st->st_start_time.tv_sec == 0) {
546*0Sstevel@tonic-gate 		if (getzoneid() != GLOBAL_ZONEID) {
547*0Sstevel@tonic-gate 			st->st_start_time.tv_sec = time(NULL);
548*0Sstevel@tonic-gate 		} else {
549*0Sstevel@tonic-gate 			/*
550*0Sstevel@tonic-gate 			 * We need to special-case the BOOT_TIME utmp entry, and
551*0Sstevel@tonic-gate 			 * drag that value out of the kernel if it's there.
552*0Sstevel@tonic-gate 			 */
553*0Sstevel@tonic-gate 			kstat_ctl_t *kc;
554*0Sstevel@tonic-gate 			kstat_t *ks;
555*0Sstevel@tonic-gate 			kstat_named_t *boot;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 			if (((kc = kstat_open()) != 0) &&
558*0Sstevel@tonic-gate 			    ((ks = kstat_lookup(kc, "unix", 0, "system_misc"))
559*0Sstevel@tonic-gate 			    != NULL) &&
560*0Sstevel@tonic-gate 			    (kstat_read(kc, ks, NULL) != -1) &&
561*0Sstevel@tonic-gate 			    ((boot = kstat_data_lookup(ks, "boot_time")) !=
562*0Sstevel@tonic-gate 			    NULL)) {
563*0Sstevel@tonic-gate 				/*
564*0Sstevel@tonic-gate 				 * If we're here, then we've successfully found
565*0Sstevel@tonic-gate 				 * the boot_time kstat... use its value.
566*0Sstevel@tonic-gate 				 */
567*0Sstevel@tonic-gate 				st->st_start_time.tv_sec = boot->value.ul;
568*0Sstevel@tonic-gate 			} else {
569*0Sstevel@tonic-gate 				st->st_start_time.tv_sec = time(NULL);
570*0Sstevel@tonic-gate 			}
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 			if (kc)
573*0Sstevel@tonic-gate 				(void) kstat_close(kc);
574*0Sstevel@tonic-gate 		}
575*0Sstevel@tonic-gate 	}
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	/*
578*0Sstevel@tonic-gate 	 * Establish our timezone if the appropriate directory is available.
579*0Sstevel@tonic-gate 	 */
580*0Sstevel@tonic-gate 	if (!st->st_log_timezone_known && stat(FS_TIMEZONE_DIR, &sb) == 0) {
581*0Sstevel@tonic-gate 		tzset();
582*0Sstevel@tonic-gate 		st->st_log_timezone_known = 1;
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	/*
586*0Sstevel@tonic-gate 	 * Establish our locale if the appropriate directory is available.  Set
587*0Sstevel@tonic-gate 	 * the locale string from the environment so we can extract template
588*0Sstevel@tonic-gate 	 * information correctly, if the locale directories aren't yet
589*0Sstevel@tonic-gate 	 * available.
590*0Sstevel@tonic-gate 	 */
591*0Sstevel@tonic-gate 	if (st->st_locale != NULL)
592*0Sstevel@tonic-gate 		free(st->st_locale);
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if ((st->st_locale = getenv("LC_ALL")) == NULL)
595*0Sstevel@tonic-gate 		if ((st->st_locale = getenv("LC_MESSAGES")) == NULL)
596*0Sstevel@tonic-gate 			st->st_locale = getenv("LANG");
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	if (!st->st_log_locale_known && stat(FS_LOCALE_DIR, &sb) == 0) {
599*0Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
600*0Sstevel@tonic-gate 		st->st_locale = setlocale(LC_MESSAGES, "");
601*0Sstevel@tonic-gate 		if (st->st_locale)
602*0Sstevel@tonic-gate 			st->st_log_locale_known = 1;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 		(void) textdomain(TEXT_DOMAIN);
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	if (st->st_locale) {
608*0Sstevel@tonic-gate 		st->st_locale = safe_strdup(st->st_locale);
609*0Sstevel@tonic-gate 		xstr_sanitize(st->st_locale);
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	if (logfile) {
613*0Sstevel@tonic-gate 		(void) fclose(logfile);
614*0Sstevel@tonic-gate 		logfile = NULL;
615*0Sstevel@tonic-gate 	}
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	/*
618*0Sstevel@tonic-gate 	 * Set syslog(3C) behaviour in all cases.
619*0Sstevel@tonic-gate 	 */
620*0Sstevel@tonic-gate 	closelog();
621*0Sstevel@tonic-gate 	openlog("svc.startd", LOG_PID | LOG_CONS, LOG_DAEMON);
622*0Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	if ((dirfd = log_dir_writeable(LOG_PREFIX_NORMAL)) == -1) {
625*0Sstevel@tonic-gate 		if ((dirfd = log_dir_writeable(LOG_PREFIX_EARLY)) == -1)
626*0Sstevel@tonic-gate 			return;
627*0Sstevel@tonic-gate 		else
628*0Sstevel@tonic-gate 			dir = LOG_PREFIX_EARLY;
629*0Sstevel@tonic-gate 	} else {
630*0Sstevel@tonic-gate 		dir = LOG_PREFIX_NORMAL;
631*0Sstevel@tonic-gate 	}
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	st->st_log_prefix = dir;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	(void) umask(fmask);
636*0Sstevel@tonic-gate 	if ((logfd = openat(dirfd, STARTD_DEFAULT_LOG, O_CREAT | O_RDWR,
637*0Sstevel@tonic-gate 	    0644)) == -1) {
638*0Sstevel@tonic-gate 		(void) close(dirfd);
639*0Sstevel@tonic-gate 		(void) umask(dmask);
640*0Sstevel@tonic-gate 		return;
641*0Sstevel@tonic-gate 	}
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	(void) close(dirfd);
644*0Sstevel@tonic-gate 	(void) umask(dmask);
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	if ((logfile = fdopen(logfd, "a")) == NULL)
647*0Sstevel@tonic-gate 		if (errno != EROFS)
648*0Sstevel@tonic-gate 			log_error(LOG_WARNING, "can't open logfile %s/%s",
649*0Sstevel@tonic-gate 			    dir, STARTD_DEFAULT_LOG);
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	if (logfile &&
652*0Sstevel@tonic-gate 	    fcntl(fileno(logfile), F_SETFD, FD_CLOEXEC) == -1)
653*0Sstevel@tonic-gate 		log_error(LOG_WARNING,
654*0Sstevel@tonic-gate 		    "couldn't mark logfile close-on-exec: %s\n",
655*0Sstevel@tonic-gate 		    strerror(errno));
656*0Sstevel@tonic-gate }
657