aboutsummaryrefslogtreecommitdiff
path: root/net/ibrdtnd/files/safety-wrapper.sh
blob: 2f35283d301492f1e2761f376381a624e491d306 (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
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
#!/bin/sh
#
# safety wrapper for IBR-DTN daemon
#
# Tasks:
#  * start IBR-DTN daemon
#  * restart the daemon after a crash
#  * if respawning to fast, then slow down with backoff
#  * check for enough space on disk and delete bundles if necessary.
#  * clean the blob directory on startup
#

DTND=/usr/sbin/dtnd
TMPCONF=/tmp/ibrdtn.config
UCI=/sbin/uci

getstate() {
	$UCI -P/var/state -q get ibrdtn.$1
	return $?
}

setstate() {
	$UCI -P/var/state -q set ibrdtn.$1=$2
	return $?
}

getconfig() {
	$UCI -q get ibrdtn.$1
	return $?
}

setconfig() {
	$UCI -q set ibrdtn.$1=$2
	return $?
}

# remove the old state file
/bin/rm /var/state/ibrdtn

# read uci configuration
BLOB_PATH=`getconfig storage.blobs`
BUNDLE_PATH=`getconfig storage.bundles`
CONTAINER_PATH=`getconfig storage.path`
CONTAINER_FILE=`getconfig storage.container`
LOG_FILE=`getconfig main.logfile`
ERR_FILE=`getconfig main.errfile`
DEBUG_LEVEL=`getconfig main.debug`
SAFEMODE=no

# run a system check
/bin/sh /usr/share/ibrdtn/systemcheck.sh

if [ $? -eq 0 ]; then
	# mount container if specified
	if [ -n "$CONTAINER_FILE" ] && [ -n "$CONTAINER_PATH" ]; then
		/bin/sh /usr/share/ibrdtn/mountcontainer.sh

		# if the mount of the container failed
		# switch to safe mode!
		if [ $? -gt 0 ]; then
			SAFEMODE=yes
		fi
	fi
else
	SAFEMODE=yes
fi

# create blob & bundle path
if [ -n "$BLOB_PATH" ]; then
	/bin/mkdir -p $BLOB_PATH

	# clean the blob directory on startup
	/bin/rm -f $BLOB_PATH/file*
fi

if [ -n "$BUNDLE_PATH" ]; then
	/bin/mkdir -p $BUNDLE_PATH
fi

LOGGING=""
if [ -n "$LOG_FILE" ]; then
	LOGGING="$LOGGING > $LOG_FILE"
else
	LOGGING="$LOGGING > /dev/null"
fi

if [ -n "$ERR_FILE" ]; then
	LOGGING="$LOGGING 2> $ERR_FILE"
else
	LOGGING="$LOGGING 2> /dev/null"
fi

if [ -z "$LOG_FILE" ] && [ -z "$ERR_FILE" ]; then
	LOGGING="-q"
fi

# check for debugging option
if [ -n "$DEBUG_LEVEL" ]; then
	DEBUG_ARGS="-v -d ${DEBUG_LEVEL}"
else
	DEBUG_ARGS=""
fi

# create configuration
if [ "$SAFEMODE" == "yes" ]; then
	/bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
else
	/bin/sh /usr/share/ibrdtn/build-config.sh $TMPCONF
fi

# set the crash counter to zero
CRASH=0

# run the daemon
setstate state running

while [ "`getstate state`" == "running" ]; do
	# run a system check
	/bin/sh /usr/share/ibrdtn/systemcheck.sh

	# run in safe mode if the system check has failed
	if [ $? -gt 0 ] && [ "$SAFEMODE" == "no" ]; then
		SAFEMODE=yes
		/usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "system check failed! Switch to safe-mode settings."
		/bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
	fi

	# measure the running time
	TIMESTART=`/bin/date +%s`
	
	# run the daemon
	echo "${DTND} ${DEBUG_ARGS} -c ${TMPCONF} ${LOGGING}" | /bin/sh
	
	# measure the stopping time
	TIMESTOP=`/bin/date +%s`
	
	# calc the running time
	let TIMERUN=$TIMESTOP-$TIMESTART
	
	# reset the CRASH counter if there is one hour between the crashes
	if [ $TIMERUN -ge 3600 ]; then
		CRASH=0
	fi
	
	# check if the daemon is crashed
	if [ "`getstate state`" == "running" ]; then
		# if the crash counter is higher than 20 switch to safe-mode settings
		if [ $CRASH -eq 20 ] && [ "$SAFEMODE" == "no" ]; then
			SAFEMODE=yes
			/usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "IBR-DTN daemon crashed 20 times! Switch to safe-mode settings."
			/bin/sh /usr/share/ibrdtn/build-config.sh --safe-mode $TMPCONF
		fi

		# increment the crash counter
		let CRASH=$CRASH+1
		
		# backoff wait timer
		let WAIT=2**$CRASH
		
		# set a upper limit for the wait time
		if [ $WAIT -ge 1800 ]; then
			WAIT=1800
		fi

		# log the crash
		/usr/bin/logger -t "ibrdtn-safe-wrapper" -p 2 "IBR-DTN daemon crashed $CRASH times! Wait $WAIT seconds."
		
		# wait sometime
		/bin/sleep $WAIT
	fi
done