xref: /titanic_41/usr/src/lib/libproc/common/Putil.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <limits.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <errno.h>
34 
35 #include "Pcontrol.h"
36 #include "Putil.h"
37 
38 /*
39  * Place the new element on the list prior to the existing element.
40  */
41 void
42 list_link(void *new, void *existing)
43 {
44 	plist_t *p = new;
45 	plist_t *q = existing;
46 
47 	if (q) {
48 		p->list_forw = q;
49 		p->list_back = q->list_back;
50 		q->list_back->list_forw = p;
51 		q->list_back = p;
52 	} else {
53 		p->list_forw = p->list_back = p;
54 	}
55 }
56 
57 /*
58  * Unchain the specified element from a list.
59  */
60 void
61 list_unlink(void *old)
62 {
63 	plist_t *p = old;
64 
65 	if (p->list_forw != p) {
66 		p->list_back->list_forw = p->list_forw;
67 		p->list_forw->list_back = p->list_back;
68 	}
69 	p->list_forw = p->list_back = p;
70 }
71 
72 /*
73  * Routines to manipulate sigset_t, fltset_t, or sysset_t.  These routines
74  * are provided as equivalents for the <sys/procfs.h> macros prfillset,
75  * premptyset, praddset, and prdelset.  These functions are preferable
76  * because they are not macros which rely on using sizeof (*sp), and thus
77  * can be used to create common code to manipulate event sets.  The set
78  * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
79  */
80 void
81 prset_fill(void *sp, size_t size)
82 {
83 	size_t i = size / sizeof (uint32_t);
84 
85 	while (i != 0)
86 		((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
87 }
88 
89 void
90 prset_empty(void *sp, size_t size)
91 {
92 	size_t i = size / sizeof (uint32_t);
93 
94 	while (i != 0)
95 		((uint32_t *)sp)[--i] = (uint32_t)0;
96 }
97 
98 void
99 prset_add(void *sp, size_t size, uint_t flag)
100 {
101 	if (flag - 1 < 32 * size / sizeof (uint32_t))
102 		((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
103 }
104 
105 void
106 prset_del(void *sp, size_t size, uint_t flag)
107 {
108 	if (flag - 1 < 32 * size / sizeof (uint32_t))
109 		((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
110 }
111 
112 int
113 prset_ismember(void *sp, size_t size, uint_t flag)
114 {
115 	return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
116 	    (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
117 }
118 
119 /*
120  * If _libproc_debug is set, printf the debug message to stderr
121  * with an appropriate prefix.
122  */
123 /*PRINTFLIKE1*/
124 void
125 dprintf(const char *format, ...)
126 {
127 	if (_libproc_debug) {
128 		va_list alist;
129 
130 		va_start(alist, format);
131 		(void) fputs("libproc DEBUG: ", stderr);
132 		(void) vfprintf(stderr, format, alist);
133 		va_end(alist);
134 	}
135 }
136 
137 /*
138  * Printf-style error reporting function.  This is used to supplement the error
139  * return codes from various libproc functions with additional text.  Since we
140  * are a library, and should not be spewing messages to stderr, we provide a
141  * default version of this function that does nothing, but by calling this
142  * function we allow the client program to define its own version of the
143  * function that will interpose on our empty default.  This may be useful for
144  * clients that wish to display such messages to the user.
145  */
146 /*ARGSUSED*/
147 /*PRINTFLIKE2*/
148 void
149 Perror_printf(struct ps_prochandle *P, const char *format, ...)
150 {
151 	/* nothing to do here */
152 }
153