%PDF- %PDF-
| Direktori : /scripts/ |
| Current File : //scripts/rdate |
#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - scripts/rdate Copyright 2021 cPanel, L.L.C.
# All rights reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use cPstrict;
use Cpanel::Unix::PID::Tiny ();
use Cpanel::SafeRun::Simple ();
use Getopt::Long ();
use Cpanel::OS ();
use Cpanel::OSSys::Env ();
my $timeout = 10;
exit( _run_alarmed( $timeout, @ARGV ) // 0 ) unless caller;
sub run(@args) {
my $print_time; # used to map usage to rdate's -p option
my $help;
Getopt::Long::GetOptionsFromArray(
\@args,
'-p|print' => \$print_time,
'-h|help' => \$help,
) or return usage(1);
return usage() if $help;
my $envtype = Cpanel::OSSys::Env::get_envtype();
# Case 48348 -- Virtual environments cannot set the system time so do nothing.
if ( $envtype =~ qr{^(?:virtuozzo|cpanel-vserver|vzcontainer)$} ) {
say "Container environment detected - rdate skipped";
return;
}
my $pid = 0;
my $check_ntpd_pid_method = Cpanel::OS::check_ntpd_pid_method();
if ( $check_ntpd_pid_method eq 'systemd_ntpd' ) {
$pid = _detect_systemd_managed_ntpd();
}
elsif ( $check_ntpd_pid_method eq 'pid_check_var_run_ntpd' ) {
my $upid = Cpanel::Unix::PID::Tiny->new();
$pid = $upid->is_pidfile_running('/var/run/ntpd.pid');
}
else {
die qq[Unknown check_ntpd_pid_method method: $check_ntpd_pid_method];
}
if ($pid) {
say "The 'ntpd' daemon is running on PID '$pid'. Exiting.";
}
else {
my @rdate = ( '/usr/local/cpanel/bin/rdate', '-s' );
push( @rdate, '-p' ) if $print_time;
push( @rdate, 'rdate.cpanel.net' );
system(@rdate);
}
return;
}
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+\(/a ) {
return $1;
}
return;
}
sub _run_alarmed {
my ( $timeout, @args ) = @_;
{
require Cpanel::Alarm;
my $alrm = Cpanel::Alarm->new( $timeout, sub { die "Failed to run due to the $timeout second timeout being exceeded.\n" } );
run(@args);
}
return;
}
sub usage($status) {
$status //= 0;
print <<EOS;
scripts/rdate
On servers without ntp daemon, use 'rdate' to adjust the system clock.
Options:
--help [-h] display this help
--print [-p] Print the time returned by the remote machine.
Usage:
scripts/rdate
scripts/rdate -p
EOS
return $status;
}