OpenWrt packages
Sign in or create your account | Project List | Help
OpenWrt packages Git Source Tree
Root/
| 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 | |
| 13 | DTND=/usr/sbin/dtnd |
| 14 | TMPCONF=/tmp/ibrdtn.config |
| 15 | UCI=/sbin/uci |
| 16 | |
| 17 | getstate() { |
| 18 | $UCI -P/var/state -q get ibrdtn.$1 |
| 19 | return $? |
| 20 | } |
| 21 | |
| 22 | setstate() { |
| 23 | $UCI -P/var/state -q set ibrdtn.$1=$2 |
| 24 | return $? |
| 25 | } |
| 26 | |
| 27 | getconfig() { |
| 28 | $UCI -q get ibrdtn.$1 |
| 29 | return $? |
| 30 | } |
| 31 | |
| 32 | setconfig() { |
| 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 |
| 41 | BLOB_PATH=`getconfig storage.blobs` |
| 42 | BUNDLE_PATH=`getconfig storage.bundles` |
| 43 | CONTAINER_PATH=`getconfig storage.path` |
| 44 | CONTAINER_FILE=`getconfig storage.container` |
| 45 | LOG_FILE=`getconfig main.logfile` |
| 46 | ERR_FILE=`getconfig main.errfile` |
| 47 | DEBUG_LEVEL=`getconfig main.debug` |
| 48 | SAFEMODE=no |
| 49 | |
| 50 | # run a system check |
| 51 | /bin/sh /usr/share/ibrdtn/systemcheck.sh |
| 52 | |
| 53 | if [ $? -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 |
| 64 | else |
| 65 | SAFEMODE=yes |
| 66 | fi |
| 67 | |
| 68 | # create blob & bundle path |
| 69 | if [ -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* |
| 74 | fi |
| 75 | |
| 76 | if [ -n "$BUNDLE_PATH" ]; then |
| 77 | /bin/mkdir -p $BUNDLE_PATH |
| 78 | fi |
| 79 | |
| 80 | LOGGING="" |
| 81 | if [ -n "$LOG_FILE" ]; then |
| 82 | LOGGING="$LOGGING > $LOG_FILE" |
| 83 | else |
| 84 | LOGGING="$LOGGING > /dev/null" |
| 85 | fi |
| 86 | |
| 87 | if [ -n "$ERR_FILE" ]; then |
| 88 | LOGGING="$LOGGING 2> $ERR_FILE" |
| 89 | else |
| 90 | LOGGING="$LOGGING 2> /dev/null" |
| 91 | fi |
| 92 | |
| 93 | if [ -z "$LOG_FILE" ] && [ -z "$ERR_FILE" ]; then |
| 94 | LOGGING="-q" |
| 95 | fi |
| 96 | |
| 97 | # check for debugging option |
| 98 | if [ -n "$DEBUG_LEVEL" ]; then |
| 99 | DEBUG_ARGS="-d ${DEBUG_LEVEL}" |
| 100 | else |
| 101 | DEBUG_ARGS="" |
| 102 | fi |
| 103 | |
| 104 | # create configuration |
| 105 | if [ "$SAFEMODE" == "yes" ]; then |
| 106 | /bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF |
| 107 | else |
| 108 | /bin/sh /usr/share/ibrdtn/build-config.sh $TMPCONF |
| 109 | fi |
| 110 | |
| 111 | # set the crash counter to zero |
| 112 | CRASH=0 |
| 113 | |
| 114 | # run the daemon |
| 115 | setstate state running |
| 116 | |
| 117 | while [ "`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 |
| 171 | done |
| 172 | |
| 173 |
