exec.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (labs(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. ksft_print_header();
  34. nscheck();
  35. ksft_set_plan(1);
  36. clock_gettime(CLOCK_MONOTONIC, &now);
  37. if (unshare_timens())
  38. return 1;
  39. if (_settime(CLOCK_MONOTONIC, OFFSET))
  40. return 1;
  41. for (i = 0; i < 2; i++) {
  42. _gettime(CLOCK_MONOTONIC, &tst, i);
  43. if (labs(tst.tv_sec - now.tv_sec) > 5)
  44. return pr_fail("%ld %ld\n",
  45. now.tv_sec, tst.tv_sec);
  46. }
  47. if (argc > 1)
  48. return 0;
  49. pid = fork();
  50. if (pid < 0)
  51. return pr_perror("fork");
  52. if (pid == 0) {
  53. char now_str[64];
  54. char *cargv[] = {"exec", now_str, NULL};
  55. char *cenv[] = {NULL};
  56. /* Check that a child process is in the new timens. */
  57. for (i = 0; i < 2; i++) {
  58. _gettime(CLOCK_MONOTONIC, &tst, i);
  59. if (labs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
  60. return pr_fail("%ld %ld\n",
  61. now.tv_sec + OFFSET, tst.tv_sec);
  62. }
  63. /* Check for proper vvar offsets after execve. */
  64. snprintf(now_str, sizeof(now_str), "%ld", now.tv_sec + OFFSET);
  65. execve("/proc/self/exe", cargv, cenv);
  66. return pr_perror("execve");
  67. }
  68. if (waitpid(pid, &status, 0) != pid)
  69. return pr_perror("waitpid");
  70. if (status)
  71. ksft_exit_fail();
  72. ksft_test_result_pass("exec\n");
  73. ksft_exit_pass();
  74. return 0;
  75. }