aboutsummaryrefslogtreecommitdiff
blob: 81f8308c9b34bec0d9f98e332cbb15b2d2c2931e (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
#!/sbin/runscript
# Distributed under the terms of the GNU General Public License v2
# $Id$

opts="${opts} watchdogrestart"

. /etc/conf.d/vdr.watchdogd

common_init() {
	vdr_home=/var/vdr
	cd ${vdr_home}

	. /usr/share/vdr/inc/functions.sh
	include rc-functions
	include plugin-functions
	VDR_LOG_FILE=/var/vdr/tmp/vdr-start-log
}

clear_logfile() {
	rm -f "${VDR_LOG_FILE}"
	printf "" > "${VDR_LOG_FILE}"
}

#
# Used to log error-messages in startscript to show them on
# OSD later when choosing apropriate point in commands.
#

vdr_log()
{
	echo "$@" >> ${VDR_LOG_FILE}
}

depend() {
	need net
	[ "${IR_CTRL}" = "lirc" ] && need lircd
	use lircd coldplug
	after checkroot
}

start_vdr() {
	einfo "Preparing start of vdr:"
	clear_logfile
	init_params
	add_daemonctrl_param --start --chdir ~vdr --exec ${VDR_BIN}
	init_plugin_loader start
	
	load_addons_prefixed pre-start || return 1

	ebegin "Starting ${VDRNAME}"
	unset MAIL
	export LOGNAME=vdr USER=vdr HOME="${vdr_home}"

	local _openvt= openvt_opts= TERM_DEV=/dev/tty${TERMINAL}
	if [ -n "${TERMINAL}" -a -e "${TERM_DEV}" ]; then
		yesno "${SWITCH_TO_TERMINAL}" && openvt_opts="-s"
		_openvt="openvt -c ${TERMINAL} ${openvt_opts} --"

		{
			clear
			einfo "Starting ${VDRNAME}"
		} >${TERM_DEV}
	else
		add_param --daemon
	fi

	debug_msg "  CMDLINE: ${_openvt} start-stop-daemon ${daemonctrl_opts} -- ${vdr_opts}"

	eval ${_openvt} start-stop-daemon ${daemonctrl_opts} -- ${vdr_opts}
	vdr_exitcode=$?
	eend $vdr_exitcode "Failed to start vdr."
	
	# TODO: Anything todo if starting failed? cleanups?

	if [ $vdr_exitcode -lt 128 ]; then
		load_addons_prefixed post-start || vdr_exitcode=1
	fi

	if [ $vdr_exitcode != 0 ]; then
		# Make sure vdr really does not run
		kill_vdr
	fi

	# show messages if we have any
	if [ "${vdr_exitcode}" = "0" -a -s "${VDR_LOG_FILE}" ]; then
		/usr/share/vdr/bin/vdr-bg.sh svdrpsend.pl mesg "Errors: Go to Commands/View VDR Start Log"
	fi

	return $vdr_exitcode
}

stop_vdr() {
	init_plugin_loader stop

	load_addons_prefixed pre-stop

	ebegin "Stopping ${VDRNAME}"
	# Use --name here to allow us to kill vdr even after a new emerge
	start-stop-daemon --stop --quiet --retry 15 --exec ${VDR_BIN} 
	exitcode=$?
	eend $exitcode "Failed to stop vdr."

	if [ $exitcode != 0 ]; then
		kill_vdr
	fi

	load_addons_prefixed post-stop
	return 0
}

kill_vdr() {
	killall ${VDR_BIN}
	if test_vdr_process; then
		sleep 2
		test_vdr_process && killall -9 ${VDR_BIN}
	fi
}


start_watchdog() {
	yesno "${ENABLE_EXTERNAL_WATCHDOG:-yes}" || return 0
	ebegin "Starting vdr watchdog"
	start-stop-daemon \
		--start \
		--background \
		--make-pidfile \
		--pidfile /var/run/vdrwatchdog.pid \
		--exec /usr/sbin/vdr-watchdogd \
		--name vdr-watchdogd
	eend $? "failed starting vdr watchdog"
	return 0
}


stop_watchdog() {
	if yesno "${ENABLE_EXTERNAL_WATCHDOG:-yes}"; then
		ebegin "Stopping vdr watchdog"
		start-stop-daemon --stop --pidfile /var/run/vdrwatchdog.pid --name vdr-watchdogd
		eend $? "failed stopping watchdog"
	else
		# Also stop watchdog if conf was changed to disabled while it was running
		start-stop-daemon --stop --pidfile /var/run/vdrwatchdog.pid --name vdr-watchdogd --quiet
	fi
	return 0
}

start() {
	common_init
	start_vdr || return 1
	start_watchdog || return 2
}

stop() {
	common_init
	stop_watchdog || return 2
	stop_vdr
}

# gets called by watchdog to restart vdr
# and possibly reload modules
watchdogrestart() {
	common_init

	( stop_vdr )

	load_addons_prefixed watchdog-restart

	( start_vdr )

	svdrpsend.pl mesg "Warning: VDR process died."
}