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