24
24
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
25
*/
26
26
27
- #define _GNU_SOURCE
27
+ #define _BSD_SOURCE
28
+ #define _DEFAULT_SOURCE
28
29
#include < AK/Random.h>
29
30
#include < LibCore/ArgsParser.h>
30
31
#include < arpa/inet.h>
31
32
#include < endian.h>
33
+ #include < inttypes.h>
32
34
#include < math.h>
33
35
#include < netdb.h>
34
36
#include < netinet/in.h>
37
39
#include < sys/socket.h>
38
40
#include < sys/time.h>
39
41
#include < sys/uio.h>
42
+ #include < time.h>
40
43
41
44
// An NtpTimestamp is a 64-bit integer that's a 32.32 binary-fixed point number.
42
45
// 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)
99
102
gmtime_r (&t.tv_sec , &tm);
100
103
size_t written = strftime (buffer, sizeof (buffer), " %Y-%m-%dT%T." , &tm);
101
104
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 );
103
106
ASSERT (written == 26 );
104
107
buffer[written++] = ' Z' ;
105
108
buffer[written] = ' \0 ' ;
@@ -108,10 +111,12 @@ static String format_ntp_timestamp(NtpTimestamp ntp_timestamp)
108
111
109
112
int main (int argc, char ** argv)
110
113
{
114
+ #ifdef __serenity__
111
115
if (pledge (" stdio inet dns settime" , nullptr ) < 0 ) {
112
116
perror (" pledge" );
113
117
return 1 ;
114
118
}
119
+ #endif
115
120
116
121
bool adjust_time = false ;
117
122
bool set_time = false ;
@@ -139,24 +144,28 @@ int main(int argc, char** argv)
139
144
return 1 ;
140
145
}
141
146
147
+ #ifdef __serenity__
142
148
if (!adjust_time && !set_time) {
143
149
if (pledge (" stdio inet dns" , nullptr ) < 0 ) {
144
150
perror (" pledge" );
145
151
return 1 ;
146
152
}
147
153
}
154
+ #endif
148
155
149
156
auto * hostent = gethostbyname (host);
150
157
if (!hostent) {
151
158
fprintf (stderr, " Lookup failed for '%s'\n " , host);
152
159
return 1 ;
153
160
}
154
161
162
+ #ifdef __serenity__
155
163
if (pledge ((adjust_time || set_time) ? " stdio inet settime" : " stdio inet" , nullptr ) < 0 ) {
156
164
perror (" pledge" );
157
165
return 1 ;
158
166
}
159
167
unveil (nullptr , nullptr );
168
+ #endif
160
169
161
170
int fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
162
171
if (fd < 0 ) {
@@ -243,7 +252,7 @@ int main(int argc, char** argv)
243
252
return 1 ;
244
253
}
245
254
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 );
247
256
return 1 ;
248
257
}
249
258
if (packet.transmit_timestamp == 0 ) {
@@ -286,15 +295,15 @@ int main(int argc, char** argv)
286
295
}
287
296
printf (" \n " );
288
297
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 ());
294
303
295
304
// 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,
296
305
// 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 );
298
307
}
299
308
300
309
// Parts of the "Clock Filter" computations, https://tools.ietf.org/html/rfc5905#section-10
0 commit comments