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

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

port=$1; shift

: ${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
    $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