From f1a6884a516a73bdba9653914aa5cc93f5280e56 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 6 Sep 2020 10:02:45 -0400 Subject: [PATCH] ntpquery: Add a -s flag to make it adjust the time --- Userland/ntpquery.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Userland/ntpquery.cpp b/Userland/ntpquery.cpp index 58a8ab2826..e87a7a2109 100644 --- a/Userland/ntpquery.cpp +++ b/Userland/ntpquery.cpp @@ -100,24 +100,33 @@ static String format_ntp_timestamp(NtpTimestamp ntp_timestamp) int main(int argc, char** argv) { - if (pledge("stdio inet dns", nullptr) < 0) { + if (pledge("stdio inet dns settime", nullptr) < 0) { perror("pledge"); return 1; } + bool set_time = false; // FIXME: Change to serenityos.pool.ntp.org once https://manage.ntppool.org/manage/vendor/zone?a=km5a8h&id=vz-14154g is approved. 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_positional_argument(host, "NTP server", "host", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); + if (!set_time) { + if (pledge("stdio inet dns", nullptr) < 0) { + perror("pledge"); + return 1; + } + } + auto* hostent = gethostbyname(host); if (!hostent) { fprintf(stderr, "Lookup failed for '%s'\n", host); return 1; } - if (pledge("stdio inet", nullptr) < 0) { + if (pledge(set_time ? "stdio inet settime" : "stdio inet", nullptr) < 0) { perror("pledge"); return 1; } @@ -180,6 +189,15 @@ int main(int argc, char** argv) NtpTimestamp transmit_timestamp = be64toh(packet.transmit_timestamp); NtpTimestamp destination_timestamp = ntp_timestamp_from_timeval(t); + if (set_time) { + // FIXME: Do all the time filtering described in 5905, or at least correct for time of flight. + timeval t = timeval_from_ntp_timestamp(transmit_timestamp); + if (settimeofday(&t, nullptr) < 0) { + perror("settimeofday"); + return 1; + } + } + printf("NTP response from %s:\n", inet_ntoa(peer_address.sin_addr)); printf("Leap Information: %d\n", packet.li_vn_mode >> 6); printf("Version Number: %d\n", (packet.li_vn_mode >> 3) & 7);