summaryrefslogtreecommitdiff
path: root/Userland/ntpquery.cpp
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-11-10 11:28:26 -0500
committerAndreas Kling <kling@serenityos.org>2020-11-10 19:03:08 +0100
commit8d9d3c94254b403784fbe126b227bcb69632bbfb (patch)
tree1a49b22fbe107b0a3c385e9385b4a20288bfc543 /Userland/ntpquery.cpp
parent5fcd34b81097a3e652a19c8e2b996efae08c3e0d (diff)
downloadserenity-8d9d3c94254b403784fbe126b227bcb69632bbfb.zip
ntpquery: Add a '-a' flag that makes it use adjtime
With this, `ntpquery` can adjust the system time without making it jump. A fun activity with this in: 0. Boot 1. Run `su` 2. Run `ntpquery -a` to adjust the time offset after boot (usually around a second) 3. Keep running `ntpquery ; adjtime` to see how the offset behind NTP and the remaining adjtime both shrink. adjtime adjustment is large enough to make the time offset go down by a bit, but we currently lose time quickly enough that by the time adjtime is done, we've only corrected the clock about halfway, and not all the way to zero. Goto 2. So this isn't all that great yet, but I think it's good enough to think about turning this into a permanently running service next.
Diffstat (limited to 'Userland/ntpquery.cpp')
-rw-r--r--Userland/ntpquery.cpp28
1 files changed, 25 insertions, 3 deletions
diff --git a/Userland/ntpquery.cpp b/Userland/ntpquery.cpp
index 1d8b94a0dc..681da67785 100644
--- a/Userland/ntpquery.cpp
+++ b/Userland/ntpquery.cpp
@@ -113,6 +113,7 @@ int main(int argc, char** argv)
return 1;
}
+ bool adjust_time = false;
bool set_time = false;
bool verbose = false;
// FIXME: Change to serenityos.pool.ntp.org once https://manage.ntppool.org/manage/vendor/zone?a=km5a8h&id=vz-14154g is approved.
@@ -127,12 +128,18 @@ int main(int argc, char** argv)
// - time.google.com , https://developers.google.com/time/smear , linear-smears over 24 hours
const char* host = "time.google.com";
Core::ArgsParser args_parser;
- args_parser.add_option(set_time, "Adjust system time (requires root)", "set", 's');
+ args_parser.add_option(adjust_time, "Gradually adjust system time (requires root)", "adjust", 'a');
+ args_parser.add_option(set_time, "Immediately set system time (requires root)", "set", 's');
args_parser.add_option(verbose, "Verbose output", "verbose", 'v');
args_parser.add_positional_argument(host, "NTP server", "host", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
- if (!set_time) {
+ if (adjust_time && set_time) {
+ fprintf(stderr, "-a and -s are mutually exclusive\n");
+ return 1;
+ }
+
+ if (!adjust_time && !set_time) {
if (pledge("stdio inet dns", nullptr) < 0) {
perror("pledge");
return 1;
@@ -145,7 +152,7 @@ int main(int argc, char** argv)
return 1;
}
- if (pledge(set_time ? "stdio inet settime" : "stdio inet", nullptr) < 0) {
+ if (pledge((adjust_time || set_time) ? "stdio inet settime" : "stdio inet", nullptr) < 0) {
perror("pledge");
return 1;
}
@@ -311,4 +318,19 @@ int main(int argc, char** argv)
if (verbose)
printf("Delay: %f\n", delay_s);
printf("Offset: %f\n", offset_s);
+
+ if (adjust_time) {
+ long delta_us = static_cast<long>(round(offset_s * 1'000'000));
+ timeval delta_timeval;
+ delta_timeval.tv_sec = delta_us / 1'000'000;
+ delta_timeval.tv_usec = delta_us % 1'000'000;
+ if (delta_timeval.tv_usec < 0) {
+ delta_timeval.tv_sec--;
+ delta_timeval.tv_usec += 1'000'000;
+ }
+ if (adjtime(&delta_timeval, nullptr) < 0) {
+ perror("adjtime set");
+ return 1;
+ }
+ }
}