xref: /freebsd/libexec/rc/rc.d/hostname (revision 325ebf37d8efc6488754051fcc2b1aaa40cefd8b)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
40696600cSBjoern A. Zeeb#
50696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
60696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
70696600cSBjoern A. Zeeb# are met:
80696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
90696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
100696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
110696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
120696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
130696600cSBjoern A. Zeeb#
140696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
150696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
180696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240696600cSBjoern A. Zeeb# SUCH DAMAGE.
250696600cSBjoern A. Zeeb#
260696600cSBjoern A. Zeeb# $FreeBSD$
270696600cSBjoern A. Zeeb#
280696600cSBjoern A. Zeeb
290696600cSBjoern A. Zeeb# PROVIDE: hostname
300696600cSBjoern A. Zeeb# REQUIRE: FILESYSTEMS
310696600cSBjoern A. Zeeb# BEFORE:  netif
320696600cSBjoern A. Zeeb
330696600cSBjoern A. Zeeb. /etc/rc.subr
340696600cSBjoern A. Zeeb. /etc/network.subr
350696600cSBjoern A. Zeeb
360696600cSBjoern A. Zeebname="hostname"
370696600cSBjoern A. Zeebdesc="Set the system\'s hostname"
380696600cSBjoern A. Zeebstart_cmd="hostname_start"
390696600cSBjoern A. Zeebstop_cmd=":"
400696600cSBjoern A. Zeeb
410696600cSBjoern A. Zeebhostname_start()
420696600cSBjoern A. Zeeb{
430696600cSBjoern A. Zeeb	# If we are not inside a jail, set the host name.
440696600cSBjoern A. Zeeb	# If we are inside a jail, set the host name if it is permitted.
450696600cSBjoern A. Zeeb	#
460696600cSBjoern A. Zeeb	if [ `$SYSCTL_N security.jail.jailed` -eq 1 ]; then
470696600cSBjoern A. Zeeb		if [ `$SYSCTL_N security.jail.set_hostname_allowed` -eq 0 ]; then
480696600cSBjoern A. Zeeb			return
490696600cSBjoern A. Zeeb		fi
500696600cSBjoern A. Zeeb	else
510696600cSBjoern A. Zeeb		# If we're not in a jail and rc.conf doesn't specify a
520696600cSBjoern A. Zeeb		# hostname, see if we can get one from kenv.
530696600cSBjoern A. Zeeb		#
540696600cSBjoern A. Zeeb		if [ -z "${hostname}" -a \
550696600cSBjoern A. Zeeb		    -n "`/bin/kenv dhcp.host-name 2> /dev/null`" ]; then
560696600cSBjoern A. Zeeb			hostname=`/bin/kenv dhcp.host-name`
570696600cSBjoern A. Zeeb		fi
580696600cSBjoern A. Zeeb	fi
590696600cSBjoern A. Zeeb
600696600cSBjoern A. Zeeb	# Have we got a hostname yet?
610696600cSBjoern A. Zeeb	#
620696600cSBjoern A. Zeeb	if [ -z "${hostname}" ]; then
630696600cSBjoern A. Zeeb		# Null hostname is probably OK if DHCP is in use,
640696600cSBjoern A. Zeeb		# or when hostname is already set (common for jails).
650696600cSBjoern A. Zeeb		#
660696600cSBjoern A. Zeeb		if [ -z "`list_net_interfaces dhcp`" -a \
670696600cSBjoern A. Zeeb		     -z "`/bin/hostname`" ]; then
680696600cSBjoern A. Zeeb			warn "\$hostname is not set -- see rc.conf(5)."
690696600cSBjoern A. Zeeb		fi
700696600cSBjoern A. Zeeb		return
710696600cSBjoern A. Zeeb	fi
720696600cSBjoern A. Zeeb
730696600cSBjoern A. Zeeb	# All right, it is safe to invoke hostname(1) now.
740696600cSBjoern A. Zeeb	#
75*325ebf37SJose Luis Duran	startmsg -n "Setting hostname: ${hostname}"
760696600cSBjoern A. Zeeb	/bin/hostname "${hostname}"
77*325ebf37SJose Luis Duran	startmsg '.'
780696600cSBjoern A. Zeeb}
790696600cSBjoern A. Zeeb
800696600cSBjoern A. Zeebload_rc_config $name
810696600cSBjoern A. Zeebrun_rc_command "$1"
82