|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/wait.h> |
| 7 | + |
| 8 | +#include <errno.h> |
| 9 | +#include <fcntl.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | +#include <unistd.h> |
| 14 | + |
| 15 | +#include <slurm/spank.h> |
| 16 | + |
| 17 | +#include "importer.h" |
| 18 | +#include "common.h" |
| 19 | + |
| 20 | +static int importer_child_wait(pid_t pid, int *log_fd, const char *cmd) |
| 21 | +{ |
| 22 | + int status; |
| 23 | + int ret; |
| 24 | + |
| 25 | + do { |
| 26 | + ret = waitpid(pid, &status, 0); |
| 27 | + } while (ret < 0 && errno == EINTR); |
| 28 | + |
| 29 | + if (ret < 0) { |
| 30 | + slurm_error("pyxis: could not wait for importer %s: %s", cmd, strerror(errno)); |
| 31 | + return (-1); |
| 32 | + } |
| 33 | + |
| 34 | + if (WIFSIGNALED(status)) { |
| 35 | + slurm_error("pyxis: importer %s terminated with signal %d", cmd, WTERMSIG(status)); |
| 36 | + memfd_print_log(log_fd, true, "importer"); |
| 37 | + return (-1); |
| 38 | + } |
| 39 | + |
| 40 | + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
| 41 | + slurm_error("pyxis: importer %s failed with exit code %d", cmd, WEXITSTATUS(status)); |
| 42 | + memfd_print_log(log_fd, true, "importer"); |
| 43 | + return (-1); |
| 44 | + } |
| 45 | + |
| 46 | + return (0); |
| 47 | +} |
| 48 | + |
| 49 | +static pid_t importer_exec(const char *importer_path, uid_t uid, gid_t gid, |
| 50 | + int stdout_fd, int stderr_fd, child_cb callback, char *const argv[]) |
| 51 | +{ |
| 52 | + int ret; |
| 53 | + int null_fd = -1; |
| 54 | + int oom_score_fd = -1; |
| 55 | + pid_t pid; |
| 56 | + char *argv_str; |
| 57 | + |
| 58 | + argv_str = join_strings(argv, " "); |
| 59 | + if (argv_str != NULL) { |
| 60 | + slurm_verbose("pyxis: running importer command: %s", argv_str); |
| 61 | + free(argv_str); |
| 62 | + } |
| 63 | + |
| 64 | + pid = fork(); |
| 65 | + if (pid < 0) { |
| 66 | + slurm_error("pyxis: fork error: %s", strerror(errno)); |
| 67 | + return (-1); |
| 68 | + } |
| 69 | + |
| 70 | + if (pid == 0) { |
| 71 | + null_fd = open("/dev/null", O_RDONLY); |
| 72 | + if (null_fd < 0) |
| 73 | + _exit(EXIT_FAILURE); |
| 74 | + |
| 75 | + ret = dup2(null_fd, STDIN_FILENO); |
| 76 | + if (ret < 0) |
| 77 | + _exit(EXIT_FAILURE); |
| 78 | + |
| 79 | + ret = dup2(stdout_fd, STDOUT_FILENO); |
| 80 | + if (ret < 0) |
| 81 | + _exit(EXIT_FAILURE); |
| 82 | + |
| 83 | + ret = dup2(stderr_fd, STDERR_FILENO); |
| 84 | + if (ret < 0) |
| 85 | + _exit(EXIT_FAILURE); |
| 86 | + |
| 87 | + oom_score_fd = open("/proc/self/oom_score_adj", O_CLOEXEC | O_WRONLY | O_APPEND); |
| 88 | + if (oom_score_fd >= 0) { |
| 89 | + dprintf(oom_score_fd, "%d", 0); |
| 90 | + close(oom_score_fd); |
| 91 | + } |
| 92 | + |
| 93 | + ret = setregid(gid, gid); |
| 94 | + if (ret < 0) |
| 95 | + _exit(EXIT_FAILURE); |
| 96 | + |
| 97 | + ret = setreuid(uid, uid); |
| 98 | + if (ret < 0) |
| 99 | + _exit(EXIT_FAILURE); |
| 100 | + |
| 101 | + if (callback != NULL) { |
| 102 | + ret = callback(); |
| 103 | + if (ret < 0) |
| 104 | + _exit(EXIT_FAILURE); |
| 105 | + } |
| 106 | + |
| 107 | + execve(importer_path, argv, environ); |
| 108 | + _exit(EXIT_FAILURE); |
| 109 | + } |
| 110 | + |
| 111 | + return (pid); |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +int importer_exec_get(const char *importer_path, uid_t uid, gid_t gid, |
| 116 | + child_cb callback, const char *image_uri, char **squashfs_path) |
| 117 | +{ |
| 118 | + char *argv[4]; |
| 119 | + int log_fd = -1; |
| 120 | + int pipe_fds[2] = {-1, -1}; |
| 121 | + pid_t child; |
| 122 | + FILE *pipe_file = NULL; |
| 123 | + char *line = NULL; |
| 124 | + |
| 125 | + log_fd = pyxis_memfd_create("importer-log", MFD_CLOEXEC); |
| 126 | + if (log_fd < 0) { |
| 127 | + slurm_error("pyxis: couldn't create in-memory log file: %s", strerror(errno)); |
| 128 | + return (-1); |
| 129 | + } |
| 130 | + |
| 131 | + /* Create pipe for reading stdout */ |
| 132 | + if (pipe(pipe_fds) < 0) { |
| 133 | + slurm_error("pyxis: could not create pipe: %s", strerror(errno)); |
| 134 | + xclose(log_fd); |
| 135 | + return (-1); |
| 136 | + } |
| 137 | + |
| 138 | + argv[0] = (char *)importer_path; |
| 139 | + argv[1] = "get"; |
| 140 | + argv[2] = (char *)image_uri; |
| 141 | + argv[3] = NULL; |
| 142 | + |
| 143 | + child = importer_exec(importer_path, uid, gid, pipe_fds[1], log_fd, callback, argv); |
| 144 | + xclose(pipe_fds[1]); /* Close write end in parent */ |
| 145 | + |
| 146 | + if (child < 0) { |
| 147 | + xclose(pipe_fds[0]); |
| 148 | + xclose(log_fd); |
| 149 | + return (-1); |
| 150 | + } |
| 151 | + |
| 152 | + /* Convert pipe fd to FILE* for get_line_from_file */ |
| 153 | + pipe_file = fdopen(pipe_fds[0], "r"); |
| 154 | + if (pipe_file == NULL) { |
| 155 | + slurm_error("pyxis: could not fdopen pipe: %s", strerror(errno)); |
| 156 | + xclose(pipe_fds[0]); |
| 157 | + xclose(log_fd); |
| 158 | + return (-1); |
| 159 | + } |
| 160 | + |
| 161 | + /* Read the squashfs path from stdout */ |
| 162 | + line = get_line_from_file(pipe_file); |
| 163 | + fclose(pipe_file); /* This also closes pipe_fds[0] */ |
| 164 | + |
| 165 | + /* Wait for child to complete */ |
| 166 | + if (importer_child_wait(child, &log_fd, "get") < 0) { |
| 167 | + free(line); |
| 168 | + xclose(log_fd); |
| 169 | + return (-1); |
| 170 | + } |
| 171 | + |
| 172 | + /* Validate we got a path */ |
| 173 | + if (line == NULL || strlen(line) == 0) { |
| 174 | + slurm_error("pyxis: importer did not return a squashfs path"); |
| 175 | + memfd_print_log(&log_fd, true, "importer"); |
| 176 | + free(line); |
| 177 | + xclose(log_fd); |
| 178 | + return (-1); |
| 179 | + } |
| 180 | + |
| 181 | + /* Return the path */ |
| 182 | + *squashfs_path = line; |
| 183 | + |
| 184 | + slurm_verbose("pyxis: importer squashfs path: %s", *squashfs_path); |
| 185 | + |
| 186 | + xclose(log_fd); |
| 187 | + |
| 188 | + return (0); |
| 189 | +} |
| 190 | + |
| 191 | +int importer_exec_release(const char *importer_path, uid_t uid, gid_t gid, |
| 192 | + child_cb callback) |
| 193 | +{ |
| 194 | + int ret; |
| 195 | + int log_fd; |
| 196 | + char *argv[3]; |
| 197 | + pid_t child; |
| 198 | + |
| 199 | + log_fd = pyxis_memfd_create("importer-log", MFD_CLOEXEC); |
| 200 | + if (log_fd < 0) { |
| 201 | + slurm_error("pyxis: couldn't create in-memory log file: %s", strerror(errno)); |
| 202 | + return (-1); |
| 203 | + } |
| 204 | + |
| 205 | + argv[0] = (char *)importer_path; |
| 206 | + argv[1] = "release"; |
| 207 | + argv[2] = NULL; |
| 208 | + |
| 209 | + child = importer_exec(importer_path, uid, gid, log_fd, log_fd, callback, argv); |
| 210 | + if (child < 0) { |
| 211 | + xclose(log_fd); |
| 212 | + return (-1); |
| 213 | + } |
| 214 | + |
| 215 | + ret = importer_child_wait(child, &log_fd, "release"); |
| 216 | + if (ret < 0) { |
| 217 | + xclose(log_fd); |
| 218 | + return (-1); |
| 219 | + } |
| 220 | + |
| 221 | + xclose(log_fd); |
| 222 | + |
| 223 | + return (0); |
| 224 | +} |
0 commit comments