Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Sys::Hostname; |
2 | ||
3 | use strict; | |
4 | ||
5 | use Carp; | |
6 | ||
7 | require Exporter; | |
8 | use XSLoader (); | |
9 | require AutoLoader; | |
10 | ||
11 | our @ISA = qw/ Exporter AutoLoader /; | |
12 | our @EXPORT = qw/ hostname /; | |
13 | ||
14 | our $VERSION = '1.1'; | |
15 | ||
16 | our $host; | |
17 | ||
18 | XSLoader::load 'Sys::Hostname', $VERSION; | |
19 | ||
20 | sub hostname { | |
21 | ||
22 | # method 1 - we already know it | |
23 | return $host if defined $host; | |
24 | ||
25 | # method 1' - try to ask the system | |
26 | $host = ghname(); | |
27 | return $host if defined $host; | |
28 | ||
29 | if ($^O eq 'VMS') { | |
30 | ||
31 | # method 2 - no sockets ==> return DECnet node name | |
32 | eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] }; | |
33 | if ($@) { return $host = $ENV{'SYS$NODE'}; } | |
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! | |
49 | $host = ''; | |
50 | Carp::croak "Cannot get host name of local machine"; | |
51 | ||
52 | } | |
53 | elsif ($^O eq 'MSWin32') { | |
54 | ($host) = gethostbyname('localhost'); | |
55 | chomp($host = `hostname 2> NUL`) unless defined $host; | |
56 | return $host; | |
57 | } | |
58 | elsif ($^O eq 'epoc') { | |
59 | $host = 'localhost'; | |
60 | return $host; | |
61 | } | |
62 | else { # Unix | |
63 | # is anyone going to make it here? | |
64 | ||
65 | # method 2 - syscall is preferred since it avoids tainting problems | |
66 | # XXX: is it such a good idea to return hostname untainted? | |
67 | eval { | |
68 | local $SIG{__DIE__}; | |
69 | require "syscall.ph"; | |
70 | $host = "\0" x 65; ## preload scalar | |
71 | syscall(&SYS_gethostname, $host, 65) == 0; | |
72 | } | |
73 | ||
74 | # method 2a - syscall using systeminfo instead of gethostname | |
75 | # -- needed on systems like Solaris | |
76 | || eval { | |
77 | local $SIG{__DIE__}; | |
78 | require "sys/syscall.ph"; | |
79 | require "sys/systeminfo.ph"; | |
80 | $host = "\0" x 65; ## preload scalar | |
81 | syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1; | |
82 | } | |
83 | ||
84 | # method 3 - trusty old hostname command | |
85 | || eval { | |
86 | local $SIG{__DIE__}; | |
87 | local $SIG{CHLD}; | |
88 | $host = `(hostname) 2>/dev/null`; # bsdish | |
89 | } | |
90 | ||
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 | ||
99 | # method 5 - sysV uname command (may truncate) | |
100 | || eval { | |
101 | local $SIG{__DIE__}; | |
102 | $host = `uname -n 2>/dev/null`; ## sysVish | |
103 | } | |
104 | ||
105 | # method 6 - Apollo pre-SR10 | |
106 | || eval { | |
107 | local $SIG{__DIE__}; | |
108 | my($a,$b,$c,$d); | |
109 | ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6); | |
110 | } | |
111 | ||
112 | # bummer | |
113 | || Carp::croak "Cannot get host name of local machine"; | |
114 | ||
115 | # remove garbage | |
116 | $host =~ tr/\0\r\n//d; | |
117 | $host; | |
118 | } | |
119 | } | |
120 | ||
121 | 1; | |
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 |