^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "timens.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define OFFSET (36000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct timespec now, tst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int status, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (argc > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (sscanf(argv[1], "%ld", &now.tv_sec) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return pr_perror("sscanf");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) _gettime(CLOCK_MONOTONIC, &tst, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (abs(tst.tv_sec - now.tv_sec) > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) nscheck();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ksft_set_plan(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) clock_gettime(CLOCK_MONOTONIC, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (unshare_timens())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (_settime(CLOCK_MONOTONIC, OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) _gettime(CLOCK_MONOTONIC, &tst, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (abs(tst.tv_sec - now.tv_sec) > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return pr_fail("%ld %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) now.tv_sec, tst.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (argc > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (pid < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return pr_perror("fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (pid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) char now_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) char *cargv[] = {"exec", now_str, NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *cenv[] = {NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Check that a child process is in the new timens. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) _gettime(CLOCK_MONOTONIC, &tst, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return pr_fail("%ld %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) now.tv_sec + OFFSET, tst.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Check for proper vvar offsets after execve. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) snprintf(now_str, sizeof(now_str), "%ld", now.tv_sec + OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) execve("/proc/self/exe", cargv, cenv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return pr_perror("execve");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (waitpid(pid, &status, 0) != pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return pr_perror("waitpid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ksft_test_result_pass("exec\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }