xref: /titanic_54/usr/src/lib/libproc/common/Putil.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 /*
23*fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*fa9e4066Sahrens  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <limits.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
367c478bd9Sstevel@tonic-gate #include "Putil.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Place the new element on the list prior to the existing element.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate void
427c478bd9Sstevel@tonic-gate list_link(void *new, void *existing)
437c478bd9Sstevel@tonic-gate {
44*fa9e4066Sahrens 	plist_t *p = new;
45*fa9e4066Sahrens 	plist_t *q = existing;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (q) {
487c478bd9Sstevel@tonic-gate 		p->list_forw = q;
497c478bd9Sstevel@tonic-gate 		p->list_back = q->list_back;
507c478bd9Sstevel@tonic-gate 		q->list_back->list_forw = p;
517c478bd9Sstevel@tonic-gate 		q->list_back = p;
527c478bd9Sstevel@tonic-gate 	} else {
537c478bd9Sstevel@tonic-gate 		p->list_forw = p->list_back = p;
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Unchain the specified element from a list.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate void
617c478bd9Sstevel@tonic-gate list_unlink(void *old)
627c478bd9Sstevel@tonic-gate {
63*fa9e4066Sahrens 	plist_t *p = old;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (p->list_forw != p) {
667c478bd9Sstevel@tonic-gate 		p->list_back->list_forw = p->list_forw;
677c478bd9Sstevel@tonic-gate 		p->list_forw->list_back = p->list_back;
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	p->list_forw = p->list_back = p;
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Routines to manipulate sigset_t, fltset_t, or sysset_t.  These routines
747c478bd9Sstevel@tonic-gate  * are provided as equivalents for the <sys/procfs.h> macros prfillset,
757c478bd9Sstevel@tonic-gate  * premptyset, praddset, and prdelset.  These functions are preferable
767c478bd9Sstevel@tonic-gate  * because they are not macros which rely on using sizeof (*sp), and thus
777c478bd9Sstevel@tonic-gate  * can be used to create common code to manipulate event sets.  The set
787c478bd9Sstevel@tonic-gate  * size must be passed explicitly, e.g. : prset_fill(&set, sizeof (set));
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate void
817c478bd9Sstevel@tonic-gate prset_fill(void *sp, size_t size)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	while (i != 0)
867c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0xFFFFFFFF;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate void
907c478bd9Sstevel@tonic-gate prset_empty(void *sp, size_t size)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	size_t i = size / sizeof (uint32_t);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	while (i != 0)
957c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[--i] = (uint32_t)0;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void
997c478bd9Sstevel@tonic-gate prset_add(void *sp, size_t size, uint_t flag)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
1027c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] |= 1U << ((flag - 1) % 32);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate void
1067c478bd9Sstevel@tonic-gate prset_del(void *sp, size_t size, uint_t flag)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	if (flag - 1 < 32 * size / sizeof (uint32_t))
1097c478bd9Sstevel@tonic-gate 		((uint32_t *)sp)[(flag - 1) / 32] &= ~(1U << ((flag - 1) % 32));
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate int
1137c478bd9Sstevel@tonic-gate prset_ismember(void *sp, size_t size, uint_t flag)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	return ((flag - 1 < 32 * size / sizeof (uint32_t)) &&
1167c478bd9Sstevel@tonic-gate 	    (((uint32_t *)sp)[(flag - 1) / 32] & (1U << ((flag - 1) % 32))));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * If _libproc_debug is set, printf the debug message to stderr
1217c478bd9Sstevel@tonic-gate  * with an appropriate prefix.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1247c478bd9Sstevel@tonic-gate void
1257c478bd9Sstevel@tonic-gate dprintf(const char *format, ...)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	if (_libproc_debug) {
1287c478bd9Sstevel@tonic-gate 		va_list alist;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		va_start(alist, format);
1317c478bd9Sstevel@tonic-gate 		(void) fputs("libproc DEBUG: ", stderr);
1327c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, alist);
1337c478bd9Sstevel@tonic-gate 		va_end(alist);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Printf-style error reporting function.  This is used to supplement the error
1397c478bd9Sstevel@tonic-gate  * return codes from various libproc functions with additional text.  Since we
1407c478bd9Sstevel@tonic-gate  * are a library, and should not be spewing messages to stderr, we provide a
1417c478bd9Sstevel@tonic-gate  * default version of this function that does nothing, but by calling this
1427c478bd9Sstevel@tonic-gate  * function we allow the client program to define its own version of the
1437c478bd9Sstevel@tonic-gate  * function that will interpose on our empty default.  This may be useful for
1447c478bd9Sstevel@tonic-gate  * clients that wish to display such messages to the user.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1477c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1487c478bd9Sstevel@tonic-gate void
1497c478bd9Sstevel@tonic-gate Perror_printf(struct ps_prochandle *P, const char *format, ...)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	/* nothing to do here */
1527c478bd9Sstevel@tonic-gate }
153