exec.c 1.8 KB

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