| Filename | /home/ss5/perl5/perlbrew/perls/tapper-perl/lib/5.16.3/x86_64-linux/Sys/Hostname.pm |
| Statements | Executed 17 statements in 536µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 15µs | 26µs | Sys::Hostname::BEGIN@3 |
| 1 | 1 | 1 | 14µs | 133µs | Sys::Hostname::BEGIN@16 |
| 1 | 1 | 1 | 8µs | 40µs | Sys::Hostname::BEGIN@5 |
| 0 | 0 | 0 | 0s | 0s | Sys::Hostname::hostname |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Sys::Hostname; | ||||
| 2 | |||||
| 3 | 2 | 22µs | 2 | 38µs | # spent 26µs (15+11) within Sys::Hostname::BEGIN@3 which was called:
# once (15µs+11µs) by Benchmark::Perl::Formance::BEGIN@20 at line 3 # spent 26µs making 1 call to Sys::Hostname::BEGIN@3
# spent 11µs making 1 call to strict::import |
| 4 | |||||
| 5 | 2 | 70µs | 2 | 72µs | # spent 40µs (8+32) within Sys::Hostname::BEGIN@5 which was called:
# once (8µs+32µs) by Benchmark::Perl::Formance::BEGIN@20 at line 5 # spent 40µs making 1 call to Sys::Hostname::BEGIN@5
# spent 32µs making 1 call to Exporter::import |
| 6 | |||||
| 7 | 1 | 500ns | require Exporter; | ||
| 8 | |||||
| 9 | 1 | 5µs | our @ISA = qw/ Exporter /; | ||
| 10 | 1 | 400ns | our @EXPORT = qw/ hostname /; | ||
| 11 | |||||
| 12 | 1 | 100ns | our $VERSION; | ||
| 13 | |||||
| 14 | 1 | 100ns | our $host; | ||
| 15 | |||||
| 16 | # spent 133µs (14+118) within Sys::Hostname::BEGIN@16 which was called:
# once (14µs+118µs) by Benchmark::Perl::Formance::BEGIN@20 at line 26 | ||||
| 17 | 1 | 400ns | $VERSION = '1.16'; | ||
| 18 | { | ||||
| 19 | 2 | 5µs | local $SIG{__DIE__}; | ||
| 20 | 1 | 1µs | eval { | ||
| 21 | 1 | 400ns | require XSLoader; | ||
| 22 | 1 | 124µs | 1 | 118µs | XSLoader::load(); # spent 118µs making 1 call to XSLoader::load |
| 23 | }; | ||||
| 24 | 1 | 2µs | warn $@ if $@; | ||
| 25 | } | ||||
| 26 | 1 | 302µs | 1 | 133µs | } # spent 133µs making 1 call to Sys::Hostname::BEGIN@16 |
| 27 | |||||
| 28 | |||||
| 29 | sub hostname { | ||||
| 30 | |||||
| 31 | # method 1 - we already know it | ||||
| 32 | return $host if defined $host; | ||||
| 33 | |||||
| 34 | # method 1' - try to ask the system | ||||
| 35 | $host = ghname() if defined &ghname; | ||||
| 36 | return $host if defined $host; | ||||
| 37 | |||||
| 38 | if ($^O eq 'VMS') { | ||||
| 39 | |||||
| 40 | # method 2 - no sockets ==> return DECnet node name | ||||
| 41 | eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] }; | ||||
| 42 | if ($@) { return $host = $ENV{'SYS$NODE'}; } | ||||
| 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! | ||||
| 58 | $host = ''; | ||||
| 59 | croak "Cannot get host name of local machine"; | ||||
| 60 | |||||
| 61 | } | ||||
| 62 | elsif ($^O eq 'MSWin32') { | ||||
| 63 | ($host) = gethostbyname('localhost'); | ||||
| 64 | chomp($host = `hostname 2> NUL`) unless defined $host; | ||||
| 65 | return $host; | ||||
| 66 | } | ||||
| 67 | elsif ($^O eq 'epoc') { | ||||
| 68 | $host = 'localhost'; | ||||
| 69 | return $host; | ||||
| 70 | } | ||||
| 71 | else { # Unix | ||||
| 72 | # is anyone going to make it here? | ||||
| 73 | |||||
| 74 | local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin'; # Paranoia. | ||||
| 75 | |||||
| 76 | # method 2 - syscall is preferred since it avoids tainting problems | ||||
| 77 | # XXX: is it such a good idea to return hostname untainted? | ||||
| 78 | eval { | ||||
| 79 | local $SIG{__DIE__}; | ||||
| 80 | require "syscall.ph"; | ||||
| 81 | $host = "\0" x 65; ## preload scalar | ||||
| 82 | syscall(&SYS_gethostname, $host, 65) == 0; | ||||
| 83 | } | ||||
| 84 | |||||
| 85 | # method 2a - syscall using systeminfo instead of gethostname | ||||
| 86 | # -- needed on systems like Solaris | ||||
| 87 | || eval { | ||||
| 88 | local $SIG{__DIE__}; | ||||
| 89 | require "sys/syscall.ph"; | ||||
| 90 | require "sys/systeminfo.ph"; | ||||
| 91 | $host = "\0" x 65; ## preload scalar | ||||
| 92 | syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1; | ||||
| 93 | } | ||||
| 94 | |||||
| 95 | # method 3 - trusty old hostname command | ||||
| 96 | || eval { | ||||
| 97 | local $SIG{__DIE__}; | ||||
| 98 | local $SIG{CHLD}; | ||||
| 99 | $host = `(hostname) 2>/dev/null`; # bsdish | ||||
| 100 | } | ||||
| 101 | |||||
| 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 | |||||
| 110 | # method 5 - sysV uname command (may truncate) | ||||
| 111 | || eval { | ||||
| 112 | local $SIG{__DIE__}; | ||||
| 113 | $host = `uname -n 2>/dev/null`; ## sysVish | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | # bummer | ||||
| 117 | || croak "Cannot get host name of local machine"; | ||||
| 118 | |||||
| 119 | # remove garbage | ||||
| 120 | $host =~ tr/\0\r\n//d; | ||||
| 121 | $host; | ||||
| 122 | } | ||||
| 123 | } | ||||
| 124 | |||||
| 125 | 1 | 3µs | 1; | ||
| 126 | |||||
| 127 | __END__ |