-
Notifications
You must be signed in to change notification settings - Fork 30
/
pyxis_slurmstepd.c
1450 lines (1187 loc) · 34.4 KB
/
pyxis_slurmstepd.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*/
#include <linux/limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <paths.h>
#include <pthread.h>
#include <sched.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <slurm/spank.h>
#include "pyxis_slurmstepd.h"
#include "common.h"
#include "config.h"
#include "args.h"
#include "seccomp_filter.h"
#include "enroot.h"
struct container {
char *name;
char *squashfs_path;
char *save_path;
bool reuse_rootfs;
bool reuse_ns;
bool temporary_squashfs;
bool temporary_rootfs;
int userns_fd;
int mntns_fd;
int cgroupns_fd;
int cwd_fd;
};
struct job_info {
uid_t uid;
gid_t gid;
bool privileged;
uint32_t jobid;
uint32_t stepid;
uint32_t local_task_count;
uint32_t total_task_count;
char **environ;
char cwd[PATH_MAX];
};
struct shared_memory {
pthread_mutex_t mutex;
atomic_uint init_tasks;
atomic_uint started_tasks;
atomic_uint completed_tasks;
pid_t pid;
pid_t ns_pid;
};
struct plugin_context {
bool enabled;
int log_fd;
struct plugin_config config;
struct plugin_args *args;
struct job_info job;
struct container container;
int user_init_rv;
struct shared_memory *shm;
};
static struct plugin_context context = {
.enabled = false,
.log_fd = -1,
.config = { .runtime_path = { 0 } },
.args = NULL,
.job = {
.uid = -1, .gid = -1, .privileged = false,
.jobid = 0, .stepid = 0,
.local_task_count = 0, .total_task_count = 0,
.environ = NULL, .cwd = { 0 }
},
.container = {
.name = NULL, .squashfs_path = NULL, .save_path = NULL,
.reuse_rootfs = false, .reuse_ns = false, .temporary_squashfs = false, .temporary_rootfs = false,
.userns_fd = -1, .mntns_fd = -1, .cgroupns_fd = -1, .cwd_fd = -1
},
.user_init_rv = 0,
};
static bool pyxis_execute_entrypoint(void)
{
return context.args->entrypoint == 1 || (context.args->entrypoint == -1 && context.config.execute_entrypoint == true);
}
int pyxis_slurmstepd_init(spank_t sp, int ac, char **av)
{
int ret;
ret = pyxis_config_parse(&context.config, ac, av);
if (ret < 0) {
slurm_error("pyxis: failed to parse configuration");
return (-1);
}
context.args = pyxis_args_register(sp);
if (context.args == NULL) {
slurm_error("pyxis: failed to register arguments");
return (-1);
}
return (0);
}
static int job_get_info(spank_t sp, struct job_info *job)
{
spank_err_t rc;
char allow_superuser[] = "false";
int rv = -1;
rc = spank_get_item(sp, S_JOB_UID, &job->uid);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job uid: %s", spank_strerror(rc));
goto fail;
}
rc = spank_get_item(sp, S_JOB_GID, &job->gid);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job gid: %s", spank_strerror(rc));
goto fail;
}
rc = spank_get_item(sp, S_JOB_ID, &job->jobid);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job ID: %s", spank_strerror(rc));
goto fail;
}
rc = spank_get_item(sp, S_JOB_STEPID, &job->stepid);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job step ID: %s", spank_strerror(rc));
goto fail;
}
rc = spank_get_item(sp, S_JOB_LOCAL_TASK_COUNT, &job->local_task_count);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job local task count: %s", spank_strerror(rc));
goto fail;
}
rc = spank_get_item(sp, S_JOB_TOTAL_TASK_COUNT, &job->total_task_count);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job total task count: %s", spank_strerror(rc));
goto fail;
}
/* This should probably be added to the API as a spank_item */
rc = spank_getenv(sp, "PWD", job->cwd, sizeof(job->cwd));
if (rc != ESPANK_SUCCESS)
slurm_info("pyxis: couldn't get job cwd path: %s", spank_strerror(rc));
rc = spank_getenv(sp, "ENROOT_ALLOW_SUPERUSER", allow_superuser, sizeof(allow_superuser));
if (rc == ESPANK_SUCCESS) {
if (job->uid == 0 && strcasecmp(allow_superuser, "no") != 0 && strcasecmp(allow_superuser, "false") != 0 &&
strcasecmp(allow_superuser, "n") != 0 && strcasecmp(allow_superuser, "f") != 0) {
job->privileged = true;
}
}
rv = 0;
fail:
return (rv);
}
static int job_get_env(spank_t sp, struct job_info *job)
{
spank_err_t rc;
char **spank_environ = NULL;
size_t environ_len = 0;
int rv = -1;
rc = spank_get_item(sp, S_JOB_ENV, &spank_environ);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: couldn't get job environment: %s", spank_strerror(rc));
goto fail;
}
if (job->environ != NULL) {
for (int i = 0; job->environ[i] != NULL; ++i)
free(job->environ[i]);
free(job->environ);
job->environ = NULL;
}
/* We need to make a copy of the environment returned by the SPANK API. */
for (size_t i = 0; spank_environ[i] != NULL; ++i)
environ_len += 1;
job->environ = malloc((environ_len + 1) * sizeof(char*));
if (job->environ == NULL)
goto fail;
job->environ[environ_len] = NULL;
for (size_t i = 0; i < environ_len; ++i) {
job->environ[i] = strdup(spank_environ[i]);
if (job->environ[i] == NULL)
goto fail;
}
rv = 0;
fail:
return (rv);
}
/* As root, create the per-user runtime directory where temporary squashfs files are stored. */
static int enroot_create_user_runtime_dir(void)
{
int ret;
char path[PATH_MAX];
ret = snprintf(path, sizeof(path), "%s/%u", context.config.runtime_path, context.job.uid);
if (ret < 0 || ret >= sizeof(path))
return (-1);
ret = mkdir(path, 0700);
if (ret < 0 && errno != EEXIST) {
slurm_error("pyxis: couldn't mkdir %s: %s", path, strerror(errno));
return (-1);
}
if (ret < 0 && errno == EEXIST)
return (0);
ret = chown(path, context.job.uid, context.job.gid);
if (ret < 0) {
slurm_error("pyxis: couldn't chown %s: %s", path, strerror(errno));
rmdir(path);
return (-1);
}
return (0);
}
int pyxis_slurmstepd_post_opt(spank_t sp, int ac, char **av)
{
int ret;
if (!pyxis_args_enabled())
return (0);
context.enabled = true;
ret = job_get_info(sp, &context.job);
if (ret < 0)
return (-1);
ret = enroot_create_user_runtime_dir();
if (ret < 0)
return (-1);
return (0);
}
static int enroot_new_log(void)
{
xclose(context.log_fd);
/* We can use CLOEXEC here since we dup2(2) this file descriptor when needed. */
context.log_fd = pyxis_memfd_create("enroot-log", MFD_CLOEXEC);
if (context.log_fd < 0)
slurm_info("pyxis: couldn't create in-memory log file: %s", strerror(errno));
return (context.log_fd);
}
/* We do not want to inherit any environment variable from slurmstepd, except PATH */
static int slurm_clear_env(void)
{
int rv = -1;
const char *p;
char *saved_path = NULL;
/* It's unclear if the pointer returned by getenv(3) will always persist after clearenv(3), so make a copy. */
p = getenv("PATH");
if (p != NULL) {
saved_path = strdup(p);
if (saved_path == NULL)
goto fail;
}
if (clearenv() != 0)
goto fail;
if (saved_path != NULL) {
if (setenv("PATH", saved_path, 1) < 0)
goto fail;
}
rv = 0;
fail:
free(saved_path);
return (rv);
}
/*
* List of environment variables that should not be passed from the Slurm job to enroot.
*/
#define PYXIS_ENV_ENTRY(s) { s, sizeof(s) - 1 }
static const struct {
const char *name;
size_t len;
} enroot_deny_env[] = {
PYXIS_ENV_ENTRY("PATH="),
PYXIS_ENV_ENTRY("LD_LIBRARY_PATH="),
PYXIS_ENV_ENTRY("LD_PRELOAD="),
PYXIS_ENV_ENTRY("SLURM_PROCID="),
PYXIS_ENV_ENTRY("SLURM_LOCALID="),
PYXIS_ENV_ENTRY("SLURM_TASK_PID="),
PYXIS_ENV_ENTRY("PMIX_RANK="),
PYXIS_ENV_ENTRY("PMI_FD="),
PYXIS_ENV_ENTRY("ENROOT_LIBRARY_PATH="),
PYXIS_ENV_ENTRY("ENROOT_SYSCONF_PATH="),
PYXIS_ENV_ENTRY("ENROOT_RUNTIME_PATH="),
PYXIS_ENV_ENTRY("ENROOT_CACHE_PATH="),
PYXIS_ENV_ENTRY("ENROOT_DATA_PATH="),
PYXIS_ENV_ENTRY("ENROOT_TEMP_PATH="),
PYXIS_ENV_ENTRY("ENROOT_ZSTD_OPTIONS="),
PYXIS_ENV_ENTRY("ENROOT_TRANSFER_RETRIES="),
PYXIS_ENV_ENTRY("ENROOT_CONNECT_TIMEOUT="),
PYXIS_ENV_ENTRY("ENROOT_TRANSFER_TIMEOUT="),
PYXIS_ENV_ENTRY("ENROOT_MAX_CONNECTIONS="),
PYXIS_ENV_ENTRY("ENROOT_ALLOW_HTTP="),
{ NULL, 0 }
};
#undef PYXIS_ENV_ENTRY
static int try_import_env(char *string)
{
for (int i = 0; enroot_deny_env[i].name != NULL; ++i) {
if (strncmp(string, enroot_deny_env[i].name, enroot_deny_env[i].len) == 0)
return (0);
}
if (putenv(string) < 0)
return (-1);
return (0);
}
static int enroot_import_job_env(char **env)
{
if (env == NULL)
return (-1);
/* Import all allowed environment variables from the job */
for (int i = 0; env[i]; ++i) {
if (try_import_env(env[i]) < 0)
return (-1);
}
return (0);
}
static int enroot_set_env(void)
{
if (slurm_clear_env() < 0)
return (-1);
if (enroot_import_job_env(context.job.environ) < 0)
return (-1);
if (context.args->mount_home == 0) {
if (setenv("ENROOT_MOUNT_HOME", "n", 1) < 0)
return (-1);
} else if (context.args->mount_home == 1) {
if (setenv("ENROOT_MOUNT_HOME", "y", 1) < 0)
return (-1);
} else {
/* If mount_home was not set by the user, we rely on the setting specified in the enroot config. */
}
if (context.args->remap_root == 0) {
if (setenv("ENROOT_REMAP_ROOT", "n", 1) < 0)
return (-1);
} else if (context.args->remap_root == 1) {
if (setenv("ENROOT_REMAP_ROOT", "y", 1) < 0)
return (-1);
} else {
/* If remap_root was not set by the user, we rely on the setting specified in the enroot config. */
}
if (context.args->writable == 0) {
if (setenv("ENROOT_ROOTFS_WRITABLE", "n", 1) < 0)
return (-1);
} else if (context.args->writable == 1) {
if (setenv("ENROOT_ROOTFS_WRITABLE", "y", 1) < 0)
return (-1);
} else {
/* If writable/readonly was not set by the user, we rely on the setting specified in the enroot config. */
}
return (0);
}
static pid_t enroot_exec_ctx(char *const argv[])
{
return enroot_exec(context.job.uid, context.job.gid, enroot_new_log(), enroot_set_env, argv);
}
static int enroot_exec_wait_ctx(char *const argv[])
{
return enroot_exec_wait(context.job.uid, context.job.gid, enroot_new_log(), enroot_set_env, argv);
}
static FILE *enroot_exec_output_ctx(char *const argv[])
{
return enroot_exec_output(context.job.uid, context.job.gid, enroot_set_env, argv);
}
static void enroot_print_log_ctx(bool error)
{
if (context.log_fd >= 0) {
enroot_print_log(context.log_fd, error);
xclose(context.log_fd);
context.log_fd = -1;
}
}
/*
* Returns -1 if an error occurs.
* Returns 0 and set *pid==-1 if container doesn't exist.
* Returns 0 and set *pid==0 if container exists but is not running.
* Returns 0 and set *pid>0 (the container PID) if the container exists and is running.
*/
static int enroot_container_get(const char *name, pid_t *pid)
{
FILE *fp;
char *line;
char *ctr_name, *ctr_pid, *saveptr;
unsigned long n;
int rv = -1;
if (name == NULL || strlen(name) == 0 || pid == NULL)
return (-1);
*pid = -1;
fp = enroot_exec_output_ctx((char *const[]){ "enroot", "list", "-f", NULL });
if (fp == NULL) {
slurm_error("pyxis: couldn't get list of existing container filesystems");
return (-1);
}
/* Skip headers line */
line = get_line_from_file(fp);
if (line == NULL) {
slurm_error("pyxis: \"enroot list -f\" did not produce any usable output");
goto fail;
}
while ((line = get_line_from_file(fp)) != NULL) {
ctr_name = strtok_r(line, " ", &saveptr);
if (ctr_name == NULL || *ctr_name == '\0')
goto fail;
if (strcmp(name, ctr_name) == 0) {
ctr_pid = strtok_r(NULL, " ", &saveptr);
if (ctr_pid == NULL || *ctr_pid == '\0') {
*pid = 0;
} else {
errno = 0;
n = strtoul(ctr_pid, NULL, 10);
if (errno != 0 || n != (pid_t)n)
goto fail;
*pid = (pid_t)n;
}
break;
}
free(line);
line = NULL;
}
rv = 0;
fail:
free(line);
if (fp) fclose(fp);
return (rv);
}
static int read_proc_environ(pid_t pid, char **result, size_t *size)
{
int ret;
char path[PATH_MAX];
int fd = -1;
char *buf = NULL;
size_t len = 0, capacity = 1024;
ssize_t n;
char *new_buf = NULL;
int rv = -1;
ret = snprintf(path, sizeof(path), "/proc/%d/environ", pid);
if (ret < 0 || ret >= sizeof(path))
goto fail;
fd = open(path, O_RDONLY);
if (fd < 0)
goto fail;
buf = malloc(capacity + 1);
if (buf == NULL)
goto fail;
while ((n = read(fd, buf + len, capacity - len)) > 0) {
len += n;
if (capacity - len == 0) {
/* Grow buffer. */
capacity *= 2;
new_buf = realloc(buf, capacity + 1);
if (new_buf == NULL)
goto fail;
buf = new_buf;
}
}
if (n < 0)
goto fail;
/* From man 5 proc, there might not be a null byte at the end. */
if (len > 0 && buf[len - 1] != '\0') {
buf[len] = '\0';
len += 1;
}
*result = buf;
*size = len;
rv = 0;
fail:
xclose(fd);
if (rv < 0)
free(buf);
return (rv);
}
static const char *container_deny_env[] = {
"LANG",
"LANGUAGE",
"LC_ALL",
NULL
};
static int spank_import_container_env(spank_t sp, pid_t pid)
{
int ret;
char *proc_environ = NULL;
size_t size;
spank_err_t rc;
int overwrite;
int rv = -1;
/* First, remove unwanted locale environment variables from the job */
for (int i = 0; container_deny_env[i] != NULL; ++i) {
/* Check if the user explicitly requested this environment variable to be preserved */
if (array_contains(context.args->env_vars, context.args->env_vars_len, container_deny_env[i]))
continue;
rc = spank_unsetenv(sp, container_deny_env[i]);
if (rc != ESPANK_SUCCESS) {
slurm_error("pyxis: failed to unset %s: %s", container_deny_env[i], spank_strerror(rc));
goto fail;
}
}
ret = read_proc_environ(pid, &proc_environ, &size);
if (ret < 0) {
slurm_error("pyxis: couldn't read /proc/%d/environ", pid);
goto fail;
}
for (size_t i = 0; i < size;) {
char *key, *value;
value = proc_environ + i;
key = strsep(&value, "=");
overwrite = 1;
if (array_contains(context.args->env_vars, context.args->env_vars_len, key))
overwrite = 0;
rc = spank_setenv(sp, key, value, overwrite);
if (rc != ESPANK_SUCCESS && rc != ESPANK_ENV_EXISTS) {
slurm_error("pyxis: failed to set %s: %s", key, spank_strerror(rc));
goto fail;
}
i += strlen(key) + 1 + strlen(value) + 1;
}
rv = 0;
fail:
free(proc_environ);
return (rv);
}
static int enroot_container_create(void)
{
int ret;
char *enroot_uri = NULL;
int rv = -1;
if (context.container.temporary_squashfs) {
if (strncmp("docker://", context.args->image, sizeof("docker://") - 1) == 0 ||
strncmp("dockerd://", context.args->image, sizeof("dockerd://") - 1) == 0) {
enroot_uri = strdup(context.args->image);
if (enroot_uri == NULL)
goto fail;
} else {
/* Assume `image` is an enroot URI for a docker image. */
ret = xasprintf(&enroot_uri, "docker://%s", context.args->image);
if (ret < 0)
goto fail;
}
/*
* Be more verbose if there is a single task in the job (it might be interactive),
* or if we are executing the batch step (S_JOB_TOTAL_TASK_COUNT=0)
*/
if (context.job.total_task_count == 0 || context.job.total_task_count == 1)
slurm_spank_log("pyxis: importing docker image: %s", context.args->image);
ret = enroot_exec_wait_ctx((char *const[]){ "enroot", "import", "--output", context.container.squashfs_path, enroot_uri, NULL });
if (ret < 0) {
slurm_error("pyxis: failed to import docker image");
enroot_print_log_ctx(true);
goto fail;
}
slurm_spank_log("pyxis: imported docker image: %s", context.args->image);
}
slurm_info("pyxis: creating container filesystem: %s", context.container.name);
ret = enroot_exec_wait_ctx((char *const[]){ "enroot", "create", "--name", context.container.name, context.container.squashfs_path, NULL });
if (ret < 0) {
slurm_error("pyxis: failed to create container filesystem");
enroot_print_log_ctx(true);
goto fail;
}
rv = 0;
fail:
free(enroot_uri);
if (context.container.temporary_squashfs) {
ret = unlink(context.container.squashfs_path);
if (ret < 0)
slurm_info("pyxis: could not remove squashfs %s: %s", context.container.squashfs_path, strerror(errno));
}
return (rv);
}
static int container_get_namespaces(pid_t pid, struct container *container)
{
int ret;
char path[PATH_MAX];
int rv = -1;
ret = snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
if (ret < 0 || ret >= sizeof(path))
goto fail;
container->userns_fd = open(path, O_RDONLY | O_CLOEXEC);
if (container->userns_fd < 0) {
slurm_error("pyxis: unable to open user namespace file: %s", strerror(errno));
goto fail;
}
ret = snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
if (ret < 0 || ret >= sizeof(path))
goto fail;
container->mntns_fd = open(path, O_RDONLY | O_CLOEXEC);
if (container->mntns_fd < 0) {
slurm_error("pyxis: unable to open mount namespace file: %s", strerror(errno));
goto fail;
}
ret = snprintf(path, sizeof(path), "/proc/%d/ns/cgroup", pid);
if (ret < 0 || ret >= sizeof(path))
goto fail;
container->cgroupns_fd = open(path, O_RDONLY | O_CLOEXEC);
/* Skip cgroup namespace if not supported */
if (container->cgroupns_fd < 0 && errno != ENOENT) {
slurm_error("pyxis: unable to open cgroup namespace file: %s", strerror(errno));
goto fail;
}
rv = 0;
fail:
return (rv);
}
static int container_get_cwd(pid_t pid, struct container *container)
{
int ret;
char path[PATH_MAX];
ret = snprintf(path, sizeof(path), "/proc/%d/cwd", pid);
if (ret < 0 || ret >= sizeof(path))
return (-1);
container->cwd_fd = open(path, O_RDONLY | O_CLOEXEC);
if (container->cwd_fd < 0) {
slurm_error("pyxis: couldn't open cwd fd: %s", strerror(errno));
return (-1);
}
return (0);
}
static int enroot_create_start_config(char (*path)[PATH_MAX])
{
int ret;
int fd = -1;
FILE *f = NULL;
char template[] = "/tmp/.enroot_config_XXXXXX";
char *line = NULL;
int rv = -1;
fd = mkstemp(template);
if (fd < 0)
goto fail;
f = fdopen(fd, "a+");
if (f == NULL)
goto fail;
if (context.args->mounts_len > 0) {
ret = fprintf(f, "mounts() {\n");
if (ret < 0)
goto fail;
/* mount entries */
for (int i = 0; i < context.args->mounts_len; ++i) {
ret = fprintf(f, "\techo \"%s\"\n", context.args->mounts[i]);
if (ret < 0)
goto fail;
}
ret = fprintf(f, "}\n");
if (ret < 0)
goto fail;
}
if (!pyxis_execute_entrypoint()) {
ret = fprintf(f, "hooks() {\n");
if (ret < 0)
goto fail;
/*
* /etc/rc.local will be sourced by /etc/rc.
* We call 'exec' from there and do not return control to /etc/rc.
*/
ret = fprintf(f, "\techo 'exec \"$@\"' > ${ENROOT_ROOTFS}/etc/rc.local\n");
if (ret < 0)
goto fail;
ret = fprintf(f, "}\n");
if (ret < 0)
goto fail;
}
if (context.args->env_vars_len > 0) {
ret = fprintf(f, "environ() {\n");
if (ret < 0)
goto fail;
for (int i = 0; i < context.args->env_vars_len; ++i) {
ret = fprintf(f, "\t[ -n \"${%1$s-}\" ] && echo \"%1$s=${%1$s}\" || :\n",
context.args->env_vars[i]);
if (ret < 0)
goto fail;
}
ret = fprintf(f, "}\n");
if (ret < 0)
goto fail;
}
/* print contents */
if (fseek(f, 0, SEEK_SET) == 0) {
slurm_verbose("pyxis: enroot start configuration script:");
while ((line = get_line_from_file(f)) != NULL) {
slurm_verbose("pyxis: %s", line);
free(line);
}
}
if (memccpy(*path, template, '\0', sizeof(*path)) == NULL)
goto fail;
rv = 0;
fail:
if (f != NULL)
fclose(f);
xclose(fd);
return (rv);
}
static pid_t enroot_container_start(void)
{
int ret;
char conf_file[PATH_MAX] = { 0 };
pid_t pid = -1;
int status;
pid_t rv = -1;
slurm_info("pyxis: starting container: %s", context.container.name);
ret = enroot_create_start_config(&conf_file);
if (ret < 0) {
slurm_error("pyxis: couldn't create enroot start configuration script");
goto fail;
}
/*
* The plugin starts the container as a subprocess and acquires handles on the
* container's namespaces. We must do this after the container runtime has called
* unshare(2) and pivot_root(2). To synchronize the plugin and the container, the
* shell inside the container sends itself SIGSTOP through the command "kill -STOP
* $$" and the plugin waits for the container to be stopped by calling waitpid(2)
* with the WUNTRACED option.
* This requires a shell inside the container, but we could do the same with a
* small static C program bind-mounted inside the container.
*/
pid = enroot_exec_ctx((char *const[]){ "enroot", "start", "--conf", conf_file, context.container.name, "sh", "-c",
"kill -STOP $$ ; exit 0", NULL });
if (pid < 0) {
slurm_error("pyxis: failed to start container");
goto fail;
}
/* Wait for the child to terminate or stop itself (with WUNTRACED). */
ret = waitpid(pid, &status, WUNTRACED);
if (ret < 0) {
slurm_error("pyxis: container start error: %s", strerror(errno));
goto fail;
}
if (WIFEXITED(status) && (ret = WEXITSTATUS(status)) != 0) {
slurm_error("pyxis: container start failed with error code: %d", ret);
goto fail;
}
if (!WIFSTOPPED(status)) {
slurm_error("pyxis: container exited too soon");
goto fail;
}
rv = pid;
fail:
if (rv == -1)
enroot_print_log_ctx(true);
else if (pyxis_execute_entrypoint() && context.args->entrypoint_log == 1)
enroot_print_log_ctx(false);
if (conf_file[0] != '\0')
unlink(conf_file);
return (rv);
}
static int enroot_container_stop(pid_t pid)
{
int ret;
if (pid <= 0)
return (-1);
ret = kill(pid, SIGCONT);
if (ret < 0) {
slurm_error("pyxis: couldn't send SIGCONT to container process: %s", strerror(errno));
return (-1);
}
return (0);
}
static struct shared_memory *shm_init(void)
{
struct shared_memory *shm = NULL;
pthread_mutexattr_t mutex_attr;
int ret;
shm = mmap(0, sizeof(*shm), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (shm == MAP_FAILED) {
shm = NULL;
goto fail;
}
ret = pthread_mutexattr_init(&mutex_attr);
if (ret < 0)
goto fail;
ret = pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
if (ret < 0)
goto fail;
ret = pthread_mutexattr_setrobust(&mutex_attr, PTHREAD_MUTEX_ROBUST);
if (ret < 0)
goto fail;
ret = pthread_mutex_init(&shm->mutex, &mutex_attr);
if (ret < 0)
goto fail;
shm->init_tasks = 0;
shm->started_tasks = 0;
shm->completed_tasks = 0;
shm->pid = -1;
shm->ns_pid = -1;
return shm;
fail:
if (shm != NULL)
munmap(shm, sizeof(*shm));
return (NULL);
}
static int shm_destroy(struct shared_memory *shm)
{
int ret;
if (shm == NULL)
return (0);
if (pthread_mutex_lock(&shm->mutex) == EOWNERDEAD)
pthread_mutex_consistent(&shm->mutex);
pthread_mutex_unlock(&shm->mutex);
ret = pthread_mutex_destroy(&shm->mutex);
if (ret < 0)
return (-1);
ret = munmap(shm, sizeof(*shm));
if (ret < 0)
return (-1);
return (0);
}
int slurm_spank_user_init(spank_t sp, int ac, char **av)
{
int ret;
spank_err_t rc;
int spank_argc = 0;
char **spank_argv = NULL;
char *container_name = NULL;
pid_t pid;
int rv = -1;
if (!context.enabled)
return (0);
context.shm = shm_init();
if (context.shm == NULL)
goto fail;
ret = job_get_env(sp, &context.job);
if (ret < 0)
goto fail;
if (context.job.stepid == SLURM_BATCH_SCRIPT) {
rc = spank_get_item(sp, S_JOB_ARGV, &spank_argc, &spank_argv);
if (rc != ESPANK_SUCCESS) {