xref: /onnv-gate/usr/src/cmd/wbem/provider/tools/rds/rdutil.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/resource.h>
30*0Sstevel@tonic-gate #include <sys/time.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <libintl.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <stdarg.h>
37*0Sstevel@tonic-gate #include <poll.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <project.h>
40*0Sstevel@tonic-gate #include <pwd.h>
41*0Sstevel@tonic-gate #include <pthread.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "rdfile.h"
44*0Sstevel@tonic-gate #include "rdimpl.h"
45*0Sstevel@tonic-gate #include "rdutil.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static char PRG_FMT[] = "%s: ";
48*0Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n";
49*0Sstevel@tonic-gate static char *modulname = "srm provider";
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate extern jmp_buf  dm_jmpbuffer;
52*0Sstevel@tonic-gate extern char errmsg[];
53*0Sstevel@tonic-gate extern void monitor_stop();
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate extern pthread_mutex_t logLock;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*PRINTFLIKE1*/
58*0Sstevel@tonic-gate void
format_err(char * format,...)59*0Sstevel@tonic-gate format_err(char *format, ...)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	int pos = 0;
62*0Sstevel@tonic-gate 	va_list alist;
63*0Sstevel@tonic-gate 	pos += sprintf(errmsg, PRG_FMT, modulname);
64*0Sstevel@tonic-gate 	va_start(alist, format);
65*0Sstevel@tonic-gate 	pos += vsprintf(errmsg + pos, format, alist);
66*0Sstevel@tonic-gate 	va_end(alist);
67*0Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
68*0Sstevel@tonic-gate 		(void) sprintf(errmsg + pos, "\n");
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*PRINTFLIKE1*/
72*0Sstevel@tonic-gate void
format_errno(char * format,...)73*0Sstevel@tonic-gate format_errno(char *format, ...)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	int err = errno, pos = 0;
76*0Sstevel@tonic-gate 	va_list alist;
77*0Sstevel@tonic-gate 	pos += sprintf(errmsg, PRG_FMT, modulname);
78*0Sstevel@tonic-gate 	va_start(alist, format);
79*0Sstevel@tonic-gate 	pos += vsprintf(errmsg + pos, format, alist);
80*0Sstevel@tonic-gate 	va_end(alist);
81*0Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
82*0Sstevel@tonic-gate 		pos += sprintf(errmsg + pos, ERR_FMT,
83*0Sstevel@tonic-gate 				(err != 0) ? strerror(err) : "");
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*PRINTFLIKE1*/
87*0Sstevel@tonic-gate void
dmerror(char * format,...)88*0Sstevel@tonic-gate dmerror(char *format, ...)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	int err = errno, pos = 0;
91*0Sstevel@tonic-gate 	va_list alist;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	pos += sprintf(errmsg, PRG_FMT, modulname);
94*0Sstevel@tonic-gate 	va_start(alist, format);
95*0Sstevel@tonic-gate 	pos += vsprintf(errmsg + pos, format, alist);
96*0Sstevel@tonic-gate 	va_end(alist);
97*0Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
98*0Sstevel@tonic-gate 		pos += sprintf(errmsg + pos, ERR_FMT, strerror(err));
99*0Sstevel@tonic-gate 	longjmp(dm_jmpbuffer, 1);
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate void *
Realloc(void * ptr,size_t size)103*0Sstevel@tonic-gate Realloc(void *ptr, size_t size)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	int	cnt = 0;
106*0Sstevel@tonic-gate 	void    *sav = ptr;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate eagain: if ((ptr = realloc(ptr, size)))
109*0Sstevel@tonic-gate 		return (ptr);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	log_err("realloc(ptr=0x%p, size=%u)", (void *)ptr, (uint_t)size);
112*0Sstevel@tonic-gate 	if ((++cnt <= 3) && (errno == EAGAIN)) {
113*0Sstevel@tonic-gate 		napms(5000); /* wait for 5 seconds */
114*0Sstevel@tonic-gate 		ptr = sav;
115*0Sstevel@tonic-gate 		goto eagain;
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 	ptr = sav;
118*0Sstevel@tonic-gate 	dmerror("not enough memory");
119*0Sstevel@tonic-gate 	/*NOTREACHED*/
120*0Sstevel@tonic-gate 	return (NULL);	/* keep gcc happy */
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate void *
Malloc(size_t size)124*0Sstevel@tonic-gate Malloc(size_t size)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	return (Realloc(NULL, size));
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate void *
Zalloc(size_t size)130*0Sstevel@tonic-gate Zalloc(size_t size)
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	return (memset(Realloc(NULL, size), 0, size));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate void
Free(void * ptr)136*0Sstevel@tonic-gate Free(void *ptr)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	free(ptr);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate int
Setrlimit()142*0Sstevel@tonic-gate Setrlimit()
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	struct rlimit rlim;
145*0Sstevel@tonic-gate 	int rv, fd_limit;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
148*0Sstevel@tonic-gate 		dmerror("getrlimit failed");
149*0Sstevel@tonic-gate 	fd_limit = (int)rlim.rlim_cur;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	rlim.rlim_cur = rlim.rlim_max;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
154*0Sstevel@tonic-gate 	    rv = fd_limit;
155*0Sstevel@tonic-gate 	else
156*0Sstevel@tonic-gate 	    rv = (int)rlim.rlim_cur;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	log_msg("fd_limit set to %d\n", rv);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	return (rv);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate void
list_alloc(list_t * list,int size)164*0Sstevel@tonic-gate list_alloc(list_t *list, int size)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	if (size > 0) {
167*0Sstevel@tonic-gate 		list->l_size = size;
168*0Sstevel@tonic-gate 		list->l_ptrs = Zalloc(sizeof (void *) * (size + 1));
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate void
list_init(list_t * list,int type)173*0Sstevel@tonic-gate list_init(list_t *list, int type)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	if (list == NULL)
176*0Sstevel@tonic-gate 		return;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	list->l_type = type;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	list->l_type = type;
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate void
getusrname(int uid,char * name,int length)185*0Sstevel@tonic-gate getusrname(int uid, char *name, int length)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	struct passwd *pwd;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if ((pwd = getpwuid(uid)) == NULL) {
190*0Sstevel@tonic-gate 		log_err("getpwuid(uid=%d,..)", uid);
191*0Sstevel@tonic-gate 		(void) snprintf(name, length, "%d", uid);
192*0Sstevel@tonic-gate 	} else {
193*0Sstevel@tonic-gate 		(void) snprintf(name, length, "%s", pwd->pw_name);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate void
getprojname(projid_t projid,char * str,int len)198*0Sstevel@tonic-gate getprojname(projid_t projid, char *str, int len)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	struct project proj;
201*0Sstevel@tonic-gate 	char projbuf[PROJECT_BUFSZ];
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	if (getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) != NULL) {
204*0Sstevel@tonic-gate 	    (void) snprintf(str, len, "%s", proj.pj_name);
205*0Sstevel@tonic-gate 	} else {
206*0Sstevel@tonic-gate 	    log_err("getprojbyid(projid=%ld,..)", (long)projid);
207*0Sstevel@tonic-gate 	    (void) snprintf(str, len, "%-6ld", (long)projid);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate void
napms(int ms)214*0Sstevel@tonic-gate napms(int ms)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	(void) poll(NULL, 0, ms);
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate 
get_timestamp()220*0Sstevel@tonic-gate longlong_t get_timestamp() {
221*0Sstevel@tonic-gate 	struct timeval tv;
222*0Sstevel@tonic-gate 	struct timezone tz;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	(void) gettimeofday(&tv, &tz);
225*0Sstevel@tonic-gate 	return (tv.tv_usec  + ((longlong_t)tv.tv_sec * MICROSEC));
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate static FILE *logf = NULL;
230*0Sstevel@tonic-gate static char buf[RDS_MAXLINE];
231*0Sstevel@tonic-gate static hrtime_t hrt_base = 0;
232*0Sstevel@tonic-gate static int call_cnt = 0;
233*0Sstevel@tonic-gate static int bytes_written = 0;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate void
log_open(char * file)236*0Sstevel@tonic-gate log_open(char *file)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	if (strcmp(file, "stderr") == 0)
239*0Sstevel@tonic-gate 		logf = stderr;
240*0Sstevel@tonic-gate 	else
241*0Sstevel@tonic-gate 		logf = fopen(file, "w+");
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate void
log_close()246*0Sstevel@tonic-gate log_close()
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	if (logf != NULL && logf != stderr)
249*0Sstevel@tonic-gate 		(void) fclose(logf);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate /*PRINTFLIKE1*/
254*0Sstevel@tonic-gate void
log_msg(char * fmt,...)255*0Sstevel@tonic-gate log_msg(char *fmt, ...)
256*0Sstevel@tonic-gate {
257*0Sstevel@tonic-gate 	va_list	ap;
258*0Sstevel@tonic-gate 	int n;
259*0Sstevel@tonic-gate 	hrtime_t hrt;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if (logf == NULL)
262*0Sstevel@tonic-gate 		return;
263*0Sstevel@tonic-gate 	if (pthread_mutex_lock(&logLock) == 0) {
264*0Sstevel@tonic-gate 	    if (logf != stderr && bytes_written > RDS_MAXLOG_FILE) {
265*0Sstevel@tonic-gate 		bytes_written = 0;
266*0Sstevel@tonic-gate 		rewind(logf);
267*0Sstevel@tonic-gate 	    }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	    if (hrt_base == 0)
270*0Sstevel@tonic-gate 		hrt_base =  gethrtime() / 1000000;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	    hrt = gethrtime() / 1000000;
273*0Sstevel@tonic-gate 	    va_start(ap, fmt);
274*0Sstevel@tonic-gate 	    (void) vsnprintf(buf, RDS_MAXLINE, fmt, ap);
275*0Sstevel@tonic-gate 	    if ((n = fprintf(logf, "%4d:%08lld ms:%s",
276*0Sstevel@tonic-gate 			call_cnt++, hrt - hrt_base, buf)) != -1)
277*0Sstevel@tonic-gate 		bytes_written += n;
278*0Sstevel@tonic-gate 	    (void) fflush(logf);
279*0Sstevel@tonic-gate 	    va_end(ap);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	    if (pthread_mutex_unlock(&logLock) != 0)
282*0Sstevel@tonic-gate 		perror("log pthread_mutex_unlock");
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	} else
285*0Sstevel@tonic-gate 	    perror("log pthread_mutex_lock");
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /*PRINTFLIKE1*/
289*0Sstevel@tonic-gate void
log_err(char * fmt,...)290*0Sstevel@tonic-gate log_err(char *fmt, ...)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	va_list	ap;
293*0Sstevel@tonic-gate 	int n;
294*0Sstevel@tonic-gate 	hrtime_t hrt;
295*0Sstevel@tonic-gate 	int err = errno;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	if (logf == NULL)
298*0Sstevel@tonic-gate 		return;
299*0Sstevel@tonic-gate 	if (pthread_mutex_lock(&logLock) == 0) {
300*0Sstevel@tonic-gate 	    if (logf != stderr && bytes_written > RDS_MAXLOG_FILE) {
301*0Sstevel@tonic-gate 		    bytes_written = 0;
302*0Sstevel@tonic-gate 		    rewind(logf);
303*0Sstevel@tonic-gate 	    }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	    if (hrt_base == 0)
306*0Sstevel@tonic-gate 		    hrt_base =  gethrtime() / 1000000;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	    hrt = gethrtime() / 1000000;
309*0Sstevel@tonic-gate 	    va_start(ap, fmt);
310*0Sstevel@tonic-gate 	    (void) vsnprintf(buf, RDS_MAXLINE, fmt, ap);
311*0Sstevel@tonic-gate 	    if ((n = fprintf(logf, "%4d:%08lld ms:ERROR: %s: (errno %d), %s\n",
312*0Sstevel@tonic-gate 			    call_cnt++, hrt - hrt_base, buf, err,
313*0Sstevel@tonic-gate 			    (err != 0) ? strerror(err) : "")) != -1)
314*0Sstevel@tonic-gate 		    bytes_written += n;
315*0Sstevel@tonic-gate 	    (void) fflush(logf);
316*0Sstevel@tonic-gate 	    va_end(ap);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	    if (pthread_mutex_unlock(&logLock) != 0)
319*0Sstevel@tonic-gate 		perror("log pthread_mutex_unlock");
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	} else
322*0Sstevel@tonic-gate 	    perror("log pthread_mutex_lock");
323*0Sstevel@tonic-gate }
324