summaryrefslogtreecommitdiff
blob: 31930531a1e245f1e2ee4923fc2e708d2e2d125b (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
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

description="Remote Modem Utility (${RC_SVCNAME#*.})"
command="/usr/sbin/ttyd"
name="${RC_SVCNAME}"

start_stop_daemon_args="--quiet --exec ${command} --name ${name}"

depend() {
	use logger
	need net
}

config_check() {
	# sanitize PTY/TTY names
	DEV_PTY="/dev/${PTY/\/dev\/}"
	DEV_TTY="${DEV_PTY/\/pty//tty}"

	# create command args
	command_args="-d ${DEV_PTY}"
	[ -n "${BAUDRATE}" ] && command_args="${command_args} -b ${BAUDRATE}"
	[ -n "${PORT_SETTINGS}" ] && command_args="${command_args} -p ${PORT_SETTINGS}"
	command_args="${command_args} ${HOST} ${PORT}"
}

device_check() {
	if [ ! -e "${DEV_PTY}" ]; then
		eerror "PTY doesn't exist: ${DEV_PTY}"
		return 1
	fi
	if [ ! -e "${DEV_TTY}" ]; then
		eerror "TTY doesn't exist: ${DEV_TTY}"
		return 1
	fi
	return 0
}

linkrel() {  # <from> <to>
	local LINK="${1%/*}/${2/\/dev\/}"
	echo "${LINK/\/dev\/}" | sed "s:[^/]\+/:../:g"
}

setup_links() {
	local LINK NODE
	for LINK in ${SYMLINK}; do
		NODE="/dev/${LINK/\/dev\/}"
		mkdir -p -- "${NODE%/*}" && ln -snf -- $(linkrel "${NODE}" "${DEV_TTY}") "${NODE}"
	done
	for LINK in ${HARDLINK}; do
		NODE="/dev/${LINK/\/dev\/}"
		mkdir -p -- "${NODE%/*}" && ln -nf -- "${DEV_TTY}" "${NODE}"
	done
}

unset_links() {
	local LINK LIST=""
	for LINK in ${SYMLINK} ${HARDLINK}; do
		LIST="${LIST} /dev/${LINK/\/dev\/}"
	done
	[ -n "${LIST}" ] && rm -f -- ${LIST}
}

setup_perms() {
	[ -n "${MODE}" ] && chmod "${MODE}" -- "${DEV_TTY}"
	[ -n "${USER}" ] && chown "${USER}" -- "${DEV_TTY}"
}

unset_perms() {
	[ -n "${USER}" ] && chown --reference "${DEV_PTY}" -- "${DEV_TTY}"
	[ -n "${MODE}" ] && chmod --reference "${DEV_PTY}" -- "${DEV_TTY}"
}

start() {
	config_check || return ${?}
	device_check || return ${?}
	ebegin "Starting ${description}"
	start-stop-daemon --start ${start_stop_daemon_args} -- ${command_args}
	if eend ${?}; then
		setup_perms
		setup_links
		return 0  # from here we never fail
	fi
}

stop() {
	config_check || return ${?}
	ebegin "Stopping ${description}"
	start-stop-daemon --stop ${start_stop_daemon_args}
	eend ${?}
	unset_links
	unset_perms
	return 0  # never fail
}