File: hostname.sh

package info (click to toggle)
inetutils 2%3A2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,684 kB
  • sloc: ansic: 132,371; sh: 12,498; yacc: 1,651; makefile: 725; perl: 72
file content (89 lines) | stat: -rwxr-xr-x 2,466 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh

# Copyright (C) 2011-2025 Free Software Foundation, Inc.
#
# This file is part of GNU Inetutils.
#
# GNU Inetutils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# GNU Inetutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see `http://www.gnu.org/licenses/'.

# Prerequisites:
#
#  * Shell: SVR3 Bourne shell, or newer.
#
#  * id(1), mktemp(1), uname(1).

. ./tools.sh

hostname=${hostname:-../src/hostname$EXEEXT}

if [ $VERBOSE ]; then
    set -x
    $hostname --version
fi

errno=0

posttest () {
    test -n "$NAMEFILE" && test -r "$NAMEFILE" && rm "$NAMEFILE"
}

our_hostname=`$hostname` || errno=$?
test $errno -eq 0 || echo "Failed to get hostname." >&2
test $errno -eq 0 || exit $errno

sys_hostname=`hostname` || errno=$?
if test $errno -ne 0; then
    echo "System hostname failed (rc $errno out $sys_hostname)." >&2
    errno=0
    sys_hostname=`uname -n` || errno=$?
    test $errno -eq 0 || echo "Failed uname (rc $errno out $sys_hostname)." >&2
    test $errno -eq 0 || exit $errno
fi

if test "$our_hostname" != "$sys_hostname"; then
    echo "Hostname mismatch $our_hostname != $sys_hostname"
    exit 1
fi

if test `func_id_uid` != 0; then
    echo "hostname: skipping tests to set host name"
else
    $need_mktemp || exit_no_mktemp

    # Only run this if hostname succeeded...
    if test $errno -eq 0; then
	$hostname `$hostname` || errno=$?
	test $errno -eq 0 || echo "Failed to set hostname." >&2
	test $errno -eq 0 || exit $errno

	NAMEFILE=`$MKTEMP tmp.XXXXXXXXXX` || errno=$?
	test $errno -eq 0 || echo >&2 'Cannot create test file.'
	test $errno -eq 0 || exit $errno

	trap posttest 0 1 2 3 15

	SAVEDNAME=`$hostname` || errno=$?
	echo $SAVEDNAME > $NAMEFILE

	if test $errno -eq 0 && test -s $NAMEFILE; then
	    $hostname -F $NAMEFILE || errno=$?
	    test $errno -eq 0 || echo >&2 'Failed to set hostname.'

	    # Attempt to rescue name using the first method.
	    test $errno -eq 0 || $hostname "$SAVEDNAME" || errno=$?
	fi
    fi
fi

exit $errno