MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/mysqlup
#!/usr/bin/perl
# cpanel - mysqlup                                Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

use strict;
use IPC::Open3                   ();
use Cpanel::Hostname             ();
use Cpanel::FileUtils::Copy      ();
use Cpanel::Config::LoadCpConf   ();
use Cpanel::SafeDir::MK          ();
use Cpanel::SafetyBits           ();
use Cpanel::SafeRun::Errors      ();
use Cpanel::Linkage              ();
use Cpanel::DbUtils              ();
use Cpanel::FileUtils::TouchFile ();
use Cpanel::SysPkgs              ();
use Cpanel::HttpRequest          ();
use cPpkgversions;
use Cpanel::MysqlUtils ();
use Cpanel::RpmUtils;
use Cpanel::RpmStor;
use Cpanel::OSSys ();
use cPanelMYSQLRPM;
use Cpanel::cPPkgs;
use Cpanel::Selinux;
use Cpanel::Update ();
use Cpanel::Rlimit ();

Cpanel::Update::safe_update_environment('mysql');
Cpanel::Update::init_UP_update('mysql');

my $buildnum = 0;
my $pkg      = 'MySQL';
my $pkgver;
my $hostname = Cpanel::Hostname::gethostname();
my $httpClient = Cpanel::HttpRequest->new( 'hideOutput' => 0 );

my @PKGLIST;

my ( $system, $nodename, $release, $version, $machine ) = Cpanel::OSSys::uname();
my $arch = $machine;
if ( $machine =~ /i.86/ ) {
    $arch = 'i386';
}

if ( !-e '/var/cpanel/cpanel.config' ) {
    system 'install', '-o', 'root', '-m644', '/usr/local/cpanel/etc/cpanel.config', '/var/cpanel/cpanel.config';
}

my %CPCONF = Cpanel::Config::LoadCpConf::loadcpconf();

# Default to 4 if mysql-version is unset
if (   !exists $CPCONF{'mysql-version'}
    || !defined $CPCONF{'mysql-version'}
    || $CPCONF{'mysql-version'} eq ''
    || $CPCONF{'mysql-version'} eq '3' ) {
    $CPCONF{'mysql-version'} = '4';
}

print "MySQL Setup Script Version 7.0\n";
$ENV{'PATH'} = $ENV{'PATH'} . ':/usr/sbin';
$ENV{'HOME'} = ( getpwnam('root') )[7];

my $force   = 0;
my $latest  = 0;
my $src     = 0;
my $verbose = 0;
while ( $#ARGV != -1 ) {
    if ( $ARGV[0] =~ m/^\-\-/ ) {
        my $arg = shift @ARGV;
        $arg =~ s/^\-\-//g;
        $arg = lc $arg;
        if ( $arg eq 'verbose' ) { $verbose = 1; }
        if ( $arg eq 'source' )  { $src     = 1; }
        if ( $arg eq 'latest' )  { $latest  = 1; }
        if ( $arg eq 'force' )   { $force   = 1; }
    }
    else {
        last;
    }
}

if ( $#ARGV != -1 && $ARGV[0] =~ /source/ ) {
    $src = 1;
}

if ( -e '/etc/gentoo-release' ) {
    print "On Gentoo, MySQL is installed using portage.\n";
    exit;
}

my $updated = 0;

if ( $system =~ /freebsd/i ) {
    $updated = freebsdmysqlinstall();
}
else {
    if ( -e '/lib/libc.so.6' ) {
        my $libcv = `/lib/libc.so.6`;
        $libcv =~ /version\s+([\d\.]+)/;
        my $glibcversion = $1;
        if ( $glibcversion && $glibcversion < 2.1 ) {
            $src = 1;
            warn 'Old glibc library. Rpms may be built from source';
        }
    }

    my $baseurl    = 'http://httpupdate.cpanel.net/mysqlinstall';
    my $cPPkg      = Cpanel::cPPkgs->new( 'MySQL', $baseurl );
    my $usingcPPkg = 0;

    my $canusecPPkg = ( $cPPkg->haspkg( $CPCONF{'mysql-version'} ) && $CPCONF{'mysql-version'} >= 4 );

    if ( !$src && $canusecPPkg ) {
        $updated    = cPPkgs_linuxmysqlinstall($cPPkg);
        $usingcPPkg = 1;
    }

    if ( $src && !$updated && $canusecPPkg ) {
        my @FILES   = $cPPkg->getfilelist( $CPCONF{'mysql-version'} );
        my @FPATH   = split( /\//, $FILES[0] );
        my $srcfile = $FPATH[$#FPATH];
        my @FMUNGE  = split( /\./, $srcfile );
        while ( $FMUNGE[$#FMUNGE] !~ /^\d+/ && $#FMUNGE > -1 ) {
            pop @FMUNGE;
        }
        my $basepkga = join( '.', @FMUNGE );
        my @DMUNGE = split( /-/, $basepkga );
        my @SRCPKG;
        push( @SRCPKG, shift(@DMUNGE) );
        while ( $DMUNGE[0] !~ /^\d+/ ) {
            shift(@DMUNGE);
        }
        push( @SRCPKG, @DMUNGE );
        my $srcpkg = join( '-', @SRCPKG ) . '.src.rpm';
        print "Resolved Package to source rpm: $srcpkg\n";

        my $url     = $baseurl . '/' . $srcpkg;
        my $rpmfile = Cpanel::UrlTools::urltofile($url);

        $httpClient->download( $url, $rpmfile );

        my $goodstatus = Cpanel::RpmUtils::checkrpm($rpmfile);
        if ( !$goodstatus ) {
            print "Failed to download a working ${rpmfile}! Falling back to generic MySQL package.\n";
        }
        else {
            my ( $rFILES, $rBLOG ) = buildmysqlrpm( $verbose, $rpmfile );
            my @FILES;
            if ( ref $rFILES eq 'ARRAY' ) { @FILES = @{$rFILES}; }

            if ( $#FILES < 2 ) {
                print join( '', @{$rBLOG} );
                die "Rpm failed to build!  Try recalling mysqlup with --source --lastest --verbose";
            }

            $ENV{'RPMINSTALL'} = 1;
            foreach my $rpmfile (@FILES) {
                Cpanel::RpmUtils::rpmpreinstall($rpmfile);
            }
            system 'rpm', '-Uvh', '--nodeps', '--force', @FILES;
            unlink(@FILES);
            $ENV{'RPMINSTALL'} = 0;
            chdir '/';
        }
    }

    if ( $updated == -1 ) {
        $updated = linuxmysqlinstall();
    }
    if ( $updated > -1 && $CPCONF{'mysql-version'} >= 5 ) {
        check_for_obsolete_embedded();
    }
}

Cpanel::Linkage::checkphplinkage();

my $touchmysql = 0;
if ( $updated > 0 ) {
    checkselinux();
    system '/scripts/perlinstaller', '--force', 'DBD::mysql';
    system '/scripts/securemysql',   '-qFa',    'removelockntmp';
    if ( -x '/usr/local/cpanel/bin/updatephpmyadmin' ) {
        system '/usr/local/cpanel/bin/updatephpmyadmin';
    }

    $touchmysql = 1;
}

my $mysqld        = Cpanel::DbUtils::find_mysqld();
my $mysqld_mtime  = ( stat($mysqld) )[9] || 0;
my $mysqlup_mtime = ( stat('/scripts/mysqlup') )[9] || 0;

if ( $mysqld_mtime < $mysqlup_mtime ) {
    $touchmysql = 1;
}

if ($touchmysql) {
    Cpanel::FileUtils::TouchFile::touchfile($mysqld);
}

print "Install Complete\n";

sub resmysql {
    if ( -e '/var/cpanel/useapt' && -e '/usr/bin/apt-get' ) {
        system( '/usr/bin/apt-get', '-y', '--fix-broken', 'install' );
    }
    system '/scripts/restartsrv_mysql';
}

sub fetchmysqlrpm {
    my $mysqlfile = shift;

    #Sample is 'MySQL-4.0/MySQL-server-4.0.23-0.i386.rpm'
    my @FL = split( /\//, $mysqlfile );
    my $file = pop(@FL);

    if ( !-e '/var/spool/cpupdates' ) {
        mkdir( '/var/spool/cpupdates', 0700 );
    }

    my @GOODURLS = cPanelMYSQLRPM::getmirrorlist();
    my $norpm    = 1;
    my @badurls  = ();
    while ($norpm) {
        foreach my $goodurl (@GOODURLS) {
            system( 'wget', '-t', '2', '--timeout', '4000', '-O', '/var/spool/cpupdates/' . $file, "--referer=${goodurl}/Downloads/${mysqlfile}", "${goodurl}/Downloads/${mysqlfile}" );
            if (   ( stat( '/var/spool/cpupdates/' . $file ) )[7] > 0
                && ( $? >> 8 == 0 ) ) {
                print "Checking $file ....";
                my $goodstatus = Cpanel::RpmUtils::checkrpm( '/var/spool/cpupdates/' . $file );
                print "Done\n";
                system( 'rpm', '-qp', '/var/spool/cpupdates/' . $file );
                if ( !$goodstatus || $? >> 8 != 0 ) {
                    print "Check failed with status: ${goodstatus} and error code " . ( $? >> 8 ) . " !\n";
                    unlink( '/var/spool/cpupdates/' . $file );
                    push @badurls, $goodurl;
                    next;
                }
                print "Check Passed ;)\n";

                #rpm is ok
                return '/var/spool/cpupdates/' . $file;
            }
            else {
                push @badurls, $goodurl;
            }
        }
        if ( $#badurls > -1 ) {
            cPanelMYSQLRPM::removemirrors(@badurls);
            foreach my $badurl (@badurls) {
                @GOODURLS = grep( !/^${badurl}$/, @GOODURLS );
            }
        }
        if ($norpm) {
            @GOODURLS = cPanelMYSQLRPM::getmirrorlist();
            if ( $#GOODURLS == -1 ) {
                die "I ran out of mirrors for MySQL.";
            }
        }
    }
}

sub freebsdmysqlinstall {
    my $version = $CPCONF{'mysql-version'} =~ /^[45]$/ ? $CPCONF{'mysql-version'} . '.0' : $CPCONF{'mysql-version'} =~ /^[456]\.\d+$/ ? $CPCONF{'mysql-version'} : '5.0';
    $version =~ s/\.//;
    @PKGLIST = ( "mysql${version}-client", "mysql${version}-server" );

    system '/scripts/checkmakeconf';
    system '/scripts/portsup';

    print "This is the $pkg installer for OS FreeBSD\n";

    my $needres  = 0;
    my $complete = 0;

    if ( !-e '/var/db/mysql' ) {
        my ( $mysqluid, $mysqlgid ) = ( getpwnam('mysql') )[ 2, 3 ];
        mkdir('/var/db/mysql');
        chown $mysqluid, $mysqlgid, '/var/db/mysql';
    }

    # Unset Rlimits since compile on FreeBSD may be very memory intensive
    Cpanel::Rlimit::set_rlimit_to_infinity();

    while ( !$complete ) {
        if ( -x '/scripts/premysqlup' ) {
            system '/scripts/premysqlup';
        }

        foreach my $pkg (@PKGLIST) {
            my $result;
            my @GS;
            if ($force) {
                @GS = ( '/scripts/installpkg', '--force', $pkg );
            }
            else {
                @GS = ( '/scripts/installpkg', '--keepnew', $pkg );
            }
            open( RNULL, '<', '/dev/null' );
            my $ipid = IPC::Open3::open3( '<&RNULL', \*INS, \*INS, @GS );
            while (<INS>) {
                print;
                $result .= $_;
            }
            close(INS);
            close(RNULL);
            waitpid( $ipid, 0 );

            my $onelineresult = $result;
            $onelineresult =~ s/\n/ /g;

            if (   $result =~ /(^|(?<!Checking if)\s)\s*\S*${pkg}.*\salready\s+installed/m
                || $result =~ /${pkg}.*\sis\s+installed/m
                || $result =~ /${pkg}.*\sis\s+newer/m ) {
                $complete = 1;
            }
            elsif ( $onelineresult =~ m/conflicts\s+with.+mysql.+(install|remove)/i ) {
                system 'pkg_delete', '-rx', 'mysql-server';
                system 'pkg_delete', '-rx', 'mysql-client';
                $complete = 0;
            }
            else {
                $complete = 1;
                $needres  = 1;
            }
        }
    }

    #fixmysqlmysqldb must come first or if !$up2date is true it will never happen :)
    installmysqldb();
    if ($needres) {
        fixmysqlmysqldb();
        if ( -x '/scripts/postmysqlup' ) {
            system '/scripts/postmysqlup';
        }

        if ( -x '/scripts/postmysqlinstall' ) {
            system '/scripts/postmysqlinstall';
        }
        resmysql();
    }
    return $needres;
}

sub cPPkgs_linuxmysqlinstall {
    my ($cPPkg) = @_;
    $cPPkg->setupfiles( $CPCONF{'mysql-version'} );
    my @FILES = $cPPkg->getfilelist( $CPCONF{'mysql-version'} );

    print 'This is the cPPkgs::' . $cPPkg->{'pkgname'} . ' ' . $cPPkg->{'pkgdata'}{ $cPPkg->{'arch'} }{ $CPCONF{'mysql-version'} }{'version'} . " installer for Linux.\n";

    my $up2date = 1;
    foreach my $url (@FILES) {
        if ( $url =~ /shared-compat/ ) {

            #            warn "FIXME:: shared-compat is no longer needed";
            #            sleep 5;
            next;
        }

        my $rpmfile = Cpanel::UrlTools::urltofile($url);
        if ( !Cpanel::RpmUtils::isinstalled( 'rpm' => $rpmfile, 'stripdot' => 0 ) || $force ) {
            if ($up2date) {

                if ( -x '/scripts/premysqlup' ) {
                    system '/scripts/premysqlup';
                }

            }

            $up2date = 0;
            mkdir "/home/${pkg}-install", 0700;
            chdir "/home/${pkg}-install" or die "Unable to chdir to /home/${pkg}-install: $!";

            $httpClient->download( $url, $rpmfile );

            my $goodstatus = Cpanel::RpmUtils::checkrpm($rpmfile);
            if ( !$goodstatus ) {
                print "Failed to download a working ${rpmfile}! Falling back to backup plan.\n";
                return -1;
            }

            $ENV{'RPMINSTALL'} = 1;
            Cpanel::RpmUtils::rpmpreinstall($rpmfile);
            system 'rpm', '-Uvh', '--nodeps', '--force', $rpmfile;
            unlink($rpmfile);
            $ENV{'RPMINSTALL'} = 0;
            chdir '/';
        }
    }

    if ($up2date) {
        print $cPPkg->{'pkgname'} . ' ' . $cPPkg->{'pkgdata'}{ $cPPkg->{'arch'} }{ $CPCONF{'mysql-version'} }{'version'} . " is up to date\n";
    }

    #fixmysqlmysqldb must come first or if !$up2date is true it will never happen :)
    installmysqldb();
    if ( !$up2date ) {
        fixmysqlmysqldb();
        if ( -x '/scripts/postmysqlup' ) {
            system '/scripts/postmysqlup';
        }

        if ( -x '/scripts/postmysqlinstall' ) {
            system('/scripts/postmysqlinstall');
        }

        resmysql();
    }
    return 1 if !$up2date;
    return 0;

}

sub linuxmysqlinstall {
    my %VINFO = cPpkgversions::getpkgversioninfo('mysql');

    # Variable used to determine if we should patch mysql_config
    my $is64bit = 0;

    my $pkgname = 'mysql';

    if ( $CPCONF{'mysql-version'} eq '5.0' ) {
        $pkgname .= '50';
    }
    if ( $CPCONF{'mysql-version'} eq '4.1' ) {
        $pkgname .= '41';
    }
    if ( $CPCONF{'mysql-version'} eq '4' || $CPCONF{'mysql-version'} eq '4.0' ) {
        $pkgname .= '40';
    }

    if ( $machine =~ /64/ ) {
        $pkgname .= '-64';
        $is64bit = 1;
    }

    $pkgver   = $VINFO{$pkgname}{'version'};
    $buildnum = $VINFO{$pkgname}{'release'};
    @PKGLIST  = split( /\,/, $VINFO{$pkgname}{'pkgs'} );

    #we install shared twice to make rpm deps happy
    my $mysqlversion = $pkgver;
    $mysqlversion =~ s/\.\w+$//g;

    print "This is the $pkg $pkgver installer release number ${buildnum} for platform\n";

    my %CVER;
    my $up2date = 1;
    foreach my $subpkg (@PKGLIST) {
        last if $force;
        my $cver = `rpm -q ${pkg}-${subpkg} 2>/dev/null`;
        chomp $cver;
        $CVER{ $pkg . '-' . $subpkg } = $cver;

        if ( $cver ne "${pkg}-${subpkg}-${pkgver}-${buildnum}" ) {
            print "${pkg}-${subpkg} is out of date ($cver != ${pkg}-${subpkg}-${pkgver}-${buildnum})\n";
            $up2date = 0;
        }
    }
    if ($force) { $up2date = 0; }

    if ($up2date) {
        print "${pkg}-${pkgver} is up to date\n";
    }
    else {
        if ( -x '/scripts/premysqlup' ) {
            system '/scripts/premysqlup';
        }

        my $syspkgobj = Cpanel::SysPkgs->new();
        if ( !$syspkgobj ) { die print "Could not create SysPkgs object\n"; }
        my @pkgs = ( 'libdb3.3-devel', 'db4-devel', 'pam-devel', 'openssl', 'openssl-devel', 'gcc', 'glibc-devel', 'compat-db', 'zlib', 'zlib-devel', 'rpm-build' );
        $syspkgobj->install( 'pkglist' => \@pkgs );

        system '/scripts/checklibssl';

        print "Resetting $pkg to cPanel Defaults and Installing the Latest Version\n";

        foreach my $subpackage (@PKGLIST) {
            last if $src;

            next if ( $CVER{"${pkg}-${subpackage}"} eq "${pkg}-${subpackage}-${pkgver}-${buildnum}" && !$force );

            print "About to fetch ${pkg}-${subpackage}-${pkgver}-${buildnum}.${arch}.rpm\n";

            my $file = fetchmysqlrpm("MySQL-${mysqlversion}/${pkg}-${subpackage}-${pkgver}-${buildnum}.${arch}.rpm");

            die "File $file is not valid" if ( $file eq '' );

            if ( ( stat($file) )[7] <= 0 ) {
                print "Failed to download ${pkg}-${subpackage}-${pkgver}-${buildnum}.${arch}.rpm .. using source instead\n";
                $latest = 1;
                $src    = 1;
                last;
            }

            $ENV{'RPMINSTALL'} = 1;
            Cpanel::RpmUtils::rpmpreinstall($file);
            system 'rpm', '-Uvh', '--nodeps', '--force', $file;
            unlink $file;
            $ENV{'RPMINSTALL'} = 0;
            chdir '/';
        }

        if ($src) {
            my $file = fetchmysqlrpm("MySQL-${mysqlversion}/${pkg}-${pkgver}-${buildnum}.src.rpm");
            my ( $rFILES, $rBLOG ) = buildmysqlrpm( $verbose, $file );
            my @FILES;
            if ( ref $rFILES eq 'ARRAY' ) { @FILES = @{$rFILES}; }

            if ( $#FILES < 2 ) {
                print join( '', @{$rBLOG} );
                die "Rpm failed to build!  Try recalling mysqlup with --source --lastest --verbose";
            }

            $ENV{'RPMINSTALL'} = 1;
            foreach my $rpmfile (@FILES) {
                Cpanel::RpmUtils::rpmpreinstall($rpmfile);
            }
            system 'rpm', '-Uvh', '--nodeps', '--force', @FILES;
            unlink(@FILES);
            $ENV{'RPMINSTALL'} = 0;
            chdir '/';
        }
        if ( $is64bit && -e '/usr/bin/mysql_config' ) {

            # Fix mysql_config
            require '/scripts/regsrep.pl';
            regsrep( '/usr/bin/mysql_config', qr{^fix_path\spkglibdir\slib[/]mysql\slib\s*$}, q{fix_path pkglibdir lib64 lib/mysql lib}, 'useregex', '', '' );
        }
        if ( -x '/scripts/postmysqlup' ) {
            system '/scripts/postmysqlup';
        }

        if ( -x '/scripts/postmysqlinstall' ) {
            system('/scripts/postmysqlinstall');
        }

    }

    #fixmysqlmysqldb must come first or if !$up2date is true it will never happen :)
    installmysqldb();
    if ( !$up2date ) {
        fixmysqlmysqldb();
        resmysql();
    }
    return 1 if !$up2date;
    return 0;
}

sub checkselinux {
    if ( Cpanel::Selinux::is_enabled() ) {
        Cpanel::Selinux::set_context( '/scripts/selinux_custom_contexts', '/' );
    }
}

sub installmysqldb {
    my $returnval    = 0;
    my $mysqldatadir = findmysqldatadir();
    if ( !-e $mysqldatadir . '/mysql' ) {
        my $mysqlinstalldb = findfileinpath('mysql_install_db');
        Cpanel::SafeRun::Errors::saferunallerrors($mysqlinstalldb);
        $returnval = 1;
    }
    Cpanel::SafetyBits::safe_recchown( 'mysql', 'mysql', $mysqldatadir );
    if ( !-d $mysqldatadir . '/mysql' ) {
        warn "Unable to create mysql privileges database";
        $returnval = 0;
    }
    return $returnval;
}

sub fixmysqlmysqldb {
    
    # TODO: Once http://bugs.mysql.com/bug.php?id=48392 is fixed, switch to using mysql_upgrade on 5.0+

    my $mysql_fix_privilege_tables = findfileinpath('mysql_fix_privilege_tables');
    if ( $mysql_fix_privilege_tables ne '' ) {
        my $dbpass = Cpanel::MysqlUtils::getmydbpass('root');
        if ( $CPCONF{'mysql-version'} eq '4.1' || $CPCONF{'mysql-version'} eq '5.0' ) {
            print Cpanel::SafeRun::Errors::saferunnoerror( $mysql_fix_privilege_tables, '--verbose', '--password=' . $dbpass );
        }
        else {
            print Cpanel::SafeRun::Errors::saferunnoerror( $mysql_fix_privilege_tables, '--password=' . $dbpass );
        }
    }
    else {
        print "mysql_fix_privilege_tables not found in path, skipping\n";
    }
}

sub findmysqldatadir {
    if ( open( my $fh, '<', '/etc/my.cnf' ) ) {
        while ( my $line = <$fh> ) {
            if ( $line =~ /^datadir\s*=\s*"?([^"]+)"?$/ ) {
                my $datadir = $1;
                if ( $datadir && -d $datadir ) {
                    return $datadir;
                }
            }
        }
        close $fh;
    }

    if ( -d '/var/db/mysql' ) {
        return '/var/db/mysql';
    }
    return '/var/lib/mysql';
}

sub findfileinpath {
    my $bin  = shift;
    my @path = qw( /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin /bin /sbin );
    foreach my $dir (@path) {
        if ( -e $dir . '/' . $bin ) {
            return $dir . '/' . $bin;
        }
    }
    return;
}

sub buildmysqlrpm {
    my ( $verbose, $dfile ) = @_;
    my (@FILES);

    enablecompilers();

    my $syspkgobj = Cpanel::SysPkgs->new();
    if ( !$syspkgobj ) { die print "Could not create SysPkgs object\n"; }
    my @pkgs = ( 'rpm', 'rpm-build', 'db-devel', 'gdbm-devel', 'db1-devel', 'compat-db', 'libtool', 'libtool-libs', 'gcc-c++', 'expect', 'gcc-c++-devel', 'ndbm-devel', 'zlib', 'zlib-devel' );
    $syspkgobj->install( 'pkglist' => \@pkgs );

    my $rpm = 'rpm';
    if ( -e '/usr/bin/rpmbuild' ) { $rpm = '/usr/bin/rpmbuild'; }

    #my $basedir = ( getpwnam('cpanel') )[7];
    my $basedir = '/usr/src';
    Cpanel::SafeDir::MK::safemkdir( $basedir . '/rpm-build/SRPMS/' );

    my $sleep_count = 0;

    my $pid;
    do {
        $pid = open( KR, '-|' );
        unless ( defined $pid ) {
            warn "cannot fork: $!";
            die 'bailing out' if $sleep_count++ > 6;
            sleep 10;
        }
    } until defined $pid;

    my @BLOG;

    #setuprpmbuildroot($basedir);
    Cpanel::SafetyBits::safe_recchown( 'cpanel', 'cpanel', $basedir . '/rpm-build' );
    if ($pid) {
        print 'Building RPM...';
        while (<KR>) {
            push( @BLOG, $_ );
            if   ($verbose) { print; }
            else            { print '.'; }
            if (/^Wrote: (\S+)/) {
                my $file = $1;
                if ( $file =~ /.(64|86)\.rpm/ ) {
                    push( @FILES, $file );
                }
            }
        }
        close(KR);
        waitpid( $pid, 0 );
        print "Done\n";
    }
    else {
        my @BP       = split( /\//, $dfile );
        my $basefile = $BP[$#BP];
        my $file     = "${basedir}/rpm-build/SRPMS/$basefile";
        Cpanel::FileUtils::Copy::safecopy( $dfile, $file );

        #Cpanel::SafetyBits::setuids('cpanel');
        #$ENV{'HOME'} = $basedir;
        #$ENV{'USER'} = 'cpanel';
        open( RNULL, '<', '/dev/null' );
        my $rpid = IPC::Open3::open3( '<&RNULL', '>&STDOUT', '>&STDOUT', $rpm, '--rebuild', $file );
        close(RNULL);
        waitpid( $rpid, 0 );
        exit;
    }

    foreach my $file (@FILES) {
        Cpanel::RpmStor::rpmstor($file);
    }
    return \@FILES, \@BLOG;
}

sub setuprpmbuildroot {
    my $dir  = shift;
    my $arch = ( Cpanel::OSSys::uname() )[4];
    mkdir( "${dir}/rpm-build",              0700 );
    mkdir( "${dir}/rpm-build/tmp",          0700 );
    mkdir( "${dir}/rpm-build/SOURCES",      0700 );
    mkdir( "${dir}/rpm-build/SPECS",        0700 );
    mkdir( "${dir}/rpm-build/BUILD",        0700 );
    mkdir( "${dir}/rpm-build/SRPMS",        0700 );
    mkdir( "${dir}/rpm-build/RPMS",         0700 );
    mkdir( "${dir}/rpm-build/RPMS/i386",    0700 );
    mkdir( "${dir}/rpm-build/RPMS/${arch}", 0700 );
    open( RPMMACROS, ">", "${dir}/.rpmmacros" );
    print RPMMACROS '%_tmppath  ' . $dir . '/rpm-build/tmp' . "\n";
    print RPMMACROS '%_topdir  ' . $dir . '/rpm-build';
    print RPMMACROS "\n";
    close(RPMMACROS);
    $ENV{'TMP'} = $dir . '/rpm-build/tmp';

}

sub enablecompilers {
    my $user  = 'cpanel';
    my $group = 'compiler';
    if ( -e "/usr/sbin/pw" ) {
        system( "/usr/sbin/pw", "usermod", $user, "-g", $user, "-G", "${group},${user}" );
    }
    else {
        system( "/usr/sbin/usermod", "-g", $user, "-G", "${group},${user}", $user );
    }

}

sub check_for_obsolete_embedded {
    my $embedded_installed = Cpanel::SafeRun::Errors::saferunnoerror( 'rpm', '-q', 'MySQL-embedded-4*' );
    chomp $embedded_installed;
    if ( $embedded_installed =~ /^MySQL-embedded-4\.\d+\.\d+\S+$/ ) {
        print "Removing obsolete ${embedded_installed} package...";
        system 'rpm -e --nodeps ' . $embedded_installed . ' 2>/dev/null';
        print "Done\n";
    }
}