xref: /onnv-gate/usr/src/cmd/rcap/common/utils.c (revision 4891)
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
53247Sgjelinek  * Common Development and Distribution License (the "License").
63247Sgjelinek  * 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*4891Svk199839  * 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 #include <sys/param.h>
290Sstevel@tonic-gate #include <libintl.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "utils.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static char ERRNO_FMT[] = ": %s";
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static char *pname = NULL;
430Sstevel@tonic-gate static rcm_level_t message_priority = RCM_WARN;
440Sstevel@tonic-gate static rcm_dst_t message_dst = RCD_STD;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void dmesg(int level, char *msg);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*PRINTFLIKE2*/
490Sstevel@tonic-gate void
500Sstevel@tonic-gate dprintfe(int level, char *format, ...)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	va_list alist;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	va_start(alist, format);
550Sstevel@tonic-gate 	vdprintfe(level, format, alist);
560Sstevel@tonic-gate 	va_end(alist);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*PRINTFLIKE2*/
600Sstevel@tonic-gate void
61*4891Svk199839 vdprintfe(int level, const char *format, va_list alist)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	char buf[LINELEN];
640Sstevel@tonic-gate 	char *c;
650Sstevel@tonic-gate 	int err = errno;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	*buf = 0;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if ((strlen(buf) + 1) < LINELEN)
700Sstevel@tonic-gate 		(void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf),
710Sstevel@tonic-gate 		    format, alist);
720Sstevel@tonic-gate 	if ((c = strchr(buf, '\n')) == NULL) {
730Sstevel@tonic-gate 		if ((strlen(buf) + 1) < LINELEN)
740Sstevel@tonic-gate 			(void) snprintf(buf + strlen(buf), LINELEN - 1 -
750Sstevel@tonic-gate 			    strlen(buf), gettext(ERRNO_FMT), strerror(err));
760Sstevel@tonic-gate 	} else
770Sstevel@tonic-gate 		*c = 0;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	dmesg(level, buf);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #ifdef DEBUG_MSG
830Sstevel@tonic-gate /*PRINTFLIKE1*/
840Sstevel@tonic-gate void
850Sstevel@tonic-gate debug(char *format, ...)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	va_list alist;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (get_message_priority() < RCM_DEBUG)
900Sstevel@tonic-gate 		return;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	va_start(alist, format);
930Sstevel@tonic-gate 	vdprintfe(RCM_DEBUG, format, alist);
940Sstevel@tonic-gate 	va_end(alist);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*PRINTFLIKE1*/
980Sstevel@tonic-gate void
990Sstevel@tonic-gate debug_high(char *format, ...)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	va_list alist;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (get_message_priority() < RCM_DEBUG_HIGH)
1040Sstevel@tonic-gate 		return;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	va_start(alist, format);
1070Sstevel@tonic-gate 	vdprintfe(RCM_DEBUG_HIGH, format, alist);
1080Sstevel@tonic-gate 	va_end(alist);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate #endif /* DEBUG_MSG */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*PRINTFLIKE1*/
1130Sstevel@tonic-gate void
114*4891Svk199839 warn(const char *format, ...)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	va_list alist;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (get_message_priority() < RCM_WARN)
1190Sstevel@tonic-gate 		return;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	va_start(alist, format);
1220Sstevel@tonic-gate 	vdprintfe(RCM_WARN, format, alist);
1230Sstevel@tonic-gate 	va_end(alist);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*PRINTFLIKE1*/
1270Sstevel@tonic-gate void
1280Sstevel@tonic-gate die(char *format, ...)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	va_list alist;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (get_message_priority() < RCM_ERR)
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	va_start(alist, format);
1360Sstevel@tonic-gate 	vdprintfe(RCM_ERR, format, alist);
1370Sstevel@tonic-gate 	va_end(alist);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	exit(E_ERROR);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*PRINTFLIKE1*/
1430Sstevel@tonic-gate void
1440Sstevel@tonic-gate info(char *format, ...)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	va_list alist;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (get_message_priority() < RCM_INFO)
1490Sstevel@tonic-gate 		return;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	va_start(alist, format);
1520Sstevel@tonic-gate 	vdprintfe(RCM_INFO, format, alist);
1530Sstevel@tonic-gate 	va_end(alist);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate char *
1570Sstevel@tonic-gate setprogname(char *arg0)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	char *p = strrchr(arg0, '/');
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (p == NULL)
1620Sstevel@tonic-gate 		p = arg0;
1630Sstevel@tonic-gate 	else
1640Sstevel@tonic-gate 		p++;
1650Sstevel@tonic-gate 	pname = p;
1660Sstevel@tonic-gate 	return (pname);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Output a message to the controlling tty or log, depending on which is
1710Sstevel@tonic-gate  * configured.  The message should contain no newlines.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate static void
1740Sstevel@tonic-gate dmesg(int level, char *msg)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	if (message_priority >= level) {
1770Sstevel@tonic-gate 		FILE *fp;
1780Sstevel@tonic-gate 		int syslog_severity = -1;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		switch (message_dst) {
1810Sstevel@tonic-gate 		case RCD_STD:
1820Sstevel@tonic-gate 			fp = level >= RCM_DEBUG ? stderr : stdout;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 			if (pname != NULL) {
1850Sstevel@tonic-gate 				(void) fputs(pname, fp);
1860Sstevel@tonic-gate 				(void) fputs(": ", fp);
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 			(void) fputs(msg, fp);
1890Sstevel@tonic-gate 			(void) fputc('\n', fp);
1900Sstevel@tonic-gate 			(void) fflush(fp);
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 		case RCD_SYSLOG:
1930Sstevel@tonic-gate 			switch (level) {
1940Sstevel@tonic-gate 			case RCM_ERR:
1950Sstevel@tonic-gate 				syslog_severity = LOG_ERR;
1960Sstevel@tonic-gate 				break;
1970Sstevel@tonic-gate 			case RCM_WARN:
1980Sstevel@tonic-gate 				syslog_severity = LOG_WARNING;
1990Sstevel@tonic-gate 				break;
2000Sstevel@tonic-gate 			case RCM_INFO:
2010Sstevel@tonic-gate 				syslog_severity = LOG_INFO;
2020Sstevel@tonic-gate 				break;
2030Sstevel@tonic-gate 			case RCM_DEBUG:
2040Sstevel@tonic-gate 				syslog_severity = LOG_DEBUG;
2050Sstevel@tonic-gate 				break;
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 			if (syslog_severity >= 0)
2080Sstevel@tonic-gate 				(void) syslog(syslog_severity, "%s", msg);
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate rcm_level_t
2150Sstevel@tonic-gate get_message_priority(void)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	return (message_priority);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate rcm_level_t
2210Sstevel@tonic-gate set_message_priority(rcm_level_t new_priority)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	rcm_level_t old_priority = message_priority;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	message_priority = new_priority;
2260Sstevel@tonic-gate 	return (old_priority);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate rcm_dst_t
2300Sstevel@tonic-gate set_message_destination(rcm_dst_t new_dst)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	rcm_dst_t old_dst = message_dst;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if ((message_dst = new_dst) == RCD_SYSLOG)
2350Sstevel@tonic-gate 		openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (old_dst);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate void
2410Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	tsp->tv_sec = hrt / NANOSEC;
2440Sstevel@tonic-gate 	tsp->tv_nsec = hrt % NANOSEC;
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate int
2480Sstevel@tonic-gate xatoi(char *p)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	int i;
2510Sstevel@tonic-gate 	char *q;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	errno = 0;
2540Sstevel@tonic-gate 	i = (int)strtol(p, &q, 10);
2550Sstevel@tonic-gate 	if (errno != 0 || q == p || i < 0 || *q != '\0') {
2560Sstevel@tonic-gate 		warn(gettext("illegal argument -- %s\n"), p);
2570Sstevel@tonic-gate 		return (-1);
2580Sstevel@tonic-gate 	} else {
2590Sstevel@tonic-gate 		return (i);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate }
2623247Sgjelinek 
2633247Sgjelinek /*
2643247Sgjelinek  * get_running_zones() calls zone_list(2) to find out how many zones are
2653247Sgjelinek  * running.  It then calls zone_list(2) again to fetch the list of running
2663247Sgjelinek  * zones (stored in *zents).
2673247Sgjelinek  */
2683247Sgjelinek int
2693247Sgjelinek get_running_zones(uint_t *nzents, zone_entry_t **zents)
2703247Sgjelinek {
2713247Sgjelinek 	zoneid_t *zids;
2723247Sgjelinek 	uint_t nzents_saved;
2733247Sgjelinek 	int i;
2743247Sgjelinek 	zone_entry_t *zentp;
2753247Sgjelinek 	zone_state_t zstate;
2763247Sgjelinek 
2773247Sgjelinek 	*zents = NULL;
2783247Sgjelinek 	if (zone_list(NULL, nzents) != 0) {
2793247Sgjelinek 		warn(gettext("could not get zoneid list\n"));
2803247Sgjelinek 		return (E_ERROR);
2813247Sgjelinek 	}
2823247Sgjelinek 
2833247Sgjelinek again:
2843247Sgjelinek 	if (*nzents == 0)
2853247Sgjelinek 		return (E_SUCCESS);
2863247Sgjelinek 
2873247Sgjelinek 	if ((zids = (zoneid_t *)calloc(*nzents, sizeof (zoneid_t))) == NULL) {
2883247Sgjelinek 		warn(gettext("out of memory: zones will not be capped\n"));
2893247Sgjelinek 		return (E_ERROR);
2903247Sgjelinek 	}
2913247Sgjelinek 
2923247Sgjelinek 	nzents_saved = *nzents;
2933247Sgjelinek 
2943247Sgjelinek 	if (zone_list(zids, nzents) != 0) {
2953247Sgjelinek 		warn(gettext("could not get zone list\n"));
2963247Sgjelinek 		free(zids);
2973247Sgjelinek 		return (E_ERROR);
2983247Sgjelinek 	}
2993247Sgjelinek 	if (*nzents != nzents_saved) {
3003247Sgjelinek 		/* list changed, try again */
3013247Sgjelinek 		free(zids);
3023247Sgjelinek 		goto again;
3033247Sgjelinek 	}
3043247Sgjelinek 
3053247Sgjelinek 	*zents = calloc(*nzents, sizeof (zone_entry_t));
3063247Sgjelinek 	if (*zents == NULL) {
3073247Sgjelinek 		warn(gettext("out of memory: zones will not be capped\n"));
3083247Sgjelinek 		free(zids);
3093247Sgjelinek 		return (E_ERROR);
3103247Sgjelinek 	}
3113247Sgjelinek 
3123247Sgjelinek 	zentp = *zents;
3133247Sgjelinek 	for (i = 0; i < *nzents; i++) {
3143247Sgjelinek 		char name[ZONENAME_MAX];
3153247Sgjelinek 
3163247Sgjelinek 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
3173247Sgjelinek 			warn(gettext("could not get name for "
3183247Sgjelinek 			    "zoneid %d\n"), zids[i]);
3193247Sgjelinek 			continue;
3203247Sgjelinek 		}
3213247Sgjelinek 
3223247Sgjelinek 		(void) strlcpy(zentp->zname, name, sizeof (zentp->zname));
3233247Sgjelinek 		zentp->zid = zids[i];
3243247Sgjelinek 		if (zone_get_state(name, &zstate) != Z_OK ||
3253247Sgjelinek 		    zstate != ZONE_STATE_RUNNING)
3263247Sgjelinek 			continue;
3273247Sgjelinek 
3283247Sgjelinek 
3293247Sgjelinek 		zentp++;
3303247Sgjelinek 	}
3313247Sgjelinek 	*nzents = zentp - *zents;
3323247Sgjelinek 
3333247Sgjelinek 	free(zids);
3343247Sgjelinek 	return (E_SUCCESS);
3353247Sgjelinek }
336