\!/ KyuuKazami \!/

Path : /scripts/
Upload :
Current File : //scripts/rdate

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - rdate                                     Copyright 2017 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;
use warnings;
use Cpanel::Unix::PID::Tiny ();
use Cpanel::GenSysInfo      ();
use Cpanel::SafeRun::Simple ();
use Getopt::Long            ();
use Cpanel::OSSys::Env      ();

my $print_time = 0;    # used to map usage to rdate's -p option

Getopt::Long::GetOptions(
    '-p|print' => \$print_time,
);

my $envtype = Cpanel::OSSys::Env::get_envtype();

# Case 48348 -- Virtual environments cannot set the system time so do nothing.
if ( $envtype eq 'virtuozzo' || $envtype eq 'cpanel-vserver' || $envtype eq 'vzcontainer' ) {
    print "Container environment detected - rdate skipped\n";
    exit 0;
}
else {
    my $pid = 0;

    my $sysinfo = Cpanel::GenSysInfo::run();
    if ( ( $sysinfo->{'rpm_dist'} eq 'centos' || $sysinfo->{'rpm_dist'} eq 'rhel' || $sysinfo->{'rpm_dist'} eq 'cloudlinux' ) && $sysinfo->{'rpm_dist_ver'} == 7 ) {
        $pid = _detect_systemd_managed_ntpd();
    }
    else {
        my $upid = Cpanel::Unix::PID::Tiny->new();
        $pid = $upid->is_pidfile_running('/var/run/ntpd.pid');
    }

    if ($pid) {
        print "The 'ntpd' daemon is running on PID '$pid'. Exiting.\n";
    }
    else {
        my @args = ( '/usr/bin/rdate', '-s' );
        push( @args, '-p' ) if $print_time;
        push( @args, 'rdate.cpanel.net' );
        system(@args);
    }
}

exit 0;

sub _detect_systemd_managed_ntpd {

    my $output = Cpanel::SafeRun::Simple::saferunallerrors( '/usr/bin/systemctl', 'status', 'ntpd' );
    if ( $output =~ m/Main\s+PID:\s+(\d+)\s+\(/ ) {
        return $1;
    }
}


@KyuuKazami