xref: /illumos-gate/usr/src/lib/abi/apptrace/common/interceptlib.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 #include <sys/types.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <dlfcn.h>
31 #include <apptrace.h>
32 #include <assert.h>
33 #include "abienv.h"
34 
35 #define	NOTID 0xffffffff
36 
37 /*
38  * This file is meant to contain support functions
39  * for interceptors.  They are built into the auditing
40  * object making the namespace available to "children"
41  * objects.
42  */
43 
44 static lwp_mutex_t abi_stdio_mutex = DEFAULTMUTEX;
45 static volatile thread_t locktid = NOTID;
46 static volatile int count;
47 
48 /* Return true on empty */
49 int
is_empty_string(char const * s)50 is_empty_string(char const *s)
51 {
52 	if (s != NULL && *s != '\0')
53 		return (0);
54 
55 	return (1);
56 }
57 
58 void
abilock(sigset_t * mask)59 abilock(sigset_t *mask)
60 {
61 	thread_t tid;
62 
63 	if ((*abi_thr_main)() != -1) {
64 		tid = (*abi_thr_self)();
65 
66 		if (tid == locktid) {
67 			count++;
68 		} else {
69 			(void) _lwp_mutex_lock(&abi_stdio_mutex);
70 			(void) sigprocmask(SIG_BLOCK, &abisigset, mask);
71 			locktid = tid;
72 			count = 1;
73 		}
74 	}
75 }
76 
77 void
abiunlock(sigset_t * mask)78 abiunlock(sigset_t *mask)
79 {
80 	thread_t tid;
81 
82 	(void) fflush(ABISTREAM);
83 
84 	if ((*abi_thr_main)() != -1) {
85 		tid = (*abi_thr_self)();
86 		assert(tid == locktid);
87 		count--;
88 		if (count <= 0) {
89 			count = 0;
90 			locktid = NOTID;
91 			(void) sigprocmask(SIG_SETMASK, mask, NULL);
92 			(void) _lwp_mutex_unlock(&abi_stdio_mutex);
93 		}
94 	}
95 }
96