1113674Smtm#!/bin/sh
2113674Smtm#
3113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4113674Smtm#
5113674Smtm# Redistribution and use in source and binary forms, with or without
6113674Smtm# modification, are permitted provided that the following conditions
7113674Smtm# are met:
8113674Smtm# 1. Redistributions of source code must retain the above copyright
9113674Smtm#    notice, this list of conditions and the following disclaimer.
10113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
11113674Smtm#    notice, this list of conditions and the following disclaimer in the
12113674Smtm#    documentation and/or other materials provided with the distribution.
13113674Smtm#
14113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15113674Smtm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16113674Smtm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17113674Smtm# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18113674Smtm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19113674Smtm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20113674Smtm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21113674Smtm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22113674Smtm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23113674Smtm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24113674Smtm# SUCH DAMAGE.
25113674Smtm#
26113674Smtm# $FreeBSD$
27113674Smtm#
28113674Smtm
29113674Smtm# PROVIDE: hostname
30168283Sdes# REQUIRE: FILESYSTEMS
31113674Smtm# BEFORE:  netif
32113674Smtm
33113674Smtm. /etc/rc.subr
34166738Syar. /etc/network.subr
35113674Smtm
36113674Smtmname="hostname"
37113674Smtmstart_cmd="hostname_start"
38113674Smtmstop_cmd=":"
39113674Smtm
40113674Smtmhostname_start()
41113674Smtm{
42126647Spjd	# If we are not inside a jail, set the host name if it is not already set.
43126647Spjd	# If we are inside a jail, set the host name even if it is already set,
44126647Spjd	# but first check if it is permitted.
45113674Smtm	#
46126647Spjd	if [ `$SYSCTL_N security.jail.jailed` -eq 1 ]; then
47126647Spjd		if [ `$SYSCTL_N security.jail.set_hostname_allowed` -eq 0 ]; then
48126647Spjd			return
49126647Spjd		fi
50127744Skrion	elif [ -n "`/bin/hostname -s`" ]; then
51126648Spjd		return
52127345Sbrooks	else
53127345Sbrooks		# If we're not in a jail and rc.conf doesn't specify a
54127345Sbrooks		# hostname, see if we can get one from kenv.
55127345Sbrooks		#
56127345Sbrooks		if [ -z "${hostname}" -a \
57127345Sbrooks		    -n "`/bin/kenv dhcp.host-name 2> /dev/null`" ]; then
58127345Sbrooks			hostname=`/bin/kenv dhcp.host-name`
59127345Sbrooks		fi
60113674Smtm	fi
61126647Spjd
62166620Syar	# Have we got a hostname yet?
63166620Syar	#
64166620Syar	if [ -z "${hostname}" ]; then
65166738Syar		# Null hostname is probably OK if DHCP is in use.
66166738Syar		#
67166738Syar		if [ -z "`list_net_interfaces dhcp`" ]; then
68229822Sdougb			warn "\$hostname is not set -- see rc.conf(5)."
69166738Syar		fi
70166620Syar		return
71166620Syar	fi
72166620Syar
73166620Syar	# All right, it is safe to invoke hostname(1) now.
74166620Syar	#
75197947Sdougb	check_startmsgs && echo -n "Setting hostname: ${hostname}"
76166620Syar	/bin/hostname "${hostname}"
77197947Sdougb	check_startmsgs && echo '.'
78113674Smtm}
79113674Smtm
80113674Smtmload_rc_config $name
81113674Smtmrun_rc_command "$1"
82