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