From 0472de45f7bbb1a6acf1c15e376f1f40b909580a Mon Sep 17 00:00:00 2001 From: "Fabio M. Di Nitto" Date: Wed, 24 Oct 2012 16:35:37 +0200 Subject: [PATCH] general: add init script and service file Signed-off-by: Fabio M. Di Nitto --- Makefile.am | 2 +- TODO | 2 +- configure.ac | 14 ++++ init/Makefile.am | 23 ++++++ init/kronosnetd.in | 140 +++++++++++++++++++++++++++++++++++++ init/kronosnetd.service.in | 12 ++++ 6 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 init/Makefile.am create mode 100644 init/kronosnetd.in create mode 100644 init/kronosnetd.service.in diff --git a/Makefile.am b/Makefile.am index a389ebe..b887de8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,7 +7,7 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = libtap libknet kronosnetd tests +SUBDIRS = init libtap libknet kronosnetd tests if BUILD_DOCS SUBDIRS += docs diff --git a/TODO b/TODO index 943c2e4..4e705b1 100644 --- a/TODO +++ b/TODO @@ -57,7 +57,7 @@ libtap: general: * add statistics -* add init script / spec file +* add spec file * v4/v6 (bindv6only check via cli) vty: diff --git a/configure.ac b/configure.ac index 4aecaf8..c5fac43 100644 --- a/configure.ac +++ b/configure.ac @@ -146,6 +146,16 @@ AC_ARG_ENABLE([publicandocs], [ --enable-publicandocs enable docs build. ], [ default="no" ]) +AC_ARG_WITH([initddir], + [ --with-initddir=DIR : path to init script directory. ], + [ INITDDIR="$withval" ], + [ INITDDIR="$sysconfdir/init.d" ]) + +AC_ARG_WITH([systemddir], + [ --with-systemddir=DIR : path to systemd unit files directory. ], + [ SYSTEMDDIR="$withval" ], + [ SYSTEMDDIR="/lib/systemd/system" ]) + AC_ARG_WITH([syslogfacility], [ --with-syslogfacility=FACILITY default syslog facility. ], @@ -176,6 +186,9 @@ DEFAULT_CONFIG_DIR=${sysconfdir}/kronosnet AM_CONDITIONAL([BUILD_DOCS], [test "x${enable_publicandocs}" = xyes]) AC_SUBST([DEFAULT_CONFIG_DIR]) +AC_SUBST([INITDDIR]) +AC_SUBST([SYSTEMDDIR]) + AC_DEFINE_UNQUOTED([DEFAULT_CONFIG_DIR], ["$(eval echo ${DEFAULT_CONFIG_DIR})"], [Default config directory]) @@ -278,6 +291,7 @@ LDFLAGS="$ENV_LDFLAGS $lt_prog_compiler_pic" AC_CONFIG_FILES([ Makefile + init/Makefile libtap/Makefile libtap/libtap.pc kronosnetd/Makefile diff --git a/init/Makefile.am b/init/Makefile.am new file mode 100644 index 0000000..cc3c73d --- /dev/null +++ b/init/Makefile.am @@ -0,0 +1,23 @@ +MAINTAINERCLEANFILES = Makefile.in + +EXTRA_DIST = kronosnetd.in kronosnetd.service.in + +systemdconfdir = $(SYSTEMDDIR) +systemdconf_DATA = kronosnetd.service +initscriptdir = $(INITDDIR) +initscript_SCRIPTS = kronosnetd + +%: %.in Makefile + rm -f $@-t $@ + cat $< | sed \ + -e 's#@''SBINDIR@#$(sbindir)#g' \ + -e 's#@''SYSCONFDIR@#$(sysconfdir)#g' \ + -e 's#@''INITDDIR@#$(INITDDIR)#g' \ + -e 's#@''LOCALSTATEDIR@#$(localstatedir)#g' \ + > $@-t + mv $@-t $@ + +all-local: $(initscript_SCRIPTS) $(systemdconf_DATA) + +clean-local: + rm -rf $(initscript_SCRIPTS) $(systemdconf_DATA) diff --git a/init/kronosnetd.in b/init/kronosnetd.in new file mode 100644 index 0000000..a8fbc73 --- /dev/null +++ b/init/kronosnetd.in @@ -0,0 +1,140 @@ +#!/bin/bash + +# chkconfig: - 20 80 +# description: kronosnetd vpn daemon +# processname: kronosnetd +# +### BEGIN INIT INFO +# Provides: kronosnetd +# Required-Start: $network $syslog +# Required-Stop: $network $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Starts and stops kronosnetd vpn daemon. +# Description: Starts and stops kronosnetd vpn daemon. +### END INIT INFO + +desc="kronosnetd" +prog="kronosnetd" + +# set secure PATH +PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@" + +success() +{ + echo -ne "[ OK ]\r" +} + +failure() +{ + echo -ne "[FAILED]\r" +} + +status() +{ + pid=$(pidof $1 2>/dev/null) + rtrn=$? + if [ $rtrn -ne 0 ]; then + echo "$1 is stopped" + else + echo "$1 (pid $pid) is running..." + fi + return $rtrn +} + +# rpm based distros +if [ -d @SYSCONFDIR@/sysconfig ]; then + [ -f @INITDDIR@/functions ] && . @INITDDIR@/functions + [ -f @SYSCONFDIR@/sysconfig/$prog ] && . @SYSCONFDIR@/sysconfig/$prog + [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/subsys/$prog" +fi + +# deb based distros +if [ -d @SYSCONFDIR@/default ]; then + [ -f @SYSCONFDIR@/default/$prog ] && . @SYSCONFDIR@/default/$prog + [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/$prog" +fi + +# The version of __pids_pidof in /etc/init.d/functions calls pidof with -x +# This means it matches scripts, including this one. +# Redefine it here so that status (from the same file) works. +# Otherwise simultaneous calls to stop() will loop forever +__pids_pidof() { + pidof -c -o $$ -o $PPID -o %PPID "$1" || \ + pidof -c -o $$ -o $PPID -o %PPID "${1##*/}" +} + +start() +{ + echo -n "Starting $desc ($prog): " + + # most recent distributions use tmpfs for @LOCALSTATEDIR@/run + # to avoid to clean it up on every boot. + # they also assume that init scripts will create + # required subdirectories for proper operations + mkdir -p @LOCALSTATEDIR@/run + + if status $prog > /dev/null 2>&1; then + success + else + $prog $KNETD_OPTS > /dev/null 2>&1 + touch $LOCK_FILE + success + fi + echo +} + +stop() +{ + ! status $prog > /dev/null 2>&1 && return + + echo -n "Signaling $desc ($prog) to terminate: " + kill -TERM $(pidof $prog) > /dev/null 2>&1 + success + echo + + echo -n "Waiting for $prog to unload:" + while status $prog > /dev/null 2>&1; do + sleep 1 + echo -n "." + done + + rm -f $LOCK_FILE + success + echo +} + +restart() +{ + stop + start +} + +rtrn=0 + +case "$1" in +start) + start +;; +restart|reload|force-reload) + restart +;; +condrestart|try-restart) + if status $prog > /dev/null 2>&1; then + restart + fi +;; +status) + status $prog + rtrn=$? +;; +stop) + stop +;; +*) + echo "usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}" + rtrn=2 +;; +esac + +exit $rtrn diff --git a/init/kronosnetd.service.in b/init/kronosnetd.service.in new file mode 100644 index 0000000..866ee29 --- /dev/null +++ b/init/kronosnetd.service.in @@ -0,0 +1,12 @@ +[Unit] +Description=kronosnetd +Requires=network.target +After=network.target syslog.target + +[Service] +Type=forking +EnvironmentFile=/etc/sysconfig/kronosnetd +ExecStart=@SBINDIR@/kronosnetd + +[Install] +WantedBy=multi-user.target -- 2.39.5