restart-NetInfo   [plain text]


#!/bin/sh
#
# restart-NetInfo
#

PIDFILE="/var/run/nibindd.pid"

#
# Check for a PID file. There's something wrong if
# the file is missing.
#
if [ ! -f ${PIDFILE} ]; then
	#
	# "nibindd" PID file not found, exiting
	#
	logger -i -p daemon.notice -t restart-NetInfo "${PIDFILE} missing"
	exit 0
fi
oPID=`cat ${PIDFILE}`

#
# Send a SIGHUP to the "nibindd" process to restart
# the NetInfo subsystem.
#
kill -HUP ${oPID}
status=$?
if [ $status -ne 0 ]; then
	logger -i -p daemon.notice -t restart-NetInfo "Could not SIGHUP nibindd, pid=${oPID}, status=${status}"
	exit 0
fi

#
# Wait a (short time) for the tag==local NetInfo
# domain to become available.
#

CHECK=1
LIMIT=60

while [ 1 ]
do
	#
	# check status of "nibindd"
	#
	if [ -f ${PIDFILE} ]; then
		nPID=`cat ${PIDFILE}`
		if [ "X${oPID}" != "X${nPID}" ]; then
			#
			# new PID detected, go wait for local domain
			#
			break
		fi
	else
		#
		# "nibindd" PID file not found (yet), delay & retry
	fi

	CHECK=`expr ${CHECK} + 1`
	if [ ${CHECK} -le ${LIMIT} ]; then
		sleep 1
	else
		#
		# We've waiting long enough, something's not right
		#
		logger -i -p daemon.notice -t restart-NetInfo "nibindd restart failed"
		exit 0
	fi
done

while [ 1 ]
do
	#
	# wait for the "netinfod local" domain to become available
	#
	niutil -read -T 1 -t 127.0.0.1/local /      > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		#
		# "local" domain has been registered
		#
		break
	fi

	CHECK=`expr ${CHECK} + 1`
	if [ ${CHECK} -le ${LIMIT} ]; then
		sleep 1
	else
		#
		# We've waiting long enough, something's not right
		#
		logger -i -p daemon.notice -t restart-NetInfo "\"netinfod local\" not starting (fast enough)"
		exit 0
	fi
done

#
# Touch the cache key to cause a notification
#
scutil << __END_OF_SCUTIL_COMMAND > /dev/null 2>&1
open
touch File:/var/run/nibindd.pid
close
quit
__END_OF_SCUTIL_COMMAND

exit 0