From 8a679892b9e6e96b10dfeb16d1caacb096bc116f Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 21 Sep 2015 00:48:24 +0200 Subject: improved install routines (added systemd service unit and initscript --- contrib/dropnroll-launcher | 28 +++++++ contrib/etc/dropnroll/autostart | 2 + contrib/etc/dropnroll/sample/config | 31 +++++++ contrib/etc/dropnroll/sample/newfile.sh | 15 ++++ contrib/initscript | 141 ++++++++++++++++++++++++++++++++ contrib/systemd.service | 9 ++ etc/dropnroll/autostart | 2 - etc/dropnroll/sample/config | 32 -------- etc/dropnroll/sample/newfile.sh | 15 ---- src/Makefile | 27 ++++-- src/configure | 29 +++++-- 11 files changed, 272 insertions(+), 59 deletions(-) create mode 100755 contrib/dropnroll-launcher create mode 100644 contrib/etc/dropnroll/autostart create mode 100644 contrib/etc/dropnroll/sample/config create mode 100755 contrib/etc/dropnroll/sample/newfile.sh create mode 100755 contrib/initscript create mode 100644 contrib/systemd.service delete mode 100644 etc/dropnroll/autostart delete mode 100644 etc/dropnroll/sample/config delete mode 100755 etc/dropnroll/sample/newfile.sh diff --git a/contrib/dropnroll-launcher b/contrib/dropnroll-launcher new file mode 100755 index 0000000..f0569f3 --- /dev/null +++ b/contrib/dropnroll-launcher @@ -0,0 +1,28 @@ +#! /bin/sh + +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +INST="$1" +NAME=dropnroll +CONFIG_DIR="/etc/$NAME" +VARRUN_DIR=/var/run/$NAME + +if [ -d "$CONFIG_DIR/$INST/" ] && [ -e "$CONFIG_DIR/$INST/config" ]; then + NEWFILE='' + test -f $CONFIG_DIR/$INST/newfile.sh && NEWFILE="-x $CONFIG_DIR/$INST/newfile.sh" + CHROOTDIR=`grep '^chroot' < $CONFIG_DIR/$INST/config | sed 's/chroot\s*//'` + if [ -n "$CHROOTDIR" ] ; then + test -d $CHROOTDIR || mkdir -p $CHROOTDIR + fi + test -d $VARRUN_DIR || mkdir -p $VARRUN_DIR + DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$INST/config | grep -e '\w' | sed 's/^/--/' | tr '\n' ' '` + exec dropnroll -D --write-pid $VARRUN_DIR/$INST.pid $NEWFILE $DAEMONARG +else + echo "Instance config directory does not exist or does not contain a config file" + exit 1 +fi + +exit 1 # should not be reached unless exec returns because of an error diff --git a/contrib/etc/dropnroll/autostart b/contrib/etc/dropnroll/autostart new file mode 100644 index 0000000..067d2c0 --- /dev/null +++ b/contrib/etc/dropnroll/autostart @@ -0,0 +1,2 @@ +sample +#disabled diff --git a/contrib/etc/dropnroll/sample/config b/contrib/etc/dropnroll/sample/config new file mode 100644 index 0000000..5c90d94 --- /dev/null +++ b/contrib/etc/dropnroll/sample/config @@ -0,0 +1,31 @@ +############################# +## Main options # +############################# + +## the path to the command socket +command-sock /var/run/dropnroll/sample.sock + +## Maximum Number of children (scripts) to call at once +max-children 8 + +## What to do when new files trigger the children limit +children-policy defer + +## Statically configured directories to watch +#dir /some/path +#dir /yet/another/path + + +############################# +## Expert options # +############################# + +## log to syslog with a level of 3 +log syslog:3,dropnroll-sample,daemon + +## change user and group after init +#username nobody +#groupname nogroup + +## chroot to users home directory +#chroot /var/run/dropnroll diff --git a/contrib/etc/dropnroll/sample/newfile.sh b/contrib/etc/dropnroll/sample/newfile.sh new file mode 100755 index 0000000..600b5f1 --- /dev/null +++ b/contrib/etc/dropnroll/sample/newfile.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +DIR=$1 +FILENAME=$2 +SIZE=$3 + +if [ -z "$FILENAME" ]; then + exit 1; +fi + +echo "new file of size $SIZE detected: $DIR/$FILENAME" >> /tmp/dropnroll-sample.log +sleep 5 +rm -f $DIR/$FILENAME >> /tmp/dropnroll-sample.log 2>&1 + +exit 0 diff --git a/contrib/initscript b/contrib/initscript new file mode 100755 index 0000000..aadf074 --- /dev/null +++ b/contrib/initscript @@ -0,0 +1,141 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: dropnroll +# Required-Start: $syslog +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start dropnroll daemon at boot time +# Description: Waits for files in folders and calls script for new files +### END INIT INFO +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +DAEMON=/usr/bin/dropnroll +CONFIG_DIR=/etc/dropnroll +NAME=dropnroll +DESC=dropnroll +VARRUN_DIR=/var/run/$NAME + +test -x $DAEMON || exit 0 + +# Include dropnroll defaults if available +if [ -f /etc/default/dropnroll ] ; then + . /etc/default/dropnroll +fi + +. /lib/lsb/init-functions + +start_dnr () { + STATUS="OK" + if [ -f $CONFIG_DIR/$DNRNAME/config ] ; then + NEWFILE='' + test -f $CONFIG_DIR/$DNRNAME/newfile.sh && NEWFILE="-x $CONFIG_DIR/$DNRNAME/newfile.sh" + CHROOTDIR=`grep '^chroot' < $CONFIG_DIR/$DNRNAME/config | sed 's/chroot\s*//'` + if [ -n "$CHROOTDIR" ] ; then + test -d $CHROOTDIR || mkdir -p $CHROOTDIR + fi + test -d $VARRUN_DIR || mkdir -p $VARRUN_DIR + chmod 777 $VARRUN_DIR + DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$DNRNAME/config | grep -e '\w' | sed 's/^/--/' | tr '\n' ' '` + $DAEMON --write-pid $VARRUN_DIR/$DNRNAME.pid $NEWFILE $DAEMONOPTS $DAEMONARG || STATUS="FAILED" + else + STATUS="no config found" + fi + echo -n "($STATUS)" +} + +stop_dnr () { + kill `cat $PIDFILE` || true + rm $PIDFILE +} + +set -e +case "$1" in + start) + echo -n "Starting $DESC:" + if test -z "$2" ; then + if [ -f $CONFIG_DIR/autostart ] ; then + for DNRNAME in `sed 's/#.*//' < $CONFIG_DIR/autostart | grep -e '\w'`; do + echo -n " $DNRNAME" + start_dnr + done + else + echo " no config found" + exit 1; + fi + else + while shift ; do + [ -z "$1" ] && break + DNRNAME=$1 + echo -n " $DNRNAME" + start_dnr + done + fi + echo "." + ;; + stop) + echo -n "Stoping $DESC:" + if test -z "$2" ; then + for PIDFILE in `ls $VARRUN_DIR/*.pid 2> /dev/null`; do + DNRNAME=`echo $PIDFILE | cut -c20-` + DNRNAME=${DNRNAME%%.pid} + echo -n " $DNRNAME" + stop_dnr + done + else + while shift ; do + [ -z "$1" ] && break + if test -e $VARRUN_DIR/$1.pid ; then + PIDFILE=`ls $VARRUN_DIR/$1.pid 2> /dev/null` + DNRNAME=`echo $PIDFILE | cut -c20-` + DNRNAME=${DNRNAME%%.pid} + echo -n " $DNRNAME" + stop_dnr + else + echo -n " (failure: No such tunnel is running: $1)" + fi + done + fi + echo "." + ;; + reload) + echo -n "Reloading $DESC:" + if test -z "$2" ; then + for PIDFILE in `ls $VARRUN_DIR/*.pid 2> /dev/null`; do + DNRNAME=`echo $PIDFILE | cut -c20-` + DNRNAME=${DNRNAME%%.pid} + echo -n " $DNRNAME" + stop_dnr + start_dnr + done + else + while shift ; do + [ -z "$1" ] && break + if test -e $VARRUN_DIR/$1.pid ; then + PIDFILE=`ls $VARRUN_DIR/$1.pid 2> /dev/null` + DNRNAME=`echo $PIDFILE | cut -c20-` + DNRNAME=${DNRNAME%%.pid} + echo -n " $DNRNAME" + stop_dnr + start_dnr + else + echo -n " (failure: No such tunnel is running: $1)" + fi + done + fi + echo "." + ;; + restart) + SCRIPT=$0 + shift + $SCRIPT stop $* + sleep 1 + $SCRIPT start $* + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|restart|reload}" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/contrib/systemd.service b/contrib/systemd.service new file mode 100644 index 0000000..af48071 --- /dev/null +++ b/contrib/systemd.service @@ -0,0 +1,9 @@ +[Unit] +Description=Drop'N'Roll Daemon (%i) + +[Service] +ExecStart=/usr/bin/dropnroll-launcher %i +PrivateDevices=yes + +[Install] +WantedBy=multi-user.target diff --git a/etc/dropnroll/autostart b/etc/dropnroll/autostart deleted file mode 100644 index 067d2c0..0000000 --- a/etc/dropnroll/autostart +++ /dev/null @@ -1,2 +0,0 @@ -sample -#disabled diff --git a/etc/dropnroll/sample/config b/etc/dropnroll/sample/config deleted file mode 100644 index 98758be..0000000 --- a/etc/dropnroll/sample/config +++ /dev/null @@ -1,32 +0,0 @@ -############################# -## Main options # -############################# - -## the path to the command socket -command-sock /var/run/dropnroll/sample.sock - -## change user and group after init -#username nobody -#groupname nogroup - - -############################# -## Debug options # -############################# - -## don't run in background -#nodaemonize - -## additional log to standard output with a level of 5 -#log stdout:5 - - -############################# -## Expert options # -############################# - -## log to syslog with a level of 3 -log syslog:3,dropnroll-sample,daemon - -## chroot to users home directory -#chroot /var/run/dropnroll diff --git a/etc/dropnroll/sample/newfile.sh b/etc/dropnroll/sample/newfile.sh deleted file mode 100755 index 600b5f1..0000000 --- a/etc/dropnroll/sample/newfile.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -DIR=$1 -FILENAME=$2 -SIZE=$3 - -if [ -z "$FILENAME" ]; then - exit 1; -fi - -echo "new file of size $SIZE detected: $DIR/$FILENAME" >> /tmp/dropnroll-sample.log -sleep 5 -rm -f $DIR/$FILENAME >> /tmp/dropnroll-sample.log 2>&1 - -exit 0 diff --git a/src/Makefile b/src/Makefile index 77619cf..886872e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -79,6 +79,11 @@ manpage: INSTALL_TARGETS := install-bin install-etc REMOVE_TARGETS := remove-bin remove-etc +ifdef SYSTEMDDIR +INSTALL_TARGETS += install-systemd +REMOVE_TARGETS += remove-systemd +endif + ifdef MANDIR INSTALL_TARGETS += install-man REMOVE_TARGETS += remove-man @@ -95,7 +100,15 @@ install-bin: $(EXECUTABLE) $(INSTALL) -d $(DESTDIR)$(BINDIR) $(INSTALL) -m 755 $(EXECUTABLE) $(DESTDIR)$(BINDIR) +install-systemd: + $(INSTALL) -d $(DESTDIR)$(SYSTEMDDIR) + $(INSTALL) -m 644 ../contrib/systemd.service $(DESTDIR)$(SYSTEMDDIR)/$(EXECUTABLE).service + $(INSTALL) -d $(DESTDIR)$(BINDIR) + $(INSTALL) -m 755 ../contrib/$(EXECUTABLE)-launcher $(DESTDIR)$(BINDIR) + install-etc: + $(INSTALL) -d $(DESTDIR)$(ETCDIR)/init.d/ + $(INSTALL) -m 755 ../contrib/initscript $(DESTDIR)$(ETCDIR)/init.d/$(EXECUTABLE) $(INSTALL) -d $(DESTDIR)$(ETCDIR)/$(EXECUTABLE) @ echo "example configurations can be found at $(EXAMPLESDIR)/$(EXECUTABLE)" > $(DESTDIR)$(ETCDIR)/$(EXECUTABLE)/README @@ -105,8 +118,8 @@ install-man: manpage install-examples: $(INSTALL) -d $(DESTDIR)$(EXAMPLESDIR)/$(EXECUTABLE) - $(INSTALL) -m 644 etc/dropnroll/autostart $(DESTDIR)$(EXAMPLESDIR)/$(EXECUTABLE)/autostart - @( cd 'etc/dropnroll/' ; \ + $(INSTALL) -m 644 ../contrib/etc/dropnroll/autostart $(DESTDIR)$(EXAMPLESDIR)/$(EXECUTABLE)/autostart + @( cd '../contrib/etc/dropnroll/' ; \ for dir in `ls`; do \ if [ -d $$dir ]; then \ echo "install $$dir configuration" ; \ @@ -128,13 +141,17 @@ remove: $(REMOVE_TARGETS) remove-bin: rm -f $(DESTDIR)$(BINDIR)/$(EXECUTABLE) -remove-etc: +remove-systemd: + rm -f $(DESTDIR)$(SYSTEMDDIR)/$(EXECUTABLE).service -remove-examples: - rm -rf $(DESTDIR)$(EXAMPLESDIR)/$(EXECUTABLE)/ +remove-etc: + rm -f $(DESTDIR)$(ETCDIR)/init.d/$(EXECUTABLE) remove-man: rm -f $(DESTDIR)$(MANDIR)/man8/$(EXECUTABLE).8 +remove-examples: + rm -rf $(DESTDIR)$(EXAMPLESDIR)/$(EXECUTABLE)/ + purge: remove rm -rf $(DESTDIR)$(ETCDIR)/$(EXECUTABLE)/ diff --git a/src/configure b/src/configure index 50e3239..f092fef 100755 --- a/src/configure +++ b/src/configure @@ -26,15 +26,14 @@ EBUILD_COMPAT=0 USE_CLANG=0 -CFLAGS='-g -O2' -LDFLAGS='-g -Wall -O2' - PREFIX='/usr/local' BINDIR='' ETCDIR='' +SYSTEMDDIR='' +INSTALLSYSTEMD=1 MANDIR='' INSTALLMANPAGE=1 -EXAMPLESDIR='../' +EXAMPLESDIR='' INSTALLEXAMPLES=1 print_usage() { @@ -43,10 +42,13 @@ print_usage() { echo " --prefix= the installation prefix (default: /usr/local)" echo " --bindir= the path to the bin directory (default: $PREFIX/bin)" echo " --sysconfdir= the path to the system configuration directory (default: $PREFIX/etc" + echo " --systemddir= the path to the systemd service unit directory (default: /usr/lib/systemd/system)" + echo " --no-systemd dont't install systemd service unit" echo " --mandir= the path to the system man pages (default: $PREFIX/share/man)" echo " --no-manpage dont't install manpage" echo " --examplesdir= the path to the examples files (default: $PREFIX/share/examples)" echo " --no-examples dont't install example files" + echo " --use-clang use clang/llvm as compiler/linker" } for arg @@ -67,6 +69,12 @@ do --sysconfdir=*) ETCDIR=${arg#--sysconfdir=} ;; + --systemddir=*) + SYSTEMDDIR=${arg#--systemddir=} + ;; + --no-systemd) + INSTALLSYSTEMD=0 + ;; --mandir=*) MANDIR=${arg#--mandir=} ;; @@ -123,7 +131,7 @@ case $TARGET in LDFLAGS=$LDFLAGS' -L/usr/local/lib' ;; *) - echo "Plattform not supported" + echo "platform not supported" exit 1; ;; esac @@ -132,6 +140,10 @@ if [ -z "$BINDIR" ]; then BINDIR=$PREFIX/bin fi +if [ -z "$SYSTEMDDIR" ]; then + SYSTEMDDIR=/usr/lib/systemd/system +fi + if [ -z "$ETCDIR" ]; then ETCDIR=$PREFIX/etc fi @@ -160,6 +172,13 @@ BINDIR := $BINDIR ETCDIR := $ETCDIR EOF +if [ $INSTALLSYSTEMD -eq 1 ]; then + echo "SYSTEMDDIR := $SYSTEMDDIR" >> include.mk + echo "installing systemd service unit" +else + echo "not installing system service unit" +fi + if [ $INSTALLMANPAGE -eq 1 ]; then echo "MANDIR := $MANDIR" >> include.mk echo "installing manpage" -- cgit v1.2.3