-
Notifications
You must be signed in to change notification settings - Fork 0
/
rina
executable file
·311 lines (266 loc) · 7.14 KB
/
rina
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
#
# Script start/stop IRATI's IPCM with a specific configuration.
#
# Usage:
# rina start <config name>
# rina stop
# rina status
#
# Environment variables to set before calling this script
#
# RINA_RUNTIME_DIR:
# Path to the runtime file (active configuration, PID file)
# RINA_CONFIG_ROOT:
# Path to the configuration files
# RINA_LOG_ROOT:
# Path where the log files will be stored.
# RINA_IPCM:
# Path to the IPCM binary
set -u
# Ensure that all variables passed as aa
ensure_variables_set() {
local v err=0
for v in "$@"; do
if [ ! -v ${v} ] || [ -z ${!v} ]; then
echo "Variable $v must be set"
err=1
fi
done
if [ $err -eq 1 ]; then
exit 1
fi
}
ensure_directory_exists() {
local err=0
for v in "$@"; do
if [ ! -d ${!v} ]; then
echo "Directory for $v does not exists: ${!v}"
err=1
fi
done
if [ $err -eq 1 ]; then
exit 1
fi
}
ensure_executable() {
local err=0
for v in "$@"; do
if [ ! -x ${!v} ]; then
echo "File for $v should be executable: ${!v}"
err=1
fi
done
if [ $err -eq 1 ]; then
exit 1
fi
}
modprobe_or_die() {
# Check if already loaded...
if [ ! -z $(lsmod | grep -q $1) ]; then
return
fi
modprobe $@
if [ $? -ne 0 ]; then
echo "Could not load module $1"
exit 1
fi
}
load_km() {
local verbosity=7
if [ ! -z ${1:-""} ] && [ "$1" = "verbose" ]; then
verbosity=9
fi
modprobe_or_die rina-irati-core irati_verbosity=${verbosity}
modprobe_or_die rina-default-plugin
modprobe_or_die normal-ipcp
modprobe_or_die shim-eth
modprobe_or_die shim-tcp-udp
}
unload_km() {
rmmod shim-tcp-udp
rmmod shim-eth
rmmod rinarp
rmmod arp826
rmmod normal-ipcp
rmmod rina-default-plugin
rmmod rina-irati-core
}
start() {
local cfgname cfgpath hostname result logpath configfile pidfile \
rtfile consoleSocket configfile upfile
cfgname=${1:-default}
hostname=$(hostname)
if [ $cfgname != "default" ]; then
if [ ! -d "${RINA_CONFIG_ROOT}/${cfgname}.${hostname}" ]; then
echo "Configuration $config, for hostname ${hostname}, does not exists in RINA_CONFIG_ROOT"
exit 1
fi
cfgpath="${RINA_CONFIG_ROOT}/${cfgname}.${hostname}"
else
if [ ! -d "${RINA_CONFIG_ROOT}/default" ]; then
echo "'default' configuration does not exit in RINA_CONFIG_ROOT"
exit 1
fi
cfgpath="${RINA_CONFIG_ROOT}/default"
fi
# Load the kernel modules if its not done already.
if [ ! -c /dev/irati-ctrl ]; then
load_km
fi
# Create the directory where the log will be put if it does not
# exists already.
if [ ! -d $RINA_LOG_ROOT/$cfgname ]; then
mkdir -p $RINA_LOG_ROOT/$cfgname
fi
logpath="${RINA_LOG_ROOT}/${cfgname}"
pidfile="${RINA_RUNTIME_DIR}/ipcm.pid"
rtfile="${RINA_RUNTIME_DIR}/ipcm.config"
consoleSocket="${RINA_RUNTIME_DIR}/ipcm.sock"
configfile="${cfgpath}/ipcmanager.conf"
upfile="${cfgpath}/up.sh"
# If there is an "up" script in the configuration directory, run it.
if [ -x $upfile ]; then
$upfile
result=$?
if [ $result -ne 0 ]; then
echo "Cannot continue, up.sh returned error code: $result"
exit 1
fi
fi
# The -o option here# allows overriding the options available in
# the 'localConfiguration' block from the command line.
start-stop-daemon \
--start \
--background \
--exec ${RINA_IPCM} \
--oknodo \
--pidfile $pidfile \
--make-pidfile \
-- \
-o logPath=$logpath \
-o consoleSocket=$consoleSocket \
-c $configfile -l debug -a console,scripting
result=$?
case $result in
0)
echo "IPCM was started with the $(basename $cfgpath) configuration"
echo $cfgname > $rtfile
;;
1)
status;;
*) echo "Error starting IPCM with the $(basename $cfgpath) configuration";;
esac
}
status() {
local pidfile result pid config
pidfile="${RINA_RUNTIME_DIR}/ipcm.pid"
rtfile="${RINA_RUNTIME_DIR}/ipcm.config"
start-stop-daemon \
--status \
--exec ${RINA_IPCM} \
--pidfile $pidfile
result=$?
if [ -f $pidfile ]; then
pid=$(cat $pidfile)
if [ $result -eq 0 ]; then
if [ -f $rtfile ]; then
config=$(cat $rtfile)
echo "IPCM is running. PID ${pid} - Configuration ${config}."
else
echo "IPCM is running. Unknown configuration!"
fi
elif [ $result -eq 1 ]; then
echo "IPCM is NOT running. PID was ${pid}. It was killed or crashed."
fi
else
echo "IPCM is NOT running."
fi
}
stop() {
local pidfile config dnfile result
config=$(cat "${RINA_RUNTIME_DIR}/ipcm.config")
pidfile="${RINA_RUNTIME_DIR}/ipcm.pid"
dnfile="${RINA_CONFIG_ROOT}/${config}.$(hostname)/down.sh"
start-stop-daemon \
--stop \
--retry TERM/5/KILL/30 \
--pidfile $pidfile \
--remove-pidfile \
--exec ${RINA_IPCM} \
--name ipcm
# If there is a down.sh file, run it.
if [ -x $dnfile ]; then
$dnfile
result=$?
if [ $result -ne 0 ]; then
echo "Error, down.sh returned error code: $result"
echo "The network configuration might require manual teardown"
fi
fi
sleep 1
if [ -c /dev/irati-ctrl ]; then
unload_km
fi
}
console() {
local consoleSocket
consoleSocket="${RINA_RUNTIME_DIR}/ipcm.sock"
socat - UNIX:$consoleSocket
}
cmd() {
local consoleSocket
consoleSocket="${RINA_RUNTIME_DIR}/ipcm.sock"
cmd=${1:-}
if [ -z "$cmd" ]; then
echo "Missing command to execute."
exit 1
fi
shift
irati-ctl --unix-socket ${consoleSocket} ${cmd} $@
}
exit_usage() {
echo "Usage: "
echo -e "\trina start <config>"
echo -e "\trina stop"
echo -e "\trina status"
echo -e "\trina console"
echo -e "\trina load-modules <verbose>"
echo -e "\trina unload-modules"
echo
echo "Unknown commands are passed to 'irati-ctl'"
exit 1
}
cmd=${1-}
# We already need at least a command on the command line.
[ -z "$cmd" ] && exit_usage
# Read a global configuration file.
if [ -f /etc/rina/rina.config ]; then
source /etc/rina/rina.config
fi
# We allow ourselves to create RINA_RUNTIME_DIR
mkdir -p ${RINA_RUNTIME_DIR}
# Validate the configuration
ensure_variables_set RINA_RUNTIME_DIR RINA_IPCM RINA_LOG_ROOT RINA_CONFIG_ROOT
ensure_directory_exists RINA_RUNTIME_DIR RINA_LOG_ROOT RINA_CONFIG_ROOT
ensure_executable RINA_IPCM
case $cmd in
start)
config=${2:-}
start $config;;
stop)
stop;;
status)
status;;
console)
console;;
config)
config;;
load-modules)
verbosity=${2:-"normal"}
load_km ${verbosity};;
unload-modules)
unload_km;;
*)
cmd $@
esac