|
| 1 | +#include <errno.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +#include <linux/limits.h> |
| 9 | + |
| 10 | +#include <sys/ioctl.h> |
| 11 | +#include <sys/stat.h> |
| 12 | +#include <sys/sysmacros.h> |
| 13 | +#include <sys/mman.h> |
| 14 | +#include <sys/types.h> |
| 15 | +#include <stdint.h> |
| 16 | +#include <pthread.h> |
| 17 | +#include <semaphore.h> |
| 18 | + |
| 19 | +#include "criu-plugin.h" |
| 20 | +#include "plugin.h" |
| 21 | +#include "xmalloc.h" |
| 22 | +#include "criu-log.h" |
| 23 | +#include "files.h" |
| 24 | + |
| 25 | +#include "common/list.h" |
| 26 | + |
| 27 | +#include "img-streamer.h" |
| 28 | +#include "image.h" |
| 29 | +#include "cr_options.h" |
| 30 | + |
| 31 | +#ifdef LOG_PREFIX |
| 32 | +#undef LOG_PREFIX |
| 33 | +#endif |
| 34 | +#define LOG_PREFIX "cedana_plugin: " |
| 35 | + |
| 36 | +#ifdef DEBUG |
| 37 | +#define plugin_log_msg(fmt, ...) pr_debug(fmt, ##__VA_ARGS__) |
| 38 | +#else |
| 39 | +#define plugin_log_msg(fmt, ...) \ |
| 40 | + { \ |
| 41 | + } |
| 42 | +#endif |
| 43 | + |
| 44 | + |
| 45 | +int write_fp(FILE *fp, const void *buf, const size_t buf_len) |
| 46 | +{ |
| 47 | + size_t len_write; |
| 48 | + |
| 49 | + len_write = fwrite(buf, 1, buf_len, fp); |
| 50 | + if (len_write != buf_len) { |
| 51 | + pr_perror("Unable to write file (wrote:%ld buf_len:%ld)", len_write, buf_len); |
| 52 | + return -EIO; |
| 53 | + } |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +int read_fp(FILE *fp, void *buf, const size_t buf_len) |
| 58 | +{ |
| 59 | + size_t len_read; |
| 60 | + |
| 61 | + len_read = fread(buf, 1, buf_len, fp); |
| 62 | + if (len_read != buf_len) { |
| 63 | + pr_perror("Unable to read file (read:%ld buf_len:%ld)", len_read, buf_len); |
| 64 | + return -EIO; |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Open an image file |
| 71 | + * |
| 72 | + * We store the size of the actual contents in the first 8-bytes of the file. This allows us to |
| 73 | + * determine the file size when using criu_image_streamer when fseek and fstat are not available. |
| 74 | + * The FILE * returned is already at the location of the first actual contents. |
| 75 | + * |
| 76 | + * @param path The file path |
| 77 | + * @param write False for read, true for write |
| 78 | + * @param size Size of actual contents |
| 79 | + * @return FILE *if successful, NULL if failed |
| 80 | + */ |
| 81 | +FILE *open_img_file(char *path, bool write, size_t *size) |
| 82 | +{ |
| 83 | + FILE *fp = NULL; |
| 84 | + int fd, ret; |
| 85 | + |
| 86 | + if (opts.stream) |
| 87 | + fd = img_streamer_open(path, write ? O_DUMP : O_RSTR); |
| 88 | + else |
| 89 | + fd = openat(criu_get_image_dir(), path, write ? (O_WRONLY | O_CREAT) : O_RDONLY, 0600); |
| 90 | + |
| 91 | + if (fd < 0) { |
| 92 | + pr_perror("%s: Failed to open for %s", path, write ? "write" : "read"); |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + |
| 96 | + fp = fdopen(fd, write ? "w" : "r"); |
| 97 | + if (!fp) { |
| 98 | + pr_perror("%s: Failed get pointer for %s", path, write ? "write" : "read"); |
| 99 | + return NULL; |
| 100 | + } |
| 101 | + |
| 102 | + if (write) |
| 103 | + ret = write_fp(fp, size, sizeof(*size)); |
| 104 | + else |
| 105 | + ret = read_fp(fp, size, sizeof(*size)); |
| 106 | + |
| 107 | + if (ret) { |
| 108 | + pr_perror("%s:Failed to access file size", path); |
| 109 | + fclose(fp); |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + |
| 113 | + pr_debug("%s:Opened file for %s with size:%ld\n", path, write ? "write" : "read", *size); |
| 114 | + return fp; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Write an image file |
| 119 | + * |
| 120 | + * We store the size of the actual contents in the first 8-bytes of the file. This allows us to |
| 121 | + * determine the file size when using criu_image_streamer when fseek and fstat are not available. |
| 122 | + * |
| 123 | + * @param path The file path |
| 124 | + * @param buf pointer to data to be written |
| 125 | + * @param buf_len size of buf |
| 126 | + * @return 0 if successful. -errno on failure |
| 127 | + */ |
| 128 | +int write_img_file(char *path, const void *buf, const size_t buf_len) |
| 129 | +{ |
| 130 | + int ret; |
| 131 | + FILE *fp; |
| 132 | + size_t len = buf_len; |
| 133 | + |
| 134 | + fp = open_img_file(path, true, &len); |
| 135 | + if (!fp) |
| 136 | + return -errno; |
| 137 | + |
| 138 | + ret = write_fp(fp, buf, buf_len); |
| 139 | + fclose(fp); /* this will also close fd */ |
| 140 | + return ret; |
| 141 | +} |
| 142 | + |
| 143 | +int read_file(const char *file_path, void *buf, const size_t buf_len) |
| 144 | +{ |
| 145 | + int ret; |
| 146 | + FILE *fp; |
| 147 | + |
| 148 | + fp = fopen(file_path, "r"); |
| 149 | + if (!fp) { |
| 150 | + pr_perror("Cannot fopen %s", file_path); |
| 151 | + return -errno; |
| 152 | + } |
| 153 | + |
| 154 | + ret = read_fp(fp, buf, buf_len); |
| 155 | + fclose(fp); /* this will also close fd */ |
| 156 | + return ret; |
| 157 | +} |
| 158 | + |
| 159 | +int cedana_plugin_init(int stage) |
| 160 | +{ |
| 161 | + pr_info("cedana_plugin: started"); |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +void cedana_plugin_fini(int stage, int ret) |
| 166 | +{ |
| 167 | + pr_info("cedana_plugin: finished"); |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +CR_PLUGIN_REGISTER("cedanagpu_plugin", cedana_plugin_init, cedana_plugin_fini) |
| 172 | + |
| 173 | +int cedana_plugin_handle_device_vma(int fd, const struct stat *st_buf) |
| 174 | +{ |
| 175 | + pr_info("cedana_plugin_handle_device_vma called"); |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__HANDLE_DEVICE_VMA, cedana_plugin_handle_device_vma) |
| 180 | + |
| 181 | + |
| 182 | +int cedana_plugin_dump_file(int fd, int id) |
| 183 | +{ |
| 184 | + pr_info("cedana_plugin_dump_file called"); |
| 185 | + return 0; |
| 186 | +} |
| 187 | + |
| 188 | +CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__DUMP_EXT_FILE, cedana_plugin_dump_file) |
| 189 | + |
| 190 | + |
| 191 | +int cedana_plugin_restore_file(int id) |
| 192 | +{ |
| 193 | + pr_info("cedana_plugin_restore_file called"); |
| 194 | + return 0; |
| 195 | +} |
| 196 | + |
| 197 | +CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__RESTORE_EXT_FILE, cedana_plugin_restore_file) |
| 198 | + |
| 199 | +int cedana_plugin_update_vmamap(const char *in_path, const uint64_t addr, const uint64_t old_offset, |
| 200 | + uint64_t *new_offset, int *updated_fd) |
| 201 | +{ |
| 202 | + pr_info("cedana_plugin_update_vmamap called"); |
| 203 | + return 0; |
| 204 | +} |
| 205 | + |
| 206 | +CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__UPDATE_VMA_MAP, cedana_plugin_update_vmamap) |
| 207 | + |
| 208 | + |
| 209 | +int cedana_plugin_resume_devices_late(int pid) |
| 210 | +{ |
| 211 | + pr_info("cedana_plugin_resume_devices_late called"); |
| 212 | + return 0; |
| 213 | +} |
| 214 | + |
| 215 | +CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__RESUME_DEVICES_LATE, cedana_plugin_resume_devices_late) |
0 commit comments