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