]> git.armaanb.net Git - opendoas.git/blobdiff - persist_timestamp.c
persist_timestamp: make tmpfs requirement optional and only available on linux
[opendoas.git] / persist_timestamp.c
index d69e7dab3773cc20089b049ee60c9869370c415f..3f2ee18fc0f0c06389581fb2df57ea17493cf407 100644 (file)
 #include "includes.h"
 
 #ifndef TIMESTAMP_DIR
-# define TIMESTAMP_DIR "/tmp/doas"
+#      define TIMESTAMP_DIR "/tmp/doas"
 #endif
-#ifndef TMPFS_MAGIC
-# define TMPFS_MAGIC 0x01021994
+
+#if defined(TIMESTAMP_TMPFS) && defined(__linux__)
+#      ifndef TMPFS_MAGIC
+#              define TMPFS_MAGIC 0x01021994
+#      endif
 #endif
 
 #define        timespecisset(tsp)              ((tsp)->tv_sec || (tsp)->tv_nsec)
@@ -147,46 +150,40 @@ check:
 
        if ((st.st_mode & S_IFMT) != S_IFDIR)
                errx(1, "timestamp directory is not a directory");
-       if ((st.st_mode & (S_IWGRP|S_IRGRP|S_IWOTH|S_IROTH)) != 0)
+       if ((st.st_mode & (S_IWGRP|S_IRGRP|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH)) != 0)
                errx(1, "timestamp directory permissions wrong");
        if (st.st_uid != 0 || st.st_gid != 0)
                errx(1, "timestamp directory is not owned by root");
        if (statfs(dir, &sf) == -1)
                err(1, "statfs");
+
+#if defined(TIMESTAMP_TMPFS) && defined(__linux__)
        if (sf.f_type != TMPFS_MAGIC)
                errx(1, "timestamp directory not on tmpfs");
+#endif
 
        free(buf);
        return 0;
 }
 
-static int
-timestamp_read(int fd, struct timespec *mono, struct timespec *real)
-{
-       if (read(fd, (void *)mono, sizeof *mono) != sizeof *mono ||
-           read(fd, (void *)real, sizeof *real) != sizeof *mono)
-               err(1, "read");
-       if (!timespecisset(mono) || !timespecisset(real))
-               errx(1, "timespecisset");
-       return 0;
-}
-
 int
 persist_check(int fd, int secs)
 {
        struct timespec mono, real, ts_mono, ts_real, timeout;
 
-       if (timestamp_read(fd, &ts_mono, &ts_real) != 0)
-               return 1;
+       if (read(fd, &ts_mono, sizeof ts_mono) != sizeof ts_mono ||
+           read(fd, &ts_real, sizeof ts_real) != sizeof ts_mono)
+               err(1, "read");
+       if (!timespecisset(&ts_mono) || !timespecisset(&ts_real))
+               errx(1, "timespecisset");
 
-       if (clock_gettime(CLOCK_MONOTONIC, &mono) != 0 || !timespecisset(&mono))
-               err(1, "clock_gettime(CLOCK_MONOTONIC, ?)");
-       if (clock_gettime(CLOCK_REALTIME, &real) != 0 || !timespecisset(&real))
-               err(1, "clock_gettime(CLOCK_REALTIME, ?)");
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono) == -1 ||
+           clock_gettime(CLOCK_REALTIME, &real) == -1)
+               err(1, "clock_gettime");
 
        if (timespeccmp(&mono, &ts_mono, >) ||
            timespeccmp(&real, &ts_real, >))
-               return 1;
+               return -1;
 
        memset(&timeout, 0, sizeof timeout);
        timeout.tv_sec = secs;
@@ -205,10 +202,9 @@ persist_set(int fd, int secs)
 {
        struct timespec mono, real, ts_mono, ts_real, timeout;
 
-       if (clock_gettime(CLOCK_MONOTONIC, &mono) != 0 || !timespecisset(&mono))
-               err(1, "clock_gettime(XLOCK_MONOTONIC, ?)");
-       if (clock_gettime(CLOCK_REALTIME, &real) != 0 || !timespecisset(&real))
-               err(1, "clock_gettime(CLOCK_REALTIME, ?)");
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono) == -1 ||
+           clock_gettime(CLOCK_REALTIME, &real) == -1)
+               err(1, "clock_gettime");
 
        memset(&timeout, 0, sizeof timeout);
        timeout.tv_sec = secs;