Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.7 KiB

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