tests: Wait until netopeer exits before cleaning up shm

Yay for tail!

Change-Id: I30aab3b1c49386ba95e361c850db9e3e457a7e18
diff --git a/tests/cleanup_datastore.bash.in b/tests/cleanup_datastore.bash.in
index 66df88c..835ab88 100755
--- a/tests/cleanup_datastore.bash.in
+++ b/tests/cleanup_datastore.bash.in
@@ -17,7 +17,17 @@
     # line, including stuff set by `exec -a`. Otherwise it matches the name in
     # /proc/{pid}/stat and that is usually limited to 15 characters, so
     # netopeer2-server appears as netopeer2-serve
+    NETOPEER_PID="$(pgrep -f "${SYSREPO_SHM_PREFIX}_netopeer2-server")"
     pkill -f "${SYSREPO_SHM_PREFIX}_netopeer2-server"
+
+    # Wait until netopeer actually exits, because I don't want to remove its repository/shm files while it is still
+    # running.
+    #
+    # One would think, that this is a job for the `wait` command, but that one unfortunately only works for child
+    # processes of the current shell. The `tail` command does implement this mechanism, so I can use that.
+    # https://stackoverflow.com/a/41613532
+    tail --pid="$NETOPEER_PID" -f /dev/null
+
     rm "$NETOPEER_SOCKET"
 fi