Root/ibrdtnd/files/safety-wrapper.sh

1#!/bin/sh
2#
3# safety wrapper for IBR-DTN daemon
4#
5# Tasks:
6# * start IBR-DTN daemon
7# * restart the daemon after a crash
8# * if respawning to fast, then slow down with backoff
9# * check for enough space on disk and delete bundles if necessary.
10# * clean the blob directory on startup
11#
12
13DTND=/usr/sbin/dtnd
14TMPCONF=/tmp/ibrdtn.config
15UCI=/sbin/uci
16
17getstate() {
18    $UCI -P/var/state -q get ibrdtn.$1
19    return $?
20}
21
22setstate() {
23    $UCI -P/var/state -q set ibrdtn.$1=$2
24    return $?
25}
26
27getconfig() {
28    $UCI -q get ibrdtn.$1
29    return $?
30}
31
32setconfig() {
33    $UCI -q set ibrdtn.$1=$2
34    return $?
35}
36
37# remove the old state file
38/bin/rm /var/state/ibrdtn
39
40# read uci configuration
41BLOB_PATH=`getconfig storage.blobs`
42BUNDLE_PATH=`getconfig storage.bundles`
43CONTAINER_PATH=`getconfig storage.path`
44CONTAINER_FILE=`getconfig storage.container`
45LOG_FILE=`getconfig main.logfile`
46ERR_FILE=`getconfig main.errfile`
47DEBUG_LEVEL=`getconfig main.debug`
48SAFEMODE=no
49
50# run a system check
51/bin/sh /usr/share/ibrdtn/systemcheck.sh
52
53if [ $? -eq 0 ]; then
54    # mount container if specified
55    if [ -n "$CONTAINER_FILE" ] && [ -n "$CONTAINER_PATH" ]; then
56        /bin/sh /usr/share/ibrdtn/mountcontainer.sh
57
58        # if the mount of the container failed
59        # switch to safe mode!
60        if [ $? -gt 0 ]; then
61            SAFEMODE=yes
62        fi
63    fi
64else
65    SAFEMODE=yes
66fi
67
68# create blob & bundle path
69if [ -n "$BLOB_PATH" ]; then
70    /bin/mkdir -p $BLOB_PATH
71
72    # clean the blob directory on startup
73    /bin/rm -f $BLOB_PATH/file*
74fi
75
76if [ -n "$BUNDLE_PATH" ]; then
77    /bin/mkdir -p $BUNDLE_PATH
78fi
79
80LOGGING=""
81if [ -n "$LOG_FILE" ]; then
82    LOGGING="$LOGGING > $LOG_FILE"
83else
84    LOGGING="$LOGGING > /dev/null"
85fi
86
87if [ -n "$ERR_FILE" ]; then
88    LOGGING="$LOGGING 2> $ERR_FILE"
89else
90    LOGGING="$LOGGING 2> /dev/null"
91fi
92
93if [ -z "$LOG_FILE" ] && [ -z "$ERR_FILE" ]; then
94    LOGGING="-q"
95fi
96
97# check for debugging option
98if [ -n "$DEBUG_LEVEL" ]; then
99    DEBUG_ARGS="-d ${DEBUG_LEVEL}"
100else
101    DEBUG_ARGS=""
102fi
103
104# create configuration
105if [ "$SAFEMODE" == "yes" ]; then
106    /bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
107else
108    /bin/sh /usr/share/ibrdtn/build-config.sh $TMPCONF
109fi
110
111# set the crash counter to zero
112CRASH=0
113
114# run the daemon
115setstate state running
116
117while [ "`getstate state`" == "running" ]; do
118    # run a system check
119    /bin/sh /usr/share/ibrdtn/systemcheck.sh
120
121    # run in safe mode if the system check has failed
122    if [ $? -gt 0 ] && [ "$SAFEMODE" == "no" ]; then
123        SAFEMODE=yes
124        /usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "system check failed! Switch to safe-mode settings."
125        /bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
126    fi
127
128    # measure the running time
129    TIMESTART=`/bin/date +%s`
130    
131    # run the daemon
132    echo "${DTND} ${DEBUG_ARGS} -c ${TMPCONF} ${LOGGING}" | /bin/sh
133    
134    # measure the stopping time
135    TIMESTOP=`/bin/date +%s`
136    
137    # calc the running time
138    let TIMERUN=$TIMESTOP-$TIMESTART
139    
140    # reset the CRASH counter if there is one hour between the crashes
141    if [ $TIMERUN -ge 3600 ]; then
142        CRASH=0
143    fi
144    
145    # check if the daemon is crashed
146    if [ "`getstate state`" == "running" ]; then
147        # if the crash counter is higher than 20 switch to safe-mode settings
148        if [ $CRASH -eq 20 ] && [ "$SAFEMODE" == "no" ]; then
149            SAFEMODE=yes
150            /usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "IBR-DTN daemon crashed 20 times! Switch to safe-mode settings."
151            /bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
152        fi
153
154        # increment the crash counter
155        let CRASH=$CRASH+1
156        
157        # backoff wait timer
158        let WAIT=2**$CRASH
159        
160        # set a upper limit for the wait time
161        if [ $WAIT -ge 1800 ]; then
162            WAIT=1800
163        fi
164
165        # log the crash
166        /usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "IBR-DTN daemon crashed $CRASH times! Wait $WAIT seconds."
167        
168        # wait sometime
169        /bin/sleep $WAIT
170    fi
171done
172
173

Archive Download this file



interactive