xref: /illumos-gate/usr/src/lib/libproc/common/Putil.c (revision dd72704bd9e794056c558153663c739e2012d721)
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  * Copyright (c) 2013 by Delphix. All rights reserved.
28  */
29 
30 #include <limits.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <errno.h>
35 
36 #include "Pcontrol.h"
37 #include "Putil.h"
38 
39 /*
40  * Routines to manipulate sigset_t, fltset_t, or sysset_t.  These routines
41  * are provided as equivalents for the <sys/procfs.h> macros prfillset,
42  * premptyset, praddset, and prdelset.  These functions are preferable
43  * because they are not macros which rely on using sizeof (*sp), and thus
44  * can be used to create common code to manipulate event sets.  The set
45  * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
46  */
47 void
48 prset_fill(void *sp, size_t size)
49 {
50 	size_t i = size / sizeof (uint32_t);
51 
52 	while (i != 0)
53 		((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
54 }
55 
56 void
57 prset_empty(void *sp, size_t size)
58 {
59 	size_t i = size / sizeof (uint32_t);
60 
61 	while (i != 0)
62 		((uint32_t *)sp)[--i] = (uint32_t)0;
63 }
64 
65 void
66 prset_add(void *sp, size_t size, uint_t flag)
67 {
68 	if (flag - 1 < 32 * size / sizeof (uint32_t))
69 		((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
70 }
71 
72 void
73 prset_del(void *sp, size_t size, uint_t flag)
74 {
75 	if (flag - 1 < 32 * size / sizeof (uint32_t))
76 		((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
77 }
78 
79 int
80 prset_ismember(void *sp, size_t size, uint_t flag)
81 {
82 	return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
83 	    (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
84 }
85 
86 /*
87  * If _libproc_debug is set, printf the debug message to stderr
88  * with an appropriate prefix.
89  */
90 /*PRINTFLIKE1*/
91 void
92 dprintf(const char *format, ...)
93 {
94 	if (_libproc_debug) {
95 		va_list alist;
96 
97 		va_start(alist, format);
98 		(void) fputs("libproc DEBUG: ", stderr);
99 		(void) vfprintf(stderr, format, alist);
100 		va_end(alist);
101 	}
102 }
103 
104 /*
105  * Printf-style error reporting function.  This is used to supplement the error
106  * return codes from various libproc functions with additional text.  Since we
107  * are a library, and should not be spewing messages to stderr, we provide a
108  * default version of this function that does nothing, but by calling this
109  * function we allow the client program to define its own version of the
110  * function that will interpose on our empty default.  This may be useful for
111  * clients that wish to display such messages to the user.
112  */
113 /*ARGSUSED*/
114 /*PRINTFLIKE2*/
115 void
116 Perror_printf(struct ps_prochandle *P, const char *format, ...)
117 {
118 	/* nothing to do here */
119 }
120 
121 /*
122  * Default operations.
123  */
124 static ssize_t
125 Pdefault_ssizet()
126 {
127 	return (-1);
128 }
129 
130 static int
131 Pdefault_int()
132 {
133 	return (-1);
134 }
135 
136 static void
137 Pdefault_void()
138 {
139 }
140 
141 static void *
142 Pdefault_voidp()
143 {
144 	return (NULL);
145 }
146 
147 static const ps_ops_t P_default_ops = {
148 	.pop_pread	= (pop_pread_t)Pdefault_ssizet,
149 	.pop_pwrite	= (pop_pwrite_t)Pdefault_ssizet,
150 	.pop_read_maps	= (pop_read_maps_t)Pdefault_int,
151 	.pop_read_aux	= (pop_read_aux_t)Pdefault_void,
152 	.pop_cred	= (pop_cred_t)Pdefault_int,
153 	.pop_priv	= (pop_priv_t)Pdefault_int,
154 	.pop_psinfo	= (pop_psinfo_t)Pdefault_voidp,
155 	.pop_status	= (pop_status_t)Pdefault_void,
156 	.pop_lstatus	= (pop_lstatus_t)Pdefault_voidp,
157 	.pop_lpsinfo	= (pop_lpsinfo_t)Pdefault_voidp,
158 	.pop_fini	= (pop_fini_t)Pdefault_void,
159 	.pop_platform	= (pop_platform_t)Pdefault_voidp,
160 	.pop_uname	= (pop_uname_t)Pdefault_int,
161 	.pop_zonename	= (pop_zonename_t)Pdefault_voidp,
162 	.pop_execname	= (pop_execname_t)Pdefault_voidp,
163 	.pop_secflags	= (pop_secflags_t)Pdefault_int,
164 #if defined(__i386) || defined(__amd64)
165 	.pop_ldt	= (pop_ldt_t)Pdefault_int
166 #endif
167 };
168 
169 /*
170  * Initialize the destination ops vector with functions from the source.
171  * Functions which are NULL in the source ops vector are set to corresponding
172  * default function in the destination vector.
173  */
174 void
175 Pinit_ops(ps_ops_t *dst, const ps_ops_t *src)
176 {
177 	*dst = P_default_ops;
178 
179 	if (src->pop_pread != NULL)
180 		dst->pop_pread = src->pop_pread;
181 	if (src->pop_pwrite != NULL)
182 		dst->pop_pwrite = src->pop_pwrite;
183 	if (src->pop_read_maps != NULL)
184 		dst->pop_read_maps = src->pop_read_maps;
185 	if (src->pop_read_aux != NULL)
186 		dst->pop_read_aux = src->pop_read_aux;
187 	if (src->pop_cred != NULL)
188 		dst->pop_cred = src->pop_cred;
189 	if (src->pop_priv != NULL)
190 		dst->pop_priv = src->pop_priv;
191 	if (src->pop_psinfo != NULL)
192 		dst->pop_psinfo = src->pop_psinfo;
193 	if (src->pop_status != NULL)
194 		dst->pop_status = src->pop_status;
195 	if (src->pop_lstatus != NULL)
196 		dst->pop_lstatus = src->pop_lstatus;
197 	if (src->pop_lpsinfo != NULL)
198 		dst->pop_lpsinfo = src->pop_lpsinfo;
199 	if (src->pop_fini != NULL)
200 		dst->pop_fini = src->pop_fini;
201 	if (src->pop_platform != NULL)
202 		dst->pop_platform = src->pop_platform;
203 	if (src->pop_uname != NULL)
204 		dst->pop_uname = src->pop_uname;
205 	if (src->pop_zonename != NULL)
206 		dst->pop_zonename = src->pop_zonename;
207 	if (src->pop_execname != NULL)
208 		dst->pop_execname = src->pop_execname;
209 	if (src->pop_secflags != NULL)
210 		dst->pop_secflags = src->pop_secflags;
211 #if defined(__i386) || defined(__amd64)
212 	if (src->pop_ldt != NULL)
213 		dst->pop_ldt = src->pop_ldt;
214 #endif
215 }
216