diff options
Diffstat (limited to 'userland/minibox/utilities/rdate.c')
-rw-r--r-- | userland/minibox/utilities/rdate.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/userland/minibox/utilities/rdate.c b/userland/minibox/utilities/rdate.c index 54a2746..bd03b0b 100644 --- a/userland/minibox/utilities/rdate.c +++ b/userland/minibox/utilities/rdate.c @@ -8,7 +8,7 @@ int rdate_main(int argc, char **argv) { int fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { + if (-1 == fd) { perror("socket"); return 1; } @@ -23,26 +23,37 @@ int rdate_main(int argc, char **argv) { hints.ai_next = NULL; int rc = getaddrinfo("time-d-g.nist.gov", "37", &hints, &result); - if (-1 == rc) { - fprintf(stderr, "Error in getaddrinfo()\n"); + if (0 != rc) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc)); return 1; } - if (connect(fd, (struct sockaddr *)result->ai_addr, result->ai_addrlen) < 0) { + if (-1 == + connect(fd, (struct sockaddr *)result->ai_addr, result->ai_addrlen)) { perror("connect"); return 1; } uint32_t t; - read(fd, &t, sizeof(t)); - t = ntohl(t); + if (-1 == (rc = read(fd, &t, sizeof(t)))) { + perror("read"); + return 1; + } close(fd); + if (sizeof(t) != rc) { + fprintf(stderr, "Invalid message recieved from time server.\n"); + return 1; + } + + t = ntohl(t); + int64_t unix_time = (t - 2208988800) * 1000; int clock_fd = open("/dev/clock", O_RDWR); int64_t current; dprintf(clock_fd, "%lld", unix_time); close(clock_fd); + freeaddrinfo(result); return 0; } |