From 8612d336918b408fd3abd844ccb8189f0d64acc5 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@openwrt.org>
Date: Sun, 15 Oct 2006 21:03:30 +0000
Subject: [PATCH] init script cleanup, use /etc/rc.d/ for enabled scripts,
 /etc/init.d/<pkgname> (enable|disable) manages symlinks

SVN-Revision: 5128
---
 package/Makefile                              |   7 +
 .../ar7-2.4/etc/init.d/{S00adam2 => adam2}    |   1 +
 .../etc/init.d/{S05netconfig => netconfig}    |   2 +
 .../etc/init.d/{S05netconfig => netconfig}    |   1 +
 .../default/etc/init.d/{S10boot => boot}      |   1 +
 .../default/etc/init.d/{S60cron => cron}      |   0
 .../default/etc/init.d/{S50httpd => httpd}    |   0
 .../etc/init.d/{S40network => network}        |   1 +
 package/base-files/default/etc/init.d/rcS     |   4 +-
 .../default/etc/init.d/{S50telnet => telnet}  |   0
 package/base-files/default/etc/rc.common      |  16 +-
 package/dnsmasq/Makefile                      |   2 +-
 package/dropbear/Makefile                     |   2 +-
 package/iptables/Makefile                     |   2 +-
 package/iptables/files/firewall.init          |   2 +-
 package/madwifi/Makefile                      |   2 +-
 package/madwifi/files/madwifi.init            |   3 +-
 package/openswan/Makefile                     |   4 +-
 package/openswan/files/ipsec.init             | 158 ++++++++++++++++++
 .../files/etc/init.d/{S50qos => qos}          |   8 +-
 20 files changed, 197 insertions(+), 19 deletions(-)
 rename package/base-files/ar7-2.4/etc/init.d/{S00adam2 => adam2} (97%)
 rename package/base-files/brcm-2.4/etc/init.d/{S05netconfig => netconfig} (99%)
 rename package/base-files/brcm-2.6/etc/init.d/{S05netconfig => netconfig} (99%)
 rename package/base-files/default/etc/init.d/{S10boot => boot} (98%)
 rename package/base-files/default/etc/init.d/{S60cron => cron} (100%)
 rename package/base-files/default/etc/init.d/{S50httpd => httpd} (100%)
 rename package/base-files/default/etc/init.d/{S40network => network} (96%)
 rename package/base-files/default/etc/init.d/{S50telnet => telnet} (100%)
 create mode 100755 package/openswan/files/ipsec.init
 rename package/qos-scripts/files/etc/init.d/{S50qos => qos} (70%)

diff --git a/package/Makefile b/package/Makefile
index 304343a2aa..d1605afe9f 100644
--- a/package/Makefile
+++ b/package/Makefile
@@ -55,6 +55,13 @@ install-targets: base-files-install $(INSTALL_PACKAGES)
 install:
 	rm -rf $(BUILD_DIR)/root
 	$(MAKE) install-targets
+	@( \
+		cd $(BUILD_DIR)/root; \
+		for script in ./etc/init.d/*; do \
+			grep '#!/bin/sh /etc/rc.common' $$script >/dev/null || continue; \
+			IPKG_INSTROOT=$(BUILD_DIR)/root $(which bash) ./etc/rc.common $$script enable; \
+		done; \
+	)
 
 index: $(PACKAGE_DIR)/Packages
 
diff --git a/package/base-files/ar7-2.4/etc/init.d/S00adam2 b/package/base-files/ar7-2.4/etc/init.d/adam2
similarity index 97%
rename from package/base-files/ar7-2.4/etc/init.d/S00adam2
rename to package/base-files/ar7-2.4/etc/init.d/adam2
index 5f5b3504c2..425bc15d69 100755
--- a/package/base-files/ar7-2.4/etc/init.d/S00adam2
+++ b/package/base-files/ar7-2.4/etc/init.d/adam2
@@ -2,6 +2,7 @@
 # ADAM2 patcher for Netgear DG834 and compatible
 # Copyright (C) 2006 OpenWrt.org
 
+START=00
 start() {
 	MD5="$(md5sum /dev/mtdblock/0  | awk '{print $1}')"
 	[ "$MD5" = "0530bfdf00ec155f4182afd70da028c1" ] && {
diff --git a/package/base-files/brcm-2.4/etc/init.d/S05netconfig b/package/base-files/brcm-2.4/etc/init.d/netconfig
similarity index 99%
rename from package/base-files/brcm-2.4/etc/init.d/S05netconfig
rename to package/base-files/brcm-2.4/etc/init.d/netconfig
index 7c36183896..ca52ee7c59 100755
--- a/package/base-files/brcm-2.4/etc/init.d/S05netconfig
+++ b/package/base-files/brcm-2.4/etc/init.d/netconfig
@@ -1,6 +1,8 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2006 OpenWrt.org
 
+START=05
+
 start() {
 	[ -e /etc/config/network ] && exit 0
 
diff --git a/package/base-files/brcm-2.6/etc/init.d/S05netconfig b/package/base-files/brcm-2.6/etc/init.d/netconfig
similarity index 99%
rename from package/base-files/brcm-2.6/etc/init.d/S05netconfig
rename to package/base-files/brcm-2.6/etc/init.d/netconfig
index 846562690e..7a818dc5dd 100755
--- a/package/base-files/brcm-2.6/etc/init.d/S05netconfig
+++ b/package/base-files/brcm-2.6/etc/init.d/netconfig
@@ -1,6 +1,7 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2006 OpenWrt.org
 
+START=05
 start() {
 	[ -e /etc/config/network ] && exit 0
 	
diff --git a/package/base-files/default/etc/init.d/S10boot b/package/base-files/default/etc/init.d/boot
similarity index 98%
rename from package/base-files/default/etc/init.d/S10boot
rename to package/base-files/default/etc/init.d/boot
index 77b5ca72d5..a5c8603a5d 100755
--- a/package/base-files/default/etc/init.d/S10boot
+++ b/package/base-files/default/etc/init.d/boot
@@ -1,6 +1,7 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2006 OpenWrt.org
 
+START=10
 start() {
 	[ -f /proc/mounts ] || /sbin/mount_root
 	[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc
diff --git a/package/base-files/default/etc/init.d/S60cron b/package/base-files/default/etc/init.d/cron
similarity index 100%
rename from package/base-files/default/etc/init.d/S60cron
rename to package/base-files/default/etc/init.d/cron
diff --git a/package/base-files/default/etc/init.d/S50httpd b/package/base-files/default/etc/init.d/httpd
similarity index 100%
rename from package/base-files/default/etc/init.d/S50httpd
rename to package/base-files/default/etc/init.d/httpd
diff --git a/package/base-files/default/etc/init.d/S40network b/package/base-files/default/etc/init.d/network
similarity index 96%
rename from package/base-files/default/etc/init.d/S40network
rename to package/base-files/default/etc/init.d/network
index c7720369fd..45d5311a7a 100755
--- a/package/base-files/default/etc/init.d/S40network
+++ b/package/base-files/default/etc/init.d/network
@@ -1,6 +1,7 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2006 OpenWrt.org
 
+START=40
 start() {
 	setup_switch() { return 0; }
 
diff --git a/package/base-files/default/etc/init.d/rcS b/package/base-files/default/etc/init.d/rcS
index 7fae7f5b05..27bc34b220 100755
--- a/package/base-files/default/etc/init.d/rcS
+++ b/package/base-files/default/etc/init.d/rcS
@@ -11,8 +11,8 @@ syslogd -C 16 #${log_ipaddr:+-L -R $log_ipaddr}
 klogd
 
 (
-	for i in /etc/init.d/S*; do
-		$i start 2>&1
+	for i in /etc/rc.d/S*; do
+		$i boot 2>&1
 	done 
 	
 	sysctl -p >&-
diff --git a/package/base-files/default/etc/init.d/S50telnet b/package/base-files/default/etc/init.d/telnet
similarity index 100%
rename from package/base-files/default/etc/init.d/S50telnet
rename to package/base-files/default/etc/init.d/telnet
diff --git a/package/base-files/default/etc/rc.common b/package/base-files/default/etc/rc.common
index 20d1efa402..a08f5e523c 100755
--- a/package/base-files/default/etc/rc.common
+++ b/package/base-files/default/etc/rc.common
@@ -1,5 +1,9 @@
 #!/bin/sh
-. /etc/functions.sh
+# Copyright (C) 2006 OpenWrt.org
+
+. $IPKG_INSTROOT/etc/functions.sh
+
+START=50
 
 start() {
 	return 0
@@ -27,12 +31,14 @@ shutdown() {
 }
 
 disable() {
-	rm -f /etc/rc.d/${initscript##*/}
+	name="$(basename "${initscript}")"
+	rm -f "$IPKG_INSTROOT/etc/rc.d/S??$name"
 }
 
 enable() {
+	name="$(basename "${initscript}")"
 	disable
-	ln -s /etc/init.d/${initscript##*/} /etc/rc.d/${initscript##*/}
+	ln -s "/etc/init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
 }
 
 depends() {
@@ -48,6 +54,8 @@ Available commands:
 	stop	Stop the service
 	restart	Restart the service
 	reload	Reload configuration files (or restart if that fails)
+	enable	Enable service autostart
+	disable	Disable service autostart
 $EXTRA_HELP
 EOF
 }
@@ -68,6 +76,8 @@ eval "case \"\$action\" in
 	restart) restart;;
 	boot) boot;;
 	shutdown) shutdown;;
+	enable) enable;;
+	disable) disable;;
 	$cmds
 	*) help;;
 esac"
diff --git a/package/dnsmasq/Makefile b/package/dnsmasq/Makefile
index 6283fdc898..d7e5f18b61 100644
--- a/package/dnsmasq/Makefile
+++ b/package/dnsmasq/Makefile
@@ -49,7 +49,7 @@ define Package/dnsmasq/install
 	install -m0755 -d $(1)/etc
 	install -m0644 ./files/dnsmasq.conf $(1)/etc/dnsmasq.conf
 	install -m0755 -d $(1)/etc/init.d
-	install -m0755 ./files/dnsmasq.init $(1)/etc/init.d/S50dnsmasq
+	install -m0755 ./files/dnsmasq.init $(1)/etc/init.d/dnsmasq
 endef
 
 $(eval $(call BuildPackage,dnsmasq))
diff --git a/package/dropbear/Makefile b/package/dropbear/Makefile
index b28b44ef9e..18e4a45708 100644
--- a/package/dropbear/Makefile
+++ b/package/dropbear/Makefile
@@ -107,7 +107,7 @@ define Package/dropbear/install
 	ln -sf ../sbin/dropbear $(1)/usr/bin/dbclient
 	ln -sf ../sbin/dropbear $(1)/usr/bin/dropbearkey
 	install -d -m0755 $(1)/etc/init.d
-	install -m0755 ./files/dropbear.init $(1)/etc/init.d/S50dropbear
+	install -m0755 ./files/dropbear.init $(1)/etc/init.d/dropbear
 endef
 
 define Package/dropbearconvert/install
diff --git a/package/iptables/Makefile b/package/iptables/Makefile
index d198a9271a..ee0c0941b1 100644
--- a/package/iptables/Makefile
+++ b/package/iptables/Makefile
@@ -195,7 +195,7 @@ define Package/iptables/install
 	install -d -m0755 $(1)/etc/config
 	install -m0644 ./files/firewall.config $(1)/etc/config/firewall
 	install -d -m0755 $(1)/etc/init.d
-	install -m0755 ./files/firewall.init $(1)/etc/init.d/S45firewall
+	install -m0755 ./files/firewall.init $(1)/etc/init.d/firewall
 	install -m0755 ./files/firewall.user $(1)/etc/
 	install -d -m0755 $(1)/usr/lib
 	install -m0644 ./files/firewall.awk $(1)/usr/lib
diff --git a/package/iptables/files/firewall.init b/package/iptables/files/firewall.init
index 731485bcdd..1e39d05fd9 100755
--- a/package/iptables/files/firewall.init
+++ b/package/iptables/files/firewall.init
@@ -2,7 +2,7 @@
 # Copyright (C) 2006 OpenWrt.org
 
 ## Please make changes in /etc/firewall.user
-
+START=45
 start() {
 	include /lib/network
 	scan_interfaces
diff --git a/package/madwifi/Makefile b/package/madwifi/Makefile
index a4180063f4..7d06fe1897 100644
--- a/package/madwifi/Makefile
+++ b/package/madwifi/Makefile
@@ -120,7 +120,7 @@ endif
 ifeq ($(findstring PCI,$(BUS)),PCI)
 	echo ath_pci >> $(1)/etc/modules.d/20-madwifi
 endif
-	install -m0755 ./files/madwifi.init $(1)/etc/init.d/S20madwifi
+	install -m0755 ./files/madwifi.init $(1)/etc/init.d/madwifi
 	$(CP) \
 		$(PKG_BUILD_DIR)/ath/ath_hal.$(LINUX_KMOD_SUFFIX) \
 		$(BUS_MODULES) \
diff --git a/package/madwifi/files/madwifi.init b/package/madwifi/files/madwifi.init
index 73eb11017e..0c3ad3c3c1 100644
--- a/package/madwifi/files/madwifi.init
+++ b/package/madwifi/files/madwifi.init
@@ -1,4 +1,5 @@
 #!/bin/sh /etc/rc.common
-start() {
+START=20
+boot() {
 	iwpriv ath0 mode 3
 }
diff --git a/package/openswan/Makefile b/package/openswan/Makefile
index 54b565efdd..b5adc54257 100644
--- a/package/openswan/Makefile
+++ b/package/openswan/Makefile
@@ -21,8 +21,6 @@ PKG_CAT:=zcat
 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
 PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-install
 
-PKG_INIT_PRIO:=60
-
 include $(INCLUDE_DIR)/package.mk
 
 define Package/openswan/Default
@@ -76,7 +74,7 @@ endef
 define Package/openswan/install
 	$(CP) $(PKG_INSTALL_DIR)/* $(1)
 	install -d -m0755 $(1)/etc/init.d
-	$(CP) $(1)/etc/rc.d/init.d/ipsec $(1)/etc/init.d/S$(PKG_INIT_PRIO)ipsec
+	$(CP) ./files/ipsec.init $(1)/etc/init.d/ipsec
 	rm -rf $(1)/usr/share
 	rm -rf $(1)/usr/man
 	rm -rf $(1)/var
diff --git a/package/openswan/files/ipsec.init b/package/openswan/files/ipsec.init
new file mode 100755
index 0000000000..33c416351d
--- /dev/null
+++ b/package/openswan/files/ipsec.init
@@ -0,0 +1,158 @@
+#!/bin/sh /etc/rc.common
+# IPsec startup and shutdown script
+# Copyright (C) 1998, 1999, 2001  Henry Spencer.
+# Copyright (C) 2002              Michael Richardson <mcr@freeswan.org>
+# Copyright (C) 2006              OpenWrt.org
+# 
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# RCSID $Id: setup.in,v 1.122.6.1 2005/07/25 19:17:03 ken Exp $
+#
+# ipsec         init.d script for starting and stopping
+#               the IPsec security subsystem (KLIPS and Pluto).
+#
+# This script becomes /etc/rc.d/init.d/ipsec (or possibly /etc/init.d/ipsec)
+# and is also accessible as "ipsec setup" (the preferred route for human
+# invocation).
+#
+# The startup and shutdown times are a difficult compromise (in particular,
+# it is almost impossible to reconcile them with the insanely early/late
+# times of NFS filesystem startup/shutdown).  Startup is after startup of
+# syslog and pcmcia support; shutdown is just before shutdown of syslog.
+#
+# chkconfig: 2345 47 76
+# description: IPsec provides encrypted and authenticated communications; \
+# KLIPS is the kernel half of it, Pluto is the user-level management daemon.
+
+START=60
+script_init() {
+	me='ipsec setup'		# for messages
+
+	# where the private directory and the config files are
+	IPSEC_EXECDIR="${IPSEC_EXECDIR-/usr/libexec/ipsec}"
+	IPSEC_LIBDIR="${IPSEC_LIBDIR-/usr/lib/ipsec}"
+	IPSEC_SBINDIR="${IPSEC_SBINDIR-/usr/sbin}"
+	IPSEC_CONFS="${IPSEC_CONFS-/etc}"
+
+	if test " $IPSEC_DIR" = " "	# if we were not called by the ipsec command
+	then
+	    # we must establish a suitable PATH ourselves
+	    PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
+	    export PATH
+
+	    IPSEC_DIR="$IPSEC_LIBDIR"
+	    export IPSEC_DIR IPSEC_CONFS IPSEC_LIBDIR IPSEC_EXECDIR
+	fi
+
+	# Check that the ipsec command is available.
+	found=
+	for dir in `echo $PATH | tr ':' ' '`
+	do
+		if test -f $dir/ipsec -a -x $dir/ipsec
+		then
+			found=yes
+			break			# NOTE BREAK OUT
+		fi
+	done
+	if ! test "$found"
+	then
+		echo "cannot find ipsec command -- \`$1' aborted" |
+			logger -s -p daemon.error -t ipsec_setup
+		exit 1
+	fi
+
+	# Pick up IPsec configuration (until we have done this, successfully, we
+	# do not know where errors should go, hence the explicit "daemon.error"s.)
+	# Note the "--export", which exports the variables created.
+	eval `ipsec _confread $config --optional --varprefix IPSEC --export --type config setup`
+
+	if test " $IPSEC_confreadstatus" != " "
+	then
+	    case $1 in 
+	    stop|--stop|_autostop) 
+		echo "$IPSEC_confreadstatus -- \`$1' may not work" |
+			logger -s -p daemon.error -t ipsec_setup;;
+
+	    *) echo "$IPSEC_confreadstatus -- \`$1' aborted" |
+		    logger -s -p daemon.error -t ipsec_setup;
+		exit 1;;
+	    esac
+	fi
+
+	IPSEC_confreadsection=${IPSEC_confreadsection:-setup}
+	export IPSEC_confreadsection
+
+	IPSECsyslog=${IPSECsyslog-daemon.error}
+	export IPSECsyslog
+
+	# misc setup
+	umask 022
+
+	mkdir -p /var/run/pluto
+}
+
+script_command() {
+	if [ "${USER}" != "root" ]
+	then
+		echo "permission denied (must be superuser)" |
+			logger -s -p $IPSECsyslog -t ipsec_setup 2>&1
+		exit 1
+	fi
+	# make sure all required directories exist
+	if [ ! -d /var/run/pluto ]
+	then
+		mkdir -p /var/run/pluto
+	fi
+	if [ ! -d /var/lock/subsys ]
+	then
+		mkdir -p /var/lock/subsys
+	fi
+	tmp=/var/run/pluto/ipsec_setup.st
+	outtmp=/var/run/pluto/ipsec_setup.out
+	(
+		ipsec _realsetup $1
+		echo "$?" >$tmp
+	) > ${outtmp} 2>&1
+	st=$?
+	if test -f $tmp
+	then
+		st=`cat $tmp`
+		rm -f $tmp
+	fi
+	if [ -f ${outtmp} ]; then
+		cat ${outtmp} | logger -s -p $IPSECsyslog -t ipsec_setup 2>&1
+		rm -f ${outtmp}
+	fi
+}
+
+
+start() {
+	script_init start "$@"
+	script_command start "$@"
+}
+
+stop() {
+	script_init stop "$@"
+	script_command stop "$@"
+}
+
+restart() {
+	script_init stop "$@"
+	script_command stop "$@"
+	script_command start "$@"
+}
+
+status() {
+	script_init status "$@"
+	ipsec _realsetup status
+}
+EXTRA_COMMANDS=status
+EXTRA_HELP="	status	Show the status of the service"
diff --git a/package/qos-scripts/files/etc/init.d/S50qos b/package/qos-scripts/files/etc/init.d/qos
similarity index 70%
rename from package/qos-scripts/files/etc/init.d/S50qos
rename to package/qos-scripts/files/etc/init.d/qos
index 87e18fb28d..4fa42d4a24 100755
--- a/package/qos-scripts/files/etc/init.d/S50qos
+++ b/package/qos-scripts/files/etc/init.d/qos
@@ -1,16 +1,14 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2006 OpenWrt.org
 
+START=50
+
 boot() {
 	/usr/lib/qos.sh firewall | sh
 }
 
 start() {
-#	FIXME: after init script migration
-#	qos-start
-#
-
-	boot
+	qos-start
 }
 
 stop() {
-- 
2.30.2