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 /* 225617Sacruz * Copyright 2007 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 942258Srm88369 * 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 982258Srm88369 * 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 129*5680Srm88369 /* 130*5680Srm88369 * This parameter can be modified using mdb to turn on & off extended 131*5680Srm88369 * internal debug logging. Although a performance loss can be expected. 132*5680Srm88369 */ 133*5680Srm88369 static int internal_debug_flags = 0x0; 134*5680Srm88369 1350Sstevel@tonic-gate #ifndef NDEBUG 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * This is a circular buffer for all (even those not emitted externally) 1380Sstevel@tonic-gate * logging messages. To read it properly you should start after the first 1390Sstevel@tonic-gate * null, go until the second, and then go back to the beginning until the 1400Sstevel@tonic-gate * first null. Or use ::startd_log in mdb. 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate static char logbuf[LOGBUF_SZ] = ""; 1430Sstevel@tonic-gate static pthread_mutex_t logbuf_mutex = PTHREAD_MUTEX_INITIALIZER; 1440Sstevel@tonic-gate #endif 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate static void 1470Sstevel@tonic-gate xstrftime_poststart(char *buf, size_t bufsize, struct timeval *time) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate long sec, usec; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate sec = time->tv_sec - st->st_start_time.tv_sec; 1520Sstevel@tonic-gate usec = time->tv_usec - st->st_start_time.tv_usec; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate if (usec < 0) { 1550Sstevel@tonic-gate sec -= 1; 1560Sstevel@tonic-gate usec += 1000000; 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate (void) snprintf(buf, bufsize, "start + %d.%02ds", sec, usec / 10000); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate static void 1630Sstevel@tonic-gate vlog_prefix(int severity, const char *prefix, const char *format, va_list args) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate char buf[512], *cp; 1660Sstevel@tonic-gate char timebuf[LOG_DATE_SIZE]; 1670Sstevel@tonic-gate struct timeval now; 1680Sstevel@tonic-gate struct tm ltime; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate #ifdef NDEBUG 1710Sstevel@tonic-gate if (severity > st->st_log_level_min) 1720Sstevel@tonic-gate return; 1730Sstevel@tonic-gate #endif 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate if (gettimeofday(&now, NULL) != 0) 1760Sstevel@tonic-gate (void) fprintf(stderr, "gettimeofday(3C) failed: %s\n", 1770Sstevel@tonic-gate strerror(errno)); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if (st->st_log_timezone_known) 1800Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), "%b %e %T", 1810Sstevel@tonic-gate localtime_r(&now.tv_sec, <ime)); 1820Sstevel@tonic-gate else 1830Sstevel@tonic-gate xstrftime_poststart(timebuf, sizeof (timebuf), &now); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s/%d%s: ", timebuf, pthread_self(), 1860Sstevel@tonic-gate prefix); 1870Sstevel@tonic-gate cp = strchr(buf, '\0'); 1880Sstevel@tonic-gate (void) vsnprintf(cp, sizeof (buf) - (cp - buf), format, args); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate #ifndef NDEBUG 1910Sstevel@tonic-gate /* Copy into logbuf. */ 1920Sstevel@tonic-gate (void) pthread_mutex_lock(&logbuf_mutex); 1930Sstevel@tonic-gate if (strlen(logbuf) + strlen(buf) + 1 <= sizeof (logbuf)) 1940Sstevel@tonic-gate (void) strcat(logbuf, buf); 1950Sstevel@tonic-gate else 1960Sstevel@tonic-gate (void) strlcpy(logbuf, buf, sizeof (logbuf)); 1970Sstevel@tonic-gate (void) pthread_mutex_unlock(&logbuf_mutex); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate if (severity > st->st_log_level_min) 2000Sstevel@tonic-gate return; 2010Sstevel@tonic-gate #endif 2020Sstevel@tonic-gate 2031958Slianep if (st->st_log_flags & STARTD_LOG_FILE && logfile) { 2040Sstevel@tonic-gate (void) fputs(buf, logfile); 2051958Slianep (void) fflush(logfile); 2061958Slianep } 2070Sstevel@tonic-gate if (st->st_log_flags & STARTD_LOG_TERMINAL) 2080Sstevel@tonic-gate (void) fputs(buf, stdout); 2091958Slianep if (st->st_log_flags & STARTD_LOG_SYSLOG && st->st_log_timezone_known) 2100Sstevel@tonic-gate vsyslog(severity, format, args); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /*PRINTFLIKE2*/ 2140Sstevel@tonic-gate void 2150Sstevel@tonic-gate log_error(int severity, const char *format, ...) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate va_list args; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate va_start(args, format); 2200Sstevel@tonic-gate vlog_prefix(severity, " ERROR", format, args); 2210Sstevel@tonic-gate va_end(args); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /*PRINTFLIKE2*/ 2250Sstevel@tonic-gate void 2260Sstevel@tonic-gate log_framework(int severity, const char *format, ...) 2270Sstevel@tonic-gate { 2280Sstevel@tonic-gate va_list args; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate va_start(args, format); 2310Sstevel@tonic-gate vlog_prefix(severity, "", format, args); 2320Sstevel@tonic-gate va_end(args); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* 236*5680Srm88369 * log_framework2() differs from log_framework() by the fact that 237*5680Srm88369 * some checking are done before logging the messages in the internal 238*5680Srm88369 * buffer for performance reasons. 239*5680Srm88369 * The messages aren't logged if: 240*5680Srm88369 * - severity >= LOG_DEBUG and 241*5680Srm88369 * - st_log_level_min < LOG_DEBUG and 242*5680Srm88369 * - internal_debug_flags is not set for 'flags' 243*5680Srm88369 */ 244*5680Srm88369 void 245*5680Srm88369 log_framework2(int severity, int flags, const char *format, ...) 246*5680Srm88369 { 247*5680Srm88369 va_list args; 248*5680Srm88369 249*5680Srm88369 if ((severity < LOG_DEBUG) || 250*5680Srm88369 (internal_debug_flags & flags) || 251*5680Srm88369 st->st_log_level_min >= LOG_DEBUG) { 252*5680Srm88369 va_start(args, format); 253*5680Srm88369 vlog_prefix(severity, "", format, args); 254*5680Srm88369 va_end(args); 255*5680Srm88369 } 256*5680Srm88369 } 257*5680Srm88369 258*5680Srm88369 /* 2590Sstevel@tonic-gate * void log_preexec() 2600Sstevel@tonic-gate * 2610Sstevel@tonic-gate * log_preexec() should be invoked prior to any exec(2) calls, to prevent the 2620Sstevel@tonic-gate * logfile and syslogd file descriptors from being leaked to child processes. 2630Sstevel@tonic-gate * Why openlog(3C) lacks a close-on-exec option is a minor mystery. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate void 2660Sstevel@tonic-gate log_preexec() 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate closelog(); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * void setlog() 2730Sstevel@tonic-gate * Close file descriptors and redirect output. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate void 2760Sstevel@tonic-gate setlog(const char *logstem) 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate int fd; 2790Sstevel@tonic-gate char logfile[PATH_MAX]; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate closefrom(0); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate (void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, logstem); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate (void) umask(fmask); 2880Sstevel@tonic-gate fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 2890Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 2900Sstevel@tonic-gate (void) umask(dmask); 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate if (fd == -1) 2930Sstevel@tonic-gate return; 2940Sstevel@tonic-gate 2955617Sacruz (void) dup2(fd, STDOUT_FILENO); 2965617Sacruz (void) dup2(fd, STDERR_FILENO); 2970Sstevel@tonic-gate 2985617Sacruz if (fd != STDOUT_FILENO && fd != STDERR_FILENO) 2990Sstevel@tonic-gate startd_close(fd); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate static int 3030Sstevel@tonic-gate log_dir_writeable(const char *path) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate int fd; 3060Sstevel@tonic-gate struct statvfs svb; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if ((fd = open(path, O_RDONLY, 0644)) == -1) 3090Sstevel@tonic-gate return (-1); 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate if (fstatvfs(fd, &svb) == -1) 3120Sstevel@tonic-gate return (-1); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (svb.f_flag & ST_RDONLY) { 3150Sstevel@tonic-gate (void) close(fd); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate fd = -1; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate return (fd); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate static void 3240Sstevel@tonic-gate vlog_instance(const char *fmri, const char *logstem, boolean_t canlog, 3250Sstevel@tonic-gate const char *format, va_list args) 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate char logfile[PATH_MAX]; 3280Sstevel@tonic-gate char *message; 3290Sstevel@tonic-gate char omessage[1024]; 3300Sstevel@tonic-gate int fd, err; 3310Sstevel@tonic-gate char timebuf[LOG_DATE_SIZE]; 3320Sstevel@tonic-gate struct tm ltime; 3330Sstevel@tonic-gate struct timeval now; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate (void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, 3360Sstevel@tonic-gate logstem); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate (void) umask(fmask); 3390Sstevel@tonic-gate fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 3400Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 3410Sstevel@tonic-gate err = errno; 3420Sstevel@tonic-gate (void) umask(dmask); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate if (fd == -1) { 3450Sstevel@tonic-gate if (canlog) 3460Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: open(%s) " 3470Sstevel@tonic-gate "failed with %s.\n", fmri, logfile, strerror(err)); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate return; 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate (void) vsnprintf(omessage, sizeof (omessage), format, args); 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate if (gettimeofday(&now, NULL) != 0) 3550Sstevel@tonic-gate (void) fprintf(stderr, "gettimeofday(3C) failed: %s\n", 3560Sstevel@tonic-gate strerror(errno)); 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate if (st->st_log_timezone_known) 3590Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), "%b %e %T", 3600Sstevel@tonic-gate localtime_r(&now.tv_sec, <ime)); 3610Sstevel@tonic-gate else 3620Sstevel@tonic-gate xstrftime_poststart(timebuf, sizeof (timebuf), &now); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate message = uu_msprintf("[ %s %s ]\n", timebuf, omessage); 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (message == NULL) { 3670Sstevel@tonic-gate if (canlog) 3680Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: %s.\n", 3690Sstevel@tonic-gate fmri, uu_strerror(uu_error())); 3700Sstevel@tonic-gate } else { 3710Sstevel@tonic-gate if (write(fd, message, strlen(message)) < 0 && canlog) 3720Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: write(%d) " 3730Sstevel@tonic-gate "failed with %s.\n", fmri, fd, 3740Sstevel@tonic-gate strerror(errno)); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate uu_free(message); 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if (close(fd) != 0 && canlog) 3800Sstevel@tonic-gate log_framework(LOG_NOTICE, "close(%d) failed: %s.\n", fd, 3810Sstevel@tonic-gate strerror(errno)); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate /* 3850Sstevel@tonic-gate * void log_instance(const restarter_inst_t *, boolean_t, const char *, ...) 3860Sstevel@tonic-gate * 3870Sstevel@tonic-gate * The log_instance() format is "[ month day time message ]". (The 3880Sstevel@tonic-gate * brackets distinguish svc.startd messages from method output.) We avoid 3890Sstevel@tonic-gate * calling log_*() functions on error when canlog is not set, since we may 3900Sstevel@tonic-gate * be called from a child process. 3910Sstevel@tonic-gate * 3920Sstevel@tonic-gate * When adding new calls to this function, consider: If this is called before 3930Sstevel@tonic-gate * any instances have started, then it should be called with canlog clear, 3940Sstevel@tonic-gate * lest we spew errors to the console when booted on the miniroot. 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate /*PRINTFLIKE3*/ 3970Sstevel@tonic-gate void 3980Sstevel@tonic-gate log_instance(const restarter_inst_t *inst, boolean_t canlog, 3990Sstevel@tonic-gate const char *format, ...) 4000Sstevel@tonic-gate { 4010Sstevel@tonic-gate va_list args; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate va_start(args, format); 4040Sstevel@tonic-gate vlog_instance(inst->ri_i.i_fmri, inst->ri_logstem, canlog, format, 4050Sstevel@tonic-gate args); 4060Sstevel@tonic-gate va_end(args); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate /* 4100Sstevel@tonic-gate * void log_instance_fmri(const char *, const char *,boolean_t, const char *, 4110Sstevel@tonic-gate * ...) 4120Sstevel@tonic-gate * 4130Sstevel@tonic-gate * The log_instance_fmri() format is "[ month day time message ]". (The 4140Sstevel@tonic-gate * brackets distinguish svc.startd messages from method output.) We avoid 4150Sstevel@tonic-gate * calling log_*() functions on error when canlog is not set, since we may 4160Sstevel@tonic-gate * be called from a child process. 4170Sstevel@tonic-gate * 4180Sstevel@tonic-gate * For new calls to this function, see the warning in log_instance()'s 4190Sstevel@tonic-gate * comment. 4200Sstevel@tonic-gate */ 4210Sstevel@tonic-gate /*PRINTFLIKE4*/ 4220Sstevel@tonic-gate void 4230Sstevel@tonic-gate log_instance_fmri(const char *fmri, const char *logstem, boolean_t canlog, 4240Sstevel@tonic-gate const char *format, ...) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate va_list args; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate va_start(args, format); 4290Sstevel@tonic-gate vlog_instance(fmri, logstem, canlog, format, args); 4300Sstevel@tonic-gate va_end(args); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * void log_transition(const restarter_inst_t *, start_outcome_t) 4350Sstevel@tonic-gate * 4360Sstevel@tonic-gate * The log_transition() format is 4370Sstevel@tonic-gate * 4380Sstevel@tonic-gate * [ _service_fmri_ _participle_ (_common_name_) ] 4390Sstevel@tonic-gate * 4400Sstevel@tonic-gate * Again, brackets separate messages from specific service instance output to 4410Sstevel@tonic-gate * the console. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate void 4440Sstevel@tonic-gate log_transition(const restarter_inst_t *inst, start_outcome_t outcome) 4450Sstevel@tonic-gate { 4460Sstevel@tonic-gate char *message; 4470Sstevel@tonic-gate char omessage[1024]; 4480Sstevel@tonic-gate char *action; 4490Sstevel@tonic-gate int severity; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate if (outcome == START_REQUESTED) { 4520Sstevel@tonic-gate char *cname = NULL; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate cname = inst->ri_common_name; 4550Sstevel@tonic-gate if (cname == NULL) 4560Sstevel@tonic-gate cname = inst->ri_C_common_name; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (!(st->st_boot_flags & STARTD_BOOT_VERBOSE)) 4590Sstevel@tonic-gate return; 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate if (inst->ri_start_index > 1) 4620Sstevel@tonic-gate return; 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if (cname) 4650Sstevel@tonic-gate (void) snprintf(omessage, sizeof (omessage), " (%s)", 4665617Sacruz cname); 4670Sstevel@tonic-gate else 4680Sstevel@tonic-gate *omessage = '\0'; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate action = gettext("starting"); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate message = uu_msprintf("[ %s %s%s ]\n", 4730Sstevel@tonic-gate inst->ri_i.i_fmri + strlen("svc:/"), action, 4740Sstevel@tonic-gate omessage); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate severity = LOG_INFO; 4770Sstevel@tonic-gate } else { 4780Sstevel@tonic-gate switch (outcome) { 4791958Slianep case MAINT_REQUESTED: 4801958Slianep action = gettext("transitioned to maintenance by " 4811958Slianep "request (see 'svcs -xv' for details)"); 4821958Slianep break; 4830Sstevel@tonic-gate case START_FAILED_REPEATEDLY: 4841958Slianep action = gettext("failed repeatedly: transitioned to " 4851958Slianep "maintenance (see 'svcs -xv' for details)"); 4860Sstevel@tonic-gate break; 4870Sstevel@tonic-gate case START_FAILED_CONFIGURATION: 4881958Slianep action = gettext("misconfigured: transitioned to " 4891958Slianep "maintenance (see 'svcs -xv' for details)"); 4900Sstevel@tonic-gate break; 4910Sstevel@tonic-gate case START_FAILED_FATAL: 4921958Slianep action = gettext("failed fatally: transitioned to " 4931958Slianep "maintenance (see 'svcs -xv' for details)"); 4940Sstevel@tonic-gate break; 4950Sstevel@tonic-gate case START_FAILED_TIMEOUT_FATAL: 4961958Slianep action = gettext("timed out: transitioned to " 4971958Slianep "maintenance (see 'svcs -xv' for details)"); 4980Sstevel@tonic-gate break; 4990Sstevel@tonic-gate case START_FAILED_OTHER: 5001958Slianep action = gettext("failed: transitioned to " 5011958Slianep "maintenance (see 'svcs -xv' for details)"); 5020Sstevel@tonic-gate break; 5030Sstevel@tonic-gate case START_REQUESTED: 5040Sstevel@tonic-gate assert(outcome != START_REQUESTED); 5050Sstevel@tonic-gate /*FALLTHROUGH*/ 5060Sstevel@tonic-gate default: 5070Sstevel@tonic-gate action = gettext("outcome unknown?"); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5101958Slianep message = uu_msprintf("[ %s %s ]\n", 5111958Slianep inst->ri_i.i_fmri + strlen("svc:/"), action); 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate severity = LOG_ERR; 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate if (message == NULL) { 5180Sstevel@tonic-gate log_error(LOG_NOTICE, 5190Sstevel@tonic-gate "Could not log boot message for %s: %s.\n", 5200Sstevel@tonic-gate inst->ri_i.i_fmri, uu_strerror(uu_error())); 5210Sstevel@tonic-gate } else { 5221958Slianep /* 5231958Slianep * All significant errors should to go to syslog to 5241958Slianep * communicate appropriate information even for systems 5251958Slianep * without a console connected during boot. Send the 5261958Slianep * message to stderr only if the severity is lower than 5271958Slianep * (indicated by >) LOG_ERR. 5281958Slianep */ 5291958Slianep if (!st->st_log_login_reached && severity > LOG_ERR) { 5300Sstevel@tonic-gate /*LINTED*/ 5310Sstevel@tonic-gate if (fprintf(stderr, message) < 0) 5320Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: " 5330Sstevel@tonic-gate "fprintf() failed with %s.\n", 5340Sstevel@tonic-gate inst->ri_i.i_fmri, strerror(errno)); 5350Sstevel@tonic-gate } else { 5360Sstevel@tonic-gate log_framework(severity, "%s %s\n", 5370Sstevel@tonic-gate inst->ri_i.i_fmri + strlen("svc:/"), action); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate uu_free(message); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * log_console - log a message to the consoles and to syslog 5460Sstevel@tonic-gate * 5470Sstevel@tonic-gate * This logs a message as-is to the console (and auxiliary consoles), 5480Sstevel@tonic-gate * as well as to the master restarter log. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate /*PRINTFLIKE2*/ 5510Sstevel@tonic-gate void 5520Sstevel@tonic-gate log_console(int severity, const char *format, ...) 5530Sstevel@tonic-gate { 5540Sstevel@tonic-gate va_list args; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate va_start(args, format); 5570Sstevel@tonic-gate vlog_prefix(severity, "", format, args); 5580Sstevel@tonic-gate va_end(args); 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate va_start(args, format); 5610Sstevel@tonic-gate (void) vfprintf(stderr, format, args); 5620Sstevel@tonic-gate va_end(args); 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* 5660Sstevel@tonic-gate * void log_init() 5670Sstevel@tonic-gate * 5680Sstevel@tonic-gate * Set up the log files, if necessary, for the current invocation. This 5690Sstevel@tonic-gate * function should be called before any other functions in this file. Set the 5700Sstevel@tonic-gate * syslog(3C) logging mask such that severities of the importance of 5710Sstevel@tonic-gate * LOG_NOTICE and above are passed through, but lower severity messages are 5720Sstevel@tonic-gate * masked out. 5730Sstevel@tonic-gate * 5740Sstevel@tonic-gate * It may be called multiple times to change the logging configuration due to 5750Sstevel@tonic-gate * administrative request. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate void 5780Sstevel@tonic-gate log_init() 5790Sstevel@tonic-gate { 5800Sstevel@tonic-gate int dirfd, logfd; 5810Sstevel@tonic-gate char *dir; 5820Sstevel@tonic-gate struct stat sb; 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (st->st_start_time.tv_sec == 0) { 5850Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 5860Sstevel@tonic-gate st->st_start_time.tv_sec = time(NULL); 5870Sstevel@tonic-gate } else { 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * We need to special-case the BOOT_TIME utmp entry, and 5900Sstevel@tonic-gate * drag that value out of the kernel if it's there. 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate kstat_ctl_t *kc; 5930Sstevel@tonic-gate kstat_t *ks; 5940Sstevel@tonic-gate kstat_named_t *boot; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (((kc = kstat_open()) != 0) && 5970Sstevel@tonic-gate ((ks = kstat_lookup(kc, "unix", 0, "system_misc")) 5980Sstevel@tonic-gate != NULL) && 5990Sstevel@tonic-gate (kstat_read(kc, ks, NULL) != -1) && 6000Sstevel@tonic-gate ((boot = kstat_data_lookup(ks, "boot_time")) != 6010Sstevel@tonic-gate NULL)) { 6020Sstevel@tonic-gate /* 6030Sstevel@tonic-gate * If we're here, then we've successfully found 6040Sstevel@tonic-gate * the boot_time kstat... use its value. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate st->st_start_time.tv_sec = boot->value.ul; 6070Sstevel@tonic-gate } else { 6080Sstevel@tonic-gate st->st_start_time.tv_sec = time(NULL); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (kc) 6120Sstevel@tonic-gate (void) kstat_close(kc); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Establish our timezone if the appropriate directory is available. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate if (!st->st_log_timezone_known && stat(FS_TIMEZONE_DIR, &sb) == 0) { 6200Sstevel@tonic-gate tzset(); 6210Sstevel@tonic-gate st->st_log_timezone_known = 1; 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Establish our locale if the appropriate directory is available. Set 6260Sstevel@tonic-gate * the locale string from the environment so we can extract template 6270Sstevel@tonic-gate * information correctly, if the locale directories aren't yet 6280Sstevel@tonic-gate * available. 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate if (st->st_locale != NULL) 6310Sstevel@tonic-gate free(st->st_locale); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate if ((st->st_locale = getenv("LC_ALL")) == NULL) 6340Sstevel@tonic-gate if ((st->st_locale = getenv("LC_MESSAGES")) == NULL) 6350Sstevel@tonic-gate st->st_locale = getenv("LANG"); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate if (!st->st_log_locale_known && stat(FS_LOCALE_DIR, &sb) == 0) { 6380Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 6390Sstevel@tonic-gate st->st_locale = setlocale(LC_MESSAGES, ""); 6400Sstevel@tonic-gate if (st->st_locale) 6410Sstevel@tonic-gate st->st_log_locale_known = 1; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate if (st->st_locale) { 6470Sstevel@tonic-gate st->st_locale = safe_strdup(st->st_locale); 6480Sstevel@tonic-gate xstr_sanitize(st->st_locale); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate if (logfile) { 6520Sstevel@tonic-gate (void) fclose(logfile); 6530Sstevel@tonic-gate logfile = NULL; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate /* 6570Sstevel@tonic-gate * Set syslog(3C) behaviour in all cases. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate closelog(); 6600Sstevel@tonic-gate openlog("svc.startd", LOG_PID | LOG_CONS, LOG_DAEMON); 6610Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE)); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate if ((dirfd = log_dir_writeable(LOG_PREFIX_NORMAL)) == -1) { 6640Sstevel@tonic-gate if ((dirfd = log_dir_writeable(LOG_PREFIX_EARLY)) == -1) 6650Sstevel@tonic-gate return; 6660Sstevel@tonic-gate else 6670Sstevel@tonic-gate dir = LOG_PREFIX_EARLY; 6680Sstevel@tonic-gate } else { 6690Sstevel@tonic-gate dir = LOG_PREFIX_NORMAL; 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate st->st_log_prefix = dir; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate (void) umask(fmask); 6750Sstevel@tonic-gate if ((logfd = openat(dirfd, STARTD_DEFAULT_LOG, O_CREAT | O_RDWR, 6760Sstevel@tonic-gate 0644)) == -1) { 6770Sstevel@tonic-gate (void) close(dirfd); 6780Sstevel@tonic-gate (void) umask(dmask); 6790Sstevel@tonic-gate return; 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate (void) close(dirfd); 6830Sstevel@tonic-gate (void) umask(dmask); 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate if ((logfile = fdopen(logfd, "a")) == NULL) 6860Sstevel@tonic-gate if (errno != EROFS) 6870Sstevel@tonic-gate log_error(LOG_WARNING, "can't open logfile %s/%s", 6880Sstevel@tonic-gate dir, STARTD_DEFAULT_LOG); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (logfile && 6910Sstevel@tonic-gate fcntl(fileno(logfile), F_SETFD, FD_CLOEXEC) == -1) 6920Sstevel@tonic-gate log_error(LOG_WARNING, 6930Sstevel@tonic-gate "couldn't mark logfile close-on-exec: %s\n", 6940Sstevel@tonic-gate strerror(errno)); 6950Sstevel@tonic-gate } 696