From 8925f396cff17987c0bee41ff313e8eee865658d Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Tue, 14 Nov 2023 19:56:22 +0200 Subject: [PATCH] Finalize support for systems using`systemd-timesyncd` and `chronyd` --- time/apply.cgi | 29 ++++++++++++++++++++++++ time/index.cgi | 58 ++++++++++++++++++++++++++++++++++++++++-------- time/lang/en | 3 +++ time/time-lib.pl | 3 +-- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/time/apply.cgi b/time/apply.cgi index d9397a82f..87af93f9f 100755 --- a/time/apply.cgi +++ b/time/apply.cgi @@ -52,6 +52,35 @@ if ($in{'action'} eq $text{'action_sync'}) { } elsif ($in{'action'} eq $text{'index_sync'} || $in{'mode'} eq 'ntp') { # Sync with a time server $access{'ntp'} || &error($text{'acl_nontp'}); + # Save service status + if (defined($in{'sync_service_name'}) && + defined($in{'sync_service_status'})) { + my $service_name = $in{'sync_service_name'}; + if ($service_name !~ /^(chronyd|systemd-timesyncd)$/) { + &error(&text('error_serviceunknown', &html_escape($service_name))); + } + my $service_status = int($in{'sync_service_status'}); + &foreign_require('init'); + if ($service_status == 2) { + # Enable service on boot + &init::enable_at_boot($service_name); + # Start service + &init::restart_action($service_name); + } + if ($service_status == 1) { + # Disable service on boot + &init::disable_at_boot($service_name); + # Start service + &init::restart_action($service_name); + } + if ($service_status == 0) { + # Disable service on boot + &init::disable_at_boot($service_name); + # Stop service + &init::stop_action($service_name); + } + } + # Run sync $in{'timeserver'} =~ /\S/ || &error($text{'error_etimeserver'}); $err = &sync_time($in{'timeserver'}, $in{'hardware'}); &error("
".&html_escape($err)."
") if ($err); diff --git a/time/index.cgi b/time/index.cgi index a60e7360c..fa0da10a0 100755 --- a/time/index.cgi +++ b/time/index.cgi @@ -162,15 +162,55 @@ if ( ( !$access{ 'sysdate' } && &has_command( "date" ) || !$access{ 'hwdate' } & print &ui_hidden("mode", "ntp"); print &ui_table_start(&hlink($text{'index_timeserver'}, "timeserver"), "width=100%", 2, [ "width=30%" ]); - - print &ui_table_row($text{'index_addresses'}, - &ui_textbox("timeserver", $config{'timeserver'}, 60)); - - # Show hardware time checkbox - if ($support_hwtime) { - print &ui_table_row(" ", - &ui_checkbox("hardware", 1, $text{'index_hardware2'}, - $config{'timeserver_hardware'})); + my $ntp_support; + if (&has_command("ntpdate") || &has_command("sntp")) { + $ntp_support = 1; + print &ui_table_row($text{'index_addresses'}, + &ui_textbox("timeserver", $config{'timeserver'}, 60)); + # Show hardware time checkbox + if ($support_hwtime) { + print &ui_table_row(" ", + &ui_checkbox("hardware", 1, $text{'index_hardware2'}, + $config{'timeserver_hardware'})); + } + } + if (&foreign_require('init') && &init::action_status('chronyd') > 0 && &has_command("chronyc")) { + my $chronyd_running = &init::status_action('chronyd'); + my $chronyd_run_atboot = &init::action_status('chronyd'); + my $ui_hiddens = &ui_hidden("sync_service_name", "chronyd"); + $ui_hiddens .= &ui_hidden("timeserver", $config{'timeserver'}) + if (!$ntp_support); + print &ui_table_row(&text('index_tabsync2', 'chronyd'), + $ui_hiddens. + &ui_radio("sync_service_status", + (($chronyd_run_atboot == 2 && $chronyd_running) ? 2 : + ($chronyd_run_atboot < 2 && $chronyd_running) ? 1 : 0), + [ [ 2, $text{'index_serviceonboot'} ], [ 1, $text{'yes'} ], [ 0, $text{'no'} ] ])); + # Show hardware time checkbox + if ($support_hwtime) { + print &ui_table_row(" ", + " ".&ui_checkbox("hardware", 1, $text{'index_hardware2'}, + $config{'timeserver_hardware'})); + } + } + if (&foreign_require('init') && &init::action_status('systemd-timesyncd') > 0) { + my $systemd_timesyncd_running = &init::status_action('systemd-timesyncd'); + my $systemd_timesyncd_run_atboot = &init::action_status('systemd-timesyncd'); + my $ui_hiddens = &ui_hidden("sync_service_name", "systemd-timesyncd"); + $ui_hiddens .= &ui_hidden("timeserver", $config{'timeserver'}) + if (!$ntp_support); + print &ui_table_row(&text('index_tabsync2', 'systemd-timesyncd'), + $ui_hiddens. + &ui_radio("sync_service_status", + (($systemd_timesyncd_run_atboot == 2 && $systemd_timesyncd_running) ? 2 : + ($systemd_timesyncd_run_atboot < 2 && $systemd_timesyncd_running) ? 1 : 0), + [ [ 2, $text{'index_serviceonboot'} ], [ 1, $text{'yes'} ], [ 0, $text{'no'} ] ])); + # Show hardware time checkbox + if ($support_hwtime) { + print &ui_table_row(" ", + " ".&ui_checkbox("hardware", 1, $text{'index_hardware2'}, + $config{'timeserver_hardware'})); + } } # Show boot-time checkbox diff --git a/time/lang/en b/time/lang/en index 93578ccfb..7fcad73a6 100644 --- a/time/lang/en +++ b/time/lang/en @@ -5,6 +5,7 @@ index_zone=Timezone index_eformat=Unrecognized hwclock output format index_sched=Synchronize on schedule? index_schedyes=Yes, at times below .. +index_serviceonboot=Yes, and start at boot index_sync=Sync and Apply index_hardware2=Set hardware time too index_boot=Synchronize when Webmin starts? @@ -14,6 +15,7 @@ index_ehwclock=$1 failed : $2 index_tabtime=Set time index_tabzone=Change timezone index_tabsync=Time server sync +index_tabsync2=Run system $1 service for synchronization index_desctime=This form is for changing the system's current time, which is used by all running processes. On operating systems that have a separate hardware clock, it can be used to set that too. index_desczone=This form allows you to set the system's default time zone, which is used to convert the system time to a human-readable format and offset. index_descsync=This form is for configuring the system to automatically synchronize the time with a remote server. Synchronization will be done using the Unix time protocol or NTP, depending on which commands are installed and what the remote system supports. @@ -54,6 +56,7 @@ error_sync=Can't sync with system time : $1 error_ediff=Time server different with local time is too large : $1 days error_entp=NTP time synchronization failed : $1 error_etimeserver=No time servers to sync with entered +error_serviceunknown=Unknown service : $1 log_set_date=Set system time to $1 log_set_hwclock=Set hardware clock to $1 diff --git a/time/time-lib.pl b/time/time-lib.pl index 44ea8db90..7164a6ff3 100755 --- a/time/time-lib.pl +++ b/time/time-lib.pl @@ -48,8 +48,7 @@ if (&has_command("ntpdate")) { elsif (&has_command("sntp")) { $out = &backquote_logged("sntp -s $servs 2>&1"); } -elsif (&has_command("chronyc")) { - &foreign_require('init'); +elsif (&foreign_require('init') && &init::action_status('chronyd') > 0 && &has_command("chronyc")) { my $chronyd_running = &init::status_action('chronyd'); $out = &backquote_logged("systemctl restart chronyd 2>&1"); $out .= &backquote_logged("chronyc makestep 2>&1");