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 2003 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 #include <sys/param.h> 30*0Sstevel@tonic-gate #include <libintl.h> 31*0Sstevel@tonic-gate #include <stdarg.h> 32*0Sstevel@tonic-gate #include <stdio.h> 33*0Sstevel@tonic-gate #include <stdlib.h> 34*0Sstevel@tonic-gate #include <strings.h> 35*0Sstevel@tonic-gate #include <syslog.h> 36*0Sstevel@tonic-gate #include <unistd.h> 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include "utils.h" 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate static char ERRNO_FMT[] = ": %s"; 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate static char *pname = NULL; 44*0Sstevel@tonic-gate static rcm_level_t message_priority = RCM_WARN; 45*0Sstevel@tonic-gate static rcm_dst_t message_dst = RCD_STD; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate static void dmesg(int level, char *msg); 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /*PRINTFLIKE2*/ 50*0Sstevel@tonic-gate void 51*0Sstevel@tonic-gate dprintfe(int level, char *format, ...) 52*0Sstevel@tonic-gate { 53*0Sstevel@tonic-gate va_list alist; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate va_start(alist, format); 56*0Sstevel@tonic-gate vdprintfe(level, format, alist); 57*0Sstevel@tonic-gate va_end(alist); 58*0Sstevel@tonic-gate } 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate /*PRINTFLIKE2*/ 61*0Sstevel@tonic-gate void 62*0Sstevel@tonic-gate vdprintfe(int level, char *format, va_list alist) 63*0Sstevel@tonic-gate { 64*0Sstevel@tonic-gate char buf[LINELEN]; 65*0Sstevel@tonic-gate char *c; 66*0Sstevel@tonic-gate int err = errno; 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate *buf = 0; 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate if ((strlen(buf) + 1) < LINELEN) 71*0Sstevel@tonic-gate (void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf), 72*0Sstevel@tonic-gate format, alist); 73*0Sstevel@tonic-gate if ((c = strchr(buf, '\n')) == NULL) { 74*0Sstevel@tonic-gate if ((strlen(buf) + 1) < LINELEN) 75*0Sstevel@tonic-gate (void) snprintf(buf + strlen(buf), LINELEN - 1 - 76*0Sstevel@tonic-gate strlen(buf), gettext(ERRNO_FMT), strerror(err)); 77*0Sstevel@tonic-gate } else 78*0Sstevel@tonic-gate *c = 0; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate dmesg(level, buf); 81*0Sstevel@tonic-gate } 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate #ifdef DEBUG_MSG 84*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 85*0Sstevel@tonic-gate void 86*0Sstevel@tonic-gate debug(char *format, ...) 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate va_list alist; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate if (get_message_priority() < RCM_DEBUG) 91*0Sstevel@tonic-gate return; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate va_start(alist, format); 94*0Sstevel@tonic-gate vdprintfe(RCM_DEBUG, format, alist); 95*0Sstevel@tonic-gate va_end(alist); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 99*0Sstevel@tonic-gate void 100*0Sstevel@tonic-gate debug_high(char *format, ...) 101*0Sstevel@tonic-gate { 102*0Sstevel@tonic-gate va_list alist; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate if (get_message_priority() < RCM_DEBUG_HIGH) 105*0Sstevel@tonic-gate return; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate va_start(alist, format); 108*0Sstevel@tonic-gate vdprintfe(RCM_DEBUG_HIGH, format, alist); 109*0Sstevel@tonic-gate va_end(alist); 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate #endif /* DEBUG_MSG */ 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 114*0Sstevel@tonic-gate void 115*0Sstevel@tonic-gate warn(char *format, ...) 116*0Sstevel@tonic-gate { 117*0Sstevel@tonic-gate va_list alist; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate if (get_message_priority() < RCM_WARN) 120*0Sstevel@tonic-gate return; 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate va_start(alist, format); 123*0Sstevel@tonic-gate vdprintfe(RCM_WARN, format, alist); 124*0Sstevel@tonic-gate va_end(alist); 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 128*0Sstevel@tonic-gate void 129*0Sstevel@tonic-gate die(char *format, ...) 130*0Sstevel@tonic-gate { 131*0Sstevel@tonic-gate va_list alist; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate if (get_message_priority() < RCM_ERR) 134*0Sstevel@tonic-gate return; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate va_start(alist, format); 137*0Sstevel@tonic-gate vdprintfe(RCM_ERR, format, alist); 138*0Sstevel@tonic-gate va_end(alist); 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate exit(E_ERROR); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 144*0Sstevel@tonic-gate void 145*0Sstevel@tonic-gate info(char *format, ...) 146*0Sstevel@tonic-gate { 147*0Sstevel@tonic-gate va_list alist; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if (get_message_priority() < RCM_INFO) 150*0Sstevel@tonic-gate return; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate va_start(alist, format); 153*0Sstevel@tonic-gate vdprintfe(RCM_INFO, format, alist); 154*0Sstevel@tonic-gate va_end(alist); 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate char * 158*0Sstevel@tonic-gate setprogname(char *arg0) 159*0Sstevel@tonic-gate { 160*0Sstevel@tonic-gate char *p = strrchr(arg0, '/'); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate if (p == NULL) 163*0Sstevel@tonic-gate p = arg0; 164*0Sstevel@tonic-gate else 165*0Sstevel@tonic-gate p++; 166*0Sstevel@tonic-gate pname = p; 167*0Sstevel@tonic-gate return (pname); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* 171*0Sstevel@tonic-gate * Output a message to the controlling tty or log, depending on which is 172*0Sstevel@tonic-gate * configured. The message should contain no newlines. 173*0Sstevel@tonic-gate */ 174*0Sstevel@tonic-gate static void 175*0Sstevel@tonic-gate dmesg(int level, char *msg) 176*0Sstevel@tonic-gate { 177*0Sstevel@tonic-gate if (message_priority >= level) { 178*0Sstevel@tonic-gate FILE *fp; 179*0Sstevel@tonic-gate int syslog_severity = -1; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate switch (message_dst) { 182*0Sstevel@tonic-gate case RCD_STD: 183*0Sstevel@tonic-gate fp = level >= RCM_DEBUG ? stderr : stdout; 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate if (pname != NULL) { 186*0Sstevel@tonic-gate (void) fputs(pname, fp); 187*0Sstevel@tonic-gate (void) fputs(": ", fp); 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate (void) fputs(msg, fp); 190*0Sstevel@tonic-gate (void) fputc('\n', fp); 191*0Sstevel@tonic-gate (void) fflush(fp); 192*0Sstevel@tonic-gate break; 193*0Sstevel@tonic-gate case RCD_SYSLOG: 194*0Sstevel@tonic-gate switch (level) { 195*0Sstevel@tonic-gate case RCM_ERR: 196*0Sstevel@tonic-gate syslog_severity = LOG_ERR; 197*0Sstevel@tonic-gate break; 198*0Sstevel@tonic-gate case RCM_WARN: 199*0Sstevel@tonic-gate syslog_severity = LOG_WARNING; 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate case RCM_INFO: 202*0Sstevel@tonic-gate syslog_severity = LOG_INFO; 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate case RCM_DEBUG: 205*0Sstevel@tonic-gate syslog_severity = LOG_DEBUG; 206*0Sstevel@tonic-gate break; 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate if (syslog_severity >= 0) 209*0Sstevel@tonic-gate (void) syslog(syslog_severity, "%s", msg); 210*0Sstevel@tonic-gate break; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate rcm_level_t 216*0Sstevel@tonic-gate get_message_priority(void) 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate return (message_priority); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate rcm_level_t 222*0Sstevel@tonic-gate set_message_priority(rcm_level_t new_priority) 223*0Sstevel@tonic-gate { 224*0Sstevel@tonic-gate rcm_level_t old_priority = message_priority; 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate message_priority = new_priority; 227*0Sstevel@tonic-gate return (old_priority); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate rcm_dst_t 231*0Sstevel@tonic-gate set_message_destination(rcm_dst_t new_dst) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate rcm_dst_t old_dst = message_dst; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate if ((message_dst = new_dst) == RCD_SYSLOG) 236*0Sstevel@tonic-gate openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON); 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate return (old_dst); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate void 242*0Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp) 243*0Sstevel@tonic-gate { 244*0Sstevel@tonic-gate tsp->tv_sec = hrt / NANOSEC; 245*0Sstevel@tonic-gate tsp->tv_nsec = hrt % NANOSEC; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate int 249*0Sstevel@tonic-gate xatoi(char *p) 250*0Sstevel@tonic-gate { 251*0Sstevel@tonic-gate int i; 252*0Sstevel@tonic-gate char *q; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate errno = 0; 255*0Sstevel@tonic-gate i = (int)strtol(p, &q, 10); 256*0Sstevel@tonic-gate if (errno != 0 || q == p || i < 0 || *q != '\0') { 257*0Sstevel@tonic-gate warn(gettext("illegal argument -- %s\n"), p); 258*0Sstevel@tonic-gate return (-1); 259*0Sstevel@tonic-gate } else { 260*0Sstevel@tonic-gate return (i); 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate } 263