File: turnserver.fedora.initd

package info (click to toggle)
turnserver 0.7.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,528 kB
  • ctags: 820
  • sloc: ansic: 10,979; sh: 1,651; makefile: 115
file content (199 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
#
# turnserver Starts the turnserver daemon
#
# chkconfig:   345 84 16
# description: TURN server

# The template for this file was taken from
# http://fedoraproject.org/wiki/Packaging:SysVInitScript#Fedora_SysV_Initscripts
#
# See also a description of the exit codes at
# http://fedoraproject.org/wiki/Packaging:SysVInitScript#Exit_Codes_for_the_Status_Action

# Source function library.
. /etc/rc.d/init.d/functions

exec="/usr/bin/turnserver"
prog="turnserver"
config="/etc/turnserver.conf"

# If running multiple instances, use port number suffix on config and pid files.
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
if [ -n "$PORTS" ]; then
    files_use_portnum=yes
else
    PORTS=3478
    files_use_portnum=
fi

lockfile=/var/lock/subsys/$prog

# Note the LSB exit codes for the status action:
# 0=running, 1=dead but pid file exists, 3=stopped
rh_status() {
    status "$@"
}

# quiet version
rh_status_q() {
    rh_status "$@" >/dev/null 2>&1
}

start_instance()
{
    local port=$1; shift

    # raise open file limit
    ulimit -n 16384

    # start it up (if not running)
    echo -n "Starting $instname: "
    status=$(rh_status -p $pidfile $prog)
    local rc=$?
    [ $rc -ne 3 ] && echo "$status"
    if [ ! -f $config ]; then
        echo "Config file not found: $config"
        rc=6
    elif [ $rc -ne 0 ]; then
        $exec -c $config -p $pidfile
        [ $? -eq 0 ] && rc=0 || rc=1
    fi
    [ $rc -eq 0 ] && success || failure
    echo
    return $rc
}

stop_instance()
{
    local port=$1; shift

    # stop it (if running)
    echo -n "Stopping $instname: "
    status=$(rh_status -p $pidfile $prog)
    local rc=$?
    if [ $rc -ne 0 ]; then
        echo "$status"
        rc=0
    else
        delay=3
        killproc -p $pidfile -d $delay $prog
        rc=$?
    fi
    [ $rc -eq 0 ] && success || failure
    echo
    return $rc
}

status_instance()
{
    local port=$1; shift

    # use $instname not $prog here to make for a more readable status message
    rh_status -p $pidfile $instname
    local rc=$?
    return $rc
}

reload_instance()
{
    local port=$1; shift

    echo -n "Reloading $instname: "
    status=$(rh_status -p $pidfile $prog)
    local rc=$?
    if [ $rc -ne 0 ]; then
        echo "$status"
        rc=7
    else
        local pid
        pid=$(cat $pidfile)
        kill -HUP $pid
        [ $? -eq 0 ] && rc=0 || rc=1
    fi
    [ $rc -eq 0 ] && success || failure
    echo
    return $rc
}

foreach_instance()
{
    local func=$1; shift

    local rc=0 instance_rc=0
    for port in $PORTS; do
        [ -n "$files_use_portnum" ] && portsuffix=$port || portsuffix=
        pidfile=/var/run/$prog$portsuffix.pid
        config=/etc/$prog$portsuffix.conf
        instname=$prog$portsuffix
        $func $port
        instance_rc=$?
        [ $instance_rc -gt $rc ] && rc=$instance_rc
    done

    return $rc
}

start()
{
    foreach_instance start_instance
    retval=$?
    [ $retval -eq 0 ] && touch $lockfile
}

stop()
{
    foreach_instance stop_instance
    retval=$?
    #[ $retval -eq 0 ] && 
    rm -f $lockfile
}

restart()
{
    stop
    start
}

### main

# parse command line
while getopts "i:" option; do
    case "$option" in
    i) PORTS="$OPTARG";;
    *) exit 1;;
    esac
done
shift $((OPTIND-1))
case $# in
1) ;;
*) echo "Usage: $0 [-i port] action"; exit 1;;
esac

# perform action
retval=0
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart|force-reload)
    restart
    ;;
reload)
    foreach_instance reload_instance
    ;;
status)
    foreach_instance status_instance
    ;;
condrestart|try-restart)
    [ -f $lockfile ] || exit 0
    restart
    ;;
*)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
    exit 2
esac
exit $?