xref: /onnv-gate/usr/src/lib/libproc/common/Putil.c (revision 789:b348f31ed315)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*789Sahrens  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "Pcontrol.h"
360Sstevel@tonic-gate #include "Putil.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Place the new element on the list prior to the existing element.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate void
list_link(void * new,void * existing)420Sstevel@tonic-gate list_link(void *new, void *existing)
430Sstevel@tonic-gate {
44*789Sahrens 	plist_t *p = new;
45*789Sahrens 	plist_t *q = existing;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	if (q) {
480Sstevel@tonic-gate 		p->list_forw = q;
490Sstevel@tonic-gate 		p->list_back = q->list_back;
500Sstevel@tonic-gate 		q->list_back->list_forw = p;
510Sstevel@tonic-gate 		q->list_back = p;
520Sstevel@tonic-gate 	} else {
530Sstevel@tonic-gate 		p->list_forw = p->list_back = p;
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Unchain the specified element from a list.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate void
list_unlink(void * old)610Sstevel@tonic-gate list_unlink(void *old)
620Sstevel@tonic-gate {
63*789Sahrens 	plist_t *p = old;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (p->list_forw != p) {
660Sstevel@tonic-gate 		p->list_back->list_forw = p->list_forw;
670Sstevel@tonic-gate 		p->list_forw->list_back = p->list_back;
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	p->list_forw = p->list_back = p;
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Routines to manipulate sigset_t, fltset_t, or sysset_t.  These routines
740Sstevel@tonic-gate  * are provided as equivalents for the <sys/procfs.h> macros prfillset,
750Sstevel@tonic-gate  * premptyset, praddset, and prdelset.  These functions are preferable
760Sstevel@tonic-gate  * because they are not macros which rely on using sizeof (*sp), and thus
770Sstevel@tonic-gate  * can be used to create common code to manipulate event sets.  The set
780Sstevel@tonic-gate  * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate void
prset_fill(void * sp,size_t size)810Sstevel@tonic-gate prset_fill(void *sp, size_t size)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	while (i != 0)
860Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate void
prset_empty(void * sp,size_t size)900Sstevel@tonic-gate prset_empty(void *sp, size_t size)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	while (i != 0)
950Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate void
prset_add(void * sp,size_t size,uint_t flag)990Sstevel@tonic-gate prset_add(void *sp, size_t size, uint_t flag)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
1020Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate void
prset_del(void * sp,size_t size,uint_t flag)1060Sstevel@tonic-gate prset_del(void *sp, size_t size, uint_t flag)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
1090Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
prset_ismember(void * sp,size_t size,uint_t flag)1130Sstevel@tonic-gate prset_ismember(void *sp, size_t size, uint_t flag)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
1160Sstevel@tonic-gate 	    (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * If _libproc_debug is set, printf the debug message to stderr
1210Sstevel@tonic-gate  * with an appropriate prefix.
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate /*PRINTFLIKE1*/
1240Sstevel@tonic-gate void
dprintf(const char * format,...)1250Sstevel@tonic-gate dprintf(const char *format, ...)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	if (_libproc_debug) {
1280Sstevel@tonic-gate 		va_list alist;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		va_start(alist, format);
1310Sstevel@tonic-gate 		(void) fputs("libproc DEBUG: ", stderr);
1320Sstevel@tonic-gate 		(void) vfprintf(stderr, format, alist);
1330Sstevel@tonic-gate 		va_end(alist);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Printf-style error reporting function.  This is used to supplement the error
1390Sstevel@tonic-gate  * return codes from various libproc functions with additional text.  Since we
1400Sstevel@tonic-gate  * are a library, and should not be spewing messages to stderr, we provide a
1410Sstevel@tonic-gate  * default version of this function that does nothing, but by calling this
1420Sstevel@tonic-gate  * function we allow the client program to define its own version of the
1430Sstevel@tonic-gate  * function that will interpose on our empty default.  This may be useful for
1440Sstevel@tonic-gate  * clients that wish to display such messages to the user.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate /*ARGSUSED*/
1470Sstevel@tonic-gate /*PRINTFLIKE2*/
1480Sstevel@tonic-gate void
Perror_printf(struct ps_prochandle * P,const char * format,...)1490Sstevel@tonic-gate Perror_printf(struct ps_prochandle *P, const char *format, ...)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	/* nothing to do here */
1520Sstevel@tonic-gate }
153