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