4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/param.h>
38 static char ERRNO_FMT
[] = ": %s";
40 static char *pname
= NULL
;
41 static rcm_level_t message_priority
= RCM_WARN
;
42 static rcm_dst_t message_dst
= RCD_STD
;
44 static void dmesg(int level
, char *msg
);
48 dprintfe(int level
, char *format
, ...)
52 va_start(alist
, format
);
53 vdprintfe(level
, format
, alist
);
59 vdprintfe(int level
, const char *format
, va_list alist
)
67 if ((strlen(buf
) + 1) < LINELEN
)
68 (void) vsnprintf(buf
+ strlen(buf
), LINELEN
- 1 - strlen(buf
),
70 if ((c
= strchr(buf
, '\n')) == NULL
) {
71 if ((strlen(buf
) + 1) < LINELEN
)
72 (void) snprintf(buf
+ strlen(buf
), LINELEN
- 1 -
73 strlen(buf
), gettext(ERRNO_FMT
), strerror(err
));
83 debug(char *format
, ...)
87 if (get_message_priority() < RCM_DEBUG
)
90 va_start(alist
, format
);
91 vdprintfe(RCM_DEBUG
, format
, alist
);
97 debug_high(char *format
, ...)
101 if (get_message_priority() < RCM_DEBUG_HIGH
)
104 va_start(alist
, format
);
105 vdprintfe(RCM_DEBUG_HIGH
, format
, alist
);
108 #endif /* DEBUG_MSG */
112 warn(const char *format
, ...)
116 if (get_message_priority() < RCM_WARN
)
119 va_start(alist
, format
);
120 vdprintfe(RCM_WARN
, format
, alist
);
126 die(char *format
, ...)
130 if (get_message_priority() < RCM_ERR
)
133 va_start(alist
, format
);
134 vdprintfe(RCM_ERR
, format
, alist
);
142 info(char *format
, ...)
146 if (get_message_priority() < RCM_INFO
)
149 va_start(alist
, format
);
150 vdprintfe(RCM_INFO
, format
, alist
);
157 char *p
= strrchr(arg0
, '/');
168 * Output a message to the controlling tty or log, depending on which is
169 * configured. The message should contain no newlines.
172 dmesg(int level
, char *msg
)
174 if (message_priority
>= level
) {
176 int syslog_severity
= -1;
178 switch (message_dst
) {
180 fp
= level
>= RCM_DEBUG
? stderr
: stdout
;
183 (void) fputs(pname
, fp
);
184 (void) fputs(": ", fp
);
186 (void) fputs(msg
, fp
);
187 (void) fputc('\n', fp
);
193 syslog_severity
= LOG_ERR
;
196 syslog_severity
= LOG_WARNING
;
199 syslog_severity
= LOG_INFO
;
202 syslog_severity
= LOG_DEBUG
;
205 if (syslog_severity
>= 0)
206 (void) syslog(syslog_severity
, "%s", msg
);
213 get_message_priority(void)
215 return (message_priority
);
219 set_message_priority(rcm_level_t new_priority
)
221 rcm_level_t old_priority
= message_priority
;
223 message_priority
= new_priority
;
224 return (old_priority
);
228 set_message_destination(rcm_dst_t new_dst
)
230 rcm_dst_t old_dst
= message_dst
;
232 if ((message_dst
= new_dst
) == RCD_SYSLOG
)
233 openlog(pname
, LOG_ODELAY
| LOG_PID
, LOG_DAEMON
);
239 hrt2ts(hrtime_t hrt
, timestruc_t
*tsp
)
241 tsp
->tv_sec
= hrt
/ NANOSEC
;
242 tsp
->tv_nsec
= hrt
% NANOSEC
;
252 i
= (int)strtol(p
, &q
, 10);
253 if (errno
!= 0 || q
== p
|| i
< 0 || *q
!= '\0') {
254 warn(gettext("illegal argument -- %s\n"), p
);
262 * get_running_zones() calls zone_list(2) to find out how many zones are
263 * running. It then calls zone_list(2) again to fetch the list of running
264 * zones (stored in *zents).
267 get_running_zones(uint_t
*nzents
, zone_entry_t
**zents
)
276 if (zone_list(NULL
, nzents
) != 0) {
277 warn(gettext("could not get zoneid list\n"));
285 if ((zids
= (zoneid_t
*)calloc(*nzents
, sizeof (zoneid_t
))) == NULL
) {
286 warn(gettext("out of memory: zones will not be capped\n"));
290 nzents_saved
= *nzents
;
292 if (zone_list(zids
, nzents
) != 0) {
293 warn(gettext("could not get zone list\n"));
297 if (*nzents
!= nzents_saved
) {
298 /* list changed, try again */
303 *zents
= calloc(*nzents
, sizeof (zone_entry_t
));
304 if (*zents
== NULL
) {
305 warn(gettext("out of memory: zones will not be capped\n"));
311 for (i
= 0; i
< *nzents
; i
++) {
312 char name
[ZONENAME_MAX
];
314 if (getzonenamebyid(zids
[i
], name
, sizeof (name
)) < 0) {
315 warn(gettext("could not get name for "
316 "zoneid %d\n"), zids
[i
]);
320 (void) strlcpy(zentp
->zname
, name
, sizeof (zentp
->zname
));
321 zentp
->zid
= zids
[i
];
322 if (zone_get_state(name
, &zstate
) != Z_OK
||
323 zstate
!= ZONE_STATE_RUNNING
)
329 *nzents
= zentp
- *zents
;