summaryrefslogtreecommitdiff
path: root/scripts/telnetd.init
blob: 7afefb3d014f33cc1327fef74218c8165b5292a7 (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
#! /bin/bash

declare progname=${0##*/}
declare action=$1; shift

if [ x$BOOTFILE != x ] && [ -r $BOOTFILE ]; then
  may_start=false
  eval $(sed -n '
/^service {$/,/^}$/ {
    / *telnet {$/,/ *}$/ {
	s/ *telnet {/may_start=true/p
	s/ *port: /port=/p
}}' $BOOTFILE)
else
  may_start=true
fi

if [ $# -ne 0 ] ; then
    port=$1; shift
fi

: ${port:=23}
: ${bb:=/bin/busybox}

test -x $bb  || exit 0

running_pid ()
{
    pidof $bb | while read pid ; do
	f=$(tr '\000' '\t' < /proc/$pid/cmdline 2>/dev/null | cut -f2)
	if [ "$f" == telnetd ] ; then
	    echo $pid
	    return
	fi
    done
    false
}

start ()
{
    local -i pid=$( running_pid )

    [ $pid -ne 0 ] && return
    $may_start || return 0
    $bb telnetd -p $port
}

stop ()
{
    local -i pid=$( running_pid )
    [ $pid -ne 0 ] && kill $pid
}

status ()
{
    pidof $bb | while read pid ; do
	f=$(tr '\000' '\t' < /proc/$pid/cmdline | cut -f2)
	if [ $f == telnetd ] ; then
	    echo running
	    return
	fi
    done
    echo not running
    false
}

case "$action" in
  start)	start;;
  stop)		stop;;
  restart)	stop; sleep 1; start;;
  status)	status;;
  *)	echo "Usage: $progname {start|stop|restart|status}"
	exit 1
esac