1#!/usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2020 Oxide Computer Company
16#
17
18#
19# Sit in a loop trying to unload the driver specified as an argument.
20#
21
22ksensor_id=
23ksensor_drv=
24ksensor_to=30
25ksensor_skew=5
26
27function get_id
28{
29
30	while [[ -z "$ksensor_id" ]]; do
31		sleep 1
32		ksensor_id=$(modinfo | awk "{
33			if (\$6 == \"$1\") {
34			    print \$1
35			} }")
36	done
37}
38
39function unload
40{
41	while :; do
42		if ! modunload -i $ksensor_id 2>/dev/null; then
43			echo "failed to unload $ksensor_drv" >&2
44		else
45			echo "unloaded $ksensor_drv"
46		fi
47		sleep $((($RANDOM % $ksensor_to) + $ksensor_skew))
48	done
49}
50
51if [[ -z "$1" ]]; then
52	echo "Missing required driver name" >&2
53	exit 1
54fi
55
56ksensor_drv=$1
57get_id $ksensor_drv
58printf "Got module id for %s: %u\n" "$ksensor_drv" $ksensor_id
59unload
60