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