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