-
Notifications
You must be signed in to change notification settings - Fork 318
/
cgroup.c
2799 lines (2352 loc) · 77.4 KB
/
cgroup.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
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include "cgroup.h"
#include "ebpf.h"
#include "utils.h"
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/vfs.h>
#include <inttypes.h>
#include <time.h>
#ifdef HAVE_SYSTEMD
# include <systemd/sd-bus.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <libgen.h>
static const cgroups_subsystem_t cgroups_subsystems[] = {"cpuset", "cpu", "devices", "pids", "memory",
"net_cls,net_prio", "freezer", "blkio",
"hugetlb", "cpu,cpuacct", "perf_event",
"unified", NULL};
const cgroups_subsystem_t *
libcrun_get_cgroups_subsystems (libcrun_error_t *err arg_unused)
{
return cgroups_subsystems;
}
struct symlink_s
{
const char *name;
const char *target;
};
static struct symlink_s cgroup_symlinks[] = {
{ "cpu", "cpu,cpuacct" },
{ "cpuacct", "cpu,cpuacct" },
{ "net_cls", "net_cls,net_prio" },
{ "net_prio", "net_cls,net_prio" },
{ NULL, NULL }
};
#define SYSTEMD_PROPERTY_PREFIX "org.systemd.property."
#ifndef CGROUP2_SUPER_MAGIC
# define CGROUP2_SUPER_MAGIC 0x63677270
#endif
#ifndef TMPFS_MAGIC
# define TMPFS_MAGIC 0x01021994
#endif
static int
detect_cgroup_mode (libcrun_error_t *err)
{
struct statfs stat;
int ret;
ret = statfs ("/sys/fs/cgroup", &stat);
if (ret < 0)
return crun_make_error (err, errno, "statfs '/sys/fs/cgroup'");
if (stat.f_type == CGROUP2_SUPER_MAGIC)
return CGROUP_MODE_UNIFIED;
if (stat.f_type != TMPFS_MAGIC)
return crun_make_error (err, 0, "invalid file system type on '/sys/fs/cgroup'");
ret = statfs ("/sys/fs/cgroup/unified", &stat);
if (ret < 0 && errno != ENOENT)
return crun_make_error (err, errno, "statfs '/sys/fs/cgroup/unified'");
if (ret < 0)
return CGROUP_MODE_LEGACY;
return stat.f_type == CGROUP2_SUPER_MAGIC ? CGROUP_MODE_HYBRID : CGROUP_MODE_LEGACY;
}
int
libcrun_get_cgroup_mode (libcrun_error_t *err)
{
int tmp;
static int cgroup_mode = 0;
if (cgroup_mode)
return cgroup_mode;
tmp = detect_cgroup_mode (err);
if (UNLIKELY (tmp < 0))
return tmp;
cgroup_mode = tmp;
return cgroup_mode;
}
static int
is_rwm (const char *str, libcrun_error_t *err)
{
const char *it;
bool r = false;
bool w = false;
bool m = false;
for (it = str; *it; it++)
switch (*it)
{
case 'r':
r = true;
break;
case 'w':
w = true;
break;
case 'm':
m = true;
break;
default:
return crun_make_error (err, 0, "invalid mode specified `%s`", str);
}
return r && w && m ? 1 : 0;
}
static int
enable_controllers (runtime_spec_schema_config_linux_resources *resources, const char *path, libcrun_error_t *err)
{
cleanup_free char *controllers = NULL;
cleanup_free char *tmp_path = NULL;
size_t controllers_len = 0;
bool has_hugetlb = false;
bool has_memory = false;
bool has_cpuset = false;
bool has_pids = false;
bool has_cpu = false;
bool has_io = false;
char *it;
int ret;
if (resources)
{
has_cpu = resources->cpu && (resources->cpu->shares
|| resources->cpu->period
|| resources->cpu->quota
|| resources->cpu->realtime_period
|| resources->cpu->realtime_runtime);
has_cpuset = resources->cpu && (resources->cpu->cpus || resources->cpu->mems);
has_io = resources->block_io;
has_memory = resources->memory;
has_pids = resources->pids;
has_hugetlb = resources->hugepage_limits_len;
}
controllers_len = xasprintf (&controllers, "%s %s %s %s %s %s",
has_cpu ? "+cpu" : "",
has_io ? "+io" : "",
has_memory ? "+memory" : "",
has_pids ? "+pids" : "",
has_cpuset ? "+cpuset" : "",
has_hugetlb ? "+hugetlb" : "");
xasprintf (&tmp_path, "%s/", path);
/* Activate the needed controllers in the root cgroup, but ignore errors here. */
ret = write_file ("/sys/fs/cgroup/cgroup.subtree_control", controllers, controllers_len, err);
if (UNLIKELY (ret < 0))
crun_error_release (err);
for (it = strchr (tmp_path + 1, '/'); it;)
{
cleanup_free char *cgroup_path = NULL;
cleanup_free char *subtree_control = NULL;
char *next_slash = strchr (it + 1, '/');
*it = '\0';
xasprintf (&cgroup_path, "/sys/fs/cgroup%s", tmp_path);
ret = mkdir (cgroup_path, 0755);
if (UNLIKELY (ret < 0 && errno != EEXIST))
return crun_make_error (err, errno, "create `%s`", cgroup_path);
if (next_slash)
{
xasprintf (&subtree_control, "%s/cgroup.subtree_control", cgroup_path);
ret = write_file (subtree_control, controllers, controllers_len, err);
if (UNLIKELY (ret < 0))
{
int e = crun_error_get_errno (err);
if (e == EPERM || e == EACCES || e == EBUSY)
{
crun_error_release (err);
goto next;
}
return ret;
}
}
next:
*it = '/';
it = next_slash;
}
return 0;
}
static int
initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *mems, libcrun_error_t *err)
{
cleanup_close int dirfd = -1;
cleanup_close int mems_fd = -1;
cleanup_close int cpus_fd = -1;
int b_len;
dirfd = open (path, O_DIRECTORY | O_RDONLY);
if (UNLIKELY (dirfd < 0))
return crun_make_error (err, errno, "open `%s`", path);
if (cpus[0] == '\0')
{
cpus_fd = openat (dirfd, "cpuset.cpus", O_RDWR);
if (UNLIKELY (cpus_fd < 0))
return crun_make_error (err, errno, "open '%s/%s'", path, "cpuset.cpus");
b_len = TEMP_FAILURE_RETRY (read (cpus_fd, cpus, 256));
if (UNLIKELY (b_len < 0))
return crun_make_error (err, errno, "read from 'cpuset.cpus'");
cpus[b_len] = '\0';
if (cpus[0] == '\n')
cpus[0] = '\0';
}
if (mems[0] == '\0')
{
mems_fd = openat (dirfd, "cpuset.mems", O_RDWR);
if (UNLIKELY (mems_fd < 0))
return crun_make_error (err, errno, "open '%s/%s'", path, "cpuset.mems");
b_len = TEMP_FAILURE_RETRY (read (mems_fd, mems, 256));
if (UNLIKELY (b_len < 0))
return crun_make_error (err, errno, "read from 'cpuset.mems'");
mems[b_len] = '\0';
if (mems[0] == '\n')
mems[0] = '\0';
}
/* look up in the parent directory. */
if (cpus[0] == '\0' || mems[0] == '\0')
{
size_t parent_path_len;
int ret;
for (parent_path_len = path_len -1; parent_path_len > 1 && path[parent_path_len] != '/'; parent_path_len--);
if (parent_path_len == 1)
return 0;
path[parent_path_len] = '\0';
ret = initialize_cpuset_subsystem_rec (path, parent_path_len, cpus, mems, err);
path[parent_path_len] = '/';
if (UNLIKELY (ret < 0))
{
/* Ignore errors here and try to write the configuration we want later on. */
crun_error_release (err);
}
}
if (cpus_fd >= 0)
{
b_len = TEMP_FAILURE_RETRY (write (cpus_fd, cpus, strlen (cpus)));
if (UNLIKELY (b_len < 0))
return crun_make_error (err, errno, "write 'cpuset.cpus'");
}
if (mems_fd >= 0)
{
b_len = TEMP_FAILURE_RETRY (write (mems_fd, mems, strlen (mems)));
if (UNLIKELY (b_len < 0))
return crun_make_error (err, errno, "write 'cpuset.mems'");
}
return 0;
}
static int
initialize_cpuset_subsystem (const char *path, libcrun_error_t *err)
{
cleanup_free char *tmp_path = xstrdup (path);
char cpus_buf[257];
char mems_buf[257];
cpus_buf[0] = mems_buf[0] = '\0';
return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, err);
}
static int
initialize_memory_subsystem (const char *path, libcrun_error_t *err)
{
const char *const files[] = {"memory.limit_in_bytes", "memory.kmem.limit_in_bytes", "memory.memsw.limit_in_bytes", NULL};
cleanup_close int dirfd = -1;
int i;
dirfd = open (path, O_DIRECTORY | O_RDONLY);
if (UNLIKELY (dirfd < 0))
return crun_make_error (err, errno, "open `%s`", path);
for (i = 0; files[i]; i++)
{
int ret;
ret = write_file_at (dirfd, files[i], "-1", 2, err);
if (UNLIKELY (ret < 0))
{
/* Ignore any error here. */
crun_error_release (err);
}
}
return 0;
}
static int
move_process_to_cgroup (pid_t pid, const char *subsystem, const char *path, libcrun_error_t *err)
{
cleanup_free char *cgroup_path_procs = NULL;
char pid_str[16];
sprintf (pid_str, "%d", pid);
xasprintf (&cgroup_path_procs, "/sys/fs/cgroup/%s%s/cgroup.procs", subsystem ? subsystem : "", path ? path : "");
return write_file (cgroup_path_procs, pid_str, strlen (pid_str), err);
}
static int
enter_cgroup_subsystem (pid_t pid, const char *subsystem, const char *path, bool create_if_missing, libcrun_error_t *err)
{
cleanup_free char *cgroup_path = NULL;
int ret;
xasprintf (&cgroup_path, "/sys/fs/cgroup/%s%s", subsystem, path ? path : "");
if (create_if_missing)
{
ret = crun_ensure_directory (cgroup_path, 0755, false, err);
if (UNLIKELY (ret < 0))
{
if (errno != EROFS)
return crun_make_error (err, errno, "creating cgroup directory `%s`", cgroup_path);
crun_error_release (err);
return 0;
}
if (strcmp (subsystem, "cpuset") == 0)
{
ret = initialize_cpuset_subsystem (cgroup_path, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (strcmp (subsystem, "memory") == 0)
{
ret = initialize_memory_subsystem (cgroup_path, err);
if (UNLIKELY (ret < 0))
return ret;
}
}
else
{
ret = crun_path_exists (cgroup_path, err);
if (UNLIKELY (ret < 0))
return ret;
if (ret == 0)
return 0;
}
return move_process_to_cgroup (pid, subsystem, path, err);
}
static int
is_rootless (libcrun_error_t *err)
{
if (geteuid ())
return 1;
return check_running_in_user_namespace (err);
}
static int
get_file_owner (const char *path, uid_t *uid, gid_t *gid)
{
struct stat st;
int ret;
#ifdef HAVE_STATX
struct statx stx;
ret = statx (AT_FDCWD, path, AT_STATX_DONT_SYNC, STATX_UID|STATX_GID, &stx);
if (UNLIKELY (ret < 0))
{
if (errno == ENOSYS || errno == EINVAL)
goto fallback;
return ret;
}
*uid = stx.stx_uid;
*gid = stx.stx_gid;
return ret;
fallback:
#endif
ret = stat (path, &st);
if (UNLIKELY (ret < 0))
return ret;
*uid = st.st_uid;
*gid = st.st_gid;
return ret;
}
static int
chown_cgroups (const char *path, uid_t uid, gid_t gid, libcrun_error_t *err)
{
cleanup_free char *cgroup_path = NULL;
cleanup_dir DIR *dir = NULL;
struct dirent *next;
int ret;
int dfd;
xasprintf (&cgroup_path, "/sys/fs/cgroup/%s", path);
dir = opendir (cgroup_path);
if (UNLIKELY (dir == NULL))
return crun_make_error (err, errno, "cannot opendir `%s`", cgroup_path);
dfd = dirfd (dir);
for (next = readdir (dir); next; next = readdir (dir))
{
const char *name = next->d_name;
/* do not chown the parent directory, but chown the current one. */
if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
continue;
ret = fchownat (dfd, name, uid, gid, AT_SYMLINK_NOFOLLOW);
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "cannot chown `%s/%s`", cgroup_path, name);
}
return 0;
}
static int
copy_owner (const char *from, const char *to, libcrun_error_t *err)
{
uid_t uid = 0;
gid_t gid = 0;
int ret;
ret = get_file_owner (from, &uid, &gid);
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "cannot get file owner for %s", from);
if (uid == 0 && gid == 0)
return 0;
return chown_cgroups (to, uid, gid, err);
}
static int
read_unified_cgroup_pid (pid_t pid, char **path, libcrun_error_t *err)
{
int ret;
char cgroup_path[32];
char *from, *to;
cleanup_free char *content = NULL;
sprintf (cgroup_path, "/proc/%d/cgroup", pid);
ret = read_all_file (cgroup_path, &content, NULL, err);
if (UNLIKELY (ret < 0))
return ret;
from = strstr (content, "0::");
if (UNLIKELY (from == NULL))
return crun_make_error (err, -1, "cannot find cgroup2 for the process %d", pid);
from += 3;
to = strchr (from, '\n');
to = strchr (from, '\n');
if (UNLIKELY (to == NULL))
return crun_make_error (err, -1, "cannot parse `%s`", cgroup_path);
*to = '\0';
*path = xstrdup (from);
return 0;
}
static int
enter_cgroup_v1 (int cgroup_mode, pid_t pid, const char *path, bool create_if_missing, libcrun_error_t *err)
{
char pid_str[16];
int ret;
size_t i;
int entered_any = 0;
int rootless;
const cgroups_subsystem_t *subsystems;
sprintf (pid_str, "%d", pid);
subsystems = libcrun_get_cgroups_subsystems (err);
if (UNLIKELY (subsystems == NULL))
return -1;
rootless = is_rootless (err);
if (UNLIKELY (rootless < 0))
return rootless;
for (i = 0; subsystems[i]; i++)
{
char subsystem_path[64];
if (cgroup_mode == CGROUP_MODE_LEGACY && strcmp (subsystems[i], "unified") == 0)
continue;
sprintf (subsystem_path, "/sys/fs/cgroup/%s", subsystems[i]);
ret = crun_path_exists (subsystem_path, err);
if (UNLIKELY (ret < 0))
return ret;
if (ret == 0)
continue;
entered_any = 1;
ret = enter_cgroup_subsystem (pid, subsystems[i], path, create_if_missing, err);
if (UNLIKELY (ret < 0))
{
int errcode = crun_error_get_errno (err);
if (rootless && (errcode == EACCES || errcode == EPERM))
{
crun_error_release (err);
continue;
}
return ret;
}
}
return entered_any ? 0 : -1;
}
static int
enter_cgroup_v2 (pid_t pid, pid_t init_pid, const char *path, bool create_if_missing, libcrun_error_t *err)
{
cleanup_free char *cgroup_path_procs = NULL;
cleanup_free char *cgroup_path = NULL;
char pid_str[16];
int repeat;
int ret;
sprintf (pid_str, "%d", pid);
xasprintf (&cgroup_path, "/sys/fs/cgroup/%s", path);
if (create_if_missing)
{
ret = crun_ensure_directory (cgroup_path, 0755, false, err);
if (UNLIKELY (ret < 0))
return ret;
}
xasprintf (&cgroup_path_procs, "/sys/fs/cgroup/%s/cgroup.procs", path);
ret = write_file (cgroup_path_procs, pid_str, strlen (pid_str), err);
if (LIKELY (ret >= 0))
return ret;
/* If the cgroup is not being created, try to handle EBUSY. */
if (create_if_missing || crun_error_get_errno (err) != EBUSY)
return ret;
crun_error_release (err);
/* There are subdirectories so it is not possible to join the initial
cgroup. Create a subdirectory and use that.
It can still fail if the container creates a subdirectory under
/sys/fs/cgroup/../crun-exec/ */
for (repeat = 0;; repeat++)
{
cleanup_free char *cgroup_crun_exec_path = NULL;
cleanup_free char *cgroup_sub_path_procs = NULL;
/* There is an init pid, try to join its cgroup. */
if (init_pid > 0)
{
ret = read_unified_cgroup_pid (init_pid, &cgroup_crun_exec_path, err);
if (UNLIKELY (ret < 0))
return ret;
/* Make sure the cgroup is below the initial cgroup specified for the container. */
if (strncmp (path, cgroup_crun_exec_path, strlen (path)))
{
free (cgroup_crun_exec_path);
cgroup_crun_exec_path = NULL;
}
}
/* There is no init_pid to lookup, try a static path. */
if (cgroup_crun_exec_path == NULL)
xasprintf (&cgroup_crun_exec_path, "%s/crun-exec", path);
xasprintf (&cgroup_sub_path_procs, "/sys/fs/cgroup/%s/cgroup.procs", cgroup_crun_exec_path);
ret = write_file (cgroup_sub_path_procs, pid_str, strlen (pid_str), err);
if (UNLIKELY (ret < 0))
{
/* The init process might have moved to a different cgroup, try again. */
if (crun_error_get_errno (err) == EBUSY && init_pid && repeat < 20)
{
crun_error_release (err);
continue;
}
return ret;
}
return copy_owner (cgroup_path_procs, cgroup_crun_exec_path, err);
}
return ret;
}
static int
enter_cgroup (int cgroup_mode, pid_t pid, pid_t init_pid, const char *path, bool create_if_missing, libcrun_error_t *err)
{
if (cgroup_mode == CGROUP_MODE_UNIFIED)
return enter_cgroup_v2 (pid, init_pid, path, create_if_missing, err);
return enter_cgroup_v1 (cgroup_mode, pid, path, create_if_missing, err);
}
int
libcrun_cgroups_create_symlinks (int dirfd, libcrun_error_t *err)
{
int i;
for (i = 0; cgroup_symlinks[i].name; i++)
{
int ret;
ret = symlinkat (cgroup_symlinks[i].target, dirfd, cgroup_symlinks[i].name);
if (UNLIKELY (ret < 0))
{
if (errno == ENOENT || errno == EEXIST)
continue;
return crun_make_error (err, errno, "symlinkat %s", cgroup_symlinks[i].name);
}
}
return 0;
}
int
libcrun_move_process_to_cgroup (pid_t pid, pid_t init_pid, char *path, libcrun_error_t *err)
{
int cgroup_mode = libcrun_get_cgroup_mode (err);
if (UNLIKELY (cgroup_mode < 0))
return cgroup_mode;
if (path == NULL || *path == '\0')
return 0;
return enter_cgroup (cgroup_mode, pid, init_pid, path, false, err);
}
#ifdef HAVE_SYSTEMD
static
void get_systemd_scope_and_slice (const char *id, const char *cgroup_path, char **scope, char **slice)
{
char *n;
if (cgroup_path == NULL || cgroup_path[0] == '\0')
{
xasprintf (scope, "crun-%s.scope", id);
return;
}
n = strchr (cgroup_path, ':');
if (n == NULL)
xasprintf (scope, "%s.scope", cgroup_path);
else
{
xasprintf (scope, "%s.scope", n + 1);
n = strchr (*scope, ':');
if (n)
*n = '-';
}
if (slice)
{
*slice = xstrdup (cgroup_path);
n = strchr (*slice, ':');
if (n)
*n = '\0';
}
}
static
int systemd_finalize (struct libcrun_cgroup_args *args, const char *suffix, libcrun_error_t *err)
{
runtime_spec_schema_config_linux_resources *resources = args->resources;
cleanup_free char *content = NULL;
int cgroup_mode = args->cgroup_mode;
char **path = args->path;
pid_t pid = args->pid;
int ret;
char *from, *to;
char *saveptr = NULL;
cleanup_free char *cgroup_path = NULL;
xasprintf (&cgroup_path, "/proc/%d/cgroup", pid);
ret = read_all_file (cgroup_path, &content, NULL, err);
if (UNLIKELY (ret < 0))
return ret;
if (cgroup_mode == CGROUP_MODE_LEGACY)
{
from = strstr (content, ":memory");
if (UNLIKELY (from == NULL))
return crun_make_error (err, -1, "cannot find memory controller for the current process");
from += 8;
to = strchr (from, '\n');
if (UNLIKELY (to == NULL))
return crun_make_error (err, -1, "cannot parse /proc/self/cgroup");
*to = '\0';
if (suffix)
xasprintf (path, "%s/%s", from, suffix);
else
*path = xstrdup (from);
*to = '\n';
}
else
{
from = strstr (content, "0::");
if (UNLIKELY (from == NULL))
return crun_make_error (err, -1, "cannot find cgroup2 for the current process");
from += 3;
to = strchr (from, '\n');
if (UNLIKELY (to == NULL))
return crun_make_error (err, -1, "cannot parse /proc/self/cgroup");
*to = '\0';
if (suffix)
xasprintf (path, "%s/%s", from, suffix);
else
*path = xstrdup (from);
*to = '\n';
}
if (cgroup_mode == CGROUP_MODE_UNIFIED)
{
if (suffix)
{
cleanup_free char *dir = NULL;
xasprintf (&dir, "/sys/fs/cgroup%s", *path);
/* On cgroup v2, processes can be only in leaf nodes. If a suffix is used,
move the process immediately to the new location before enabling
the controllers.
*/
ret = crun_ensure_directory (dir, 0755, true, err);
if (UNLIKELY (ret < 0))
return ret;
ret = chown_cgroups (*path, args->root_uid, args->root_gid, err);
if (UNLIKELY (ret < 0))
return ret;
ret = move_process_to_cgroup (pid, NULL, *path, err);
if (UNLIKELY (ret < 0))
return ret;
}
ret = enable_controllers (resources, *path, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (cgroup_mode != CGROUP_MODE_UNIFIED && geteuid ())
return 0;
for (from = strtok_r (content, "\n", &saveptr); from; from = strtok_r (NULL, "\n", &saveptr))
{
char *subpath, *subsystem;
subsystem = strchr (from, ':') + 1;
subpath = strchr (subsystem, ':') + 1;
*(subpath - 1) = '\0';
if (strcmp (subpath, *path))
{
ret = enter_cgroup_subsystem (pid, subsystem, *path, 1, err);
if (UNLIKELY (ret < 0))
{
/* If it is a named hierarchy, skip the error. */
/* skip named hierarchies that have no cgroup controller */
if (strchr (subsystem, '='))
{
crun_error_release (err);
continue;
}
return ret;
}
}
}
return 0;
}
struct systemd_job_removed_s
{
const char *path;
const char *op;
int terminated;
libcrun_error_t err;
};
static int
systemd_job_removed (sd_bus_message *m, void *userdata, sd_bus_error *error arg_unused)
{
const char *path, *unit, *result;
uint32_t id;
int ret;
struct systemd_job_removed_s *d = userdata;
ret = sd_bus_message_read (m, "uoss", &id, &path, &unit, &result);
if (ret < 0)
return -1;
if (strcmp (d->path, path) == 0)
{
d->terminated = 1;
if (strcmp (result, "done") != 0)
crun_make_error (&d->err, 0, "error %s systemd unit `%s`: got `%s`", d->op, unit, result);
}
return 0;
}
static int
systemd_check_job_status_setup (sd_bus *bus,
struct systemd_job_removed_s *data,
libcrun_error_t *err)
{
int ret;
ret = sd_bus_match_signal_async (bus,
NULL,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"JobRemoved",
systemd_job_removed,
NULL,
data);
if (UNLIKELY (ret < 0))
return crun_make_error (err, -ret, "sd-bus match signal");
return 0;
}
static int
systemd_check_job_status (sd_bus *bus,
struct systemd_job_removed_s *data,
const char *path,
const char *op,
libcrun_error_t *err)
{
int sd_err;
data->path = path;
data->op = op;
while (!data->terminated)
{
sd_err = sd_bus_process (bus, NULL);
if (UNLIKELY (sd_err < 0))
return crun_make_error (err, -sd_err, "sd-bus process");
if (sd_err != 0)
continue;
sd_err = sd_bus_wait (bus, (uint64_t) -1);
if (UNLIKELY (sd_err < 0))
return crun_make_error (err, -sd_err, "sd-bus wait");
}
if (data->err != NULL)
{
*err = data->err;
return -1;
}
return 0;
}
/* Parse a gvariant string. Support only a subset of types, just enough for systemd . */
static int
append_systemd_annotation (sd_bus_message *m,
const char *name,
size_t name_len,
const char *value,
libcrun_error_t *err)
{
cleanup_free char *tmp_name = NULL;
uint32_t factor = 1;
const char *it;
int sd_err;
while (*value == ' ')
value++;
it = value;
/* If the name has the form NameSec, convert it to NameUSec. */
if (name_len > 4
&& name[name_len - 4] != 'U'
&& name[name_len - 3] == 'S'
&& name[name_len - 2] == 'e'
&& name[name_len - 1] == 'c')
{
factor = 1000000;
tmp_name = xmalloc (name_len + 2);
memcpy (tmp_name, name, name_len - 3);
memcpy (tmp_name + name_len - 3, "USec", 5);
name = tmp_name;
}
if ((strcmp (it, "true") == 0) || (strcmp (it, "false") == 0))
{
bool b = *it == 't';
sd_err = sd_bus_message_append (m, "(sv)", name, "b", b);
if (UNLIKELY (sd_err < 0))
return crun_make_error (err, -sd_err, "sd-bus message append `%s`", name);
return 0;
}
else if (*it == '\'')
{
cleanup_free char *v_start = NULL;
char *end;
it = v_start = xstrdup (value);
end = strchr (it + 1, '\'');
if (end == NULL)
return crun_make_error (err, 0, "invalid variant `%s`", value);
*end = '\0';
sd_err = sd_bus_message_append (m, "(sv)", name, "s", it + 1);
if (UNLIKELY (sd_err < 0))
return crun_make_error (err, -sd_err, "sd-bus message append `%s`", name);
return 0;
}
else if (has_prefix (it, "uint64 "))
{
char *endptr = NULL;
uint64_t v;
errno = 0;
v = strtoull (it + sizeof ("uint64"), &endptr, 10);
if (UNLIKELY (errno != 0 || *endptr))
return crun_make_error (err, errno, "invalid value for `%s`", name);
sd_err = sd_bus_message_append (m, "(sv)", name, "t", (uint64_t)(v * factor));
if (UNLIKELY (sd_err < 0))
return crun_make_error (err, -sd_err, "sd-bus message append `%s`", name);
return 0;
}
else if (has_prefix (it, "int64 "))
{
char *endptr = NULL;
int64_t v;
errno = 0;
v = strtoll (it + sizeof ("int64"), &endptr, 10);
if (UNLIKELY (errno != 0 || *endptr))
return crun_make_error (err, errno, "invalid value for `%s`", name);