diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2020-12-07 19:54:24 +0100 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2020-12-07 19:55:28 +0100 |
commit | 83d829c2da62214295a2a346d73d50430cd7b492 (patch) | |
tree | b91b65635477fb2f6c1e7432e3de1223b752ecff | |
parent | c3d6a9acf1b1420965e982d743307f8a424a4ec3 (diff) | |
download | ntpd-setwait-83d829c2da62214295a2a346d73d50430cd7b492.tar.gz ntpd-setwait-83d829c2da62214295a2a346d73d50430cd7b492.tar.bz2 ntpd-setwait-83d829c2da62214295a2a346d73d50430cd7b492.zip |
main.c: limit logs to not fill up disk in case of errors
Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r-- | main.c | 37 |
1 files changed, 30 insertions, 7 deletions
@@ -71,6 +71,30 @@ ========================================================================== */ +void error +( + const char *msg /* message to print */ +) +{ + time_t now; /* current time */ + static time_t last_log; /* last time when message was printed */ + static const char *last_msg; /* last log that was printed */ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + now = time(NULL); + + /* if we are printing same message twice, + * and 60 seconds did not pass from last + * print, then do not print this log */ + if (msg == last_msg && (now - last_log) < 10) + return; + + perror(msg); + last_msg = msg; + last_log = now; + return; +} + /* ========================================================================== Reads current timestamp from random ntp server. As a source, we use time at which ntp packet left server to us. @@ -137,8 +161,7 @@ static int get_ts_from_ntp */ errcnt = 60; - fprintf(stderr, "w/getaddrinfo(%s, 123): %s\n", host, - strerror(errno)); + error("w/getaddrinfo()"); } return -1; @@ -171,7 +194,7 @@ static int get_ts_from_ntp * create socket. */ - perror("w/no available address found"); + error("w/no available address found"); freeaddrinfo(res); return -1; } @@ -187,7 +210,7 @@ static int get_ts_from_ntp /* couldn't send whole packet */ - perror("w/sendto() ntp request"); + error("w/sendto() ntp request"); freeaddrinfo(res); close(fd); return -1; @@ -215,7 +238,7 @@ static int get_ts_from_ntp /* select() failed in a bad way */ - perror("w/select()"); + error("w/select()"); freeaddrinfo(res); close(fd); return -1; @@ -241,7 +264,7 @@ static int get_ts_from_ntp /* couldn't receive whole packet */ - perror("w/read() ntp response"); + error("w/read() ntp response"); freeaddrinfo(res); close(fd); return -1; @@ -442,7 +465,7 @@ int main /* couldn't set the time, go back to start */ - perror("w/settimeofday()"); + error("w/settimeofday()"); continue; } |