1#!@DEFAULT_INIT_SHELL@
2# shellcheck disable=SC2154
3#
4# zfs-zed
5#
6# chkconfig:    2345 29 99
7# description:  This script will start and stop the ZFS Event Daemon.
8# probe: true
9#
10### BEGIN INIT INFO
11# Provides:          zfs-zed
12# Required-Start:    zfs-mount
13# Required-Stop:     zfs-mount
14# Default-Start:     2 3 4 5
15# Default-Stop:      0 1 6
16# X-Stop-After:      zfs-share
17# Short-Description: ZFS Event Daemon
18# Description:       zed monitors ZFS events. When a zevent is posted, zed
19#                    will run any scripts that have been enabled for the
20#                    corresponding zevent class.
21### END INIT INFO
22#
23# Released under the 2-clause BSD license.
24#
25# This script is based on debian/zfsutils.zfs.init from the
26# Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
27
28# Source the common init script
29. @sysconfdir@/zfs/zfs-functions
30
31ZED_NAME="zed"
32ZED_PIDFILE="@runstatedir@/$ZED_NAME.pid"
33
34# shellcheck disable=SC2034
35extra_started_commands="reload"
36
37# Exit if the package is not installed
38[ -x "$ZED" ] || exit 0
39
40# ----------------------------------------------------
41
42do_depend()
43{
44	after zfs-mount localmount
45}
46
47do_start()
48{
49	check_module_loaded "zfs" || exit 0
50
51	ZED_ARGS="$ZED_ARGS -p $ZED_PIDFILE"
52
53	zfs_action "Starting ZFS Event Daemon" zfs_daemon_start \
54	    "$ZED_PIDFILE" "$ZED" "$ZED_ARGS"
55	return "$?"
56}
57
58do_stop()
59{
60	local pools
61	check_module_loaded "zfs" || exit 0
62
63	zfs_action "Stopping ZFS Event Daemon" zfs_daemon_stop \
64	   "$ZED_PIDFILE" "$ZED" "$ZED_NAME" || return "$?"
65
66	# Let's see if we have any pools imported
67	pools=$("$ZPOOL" list -H -oname)
68	if [ -z "$pools" ]
69	then
70		# No pools imported, it is/should be safe/possible to
71		# unload modules.
72		zfs_action "Unloading modules" rmmod zfs spl
73		return "$?"
74	fi
75}
76
77do_status()
78{
79	check_module_loaded "zfs" || exit 0
80
81	zfs_daemon_status "$ZED_PIDFILE" "$ZED" "$ZED_NAME"
82	return "$?"
83}
84
85do_reload()
86{
87	check_module_loaded "zfs" || exit 0
88
89	zfs_action "Reloading ZFS Event Daemon" zfs_daemon_reload \
90	    "$ZED_PIDFILE" "$ZED_NAME"
91	return "$?"
92}
93
94# ----------------------------------------------------
95
96if @IS_SYSV_RC@
97then
98	case "$1" in
99		start)
100			do_start
101			;;
102		stop)
103			do_stop
104			;;
105		status)
106			do_status
107			;;
108		reload|force-reload)
109			do_reload
110			;;
111		restart)
112			do_stop
113			do_start
114			;;
115		*)
116			[ -n "$1" ] && echo "Error: Unknown command $1."
117			echo "Usage: $0 {start|stop|status|reload|restart}"
118			exit 1
119			;;
120	esac
121
122	exit $?
123else
124	# Create wrapper functions since Gentoo don't use the case part.
125	depend() { do_depend; }
126	start() { do_start; }
127	stop() { do_stop; }
128	status() { do_status; }
129	reload() { do_reload; }
130fi
131