external/bsd/dhcpcd/dist/dhcpcd-hooks/30-hostname
author yamt <yamt@NetBSD.org>
Tue, 17 Apr 2012 00:01:34 +0000
branchyamt-pagecache
changeset 280360 e98874280705
parent 199649 7abaf08f3305
child 219290 4061a119e536
child 277758 4e44be457bc3
child 280393 591e5f54cb27
permissions -rw-r--r--
sync with head

# Set the hostname from DHCP data if required

# Some systems don't have hostname(1)
_hostname()
{
	local name=

	if [ -z "$1" ]; then
		if [ -r /proc/sys/kernel/hostname ]; then
			read name </proc/sys/kernel/hostname && echo "$name"
		else
			hostname
		fi
		return $?
	fi

	if [ -w /proc/sys/kernel/hostname ]; then
		echo "$1" >/proc/sys/kernel/hostname
	else
		hostname "$1"
	fi
}

need_hostname()
{
	local hostname=""

	case "$force_hostname" in
	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) ;;
	*) hostname="$(_hostname)";;
	esac
	case "$hostname" in
	""|"(none)"|localhost|localhost.localdomain)
		[ -n "$new_host_name" -o -n "$new_fqdn_name" ];;
	"$old_host_name"|"$old_fqdn_name")
		true;;
	*)
		false;;
	esac
}

try_hostname()
{
	if valid_domainname "$1"; then
		_hostname "$1"
	else
		syslog err "Invalid hostname: $1"
	fi
}

set_hostname()
{
	if need_hostname; then
		if [ -n "$new_host_name" ]; then
			try_hostname "$new_host_name"
		elif [ -n "$new_fqdn_name" ]; then
			try_hostname "$new_fqdn_name"
		fi
	fi
}

if $if_up; then
	set_hostname
fi