xref: /titanic_53/usr/src/lib/libproc/common/Putil.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 (c) 1998-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
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 <limits.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <errno.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
36*7c478bd9Sstevel@tonic-gate #include "Putil.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Place the new element on the list prior to the existing element.
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate void
42*7c478bd9Sstevel@tonic-gate list_link(void *new, void *existing)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	list_t *p = new;
45*7c478bd9Sstevel@tonic-gate 	list_t *q = existing;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	if (q) {
48*7c478bd9Sstevel@tonic-gate 		p->list_forw = q;
49*7c478bd9Sstevel@tonic-gate 		p->list_back = q->list_back;
50*7c478bd9Sstevel@tonic-gate 		q->list_back->list_forw = p;
51*7c478bd9Sstevel@tonic-gate 		q->list_back = p;
52*7c478bd9Sstevel@tonic-gate 	} else {
53*7c478bd9Sstevel@tonic-gate 		p->list_forw = p->list_back = p;
54*7c478bd9Sstevel@tonic-gate 	}
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate  * Unchain the specified element from a list.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate void
61*7c478bd9Sstevel@tonic-gate list_unlink(void *old)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	list_t *p = old;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if (p->list_forw != p) {
66*7c478bd9Sstevel@tonic-gate 		p->list_back->list_forw = p->list_forw;
67*7c478bd9Sstevel@tonic-gate 		p->list_forw->list_back = p->list_back;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	p->list_forw = p->list_back = p;
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * Routines to manipulate sigset_t, fltset_t, or sysset_t.  These routines
74*7c478bd9Sstevel@tonic-gate  * are provided as equivalents for the <sys/procfs.h> macros prfillset,
75*7c478bd9Sstevel@tonic-gate  * premptyset, praddset, and prdelset.  These functions are preferable
76*7c478bd9Sstevel@tonic-gate  * because they are not macros which rely on using sizeof (*sp), and thus
77*7c478bd9Sstevel@tonic-gate  * can be used to create common code to manipulate event sets.  The set
78*7c478bd9Sstevel@tonic-gate  * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate void
81*7c478bd9Sstevel@tonic-gate prset_fill(void *sp, size_t size)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	while (i != 0)
86*7c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate void
90*7c478bd9Sstevel@tonic-gate prset_empty(void *sp, size_t size)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	while (i != 0)
95*7c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0;
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate void
99*7c478bd9Sstevel@tonic-gate prset_add(void *sp, size_t size, uint_t flag)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
102*7c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate void
106*7c478bd9Sstevel@tonic-gate prset_del(void *sp, size_t size, uint_t flag)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
109*7c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate int
113*7c478bd9Sstevel@tonic-gate prset_ismember(void *sp, size_t size, uint_t flag)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
116*7c478bd9Sstevel@tonic-gate 	    (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * If _libproc_debug is set, printf the debug message to stderr
121*7c478bd9Sstevel@tonic-gate  * with an appropriate prefix.
122*7c478bd9Sstevel@tonic-gate  */
123*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
124*7c478bd9Sstevel@tonic-gate void
125*7c478bd9Sstevel@tonic-gate dprintf(const char *format, ...)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	if (_libproc_debug) {
128*7c478bd9Sstevel@tonic-gate 		va_list alist;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		va_start(alist, format);
131*7c478bd9Sstevel@tonic-gate 		(void) fputs("libproc DEBUG: ", stderr);
132*7c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, alist);
133*7c478bd9Sstevel@tonic-gate 		va_end(alist);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate  * Printf-style error reporting function.  This is used to supplement the error
139*7c478bd9Sstevel@tonic-gate  * return codes from various libproc functions with additional text.  Since we
140*7c478bd9Sstevel@tonic-gate  * are a library, and should not be spewing messages to stderr, we provide a
141*7c478bd9Sstevel@tonic-gate  * default version of this function that does nothing, but by calling this
142*7c478bd9Sstevel@tonic-gate  * function we allow the client program to define its own version of the
143*7c478bd9Sstevel@tonic-gate  * function that will interpose on our empty default.  This may be useful for
144*7c478bd9Sstevel@tonic-gate  * clients that wish to display such messages to the user.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
147*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
148*7c478bd9Sstevel@tonic-gate void
149*7c478bd9Sstevel@tonic-gate Perror_printf(struct ps_prochandle *P, const char *format, ...)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	/* nothing to do here */
152*7c478bd9Sstevel@tonic-gate }
153