mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:37:35 +00:00
ntpquery: Add a -s flag to make it adjust the time
This commit is contained in:
parent
0fff4e11a6
commit
f1a6884a51
1 changed files with 20 additions and 2 deletions
|
@ -100,24 +100,33 @@ static String format_ntp_timestamp(NtpTimestamp ntp_timestamp)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (pledge("stdio inet dns", nullptr) < 0) {
|
if (pledge("stdio inet dns settime", nullptr) < 0) {
|
||||||
perror("pledge");
|
perror("pledge");
|
||||||
return 1;
|
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.
|
// 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";
|
const char* host = "time.google.com";
|
||||||
Core::ArgsParser args_parser;
|
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.add_positional_argument(host, "NTP server", "host", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (!set_time) {
|
||||||
|
if (pledge("stdio inet dns", nullptr) < 0) {
|
||||||
|
perror("pledge");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto* hostent = gethostbyname(host);
|
auto* hostent = gethostbyname(host);
|
||||||
if (!hostent) {
|
if (!hostent) {
|
||||||
fprintf(stderr, "Lookup failed for '%s'\n", host);
|
fprintf(stderr, "Lookup failed for '%s'\n", host);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pledge("stdio inet", nullptr) < 0) {
|
if (pledge(set_time ? "stdio inet settime" : "stdio inet", nullptr) < 0) {
|
||||||
perror("pledge");
|
perror("pledge");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -180,6 +189,15 @@ int main(int argc, char** argv)
|
||||||
NtpTimestamp transmit_timestamp = be64toh(packet.transmit_timestamp);
|
NtpTimestamp transmit_timestamp = be64toh(packet.transmit_timestamp);
|
||||||
NtpTimestamp destination_timestamp = ntp_timestamp_from_timeval(t);
|
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("NTP response from %s:\n", inet_ntoa(peer_address.sin_addr));
|
||||||
printf("Leap Information: %d\n", packet.li_vn_mode >> 6);
|
printf("Leap Information: %d\n", packet.li_vn_mode >> 6);
|
||||||
printf("Version Number: %d\n", (packet.li_vn_mode >> 3) & 7);
|
printf("Version Number: %d\n", (packet.li_vn_mode >> 3) & 7);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue