package Cpanel::Hostname; # cpanel - Cpanel/Hostname.pm Copyright(c) 2013 cPanel, Inc. # All rights Reserved. # copyright@cpanel.net http://cpanel.net # This code is subject to the cPanel license. Unauthorized copying is prohibited use strict; ### DO NOT ADD DEPS AS THIS WILL BREAK UPCP ### use Cpanel::LoadFile (); ### DO NOT ADD DEPS AS THIS WILL BREAK UPCP ### our $VERSION = 1.6; our $hostname; sub gethostname { #do not load cpanel sys if its not already loaded if ( $INC{'Cpanel/Sys/Hostname.pm'} ) { goto &Cpanel::Sys::Hostname::gethostname; } if ($hostname) { return $hostname; } if ( -r '/proc/sys/kernel/hostname' ) { chomp( $hostname = Cpanel::LoadFile::loadfile( '/proc/sys/kernel/hostname', { 'skip_exists_check' => 1 } ) ); $hostname =~ s/[\r\n]//g; # chomp is not enough } if ( !$hostname ) { # This cannot rely on SafeRun because we cannot import here due to memory constraints #fork / exec is probably cheaper then the import from Cpanel::Sys chomp( $hostname = `/usr/local/cpanel/bin/hostname` ); $hostname =~ s/[\r\n]//g; # chomp is not enough } if ( !$hostname ) { eval 'require Cpanel::Sys::Hostname; local $SIG{__DIE__}; local $SIG{__WARN__}; $hostname = Cpanel::Sys::Hostname::gethostname();'; } $hostname =~ tr{A-Z}{a-z}; # hostnames must be lowercase (see Cpanel::Sys::Hostname::make_hostname_lowercase) return $hostname; } sub shorthostname { return ( split( /\./, ( gethostname() || '' ) ) )[0]; } 1;