-
Notifications
You must be signed in to change notification settings - Fork 30
/
enroot.c
204 lines (164 loc) · 3.96 KB
/
enroot.c
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
/*
* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*/
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <slurm/spank.h>
#include "enroot.h"
#include "common.h"
pid_t enroot_exec(uid_t uid, gid_t gid, int log_fd,
child_cb callback, char *const argv[])
{
int ret;
int null_fd = -1;
int target_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 enroot 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_RDWR);
if (null_fd < 0)
_exit(EXIT_FAILURE);
ret = dup2(null_fd, STDIN_FILENO);
if (ret < 0)
_exit(EXIT_FAILURE);
/* Redirect stdout/stderr to the log file or /dev/null */
if (log_fd >= 0)
target_fd = log_fd;
else
target_fd = null_fd;
ret = dup2(target_fd, STDOUT_FILENO);
if (ret < 0)
_exit(EXIT_FAILURE);
ret = dup2(target_fd, STDERR_FILENO);
if (ret < 0)
_exit(EXIT_FAILURE);
/*
* Attempt to set oom_score_adj to 0, as it's often set to -1000 (OOM killing
* disabled), inherited from slurmstepd or slurmd.
*/
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);
}
execvpe("enroot", argv, environ);
_exit(EXIT_FAILURE);
}
return (pid);
}
static int child_wait(pid_t pid)
{
int status;
int ret;
ret = waitpid(pid, &status, 0);
if (ret < 0) {
slurm_error("pyxis: could not wait for child %d: %s", pid, strerror(errno));
return (-1);
}
if (WIFSIGNALED(status)) {
slurm_error("pyxis: child %d terminated with signal %d", pid, WTERMSIG(status));
return (-1);
}
if (WIFEXITED(status) && (status = WEXITSTATUS(status)) != 0) {
slurm_error("pyxis: child %d failed with error code: %d", pid, status);
return (-1);
}
return (0);
}
int enroot_exec_wait(uid_t uid, gid_t gid, int log_fd,
child_cb callback, char *const argv[])
{
int ret;
pid_t child;
child = enroot_exec(uid, gid, log_fd, callback, argv);
if (child < 0)
return (-1);
ret = child_wait(child);
if (ret < 0)
return (-1);
return (0);
}
void enroot_print_log(int log_fd, bool error)
{
int ret;
FILE *fp;
char *line;
ret = lseek(log_fd, 0, SEEK_SET);
if (ret < 0) {
slurm_info("pyxis: couldn't rewind log file: %s", strerror(errno));
return;
}
fp = fdopen(log_fd, "r");
if (fp == NULL) {
slurm_info("pyxis: couldn't open in-memory log for printing: %s", strerror(errno));
return;
}
if (error)
slurm_error("pyxis: printing enroot log file:");
while ((line = get_line_from_file(fp)) != NULL) {
if (error)
slurm_error("pyxis: %s", line);
else
slurm_spank_log("%s", line);
free(line);
}
return;
}
FILE *enroot_exec_output(uid_t uid, gid_t gid,
child_cb callback, char *const argv[])
{
int ret;
int log_fd = -1;
FILE *fp = NULL;
log_fd = pyxis_memfd_create("enroot-log", MFD_CLOEXEC);
if (log_fd < 0) {
slurm_error("pyxis: couldn't create in-memory log file: %s", strerror(errno));
return (NULL);
}
ret = enroot_exec_wait(uid, gid, log_fd, callback, argv);
if (ret < 0) {
slurm_error("pyxis: couldn't execute enroot command");
enroot_print_log(log_fd, true);
goto fail;
}
ret = lseek(log_fd, 0, SEEK_SET);
if (ret < 0) {
slurm_error("pyxis: couldn't rewind log file: %s", strerror(errno));
goto fail;
}
fp = fdopen(log_fd, "r");
if (fp == NULL) {
slurm_error("pyxis: couldn't open in-memory log file: %s", strerror(errno));
goto fail;
}
log_fd = -1;
fail:
xclose(log_fd);
return (fp);
}