17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50209230bSgjelinek * Common Development and Distribution License (the "License").
60209230bSgjelinek * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*23a1cceaSRoger A. Faulkner
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <libintl.h>
287c478bd9Sstevel@tonic-gate #include <stdarg.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "utils.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static char ERRNO_FMT[] = ": %s";
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate static char *pname = NULL;
417c478bd9Sstevel@tonic-gate static rcm_level_t message_priority = RCM_WARN;
427c478bd9Sstevel@tonic-gate static rcm_dst_t message_dst = RCD_STD;
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate static void dmesg(int level, char *msg);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
477c478bd9Sstevel@tonic-gate void
dprintfe(int level,char * format,...)487c478bd9Sstevel@tonic-gate dprintfe(int level, char *format, ...)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate va_list alist;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate va_start(alist, format);
537c478bd9Sstevel@tonic-gate vdprintfe(level, format, alist);
547c478bd9Sstevel@tonic-gate va_end(alist);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
587c478bd9Sstevel@tonic-gate void
vdprintfe(int level,const char * format,va_list alist)59d362b749Svk vdprintfe(int level, const char *format, va_list alist)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate char buf[LINELEN];
627c478bd9Sstevel@tonic-gate char *c;
637c478bd9Sstevel@tonic-gate int err = errno;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate *buf = 0;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if ((strlen(buf) + 1) < LINELEN)
687c478bd9Sstevel@tonic-gate (void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf),
697c478bd9Sstevel@tonic-gate format, alist);
707c478bd9Sstevel@tonic-gate if ((c = strchr(buf, '\n')) == NULL) {
717c478bd9Sstevel@tonic-gate if ((strlen(buf) + 1) < LINELEN)
727c478bd9Sstevel@tonic-gate (void) snprintf(buf + strlen(buf), LINELEN - 1 -
737c478bd9Sstevel@tonic-gate strlen(buf), gettext(ERRNO_FMT), strerror(err));
747c478bd9Sstevel@tonic-gate } else
757c478bd9Sstevel@tonic-gate *c = 0;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate dmesg(level, buf);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #ifdef DEBUG_MSG
817c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
827c478bd9Sstevel@tonic-gate void
debug(char * format,...)837c478bd9Sstevel@tonic-gate debug(char *format, ...)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate va_list alist;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (get_message_priority() < RCM_DEBUG)
887c478bd9Sstevel@tonic-gate return;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate va_start(alist, format);
917c478bd9Sstevel@tonic-gate vdprintfe(RCM_DEBUG, format, alist);
927c478bd9Sstevel@tonic-gate va_end(alist);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
967c478bd9Sstevel@tonic-gate void
debug_high(char * format,...)977c478bd9Sstevel@tonic-gate debug_high(char *format, ...)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate va_list alist;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (get_message_priority() < RCM_DEBUG_HIGH)
1027c478bd9Sstevel@tonic-gate return;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate va_start(alist, format);
1057c478bd9Sstevel@tonic-gate vdprintfe(RCM_DEBUG_HIGH, format, alist);
1067c478bd9Sstevel@tonic-gate va_end(alist);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate #endif /* DEBUG_MSG */
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1117c478bd9Sstevel@tonic-gate void
warn(const char * format,...)112d362b749Svk warn(const char *format, ...)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate va_list alist;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (get_message_priority() < RCM_WARN)
1177c478bd9Sstevel@tonic-gate return;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate va_start(alist, format);
1207c478bd9Sstevel@tonic-gate vdprintfe(RCM_WARN, format, alist);
1217c478bd9Sstevel@tonic-gate va_end(alist);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1257c478bd9Sstevel@tonic-gate void
die(char * format,...)1267c478bd9Sstevel@tonic-gate die(char *format, ...)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate va_list alist;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (get_message_priority() < RCM_ERR)
1317c478bd9Sstevel@tonic-gate return;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate va_start(alist, format);
1347c478bd9Sstevel@tonic-gate vdprintfe(RCM_ERR, format, alist);
1357c478bd9Sstevel@tonic-gate va_end(alist);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate exit(E_ERROR);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1417c478bd9Sstevel@tonic-gate void
info(char * format,...)1427c478bd9Sstevel@tonic-gate info(char *format, ...)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate va_list alist;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (get_message_priority() < RCM_INFO)
1477c478bd9Sstevel@tonic-gate return;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate va_start(alist, format);
1507c478bd9Sstevel@tonic-gate vdprintfe(RCM_INFO, format, alist);
1517c478bd9Sstevel@tonic-gate va_end(alist);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate char *
setpname(char * arg0)155*23a1cceaSRoger A. Faulkner setpname(char *arg0)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate char *p = strrchr(arg0, '/');
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (p == NULL)
1607c478bd9Sstevel@tonic-gate p = arg0;
1617c478bd9Sstevel@tonic-gate else
1627c478bd9Sstevel@tonic-gate p++;
1637c478bd9Sstevel@tonic-gate pname = p;
1647c478bd9Sstevel@tonic-gate return (pname);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate * Output a message to the controlling tty or log, depending on which is
1697c478bd9Sstevel@tonic-gate * configured. The message should contain no newlines.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate static void
dmesg(int level,char * msg)1727c478bd9Sstevel@tonic-gate dmesg(int level, char *msg)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate if (message_priority >= level) {
1757c478bd9Sstevel@tonic-gate FILE *fp;
1767c478bd9Sstevel@tonic-gate int syslog_severity = -1;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate switch (message_dst) {
1797c478bd9Sstevel@tonic-gate case RCD_STD:
1807c478bd9Sstevel@tonic-gate fp = level >= RCM_DEBUG ? stderr : stdout;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate if (pname != NULL) {
1837c478bd9Sstevel@tonic-gate (void) fputs(pname, fp);
1847c478bd9Sstevel@tonic-gate (void) fputs(": ", fp);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate (void) fputs(msg, fp);
1877c478bd9Sstevel@tonic-gate (void) fputc('\n', fp);
1887c478bd9Sstevel@tonic-gate (void) fflush(fp);
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate case RCD_SYSLOG:
1917c478bd9Sstevel@tonic-gate switch (level) {
1927c478bd9Sstevel@tonic-gate case RCM_ERR:
1937c478bd9Sstevel@tonic-gate syslog_severity = LOG_ERR;
1947c478bd9Sstevel@tonic-gate break;
1957c478bd9Sstevel@tonic-gate case RCM_WARN:
1967c478bd9Sstevel@tonic-gate syslog_severity = LOG_WARNING;
1977c478bd9Sstevel@tonic-gate break;
1987c478bd9Sstevel@tonic-gate case RCM_INFO:
1997c478bd9Sstevel@tonic-gate syslog_severity = LOG_INFO;
2007c478bd9Sstevel@tonic-gate break;
2017c478bd9Sstevel@tonic-gate case RCM_DEBUG:
2027c478bd9Sstevel@tonic-gate syslog_severity = LOG_DEBUG;
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate if (syslog_severity >= 0)
2067c478bd9Sstevel@tonic-gate (void) syslog(syslog_severity, "%s", msg);
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate rcm_level_t
get_message_priority(void)2137c478bd9Sstevel@tonic-gate get_message_priority(void)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate return (message_priority);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate rcm_level_t
set_message_priority(rcm_level_t new_priority)2197c478bd9Sstevel@tonic-gate set_message_priority(rcm_level_t new_priority)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate rcm_level_t old_priority = message_priority;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate message_priority = new_priority;
2247c478bd9Sstevel@tonic-gate return (old_priority);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate rcm_dst_t
set_message_destination(rcm_dst_t new_dst)2287c478bd9Sstevel@tonic-gate set_message_destination(rcm_dst_t new_dst)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate rcm_dst_t old_dst = message_dst;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate if ((message_dst = new_dst) == RCD_SYSLOG)
2337c478bd9Sstevel@tonic-gate openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON);
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate return (old_dst);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)2397c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate tsp->tv_sec = hrt / NANOSEC;
2427c478bd9Sstevel@tonic-gate tsp->tv_nsec = hrt % NANOSEC;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate int
xatoi(char * p)2467c478bd9Sstevel@tonic-gate xatoi(char *p)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate int i;
2497c478bd9Sstevel@tonic-gate char *q;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate errno = 0;
2527c478bd9Sstevel@tonic-gate i = (int)strtol(p, &q, 10);
2537c478bd9Sstevel@tonic-gate if (errno != 0 || q == p || i < 0 || *q != '\0') {
2547c478bd9Sstevel@tonic-gate warn(gettext("illegal argument -- %s\n"), p);
2557c478bd9Sstevel@tonic-gate return (-1);
2567c478bd9Sstevel@tonic-gate } else {
2577c478bd9Sstevel@tonic-gate return (i);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate }
2600209230bSgjelinek
2610209230bSgjelinek /*
2620209230bSgjelinek * get_running_zones() calls zone_list(2) to find out how many zones are
2630209230bSgjelinek * running. It then calls zone_list(2) again to fetch the list of running
2640209230bSgjelinek * zones (stored in *zents).
2650209230bSgjelinek */
2660209230bSgjelinek int
get_running_zones(uint_t * nzents,zone_entry_t ** zents)2670209230bSgjelinek get_running_zones(uint_t *nzents, zone_entry_t **zents)
2680209230bSgjelinek {
2690209230bSgjelinek zoneid_t *zids;
2700209230bSgjelinek uint_t nzents_saved;
2710209230bSgjelinek int i;
2720209230bSgjelinek zone_entry_t *zentp;
2730209230bSgjelinek zone_state_t zstate;
2740209230bSgjelinek
2750209230bSgjelinek *zents = NULL;
2760209230bSgjelinek if (zone_list(NULL, nzents) != 0) {
2770209230bSgjelinek warn(gettext("could not get zoneid list\n"));
2780209230bSgjelinek return (E_ERROR);
2790209230bSgjelinek }
2800209230bSgjelinek
2810209230bSgjelinek again:
2820209230bSgjelinek if (*nzents == 0)
2830209230bSgjelinek return (E_SUCCESS);
2840209230bSgjelinek
2850209230bSgjelinek if ((zids = (zoneid_t *)calloc(*nzents, sizeof (zoneid_t))) == NULL) {
2860209230bSgjelinek warn(gettext("out of memory: zones will not be capped\n"));
2870209230bSgjelinek return (E_ERROR);
2880209230bSgjelinek }
2890209230bSgjelinek
2900209230bSgjelinek nzents_saved = *nzents;
2910209230bSgjelinek
2920209230bSgjelinek if (zone_list(zids, nzents) != 0) {
2930209230bSgjelinek warn(gettext("could not get zone list\n"));
2940209230bSgjelinek free(zids);
2950209230bSgjelinek return (E_ERROR);
2960209230bSgjelinek }
2970209230bSgjelinek if (*nzents != nzents_saved) {
2980209230bSgjelinek /* list changed, try again */
2990209230bSgjelinek free(zids);
3000209230bSgjelinek goto again;
3010209230bSgjelinek }
3020209230bSgjelinek
3030209230bSgjelinek *zents = calloc(*nzents, sizeof (zone_entry_t));
3040209230bSgjelinek if (*zents == NULL) {
3050209230bSgjelinek warn(gettext("out of memory: zones will not be capped\n"));
3060209230bSgjelinek free(zids);
3070209230bSgjelinek return (E_ERROR);
3080209230bSgjelinek }
3090209230bSgjelinek
3100209230bSgjelinek zentp = *zents;
3110209230bSgjelinek for (i = 0; i < *nzents; i++) {
3120209230bSgjelinek char name[ZONENAME_MAX];
3130209230bSgjelinek
3140209230bSgjelinek if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
3150209230bSgjelinek warn(gettext("could not get name for "
3160209230bSgjelinek "zoneid %d\n"), zids[i]);
3170209230bSgjelinek continue;
3180209230bSgjelinek }
3190209230bSgjelinek
3200209230bSgjelinek (void) strlcpy(zentp->zname, name, sizeof (zentp->zname));
3210209230bSgjelinek zentp->zid = zids[i];
3220209230bSgjelinek if (zone_get_state(name, &zstate) != Z_OK ||
3230209230bSgjelinek zstate != ZONE_STATE_RUNNING)
3240209230bSgjelinek continue;
3250209230bSgjelinek
3260209230bSgjelinek
3270209230bSgjelinek zentp++;
3280209230bSgjelinek }
3290209230bSgjelinek *nzents = zentp - *zents;
3300209230bSgjelinek
3310209230bSgjelinek free(zids);
3320209230bSgjelinek return (E_SUCCESS);
3330209230bSgjelinek }
334