3 * Copyright © 2014-2016 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
6 * See COPYING file for details.
9 #define FUSE_USE_VERSION 26
11 #define __STDC_FORMAT_MACROS
28 #include <linux/magic.h>
29 #include <linux/sched.h>
30 #include <sys/epoll.h>
32 #include <sys/mount.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/syscall.h>
36 #include <sys/sysinfo.h>
40 #include "config.h" // for VERSION
42 /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */
43 #define LXCFS_NUMSTRLEN64 21
45 /* Define pivot_root() if missing from the C library */
46 #ifndef HAVE_PIVOT_ROOT
47 static int pivot_root(const char * new_root
, const char * put_old
)
49 #ifdef __NR_pivot_root
50 return syscall(__NR_pivot_root
, new_root
, put_old
);
57 extern int pivot_root(const char * new_root
, const char * put_old
);
63 LXC_TYPE_PROC_MEMINFO
,
64 LXC_TYPE_PROC_CPUINFO
,
67 LXC_TYPE_PROC_DISKSTATS
,
69 LXC_TYPE_PROC_LOADAVG
,
77 char *buf
; // unused as of yet
79 int size
; //actual data size
83 /* The function of hash table.*/
84 #define LOAD_SIZE 100 /*the size of hash_table */
85 #define FLUSH_TIME 5 /*the flush rate */
86 #define DEPTH_DIR 3 /*the depth of per cgroup */
87 /* The function of calculate loadavg .*/
88 #define FSHIFT 11 /* nr of bits of precision */
89 #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
90 #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
91 #define EXP_5 2014 /* 1/exp(5sec/5min) */
92 #define EXP_15 2037 /* 1/exp(5sec/15min) */
93 #define LOAD_INT(x) ((x) >> FSHIFT)
94 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
96 * This parameter is used for proc_loadavg_read().
97 * 1 means use loadavg, 0 means not use.
99 static int loadavg
= 0;
100 static volatile sig_atomic_t loadavg_stop
= 0;
101 static int calc_hash(char *name
)
103 unsigned int hash
= 0;
105 /* ELFHash algorithm. */
107 hash
= (hash
<< 4) + *name
++;
108 x
= hash
& 0xf0000000;
113 return ((hash
& 0x7fffffff) % LOAD_SIZE
);
118 unsigned long avenrun
[3]; /* Load averages */
119 unsigned int run_pid
;
120 unsigned int total_pid
;
121 unsigned int last_pid
;
122 int cfd
; /* The file descriptor of the mounted cgroup */
123 struct load_node
*next
;
124 struct load_node
**pre
;
129 * The lock is about insert load_node and refresh load_node.To the first
130 * load_node of each hash bucket, insert and refresh in this hash bucket is
131 * mutually exclusive.
133 pthread_mutex_t lock
;
135 * The rdlock is about read loadavg and delete load_node.To each hash
136 * bucket, read and delete is mutually exclusive. But at the same time, we
137 * allow paratactic read operation. This rdlock is at list level.
139 pthread_rwlock_t rdlock
;
141 * The rilock is about read loadavg and insert load_node.To the first
142 * load_node of each hash bucket, read and insert is mutually exclusive.
143 * But at the same time, we allow paratactic read operation.
145 pthread_rwlock_t rilock
;
146 struct load_node
*next
;
149 static struct load_head load_hash
[LOAD_SIZE
]; /* hash table */
151 * init_load initialize the hash table.
152 * Return 0 on success, return -1 on failure.
154 static int init_load(void)
159 for (i
= 0; i
< LOAD_SIZE
; i
++) {
160 load_hash
[i
].next
= NULL
;
161 ret
= pthread_mutex_init(&load_hash
[i
].lock
, NULL
);
163 lxcfs_error("%s\n", "Failed to initialize lock");
166 ret
= pthread_rwlock_init(&load_hash
[i
].rdlock
, NULL
);
168 lxcfs_error("%s\n", "Failed to initialize rdlock");
171 ret
= pthread_rwlock_init(&load_hash
[i
].rilock
, NULL
);
173 lxcfs_error("%s\n", "Failed to initialize rilock");
179 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
181 pthread_mutex_destroy(&load_hash
[i
].lock
);
185 pthread_mutex_destroy(&load_hash
[i
].lock
);
186 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
187 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
192 static void insert_node(struct load_node
**n
, int locate
)
196 pthread_mutex_lock(&load_hash
[locate
].lock
);
197 pthread_rwlock_wrlock(&load_hash
[locate
].rilock
);
198 f
= load_hash
[locate
].next
;
199 load_hash
[locate
].next
= *n
;
201 (*n
)->pre
= &(load_hash
[locate
].next
);
203 f
->pre
= &((*n
)->next
);
205 pthread_mutex_unlock(&load_hash
[locate
].lock
);
206 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
209 * locate_node() finds special node. Not return NULL means success.
210 * It should be noted that rdlock isn't unlocked at the end of code
211 * because this function is used to read special node. Delete is not
212 * allowed before read has ended.
213 * unlock rdlock only in proc_loadavg_read().
215 static struct load_node
*locate_node(char *cg
, int locate
)
217 struct load_node
*f
= NULL
;
220 pthread_rwlock_rdlock(&load_hash
[locate
].rilock
);
221 pthread_rwlock_rdlock(&load_hash
[locate
].rdlock
);
222 if (load_hash
[locate
].next
== NULL
) {
223 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
226 f
= load_hash
[locate
].next
;
227 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
228 while (f
&& ((i
= strcmp(f
->cg
, cg
)) != 0))
232 /* Delete the load_node n and return the next node of it. */
233 static struct load_node
*del_node(struct load_node
*n
, int locate
)
237 pthread_rwlock_wrlock(&load_hash
[locate
].rdlock
);
238 if (n
->next
== NULL
) {
242 n
->next
->pre
= n
->pre
;
247 pthread_rwlock_unlock(&load_hash
[locate
].rdlock
);
251 static void load_free(void)
254 struct load_node
*f
, *p
;
256 for (i
= 0; i
< LOAD_SIZE
; i
++) {
257 pthread_mutex_lock(&load_hash
[i
].lock
);
258 pthread_rwlock_wrlock(&load_hash
[i
].rilock
);
259 pthread_rwlock_wrlock(&load_hash
[i
].rdlock
);
260 if (load_hash
[i
].next
== NULL
) {
261 pthread_mutex_unlock(&load_hash
[i
].lock
);
262 pthread_mutex_destroy(&load_hash
[i
].lock
);
263 pthread_rwlock_unlock(&load_hash
[i
].rilock
);
264 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
265 pthread_rwlock_unlock(&load_hash
[i
].rdlock
);
266 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
269 for (f
= load_hash
[i
].next
; f
; ) {
275 pthread_mutex_unlock(&load_hash
[i
].lock
);
276 pthread_mutex_destroy(&load_hash
[i
].lock
);
277 pthread_rwlock_unlock(&load_hash
[i
].rilock
);
278 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
279 pthread_rwlock_unlock(&load_hash
[i
].rdlock
);
280 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
283 /* Reserve buffer size to account for file size changes. */
284 #define BUF_RESERVE_SIZE 512
287 * A table caching which pid is init for a pid namespace.
288 * When looking up which pid is init for $qpid, we first
289 * 1. Stat /proc/$qpid/ns/pid.
290 * 2. Check whether the ino_t is in our store.
291 * a. if not, fork a child in qpid's ns to send us
292 * ucred.pid = 1, and read the initpid. Cache
293 * initpid and creation time for /proc/initpid
294 * in a new store entry.
295 * b. if so, verify that /proc/initpid still matches
296 * what we have saved. If not, clear the store
297 * entry and go back to a. If so, return the
300 struct pidns_init_store
{
301 ino_t ino
; // inode number for /proc/$pid/ns/pid
302 pid_t initpid
; // the pid of nit in that ns
303 long int ctime
; // the time at which /proc/$initpid was created
304 struct pidns_init_store
*next
;
308 /* lol - look at how they are allocated in the kernel */
309 #define PIDNS_HASH_SIZE 4096
310 #define HASH(x) ((x) % PIDNS_HASH_SIZE)
312 static struct pidns_init_store
*pidns_hash_table
[PIDNS_HASH_SIZE
];
313 static pthread_mutex_t pidns_store_mutex
= PTHREAD_MUTEX_INITIALIZER
;
314 static void lock_mutex(pthread_mutex_t
*l
)
318 if ((ret
= pthread_mutex_lock(l
)) != 0) {
319 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
324 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
325 * Number of hierarchies mounted. */
326 static int num_hierarchies
;
328 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
329 * Hierachies mounted {cpuset, blkio, ...}:
330 * Initialized via __constructor__ collect_and_mount_subsystems(). */
331 static char **hierarchies
;
333 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
334 * Open file descriptors:
335 * @fd_hierarchies[i] refers to cgroup @hierarchies[i]. They are mounted in a
336 * private mount namespace.
337 * Initialized via __constructor__ collect_and_mount_subsystems().
338 * @fd_hierarchies[i] can be used to perform file operations on the cgroup
339 * mounts and respective files in the private namespace even when located in
340 * another namespace using the *at() family of functions
341 * {openat(), fchownat(), ...}. */
342 static int *fd_hierarchies
;
343 static int cgroup_mount_ns_fd
= -1;
345 static void unlock_mutex(pthread_mutex_t
*l
)
349 if ((ret
= pthread_mutex_unlock(l
)) != 0) {
350 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
355 static void store_lock(void)
357 lock_mutex(&pidns_store_mutex
);
360 static void store_unlock(void)
362 unlock_mutex(&pidns_store_mutex
);
365 /* Must be called under store_lock */
366 static bool initpid_still_valid(struct pidns_init_store
*e
, struct stat
*nsfdsb
)
371 snprintf(fnam
, 100, "/proc/%d", e
->initpid
);
372 if (stat(fnam
, &initsb
) < 0)
375 lxcfs_debug("Comparing ctime %ld == %ld for pid %d.\n", e
->ctime
,
376 initsb
.st_ctime
, e
->initpid
);
378 if (e
->ctime
!= initsb
.st_ctime
)
383 /* Must be called under store_lock */
384 static void remove_initpid(struct pidns_init_store
*e
)
386 struct pidns_init_store
*tmp
;
389 lxcfs_debug("Remove_initpid: removing entry for %d.\n", e
->initpid
);
392 if (pidns_hash_table
[h
] == e
) {
393 pidns_hash_table
[h
] = e
->next
;
398 tmp
= pidns_hash_table
[h
];
400 if (tmp
->next
== e
) {
410 /* Must be called under store_lock */
411 static void prune_initpid_store(void)
413 static long int last_prune
= 0;
414 struct pidns_init_store
*e
, *prev
, *delme
;
415 long int now
, threshold
;
419 last_prune
= time(NULL
);
423 if (now
< last_prune
+ PURGE_SECS
)
426 lxcfs_debug("%s\n", "Pruning.");
429 threshold
= now
- 2 * PURGE_SECS
;
431 for (i
= 0; i
< PIDNS_HASH_SIZE
; i
++) {
432 for (prev
= NULL
, e
= pidns_hash_table
[i
]; e
; ) {
433 if (e
->lastcheck
< threshold
) {
435 lxcfs_debug("Removing cached entry for %d.\n", e
->initpid
);
439 prev
->next
= e
->next
;
441 pidns_hash_table
[i
] = e
->next
;
452 /* Must be called under store_lock */
453 static void save_initpid(struct stat
*sb
, pid_t pid
)
455 struct pidns_init_store
*e
;
460 lxcfs_debug("Save_initpid: adding entry for %d.\n", pid
);
462 snprintf(fpath
, 100, "/proc/%d", pid
);
463 if (stat(fpath
, &procsb
) < 0)
466 e
= malloc(sizeof(*e
));
470 e
->ctime
= procsb
.st_ctime
;
472 e
->next
= pidns_hash_table
[h
];
473 e
->lastcheck
= time(NULL
);
474 pidns_hash_table
[h
] = e
;
478 * Given the stat(2) info for a nsfd pid inode, lookup the init_pid_store
479 * entry for the inode number and creation time. Verify that the init pid
480 * is still valid. If not, remove it. Return the entry if valid, NULL
482 * Must be called under store_lock
484 static struct pidns_init_store
*lookup_verify_initpid(struct stat
*sb
)
486 int h
= HASH(sb
->st_ino
);
487 struct pidns_init_store
*e
= pidns_hash_table
[h
];
490 if (e
->ino
== sb
->st_ino
) {
491 if (initpid_still_valid(e
, sb
)) {
492 e
->lastcheck
= time(NULL
);
504 static int is_dir(const char *path
, int fd
)
507 int ret
= fstatat(fd
, path
, &statbuf
, fd
);
508 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
513 static char *must_copy_string(const char *str
)
525 static inline void drop_trailing_newlines(char *s
)
529 for (l
=strlen(s
); l
>0 && s
[l
-1] == '\n'; l
--)
533 #define BATCH_SIZE 50
534 static void dorealloc(char **mem
, size_t oldlen
, size_t newlen
)
536 int newbatches
= (newlen
/ BATCH_SIZE
) + 1;
537 int oldbatches
= (oldlen
/ BATCH_SIZE
) + 1;
539 if (!*mem
|| newbatches
> oldbatches
) {
542 tmp
= realloc(*mem
, newbatches
* BATCH_SIZE
);
547 static void append_line(char **contents
, size_t *len
, char *line
, ssize_t linelen
)
549 size_t newlen
= *len
+ linelen
;
550 dorealloc(contents
, *len
, newlen
+ 1);
551 memcpy(*contents
+ *len
, line
, linelen
+1);
555 static char *slurp_file(const char *from
, int fd
)
558 char *contents
= NULL
;
559 FILE *f
= fdopen(fd
, "r");
560 size_t len
= 0, fulllen
= 0;
566 while ((linelen
= getline(&line
, &len
, f
)) != -1) {
567 append_line(&contents
, &fulllen
, line
, linelen
);
572 drop_trailing_newlines(contents
);
577 static bool write_string(const char *fnam
, const char *string
, int fd
)
582 if (!(f
= fdopen(fd
, "w")))
584 len
= strlen(string
);
585 ret
= fwrite(string
, 1, len
, f
);
587 lxcfs_error("Error writing to file: %s\n", strerror(errno
));
592 lxcfs_error("Error writing to file: %s\n", strerror(errno
));
605 static bool store_hierarchy(char *stridx
, char *h
)
607 if (num_hierarchies
% ALLOC_NUM
== 0) {
608 size_t n
= (num_hierarchies
/ ALLOC_NUM
) + 1;
610 char **tmp
= realloc(hierarchies
, n
* sizeof(char *));
612 lxcfs_error("%s\n", strerror(errno
));
618 hierarchies
[num_hierarchies
++] = must_copy_string(h
);
622 static void print_subsystems(void)
626 fprintf(stderr
, "mount namespace: %d\n", cgroup_mount_ns_fd
);
627 fprintf(stderr
, "hierarchies:\n");
628 for (i
= 0; i
< num_hierarchies
; i
++) {
630 fprintf(stderr
, " %2d: fd: %3d: %s\n", i
,
631 fd_hierarchies
[i
], hierarchies
[i
]);
635 static bool in_comma_list(const char *needle
, const char *haystack
)
637 const char *s
= haystack
, *e
;
638 size_t nlen
= strlen(needle
);
640 while (*s
&& (e
= strchr(s
, ','))) {
645 if (strncmp(needle
, s
, nlen
) == 0)
649 if (strcmp(needle
, s
) == 0)
654 /* do we need to do any massaging here? I'm not sure... */
655 /* Return the mounted controller and store the corresponding open file descriptor
656 * referring to the controller mountpoint in the private lxcfs namespace in
659 static char *find_mounted_controller(const char *controller
, int *cfd
)
663 for (i
= 0; i
< num_hierarchies
; i
++) {
666 if (strcmp(hierarchies
[i
], controller
) == 0) {
667 *cfd
= fd_hierarchies
[i
];
668 return hierarchies
[i
];
670 if (in_comma_list(controller
, hierarchies
[i
])) {
671 *cfd
= fd_hierarchies
[i
];
672 return hierarchies
[i
];
679 bool cgfs_set_value(const char *controller
, const char *cgroup
, const char *file
,
686 tmpc
= find_mounted_controller(controller
, &cfd
);
690 /* Make sure we pass a relative path to *at() family of functions.
691 * . + /cgroup + / + file + \0
693 len
= strlen(cgroup
) + strlen(file
) + 3;
695 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
696 if (ret
< 0 || (size_t)ret
>= len
)
699 fd
= openat(cfd
, fnam
, O_WRONLY
);
703 return write_string(fnam
, value
, fd
);
706 // Chown all the files in the cgroup directory. We do this when we create
707 // a cgroup on behalf of a user.
708 static void chown_all_cgroup_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
710 struct dirent
*direntp
;
711 char path
[MAXPATHLEN
];
716 len
= strlen(dirname
);
717 if (len
>= MAXPATHLEN
) {
718 lxcfs_error("Pathname too long: %s\n", dirname
);
722 fd1
= openat(fd
, dirname
, O_DIRECTORY
);
728 lxcfs_error("Failed to open %s\n", dirname
);
732 while ((direntp
= readdir(d
))) {
733 if (!strcmp(direntp
->d_name
, ".") || !strcmp(direntp
->d_name
, ".."))
735 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
736 if (ret
< 0 || ret
>= MAXPATHLEN
) {
737 lxcfs_error("Pathname too long under %s\n", dirname
);
740 if (fchownat(fd
, path
, uid
, gid
, 0) < 0)
741 lxcfs_error("Failed to chown file %s to %u:%u", path
, uid
, gid
);
746 int cgfs_create(const char *controller
, const char *cg
, uid_t uid
, gid_t gid
)
752 tmpc
= find_mounted_controller(controller
, &cfd
);
756 /* Make sure we pass a relative path to *at() family of functions.
759 len
= strlen(cg
) + 2;
760 dirnam
= alloca(len
);
761 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
763 if (mkdirat(cfd
, dirnam
, 0755) < 0)
766 if (uid
== 0 && gid
== 0)
769 if (fchownat(cfd
, dirnam
, uid
, gid
, 0) < 0)
772 chown_all_cgroup_files(dirnam
, uid
, gid
, cfd
);
777 static bool recursive_rmdir(const char *dirname
, int fd
, const int cfd
)
779 struct dirent
*direntp
;
782 char pathname
[MAXPATHLEN
];
785 dupfd
= dup(fd
); // fdopendir() does bad things once it uses an fd.
789 dir
= fdopendir(dupfd
);
791 lxcfs_debug("Failed to open %s: %s.\n", dirname
, strerror(errno
));
796 while ((direntp
= readdir(dir
))) {
800 if (!strcmp(direntp
->d_name
, ".") ||
801 !strcmp(direntp
->d_name
, ".."))
804 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
805 if (rc
< 0 || rc
>= MAXPATHLEN
) {
806 lxcfs_error("%s\n", "Pathname too long.");
810 rc
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
812 lxcfs_debug("Failed to stat %s: %s.\n", pathname
, strerror(errno
));
815 if (S_ISDIR(mystat
.st_mode
))
816 if (!recursive_rmdir(pathname
, fd
, cfd
))
817 lxcfs_debug("Error removing %s.\n", pathname
);
821 if (closedir(dir
) < 0) {
822 lxcfs_error("Failed to close directory %s: %s\n", dirname
, strerror(errno
));
826 if (unlinkat(cfd
, dirname
, AT_REMOVEDIR
) < 0) {
827 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
836 bool cgfs_remove(const char *controller
, const char *cg
)
843 tmpc
= find_mounted_controller(controller
, &cfd
);
847 /* Make sure we pass a relative path to *at() family of functions.
850 len
= strlen(cg
) + 2;
851 dirnam
= alloca(len
);
852 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
854 fd
= openat(cfd
, dirnam
, O_DIRECTORY
);
858 bret
= recursive_rmdir(dirnam
, fd
, cfd
);
863 bool cgfs_chmod_file(const char *controller
, const char *file
, mode_t mode
)
867 char *pathname
, *tmpc
;
869 tmpc
= find_mounted_controller(controller
, &cfd
);
873 /* Make sure we pass a relative path to *at() family of functions.
876 len
= strlen(file
) + 2;
877 pathname
= alloca(len
);
878 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
879 if (fchmodat(cfd
, pathname
, mode
, 0) < 0)
884 static int chown_tasks_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
889 len
= strlen(dirname
) + strlen("/cgroup.procs") + 1;
891 snprintf(fname
, len
, "%s/tasks", dirname
);
892 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
894 snprintf(fname
, len
, "%s/cgroup.procs", dirname
);
895 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
900 int cgfs_chown_file(const char *controller
, const char *file
, uid_t uid
, gid_t gid
)
904 char *pathname
, *tmpc
;
906 tmpc
= find_mounted_controller(controller
, &cfd
);
910 /* Make sure we pass a relative path to *at() family of functions.
913 len
= strlen(file
) + 2;
914 pathname
= alloca(len
);
915 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
916 if (fchownat(cfd
, pathname
, uid
, gid
, 0) < 0)
919 if (is_dir(pathname
, cfd
))
920 // like cgmanager did, we want to chown the tasks file as well
921 return chown_tasks_files(pathname
, uid
, gid
, cfd
);
926 FILE *open_pids_file(const char *controller
, const char *cgroup
)
930 char *pathname
, *tmpc
;
932 tmpc
= find_mounted_controller(controller
, &cfd
);
936 /* Make sure we pass a relative path to *at() family of functions.
937 * . + /cgroup + / "cgroup.procs" + \0
939 len
= strlen(cgroup
) + strlen("cgroup.procs") + 3;
940 pathname
= alloca(len
);
941 snprintf(pathname
, len
, "%s%s/cgroup.procs", *cgroup
== '/' ? "." : "", cgroup
);
943 fd
= openat(cfd
, pathname
, O_WRONLY
);
947 return fdopen(fd
, "w");
950 static bool cgfs_iterate_cgroup(const char *controller
, const char *cgroup
, bool directories
,
951 void ***list
, size_t typesize
,
952 void* (*iterator
)(const char*, const char*, const char*))
957 char pathname
[MAXPATHLEN
];
958 size_t sz
= 0, asz
= 0;
959 struct dirent
*dirent
;
962 tmpc
= find_mounted_controller(controller
, &cfd
);
967 /* Make sure we pass a relative path to *at() family of functions. */
968 len
= strlen(cgroup
) + 1 /* . */ + 1 /* \0 */;
970 ret
= snprintf(cg
, len
, "%s%s", *cgroup
== '/' ? "." : "", cgroup
);
971 if (ret
< 0 || (size_t)ret
>= len
) {
972 lxcfs_error("Pathname too long under %s\n", cgroup
);
976 fd
= openat(cfd
, cg
, O_DIRECTORY
);
984 while ((dirent
= readdir(dir
))) {
987 if (!strcmp(dirent
->d_name
, ".") ||
988 !strcmp(dirent
->d_name
, ".."))
991 ret
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", cg
, dirent
->d_name
);
992 if (ret
< 0 || ret
>= MAXPATHLEN
) {
993 lxcfs_error("Pathname too long under %s\n", cg
);
997 ret
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
999 lxcfs_error("Failed to stat %s: %s\n", pathname
, strerror(errno
));
1002 if ((!directories
&& !S_ISREG(mystat
.st_mode
)) ||
1003 (directories
&& !S_ISDIR(mystat
.st_mode
)))
1010 tmp
= realloc(*list
, asz
* typesize
);
1014 (*list
)[sz
] = (*iterator
)(controller
, cg
, dirent
->d_name
);
1015 (*list
)[sz
+1] = NULL
;
1018 if (closedir(dir
) < 0) {
1019 lxcfs_error("Failed closedir for %s: %s\n", cgroup
, strerror(errno
));
1025 static void *make_children_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
1029 dup
= strdup(dir_entry
);
1034 bool cgfs_list_children(const char *controller
, const char *cgroup
, char ***list
)
1036 return cgfs_iterate_cgroup(controller
, cgroup
, true, (void***)list
, sizeof(*list
), &make_children_list_entry
);
1039 void free_key(struct cgfs_files
*k
)
1047 void free_keys(struct cgfs_files
**keys
)
1053 for (i
= 0; keys
[i
]; i
++) {
1059 bool cgfs_get_value(const char *controller
, const char *cgroup
, const char *file
, char **value
)
1065 tmpc
= find_mounted_controller(controller
, &cfd
);
1069 /* Make sure we pass a relative path to *at() family of functions.
1070 * . + /cgroup + / + file + \0
1072 len
= strlen(cgroup
) + strlen(file
) + 3;
1074 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
1075 if (ret
< 0 || (size_t)ret
>= len
)
1078 fd
= openat(cfd
, fnam
, O_RDONLY
);
1082 *value
= slurp_file(fnam
, fd
);
1083 return *value
!= NULL
;
1086 struct cgfs_files
*cgfs_get_key(const char *controller
, const char *cgroup
, const char *file
)
1092 struct cgfs_files
*newkey
;
1094 tmpc
= find_mounted_controller(controller
, &cfd
);
1098 if (file
&& *file
== '/')
1101 if (file
&& strchr(file
, '/'))
1104 /* Make sure we pass a relative path to *at() family of functions.
1105 * . + /cgroup + / + file + \0
1107 len
= strlen(cgroup
) + 3;
1109 len
+= strlen(file
) + 1;
1111 snprintf(fnam
, len
, "%s%s%s%s", *cgroup
== '/' ? "." : "", cgroup
,
1112 file
? "/" : "", file
? file
: "");
1114 ret
= fstatat(cfd
, fnam
, &sb
, 0);
1119 newkey
= malloc(sizeof(struct cgfs_files
));
1122 newkey
->name
= must_copy_string(file
);
1123 else if (strrchr(cgroup
, '/'))
1124 newkey
->name
= must_copy_string(strrchr(cgroup
, '/'));
1126 newkey
->name
= must_copy_string(cgroup
);
1127 newkey
->uid
= sb
.st_uid
;
1128 newkey
->gid
= sb
.st_gid
;
1129 newkey
->mode
= sb
.st_mode
;
1134 static void *make_key_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
1136 struct cgfs_files
*entry
= cgfs_get_key(controller
, cgroup
, dir_entry
);
1138 lxcfs_error("Error getting files under %s:%s\n", controller
,
1144 bool cgfs_list_keys(const char *controller
, const char *cgroup
, struct cgfs_files
***keys
)
1146 return cgfs_iterate_cgroup(controller
, cgroup
, false, (void***)keys
, sizeof(*keys
), &make_key_list_entry
);
1149 bool is_child_cgroup(const char *controller
, const char *cgroup
, const char *f
)
1157 tmpc
= find_mounted_controller(controller
, &cfd
);
1161 /* Make sure we pass a relative path to *at() family of functions.
1162 * . + /cgroup + / + f + \0
1164 len
= strlen(cgroup
) + strlen(f
) + 3;
1166 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, f
);
1167 if (ret
< 0 || (size_t)ret
>= len
)
1170 ret
= fstatat(cfd
, fnam
, &sb
, 0);
1171 if (ret
< 0 || !S_ISDIR(sb
.st_mode
))
1177 #define SEND_CREDS_OK 0
1178 #define SEND_CREDS_NOTSK 1
1179 #define SEND_CREDS_FAIL 2
1180 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
);
1181 static int wait_for_pid(pid_t pid
);
1182 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
);
1183 static int send_creds_clone_wrapper(void *arg
);
1186 * clone a task which switches to @task's namespace and writes '1'.
1187 * over a unix sock so we can read the task's reaper's pid in our
1190 * Note: glibc's fork() does not respect pidns, which can lead to failed
1191 * assertions inside glibc (and thus failed forks) if the child's pid in
1192 * the pidns and the parent pid outside are identical. Using clone prevents
1195 static void write_task_init_pid_exit(int sock
, pid_t target
)
1200 size_t stack_size
= sysconf(_SC_PAGESIZE
);
1201 void *stack
= alloca(stack_size
);
1203 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", (int)target
);
1204 if (ret
< 0 || ret
>= sizeof(fnam
))
1207 fd
= open(fnam
, O_RDONLY
);
1209 perror("write_task_init_pid_exit open of ns/pid");
1213 perror("write_task_init_pid_exit setns 1");
1217 pid
= clone(send_creds_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &sock
);
1221 if (!wait_for_pid(pid
))
1227 static int send_creds_clone_wrapper(void *arg
) {
1230 int sock
= *(int *)arg
;
1232 /* we are the child */
1237 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
)
1242 static pid_t
get_init_pid_for_task(pid_t task
)
1250 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1251 perror("socketpair");
1260 write_task_init_pid_exit(sock
[0], task
);
1264 if (!recv_creds(sock
[1], &cred
, &v
))
1276 static pid_t
lookup_initpid_in_store(pid_t qpid
)
1280 struct pidns_init_store
*e
;
1283 snprintf(fnam
, 100, "/proc/%d/ns/pid", qpid
);
1285 if (stat(fnam
, &sb
) < 0)
1287 e
= lookup_verify_initpid(&sb
);
1289 answer
= e
->initpid
;
1292 answer
= get_init_pid_for_task(qpid
);
1294 save_initpid(&sb
, answer
);
1297 /* we prune at end in case we are returning
1298 * the value we were about to return */
1299 prune_initpid_store();
1304 static int wait_for_pid(pid_t pid
)
1312 ret
= waitpid(pid
, &status
, 0);
1320 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
1327 * append pid to *src.
1328 * src: a pointer to a char* in which ot append the pid.
1329 * sz: the number of characters printed so far, minus trailing \0.
1330 * asz: the allocated size so far
1331 * pid: the pid to append
1333 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
1337 int tmplen
= sprintf(tmp
, "%d\n", (int)pid
);
1339 if (!*src
|| tmplen
+ *sz
+ 1 >= *asz
) {
1342 tmp
= realloc(*src
, *asz
+ BUF_RESERVE_SIZE
);
1345 *asz
+= BUF_RESERVE_SIZE
;
1347 memcpy((*src
) +*sz
, tmp
, tmplen
+1); /* include the \0 */
1352 * Given a open file * to /proc/pid/{u,g}id_map, and an id
1353 * valid in the caller's namespace, return the id mapped into
1355 * Returns the mapped id, or -1 on error.
1358 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
1360 unsigned int nsuid
, // base id for a range in the idfile's namespace
1361 hostuid
, // base id for a range in the caller's namespace
1362 count
; // number of ids in this range
1366 fseek(idfile
, 0L, SEEK_SET
);
1367 while (fgets(line
, 400, idfile
)) {
1368 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
1371 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
1373 * uids wrapped around - unexpected as this is a procfile,
1376 lxcfs_error("pid wrapparound at entry %u %u %u in %s\n",
1377 nsuid
, hostuid
, count
, line
);
1380 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
1382 * now since hostuid <= in_id < hostuid+count, and
1383 * hostuid+count and nsuid+count do not wrap around,
1384 * we know that nsuid+(in_id-hostuid) which must be
1385 * less that nsuid+(count) must not wrap around
1387 return (in_id
- hostuid
) + nsuid
;
1396 * for is_privileged_over,
1397 * specify whether we require the calling uid to be root in his
1400 #define NS_ROOT_REQD true
1401 #define NS_ROOT_OPT false
1405 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
1407 char fpath
[PROCLEN
];
1409 bool answer
= false;
1412 if (victim
== -1 || uid
== -1)
1416 * If the request is one not requiring root in the namespace,
1417 * then having the same uid suffices. (i.e. uid 1000 has write
1418 * access to files owned by uid 1000
1420 if (!req_ns_root
&& uid
== victim
)
1423 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
1424 if (ret
< 0 || ret
>= PROCLEN
)
1426 FILE *f
= fopen(fpath
, "r");
1430 /* if caller's not root in his namespace, reject */
1431 nsuid
= convert_id_to_ns(f
, uid
);
1436 * If victim is not mapped into caller's ns, reject.
1437 * XXX I'm not sure this check is needed given that fuse
1438 * will be sending requests where the vfs has converted
1440 nsuid
= convert_id_to_ns(f
, victim
);
1451 static bool perms_include(int fmode
, mode_t req_mode
)
1455 switch (req_mode
& O_ACCMODE
) {
1463 r
= S_IROTH
| S_IWOTH
;
1468 return ((fmode
& r
) == r
);
1474 * querycg is /a/b/c/d/e
1477 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
1481 if (strlen(taskcg
) <= strlen(querycg
)) {
1482 lxcfs_error("%s\n", "I was fed bad input.");
1486 if ((strcmp(querycg
, "/") == 0) || (strcmp(querycg
, "./") == 0))
1487 start
= strdup(taskcg
+ 1);
1489 start
= strdup(taskcg
+ strlen(querycg
) + 1);
1492 end
= strchr(start
, '/');
1498 static void stripnewline(char *x
)
1500 size_t l
= strlen(x
);
1501 if (l
&& x
[l
-1] == '\n')
1505 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1510 char *answer
= NULL
;
1514 const char *h
= find_mounted_controller(contrl
, &cfd
);
1518 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
1519 if (ret
< 0 || ret
>= PROCLEN
)
1521 if (!(f
= fopen(fnam
, "r")))
1524 while (getline(&line
, &len
, f
) != -1) {
1528 c1
= strchr(line
, ':');
1532 c2
= strchr(c1
, ':');
1536 if (strcmp(c1
, h
) != 0)
1541 answer
= strdup(c2
);
1553 * check whether a fuse context may access a cgroup dir or file
1555 * If file is not null, it is a cgroup file to check under cg.
1556 * If file is null, then we are checking perms on cg itself.
1558 * For files we can check the mode of the list_keys result.
1559 * For cgroups, we must make assumptions based on the files under the
1560 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
1563 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
1565 struct cgfs_files
*k
= NULL
;
1568 k
= cgfs_get_key(contrl
, cg
, file
);
1572 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1573 if (perms_include(k
->mode
>> 6, mode
)) {
1578 if (fc
->gid
== k
->gid
) {
1579 if (perms_include(k
->mode
>> 3, mode
)) {
1584 ret
= perms_include(k
->mode
, mode
);
1591 #define INITSCOPE "/init.scope"
1592 static void prune_init_slice(char *cg
)
1595 size_t cg_len
= strlen(cg
), initscope_len
= strlen(INITSCOPE
);
1597 if (cg_len
< initscope_len
)
1600 point
= cg
+ cg_len
- initscope_len
;
1601 if (strcmp(point
, INITSCOPE
) == 0) {
1610 * If pid is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
1611 * If pid is in /a, he may act on /a/b, but not on /b.
1612 * if the answer is false and nextcg is not NULL, then *nextcg will point
1613 * to a string containing the next cgroup directory under cg, which must be
1614 * freed by the caller.
1616 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
1618 bool answer
= false;
1619 char *c2
= get_pid_cgroup(pid
, contrl
);
1624 prune_init_slice(c2
);
1627 * callers pass in '/' or './' (openat()) for root cgroup, otherwise
1628 * they pass in a cgroup without leading '/'
1630 * The original line here was:
1631 * linecmp = *cg == '/' ? c2 : c2+1;
1632 * TODO: I'm not sure why you'd want to increment when *cg != '/'?
1633 * Serge, do you know?
1635 if (*cg
== '/' || !strncmp(cg
, "./", 2))
1639 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
1641 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
1653 * If pid is in /a/b/c, he may see that /a exists, but not /b or /a/c.
1655 static bool caller_may_see_dir(pid_t pid
, const char *contrl
, const char *cg
)
1657 bool answer
= false;
1659 size_t target_len
, task_len
;
1661 if (strcmp(cg
, "/") == 0 || strcmp(cg
, "./") == 0)
1664 c2
= get_pid_cgroup(pid
, contrl
);
1667 prune_init_slice(c2
);
1670 target_len
= strlen(cg
);
1671 task_len
= strlen(task_cg
);
1672 if (task_len
== 0) {
1673 /* Task is in the root cg, it can see everything. This case is
1674 * not handled by the strmcps below, since they test for the
1675 * last /, but that is the first / that we've chopped off
1681 if (strcmp(cg
, task_cg
) == 0) {
1685 if (target_len
< task_len
) {
1686 /* looking up a parent dir */
1687 if (strncmp(task_cg
, cg
, target_len
) == 0 && task_cg
[target_len
] == '/')
1691 if (target_len
> task_len
) {
1692 /* looking up a child dir */
1693 if (strncmp(task_cg
, cg
, task_len
) == 0 && cg
[task_len
] == '/')
1704 * given /cgroup/freezer/a/b, return "freezer".
1705 * the returned char* should NOT be freed.
1707 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
1710 char *contr
, *slash
;
1712 if (strlen(path
) < 9) {
1716 if (*(path
+ 7) != '/') {
1721 contr
= strdupa(p1
);
1726 slash
= strstr(contr
, "/");
1731 for (i
= 0; i
< num_hierarchies
; i
++) {
1732 if (hierarchies
[i
] && strcmp(hierarchies
[i
], contr
) == 0)
1733 return hierarchies
[i
];
1740 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
1741 * Note that the returned value may include files (keynames) etc
1743 static const char *find_cgroup_in_path(const char *path
)
1747 if (strlen(path
) < 9) {
1751 p1
= strstr(path
+ 8, "/");
1761 * split the last path element from the path in @cg.
1762 * @dir is newly allocated and should be freed, @last not
1764 static void get_cgdir_and_path(const char *cg
, char **dir
, char **last
)
1771 *last
= strrchr(cg
, '/');
1776 p
= strrchr(*dir
, '/');
1781 * FUSE ops for /cgroup
1784 int cg_getattr(const char *path
, struct stat
*sb
)
1786 struct timespec now
;
1787 struct fuse_context
*fc
= fuse_get_context();
1788 char * cgdir
= NULL
;
1789 char *last
= NULL
, *path1
, *path2
;
1790 struct cgfs_files
*k
= NULL
;
1792 const char *controller
= NULL
;
1799 memset(sb
, 0, sizeof(struct stat
));
1801 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1804 sb
->st_uid
= sb
->st_gid
= 0;
1805 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1808 if (strcmp(path
, "/cgroup") == 0) {
1809 sb
->st_mode
= S_IFDIR
| 00755;
1814 controller
= pick_controller_from_path(fc
, path
);
1817 cgroup
= find_cgroup_in_path(path
);
1819 /* this is just /cgroup/controller, return it as a dir */
1820 sb
->st_mode
= S_IFDIR
| 00755;
1825 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1835 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1838 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
1839 * Then check that caller's cgroup is under path if last is a child
1840 * cgroup, or cgdir if last is a file */
1842 if (is_child_cgroup(controller
, path1
, path2
)) {
1843 if (!caller_may_see_dir(initpid
, controller
, cgroup
)) {
1847 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
1848 /* this is just /cgroup/controller, return it as a dir */
1849 sb
->st_mode
= S_IFDIR
| 00555;
1854 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
1859 // get uid, gid, from '/tasks' file and make up a mode
1860 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1861 sb
->st_mode
= S_IFDIR
| 00755;
1862 k
= cgfs_get_key(controller
, cgroup
, NULL
);
1864 sb
->st_uid
= sb
->st_gid
= 0;
1866 sb
->st_uid
= k
->uid
;
1867 sb
->st_gid
= k
->gid
;
1875 if ((k
= cgfs_get_key(controller
, path1
, path2
)) != NULL
) {
1876 sb
->st_mode
= S_IFREG
| k
->mode
;
1878 sb
->st_uid
= k
->uid
;
1879 sb
->st_gid
= k
->gid
;
1882 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
1894 int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
1896 struct fuse_context
*fc
= fuse_get_context();
1898 struct file_info
*dir_info
;
1899 char *controller
= NULL
;
1904 if (strcmp(path
, "/cgroup") == 0) {
1908 // return list of keys for the controller, and list of child cgroups
1909 controller
= pick_controller_from_path(fc
, path
);
1913 cgroup
= find_cgroup_in_path(path
);
1915 /* this is just /cgroup/controller, return its contents */
1920 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1924 if (!caller_may_see_dir(initpid
, controller
, cgroup
))
1926 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
1930 /* we'll free this at cg_releasedir */
1931 dir_info
= malloc(sizeof(*dir_info
));
1934 dir_info
->controller
= must_copy_string(controller
);
1935 dir_info
->cgroup
= must_copy_string(cgroup
);
1936 dir_info
->type
= LXC_TYPE_CGDIR
;
1937 dir_info
->buf
= NULL
;
1938 dir_info
->file
= NULL
;
1939 dir_info
->buflen
= 0;
1941 fi
->fh
= (unsigned long)dir_info
;
1945 int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1946 struct fuse_file_info
*fi
)
1948 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1949 struct cgfs_files
**list
= NULL
;
1951 char *nextcg
= NULL
;
1952 struct fuse_context
*fc
= fuse_get_context();
1953 char **clist
= NULL
;
1955 if (filler(buf
, ".", NULL
, 0) != 0 || filler(buf
, "..", NULL
, 0) != 0)
1958 if (d
->type
!= LXC_TYPE_CGDIR
) {
1959 lxcfs_error("%s\n", "Internal error: file cache info used in readdir.");
1962 if (!d
->cgroup
&& !d
->controller
) {
1963 // ls /var/lib/lxcfs/cgroup - just show list of controllers
1966 for (i
= 0; i
< num_hierarchies
; i
++) {
1967 if (hierarchies
[i
] && filler(buf
, hierarchies
[i
], NULL
, 0) != 0) {
1974 if (!cgfs_list_keys(d
->controller
, d
->cgroup
, &list
)) {
1975 // not a valid cgroup
1980 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1983 if (!caller_is_in_ancestor(initpid
, d
->controller
, d
->cgroup
, &nextcg
)) {
1985 ret
= filler(buf
, nextcg
, NULL
, 0);
1996 for (i
= 0; list
[i
]; i
++) {
1997 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
2003 // now get the list of child cgroups
2005 if (!cgfs_list_children(d
->controller
, d
->cgroup
, &clist
)) {
2010 for (i
= 0; clist
[i
]; i
++) {
2011 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
2022 for (i
= 0; clist
[i
]; i
++)
2029 static void do_release_file_info(struct fuse_file_info
*fi
)
2031 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2038 free(f
->controller
);
2039 f
->controller
= NULL
;
2049 int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
2051 do_release_file_info(fi
);
2055 int cg_open(const char *path
, struct fuse_file_info
*fi
)
2058 char *last
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
2059 struct cgfs_files
*k
= NULL
;
2060 struct file_info
*file_info
;
2061 struct fuse_context
*fc
= fuse_get_context();
2067 controller
= pick_controller_from_path(fc
, path
);
2070 cgroup
= find_cgroup_in_path(path
);
2074 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2083 k
= cgfs_get_key(controller
, path1
, path2
);
2090 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2093 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
2097 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
2102 /* we'll free this at cg_release */
2103 file_info
= malloc(sizeof(*file_info
));
2108 file_info
->controller
= must_copy_string(controller
);
2109 file_info
->cgroup
= must_copy_string(path1
);
2110 file_info
->file
= must_copy_string(path2
);
2111 file_info
->type
= LXC_TYPE_CGFILE
;
2112 file_info
->buf
= NULL
;
2113 file_info
->buflen
= 0;
2115 fi
->fh
= (unsigned long)file_info
;
2123 int cg_access(const char *path
, int mode
)
2127 char *path1
, *path2
, *controller
;
2128 char *last
= NULL
, *cgdir
= NULL
;
2129 struct cgfs_files
*k
= NULL
;
2130 struct fuse_context
*fc
= fuse_get_context();
2132 if (strcmp(path
, "/cgroup") == 0)
2138 controller
= pick_controller_from_path(fc
, path
);
2141 cgroup
= find_cgroup_in_path(path
);
2143 // access("/sys/fs/cgroup/systemd", mode) - rx allowed, w not
2144 if ((mode
& W_OK
) == 0)
2149 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2158 k
= cgfs_get_key(controller
, path1
, path2
);
2160 if ((mode
& W_OK
) == 0)
2168 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2171 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
2175 if (!fc_may_access(fc
, controller
, path1
, path2
, mode
)) {
2187 int cg_release(const char *path
, struct fuse_file_info
*fi
)
2189 do_release_file_info(fi
);
2193 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
2195 static bool wait_for_sock(int sock
, int timeout
)
2197 struct epoll_event ev
;
2198 int epfd
, ret
, now
, starttime
, deltatime
, saved_errno
;
2200 if ((starttime
= time(NULL
)) < 0)
2203 if ((epfd
= epoll_create(1)) < 0) {
2204 lxcfs_error("%s\n", "Failed to create epoll socket: %m.");
2208 ev
.events
= POLLIN_SET
;
2210 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, sock
, &ev
) < 0) {
2211 lxcfs_error("%s\n", "Failed adding socket to epoll: %m.");
2217 if ((now
= time(NULL
)) < 0) {
2222 deltatime
= (starttime
+ timeout
) - now
;
2223 if (deltatime
< 0) { // timeout
2228 ret
= epoll_wait(epfd
, &ev
, 1, 1000*deltatime
+ 1);
2229 if (ret
< 0 && errno
== EINTR
)
2231 saved_errno
= errno
;
2235 errno
= saved_errno
;
2241 static int msgrecv(int sockfd
, void *buf
, size_t len
)
2243 if (!wait_for_sock(sockfd
, 2))
2245 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
2248 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
2250 struct msghdr msg
= { 0 };
2252 struct cmsghdr
*cmsg
;
2253 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2258 if (msgrecv(sock
, buf
, 1) != 1) {
2259 lxcfs_error("%s\n", "Error getting reply from server over socketpair.");
2260 return SEND_CREDS_FAIL
;
2264 msg
.msg_control
= cmsgbuf
;
2265 msg
.msg_controllen
= sizeof(cmsgbuf
);
2267 cmsg
= CMSG_FIRSTHDR(&msg
);
2268 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
2269 cmsg
->cmsg_level
= SOL_SOCKET
;
2270 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
2271 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
2273 msg
.msg_name
= NULL
;
2274 msg
.msg_namelen
= 0;
2278 iov
.iov_len
= sizeof(buf
);
2282 if (sendmsg(sock
, &msg
, 0) < 0) {
2283 lxcfs_error("Failed at sendmsg: %s.\n",strerror(errno
));
2285 return SEND_CREDS_NOTSK
;
2286 return SEND_CREDS_FAIL
;
2289 return SEND_CREDS_OK
;
2292 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
2294 struct msghdr msg
= { 0 };
2296 struct cmsghdr
*cmsg
;
2297 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2308 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
2309 lxcfs_error("Failed to set passcred: %s\n", strerror(errno
));
2313 if (write(sock
, buf
, 1) != 1) {
2314 lxcfs_error("Failed to start write on scm fd: %s\n", strerror(errno
));
2318 msg
.msg_name
= NULL
;
2319 msg
.msg_namelen
= 0;
2320 msg
.msg_control
= cmsgbuf
;
2321 msg
.msg_controllen
= sizeof(cmsgbuf
);
2324 iov
.iov_len
= sizeof(buf
);
2328 if (!wait_for_sock(sock
, 2)) {
2329 lxcfs_error("Timed out waiting for scm_cred: %s\n", strerror(errno
));
2332 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
2334 lxcfs_error("Failed to receive scm_cred: %s\n", strerror(errno
));
2338 cmsg
= CMSG_FIRSTHDR(&msg
);
2340 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
2341 cmsg
->cmsg_level
== SOL_SOCKET
&&
2342 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
2343 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
2350 struct pid_ns_clone_args
{
2354 int (*wrapped
) (int, pid_t
); // pid_from_ns or pid_to_ns
2358 * pid_ns_clone_wrapper - wraps pid_to_ns or pid_from_ns for usage
2359 * with clone(). This simply writes '1' as ACK back to the parent
2360 * before calling the actual wrapped function.
2362 static int pid_ns_clone_wrapper(void *arg
) {
2363 struct pid_ns_clone_args
* args
= (struct pid_ns_clone_args
*) arg
;
2366 close(args
->cpipe
[0]);
2367 if (write(args
->cpipe
[1], &b
, sizeof(char)) < 0)
2368 lxcfs_error("(child): error on write: %s.\n", strerror(errno
));
2369 close(args
->cpipe
[1]);
2370 return args
->wrapped(args
->sock
, args
->tpid
);
2374 * pid_to_ns - reads pids from a ucred over a socket, then writes the
2375 * int value back over the socket. This shifts the pid from the
2376 * sender's pidns into tpid's pidns.
2378 static int pid_to_ns(int sock
, pid_t tpid
)
2383 while (recv_creds(sock
, &cred
, &v
)) {
2386 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
2394 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
2395 * in your old pidns. Only children which you clone will be in the target
2396 * pidns. So the pid_to_ns_wrapper does the setns, then clones a child to
2397 * actually convert pids.
2399 * Note: glibc's fork() does not respect pidns, which can lead to failed
2400 * assertions inside glibc (and thus failed forks) if the child's pid in
2401 * the pidns and the parent pid outside are identical. Using clone prevents
2404 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
2406 int newnsfd
= -1, ret
, cpipe
[2];
2411 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2412 if (ret
< 0 || ret
>= sizeof(fnam
))
2414 newnsfd
= open(fnam
, O_RDONLY
);
2417 if (setns(newnsfd
, 0) < 0)
2421 if (pipe(cpipe
) < 0)
2424 struct pid_ns_clone_args args
= {
2428 .wrapped
= &pid_to_ns
2430 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2431 void *stack
= alloca(stack_size
);
2433 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2437 // give the child 1 second to be done forking and
2439 if (!wait_for_sock(cpipe
[0], 1))
2441 ret
= read(cpipe
[0], &v
, 1);
2442 if (ret
!= sizeof(char) || v
!= '1')
2445 if (!wait_for_pid(cpid
))
2451 * To read cgroup files with a particular pid, we will setns into the child
2452 * pidns, open a pipe, fork a child - which will be the first to really be in
2453 * the child ns - which does the cgfs_get_value and writes the data to the pipe.
2455 bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
2457 int sock
[2] = {-1, -1};
2458 char *tmpdata
= NULL
;
2460 pid_t qpid
, cpid
= -1;
2461 bool answer
= false;
2464 size_t sz
= 0, asz
= 0;
2466 if (!cgfs_get_value(contrl
, cg
, file
, &tmpdata
))
2470 * Now we read the pids from returned data one by one, pass
2471 * them into a child in the target namespace, read back the
2472 * translated pids, and put them into our to-return data
2475 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2476 perror("socketpair");
2485 if (!cpid
) // child - exits when done
2486 pid_to_ns_wrapper(sock
[1], tpid
);
2488 char *ptr
= tmpdata
;
2491 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
2493 ret
= send_creds(sock
[0], &cred
, v
, true);
2495 if (ret
== SEND_CREDS_NOTSK
)
2497 if (ret
== SEND_CREDS_FAIL
)
2500 // read converted results
2501 if (!wait_for_sock(sock
[0], 2)) {
2502 lxcfs_error("Timed out waiting for pid from child: %s.\n", strerror(errno
));
2505 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2506 lxcfs_error("Error reading pid from child: %s.\n", strerror(errno
));
2509 must_strcat_pid(d
, &sz
, &asz
, qpid
);
2511 ptr
= strchr(ptr
, '\n');
2517 cred
.pid
= getpid();
2519 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
2520 // failed to ask child to exit
2521 lxcfs_error("Failed to ask child to exit: %s.\n", strerror(errno
));
2531 if (sock
[0] != -1) {
2538 int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2539 struct fuse_file_info
*fi
)
2541 struct fuse_context
*fc
= fuse_get_context();
2542 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2543 struct cgfs_files
*k
= NULL
;
2548 if (f
->type
!= LXC_TYPE_CGFILE
) {
2549 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_read.");
2562 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2568 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) {
2573 if (strcmp(f
->file
, "tasks") == 0 ||
2574 strcmp(f
->file
, "/tasks") == 0 ||
2575 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2576 strcmp(f
->file
, "cgroup.procs") == 0)
2577 // special case - we have to translate the pids
2578 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
2580 r
= cgfs_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
2594 memcpy(buf
, data
, s
);
2595 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
2605 static int pid_from_ns(int sock
, pid_t tpid
)
2615 if (!wait_for_sock(sock
, 2)) {
2616 lxcfs_error("%s\n", "Timeout reading from parent.");
2619 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
2620 lxcfs_error("Bad read from parent: %s.\n", strerror(errno
));
2623 if (vpid
== -1) // done
2627 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
2629 cred
.pid
= getpid();
2630 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
2637 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
2639 int newnsfd
= -1, ret
, cpipe
[2];
2644 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2645 if (ret
< 0 || ret
>= sizeof(fnam
))
2647 newnsfd
= open(fnam
, O_RDONLY
);
2650 if (setns(newnsfd
, 0) < 0)
2654 if (pipe(cpipe
) < 0)
2657 struct pid_ns_clone_args args
= {
2661 .wrapped
= &pid_from_ns
2663 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2664 void *stack
= alloca(stack_size
);
2666 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2670 // give the child 1 second to be done forking and
2672 if (!wait_for_sock(cpipe
[0], 1))
2674 ret
= read(cpipe
[0], &v
, 1);
2675 if (ret
!= sizeof(char) || v
!= '1')
2678 if (!wait_for_pid(cpid
))
2684 * Given host @uid, return the uid to which it maps in
2685 * @pid's user namespace, or -1 if none.
2687 bool hostuid_to_ns(uid_t uid
, pid_t pid
, uid_t
*answer
)
2692 sprintf(line
, "/proc/%d/uid_map", pid
);
2693 if ((f
= fopen(line
, "r")) == NULL
) {
2697 *answer
= convert_id_to_ns(f
, uid
);
2706 * get_pid_creds: get the real uid and gid of @pid from
2708 * (XXX should we use euid here?)
2710 void get_pid_creds(pid_t pid
, uid_t
*uid
, gid_t
*gid
)
2719 sprintf(line
, "/proc/%d/status", pid
);
2720 if ((f
= fopen(line
, "r")) == NULL
) {
2721 lxcfs_error("Error opening %s: %s\n", line
, strerror(errno
));
2724 while (fgets(line
, 400, f
)) {
2725 if (strncmp(line
, "Uid:", 4) == 0) {
2726 if (sscanf(line
+4, "%u", &u
) != 1) {
2727 lxcfs_error("bad uid line for pid %u\n", pid
);
2732 } else if (strncmp(line
, "Gid:", 4) == 0) {
2733 if (sscanf(line
+4, "%u", &g
) != 1) {
2734 lxcfs_error("bad gid line for pid %u\n", pid
);
2745 * May the requestor @r move victim @v to a new cgroup?
2746 * This is allowed if
2747 * . they are the same task
2748 * . they are ownedy by the same uid
2749 * . @r is root on the host, or
2750 * . @v's uid is mapped into @r's where @r is root.
2752 bool may_move_pid(pid_t r
, uid_t r_uid
, pid_t v
)
2754 uid_t v_uid
, tmpuid
;
2761 get_pid_creds(v
, &v_uid
, &v_gid
);
2764 if (hostuid_to_ns(r_uid
, r
, &tmpuid
) && tmpuid
== 0
2765 && hostuid_to_ns(v_uid
, r
, &tmpuid
))
2770 static bool do_write_pids(pid_t tpid
, uid_t tuid
, const char *contrl
, const char *cg
,
2771 const char *file
, const char *buf
)
2773 int sock
[2] = {-1, -1};
2774 pid_t qpid
, cpid
= -1;
2775 FILE *pids_file
= NULL
;
2776 bool answer
= false, fail
= false;
2778 pids_file
= open_pids_file(contrl
, cg
);
2783 * write the pids to a socket, have helper in writer's pidns
2784 * call movepid for us
2786 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2787 perror("socketpair");
2795 if (!cpid
) { // child
2797 pid_from_ns_wrapper(sock
[1], tpid
);
2800 const char *ptr
= buf
;
2801 while (sscanf(ptr
, "%d", &qpid
) == 1) {
2805 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2806 lxcfs_error("Error writing pid to child: %s.\n", strerror(errno
));
2810 if (recv_creds(sock
[0], &cred
, &v
)) {
2812 if (!may_move_pid(tpid
, tuid
, cred
.pid
)) {
2816 if (fprintf(pids_file
, "%d", (int) cred
.pid
) < 0)
2821 ptr
= strchr(ptr
, '\n');
2827 /* All good, write the value */
2829 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
2830 lxcfs_error("%s\n", "Warning: failed to ask child to exit.");
2838 if (sock
[0] != -1) {
2843 if (fclose(pids_file
) != 0)
2849 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2850 struct fuse_file_info
*fi
)
2852 struct fuse_context
*fc
= fuse_get_context();
2853 char *localbuf
= NULL
;
2854 struct cgfs_files
*k
= NULL
;
2855 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2858 if (f
->type
!= LXC_TYPE_CGFILE
) {
2859 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_write.");
2869 localbuf
= alloca(size
+1);
2870 localbuf
[size
] = '\0';
2871 memcpy(localbuf
, buf
, size
);
2873 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2878 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
2883 if (strcmp(f
->file
, "tasks") == 0 ||
2884 strcmp(f
->file
, "/tasks") == 0 ||
2885 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2886 strcmp(f
->file
, "cgroup.procs") == 0)
2887 // special case - we have to translate the pids
2888 r
= do_write_pids(fc
->pid
, fc
->uid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2890 r
= cgfs_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2900 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
2902 struct fuse_context
*fc
= fuse_get_context();
2903 char *cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2904 struct cgfs_files
*k
= NULL
;
2911 if (strcmp(path
, "/cgroup") == 0)
2914 controller
= pick_controller_from_path(fc
, path
);
2916 return errno
== ENOENT
? -EPERM
: -errno
;
2918 cgroup
= find_cgroup_in_path(path
);
2920 /* this is just /cgroup/controller */
2923 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2933 if (is_child_cgroup(controller
, path1
, path2
)) {
2934 // get uid, gid, from '/tasks' file and make up a mode
2935 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2936 k
= cgfs_get_key(controller
, cgroup
, "tasks");
2939 k
= cgfs_get_key(controller
, path1
, path2
);
2947 * This being a fuse request, the uid and gid must be valid
2948 * in the caller's namespace. So we can just check to make
2949 * sure that the caller is root in his uid, and privileged
2950 * over the file's current owner.
2952 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
2957 ret
= cgfs_chown_file(controller
, cgroup
, uid
, gid
);
2966 int cg_chmod(const char *path
, mode_t mode
)
2968 struct fuse_context
*fc
= fuse_get_context();
2969 char * cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2970 struct cgfs_files
*k
= NULL
;
2977 if (strcmp(path
, "/cgroup") == 0)
2980 controller
= pick_controller_from_path(fc
, path
);
2982 return errno
== ENOENT
? -EPERM
: -errno
;
2984 cgroup
= find_cgroup_in_path(path
);
2986 /* this is just /cgroup/controller */
2989 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2999 if (is_child_cgroup(controller
, path1
, path2
)) {
3000 // get uid, gid, from '/tasks' file and make up a mode
3001 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
3002 k
= cgfs_get_key(controller
, cgroup
, "tasks");
3005 k
= cgfs_get_key(controller
, path1
, path2
);
3013 * This being a fuse request, the uid and gid must be valid
3014 * in the caller's namespace. So we can just check to make
3015 * sure that the caller is root in his uid, and privileged
3016 * over the file's current owner.
3018 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
3023 if (!cgfs_chmod_file(controller
, cgroup
, mode
)) {
3035 int cg_mkdir(const char *path
, mode_t mode
)
3037 struct fuse_context
*fc
= fuse_get_context();
3038 char *last
= NULL
, *path1
, *cgdir
= NULL
, *controller
, *next
= NULL
;
3045 controller
= pick_controller_from_path(fc
, path
);
3047 return errno
== ENOENT
? -EPERM
: -errno
;
3049 cgroup
= find_cgroup_in_path(path
);
3053 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3059 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3062 if (!caller_is_in_ancestor(initpid
, controller
, path1
, &next
)) {
3065 else if (last
&& strcmp(next
, last
) == 0)
3072 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
3076 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
3081 ret
= cgfs_create(controller
, cgroup
, fc
->uid
, fc
->gid
);
3089 int cg_rmdir(const char *path
)
3091 struct fuse_context
*fc
= fuse_get_context();
3092 char *last
= NULL
, *cgdir
= NULL
, *controller
, *next
= NULL
;
3099 controller
= pick_controller_from_path(fc
, path
);
3100 if (!controller
) /* Someone's trying to delete "/cgroup". */
3103 cgroup
= find_cgroup_in_path(path
);
3104 if (!cgroup
) /* Someone's trying to delete a controller e.g. "/blkio". */
3107 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3109 /* Someone's trying to delete a cgroup on the same level as the
3110 * "/lxc" cgroup e.g. rmdir "/cgroup/blkio/lxc" or
3111 * rmdir "/cgroup/blkio/init.slice".
3117 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3120 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, &next
)) {
3121 if (!last
|| (next
&& (strcmp(next
, last
) == 0)))
3128 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
3132 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
3137 if (!cgfs_remove(controller
, cgroup
)) {
3150 static bool startswith(const char *line
, const char *pref
)
3152 if (strncmp(line
, pref
, strlen(pref
)) == 0)
3157 static void parse_memstat(char *memstat
, unsigned long *cached
,
3158 unsigned long *active_anon
, unsigned long *inactive_anon
,
3159 unsigned long *active_file
, unsigned long *inactive_file
,
3160 unsigned long *unevictable
)
3165 if (startswith(memstat
, "total_cache")) {
3166 sscanf(memstat
+ 11, "%lu", cached
);
3168 } else if (startswith(memstat
, "total_active_anon")) {
3169 sscanf(memstat
+ 17, "%lu", active_anon
);
3170 *active_anon
/= 1024;
3171 } else if (startswith(memstat
, "total_inactive_anon")) {
3172 sscanf(memstat
+ 19, "%lu", inactive_anon
);
3173 *inactive_anon
/= 1024;
3174 } else if (startswith(memstat
, "total_active_file")) {
3175 sscanf(memstat
+ 17, "%lu", active_file
);
3176 *active_file
/= 1024;
3177 } else if (startswith(memstat
, "total_inactive_file")) {
3178 sscanf(memstat
+ 19, "%lu", inactive_file
);
3179 *inactive_file
/= 1024;
3180 } else if (startswith(memstat
, "total_unevictable")) {
3181 sscanf(memstat
+ 17, "%lu", unevictable
);
3182 *unevictable
/= 1024;
3184 eol
= strchr(memstat
, '\n');
3191 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
3197 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
3199 size_t len
= strlen(key
);
3203 if (startswith(str
, key
)) {
3204 sscanf(str
+ len
, "%lu", v
);
3207 eol
= strchr(str
, '\n');
3214 static int read_file(const char *path
, char *buf
, size_t size
,
3215 struct file_info
*d
)
3217 size_t linelen
= 0, total_len
= 0, rv
= 0;
3219 char *cache
= d
->buf
;
3220 size_t cache_size
= d
->buflen
;
3221 FILE *f
= fopen(path
, "r");
3225 while (getline(&line
, &linelen
, f
) != -1) {
3226 ssize_t l
= snprintf(cache
, cache_size
, "%s", line
);
3228 perror("Error writing to cache");
3232 if (l
>= cache_size
) {
3233 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3242 d
->size
= total_len
;
3243 if (total_len
> size
)
3246 /* read from off 0 */
3247 memcpy(buf
, d
->buf
, total_len
);
3256 * FUSE ops for /proc
3259 static unsigned long get_memlimit(const char *cgroup
, const char *file
)
3261 char *memlimit_str
= NULL
;
3262 unsigned long memlimit
= -1;
3264 if (cgfs_get_value("memory", cgroup
, file
, &memlimit_str
))
3265 memlimit
= strtoul(memlimit_str
, NULL
, 10);
3272 static unsigned long get_min_memlimit(const char *cgroup
, const char *file
)
3274 char *copy
= strdupa(cgroup
);
3275 unsigned long memlimit
= 0, retlimit
;
3277 retlimit
= get_memlimit(copy
, file
);
3279 while (strcmp(copy
, "/") != 0) {
3280 copy
= dirname(copy
);
3281 memlimit
= get_memlimit(copy
, file
);
3282 if (memlimit
!= -1 && memlimit
< retlimit
)
3283 retlimit
= memlimit
;
3289 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
3290 struct fuse_file_info
*fi
)
3292 struct fuse_context
*fc
= fuse_get_context();
3293 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3295 char *memusage_str
= NULL
, *memstat_str
= NULL
,
3296 *memswlimit_str
= NULL
, *memswusage_str
= NULL
;
3297 unsigned long memlimit
= 0, memusage
= 0, memswlimit
= 0, memswusage
= 0,
3298 cached
= 0, hosttotal
= 0, active_anon
= 0, inactive_anon
= 0,
3299 active_file
= 0, inactive_file
= 0, unevictable
= 0,
3302 size_t linelen
= 0, total_len
= 0, rv
= 0;
3303 char *cache
= d
->buf
;
3304 size_t cache_size
= d
->buflen
;
3308 if (offset
> d
->size
)
3312 int left
= d
->size
- offset
;
3313 total_len
= left
> size
? size
: left
;
3314 memcpy(buf
, cache
+ offset
, total_len
);
3318 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3321 cg
= get_pid_cgroup(initpid
, "memory");
3323 return read_file("/proc/meminfo", buf
, size
, d
);
3324 prune_init_slice(cg
);
3326 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
3327 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
3329 if (!cgfs_get_value("memory", cg
, "memory.stat", &memstat_str
))
3332 // Following values are allowed to fail, because swapaccount might be turned
3333 // off for current kernel
3334 if(cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
) &&
3335 cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
))
3337 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
3338 memswusage
= strtoul(memswusage_str
, NULL
, 10);
3340 memswlimit
= memswlimit
/ 1024;
3341 memswusage
= memswusage
/ 1024;
3344 memusage
= strtoul(memusage_str
, NULL
, 10);
3348 parse_memstat(memstat_str
, &cached
, &active_anon
,
3349 &inactive_anon
, &active_file
, &inactive_file
,
3352 f
= fopen("/proc/meminfo", "r");
3356 while (getline(&line
, &linelen
, f
) != -1) {
3358 char *printme
, lbuf
[100];
3360 memset(lbuf
, 0, 100);
3361 if (startswith(line
, "MemTotal:")) {
3362 sscanf(line
+sizeof("MemTotal:")-1, "%lu", &hosttotal
);
3363 if (hosttotal
< memlimit
)
3364 memlimit
= hosttotal
;
3365 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
3367 } else if (startswith(line
, "MemFree:")) {
3368 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
3370 } else if (startswith(line
, "MemAvailable:")) {
3371 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
+ cached
);
3373 } else if (startswith(line
, "SwapTotal:") && memswlimit
> 0) {
3374 sscanf(line
+sizeof("SwapTotal:")-1, "%lu", &hostswtotal
);
3375 if (hostswtotal
< memswlimit
)
3376 memswlimit
= hostswtotal
;
3377 snprintf(lbuf
, 100, "SwapTotal: %8lu kB\n", memswlimit
);
3379 } else if (startswith(line
, "SwapFree:") && memswlimit
> 0 && memswusage
> 0) {
3380 unsigned long swaptotal
= memswlimit
,
3381 swapusage
= memswusage
- memusage
,
3382 swapfree
= swapusage
< swaptotal
? swaptotal
- swapusage
: 0;
3383 snprintf(lbuf
, 100, "SwapFree: %8lu kB\n", swapfree
);
3385 } else if (startswith(line
, "Slab:")) {
3386 snprintf(lbuf
, 100, "Slab: %8lu kB\n", 0UL);
3388 } else if (startswith(line
, "Buffers:")) {
3389 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
3391 } else if (startswith(line
, "Cached:")) {
3392 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
3394 } else if (startswith(line
, "SwapCached:")) {
3395 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
3397 } else if (startswith(line
, "Active:")) {
3398 snprintf(lbuf
, 100, "Active: %8lu kB\n",
3399 active_anon
+ active_file
);
3401 } else if (startswith(line
, "Inactive:")) {
3402 snprintf(lbuf
, 100, "Inactive: %8lu kB\n",
3403 inactive_anon
+ inactive_file
);
3405 } else if (startswith(line
, "Active(anon)")) {
3406 snprintf(lbuf
, 100, "Active(anon): %8lu kB\n", active_anon
);
3408 } else if (startswith(line
, "Inactive(anon)")) {
3409 snprintf(lbuf
, 100, "Inactive(anon): %8lu kB\n", inactive_anon
);
3411 } else if (startswith(line
, "Active(file)")) {
3412 snprintf(lbuf
, 100, "Active(file): %8lu kB\n", active_file
);
3414 } else if (startswith(line
, "Inactive(file)")) {
3415 snprintf(lbuf
, 100, "Inactive(file): %8lu kB\n", inactive_file
);
3417 } else if (startswith(line
, "Unevictable")) {
3418 snprintf(lbuf
, 100, "Unevictable: %8lu kB\n", unevictable
);
3420 } else if (startswith(line
, "SReclaimable")) {
3421 snprintf(lbuf
, 100, "SReclaimable: %8lu kB\n", 0UL);
3423 } else if (startswith(line
, "SUnreclaim")) {
3424 snprintf(lbuf
, 100, "SUnreclaim: %8lu kB\n", 0UL);
3429 l
= snprintf(cache
, cache_size
, "%s", printme
);
3431 perror("Error writing to cache");
3436 if (l
>= cache_size
) {
3437 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3448 d
->size
= total_len
;
3449 if (total_len
> size
) total_len
= size
;
3450 memcpy(buf
, d
->buf
, total_len
);
3459 free(memswlimit_str
);
3460 free(memswusage_str
);
3466 * Read the cpuset.cpus for cg
3467 * Return the answer in a newly allocated string which must be freed
3469 static char *get_cpuset(const char *cg
)
3473 if (!cgfs_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
3478 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
3480 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
3484 if (sscanf(line
, "processor : %d", &cpu
) != 1)
3486 return cpu_in_cpuset(cpu
, cpuset
);
3490 * check whether this is a '^processor" line in /proc/cpuinfo
3492 static bool is_processor_line(const char *line
)
3496 if (sscanf(line
, "processor : %d", &cpu
) == 1)
3501 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
3502 struct fuse_file_info
*fi
)
3504 struct fuse_context
*fc
= fuse_get_context();
3505 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3507 char *cpuset
= NULL
;
3509 size_t linelen
= 0, total_len
= 0, rv
= 0;
3510 bool am_printing
= false, firstline
= true, is_s390x
= false;
3511 int curcpu
= -1, cpu
;
3512 char *cache
= d
->buf
;
3513 size_t cache_size
= d
->buflen
;
3517 if (offset
> d
->size
)
3521 int left
= d
->size
- offset
;
3522 total_len
= left
> size
? size
: left
;
3523 memcpy(buf
, cache
+ offset
, total_len
);
3527 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3530 cg
= get_pid_cgroup(initpid
, "cpuset");
3532 return read_file("proc/cpuinfo", buf
, size
, d
);
3533 prune_init_slice(cg
);
3535 cpuset
= get_cpuset(cg
);
3539 f
= fopen("/proc/cpuinfo", "r");
3543 while (getline(&line
, &linelen
, f
) != -1) {
3547 if (strstr(line
, "IBM/S390") != NULL
) {
3553 if (strncmp(line
, "# processors:", 12) == 0)
3555 if (is_processor_line(line
)) {
3556 am_printing
= cpuline_in_cpuset(line
, cpuset
);
3559 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
3561 perror("Error writing to cache");
3565 if (l
>= cache_size
) {
3566 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3575 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
3577 if (!cpu_in_cpuset(cpu
, cpuset
))
3580 p
= strchr(line
, ':');
3584 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
3586 perror("Error writing to cache");
3590 if (l
>= cache_size
) {
3591 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3602 l
= snprintf(cache
, cache_size
, "%s", line
);
3604 perror("Error writing to cache");
3608 if (l
>= cache_size
) {
3609 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3620 char *origcache
= d
->buf
;
3623 d
->buf
= malloc(d
->buflen
);
3626 cache_size
= d
->buflen
;
3628 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
3629 if (l
< 0 || l
>= cache_size
) {
3636 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
3637 if (l
< 0 || l
>= cache_size
) {
3644 l
= snprintf(cache
, cache_size
, "%s", origcache
);
3646 if (l
< 0 || l
>= cache_size
)
3652 d
->size
= total_len
;
3653 if (total_len
> size
) total_len
= size
;
3655 /* read from off 0 */
3656 memcpy(buf
, d
->buf
, total_len
);
3667 static uint64_t get_reaper_start_time(pid_t pid
)
3672 /* strlen("/proc/") = 6
3676 * strlen("/stat") = 5
3680 #define __PROC_PID_STAT_LEN (6 + LXCFS_NUMSTRLEN64 + 5 + 1)
3681 char path
[__PROC_PID_STAT_LEN
];
3684 qpid
= lookup_initpid_in_store(pid
);
3686 /* Caller can check for EINVAL on 0. */
3691 ret
= snprintf(path
, __PROC_PID_STAT_LEN
, "/proc/%d/stat", qpid
);
3692 if (ret
< 0 || ret
>= __PROC_PID_STAT_LEN
) {
3693 /* Caller can check for EINVAL on 0. */
3698 f
= fopen(path
, "r");
3700 /* Caller can check for EINVAL on 0. */
3705 /* Note that the *scanf() argument supression requires that length
3706 * modifiers such as "l" are omitted. Otherwise some compilers will yell
3707 * at us. It's like telling someone you're not married and then asking
3708 * if you can bring your wife to the party.
3710 ret
= fscanf(f
, "%*d " /* (1) pid %d */
3711 "%*s " /* (2) comm %s */
3712 "%*c " /* (3) state %c */
3713 "%*d " /* (4) ppid %d */
3714 "%*d " /* (5) pgrp %d */
3715 "%*d " /* (6) session %d */
3716 "%*d " /* (7) tty_nr %d */
3717 "%*d " /* (8) tpgid %d */
3718 "%*u " /* (9) flags %u */
3719 "%*u " /* (10) minflt %lu */
3720 "%*u " /* (11) cminflt %lu */
3721 "%*u " /* (12) majflt %lu */
3722 "%*u " /* (13) cmajflt %lu */
3723 "%*u " /* (14) utime %lu */
3724 "%*u " /* (15) stime %lu */
3725 "%*d " /* (16) cutime %ld */
3726 "%*d " /* (17) cstime %ld */
3727 "%*d " /* (18) priority %ld */
3728 "%*d " /* (19) nice %ld */
3729 "%*d " /* (20) num_threads %ld */
3730 "%*d " /* (21) itrealvalue %ld */
3731 "%" PRIu64
, /* (22) starttime %llu */
3735 /* Caller can check for EINVAL on 0. */
3746 static uint64_t get_reaper_start_time_in_sec(pid_t pid
)
3748 uint64_t clockticks
;
3749 int64_t ticks_per_sec
;
3751 clockticks
= get_reaper_start_time(pid
);
3752 if (clockticks
== 0 && errno
== EINVAL
) {
3753 lxcfs_debug("failed to retrieve start time of pid %d\n", pid
);
3757 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
3758 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
3761 "failed to determine number of clock ticks in a second");
3765 return (clockticks
/= ticks_per_sec
);
3768 static uint64_t get_reaper_age(pid_t pid
)
3770 uint64_t procstart
, uptime
, procage
;
3772 /* We need to substract the time the process has started since system
3773 * boot minus the time when the system has started to get the actual
3776 procstart
= get_reaper_start_time_in_sec(pid
);
3777 procage
= procstart
;
3778 if (procstart
> 0) {
3780 struct timespec spec
;
3782 ret
= clock_gettime(CLOCK_BOOTTIME
, &spec
);
3785 /* We could make this more precise here by using the tv_nsec
3786 * field in the timespec struct and convert it to milliseconds
3787 * and then create a double for the seconds and milliseconds but
3788 * that seems more work than it is worth.
3790 uptime
= spec
.tv_sec
;
3791 procage
= uptime
- procstart
;
3797 #define CPUALL_MAX_SIZE (BUF_RESERVE_SIZE / 2)
3798 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
3799 struct fuse_file_info
*fi
)
3801 struct fuse_context
*fc
= fuse_get_context();
3802 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3804 char *cpuset
= NULL
;
3806 size_t linelen
= 0, total_len
= 0, rv
= 0;
3807 int curcpu
= -1; /* cpu numbering starts at 0 */
3808 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
3809 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
3810 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0, guest_nice_sum
= 0;
3811 char cpuall
[CPUALL_MAX_SIZE
];
3812 /* reserve for cpu all */
3813 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
3814 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
3818 if (offset
> d
->size
)
3822 int left
= d
->size
- offset
;
3823 total_len
= left
> size
? size
: left
;
3824 memcpy(buf
, d
->buf
+ offset
, total_len
);
3828 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3831 cg
= get_pid_cgroup(initpid
, "cpuset");
3833 return read_file("/proc/stat", buf
, size
, d
);
3834 prune_init_slice(cg
);
3836 cpuset
= get_cpuset(cg
);
3840 f
= fopen("/proc/stat", "r");
3845 if (getline(&line
, &linelen
, f
) < 0) {
3846 lxcfs_error("%s\n", "proc_stat_read read first line failed.");
3850 while (getline(&line
, &linelen
, f
) != -1) {
3853 char cpu_char
[10]; /* That's a lot of cores */
3856 if (strlen(line
) == 0)
3858 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
3859 /* not a ^cpuN line containing a number N, just print it */
3860 l
= snprintf(cache
, cache_size
, "%s", line
);
3862 perror("Error writing to cache");
3866 if (l
>= cache_size
) {
3867 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3877 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
3879 if (!cpu_in_cpuset(cpu
, cpuset
))
3883 c
= strchr(line
, ' ');
3886 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
3888 perror("Error writing to cache");
3893 if (l
>= cache_size
) {
3894 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3903 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
3917 system_sum
+= system
;
3919 iowait_sum
+= iowait
;
3921 softirq_sum
+= softirq
;
3924 guest_nice_sum
+= guest_nice
;
3929 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
3940 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
) {
3941 memcpy(cache
, cpuall
, cpuall_len
);
3942 cache
+= cpuall_len
;
3944 /* shouldn't happen */
3945 lxcfs_error("proc_stat_read copy cpuall failed, cpuall_len=%d.", cpuall_len
);
3949 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
3950 total_len
+= cpuall_len
;
3952 d
->size
= total_len
;
3953 if (total_len
> size
)
3956 memcpy(buf
, d
->buf
, total_len
);
3968 /* This function retrieves the busy time of a group of tasks by looking at
3969 * cpuacct.usage. Unfortunately, this only makes sense when the container has
3970 * been given it's own cpuacct cgroup. If not, this function will take the busy
3971 * time of all other taks that do not actually belong to the container into
3972 * account as well. If someone has a clever solution for this please send a
3975 static unsigned long get_reaper_busy(pid_t task
)
3977 pid_t initpid
= lookup_initpid_in_store(task
);
3978 char *cgroup
= NULL
, *usage_str
= NULL
;
3979 unsigned long usage
= 0;
3984 cgroup
= get_pid_cgroup(initpid
, "cpuacct");
3987 prune_init_slice(cgroup
);
3988 if (!cgfs_get_value("cpuacct", cgroup
, "cpuacct.usage", &usage_str
))
3990 usage
= strtoul(usage_str
, NULL
, 10);
3991 usage
/= 1000000000;
4004 fd
= creat("/tmp/lxcfs-iwashere", 0644);
4011 * We read /proc/uptime and reuse its second field.
4012 * For the first field, we use the mtime for the reaper for
4013 * the calling pid as returned by getreaperage
4015 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
4016 struct fuse_file_info
*fi
)
4018 struct fuse_context
*fc
= fuse_get_context();
4019 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4020 unsigned long int busytime
= get_reaper_busy(fc
->pid
);
4021 char *cache
= d
->buf
;
4022 ssize_t total_len
= 0;
4023 uint64_t idletime
, reaperage
;
4032 if (offset
> d
->size
)
4034 int left
= d
->size
- offset
;
4035 total_len
= left
> size
? size
: left
;
4036 memcpy(buf
, cache
+ offset
, total_len
);
4040 reaperage
= get_reaper_age(fc
->pid
);
4041 /* To understand why this is done, please read the comment to the
4042 * get_reaper_busy() function.
4044 idletime
= reaperage
;
4045 if (reaperage
>= busytime
)
4046 idletime
= reaperage
- busytime
;
4048 total_len
= snprintf(d
->buf
, d
->buflen
, "%"PRIu64
".00 %"PRIu64
".00\n", reaperage
, idletime
);
4049 if (total_len
< 0 || total_len
>= d
->buflen
){
4050 lxcfs_error("%s\n", "failed to write to cache");
4054 d
->size
= (int)total_len
;
4057 if (total_len
> size
) total_len
= size
;
4059 memcpy(buf
, d
->buf
, total_len
);
4063 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
4064 struct fuse_file_info
*fi
)
4067 struct fuse_context
*fc
= fuse_get_context();
4068 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4070 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
4071 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
4072 unsigned long read
= 0, write
= 0;
4073 unsigned long read_merged
= 0, write_merged
= 0;
4074 unsigned long read_sectors
= 0, write_sectors
= 0;
4075 unsigned long read_ticks
= 0, write_ticks
= 0;
4076 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
4077 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
4078 char *cache
= d
->buf
;
4079 size_t cache_size
= d
->buflen
;
4081 size_t linelen
= 0, total_len
= 0, rv
= 0;
4082 unsigned int major
= 0, minor
= 0;
4087 if (offset
> d
->size
)
4091 int left
= d
->size
- offset
;
4092 total_len
= left
> size
? size
: left
;
4093 memcpy(buf
, cache
+ offset
, total_len
);
4097 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
4100 cg
= get_pid_cgroup(initpid
, "blkio");
4102 return read_file("/proc/diskstats", buf
, size
, d
);
4103 prune_init_slice(cg
);
4105 if (!cgfs_get_value("blkio", cg
, "blkio.io_serviced_recursive", &io_serviced_str
))
4107 if (!cgfs_get_value("blkio", cg
, "blkio.io_merged_recursive", &io_merged_str
))
4109 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_bytes_recursive", &io_service_bytes_str
))
4111 if (!cgfs_get_value("blkio", cg
, "blkio.io_wait_time_recursive", &io_wait_time_str
))
4113 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_time_recursive", &io_service_time_str
))
4117 f
= fopen("/proc/diskstats", "r");
4121 while (getline(&line
, &linelen
, f
) != -1) {
4125 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
4129 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
4130 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
4131 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
4132 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
4133 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
4134 read_sectors
= read_sectors
/512;
4135 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
4136 write_sectors
= write_sectors
/512;
4138 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
4139 rd_svctm
= rd_svctm
/1000000;
4140 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
4141 rd_wait
= rd_wait
/1000000;
4142 read_ticks
= rd_svctm
+ rd_wait
;
4144 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
4145 wr_svctm
= wr_svctm
/1000000;
4146 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
4147 wr_wait
= wr_wait
/1000000;
4148 write_ticks
= wr_svctm
+ wr_wait
;
4150 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
4151 tot_ticks
= tot_ticks
/1000000;
4153 memset(lbuf
, 0, 256);
4154 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
)
4155 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
4156 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
4157 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
4161 l
= snprintf(cache
, cache_size
, "%s", lbuf
);
4163 perror("Error writing to fuse buf");
4167 if (l
>= cache_size
) {
4168 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4178 d
->size
= total_len
;
4179 if (total_len
> size
) total_len
= size
;
4180 memcpy(buf
, d
->buf
, total_len
);
4188 free(io_serviced_str
);
4189 free(io_merged_str
);
4190 free(io_service_bytes_str
);
4191 free(io_wait_time_str
);
4192 free(io_service_time_str
);
4196 static int proc_swaps_read(char *buf
, size_t size
, off_t offset
,
4197 struct fuse_file_info
*fi
)
4199 struct fuse_context
*fc
= fuse_get_context();
4200 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4202 char *memswlimit_str
= NULL
, *memlimit_str
= NULL
, *memusage_str
= NULL
, *memswusage_str
= NULL
;
4203 unsigned long memswlimit
= 0, memlimit
= 0, memusage
= 0, memswusage
= 0, swap_total
= 0, swap_free
= 0;
4204 ssize_t total_len
= 0, rv
= 0;
4206 char *cache
= d
->buf
;
4209 if (offset
> d
->size
)
4213 int left
= d
->size
- offset
;
4214 total_len
= left
> size
? size
: left
;
4215 memcpy(buf
, cache
+ offset
, total_len
);
4219 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
4222 cg
= get_pid_cgroup(initpid
, "memory");
4224 return read_file("/proc/swaps", buf
, size
, d
);
4225 prune_init_slice(cg
);
4227 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
4229 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
4232 memusage
= strtoul(memusage_str
, NULL
, 10);
4234 if (cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
) &&
4235 cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
)) {
4237 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
4238 memswusage
= strtoul(memswusage_str
, NULL
, 10);
4240 swap_total
= (memswlimit
- memlimit
) / 1024;
4241 swap_free
= (memswusage
- memusage
) / 1024;
4244 total_len
= snprintf(d
->buf
, d
->size
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
4246 /* When no mem + swap limit is specified or swapaccount=0*/
4250 FILE *f
= fopen("/proc/meminfo", "r");
4255 while (getline(&line
, &linelen
, f
) != -1) {
4256 if (startswith(line
, "SwapTotal:")) {
4257 sscanf(line
, "SwapTotal: %8lu kB", &swap_total
);
4258 } else if (startswith(line
, "SwapFree:")) {
4259 sscanf(line
, "SwapFree: %8lu kB", &swap_free
);
4267 if (swap_total
> 0) {
4268 l
= snprintf(d
->buf
+ total_len
, d
->size
- total_len
,
4269 "none%*svirtual\t\t%lu\t%lu\t0\n", 36, " ",
4270 swap_total
, swap_free
);
4274 if (total_len
< 0 || l
< 0) {
4275 perror("Error writing to cache");
4281 d
->size
= (int)total_len
;
4283 if (total_len
> size
) total_len
= size
;
4284 memcpy(buf
, d
->buf
, total_len
);
4289 free(memswlimit_str
);
4292 free(memswusage_str
);
4296 * Find the process pid from cgroup path.
4297 * eg:from /sys/fs/cgroup/cpu/docker/containerid/cgroup.procs to find the process pid.
4298 * @pid_buf : put pid to pid_buf.
4299 * @dpath : the path of cgroup. eg: /docker/containerid or /docker/containerid/child-cgroup ...
4300 * @depth : the depth of cgroup in container.
4301 * @sum : return the number of pid.
4302 * @cfd : the file descriptor of the mounted cgroup. eg: /sys/fs/cgroup/cpu
4304 static int calc_pid(char ***pid_buf
, char *dpath
, int depth
, int sum
, int cfd
)
4308 struct dirent
*file
;
4313 char *path_dir
, *path
;
4316 /* path = dpath + "/cgroup.procs" + /0 */
4318 path
= malloc(strlen(dpath
) + 20);
4321 strcpy(path
, dpath
);
4322 fd
= openat(cfd
, path
, O_RDONLY
);
4326 dir
= fdopendir(fd
);
4332 while (((file
= readdir(dir
)) != NULL
) && depth
> 0) {
4333 if (strncmp(file
->d_name
, ".", 1) == 0)
4335 if (strncmp(file
->d_name
, "..", 1) == 0)
4337 if (file
->d_type
== DT_DIR
) {
4338 /* path + '/' + d_name +/0 */
4340 path_dir
= malloc(strlen(path
) + 2 + sizeof(file
->d_name
));
4341 } while (!path_dir
);
4342 strcpy(path_dir
, path
);
4343 strcat(path_dir
, "/");
4344 strcat(path_dir
, file
->d_name
);
4346 sum
= calc_pid(pid_buf
, path_dir
, pd
, sum
, cfd
);
4352 strcat(path
, "/cgroup.procs");
4353 fd
= openat(cfd
, path
, O_RDONLY
);
4357 f
= fdopen(fd
, "r");
4363 while (getline(&line
, &linelen
, f
) != -1) {
4365 pid
= realloc(*pid_buf
, sizeof(char *) * (sum
+ 1));
4369 *(*pid_buf
+ sum
) = malloc(strlen(line
) + 1);
4370 } while (*(*pid_buf
+ sum
) == NULL
);
4371 strcpy(*(*pid_buf
+ sum
), line
);
4380 * calc_load calculates the load according to the following formula:
4381 * load1 = load0 * exp + active * (1 - exp)
4383 * @load1: the new loadavg.
4384 * @load0: the former loadavg.
4385 * @active: the total number of running pid at this moment.
4386 * @exp: the fixed-point defined in the beginning.
4388 static unsigned long
4389 calc_load(unsigned long load
, unsigned long exp
, unsigned long active
)
4391 unsigned long newload
;
4393 active
= active
> 0 ? active
* FIXED_1
: 0;
4394 newload
= load
* exp
+ active
* (FIXED_1
- exp
);
4396 newload
+= FIXED_1
- 1;
4398 return newload
/ FIXED_1
;
4402 * Return 0 means that container p->cg is closed.
4403 * Return -1 means that error occurred in refresh.
4404 * Positive num equals the total number of pid.
4406 static int refresh_load(struct load_node
*p
, char *path
)
4410 char proc_path
[256];
4411 int i
, ret
, run_pid
= 0, total_pid
= 0, last_pid
= 0;
4416 struct dirent
*file
;
4419 idbuf
= malloc(sizeof(char *));
4421 sum
= calc_pid(&idbuf
, path
, DEPTH_DIR
, 0, p
->cfd
);
4426 for (i
= 0; i
< sum
; i
++) {
4428 length
= strlen(idbuf
[i
])-1;
4429 idbuf
[i
][length
] = '\0';
4430 ret
= snprintf(proc_path
, 256, "/proc/%s/task", idbuf
[i
]);
4431 if (ret
< 0 || ret
> 255) {
4432 lxcfs_error("%s\n", "snprintf() failed in refresh_load.");
4438 dp
= opendir(proc_path
);
4440 lxcfs_error("%s\n", "Open proc_path failed in refresh_load.");
4443 while ((file
= readdir(dp
)) != NULL
) {
4444 if (strncmp(file
->d_name
, ".", 1) == 0)
4446 if (strncmp(file
->d_name
, "..", 1) == 0)
4449 /* We make the biggest pid become last_pid.*/
4450 ret
= atof(file
->d_name
);
4451 last_pid
= (ret
> last_pid
) ? ret
: last_pid
;
4453 ret
= snprintf(proc_path
, 256, "/proc/%s/task/%s/status", idbuf
[i
], file
->d_name
);
4454 if (ret
< 0 || ret
> 255) {
4455 lxcfs_error("%s\n", "snprintf() failed in refresh_load.");
4461 f
= fopen(proc_path
, "r");
4463 while (getline(&line
, &linelen
, f
) != -1) {
4465 if ((line
[0] == 'S') && (line
[1] == 't'))
4468 if ((line
[7] == 'R') || (line
[7] == 'D'))
4475 /*Calculate the loadavg.*/
4476 p
->avenrun
[0] = calc_load(p
->avenrun
[0], EXP_1
, run_pid
);
4477 p
->avenrun
[1] = calc_load(p
->avenrun
[1], EXP_5
, run_pid
);
4478 p
->avenrun
[2] = calc_load(p
->avenrun
[2], EXP_15
, run_pid
);
4479 p
->run_pid
= run_pid
;
4480 p
->total_pid
= total_pid
;
4481 p
->last_pid
= last_pid
;
4492 * Traverse the hash table and update it.
4494 void *load_begin(void *arg
)
4498 int i
, sum
, length
, ret
;
4499 struct load_node
*f
;
4501 clock_t time1
, time2
;
4504 if (loadavg_stop
== 1)
4508 for (i
= 0; i
< LOAD_SIZE
; i
++) {
4509 pthread_mutex_lock(&load_hash
[i
].lock
);
4510 if (load_hash
[i
].next
== NULL
) {
4511 pthread_mutex_unlock(&load_hash
[i
].lock
);
4514 f
= load_hash
[i
].next
;
4517 length
= strlen(f
->cg
) + 2;
4519 /* strlen(f->cg) + '.' or '' + \0 */
4520 path
= malloc(length
);
4523 ret
= snprintf(path
, length
, "%s%s", *(f
->cg
) == '/' ? "." : "", f
->cg
);
4524 if (ret
< 0 || ret
> length
- 1) {
4525 /* snprintf failed, ignore the node.*/
4526 lxcfs_error("Refresh node %s failed for snprintf().\n", f
->cg
);
4529 sum
= refresh_load(f
, path
);
4536 /* load_hash[i].lock locks only on the first node.*/
4537 if (first_node
== 1) {
4539 pthread_mutex_unlock(&load_hash
[i
].lock
);
4544 if (loadavg_stop
== 1)
4548 usleep(FLUSH_TIME
* 1000000 - (int)((time2
- time1
) * 1000000 / CLOCKS_PER_SEC
));
4552 static int proc_loadavg_read(char *buf
, size_t size
, off_t offset
,
4553 struct fuse_file_info
*fi
)
4555 struct fuse_context
*fc
= fuse_get_context();
4556 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4559 size_t total_len
= 0;
4560 char *cache
= d
->buf
;
4561 struct load_node
*n
;
4564 unsigned long a
, b
, c
;
4567 if (offset
> d
->size
)
4571 int left
= d
->size
- offset
;
4572 total_len
= left
> size
? size
: left
;
4573 memcpy(buf
, cache
+ offset
, total_len
);
4577 return read_file("/proc/loadavg", buf
, size
, d
);
4579 initpid
= lookup_initpid_in_store(fc
->pid
);
4582 cg
= get_pid_cgroup(initpid
, "cpu");
4584 return read_file("/proc/loadavg", buf
, size
, d
);
4586 prune_init_slice(cg
);
4587 hash
= calc_hash(cg
);
4588 n
= locate_node(cg
, hash
);
4592 if (!find_mounted_controller("cpu", &cfd
)) {
4594 * In locate_node() above, pthread_rwlock_unlock() isn't used
4595 * because delete is not allowed before read has ended.
4597 pthread_rwlock_unlock(&load_hash
[hash
].rdlock
);
4601 n
= malloc(sizeof(struct load_node
));
4605 n
->cg
= malloc(strlen(cg
)+1);
4613 n
->last_pid
= initpid
;
4615 insert_node(&n
, hash
);
4617 a
= n
->avenrun
[0] + (FIXED_1
/200);
4618 b
= n
->avenrun
[1] + (FIXED_1
/200);
4619 c
= n
->avenrun
[2] + (FIXED_1
/200);
4620 total_len
= snprintf(d
->buf
, d
->buflen
, "%lu.%02lu %lu.%02lu %lu.%02lu %d/%d %d\n",
4621 LOAD_INT(a
), LOAD_FRAC(a
),
4622 LOAD_INT(b
), LOAD_FRAC(b
),
4623 LOAD_INT(c
), LOAD_FRAC(c
),
4624 n
->run_pid
, n
->total_pid
, n
->last_pid
);
4625 pthread_rwlock_unlock(&load_hash
[hash
].rdlock
);
4626 if (total_len
< 0 || total_len
>= d
->buflen
) {
4627 lxcfs_error("%s\n", "Failed to write to cache");
4630 d
->size
= (int)total_len
;
4633 if (total_len
> size
)
4635 memcpy(buf
, d
->buf
, total_len
);
4638 /* Return a positive number on success, return 0 on failure.*/
4639 pthread_t
load_daemon(int load_use
)
4646 lxcfs_error("%s\n", "Initialize hash_table fails in load_daemon!");
4649 ret
= pthread_create(&pid
, NULL
, load_begin
, NULL
);
4651 lxcfs_error("%s\n", "Create pthread fails in load_daemon!");
4655 /* use loadavg, here loadavg = 1*/
4660 /* Returns 0 on success. */
4661 int stop_load_daemon(pthread_t pid
)
4665 /* Signal the thread to gracefully stop */
4668 s
= pthread_join(pid
, NULL
); /* Make sure sub thread has been canceled. */
4670 lxcfs_error("%s\n", "stop_load_daemon error: failed to join");
4680 static off_t
get_procfile_size(const char *which
)
4682 FILE *f
= fopen(which
, "r");
4685 ssize_t sz
, answer
= 0;
4689 while ((sz
= getline(&line
, &len
, f
)) != -1)
4697 int proc_getattr(const char *path
, struct stat
*sb
)
4699 struct timespec now
;
4701 memset(sb
, 0, sizeof(struct stat
));
4702 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
4704 sb
->st_uid
= sb
->st_gid
= 0;
4705 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
4706 if (strcmp(path
, "/proc") == 0) {
4707 sb
->st_mode
= S_IFDIR
| 00555;
4711 if (strcmp(path
, "/proc/meminfo") == 0 ||
4712 strcmp(path
, "/proc/cpuinfo") == 0 ||
4713 strcmp(path
, "/proc/uptime") == 0 ||
4714 strcmp(path
, "/proc/stat") == 0 ||
4715 strcmp(path
, "/proc/diskstats") == 0 ||
4716 strcmp(path
, "/proc/swaps") == 0 ||
4717 strcmp(path
, "/proc/loadavg") == 0) {
4719 sb
->st_mode
= S_IFREG
| 00444;
4727 int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
4728 struct fuse_file_info
*fi
)
4730 if (filler(buf
, ".", NULL
, 0) != 0 ||
4731 filler(buf
, "..", NULL
, 0) != 0 ||
4732 filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
4733 filler(buf
, "meminfo", NULL
, 0) != 0 ||
4734 filler(buf
, "stat", NULL
, 0) != 0 ||
4735 filler(buf
, "uptime", NULL
, 0) != 0 ||
4736 filler(buf
, "diskstats", NULL
, 0) != 0 ||
4737 filler(buf
, "swaps", NULL
, 0) != 0 ||
4738 filler(buf
, "loadavg", NULL
, 0) != 0)
4743 int proc_open(const char *path
, struct fuse_file_info
*fi
)
4746 struct file_info
*info
;
4748 if (strcmp(path
, "/proc/meminfo") == 0)
4749 type
= LXC_TYPE_PROC_MEMINFO
;
4750 else if (strcmp(path
, "/proc/cpuinfo") == 0)
4751 type
= LXC_TYPE_PROC_CPUINFO
;
4752 else if (strcmp(path
, "/proc/uptime") == 0)
4753 type
= LXC_TYPE_PROC_UPTIME
;
4754 else if (strcmp(path
, "/proc/stat") == 0)
4755 type
= LXC_TYPE_PROC_STAT
;
4756 else if (strcmp(path
, "/proc/diskstats") == 0)
4757 type
= LXC_TYPE_PROC_DISKSTATS
;
4758 else if (strcmp(path
, "/proc/swaps") == 0)
4759 type
= LXC_TYPE_PROC_SWAPS
;
4760 else if (strcmp(path
, "/proc/loadavg") == 0)
4761 type
= LXC_TYPE_PROC_LOADAVG
;
4765 info
= malloc(sizeof(*info
));
4769 memset(info
, 0, sizeof(*info
));
4772 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
4774 info
->buf
= malloc(info
->buflen
);
4775 } while (!info
->buf
);
4776 memset(info
->buf
, 0, info
->buflen
);
4777 /* set actual size to buffer size */
4778 info
->size
= info
->buflen
;
4780 fi
->fh
= (unsigned long)info
;
4784 int proc_access(const char *path
, int mask
)
4786 if (strcmp(path
, "/proc") == 0 && access(path
, R_OK
) == 0)
4789 /* these are all read-only */
4790 if ((mask
& ~R_OK
) != 0)
4795 int proc_release(const char *path
, struct fuse_file_info
*fi
)
4797 do_release_file_info(fi
);
4801 int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
4802 struct fuse_file_info
*fi
)
4804 struct file_info
*f
= (struct file_info
*) fi
->fh
;
4807 case LXC_TYPE_PROC_MEMINFO
:
4808 return proc_meminfo_read(buf
, size
, offset
, fi
);
4809 case LXC_TYPE_PROC_CPUINFO
:
4810 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
4811 case LXC_TYPE_PROC_UPTIME
:
4812 return proc_uptime_read(buf
, size
, offset
, fi
);
4813 case LXC_TYPE_PROC_STAT
:
4814 return proc_stat_read(buf
, size
, offset
, fi
);
4815 case LXC_TYPE_PROC_DISKSTATS
:
4816 return proc_diskstats_read(buf
, size
, offset
, fi
);
4817 case LXC_TYPE_PROC_SWAPS
:
4818 return proc_swaps_read(buf
, size
, offset
, fi
);
4819 case LXC_TYPE_PROC_LOADAVG
:
4820 return proc_loadavg_read(buf
, size
, offset
, fi
);
4827 * Functions needed to setup cgroups in the __constructor__.
4830 static bool mkdir_p(const char *dir
, mode_t mode
)
4832 const char *tmp
= dir
;
4833 const char *orig
= dir
;
4837 dir
= tmp
+ strspn(tmp
, "/");
4838 tmp
= dir
+ strcspn(dir
, "/");
4839 makeme
= strndup(orig
, dir
- orig
);
4842 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
4843 lxcfs_error("Failed to create directory '%s': %s.\n",
4844 makeme
, strerror(errno
));
4849 } while(tmp
!= dir
);
4854 static bool umount_if_mounted(void)
4856 if (umount2(BASEDIR
, MNT_DETACH
) < 0 && errno
!= EINVAL
) {
4857 lxcfs_error("Failed to unmount %s: %s.\n", BASEDIR
, strerror(errno
));
4863 /* __typeof__ should be safe to use with all compilers. */
4864 typedef __typeof__(((struct statfs
*)NULL
)->f_type
) fs_type_magic
;
4865 static bool has_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
4867 return (fs
->f_type
== (fs_type_magic
)magic_val
);
4871 * looking at fs/proc_namespace.c, it appears we can
4872 * actually expect the rootfs entry to very specifically contain
4873 * " - rootfs rootfs "
4874 * IIUC, so long as we've chrooted so that rootfs is not our root,
4875 * the rootfs entry should always be skipped in mountinfo contents.
4877 static bool is_on_ramfs(void)
4885 f
= fopen("/proc/self/mountinfo", "r");
4889 while (getline(&line
, &len
, f
) != -1) {
4890 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
4891 p
= strchr(p
+ 1, ' ');
4894 p2
= strchr(p
+ 1, ' ');
4898 if (strcmp(p
+ 1, "/") == 0) {
4899 // this is '/'. is it the ramfs?
4900 p
= strchr(p2
+ 1, '-');
4901 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
4913 static int pivot_enter()
4915 int ret
= -1, oldroot
= -1, newroot
= -1;
4917 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
);
4919 lxcfs_error("%s\n", "Failed to open old root for fchdir.");
4923 newroot
= open(ROOTDIR
, O_DIRECTORY
| O_RDONLY
);
4925 lxcfs_error("%s\n", "Failed to open new root for fchdir.");
4929 /* change into new root fs */
4930 if (fchdir(newroot
) < 0) {
4931 lxcfs_error("Failed to change directory to new rootfs: %s.\n", ROOTDIR
);
4935 /* pivot_root into our new root fs */
4936 if (pivot_root(".", ".") < 0) {
4937 lxcfs_error("pivot_root() syscall failed: %s.\n", strerror(errno
));
4942 * At this point the old-root is mounted on top of our new-root.
4943 * To unmounted it we must not be chdir'd into it, so escape back
4946 if (fchdir(oldroot
) < 0) {
4947 lxcfs_error("%s\n", "Failed to enter old root.");
4951 if (umount2(".", MNT_DETACH
) < 0) {
4952 lxcfs_error("%s\n", "Failed to detach old root.");
4956 if (fchdir(newroot
) < 0) {
4957 lxcfs_error("%s\n", "Failed to re-enter new root.");
4972 static int chroot_enter()
4974 if (mount(ROOTDIR
, "/", NULL
, MS_REC
| MS_BIND
, NULL
)) {
4975 lxcfs_error("Failed to recursively bind-mount %s into /.", ROOTDIR
);
4979 if (chroot(".") < 0) {
4980 lxcfs_error("Call to chroot() failed: %s.\n", strerror(errno
));
4984 if (chdir("/") < 0) {
4985 lxcfs_error("Failed to change directory: %s.\n", strerror(errno
));
4992 static int permute_and_enter(void)
4996 if (statfs("/", &sb
) < 0) {
4997 lxcfs_error("%s\n", "Could not stat / mountpoint.");
5001 /* has_fs_type() is not reliable. When the ramfs is a tmpfs it will
5002 * likely report TMPFS_MAGIC. Hence, when it reports no we still check
5003 * /proc/1/mountinfo. */
5004 if (has_fs_type(&sb
, RAMFS_MAGIC
) || is_on_ramfs())
5005 return chroot_enter();
5007 if (pivot_enter() < 0) {
5008 lxcfs_error("%s\n", "Could not perform pivot root.");
5015 /* Prepare our new clean root. */
5016 static int permute_prepare(void)
5018 if (mkdir(ROOTDIR
, 0700) < 0 && errno
!= EEXIST
) {
5019 lxcfs_error("%s\n", "Failed to create directory for new root.");
5023 if (mount("/", ROOTDIR
, NULL
, MS_BIND
, 0) < 0) {
5024 lxcfs_error("Failed to bind-mount / for new root: %s.\n", strerror(errno
));
5028 if (mount(RUNTIME_PATH
, ROOTDIR RUNTIME_PATH
, NULL
, MS_BIND
, 0) < 0) {
5029 lxcfs_error("Failed to bind-mount /run into new root: %s.\n", strerror(errno
));
5033 if (mount(BASEDIR
, ROOTDIR BASEDIR
, NULL
, MS_REC
| MS_MOVE
, 0) < 0) {
5034 printf("Failed to move " BASEDIR
" into new root: %s.\n", strerror(errno
));
5041 /* Calls chroot() on ramfs, pivot_root() in all other cases. */
5042 static bool permute_root(void)
5044 /* Prepare new root. */
5045 if (permute_prepare() < 0)
5048 /* Pivot into new root. */
5049 if (permute_and_enter() < 0)
5055 static int preserve_mnt_ns(int pid
)
5058 size_t len
= sizeof("/proc/") + 21 + sizeof("/ns/mnt");
5061 ret
= snprintf(path
, len
, "/proc/%d/ns/mnt", pid
);
5062 if (ret
< 0 || (size_t)ret
>= len
)
5065 return open(path
, O_RDONLY
| O_CLOEXEC
);
5068 static bool cgfs_prepare_mounts(void)
5070 if (!mkdir_p(BASEDIR
, 0700)) {
5071 lxcfs_error("%s\n", "Failed to create lxcfs cgroup mountpoint.");
5075 if (!umount_if_mounted()) {
5076 lxcfs_error("%s\n", "Failed to clean up old lxcfs cgroup mountpoint.");
5080 if (unshare(CLONE_NEWNS
) < 0) {
5081 lxcfs_error("Failed to unshare mount namespace: %s.\n", strerror(errno
));
5085 cgroup_mount_ns_fd
= preserve_mnt_ns(getpid());
5086 if (cgroup_mount_ns_fd
< 0) {
5087 lxcfs_error("Failed to preserve mount namespace: %s.\n", strerror(errno
));
5091 if (mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0) < 0) {
5092 lxcfs_error("Failed to remount / private: %s.\n", strerror(errno
));
5096 if (mount("tmpfs", BASEDIR
, "tmpfs", 0, "size=100000,mode=700") < 0) {
5097 lxcfs_error("%s\n", "Failed to mount tmpfs over lxcfs cgroup mountpoint.");
5104 static bool cgfs_mount_hierarchies(void)
5110 for (i
= 0; i
< num_hierarchies
; i
++) {
5111 char *controller
= hierarchies
[i
];
5113 clen
= strlen(controller
);
5114 len
= strlen(BASEDIR
) + clen
+ 2;
5115 target
= malloc(len
);
5119 ret
= snprintf(target
, len
, "%s/%s", BASEDIR
, controller
);
5120 if (ret
< 0 || ret
>= len
) {
5124 if (mkdir(target
, 0755) < 0 && errno
!= EEXIST
) {
5128 if (!strcmp(controller
, "unified"))
5129 ret
= mount("none", target
, "cgroup2", 0, NULL
);
5131 ret
= mount(controller
, target
, "cgroup", 0, controller
);
5133 lxcfs_error("Failed mounting cgroup %s: %s\n", controller
, strerror(errno
));
5138 fd_hierarchies
[i
] = open(target
, O_DIRECTORY
);
5139 if (fd_hierarchies
[i
] < 0) {
5148 static bool cgfs_setup_controllers(void)
5150 if (!cgfs_prepare_mounts())
5153 if (!cgfs_mount_hierarchies()) {
5154 lxcfs_error("%s\n", "Failed to set up private lxcfs cgroup mounts.");
5158 if (!permute_root())
5164 static void __attribute__((constructor
)) collect_and_mount_subsystems(void)
5167 char *cret
, *line
= NULL
;
5168 char cwd
[MAXPATHLEN
];
5170 int i
, init_ns
= -1;
5171 bool found_unified
= false;
5173 if ((f
= fopen("/proc/self/cgroup", "r")) == NULL
) {
5174 lxcfs_error("Error opening /proc/self/cgroup: %s\n", strerror(errno
));
5178 while (getline(&line
, &len
, f
) != -1) {
5181 p
= strchr(line
, ':');
5187 p2
= strrchr(p
, ':');
5192 /* With cgroupv2 /proc/self/cgroup can contain entries of the
5193 * form: 0::/ This will cause lxcfs to fail the cgroup mounts
5194 * because it parses out the empty string "" and later on passes
5195 * it to mount(). Let's skip such entries.
5197 if (!strcmp(p
, "") && !strcmp(idx
, "0") && !found_unified
) {
5198 found_unified
= true;
5202 if (!store_hierarchy(line
, p
))
5206 /* Preserve initial namespace. */
5207 init_ns
= preserve_mnt_ns(getpid());
5209 lxcfs_error("%s\n", "Failed to preserve initial mount namespace.");
5213 fd_hierarchies
= malloc(sizeof(int) * num_hierarchies
);
5214 if (!fd_hierarchies
) {
5215 lxcfs_error("%s\n", strerror(errno
));
5219 for (i
= 0; i
< num_hierarchies
; i
++)
5220 fd_hierarchies
[i
] = -1;
5222 cret
= getcwd(cwd
, MAXPATHLEN
);
5224 lxcfs_debug("Could not retrieve current working directory: %s.\n", strerror(errno
));
5226 /* This function calls unshare(CLONE_NEWNS) our initial mount namespace
5227 * to privately mount lxcfs cgroups. */
5228 if (!cgfs_setup_controllers()) {
5229 lxcfs_error("%s\n", "Failed to setup private cgroup mounts for lxcfs.");
5233 if (setns(init_ns
, 0) < 0) {
5234 lxcfs_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno
));
5238 if (!cret
|| chdir(cwd
) < 0)
5239 lxcfs_debug("Could not change back to original working directory: %s.\n", strerror(errno
));
5250 static void __attribute__((destructor
)) free_subsystems(void)
5254 lxcfs_debug("%s\n", "Running destructor for liblxcfs.");
5256 for (i
= 0; i
< num_hierarchies
; i
++) {
5258 free(hierarchies
[i
]);
5259 if (fd_hierarchies
&& fd_hierarchies
[i
] >= 0)
5260 close(fd_hierarchies
[i
]);
5263 free(fd_hierarchies
);
5265 if (cgroup_mount_ns_fd
>= 0)
5266 close(cgroup_mount_ns_fd
);