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