luo_test_utils.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2025, Google LLC.
  4. * Pasha Tatashin <pasha.tatashin@soleen.com>
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <getopt.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/syscall.h>
  15. #include <sys/mman.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <errno.h>
  19. #include <stdarg.h>
  20. #include "luo_test_utils.h"
  21. int luo_open_device(void)
  22. {
  23. return open(LUO_DEVICE, O_RDWR);
  24. }
  25. int luo_create_session(int luo_fd, const char *name)
  26. {
  27. struct liveupdate_ioctl_create_session arg = { .size = sizeof(arg) };
  28. snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s",
  29. LIVEUPDATE_SESSION_NAME_LENGTH - 1, name);
  30. if (ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg) < 0)
  31. return -errno;
  32. return arg.fd;
  33. }
  34. int luo_retrieve_session(int luo_fd, const char *name)
  35. {
  36. struct liveupdate_ioctl_retrieve_session arg = { .size = sizeof(arg) };
  37. snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s",
  38. LIVEUPDATE_SESSION_NAME_LENGTH - 1, name);
  39. if (ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg) < 0)
  40. return -errno;
  41. return arg.fd;
  42. }
  43. int create_and_preserve_memfd(int session_fd, int token, const char *data)
  44. {
  45. struct liveupdate_session_preserve_fd arg = { .size = sizeof(arg) };
  46. long page_size = sysconf(_SC_PAGE_SIZE);
  47. void *map = MAP_FAILED;
  48. int mfd = -1, ret = -1;
  49. mfd = memfd_create("test_mfd", 0);
  50. if (mfd < 0)
  51. return -errno;
  52. if (ftruncate(mfd, page_size) != 0)
  53. goto out;
  54. map = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, mfd, 0);
  55. if (map == MAP_FAILED)
  56. goto out;
  57. snprintf(map, page_size, "%s", data);
  58. munmap(map, page_size);
  59. arg.fd = mfd;
  60. arg.token = token;
  61. if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0)
  62. goto out;
  63. ret = 0;
  64. out:
  65. if (ret != 0 && errno != 0)
  66. ret = -errno;
  67. if (mfd >= 0)
  68. close(mfd);
  69. return ret;
  70. }
  71. int restore_and_verify_memfd(int session_fd, int token,
  72. const char *expected_data)
  73. {
  74. struct liveupdate_session_retrieve_fd arg = { .size = sizeof(arg) };
  75. long page_size = sysconf(_SC_PAGE_SIZE);
  76. void *map = MAP_FAILED;
  77. int mfd = -1, ret = -1;
  78. arg.token = token;
  79. if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0)
  80. return -errno;
  81. mfd = arg.fd;
  82. map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, mfd, 0);
  83. if (map == MAP_FAILED)
  84. goto out;
  85. if (expected_data && strcmp(expected_data, map) != 0) {
  86. ksft_print_msg("Data mismatch! Expected '%s', Got '%s'\n",
  87. expected_data, (char *)map);
  88. ret = -EINVAL;
  89. goto out_munmap;
  90. }
  91. ret = mfd;
  92. out_munmap:
  93. munmap(map, page_size);
  94. out:
  95. if (ret < 0 && errno != 0)
  96. ret = -errno;
  97. if (ret < 0 && mfd >= 0)
  98. close(mfd);
  99. return ret;
  100. }
  101. int luo_session_finish(int session_fd)
  102. {
  103. struct liveupdate_session_finish arg = { .size = sizeof(arg) };
  104. if (ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, &arg) < 0)
  105. return -errno;
  106. return 0;
  107. }
  108. void create_state_file(int luo_fd, const char *session_name, int token,
  109. int next_stage)
  110. {
  111. char buf[32];
  112. int state_session_fd;
  113. state_session_fd = luo_create_session(luo_fd, session_name);
  114. if (state_session_fd < 0)
  115. fail_exit("luo_create_session for state tracking");
  116. snprintf(buf, sizeof(buf), "%d", next_stage);
  117. if (create_and_preserve_memfd(state_session_fd, token, buf) < 0)
  118. fail_exit("create_and_preserve_memfd for state tracking");
  119. /*
  120. * DO NOT close session FD, otherwise it is going to be unpreserved
  121. */
  122. }
  123. void restore_and_read_stage(int state_session_fd, int token, int *stage)
  124. {
  125. char buf[32] = {0};
  126. int mfd;
  127. mfd = restore_and_verify_memfd(state_session_fd, token, NULL);
  128. if (mfd < 0)
  129. fail_exit("failed to restore state memfd");
  130. if (read(mfd, buf, sizeof(buf) - 1) < 0)
  131. fail_exit("failed to read state mfd");
  132. *stage = atoi(buf);
  133. close(mfd);
  134. }
  135. void daemonize_and_wait(void)
  136. {
  137. pid_t pid;
  138. ksft_print_msg("[STAGE 1] Forking persistent child to hold sessions...\n");
  139. pid = fork();
  140. if (pid < 0)
  141. fail_exit("fork failed");
  142. if (pid > 0) {
  143. ksft_print_msg("[STAGE 1] Child PID: %d. Resources are pinned.\n", pid);
  144. ksft_print_msg("[STAGE 1] You may now perform kexec reboot.\n");
  145. exit(EXIT_SUCCESS);
  146. }
  147. /* Detach from terminal so closing the window doesn't kill us */
  148. if (setsid() < 0)
  149. fail_exit("setsid failed");
  150. close(STDIN_FILENO);
  151. close(STDOUT_FILENO);
  152. close(STDERR_FILENO);
  153. /* Change dir to root to avoid locking filesystems */
  154. if (chdir("/") < 0)
  155. exit(EXIT_FAILURE);
  156. while (1)
  157. sleep(60);
  158. }
  159. static int parse_stage_args(int argc, char *argv[])
  160. {
  161. static struct option long_options[] = {
  162. {"stage", required_argument, 0, 's'},
  163. {0, 0, 0, 0}
  164. };
  165. int option_index = 0;
  166. int stage = 1;
  167. int opt;
  168. optind = 1;
  169. while ((opt = getopt_long(argc, argv, "s:", long_options, &option_index)) != -1) {
  170. switch (opt) {
  171. case 's':
  172. stage = atoi(optarg);
  173. if (stage != 1 && stage != 2)
  174. fail_exit("Invalid stage argument");
  175. break;
  176. default:
  177. fail_exit("Unknown argument");
  178. }
  179. }
  180. return stage;
  181. }
  182. int luo_test(int argc, char *argv[],
  183. const char *state_session_name,
  184. luo_test_stage1_fn stage1,
  185. luo_test_stage2_fn stage2)
  186. {
  187. int target_stage = parse_stage_args(argc, argv);
  188. int luo_fd = luo_open_device();
  189. int state_session_fd;
  190. int detected_stage;
  191. if (luo_fd < 0) {
  192. ksft_exit_skip("Failed to open %s. Is the luo module loaded?\n",
  193. LUO_DEVICE);
  194. }
  195. state_session_fd = luo_retrieve_session(luo_fd, state_session_name);
  196. if (state_session_fd == -ENOENT)
  197. detected_stage = 1;
  198. else if (state_session_fd >= 0)
  199. detected_stage = 2;
  200. else
  201. fail_exit("Failed to check for state session");
  202. if (target_stage != detected_stage) {
  203. ksft_exit_fail_msg("Stage mismatch Requested --stage %d, but system is in stage %d.\n"
  204. "(State session %s: %s)\n",
  205. target_stage, detected_stage, state_session_name,
  206. (detected_stage == 2) ? "EXISTS" : "MISSING");
  207. }
  208. if (target_stage == 1)
  209. stage1(luo_fd);
  210. else
  211. stage2(luo_fd, state_session_fd);
  212. return 0;
  213. }