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
prset_fill(void * sp,size_t size)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
prset_empty(void * sp,size_t size)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
prset_add(void * sp,size_t size,uint_t flag)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
prset_del(void * sp,size_t size,uint_t flag)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
prset_ismember(void * sp,size_t size,uint_t flag)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
dprintf(const char * format,...)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
Perror_printf(struct ps_prochandle * P,const char * format,...)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
Pdefault_ssizet()125 Pdefault_ssizet()
126 {
127 errno = ENOTSUP;
128 return (-1);
129 }
130
131 static int
Pdefault_int()132 Pdefault_int()
133 {
134 errno = ENOTSUP;
135 return (-1);
136 }
137
138 static void
Pdefault_void()139 Pdefault_void()
140 {
141 }
142
143 static void *
Pdefault_voidp()144 Pdefault_voidp()
145 {
146 errno = ENOTSUP;
147 return (NULL);
148 }
149
150 static const ps_ops_t P_default_ops = {
151 .pop_pread = (pop_pread_t)Pdefault_ssizet,
152 .pop_pwrite = (pop_pwrite_t)Pdefault_ssizet,
153 .pop_read_maps = (pop_read_maps_t)Pdefault_int,
154 .pop_read_aux = (pop_read_aux_t)Pdefault_void,
155 .pop_cred = (pop_cred_t)Pdefault_int,
156 .pop_priv = (pop_priv_t)Pdefault_int,
157 .pop_psinfo = (pop_psinfo_t)Pdefault_voidp,
158 .pop_status = (pop_status_t)Pdefault_void,
159 .pop_lstatus = (pop_lstatus_t)Pdefault_voidp,
160 .pop_lpsinfo = (pop_lpsinfo_t)Pdefault_voidp,
161 .pop_fini = (pop_fini_t)Pdefault_void,
162 .pop_platform = (pop_platform_t)Pdefault_voidp,
163 .pop_uname = (pop_uname_t)Pdefault_int,
164 .pop_zonename = (pop_zonename_t)Pdefault_voidp,
165 .pop_execname = (pop_execname_t)Pdefault_voidp,
166 .pop_secflags = (pop_secflags_t)Pdefault_int,
167 .pop_cwd = (pop_cwd_t)Pdefault_int,
168 #if defined(__i386) || defined(__amd64)
169 .pop_ldt = (pop_ldt_t)Pdefault_int
170 #endif
171 };
172
173 /*
174 * Initialize the destination ops vector with functions from the source.
175 * Functions which are NULL in the source ops vector are set to corresponding
176 * default function in the destination vector.
177 */
178 void
Pinit_ops(ps_ops_t * dst,const ps_ops_t * src)179 Pinit_ops(ps_ops_t *dst, const ps_ops_t *src)
180 {
181 *dst = P_default_ops;
182
183 if (src->pop_pread != NULL)
184 dst->pop_pread = src->pop_pread;
185 if (src->pop_pwrite != NULL)
186 dst->pop_pwrite = src->pop_pwrite;
187 if (src->pop_read_maps != NULL)
188 dst->pop_read_maps = src->pop_read_maps;
189 if (src->pop_read_aux != NULL)
190 dst->pop_read_aux = src->pop_read_aux;
191 if (src->pop_cred != NULL)
192 dst->pop_cred = src->pop_cred;
193 if (src->pop_priv != NULL)
194 dst->pop_priv = src->pop_priv;
195 if (src->pop_psinfo != NULL)
196 dst->pop_psinfo = src->pop_psinfo;
197 if (src->pop_status != NULL)
198 dst->pop_status = src->pop_status;
199 if (src->pop_lstatus != NULL)
200 dst->pop_lstatus = src->pop_lstatus;
201 if (src->pop_lpsinfo != NULL)
202 dst->pop_lpsinfo = src->pop_lpsinfo;
203 if (src->pop_fini != NULL)
204 dst->pop_fini = src->pop_fini;
205 if (src->pop_platform != NULL)
206 dst->pop_platform = src->pop_platform;
207 if (src->pop_uname != NULL)
208 dst->pop_uname = src->pop_uname;
209 if (src->pop_zonename != NULL)
210 dst->pop_zonename = src->pop_zonename;
211 if (src->pop_execname != NULL)
212 dst->pop_execname = src->pop_execname;
213 if (src->pop_secflags != NULL)
214 dst->pop_secflags = src->pop_secflags;
215 if (src->pop_cwd != NULL)
216 dst->pop_cwd = src->pop_cwd;
217 #if defined(__i386) || defined(__amd64)
218 if (src->pop_ldt != NULL)
219 dst->pop_ldt = src->pop_ldt;
220 #endif
221 }
222