3 * Copyright © 2014-2016 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
6 * See COPYING file for details.
9 #define FUSE_USE_VERSION 26
11 #define __STDC_FORMAT_MACROS
28 #include <linux/magic.h>
29 #include <linux/sched.h>
30 #include <sys/epoll.h>
32 #include <sys/mount.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/syscall.h>
36 #include <sys/sysinfo.h>
40 #include "config.h" // for VERSION
42 /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */
43 #define LXCFS_NUMSTRLEN64 21
45 /* Define pivot_root() if missing from the C library */
46 #ifndef HAVE_PIVOT_ROOT
47 static int pivot_root(const char * new_root
, const char * put_old
)
49 #ifdef __NR_pivot_root
50 return syscall(__NR_pivot_root
, new_root
, put_old
);
57 extern int pivot_root(const char * new_root
, const char * put_old
);
63 LXC_TYPE_PROC_MEMINFO
,
64 LXC_TYPE_PROC_CPUINFO
,
67 LXC_TYPE_PROC_DISKSTATS
,
76 char *buf
; // unused as of yet
78 int size
; //actual data size
82 /* Reserve buffer size to account for file size changes. */
83 #define BUF_RESERVE_SIZE 512
86 * A table caching which pid is init for a pid namespace.
87 * When looking up which pid is init for $qpid, we first
88 * 1. Stat /proc/$qpid/ns/pid.
89 * 2. Check whether the ino_t is in our store.
90 * a. if not, fork a child in qpid's ns to send us
91 * ucred.pid = 1, and read the initpid. Cache
92 * initpid and creation time for /proc/initpid
93 * in a new store entry.
94 * b. if so, verify that /proc/initpid still matches
95 * what we have saved. If not, clear the store
96 * entry and go back to a. If so, return the
99 struct pidns_init_store
{
100 ino_t ino
; // inode number for /proc/$pid/ns/pid
101 pid_t initpid
; // the pid of nit in that ns
102 long int ctime
; // the time at which /proc/$initpid was created
103 struct pidns_init_store
*next
;
107 /* lol - look at how they are allocated in the kernel */
108 #define PIDNS_HASH_SIZE 4096
109 #define HASH(x) ((x) % PIDNS_HASH_SIZE)
111 static struct pidns_init_store
*pidns_hash_table
[PIDNS_HASH_SIZE
];
112 static pthread_mutex_t pidns_store_mutex
= PTHREAD_MUTEX_INITIALIZER
;
113 static void lock_mutex(pthread_mutex_t
*l
)
117 if ((ret
= pthread_mutex_lock(l
)) != 0) {
118 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
123 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
124 * Number of hierarchies mounted. */
125 static int num_hierarchies
;
127 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
128 * Hierachies mounted {cpuset, blkio, ...}:
129 * Initialized via __constructor__ collect_and_mount_subsystems(). */
130 static char **hierarchies
;
132 /* READ-ONLY after __constructor__ collect_and_mount_subsystems() has run.
133 * Open file descriptors:
134 * @fd_hierarchies[i] refers to cgroup @hierarchies[i]. They are mounted in a
135 * private mount namespace.
136 * Initialized via __constructor__ collect_and_mount_subsystems().
137 * @fd_hierarchies[i] can be used to perform file operations on the cgroup
138 * mounts and respective files in the private namespace even when located in
139 * another namespace using the *at() family of functions
140 * {openat(), fchownat(), ...}. */
141 static int *fd_hierarchies
;
142 static int cgroup_mount_ns_fd
= -1;
144 static void unlock_mutex(pthread_mutex_t
*l
)
148 if ((ret
= pthread_mutex_unlock(l
)) != 0) {
149 lxcfs_error("returned:%d %s\n", ret
, strerror(ret
));
154 static void store_lock(void)
156 lock_mutex(&pidns_store_mutex
);
159 static void store_unlock(void)
161 unlock_mutex(&pidns_store_mutex
);
164 /* Must be called under store_lock */
165 static bool initpid_still_valid(struct pidns_init_store
*e
, struct stat
*nsfdsb
)
170 snprintf(fnam
, 100, "/proc/%d", e
->initpid
);
171 if (stat(fnam
, &initsb
) < 0)
174 lxcfs_debug("Comparing ctime %ld == %ld for pid %d.\n", e
->ctime
,
175 initsb
.st_ctime
, e
->initpid
);
177 if (e
->ctime
!= initsb
.st_ctime
)
182 /* Must be called under store_lock */
183 static void remove_initpid(struct pidns_init_store
*e
)
185 struct pidns_init_store
*tmp
;
188 lxcfs_debug("Remove_initpid: removing entry for %d.\n", e
->initpid
);
191 if (pidns_hash_table
[h
] == e
) {
192 pidns_hash_table
[h
] = e
->next
;
197 tmp
= pidns_hash_table
[h
];
199 if (tmp
->next
== e
) {
209 /* Must be called under store_lock */
210 static void prune_initpid_store(void)
212 static long int last_prune
= 0;
213 struct pidns_init_store
*e
, *prev
, *delme
;
214 long int now
, threshold
;
218 last_prune
= time(NULL
);
222 if (now
< last_prune
+ PURGE_SECS
)
225 lxcfs_debug("%s\n", "Pruning.");
228 threshold
= now
- 2 * PURGE_SECS
;
230 for (i
= 0; i
< PIDNS_HASH_SIZE
; i
++) {
231 for (prev
= NULL
, e
= pidns_hash_table
[i
]; e
; ) {
232 if (e
->lastcheck
< threshold
) {
234 lxcfs_debug("Removing cached entry for %d.\n", e
->initpid
);
238 prev
->next
= e
->next
;
240 pidns_hash_table
[i
] = e
->next
;
251 /* Must be called under store_lock */
252 static void save_initpid(struct stat
*sb
, pid_t pid
)
254 struct pidns_init_store
*e
;
259 lxcfs_debug("Save_initpid: adding entry for %d.\n", pid
);
261 snprintf(fpath
, 100, "/proc/%d", pid
);
262 if (stat(fpath
, &procsb
) < 0)
265 e
= malloc(sizeof(*e
));
269 e
->ctime
= procsb
.st_ctime
;
271 e
->next
= pidns_hash_table
[h
];
272 e
->lastcheck
= time(NULL
);
273 pidns_hash_table
[h
] = e
;
277 * Given the stat(2) info for a nsfd pid inode, lookup the init_pid_store
278 * entry for the inode number and creation time. Verify that the init pid
279 * is still valid. If not, remove it. Return the entry if valid, NULL
281 * Must be called under store_lock
283 static struct pidns_init_store
*lookup_verify_initpid(struct stat
*sb
)
285 int h
= HASH(sb
->st_ino
);
286 struct pidns_init_store
*e
= pidns_hash_table
[h
];
289 if (e
->ino
== sb
->st_ino
) {
290 if (initpid_still_valid(e
, sb
)) {
291 e
->lastcheck
= time(NULL
);
303 static int is_dir(const char *path
, int fd
)
306 int ret
= fstatat(fd
, path
, &statbuf
, fd
);
307 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
312 static char *must_copy_string(const char *str
)
324 static inline void drop_trailing_newlines(char *s
)
328 for (l
=strlen(s
); l
>0 && s
[l
-1] == '\n'; l
--)
332 #define BATCH_SIZE 50
333 static void dorealloc(char **mem
, size_t oldlen
, size_t newlen
)
335 int newbatches
= (newlen
/ BATCH_SIZE
) + 1;
336 int oldbatches
= (oldlen
/ BATCH_SIZE
) + 1;
338 if (!*mem
|| newbatches
> oldbatches
) {
341 tmp
= realloc(*mem
, newbatches
* BATCH_SIZE
);
346 static void append_line(char **contents
, size_t *len
, char *line
, ssize_t linelen
)
348 size_t newlen
= *len
+ linelen
;
349 dorealloc(contents
, *len
, newlen
+ 1);
350 memcpy(*contents
+ *len
, line
, linelen
+1);
354 static char *slurp_file(const char *from
, int fd
)
357 char *contents
= NULL
;
358 FILE *f
= fdopen(fd
, "r");
359 size_t len
= 0, fulllen
= 0;
365 while ((linelen
= getline(&line
, &len
, f
)) != -1) {
366 append_line(&contents
, &fulllen
, line
, linelen
);
371 drop_trailing_newlines(contents
);
376 static bool write_string(const char *fnam
, const char *string
, int fd
)
381 if (!(f
= fdopen(fd
, "w")))
383 len
= strlen(string
);
384 ret
= fwrite(string
, 1, len
, f
);
386 lxcfs_error("Error writing to file: %s\n", strerror(errno
));
391 lxcfs_error("Error writing to file: %s\n", strerror(errno
));
404 static bool store_hierarchy(char *stridx
, char *h
)
406 if (num_hierarchies
% ALLOC_NUM
== 0) {
407 size_t n
= (num_hierarchies
/ ALLOC_NUM
) + 1;
409 char **tmp
= realloc(hierarchies
, n
* sizeof(char *));
411 lxcfs_error("%s\n", strerror(errno
));
417 hierarchies
[num_hierarchies
++] = must_copy_string(h
);
421 static void print_subsystems(void)
425 fprintf(stderr
, "mount namespace: %d\n", cgroup_mount_ns_fd
);
426 fprintf(stderr
, "hierarchies:\n");
427 for (i
= 0; i
< num_hierarchies
; i
++) {
429 fprintf(stderr
, " %2d: fd: %3d: %s\n", i
,
430 fd_hierarchies
[i
], hierarchies
[i
]);
434 static bool in_comma_list(const char *needle
, const char *haystack
)
436 const char *s
= haystack
, *e
;
437 size_t nlen
= strlen(needle
);
439 while (*s
&& (e
= strchr(s
, ','))) {
444 if (strncmp(needle
, s
, nlen
) == 0)
448 if (strcmp(needle
, s
) == 0)
453 /* do we need to do any massaging here? I'm not sure... */
454 /* Return the mounted controller and store the corresponding open file descriptor
455 * referring to the controller mountpoint in the private lxcfs namespace in
458 static char *find_mounted_controller(const char *controller
, int *cfd
)
462 for (i
= 0; i
< num_hierarchies
; i
++) {
465 if (strcmp(hierarchies
[i
], controller
) == 0) {
466 *cfd
= fd_hierarchies
[i
];
467 return hierarchies
[i
];
469 if (in_comma_list(controller
, hierarchies
[i
])) {
470 *cfd
= fd_hierarchies
[i
];
471 return hierarchies
[i
];
478 bool cgfs_set_value(const char *controller
, const char *cgroup
, const char *file
,
485 tmpc
= find_mounted_controller(controller
, &cfd
);
489 /* Make sure we pass a relative path to *at() family of functions.
490 * . + /cgroup + / + file + \0
492 len
= strlen(cgroup
) + strlen(file
) + 3;
494 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
495 if (ret
< 0 || (size_t)ret
>= len
)
498 fd
= openat(cfd
, fnam
, O_WRONLY
);
502 return write_string(fnam
, value
, fd
);
505 // Chown all the files in the cgroup directory. We do this when we create
506 // a cgroup on behalf of a user.
507 static void chown_all_cgroup_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
509 struct dirent
*direntp
;
510 char path
[MAXPATHLEN
];
515 len
= strlen(dirname
);
516 if (len
>= MAXPATHLEN
) {
517 lxcfs_error("Pathname too long: %s\n", dirname
);
521 fd1
= openat(fd
, dirname
, O_DIRECTORY
);
527 lxcfs_error("Failed to open %s\n", dirname
);
531 while ((direntp
= readdir(d
))) {
532 if (!strcmp(direntp
->d_name
, ".") || !strcmp(direntp
->d_name
, ".."))
534 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
535 if (ret
< 0 || ret
>= MAXPATHLEN
) {
536 lxcfs_error("Pathname too long under %s\n", dirname
);
539 if (fchownat(fd
, path
, uid
, gid
, 0) < 0)
540 lxcfs_error("Failed to chown file %s to %u:%u", path
, uid
, gid
);
545 int cgfs_create(const char *controller
, const char *cg
, uid_t uid
, gid_t gid
)
551 tmpc
= find_mounted_controller(controller
, &cfd
);
555 /* Make sure we pass a relative path to *at() family of functions.
558 len
= strlen(cg
) + 2;
559 dirnam
= alloca(len
);
560 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
562 if (mkdirat(cfd
, dirnam
, 0755) < 0)
565 if (uid
== 0 && gid
== 0)
568 if (fchownat(cfd
, dirnam
, uid
, gid
, 0) < 0)
571 chown_all_cgroup_files(dirnam
, uid
, gid
, cfd
);
576 static bool recursive_rmdir(const char *dirname
, int fd
, const int cfd
)
578 struct dirent
*direntp
;
581 char pathname
[MAXPATHLEN
];
584 dupfd
= dup(fd
); // fdopendir() does bad things once it uses an fd.
588 dir
= fdopendir(dupfd
);
590 lxcfs_debug("Failed to open %s: %s.\n", dirname
, strerror(errno
));
595 while ((direntp
= readdir(dir
))) {
599 if (!strcmp(direntp
->d_name
, ".") ||
600 !strcmp(direntp
->d_name
, ".."))
603 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
604 if (rc
< 0 || rc
>= MAXPATHLEN
) {
605 lxcfs_error("%s\n", "Pathname too long.");
609 rc
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
611 lxcfs_debug("Failed to stat %s: %s.\n", pathname
, strerror(errno
));
614 if (S_ISDIR(mystat
.st_mode
))
615 if (!recursive_rmdir(pathname
, fd
, cfd
))
616 lxcfs_debug("Error removing %s.\n", pathname
);
620 if (closedir(dir
) < 0) {
621 lxcfs_error("Failed to close directory %s: %s\n", dirname
, strerror(errno
));
625 if (unlinkat(cfd
, dirname
, AT_REMOVEDIR
) < 0) {
626 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
635 bool cgfs_remove(const char *controller
, const char *cg
)
642 tmpc
= find_mounted_controller(controller
, &cfd
);
646 /* Make sure we pass a relative path to *at() family of functions.
649 len
= strlen(cg
) + 2;
650 dirnam
= alloca(len
);
651 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
653 fd
= openat(cfd
, dirnam
, O_DIRECTORY
);
657 bret
= recursive_rmdir(dirnam
, fd
, cfd
);
662 bool cgfs_chmod_file(const char *controller
, const char *file
, mode_t mode
)
666 char *pathname
, *tmpc
;
668 tmpc
= find_mounted_controller(controller
, &cfd
);
672 /* Make sure we pass a relative path to *at() family of functions.
675 len
= strlen(file
) + 2;
676 pathname
= alloca(len
);
677 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
678 if (fchmodat(cfd
, pathname
, mode
, 0) < 0)
683 static int chown_tasks_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
688 len
= strlen(dirname
) + strlen("/cgroup.procs") + 1;
690 snprintf(fname
, len
, "%s/tasks", dirname
);
691 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
693 snprintf(fname
, len
, "%s/cgroup.procs", dirname
);
694 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
699 int cgfs_chown_file(const char *controller
, const char *file
, uid_t uid
, gid_t gid
)
703 char *pathname
, *tmpc
;
705 tmpc
= find_mounted_controller(controller
, &cfd
);
709 /* Make sure we pass a relative path to *at() family of functions.
712 len
= strlen(file
) + 2;
713 pathname
= alloca(len
);
714 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
715 if (fchownat(cfd
, pathname
, uid
, gid
, 0) < 0)
718 if (is_dir(pathname
, cfd
))
719 // like cgmanager did, we want to chown the tasks file as well
720 return chown_tasks_files(pathname
, uid
, gid
, cfd
);
725 FILE *open_pids_file(const char *controller
, const char *cgroup
)
729 char *pathname
, *tmpc
;
731 tmpc
= find_mounted_controller(controller
, &cfd
);
735 /* Make sure we pass a relative path to *at() family of functions.
736 * . + /cgroup + / "cgroup.procs" + \0
738 len
= strlen(cgroup
) + strlen("cgroup.procs") + 3;
739 pathname
= alloca(len
);
740 snprintf(pathname
, len
, "%s%s/cgroup.procs", *cgroup
== '/' ? "." : "", cgroup
);
742 fd
= openat(cfd
, pathname
, O_WRONLY
);
746 return fdopen(fd
, "w");
749 static bool cgfs_iterate_cgroup(const char *controller
, const char *cgroup
, bool directories
,
750 void ***list
, size_t typesize
,
751 void* (*iterator
)(const char*, const char*, const char*))
756 char pathname
[MAXPATHLEN
];
757 size_t sz
= 0, asz
= 0;
758 struct dirent
*dirent
;
761 tmpc
= find_mounted_controller(controller
, &cfd
);
766 /* Make sure we pass a relative path to *at() family of functions. */
767 len
= strlen(cgroup
) + 1 /* . */ + 1 /* \0 */;
769 ret
= snprintf(cg
, len
, "%s%s", *cgroup
== '/' ? "." : "", cgroup
);
770 if (ret
< 0 || (size_t)ret
>= len
) {
771 lxcfs_error("Pathname too long under %s\n", cgroup
);
775 fd
= openat(cfd
, cg
, O_DIRECTORY
);
783 while ((dirent
= readdir(dir
))) {
786 if (!strcmp(dirent
->d_name
, ".") ||
787 !strcmp(dirent
->d_name
, ".."))
790 ret
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", cg
, dirent
->d_name
);
791 if (ret
< 0 || ret
>= MAXPATHLEN
) {
792 lxcfs_error("Pathname too long under %s\n", cg
);
796 ret
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
798 lxcfs_error("Failed to stat %s: %s\n", pathname
, strerror(errno
));
801 if ((!directories
&& !S_ISREG(mystat
.st_mode
)) ||
802 (directories
&& !S_ISDIR(mystat
.st_mode
)))
809 tmp
= realloc(*list
, asz
* typesize
);
813 (*list
)[sz
] = (*iterator
)(controller
, cg
, dirent
->d_name
);
814 (*list
)[sz
+1] = NULL
;
817 if (closedir(dir
) < 0) {
818 lxcfs_error("Failed closedir for %s: %s\n", cgroup
, strerror(errno
));
824 static void *make_children_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
828 dup
= strdup(dir_entry
);
833 bool cgfs_list_children(const char *controller
, const char *cgroup
, char ***list
)
835 return cgfs_iterate_cgroup(controller
, cgroup
, true, (void***)list
, sizeof(*list
), &make_children_list_entry
);
838 void free_key(struct cgfs_files
*k
)
846 void free_keys(struct cgfs_files
**keys
)
852 for (i
= 0; keys
[i
]; i
++) {
858 bool cgfs_get_value(const char *controller
, const char *cgroup
, const char *file
, char **value
)
864 tmpc
= find_mounted_controller(controller
, &cfd
);
868 /* Make sure we pass a relative path to *at() family of functions.
869 * . + /cgroup + / + file + \0
871 len
= strlen(cgroup
) + strlen(file
) + 3;
873 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
874 if (ret
< 0 || (size_t)ret
>= len
)
877 fd
= openat(cfd
, fnam
, O_RDONLY
);
881 *value
= slurp_file(fnam
, fd
);
882 return *value
!= NULL
;
885 struct cgfs_files
*cgfs_get_key(const char *controller
, const char *cgroup
, const char *file
)
891 struct cgfs_files
*newkey
;
893 tmpc
= find_mounted_controller(controller
, &cfd
);
897 if (file
&& *file
== '/')
900 if (file
&& strchr(file
, '/'))
903 /* Make sure we pass a relative path to *at() family of functions.
904 * . + /cgroup + / + file + \0
906 len
= strlen(cgroup
) + 3;
908 len
+= strlen(file
) + 1;
910 snprintf(fnam
, len
, "%s%s%s%s", *cgroup
== '/' ? "." : "", cgroup
,
911 file
? "/" : "", file
? file
: "");
913 ret
= fstatat(cfd
, fnam
, &sb
, 0);
918 newkey
= malloc(sizeof(struct cgfs_files
));
921 newkey
->name
= must_copy_string(file
);
922 else if (strrchr(cgroup
, '/'))
923 newkey
->name
= must_copy_string(strrchr(cgroup
, '/'));
925 newkey
->name
= must_copy_string(cgroup
);
926 newkey
->uid
= sb
.st_uid
;
927 newkey
->gid
= sb
.st_gid
;
928 newkey
->mode
= sb
.st_mode
;
933 static void *make_key_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
935 struct cgfs_files
*entry
= cgfs_get_key(controller
, cgroup
, dir_entry
);
937 lxcfs_error("Error getting files under %s:%s\n", controller
,
943 bool cgfs_list_keys(const char *controller
, const char *cgroup
, struct cgfs_files
***keys
)
945 return cgfs_iterate_cgroup(controller
, cgroup
, false, (void***)keys
, sizeof(*keys
), &make_key_list_entry
);
948 bool is_child_cgroup(const char *controller
, const char *cgroup
, const char *f
)
956 tmpc
= find_mounted_controller(controller
, &cfd
);
960 /* Make sure we pass a relative path to *at() family of functions.
961 * . + /cgroup + / + f + \0
963 len
= strlen(cgroup
) + strlen(f
) + 3;
965 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, f
);
966 if (ret
< 0 || (size_t)ret
>= len
)
969 ret
= fstatat(cfd
, fnam
, &sb
, 0);
970 if (ret
< 0 || !S_ISDIR(sb
.st_mode
))
976 #define SEND_CREDS_OK 0
977 #define SEND_CREDS_NOTSK 1
978 #define SEND_CREDS_FAIL 2
979 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
);
980 static int wait_for_pid(pid_t pid
);
981 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
);
982 static int send_creds_clone_wrapper(void *arg
);
985 * clone a task which switches to @task's namespace and writes '1'.
986 * over a unix sock so we can read the task's reaper's pid in our
989 * Note: glibc's fork() does not respect pidns, which can lead to failed
990 * assertions inside glibc (and thus failed forks) if the child's pid in
991 * the pidns and the parent pid outside are identical. Using clone prevents
994 static void write_task_init_pid_exit(int sock
, pid_t target
)
999 size_t stack_size
= sysconf(_SC_PAGESIZE
);
1000 void *stack
= alloca(stack_size
);
1002 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", (int)target
);
1003 if (ret
< 0 || ret
>= sizeof(fnam
))
1006 fd
= open(fnam
, O_RDONLY
);
1008 perror("write_task_init_pid_exit open of ns/pid");
1012 perror("write_task_init_pid_exit setns 1");
1016 pid
= clone(send_creds_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &sock
);
1020 if (!wait_for_pid(pid
))
1026 static int send_creds_clone_wrapper(void *arg
) {
1029 int sock
= *(int *)arg
;
1031 /* we are the child */
1036 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
)
1041 static pid_t
get_init_pid_for_task(pid_t task
)
1049 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1050 perror("socketpair");
1059 write_task_init_pid_exit(sock
[0], task
);
1063 if (!recv_creds(sock
[1], &cred
, &v
))
1075 static pid_t
lookup_initpid_in_store(pid_t qpid
)
1079 struct pidns_init_store
*e
;
1082 snprintf(fnam
, 100, "/proc/%d/ns/pid", qpid
);
1084 if (stat(fnam
, &sb
) < 0)
1086 e
= lookup_verify_initpid(&sb
);
1088 answer
= e
->initpid
;
1091 answer
= get_init_pid_for_task(qpid
);
1093 save_initpid(&sb
, answer
);
1096 /* we prune at end in case we are returning
1097 * the value we were about to return */
1098 prune_initpid_store();
1103 static int wait_for_pid(pid_t pid
)
1111 ret
= waitpid(pid
, &status
, 0);
1119 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
1126 * append pid to *src.
1127 * src: a pointer to a char* in which ot append the pid.
1128 * sz: the number of characters printed so far, minus trailing \0.
1129 * asz: the allocated size so far
1130 * pid: the pid to append
1132 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
1136 int tmplen
= sprintf(tmp
, "%d\n", (int)pid
);
1138 if (!*src
|| tmplen
+ *sz
+ 1 >= *asz
) {
1141 tmp
= realloc(*src
, *asz
+ BUF_RESERVE_SIZE
);
1144 *asz
+= BUF_RESERVE_SIZE
;
1146 memcpy((*src
) +*sz
, tmp
, tmplen
+1); /* include the \0 */
1151 * Given a open file * to /proc/pid/{u,g}id_map, and an id
1152 * valid in the caller's namespace, return the id mapped into
1154 * Returns the mapped id, or -1 on error.
1157 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
1159 unsigned int nsuid
, // base id for a range in the idfile's namespace
1160 hostuid
, // base id for a range in the caller's namespace
1161 count
; // number of ids in this range
1165 fseek(idfile
, 0L, SEEK_SET
);
1166 while (fgets(line
, 400, idfile
)) {
1167 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
1170 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
1172 * uids wrapped around - unexpected as this is a procfile,
1175 lxcfs_error("pid wrapparound at entry %u %u %u in %s\n",
1176 nsuid
, hostuid
, count
, line
);
1179 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
1181 * now since hostuid <= in_id < hostuid+count, and
1182 * hostuid+count and nsuid+count do not wrap around,
1183 * we know that nsuid+(in_id-hostuid) which must be
1184 * less that nsuid+(count) must not wrap around
1186 return (in_id
- hostuid
) + nsuid
;
1195 * for is_privileged_over,
1196 * specify whether we require the calling uid to be root in his
1199 #define NS_ROOT_REQD true
1200 #define NS_ROOT_OPT false
1204 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
1206 char fpath
[PROCLEN
];
1208 bool answer
= false;
1211 if (victim
== -1 || uid
== -1)
1215 * If the request is one not requiring root in the namespace,
1216 * then having the same uid suffices. (i.e. uid 1000 has write
1217 * access to files owned by uid 1000
1219 if (!req_ns_root
&& uid
== victim
)
1222 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
1223 if (ret
< 0 || ret
>= PROCLEN
)
1225 FILE *f
= fopen(fpath
, "r");
1229 /* if caller's not root in his namespace, reject */
1230 nsuid
= convert_id_to_ns(f
, uid
);
1235 * If victim is not mapped into caller's ns, reject.
1236 * XXX I'm not sure this check is needed given that fuse
1237 * will be sending requests where the vfs has converted
1239 nsuid
= convert_id_to_ns(f
, victim
);
1250 static bool perms_include(int fmode
, mode_t req_mode
)
1254 switch (req_mode
& O_ACCMODE
) {
1262 r
= S_IROTH
| S_IWOTH
;
1267 return ((fmode
& r
) == r
);
1273 * querycg is /a/b/c/d/e
1276 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
1280 if (strlen(taskcg
) <= strlen(querycg
)) {
1281 lxcfs_error("%s\n", "I was fed bad input.");
1285 if ((strcmp(querycg
, "/") == 0) || (strcmp(querycg
, "./") == 0))
1286 start
= strdup(taskcg
+ 1);
1288 start
= strdup(taskcg
+ strlen(querycg
) + 1);
1291 end
= strchr(start
, '/');
1297 static void stripnewline(char *x
)
1299 size_t l
= strlen(x
);
1300 if (l
&& x
[l
-1] == '\n')
1304 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1309 char *answer
= NULL
;
1313 const char *h
= find_mounted_controller(contrl
, &cfd
);
1317 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
1318 if (ret
< 0 || ret
>= PROCLEN
)
1320 if (!(f
= fopen(fnam
, "r")))
1323 while (getline(&line
, &len
, f
) != -1) {
1327 c1
= strchr(line
, ':');
1331 c2
= strchr(c1
, ':');
1335 if (strcmp(c1
, h
) != 0)
1340 answer
= strdup(c2
);
1352 * check whether a fuse context may access a cgroup dir or file
1354 * If file is not null, it is a cgroup file to check under cg.
1355 * If file is null, then we are checking perms on cg itself.
1357 * For files we can check the mode of the list_keys result.
1358 * For cgroups, we must make assumptions based on the files under the
1359 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
1362 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
1364 struct cgfs_files
*k
= NULL
;
1367 k
= cgfs_get_key(contrl
, cg
, file
);
1371 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1372 if (perms_include(k
->mode
>> 6, mode
)) {
1377 if (fc
->gid
== k
->gid
) {
1378 if (perms_include(k
->mode
>> 3, mode
)) {
1383 ret
= perms_include(k
->mode
, mode
);
1390 #define INITSCOPE "/init.scope"
1391 static void prune_init_slice(char *cg
)
1394 size_t cg_len
= strlen(cg
), initscope_len
= strlen(INITSCOPE
);
1396 if (cg_len
< initscope_len
)
1399 point
= cg
+ cg_len
- initscope_len
;
1400 if (strcmp(point
, INITSCOPE
) == 0) {
1409 * If pid is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
1410 * If pid is in /a, he may act on /a/b, but not on /b.
1411 * if the answer is false and nextcg is not NULL, then *nextcg will point
1412 * to a string containing the next cgroup directory under cg, which must be
1413 * freed by the caller.
1415 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
1417 bool answer
= false;
1418 char *c2
= get_pid_cgroup(pid
, contrl
);
1423 prune_init_slice(c2
);
1426 * callers pass in '/' or './' (openat()) for root cgroup, otherwise
1427 * they pass in a cgroup without leading '/'
1429 * The original line here was:
1430 * linecmp = *cg == '/' ? c2 : c2+1;
1431 * TODO: I'm not sure why you'd want to increment when *cg != '/'?
1432 * Serge, do you know?
1434 if (*cg
== '/' || !strncmp(cg
, "./", 2))
1438 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
1440 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
1452 * If pid is in /a/b/c, he may see that /a exists, but not /b or /a/c.
1454 static bool caller_may_see_dir(pid_t pid
, const char *contrl
, const char *cg
)
1456 bool answer
= false;
1458 size_t target_len
, task_len
;
1460 if (strcmp(cg
, "/") == 0 || strcmp(cg
, "./") == 0)
1463 c2
= get_pid_cgroup(pid
, contrl
);
1466 prune_init_slice(c2
);
1469 target_len
= strlen(cg
);
1470 task_len
= strlen(task_cg
);
1471 if (task_len
== 0) {
1472 /* Task is in the root cg, it can see everything. This case is
1473 * not handled by the strmcps below, since they test for the
1474 * last /, but that is the first / that we've chopped off
1480 if (strcmp(cg
, task_cg
) == 0) {
1484 if (target_len
< task_len
) {
1485 /* looking up a parent dir */
1486 if (strncmp(task_cg
, cg
, target_len
) == 0 && task_cg
[target_len
] == '/')
1490 if (target_len
> task_len
) {
1491 /* looking up a child dir */
1492 if (strncmp(task_cg
, cg
, task_len
) == 0 && cg
[task_len
] == '/')
1503 * given /cgroup/freezer/a/b, return "freezer".
1504 * the returned char* should NOT be freed.
1506 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
1509 char *contr
, *slash
;
1511 if (strlen(path
) < 9) {
1515 if (*(path
+ 7) != '/') {
1520 contr
= strdupa(p1
);
1525 slash
= strstr(contr
, "/");
1530 for (i
= 0; i
< num_hierarchies
; i
++) {
1531 if (hierarchies
[i
] && strcmp(hierarchies
[i
], contr
) == 0)
1532 return hierarchies
[i
];
1539 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
1540 * Note that the returned value may include files (keynames) etc
1542 static const char *find_cgroup_in_path(const char *path
)
1546 if (strlen(path
) < 9) {
1550 p1
= strstr(path
+ 8, "/");
1560 * split the last path element from the path in @cg.
1561 * @dir is newly allocated and should be freed, @last not
1563 static void get_cgdir_and_path(const char *cg
, char **dir
, char **last
)
1570 *last
= strrchr(cg
, '/');
1575 p
= strrchr(*dir
, '/');
1580 * FUSE ops for /cgroup
1583 int cg_getattr(const char *path
, struct stat
*sb
)
1585 struct timespec now
;
1586 struct fuse_context
*fc
= fuse_get_context();
1587 char * cgdir
= NULL
;
1588 char *last
= NULL
, *path1
, *path2
;
1589 struct cgfs_files
*k
= NULL
;
1591 const char *controller
= NULL
;
1598 memset(sb
, 0, sizeof(struct stat
));
1600 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1603 sb
->st_uid
= sb
->st_gid
= 0;
1604 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1607 if (strcmp(path
, "/cgroup") == 0) {
1608 sb
->st_mode
= S_IFDIR
| 00755;
1613 controller
= pick_controller_from_path(fc
, path
);
1616 cgroup
= find_cgroup_in_path(path
);
1618 /* this is just /cgroup/controller, return it as a dir */
1619 sb
->st_mode
= S_IFDIR
| 00755;
1624 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1634 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1637 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
1638 * Then check that caller's cgroup is under path if last is a child
1639 * cgroup, or cgdir if last is a file */
1641 if (is_child_cgroup(controller
, path1
, path2
)) {
1642 if (!caller_may_see_dir(initpid
, controller
, cgroup
)) {
1646 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
1647 /* this is just /cgroup/controller, return it as a dir */
1648 sb
->st_mode
= S_IFDIR
| 00555;
1653 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
1658 // get uid, gid, from '/tasks' file and make up a mode
1659 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1660 sb
->st_mode
= S_IFDIR
| 00755;
1661 k
= cgfs_get_key(controller
, cgroup
, NULL
);
1663 sb
->st_uid
= sb
->st_gid
= 0;
1665 sb
->st_uid
= k
->uid
;
1666 sb
->st_gid
= k
->gid
;
1674 if ((k
= cgfs_get_key(controller
, path1
, path2
)) != NULL
) {
1675 sb
->st_mode
= S_IFREG
| k
->mode
;
1677 sb
->st_uid
= k
->uid
;
1678 sb
->st_gid
= k
->gid
;
1681 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
1693 int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
1695 struct fuse_context
*fc
= fuse_get_context();
1697 struct file_info
*dir_info
;
1698 char *controller
= NULL
;
1703 if (strcmp(path
, "/cgroup") == 0) {
1707 // return list of keys for the controller, and list of child cgroups
1708 controller
= pick_controller_from_path(fc
, path
);
1712 cgroup
= find_cgroup_in_path(path
);
1714 /* this is just /cgroup/controller, return its contents */
1719 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1723 if (!caller_may_see_dir(initpid
, controller
, cgroup
))
1725 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
1729 /* we'll free this at cg_releasedir */
1730 dir_info
= malloc(sizeof(*dir_info
));
1733 dir_info
->controller
= must_copy_string(controller
);
1734 dir_info
->cgroup
= must_copy_string(cgroup
);
1735 dir_info
->type
= LXC_TYPE_CGDIR
;
1736 dir_info
->buf
= NULL
;
1737 dir_info
->file
= NULL
;
1738 dir_info
->buflen
= 0;
1740 fi
->fh
= (unsigned long)dir_info
;
1744 int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1745 struct fuse_file_info
*fi
)
1747 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1748 struct cgfs_files
**list
= NULL
;
1750 char *nextcg
= NULL
;
1751 struct fuse_context
*fc
= fuse_get_context();
1752 char **clist
= NULL
;
1754 if (filler(buf
, ".", NULL
, 0) != 0 || filler(buf
, "..", NULL
, 0) != 0)
1757 if (d
->type
!= LXC_TYPE_CGDIR
) {
1758 lxcfs_error("%s\n", "Internal error: file cache info used in readdir.");
1761 if (!d
->cgroup
&& !d
->controller
) {
1762 // ls /var/lib/lxcfs/cgroup - just show list of controllers
1765 for (i
= 0; i
< num_hierarchies
; i
++) {
1766 if (hierarchies
[i
] && filler(buf
, hierarchies
[i
], NULL
, 0) != 0) {
1773 if (!cgfs_list_keys(d
->controller
, d
->cgroup
, &list
)) {
1774 // not a valid cgroup
1779 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1782 if (!caller_is_in_ancestor(initpid
, d
->controller
, d
->cgroup
, &nextcg
)) {
1784 ret
= filler(buf
, nextcg
, NULL
, 0);
1795 for (i
= 0; list
[i
]; i
++) {
1796 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
1802 // now get the list of child cgroups
1804 if (!cgfs_list_children(d
->controller
, d
->cgroup
, &clist
)) {
1809 for (i
= 0; clist
[i
]; i
++) {
1810 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
1821 for (i
= 0; clist
[i
]; i
++)
1828 static void do_release_file_info(struct fuse_file_info
*fi
)
1830 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1837 free(f
->controller
);
1838 f
->controller
= NULL
;
1848 int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
1850 do_release_file_info(fi
);
1854 int cg_open(const char *path
, struct fuse_file_info
*fi
)
1857 char *last
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
1858 struct cgfs_files
*k
= NULL
;
1859 struct file_info
*file_info
;
1860 struct fuse_context
*fc
= fuse_get_context();
1866 controller
= pick_controller_from_path(fc
, path
);
1869 cgroup
= find_cgroup_in_path(path
);
1873 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1882 k
= cgfs_get_key(controller
, path1
, path2
);
1889 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1892 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
1896 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
1901 /* we'll free this at cg_release */
1902 file_info
= malloc(sizeof(*file_info
));
1907 file_info
->controller
= must_copy_string(controller
);
1908 file_info
->cgroup
= must_copy_string(path1
);
1909 file_info
->file
= must_copy_string(path2
);
1910 file_info
->type
= LXC_TYPE_CGFILE
;
1911 file_info
->buf
= NULL
;
1912 file_info
->buflen
= 0;
1914 fi
->fh
= (unsigned long)file_info
;
1922 int cg_access(const char *path
, int mode
)
1926 char *path1
, *path2
, *controller
;
1927 char *last
= NULL
, *cgdir
= NULL
;
1928 struct cgfs_files
*k
= NULL
;
1929 struct fuse_context
*fc
= fuse_get_context();
1931 if (strcmp(path
, "/cgroup") == 0)
1937 controller
= pick_controller_from_path(fc
, path
);
1940 cgroup
= find_cgroup_in_path(path
);
1942 // access("/sys/fs/cgroup/systemd", mode) - rx allowed, w not
1943 if ((mode
& W_OK
) == 0)
1948 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1957 k
= cgfs_get_key(controller
, path1
, path2
);
1959 if ((mode
& W_OK
) == 0)
1967 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1970 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
1974 if (!fc_may_access(fc
, controller
, path1
, path2
, mode
)) {
1986 int cg_release(const char *path
, struct fuse_file_info
*fi
)
1988 do_release_file_info(fi
);
1992 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
1994 static bool wait_for_sock(int sock
, int timeout
)
1996 struct epoll_event ev
;
1997 int epfd
, ret
, now
, starttime
, deltatime
, saved_errno
;
1999 if ((starttime
= time(NULL
)) < 0)
2002 if ((epfd
= epoll_create(1)) < 0) {
2003 lxcfs_error("%s\n", "Failed to create epoll socket: %m.");
2007 ev
.events
= POLLIN_SET
;
2009 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, sock
, &ev
) < 0) {
2010 lxcfs_error("%s\n", "Failed adding socket to epoll: %m.");
2016 if ((now
= time(NULL
)) < 0) {
2021 deltatime
= (starttime
+ timeout
) - now
;
2022 if (deltatime
< 0) { // timeout
2027 ret
= epoll_wait(epfd
, &ev
, 1, 1000*deltatime
+ 1);
2028 if (ret
< 0 && errno
== EINTR
)
2030 saved_errno
= errno
;
2034 errno
= saved_errno
;
2040 static int msgrecv(int sockfd
, void *buf
, size_t len
)
2042 if (!wait_for_sock(sockfd
, 2))
2044 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
2047 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
2049 struct msghdr msg
= { 0 };
2051 struct cmsghdr
*cmsg
;
2052 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2057 if (msgrecv(sock
, buf
, 1) != 1) {
2058 lxcfs_error("%s\n", "Error getting reply from server over socketpair.");
2059 return SEND_CREDS_FAIL
;
2063 msg
.msg_control
= cmsgbuf
;
2064 msg
.msg_controllen
= sizeof(cmsgbuf
);
2066 cmsg
= CMSG_FIRSTHDR(&msg
);
2067 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
2068 cmsg
->cmsg_level
= SOL_SOCKET
;
2069 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
2070 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
2072 msg
.msg_name
= NULL
;
2073 msg
.msg_namelen
= 0;
2077 iov
.iov_len
= sizeof(buf
);
2081 if (sendmsg(sock
, &msg
, 0) < 0) {
2082 lxcfs_error("Failed at sendmsg: %s.\n",strerror(errno
));
2084 return SEND_CREDS_NOTSK
;
2085 return SEND_CREDS_FAIL
;
2088 return SEND_CREDS_OK
;
2091 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
2093 struct msghdr msg
= { 0 };
2095 struct cmsghdr
*cmsg
;
2096 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2107 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
2108 lxcfs_error("Failed to set passcred: %s\n", strerror(errno
));
2112 if (write(sock
, buf
, 1) != 1) {
2113 lxcfs_error("Failed to start write on scm fd: %s\n", strerror(errno
));
2117 msg
.msg_name
= NULL
;
2118 msg
.msg_namelen
= 0;
2119 msg
.msg_control
= cmsgbuf
;
2120 msg
.msg_controllen
= sizeof(cmsgbuf
);
2123 iov
.iov_len
= sizeof(buf
);
2127 if (!wait_for_sock(sock
, 2)) {
2128 lxcfs_error("Timed out waiting for scm_cred: %s\n", strerror(errno
));
2131 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
2133 lxcfs_error("Failed to receive scm_cred: %s\n", strerror(errno
));
2137 cmsg
= CMSG_FIRSTHDR(&msg
);
2139 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
2140 cmsg
->cmsg_level
== SOL_SOCKET
&&
2141 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
2142 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
2149 struct pid_ns_clone_args
{
2153 int (*wrapped
) (int, pid_t
); // pid_from_ns or pid_to_ns
2157 * pid_ns_clone_wrapper - wraps pid_to_ns or pid_from_ns for usage
2158 * with clone(). This simply writes '1' as ACK back to the parent
2159 * before calling the actual wrapped function.
2161 static int pid_ns_clone_wrapper(void *arg
) {
2162 struct pid_ns_clone_args
* args
= (struct pid_ns_clone_args
*) arg
;
2165 close(args
->cpipe
[0]);
2166 if (write(args
->cpipe
[1], &b
, sizeof(char)) < 0)
2167 lxcfs_error("(child): error on write: %s.\n", strerror(errno
));
2168 close(args
->cpipe
[1]);
2169 return args
->wrapped(args
->sock
, args
->tpid
);
2173 * pid_to_ns - reads pids from a ucred over a socket, then writes the
2174 * int value back over the socket. This shifts the pid from the
2175 * sender's pidns into tpid's pidns.
2177 static int pid_to_ns(int sock
, pid_t tpid
)
2182 while (recv_creds(sock
, &cred
, &v
)) {
2185 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
2193 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
2194 * in your old pidns. Only children which you clone will be in the target
2195 * pidns. So the pid_to_ns_wrapper does the setns, then clones a child to
2196 * actually convert pids.
2198 * Note: glibc's fork() does not respect pidns, which can lead to failed
2199 * assertions inside glibc (and thus failed forks) if the child's pid in
2200 * the pidns and the parent pid outside are identical. Using clone prevents
2203 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
2205 int newnsfd
= -1, ret
, cpipe
[2];
2210 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2211 if (ret
< 0 || ret
>= sizeof(fnam
))
2213 newnsfd
= open(fnam
, O_RDONLY
);
2216 if (setns(newnsfd
, 0) < 0)
2220 if (pipe(cpipe
) < 0)
2223 struct pid_ns_clone_args args
= {
2227 .wrapped
= &pid_to_ns
2229 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2230 void *stack
= alloca(stack_size
);
2232 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2236 // give the child 1 second to be done forking and
2238 if (!wait_for_sock(cpipe
[0], 1))
2240 ret
= read(cpipe
[0], &v
, 1);
2241 if (ret
!= sizeof(char) || v
!= '1')
2244 if (!wait_for_pid(cpid
))
2250 * To read cgroup files with a particular pid, we will setns into the child
2251 * pidns, open a pipe, fork a child - which will be the first to really be in
2252 * the child ns - which does the cgfs_get_value and writes the data to the pipe.
2254 bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
2256 int sock
[2] = {-1, -1};
2257 char *tmpdata
= NULL
;
2259 pid_t qpid
, cpid
= -1;
2260 bool answer
= false;
2263 size_t sz
= 0, asz
= 0;
2265 if (!cgfs_get_value(contrl
, cg
, file
, &tmpdata
))
2269 * Now we read the pids from returned data one by one, pass
2270 * them into a child in the target namespace, read back the
2271 * translated pids, and put them into our to-return data
2274 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2275 perror("socketpair");
2284 if (!cpid
) // child - exits when done
2285 pid_to_ns_wrapper(sock
[1], tpid
);
2287 char *ptr
= tmpdata
;
2290 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
2292 ret
= send_creds(sock
[0], &cred
, v
, true);
2294 if (ret
== SEND_CREDS_NOTSK
)
2296 if (ret
== SEND_CREDS_FAIL
)
2299 // read converted results
2300 if (!wait_for_sock(sock
[0], 2)) {
2301 lxcfs_error("Timed out waiting for pid from child: %s.\n", strerror(errno
));
2304 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2305 lxcfs_error("Error reading pid from child: %s.\n", strerror(errno
));
2308 must_strcat_pid(d
, &sz
, &asz
, qpid
);
2310 ptr
= strchr(ptr
, '\n');
2316 cred
.pid
= getpid();
2318 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
2319 // failed to ask child to exit
2320 lxcfs_error("Failed to ask child to exit: %s.\n", strerror(errno
));
2330 if (sock
[0] != -1) {
2337 int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2338 struct fuse_file_info
*fi
)
2340 struct fuse_context
*fc
= fuse_get_context();
2341 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2342 struct cgfs_files
*k
= NULL
;
2347 if (f
->type
!= LXC_TYPE_CGFILE
) {
2348 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_read.");
2361 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2367 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) {
2372 if (strcmp(f
->file
, "tasks") == 0 ||
2373 strcmp(f
->file
, "/tasks") == 0 ||
2374 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2375 strcmp(f
->file
, "cgroup.procs") == 0)
2376 // special case - we have to translate the pids
2377 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
2379 r
= cgfs_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
2393 memcpy(buf
, data
, s
);
2394 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
2404 static int pid_from_ns(int sock
, pid_t tpid
)
2414 if (!wait_for_sock(sock
, 2)) {
2415 lxcfs_error("%s\n", "Timeout reading from parent.");
2418 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
2419 lxcfs_error("Bad read from parent: %s.\n", strerror(errno
));
2422 if (vpid
== -1) // done
2426 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
2428 cred
.pid
= getpid();
2429 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
2436 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
2438 int newnsfd
= -1, ret
, cpipe
[2];
2443 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2444 if (ret
< 0 || ret
>= sizeof(fnam
))
2446 newnsfd
= open(fnam
, O_RDONLY
);
2449 if (setns(newnsfd
, 0) < 0)
2453 if (pipe(cpipe
) < 0)
2456 struct pid_ns_clone_args args
= {
2460 .wrapped
= &pid_from_ns
2462 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2463 void *stack
= alloca(stack_size
);
2465 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2469 // give the child 1 second to be done forking and
2471 if (!wait_for_sock(cpipe
[0], 1))
2473 ret
= read(cpipe
[0], &v
, 1);
2474 if (ret
!= sizeof(char) || v
!= '1')
2477 if (!wait_for_pid(cpid
))
2483 * Given host @uid, return the uid to which it maps in
2484 * @pid's user namespace, or -1 if none.
2486 bool hostuid_to_ns(uid_t uid
, pid_t pid
, uid_t
*answer
)
2491 sprintf(line
, "/proc/%d/uid_map", pid
);
2492 if ((f
= fopen(line
, "r")) == NULL
) {
2496 *answer
= convert_id_to_ns(f
, uid
);
2505 * get_pid_creds: get the real uid and gid of @pid from
2507 * (XXX should we use euid here?)
2509 void get_pid_creds(pid_t pid
, uid_t
*uid
, gid_t
*gid
)
2518 sprintf(line
, "/proc/%d/status", pid
);
2519 if ((f
= fopen(line
, "r")) == NULL
) {
2520 lxcfs_error("Error opening %s: %s\n", line
, strerror(errno
));
2523 while (fgets(line
, 400, f
)) {
2524 if (strncmp(line
, "Uid:", 4) == 0) {
2525 if (sscanf(line
+4, "%u", &u
) != 1) {
2526 lxcfs_error("bad uid line for pid %u\n", pid
);
2531 } else if (strncmp(line
, "Gid:", 4) == 0) {
2532 if (sscanf(line
+4, "%u", &g
) != 1) {
2533 lxcfs_error("bad gid line for pid %u\n", pid
);
2544 * May the requestor @r move victim @v to a new cgroup?
2545 * This is allowed if
2546 * . they are the same task
2547 * . they are ownedy by the same uid
2548 * . @r is root on the host, or
2549 * . @v's uid is mapped into @r's where @r is root.
2551 bool may_move_pid(pid_t r
, uid_t r_uid
, pid_t v
)
2553 uid_t v_uid
, tmpuid
;
2560 get_pid_creds(v
, &v_uid
, &v_gid
);
2563 if (hostuid_to_ns(r_uid
, r
, &tmpuid
) && tmpuid
== 0
2564 && hostuid_to_ns(v_uid
, r
, &tmpuid
))
2569 static bool do_write_pids(pid_t tpid
, uid_t tuid
, const char *contrl
, const char *cg
,
2570 const char *file
, const char *buf
)
2572 int sock
[2] = {-1, -1};
2573 pid_t qpid
, cpid
= -1;
2574 FILE *pids_file
= NULL
;
2575 bool answer
= false, fail
= false;
2577 pids_file
= open_pids_file(contrl
, cg
);
2582 * write the pids to a socket, have helper in writer's pidns
2583 * call movepid for us
2585 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2586 perror("socketpair");
2594 if (!cpid
) { // child
2596 pid_from_ns_wrapper(sock
[1], tpid
);
2599 const char *ptr
= buf
;
2600 while (sscanf(ptr
, "%d", &qpid
) == 1) {
2604 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2605 lxcfs_error("Error writing pid to child: %s.\n", strerror(errno
));
2609 if (recv_creds(sock
[0], &cred
, &v
)) {
2611 if (!may_move_pid(tpid
, tuid
, cred
.pid
)) {
2615 if (fprintf(pids_file
, "%d", (int) cred
.pid
) < 0)
2620 ptr
= strchr(ptr
, '\n');
2626 /* All good, write the value */
2628 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
2629 lxcfs_error("%s\n", "Warning: failed to ask child to exit.");
2637 if (sock
[0] != -1) {
2642 if (fclose(pids_file
) != 0)
2648 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2649 struct fuse_file_info
*fi
)
2651 struct fuse_context
*fc
= fuse_get_context();
2652 char *localbuf
= NULL
;
2653 struct cgfs_files
*k
= NULL
;
2654 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2657 if (f
->type
!= LXC_TYPE_CGFILE
) {
2658 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_write.");
2668 localbuf
= alloca(size
+1);
2669 localbuf
[size
] = '\0';
2670 memcpy(localbuf
, buf
, size
);
2672 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2677 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
2682 if (strcmp(f
->file
, "tasks") == 0 ||
2683 strcmp(f
->file
, "/tasks") == 0 ||
2684 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2685 strcmp(f
->file
, "cgroup.procs") == 0)
2686 // special case - we have to translate the pids
2687 r
= do_write_pids(fc
->pid
, fc
->uid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2689 r
= cgfs_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2699 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
2701 struct fuse_context
*fc
= fuse_get_context();
2702 char *cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2703 struct cgfs_files
*k
= NULL
;
2710 if (strcmp(path
, "/cgroup") == 0)
2713 controller
= pick_controller_from_path(fc
, path
);
2715 return errno
== ENOENT
? -EPERM
: -errno
;
2717 cgroup
= find_cgroup_in_path(path
);
2719 /* this is just /cgroup/controller */
2722 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2732 if (is_child_cgroup(controller
, path1
, path2
)) {
2733 // get uid, gid, from '/tasks' file and make up a mode
2734 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2735 k
= cgfs_get_key(controller
, cgroup
, "tasks");
2738 k
= cgfs_get_key(controller
, path1
, path2
);
2746 * This being a fuse request, the uid and gid must be valid
2747 * in the caller's namespace. So we can just check to make
2748 * sure that the caller is root in his uid, and privileged
2749 * over the file's current owner.
2751 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
2756 ret
= cgfs_chown_file(controller
, cgroup
, uid
, gid
);
2765 int cg_chmod(const char *path
, mode_t mode
)
2767 struct fuse_context
*fc
= fuse_get_context();
2768 char * cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2769 struct cgfs_files
*k
= NULL
;
2776 if (strcmp(path
, "/cgroup") == 0)
2779 controller
= pick_controller_from_path(fc
, path
);
2781 return errno
== ENOENT
? -EPERM
: -errno
;
2783 cgroup
= find_cgroup_in_path(path
);
2785 /* this is just /cgroup/controller */
2788 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2798 if (is_child_cgroup(controller
, path1
, path2
)) {
2799 // get uid, gid, from '/tasks' file and make up a mode
2800 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2801 k
= cgfs_get_key(controller
, cgroup
, "tasks");
2804 k
= cgfs_get_key(controller
, path1
, path2
);
2812 * This being a fuse request, the uid and gid must be valid
2813 * in the caller's namespace. So we can just check to make
2814 * sure that the caller is root in his uid, and privileged
2815 * over the file's current owner.
2817 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
2822 if (!cgfs_chmod_file(controller
, cgroup
, mode
)) {
2834 int cg_mkdir(const char *path
, mode_t mode
)
2836 struct fuse_context
*fc
= fuse_get_context();
2837 char *last
= NULL
, *path1
, *cgdir
= NULL
, *controller
, *next
= NULL
;
2844 controller
= pick_controller_from_path(fc
, path
);
2846 return errno
== ENOENT
? -EPERM
: -errno
;
2848 cgroup
= find_cgroup_in_path(path
);
2852 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2858 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2861 if (!caller_is_in_ancestor(initpid
, controller
, path1
, &next
)) {
2864 else if (last
&& strcmp(next
, last
) == 0)
2871 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
2875 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
2880 ret
= cgfs_create(controller
, cgroup
, fc
->uid
, fc
->gid
);
2888 int cg_rmdir(const char *path
)
2890 struct fuse_context
*fc
= fuse_get_context();
2891 char *last
= NULL
, *cgdir
= NULL
, *controller
, *next
= NULL
;
2898 controller
= pick_controller_from_path(fc
, path
);
2899 if (!controller
) /* Someone's trying to delete "/cgroup". */
2902 cgroup
= find_cgroup_in_path(path
);
2903 if (!cgroup
) /* Someone's trying to delete a controller e.g. "/blkio". */
2906 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2908 /* Someone's trying to delete a cgroup on the same level as the
2909 * "/lxc" cgroup e.g. rmdir "/cgroup/blkio/lxc" or
2910 * rmdir "/cgroup/blkio/init.slice".
2916 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2919 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, &next
)) {
2920 if (!last
|| (next
&& (strcmp(next
, last
) == 0)))
2927 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
2931 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
2936 if (!cgfs_remove(controller
, cgroup
)) {
2949 static bool startswith(const char *line
, const char *pref
)
2951 if (strncmp(line
, pref
, strlen(pref
)) == 0)
2956 static void parse_memstat(char *memstat
, unsigned long *cached
,
2957 unsigned long *active_anon
, unsigned long *inactive_anon
,
2958 unsigned long *active_file
, unsigned long *inactive_file
,
2959 unsigned long *unevictable
)
2964 if (startswith(memstat
, "total_cache")) {
2965 sscanf(memstat
+ 11, "%lu", cached
);
2967 } else if (startswith(memstat
, "total_active_anon")) {
2968 sscanf(memstat
+ 17, "%lu", active_anon
);
2969 *active_anon
/= 1024;
2970 } else if (startswith(memstat
, "total_inactive_anon")) {
2971 sscanf(memstat
+ 19, "%lu", inactive_anon
);
2972 *inactive_anon
/= 1024;
2973 } else if (startswith(memstat
, "total_active_file")) {
2974 sscanf(memstat
+ 17, "%lu", active_file
);
2975 *active_file
/= 1024;
2976 } else if (startswith(memstat
, "total_inactive_file")) {
2977 sscanf(memstat
+ 19, "%lu", inactive_file
);
2978 *inactive_file
/= 1024;
2979 } else if (startswith(memstat
, "total_unevictable")) {
2980 sscanf(memstat
+ 17, "%lu", unevictable
);
2981 *unevictable
/= 1024;
2983 eol
= strchr(memstat
, '\n');
2990 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
2996 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
2998 size_t len
= strlen(key
);
3002 if (startswith(str
, key
)) {
3003 sscanf(str
+ len
, "%lu", v
);
3006 eol
= strchr(str
, '\n');
3013 static int read_file(const char *path
, char *buf
, size_t size
,
3014 struct file_info
*d
)
3016 size_t linelen
= 0, total_len
= 0, rv
= 0;
3018 char *cache
= d
->buf
;
3019 size_t cache_size
= d
->buflen
;
3020 FILE *f
= fopen(path
, "r");
3024 while (getline(&line
, &linelen
, f
) != -1) {
3025 ssize_t l
= snprintf(cache
, cache_size
, "%s", line
);
3027 perror("Error writing to cache");
3031 if (l
>= cache_size
) {
3032 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3041 d
->size
= total_len
;
3042 if (total_len
> size
)
3045 /* read from off 0 */
3046 memcpy(buf
, d
->buf
, total_len
);
3055 * FUSE ops for /proc
3058 static unsigned long get_memlimit(const char *cgroup
, const char *file
)
3060 char *memlimit_str
= NULL
;
3061 unsigned long memlimit
= -1;
3063 if (cgfs_get_value("memory", cgroup
, file
, &memlimit_str
))
3064 memlimit
= strtoul(memlimit_str
, NULL
, 10);
3071 static unsigned long get_min_memlimit(const char *cgroup
, const char *file
)
3073 char *copy
= strdupa(cgroup
);
3074 unsigned long memlimit
= 0, retlimit
;
3076 retlimit
= get_memlimit(copy
, file
);
3078 while (strcmp(copy
, "/") != 0) {
3079 copy
= dirname(copy
);
3080 memlimit
= get_memlimit(copy
, file
);
3081 if (memlimit
!= -1 && memlimit
< retlimit
)
3082 retlimit
= memlimit
;
3088 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
3089 struct fuse_file_info
*fi
)
3091 struct fuse_context
*fc
= fuse_get_context();
3092 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3094 char *memusage_str
= NULL
, *memstat_str
= NULL
,
3095 *memswlimit_str
= NULL
, *memswusage_str
= NULL
;
3096 unsigned long memlimit
= 0, memusage
= 0, memswlimit
= 0, memswusage
= 0,
3097 cached
= 0, hosttotal
= 0, active_anon
= 0, inactive_anon
= 0,
3098 active_file
= 0, inactive_file
= 0, unevictable
= 0,
3101 size_t linelen
= 0, total_len
= 0, rv
= 0;
3102 char *cache
= d
->buf
;
3103 size_t cache_size
= d
->buflen
;
3107 if (offset
> d
->size
)
3111 int left
= d
->size
- offset
;
3112 total_len
= left
> size
? size
: left
;
3113 memcpy(buf
, cache
+ offset
, total_len
);
3117 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3120 cg
= get_pid_cgroup(initpid
, "memory");
3122 return read_file("/proc/meminfo", buf
, size
, d
);
3123 prune_init_slice(cg
);
3125 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
3126 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
3128 if (!cgfs_get_value("memory", cg
, "memory.stat", &memstat_str
))
3131 // Following values are allowed to fail, because swapaccount might be turned
3132 // off for current kernel
3133 if(cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
) &&
3134 cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
))
3136 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
3137 memswusage
= strtoul(memswusage_str
, NULL
, 10);
3139 memswlimit
= memswlimit
/ 1024;
3140 memswusage
= memswusage
/ 1024;
3143 memusage
= strtoul(memusage_str
, NULL
, 10);
3147 parse_memstat(memstat_str
, &cached
, &active_anon
,
3148 &inactive_anon
, &active_file
, &inactive_file
,
3151 f
= fopen("/proc/meminfo", "r");
3155 while (getline(&line
, &linelen
, f
) != -1) {
3157 char *printme
, lbuf
[100];
3159 memset(lbuf
, 0, 100);
3160 if (startswith(line
, "MemTotal:")) {
3161 sscanf(line
+sizeof("MemTotal:")-1, "%lu", &hosttotal
);
3162 if (hosttotal
< memlimit
)
3163 memlimit
= hosttotal
;
3164 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
3166 } else if (startswith(line
, "MemFree:")) {
3167 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
3169 } else if (startswith(line
, "MemAvailable:")) {
3170 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
+ cached
);
3172 } else if (startswith(line
, "SwapTotal:") && memswlimit
> 0) {
3173 sscanf(line
+sizeof("SwapTotal:")-1, "%lu", &hostswtotal
);
3174 if (hostswtotal
< memswlimit
)
3175 memswlimit
= hostswtotal
;
3176 snprintf(lbuf
, 100, "SwapTotal: %8lu kB\n", memswlimit
);
3178 } else if (startswith(line
, "SwapFree:") && memswlimit
> 0 && memswusage
> 0) {
3179 unsigned long swaptotal
= memswlimit
,
3180 swapusage
= memswusage
- memusage
,
3181 swapfree
= swapusage
< swaptotal
? swaptotal
- swapusage
: 0;
3182 snprintf(lbuf
, 100, "SwapFree: %8lu kB\n", swapfree
);
3184 } else if (startswith(line
, "Slab:")) {
3185 snprintf(lbuf
, 100, "Slab: %8lu kB\n", 0UL);
3187 } else if (startswith(line
, "Buffers:")) {
3188 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
3190 } else if (startswith(line
, "Cached:")) {
3191 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
3193 } else if (startswith(line
, "SwapCached:")) {
3194 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
3196 } else if (startswith(line
, "Active:")) {
3197 snprintf(lbuf
, 100, "Active: %8lu kB\n",
3198 active_anon
+ active_file
);
3200 } else if (startswith(line
, "Inactive:")) {
3201 snprintf(lbuf
, 100, "Inactive: %8lu kB\n",
3202 inactive_anon
+ inactive_file
);
3204 } else if (startswith(line
, "Active(anon)")) {
3205 snprintf(lbuf
, 100, "Active(anon): %8lu kB\n", active_anon
);
3207 } else if (startswith(line
, "Inactive(anon)")) {
3208 snprintf(lbuf
, 100, "Inactive(anon): %8lu kB\n", inactive_anon
);
3210 } else if (startswith(line
, "Active(file)")) {
3211 snprintf(lbuf
, 100, "Active(file): %8lu kB\n", active_file
);
3213 } else if (startswith(line
, "Inactive(file)")) {
3214 snprintf(lbuf
, 100, "Inactive(file): %8lu kB\n", inactive_file
);
3216 } else if (startswith(line
, "Unevictable")) {
3217 snprintf(lbuf
, 100, "Unevictable: %8lu kB\n", unevictable
);
3219 } else if (startswith(line
, "SReclaimable")) {
3220 snprintf(lbuf
, 100, "SReclaimable: %8lu kB\n", 0UL);
3222 } else if (startswith(line
, "SUnreclaim")) {
3223 snprintf(lbuf
, 100, "SUnreclaim: %8lu kB\n", 0UL);
3228 l
= snprintf(cache
, cache_size
, "%s", printme
);
3230 perror("Error writing to cache");
3235 if (l
>= cache_size
) {
3236 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3247 d
->size
= total_len
;
3248 if (total_len
> size
) total_len
= size
;
3249 memcpy(buf
, d
->buf
, total_len
);
3258 free(memswlimit_str
);
3259 free(memswusage_str
);
3265 * Read the cpuset.cpus for cg
3266 * Return the answer in a newly allocated string which must be freed
3268 static char *get_cpuset(const char *cg
)
3272 if (!cgfs_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
3277 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
3279 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
3283 if (sscanf(line
, "processor : %d", &cpu
) != 1)
3285 return cpu_in_cpuset(cpu
, cpuset
);
3289 * check whether this is a '^processor" line in /proc/cpuinfo
3291 static bool is_processor_line(const char *line
)
3295 if (sscanf(line
, "processor : %d", &cpu
) == 1)
3300 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
3301 struct fuse_file_info
*fi
)
3303 struct fuse_context
*fc
= fuse_get_context();
3304 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3306 char *cpuset
= NULL
;
3308 size_t linelen
= 0, total_len
= 0, rv
= 0;
3309 bool am_printing
= false, firstline
= true, is_s390x
= false;
3310 int curcpu
= -1, cpu
;
3311 char *cache
= d
->buf
;
3312 size_t cache_size
= d
->buflen
;
3316 if (offset
> d
->size
)
3320 int left
= d
->size
- offset
;
3321 total_len
= left
> size
? size
: left
;
3322 memcpy(buf
, cache
+ offset
, total_len
);
3326 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3329 cg
= get_pid_cgroup(initpid
, "cpuset");
3331 return read_file("proc/cpuinfo", buf
, size
, d
);
3332 prune_init_slice(cg
);
3334 cpuset
= get_cpuset(cg
);
3338 f
= fopen("/proc/cpuinfo", "r");
3342 while (getline(&line
, &linelen
, f
) != -1) {
3346 if (strstr(line
, "IBM/S390") != NULL
) {
3352 if (strncmp(line
, "# processors:", 12) == 0)
3354 if (is_processor_line(line
)) {
3355 am_printing
= cpuline_in_cpuset(line
, cpuset
);
3358 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
3360 perror("Error writing to cache");
3364 if (l
>= cache_size
) {
3365 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3374 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
3376 if (!cpu_in_cpuset(cpu
, cpuset
))
3379 p
= strchr(line
, ':');
3383 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
3385 perror("Error writing to cache");
3389 if (l
>= cache_size
) {
3390 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3401 l
= snprintf(cache
, cache_size
, "%s", line
);
3403 perror("Error writing to cache");
3407 if (l
>= cache_size
) {
3408 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3419 char *origcache
= d
->buf
;
3422 d
->buf
= malloc(d
->buflen
);
3425 cache_size
= d
->buflen
;
3427 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
3428 if (l
< 0 || l
>= cache_size
) {
3435 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
3436 if (l
< 0 || l
>= cache_size
) {
3443 l
= snprintf(cache
, cache_size
, "%s", origcache
);
3445 if (l
< 0 || l
>= cache_size
)
3451 d
->size
= total_len
;
3452 if (total_len
> size
) total_len
= size
;
3454 /* read from off 0 */
3455 memcpy(buf
, d
->buf
, total_len
);
3466 static uint64_t get_reaper_start_time(pid_t pid
)
3471 /* strlen("/proc/") = 6
3475 * strlen("/stat") = 5
3479 #define __PROC_PID_STAT_LEN (6 + LXCFS_NUMSTRLEN64 + 5 + 1)
3480 char path
[__PROC_PID_STAT_LEN
];
3483 qpid
= lookup_initpid_in_store(pid
);
3485 /* Caller can check for EINVAL on 0. */
3490 ret
= snprintf(path
, __PROC_PID_STAT_LEN
, "/proc/%d/stat", qpid
);
3491 if (ret
< 0 || ret
>= __PROC_PID_STAT_LEN
) {
3492 /* Caller can check for EINVAL on 0. */
3497 f
= fopen(path
, "r");
3499 /* Caller can check for EINVAL on 0. */
3504 /* Note that the *scanf() argument supression requires that length
3505 * modifiers such as "l" are omitted. Otherwise some compilers will yell
3506 * at us. It's like telling someone you're not married and then asking
3507 * if you can bring your wife to the party.
3509 ret
= fscanf(f
, "%*d " /* (1) pid %d */
3510 "%*s " /* (2) comm %s */
3511 "%*c " /* (3) state %c */
3512 "%*d " /* (4) ppid %d */
3513 "%*d " /* (5) pgrp %d */
3514 "%*d " /* (6) session %d */
3515 "%*d " /* (7) tty_nr %d */
3516 "%*d " /* (8) tpgid %d */
3517 "%*u " /* (9) flags %u */
3518 "%*u " /* (10) minflt %lu */
3519 "%*u " /* (11) cminflt %lu */
3520 "%*u " /* (12) majflt %lu */
3521 "%*u " /* (13) cmajflt %lu */
3522 "%*u " /* (14) utime %lu */
3523 "%*u " /* (15) stime %lu */
3524 "%*d " /* (16) cutime %ld */
3525 "%*d " /* (17) cstime %ld */
3526 "%*d " /* (18) priority %ld */
3527 "%*d " /* (19) nice %ld */
3528 "%*d " /* (20) num_threads %ld */
3529 "%*d " /* (21) itrealvalue %ld */
3530 "%" PRIu64
, /* (22) starttime %llu */
3534 /* Caller can check for EINVAL on 0. */
3545 static uint64_t get_reaper_start_time_in_sec(pid_t pid
)
3547 uint64_t clockticks
;
3548 int64_t ticks_per_sec
;
3550 clockticks
= get_reaper_start_time(pid
);
3551 if (clockticks
== 0 && errno
== EINVAL
) {
3552 lxcfs_debug("failed to retrieve start time of pid %d\n", pid
);
3556 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
3557 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
3560 "failed to determine number of clock ticks in a second");
3564 return (clockticks
/= ticks_per_sec
);
3567 static uint64_t get_reaper_age(pid_t pid
)
3569 uint64_t procstart
, uptime
, procage
;
3571 /* We need to substract the time the process has started since system
3572 * boot minus the time when the system has started to get the actual
3575 procstart
= get_reaper_start_time_in_sec(pid
);
3576 procage
= procstart
;
3577 if (procstart
> 0) {
3579 struct timespec spec
;
3581 ret
= clock_gettime(CLOCK_BOOTTIME
, &spec
);
3584 /* We could make this more precise here by using the tv_nsec
3585 * field in the timespec struct and convert it to milliseconds
3586 * and then create a double for the seconds and milliseconds but
3587 * that seems more work than it is worth.
3589 uptime
= spec
.tv_sec
;
3590 procage
= uptime
- procstart
;
3596 #define CPUALL_MAX_SIZE (BUF_RESERVE_SIZE / 2)
3597 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
3598 struct fuse_file_info
*fi
)
3600 struct fuse_context
*fc
= fuse_get_context();
3601 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3603 char *cpuset
= NULL
;
3605 size_t linelen
= 0, total_len
= 0, rv
= 0;
3606 int curcpu
= -1; /* cpu numbering starts at 0 */
3607 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
3608 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
3609 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0, guest_nice_sum
= 0;
3610 char cpuall
[CPUALL_MAX_SIZE
];
3611 /* reserve for cpu all */
3612 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
3613 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
3617 if (offset
> d
->size
)
3621 int left
= d
->size
- offset
;
3622 total_len
= left
> size
? size
: left
;
3623 memcpy(buf
, d
->buf
+ offset
, total_len
);
3627 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3630 cg
= get_pid_cgroup(initpid
, "cpuset");
3632 return read_file("/proc/stat", buf
, size
, d
);
3633 prune_init_slice(cg
);
3635 cpuset
= get_cpuset(cg
);
3639 f
= fopen("/proc/stat", "r");
3644 if (getline(&line
, &linelen
, f
) < 0) {
3645 lxcfs_error("%s\n", "proc_stat_read read first line failed.");
3649 while (getline(&line
, &linelen
, f
) != -1) {
3652 char cpu_char
[10]; /* That's a lot of cores */
3655 if (strlen(line
) == 0)
3657 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
3658 /* not a ^cpuN line containing a number N, just print it */
3659 l
= snprintf(cache
, cache_size
, "%s", line
);
3661 perror("Error writing to cache");
3665 if (l
>= cache_size
) {
3666 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3676 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
3678 if (!cpu_in_cpuset(cpu
, cpuset
))
3682 c
= strchr(line
, ' ');
3685 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
3687 perror("Error writing to cache");
3692 if (l
>= cache_size
) {
3693 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3702 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
3716 system_sum
+= system
;
3718 iowait_sum
+= iowait
;
3720 softirq_sum
+= softirq
;
3723 guest_nice_sum
+= guest_nice
;
3728 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
3739 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
) {
3740 memcpy(cache
, cpuall
, cpuall_len
);
3741 cache
+= cpuall_len
;
3743 /* shouldn't happen */
3744 lxcfs_error("proc_stat_read copy cpuall failed, cpuall_len=%d.", cpuall_len
);
3748 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
3749 total_len
+= cpuall_len
;
3751 d
->size
= total_len
;
3752 if (total_len
> size
)
3755 memcpy(buf
, d
->buf
, total_len
);
3767 /* This function retrieves the busy time of a group of tasks by looking at
3768 * cpuacct.usage. Unfortunately, this only makes sense when the container has
3769 * been given it's own cpuacct cgroup. If not, this function will take the busy
3770 * time of all other taks that do not actually belong to the container into
3771 * account as well. If someone has a clever solution for this please send a
3774 static unsigned long get_reaper_busy(pid_t task
)
3776 pid_t initpid
= lookup_initpid_in_store(task
);
3777 char *cgroup
= NULL
, *usage_str
= NULL
;
3778 unsigned long usage
= 0;
3783 cgroup
= get_pid_cgroup(initpid
, "cpuacct");
3786 prune_init_slice(cgroup
);
3787 if (!cgfs_get_value("cpuacct", cgroup
, "cpuacct.usage", &usage_str
))
3789 usage
= strtoul(usage_str
, NULL
, 10);
3790 usage
/= 1000000000;
3803 fd
= creat("/tmp/lxcfs-iwashere", 0644);
3810 * We read /proc/uptime and reuse its second field.
3811 * For the first field, we use the mtime for the reaper for
3812 * the calling pid as returned by getreaperage
3814 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
3815 struct fuse_file_info
*fi
)
3817 struct fuse_context
*fc
= fuse_get_context();
3818 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3819 unsigned long int busytime
= get_reaper_busy(fc
->pid
);
3820 char *cache
= d
->buf
;
3821 ssize_t total_len
= 0;
3822 uint64_t idletime
, reaperage
;
3831 if (offset
> d
->size
)
3833 int left
= d
->size
- offset
;
3834 total_len
= left
> size
? size
: left
;
3835 memcpy(buf
, cache
+ offset
, total_len
);
3839 reaperage
= get_reaper_age(fc
->pid
);
3840 /* To understand why this is done, please read the comment to the
3841 * get_reaper_busy() function.
3843 idletime
= reaperage
;
3844 if (reaperage
>= busytime
)
3845 idletime
= reaperage
- busytime
;
3847 total_len
= snprintf(d
->buf
, d
->buflen
, "%"PRIu64
".00 %"PRIu64
".00\n", reaperage
, idletime
);
3848 if (total_len
< 0 || total_len
>= d
->buflen
){
3849 lxcfs_error("%s\n", "failed to write to cache");
3853 d
->size
= (int)total_len
;
3856 if (total_len
> size
) total_len
= size
;
3858 memcpy(buf
, d
->buf
, total_len
);
3862 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
3863 struct fuse_file_info
*fi
)
3866 struct fuse_context
*fc
= fuse_get_context();
3867 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3869 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
3870 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
3871 unsigned long read
= 0, write
= 0;
3872 unsigned long read_merged
= 0, write_merged
= 0;
3873 unsigned long read_sectors
= 0, write_sectors
= 0;
3874 unsigned long read_ticks
= 0, write_ticks
= 0;
3875 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
3876 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
3877 char *cache
= d
->buf
;
3878 size_t cache_size
= d
->buflen
;
3880 size_t linelen
= 0, total_len
= 0, rv
= 0;
3881 unsigned int major
= 0, minor
= 0;
3886 if (offset
> d
->size
)
3890 int left
= d
->size
- offset
;
3891 total_len
= left
> size
? size
: left
;
3892 memcpy(buf
, cache
+ offset
, total_len
);
3896 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3899 cg
= get_pid_cgroup(initpid
, "blkio");
3901 return read_file("/proc/diskstats", buf
, size
, d
);
3902 prune_init_slice(cg
);
3904 if (!cgfs_get_value("blkio", cg
, "blkio.io_serviced_recursive", &io_serviced_str
))
3906 if (!cgfs_get_value("blkio", cg
, "blkio.io_merged_recursive", &io_merged_str
))
3908 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_bytes_recursive", &io_service_bytes_str
))
3910 if (!cgfs_get_value("blkio", cg
, "blkio.io_wait_time_recursive", &io_wait_time_str
))
3912 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_time_recursive", &io_service_time_str
))
3916 f
= fopen("/proc/diskstats", "r");
3920 while (getline(&line
, &linelen
, f
) != -1) {
3924 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
3928 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
3929 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
3930 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
3931 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
3932 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
3933 read_sectors
= read_sectors
/512;
3934 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
3935 write_sectors
= write_sectors
/512;
3937 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
3938 rd_svctm
= rd_svctm
/1000000;
3939 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
3940 rd_wait
= rd_wait
/1000000;
3941 read_ticks
= rd_svctm
+ rd_wait
;
3943 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
3944 wr_svctm
= wr_svctm
/1000000;
3945 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
3946 wr_wait
= wr_wait
/1000000;
3947 write_ticks
= wr_svctm
+ wr_wait
;
3949 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
3950 tot_ticks
= tot_ticks
/1000000;
3952 memset(lbuf
, 0, 256);
3953 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
)
3954 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
3955 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
3956 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
3960 l
= snprintf(cache
, cache_size
, "%s", lbuf
);
3962 perror("Error writing to fuse buf");
3966 if (l
>= cache_size
) {
3967 lxcfs_error("%s\n", "Internal error: truncated write to cache.");
3977 d
->size
= total_len
;
3978 if (total_len
> size
) total_len
= size
;
3979 memcpy(buf
, d
->buf
, total_len
);
3987 free(io_serviced_str
);
3988 free(io_merged_str
);
3989 free(io_service_bytes_str
);
3990 free(io_wait_time_str
);
3991 free(io_service_time_str
);
3995 static int proc_swaps_read(char *buf
, size_t size
, off_t offset
,
3996 struct fuse_file_info
*fi
)
3998 struct fuse_context
*fc
= fuse_get_context();
3999 struct file_info
*d
= (struct file_info
*)fi
->fh
;
4001 char *memswlimit_str
= NULL
, *memlimit_str
= NULL
, *memusage_str
= NULL
, *memswusage_str
= NULL
;
4002 unsigned long memswlimit
= 0, memlimit
= 0, memusage
= 0, memswusage
= 0, swap_total
= 0, swap_free
= 0;
4003 ssize_t total_len
= 0, rv
= 0;
4005 char *cache
= d
->buf
;
4008 if (offset
> d
->size
)
4012 int left
= d
->size
- offset
;
4013 total_len
= left
> size
? size
: left
;
4014 memcpy(buf
, cache
+ offset
, total_len
);
4018 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
4021 cg
= get_pid_cgroup(initpid
, "memory");
4023 return read_file("/proc/swaps", buf
, size
, d
);
4024 prune_init_slice(cg
);
4026 memlimit
= get_min_memlimit(cg
, "memory.limit_in_bytes");
4028 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
4031 memusage
= strtoul(memusage_str
, NULL
, 10);
4033 if (cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
) &&
4034 cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
)) {
4036 memswlimit
= get_min_memlimit(cg
, "memory.memsw.limit_in_bytes");
4037 memswusage
= strtoul(memswusage_str
, NULL
, 10);
4039 swap_total
= (memswlimit
- memlimit
) / 1024;
4040 swap_free
= (memswusage
- memusage
) / 1024;
4043 total_len
= snprintf(d
->buf
, d
->size
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
4045 /* When no mem + swap limit is specified or swapaccount=0*/
4049 FILE *f
= fopen("/proc/meminfo", "r");
4054 while (getline(&line
, &linelen
, f
) != -1) {
4055 if (startswith(line
, "SwapTotal:")) {
4056 sscanf(line
, "SwapTotal: %8lu kB", &swap_total
);
4057 } else if (startswith(line
, "SwapFree:")) {
4058 sscanf(line
, "SwapFree: %8lu kB", &swap_free
);
4066 if (swap_total
> 0) {
4067 l
= snprintf(d
->buf
+ total_len
, d
->size
- total_len
,
4068 "none%*svirtual\t\t%lu\t%lu\t0\n", 36, " ",
4069 swap_total
, swap_free
);
4073 if (total_len
< 0 || l
< 0) {
4074 perror("Error writing to cache");
4080 d
->size
= (int)total_len
;
4082 if (total_len
> size
) total_len
= size
;
4083 memcpy(buf
, d
->buf
, total_len
);
4088 free(memswlimit_str
);
4091 free(memswusage_str
);
4095 static off_t
get_procfile_size(const char *which
)
4097 FILE *f
= fopen(which
, "r");
4100 ssize_t sz
, answer
= 0;
4104 while ((sz
= getline(&line
, &len
, f
)) != -1)
4112 int proc_getattr(const char *path
, struct stat
*sb
)
4114 struct timespec now
;
4116 memset(sb
, 0, sizeof(struct stat
));
4117 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
4119 sb
->st_uid
= sb
->st_gid
= 0;
4120 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
4121 if (strcmp(path
, "/proc") == 0) {
4122 sb
->st_mode
= S_IFDIR
| 00555;
4126 if (strcmp(path
, "/proc/meminfo") == 0 ||
4127 strcmp(path
, "/proc/cpuinfo") == 0 ||
4128 strcmp(path
, "/proc/uptime") == 0 ||
4129 strcmp(path
, "/proc/stat") == 0 ||
4130 strcmp(path
, "/proc/diskstats") == 0 ||
4131 strcmp(path
, "/proc/swaps") == 0) {
4133 sb
->st_mode
= S_IFREG
| 00444;
4141 int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
4142 struct fuse_file_info
*fi
)
4144 if (filler(buf
, ".", NULL
, 0) != 0 ||
4145 filler(buf
, "..", NULL
, 0) != 0 ||
4146 filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
4147 filler(buf
, "meminfo", NULL
, 0) != 0 ||
4148 filler(buf
, "stat", NULL
, 0) != 0 ||
4149 filler(buf
, "uptime", NULL
, 0) != 0 ||
4150 filler(buf
, "diskstats", NULL
, 0) != 0 ||
4151 filler(buf
, "swaps", NULL
, 0) != 0)
4156 int proc_open(const char *path
, struct fuse_file_info
*fi
)
4159 struct file_info
*info
;
4161 if (strcmp(path
, "/proc/meminfo") == 0)
4162 type
= LXC_TYPE_PROC_MEMINFO
;
4163 else if (strcmp(path
, "/proc/cpuinfo") == 0)
4164 type
= LXC_TYPE_PROC_CPUINFO
;
4165 else if (strcmp(path
, "/proc/uptime") == 0)
4166 type
= LXC_TYPE_PROC_UPTIME
;
4167 else if (strcmp(path
, "/proc/stat") == 0)
4168 type
= LXC_TYPE_PROC_STAT
;
4169 else if (strcmp(path
, "/proc/diskstats") == 0)
4170 type
= LXC_TYPE_PROC_DISKSTATS
;
4171 else if (strcmp(path
, "/proc/swaps") == 0)
4172 type
= LXC_TYPE_PROC_SWAPS
;
4176 info
= malloc(sizeof(*info
));
4180 memset(info
, 0, sizeof(*info
));
4183 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
4185 info
->buf
= malloc(info
->buflen
);
4186 } while (!info
->buf
);
4187 memset(info
->buf
, 0, info
->buflen
);
4188 /* set actual size to buffer size */
4189 info
->size
= info
->buflen
;
4191 fi
->fh
= (unsigned long)info
;
4195 int proc_access(const char *path
, int mask
)
4197 if (strcmp(path
, "/proc") == 0 && access(path
, R_OK
) == 0)
4200 /* these are all read-only */
4201 if ((mask
& ~R_OK
) != 0)
4206 int proc_release(const char *path
, struct fuse_file_info
*fi
)
4208 do_release_file_info(fi
);
4212 int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
4213 struct fuse_file_info
*fi
)
4215 struct file_info
*f
= (struct file_info
*) fi
->fh
;
4218 case LXC_TYPE_PROC_MEMINFO
:
4219 return proc_meminfo_read(buf
, size
, offset
, fi
);
4220 case LXC_TYPE_PROC_CPUINFO
:
4221 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
4222 case LXC_TYPE_PROC_UPTIME
:
4223 return proc_uptime_read(buf
, size
, offset
, fi
);
4224 case LXC_TYPE_PROC_STAT
:
4225 return proc_stat_read(buf
, size
, offset
, fi
);
4226 case LXC_TYPE_PROC_DISKSTATS
:
4227 return proc_diskstats_read(buf
, size
, offset
, fi
);
4228 case LXC_TYPE_PROC_SWAPS
:
4229 return proc_swaps_read(buf
, size
, offset
, fi
);
4236 * Functions needed to setup cgroups in the __constructor__.
4239 static bool mkdir_p(const char *dir
, mode_t mode
)
4241 const char *tmp
= dir
;
4242 const char *orig
= dir
;
4246 dir
= tmp
+ strspn(tmp
, "/");
4247 tmp
= dir
+ strcspn(dir
, "/");
4248 makeme
= strndup(orig
, dir
- orig
);
4251 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
4252 lxcfs_error("Failed to create directory '%s': %s.\n",
4253 makeme
, strerror(errno
));
4258 } while(tmp
!= dir
);
4263 static bool umount_if_mounted(void)
4265 if (umount2(BASEDIR
, MNT_DETACH
) < 0 && errno
!= EINVAL
) {
4266 lxcfs_error("Failed to unmount %s: %s.\n", BASEDIR
, strerror(errno
));
4272 /* __typeof__ should be safe to use with all compilers. */
4273 typedef __typeof__(((struct statfs
*)NULL
)->f_type
) fs_type_magic
;
4274 static bool has_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
4276 return (fs
->f_type
== (fs_type_magic
)magic_val
);
4280 * looking at fs/proc_namespace.c, it appears we can
4281 * actually expect the rootfs entry to very specifically contain
4282 * " - rootfs rootfs "
4283 * IIUC, so long as we've chrooted so that rootfs is not our root,
4284 * the rootfs entry should always be skipped in mountinfo contents.
4286 static bool is_on_ramfs(void)
4294 f
= fopen("/proc/self/mountinfo", "r");
4298 while (getline(&line
, &len
, f
) != -1) {
4299 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
4300 p
= strchr(p
+ 1, ' ');
4303 p2
= strchr(p
+ 1, ' ');
4307 if (strcmp(p
+ 1, "/") == 0) {
4308 // this is '/'. is it the ramfs?
4309 p
= strchr(p2
+ 1, '-');
4310 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
4322 static int pivot_enter()
4324 int ret
= -1, oldroot
= -1, newroot
= -1;
4326 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
);
4328 lxcfs_error("%s\n", "Failed to open old root for fchdir.");
4332 newroot
= open(ROOTDIR
, O_DIRECTORY
| O_RDONLY
);
4334 lxcfs_error("%s\n", "Failed to open new root for fchdir.");
4338 /* change into new root fs */
4339 if (fchdir(newroot
) < 0) {
4340 lxcfs_error("Failed to change directory to new rootfs: %s.\n", ROOTDIR
);
4344 /* pivot_root into our new root fs */
4345 if (pivot_root(".", ".") < 0) {
4346 lxcfs_error("pivot_root() syscall failed: %s.\n", strerror(errno
));
4351 * At this point the old-root is mounted on top of our new-root.
4352 * To unmounted it we must not be chdir'd into it, so escape back
4355 if (fchdir(oldroot
) < 0) {
4356 lxcfs_error("%s\n", "Failed to enter old root.");
4360 if (umount2(".", MNT_DETACH
) < 0) {
4361 lxcfs_error("%s\n", "Failed to detach old root.");
4365 if (fchdir(newroot
) < 0) {
4366 lxcfs_error("%s\n", "Failed to re-enter new root.");
4381 static int chroot_enter()
4383 if (mount(ROOTDIR
, "/", NULL
, MS_REC
| MS_BIND
, NULL
)) {
4384 lxcfs_error("Failed to recursively bind-mount %s into /.", ROOTDIR
);
4388 if (chroot(".") < 0) {
4389 lxcfs_error("Call to chroot() failed: %s.\n", strerror(errno
));
4393 if (chdir("/") < 0) {
4394 lxcfs_error("Failed to change directory: %s.\n", strerror(errno
));
4401 static int permute_and_enter(void)
4405 if (statfs("/", &sb
) < 0) {
4406 lxcfs_error("%s\n", "Could not stat / mountpoint.");
4410 /* has_fs_type() is not reliable. When the ramfs is a tmpfs it will
4411 * likely report TMPFS_MAGIC. Hence, when it reports no we still check
4412 * /proc/1/mountinfo. */
4413 if (has_fs_type(&sb
, RAMFS_MAGIC
) || is_on_ramfs())
4414 return chroot_enter();
4416 if (pivot_enter() < 0) {
4417 lxcfs_error("%s\n", "Could not perform pivot root.");
4424 /* Prepare our new clean root. */
4425 static int permute_prepare(void)
4427 if (mkdir(ROOTDIR
, 0700) < 0 && errno
!= EEXIST
) {
4428 lxcfs_error("%s\n", "Failed to create directory for new root.");
4432 if (mount("/", ROOTDIR
, NULL
, MS_BIND
, 0) < 0) {
4433 lxcfs_error("Failed to bind-mount / for new root: %s.\n", strerror(errno
));
4437 if (mount(RUNTIME_PATH
, ROOTDIR RUNTIME_PATH
, NULL
, MS_BIND
, 0) < 0) {
4438 lxcfs_error("Failed to bind-mount /run into new root: %s.\n", strerror(errno
));
4442 if (mount(BASEDIR
, ROOTDIR BASEDIR
, NULL
, MS_REC
| MS_MOVE
, 0) < 0) {
4443 printf("Failed to move " BASEDIR
" into new root: %s.\n", strerror(errno
));
4450 /* Calls chroot() on ramfs, pivot_root() in all other cases. */
4451 static bool permute_root(void)
4453 /* Prepare new root. */
4454 if (permute_prepare() < 0)
4457 /* Pivot into new root. */
4458 if (permute_and_enter() < 0)
4464 static int preserve_mnt_ns(int pid
)
4467 size_t len
= sizeof("/proc/") + 21 + sizeof("/ns/mnt");
4470 ret
= snprintf(path
, len
, "/proc/%d/ns/mnt", pid
);
4471 if (ret
< 0 || (size_t)ret
>= len
)
4474 return open(path
, O_RDONLY
| O_CLOEXEC
);
4477 static bool cgfs_prepare_mounts(void)
4479 if (!mkdir_p(BASEDIR
, 0700)) {
4480 lxcfs_error("%s\n", "Failed to create lxcfs cgroup mountpoint.");
4484 if (!umount_if_mounted()) {
4485 lxcfs_error("%s\n", "Failed to clean up old lxcfs cgroup mountpoint.");
4489 if (unshare(CLONE_NEWNS
) < 0) {
4490 lxcfs_error("Failed to unshare mount namespace: %s.\n", strerror(errno
));
4494 cgroup_mount_ns_fd
= preserve_mnt_ns(getpid());
4495 if (cgroup_mount_ns_fd
< 0) {
4496 lxcfs_error("Failed to preserve mount namespace: %s.\n", strerror(errno
));
4500 if (mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0) < 0) {
4501 lxcfs_error("Failed to remount / private: %s.\n", strerror(errno
));
4505 if (mount("tmpfs", BASEDIR
, "tmpfs", 0, "size=100000,mode=700") < 0) {
4506 lxcfs_error("%s\n", "Failed to mount tmpfs over lxcfs cgroup mountpoint.");
4513 static bool cgfs_mount_hierarchies(void)
4519 for (i
= 0; i
< num_hierarchies
; i
++) {
4520 char *controller
= hierarchies
[i
];
4522 clen
= strlen(controller
);
4523 len
= strlen(BASEDIR
) + clen
+ 2;
4524 target
= malloc(len
);
4528 ret
= snprintf(target
, len
, "%s/%s", BASEDIR
, controller
);
4529 if (ret
< 0 || ret
>= len
) {
4533 if (mkdir(target
, 0755) < 0 && errno
!= EEXIST
) {
4537 if (!strcmp(controller
, "unified"))
4538 ret
= mount("none", target
, "cgroup2", 0, NULL
);
4540 ret
= mount(controller
, target
, "cgroup", 0, controller
);
4542 lxcfs_error("Failed mounting cgroup %s: %s\n", controller
, strerror(errno
));
4547 fd_hierarchies
[i
] = open(target
, O_DIRECTORY
);
4548 if (fd_hierarchies
[i
] < 0) {
4557 static bool cgfs_setup_controllers(void)
4559 if (!cgfs_prepare_mounts())
4562 if (!cgfs_mount_hierarchies()) {
4563 lxcfs_error("%s\n", "Failed to set up private lxcfs cgroup mounts.");
4567 if (!permute_root())
4573 static void __attribute__((constructor
)) collect_and_mount_subsystems(void)
4576 char *cret
, *line
= NULL
;
4577 char cwd
[MAXPATHLEN
];
4579 int i
, init_ns
= -1;
4580 bool found_unified
= false;
4582 if ((f
= fopen("/proc/self/cgroup", "r")) == NULL
) {
4583 lxcfs_error("Error opening /proc/self/cgroup: %s\n", strerror(errno
));
4587 while (getline(&line
, &len
, f
) != -1) {
4590 p
= strchr(line
, ':');
4596 p2
= strrchr(p
, ':');
4601 /* With cgroupv2 /proc/self/cgroup can contain entries of the
4602 * form: 0::/ This will cause lxcfs to fail the cgroup mounts
4603 * because it parses out the empty string "" and later on passes
4604 * it to mount(). Let's skip such entries.
4606 if (!strcmp(p
, "") && !strcmp(idx
, "0") && !found_unified
) {
4607 found_unified
= true;
4611 if (!store_hierarchy(line
, p
))
4615 /* Preserve initial namespace. */
4616 init_ns
= preserve_mnt_ns(getpid());
4618 lxcfs_error("%s\n", "Failed to preserve initial mount namespace.");
4622 fd_hierarchies
= malloc(sizeof(int) * num_hierarchies
);
4623 if (!fd_hierarchies
) {
4624 lxcfs_error("%s\n", strerror(errno
));
4628 for (i
= 0; i
< num_hierarchies
; i
++)
4629 fd_hierarchies
[i
] = -1;
4631 cret
= getcwd(cwd
, MAXPATHLEN
);
4633 lxcfs_debug("Could not retrieve current working directory: %s.\n", strerror(errno
));
4635 /* This function calls unshare(CLONE_NEWNS) our initial mount namespace
4636 * to privately mount lxcfs cgroups. */
4637 if (!cgfs_setup_controllers()) {
4638 lxcfs_error("%s\n", "Failed to setup private cgroup mounts for lxcfs.");
4642 if (setns(init_ns
, 0) < 0) {
4643 lxcfs_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno
));
4647 if (!cret
|| chdir(cwd
) < 0)
4648 lxcfs_debug("Could not change back to original working directory: %s.\n", strerror(errno
));
4659 static void __attribute__((destructor
)) free_subsystems(void)
4663 lxcfs_debug("%s\n", "Running destructor for liblxcfs.");
4665 for (i
= 0; i
< num_hierarchies
; i
++) {
4667 free(hierarchies
[i
]);
4668 if (fd_hierarchies
&& fd_hierarchies
[i
] >= 0)
4669 close(fd_hierarchies
[i
]);
4672 free(fd_hierarchies
);
4674 if (cgroup_mount_ns_fd
>= 0)
4675 close(cgroup_mount_ns_fd
);