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>
41 #include "memory_utils.h"
43 /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */
44 #define LXCFS_NUMSTRLEN64 21
46 /* Define pivot_root() if missing from the C library */
47 #ifndef HAVE_PIVOT_ROOT
48 static int pivot_root(const char * new_root
, const char * put_old
)
50 #ifdef __NR_pivot_root
51 return syscall(__NR_pivot_root
, new_root
, put_old
);
58 extern int pivot_root(const char * new_root
, const char * put_old
);
64 LXC_TYPE_PROC_MEMINFO
,
65 LXC_TYPE_PROC_CPUINFO
,
68 LXC_TYPE_PROC_DISKSTATS
,
70 LXC_TYPE_PROC_LOADAVG
,
78 char *buf
; // unused as of yet
80 int size
; //actual data size
84 struct cpuacct_usage
{
91 /* The function of hash table.*/
92 #define LOAD_SIZE 100 /*the size of hash_table */
93 #define FLUSH_TIME 5 /*the flush rate */
94 #define DEPTH_DIR 3 /*the depth of per cgroup */
95 /* The function of calculate loadavg .*/
96 #define FSHIFT 11 /* nr of bits of precision */
97 #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
98 #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
99 #define EXP_5 2014 /* 1/exp(5sec/5min) */
100 #define EXP_15 2037 /* 1/exp(5sec/15min) */
101 #define LOAD_INT(x) ((x) >> FSHIFT)
102 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
104 * This parameter is used for proc_loadavg_read().
105 * 1 means use loadavg, 0 means not use.
107 static int loadavg
= 0;
108 static volatile sig_atomic_t loadavg_stop
= 0;
109 static int calc_hash(const char *name
)
111 unsigned int hash
= 0;
113 /* ELFHash algorithm. */
115 hash
= (hash
<< 4) + *name
++;
116 x
= hash
& 0xf0000000;
121 return (hash
& 0x7fffffff);
126 unsigned long avenrun
[3]; /* Load averages */
127 unsigned int run_pid
;
128 unsigned int total_pid
;
129 unsigned int last_pid
;
130 int cfd
; /* The file descriptor of the mounted cgroup */
131 struct load_node
*next
;
132 struct load_node
**pre
;
137 * The lock is about insert load_node and refresh load_node.To the first
138 * load_node of each hash bucket, insert and refresh in this hash bucket is
139 * mutually exclusive.
141 pthread_mutex_t lock
;
143 * The rdlock is about read loadavg and delete load_node.To each hash
144 * bucket, read and delete is mutually exclusive. But at the same time, we
145 * allow paratactic read operation. This rdlock is at list level.
147 pthread_rwlock_t rdlock
;
149 * The rilock is about read loadavg and insert load_node.To the first
150 * load_node of each hash bucket, read and insert is mutually exclusive.
151 * But at the same time, we allow paratactic read operation.
153 pthread_rwlock_t rilock
;
154 struct load_node
*next
;
157 static struct load_head load_hash
[LOAD_SIZE
]; /* hash table */
159 * init_load initialize the hash table.
160 * Return 0 on success, return -1 on failure.
162 static int init_load(void)
167 for (i
= 0; i
< LOAD_SIZE
; i
++) {
168 load_hash
[i
].next
= NULL
;
169 ret
= pthread_mutex_init(&load_hash
[i
].lock
, NULL
);
171 lxcfs_error("%s\n", "Failed to initialize lock");
174 ret
= pthread_rwlock_init(&load_hash
[i
].rdlock
, NULL
);
176 lxcfs_error("%s\n", "Failed to initialize rdlock");
179 ret
= pthread_rwlock_init(&load_hash
[i
].rilock
, NULL
);
181 lxcfs_error("%s\n", "Failed to initialize rilock");
187 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
189 pthread_mutex_destroy(&load_hash
[i
].lock
);
193 pthread_mutex_destroy(&load_hash
[i
].lock
);
194 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
195 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
200 static void insert_node(struct load_node
**n
, int locate
)
204 pthread_mutex_lock(&load_hash
[locate
].lock
);
205 pthread_rwlock_wrlock(&load_hash
[locate
].rilock
);
206 f
= load_hash
[locate
].next
;
207 load_hash
[locate
].next
= *n
;
209 (*n
)->pre
= &(load_hash
[locate
].next
);
211 f
->pre
= &((*n
)->next
);
213 pthread_mutex_unlock(&load_hash
[locate
].lock
);
214 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
217 * locate_node() finds special node. Not return NULL means success.
218 * It should be noted that rdlock isn't unlocked at the end of code
219 * because this function is used to read special node. Delete is not
220 * allowed before read has ended.
221 * unlock rdlock only in proc_loadavg_read().
223 static struct load_node
*locate_node(char *cg
, int locate
)
225 struct load_node
*f
= NULL
;
228 pthread_rwlock_rdlock(&load_hash
[locate
].rilock
);
229 pthread_rwlock_rdlock(&load_hash
[locate
].rdlock
);
230 if (load_hash
[locate
].next
== NULL
) {
231 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
234 f
= load_hash
[locate
].next
;
235 pthread_rwlock_unlock(&load_hash
[locate
].rilock
);
236 while (f
&& ((i
= strcmp(f
->cg
, cg
)) != 0))
240 /* Delete the load_node n and return the next node of it. */
241 static struct load_node
*del_node(struct load_node
*n
, int locate
)
245 pthread_rwlock_wrlock(&load_hash
[locate
].rdlock
);
246 if (n
->next
== NULL
) {
250 n
->next
->pre
= n
->pre
;
253 __free_move__(n
->cg
);
255 pthread_rwlock_unlock(&load_hash
[locate
].rdlock
);
259 static void load_free(void)
262 struct load_node
*f
, *p
;
264 for (i
= 0; i
< LOAD_SIZE
; i
++) {
265 pthread_mutex_lock(&load_hash
[i
].lock
);
266 pthread_rwlock_wrlock(&load_hash
[i
].rilock
);
267 pthread_rwlock_wrlock(&load_hash
[i
].rdlock
);
268 if (load_hash
[i
].next
== NULL
) {
269 pthread_mutex_unlock(&load_hash
[i
].lock
);
270 pthread_mutex_destroy(&load_hash
[i
].lock
);
271 pthread_rwlock_unlock(&load_hash
[i
].rilock
);
272 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
273 pthread_rwlock_unlock(&load_hash
[i
].rdlock
);
274 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
277 for (f
= load_hash
[i
].next
; f
; ) {
278 __free_move__(f
->cg
);
283 pthread_mutex_unlock(&load_hash
[i
].lock
);
284 pthread_mutex_destroy(&load_hash
[i
].lock
);
285 pthread_rwlock_unlock(&load_hash
[i
].rilock
);
286 pthread_rwlock_destroy(&load_hash
[i
].rilock
);
287 pthread_rwlock_unlock(&load_hash
[i
].rdlock
);
288 pthread_rwlock_destroy(&load_hash
[i
].rdlock
);
292 /* Data for CPU view */
293 struct cg_proc_stat
{
295 struct cpuacct_usage
*usage
; // Real usage as read from the host's /proc/stat
296 struct cpuacct_usage
*view
; // Usage stats reported to the container
298 pthread_mutex_t lock
; // For node manipulation
299 struct cg_proc_stat
*next
;
302 struct cg_proc_stat_head
{
303 struct cg_proc_stat
*next
;
307 * For access to the list. Reading can be parallel, pruning is exclusive.
309 pthread_rwlock_t lock
;
312 #define CPUVIEW_HASH_SIZE 100
313 static struct cg_proc_stat_head
*proc_stat_history
[CPUVIEW_HASH_SIZE
];
315 static bool cpuview_init_head(struct cg_proc_stat_head
**head
)
317 *head
= malloc(sizeof(struct cg_proc_stat_head
));
319 lxcfs_error("%s\n", strerror(errno
));
323 (*head
)->lastcheck
= time(NULL
);
324 (*head
)->next
= NULL
;
326 if (pthread_rwlock_init(&(*head
)->lock
, NULL
) != 0) {
327 lxcfs_error("%s\n", "Failed to initialize list lock");
328 __free_move__(*head
);
335 static bool init_cpuview()
339 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
340 proc_stat_history
[i
] = NULL
;
342 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
343 if (!cpuview_init_head(&proc_stat_history
[i
]))
350 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
351 if (proc_stat_history
[i
]) {
352 __free_move__(proc_stat_history
[i
]);
359 static void free_proc_stat_node(struct cg_proc_stat
*node
)
361 pthread_mutex_destroy(&node
->lock
);
362 __free_move__(node
->cg
);
363 __free_move__(node
->usage
);
364 __free_move__(node
->view
);
368 static void cpuview_free_head(struct cg_proc_stat_head
*head
)
370 struct cg_proc_stat
*node
, *tmp
;
378 free_proc_stat_node(tmp
);
385 pthread_rwlock_destroy(&head
->lock
);
389 static void free_cpuview()
393 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
394 if (proc_stat_history
[i
])
395 cpuview_free_head(proc_stat_history
[i
]);
399 /* Reserve buffer size to account for file size changes. */
400 #define BUF_RESERVE_SIZE 512
403 * A table caching which pid is init for a pid namespace.
404 * When looking up which pid is init for $qpid, we first
405 * 1. Stat /proc/$qpid/ns/pid.
406 * 2. Check whether the ino_t is in our store.
407 * a. if not, fork a child in qpid's ns to send us
408 * ucred.pid = 1, and read the initpid. Cache
409 * initpid and creation time for /proc/initpid
410 * in a new store entry.
411 * b. if so, verify that /proc/initpid still matches
412 * what we have saved. If not, clear the store
413 * entry and go back to a. If so, return the
416 struct pidns_init_store
{
417 ino_t ino
; // inode number for /proc/$pid/ns/pid
418 pid_t initpid
; // the pid of nit in that ns
419 long int ctime
; // the time at which /proc/$initpid was created
420 struct pidns_init_store
*next
;
424 /* lol - look at how they are allocated in the kernel */
425 #define PIDNS_HASH_SIZE 4096
426 #define HASH(x) ((x) % PIDNS_HASH_SIZE)
428 static struct pidns_init_store
*pidns_hash_table
[PIDNS_HASH_SIZE
];
429 static pthread_mutex_t pidns_store_mutex
= PTHREAD_MUTEX_INITIALIZER
;
430 static void lock_mutex(pthread_mutex_t
*l
)
434 if ((ret
= pthread_mutex_lock(l
)) != 0) {
435 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
440 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
441 * Number of hierarchies mounted. */
442 static int num_hierarchies
;
444 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
445 * Hierachies mounted {cpuset, blkio, ...}:
446 * Initialized via __constructor__ collect_and_mount_subsystems(). */
447 static char **hierarchies
;
449 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
450 * Open file descriptors:
451 * @fd_hierarchies[i] refers to cgroup @hierarchies[i]. They are mounted in a
452 * private mount namespace.
453 * Initialized via __constructor__ collect_and_mount_subsystems().
454 * @fd_hierarchies[i] can be used to perform file operations on the cgroup
455 * mounts and respective files in the private namespace even when located in
456 * another namespace using the *at() family of functions
457 * {openat(), fchownat(), ...}. */
458 static int *fd_hierarchies
;
459 static int cgroup_mount_ns_fd
= -1;
461 static void unlock_mutex(pthread_mutex_t
*l
)
465 if ((ret
= pthread_mutex_unlock(l
)) != 0) {
466 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
471 static void store_lock(void)
473 lock_mutex(&pidns_store_mutex
);
476 static void store_unlock(void)
478 unlock_mutex(&pidns_store_mutex
);
481 /* Must be called under store_lock */
482 static bool initpid_still_valid(struct pidns_init_store
*e
, struct stat
*nsfdsb
)
487 snprintf(fnam
, 100, "/proc/%d", e
->initpid
);
488 if (stat(fnam
, &initsb
) < 0)
491 lxcfs_debug("Comparing ctime %ld == %ld for pid %d.\n", e
->ctime
,
492 initsb
.st_ctime
, e
->initpid
);
494 if (e
->ctime
!= initsb
.st_ctime
)
499 /* Must be called under store_lock */
500 static void remove_initpid(struct pidns_init_store
*e
)
502 struct pidns_init_store
*tmp
;
505 lxcfs_debug("Remove_initpid: removing entry for %d.\n", e
->initpid
);
508 if (pidns_hash_table
[h
] == e
) {
509 pidns_hash_table
[h
] = e
->next
;
514 tmp
= pidns_hash_table
[h
];
516 if (tmp
->next
== e
) {
526 /* Must be called under store_lock */
527 static void prune_initpid_store(void)
529 static long int last_prune
= 0;
530 long int now
, threshold
;
534 last_prune
= time(NULL
);
538 if (now
< last_prune
+ PURGE_SECS
)
541 lxcfs_debug("%s\n", "Pruning.");
544 threshold
= now
- 2 * PURGE_SECS
;
546 for (i
= 0; i
< PIDNS_HASH_SIZE
; i
++) {
547 struct pidns_init_store
*e
, *prev
;
549 for (prev
= NULL
, e
= pidns_hash_table
[i
]; e
; ) {
550 __do_free
struct pidns_init_store
*delme
= NULL
;
552 if (e
->lastcheck
< threshold
) {
554 lxcfs_debug("Removing cached entry for %d.\n", e
->initpid
);
558 prev
->next
= e
->next
;
560 pidns_hash_table
[i
] = e
->next
;
570 /* Must be called under store_lock */
571 static void save_initpid(struct stat
*sb
, pid_t pid
)
573 struct pidns_init_store
*e
;
578 lxcfs_debug("Save_initpid: adding entry for %d.\n", pid
);
580 snprintf(fpath
, 100, "/proc/%d", pid
);
581 if (stat(fpath
, &procsb
) < 0)
584 e
= malloc(sizeof(*e
));
588 e
->ctime
= procsb
.st_ctime
;
590 e
->next
= pidns_hash_table
[h
];
591 e
->lastcheck
= time(NULL
);
592 pidns_hash_table
[h
] = e
;
596 * Given the stat(2) info for a nsfd pid inode, lookup the init_pid_store
597 * entry for the inode number and creation time. Verify that the init pid
598 * is still valid. If not, remove it. Return the entry if valid, NULL
600 * Must be called under store_lock
602 static struct pidns_init_store
*lookup_verify_initpid(struct stat
*sb
)
604 int h
= HASH(sb
->st_ino
);
605 struct pidns_init_store
*e
= pidns_hash_table
[h
];
608 if (e
->ino
== sb
->st_ino
) {
609 if (initpid_still_valid(e
, sb
)) {
610 e
->lastcheck
= time(NULL
);
622 static int is_dir(const char *path
, int fd
)
625 int ret
= fstatat(fd
, path
, &statbuf
, fd
);
626 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
631 static char *must_copy_string(const char *str
)
643 static inline void drop_trailing_newlines(char *s
)
647 for (l
=strlen(s
); l
>0 && s
[l
-1] == '\n'; l
--)
651 #define BATCH_SIZE 50
652 static void dorealloc(char **mem
, size_t oldlen
, size_t newlen
)
654 int newbatches
= (newlen
/ BATCH_SIZE
) + 1;
655 int oldbatches
= (oldlen
/ BATCH_SIZE
) + 1;
657 if (!*mem
|| newbatches
> oldbatches
) {
660 tmp
= realloc(*mem
, newbatches
* BATCH_SIZE
);
665 static void append_line(char **contents
, size_t *len
, char *line
, ssize_t linelen
)
667 size_t newlen
= *len
+ linelen
;
668 dorealloc(contents
, *len
, newlen
+ 1);
669 memcpy(*contents
+ *len
, line
, linelen
+1);
673 static char *slurp_file(const char *from
, int fd
)
675 __do_free
char *line
= NULL
;
676 __do_fclose
FILE *f
= NULL
;
677 char *contents
= NULL
;
678 size_t len
= 0, fulllen
= 0;
685 while ((linelen
= getline(&line
, &len
, f
)) != -1)
686 append_line(&contents
, &fulllen
, line
, linelen
);
689 drop_trailing_newlines(contents
);
694 static bool write_string(const char *fnam
, const char *string
, int fd
)
703 len
= strlen(string
);
704 ret
= fwrite(string
, 1, len
, f
);
706 lxcfs_error("%s - Error writing \"%s\" to \"%s\"\n",
707 strerror(errno
), string
, fnam
);
713 lxcfs_error("%s - Failed to close \"%s\"\n", strerror(errno
), fnam
);
727 static bool store_hierarchy(char *stridx
, char *h
)
729 if (num_hierarchies
% ALLOC_NUM
== 0) {
730 size_t n
= (num_hierarchies
/ ALLOC_NUM
) + 1;
732 char **tmp
= realloc(hierarchies
, n
* sizeof(char *));
734 lxcfs_error("%s\n", strerror(errno
));
740 hierarchies
[num_hierarchies
++] = must_copy_string(h
);
744 static void print_subsystems(void)
748 fprintf(stderr
, "mount namespace: %d\n", cgroup_mount_ns_fd
);
749 fprintf(stderr
, "hierarchies:\n");
750 for (i
= 0; i
< num_hierarchies
; i
++) {
752 fprintf(stderr
, " %2d: fd: %3d: %s\n", i
,
753 fd_hierarchies
[i
], hierarchies
[i
]);
757 static bool in_comma_list(const char *needle
, const char *haystack
)
759 const char *s
= haystack
, *e
;
760 size_t nlen
= strlen(needle
);
762 while (*s
&& (e
= strchr(s
, ','))) {
767 if (strncmp(needle
, s
, nlen
) == 0)
771 if (strcmp(needle
, s
) == 0)
776 /* do we need to do any massaging here? I'm not sure... */
777 /* Return the mounted controller and store the corresponding open file descriptor
778 * referring to the controller mountpoint in the private lxcfs namespace in
781 static char *find_mounted_controller(const char *controller
, int *cfd
)
785 for (i
= 0; i
< num_hierarchies
; i
++) {
788 if (strcmp(hierarchies
[i
], controller
) == 0) {
789 *cfd
= fd_hierarchies
[i
];
790 return hierarchies
[i
];
792 if (in_comma_list(controller
, hierarchies
[i
])) {
793 *cfd
= fd_hierarchies
[i
];
794 return hierarchies
[i
];
801 bool cgfs_set_value(const char *controller
, const char *cgroup
, const char *file
,
808 tmpc
= find_mounted_controller(controller
, &cfd
);
812 /* Make sure we pass a relative path to *at() family of functions.
813 * . + /cgroup + / + file + \0
815 len
= strlen(cgroup
) + strlen(file
) + 3;
817 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
818 if (ret
< 0 || (size_t)ret
>= len
)
821 fd
= openat(cfd
, fnam
, O_WRONLY
);
825 return write_string(fnam
, value
, fd
);
828 // Chown all the files in the cgroup directory. We do this when we create
829 // a cgroup on behalf of a user.
830 static void chown_all_cgroup_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
832 struct dirent
*direntp
;
833 char path
[MAXPATHLEN
];
838 len
= strlen(dirname
);
839 if (len
>= MAXPATHLEN
) {
840 lxcfs_error("Pathname too long: %s\n", dirname
);
844 fd1
= openat(fd
, dirname
, O_DIRECTORY
);
850 lxcfs_error("Failed to open %s\n", dirname
);
854 while ((direntp
= readdir(d
))) {
855 if (!strcmp(direntp
->d_name
, ".") || !strcmp(direntp
->d_name
, ".."))
857 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
858 if (ret
< 0 || ret
>= MAXPATHLEN
) {
859 lxcfs_error("Pathname too long under %s\n", dirname
);
862 if (fchownat(fd
, path
, uid
, gid
, 0) < 0)
863 lxcfs_error("Failed to chown file %s to %u:%u", path
, uid
, gid
);
868 int cgfs_create(const char *controller
, const char *cg
, uid_t uid
, gid_t gid
)
874 tmpc
= find_mounted_controller(controller
, &cfd
);
878 /* Make sure we pass a relative path to *at() family of functions.
881 len
= strlen(cg
) + 2;
882 dirnam
= alloca(len
);
883 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
885 if (mkdirat(cfd
, dirnam
, 0755) < 0)
888 if (uid
== 0 && gid
== 0)
891 if (fchownat(cfd
, dirnam
, uid
, gid
, 0) < 0)
894 chown_all_cgroup_files(dirnam
, uid
, gid
, cfd
);
899 static bool recursive_rmdir(const char *dirname
, int fd
, const int cfd
)
901 struct dirent
*direntp
;
904 char pathname
[MAXPATHLEN
];
907 dupfd
= dup(fd
); // fdopendir() does bad things once it uses an fd.
911 dir
= fdopendir(dupfd
);
913 lxcfs_debug("Failed to open %s: %s.\n", dirname
, strerror(errno
));
918 while ((direntp
= readdir(dir
))) {
922 if (!strcmp(direntp
->d_name
, ".") ||
923 !strcmp(direntp
->d_name
, ".."))
926 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
927 if (rc
< 0 || rc
>= MAXPATHLEN
) {
928 lxcfs_error("%s\n", "Pathname too long.");
932 rc
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
934 lxcfs_debug("Failed to stat %s: %s.\n", pathname
, strerror(errno
));
937 if (S_ISDIR(mystat
.st_mode
))
938 if (!recursive_rmdir(pathname
, fd
, cfd
))
939 lxcfs_debug("Error removing %s.\n", pathname
);
943 if (closedir(dir
) < 0) {
944 lxcfs_error("Failed to close directory %s: %s\n", dirname
, strerror(errno
));
948 if (unlinkat(cfd
, dirname
, AT_REMOVEDIR
) < 0) {
949 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
958 bool cgfs_remove(const char *controller
, const char *cg
)
965 tmpc
= find_mounted_controller(controller
, &cfd
);
969 /* Make sure we pass a relative path to *at() family of functions.
972 len
= strlen(cg
) + 2;
973 dirnam
= alloca(len
);
974 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
976 fd
= openat(cfd
, dirnam
, O_DIRECTORY
);
980 bret
= recursive_rmdir(dirnam
, fd
, cfd
);
985 bool cgfs_chmod_file(const char *controller
, const char *file
, mode_t mode
)
989 char *pathname
, *tmpc
;
991 tmpc
= find_mounted_controller(controller
, &cfd
);
995 /* Make sure we pass a relative path to *at() family of functions.
998 len
= strlen(file
) + 2;
999 pathname
= alloca(len
);
1000 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
1001 if (fchmodat(cfd
, pathname
, mode
, 0) < 0)
1006 static int chown_tasks_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
1011 len
= strlen(dirname
) + strlen("/cgroup.procs") + 1;
1012 fname
= alloca(len
);
1013 snprintf(fname
, len
, "%s/tasks", dirname
);
1014 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
1016 snprintf(fname
, len
, "%s/cgroup.procs", dirname
);
1017 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
1022 int cgfs_chown_file(const char *controller
, const char *file
, uid_t uid
, gid_t gid
)
1026 char *pathname
, *tmpc
;
1028 tmpc
= find_mounted_controller(controller
, &cfd
);
1032 /* Make sure we pass a relative path to *at() family of functions.
1035 len
= strlen(file
) + 2;
1036 pathname
= alloca(len
);
1037 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
1038 if (fchownat(cfd
, pathname
, uid
, gid
, 0) < 0)
1041 if (is_dir(pathname
, cfd
))
1042 // like cgmanager did, we want to chown the tasks file as well
1043 return chown_tasks_files(pathname
, uid
, gid
, cfd
);
1048 FILE *open_pids_file(const char *controller
, const char *cgroup
)
1052 char *pathname
, *tmpc
;
1054 tmpc
= find_mounted_controller(controller
, &cfd
);
1058 /* Make sure we pass a relative path to *at() family of functions.
1059 * . + /cgroup + / "cgroup.procs" + \0
1061 len
= strlen(cgroup
) + strlen("cgroup.procs") + 3;
1062 pathname
= alloca(len
);
1063 snprintf(pathname
, len
, "%s%s/cgroup.procs", *cgroup
== '/' ? "." : "", cgroup
);
1065 fd
= openat(cfd
, pathname
, O_WRONLY
);
1069 return fdopen(fd
, "w");
1072 static bool cgfs_iterate_cgroup(const char *controller
, const char *cgroup
, bool directories
,
1073 void ***list
, size_t typesize
,
1074 void* (*iterator
)(const char*, const char*, const char*))
1079 char pathname
[MAXPATHLEN
];
1080 size_t sz
= 0, asz
= 0;
1081 struct dirent
*dirent
;
1084 tmpc
= find_mounted_controller(controller
, &cfd
);
1089 /* Make sure we pass a relative path to *at() family of functions. */
1090 len
= strlen(cgroup
) + 1 /* . */ + 1 /* \0 */;
1092 ret
= snprintf(cg
, len
, "%s%s", *cgroup
== '/' ? "." : "", cgroup
);
1093 if (ret
< 0 || (size_t)ret
>= len
) {
1094 lxcfs_error("Pathname too long under %s\n", cgroup
);
1098 fd
= openat(cfd
, cg
, O_DIRECTORY
);
1102 dir
= fdopendir(fd
);
1106 while ((dirent
= readdir(dir
))) {
1109 if (!strcmp(dirent
->d_name
, ".") ||
1110 !strcmp(dirent
->d_name
, ".."))
1113 ret
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", cg
, dirent
->d_name
);
1114 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1115 lxcfs_error("Pathname too long under %s\n", cg
);
1119 ret
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
1121 lxcfs_error("Failed to stat %s: %s\n", pathname
, strerror(errno
));
1124 if ((!directories
&& !S_ISREG(mystat
.st_mode
)) ||
1125 (directories
&& !S_ISDIR(mystat
.st_mode
)))
1132 tmp
= realloc(*list
, asz
* typesize
);
1136 (*list
)[sz
] = (*iterator
)(controller
, cg
, dirent
->d_name
);
1137 (*list
)[sz
+1] = NULL
;
1140 if (closedir(dir
) < 0) {
1141 lxcfs_error("Failed closedir for %s: %s\n", cgroup
, strerror(errno
));
1147 static void *make_children_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
1151 dup
= strdup(dir_entry
);
1156 bool cgfs_list_children(const char *controller
, const char *cgroup
, char ***list
)
1158 return cgfs_iterate_cgroup(controller
, cgroup
, true, (void***)list
, sizeof(*list
), &make_children_list_entry
);
1161 void free_key(struct cgfs_files
*k
)
1166 __free_move__(k
->name
);
1170 void free_keys(struct cgfs_files
**keys
)
1177 for (i
= 0; keys
[i
]; i
++)
1180 __free_move__(keys
);
1183 bool cgfs_get_value(const char *controller
, const char *cgroup
, const char *file
, char **value
)
1189 tmpc
= find_mounted_controller(controller
, &cfd
);
1193 /* Make sure we pass a relative path to *at() family of functions.
1194 * . + /cgroup + / + file + \0
1196 len
= strlen(cgroup
) + strlen(file
) + 3;
1198 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
1199 if (ret
< 0 || (size_t)ret
>= len
)
1202 fd
= openat(cfd
, fnam
, O_RDONLY
);
1206 *value
= slurp_file(fnam
, fd
);
1207 return *value
!= NULL
;
1210 bool cgfs_param_exist(const char *controller
, const char *cgroup
, const char *file
)
1216 tmpc
= find_mounted_controller(controller
, &cfd
);
1220 /* Make sure we pass a relative path to *at() family of functions.
1221 * . + /cgroup + / + file + \0
1223 len
= strlen(cgroup
) + strlen(file
) + 3;
1225 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
1226 if (ret
< 0 || (size_t)ret
>= len
)
1229 return (faccessat(cfd
, fnam
, F_OK
, 0) == 0);
1232 struct cgfs_files
*cgfs_get_key(const char *controller
, const char *cgroup
, const char *file
)
1238 struct cgfs_files
*newkey
;
1240 tmpc
= find_mounted_controller(controller
, &cfd
);
1244 if (file
&& *file
== '/')
1247 if (file
&& strchr(file
, '/'))
1250 /* Make sure we pass a relative path to *at() family of functions.
1251 * . + /cgroup + / + file + \0
1253 len
= strlen(cgroup
) + 3;
1255 len
+= strlen(file
) + 1;
1257 snprintf(fnam
, len
, "%s%s%s%s", *cgroup
== '/' ? "." : "", cgroup
,
1258 file
? "/" : "", file
? file
: "");
1260 ret
= fstatat(cfd
, fnam
, &sb
, 0);
1265 newkey
= malloc(sizeof(struct cgfs_files
));
1268 newkey
->name
= must_copy_string(file
);
1269 else if (strrchr(cgroup
, '/'))
1270 newkey
->name
= must_copy_string(strrchr(cgroup
, '/'));
1272 newkey
->name
= must_copy_string(cgroup
);
1273 newkey
->uid
= sb
.st_uid
;
1274 newkey
->gid
= sb
.st_gid
;
1275 newkey
->mode
= sb
.st_mode
;
1280 static void *make_key_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
1282 struct cgfs_files
*entry
= cgfs_get_key(controller
, cgroup
, dir_entry
);
1284 lxcfs_error("Error getting files under %s:%s\n", controller
,
1290 bool cgfs_list_keys(const char *controller
, const char *cgroup
, struct cgfs_files
***keys
)
1292 return cgfs_iterate_cgroup(controller
, cgroup
, false, (void***)keys
, sizeof(*keys
), &make_key_list_entry
);
1295 bool is_child_cgroup(const char *controller
, const char *cgroup
, const char *f
)
1303 tmpc
= find_mounted_controller(controller
, &cfd
);
1307 /* Make sure we pass a relative path to *at() family of functions.
1308 * . + /cgroup + / + f + \0
1310 len
= strlen(cgroup
) + strlen(f
) + 3;
1312 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, f
);
1313 if (ret
< 0 || (size_t)ret
>= len
)
1316 ret
= fstatat(cfd
, fnam
, &sb
, 0);
1317 if (ret
< 0 || !S_ISDIR(sb
.st_mode
))
1323 #define SEND_CREDS_OK 0
1324 #define SEND_CREDS_NOTSK 1
1325 #define SEND_CREDS_FAIL 2
1326 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
);
1327 static int wait_for_pid(pid_t pid
);
1328 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
);
1329 static int send_creds_clone_wrapper(void *arg
);
1332 * clone a task which switches to @task's namespace and writes '1'.
1333 * over a unix sock so we can read the task's reaper's pid in our
1336 * Note: glibc's fork() does not respect pidns, which can lead to failed
1337 * assertions inside glibc (and thus failed forks) if the child's pid in
1338 * the pidns and the parent pid outside are identical. Using clone prevents
1341 static void write_task_init_pid_exit(int sock
, pid_t target
)
1346 size_t stack_size
= sysconf(_SC_PAGESIZE
);
1347 void *stack
= alloca(stack_size
);
1349 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", (int)target
);
1350 if (ret
< 0 || ret
>= sizeof(fnam
))
1353 fd
= open(fnam
, O_RDONLY
);
1355 perror("write_task_init_pid_exit open of ns/pid");
1359 perror("write_task_init_pid_exit setns 1");
1363 pid
= clone(send_creds_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &sock
);
1367 if (!wait_for_pid(pid
))
1373 static int send_creds_clone_wrapper(void *arg
) {
1376 int sock
= *(int *)arg
;
1378 /* we are the child */
1383 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
)
1388 static pid_t
get_init_pid_for_task(pid_t task
)
1396 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1397 perror("socketpair");
1406 write_task_init_pid_exit(sock
[0], task
);
1410 if (!recv_creds(sock
[1], &cred
, &v
))
1422 static pid_t
lookup_initpid_in_store(pid_t qpid
)
1426 struct pidns_init_store
*e
;
1429 snprintf(fnam
, 100, "/proc/%d/ns/pid", qpid
);
1431 if (stat(fnam
, &sb
) < 0)
1433 e
= lookup_verify_initpid(&sb
);
1435 answer
= e
->initpid
;
1438 answer
= get_init_pid_for_task(qpid
);
1440 save_initpid(&sb
, answer
);
1443 /* we prune at end in case we are returning
1444 * the value we were about to return */
1445 prune_initpid_store();
1450 static int wait_for_pid(pid_t pid
)
1458 ret
= waitpid(pid
, &status
, 0);
1466 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
1473 * append pid to *src.
1474 * src: a pointer to a char* in which ot append the pid.
1475 * sz: the number of characters printed so far, minus trailing \0.
1476 * asz: the allocated size so far
1477 * pid: the pid to append
1479 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
1483 int tmplen
= sprintf(tmp
, "%d\n", (int)pid
);
1485 if (!*src
|| tmplen
+ *sz
+ 1 >= *asz
) {
1488 tmp
= realloc(*src
, *asz
+ BUF_RESERVE_SIZE
);
1491 *asz
+= BUF_RESERVE_SIZE
;
1493 memcpy((*src
) +*sz
, tmp
, tmplen
+1); /* include the \0 */
1498 * Given a open file * to /proc/pid/{u,g}id_map, and an id
1499 * valid in the caller's namespace, return the id mapped into
1501 * Returns the mapped id, or -1 on error.
1504 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
1506 unsigned int nsuid
, // base id for a range in the idfile's namespace
1507 hostuid
, // base id for a range in the caller's namespace
1508 count
; // number of ids in this range
1512 fseek(idfile
, 0L, SEEK_SET
);
1513 while (fgets(line
, 400, idfile
)) {
1514 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
1517 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
1519 * uids wrapped around - unexpected as this is a procfile,
1522 lxcfs_error("pid wrapparound at entry %u %u %u in %s\n",
1523 nsuid
, hostuid
, count
, line
);
1526 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
1528 * now since hostuid <= in_id < hostuid+count, and
1529 * hostuid+count and nsuid+count do not wrap around,
1530 * we know that nsuid+(in_id-hostuid) which must be
1531 * less that nsuid+(count) must not wrap around
1533 return (in_id
- hostuid
) + nsuid
;
1542 * for is_privileged_over,
1543 * specify whether we require the calling uid to be root in his
1546 #define NS_ROOT_REQD true
1547 #define NS_ROOT_OPT false
1551 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
1553 char fpath
[PROCLEN
];
1555 bool answer
= false;
1558 if (victim
== -1 || uid
== -1)
1562 * If the request is one not requiring root in the namespace,
1563 * then having the same uid suffices. (i.e. uid 1000 has write
1564 * access to files owned by uid 1000
1566 if (!req_ns_root
&& uid
== victim
)
1569 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
1570 if (ret
< 0 || ret
>= PROCLEN
)
1572 FILE *f
= fopen(fpath
, "r");
1576 /* if caller's not root in his namespace, reject */
1577 nsuid
= convert_id_to_ns(f
, uid
);
1582 * If victim is not mapped into caller's ns, reject.
1583 * XXX I'm not sure this check is needed given that fuse
1584 * will be sending requests where the vfs has converted
1586 nsuid
= convert_id_to_ns(f
, victim
);
1597 static bool perms_include(int fmode
, mode_t req_mode
)
1601 switch (req_mode
& O_ACCMODE
) {
1609 r
= S_IROTH
| S_IWOTH
;
1614 return ((fmode
& r
) == r
);
1620 * querycg is /a/b/c/d/e
1623 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
1627 if (strlen(taskcg
) <= strlen(querycg
)) {
1628 lxcfs_error("%s\n", "I was fed bad input.");
1632 if ((strcmp(querycg
, "/") == 0) || (strcmp(querycg
, "./") == 0))
1633 start
= strdup(taskcg
+ 1);
1635 start
= strdup(taskcg
+ strlen(querycg
) + 1);
1638 end
= strchr(start
, '/');
1644 static void stripnewline(char *x
)
1646 size_t l
= strlen(x
);
1647 if (l
&& x
[l
-1] == '\n')
1651 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1653 __do_free
char *line
= NULL
;
1654 __do_fclose
FILE *f
= NULL
;
1657 char *answer
= NULL
;
1660 const char *h
= find_mounted_controller(contrl
, &cfd
);
1664 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
1665 if (ret
< 0 || ret
>= PROCLEN
)
1667 if (!(f
= fopen(fnam
, "r")))
1670 while (getline(&line
, &len
, f
) != -1) {
1676 c1
= strchr(line
, ':');
1682 c2
= strchr(c1
, ':');
1688 if (strcmp(c1
, h
) != 0)
1696 answer
= strdup(c2
);
1706 * check whether a fuse context may access a cgroup dir or file
1708 * If file is not null, it is a cgroup file to check under cg.
1709 * If file is null, then we are checking perms on cg itself.
1711 * For files we can check the mode of the list_keys result.
1712 * For cgroups, we must make assumptions based on the files under the
1713 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
1716 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
1718 struct cgfs_files
*k
= NULL
;
1721 k
= cgfs_get_key(contrl
, cg
, file
);
1725 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1726 if (perms_include(k
->mode
>> 6, mode
)) {
1731 if (fc
->gid
== k
->gid
) {
1732 if (perms_include(k
->mode
>> 3, mode
)) {
1737 ret
= perms_include(k
->mode
, mode
);
1744 #define INITSCOPE "/init.scope"
1745 static void prune_init_slice(char *cg
)
1748 size_t cg_len
= strlen(cg
), initscope_len
= strlen(INITSCOPE
);
1750 if (cg_len
< initscope_len
)
1753 point
= cg
+ cg_len
- initscope_len
;
1754 if (strcmp(point
, INITSCOPE
) == 0) {
1763 * If pid is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
1764 * If pid is in /a, he may act on /a/b, but not on /b.
1765 * if the answer is false and nextcg is not NULL, then *nextcg will point
1766 * to a string containing the next cgroup directory under cg, which must be
1767 * freed by the caller.
1769 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
1771 __do_free
char *c2
= NULL
;
1772 bool answer
= false;
1775 c2
= get_pid_cgroup(pid
, contrl
);
1778 prune_init_slice(c2
);
1781 * callers pass in '/' or './' (openat()) for root cgroup, otherwise
1782 * they pass in a cgroup without leading '/'
1784 * The original line here was:
1785 * linecmp = *cg == '/' ? c2 : c2+1;
1786 * TODO: I'm not sure why you'd want to increment when *cg != '/'?
1787 * Serge, do you know?
1789 if (*cg
== '/' || !strncmp(cg
, "./", 2))
1793 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
1795 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
1805 * If pid is in /a/b/c, he may see that /a exists, but not /b or /a/c.
1807 static bool caller_may_see_dir(pid_t pid
, const char *contrl
, const char *cg
)
1809 __do_free
char *c2
= NULL
;
1810 bool answer
= false;
1812 size_t target_len
, task_len
;
1814 if (strcmp(cg
, "/") == 0 || strcmp(cg
, "./") == 0)
1817 c2
= get_pid_cgroup(pid
, contrl
);
1820 prune_init_slice(c2
);
1823 target_len
= strlen(cg
);
1824 task_len
= strlen(task_cg
);
1825 if (task_len
== 0) {
1826 /* Task is in the root cg, it can see everything. This case is
1827 * not handled by the strmcps below, since they test for the
1828 * last /, but that is the first / that we've chopped off
1834 if (strcmp(cg
, task_cg
) == 0) {
1838 if (target_len
< task_len
) {
1839 /* looking up a parent dir */
1840 if (strncmp(task_cg
, cg
, target_len
) == 0 && task_cg
[target_len
] == '/')
1844 if (target_len
> task_len
) {
1845 /* looking up a child dir */
1846 if (strncmp(task_cg
, cg
, task_len
) == 0 && cg
[task_len
] == '/')
1856 * given /cgroup/freezer/a/b, return "freezer".
1857 * the returned char* should NOT be freed.
1859 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
1862 char *contr
, *slash
;
1864 if (strlen(path
) < 9) {
1868 if (*(path
+ 7) != '/') {
1873 contr
= strdupa(p1
);
1878 slash
= strstr(contr
, "/");
1883 for (i
= 0; i
< num_hierarchies
; i
++) {
1884 if (hierarchies
[i
] && strcmp(hierarchies
[i
], contr
) == 0)
1885 return hierarchies
[i
];
1892 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
1893 * Note that the returned value may include files (keynames) etc
1895 static const char *find_cgroup_in_path(const char *path
)
1899 if (strlen(path
) < 9) {
1903 p1
= strstr(path
+ 8, "/");
1913 * split the last path element from the path in @cg.
1914 * @dir is newly allocated and should be freed, @last not
1916 static void get_cgdir_and_path(const char *cg
, char **dir
, char **last
)
1923 *last
= strrchr(cg
, '/');
1928 p
= strrchr(*dir
, '/');
1933 * FUSE ops for /cgroup
1936 int cg_getattr(const char *path
, struct stat
*sb
)
1938 __do_free
char * cgdir
= NULL
;
1939 struct timespec now
;
1940 struct fuse_context
*fc
= fuse_get_context();
1941 char *last
= NULL
, *path1
, *path2
;
1942 struct cgfs_files
*k
= NULL
;
1944 const char *controller
= NULL
;
1951 memset(sb
, 0, sizeof(struct stat
));
1953 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1956 sb
->st_uid
= sb
->st_gid
= 0;
1957 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1960 if (strcmp(path
, "/cgroup") == 0) {
1961 sb
->st_mode
= S_IFDIR
| 00755;
1966 controller
= pick_controller_from_path(fc
, path
);
1969 cgroup
= find_cgroup_in_path(path
);
1971 /* this is just /cgroup/controller, return it as a dir */
1972 sb
->st_mode
= S_IFDIR
| 00755;
1977 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1987 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1990 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
1991 * Then check that caller's cgroup is under path if last is a child
1992 * cgroup, or cgdir if last is a file */
1994 if (is_child_cgroup(controller
, path1
, path2
)) {
1995 if (!caller_may_see_dir(initpid
, controller
, cgroup
)) {
1999 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
2000 /* this is just /cgroup/controller, return it as a dir */
2001 sb
->st_mode
= S_IFDIR
| 00555;
2006 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
2011 // get uid, gid, from '/tasks' file and make up a mode
2012 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2013 sb
->st_mode
= S_IFDIR
| 00755;
2014 k
= cgfs_get_key(controller
, cgroup
, NULL
);
2016 sb
->st_uid
= sb
->st_gid
= 0;
2018 sb
->st_uid
= k
->uid
;
2019 sb
->st_gid
= k
->gid
;
2027 if ((k
= cgfs_get_key(controller
, path1
, path2
)) != NULL
) {
2028 sb
->st_mode
= S_IFREG
| k
->mode
;
2030 sb
->st_uid
= k
->uid
;
2031 sb
->st_gid
= k
->gid
;
2034 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
2045 int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
2047 struct fuse_context
*fc
= fuse_get_context();
2049 struct file_info
*dir_info
;
2050 char *controller
= NULL
;
2055 if (strcmp(path
, "/cgroup") == 0) {
2059 // return list of keys for the controller, and list of child cgroups
2060 controller
= pick_controller_from_path(fc
, path
);
2064 cgroup
= find_cgroup_in_path(path
);
2066 /* this is just /cgroup/controller, return its contents */
2071 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2075 if (!caller_may_see_dir(initpid
, controller
, cgroup
))
2077 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
2081 /* we'll free this at cg_releasedir */
2082 dir_info
= malloc(sizeof(*dir_info
));
2085 dir_info
->controller
= must_copy_string(controller
);
2086 dir_info
->cgroup
= must_copy_string(cgroup
);
2087 dir_info
->type
= LXC_TYPE_CGDIR
;
2088 dir_info
->buf
= NULL
;
2089 dir_info
->file
= NULL
;
2090 dir_info
->buflen
= 0;
2092 fi
->fh
= (unsigned long)dir_info
;
2096 int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2097 struct fuse_file_info
*fi
)
2099 __do_free
char *nextcg
= NULL
;
2100 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2101 struct cgfs_files
**list
= NULL
;
2103 struct fuse_context
*fc
= fuse_get_context();
2104 char **clist
= NULL
;
2106 if (filler(buf
, ".", NULL
, 0) != 0 || filler(buf
, "..", NULL
, 0) != 0)
2109 if (d
->type
!= LXC_TYPE_CGDIR
) {
2110 lxcfs_error("%s\n", "Internal error: file cache info used in readdir.");
2113 if (!d
->cgroup
&& !d
->controller
) {
2114 // ls /var/lib/lxcfs/cgroup - just show list of controllers
2117 for (i
= 0; i
< num_hierarchies
; i
++) {
2118 if (hierarchies
[i
] && filler(buf
, hierarchies
[i
], NULL
, 0) != 0) {
2125 if (!cgfs_list_keys(d
->controller
, d
->cgroup
, &list
)) {
2126 // not a valid cgroup
2131 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2134 if (!caller_is_in_ancestor(initpid
, d
->controller
, d
->cgroup
, &nextcg
)) {
2136 ret
= filler(buf
, nextcg
, NULL
, 0);
2146 for (i
= 0; list
&& list
[i
]; i
++) {
2147 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
2153 // now get the list of child cgroups
2155 if (!cgfs_list_children(d
->controller
, d
->cgroup
, &clist
)) {
2160 for (i
= 0; clist
[i
]; i
++) {
2161 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
2172 for (i
= 0; clist
[i
]; i
++)
2173 __free_move__(clist
[i
]);
2174 __free_move__(clist
);
2179 static void do_release_file_info(struct fuse_file_info
*fi
)
2181 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2188 __free_move__(f
->controller
);
2189 __free_move__(f
->cgroup
);
2190 __free_move__(f
->file
);
2191 __free_move__(f
->buf
);
2195 int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
2197 do_release_file_info(fi
);
2201 int cg_open(const char *path
, struct fuse_file_info
*fi
)
2203 __do_free
char *cgdir
= NULL
;
2205 char *last
= NULL
, *path1
, *path2
, *controller
;
2206 struct cgfs_files
*k
= NULL
;
2207 struct file_info
*file_info
;
2208 struct fuse_context
*fc
= fuse_get_context();
2214 controller
= pick_controller_from_path(fc
, path
);
2217 cgroup
= find_cgroup_in_path(path
);
2221 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2230 k
= cgfs_get_key(controller
, path1
, path2
);
2237 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2240 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
2244 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
2249 /* we'll free this at cg_release */
2250 file_info
= malloc(sizeof(*file_info
));
2255 file_info
->controller
= must_copy_string(controller
);
2256 file_info
->cgroup
= must_copy_string(path1
);
2257 file_info
->file
= must_copy_string(path2
);
2258 file_info
->type
= LXC_TYPE_CGFILE
;
2259 file_info
->buf
= NULL
;
2260 file_info
->buflen
= 0;
2262 fi
->fh
= (unsigned long)file_info
;
2269 int cg_access(const char *path
, int mode
)
2271 __do_free
char *cgdir
= NULL
;
2274 char *path1
, *path2
, *controller
;
2276 struct cgfs_files
*k
= NULL
;
2277 struct fuse_context
*fc
= fuse_get_context();
2279 if (strcmp(path
, "/cgroup") == 0)
2285 controller
= pick_controller_from_path(fc
, path
);
2288 cgroup
= find_cgroup_in_path(path
);
2290 // access("/sys/fs/cgroup/systemd", mode) - rx allowed, w not
2291 if ((mode
& W_OK
) == 0)
2296 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2305 k
= cgfs_get_key(controller
, path1
, path2
);
2307 if ((mode
& W_OK
) == 0)
2315 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2318 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
2322 if (!fc_may_access(fc
, controller
, path1
, path2
, mode
)) {
2333 int cg_release(const char *path
, struct fuse_file_info
*fi
)
2335 do_release_file_info(fi
);
2339 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
2341 static bool wait_for_sock(int sock
, int timeout
)
2343 struct epoll_event ev
;
2344 int epfd
, ret
, now
, starttime
, deltatime
, saved_errno
;
2346 if ((starttime
= time(NULL
)) < 0)
2349 if ((epfd
= epoll_create(1)) < 0) {
2350 lxcfs_error("%s\n", "Failed to create epoll socket: %m.");
2354 ev
.events
= POLLIN_SET
;
2356 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, sock
, &ev
) < 0) {
2357 lxcfs_error("%s\n", "Failed adding socket to epoll: %m.");
2363 if ((now
= time(NULL
)) < 0) {
2368 deltatime
= (starttime
+ timeout
) - now
;
2369 if (deltatime
< 0) { // timeout
2374 ret
= epoll_wait(epfd
, &ev
, 1, 1000*deltatime
+ 1);
2375 if (ret
< 0 && errno
== EINTR
)
2377 saved_errno
= errno
;
2381 errno
= saved_errno
;
2387 static int msgrecv(int sockfd
, void *buf
, size_t len
)
2389 if (!wait_for_sock(sockfd
, 2))
2391 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
2394 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
2396 struct msghdr msg
= { 0 };
2398 struct cmsghdr
*cmsg
;
2399 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2404 if (msgrecv(sock
, buf
, 1) != 1) {
2405 lxcfs_error("%s\n", "Error getting reply from server over socketpair.");
2406 return SEND_CREDS_FAIL
;
2410 msg
.msg_control
= cmsgbuf
;
2411 msg
.msg_controllen
= sizeof(cmsgbuf
);
2413 cmsg
= CMSG_FIRSTHDR(&msg
);
2414 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
2415 cmsg
->cmsg_level
= SOL_SOCKET
;
2416 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
2417 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
2419 msg
.msg_name
= NULL
;
2420 msg
.msg_namelen
= 0;
2424 iov
.iov_len
= sizeof(buf
);
2428 if (sendmsg(sock
, &msg
, 0) < 0) {
2429 lxcfs_error("Failed at sendmsg: %s.\n",strerror(errno
));
2431 return SEND_CREDS_NOTSK
;
2432 return SEND_CREDS_FAIL
;
2435 return SEND_CREDS_OK
;
2438 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
2440 struct msghdr msg
= { 0 };
2442 struct cmsghdr
*cmsg
;
2443 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2454 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
2455 lxcfs_error("Failed to set passcred: %s\n", strerror(errno
));
2459 if (write(sock
, buf
, 1) != 1) {
2460 lxcfs_error("Failed to start write on scm fd: %s\n", strerror(errno
));
2464 msg
.msg_name
= NULL
;
2465 msg
.msg_namelen
= 0;
2466 msg
.msg_control
= cmsgbuf
;
2467 msg
.msg_controllen
= sizeof(cmsgbuf
);
2470 iov
.iov_len
= sizeof(buf
);
2474 if (!wait_for_sock(sock
, 2)) {
2475 lxcfs_error("Timed out waiting for scm_cred: %s\n", strerror(errno
));
2478 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
2480 lxcfs_error("Failed to receive scm_cred: %s\n", strerror(errno
));
2484 cmsg
= CMSG_FIRSTHDR(&msg
);
2486 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
2487 cmsg
->cmsg_level
== SOL_SOCKET
&&
2488 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
2489 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
2496 struct pid_ns_clone_args
{
2500 int (*wrapped
) (int, pid_t
); // pid_from_ns or pid_to_ns
2504 * pid_ns_clone_wrapper - wraps pid_to_ns or pid_from_ns for usage
2505 * with clone(). This simply writes '1' as ACK back to the parent
2506 * before calling the actual wrapped function.
2508 static int pid_ns_clone_wrapper(void *arg
) {
2509 struct pid_ns_clone_args
* args
= (struct pid_ns_clone_args
*) arg
;
2512 close(args
->cpipe
[0]);
2513 if (write(args
->cpipe
[1], &b
, sizeof(char)) < 0)
2514 lxcfs_error("(child): error on write: %s.\n", strerror(errno
));
2515 close(args
->cpipe
[1]);
2516 return args
->wrapped(args
->sock
, args
->tpid
);
2520 * pid_to_ns - reads pids from a ucred over a socket, then writes the
2521 * int value back over the socket. This shifts the pid from the
2522 * sender's pidns into tpid's pidns.
2524 static int pid_to_ns(int sock
, pid_t tpid
)
2529 while (recv_creds(sock
, &cred
, &v
)) {
2532 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
2540 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
2541 * in your old pidns. Only children which you clone will be in the target
2542 * pidns. So the pid_to_ns_wrapper does the setns, then clones a child to
2543 * actually convert pids.
2545 * Note: glibc's fork() does not respect pidns, which can lead to failed
2546 * assertions inside glibc (and thus failed forks) if the child's pid in
2547 * the pidns and the parent pid outside are identical. Using clone prevents
2550 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
2552 int newnsfd
= -1, ret
, cpipe
[2];
2557 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2558 if (ret
< 0 || ret
>= sizeof(fnam
))
2560 newnsfd
= open(fnam
, O_RDONLY
);
2563 if (setns(newnsfd
, 0) < 0)
2567 if (pipe(cpipe
) < 0)
2570 struct pid_ns_clone_args args
= {
2574 .wrapped
= &pid_to_ns
2576 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2577 void *stack
= alloca(stack_size
);
2579 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2583 // give the child 1 second to be done forking and
2585 if (!wait_for_sock(cpipe
[0], 1))
2587 ret
= read(cpipe
[0], &v
, 1);
2588 if (ret
!= sizeof(char) || v
!= '1')
2591 if (!wait_for_pid(cpid
))
2597 * To read cgroup files with a particular pid, we will setns into the child
2598 * pidns, open a pipe, fork a child - which will be the first to really be in
2599 * the child ns - which does the cgfs_get_value and writes the data to the pipe.
2601 bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
2603 int sock
[2] = {-1, -1};
2604 __do_free
char *tmpdata
= NULL
;
2606 pid_t qpid
, cpid
= -1;
2607 bool answer
= false;
2610 size_t sz
= 0, asz
= 0;
2612 if (!cgfs_get_value(contrl
, cg
, file
, &tmpdata
))
2616 * Now we read the pids from returned data one by one, pass
2617 * them into a child in the target namespace, read back the
2618 * translated pids, and put them into our to-return data
2621 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2622 perror("socketpair");
2630 if (!cpid
) // child - exits when done
2631 pid_to_ns_wrapper(sock
[1], tpid
);
2633 char *ptr
= tmpdata
;
2636 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
2638 ret
= send_creds(sock
[0], &cred
, v
, true);
2640 if (ret
== SEND_CREDS_NOTSK
)
2642 if (ret
== SEND_CREDS_FAIL
)
2645 // read converted results
2646 if (!wait_for_sock(sock
[0], 2)) {
2647 lxcfs_error("Timed out waiting for pid from child: %s.\n", strerror(errno
));
2650 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2651 lxcfs_error("Error reading pid from child: %s.\n", strerror(errno
));
2654 must_strcat_pid(d
, &sz
, &asz
, qpid
);
2656 ptr
= strchr(ptr
, '\n');
2662 cred
.pid
= getpid();
2664 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
2665 // failed to ask child to exit
2666 lxcfs_error("Failed to ask child to exit: %s.\n", strerror(errno
));
2675 if (sock
[0] != -1) {
2682 int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2683 struct fuse_file_info
*fi
)
2685 __do_free
char *data
= NULL
;
2686 struct fuse_context
*fc
= fuse_get_context();
2687 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2688 struct cgfs_files
*k
= NULL
;
2692 if (f
->type
!= LXC_TYPE_CGFILE
) {
2693 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_read.");
2706 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2712 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) {
2717 if (strcmp(f
->file
, "tasks") == 0 ||
2718 strcmp(f
->file
, "/tasks") == 0 ||
2719 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2720 strcmp(f
->file
, "cgroup.procs") == 0)
2721 // special case - we have to translate the pids
2722 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
2724 r
= cgfs_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
2738 memcpy(buf
, data
, s
);
2739 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
2748 static int pid_from_ns(int sock
, pid_t tpid
)
2758 if (!wait_for_sock(sock
, 2)) {
2759 lxcfs_error("%s\n", "Timeout reading from parent.");
2762 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
2763 lxcfs_error("Bad read from parent: %s.\n", strerror(errno
));
2766 if (vpid
== -1) // done
2770 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
2772 cred
.pid
= getpid();
2773 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
2780 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
2782 int newnsfd
= -1, ret
, cpipe
[2];
2787 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2788 if (ret
< 0 || ret
>= sizeof(fnam
))
2790 newnsfd
= open(fnam
, O_RDONLY
);
2793 if (setns(newnsfd
, 0) < 0)
2797 if (pipe(cpipe
) < 0)
2800 struct pid_ns_clone_args args
= {
2804 .wrapped
= &pid_from_ns
2806 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2807 void *stack
= alloca(stack_size
);
2809 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2813 // give the child 1 second to be done forking and
2815 if (!wait_for_sock(cpipe
[0], 1))
2817 ret
= read(cpipe
[0], &v
, 1);
2818 if (ret
!= sizeof(char) || v
!= '1')
2821 if (!wait_for_pid(cpid
))
2827 * Given host @uid, return the uid to which it maps in
2828 * @pid's user namespace, or -1 if none.
2830 bool hostuid_to_ns(uid_t uid
, pid_t pid
, uid_t
*answer
)
2835 sprintf(line
, "/proc/%d/uid_map", pid
);
2836 if ((f
= fopen(line
, "r")) == NULL
) {
2840 *answer
= convert_id_to_ns(f
, uid
);
2849 * get_pid_creds: get the real uid and gid of @pid from
2851 * (XXX should we use euid here?)
2853 void get_pid_creds(pid_t pid
, uid_t
*uid
, gid_t
*gid
)
2862 sprintf(line
, "/proc/%d/status", pid
);
2863 if ((f
= fopen(line
, "r")) == NULL
) {
2864 lxcfs_error("Error opening %s: %s\n", line
, strerror(errno
));
2867 while (fgets(line
, 400, f
)) {
2868 if (strncmp(line
, "Uid:", 4) == 0) {
2869 if (sscanf(line
+4, "%u", &u
) != 1) {
2870 lxcfs_error("bad uid line for pid %u\n", pid
);
2875 } else if (strncmp(line
, "Gid:", 4) == 0) {
2876 if (sscanf(line
+4, "%u", &g
) != 1) {
2877 lxcfs_error("bad gid line for pid %u\n", pid
);
2888 * May the requestor @r move victim @v to a new cgroup?
2889 * This is allowed if
2890 * . they are the same task
2891 * . they are ownedy by the same uid
2892 * . @r is root on the host, or
2893 * . @v's uid is mapped into @r's where @r is root.
2895 bool may_move_pid(pid_t r
, uid_t r_uid
, pid_t v
)
2897 uid_t v_uid
, tmpuid
;
2904 get_pid_creds(v
, &v_uid
, &v_gid
);
2907 if (hostuid_to_ns(r_uid
, r
, &tmpuid
) && tmpuid
== 0
2908 && hostuid_to_ns(v_uid
, r
, &tmpuid
))
2913 static bool do_write_pids(pid_t tpid
, uid_t tuid
, const char *contrl
, const char *cg
,
2914 const char *file
, const char *buf
)
2916 int sock
[2] = {-1, -1};
2917 pid_t qpid
, cpid
= -1;
2918 FILE *pids_file
= NULL
;
2919 bool answer
= false, fail
= false;
2921 pids_file
= open_pids_file(contrl
, cg
);
2926 * write the pids to a socket, have helper in writer's pidns
2927 * call movepid for us
2929 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2930 perror("socketpair");
2938 if (!cpid
) { // child
2940 pid_from_ns_wrapper(sock
[1], tpid
);
2943 const char *ptr
= buf
;
2944 while (sscanf(ptr
, "%d", &qpid
) == 1) {
2948 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2949 lxcfs_error("Error writing pid to child: %s.\n", strerror(errno
));
2953 if (recv_creds(sock
[0], &cred
, &v
)) {
2955 if (!may_move_pid(tpid
, tuid
, cred
.pid
)) {
2959 if (fprintf(pids_file
, "%d", (int) cred
.pid
) < 0)
2964 ptr
= strchr(ptr
, '\n');
2970 /* All good, write the value */
2972 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
2973 lxcfs_error("%s\n", "Warning: failed to ask child to exit.");
2981 if (sock
[0] != -1) {
2986 if (fclose(pids_file
) != 0)
2992 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2993 struct fuse_file_info
*fi
)
2995 struct fuse_context
*fc
= fuse_get_context();
2996 char *localbuf
= NULL
;
2997 struct cgfs_files
*k
= NULL
;
2998 struct file_info
*f
= (struct file_info
*)fi
->fh
;
3001 if (f
->type
!= LXC_TYPE_CGFILE
) {
3002 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_write.");
3012 localbuf
= alloca(size
+1);
3013 localbuf
[size
] = '\0';
3014 memcpy(localbuf
, buf
, size
);
3016 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
3021 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
3026 if (strcmp(f
->file
, "tasks") == 0 ||
3027 strcmp(f
->file
, "/tasks") == 0 ||
3028 strcmp(f
->file
, "/cgroup.procs") == 0 ||
3029 strcmp(f
->file
, "cgroup.procs") == 0)
3030 // special case - we have to translate the pids
3031 r
= do_write_pids(fc
->pid
, fc
->uid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
3033 r
= cgfs_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
3043 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
3045 __do_free
char *cgdir
= NULL
;
3046 struct fuse_context
*fc
= fuse_get_context();
3047 char *last
= NULL
, *path1
, *path2
, *controller
;
3048 struct cgfs_files
*k
= NULL
;
3055 if (strcmp(path
, "/cgroup") == 0)
3058 controller
= pick_controller_from_path(fc
, path
);
3060 return errno
== ENOENT
? -EPERM
: -errno
;
3062 cgroup
= find_cgroup_in_path(path
);
3064 /* this is just /cgroup/controller */
3067 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3077 if (is_child_cgroup(controller
, path1
, path2
)) {
3078 // get uid, gid, from '/tasks' file and make up a mode
3079 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
3080 k
= cgfs_get_key(controller
, cgroup
, "tasks");
3083 k
= cgfs_get_key(controller
, path1
, path2
);
3091 * This being a fuse request, the uid and gid must be valid
3092 * in the caller's namespace. So we can just check to make
3093 * sure that the caller is root in his uid, and privileged
3094 * over the file's current owner.
3096 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
3101 ret
= cgfs_chown_file(controller
, cgroup
, uid
, gid
);
3109 int cg_chmod(const char *path
, mode_t mode
)
3111 __do_free
char *cgdir
= NULL
;
3112 struct fuse_context
*fc
= fuse_get_context();
3113 char *last
= NULL
, *path1
, *path2
, *controller
;
3114 struct cgfs_files
*k
= NULL
;
3121 if (strcmp(path
, "/cgroup") == 0)
3124 controller
= pick_controller_from_path(fc
, path
);
3126 return errno
== ENOENT
? -EPERM
: -errno
;
3128 cgroup
= find_cgroup_in_path(path
);
3130 /* this is just /cgroup/controller */
3133 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3143 if (is_child_cgroup(controller
, path1
, path2
)) {
3144 // get uid, gid, from '/tasks' file and make up a mode
3145 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
3146 k
= cgfs_get_key(controller
, cgroup
, "tasks");
3149 k
= cgfs_get_key(controller
, path1
, path2
);
3157 * This being a fuse request, the uid and gid must be valid
3158 * in the caller's namespace. So we can just check to make
3159 * sure that the caller is root in his uid, and privileged
3160 * over the file's current owner.
3162 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
3167 if (!cgfs_chmod_file(controller
, cgroup
, mode
)) {
3178 int cg_mkdir(const char *path
, mode_t mode
)
3180 __do_free
char *cgdir
= NULL
, *next
= NULL
;
3181 struct fuse_context
*fc
= fuse_get_context();
3182 char *last
= NULL
, *path1
, *controller
;
3189 controller
= pick_controller_from_path(fc
, path
);
3191 return errno
== ENOENT
? -EPERM
: -errno
;
3193 cgroup
= find_cgroup_in_path(path
);
3197 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3203 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3206 if (!caller_is_in_ancestor(initpid
, controller
, path1
, &next
)) {
3209 else if (last
&& strcmp(next
, last
) == 0)
3216 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
3220 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
3225 ret
= cgfs_create(controller
, cgroup
, fc
->uid
, fc
->gid
);
3231 int cg_rmdir(const char *path
)
3233 __do_free
char *cgdir
= NULL
, *next
= NULL
;
3234 struct fuse_context
*fc
= fuse_get_context();
3235 char *last
= NULL
, *controller
;
3242 controller
= pick_controller_from_path(fc
, path
);
3243 if (!controller
) /* Someone's trying to delete "/cgroup". */
3246 cgroup
= find_cgroup_in_path(path
);
3247 if (!cgroup
) /* Someone's trying to delete a controller e.g. "/blkio". */
3250 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
3252 /* Someone's trying to delete a cgroup on the same level as the
3253 * "/lxc" cgroup e.g. rmdir "/cgroup/blkio/lxc" or
3254 * rmdir "/cgroup/blkio/init.slice".
3260 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3263 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, &next
)) {
3264 if (!last
|| (next
&& (strcmp(next
, last
) == 0)))
3271 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
3275 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
3280 if (!cgfs_remove(controller
, cgroup
)) {
3291 static bool startswith(const char *line
, const char *pref
)
3293 if (strncmp(line
, pref
, strlen(pref
)) == 0)
3298 static void parse_memstat(char *memstat
, unsigned long *cached
,
3299 unsigned long *active_anon
, unsigned long *inactive_anon
,
3300 unsigned long *active_file
, unsigned long *inactive_file
,
3301 unsigned long *unevictable
, unsigned long *shmem
)
3306 if (startswith(memstat
, "total_cache")) {
3307 sscanf(memstat
+ 11, "%lu", cached
);
3309 } else if (startswith(memstat
, "total_active_anon")) {
3310 sscanf(memstat
+ 17, "%lu", active_anon
);
3311 *active_anon
/= 1024;
3312 } else if (startswith(memstat
, "total_inactive_anon")) {
3313 sscanf(memstat
+ 19, "%lu", inactive_anon
);
3314 *inactive_anon
/= 1024;
3315 } else if (startswith(memstat
, "total_active_file")) {
3316 sscanf(memstat
+ 17, "%lu", active_file
);
3317 *active_file
/= 1024;
3318 } else if (startswith(memstat
, "total_inactive_file")) {
3319 sscanf(memstat
+ 19, "%lu", inactive_file
);
3320 *inactive_file
/= 1024;
3321 } else if (startswith(memstat
, "total_unevictable")) {
3322 sscanf(memstat
+ 17, "%lu", unevictable
);
3323 *unevictable
/= 1024;
3324 } else if (startswith(memstat
, "total_shmem")) {
3325 sscanf(memstat
+ 11, "%lu", shmem
);
3328 eol
= strchr(memstat
, '\n');
3335 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
3341 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
3343 size_t len
= strlen(key
);
3347 if (startswith(str
, key
)) {
3348 sscanf(str
+ len
, "%lu", v
);
3351 eol
= strchr(str
, '\n');
3358 static int read_file(const char *path
, char *buf
, size_t size
,
3359 struct file_info
*d
)
3361 __do_free
char *line
= NULL
;
3362 __do_fclose
FILE *f
= NULL
;
3363 size_t linelen
= 0, total_len
= 0;
3364 char *cache
= d
->buf
;
3365 size_t cache_size
= d
->buflen
;
3367 f
= fopen(path
, "r");
3371 while (getline(&line
, &linelen
, f
) != -1) {
3372 ssize_t l
= snprintf(cache
, cache_size
, "%s", line
);
3374 perror("Error writing to cache");
3377 if (l
>= cache_size
) {
3378 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3386 d
->size
= total_len
;
3387 if (total_len
> size
)
3390 /* read from off 0 */
3391 memcpy(buf
, d
->buf
, total_len
);
3396 * FUSE ops for /proc
3399 static unsigned long get_memlimit(const char *cgroup
, const char *file
)
3401 __do_free
char *memlimit_str
= NULL
;
3402 unsigned long memlimit
= -1;
3404 if (cgfs_get_value("memory", cgroup
, file
, &memlimit_str
))
3405 memlimit
= strtoul(memlimit_str
, NULL
, 10);
3410 static unsigned long get_min_memlimit(const char *cgroup
, const char *file
)
3412 char *copy
= strdupa(cgroup
);
3413 unsigned long memlimit
= 0, retlimit
;
3415 retlimit
= get_memlimit(copy
, file
);
3417 while (strcmp(copy
, "/") != 0) {
3418 copy
= dirname(copy
);
3419 memlimit
= get_memlimit(copy
, file
);
3420 if (memlimit
!= -1 && memlimit
< retlimit
)
3421 retlimit
= memlimit
;
3427 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
3428 struct fuse_file_info
*fi
)
3430 __do_free
char *cg
= NULL
, *line
= NULL
, *memusage_str
= NULL
,
3431 *memstat_str
= NULL
, *memswlimit_str
= NULL
,
3432 *memswusage_str
= NULL
;
3433 __do_fclose
FILE *f
= NULL
;
3434 struct fuse_context
*fc
= fuse_get_context();
3435 struct lxcfs_opts
*opts
= (struct lxcfs_opts
*) fuse_get_context()->private_data
;
3436 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3437 unsigned long memlimit
= 0, memusage
= 0, memswlimit
= 0, memswusage
= 0,
3438 cached
= 0, hosttotal
= 0, active_anon
= 0, inactive_anon
= 0,
3439 active_file
= 0, inactive_file
= 0, unevictable
= 0, shmem
= 0,
3441 size_t linelen
= 0, total_len
= 0, rv
= 0;
3442 char *cache
= d
->buf
;
3443 size_t cache_size
= d
->buflen
;
3446 if (offset
> d
->size
)
3450 int left
= d
->size
- offset
;
3451 total_len
= left
> size
? size
: left
;
3452 memcpy(buf
, cache
+ offset
, total_len
);
3456 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3459 cg
= get_pid_cgroup(initpid
, "memory");
3461 return read_file("/proc/meminfo", buf
, size
, d
);
3462 prune_init_slice(cg
);
3464 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
3465 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
3467 if (!cgfs_get_value("memory", cg
, "memory.stat", &memstat_str
))
3470 // Following values are allowed to fail, because swapaccount might be turned
3471 // off for current kernel
3472 if(cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
) &&
3473 cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
))
3475 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
3476 memswusage
= strtoul(memswusage_str
, NULL
, 10);
3478 memswlimit
= memswlimit
/ 1024;
3479 memswusage
= memswusage
/ 1024;
3482 memusage
= strtoul(memusage_str
, NULL
, 10);
3486 parse_memstat(memstat_str
, &cached
, &active_anon
,
3487 &inactive_anon
, &active_file
, &inactive_file
,
3488 &unevictable
, &shmem
);
3490 f
= fopen("/proc/meminfo", "r");
3494 while (getline(&line
, &linelen
, f
) != -1) {
3496 char *printme
, lbuf
[100];
3498 memset(lbuf
, 0, 100);
3499 if (startswith(line
, "MemTotal:")) {
3500 sscanf(line
+sizeof("MemTotal:")-1, "%lu", &hosttotal
);
3501 if (hosttotal
< memlimit
)
3502 memlimit
= hosttotal
;
3503 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
3505 } else if (startswith(line
, "MemFree:")) {
3506 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
3508 } else if (startswith(line
, "MemAvailable:")) {
3509 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
+ cached
);
3511 } else if (startswith(line
, "SwapTotal:") && memswlimit
> 0 && opts
->swap_off
== false) {
3512 sscanf(line
+sizeof("SwapTotal:")-1, "%lu", &hostswtotal
);
3513 if (hostswtotal
< memswlimit
)
3514 memswlimit
= hostswtotal
;
3515 snprintf(lbuf
, 100, "SwapTotal: %8lu kB\n", memswlimit
);
3517 } else if (startswith(line
, "SwapTotal:") && opts
->swap_off
== true) {
3518 snprintf(lbuf
, 100, "SwapTotal: %8lu kB\n", 0UL);
3520 } else if (startswith(line
, "SwapFree:") && memswlimit
> 0 && memswusage
> 0 && opts
->swap_off
== false) {
3521 unsigned long swaptotal
= memswlimit
,
3522 swapusage
= memswusage
- memusage
,
3523 swapfree
= swapusage
< swaptotal
? swaptotal
- swapusage
: 0;
3524 snprintf(lbuf
, 100, "SwapFree: %8lu kB\n", swapfree
);
3526 } else if (startswith(line
, "SwapFree:") && opts
->swap_off
== true) {
3527 snprintf(lbuf
, 100, "SwapFree: %8lu kB\n", 0UL);
3529 } else if (startswith(line
, "Slab:")) {
3530 snprintf(lbuf
, 100, "Slab: %8lu kB\n", 0UL);
3532 } else if (startswith(line
, "Buffers:")) {
3533 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
3535 } else if (startswith(line
, "Cached:")) {
3536 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
3538 } else if (startswith(line
, "SwapCached:")) {
3539 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
3541 } else if (startswith(line
, "Active:")) {
3542 snprintf(lbuf
, 100, "Active: %8lu kB\n",
3543 active_anon
+ active_file
);
3545 } else if (startswith(line
, "Inactive:")) {
3546 snprintf(lbuf
, 100, "Inactive: %8lu kB\n",
3547 inactive_anon
+ inactive_file
);
3549 } else if (startswith(line
, "Active(anon)")) {
3550 snprintf(lbuf
, 100, "Active(anon): %8lu kB\n", active_anon
);
3552 } else if (startswith(line
, "Inactive(anon)")) {
3553 snprintf(lbuf
, 100, "Inactive(anon): %8lu kB\n", inactive_anon
);
3555 } else if (startswith(line
, "Active(file)")) {
3556 snprintf(lbuf
, 100, "Active(file): %8lu kB\n", active_file
);
3558 } else if (startswith(line
, "Inactive(file)")) {
3559 snprintf(lbuf
, 100, "Inactive(file): %8lu kB\n", inactive_file
);
3561 } else if (startswith(line
, "Unevictable")) {
3562 snprintf(lbuf
, 100, "Unevictable: %8lu kB\n", unevictable
);
3564 } else if (startswith(line
, "SReclaimable")) {
3565 snprintf(lbuf
, 100, "SReclaimable: %8lu kB\n", 0UL);
3567 } else if (startswith(line
, "SUnreclaim")) {
3568 snprintf(lbuf
, 100, "SUnreclaim: %8lu kB\n", 0UL);
3570 } else if (startswith(line
, "Shmem:")) {
3571 snprintf(lbuf
, 100, "Shmem: %8lu kB\n", shmem
);
3573 } else if (startswith(line
, "ShmemHugePages")) {
3574 snprintf(lbuf
, 100, "ShmemHugePages: %8lu kB\n", 0UL);
3576 } else if (startswith(line
, "ShmemPmdMapped")) {
3577 snprintf(lbuf
, 100, "ShmemPmdMapped: %8lu kB\n", 0UL);
3582 l
= snprintf(cache
, cache_size
, "%s", printme
);
3584 perror("Error writing to cache");
3589 if (l
>= cache_size
) {
3590 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3601 d
->size
= total_len
;
3602 if (total_len
> size
) total_len
= size
;
3603 memcpy(buf
, d
->buf
, total_len
);
3611 * Read the cpuset.cpus for cg
3612 * Return the answer in a newly allocated string which must be freed
3614 static char *get_cpuset(const char *cg
)
3618 if (!cgfs_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
3623 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
3625 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
3629 if (sscanf(line
, "processor : %d", &cpu
) != 1)
3631 return cpu_in_cpuset(cpu
, cpuset
);
3635 * Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or `cpu.cfs_period_us`,
3636 * depending on `param`. Parameter value is returned throuh `value`.
3638 static bool read_cpu_cfs_param(const char *cg
, const char *param
, int64_t *value
)
3640 __do_free
char *str
= NULL
;
3642 char file
[11 + 6 + 1]; // cpu.cfs__us + quota/period + \0
3644 sprintf(file
, "cpu.cfs_%s_us", param
);
3646 if (!cgfs_get_value("cpu", cg
, file
, &str
))
3649 if (sscanf(str
, "%ld", value
) != 1)
3659 * Return the maximum number of visible CPUs based on CPU quotas.
3660 * If there is no quota set, zero is returned.
3662 int max_cpu_count(const char *cg
)
3665 int64_t cfs_quota
, cfs_period
;
3667 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
3670 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
3673 if (cfs_quota
<= 0 || cfs_period
<= 0)
3676 rv
= cfs_quota
/ cfs_period
;
3678 /* In case quota/period does not yield a whole number, add one CPU for
3681 if ((cfs_quota
% cfs_period
) > 0)
3684 nprocs
= get_nprocs();
3693 * Determine whether CPU views should be used or not.
3695 bool use_cpuview(const char *cg
)
3700 tmpc
= find_mounted_controller("cpu", &cfd
);
3704 tmpc
= find_mounted_controller("cpuacct", &cfd
);
3712 * check whether this is a '^processor" line in /proc/cpuinfo
3714 static bool is_processor_line(const char *line
)
3718 if (sscanf(line
, "processor : %d", &cpu
) == 1)
3723 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
3724 struct fuse_file_info
*fi
)
3726 __do_free
char *cg
= NULL
, *cpuset
= NULL
, *line
= NULL
;
3727 __do_fclose
FILE *f
= NULL
;
3728 struct fuse_context
*fc
= fuse_get_context();
3729 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3730 size_t linelen
= 0, total_len
= 0, rv
= 0;
3731 bool am_printing
= false, firstline
= true, is_s390x
= false;
3732 int curcpu
= -1, cpu
, max_cpus
= 0;
3734 char *cache
= d
->buf
;
3735 size_t cache_size
= d
->buflen
;
3738 if (offset
> d
->size
)
3742 int left
= d
->size
- offset
;
3743 total_len
= left
> size
? size
: left
;
3744 memcpy(buf
, cache
+ offset
, total_len
);
3748 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3751 cg
= get_pid_cgroup(initpid
, "cpuset");
3753 return read_file("proc/cpuinfo", buf
, size
, d
);
3754 prune_init_slice(cg
);
3756 cpuset
= get_cpuset(cg
);
3760 use_view
= use_cpuview(cg
);
3763 max_cpus
= max_cpu_count(cg
);
3765 f
= fopen("/proc/cpuinfo", "r");
3769 while (getline(&line
, &linelen
, f
) != -1) {
3773 if (strstr(line
, "IBM/S390") != NULL
) {
3779 if (strncmp(line
, "# processors:", 12) == 0)
3781 if (is_processor_line(line
)) {
3782 if (use_view
&& max_cpus
> 0 && (curcpu
+1) == max_cpus
)
3784 am_printing
= cpuline_in_cpuset(line
, cpuset
);
3787 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
3789 perror("Error writing to cache");
3793 if (l
>= cache_size
) {
3794 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3803 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
3805 if (use_view
&& max_cpus
> 0 && (curcpu
+1) == max_cpus
)
3807 if (!cpu_in_cpuset(cpu
, cpuset
))
3810 p
= strchr(line
, ':');
3814 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
3816 perror("Error writing to cache");
3820 if (l
>= cache_size
) {
3821 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3832 l
= snprintf(cache
, cache_size
, "%s", line
);
3834 perror("Error writing to cache");
3838 if (l
>= cache_size
) {
3839 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3850 __do_free
char *origcache
= d
->buf
;
3853 d
->buf
= malloc(d
->buflen
);
3856 cache_size
= d
->buflen
;
3858 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
3859 if (l
< 0 || l
>= cache_size
)
3864 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
3865 if (l
< 0 || l
>= cache_size
)
3870 l
= snprintf(cache
, cache_size
, "%s", origcache
);
3871 if (l
< 0 || l
>= cache_size
)
3877 d
->size
= total_len
;
3878 if (total_len
> size
) total_len
= size
;
3880 /* read from off 0 */
3881 memcpy(buf
, d
->buf
, total_len
);
3887 static uint64_t get_reaper_start_time(pid_t pid
)
3892 /* strlen("/proc/") = 6
3896 * strlen("/stat") = 5
3900 #define __PROC_PID_STAT_LEN (6 + LXCFS_NUMSTRLEN64 + 5 + 1)
3901 char path
[__PROC_PID_STAT_LEN
];
3904 qpid
= lookup_initpid_in_store(pid
);
3906 /* Caller can check for EINVAL on 0. */
3911 ret
= snprintf(path
, __PROC_PID_STAT_LEN
, "/proc/%d/stat", qpid
);
3912 if (ret
< 0 || ret
>= __PROC_PID_STAT_LEN
) {
3913 /* Caller can check for EINVAL on 0. */
3918 f
= fopen(path
, "r");
3920 /* Caller can check for EINVAL on 0. */
3925 /* Note that the *scanf() argument supression requires that length
3926 * modifiers such as "l" are omitted. Otherwise some compilers will yell
3927 * at us. It's like telling someone you're not married and then asking
3928 * if you can bring your wife to the party.
3930 ret
= fscanf(f
, "%*d " /* (1) pid %d */
3931 "%*s " /* (2) comm %s */
3932 "%*c " /* (3) state %c */
3933 "%*d " /* (4) ppid %d */
3934 "%*d " /* (5) pgrp %d */
3935 "%*d " /* (6) session %d */
3936 "%*d " /* (7) tty_nr %d */
3937 "%*d " /* (8) tpgid %d */
3938 "%*u " /* (9) flags %u */
3939 "%*u " /* (10) minflt %lu */
3940 "%*u " /* (11) cminflt %lu */
3941 "%*u " /* (12) majflt %lu */
3942 "%*u " /* (13) cmajflt %lu */
3943 "%*u " /* (14) utime %lu */
3944 "%*u " /* (15) stime %lu */
3945 "%*d " /* (16) cutime %ld */
3946 "%*d " /* (17) cstime %ld */
3947 "%*d " /* (18) priority %ld */
3948 "%*d " /* (19) nice %ld */
3949 "%*d " /* (20) num_threads %ld */
3950 "%*d " /* (21) itrealvalue %ld */
3951 "%" PRIu64
, /* (22) starttime %llu */
3955 /* Caller can check for EINVAL on 0. */
3966 static uint64_t get_reaper_start_time_in_sec(pid_t pid
)
3968 uint64_t clockticks
;
3969 int64_t ticks_per_sec
;
3971 clockticks
= get_reaper_start_time(pid
);
3972 if (clockticks
== 0 && errno
== EINVAL
) {
3973 lxcfs_debug("failed to retrieve start time of pid %d\n", pid
);
3977 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
3978 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
3981 "failed to determine number of clock ticks in a second");
3985 return (clockticks
/= ticks_per_sec
);
3988 static uint64_t get_reaper_age(pid_t pid
)
3990 uint64_t procstart
, uptime
, procage
;
3992 /* We need to substract the time the process has started since system
3993 * boot minus the time when the system has started to get the actual
3996 procstart
= get_reaper_start_time_in_sec(pid
);
3997 procage
= procstart
;
3998 if (procstart
> 0) {
4000 struct timespec spec
;
4002 ret
= clock_gettime(CLOCK_BOOTTIME
, &spec
);
4005 /* We could make this more precise here by using the tv_nsec
4006 * field in the timespec struct and convert it to milliseconds
4007 * and then create a double for the seconds and milliseconds but
4008 * that seems more work than it is worth.
4010 uptime
= spec
.tv_sec
;
4011 procage
= uptime
- procstart
;
4018 * Returns 0 on success.
4019 * It is the caller's responsibility to free `return_usage`, unless this
4020 * function returns an error.
4022 static int read_cpuacct_usage_all(char *cg
, char *cpuset
, struct cpuacct_usage
**return_usage
, int *size
)
4024 int cpucount
= get_nprocs_conf();
4025 struct cpuacct_usage
*cpu_usage
;
4026 int rv
= 0, i
, j
, ret
, read_pos
= 0, read_cnt
;
4028 uint64_t cg_user
, cg_system
;
4029 int64_t ticks_per_sec
;
4030 char *usage_str
= NULL
;
4032 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
4034 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
4037 "read_cpuacct_usage_all failed to determine number of clock ticks "
4042 cpu_usage
= malloc(sizeof(struct cpuacct_usage
) * cpucount
);
4046 if (!cgfs_get_value("cpuacct", cg
, "cpuacct.usage_all", &usage_str
)) {
4051 if (sscanf(usage_str
, "cpu user system\n%n", &read_cnt
) != 0) {
4052 lxcfs_error("read_cpuacct_usage_all reading first line from "
4053 "%s/cpuacct.usage_all failed.\n", cg
);
4058 read_pos
+= read_cnt
;
4060 for (i
= 0, j
= 0; i
< cpucount
; i
++) {
4061 ret
= sscanf(usage_str
+ read_pos
, "%d %lu %lu\n%n", &cg_cpu
, &cg_user
,
4062 &cg_system
, &read_cnt
);
4068 lxcfs_error("read_cpuacct_usage_all reading from %s/cpuacct.usage_all "
4074 read_pos
+= read_cnt
;
4076 /* Convert the time from nanoseconds to USER_HZ */
4077 cpu_usage
[j
].user
= cg_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
4078 cpu_usage
[j
].system
= cg_system
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
4083 *return_usage
= cpu_usage
;
4092 *return_usage
= NULL
;
4098 static unsigned long diff_cpu_usage(struct cpuacct_usage
*older
, struct cpuacct_usage
*newer
, struct cpuacct_usage
*diff
, int cpu_count
)
4101 unsigned long sum
= 0;
4103 for (i
= 0; i
< cpu_count
; i
++) {
4104 if (!newer
[i
].online
)
4107 /* When cpuset is changed on the fly, the CPUs might get reordered.
4108 * We could either reset all counters, or check that the substractions
4109 * below will return expected results.
4111 if (newer
[i
].user
> older
[i
].user
)
4112 diff
[i
].user
= newer
[i
].user
- older
[i
].user
;
4116 if (newer
[i
].system
> older
[i
].system
)
4117 diff
[i
].system
= newer
[i
].system
- older
[i
].system
;
4121 if (newer
[i
].idle
> older
[i
].idle
)
4122 diff
[i
].idle
= newer
[i
].idle
- older
[i
].idle
;
4126 sum
+= diff
[i
].user
;
4127 sum
+= diff
[i
].system
;
4128 sum
+= diff
[i
].idle
;
4134 static void add_cpu_usage(unsigned long *surplus
, struct cpuacct_usage
*usage
, unsigned long *counter
, unsigned long threshold
)
4136 unsigned long free_space
, to_add
;
4138 free_space
= threshold
- usage
->user
- usage
->system
;
4140 if (free_space
> usage
->idle
)
4141 free_space
= usage
->idle
;
4143 to_add
= free_space
> *surplus
? *surplus
: free_space
;
4146 usage
->idle
-= to_add
;
4150 static struct cg_proc_stat
*prune_proc_stat_list(struct cg_proc_stat
*node
)
4152 struct cg_proc_stat
*first
= NULL
, *prev
, *tmp
;
4154 for (prev
= NULL
; node
; ) {
4155 if (!cgfs_param_exist("cpu", node
->cg
, "cpu.shares")) {
4157 lxcfs_debug("Removing stat node for %s\n", node
->cg
);
4160 prev
->next
= node
->next
;
4165 free_proc_stat_node(tmp
);
4177 #define PROC_STAT_PRUNE_INTERVAL 10
4178 static void prune_proc_stat_history(void)
4181 time_t now
= time(NULL
);
4183 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
4184 pthread_rwlock_wrlock(&proc_stat_history
[i
]->lock
);
4186 if ((proc_stat_history
[i
]->lastcheck
+ PROC_STAT_PRUNE_INTERVAL
) > now
) {
4187 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
4191 if (proc_stat_history
[i
]->next
) {
4192 proc_stat_history
[i
]->next
= prune_proc_stat_list(proc_stat_history
[i
]->next
);
4193 proc_stat_history
[i
]->lastcheck
= now
;
4196 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
4200 static struct cg_proc_stat
*find_proc_stat_node(struct cg_proc_stat_head
*head
, const char *cg
)
4202 struct cg_proc_stat
*node
;
4204 pthread_rwlock_rdlock(&head
->lock
);
4207 pthread_rwlock_unlock(&head
->lock
);
4214 if (strcmp(cg
, node
->cg
) == 0)
4216 } while ((node
= node
->next
));
4221 pthread_rwlock_unlock(&head
->lock
);
4222 prune_proc_stat_history();
4226 static struct cg_proc_stat
*new_proc_stat_node(struct cpuacct_usage
*usage
, int cpu_count
, const char *cg
)
4228 struct cg_proc_stat
*node
;
4231 node
= malloc(sizeof(struct cg_proc_stat
));
4239 node
->cg
= malloc(strlen(cg
) + 1);
4243 strcpy(node
->cg
, cg
);
4245 node
->usage
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
4249 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
4251 node
->view
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
4255 node
->cpu_count
= cpu_count
;
4258 if (pthread_mutex_init(&node
->lock
, NULL
) != 0) {
4259 lxcfs_error("%s\n", "Failed to initialize node lock");
4263 for (i
= 0; i
< cpu_count
; i
++) {
4264 node
->view
[i
].user
= 0;
4265 node
->view
[i
].system
= 0;
4266 node
->view
[i
].idle
= 0;
4272 if (node
&& node
->cg
)
4274 if (node
&& node
->usage
)
4276 if (node
&& node
->view
)
4284 static struct cg_proc_stat
*add_proc_stat_node(struct cg_proc_stat
*new_node
)
4286 int hash
= calc_hash(new_node
->cg
) % CPUVIEW_HASH_SIZE
;
4287 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
4288 struct cg_proc_stat
*node
, *rv
= new_node
;
4290 pthread_rwlock_wrlock(&head
->lock
);
4293 head
->next
= new_node
;
4300 if (strcmp(node
->cg
, new_node
->cg
) == 0) {
4301 /* The node is already present, return it */
4302 free_proc_stat_node(new_node
);
4312 node
->next
= new_node
;
4317 pthread_rwlock_unlock(&head
->lock
);
4321 static bool expand_proc_stat_node(struct cg_proc_stat
*node
, int cpu_count
)
4323 struct cpuacct_usage
*new_usage
, *new_view
;
4326 /* Allocate new memory */
4327 new_usage
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
4331 new_view
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
4337 /* Copy existing data & initialize new elements */
4338 for (i
= 0; i
< cpu_count
; i
++) {
4339 if (i
< node
->cpu_count
) {
4340 new_usage
[i
].user
= node
->usage
[i
].user
;
4341 new_usage
[i
].system
= node
->usage
[i
].system
;
4342 new_usage
[i
].idle
= node
->usage
[i
].idle
;
4344 new_view
[i
].user
= node
->view
[i
].user
;
4345 new_view
[i
].system
= node
->view
[i
].system
;
4346 new_view
[i
].idle
= node
->view
[i
].idle
;
4348 new_usage
[i
].user
= 0;
4349 new_usage
[i
].system
= 0;
4350 new_usage
[i
].idle
= 0;
4352 new_view
[i
].user
= 0;
4353 new_view
[i
].system
= 0;
4354 new_view
[i
].idle
= 0;
4361 node
->usage
= new_usage
;
4362 node
->view
= new_view
;
4363 node
->cpu_count
= cpu_count
;
4368 static struct cg_proc_stat
*find_or_create_proc_stat_node(struct cpuacct_usage
*usage
, int cpu_count
, const char *cg
)
4370 int hash
= calc_hash(cg
) % CPUVIEW_HASH_SIZE
;
4371 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
4372 struct cg_proc_stat
*node
;
4374 node
= find_proc_stat_node(head
, cg
);
4377 node
= new_proc_stat_node(usage
, cpu_count
, cg
);
4381 node
= add_proc_stat_node(node
);
4382 lxcfs_debug("New stat node (%d) for %s\n", cpu_count
, cg
);
4385 pthread_mutex_lock(&node
->lock
);
4387 /* If additional CPUs on the host have been enabled, CPU usage counter
4388 * arrays have to be expanded */
4389 if (node
->cpu_count
< cpu_count
) {
4390 lxcfs_debug("Expanding stat node %d->%d for %s\n",
4391 node
->cpu_count
, cpu_count
, cg
);
4393 if (!expand_proc_stat_node(node
, cpu_count
)) {
4394 pthread_mutex_unlock(&node
->lock
);
4395 lxcfs_debug("Unable to expand stat node %d->%d for %s\n",
4396 node
->cpu_count
, cpu_count
, cg
);
4404 static void reset_proc_stat_node(struct cg_proc_stat
*node
, struct cpuacct_usage
*usage
, int cpu_count
)
4408 lxcfs_debug("Resetting stat node for %s\n", node
->cg
);
4409 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
4411 for (i
= 0; i
< cpu_count
; i
++) {
4412 node
->view
[i
].user
= 0;
4413 node
->view
[i
].system
= 0;
4414 node
->view
[i
].idle
= 0;
4417 node
->cpu_count
= cpu_count
;
4420 static int cpuview_proc_stat(const char *cg
, const char *cpuset
, struct cpuacct_usage
*cg_cpu_usage
, int cg_cpu_usage_size
, FILE *f
, char *buf
, size_t buf_size
)
4423 size_t linelen
= 0, total_len
= 0, rv
= 0, l
;
4424 int curcpu
= -1; /* cpu numbering starts at 0 */
4426 int max_cpus
= max_cpu_count(cg
), cpu_cnt
= 0;
4427 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
4428 unsigned long user_sum
= 0, system_sum
= 0, idle_sum
= 0;
4429 unsigned long user_surplus
= 0, system_surplus
= 0;
4430 unsigned long total_sum
, threshold
;
4431 struct cg_proc_stat
*stat_node
;
4432 struct cpuacct_usage
*diff
= NULL
;
4433 int nprocs
= get_nprocs_conf();
4435 if (cg_cpu_usage_size
< nprocs
)
4436 nprocs
= cg_cpu_usage_size
;
4438 /* Read all CPU stats and stop when we've encountered other lines */
4439 while (getline(&line
, &linelen
, f
) != -1) {
4441 char cpu_char
[10]; /* That's a lot of cores */
4442 uint64_t all_used
, cg_used
;
4444 if (strlen(line
) == 0)
4446 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
4447 /* not a ^cpuN line containing a number N */
4451 if (sscanf(cpu_char
, "%d", &physcpu
) != 1)
4454 if (physcpu
>= cg_cpu_usage_size
)
4460 if (!cpu_in_cpuset(physcpu
, cpuset
)) {
4461 for (i
= curcpu
; i
<= physcpu
; i
++) {
4462 cg_cpu_usage
[i
].online
= false;
4467 if (curcpu
< physcpu
) {
4468 /* Some CPUs may be disabled */
4469 for (i
= curcpu
; i
< physcpu
; i
++)
4470 cg_cpu_usage
[i
].online
= false;
4475 cg_cpu_usage
[curcpu
].online
= true;
4477 ret
= sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
4492 all_used
= user
+ nice
+ system
+ iowait
+ irq
+ softirq
+ steal
+ guest
+ guest_nice
;
4493 cg_used
= cg_cpu_usage
[curcpu
].user
+ cg_cpu_usage
[curcpu
].system
;
4495 if (all_used
>= cg_used
) {
4496 cg_cpu_usage
[curcpu
].idle
= idle
+ (all_used
- cg_used
);
4499 lxcfs_error("cpu%d from %s has unexpected cpu time: %lu in /proc/stat, "
4500 "%lu in cpuacct.usage_all; unable to determine idle time\n",
4501 curcpu
, cg
, all_used
, cg_used
);
4502 cg_cpu_usage
[curcpu
].idle
= idle
;
4506 /* Cannot use more CPUs than is available due to cpuset */
4507 if (max_cpus
> cpu_cnt
)
4510 stat_node
= find_or_create_proc_stat_node(cg_cpu_usage
, nprocs
, cg
);
4513 lxcfs_error("unable to find/create stat node for %s\n", cg
);
4518 diff
= malloc(sizeof(struct cpuacct_usage
) * nprocs
);
4525 * If the new values are LOWER than values stored in memory, it means
4526 * the cgroup has been reset/recreated and we should reset too.
4528 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
4529 if (!cg_cpu_usage
[curcpu
].online
)
4532 if (cg_cpu_usage
[curcpu
].user
< stat_node
->usage
[curcpu
].user
)
4533 reset_proc_stat_node(stat_node
, cg_cpu_usage
, nprocs
);
4538 total_sum
= diff_cpu_usage(stat_node
->usage
, cg_cpu_usage
, diff
, nprocs
);
4540 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
4541 stat_node
->usage
[curcpu
].online
= cg_cpu_usage
[curcpu
].online
;
4543 if (!stat_node
->usage
[curcpu
].online
)
4548 stat_node
->usage
[curcpu
].user
+= diff
[curcpu
].user
;
4549 stat_node
->usage
[curcpu
].system
+= diff
[curcpu
].system
;
4550 stat_node
->usage
[curcpu
].idle
+= diff
[curcpu
].idle
;
4552 if (max_cpus
> 0 && i
>= max_cpus
) {
4553 user_surplus
+= diff
[curcpu
].user
;
4554 system_surplus
+= diff
[curcpu
].system
;
4558 /* Calculate usage counters of visible CPUs */
4560 /* threshold = maximum usage per cpu, including idle */
4561 threshold
= total_sum
/ cpu_cnt
* max_cpus
;
4563 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
4567 if (!stat_node
->usage
[curcpu
].online
)
4572 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
4582 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
4585 /* If there is still room, add system */
4589 &diff
[curcpu
].system
,
4593 if (user_surplus
> 0)
4594 lxcfs_debug("leftover user: %lu for %s\n", user_surplus
, cg
);
4595 if (system_surplus
> 0)
4596 lxcfs_debug("leftover system: %lu for %s\n", system_surplus
, cg
);
4598 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
4602 if (!stat_node
->usage
[curcpu
].online
)
4607 stat_node
->view
[curcpu
].user
+= diff
[curcpu
].user
;
4608 stat_node
->view
[curcpu
].system
+= diff
[curcpu
].system
;
4609 stat_node
->view
[curcpu
].idle
+= diff
[curcpu
].idle
;
4611 user_sum
+= stat_node
->view
[curcpu
].user
;
4612 system_sum
+= stat_node
->view
[curcpu
].system
;
4613 idle_sum
+= stat_node
->view
[curcpu
].idle
;
4617 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
4618 if (!stat_node
->usage
[curcpu
].online
)
4621 stat_node
->view
[curcpu
].user
= stat_node
->usage
[curcpu
].user
;
4622 stat_node
->view
[curcpu
].system
= stat_node
->usage
[curcpu
].system
;
4623 stat_node
->view
[curcpu
].idle
= stat_node
->usage
[curcpu
].idle
;
4625 user_sum
+= stat_node
->view
[curcpu
].user
;
4626 system_sum
+= stat_node
->view
[curcpu
].system
;
4627 idle_sum
+= stat_node
->view
[curcpu
].idle
;
4631 /* Render the file */
4633 l
= snprintf(buf
, buf_size
, "cpu %lu 0 %lu %lu 0 0 0 0 0 0\n",
4639 perror("Error writing to cache");
4644 if (l
>= buf_size
) {
4645 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4654 /* Render visible CPUs */
4655 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
4656 if (!stat_node
->usage
[curcpu
].online
)
4661 if (max_cpus
> 0 && i
== max_cpus
)
4664 l
= snprintf(buf
, buf_size
, "cpu%d %lu 0 %lu %lu 0 0 0 0 0 0\n",
4666 stat_node
->view
[curcpu
].user
,
4667 stat_node
->view
[curcpu
].system
,
4668 stat_node
->view
[curcpu
].idle
);
4671 perror("Error writing to cache");
4676 if (l
>= buf_size
) {
4677 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4687 /* Pass the rest of /proc/stat, start with the last line read */
4688 l
= snprintf(buf
, buf_size
, "%s", line
);
4691 perror("Error writing to cache");
4696 if (l
>= buf_size
) {
4697 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4706 /* Pass the rest of the host's /proc/stat */
4707 while (getline(&line
, &linelen
, f
) != -1) {
4708 l
= snprintf(buf
, buf_size
, "%s", line
);
4710 perror("Error writing to cache");
4714 if (l
>= buf_size
) {
4715 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4728 pthread_mutex_unlock(&stat_node
->lock
);
4736 #define CPUALL_MAX_SIZE (BUF_RESERVE_SIZE / 2)
4737 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
4738 struct fuse_file_info
*fi
)
4740 struct fuse_context
*fc
= fuse_get_context();
4741 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4743 char *cpuset
= NULL
;
4745 size_t linelen
= 0, total_len
= 0, rv
= 0;
4746 int curcpu
= -1; /* cpu numbering starts at 0 */
4748 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
4749 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
4750 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0, guest_nice_sum
= 0;
4751 char cpuall
[CPUALL_MAX_SIZE
];
4752 /* reserve for cpu all */
4753 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
4754 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
4756 struct cpuacct_usage
*cg_cpu_usage
= NULL
;
4757 int cg_cpu_usage_size
= 0;
4760 if (offset
> d
->size
)
4764 int left
= d
->size
- offset
;
4765 total_len
= left
> size
? size
: left
;
4766 memcpy(buf
, d
->buf
+ offset
, total_len
);
4770 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
4773 cg
= get_pid_cgroup(initpid
, "cpuset");
4775 return read_file("/proc/stat", buf
, size
, d
);
4776 prune_init_slice(cg
);
4778 cpuset
= get_cpuset(cg
);
4783 * Read cpuacct.usage_all for all CPUs.
4784 * If the cpuacct cgroup is present, it is used to calculate the container's
4785 * CPU usage. If not, values from the host's /proc/stat are used.
4787 if (read_cpuacct_usage_all(cg
, cpuset
, &cg_cpu_usage
, &cg_cpu_usage_size
) != 0) {
4788 lxcfs_debug("%s\n", "proc_stat_read failed to read from cpuacct, "
4789 "falling back to the host's /proc/stat");
4792 f
= fopen("/proc/stat", "r");
4797 if (getline(&line
, &linelen
, f
) < 0) {
4798 lxcfs_error("%s\n", "proc_stat_read read first line failed.");
4802 if (use_cpuview(cg
) && cg_cpu_usage
) {
4803 total_len
= cpuview_proc_stat(cg
, cpuset
, cg_cpu_usage
, cg_cpu_usage_size
,
4804 f
, d
->buf
, d
->buflen
);
4808 while (getline(&line
, &linelen
, f
) != -1) {
4810 char cpu_char
[10]; /* That's a lot of cores */
4812 uint64_t all_used
, cg_used
, new_idle
;
4815 if (strlen(line
) == 0)
4817 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
4818 /* not a ^cpuN line containing a number N, just print it */
4819 l
= snprintf(cache
, cache_size
, "%s", line
);
4821 perror("Error writing to cache");
4825 if (l
>= cache_size
) {
4826 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4836 if (sscanf(cpu_char
, "%d", &physcpu
) != 1)
4838 if (!cpu_in_cpuset(physcpu
, cpuset
))
4842 ret
= sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
4854 if (ret
!= 10 || !cg_cpu_usage
) {
4855 c
= strchr(line
, ' ');
4858 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
4860 perror("Error writing to cache");
4865 if (l
>= cache_size
) {
4866 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4880 if (physcpu
>= cg_cpu_usage_size
)
4883 all_used
= user
+ nice
+ system
+ iowait
+ irq
+ softirq
+ steal
+ guest
+ guest_nice
;
4884 cg_used
= cg_cpu_usage
[physcpu
].user
+ cg_cpu_usage
[physcpu
].system
;
4886 if (all_used
>= cg_used
) {
4887 new_idle
= idle
+ (all_used
- cg_used
);
4890 lxcfs_error("cpu%d from %s has unexpected cpu time: %lu in /proc/stat, "
4891 "%lu in cpuacct.usage_all; unable to determine idle time\n",
4892 curcpu
, cg
, all_used
, cg_used
);
4896 l
= snprintf(cache
, cache_size
, "cpu%d %lu 0 %lu %lu 0 0 0 0 0 0\n",
4897 curcpu
, cg_cpu_usage
[physcpu
].user
, cg_cpu_usage
[physcpu
].system
,
4901 perror("Error writing to cache");
4906 if (l
>= cache_size
) {
4907 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
4916 user_sum
+= cg_cpu_usage
[physcpu
].user
;
4917 system_sum
+= cg_cpu_usage
[physcpu
].system
;
4918 idle_sum
+= new_idle
;
4923 system_sum
+= system
;
4925 iowait_sum
+= iowait
;
4927 softirq_sum
+= softirq
;
4930 guest_nice_sum
+= guest_nice
;
4936 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
4947 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
) {
4948 memcpy(cache
, cpuall
, cpuall_len
);
4949 cache
+= cpuall_len
;
4951 /* shouldn't happen */
4952 lxcfs_error("proc_stat_read copy cpuall failed, cpuall_len=%d.", cpuall_len
);
4956 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
4957 total_len
+= cpuall_len
;
4961 d
->size
= total_len
;
4962 if (total_len
> size
)
4965 memcpy(buf
, d
->buf
, total_len
);
4979 /* This function retrieves the busy time of a group of tasks by looking at
4980 * cpuacct.usage. Unfortunately, this only makes sense when the container has
4981 * been given it's own cpuacct cgroup. If not, this function will take the busy
4982 * time of all other taks that do not actually belong to the container into
4983 * account as well. If someone has a clever solution for this please send a
4986 static unsigned long get_reaper_busy(pid_t task
)
4988 pid_t initpid
= lookup_initpid_in_store(task
);
4989 char *cgroup
= NULL
, *usage_str
= NULL
;
4990 unsigned long usage
= 0;
4995 cgroup
= get_pid_cgroup(initpid
, "cpuacct");
4998 prune_init_slice(cgroup
);
4999 if (!cgfs_get_value("cpuacct", cgroup
, "cpuacct.usage", &usage_str
))
5001 usage
= strtoul(usage_str
, NULL
, 10);
5002 usage
/= 1000000000;
5015 fd
= creat("/tmp/lxcfs-iwashere", 0644);
5022 * We read /proc/uptime and reuse its second field.
5023 * For the first field, we use the mtime for the reaper for
5024 * the calling pid as returned by getreaperage
5026 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
5027 struct fuse_file_info
*fi
)
5029 struct fuse_context
*fc
= fuse_get_context();
5030 struct file_info
*d
= (struct file_info
*)fi
->fh
;
5031 unsigned long int busytime
= get_reaper_busy(fc
->pid
);
5032 char *cache
= d
->buf
;
5033 ssize_t total_len
= 0;
5034 uint64_t idletime
, reaperage
;
5043 if (offset
> d
->size
)
5045 int left
= d
->size
- offset
;
5046 total_len
= left
> size
? size
: left
;
5047 memcpy(buf
, cache
+ offset
, total_len
);
5051 reaperage
= get_reaper_age(fc
->pid
);
5052 /* To understand why this is done, please read the comment to the
5053 * get_reaper_busy() function.
5055 idletime
= reaperage
;
5056 if (reaperage
>= busytime
)
5057 idletime
= reaperage
- busytime
;
5059 total_len
= snprintf(d
->buf
, d
->buflen
, "%"PRIu64
".00 %"PRIu64
".00\n", reaperage
, idletime
);
5060 if (total_len
< 0 || total_len
>= d
->buflen
){
5061 lxcfs_error("%s\n", "failed to write to cache");
5065 d
->size
= (int)total_len
;
5068 if (total_len
> size
) total_len
= size
;
5070 memcpy(buf
, d
->buf
, total_len
);
5074 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
5075 struct fuse_file_info
*fi
)
5078 struct fuse_context
*fc
= fuse_get_context();
5079 struct file_info
*d
= (struct file_info
*)fi
->fh
;
5081 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
5082 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
5083 unsigned long read
= 0, write
= 0;
5084 unsigned long read_merged
= 0, write_merged
= 0;
5085 unsigned long read_sectors
= 0, write_sectors
= 0;
5086 unsigned long read_ticks
= 0, write_ticks
= 0;
5087 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
5088 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
5089 char *cache
= d
->buf
;
5090 size_t cache_size
= d
->buflen
;
5092 size_t linelen
= 0, total_len
= 0, rv
= 0;
5093 unsigned int major
= 0, minor
= 0;
5098 if (offset
> d
->size
)
5102 int left
= d
->size
- offset
;
5103 total_len
= left
> size
? size
: left
;
5104 memcpy(buf
, cache
+ offset
, total_len
);
5108 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
5111 cg
= get_pid_cgroup(initpid
, "blkio");
5113 return read_file("/proc/diskstats", buf
, size
, d
);
5114 prune_init_slice(cg
);
5116 if (!cgfs_get_value("blkio", cg
, "blkio.io_serviced_recursive", &io_serviced_str
))
5118 if (!cgfs_get_value("blkio", cg
, "blkio.io_merged_recursive", &io_merged_str
))
5120 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_bytes_recursive", &io_service_bytes_str
))
5122 if (!cgfs_get_value("blkio", cg
, "blkio.io_wait_time_recursive", &io_wait_time_str
))
5124 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_time_recursive", &io_service_time_str
))
5128 f
= fopen("/proc/diskstats", "r");
5132 while (getline(&line
, &linelen
, f
) != -1) {
5136 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
5140 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
5141 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
5142 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
5143 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
5144 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
5145 read_sectors
= read_sectors
/512;
5146 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
5147 write_sectors
= write_sectors
/512;
5149 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
5150 rd_svctm
= rd_svctm
/1000000;
5151 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
5152 rd_wait
= rd_wait
/1000000;
5153 read_ticks
= rd_svctm
+ rd_wait
;
5155 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
5156 wr_svctm
= wr_svctm
/1000000;
5157 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
5158 wr_wait
= wr_wait
/1000000;
5159 write_ticks
= wr_svctm
+ wr_wait
;
5161 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
5162 tot_ticks
= tot_ticks
/1000000;
5164 memset(lbuf
, 0, 256);
5165 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
)
5166 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
5167 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
5168 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
5172 l
= snprintf(cache
, cache_size
, "%s", lbuf
);
5174 perror("Error writing to fuse buf");
5178 if (l
>= cache_size
) {
5179 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
5189 d
->size
= total_len
;
5190 if (total_len
> size
) total_len
= size
;
5191 memcpy(buf
, d
->buf
, total_len
);
5199 free(io_serviced_str
);
5200 free(io_merged_str
);
5201 free(io_service_bytes_str
);
5202 free(io_wait_time_str
);
5203 free(io_service_time_str
);
5207 static int proc_swaps_read(char *buf
, size_t size
, off_t offset
,
5208 struct fuse_file_info
*fi
)
5210 struct fuse_context
*fc
= fuse_get_context();
5211 struct file_info
*d
= (struct file_info
*)fi
->fh
;
5213 char *memswlimit_str
= NULL
, *memlimit_str
= NULL
, *memusage_str
= NULL
, *memswusage_str
= NULL
;
5214 unsigned long memswlimit
= 0, memlimit
= 0, memusage
= 0, memswusage
= 0, swap_total
= 0, swap_free
= 0;
5215 ssize_t total_len
= 0, rv
= 0;
5217 char *cache
= d
->buf
;
5220 if (offset
> d
->size
)
5224 int left
= d
->size
- offset
;
5225 total_len
= left
> size
? size
: left
;
5226 memcpy(buf
, cache
+ offset
, total_len
);
5230 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
5233 cg
= get_pid_cgroup(initpid
, "memory");
5235 return read_file("/proc/swaps", buf
, size
, d
);
5236 prune_init_slice(cg
);
5238 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
5240 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
5243 memusage
= strtoul(memusage_str
, NULL
, 10);
5245 if (cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
) &&
5246 cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
)) {
5248 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
5249 memswusage
= strtoul(memswusage_str
, NULL
, 10);
5251 swap_total
= (memswlimit
- memlimit
) / 1024;
5252 swap_free
= (memswusage
- memusage
) / 1024;
5255 total_len
= snprintf(d
->buf
, d
->size
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
5257 /* When no mem + swap limit is specified or swapaccount=0*/
5261 FILE *f
= fopen("/proc/meminfo", "r");
5266 while (getline(&line
, &linelen
, f
) != -1) {
5267 if (startswith(line
, "SwapTotal:")) {
5268 sscanf(line
, "SwapTotal: %8lu kB", &swap_total
);
5269 } else if (startswith(line
, "SwapFree:")) {
5270 sscanf(line
, "SwapFree: %8lu kB", &swap_free
);
5278 if (swap_total
> 0) {
5279 l
= snprintf(d
->buf
+ total_len
, d
->size
- total_len
,
5280 "none%*svirtual\t\t%lu\t%lu\t0\n", 36, " ",
5281 swap_total
, swap_free
);
5285 if (total_len
< 0 || l
< 0) {
5286 perror("Error writing to cache");
5292 d
->size
= (int)total_len
;
5294 if (total_len
> size
) total_len
= size
;
5295 memcpy(buf
, d
->buf
, total_len
);
5300 free(memswlimit_str
);
5303 free(memswusage_str
);
5307 * Find the process pid from cgroup path.
5308 * eg:from /sys/fs/cgroup/cpu/docker/containerid/cgroup.procs to find the process pid.
5309 * @pid_buf : put pid to pid_buf.
5310 * @dpath : the path of cgroup. eg: /docker/containerid or /docker/containerid/child-cgroup ...
5311 * @depth : the depth of cgroup in container.
5312 * @sum : return the number of pid.
5313 * @cfd : the file descriptor of the mounted cgroup. eg: /sys/fs/cgroup/cpu
5315 static int calc_pid(char ***pid_buf
, char *dpath
, int depth
, int sum
, int cfd
)
5319 struct dirent
*file
;
5324 char *path_dir
, *path
;
5327 /* path = dpath + "/cgroup.procs" + /0 */
5329 path
= malloc(strlen(dpath
) + 20);
5332 strcpy(path
, dpath
);
5333 fd
= openat(cfd
, path
, O_RDONLY
);
5337 dir
= fdopendir(fd
);
5343 while (((file
= readdir(dir
)) != NULL
) && depth
> 0) {
5344 if (strncmp(file
->d_name
, ".", 1) == 0)
5346 if (strncmp(file
->d_name
, "..", 1) == 0)
5348 if (file
->d_type
== DT_DIR
) {
5349 /* path + '/' + d_name +/0 */
5351 path_dir
= malloc(strlen(path
) + 2 + sizeof(file
->d_name
));
5352 } while (!path_dir
);
5353 strcpy(path_dir
, path
);
5354 strcat(path_dir
, "/");
5355 strcat(path_dir
, file
->d_name
);
5357 sum
= calc_pid(pid_buf
, path_dir
, pd
, sum
, cfd
);
5363 strcat(path
, "/cgroup.procs");
5364 fd
= openat(cfd
, path
, O_RDONLY
);
5368 f
= fdopen(fd
, "r");
5374 while (getline(&line
, &linelen
, f
) != -1) {
5376 pid
= realloc(*pid_buf
, sizeof(char *) * (sum
+ 1));
5380 *(*pid_buf
+ sum
) = malloc(strlen(line
) + 1);
5381 } while (*(*pid_buf
+ sum
) == NULL
);
5382 strcpy(*(*pid_buf
+ sum
), line
);
5393 * calc_load calculates the load according to the following formula:
5394 * load1 = load0 * exp + active * (1 - exp)
5396 * @load1: the new loadavg.
5397 * @load0: the former loadavg.
5398 * @active: the total number of running pid at this moment.
5399 * @exp: the fixed-point defined in the beginning.
5401 static unsigned long
5402 calc_load(unsigned long load
, unsigned long exp
, unsigned long active
)
5404 unsigned long newload
;
5406 active
= active
> 0 ? active
* FIXED_1
: 0;
5407 newload
= load
* exp
+ active
* (FIXED_1
- exp
);
5409 newload
+= FIXED_1
- 1;
5411 return newload
/ FIXED_1
;
5415 * Return 0 means that container p->cg is closed.
5416 * Return -1 means that error occurred in refresh.
5417 * Positive num equals the total number of pid.
5419 static int refresh_load(struct load_node
*p
, char *path
)
5423 char proc_path
[256];
5424 int i
, ret
, run_pid
= 0, total_pid
= 0, last_pid
= 0;
5429 struct dirent
*file
;
5432 idbuf
= malloc(sizeof(char *));
5434 sum
= calc_pid(&idbuf
, path
, DEPTH_DIR
, 0, p
->cfd
);
5439 for (i
= 0; i
< sum
; i
++) {
5441 length
= strlen(idbuf
[i
])-1;
5442 idbuf
[i
][length
] = '\0';
5443 ret
= snprintf(proc_path
, 256, "/proc/%s/task", idbuf
[i
]);
5444 if (ret
< 0 || ret
> 255) {
5445 lxcfs_error("%s\n", "snprintf() failed in refresh_load.");
5451 dp
= opendir(proc_path
);
5453 lxcfs_error("%s\n", "Open proc_path failed in refresh_load.");
5456 while ((file
= readdir(dp
)) != NULL
) {
5457 if (strncmp(file
->d_name
, ".", 1) == 0)
5459 if (strncmp(file
->d_name
, "..", 1) == 0)
5462 /* We make the biggest pid become last_pid.*/
5463 ret
= atof(file
->d_name
);
5464 last_pid
= (ret
> last_pid
) ? ret
: last_pid
;
5466 ret
= snprintf(proc_path
, 256, "/proc/%s/task/%s/status", idbuf
[i
], file
->d_name
);
5467 if (ret
< 0 || ret
> 255) {
5468 lxcfs_error("%s\n", "snprintf() failed in refresh_load.");
5474 f
= fopen(proc_path
, "r");
5476 while (getline(&line
, &linelen
, f
) != -1) {
5478 if ((line
[0] == 'S') && (line
[1] == 't'))
5481 if ((line
[7] == 'R') || (line
[7] == 'D'))
5488 /*Calculate the loadavg.*/
5489 p
->avenrun
[0] = calc_load(p
->avenrun
[0], EXP_1
, run_pid
);
5490 p
->avenrun
[1] = calc_load(p
->avenrun
[1], EXP_5
, run_pid
);
5491 p
->avenrun
[2] = calc_load(p
->avenrun
[2], EXP_15
, run_pid
);
5492 p
->run_pid
= run_pid
;
5493 p
->total_pid
= total_pid
;
5494 p
->last_pid
= last_pid
;
5505 * Traverse the hash table and update it.
5507 void *load_begin(void *arg
)
5511 int i
, sum
, length
, ret
;
5512 struct load_node
*f
;
5514 clock_t time1
, time2
;
5517 if (loadavg_stop
== 1)
5521 for (i
= 0; i
< LOAD_SIZE
; i
++) {
5522 pthread_mutex_lock(&load_hash
[i
].lock
);
5523 if (load_hash
[i
].next
== NULL
) {
5524 pthread_mutex_unlock(&load_hash
[i
].lock
);
5527 f
= load_hash
[i
].next
;
5530 length
= strlen(f
->cg
) + 2;
5532 /* strlen(f->cg) + '.' or '' + \0 */
5533 path
= malloc(length
);
5536 ret
= snprintf(path
, length
, "%s%s", *(f
->cg
) == '/' ? "." : "", f
->cg
);
5537 if (ret
< 0 || ret
> length
- 1) {
5538 /* snprintf failed, ignore the node.*/
5539 lxcfs_error("Refresh node %s failed for snprintf().\n", f
->cg
);
5542 sum
= refresh_load(f
, path
);
5549 /* load_hash[i].lock locks only on the first node.*/
5550 if (first_node
== 1) {
5552 pthread_mutex_unlock(&load_hash
[i
].lock
);
5557 if (loadavg_stop
== 1)
5561 usleep(FLUSH_TIME
* 1000000 - (int)((time2
- time1
) * 1000000 / CLOCKS_PER_SEC
));
5565 static int proc_loadavg_read(char *buf
, size_t size
, off_t offset
,
5566 struct fuse_file_info
*fi
)
5568 struct fuse_context
*fc
= fuse_get_context();
5569 struct file_info
*d
= (struct file_info
*)fi
->fh
;
5572 size_t total_len
= 0;
5573 char *cache
= d
->buf
;
5574 struct load_node
*n
;
5577 unsigned long a
, b
, c
;
5580 if (offset
> d
->size
)
5584 int left
= d
->size
- offset
;
5585 total_len
= left
> size
? size
: left
;
5586 memcpy(buf
, cache
+ offset
, total_len
);
5590 return read_file("/proc/loadavg", buf
, size
, d
);
5592 initpid
= lookup_initpid_in_store(fc
->pid
);
5595 cg
= get_pid_cgroup(initpid
, "cpu");
5597 return read_file("/proc/loadavg", buf
, size
, d
);
5599 prune_init_slice(cg
);
5600 hash
= calc_hash(cg
) % LOAD_SIZE
;
5601 n
= locate_node(cg
, hash
);
5605 if (!find_mounted_controller("cpu", &cfd
)) {
5607 * In locate_node() above, pthread_rwlock_unlock() isn't used
5608 * because delete is not allowed before read has ended.
5610 pthread_rwlock_unlock(&load_hash
[hash
].rdlock
);
5615 n
= malloc(sizeof(struct load_node
));
5619 n
->cg
= malloc(strlen(cg
)+1);
5627 n
->last_pid
= initpid
;
5629 insert_node(&n
, hash
);
5631 a
= n
->avenrun
[0] + (FIXED_1
/200);
5632 b
= n
->avenrun
[1] + (FIXED_1
/200);
5633 c
= n
->avenrun
[2] + (FIXED_1
/200);
5634 total_len
= snprintf(d
->buf
, d
->buflen
, "%lu.%02lu %lu.%02lu %lu.%02lu %d/%d %d\n",
5635 LOAD_INT(a
), LOAD_FRAC(a
),
5636 LOAD_INT(b
), LOAD_FRAC(b
),
5637 LOAD_INT(c
), LOAD_FRAC(c
),
5638 n
->run_pid
, n
->total_pid
, n
->last_pid
);
5639 pthread_rwlock_unlock(&load_hash
[hash
].rdlock
);
5640 if (total_len
< 0 || total_len
>= d
->buflen
) {
5641 lxcfs_error("%s\n", "Failed to write to cache");
5645 d
->size
= (int)total_len
;
5648 if (total_len
> size
)
5650 memcpy(buf
, d
->buf
, total_len
);
5657 /* Return a positive number on success, return 0 on failure.*/
5658 pthread_t
load_daemon(int load_use
)
5665 lxcfs_error("%s\n", "Initialize hash_table fails in load_daemon!");
5668 ret
= pthread_create(&pid
, NULL
, load_begin
, NULL
);
5670 lxcfs_error("%s\n", "Create pthread fails in load_daemon!");
5674 /* use loadavg, here loadavg = 1*/
5679 /* Returns 0 on success. */
5680 int stop_load_daemon(pthread_t pid
)
5684 /* Signal the thread to gracefully stop */
5687 s
= pthread_join(pid
, NULL
); /* Make sure sub thread has been canceled. */
5689 lxcfs_error("%s\n", "stop_load_daemon error: failed to join");
5699 static off_t
get_procfile_size(const char *which
)
5701 FILE *f
= fopen(which
, "r");
5704 ssize_t sz
, answer
= 0;
5708 while ((sz
= getline(&line
, &len
, f
)) != -1)
5716 int proc_getattr(const char *path
, struct stat
*sb
)
5718 struct timespec now
;
5720 memset(sb
, 0, sizeof(struct stat
));
5721 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
5723 sb
->st_uid
= sb
->st_gid
= 0;
5724 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
5725 if (strcmp(path
, "/proc") == 0) {
5726 sb
->st_mode
= S_IFDIR
| 00555;
5730 if (strcmp(path
, "/proc/meminfo") == 0 ||
5731 strcmp(path
, "/proc/cpuinfo") == 0 ||
5732 strcmp(path
, "/proc/uptime") == 0 ||
5733 strcmp(path
, "/proc/stat") == 0 ||
5734 strcmp(path
, "/proc/diskstats") == 0 ||
5735 strcmp(path
, "/proc/swaps") == 0 ||
5736 strcmp(path
, "/proc/loadavg") == 0) {
5738 sb
->st_mode
= S_IFREG
| 00444;
5746 int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
5747 struct fuse_file_info
*fi
)
5749 if (filler(buf
, ".", NULL
, 0) != 0 ||
5750 filler(buf
, "..", NULL
, 0) != 0 ||
5751 filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
5752 filler(buf
, "meminfo", NULL
, 0) != 0 ||
5753 filler(buf
, "stat", NULL
, 0) != 0 ||
5754 filler(buf
, "uptime", NULL
, 0) != 0 ||
5755 filler(buf
, "diskstats", NULL
, 0) != 0 ||
5756 filler(buf
, "swaps", NULL
, 0) != 0 ||
5757 filler(buf
, "loadavg", NULL
, 0) != 0)
5762 int proc_open(const char *path
, struct fuse_file_info
*fi
)
5765 struct file_info
*info
;
5767 if (strcmp(path
, "/proc/meminfo") == 0)
5768 type
= LXC_TYPE_PROC_MEMINFO
;
5769 else if (strcmp(path
, "/proc/cpuinfo") == 0)
5770 type
= LXC_TYPE_PROC_CPUINFO
;
5771 else if (strcmp(path
, "/proc/uptime") == 0)
5772 type
= LXC_TYPE_PROC_UPTIME
;
5773 else if (strcmp(path
, "/proc/stat") == 0)
5774 type
= LXC_TYPE_PROC_STAT
;
5775 else if (strcmp(path
, "/proc/diskstats") == 0)
5776 type
= LXC_TYPE_PROC_DISKSTATS
;
5777 else if (strcmp(path
, "/proc/swaps") == 0)
5778 type
= LXC_TYPE_PROC_SWAPS
;
5779 else if (strcmp(path
, "/proc/loadavg") == 0)
5780 type
= LXC_TYPE_PROC_LOADAVG
;
5784 info
= malloc(sizeof(*info
));
5788 memset(info
, 0, sizeof(*info
));
5791 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
5793 info
->buf
= malloc(info
->buflen
);
5794 } while (!info
->buf
);
5795 memset(info
->buf
, 0, info
->buflen
);
5796 /* set actual size to buffer size */
5797 info
->size
= info
->buflen
;
5799 fi
->fh
= (unsigned long)info
;
5803 int proc_access(const char *path
, int mask
)
5805 if (strcmp(path
, "/proc") == 0 && access(path
, R_OK
) == 0)
5808 /* these are all read-only */
5809 if ((mask
& ~R_OK
) != 0)
5814 int proc_release(const char *path
, struct fuse_file_info
*fi
)
5816 do_release_file_info(fi
);
5820 int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
5821 struct fuse_file_info
*fi
)
5823 struct file_info
*f
= (struct file_info
*) fi
->fh
;
5826 case LXC_TYPE_PROC_MEMINFO
:
5827 return proc_meminfo_read(buf
, size
, offset
, fi
);
5828 case LXC_TYPE_PROC_CPUINFO
:
5829 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
5830 case LXC_TYPE_PROC_UPTIME
:
5831 return proc_uptime_read(buf
, size
, offset
, fi
);
5832 case LXC_TYPE_PROC_STAT
:
5833 return proc_stat_read(buf
, size
, offset
, fi
);
5834 case LXC_TYPE_PROC_DISKSTATS
:
5835 return proc_diskstats_read(buf
, size
, offset
, fi
);
5836 case LXC_TYPE_PROC_SWAPS
:
5837 return proc_swaps_read(buf
, size
, offset
, fi
);
5838 case LXC_TYPE_PROC_LOADAVG
:
5839 return proc_loadavg_read(buf
, size
, offset
, fi
);
5846 * Functions needed to setup cgroups in the __constructor__.
5849 static bool mkdir_p(const char *dir
, mode_t mode
)
5851 const char *tmp
= dir
;
5852 const char *orig
= dir
;
5856 dir
= tmp
+ strspn(tmp
, "/");
5857 tmp
= dir
+ strcspn(dir
, "/");
5858 makeme
= strndup(orig
, dir
- orig
);
5861 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
5862 lxcfs_error("Failed to create directory '%s': %s.\n",
5863 makeme
, strerror(errno
));
5868 } while(tmp
!= dir
);
5873 static bool umount_if_mounted(void)
5875 if (umount2(BASEDIR
, MNT_DETACH
) < 0 && errno
!= EINVAL
) {
5876 lxcfs_error("Failed to unmount %s: %s.\n", BASEDIR
, strerror(errno
));
5882 /* __typeof__ should be safe to use with all compilers. */
5883 typedef __typeof__(((struct statfs
*)NULL
)->f_type
) fs_type_magic
;
5884 static bool has_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
5886 return (fs
->f_type
== (fs_type_magic
)magic_val
);
5890 * looking at fs/proc_namespace.c, it appears we can
5891 * actually expect the rootfs entry to very specifically contain
5892 * " - rootfs rootfs "
5893 * IIUC, so long as we've chrooted so that rootfs is not our root,
5894 * the rootfs entry should always be skipped in mountinfo contents.
5896 static bool is_on_ramfs(void)
5904 f
= fopen("/proc/self/mountinfo", "r");
5908 while (getline(&line
, &len
, f
) != -1) {
5909 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
5910 p
= strchr(p
+ 1, ' ');
5913 p2
= strchr(p
+ 1, ' ');
5917 if (strcmp(p
+ 1, "/") == 0) {
5918 // this is '/'. is it the ramfs?
5919 p
= strchr(p2
+ 1, '-');
5920 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
5932 static int pivot_enter()
5934 int ret
= -1, oldroot
= -1, newroot
= -1;
5936 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
);
5938 lxcfs_error("%s\n", "Failed to open old root for fchdir.");
5942 newroot
= open(ROOTDIR
, O_DIRECTORY
| O_RDONLY
);
5944 lxcfs_error("%s\n", "Failed to open new root for fchdir.");
5948 /* change into new root fs */
5949 if (fchdir(newroot
) < 0) {
5950 lxcfs_error("Failed to change directory to new rootfs: %s.\n", ROOTDIR
);
5954 /* pivot_root into our new root fs */
5955 if (pivot_root(".", ".") < 0) {
5956 lxcfs_error("pivot_root() syscall failed: %s.\n", strerror(errno
));
5961 * At this point the old-root is mounted on top of our new-root.
5962 * To unmounted it we must not be chdir'd into it, so escape back
5965 if (fchdir(oldroot
) < 0) {
5966 lxcfs_error("%s\n", "Failed to enter old root.");
5970 if (umount2(".", MNT_DETACH
) < 0) {
5971 lxcfs_error("%s\n", "Failed to detach old root.");
5975 if (fchdir(newroot
) < 0) {
5976 lxcfs_error("%s\n", "Failed to re-enter new root.");
5991 static int chroot_enter()
5993 if (mount(ROOTDIR
, "/", NULL
, MS_REC
| MS_BIND
, NULL
)) {
5994 lxcfs_error("Failed to recursively bind-mount %s into /.", ROOTDIR
);
5998 if (chroot(".") < 0) {
5999 lxcfs_error("Call to chroot() failed: %s.\n", strerror(errno
));
6003 if (chdir("/") < 0) {
6004 lxcfs_error("Failed to change directory: %s.\n", strerror(errno
));
6011 static int permute_and_enter(void)
6015 if (statfs("/", &sb
) < 0) {
6016 lxcfs_error("%s\n", "Could not stat / mountpoint.");
6020 /* has_fs_type() is not reliable. When the ramfs is a tmpfs it will
6021 * likely report TMPFS_MAGIC. Hence, when it reports no we still check
6022 * /proc/1/mountinfo. */
6023 if (has_fs_type(&sb
, RAMFS_MAGIC
) || is_on_ramfs())
6024 return chroot_enter();
6026 if (pivot_enter() < 0) {
6027 lxcfs_error("%s\n", "Could not perform pivot root.");
6034 /* Prepare our new clean root. */
6035 static int permute_prepare(void)
6037 if (mkdir(ROOTDIR
, 0700) < 0 && errno
!= EEXIST
) {
6038 lxcfs_error("%s\n", "Failed to create directory for new root.");
6042 if (mount("/", ROOTDIR
, NULL
, MS_BIND
, 0) < 0) {
6043 lxcfs_error("Failed to bind-mount / for new root: %s.\n", strerror(errno
));
6047 if (mount(RUNTIME_PATH
, ROOTDIR RUNTIME_PATH
, NULL
, MS_BIND
, 0) < 0) {
6048 lxcfs_error("Failed to bind-mount /run into new root: %s.\n", strerror(errno
));
6052 if (mount(BASEDIR
, ROOTDIR BASEDIR
, NULL
, MS_REC
| MS_MOVE
, 0) < 0) {
6053 printf("Failed to move " BASEDIR
" into new root: %s.\n", strerror(errno
));
6060 /* Calls chroot() on ramfs, pivot_root() in all other cases. */
6061 static bool permute_root(void)
6063 /* Prepare new root. */
6064 if (permute_prepare() < 0)
6067 /* Pivot into new root. */
6068 if (permute_and_enter() < 0)
6074 static int preserve_mnt_ns(int pid
)
6077 size_t len
= sizeof("/proc/") + 21 + sizeof("/ns/mnt");
6080 ret
= snprintf(path
, len
, "/proc/%d/ns/mnt", pid
);
6081 if (ret
< 0 || (size_t)ret
>= len
)
6084 return open(path
, O_RDONLY
| O_CLOEXEC
);
6087 static bool cgfs_prepare_mounts(void)
6089 if (!mkdir_p(BASEDIR
, 0700)) {
6090 lxcfs_error("%s\n", "Failed to create lxcfs cgroup mountpoint.");
6094 if (!umount_if_mounted()) {
6095 lxcfs_error("%s\n", "Failed to clean up old lxcfs cgroup mountpoint.");
6099 if (unshare(CLONE_NEWNS
) < 0) {
6100 lxcfs_error("Failed to unshare mount namespace: %s.\n", strerror(errno
));
6104 cgroup_mount_ns_fd
= preserve_mnt_ns(getpid());
6105 if (cgroup_mount_ns_fd
< 0) {
6106 lxcfs_error("Failed to preserve mount namespace: %s.\n", strerror(errno
));
6110 if (mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0) < 0) {
6111 lxcfs_error("Failed to remount / private: %s.\n", strerror(errno
));
6115 if (mount("tmpfs", BASEDIR
, "tmpfs", 0, "size=100000,mode=700") < 0) {
6116 lxcfs_error("%s\n", "Failed to mount tmpfs over lxcfs cgroup mountpoint.");
6123 static bool cgfs_mount_hierarchies(void)
6129 for (i
= 0; i
< num_hierarchies
; i
++) {
6130 char *controller
= hierarchies
[i
];
6132 clen
= strlen(controller
);
6133 len
= strlen(BASEDIR
) + clen
+ 2;
6134 target
= malloc(len
);
6138 ret
= snprintf(target
, len
, "%s/%s", BASEDIR
, controller
);
6139 if (ret
< 0 || ret
>= len
) {
6143 if (mkdir(target
, 0755) < 0 && errno
!= EEXIST
) {
6147 if (!strcmp(controller
, "unified"))
6148 ret
= mount("none", target
, "cgroup2", 0, NULL
);
6150 ret
= mount(controller
, target
, "cgroup", 0, controller
);
6152 lxcfs_error("Failed mounting cgroup %s: %s\n", controller
, strerror(errno
));
6157 fd_hierarchies
[i
] = open(target
, O_DIRECTORY
);
6158 if (fd_hierarchies
[i
] < 0) {
6167 static bool cgfs_setup_controllers(void)
6169 if (!cgfs_prepare_mounts())
6172 if (!cgfs_mount_hierarchies()) {
6173 lxcfs_error("%s\n", "Failed to set up private lxcfs cgroup mounts.");
6177 if (!permute_root())
6183 static void __attribute__((constructor
)) collect_and_mount_subsystems(void)
6186 char *cret
, *line
= NULL
;
6187 char cwd
[MAXPATHLEN
];
6189 int i
, init_ns
= -1;
6190 bool found_unified
= false;
6192 if ((f
= fopen("/proc/self/cgroup", "r")) == NULL
) {
6193 lxcfs_error("Error opening /proc/self/cgroup: %s\n", strerror(errno
));
6197 while (getline(&line
, &len
, f
) != -1) {
6200 p
= strchr(line
, ':');
6206 p2
= strrchr(p
, ':');
6211 /* With cgroupv2 /proc/self/cgroup can contain entries of the
6212 * form: 0::/ This will cause lxcfs to fail the cgroup mounts
6213 * because it parses out the empty string "" and later on passes
6214 * it to mount(). Let's skip such entries.
6216 if (!strcmp(p
, "") && !strcmp(idx
, "0") && !found_unified
) {
6217 found_unified
= true;
6221 if (!store_hierarchy(line
, p
))
6225 /* Preserve initial namespace. */
6226 init_ns
= preserve_mnt_ns(getpid());
6228 lxcfs_error("%s\n", "Failed to preserve initial mount namespace.");
6232 fd_hierarchies
= malloc(sizeof(int) * num_hierarchies
);
6233 if (!fd_hierarchies
) {
6234 lxcfs_error("%s\n", strerror(errno
));
6238 for (i
= 0; i
< num_hierarchies
; i
++)
6239 fd_hierarchies
[i
] = -1;
6241 cret
= getcwd(cwd
, MAXPATHLEN
);
6243 lxcfs_debug("Could not retrieve current working directory: %s.\n", strerror(errno
));
6245 /* This function calls unshare(CLONE_NEWNS) our initial mount namespace
6246 * to privately mount lxcfs cgroups. */
6247 if (!cgfs_setup_controllers()) {
6248 lxcfs_error("%s\n", "Failed to setup private cgroup mounts for lxcfs.");
6252 if (setns(init_ns
, 0) < 0) {
6253 lxcfs_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno
));
6257 if (!cret
|| chdir(cwd
) < 0)
6258 lxcfs_debug("Could not change back to original working directory: %s.\n", strerror(errno
));
6260 if (!init_cpuview()) {
6261 lxcfs_error("%s\n", "failed to init CPU view");
6274 static void __attribute__((destructor
)) free_subsystems(void)
6278 lxcfs_debug("%s\n", "Running destructor for liblxcfs.");
6280 for (i
= 0; i
< num_hierarchies
; i
++) {
6282 free(hierarchies
[i
]);
6283 if (fd_hierarchies
&& fd_hierarchies
[i
] >= 0)
6284 close(fd_hierarchies
[i
]);
6287 free(fd_hierarchies
);
6290 if (cgroup_mount_ns_fd
>= 0)
6291 close(cgroup_mount_ns_fd
);