Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | package Sys::Hostname; |
8990e307 | 2 | |
f91101c9 | 3 | use strict; |
cb1a09d0 | 4 | |
f91101c9 | 5 | use Carp; |
cb1a09d0 | 6 | |
f91101c9 | 7 | require Exporter; |
f91101c9 | 8 | require AutoLoader; |
cb1a09d0 | 9 | |
f91101c9 GS |
10 | our @ISA = qw/ Exporter AutoLoader /; |
11 | our @EXPORT = qw/ hostname /; | |
cb1a09d0 | 12 | |
58ca468a | 13 | our $VERSION; |
cb1a09d0 | 14 | |
f91101c9 | 15 | our $host; |
cb1a09d0 | 16 | |
58ca468a | 17 | BEGIN { |
4522225b | 18 | $VERSION = '1.11'; |
58ca468a SR |
19 | { |
20 | local $SIG{__DIE__}; | |
21 | eval { | |
22 | require XSLoader; | |
23 | XSLoader::load('Sys::Hostname', $VERSION); | |
24 | }; | |
25 | warn $@ if $@; | |
26 | } | |
27 | } | |
28 | ||
8990e307 LW |
29 | |
30 | sub hostname { | |
a0d0e21e | 31 | |
567d72c2 | 32 | # method 1 - we already know it |
33 | return $host if defined $host; | |
34 | ||
f91101c9 | 35 | # method 1' - try to ask the system |
58ca468a | 36 | $host = ghname() if defined &ghname; |
f91101c9 GS |
37 | return $host if defined $host; |
38 | ||
c5f45532 | 39 | if ($^O eq 'VMS') { |
567d72c2 | 40 | |
41 | # method 2 - no sockets ==> return DECnet node name | |
84902520 | 42 | eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] }; |
c5f45532 | 43 | if ($@) { return $host = $ENV{'SYS$NODE'}; } |
567d72c2 | 44 | |
45 | # method 3 - has someone else done the job already? It's common for the | |
46 | # TCP/IP stack to advertise the hostname via a logical name. (Are | |
47 | # there any other logicals which TCP/IP stacks use for the host name?) | |
48 | $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} || | |
49 | $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} || | |
50 | $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'}; | |
51 | return $host if $host; | |
52 | ||
53 | # method 4 - does hostname happen to work? | |
54 | my($rslt) = `hostname`; | |
55 | if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; } | |
56 | return $host if $host; | |
57 | ||
58 | # rats! | |
c5f45532 | 59 | $host = ''; |
6286f723 | 60 | croak "Cannot get host name of local machine"; |
567d72c2 | 61 | |
62 | } | |
7bac28a0 | 63 | elsif ($^O eq 'MSWin32') { |
64 | ($host) = gethostbyname('localhost'); | |
65 | chomp($host = `hostname 2> NUL`) unless defined $host; | |
66 | return $host; | |
67 | } | |
3a2f06e9 GS |
68 | elsif ($^O eq 'epoc') { |
69 | $host = 'localhost'; | |
70 | return $host; | |
71 | } | |
567d72c2 | 72 | else { # Unix |
f91101c9 | 73 | # is anyone going to make it here? |
8990e307 | 74 | |
18c8aed3 JH |
75 | local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin'; # Paranoia. |
76 | ||
154a3d54 | 77 | # method 2 - syscall is preferred since it avoids tainting problems |
f91101c9 | 78 | # XXX: is it such a good idea to return hostname untainted? |
8990e307 | 79 | eval { |
84902520 | 80 | local $SIG{__DIE__}; |
6bb694c1 | 81 | require "syscall.ph"; |
8990e307 | 82 | $host = "\0" x 65; ## preload scalar |
6bb694c1 | 83 | syscall(&SYS_gethostname, $host, 65) == 0; |
8990e307 LW |
84 | } |
85 | ||
154a3d54 | 86 | # method 2a - syscall using systeminfo instead of gethostname |
67693aa5 TB |
87 | # -- needed on systems like Solaris |
88 | || eval { | |
89 | local $SIG{__DIE__}; | |
6bb694c1 GS |
90 | require "sys/syscall.ph"; |
91 | require "sys/systeminfo.ph"; | |
67693aa5 | 92 | $host = "\0" x 65; ## preload scalar |
6bb694c1 | 93 | syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1; |
67693aa5 TB |
94 | } |
95 | ||
154a3d54 | 96 | # method 3 - trusty old hostname command |
8990e307 | 97 | || eval { |
84902520 | 98 | local $SIG{__DIE__}; |
b522bf06 | 99 | local $SIG{CHLD}; |
a0d0e21e | 100 | $host = `(hostname) 2>/dev/null`; # bsdish |
8990e307 LW |
101 | } |
102 | ||
154a3d54 GS |
103 | # method 4 - use POSIX::uname(), which strictly can't be expected to be |
104 | # correct | |
105 | || eval { | |
106 | local $SIG{__DIE__}; | |
107 | require POSIX; | |
108 | $host = (POSIX::uname())[1]; | |
109 | } | |
110 | ||
6bb694c1 | 111 | # method 5 - sysV uname command (may truncate) |
8990e307 | 112 | || eval { |
84902520 | 113 | local $SIG{__DIE__}; |
85e6fe83 | 114 | $host = `uname -n 2>/dev/null`; ## sysVish |
8990e307 LW |
115 | } |
116 | ||
6bb694c1 | 117 | # method 6 - Apollo pre-SR10 |
8990e307 | 118 | || eval { |
84902520 | 119 | local $SIG{__DIE__}; |
f91101c9 | 120 | my($a,$b,$c,$d); |
8990e307 LW |
121 | ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6); |
122 | } | |
123 | ||
124 | # bummer | |
6286f723 | 125 | || croak "Cannot get host name of local machine"; |
8990e307 LW |
126 | |
127 | # remove garbage | |
128 | $host =~ tr/\0\r\n//d; | |
129 | $host; | |
567d72c2 | 130 | } |
8990e307 LW |
131 | } |
132 | ||
133 | 1; | |
f91101c9 GS |
134 | |
135 | __END__ | |
136 | ||
137 | =head1 NAME | |
138 | ||
139 | Sys::Hostname - Try every conceivable way to get hostname | |
140 | ||
141 | =head1 SYNOPSIS | |
142 | ||
143 | use Sys::Hostname; | |
144 | $host = hostname; | |
145 | ||
146 | =head1 DESCRIPTION | |
147 | ||
148 | Attempts several methods of getting the system hostname and | |
149 | then caches the result. It tries the first available of the C | |
150 | library's gethostname(), C<`$Config{aphostname}`>, uname(2), | |
151 | C<syscall(SYS_gethostname)>, C<`hostname`>, C<`uname -n`>, | |
152 | and the file F</com/host>. If all that fails it C<croak>s. | |
153 | ||
154 | All NULs, returns, and newlines are removed from the result. | |
155 | ||
156 | =head1 AUTHOR | |
157 | ||
158 | David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt> | |
159 | ||
160 | Texas Instruments | |
161 | ||
162 | XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt> | |
163 | ||
164 | =cut | |
165 |