You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.7 KiB

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