xref: /onnv-gate/usr/src/lib/abi/apptrace/common/interceptlib.c (revision 3153:009bcec97912)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3153Sdg199075  * Common Development and Distribution License (the "License").
6*3153Sdg199075  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3153Sdg199075  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <dlfcn.h>
330Sstevel@tonic-gate #include <apptrace.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include "abienv.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	NOTID 0xffffffff
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * This file is meant to contain support functions
410Sstevel@tonic-gate  * for interceptors.  They are built into the auditing
420Sstevel@tonic-gate  * object making the namespace available to "children"
430Sstevel@tonic-gate  * objects.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static lwp_mutex_t abi_stdio_mutex = DEFAULTMUTEX;
470Sstevel@tonic-gate static volatile thread_t locktid = NOTID;
480Sstevel@tonic-gate static volatile int count;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* Return true on empty */
510Sstevel@tonic-gate int
is_empty_string(char const * s)520Sstevel@tonic-gate is_empty_string(char const *s)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	if (s != NULL && *s != '\0')
550Sstevel@tonic-gate 		return (0);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	return (1);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate void
abilock(sigset_t * mask)610Sstevel@tonic-gate abilock(sigset_t *mask)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	thread_t tid;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if ((*abi_thr_main)() != -1) {
660Sstevel@tonic-gate 		tid = (*abi_thr_self)();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 		if (tid == locktid) {
690Sstevel@tonic-gate 			count++;
700Sstevel@tonic-gate 		} else {
710Sstevel@tonic-gate 			(void) _lwp_mutex_lock(&abi_stdio_mutex);
720Sstevel@tonic-gate 			(void) sigprocmask(SIG_BLOCK, &abisigset, mask);
730Sstevel@tonic-gate 			locktid = tid;
740Sstevel@tonic-gate 			count = 1;
750Sstevel@tonic-gate 		}
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate void
abiunlock(sigset_t * mask)800Sstevel@tonic-gate abiunlock(sigset_t *mask)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	thread_t tid;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	(void) fflush(ABISTREAM);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if ((*abi_thr_main)() != -1) {
870Sstevel@tonic-gate 		tid = (*abi_thr_self)();
880Sstevel@tonic-gate 		assert(tid == locktid);
890Sstevel@tonic-gate 		count--;
900Sstevel@tonic-gate 		if (count <= 0) {
910Sstevel@tonic-gate 			count = 0;
920Sstevel@tonic-gate 			locktid = NOTID;
930Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, mask, NULL);
940Sstevel@tonic-gate 			(void) _lwp_mutex_unlock(&abi_stdio_mutex);
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate }
98