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