osmocom-tcpdump: Add a tcpdump wrapper that logs to finite space

tcpdump can create a new log file every couple of bytes, it can also
be asked to limit the number of logfiles and wraps the counter back to
zero, and at least tcpdump 4.1 can also compress the log file with an
external script. Create a start script around this feature to log to
a given directory but not use more space than allowed to. The script
was created by Jan Luebbe and then modified.
diff --git a/recipes-osmocom/osmocom-tcpdump/files/osmocom-tcpdump b/recipes-osmocom/osmocom-tcpdump/files/osmocom-tcpdump
new file mode 100755
index 0000000..5886c29
--- /dev/null
+++ b/recipes-osmocom/osmocom-tcpdump/files/osmocom-tcpdump
@@ -0,0 +1,111 @@
+#!/bin/sh
+### BEGIN INIT INFO
+# Provides:          on-waves-capture
+# Required-Start:    $remote_fs $network
+# Should-Start:      udev
+# Required-Stop:     $remote_fs $network
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+### END INIT INFO
+
+# Do NOT "set -e"
+
+# PATH should only include /usr/* if it runs after the mountnfs.sh script
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="Osmocom traffic capture"
+NAME="tcpdump"
+DAEMON=/usr/sbin/$NAME
+PIDFILE=/var/run/osmocom-tcpdump-$NAME.pid
+SCRIPTNAME=/etc/init.d/osmocom-tcpdump
+
+
+CAPTURE_ENABLED="0"
+CAPTURE_LIMIT="8"
+CAPTURE_SIZE="1"
+CAPTURE_BASENAME="/tmp/tcpdump-capture-"
+CAPTURE_COMPRESS="-z /usr/sbin/tcpdump-compress"
+
+if [ -e /etc/default/osmocom-tcpdump ]; then
+	. /etc/default/osmocom-tcpdump
+fi
+
+
+CAPTURE_OPTIONS="-pi $CAPTURE_DEVICE -s0 -w $CAPTURE_BASENAME -C $CAPTURE_SIZE -W $CAPTURE_LIMIT $CAPTURE_COMPRESS"
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+	# Return
+	#   0 if daemon has been started
+	#   1 if daemon was already running
+	#   2 if daemon could not be started
+	if [ -z "$CAPTURE_DEVICE" -o -z "$CAPTURE_FILTER" ]; then
+		echo "Unable to start $DESC" "$NAME" "(unconfigured)"
+		exit 2
+	fi
+	if [ "$CAPTURE_ENABLED" != "1" ]; then
+		echo "Not starting $DESC" "$NAME" "(disabled)"
+		exit 0
+	fi
+	#start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
+	#	|| return 1
+	echo "Starting $DESC" "$NAME"
+	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
+		--background --make-pidfile -- $CAPTURE_OPTIONS $CAPTURE_FILTER \
+		|| return 2
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+	# Return
+	#   0 if daemon has been stopped
+	#   1 if daemon was already stopped
+	#   2 if daemon could not be stopped
+	#   other if a failure occurred
+	echo "Stopping $DESC" "$NAME"
+	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
+	RETVAL="$?"
+	[ "$RETVAL" = 2 ] && return 2
+	# Many daemons don't delete their pidfiles when they exit.
+	rm -f $PIDFILE
+	return "$RETVAL"
+}
+
+case "$1" in
+  start)
+	do_start
+	exit $?
+	;;
+  stop)
+	do_stop
+	exit $?
+	;;
+  restart)
+	#
+	# If the "reload" option is implemented then remove the
+	# 'force-reload' alias
+	#
+	do_stop
+	case "$?" in
+	  0|1)
+		do_start
+		exit $?
+		;;
+	  *)
+		# Failed to stop
+		exit 1
+		;;
+	esac
+	;;
+  *)
+	echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
+	exit 3
+	;;
+esac
+
+:
diff --git a/recipes-osmocom/osmocom-tcpdump/files/tcpdump-compress b/recipes-osmocom/osmocom-tcpdump/files/tcpdump-compress
new file mode 100755
index 0000000..0a55605
--- /dev/null
+++ b/recipes-osmocom/osmocom-tcpdump/files/tcpdump-compress
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+rm -f $1.pcap.gz || true
+gzip -c $1 > $1.pcap.gz && rm -f $1 || true
diff --git a/recipes-osmocom/osmocom-tcpdump/osmocom-tcpdump_1.0.bb b/recipes-osmocom/osmocom-tcpdump/osmocom-tcpdump_1.0.bb
new file mode 100644
index 0000000..b86c8e3
--- /dev/null
+++ b/recipes-osmocom/osmocom-tcpdump/osmocom-tcpdump_1.0.bb
@@ -0,0 +1,25 @@
+DESCRIPTION = "Osmocom round-robin tcpdump/capture"
+LICENSE = "GPLv3+"
+RDEPENDS = "tcpdump bash"
+PR = "r10"
+
+LIC_FILES_CHKSUM = "file://${COREBASE}/LICENSE;md5=3f40d7994397109285ec7b81fdeb3b58"
+
+SRC_URI = " \
+  file://osmocom-tcpdump \
+  file://tcpdump-compress \
+"
+
+do_install () {
+  install -d ${D}${sysconfdir}/init.d
+  install -d ${D}${sysconfdir}/default
+  install -d ${D}${sbindir}
+
+  install -m 0755	${WORKDIR}/osmocom-tcpdump		${D}${sysconfdir}/init.d/
+  install -m 0755	${WORKDIR}/tcpdump-compress		${D}${sbindir}/
+}
+
+inherit update-rc.d
+
+INITSCRIPT_NAME = "osmocom-tcpdump"
+INITSCRIPT_PARAMS = "defaults 21"