From f3ad18f64c46e27251772b8daa12f3309db2f11b Mon Sep 17 00:00:00 2001
From: Mike Hibler <mike@flux.utah.edu>
Date: Tue, 19 Dec 2006 23:47:42 +0000
Subject: [PATCH] Doofy program to allow us to check whether netmond is ready
 for connections. We use this in the monitor/auto-start script to ensure the
 monitor is ready before declaring ourselves done.

---
 pelab/libnetmon/GNUmakefile   |  4 ++--
 pelab/libnetmon/netmonup.c    | 19 +++++++++++++++++++
 pelab/make-tarball            |  2 +-
 pelab/monitor/auto-monitor.sh | 19 +++++++++++++++++--
 4 files changed, 39 insertions(+), 5 deletions(-)
 create mode 100644 pelab/libnetmon/netmonup.c

diff --git a/pelab/libnetmon/GNUmakefile b/pelab/libnetmon/GNUmakefile
index 52587bf69f..b5193d9333 100644
--- a/pelab/libnetmon/GNUmakefile
+++ b/pelab/libnetmon/GNUmakefile
@@ -15,10 +15,10 @@ else
     LD_FLAGS=
 endif
 
-all: libnetmon.so netmond
+all: libnetmon.so netmond netmonup
 
 %.so: %.c %.h
 	$(CC) $(CFLAGS) $(LD_FLAGS) -shared -o $@ $<
 
 clean:
-	-rm *.so netmond
+	-rm *.so netmond netmonup
diff --git a/pelab/libnetmon/netmonup.c b/pelab/libnetmon/netmonup.c
new file mode 100644
index 0000000000..b4df33528c
--- /dev/null
+++ b/pelab/libnetmon/netmonup.c
@@ -0,0 +1,19 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+
+/*
+ * Quick hack: just open a socket.
+ * Return 0 if it works, non-zero otherwise.
+ * When run under instrument.sh, will tell us if netmond is ready for traffic.
+ */
+int
+main()
+{
+	int rv = socket(PF_INET, SOCK_DGRAM, 0);
+	if (rv >= 0) {
+		close(rv);
+		exit(0);
+	}
+	exit(rv);
+}
diff --git a/pelab/make-tarball b/pelab/make-tarball
index acde58b761..c115480433 100755
--- a/pelab/make-tarball
+++ b/pelab/make-tarball
@@ -21,7 +21,7 @@ fi
 
 echo "##### cleaning up a little"
 rm -f magent/*.o stub/*.o
-strip magent/magent stub/stubd
+strip magent/magent stub/stubd libnetmon/netmonup
 
 echo "##### creating tarball $path from .."
 tar czf $path -C .. $files
diff --git a/pelab/monitor/auto-monitor.sh b/pelab/monitor/auto-monitor.sh
index cb1a273b8f..59e8ce45d3 100644
--- a/pelab/monitor/auto-monitor.sh
+++ b/pelab/monitor/auto-monitor.sh
@@ -9,6 +9,11 @@ ARGS=$*
 
 . `dirname $0`/../common-env.sh
 
+#
+# How long to wait for netmond.
+#
+NETMOND_TIMO=60
+
 #
 # Wait for all of the stubs to start
 #
@@ -29,9 +34,19 @@ MONPID=$!
 trap "{ $AS_ROOT kill $MONPID; $AS_ROOT killall netmond; true; }" TERM
 
 #
-# Give it time to come up
+# Make sure it is ready to receive requests.
+# We do this with a simple program that just opens a socket.
+# When run under libnetmon, it will fail if netmond is not yet running.
 #
-sleep 1
+if [ -x ${NETMON_DIR}/netmonup ]; then
+    LIBNETMON_CONNECTTIMO=$NETMOND_TIMO \
+	${MONITOR_DIR}/instrument.sh ${NETMON_DIR}/netmonup
+    if [ $? -ne 0 ]; then
+        echo "**** WARNING: netmond failed to start after $NETMOND_TIMO seconds"
+    fi
+else
+    sleep 2
+fi
 
 #
 # Wait for all the monitors to come up
-- 
GitLab