17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23fa9e4066Sahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24fa9e4066Sahrens * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
26*2a12f85aSJeremy Jones /*
27*2a12f85aSJeremy Jones * Copyright (c) 2013 by Delphix. All rights reserved.
28*2a12f85aSJeremy Jones */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
377c478bd9Sstevel@tonic-gate #include "Putil.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * Place the new element on the list prior to the existing element.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate void
list_link(void * new,void * existing)437c478bd9Sstevel@tonic-gate list_link(void *new, void *existing)
447c478bd9Sstevel@tonic-gate {
45fa9e4066Sahrens plist_t *p = new;
46fa9e4066Sahrens plist_t *q = existing;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate if (q) {
497c478bd9Sstevel@tonic-gate p->list_forw = q;
507c478bd9Sstevel@tonic-gate p->list_back = q->list_back;
517c478bd9Sstevel@tonic-gate q->list_back->list_forw = p;
527c478bd9Sstevel@tonic-gate q->list_back = p;
537c478bd9Sstevel@tonic-gate } else {
547c478bd9Sstevel@tonic-gate p->list_forw = p->list_back = p;
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * Unchain the specified element from a list.
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate void
list_unlink(void * old)627c478bd9Sstevel@tonic-gate list_unlink(void *old)
637c478bd9Sstevel@tonic-gate {
64fa9e4066Sahrens plist_t *p = old;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate if (p->list_forw != p) {
677c478bd9Sstevel@tonic-gate p->list_back->list_forw = p->list_forw;
687c478bd9Sstevel@tonic-gate p->list_forw->list_back = p->list_back;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate p->list_forw = p->list_back = p;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * Routines to manipulate sigset_t, fltset_t, or sysset_t. These routines
757c478bd9Sstevel@tonic-gate * are provided as equivalents for the <sys/procfs.h> macros prfillset,
767c478bd9Sstevel@tonic-gate * premptyset, praddset, and prdelset. These functions are preferable
777c478bd9Sstevel@tonic-gate * because they are not macros which rely on using sizeof (*sp), and thus
787c478bd9Sstevel@tonic-gate * can be used to create common code to manipulate event sets. The set
797c478bd9Sstevel@tonic-gate * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate void
prset_fill(void * sp,size_t size)827c478bd9Sstevel@tonic-gate prset_fill(void *sp, size_t size)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate size_t i = size / sizeof (uint32_t);
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate while (i != 0)
877c478bd9Sstevel@tonic-gate ((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate void
prset_empty(void * sp,size_t size)917c478bd9Sstevel@tonic-gate prset_empty(void *sp, size_t size)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate size_t i = size / sizeof (uint32_t);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate while (i != 0)
967c478bd9Sstevel@tonic-gate ((uint32_t *)sp)[--i] = (uint32_t)0;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate void
prset_add(void * sp,size_t size,uint_t flag)1007c478bd9Sstevel@tonic-gate prset_add(void *sp, size_t size, uint_t flag)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate if (flag - 1 < 32 * size / sizeof (uint32_t))
1037c478bd9Sstevel@tonic-gate ((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate void
prset_del(void * sp,size_t size,uint_t flag)1077c478bd9Sstevel@tonic-gate prset_del(void *sp, size_t size, uint_t flag)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate if (flag - 1 < 32 * size / sizeof (uint32_t))
1107c478bd9Sstevel@tonic-gate ((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate int
prset_ismember(void * sp,size_t size,uint_t flag)1147c478bd9Sstevel@tonic-gate prset_ismember(void *sp, size_t size, uint_t flag)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
1177c478bd9Sstevel@tonic-gate (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * If _libproc_debug is set, printf the debug message to stderr
1227c478bd9Sstevel@tonic-gate * with an appropriate prefix.
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1257c478bd9Sstevel@tonic-gate void
dprintf(const char * format,...)1267c478bd9Sstevel@tonic-gate dprintf(const char *format, ...)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate if (_libproc_debug) {
1297c478bd9Sstevel@tonic-gate va_list alist;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate va_start(alist, format);
1327c478bd9Sstevel@tonic-gate (void) fputs("libproc DEBUG: ", stderr);
1337c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
1347c478bd9Sstevel@tonic-gate va_end(alist);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Printf-style error reporting function. This is used to supplement the error
1407c478bd9Sstevel@tonic-gate * return codes from various libproc functions with additional text. Since we
1417c478bd9Sstevel@tonic-gate * are a library, and should not be spewing messages to stderr, we provide a
1427c478bd9Sstevel@tonic-gate * default version of this function that does nothing, but by calling this
1437c478bd9Sstevel@tonic-gate * function we allow the client program to define its own version of the
1447c478bd9Sstevel@tonic-gate * function that will interpose on our empty default. This may be useful for
1457c478bd9Sstevel@tonic-gate * clients that wish to display such messages to the user.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1487c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1497c478bd9Sstevel@tonic-gate void
Perror_printf(struct ps_prochandle * P,const char * format,...)1507c478bd9Sstevel@tonic-gate Perror_printf(struct ps_prochandle *P, const char *format, ...)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate /* nothing to do here */
1537c478bd9Sstevel@tonic-gate }
154*2a12f85aSJeremy Jones
155*2a12f85aSJeremy Jones /*
156*2a12f85aSJeremy Jones * Default operations.
157*2a12f85aSJeremy Jones */
158*2a12f85aSJeremy Jones static ssize_t
Pdefault_ssizet()159*2a12f85aSJeremy Jones Pdefault_ssizet()
160*2a12f85aSJeremy Jones {
161*2a12f85aSJeremy Jones return (-1);
162*2a12f85aSJeremy Jones }
163*2a12f85aSJeremy Jones
164*2a12f85aSJeremy Jones static int
Pdefault_int()165*2a12f85aSJeremy Jones Pdefault_int()
166*2a12f85aSJeremy Jones {
167*2a12f85aSJeremy Jones return (-1);
168*2a12f85aSJeremy Jones }
169*2a12f85aSJeremy Jones
170*2a12f85aSJeremy Jones static void
Pdefault_void()171*2a12f85aSJeremy Jones Pdefault_void()
172*2a12f85aSJeremy Jones {
173*2a12f85aSJeremy Jones }
174*2a12f85aSJeremy Jones
175*2a12f85aSJeremy Jones static void *
Pdefault_voidp()176*2a12f85aSJeremy Jones Pdefault_voidp()
177*2a12f85aSJeremy Jones {
178*2a12f85aSJeremy Jones return (NULL);
179*2a12f85aSJeremy Jones }
180*2a12f85aSJeremy Jones
181*2a12f85aSJeremy Jones static const ps_ops_t P_default_ops = {
182*2a12f85aSJeremy Jones .pop_pread = (pop_pread_t)Pdefault_ssizet,
183*2a12f85aSJeremy Jones .pop_pwrite = (pop_pwrite_t)Pdefault_ssizet,
184*2a12f85aSJeremy Jones .pop_read_maps = (pop_read_maps_t)Pdefault_int,
185*2a12f85aSJeremy Jones .pop_read_aux = (pop_read_aux_t)Pdefault_void,
186*2a12f85aSJeremy Jones .pop_cred = (pop_cred_t)Pdefault_int,
187*2a12f85aSJeremy Jones .pop_priv = (pop_priv_t)Pdefault_int,
188*2a12f85aSJeremy Jones .pop_psinfo = (pop_psinfo_t)Pdefault_voidp,
189*2a12f85aSJeremy Jones .pop_status = (pop_status_t)Pdefault_void,
190*2a12f85aSJeremy Jones .pop_lstatus = (pop_lstatus_t)Pdefault_voidp,
191*2a12f85aSJeremy Jones .pop_lpsinfo = (pop_lpsinfo_t)Pdefault_voidp,
192*2a12f85aSJeremy Jones .pop_fini = (pop_fini_t)Pdefault_void,
193*2a12f85aSJeremy Jones .pop_platform = (pop_platform_t)Pdefault_voidp,
194*2a12f85aSJeremy Jones .pop_uname = (pop_uname_t)Pdefault_int,
195*2a12f85aSJeremy Jones .pop_zonename = (pop_zonename_t)Pdefault_voidp,
196*2a12f85aSJeremy Jones .pop_execname = (pop_execname_t)Pdefault_voidp,
197*2a12f85aSJeremy Jones #if defined(__i386) || defined(__amd64)
198*2a12f85aSJeremy Jones .pop_ldt = (pop_ldt_t)Pdefault_int
199*2a12f85aSJeremy Jones #endif
200*2a12f85aSJeremy Jones };
201*2a12f85aSJeremy Jones
202*2a12f85aSJeremy Jones /*
203*2a12f85aSJeremy Jones * Initialize the destination ops vector with functions from the source.
204*2a12f85aSJeremy Jones * Functions which are NULL in the source ops vector are set to corresponding
205*2a12f85aSJeremy Jones * default function in the destination vector.
206*2a12f85aSJeremy Jones */
207*2a12f85aSJeremy Jones void
Pinit_ops(ps_ops_t * dst,const ps_ops_t * src)208*2a12f85aSJeremy Jones Pinit_ops(ps_ops_t *dst, const ps_ops_t *src)
209*2a12f85aSJeremy Jones {
210*2a12f85aSJeremy Jones *dst = P_default_ops;
211*2a12f85aSJeremy Jones
212*2a12f85aSJeremy Jones if (src->pop_pread != NULL)
213*2a12f85aSJeremy Jones dst->pop_pread = src->pop_pread;
214*2a12f85aSJeremy Jones if (src->pop_pwrite != NULL)
215*2a12f85aSJeremy Jones dst->pop_pwrite = src->pop_pwrite;
216*2a12f85aSJeremy Jones if (src->pop_read_maps != NULL)
217*2a12f85aSJeremy Jones dst->pop_read_maps = src->pop_read_maps;
218*2a12f85aSJeremy Jones if (src->pop_read_aux != NULL)
219*2a12f85aSJeremy Jones dst->pop_read_aux = src->pop_read_aux;
220*2a12f85aSJeremy Jones if (src->pop_cred != NULL)
221*2a12f85aSJeremy Jones dst->pop_cred = src->pop_cred;
222*2a12f85aSJeremy Jones if (src->pop_priv != NULL)
223*2a12f85aSJeremy Jones dst->pop_priv = src->pop_priv;
224*2a12f85aSJeremy Jones if (src->pop_psinfo != NULL)
225*2a12f85aSJeremy Jones dst->pop_psinfo = src->pop_psinfo;
226*2a12f85aSJeremy Jones if (src->pop_status != NULL)
227*2a12f85aSJeremy Jones dst->pop_status = src->pop_status;
228*2a12f85aSJeremy Jones if (src->pop_lstatus != NULL)
229*2a12f85aSJeremy Jones dst->pop_lstatus = src->pop_lstatus;
230*2a12f85aSJeremy Jones if (src->pop_lpsinfo != NULL)
231*2a12f85aSJeremy Jones dst->pop_lpsinfo = src->pop_lpsinfo;
232*2a12f85aSJeremy Jones if (src->pop_fini != NULL)
233*2a12f85aSJeremy Jones dst->pop_fini = src->pop_fini;
234*2a12f85aSJeremy Jones if (src->pop_platform != NULL)
235*2a12f85aSJeremy Jones dst->pop_platform = src->pop_platform;
236*2a12f85aSJeremy Jones if (src->pop_uname != NULL)
237*2a12f85aSJeremy Jones dst->pop_uname = src->pop_uname;
238*2a12f85aSJeremy Jones if (src->pop_zonename != NULL)
239*2a12f85aSJeremy Jones dst->pop_zonename = src->pop_zonename;
240*2a12f85aSJeremy Jones if (src->pop_execname != NULL)
241*2a12f85aSJeremy Jones dst->pop_execname = src->pop_execname;
242*2a12f85aSJeremy Jones #if defined(__i386) || defined(__amd64)
243*2a12f85aSJeremy Jones if (src->pop_ldt != NULL)
244*2a12f85aSJeremy Jones dst->pop_ldt = src->pop_ldt;
245*2a12f85aSJeremy Jones #endif
246*2a12f85aSJeremy Jones }
247