From 081008358802b5ff77a78f49dc19de605dcd555c Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Sun, 10 May 2026 19:48:18 +0200 Subject: [PATCH] Fix skip hwclock when unavailable #2693 --- time/apply.cgi | 14 +++-------- time/index.cgi | 5 ++-- time/time-lib.pl | 65 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/time/apply.cgi b/time/apply.cgi index cda7a9017..0b5b22974 100755 --- a/time/apply.cgi +++ b/time/apply.cgi @@ -1,8 +1,5 @@ #!/usr/local/bin/perl -local $format; -local $out; - require "./time-lib.pl"; use Time::Local; @@ -17,17 +14,15 @@ $mode = "time"; if ($in{'action'} eq $text{'action_sync'}) { # Set system time to hardware time &error( $text{ 'acl_nosys' } ) if( $access{ 'sysdate' } ); - local $flags = &get_hwclock_flags(); - $out = &backquote_logged("hwclock $flags --hctosys"); - &error( &text( 'error_sync', $out ) ) if( $out ne "" ); + $err = &set_system_time_to_hardware_time(); + &error( &text( 'error_sync', &html_escape($err) ) ) if ($err); &webmin_log("sync"); } elsif ($in{'action'} eq $text{'action_sync_s'}) { # Set hardware time to system time &error( $text{ 'acl_nohw' } ) if( $access{ 'hwdate' } && $access{'sysdate'} ); - local $flags = &get_hwclock_flags(); - $out = &backquote_logged("hwclock $flags --systohc"); - &error( &text( 'error_sync', $out ) ) if( $out ne "" ); + $err = &set_hardware_time_to_system_time(); + &error( &text( 'error_sync', &html_escape($err) ) ) if ($err); &webmin_log("sync_s"); } elsif($in{'action'} eq $text{'action_apply'} || $in{'mode'} eq 'sysdate' ) { @@ -111,4 +106,3 @@ if ($in{'action'} eq $text{'action_sync'}) { } &redirect("index.cgi?mode=$mode"); - diff --git a/time/index.cgi b/time/index.cgi index e8035850a..408838746 100755 --- a/time/index.cgi +++ b/time/index.cgi @@ -104,7 +104,7 @@ if ($support_hwtime) { print &tabletime(&hlink($text{'hw_title'}, "hardware_time"), 0, %hw_date); print &ui_submit($text{'action_save'}, "action"); - if (support_hwtime()) { + if ($support_hwtime) { print &ui_submit($text{'action_sync_s'}, "action"); } print &ui_form_end(); @@ -153,7 +153,8 @@ if ($access{'timezone'} && &has_timezone()) { print &ui_tabs_end_tab(); } -if ( ( !$access{ 'sysdate' } && &has_command( "date" ) || !$access{ 'hwdate' } && &has_command( "hwclock" ) ) && $access{'ntp'} ) +if (((!$access{'sysdate'} && &has_command("date")) || + (!$access{'hwdate'} && $support_hwtime)) && $access{'ntp'}) { # Show time server input print &ui_tabs_start_tab("mode", "sync"); diff --git a/time/time-lib.pl b/time/time-lib.pl index 86655625b..09ea40b33 100755 --- a/time/time-lib.pl +++ b/time/time-lib.pl @@ -102,9 +102,10 @@ elsif ($?) { if ($hwtoo) { # Set hardware clock time to match system time (which is now correct) - my $flags = &get_hwclock_flags(); - my $out = &backquote_logged("hwclock $flags --systohc"); - return $? ? $out : undef; + if (&support_hwtime()) { + my $err = &set_hardware_time_to_system_time(); + return $err if ($err); + } } return undef; @@ -163,16 +164,34 @@ else { } } +# hwclock_command() +# Returns the path to the hwclock command, if available +sub hwclock_command +{ +return &has_command("hwclock"); +} + +# hwclock_missing_error() +sub hwclock_missing_error +{ +return &text('error_cnf', "hwclock"); +} + # get_hardware_time() # Returns the current hardware time, in localtime format. On failure returns # an empty array, and sets the global $get_hardware_time_error sub get_hardware_time { +my $hwclock = &hwclock_command(); +$get_hardware_time_error = undef; +if (!$hwclock) { + $get_hardware_time_error = &hwclock_missing_error(); + return ( ); + } my $flags = &get_hwclock_flags(); $flags ||= ""; -$get_hardware_time_error = undef; &clean_language(); -my $out = &backquote_command("hwclock $flags 2>/dev/null"); +my $out = &backquote_command("$hwclock $flags 2>/dev/null"); &reset_environment(); if ($out =~ /^(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)\s+/) { return ($6, $5, $4, $3, &month_to_number($2), $7-1900, &weekday_to_number($1)); @@ -186,7 +205,7 @@ elsif ($out =~ /^(\d+)\-(\d+)\-(\d+)\s+(\d+):(\d+):(\d+)/) { } else { $get_hardware_time_error = &text('index_ehwclock', - "".&html_escape("hwclock $flags")."", + "".&html_escape("$hwclock $flags")."", "
".&html_escape($out)."
"); return ( ); } @@ -203,15 +222,41 @@ return localtime(time()); sub set_hardware_time { my ($second, $minute, $hour, $date, $month, $year) = @_; +my $hwclock = &hwclock_command(); +return &hwclock_missing_error() if (!$hwclock); $month++; $year += 1900; my $format = "--set --date=". quotemeta("$year-$month-$date $hour:$minute:$second"); my $flags = &get_hwclock_flags(); -my $out = &backquote_logged("hwclock $flags $format 2>&1"); +my $out = &backquote_logged("$hwclock $flags $format 2>&1"); return $? ? $out : undef; } +# set_hardware_time_to_system_time() +# Sets the hardware time to the current system time +sub set_hardware_time_to_system_time +{ +my $hwclock = &hwclock_command(); +return &hwclock_missing_error() if (!$hwclock); +my $flags = &get_hwclock_flags(); +$flags ||= ""; +my $out = &backquote_logged("$hwclock $flags --systohc 2>&1"); +return $? ? $out : undef; +} + +# set_system_time_to_hardware_time() +# Sets the system time to the current hardware time +sub set_system_time_to_hardware_time +{ +my $hwclock = &hwclock_command(); +return &hwclock_missing_error() if (!$hwclock); +my $flags = &get_hwclock_flags(); +$flags ||= ""; +my $out = &backquote_logged("$hwclock $flags --hctosys 2>&1"); +return $? || $out ne "" ? $out : undef; +} + # set_system_time(secs, mins, hours, day, month, year) sub set_system_time { @@ -278,8 +323,9 @@ return defined($_[0]) ? ucfirst($weekday_names[$_[0]]) : undef; # Returns 1 if this system supports setting the hardware clock. sub support_hwtime { -return &has_command("hwclock") && - &execute_command("hwclock") == 0 && +my $hwclock = &hwclock_command(); +return $hwclock && + &execute_command($hwclock) == 0 && !&running_in_xen() && !&running_in_vserver() && !&running_in_openvz() && !&running_in_zone(); } @@ -307,4 +353,3 @@ if ($modconf_info) { } 1; -