Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Add a 'status' switch to startup items' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Add a 'status' switch to startup items
Authored by: bluehz on Mar 18, '04 12:31:15PM
Great tip thx - I used your hint to rewrite my custom firewall startup. Was a bit tricky since ipfw does not actually exhibit a process id that I can see. So I ended up managing a fake pid file. Its not the best script in the world but it works for me and I learned something in the process. I know the parsing of the command line arguments is ugly, but I spent an hour this morning reading up on the subject and never could get anything else to work. If soene guru out can enlighten me on a better way - I sure would like that.

I usually symlink this into /usr/local/bin/ipfwctl (learned that from my Slackware stuff) so its easy to startup.


#!/bin/sh
# add -xv for debugging

# ipfw init file.
#
#
#
# add this line in /etc/hostconfig
#	CUST_FIREWALL=-YES-

. /etc/rc.common

# variables
IPFW=/sbin/ipfw
verbose="false"
function usage () {
   cat <<EOF
Usage: `basename $0` [start|stop|restart|status] [-v]
   start   starts the firewall
   stop    stops the firewall
   restart restarts the firewall
   status  reports status of firewall with optional
           verbose [-v] listing of firewall rules
   -v      verbose (only in status)
EOF
exit 1
}

# parse command line arguments
if [ $# -gt 1 ]; then
	if [ $2 = "-v" ]; then
		verbose="true"
	else
		usage
	fi
fi

StartService ()
{
	if [ "${CUST_FIREWALL:=-YES-}" = "-YES-" ]; then
		ConsoleMessage "Configuring IPFW"
		/usr/sbin/sysctl -w net.inet.ip.fw.verbose=1
		CheckForNetwork

		# check for network
		if [ "${NETWORKUP}" = "-NO-" ]; then 
			echo "Network not available"; exit; 
		fi

		# remove stale fake pid
		if [ -f /var/run/ipfw.pid ]; then
			rm /var/run/ipfw.pid
		fi

		# clear all rules
		${IPFW} -f flush

		# start firewall
		ConsoleMessage "Starting Custom Firewall..."
		${IPFW} -q /etc/ipfw.conf

		# check status and write fake pid		
		if [ $? -eq 0 ]; then
			ConsoleMessage "Custom Firewall successfully started..."
			touch /var/run/ipfw.pid
		else
			ConsoleMessage "Could not start Custom Firewall..."
			exit 1
		fi
	fi
}

StopService ()
{
	# stop service and remove fake pid
	if [ -f /var/run/ipfw.pid ]; then
	       	ConsoleMessage "Stopping Custom Firewall..."
       		${IPFW} -f flush
		rm /var/run/ipfw.pid
	else
		ConsoleMessage "Custom Firewall is not running!"
	fi
}


RestartService () 
{ 
	StopService; 
	StartService; 
}

StatusService ()
{
	# check for fake pid
	if [ -f /var/run/ipfw.pid ]; then

	# check for verbose flag
	if [ $verbose = "true" ]; then
	echo "here"
    			ConsoleMessage "Custom Firewall is running!"
    			${IPFW} list
			else
				ConsoleMessage "Custom Firewall is running!"
 fi
	else
		ConsoleMessage "Custom Firewall is not running!"
    fi
}

RunService "$1"



[ Reply to This | # ]