Skip to content

Commit 5c2e8b6

Browse files
nicoawesomekling
authored andcommitted
Lagom: Add ntpquery to lagom build
1 parent 81add73 commit 5c2e8b6

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Meta/Lagom/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ if (BUILD_LAGOM)
8686
target_link_libraries(js_lagom stdc++)
8787
target_link_libraries(js_lagom pthread)
8888

89+
add_executable(ntpquery_lagom ../../Userland/ntpquery.cpp)
90+
set_target_properties(ntpquery_lagom PROPERTIES OUTPUT_NAME ntpquery)
91+
target_link_libraries(ntpquery_lagom Lagom)
92+
8993
add_executable(test-js_lagom ../../Userland/test-js.cpp)
9094
set_target_properties(test-js_lagom PROPERTIES OUTPUT_NAME test-js)
9195
target_link_libraries(test-js_lagom Lagom)

Userland/ntpquery.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

27-
#define _GNU_SOURCE
27+
#define _BSD_SOURCE
28+
#define _DEFAULT_SOURCE
2829
#include <AK/Random.h>
2930
#include <LibCore/ArgsParser.h>
3031
#include <arpa/inet.h>
3132
#include <endian.h>
33+
#include <inttypes.h>
3234
#include <math.h>
3335
#include <netdb.h>
3436
#include <netinet/in.h>
@@ -37,6 +39,7 @@
3739
#include <sys/socket.h>
3840
#include <sys/time.h>
3941
#include <sys/uio.h>
42+
#include <time.h>
4043

4144
// An NtpTimestamp is a 64-bit integer that's a 32.32 binary-fixed point number.
4245
// The integral part in the upper 32 bits represents seconds since 1900-01-01.
@@ -99,7 +102,7 @@ static String format_ntp_timestamp(NtpTimestamp ntp_timestamp)
99102
gmtime_r(&t.tv_sec, &tm);
100103
size_t written = strftime(buffer, sizeof(buffer), "%Y-%m-%dT%T.", &tm);
101104
ASSERT(written == 20);
102-
written += snprintf(buffer + written, sizeof(buffer) - written, "%06d", t.tv_usec);
105+
written += snprintf(buffer + written, sizeof(buffer) - written, "%06d", (int)t.tv_usec);
103106
ASSERT(written == 26);
104107
buffer[written++] = 'Z';
105108
buffer[written] = '\0';
@@ -108,10 +111,12 @@ static String format_ntp_timestamp(NtpTimestamp ntp_timestamp)
108111

109112
int main(int argc, char** argv)
110113
{
114+
#ifdef __serenity__
111115
if (pledge("stdio inet dns settime", nullptr) < 0) {
112116
perror("pledge");
113117
return 1;
114118
}
119+
#endif
115120

116121
bool adjust_time = false;
117122
bool set_time = false;
@@ -139,24 +144,28 @@ int main(int argc, char** argv)
139144
return 1;
140145
}
141146

147+
#ifdef __serenity__
142148
if (!adjust_time && !set_time) {
143149
if (pledge("stdio inet dns", nullptr) < 0) {
144150
perror("pledge");
145151
return 1;
146152
}
147153
}
154+
#endif
148155

149156
auto* hostent = gethostbyname(host);
150157
if (!hostent) {
151158
fprintf(stderr, "Lookup failed for '%s'\n", host);
152159
return 1;
153160
}
154161

162+
#ifdef __serenity__
155163
if (pledge((adjust_time || set_time) ? "stdio inet settime" : "stdio inet", nullptr) < 0) {
156164
perror("pledge");
157165
return 1;
158166
}
159167
unveil(nullptr, nullptr);
168+
#endif
160169

161170
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
162171
if (fd < 0) {
@@ -243,7 +252,7 @@ int main(int argc, char** argv)
243252
return 1;
244253
}
245254
if (packet.origin_timestamp != random_transmit_timestamp) {
246-
fprintf(stderr, "expected %#016llx as origin timestamp, got %#016llx\n", random_transmit_timestamp, packet.origin_timestamp);
255+
fprintf(stderr, "expected %#016" PRIx64 " as origin timestamp, got %#016" PRIx64 "\n", random_transmit_timestamp, packet.origin_timestamp);
247256
return 1;
248257
}
249258
if (packet.transmit_timestamp == 0) {
@@ -286,15 +295,15 @@ int main(int argc, char** argv)
286295
}
287296
printf("\n");
288297

289-
printf("Reference timestamp: %#016llx (%s)\n", be64toh(packet.reference_timestamp), format_ntp_timestamp(be64toh(packet.reference_timestamp)).characters());
290-
printf("Origin timestamp: %#016llx (%s)\n", origin_timestamp, format_ntp_timestamp(origin_timestamp).characters());
291-
printf("Receive timestamp: %#016llx (%s)\n", receive_timestamp, format_ntp_timestamp(receive_timestamp).characters());
292-
printf("Transmit timestamp: %#016llx (%s)\n", transmit_timestamp, format_ntp_timestamp(transmit_timestamp).characters());
293-
printf("Destination timestamp: %#016llx (%s)\n", destination_timestamp, format_ntp_timestamp(destination_timestamp).characters());
298+
printf("Reference timestamp: %#016" PRIx64 " (%s)\n", be64toh(packet.reference_timestamp), format_ntp_timestamp(be64toh(packet.reference_timestamp)).characters());
299+
printf("Origin timestamp: %#016" PRIx64 " (%s)\n", origin_timestamp, format_ntp_timestamp(origin_timestamp).characters());
300+
printf("Receive timestamp: %#016" PRIx64 " (%s)\n", receive_timestamp, format_ntp_timestamp(receive_timestamp).characters());
301+
printf("Transmit timestamp: %#016" PRIx64 " (%s)\n", transmit_timestamp, format_ntp_timestamp(transmit_timestamp).characters());
302+
printf("Destination timestamp: %#016" PRIx64 " (%s)\n", destination_timestamp, format_ntp_timestamp(destination_timestamp).characters());
294303

295304
// When the system isn't under load, user-space t and packet_t are identical. If a shell with `yes` is running, it can be as high as 30ms in this program,
296305
// which gets user-space time immediately after the recvmsg() call. In programs that have an event loop reading from multiple sockets, it could be higher.
297-
printf("Receive latency: %lld.%06d s\n", kernel_to_userspace_latency.tv_sec, kernel_to_userspace_latency.tv_usec);
306+
printf("Receive latency: %" PRId64 ".%06d s\n", kernel_to_userspace_latency.tv_sec, (int)kernel_to_userspace_latency.tv_usec);
298307
}
299308

300309
// Parts of the "Clock Filter" computations, https://tools.ietf.org/html/rfc5905#section-10

0 commit comments

Comments
 (0)