Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dist/tools/ethos: deduplicate setup_network.sh, start_network.sh #11840

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions dist/tools/ethos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ To use, add

to app Makefile, "make clean all flash", then run this tool as follows:
# sudo ./ethos <tap-device> <serial>

## setup_network.sh

This script sets up a tap device, configures a prefix and starts a uhcpd server
serving that prefix towards the tap device.
The tap device will be owned by the calling user, or if called with sudo, with
the user that uses sudo. That way, ethos can use it without being root / using
sudo.

E.g., use as follows:

$ sudo ./setup_network.sh riot0 2001:db8::0/64

Keep that running, then in another shell start ethos:

$ ethos riot0 <serial>
48 changes: 48 additions & 0 deletions dist/tools/ethos/setup_network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

create_tap() {
ip tuntap add ${TAP} mode tap user ${_USER}
sysctl -w net.ipv6.conf.${TAP}.forwarding=1
sysctl -w net.ipv6.conf.${TAP}.accept_ra=0
ip link set ${TAP} up
ip a a fe80::1/64 dev ${TAP}
ip a a fd00:dead:beef::1/128 dev lo
ip route add ${PREFIX} via fe80::2 dev ${TAP}
}

remove_tap() {
ip tuntap del ${TAP} mode tap
}

cleanup() {
trap "" INT QUIT TERM EXIT
echo "Cleaning up network..."
remove_tap
ip a d fd00:dead:beef::1/128 dev lo
}

start_uhcpd() {
${UHCPD} ${TAP} ${PREFIX}
}

TAP=$1
PREFIX=$2
_USER=$3
: ${UHCPD:="$(readlink -f $(dirname $0)"/../uhcpd/bin")/uhcpd"}

[ -z "${TAP}" -o -z "${PREFIX}" ] && {
echo "usage: $0 <tap-device> <prefix> [<user>]"
exit 1
}

if [ -z "$_USER" ]; then
if [ -n "$SUDO_USER" ] ; then
_USER=$SUDO_USER
else
_USER=$USER
fi
fi

trap "cleanup" INT QUIT TERM EXIT

create_tap && start_uhcpd
28 changes: 5 additions & 23 deletions dist/tools/ethos/start_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,14 @@

ETHOS_DIR="$(dirname $(readlink -f $0))"

create_tap() {
ip tuntap add ${TAP} mode tap user ${USER}
sysctl -w net.ipv6.conf.${TAP}.forwarding=1
sysctl -w net.ipv6.conf.${TAP}.accept_ra=0
ip link set ${TAP} up
ip a a fe80::1/64 dev ${TAP}
ip a a fd00:dead:beef::1/128 dev lo
ip route add ${PREFIX} via fe80::2 dev ${TAP}
}

remove_tap() {
ip tuntap del ${TAP} mode tap
}

cleanup() {
echo "Cleaning up..."
remove_tap
ip a d fd00:dead:beef::1/128 dev lo
kill ${UHCPD_PID}
[ -n "${SETUP_NETWORK_PID}" ] && kill -TERM ${SETUP_NETWORK_PID}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails to kill all the decedent processes, I would suggest this change:

diff --git a/dist/tools/ethos/start_network.sh b/dist/tools/ethos/start_network.sh
index 4f832a258..a457735e0 100755
--- a/dist/tools/ethos/start_network.sh
+++ b/dist/tools/ethos/start_network.sh
@@ -3,13 +3,13 @@
 ETHOS_DIR="$(dirname $(readlink -f $0))"
 
 cleanup() {
-    [ -n "${SETUP_NETWORK_PID}" ] && kill -TERM ${SETUP_NETWORK_PID}
     trap "" INT QUIT TERM EXIT
+    [ -n "${SETUP_NETWORK_GPID}" ] && kill -TERM -$SETUP_NETWORK_GPID
 }
 
 setup_network() {
     ${ETHOS_DIR}/setup_network.sh $1 $2 &
-    SETUP_NETWORK_PID=$!
+    SETUP_NETWORK_GPID=$(ps -o pgid= $! | grep -o '[0-9]*')
 }
 
 PORT=$1

trap "" INT QUIT TERM EXIT
}

start_uhcpd() {
${UHCPD} ${TAP} ${PREFIX} > /dev/null &
UHCPD_PID=$!
setup_network() {
${ETHOS_DIR}/setup_network.sh $* &
SETUP_NETWORK_PID=$!
}

PORT=$1
Expand All @@ -46,5 +29,4 @@ UHCPD="$(readlink -f "${ETHOS_DIR}/../uhcpd/bin")/uhcpd"

trap "cleanup" INT QUIT TERM EXIT


create_tap && start_uhcpd && "${ETHOS_DIR}/ethos" ${TAP} ${PORT} ${BAUDRATE}
setup_network ${TAP} ${PREFIX} && "${ETHOS_DIR}/ethos" ${TAP} ${PORT} ${BAUDRATE}