summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/scripts/fmsim.ksh
blob: 6e20f25d431c2ca2d7b3d149ff8dee468b473b13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"

export PATH=/usr/bin:/usr/sbin:/usr/ccs/bin
unset ENV TMPDIR
umask 022
cwd=$PWD

isa=$(uname -p)
if [[ $isa = sparc ]]; then
	isa64=sparcv9
elif [[ $isa = i386 ]]; then
	isa64=amd64
else
	isa64=unknown
fi

if [[ -n "$CODEMGR_WS" ]]; then
	sysroot=$CODEMGR_WS/proto/root_$isa
elif [[ -n "$ROOT" ]]; then
	sysroot=$ROOT
else
	sysroot=/
fi

quote=
eol='\'
files=

simchan=com.sun:fm:fmd$$
simroot=/tmp/fmd.$$
simscript=run
simpid=

truss_cmd=
truss_args=
dump_args=
inj_args=
fmd_args=

opt_h=false
opt_i=false
opt_s=false
opt_w=false
opt_x=false

function cp_so
{
	nm -ghp $1 2>/dev/null | while read addr type name; do
		[[ $type != T ]] && continue
		case $name in
		_fmd_init)        cp $1 $2/usr/lib/fm/fmd/plugins; return ;;
		fmd_fmri_nvl2str) cp $1 $2/usr/lib/fm/fmd/schemes; return ;;
		topo_load)        cp $1 $2/usr/lib/fm/topo/plugins; return ;;
		esac
	done
	die "\nunknown .so type -- $1"
}

function cp_topo
{
	mkdir -p $2/usr/lib/fm/topo/maps
	cp $1 $2/usr/lib/fm/topo/maps; 
	for platdir in $2/usr/platform/*/lib/fm/topo/maps; do
		rm -f $platdir/* 2>/dev/null
	done
}

function list_cmds
{
	for cmd in fmadm fmdump fmstat; do
		echo usr/sbin/$cmd
	done
}

function wait_status
{
	if [[ $1 -gt 128 ]]; then
		sig=$(kill -l $(($1 - 128)))
		die "fmd terminated from signal $sig (see $simroot)"
	elif [[ $1 -ne 0 ]]; then
		die "fmd terminated with status $1 (see $simroot)"
	fi
}

function wait_prompt
{
	echo "fmsim: [ Press return to $* ] \c"
	mode=$(stty -g)
	stty -echo -isig min 1 time 0
	read s; echo
	stty $mode
}

function die
{
	echo "fmsim: $*" >& 2
	$opt_w && wait_prompt exit
	[[ -n "$simpid" ]] && exit 1 || exit 2
}

while [[ $# -gt 0 ]]; do
	OPTIND=1; while getopts ':d:D:ehio:st:vVwx' c; do
		case "$c" in
		d)
			simroot=$OPTARG
			;;
		D)
			truss_cmd=dtrace
			truss_args="-s $OPTARG -c"
			quote="'"; eol=""
			;;
		e|v|V)
			dump_args="$dump_args -$c"
			;;
		h|i|s|w|x)
			eval opt_$c'='true
			;;
		o)
			fmd_args="$fmd_args -o $OPTARG"
			;;
		t)
			truss_cmd=truss
			truss_args="$OPTARG"
			;;
		:)
			die "option requires an argument -- $OPTARG"
			;;
		*)
			die "illegal option -- $OPTARG"
			;;
		esac
	done
	let OPTIND="$OPTIND - 1"; shift $OPTIND

	if [[ $# -gt 0 ]]; then
		if [[ -d $1 ]]; then
			files="$files $1/*"
		else
			files="$files $1"
		fi
		shift
	fi
done

for file in $files; do
	[[ -r $file ]] || die "input file is missing or not readable -- $file"
done

if $opt_h || [[ -z "$files" && $opt_i = false ]]; then
	echo "Usage: fmsim [-ehisvVwx] [-d dir] [-D a.d] [-o opt=val]" \
	    "[-t args] [file ...]"

	echo "\t-d  set the simulation root directory to the given location"
	echo "\t-D  start fmd(1M) using dtrace(1M) and specified D script"
	echo "\t-e  display error log content instead of fault log content"
	echo "\t-h  display usage information for fmsim and exit"
	echo "\t-i  set interactive mode: do not stop after sending events"
	echo "\t-o  set fmd(1M) option to specified value during simulation"
	echo "\t-s  set up simulation world but do not actually run simulation"
	echo "\t-t  start fmd(1M) using truss(1) and specified arguments"
	echo "\t-v  set verbose mode: display additional event detail"
	echo "\t-V  set very verbose mode: display complete event contents"
	echo "\t-w  wait for a keypress after simulation completes"
	echo "\t-x  delete simulation world if simulation is successful"

	exit 0
fi

echo "fmsim: creating simulation world $simroot ... \c"
[[ -d $simroot ]] || mkdir -p $simroot || exit 1
cd $simroot || exit 1
echo "done."

echo "fmsim: populating /var ... \c"
mkdir -p -m 0755 var/fm/fmd
mkdir -p -m 0700 var/fm/fmd/ckpt
mkdir -p -m 0700 var/fm/fmd/rsrc
mkdir -p -m 0700 var/fm/fmd/xprt
echo "done."

echo "fmsim: populating /usr/lib/fm from $sysroot ... \c"
(cd $sysroot && find usr/lib/fm -depth -print | cpio -pdmu $simroot)

for platdir in $sysroot/usr/platform/*/lib/fm; do
	[[ -d $platdir ]] && platdir=${platdir#$sysroot} || continue
	echo "fmsim: populating $platdir from $sysroot ... \c"
	(cd $sysroot && find ${platdir#/} -depth -print | cpio -pdmu $simroot)
done

echo "fmsim: populating /usr/lib/locale/$LANG from $sysroot ... \c"
(cd $sysroot && find usr/lib/locale/$LANG -depth -print | cpio -pdmu $simroot)

echo "fmsim: populating /usr/sbin from $sysroot ... \c"
(cd $sysroot && list_cmds | cpio -pdmu $simroot)

echo "fmsim: adding customizations:\c"
cd $cwd || exit $1

for file in $files; do
	base=$(basename $file)
	case $base in
	*.cmd)	die "\neversholt command file not yet supported -- $file" ;;
	fmd.conf) cp $file $simroot/etc/fm/fmd ;;
	*.conf)	cp $file $simroot/usr/lib/fm/fmd/plugins ;;
	*.dict)	cp $file $simroot/usr/lib/fm/dict ;;
	*.eft) cp $file $simroot/usr/lib/fm/eft ;;
	*.esc)	die "\neversholt source file not yet supported -- $file" ;;
	*.inj)	inj_args="$inj_args $file" ;;
	*.log)	inj_args="$inj_args $file" ;;
	*log)	inj_args="$inj_args $file" ;;
	*.mo)	cp $file $simroot/usr/lib/locale/$LANG/LC_MESSAGES ;;
	*.so)	cp_so $file $simroot ;;
	*.topo) die "\n .topo files not supported -- $file" ;;
	*.xml) cp_topo $file $simroot ;;
	*)	die "\nunknown file type or suffix -- $file" ;;
	esac
	echo " $base\c"
done

cd $simroot || exit 1
echo " done."

echo "fmsim: generating script ... \c"
cat >$simscript <<EOS
#!/bin/ksh -p
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)fmsim.ksh	1.5	06/10/11 SMI"

#
# fmsim(1M) script generated for $simroot $(date)
#

export LD_LIBRARY_PATH=$simroot/usr/lib:$simroot/usr/lib/fm
export LD_LIBRARY_PATH_64=$simroot/usr/lib/64:$simroot/usr/lib/fm/$isa64

export _THREAD_ERROR_DETECTION=2

exec $truss_cmd $truss_args $quote./usr/lib/fm/fmd/fmd -R $simroot $eol
    -o fg=true -o clock=simulated $eol
    -o rpc.adm.prog=0 -o rpc.adm.path=$simroot/rpc $eol
    -o sysevent-transport:device=/dev/null $eol
    -o sysevent-transport:channel=$simchan $fmd_args$quote

EOS

chmod 0555 $simscript
echo "done."

if $opt_s; then
	echo "fmsim: simulation is saved in $simroot"
	exit 0
fi

export LD_LIBRARY_PATH=$simroot/usr/lib:$simroot/usr/lib/fm
export LD_LIBRARY_PATH_64=$simroot/usr/lib/64:$simroot/usr/lib/fm/$isa64

echo "fmsim: simulation $$ running fmd(1M)\c"
./usr/lib/fm/fmd/fmd -V | cut -d: -f2

./$simscript &
simpid=$!
trap '' INT HUP
cd $cwd
i=0

while [[ ! -s $simroot/rpc ]]; do
	[[ $i -ge 30 ]] && kill -9 $simpid >/dev/null 2>&1
	kill -0 $simpid >/dev/null 2>&1 || break
	let i="$i + 1"
	sleep 1
done

kill -0 $simpid >/dev/null 2>&1 || {
	wait $simpid
	wait_status $?
}

echo "fmsim: rpc adm requests can rendezvous at" $(<$simroot/rpc)
echo "fmsim: injectors should use channel $simchan"
echo "fmsim: debuggers should attach to PID $simpid"

for arg in $inj_args; do
	echo "fmsim: injecting events from $arg ... \c"
	$simroot/usr/lib/fm/fmd/fminject -q -c $simchan $arg || {
		echo "fmsim: fminject failed for $arg: aborting simulation" >& 2
		kill $simpid >/dev/null 2>&1
	}
	echo "done."
done

if [[ $opt_i = false ]]; then
	echo "fmsim: injecting event to advance to end-of-time ... \c"
	echo 'endhrtime;' | $simroot/usr/lib/fm/fmd/fminject -q -c $simchan -
	echo "done."
fi

wait $simpid
status=$?

if [[ -f $simroot/var/fm/fmd/errlog ]]; then
	echo; $simroot/usr/sbin/fmdump -R $simroot $dump_args; echo
fi

wait_status $status
$opt_w && wait_prompt exit
$opt_x && rm -rf $simroot

exit 0