aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2020-09-04 21:51:38 +0200
committerMattias Andrée <maandree@kth.se>2020-09-04 21:51:38 +0200
commit8c85de545f2fc998accd7d6f88bebf234c728ce5 (patch)
tree93866e6ab9e378dd96d7cb063f5a84096e5224cc
parentUse sigaction instead of signal and siginterrupt (diff)
downloadmongotimer-8c85de545f2fc998accd7d6f88bebf234c728ce5.tar.gz
mongotimer-8c85de545f2fc998accd7d6f88bebf234c728ce5.tar.bz2
mongotimer-8c85de545f2fc998accd7d6f88bebf234c728ce5.tar.xz
Add leap second support
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--config.mk2
-rw-r--r--mongoclock.c25
2 files changed, 25 insertions, 2 deletions
diff --git a/config.mk b/config.mk
index 9e148ca..0911b00 100644
--- a/config.mk
+++ b/config.mk
@@ -2,5 +2,5 @@ PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man
CFLAGS = -std=c99 -Wall -Wextra -O2
-CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
+CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -DUSE_ADJTIMEX
LDFLAGS = -s
diff --git a/mongoclock.c b/mongoclock.c
index cf34e6d..442a380 100644
--- a/mongoclock.c
+++ b/mongoclock.c
@@ -9,6 +9,9 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
+#ifdef USE_ADJTIMEX
+# include <sys/timex.h>
+#endif
#include "arg.h"
@@ -80,7 +83,6 @@ print_time(const char ***str, size_t y, size_t x)
int
main(int argc, char *argv[])
{
- time_t now_;
struct tm *now;
const char **digits[9];
size_t x = 0, y = 0;
@@ -90,6 +92,12 @@ main(int argc, char *argv[])
struct itimerspec itimerspec;
uint64_t _overrun;
struct sigaction sigact;
+#ifdef USE_ADJTIMEX
+ struct timex timex;
+ int r;
+#else
+ time_t now_;
+#endif
ARGBEGIN {
default:
@@ -123,6 +131,10 @@ main(int argc, char *argv[])
sigact.sa_handler = sigwinch;
sigaction(SIGWINCH, &sigact, NULL);
+#ifdef USE_ADJTIMEX
+ memset(&timex, 0, sizeof(timex));
+#endif
+
while (!caught_sigterm) {
if (caught_sigwinch) {
if (ioctl(STDOUT_FILENO, (unsigned long)TIOCGWINSZ, &winsize) < 0) {
@@ -152,12 +164,23 @@ main(int argc, char *argv[])
continue;
}
+#ifdef USE_ADJTIMEX
+ r = adjtimex(&timex);
+ if (r == -1)
+ goto fail;
+ now = localtime(&timex.time.tv_sec);
+ if (now == NULL)
+ goto fail;
+ if (r == TIME_OOP)
+ now->tm_sec += 1;
+#else
now_ = time(NULL);
if (now_ == -1)
goto fail;
now = localtime(&now_);
if (now == NULL)
goto fail;
+#endif
digits[0] = mongo_ds[now->tm_hour / 10];
digits[1] = mongo_ds[now->tm_hour % 10];