summaryrefslogtreecommitdiff
blob: 3302ea37bc9b76210ece2d273e5f1494f0d0311c (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/runscript
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-misc/termpkg/files/ttyd.initd,v 1.1 2008/09/23 19:34:00 sbriesen Exp $

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

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 --quiet --exec "${command}" -- ${command_args}
	if eend ${?}; then
		setup_perms
		setup_links
		return 0  # from here we never fail
	fi
}

stop() {
	config_check || return ${?}
	# we can't use start-stop-daemon, because we don't have a pidfile
	# *and* multiple processes can run at the same time. So we use
	# pkill to identify *our* process via commandline args.
	ebegin "Stopping ${description}"
	/usr/bin/pkill -f "^${command} -d ${DEV_PTY} "
	eend ${?}
	unset_links
	unset_perms
	return 0  # never fail
}