vfork_exec.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <pthread.h>
  16. #include "log.h"
  17. #include "timens.h"
  18. #define OFFSET (36000)
  19. struct thread_args {
  20. char *tst_name;
  21. struct timespec *now;
  22. };
  23. static void *tcheck(void *_args)
  24. {
  25. struct thread_args *args = _args;
  26. struct timespec *now = args->now, tst;
  27. int i;
  28. for (i = 0; i < 2; i++) {
  29. _gettime(CLOCK_MONOTONIC, &tst, i);
  30. if (labs(tst.tv_sec - now->tv_sec) > 5) {
  31. pr_fail("%s: in-thread: unexpected value: %ld (%ld)\n",
  32. args->tst_name, tst.tv_sec, now->tv_sec);
  33. return (void *)1UL;
  34. }
  35. }
  36. return NULL;
  37. }
  38. static int check_in_thread(char *tst_name, struct timespec *now)
  39. {
  40. struct thread_args args = {
  41. .tst_name = tst_name,
  42. .now = now,
  43. };
  44. pthread_t th;
  45. void *retval;
  46. if (pthread_create(&th, NULL, tcheck, &args))
  47. return pr_perror("thread");
  48. if (pthread_join(th, &retval))
  49. return pr_perror("pthread_join");
  50. return !(retval == NULL);
  51. }
  52. static int check(char *tst_name, struct timespec *now)
  53. {
  54. struct timespec tst;
  55. int i;
  56. for (i = 0; i < 2; i++) {
  57. _gettime(CLOCK_MONOTONIC, &tst, i);
  58. if (labs(tst.tv_sec - now->tv_sec) > 5)
  59. return pr_fail("%s: unexpected value: %ld (%ld)\n",
  60. tst_name, tst.tv_sec, now->tv_sec);
  61. }
  62. if (check_in_thread(tst_name, now))
  63. return 1;
  64. ksft_test_result_pass("%s\n", tst_name);
  65. return 0;
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69. struct timespec now;
  70. int status;
  71. pid_t pid;
  72. if (argc > 1) {
  73. char *endptr;
  74. ksft_cnt.ksft_pass = 1;
  75. now.tv_sec = strtoul(argv[1], &endptr, 0);
  76. if (*endptr != 0)
  77. return pr_perror("strtoul");
  78. return check("child after exec", &now);
  79. }
  80. ksft_print_header();
  81. nscheck();
  82. ksft_set_plan(4);
  83. clock_gettime(CLOCK_MONOTONIC, &now);
  84. if (unshare_timens())
  85. return 1;
  86. if (_settime(CLOCK_MONOTONIC, OFFSET))
  87. return 1;
  88. if (check("parent before vfork", &now))
  89. return 1;
  90. pid = vfork();
  91. if (pid < 0)
  92. return pr_perror("fork");
  93. if (pid == 0) {
  94. char now_str[64];
  95. char *cargv[] = {"exec", now_str, NULL};
  96. char *cenv[] = {NULL};
  97. /* Check for proper vvar offsets after execve. */
  98. snprintf(now_str, sizeof(now_str), "%ld", now.tv_sec + OFFSET);
  99. execve("/proc/self/exe", cargv, cenv);
  100. pr_perror("execve");
  101. _exit(1);
  102. }
  103. if (waitpid(pid, &status, 0) != pid)
  104. return pr_perror("waitpid");
  105. if (status)
  106. ksft_exit_fail();
  107. ksft_inc_pass_cnt();
  108. ksft_test_result_pass("wait for child\n");
  109. /* Check that we are still in the source timens. */
  110. if (check("parent after vfork", &now))
  111. return 1;
  112. ksft_exit_pass();
  113. return 0;
  114. }