xref: /titanic_44/usr/src/lib/libtnfctl/close.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Interface to close a tnfctl handle
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "tnfctl_int.h"
33*7c478bd9Sstevel@tonic-gate #include "kernel_int.h"
34*7c478bd9Sstevel@tonic-gate #include "dbg.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /* for bug 1253419 - guard against multiple tracing */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate extern mutex_t		_tnfctl_internalguard_lock;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Close a tnfctl handle - close any open fds and free up any memory
46*7c478bd9Sstevel@tonic-gate  * that was allocated.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t
tnfctl_close(tnfctl_handle_t * hdl,tnfctl_targ_op_t action)49*7c478bd9Sstevel@tonic-gate tnfctl_close(tnfctl_handle_t *hdl, tnfctl_targ_op_t action)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	tnfctl_errcode_t	prexstat;
52*7c478bd9Sstevel@tonic-gate 	prb_status_t	prbstat;
53*7c478bd9Sstevel@tonic-gate 	prb_proc_ctl_t	*proc_p;
54*7c478bd9Sstevel@tonic-gate 	tnfctl_probe_t	*probe_hdl, *tmp_hdl;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (hdl == NULL)
57*7c478bd9Sstevel@tonic-gate 		return (TNFCTL_ERR_NONE);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (hdl->mode == KERNEL_MODE) {
60*7c478bd9Sstevel@tonic-gate 		prexstat = _tnfctl_prbk_close(hdl);
61*7c478bd9Sstevel@tonic-gate 		if (prexstat)
62*7c478bd9Sstevel@tonic-gate 			return (prexstat);
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if (hdl->mode == INTERNAL_MODE) {
66*7c478bd9Sstevel@tonic-gate 		_tnfctl_internal_releaselock();
67*7c478bd9Sstevel@tonic-gate 	} else if (hdl->mode != KERNEL_MODE) {
68*7c478bd9Sstevel@tonic-gate 		_tnfctl_external_releaselock(hdl);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	_tnfctl_free_objs_and_probes(hdl);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/* free probe handles */
74*7c478bd9Sstevel@tonic-gate 	probe_hdl = hdl->probe_handle_list_head;
75*7c478bd9Sstevel@tonic-gate 	while (probe_hdl != NULL) {
76*7c478bd9Sstevel@tonic-gate 		/* call the destructor function for client probe data */
77*7c478bd9Sstevel@tonic-gate 		if (hdl->destroy_func)
78*7c478bd9Sstevel@tonic-gate 			hdl->destroy_func(probe_hdl->client_registered_data);
79*7c478bd9Sstevel@tonic-gate 		tmp_hdl = probe_hdl;
80*7c478bd9Sstevel@tonic-gate 		probe_hdl = probe_hdl->next;
81*7c478bd9Sstevel@tonic-gate 		free(tmp_hdl);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	hdl->probe_handle_list_head = NULL;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (hdl->mode != DIRECT_MODE) {
86*7c478bd9Sstevel@tonic-gate 		/* indirect, internal, or kernel mode */
87*7c478bd9Sstevel@tonic-gate 		free(hdl);
88*7c478bd9Sstevel@tonic-gate 		return (TNFCTL_ERR_NONE);
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	/* DIRECT_MODE */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	proc_p = hdl->proc_p;
94*7c478bd9Sstevel@tonic-gate 	if (proc_p == NULL) {
95*7c478bd9Sstevel@tonic-gate 		free(hdl);
96*7c478bd9Sstevel@tonic-gate 		return (TNFCTL_ERR_NONE);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	switch (action) {
100*7c478bd9Sstevel@tonic-gate 	case TNFCTL_TARG_DEFAULT:
101*7c478bd9Sstevel@tonic-gate 		break;
102*7c478bd9Sstevel@tonic-gate 	case TNFCTL_TARG_KILL:
103*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setklc(proc_p, B_TRUE);
104*7c478bd9Sstevel@tonic-gate 		if (prbstat)
105*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
106*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setrlc(proc_p, B_FALSE);
107*7c478bd9Sstevel@tonic-gate 		if (prbstat)
108*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
109*7c478bd9Sstevel@tonic-gate 		break;
110*7c478bd9Sstevel@tonic-gate 	case TNFCTL_TARG_RESUME:
111*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setklc(proc_p, B_FALSE);
112*7c478bd9Sstevel@tonic-gate 		if (prbstat)
113*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
114*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setrlc(proc_p, B_TRUE);
115*7c478bd9Sstevel@tonic-gate 		if (prbstat)
116*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
117*7c478bd9Sstevel@tonic-gate 		break;
118*7c478bd9Sstevel@tonic-gate 	case TNFCTL_TARG_SUSPEND:
119*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setklc(proc_p, B_FALSE);
120*7c478bd9Sstevel@tonic-gate 		if (prbstat)
121*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
122*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_setrlc(proc_p, B_FALSE);
123*7c478bd9Sstevel@tonic-gate 		if (prbstat)
124*7c478bd9Sstevel@tonic-gate 			return (_tnfctl_map_to_errcode(prbstat));
125*7c478bd9Sstevel@tonic-gate 		break;
126*7c478bd9Sstevel@tonic-gate 	default:
127*7c478bd9Sstevel@tonic-gate 		return (TNFCTL_ERR_BADARG);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	prbstat = prb_proc_close(proc_p);
130*7c478bd9Sstevel@tonic-gate 	free(hdl);
131*7c478bd9Sstevel@tonic-gate 	return (_tnfctl_map_to_errcode(prbstat));
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_internal_releaselock()135*7c478bd9Sstevel@tonic-gate _tnfctl_internal_releaselock()
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	mutex_lock(&_tnfctl_internalguard_lock);
138*7c478bd9Sstevel@tonic-gate 	_tnfctl_internal_tracing_flag = 0;
139*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&_tnfctl_internalguard_lock);
140*7c478bd9Sstevel@tonic-gate 	return (TNFCTL_ERR_NONE);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_external_releaselock(tnfctl_handle_t * hdl)144*7c478bd9Sstevel@tonic-gate _tnfctl_external_releaselock(tnfctl_handle_t *hdl)
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate 	tnfctl_errcode_t	prexstat;
147*7c478bd9Sstevel@tonic-gate 	prb_status_t		prbstat;
148*7c478bd9Sstevel@tonic-gate 	uintptr_t		targ_symbol_ptr;
149*7c478bd9Sstevel@tonic-gate 	pid_t			pidzero = 0;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	prexstat = _tnfctl_sym_find(hdl, TNFCTL_EXTERNAL_TRACEDPID,
152*7c478bd9Sstevel@tonic-gate 	    &targ_symbol_ptr);
153*7c478bd9Sstevel@tonic-gate 	if (prexstat) {
154*7c478bd9Sstevel@tonic-gate 	return (prexstat);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	prbstat = hdl->p_write(hdl->proc_p, targ_symbol_ptr,
157*7c478bd9Sstevel@tonic-gate 	&pidzero, sizeof (pidzero));
158*7c478bd9Sstevel@tonic-gate 	if (prbstat) {
159*7c478bd9Sstevel@tonic-gate 	return (_tnfctl_map_to_errcode(prbstat));
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 	return (TNFCTL_ERR_NONE);
162*7c478bd9Sstevel@tonic-gate }
163