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