-
Notifications
You must be signed in to change notification settings - Fork 2
/
bindings.c
4683 lines (4031 loc) · 105 KB
/
bindings.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
/* lxcfs
*
* Copyright © 2014-2016 Canonical, Inc
* Author: Serge Hallyn <[email protected]>
*
* See COPYING file for details.
*/
#define FUSE_USE_VERSION 26
#define __STDC_FORMAT_MACROS
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <inttypes.h>
#include <libgen.h>
#include <pthread.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wait.h>
#include <linux/magic.h>
#include <linux/sched.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <sys/vfs.h>
#include "bindings.h"
#include "config.h" // for VERSION
/* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */
#define LXCFS_NUMSTRLEN64 21
/* Define pivot_root() if missing from the C library */
#ifndef HAVE_PIVOT_ROOT
static int pivot_root(const char * new_root, const char * put_old)
{
#ifdef __NR_pivot_root
return syscall(__NR_pivot_root, new_root, put_old);
#else
errno = ENOSYS;
return -1;
#endif
}
#else
extern int pivot_root(const char * new_root, const char * put_old);
#endif
enum {
LXC_TYPE_CGDIR,
LXC_TYPE_CGFILE,
LXC_TYPE_PROC_MEMINFO,
LXC_TYPE_PROC_CPUINFO,
LXC_TYPE_PROC_UPTIME,
LXC_TYPE_PROC_STAT,
LXC_TYPE_PROC_DISKSTATS,
LXC_TYPE_PROC_SWAPS,
};
struct file_info {
char *controller;
char *cgroup;
char *file;
int type;
char *buf; // unused as of yet
int buflen;
int size; //actual data size
int cached;
};
/* Reserve buffer size to account for file size changes. */
#define BUF_RESERVE_SIZE 512
/*
* A table caching which pid is init for a pid namespace.
* When looking up which pid is init for $qpid, we first
* 1. Stat /proc/$qpid/ns/pid.
* 2. Check whether the ino_t is in our store.
* a. if not, fork a child in qpid's ns to send us
* ucred.pid = 1, and read the initpid. Cache
* initpid and creation time for /proc/initpid
* in a new store entry.
* b. if so, verify that /proc/initpid still matches
* what we have saved. If not, clear the store
* entry and go back to a. If so, return the
* cached initpid.
*/
struct pidns_init_store {
ino_t ino; // inode number for /proc/$pid/ns/pid
pid_t initpid; // the pid of nit in that ns
long int ctime; // the time at which /proc/$initpid was created
struct pidns_init_store *next;
long int lastcheck;
};
/* lol - look at how they are allocated in the kernel */
#define PIDNS_HASH_SIZE 4096
#define HASH(x) ((x) % PIDNS_HASH_SIZE)
static struct pidns_init_store *pidns_hash_table[PIDNS_HASH_SIZE];
static pthread_mutex_t pidns_store_mutex = PTHREAD_MUTEX_INITIALIZER;
static void lock_mutex(pthread_mutex_t *l)
{
int ret;
if ((ret = pthread_mutex_lock(l)) != 0) {
lxcfs_error("returned:%d %s\n", ret, strerror(ret));
exit(1);
}
}
/* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
* Number of hierarchies mounted. */
static int num_hierarchies;
/* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
* Hierachies mounted {cpuset, blkio, ...}:
* Initialized via __constructor__ collect_and_mount_subsystems(). */
static char **hierarchies;
/* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
* Open file descriptors:
* @fd_hierarchies[i] refers to cgroup @hierarchies[i]. They are mounted in a
* private mount namespace.
* Initialized via __constructor__ collect_and_mount_subsystems().
* @fd_hierarchies[i] can be used to perform file operations on the cgroup
* mounts and respective files in the private namespace even when located in
* another namespace using the *at() family of functions
* {openat(), fchownat(), ...}. */
static int *fd_hierarchies;
static void unlock_mutex(pthread_mutex_t *l)
{
int ret;
if ((ret = pthread_mutex_unlock(l)) != 0) {
lxcfs_error("returned:%d %s\n", ret, strerror(ret));
exit(1);
}
}
static void store_lock(void)
{
lock_mutex(&pidns_store_mutex);
}
static void store_unlock(void)
{
unlock_mutex(&pidns_store_mutex);
}
/* Must be called under store_lock */
static bool initpid_still_valid(struct pidns_init_store *e, struct stat *nsfdsb)
{
struct stat initsb;
char fnam[100];
snprintf(fnam, 100, "/proc/%d", e->initpid);
if (stat(fnam, &initsb) < 0)
return false;
lxcfs_debug("Comparing ctime %ld == %ld for pid %d.\n", e->ctime,
initsb.st_ctime, e->initpid);
if (e->ctime != initsb.st_ctime)
return false;
return true;
}
/* Must be called under store_lock */
static void remove_initpid(struct pidns_init_store *e)
{
struct pidns_init_store *tmp;
int h;
lxcfs_debug("Remove_initpid: removing entry for %d.\n", e->initpid);
h = HASH(e->ino);
if (pidns_hash_table[h] == e) {
pidns_hash_table[h] = e->next;
free(e);
return;
}
tmp = pidns_hash_table[h];
while (tmp) {
if (tmp->next == e) {
tmp->next = e->next;
free(e);
return;
}
tmp = tmp->next;
}
}
#define PURGE_SECS 5
/* Must be called under store_lock */
static void prune_initpid_store(void)
{
static long int last_prune = 0;
struct pidns_init_store *e, *prev, *delme;
long int now, threshold;
int i;
if (!last_prune) {
last_prune = time(NULL);
return;
}
now = time(NULL);
if (now < last_prune + PURGE_SECS)
return;
lxcfs_debug("%s\n", "Pruning.");
last_prune = now;
threshold = now - 2 * PURGE_SECS;
for (i = 0; i < PIDNS_HASH_SIZE; i++) {
for (prev = NULL, e = pidns_hash_table[i]; e; ) {
if (e->lastcheck < threshold) {
lxcfs_debug("Removing cached entry for %d.\n", e->initpid);
delme = e;
if (prev)
prev->next = e->next;
else
pidns_hash_table[i] = e->next;
e = e->next;
free(delme);
} else {
prev = e;
e = e->next;
}
}
}
}
/* Must be called under store_lock */
static void save_initpid(struct stat *sb, pid_t pid)
{
struct pidns_init_store *e;
char fpath[100];
struct stat procsb;
int h;
lxcfs_debug("Save_initpid: adding entry for %d.\n", pid);
snprintf(fpath, 100, "/proc/%d", pid);
if (stat(fpath, &procsb) < 0)
return;
do {
e = malloc(sizeof(*e));
} while (!e);
e->ino = sb->st_ino;
e->initpid = pid;
e->ctime = procsb.st_ctime;
h = HASH(e->ino);
e->next = pidns_hash_table[h];
e->lastcheck = time(NULL);
pidns_hash_table[h] = e;
}
/*
* Given the stat(2) info for a nsfd pid inode, lookup the init_pid_store
* entry for the inode number and creation time. Verify that the init pid
* is still valid. If not, remove it. Return the entry if valid, NULL
* otherwise.
* Must be called under store_lock
*/
static struct pidns_init_store *lookup_verify_initpid(struct stat *sb)
{
int h = HASH(sb->st_ino);
struct pidns_init_store *e = pidns_hash_table[h];
while (e) {
if (e->ino == sb->st_ino) {
if (initpid_still_valid(e, sb)) {
e->lastcheck = time(NULL);
return e;
}
remove_initpid(e);
return NULL;
}
e = e->next;
}
return NULL;
}
static int is_dir(const char *path, int fd)
{
struct stat statbuf;
int ret = fstatat(fd, path, &statbuf, fd);
if (ret == 0 && S_ISDIR(statbuf.st_mode))
return 1;
return 0;
}
static char *must_copy_string(const char *str)
{
char *dup = NULL;
if (!str)
return NULL;
do {
dup = strdup(str);
} while (!dup);
return dup;
}
static inline void drop_trailing_newlines(char *s)
{
int l;
for (l=strlen(s); l>0 && s[l-1] == '\n'; l--)
s[l-1] = '\0';
}
#define BATCH_SIZE 50
static void dorealloc(char **mem, size_t oldlen, size_t newlen)
{
int newbatches = (newlen / BATCH_SIZE) + 1;
int oldbatches = (oldlen / BATCH_SIZE) + 1;
if (!*mem || newbatches > oldbatches) {
char *tmp;
do {
tmp = realloc(*mem, newbatches * BATCH_SIZE);
} while (!tmp);
*mem = tmp;
}
}
static void append_line(char **contents, size_t *len, char *line, ssize_t linelen)
{
size_t newlen = *len + linelen;
dorealloc(contents, *len, newlen + 1);
memcpy(*contents + *len, line, linelen+1);
*len = newlen;
}
static char *slurp_file(const char *from, int fd)
{
char *line = NULL;
char *contents = NULL;
FILE *f = fdopen(fd, "r");
size_t len = 0, fulllen = 0;
ssize_t linelen;
if (!f)
return NULL;
while ((linelen = getline(&line, &len, f)) != -1) {
append_line(&contents, &fulllen, line, linelen);
}
fclose(f);
if (contents)
drop_trailing_newlines(contents);
free(line);
return contents;
}
static bool write_string(const char *fnam, const char *string, int fd)
{
FILE *f;
size_t len, ret;
if (!(f = fdopen(fd, "w")))
return false;
len = strlen(string);
ret = fwrite(string, 1, len, f);
if (ret != len) {
lxcfs_error("Error writing to file: %s\n", strerror(errno));
fclose(f);
return false;
}
if (fclose(f) < 0) {
lxcfs_error("Error writing to file: %s\n", strerror(errno));
return false;
}
return true;
}
struct cgfs_files {
char *name;
uint32_t uid, gid;
uint32_t mode;
};
#define ALLOC_NUM 20
static bool store_hierarchy(char *stridx, char *h)
{
if (num_hierarchies % ALLOC_NUM == 0) {
size_t n = (num_hierarchies / ALLOC_NUM) + 1;
n *= ALLOC_NUM;
char **tmp = realloc(hierarchies, n * sizeof(char *));
if (!tmp) {
lxcfs_error("%s\n", strerror(errno));
exit(1);
}
hierarchies = tmp;
}
hierarchies[num_hierarchies++] = must_copy_string(h);
return true;
}
static void print_subsystems(void)
{
int i;
fprintf(stderr, "hierarchies:\n");
for (i = 0; i < num_hierarchies; i++) {
if (hierarchies[i])
fprintf(stderr, " %2d: fd: %3d: %s\n", i,
fd_hierarchies[i], hierarchies[i]);
}
}
static bool in_comma_list(const char *needle, const char *haystack)
{
const char *s = haystack, *e;
size_t nlen = strlen(needle);
while (*s && (e = strchr(s, ','))) {
if (nlen != e - s) {
s = e + 1;
continue;
}
if (strncmp(needle, s, nlen) == 0)
return true;
s = e + 1;
}
if (strcmp(needle, s) == 0)
return true;
return false;
}
/* do we need to do any massaging here? I'm not sure... */
/* Return the mounted controller and store the corresponding open file descriptor
* referring to the controller mountpoint in the private lxcfs namespace in
* @cfd.
*/
static char *find_mounted_controller(const char *controller, int *cfd)
{
int i;
for (i = 0; i < num_hierarchies; i++) {
if (!hierarchies[i])
continue;
if (strcmp(hierarchies[i], controller) == 0) {
*cfd = fd_hierarchies[i];
return hierarchies[i];
}
if (in_comma_list(controller, hierarchies[i])) {
*cfd = fd_hierarchies[i];
return hierarchies[i];
}
}
return NULL;
}
bool cgfs_set_value(const char *controller, const char *cgroup, const char *file,
const char *value)
{
int ret, fd, cfd;
size_t len;
char *fnam, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cgroup + / + file + \0
*/
len = strlen(cgroup) + strlen(file) + 3;
fnam = alloca(len);
ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, file);
if (ret < 0 || (size_t)ret >= len)
return false;
fd = openat(cfd, fnam, O_WRONLY);
if (fd < 0)
return false;
return write_string(fnam, value, fd);
}
// Chown all the files in the cgroup directory. We do this when we create
// a cgroup on behalf of a user.
static void chown_all_cgroup_files(const char *dirname, uid_t uid, gid_t gid, int fd)
{
struct dirent *direntp;
char path[MAXPATHLEN];
size_t len;
DIR *d;
int fd1, ret;
len = strlen(dirname);
if (len >= MAXPATHLEN) {
lxcfs_error("Pathname too long: %s\n", dirname);
return;
}
fd1 = openat(fd, dirname, O_DIRECTORY);
if (fd1 < 0)
return;
d = fdopendir(fd1);
if (!d) {
lxcfs_error("Failed to open %s\n", dirname);
return;
}
while ((direntp = readdir(d))) {
if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
continue;
ret = snprintf(path, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
if (ret < 0 || ret >= MAXPATHLEN) {
lxcfs_error("Pathname too long under %s\n", dirname);
continue;
}
if (fchownat(fd, path, uid, gid, 0) < 0)
lxcfs_error("Failed to chown file %s to %u:%u", path, uid, gid);
}
closedir(d);
}
int cgfs_create(const char *controller, const char *cg, uid_t uid, gid_t gid)
{
int cfd;
size_t len;
char *dirnam, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return -EINVAL;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cg + \0
*/
len = strlen(cg) + 2;
dirnam = alloca(len);
snprintf(dirnam, len, "%s%s", *cg == '/' ? "." : "", cg);
if (mkdirat(cfd, dirnam, 0755) < 0)
return -errno;
if (uid == 0 && gid == 0)
return 0;
if (fchownat(cfd, dirnam, uid, gid, 0) < 0)
return -errno;
chown_all_cgroup_files(dirnam, uid, gid, cfd);
return 0;
}
static bool recursive_rmdir(const char *dirname, int fd, const int cfd)
{
struct dirent *direntp;
DIR *dir;
bool ret = false;
char pathname[MAXPATHLEN];
int dupfd;
dupfd = dup(fd); // fdopendir() does bad things once it uses an fd.
if (dupfd < 0)
return false;
dir = fdopendir(dupfd);
if (!dir) {
lxcfs_debug("Failed to open %s: %s.\n", dirname, strerror(errno));
close(dupfd);
return false;
}
while ((direntp = readdir(dir))) {
struct stat mystat;
int rc;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
if (rc < 0 || rc >= MAXPATHLEN) {
lxcfs_error("%s\n", "Pathname too long.");
continue;
}
rc = fstatat(cfd, pathname, &mystat, AT_SYMLINK_NOFOLLOW);
if (rc) {
lxcfs_debug("Failed to stat %s: %s.\n", pathname, strerror(errno));
continue;
}
if (S_ISDIR(mystat.st_mode))
if (!recursive_rmdir(pathname, fd, cfd))
lxcfs_debug("Error removing %s.\n", pathname);
}
ret = true;
if (closedir(dir) < 0) {
lxcfs_error("Failed to close directory %s: %s\n", dirname, strerror(errno));
ret = false;
}
if (unlinkat(cfd, dirname, AT_REMOVEDIR) < 0) {
lxcfs_debug("Failed to delete %s: %s.\n", dirname, strerror(errno));
ret = false;
}
close(dupfd);
return ret;
}
bool cgfs_remove(const char *controller, const char *cg)
{
int fd, cfd;
size_t len;
char *dirnam, *tmpc;
bool bret;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cg + \0
*/
len = strlen(cg) + 2;
dirnam = alloca(len);
snprintf(dirnam, len, "%s%s", *cg == '/' ? "." : "", cg);
fd = openat(cfd, dirnam, O_DIRECTORY);
if (fd < 0)
return false;
bret = recursive_rmdir(dirnam, fd, cfd);
close(fd);
return bret;
}
bool cgfs_chmod_file(const char *controller, const char *file, mode_t mode)
{
int cfd;
size_t len;
char *pathname, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions.
* . + /file + \0
*/
len = strlen(file) + 2;
pathname = alloca(len);
snprintf(pathname, len, "%s%s", *file == '/' ? "." : "", file);
if (fchmodat(cfd, pathname, mode, 0) < 0)
return false;
return true;
}
static int chown_tasks_files(const char *dirname, uid_t uid, gid_t gid, int fd)
{
size_t len;
char *fname;
len = strlen(dirname) + strlen("/cgroup.procs") + 1;
fname = alloca(len);
snprintf(fname, len, "%s/tasks", dirname);
if (fchownat(fd, fname, uid, gid, 0) != 0)
return -errno;
snprintf(fname, len, "%s/cgroup.procs", dirname);
if (fchownat(fd, fname, uid, gid, 0) != 0)
return -errno;
return 0;
}
int cgfs_chown_file(const char *controller, const char *file, uid_t uid, gid_t gid)
{
int cfd;
size_t len;
char *pathname, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return -EINVAL;
/* Make sure we pass a relative path to *at() family of functions.
* . + /file + \0
*/
len = strlen(file) + 2;
pathname = alloca(len);
snprintf(pathname, len, "%s%s", *file == '/' ? "." : "", file);
if (fchownat(cfd, pathname, uid, gid, 0) < 0)
return -errno;
if (is_dir(pathname, cfd))
// like cgmanager did, we want to chown the tasks file as well
return chown_tasks_files(pathname, uid, gid, cfd);
return 0;
}
FILE *open_pids_file(const char *controller, const char *cgroup)
{
int fd, cfd;
size_t len;
char *pathname, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return NULL;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cgroup + / "cgroup.procs" + \0
*/
len = strlen(cgroup) + strlen("cgroup.procs") + 3;
pathname = alloca(len);
snprintf(pathname, len, "%s%s/cgroup.procs", *cgroup == '/' ? "." : "", cgroup);
fd = openat(cfd, pathname, O_WRONLY);
if (fd < 0)
return NULL;
return fdopen(fd, "w");
}
static bool cgfs_iterate_cgroup(const char *controller, const char *cgroup, bool directories,
void ***list, size_t typesize,
void* (*iterator)(const char*, const char*, const char*))
{
int cfd, fd, ret;
size_t len;
char *cg, *tmpc;
char pathname[MAXPATHLEN];
size_t sz = 0, asz = 0;
struct dirent *dirent;
DIR *dir;
tmpc = find_mounted_controller(controller, &cfd);
*list = NULL;
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions. */
len = strlen(cgroup) + 1 /* . */ + 1 /* \0 */;
cg = alloca(len);
ret = snprintf(cg, len, "%s%s", *cgroup == '/' ? "." : "", cgroup);
if (ret < 0 || (size_t)ret >= len) {
lxcfs_error("Pathname too long under %s\n", cgroup);
return false;
}
fd = openat(cfd, cg, O_DIRECTORY);
if (fd < 0)
return false;
dir = fdopendir(fd);
if (!dir)
return false;
while ((dirent = readdir(dir))) {
struct stat mystat;
if (!strcmp(dirent->d_name, ".") ||
!strcmp(dirent->d_name, ".."))
continue;
ret = snprintf(pathname, MAXPATHLEN, "%s/%s", cg, dirent->d_name);
if (ret < 0 || ret >= MAXPATHLEN) {
lxcfs_error("Pathname too long under %s\n", cg);
continue;
}
ret = fstatat(cfd, pathname, &mystat, AT_SYMLINK_NOFOLLOW);
if (ret) {
lxcfs_error("Failed to stat %s: %s\n", pathname, strerror(errno));
continue;
}
if ((!directories && !S_ISREG(mystat.st_mode)) ||
(directories && !S_ISDIR(mystat.st_mode)))
continue;
if (sz+2 >= asz) {
void **tmp;
asz += BATCH_SIZE;
do {
tmp = realloc(*list, asz * typesize);
} while (!tmp);
*list = tmp;
}
(*list)[sz] = (*iterator)(controller, cg, dirent->d_name);
(*list)[sz+1] = NULL;
sz++;
}
if (closedir(dir) < 0) {
lxcfs_error("Failed closedir for %s: %s\n", cgroup, strerror(errno));
return false;
}
return true;
}
static void *make_children_list_entry(const char *controller, const char *cgroup, const char *dir_entry)
{
char *dup;
do {
dup = strdup(dir_entry);
} while (!dup);
return dup;
}
bool cgfs_list_children(const char *controller, const char *cgroup, char ***list)
{
return cgfs_iterate_cgroup(controller, cgroup, true, (void***)list, sizeof(*list), &make_children_list_entry);
}
void free_key(struct cgfs_files *k)
{
if (!k)
return;
free(k->name);
free(k);
}
void free_keys(struct cgfs_files **keys)
{
int i;
if (!keys)
return;
for (i = 0; keys[i]; i++) {
free_key(keys[i]);
}
free(keys);
}
bool cgfs_get_value(const char *controller, const char *cgroup, const char *file, char **value)
{
int ret, fd, cfd;
size_t len;
char *fnam, *tmpc;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cgroup + / + file + \0
*/
len = strlen(cgroup) + strlen(file) + 3;
fnam = alloca(len);
ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, file);
if (ret < 0 || (size_t)ret >= len)
return false;
fd = openat(cfd, fnam, O_RDONLY);
if (fd < 0)
return false;
*value = slurp_file(fnam, fd);
return *value != NULL;
}
struct cgfs_files *cgfs_get_key(const char *controller, const char *cgroup, const char *file)
{
int ret, cfd;
size_t len;
char *fnam, *tmpc;
struct stat sb;
struct cgfs_files *newkey;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
if (file && *file == '/')
file++;
if (file && strchr(file, '/'))
return NULL;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cgroup + / + file + \0
*/
len = strlen(cgroup) + 3;
if (file)
len += strlen(file) + 1;
fnam = alloca(len);
snprintf(fnam, len, "%s%s%s%s", *cgroup == '/' ? "." : "", cgroup,
file ? "/" : "", file ? file : "");
ret = fstatat(cfd, fnam, &sb, 0);
if (ret < 0)
return NULL;
do {
newkey = malloc(sizeof(struct cgfs_files));
} while (!newkey);
if (file)
newkey->name = must_copy_string(file);
else if (strrchr(cgroup, '/'))
newkey->name = must_copy_string(strrchr(cgroup, '/'));
else
newkey->name = must_copy_string(cgroup);
newkey->uid = sb.st_uid;
newkey->gid = sb.st_gid;
newkey->mode = sb.st_mode;
return newkey;
}
static void *make_key_list_entry(const char *controller, const char *cgroup, const char *dir_entry)
{
struct cgfs_files *entry = cgfs_get_key(controller, cgroup, dir_entry);
if (!entry) {
lxcfs_error("Error getting files under %s:%s\n", controller,
cgroup);
}
return entry;
}
bool cgfs_list_keys(const char *controller, const char *cgroup, struct cgfs_files ***keys)
{
return cgfs_iterate_cgroup(controller, cgroup, false, (void***)keys, sizeof(*keys), &make_key_list_entry);
}
bool is_child_cgroup(const char *controller, const char *cgroup, const char *f)
{
int cfd;
size_t len;
char *fnam, *tmpc;
int ret;
struct stat sb;
tmpc = find_mounted_controller(controller, &cfd);
if (!tmpc)
return false;
/* Make sure we pass a relative path to *at() family of functions.
* . + /cgroup + / + f + \0
*/
len = strlen(cgroup) + strlen(f) + 3;
fnam = alloca(len);
ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, f);
if (ret < 0 || (size_t)ret >= len)
return false;
ret = fstatat(cfd, fnam, &sb, 0);
if (ret < 0 || !S_ISDIR(sb.st_mode))
return false;
return true;
}
#define SEND_CREDS_OK 0
#define SEND_CREDS_NOTSK 1
#define SEND_CREDS_FAIL 2
static bool recv_creds(int sock, struct ucred *cred, char *v);
static int wait_for_pid(pid_t pid);
static int send_creds(int sock, struct ucred *cred, char v, bool pingfirst);
static int send_creds_clone_wrapper(void *arg);
/*
* clone a task which switches to @task's namespace and writes '1'.
* over a unix sock so we can read the task's reaper's pid in our
* namespace
*
* Note: glibc's fork() does not respect pidns, which can lead to failed
* assertions inside glibc (and thus failed forks) if the child's pid in
* the pidns and the parent pid outside are identical. Using clone prevents
* this issue.
*/
static void write_task_init_pid_exit(int sock, pid_t target)
{
char fnam[100];
pid_t pid;
int fd, ret;
size_t stack_size = sysconf(_SC_PAGESIZE);
void *stack = alloca(stack_size);
ret = snprintf(fnam, sizeof(fnam), "/proc/%d/ns/pid", (int)target);