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 /* 22*5617Sacruz * 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 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 static char logbuf[LOGBUF_SZ] = ""; 1370Sstevel@tonic-gate static pthread_mutex_t logbuf_mutex = PTHREAD_MUTEX_INITIALIZER; 1380Sstevel@tonic-gate #endif 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate static void 1410Sstevel@tonic-gate xstrftime_poststart(char *buf, size_t bufsize, struct timeval *time) 1420Sstevel@tonic-gate { 1430Sstevel@tonic-gate long sec, usec; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate sec = time->tv_sec - st->st_start_time.tv_sec; 1460Sstevel@tonic-gate usec = time->tv_usec - st->st_start_time.tv_usec; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (usec < 0) { 1490Sstevel@tonic-gate sec -= 1; 1500Sstevel@tonic-gate usec += 1000000; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate (void) snprintf(buf, bufsize, "start + %d.%02ds", sec, usec / 10000); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate static void 1570Sstevel@tonic-gate vlog_prefix(int severity, const char *prefix, const char *format, va_list args) 1580Sstevel@tonic-gate { 1590Sstevel@tonic-gate char buf[512], *cp; 1600Sstevel@tonic-gate char timebuf[LOG_DATE_SIZE]; 1610Sstevel@tonic-gate struct timeval now; 1620Sstevel@tonic-gate struct tm ltime; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #ifdef NDEBUG 1650Sstevel@tonic-gate if (severity > st->st_log_level_min) 1660Sstevel@tonic-gate return; 1670Sstevel@tonic-gate #endif 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (gettimeofday(&now, NULL) != 0) 1700Sstevel@tonic-gate (void) fprintf(stderr, "gettimeofday(3C) failed: %s\n", 1710Sstevel@tonic-gate strerror(errno)); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (st->st_log_timezone_known) 1740Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), "%b %e %T", 1750Sstevel@tonic-gate localtime_r(&now.tv_sec, <ime)); 1760Sstevel@tonic-gate else 1770Sstevel@tonic-gate xstrftime_poststart(timebuf, sizeof (timebuf), &now); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s/%d%s: ", timebuf, pthread_self(), 1800Sstevel@tonic-gate prefix); 1810Sstevel@tonic-gate cp = strchr(buf, '\0'); 1820Sstevel@tonic-gate (void) vsnprintf(cp, sizeof (buf) - (cp - buf), format, args); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate #ifndef NDEBUG 1850Sstevel@tonic-gate /* Copy into logbuf. */ 1860Sstevel@tonic-gate (void) pthread_mutex_lock(&logbuf_mutex); 1870Sstevel@tonic-gate if (strlen(logbuf) + strlen(buf) + 1 <= sizeof (logbuf)) 1880Sstevel@tonic-gate (void) strcat(logbuf, buf); 1890Sstevel@tonic-gate else 1900Sstevel@tonic-gate (void) strlcpy(logbuf, buf, sizeof (logbuf)); 1910Sstevel@tonic-gate (void) pthread_mutex_unlock(&logbuf_mutex); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (severity > st->st_log_level_min) 1940Sstevel@tonic-gate return; 1950Sstevel@tonic-gate #endif 1960Sstevel@tonic-gate 1971958Slianep if (st->st_log_flags & STARTD_LOG_FILE && logfile) { 1980Sstevel@tonic-gate (void) fputs(buf, logfile); 1991958Slianep (void) fflush(logfile); 2001958Slianep } 2010Sstevel@tonic-gate if (st->st_log_flags & STARTD_LOG_TERMINAL) 2020Sstevel@tonic-gate (void) fputs(buf, stdout); 2031958Slianep if (st->st_log_flags & STARTD_LOG_SYSLOG && st->st_log_timezone_known) 2040Sstevel@tonic-gate vsyslog(severity, format, args); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /*PRINTFLIKE2*/ 2080Sstevel@tonic-gate void 2090Sstevel@tonic-gate log_error(int severity, const char *format, ...) 2100Sstevel@tonic-gate { 2110Sstevel@tonic-gate va_list args; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate va_start(args, format); 2140Sstevel@tonic-gate vlog_prefix(severity, " ERROR", format, args); 2150Sstevel@tonic-gate va_end(args); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /*PRINTFLIKE2*/ 2190Sstevel@tonic-gate void 2200Sstevel@tonic-gate log_framework(int severity, const char *format, ...) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate va_list args; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate va_start(args, format); 2250Sstevel@tonic-gate vlog_prefix(severity, "", format, args); 2260Sstevel@tonic-gate va_end(args); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * void log_preexec() 2310Sstevel@tonic-gate * 2320Sstevel@tonic-gate * log_preexec() should be invoked prior to any exec(2) calls, to prevent the 2330Sstevel@tonic-gate * logfile and syslogd file descriptors from being leaked to child processes. 2340Sstevel@tonic-gate * Why openlog(3C) lacks a close-on-exec option is a minor mystery. 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate void 2370Sstevel@tonic-gate log_preexec() 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate closelog(); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * void setlog() 2440Sstevel@tonic-gate * Close file descriptors and redirect output. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate void 2470Sstevel@tonic-gate setlog(const char *logstem) 2480Sstevel@tonic-gate { 2490Sstevel@tonic-gate int fd; 2500Sstevel@tonic-gate char logfile[PATH_MAX]; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate closefrom(0); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate (void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, logstem); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate (void) umask(fmask); 2590Sstevel@tonic-gate fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 2600Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 2610Sstevel@tonic-gate (void) umask(dmask); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate if (fd == -1) 2640Sstevel@tonic-gate return; 2650Sstevel@tonic-gate 266*5617Sacruz (void) dup2(fd, STDOUT_FILENO); 267*5617Sacruz (void) dup2(fd, STDERR_FILENO); 2680Sstevel@tonic-gate 269*5617Sacruz if (fd != STDOUT_FILENO && fd != STDERR_FILENO) 2700Sstevel@tonic-gate startd_close(fd); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate static int 2740Sstevel@tonic-gate log_dir_writeable(const char *path) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate int fd; 2770Sstevel@tonic-gate struct statvfs svb; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate if ((fd = open(path, O_RDONLY, 0644)) == -1) 2800Sstevel@tonic-gate return (-1); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (fstatvfs(fd, &svb) == -1) 2830Sstevel@tonic-gate return (-1); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate if (svb.f_flag & ST_RDONLY) { 2860Sstevel@tonic-gate (void) close(fd); 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate fd = -1; 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate return (fd); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate static void 2950Sstevel@tonic-gate vlog_instance(const char *fmri, const char *logstem, boolean_t canlog, 2960Sstevel@tonic-gate const char *format, va_list args) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate char logfile[PATH_MAX]; 2990Sstevel@tonic-gate char *message; 3000Sstevel@tonic-gate char omessage[1024]; 3010Sstevel@tonic-gate int fd, err; 3020Sstevel@tonic-gate char timebuf[LOG_DATE_SIZE]; 3030Sstevel@tonic-gate struct tm ltime; 3040Sstevel@tonic-gate struct timeval now; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate (void) snprintf(logfile, PATH_MAX, "%s/%s", st->st_log_prefix, 3070Sstevel@tonic-gate logstem); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate (void) umask(fmask); 3100Sstevel@tonic-gate fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 3110Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 3120Sstevel@tonic-gate err = errno; 3130Sstevel@tonic-gate (void) umask(dmask); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate if (fd == -1) { 3160Sstevel@tonic-gate if (canlog) 3170Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: open(%s) " 3180Sstevel@tonic-gate "failed with %s.\n", fmri, logfile, strerror(err)); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate return; 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate (void) vsnprintf(omessage, sizeof (omessage), format, args); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (gettimeofday(&now, NULL) != 0) 3260Sstevel@tonic-gate (void) fprintf(stderr, "gettimeofday(3C) failed: %s\n", 3270Sstevel@tonic-gate strerror(errno)); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate if (st->st_log_timezone_known) 3300Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), "%b %e %T", 3310Sstevel@tonic-gate localtime_r(&now.tv_sec, <ime)); 3320Sstevel@tonic-gate else 3330Sstevel@tonic-gate xstrftime_poststart(timebuf, sizeof (timebuf), &now); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate message = uu_msprintf("[ %s %s ]\n", timebuf, omessage); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (message == NULL) { 3380Sstevel@tonic-gate if (canlog) 3390Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: %s.\n", 3400Sstevel@tonic-gate fmri, uu_strerror(uu_error())); 3410Sstevel@tonic-gate } else { 3420Sstevel@tonic-gate if (write(fd, message, strlen(message)) < 0 && canlog) 3430Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: write(%d) " 3440Sstevel@tonic-gate "failed with %s.\n", fmri, fd, 3450Sstevel@tonic-gate strerror(errno)); 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate uu_free(message); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (close(fd) != 0 && canlog) 3510Sstevel@tonic-gate log_framework(LOG_NOTICE, "close(%d) failed: %s.\n", fd, 3520Sstevel@tonic-gate strerror(errno)); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * void log_instance(const restarter_inst_t *, boolean_t, const char *, ...) 3570Sstevel@tonic-gate * 3580Sstevel@tonic-gate * The log_instance() format is "[ month day time message ]". (The 3590Sstevel@tonic-gate * brackets distinguish svc.startd messages from method output.) We avoid 3600Sstevel@tonic-gate * calling log_*() functions on error when canlog is not set, since we may 3610Sstevel@tonic-gate * be called from a child process. 3620Sstevel@tonic-gate * 3630Sstevel@tonic-gate * When adding new calls to this function, consider: If this is called before 3640Sstevel@tonic-gate * any instances have started, then it should be called with canlog clear, 3650Sstevel@tonic-gate * lest we spew errors to the console when booted on the miniroot. 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate /*PRINTFLIKE3*/ 3680Sstevel@tonic-gate void 3690Sstevel@tonic-gate log_instance(const restarter_inst_t *inst, boolean_t canlog, 3700Sstevel@tonic-gate const char *format, ...) 3710Sstevel@tonic-gate { 3720Sstevel@tonic-gate va_list args; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate va_start(args, format); 3750Sstevel@tonic-gate vlog_instance(inst->ri_i.i_fmri, inst->ri_logstem, canlog, format, 3760Sstevel@tonic-gate args); 3770Sstevel@tonic-gate va_end(args); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * void log_instance_fmri(const char *, const char *,boolean_t, const char *, 3820Sstevel@tonic-gate * ...) 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * The log_instance_fmri() format is "[ month day time message ]". (The 3850Sstevel@tonic-gate * brackets distinguish svc.startd messages from method output.) We avoid 3860Sstevel@tonic-gate * calling log_*() functions on error when canlog is not set, since we may 3870Sstevel@tonic-gate * be called from a child process. 3880Sstevel@tonic-gate * 3890Sstevel@tonic-gate * For new calls to this function, see the warning in log_instance()'s 3900Sstevel@tonic-gate * comment. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate /*PRINTFLIKE4*/ 3930Sstevel@tonic-gate void 3940Sstevel@tonic-gate log_instance_fmri(const char *fmri, const char *logstem, boolean_t canlog, 3950Sstevel@tonic-gate const char *format, ...) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate va_list args; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate va_start(args, format); 4000Sstevel@tonic-gate vlog_instance(fmri, logstem, canlog, format, args); 4010Sstevel@tonic-gate va_end(args); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * void log_transition(const restarter_inst_t *, start_outcome_t) 4060Sstevel@tonic-gate * 4070Sstevel@tonic-gate * The log_transition() format is 4080Sstevel@tonic-gate * 4090Sstevel@tonic-gate * [ _service_fmri_ _participle_ (_common_name_) ] 4100Sstevel@tonic-gate * 4110Sstevel@tonic-gate * Again, brackets separate messages from specific service instance output to 4120Sstevel@tonic-gate * the console. 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate void 4150Sstevel@tonic-gate log_transition(const restarter_inst_t *inst, start_outcome_t outcome) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate char *message; 4180Sstevel@tonic-gate char omessage[1024]; 4190Sstevel@tonic-gate char *action; 4200Sstevel@tonic-gate int severity; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate if (outcome == START_REQUESTED) { 4230Sstevel@tonic-gate char *cname = NULL; 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate cname = inst->ri_common_name; 4260Sstevel@tonic-gate if (cname == NULL) 4270Sstevel@tonic-gate cname = inst->ri_C_common_name; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate if (!(st->st_boot_flags & STARTD_BOOT_VERBOSE)) 4300Sstevel@tonic-gate return; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (inst->ri_start_index > 1) 4330Sstevel@tonic-gate return; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if (cname) 4360Sstevel@tonic-gate (void) snprintf(omessage, sizeof (omessage), " (%s)", 437*5617Sacruz cname); 4380Sstevel@tonic-gate else 4390Sstevel@tonic-gate *omessage = '\0'; 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate action = gettext("starting"); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate message = uu_msprintf("[ %s %s%s ]\n", 4440Sstevel@tonic-gate inst->ri_i.i_fmri + strlen("svc:/"), action, 4450Sstevel@tonic-gate omessage); 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate severity = LOG_INFO; 4480Sstevel@tonic-gate } else { 4490Sstevel@tonic-gate switch (outcome) { 4501958Slianep case MAINT_REQUESTED: 4511958Slianep action = gettext("transitioned to maintenance by " 4521958Slianep "request (see 'svcs -xv' for details)"); 4531958Slianep break; 4540Sstevel@tonic-gate case START_FAILED_REPEATEDLY: 4551958Slianep action = gettext("failed repeatedly: transitioned to " 4561958Slianep "maintenance (see 'svcs -xv' for details)"); 4570Sstevel@tonic-gate break; 4580Sstevel@tonic-gate case START_FAILED_CONFIGURATION: 4591958Slianep action = gettext("misconfigured: transitioned to " 4601958Slianep "maintenance (see 'svcs -xv' for details)"); 4610Sstevel@tonic-gate break; 4620Sstevel@tonic-gate case START_FAILED_FATAL: 4631958Slianep action = gettext("failed fatally: transitioned to " 4641958Slianep "maintenance (see 'svcs -xv' for details)"); 4650Sstevel@tonic-gate break; 4660Sstevel@tonic-gate case START_FAILED_TIMEOUT_FATAL: 4671958Slianep action = gettext("timed out: transitioned to " 4681958Slianep "maintenance (see 'svcs -xv' for details)"); 4690Sstevel@tonic-gate break; 4700Sstevel@tonic-gate case START_FAILED_OTHER: 4711958Slianep action = gettext("failed: transitioned to " 4721958Slianep "maintenance (see 'svcs -xv' for details)"); 4730Sstevel@tonic-gate break; 4740Sstevel@tonic-gate case START_REQUESTED: 4750Sstevel@tonic-gate assert(outcome != START_REQUESTED); 4760Sstevel@tonic-gate /*FALLTHROUGH*/ 4770Sstevel@tonic-gate default: 4780Sstevel@tonic-gate action = gettext("outcome unknown?"); 4790Sstevel@tonic-gate } 4800Sstevel@tonic-gate 4811958Slianep message = uu_msprintf("[ %s %s ]\n", 4821958Slianep inst->ri_i.i_fmri + strlen("svc:/"), action); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate severity = LOG_ERR; 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (message == NULL) { 4890Sstevel@tonic-gate log_error(LOG_NOTICE, 4900Sstevel@tonic-gate "Could not log boot message for %s: %s.\n", 4910Sstevel@tonic-gate inst->ri_i.i_fmri, uu_strerror(uu_error())); 4920Sstevel@tonic-gate } else { 4931958Slianep /* 4941958Slianep * All significant errors should to go to syslog to 4951958Slianep * communicate appropriate information even for systems 4961958Slianep * without a console connected during boot. Send the 4971958Slianep * message to stderr only if the severity is lower than 4981958Slianep * (indicated by >) LOG_ERR. 4991958Slianep */ 5001958Slianep if (!st->st_log_login_reached && severity > LOG_ERR) { 5010Sstevel@tonic-gate /*LINTED*/ 5020Sstevel@tonic-gate if (fprintf(stderr, message) < 0) 5030Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not log for %s: " 5040Sstevel@tonic-gate "fprintf() failed with %s.\n", 5050Sstevel@tonic-gate inst->ri_i.i_fmri, strerror(errno)); 5060Sstevel@tonic-gate } else { 5070Sstevel@tonic-gate log_framework(severity, "%s %s\n", 5080Sstevel@tonic-gate inst->ri_i.i_fmri + strlen("svc:/"), action); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate uu_free(message); 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* 5160Sstevel@tonic-gate * log_console - log a message to the consoles and to syslog 5170Sstevel@tonic-gate * 5180Sstevel@tonic-gate * This logs a message as-is to the console (and auxiliary consoles), 5190Sstevel@tonic-gate * as well as to the master restarter log. 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate /*PRINTFLIKE2*/ 5220Sstevel@tonic-gate void 5230Sstevel@tonic-gate log_console(int severity, const char *format, ...) 5240Sstevel@tonic-gate { 5250Sstevel@tonic-gate va_list args; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate va_start(args, format); 5280Sstevel@tonic-gate vlog_prefix(severity, "", format, args); 5290Sstevel@tonic-gate va_end(args); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate va_start(args, format); 5320Sstevel@tonic-gate (void) vfprintf(stderr, format, args); 5330Sstevel@tonic-gate va_end(args); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * void log_init() 5380Sstevel@tonic-gate * 5390Sstevel@tonic-gate * Set up the log files, if necessary, for the current invocation. This 5400Sstevel@tonic-gate * function should be called before any other functions in this file. Set the 5410Sstevel@tonic-gate * syslog(3C) logging mask such that severities of the importance of 5420Sstevel@tonic-gate * LOG_NOTICE and above are passed through, but lower severity messages are 5430Sstevel@tonic-gate * masked out. 5440Sstevel@tonic-gate * 5450Sstevel@tonic-gate * It may be called multiple times to change the logging configuration due to 5460Sstevel@tonic-gate * administrative request. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate void 5490Sstevel@tonic-gate log_init() 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate int dirfd, logfd; 5520Sstevel@tonic-gate char *dir; 5530Sstevel@tonic-gate struct stat sb; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (st->st_start_time.tv_sec == 0) { 5560Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 5570Sstevel@tonic-gate st->st_start_time.tv_sec = time(NULL); 5580Sstevel@tonic-gate } else { 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * We need to special-case the BOOT_TIME utmp entry, and 5610Sstevel@tonic-gate * drag that value out of the kernel if it's there. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate kstat_ctl_t *kc; 5640Sstevel@tonic-gate kstat_t *ks; 5650Sstevel@tonic-gate kstat_named_t *boot; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate if (((kc = kstat_open()) != 0) && 5680Sstevel@tonic-gate ((ks = kstat_lookup(kc, "unix", 0, "system_misc")) 5690Sstevel@tonic-gate != NULL) && 5700Sstevel@tonic-gate (kstat_read(kc, ks, NULL) != -1) && 5710Sstevel@tonic-gate ((boot = kstat_data_lookup(ks, "boot_time")) != 5720Sstevel@tonic-gate NULL)) { 5730Sstevel@tonic-gate /* 5740Sstevel@tonic-gate * If we're here, then we've successfully found 5750Sstevel@tonic-gate * the boot_time kstat... use its value. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate st->st_start_time.tv_sec = boot->value.ul; 5780Sstevel@tonic-gate } else { 5790Sstevel@tonic-gate st->st_start_time.tv_sec = time(NULL); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (kc) 5830Sstevel@tonic-gate (void) kstat_close(kc); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate /* 5880Sstevel@tonic-gate * Establish our timezone if the appropriate directory is available. 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate if (!st->st_log_timezone_known && stat(FS_TIMEZONE_DIR, &sb) == 0) { 5910Sstevel@tonic-gate tzset(); 5920Sstevel@tonic-gate st->st_log_timezone_known = 1; 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate /* 5960Sstevel@tonic-gate * Establish our locale if the appropriate directory is available. Set 5970Sstevel@tonic-gate * the locale string from the environment so we can extract template 5980Sstevel@tonic-gate * information correctly, if the locale directories aren't yet 5990Sstevel@tonic-gate * available. 6000Sstevel@tonic-gate */ 6010Sstevel@tonic-gate if (st->st_locale != NULL) 6020Sstevel@tonic-gate free(st->st_locale); 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if ((st->st_locale = getenv("LC_ALL")) == NULL) 6050Sstevel@tonic-gate if ((st->st_locale = getenv("LC_MESSAGES")) == NULL) 6060Sstevel@tonic-gate st->st_locale = getenv("LANG"); 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate if (!st->st_log_locale_known && stat(FS_LOCALE_DIR, &sb) == 0) { 6090Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 6100Sstevel@tonic-gate st->st_locale = setlocale(LC_MESSAGES, ""); 6110Sstevel@tonic-gate if (st->st_locale) 6120Sstevel@tonic-gate st->st_log_locale_known = 1; 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate if (st->st_locale) { 6180Sstevel@tonic-gate st->st_locale = safe_strdup(st->st_locale); 6190Sstevel@tonic-gate xstr_sanitize(st->st_locale); 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (logfile) { 6230Sstevel@tonic-gate (void) fclose(logfile); 6240Sstevel@tonic-gate logfile = NULL; 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * Set syslog(3C) behaviour in all cases. 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate closelog(); 6310Sstevel@tonic-gate openlog("svc.startd", LOG_PID | LOG_CONS, LOG_DAEMON); 6320Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE)); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if ((dirfd = log_dir_writeable(LOG_PREFIX_NORMAL)) == -1) { 6350Sstevel@tonic-gate if ((dirfd = log_dir_writeable(LOG_PREFIX_EARLY)) == -1) 6360Sstevel@tonic-gate return; 6370Sstevel@tonic-gate else 6380Sstevel@tonic-gate dir = LOG_PREFIX_EARLY; 6390Sstevel@tonic-gate } else { 6400Sstevel@tonic-gate dir = LOG_PREFIX_NORMAL; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate st->st_log_prefix = dir; 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate (void) umask(fmask); 6460Sstevel@tonic-gate if ((logfd = openat(dirfd, STARTD_DEFAULT_LOG, O_CREAT | O_RDWR, 6470Sstevel@tonic-gate 0644)) == -1) { 6480Sstevel@tonic-gate (void) close(dirfd); 6490Sstevel@tonic-gate (void) umask(dmask); 6500Sstevel@tonic-gate return; 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate (void) close(dirfd); 6540Sstevel@tonic-gate (void) umask(dmask); 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if ((logfile = fdopen(logfd, "a")) == NULL) 6570Sstevel@tonic-gate if (errno != EROFS) 6580Sstevel@tonic-gate log_error(LOG_WARNING, "can't open logfile %s/%s", 6590Sstevel@tonic-gate dir, STARTD_DEFAULT_LOG); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if (logfile && 6620Sstevel@tonic-gate fcntl(fileno(logfile), F_SETFD, FD_CLOEXEC) == -1) 6630Sstevel@tonic-gate log_error(LOG_WARNING, 6640Sstevel@tonic-gate "couldn't mark logfile close-on-exec: %s\n", 6650Sstevel@tonic-gate strerror(errno)); 6660Sstevel@tonic-gate } 667