xref: /titanic_52/usr/src/cmd/rcap/common/utils.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
30*7c478bd9Sstevel@tonic-gate #include <libintl.h>
31*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <strings.h>
35*7c478bd9Sstevel@tonic-gate #include <syslog.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "utils.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static char ERRNO_FMT[] = ": %s";
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static char *pname = NULL;
44*7c478bd9Sstevel@tonic-gate static rcm_level_t message_priority = RCM_WARN;
45*7c478bd9Sstevel@tonic-gate static rcm_dst_t message_dst = RCD_STD;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static void dmesg(int level, char *msg);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
50*7c478bd9Sstevel@tonic-gate void
51*7c478bd9Sstevel@tonic-gate dprintfe(int level, char *format, ...)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	va_list alist;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
56*7c478bd9Sstevel@tonic-gate 	vdprintfe(level, format, alist);
57*7c478bd9Sstevel@tonic-gate 	va_end(alist);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
61*7c478bd9Sstevel@tonic-gate void
62*7c478bd9Sstevel@tonic-gate vdprintfe(int level, char *format, va_list alist)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	char buf[LINELEN];
65*7c478bd9Sstevel@tonic-gate 	char *c;
66*7c478bd9Sstevel@tonic-gate 	int err = errno;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	*buf = 0;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if ((strlen(buf) + 1) < LINELEN)
71*7c478bd9Sstevel@tonic-gate 		(void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf),
72*7c478bd9Sstevel@tonic-gate 		    format, alist);
73*7c478bd9Sstevel@tonic-gate 	if ((c = strchr(buf, '\n')) == NULL) {
74*7c478bd9Sstevel@tonic-gate 		if ((strlen(buf) + 1) < LINELEN)
75*7c478bd9Sstevel@tonic-gate 			(void) snprintf(buf + strlen(buf), LINELEN - 1 -
76*7c478bd9Sstevel@tonic-gate 			    strlen(buf), gettext(ERRNO_FMT), strerror(err));
77*7c478bd9Sstevel@tonic-gate 	} else
78*7c478bd9Sstevel@tonic-gate 		*c = 0;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	dmesg(level, buf);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_MSG
84*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
85*7c478bd9Sstevel@tonic-gate void
86*7c478bd9Sstevel@tonic-gate debug(char *format, ...)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	va_list alist;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (get_message_priority() < RCM_DEBUG)
91*7c478bd9Sstevel@tonic-gate 		return;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
94*7c478bd9Sstevel@tonic-gate 	vdprintfe(RCM_DEBUG, format, alist);
95*7c478bd9Sstevel@tonic-gate 	va_end(alist);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
99*7c478bd9Sstevel@tonic-gate void
100*7c478bd9Sstevel@tonic-gate debug_high(char *format, ...)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	va_list alist;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (get_message_priority() < RCM_DEBUG_HIGH)
105*7c478bd9Sstevel@tonic-gate 		return;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
108*7c478bd9Sstevel@tonic-gate 	vdprintfe(RCM_DEBUG_HIGH, format, alist);
109*7c478bd9Sstevel@tonic-gate 	va_end(alist);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate #endif /* DEBUG_MSG */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
114*7c478bd9Sstevel@tonic-gate void
115*7c478bd9Sstevel@tonic-gate warn(char *format, ...)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	va_list alist;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	if (get_message_priority() < RCM_WARN)
120*7c478bd9Sstevel@tonic-gate 		return;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
123*7c478bd9Sstevel@tonic-gate 	vdprintfe(RCM_WARN, format, alist);
124*7c478bd9Sstevel@tonic-gate 	va_end(alist);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
128*7c478bd9Sstevel@tonic-gate void
129*7c478bd9Sstevel@tonic-gate die(char *format, ...)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	va_list alist;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (get_message_priority() < RCM_ERR)
134*7c478bd9Sstevel@tonic-gate 		return;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
137*7c478bd9Sstevel@tonic-gate 	vdprintfe(RCM_ERR, format, alist);
138*7c478bd9Sstevel@tonic-gate 	va_end(alist);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
144*7c478bd9Sstevel@tonic-gate void
145*7c478bd9Sstevel@tonic-gate info(char *format, ...)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	va_list alist;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (get_message_priority() < RCM_INFO)
150*7c478bd9Sstevel@tonic-gate 		return;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
153*7c478bd9Sstevel@tonic-gate 	vdprintfe(RCM_INFO, format, alist);
154*7c478bd9Sstevel@tonic-gate 	va_end(alist);
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate char *
158*7c478bd9Sstevel@tonic-gate setprogname(char *arg0)
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	char *p = strrchr(arg0, '/');
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
163*7c478bd9Sstevel@tonic-gate 		p = arg0;
164*7c478bd9Sstevel@tonic-gate 	else
165*7c478bd9Sstevel@tonic-gate 		p++;
166*7c478bd9Sstevel@tonic-gate 	pname = p;
167*7c478bd9Sstevel@tonic-gate 	return (pname);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate  * Output a message to the controlling tty or log, depending on which is
172*7c478bd9Sstevel@tonic-gate  * configured.  The message should contain no newlines.
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate static void
175*7c478bd9Sstevel@tonic-gate dmesg(int level, char *msg)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	if (message_priority >= level) {
178*7c478bd9Sstevel@tonic-gate 		FILE *fp;
179*7c478bd9Sstevel@tonic-gate 		int syslog_severity = -1;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 		switch (message_dst) {
182*7c478bd9Sstevel@tonic-gate 		case RCD_STD:
183*7c478bd9Sstevel@tonic-gate 			fp = level >= RCM_DEBUG ? stderr : stdout;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 			if (pname != NULL) {
186*7c478bd9Sstevel@tonic-gate 				(void) fputs(pname, fp);
187*7c478bd9Sstevel@tonic-gate 				(void) fputs(": ", fp);
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 			(void) fputs(msg, fp);
190*7c478bd9Sstevel@tonic-gate 			(void) fputc('\n', fp);
191*7c478bd9Sstevel@tonic-gate 			(void) fflush(fp);
192*7c478bd9Sstevel@tonic-gate 			break;
193*7c478bd9Sstevel@tonic-gate 		case RCD_SYSLOG:
194*7c478bd9Sstevel@tonic-gate 			switch (level) {
195*7c478bd9Sstevel@tonic-gate 			case RCM_ERR:
196*7c478bd9Sstevel@tonic-gate 				syslog_severity = LOG_ERR;
197*7c478bd9Sstevel@tonic-gate 				break;
198*7c478bd9Sstevel@tonic-gate 			case RCM_WARN:
199*7c478bd9Sstevel@tonic-gate 				syslog_severity = LOG_WARNING;
200*7c478bd9Sstevel@tonic-gate 				break;
201*7c478bd9Sstevel@tonic-gate 			case RCM_INFO:
202*7c478bd9Sstevel@tonic-gate 				syslog_severity = LOG_INFO;
203*7c478bd9Sstevel@tonic-gate 				break;
204*7c478bd9Sstevel@tonic-gate 			case RCM_DEBUG:
205*7c478bd9Sstevel@tonic-gate 				syslog_severity = LOG_DEBUG;
206*7c478bd9Sstevel@tonic-gate 				break;
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 			if (syslog_severity >= 0)
209*7c478bd9Sstevel@tonic-gate 				(void) syslog(syslog_severity, "%s", msg);
210*7c478bd9Sstevel@tonic-gate 			break;
211*7c478bd9Sstevel@tonic-gate 		}
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate rcm_level_t
216*7c478bd9Sstevel@tonic-gate get_message_priority(void)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	return (message_priority);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate rcm_level_t
222*7c478bd9Sstevel@tonic-gate set_message_priority(rcm_level_t new_priority)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	rcm_level_t old_priority = message_priority;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	message_priority = new_priority;
227*7c478bd9Sstevel@tonic-gate 	return (old_priority);
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate rcm_dst_t
231*7c478bd9Sstevel@tonic-gate set_message_destination(rcm_dst_t new_dst)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	rcm_dst_t old_dst = message_dst;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	if ((message_dst = new_dst) == RCD_SYSLOG)
236*7c478bd9Sstevel@tonic-gate 		openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	return (old_dst);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate void
242*7c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	tsp->tv_sec = hrt / NANOSEC;
245*7c478bd9Sstevel@tonic-gate 	tsp->tv_nsec = hrt % NANOSEC;
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate int
249*7c478bd9Sstevel@tonic-gate xatoi(char *p)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	int i;
252*7c478bd9Sstevel@tonic-gate 	char *q;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	errno = 0;
255*7c478bd9Sstevel@tonic-gate 	i = (int)strtol(p, &q, 10);
256*7c478bd9Sstevel@tonic-gate 	if (errno != 0 || q == p || i < 0 || *q != '\0') {
257*7c478bd9Sstevel@tonic-gate 		warn(gettext("illegal argument -- %s\n"), p);
258*7c478bd9Sstevel@tonic-gate 		return (-1);
259*7c478bd9Sstevel@tonic-gate 	} else {
260*7c478bd9Sstevel@tonic-gate 		return (i);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate }
263