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