Skip to content

Commit

Permalink
Improve handling of permissions around clock control.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 committed Oct 25, 2023
1 parent 95e254e commit 53a15d8
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 69 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [unreleased]

### Added
- Server can now be run without permission to change the system clock so long
as no time sources are configured.

### Changed
- The sources section can be left out of the configuration now.
- When no sources are configured, the daemon will merely state it won't change
Expand Down
35 changes: 17 additions & 18 deletions ntp-proto/src/algorithm/kalman/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,13 @@ impl<C: NtpClock, PeerID: Hash + Eq + Copy + Debug> TimeSyncController<C, PeerID
synchronization_config: SynchronizationConfig,
peer_defaults_config: SourceDefaultsConfig,
algo_config: Self::AlgorithmConfig,
) -> Self {
) -> Result<Self, C::Error> {
// Setup clock
clock
.disable_ntp_algorithm()
.expect("Unable to change system time");
clock
.status_update(NtpLeapIndicator::Unknown)
.expect("Unable to update clock");
clock
.set_frequency(0.0)
.expect("Unable to set system clock frequency");

KalmanClockController {
clock.disable_ntp_algorithm()?;
clock.status_update(NtpLeapIndicator::Unknown)?;
clock.set_frequency(0.0)?;

Ok(KalmanClockController {
peers: HashMap::new(),
clock,
synchronization_config,
Expand All @@ -333,7 +327,7 @@ impl<C: NtpClock, PeerID: Hash + Eq + Copy + Debug> TimeSyncController<C, PeerID
desired_freq: 0.0,
timedata: TimeSnapshot::default(),
in_startup: true,
}
})
}

fn update_config(
Expand Down Expand Up @@ -466,7 +460,8 @@ mod tests {
synchronization_config,
peer_defaults_config,
algo_config,
);
)
.unwrap();
let mut cur_instant = NtpInstant::now();

// ignore startup steer of frequency.
Expand Down Expand Up @@ -531,7 +526,8 @@ mod tests {
synchronization_config,
peer_defaults_config,
algo_config,
);
)
.unwrap();

algo.in_startup = false;
algo.steer_offset(1000.0, 0.0);
Expand Down Expand Up @@ -560,7 +556,8 @@ mod tests {
synchronization_config,
peer_defaults_config,
algo_config,
);
)
.unwrap();

algo.in_startup = false;
algo.steer_offset(1000.0, 0.0);
Expand All @@ -584,7 +581,8 @@ mod tests {
synchronization_config,
peer_defaults_config,
algo_config,
);
)
.unwrap();
let mut cur_instant = NtpInstant::now();

// ignore startup steer of frequency.
Expand Down Expand Up @@ -640,7 +638,8 @@ mod tests {
synchronization_config,
peer_defaults_config,
algo_config,
);
)
.unwrap();
let mut cur_instant = NtpInstant::now();

// ignore startup steer of frequency.
Expand Down
4 changes: 2 additions & 2 deletions ntp-proto/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<PeerID: Eq + Copy + Debug> Default for StateUpdate<PeerID> {
}
}

pub trait TimeSyncController<C: NtpClock, PeerID: Hash + Eq + Copy + Debug> {
pub trait TimeSyncController<C: NtpClock, PeerID: Hash + Eq + Copy + Debug>: Sized {
type AlgorithmConfig: Debug + Copy + DeserializeOwned;

/// Create a new clock controller controling the given clock
Expand All @@ -54,7 +54,7 @@ pub trait TimeSyncController<C: NtpClock, PeerID: Hash + Eq + Copy + Debug> {
synchronization_config: SynchronizationConfig,
peer_defaults_config: SourceDefaultsConfig,
algorithm_config: Self::AlgorithmConfig,
) -> Self;
) -> Result<Self, C::Error>;
/// Update used system config
fn update_config(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion ntp-proto/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
/// This needs to be a trait as a single system can have multiple clocks
/// which need different implementation for steering and/or now.
pub trait NtpClock: Clone + Send + 'static {
type Error: std::error::Error;
type Error: std::error::Error + Send + Sync;

// Get current time
fn now(&self) -> Result<NtpTimestamp, Self::Error>;
Expand Down
7 changes: 5 additions & 2 deletions ntpd/bin/ntp-daemon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![forbid(unsafe_code)]

use std::process;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
ntpd::daemon_main().await
async fn main() {
let result = ntpd::daemon_main().await;
process::exit(if result.is_ok() { 0 } else { 1 });
}
Loading

0 comments on commit 53a15d8

Please sign in to comment.