xref: /onnv-gate/usr/src/cmd/rcap/common/utils.c (revision 3247)
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
5*3247Sgjelinek  * Common Development and Distribution License (the "License").
6*3247Sgjelinek  * 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*3247Sgjelinek  * Copyright 2006 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
610Sstevel@tonic-gate vdprintfe(int level, 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
1140Sstevel@tonic-gate warn(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 }
262*3247Sgjelinek 
263*3247Sgjelinek /*
264*3247Sgjelinek  * get_running_zones() calls zone_list(2) to find out how many zones are
265*3247Sgjelinek  * running.  It then calls zone_list(2) again to fetch the list of running
266*3247Sgjelinek  * zones (stored in *zents).
267*3247Sgjelinek  */
268*3247Sgjelinek int
269*3247Sgjelinek get_running_zones(uint_t *nzents, zone_entry_t **zents)
270*3247Sgjelinek {
271*3247Sgjelinek 	zoneid_t *zids;
272*3247Sgjelinek 	uint_t nzents_saved;
273*3247Sgjelinek 	int i;
274*3247Sgjelinek 	zone_entry_t *zentp;
275*3247Sgjelinek 	zone_state_t zstate;
276*3247Sgjelinek 
277*3247Sgjelinek 	*zents = NULL;
278*3247Sgjelinek 	if (zone_list(NULL, nzents) != 0) {
279*3247Sgjelinek 		warn(gettext("could not get zoneid list\n"));
280*3247Sgjelinek 		return (E_ERROR);
281*3247Sgjelinek 	}
282*3247Sgjelinek 
283*3247Sgjelinek again:
284*3247Sgjelinek 	if (*nzents == 0)
285*3247Sgjelinek 		return (E_SUCCESS);
286*3247Sgjelinek 
287*3247Sgjelinek 	if ((zids = (zoneid_t *)calloc(*nzents, sizeof (zoneid_t))) == NULL) {
288*3247Sgjelinek 		warn(gettext("out of memory: zones will not be capped\n"));
289*3247Sgjelinek 		return (E_ERROR);
290*3247Sgjelinek 	}
291*3247Sgjelinek 
292*3247Sgjelinek 	nzents_saved = *nzents;
293*3247Sgjelinek 
294*3247Sgjelinek 	if (zone_list(zids, nzents) != 0) {
295*3247Sgjelinek 		warn(gettext("could not get zone list\n"));
296*3247Sgjelinek 		free(zids);
297*3247Sgjelinek 		return (E_ERROR);
298*3247Sgjelinek 	}
299*3247Sgjelinek 	if (*nzents != nzents_saved) {
300*3247Sgjelinek 		/* list changed, try again */
301*3247Sgjelinek 		free(zids);
302*3247Sgjelinek 		goto again;
303*3247Sgjelinek 	}
304*3247Sgjelinek 
305*3247Sgjelinek 	*zents = calloc(*nzents, sizeof (zone_entry_t));
306*3247Sgjelinek 	if (*zents == NULL) {
307*3247Sgjelinek 		warn(gettext("out of memory: zones will not be capped\n"));
308*3247Sgjelinek 		free(zids);
309*3247Sgjelinek 		return (E_ERROR);
310*3247Sgjelinek 	}
311*3247Sgjelinek 
312*3247Sgjelinek 	zentp = *zents;
313*3247Sgjelinek 	for (i = 0; i < *nzents; i++) {
314*3247Sgjelinek 		char name[ZONENAME_MAX];
315*3247Sgjelinek 
316*3247Sgjelinek 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
317*3247Sgjelinek 			warn(gettext("could not get name for "
318*3247Sgjelinek 			    "zoneid %d\n"), zids[i]);
319*3247Sgjelinek 			continue;
320*3247Sgjelinek 		}
321*3247Sgjelinek 
322*3247Sgjelinek 		(void) strlcpy(zentp->zname, name, sizeof (zentp->zname));
323*3247Sgjelinek 		zentp->zid = zids[i];
324*3247Sgjelinek 		if (zone_get_state(name, &zstate) != Z_OK ||
325*3247Sgjelinek 		    zstate != ZONE_STATE_RUNNING)
326*3247Sgjelinek 			continue;
327*3247Sgjelinek 
328*3247Sgjelinek 
329*3247Sgjelinek 		zentp++;
330*3247Sgjelinek 	}
331*3247Sgjelinek 	*nzents = zentp - *zents;
332*3247Sgjelinek 
333*3247Sgjelinek 	free(zids);
334*3247Sgjelinek 	return (E_SUCCESS);
335*3247Sgjelinek }
336