xref: /titanic_52/usr/src/lib/abi/apptrace/common/interceptlib.c (revision 47911a7d5f24c2fc37e7b5bcc696fe32e750c16c)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <dlfcn.h>
33 #include <apptrace.h>
34 #include <assert.h>
35 #include "abienv.h"
36 
37 #define	NOTID 0xffffffff
38 
39 /*
40  * This file is meant to contain support functions
41  * for interceptors.  They are built into the auditing
42  * object making the namespace available to "children"
43  * objects.
44  */
45 
46 static lwp_mutex_t abi_stdio_mutex = DEFAULTMUTEX;
47 static volatile thread_t locktid = NOTID;
48 static volatile int count;
49 
50 /* Return true on empty */
51 int
52 is_empty_string(char const *s)
53 {
54 	if (s != NULL && *s != '\0')
55 		return (0);
56 
57 	return (1);
58 }
59 
60 void
61 abilock(sigset_t *mask)
62 {
63 	thread_t tid;
64 
65 	if ((*abi_thr_main)() != -1) {
66 		tid = (*abi_thr_self)();
67 
68 		if (tid == locktid) {
69 			count++;
70 		} else {
71 			(void) _lwp_mutex_lock(&abi_stdio_mutex);
72 			(void) sigprocmask(SIG_BLOCK, &abisigset, mask);
73 			locktid = tid;
74 			count = 1;
75 		}
76 	}
77 }
78 
79 void
80 abiunlock(sigset_t *mask)
81 {
82 	thread_t tid;
83 
84 	(void) fflush(ABISTREAM);
85 
86 	if ((*abi_thr_main)() != -1) {
87 		tid = (*abi_thr_self)();
88 		assert(tid == locktid);
89 		count--;
90 		if (count <= 0) {
91 			count = 0;
92 			locktid = NOTID;
93 			(void) sigprocmask(SIG_SETMASK, mask, NULL);
94 			(void) _lwp_mutex_unlock(&abi_stdio_mutex);
95 		}
96 	}
97 }
98