1178476Sjb#
2178476Sjb# CDDL HEADER START
3178476Sjb#
4178476Sjb# The contents of this file are subject to the terms of the
5178476Sjb# Common Development and Distribution License (the "License").
6178476Sjb# You may not use this file except in compliance with the License.
7178476Sjb#
8178476Sjb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178476Sjb# or http://www.opensolaris.org/os/licensing.
10178476Sjb# See the License for the specific language governing permissions
11178476Sjb# and limitations under the License.
12178476Sjb#
13178476Sjb# When distributing Covered Code, include this CDDL HEADER in each
14178476Sjb# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178476Sjb# If applicable, add the following below this CDDL HEADER, with the
16178476Sjb# fields enclosed by brackets "[]" replaced with your own identifying
17178476Sjb# information: Portions Copyright [yyyy] [name of copyright owner]
18178476Sjb#
19178476Sjb# CDDL HEADER END
20178476Sjb#
21178476Sjb
22178476Sjb#
23178476Sjb# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb# Use is subject to license terms.
25178476Sjb#
26178476Sjb# ident	"%Z%%M%	%I%	%E% SMI"
27178476Sjb
28178476Sjbunload()
29178476Sjb{
30178476Sjb	#
31178476Sjb	# Get the list of services whose processes have USDT probes.  Ideally
32178476Sjb	# it would be possible to unload the fasttrap provider while USDT
33178476Sjb	# probes exist -- once that fix is integrated, this hack can go away
34178476Sjb	# We create two lists -- one of regular SMF services and one of legacy
35178476Sjb	# services -- since each must be enabled and disabled using a specific
36178476Sjb	# mechanism.
37178476Sjb	#
38178476Sjb	pids=$(dtrace -l | \
39178476Sjb	    perl -ne 'print "$1\n" if (/^\s*\S+\s+\S*\D(\d+)\s+/);' | \
40178476Sjb	    sort | uniq | tr '\n' ',')
41178476Sjb
42178476Sjb	ctids=$(ps -p $pids -o ctid | tail +2 | sort | uniq)
43178476Sjb	svcs=
44178476Sjb	lrcs=
45178476Sjb
46178476Sjb	for ct in $ctids
47178476Sjb	do
48178476Sjb		line=$(svcs -o fmri,ctid | grep " $ct\$")
49178476Sjb		svc=$(echo $line | cut -d' ' -f1)
50178476Sjb
51178476Sjb		if [[ $(svcs -Ho STA $svc) == "LRC" ]]; then
52178476Sjb			lrc=$(svcs -Ho SVC $svc | tr _ '?')
53178476Sjb			lrcs="$lrcs $lrc"
54178476Sjb		else
55178476Sjb			svcs="$svcs $svc"
56178476Sjb	fi
57178476Sjb	done
58178476Sjb
59178476Sjb	for svc in $svcs
60178476Sjb	do
61178476Sjb		svcadm disable -ts $svc
62178476Sjb	done
63178476Sjb
64178476Sjb	for lrc in $lrcs
65178476Sjb	do
66178476Sjb		#
67178476Sjb		# Does it seem a little paternalistic that lsvcrun requires
68178476Sjb		# this environment variable to be set? I'd say so...
69178476Sjb		#
70178476Sjb		SMF_RESTARTER=svc:/system/svc/restarter:default \
71178476Sjb		    /lib/svc/bin/lsvcrun $lrc stop
72178476Sjb	done
73178476Sjb
74178476Sjb	modunload -i 0
75178476Sjb	modunload -i 0
76178476Sjb	modunload -i 0
77178476Sjb	modinfo | grep dtrace
78178476Sjb	success=$?
79178476Sjb
80178476Sjb	for svc in $svcs
81178476Sjb	do
82178476Sjb		svcadm enable -ts $svc
83178476Sjb	done
84178476Sjb
85178476Sjb	for lrc in $lrcs
86178476Sjb	do
87178476Sjb		SMF_RESTARTER=svc:/system/svc/restarter:default \
88178476Sjb		    /lib/svc/bin/lsvcrun $lrc start
89178476Sjb	done
90178476Sjb
91178476Sjb	if [ ! $success ]; then
92178476Sjb		echo $tst: could not unload dtrace
93178476Sjb		exit 1
94178476Sjb	fi
95178476Sjb}
96178476Sjb
97178476Sjbscript1()
98178476Sjb{
99178476Sjb	$dtrace -s /dev/stdin <<EOF
100178476Sjb	syscall:::entry
101178476Sjb	/pid != $ppid/
102178476Sjb	{
103178476Sjb		@a[probefunc] = count();
104178476Sjb	}
105178476Sjb
106178476Sjb	tick-1sec
107178476Sjb	/i++ == 5/
108178476Sjb	{
109178476Sjb		exit(0);
110178476Sjb	}
111178476SjbEOF
112178476Sjb}
113178476Sjb
114178476Sjbscript2()
115178476Sjb{
116178476Sjb	$dtrace -s /dev/stdin <<EOF
117178476Sjb
118178476Sjb	#pragma D option statusrate=1ms
119178476Sjb
120178476Sjb	syscall:::entry
121178476Sjb	/pid == $ppid/
122178476Sjb	{
123178476Sjb		ttl++;
124178476Sjb	}
125178476Sjb
126178476Sjb	tick-1sec
127178476Sjb	/i++ == 5/
128178476Sjb	{
129178476Sjb		exit(2);
130178476Sjb	}
131178476Sjb
132178476Sjb	END
133178476Sjb	/ttl/
134178476Sjb	{
135178476Sjb		printf("success; ttl is %d", ttl);
136178476Sjb		exit(0);
137178476Sjb	}
138178476Sjb
139178476Sjb	END
140178476Sjb	/ttl == 0/
141178476Sjb	{
142178476Sjb		printf("error -- total should be non-zero");
143178476Sjb		exit(1);
144178476Sjb	}
145178476SjbEOF
146178476Sjb}
147178476Sjb
148178476Sjbif [ $# != 1 ]; then
149178476Sjb	echo expected one argument: '<'dtrace-path'>'
150178476Sjb	exit 2
151178476Sjbfi
152178476Sjb
153178476Sjbppid=$$
154178476Sjbdtrace=$1
155178476Sjb
156178476Sjbunload
157178476Sjbscript1 &
158178476Sjbchild=$!
159178476Sjb
160178476Sjblet waited=0
161178476Sjb
162178476Sjbwhile [ "$waited" -lt 5 ]; do
163178476Sjb	seconds=`date +%S`
164178476Sjb
165178476Sjb	if [ "$seconds" -ne "$last" ]; then
166178476Sjb		last=$seconds
167178476Sjb		let waited=waited+1
168178476Sjb	fi
169178476Sjbdone
170178476Sjb
171178476Sjbwait $child
172178476Sjbstatus=$?
173178476Sjb
174178476Sjbif [ "$status" -ne 0 ]; then
175178476Sjb	echo $tst: first dtrace failed
176178476Sjb	exit $status
177178476Sjbfi
178178476Sjb
179178476Sjbunload
180178476Sjbscript2 &
181178476Sjbchild=$!
182178476Sjb
183178476Sjblet waited=0
184178476Sjb
185178476Sjbwhile [ "$waited" -lt 10 ]; do
186178476Sjb	seconds=`date +%S`
187178476Sjb
188178476Sjb	if [ "$seconds" -ne "$last" ]; then
189178476Sjb		last=$seconds
190178476Sjb		let waited=waited+1
191178476Sjb	fi
192178476Sjbdone
193178476Sjb
194178476Sjbwait $child
195178476Sjbstatus=$?
196178476Sjb
197178476Sjbexit $status
198