aboutsummaryrefslogtreecommitdiffstats
path: root/community/jool-tools/jool.initd
blob: e666c457297be814d42d91d345be354a27aead52 (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
#!/sbin/openrc-run

case "${RC_SVCNAME#*[.-]}" in
	jool[_-]siit)
		name="SIIT"
		description="Stateless IP/ICMP Translator"
		command="/usr/bin/jool_siit"
		kmod_name="jool_siit"
	;;
	*)
		name="NAT64"
		description="Stateful NAT64"
		kmod_name="jool"
		command="/usr/bin/jool"
	;;
esac

: ${cfgfile:="/etc/jool/$RC_SVCNAME.conf"}

required_files="$cfgfile"

depends() {
	need net
}

start_pre() {
	resolve_instance_name

	# Don't load module if it's already loaded.
	if modprobe -qn "$kmod_name" && ! modprobe -qn --first-time "$kmod_name"; then
		return 0
	fi

	ebegin "Loading $kmod_name kernel module"
	modprobe -q $kmod_name
	eend $?
}

start() {
	ebegin "Loading $name instance $instance_name"
	$command -i "$instance_name" file handle "$cfgfile"
	eend $?
}

stop_pre() {
	resolve_instance_name
}

stop() {
	case $(instance_status) in
		Running)
			ebegin "Unloading $name instance $instance_name"
			$command instance remove "$instance_name"
			eend $?
		;;
		*)
			ewarn "WARNING: $name instance $instance_name is not running"
			return 0
		;;
	esac
}

status() {
	resolve_instance_name

	case "$(instance_status)" in
		Running)
			einfo "status: running"
			return 0
		;;
		Dead)
			if service_started || service_crashed; then
				eerror "status: crashed"
				return 32
			else
				einfo "status: stopped"
				return 3
			fi
		;;
		*)
			eerror "status: error"
			$command -i "$instance_name" instance status >&2
			return 32
		;;
	esac
}

instance_status() {
	$command -i "$instance_name" instance status 2>/dev/null | head -1
}

resolve_instance_name() {
	instance_name=$(sed -En 's/.*"instance":\s*"([^"]+)".*/\1/p' "$cfgfile")

	if [ -z "$instance_name" ] && [ "${RC_SVCNAME#*[.-]}" != "$RC_SVCNAME" ]; then
		instance_name="${RC_SVCNAME#*[.-]}"
	fi
	: ${instance_name:="default"}
}