Make nanosleep regress actually test something.
authorbluhm <bluhm@openbsd.org>
Thu, 29 Feb 2024 21:47:02 +0000 (21:47 +0000)
committerbluhm <bluhm@openbsd.org>
Thu, 29 Feb 2024 21:47:02 +0000 (21:47 +0000)
Do not call exit 0 in parent and child process.  This skiped the
testing in multiple subtests.  Use meaningful variable names to
figure out what is going wrong after the tests have been enabled.
Fix the test logic.  Add missing break in switch statement, so that
success is reported.

regress/sys/kern/nanosleep/Makefile
regress/sys/kern/nanosleep/nanosleep.c

index 919e33e..05b75a6 100644 (file)
@@ -1,7 +1,8 @@
-#      $OpenBSD: Makefile,v 1.4 2018/05/22 18:33:41 cheloha Exp $
+#      $OpenBSD: Makefile,v 1.5 2024/02/29 21:47:02 bluhm Exp $
 
 PROG=  nanosleep
 SRCS=  nanosleep.c
+WARNINGS=      yes
 
 trivial: nanosleep
        ./nanosleep -t
index 790707a..95bfa79 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: nanosleep.c,v 1.8 2024/02/29 18:17:41 bluhm Exp $     */
+/*     $OpenBSD: nanosleep.c,v 1.9 2024/02/29 21:47:02 bluhm Exp $     */
 /*
  *     Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
  */
@@ -50,6 +50,7 @@ main(int argc, char **argv)
                        break;
                case 'S':
                        ret |= short_time();
+                       break;
                default:
                        fprintf(stderr, "Usage: nanosleep [-itseSE]\n");
                        exit(1);
@@ -67,14 +68,14 @@ sighandler(int signum)
 int
 trivial(void)
 {
-       struct timespec ts, rts;
-
-       ts.tv_sec = 0;
-       ts.tv_nsec = 30000000;
-       rts.tv_sec = 4711;      /* Just add to the confusion */
-       rts.tv_nsec = 4711;
-       if (nanosleep(&ts, &rts) < 0) {
-               warn("trivial: nanosleep");
+       struct timespec timeout, remainder;
+
+       timeout.tv_sec = 0;
+       timeout.tv_nsec = 30000000;
+       remainder.tv_sec = 4711;        /* Just add to the confusion */
+       remainder.tv_nsec = 4711;
+       if (nanosleep(&timeout, &remainder) < 0) {
+               warn("%s: nanosleep", __func__);
                return 1;
        }
 
@@ -83,9 +84,9 @@ trivial(void)
         * amount of time we want to sleep.
         * If we receive any signal, something is wrong anyway.
         */
-       if (rts.tv_sec != 0 || rts.tv_nsec != 0) {
-               warnx("trivial: non-zero time? %lld/%ld", (long long)rts.tv_sec,
-                   rts.tv_nsec);
+       if (remainder.tv_sec != 0 || remainder.tv_nsec != 0) {
+               warnx("%s: non-zero time: %lld.%09ld", __func__,
+                   (long long)remainder.tv_sec, remainder.tv_nsec);
                return 1;
        }
 
@@ -95,7 +96,7 @@ trivial(void)
 int
 with_signal(void)
 {
-       struct timespec ts, rts;
+       struct timespec timeout, remainder;
        pid_t pid;
        int status;
 
@@ -106,29 +107,34 @@ with_signal(void)
        switch(fork()) {
        case -1:
                err(1, "fork");
-       default:
-               ts.tv_sec = 1;
-               ts.tv_nsec = 0;
-               nanosleep(&ts, NULL);
+       case 0:
+               timeout.tv_sec = 1;
+               timeout.tv_nsec = 0;
+               nanosleep(&timeout, NULL);
                kill(pid, SIGUSR1);
-               exit(0);
+               _exit(0);
+       default:
+               break;
        }
 
-       ts.tv_sec = 10;
-       ts.tv_nsec = 0;
-       rts.tv_sec = 0;
-       rts.tv_nsec = 0;
-       if (nanosleep(&ts, &rts) == 0) {
-               warnx("with-signal: nanosleep");
+       timeout.tv_sec = 10;
+       timeout.tv_nsec = 0;
+       remainder.tv_sec = 0;
+       remainder.tv_nsec = 0;
+       if (nanosleep(&timeout, &remainder) == 0) {
+               warnx("%s: nanosleep", __func__);
                return 1;
        }
-       if (rts.tv_sec == 0 && rts.tv_nsec == 0) {
-               warnx("with-signal: zero time");
+
+       if (remainder.tv_sec == 0 && remainder.tv_nsec == 0) {
+               warnx("%s: zero time", __func__);
                return 1;
        }
 
        if (wait(&status) < 0)
                err(1, "wait");
+       if (status != 0)
+               errx(1, "status");
 
        return 0;
 }
@@ -136,31 +142,32 @@ with_signal(void)
 int
 time_elapsed(void)
 {
-       struct timespec ts;
-       struct timespec stv, etv;
+       struct timespec timeout;
+       struct timespec start, end, duration;
 
-       ts.tv_sec = 0;
-       ts.tv_nsec = 500000000;
+       timeout.tv_sec = 0;
+       timeout.tv_nsec = 500000000;
 
-       if (clock_gettime(CLOCK_MONOTONIC, &stv) < 0) {
-               warn("clock_gettime");
+       if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
+               warn("%s: clock_gettime", __func__);
                return 1;
        }
 
-       if (nanosleep(&ts, NULL) < 0) {
-               warn("nanosleep");
+       if (nanosleep(&timeout, NULL) < 0) {
+               warn("%s: nanosleep", __func__);
                return 1;
        }
 
-       if (clock_gettime(CLOCK_MONOTONIC, &etv) < 0) {
-               warn("clock_gettime");
+       if (clock_gettime(CLOCK_MONOTONIC, &end) < 0) {
+               warn("%s: clock_gettime", __func__);
                return 1;
        }
 
-       timespecsub(&etv, &stv, &stv);
+       timespecsub(&end, &start, &duration);
 
-       if (stv.tv_sec == 0 && stv.tv_nsec < 500000000) {
-               warnx("slept less than 0.5 sec");
+       if (duration.tv_sec == 0 && duration.tv_nsec < 500000000) {
+               warnx("%s: slept less than 0.5 sec: %lld.%09ld", __func__,
+                   (long long)duration.tv_sec, duration.tv_nsec);
                return 1;
        }
 
@@ -170,8 +177,8 @@ time_elapsed(void)
 int
 time_elapsed_with_signal(void)
 {
-       struct timespec ts, rts;
-       struct timespec stv, etv;
+       struct timespec timeout, remainder;
+       struct timespec start, end, duration;
        pid_t pid;
        int status;
 
@@ -182,49 +189,52 @@ time_elapsed_with_signal(void)
        switch(fork()) {
        case -1:
                err(1, "fork");
-       default:
-               ts.tv_sec = 1;
-               ts.tv_nsec = 0;
-               nanosleep(&ts, NULL);
+       case 0:
+               timeout.tv_sec = 1;
+               timeout.tv_nsec = 0;
+               nanosleep(&timeout, NULL);
                kill(pid, SIGUSR1);
-               exit(0);
+               _exit(0);
+       default:
+               break;
        }
 
-       ts.tv_sec = 10;
-       ts.tv_nsec = 0;
-       rts.tv_sec = 0;
-       rts.tv_nsec = 0;
-
-       if (clock_gettime(CLOCK_MONOTONIC, &stv) < 0) {
-               warn("clock_gettime");
+       if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
+               warn("%s: clock_gettime", __func__);
                return 1;
        }
 
-       if (nanosleep(&ts, &rts) == 0) {
-               warnx("nanosleep");
+       timeout.tv_sec = 10;
+       timeout.tv_nsec = 0;
+       remainder.tv_sec = 0;
+       remainder.tv_nsec = 0;
+       if (nanosleep(&timeout, &remainder) == 0) {
+               warnx("%s: nanosleep", __func__);
                return 1;
        }
 
-       if (clock_gettime(CLOCK_MONOTONIC, &etv) < 0) {
-               warn("clock_gettime");
+       if (clock_gettime(CLOCK_MONOTONIC, &end) < 0) {
+               warn("%s: clock_gettime", __func__);
                return 1;
        }
 
-       timespecsub(&etv, &stv, &stv);
-
-       etv.tv_sec = rts.tv_sec;
-       etv.tv_nsec = rts.tv_nsec;
-
-       timespecadd(&etv, &stv, &stv);
+       timespecsub(&end, &start, &duration);
+       timespecadd(&duration, &remainder, &timeout);
+       /* XXX remainder may be one tick too small */
+       remainder.tv_sec = 0;
+       remainder.tv_nsec = 10000000;
+       timespecadd(&timeout, &remainder, &timeout);
 
-       if (stv.tv_sec < 10) {
-               warnx("slept time + leftover time < 10 sec");
+       if (timeout.tv_sec < 10) {
+               warnx("%s: slept time + leftover time < 10 sec: %lld.%09ld",
+                   __func__, (long long)timeout.tv_sec, timeout.tv_nsec);
                return 1;
        }
 
-
        if (wait(&status) < 0)
                err(1, "wait");
+       if (status != 0)
+               errx(1, "status");
 
        return 0;
 }
@@ -232,7 +242,7 @@ time_elapsed_with_signal(void)
 int
 short_time(void)
 {
-       struct timespec ts, rts;
+       struct timespec timeout;
        pid_t pid;
        int status;
 
@@ -243,24 +253,28 @@ short_time(void)
        switch(fork()) {
        case -1:
                err(1, "fork");
-       default:
+       case 0:
                /* Sleep two seconds, then shoot parent. */
-               ts.tv_sec = 2;
-               ts.tv_nsec = 0;
-               nanosleep(&ts, NULL);
+               timeout.tv_sec = 2;
+               timeout.tv_nsec = 0;
+               nanosleep(&timeout, NULL);
                kill(pid, SIGUSR1);
-               exit(0);
+               _exit(0);
+       default:
+               break;
        }
 
-       ts.tv_sec = 0;
-       ts.tv_nsec = 1;
-       if (nanosleep(&ts, NULL) <= 0) {
-               warn("short_time: nanosleep");
+       timeout.tv_sec = 0;
+       timeout.tv_nsec = 1;
+       if (nanosleep(&timeout, NULL) < 0) {
+               warn("%s: nanosleep", __func__);
                return 1;
        }
 
        if (wait(&status) < 0)
                err(1, "wait");
+       if (status != 0)
+               errx(1, "status");
 
        return 0;
 }
@@ -268,14 +282,14 @@ short_time(void)
 int
 invalid_time(void)
 {
-       struct timespec ts[3] = { {-1, 0}, {0, -1}, {0, 1000000000L} };
+       struct timespec timeout[3] = { {-1, 0}, {0, -1}, {0, 1000000000L} };
        int i, status;
 
        for (i = 0; i < 3; i++) {
-               status = nanosleep(&ts[i], NULL);
+               status = nanosleep(&timeout[i], NULL);
                if (status != -1 || errno != EINVAL) {
-                       warnx("invalid-time: nanosleep %lld %ld",
-                           (long long)ts[i].tv_sec, ts[i].tv_nsec);
+                       warnx("%s: nanosleep %lld %ld", __func__,
+                           (long long)timeout[i].tv_sec, timeout[i].tv_nsec);
                        return 1;
                }
        }