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
24 #include <linux/sched.h>
25 #include <sys/param.h>
26 #include <sys/socket.h>
27 #include <sys/mount.h>
28 #include <sys/epoll.h>
33 #include "config.h" // for VERSION
38 LXC_TYPE_PROC_MEMINFO
,
39 LXC_TYPE_PROC_CPUINFO
,
42 LXC_TYPE_PROC_DISKSTATS
,
51 char *buf
; // unused as of yet
53 int size
; //actual data size
57 /* reserve buffer size, for cpuall in /proc/stat */
58 #define BUF_RESERVE_SIZE 256
61 * A table caching which pid is init for a pid namespace.
62 * When looking up which pid is init for $qpid, we first
63 * 1. Stat /proc/$qpid/ns/pid.
64 * 2. Check whether the ino_t is in our store.
65 * a. if not, fork a child in qpid's ns to send us
66 * ucred.pid = 1, and read the initpid. Cache
67 * initpid and creation time for /proc/initpid
68 * in a new store entry.
69 * b. if so, verify that /proc/initpid still matches
70 * what we have saved. If not, clear the store
71 * entry and go back to a. If so, return the
74 struct pidns_init_store
{
75 ino_t ino
; // inode number for /proc/$pid/ns/pid
76 pid_t initpid
; // the pid of nit in that ns
77 long int ctime
; // the time at which /proc/$initpid was created
78 struct pidns_init_store
*next
;
82 /* lol - look at how they are allocated in the kernel */
83 #define PIDNS_HASH_SIZE 4096
84 #define HASH(x) ((x) % PIDNS_HASH_SIZE)
86 static struct pidns_init_store
*pidns_hash_table
[PIDNS_HASH_SIZE
];
87 static pthread_mutex_t pidns_store_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 static void lock_mutex(pthread_mutex_t
*l
)
92 if ((ret
= pthread_mutex_lock(l
)) != 0) {
93 fprintf(stderr
, "pthread_mutex_lock returned:%d %s\n", ret
, strerror(ret
));
98 static void unlock_mutex(pthread_mutex_t
*l
)
102 if ((ret
= pthread_mutex_unlock(l
)) != 0) {
103 fprintf(stderr
, "pthread_mutex_unlock returned:%d %s\n", ret
, strerror(ret
));
108 static void store_lock(void)
110 lock_mutex(&pidns_store_mutex
);
113 static void store_unlock(void)
115 unlock_mutex(&pidns_store_mutex
);
118 /* Must be called under store_lock */
119 static bool initpid_still_valid(struct pidns_init_store
*e
, struct stat
*nsfdsb
)
124 snprintf(fnam
, 100, "/proc/%d", e
->initpid
);
125 if (stat(fnam
, &initsb
) < 0)
128 fprintf(stderr
, "comparing ctime %ld %ld for pid %d\n",
129 e
->ctime
, initsb
.st_ctime
, e
->initpid
);
131 if (e
->ctime
!= initsb
.st_ctime
)
136 /* Must be called under store_lock */
137 static void remove_initpid(struct pidns_init_store
*e
)
139 struct pidns_init_store
*tmp
;
143 fprintf(stderr
, "remove_initpid: removing entry for %d\n", e
->initpid
);
146 if (pidns_hash_table
[h
] == e
) {
147 pidns_hash_table
[h
] = e
->next
;
152 tmp
= pidns_hash_table
[h
];
154 if (tmp
->next
== e
) {
164 /* Must be called under store_lock */
165 static void prune_initpid_store(void)
167 static long int last_prune
= 0;
168 struct pidns_init_store
*e
, *prev
, *delme
;
169 long int now
, threshold
;
173 last_prune
= time(NULL
);
177 if (now
< last_prune
+ PURGE_SECS
)
180 fprintf(stderr
, "pruning\n");
183 threshold
= now
- 2 * PURGE_SECS
;
185 for (i
= 0; i
< PIDNS_HASH_SIZE
; i
++) {
186 for (prev
= NULL
, e
= pidns_hash_table
[i
]; e
; ) {
187 if (e
->lastcheck
< threshold
) {
189 fprintf(stderr
, "Removing cached entry for %d\n", e
->initpid
);
193 prev
->next
= e
->next
;
195 pidns_hash_table
[i
] = e
->next
;
206 /* Must be called under store_lock */
207 static void save_initpid(struct stat
*sb
, pid_t pid
)
209 struct pidns_init_store
*e
;
215 fprintf(stderr
, "save_initpid: adding entry for %d\n", pid
);
217 snprintf(fpath
, 100, "/proc/%d", pid
);
218 if (stat(fpath
, &procsb
) < 0)
221 e
= malloc(sizeof(*e
));
225 e
->ctime
= procsb
.st_ctime
;
227 e
->next
= pidns_hash_table
[h
];
228 e
->lastcheck
= time(NULL
);
229 pidns_hash_table
[h
] = e
;
233 * Given the stat(2) info for a nsfd pid inode, lookup the init_pid_store
234 * entry for the inode number and creation time. Verify that the init pid
235 * is still valid. If not, remove it. Return the entry if valid, NULL
237 * Must be called under store_lock
239 static struct pidns_init_store
*lookup_verify_initpid(struct stat
*sb
)
241 int h
= HASH(sb
->st_ino
);
242 struct pidns_init_store
*e
= pidns_hash_table
[h
];
245 if (e
->ino
== sb
->st_ino
) {
246 if (initpid_still_valid(e
, sb
)) {
247 e
->lastcheck
= time(NULL
);
259 static int is_dir(const char *path
, int fd
)
262 int ret
= fstatat(fd
, path
, &statbuf
, fd
);
263 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
268 static char *must_copy_string(const char *str
)
280 static inline void drop_trailing_newlines(char *s
)
284 for (l
=strlen(s
); l
>0 && s
[l
-1] == '\n'; l
--)
288 #define BATCH_SIZE 50
289 static void dorealloc(char **mem
, size_t oldlen
, size_t newlen
)
291 int newbatches
= (newlen
/ BATCH_SIZE
) + 1;
292 int oldbatches
= (oldlen
/ BATCH_SIZE
) + 1;
294 if (!*mem
|| newbatches
> oldbatches
) {
297 tmp
= realloc(*mem
, newbatches
* BATCH_SIZE
);
302 static void append_line(char **contents
, size_t *len
, char *line
, ssize_t linelen
)
304 size_t newlen
= *len
+ linelen
;
305 dorealloc(contents
, *len
, newlen
+ 1);
306 memcpy(*contents
+ *len
, line
, linelen
+1);
310 static char *slurp_file(const char *from
, int fd
)
313 char *contents
= NULL
;
314 FILE *f
= fdopen(fd
, "r");
315 size_t len
= 0, fulllen
= 0;
321 while ((linelen
= getline(&line
, &len
, f
)) != -1) {
322 append_line(&contents
, &fulllen
, line
, linelen
);
327 drop_trailing_newlines(contents
);
332 static bool write_string(const char *fnam
, const char *string
, int fd
)
337 if (!(f
= fdopen(fd
, "w")))
339 len
= strlen(string
);
340 ret
= fwrite(string
, 1, len
, f
);
342 fprintf(stderr
, "Error writing to file: %s\n", strerror(errno
));
347 fprintf(stderr
, "Error writing to file: %s\n", strerror(errno
));
360 static bool store_hierarchy(char *stridx
, char *h
)
362 if (num_hierarchies
% ALLOC_NUM
== 0) {
363 size_t n
= (num_hierarchies
/ ALLOC_NUM
) + 1;
365 char **tmp
= realloc(hierarchies
, n
* sizeof(char *));
367 fprintf(stderr
, "Out of memory\n");
373 hierarchies
[num_hierarchies
++] = must_copy_string(h
);
377 static void print_subsystems(void)
381 fprintf(stderr
, "hierarchies:\n");
382 for (i
= 0; i
< num_hierarchies
; i
++) {
384 fprintf(stderr
, " %d: %s\n", i
, hierarchies
[i
]);
388 static bool in_comma_list(const char *needle
, const char *haystack
)
390 const char *s
= haystack
, *e
;
391 size_t nlen
= strlen(needle
);
393 while (*s
&& (e
= index(s
, ','))) {
398 if (strncmp(needle
, s
, nlen
) == 0)
402 if (strcmp(needle
, s
) == 0)
407 /* do we need to do any massaging here? I'm not sure... */
408 /* Return the mounted controller and store the corresponding open file descriptor
409 * referring to the controller mountpoint in the private lxcfs namespace in
412 static char *find_mounted_controller(const char *controller
, int *cfd
)
416 for (i
= 0; i
< num_hierarchies
; i
++) {
419 if (strcmp(hierarchies
[i
], controller
) == 0) {
420 *cfd
= fd_hierarchies
[i
];
421 return hierarchies
[i
];
423 if (in_comma_list(controller
, hierarchies
[i
])) {
424 *cfd
= fd_hierarchies
[i
];
425 return hierarchies
[i
];
432 bool cgfs_set_value(const char *controller
, const char *cgroup
, const char *file
,
437 char *fnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
441 /* . + /cgroup + / + file + \0 */
442 len
= strlen(cgroup
) + strlen(file
) + 3;
444 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
445 if (ret
< 0 || (size_t)ret
>= len
)
448 fd
= openat(cfd
, fnam
, O_WRONLY
);
452 return write_string(fnam
, value
, fd
);
455 // Chown all the files in the cgroup directory. We do this when we create
456 // a cgroup on behalf of a user.
457 static void chown_all_cgroup_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
459 struct dirent
*direntp
;
460 char path
[MAXPATHLEN
];
465 len
= strlen(dirname
);
466 if (len
>= MAXPATHLEN
) {
467 fprintf(stderr
, "chown_all_cgroup_files: pathname too long: %s\n", dirname
);
471 fd1
= openat(fd
, dirname
, O_DIRECTORY
);
477 fprintf(stderr
, "chown_all_cgroup_files: failed to open %s\n", dirname
);
481 while ((direntp
= readdir(d
))) {
482 if (!strcmp(direntp
->d_name
, ".") || !strcmp(direntp
->d_name
, ".."))
484 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
485 if (ret
< 0 || ret
>= MAXPATHLEN
) {
486 fprintf(stderr
, "chown_all_cgroup_files: pathname too long under %s\n", dirname
);
489 if (fchownat(fd
, path
, uid
, gid
, 0) < 0)
490 fprintf(stderr
, "Failed to chown file %s to %u:%u", path
, uid
, gid
);
495 int cgfs_create(const char *controller
, const char *cg
, uid_t uid
, gid_t gid
)
499 char *dirnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
504 len
= strlen(cg
) + 2;
505 dirnam
= alloca(len
);
506 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
508 if (mkdirat(cfd
, dirnam
, 0755) < 0)
511 if (uid
== 0 && gid
== 0)
514 if (fchownat(cfd
, dirnam
, uid
, gid
, 0) < 0)
517 chown_all_cgroup_files(dirnam
, uid
, gid
, cfd
);
522 static bool recursive_rmdir(const char *dirname
, int fd
)
524 struct dirent
*direntp
;
527 char pathname
[MAXPATHLEN
];
530 dupfd
= dup(fd
); // fdopendir() does bad things once it uses an fd.
534 dir
= fdopendir(dupfd
);
537 fprintf(stderr
, "%s: failed to open %s: %s\n", __func__
, dirname
, strerror(errno
));
542 while ((direntp
= readdir(dir
))) {
549 if (!strcmp(direntp
->d_name
, ".") ||
550 !strcmp(direntp
->d_name
, ".."))
553 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
554 if (rc
< 0 || rc
>= MAXPATHLEN
) {
555 fprintf(stderr
, "pathname too long\n");
559 ret
= fstatat(fd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
562 fprintf(stderr
, "%s: failed to stat %s: %s\n", __func__
, pathname
, strerror(errno
));
566 if (S_ISDIR(mystat
.st_mode
)) {
567 if (!recursive_rmdir(pathname
, fd
)) {
569 fprintf(stderr
, "Error removing %s\n", pathname
);
576 if (closedir(dir
) < 0) {
577 fprintf(stderr
, "%s: failed to close directory %s: %s\n", __func__
, dirname
, strerror(errno
));
581 if (unlinkat(fd
, dirname
, AT_REMOVEDIR
) < 0) {
583 fprintf(stderr
, "%s: failed to delete %s: %s\n", __func__
, dirname
, strerror(errno
));
592 bool cgfs_remove(const char *controller
, const char *cg
)
596 char *dirnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
601 len
= strlen(cg
) + 2;
602 dirnam
= alloca(len
);
603 snprintf(dirnam
, len
, "%s%s", *cg
== '/' ? "." : "", cg
);
605 fd
= openat(cfd
, dirnam
, O_DIRECTORY
);
609 return recursive_rmdir(dirnam
, fd
);
612 bool cgfs_chmod_file(const char *controller
, const char *file
, mode_t mode
)
616 char *pathname
, *tmpc
= find_mounted_controller(controller
, &cfd
);
621 len
= strlen(file
) + 2;
622 pathname
= alloca(len
);
623 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
624 if (fchmodat(cfd
, pathname
, mode
, 0) < 0)
629 static int chown_tasks_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
634 len
= strlen(dirname
) + strlen("/cgroup.procs") + 1;
636 snprintf(fname
, len
, "%s/tasks", dirname
);
637 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
639 snprintf(fname
, len
, "%s/cgroup.procs", dirname
);
640 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
645 int cgfs_chown_file(const char *controller
, const char *file
, uid_t uid
, gid_t gid
)
649 char *pathname
, *tmpc
= find_mounted_controller(controller
, &cfd
);
654 len
= strlen(file
) + 2;
655 pathname
= alloca(len
);
656 snprintf(pathname
, len
, "%s%s", *file
== '/' ? "." : "", file
);
657 if (fchownat(cfd
, pathname
, uid
, gid
, 0) < 0)
660 if (is_dir(pathname
, cfd
))
661 // like cgmanager did, we want to chown the tasks file as well
662 return chown_tasks_files(pathname
, uid
, gid
, cfd
);
667 FILE *open_pids_file(const char *controller
, const char *cgroup
)
671 char *pathname
, *tmpc
= find_mounted_controller(controller
, &cfd
);
675 /* BASEDIR / tmpc / cgroup / "cgroup.procs" \0 */
676 len
= strlen(BASEDIR
) + strlen(tmpc
) + strlen(cgroup
) + 4 + strlen("cgroup.procs");
677 pathname
= alloca(len
);
678 snprintf(pathname
, len
, "%s/%s/%s/cgroup.procs", BASEDIR
, tmpc
, cgroup
);
679 return fopen(pathname
, "w");
682 static bool cgfs_iterate_cgroup(const char *controller
, const char *cgroup
, bool directories
,
683 void ***list
, size_t typesize
,
684 void* (*iterator
)(const char*, const char*, const char*))
689 char pathname
[MAXPATHLEN
];
690 size_t sz
= 0, asz
= 0;
691 struct dirent
*dirent
;
694 tmpc
= find_mounted_controller(controller
, &cfd
);
699 /* Make sure we pass a relative path to openat(). */
700 len
= strlen(cgroup
) + 1 /* . */ + 1 /* \0 */;
702 ret
= snprintf(cg
, len
, "%s%s", *cgroup
== '/' ? "." : "", cgroup
);
703 if (ret
< 0 || (size_t)ret
>= len
) {
704 fprintf(stderr
, "%s: pathname too long under %s\n", __func__
, cgroup
);
708 fd
= openat(cfd
, cg
, O_DIRECTORY
);
716 while ((dirent
= readdir(dir
))) {
719 if (!strcmp(dirent
->d_name
, ".") ||
720 !strcmp(dirent
->d_name
, ".."))
723 ret
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", cg
, dirent
->d_name
);
724 if (ret
< 0 || ret
>= MAXPATHLEN
) {
725 fprintf(stderr
, "%s: pathname too long under %s\n", __func__
, cg
);
729 ret
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
731 fprintf(stderr
, "%s: failed to stat %s: %s\n", __func__
, pathname
, strerror(errno
));
734 if ((!directories
&& !S_ISREG(mystat
.st_mode
)) ||
735 (directories
&& !S_ISDIR(mystat
.st_mode
)))
742 tmp
= realloc(*list
, asz
* typesize
);
746 (*list
)[sz
] = (*iterator
)(controller
, cg
, dirent
->d_name
);
747 (*list
)[sz
+1] = NULL
;
750 if (closedir(dir
) < 0) {
751 fprintf(stderr
, "%s: failed closedir for %s: %s\n", __func__
, cgroup
, strerror(errno
));
757 static void *make_children_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
761 dup
= strdup(dir_entry
);
766 bool cgfs_list_children(const char *controller
, const char *cgroup
, char ***list
)
768 return cgfs_iterate_cgroup(controller
, cgroup
, true, (void***)list
, sizeof(*list
), &make_children_list_entry
);
771 void free_key(struct cgfs_files
*k
)
779 void free_keys(struct cgfs_files
**keys
)
785 for (i
= 0; keys
[i
]; i
++) {
791 bool cgfs_get_value(const char *controller
, const char *cgroup
, const char *file
, char **value
)
795 char *fnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
799 /* . + /cgroup + / + file + \0 */
800 len
= strlen(cgroup
) + strlen(file
) + 3;
802 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, file
);
803 if (ret
< 0 || (size_t)ret
>= len
)
806 fd
= openat(cfd
, fnam
, O_RDONLY
);
810 *value
= slurp_file(fnam
, fd
);
811 return *value
!= NULL
;
814 struct cgfs_files
*cgfs_get_key(const char *controller
, const char *cgroup
, const char *file
)
818 char *fnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
820 struct cgfs_files
*newkey
;
825 if (file
&& *file
== '/')
828 if (file
&& index(file
, '/'))
831 /* . + /cgroup + / + file + \0 */
832 len
= strlen(cgroup
) + 3;
834 len
+= strlen(file
) + 1;
836 snprintf(fnam
, len
, "%s%s%s%s", *cgroup
== '/' ? "." : "", cgroup
,
837 file
? "/" : "", file
? file
: "");
839 ret
= fstatat(cfd
, fnam
, &sb
, 0);
844 newkey
= malloc(sizeof(struct cgfs_files
));
847 newkey
->name
= must_copy_string(file
);
848 else if (rindex(cgroup
, '/'))
849 newkey
->name
= must_copy_string(rindex(cgroup
, '/'));
851 newkey
->name
= must_copy_string(cgroup
);
852 newkey
->uid
= sb
.st_uid
;
853 newkey
->gid
= sb
.st_gid
;
854 newkey
->mode
= sb
.st_mode
;
859 static void *make_key_list_entry(const char *controller
, const char *cgroup
, const char *dir_entry
)
861 struct cgfs_files
*entry
= cgfs_get_key(controller
, cgroup
, dir_entry
);
863 fprintf(stderr
, "%s: Error getting files under %s:%s\n",
864 __func__
, controller
, cgroup
);
869 bool cgfs_list_keys(const char *controller
, const char *cgroup
, struct cgfs_files
***keys
)
871 return cgfs_iterate_cgroup(controller
, cgroup
, false, (void***)keys
, sizeof(*keys
), &make_key_list_entry
);
874 bool is_child_cgroup(const char *controller
, const char *cgroup
, const char *f
)
878 char *fnam
, *tmpc
= find_mounted_controller(controller
, &cfd
);
884 /* . + /cgroup + / + f + \0 */
885 len
= strlen(cgroup
) + strlen(f
) + 3;
887 ret
= snprintf(fnam
, len
, "%s%s/%s", *cgroup
== '/' ? "." : "", cgroup
, f
);
888 if (ret
< 0 || (size_t)ret
>= len
)
891 ret
= fstatat(cfd
, fnam
, &sb
, 0);
892 if (ret
< 0 || !S_ISDIR(sb
.st_mode
))
897 #define SEND_CREDS_OK 0
898 #define SEND_CREDS_NOTSK 1
899 #define SEND_CREDS_FAIL 2
900 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
);
901 static int wait_for_pid(pid_t pid
);
902 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
);
903 static int send_creds_clone_wrapper(void *arg
);
906 * clone a task which switches to @task's namespace and writes '1'.
907 * over a unix sock so we can read the task's reaper's pid in our
910 * Note: glibc's fork() does not respect pidns, which can lead to failed
911 * assertions inside glibc (and thus failed forks) if the child's pid in
912 * the pidns and the parent pid outside are identical. Using clone prevents
915 static void write_task_init_pid_exit(int sock
, pid_t target
)
920 size_t stack_size
= sysconf(_SC_PAGESIZE
);
921 void *stack
= alloca(stack_size
);
923 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", (int)target
);
924 if (ret
< 0 || ret
>= sizeof(fnam
))
927 fd
= open(fnam
, O_RDONLY
);
929 perror("write_task_init_pid_exit open of ns/pid");
933 perror("write_task_init_pid_exit setns 1");
937 pid
= clone(send_creds_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &sock
);
941 if (!wait_for_pid(pid
))
947 static int send_creds_clone_wrapper(void *arg
) {
950 int sock
= *(int *)arg
;
952 /* we are the child */
957 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
)
962 static pid_t
get_init_pid_for_task(pid_t task
)
970 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
971 perror("socketpair");
980 write_task_init_pid_exit(sock
[0], task
);
984 if (!recv_creds(sock
[1], &cred
, &v
))
996 static pid_t
lookup_initpid_in_store(pid_t qpid
)
1000 struct pidns_init_store
*e
;
1003 snprintf(fnam
, 100, "/proc/%d/ns/pid", qpid
);
1005 if (stat(fnam
, &sb
) < 0)
1007 e
= lookup_verify_initpid(&sb
);
1009 answer
= e
->initpid
;
1012 answer
= get_init_pid_for_task(qpid
);
1014 save_initpid(&sb
, answer
);
1017 /* we prune at end in case we are returning
1018 * the value we were about to return */
1019 prune_initpid_store();
1024 static int wait_for_pid(pid_t pid
)
1032 ret
= waitpid(pid
, &status
, 0);
1040 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
1047 * append pid to *src.
1048 * src: a pointer to a char* in which ot append the pid.
1049 * sz: the number of characters printed so far, minus trailing \0.
1050 * asz: the allocated size so far
1051 * pid: the pid to append
1053 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
1057 int tmplen
= sprintf(tmp
, "%d\n", (int)pid
);
1059 if (!*src
|| tmplen
+ *sz
+ 1 >= *asz
) {
1062 tmp
= realloc(*src
, *asz
+ BUF_RESERVE_SIZE
);
1065 *asz
+= BUF_RESERVE_SIZE
;
1067 memcpy((*src
) +*sz
, tmp
, tmplen
+1); /* include the \0 */
1072 * Given a open file * to /proc/pid/{u,g}id_map, and an id
1073 * valid in the caller's namespace, return the id mapped into
1075 * Returns the mapped id, or -1 on error.
1078 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
1080 unsigned int nsuid
, // base id for a range in the idfile's namespace
1081 hostuid
, // base id for a range in the caller's namespace
1082 count
; // number of ids in this range
1086 fseek(idfile
, 0L, SEEK_SET
);
1087 while (fgets(line
, 400, idfile
)) {
1088 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
1091 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
1093 * uids wrapped around - unexpected as this is a procfile,
1096 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s\n",
1097 nsuid
, hostuid
, count
, line
);
1100 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
1102 * now since hostuid <= in_id < hostuid+count, and
1103 * hostuid+count and nsuid+count do not wrap around,
1104 * we know that nsuid+(in_id-hostuid) which must be
1105 * less that nsuid+(count) must not wrap around
1107 return (in_id
- hostuid
) + nsuid
;
1116 * for is_privileged_over,
1117 * specify whether we require the calling uid to be root in his
1120 #define NS_ROOT_REQD true
1121 #define NS_ROOT_OPT false
1125 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
1127 char fpath
[PROCLEN
];
1129 bool answer
= false;
1132 if (victim
== -1 || uid
== -1)
1136 * If the request is one not requiring root in the namespace,
1137 * then having the same uid suffices. (i.e. uid 1000 has write
1138 * access to files owned by uid 1000
1140 if (!req_ns_root
&& uid
== victim
)
1143 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
1144 if (ret
< 0 || ret
>= PROCLEN
)
1146 FILE *f
= fopen(fpath
, "r");
1150 /* if caller's not root in his namespace, reject */
1151 nsuid
= convert_id_to_ns(f
, uid
);
1156 * If victim is not mapped into caller's ns, reject.
1157 * XXX I'm not sure this check is needed given that fuse
1158 * will be sending requests where the vfs has converted
1160 nsuid
= convert_id_to_ns(f
, victim
);
1171 static bool perms_include(int fmode
, mode_t req_mode
)
1175 switch (req_mode
& O_ACCMODE
) {
1183 r
= S_IROTH
| S_IWOTH
;
1188 return ((fmode
& r
) == r
);
1194 * querycg is /a/b/c/d/e
1197 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
1201 if (strlen(taskcg
) <= strlen(querycg
)) {
1202 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
1206 if (strcmp(querycg
, "/") == 0)
1207 start
= strdup(taskcg
+ 1);
1209 start
= strdup(taskcg
+ strlen(querycg
) + 1);
1212 end
= strchr(start
, '/');
1218 static void stripnewline(char *x
)
1220 size_t l
= strlen(x
);
1221 if (l
&& x
[l
-1] == '\n')
1225 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1230 char *answer
= NULL
;
1234 const char *h
= find_mounted_controller(contrl
, &cfd
);
1238 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
1239 if (ret
< 0 || ret
>= PROCLEN
)
1241 if (!(f
= fopen(fnam
, "r")))
1244 while (getline(&line
, &len
, f
) != -1) {
1248 c1
= strchr(line
, ':');
1252 c2
= strchr(c1
, ':');
1256 if (strcmp(c1
, h
) != 0)
1261 answer
= strdup(c2
);
1273 * check whether a fuse context may access a cgroup dir or file
1275 * If file is not null, it is a cgroup file to check under cg.
1276 * If file is null, then we are checking perms on cg itself.
1278 * For files we can check the mode of the list_keys result.
1279 * For cgroups, we must make assumptions based on the files under the
1280 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
1283 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
1285 struct cgfs_files
*k
= NULL
;
1288 k
= cgfs_get_key(contrl
, cg
, file
);
1292 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1293 if (perms_include(k
->mode
>> 6, mode
)) {
1298 if (fc
->gid
== k
->gid
) {
1299 if (perms_include(k
->mode
>> 3, mode
)) {
1304 ret
= perms_include(k
->mode
, mode
);
1311 #define INITSCOPE "/init.scope"
1312 static void prune_init_slice(char *cg
)
1315 size_t cg_len
= strlen(cg
), initscope_len
= strlen(INITSCOPE
);
1317 if (cg_len
< initscope_len
)
1320 point
= cg
+ cg_len
- initscope_len
;
1321 if (strcmp(point
, INITSCOPE
) == 0) {
1330 * If pid is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
1331 * If pid is in /a, he may act on /a/b, but not on /b.
1332 * if the answer is false and nextcg is not NULL, then *nextcg will point
1333 * to a string containing the next cgroup directory under cg, which must be
1334 * freed by the caller.
1336 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
1338 bool answer
= false;
1339 char *c2
= get_pid_cgroup(pid
, contrl
);
1344 prune_init_slice(c2
);
1347 * callers pass in '/' or './' (openat()) for root cgroup, otherwise
1348 * they pass in a cgroup without leading '/'
1350 * The original line here was:
1351 * linecmp = *cg == '/' ? c2 : c2+1;
1352 * TODO: I'm not sure why you'd want to increment when *cg != '/'?
1353 * Serge, do you know?
1355 if (*cg
== '/' || !strncmp(cg
, "./", 2))
1359 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
1361 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
1373 * If pid is in /a/b/c, he may see that /a exists, but not /b or /a/c.
1375 static bool caller_may_see_dir(pid_t pid
, const char *contrl
, const char *cg
)
1377 bool answer
= false;
1379 size_t target_len
, task_len
;
1381 if (strcmp(cg
, "/") == 0 || strcmp(cg
, "./") == 0)
1384 c2
= get_pid_cgroup(pid
, contrl
);
1387 prune_init_slice(c2
);
1390 target_len
= strlen(cg
);
1391 task_len
= strlen(task_cg
);
1392 if (task_len
== 0) {
1393 /* Task is in the root cg, it can see everything. This case is
1394 * not handled by the strmcps below, since they test for the
1395 * last /, but that is the first / that we've chopped off
1401 if (strcmp(cg
, task_cg
) == 0) {
1405 if (target_len
< task_len
) {
1406 /* looking up a parent dir */
1407 if (strncmp(task_cg
, cg
, target_len
) == 0 && task_cg
[target_len
] == '/')
1411 if (target_len
> task_len
) {
1412 /* looking up a child dir */
1413 if (strncmp(task_cg
, cg
, task_len
) == 0 && cg
[task_len
] == '/')
1424 * given /cgroup/freezer/a/b, return "freezer".
1425 * the returned char* should NOT be freed.
1427 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
1430 char *contr
, *slash
;
1432 if (strlen(path
) < 9)
1434 if (*(path
+7) != '/')
1437 contr
= strdupa(p1
);
1440 slash
= strstr(contr
, "/");
1445 for (i
= 0; i
< num_hierarchies
; i
++) {
1446 if (hierarchies
[i
] && strcmp(hierarchies
[i
], contr
) == 0)
1447 return hierarchies
[i
];
1453 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
1454 * Note that the returned value may include files (keynames) etc
1456 static const char *find_cgroup_in_path(const char *path
)
1460 if (strlen(path
) < 9)
1462 p1
= strstr(path
+8, "/");
1469 * split the last path element from the path in @cg.
1470 * @dir is newly allocated and should be freed, @last not
1472 static void get_cgdir_and_path(const char *cg
, char **dir
, char **last
)
1479 *last
= strrchr(cg
, '/');
1484 p
= strrchr(*dir
, '/');
1489 * FUSE ops for /cgroup
1492 int cg_getattr(const char *path
, struct stat
*sb
)
1494 struct timespec now
;
1495 struct fuse_context
*fc
= fuse_get_context();
1496 char * cgdir
= NULL
;
1497 char *last
= NULL
, *path1
, *path2
;
1498 struct cgfs_files
*k
= NULL
;
1500 const char *controller
= NULL
;
1507 memset(sb
, 0, sizeof(struct stat
));
1509 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1512 sb
->st_uid
= sb
->st_gid
= 0;
1513 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1516 if (strcmp(path
, "/cgroup") == 0) {
1517 sb
->st_mode
= S_IFDIR
| 00755;
1522 controller
= pick_controller_from_path(fc
, path
);
1525 cgroup
= find_cgroup_in_path(path
);
1527 /* this is just /cgroup/controller, return it as a dir */
1528 sb
->st_mode
= S_IFDIR
| 00755;
1533 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1543 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1546 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
1547 * Then check that caller's cgroup is under path if last is a child
1548 * cgroup, or cgdir if last is a file */
1550 if (is_child_cgroup(controller
, path1
, path2
)) {
1551 if (!caller_may_see_dir(initpid
, controller
, cgroup
)) {
1555 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
1556 /* this is just /cgroup/controller, return it as a dir */
1557 sb
->st_mode
= S_IFDIR
| 00555;
1562 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
1567 // get uid, gid, from '/tasks' file and make up a mode
1568 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1569 sb
->st_mode
= S_IFDIR
| 00755;
1570 k
= cgfs_get_key(controller
, cgroup
, NULL
);
1572 sb
->st_uid
= sb
->st_gid
= 0;
1574 sb
->st_uid
= k
->uid
;
1575 sb
->st_gid
= k
->gid
;
1583 if ((k
= cgfs_get_key(controller
, path1
, path2
)) != NULL
) {
1584 sb
->st_mode
= S_IFREG
| k
->mode
;
1586 sb
->st_uid
= k
->uid
;
1587 sb
->st_gid
= k
->gid
;
1590 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
1594 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
)) {
1607 int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
1609 struct fuse_context
*fc
= fuse_get_context();
1611 struct file_info
*dir_info
;
1612 char *controller
= NULL
;
1617 if (strcmp(path
, "/cgroup") == 0) {
1621 // return list of keys for the controller, and list of child cgroups
1622 controller
= pick_controller_from_path(fc
, path
);
1626 cgroup
= find_cgroup_in_path(path
);
1628 /* this is just /cgroup/controller, return its contents */
1633 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1637 if (!caller_may_see_dir(initpid
, controller
, cgroup
))
1639 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
1643 /* we'll free this at cg_releasedir */
1644 dir_info
= malloc(sizeof(*dir_info
));
1647 dir_info
->controller
= must_copy_string(controller
);
1648 dir_info
->cgroup
= must_copy_string(cgroup
);
1649 dir_info
->type
= LXC_TYPE_CGDIR
;
1650 dir_info
->buf
= NULL
;
1651 dir_info
->file
= NULL
;
1652 dir_info
->buflen
= 0;
1654 fi
->fh
= (unsigned long)dir_info
;
1658 int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1659 struct fuse_file_info
*fi
)
1661 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1662 struct cgfs_files
**list
= NULL
;
1664 char *nextcg
= NULL
;
1665 struct fuse_context
*fc
= fuse_get_context();
1666 char **clist
= NULL
;
1668 if (d
->type
!= LXC_TYPE_CGDIR
) {
1669 fprintf(stderr
, "Internal error: file cache info used in readdir\n");
1672 if (!d
->cgroup
&& !d
->controller
) {
1673 // ls /var/lib/lxcfs/cgroup - just show list of controllers
1676 for (i
= 0; i
< num_hierarchies
; i
++) {
1677 if (hierarchies
[i
] && filler(buf
, hierarchies
[i
], NULL
, 0) != 0) {
1684 if (!cgfs_list_keys(d
->controller
, d
->cgroup
, &list
)) {
1685 // not a valid cgroup
1690 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1693 if (!caller_is_in_ancestor(initpid
, d
->controller
, d
->cgroup
, &nextcg
)) {
1695 ret
= filler(buf
, nextcg
, NULL
, 0);
1706 for (i
= 0; list
[i
]; i
++) {
1707 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
1713 // now get the list of child cgroups
1715 if (!cgfs_list_children(d
->controller
, d
->cgroup
, &clist
)) {
1720 for (i
= 0; clist
[i
]; i
++) {
1721 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
1732 for (i
= 0; clist
[i
]; i
++)
1739 static void do_release_file_info(struct fuse_file_info
*fi
)
1741 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1748 free(f
->controller
);
1749 f
->controller
= NULL
;
1759 int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
1761 do_release_file_info(fi
);
1765 int cg_open(const char *path
, struct fuse_file_info
*fi
)
1768 char *last
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
1769 struct cgfs_files
*k
= NULL
;
1770 struct file_info
*file_info
;
1771 struct fuse_context
*fc
= fuse_get_context();
1777 controller
= pick_controller_from_path(fc
, path
);
1780 cgroup
= find_cgroup_in_path(path
);
1784 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1793 k
= cgfs_get_key(controller
, path1
, path2
);
1800 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1803 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
1807 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
1812 /* we'll free this at cg_release */
1813 file_info
= malloc(sizeof(*file_info
));
1818 file_info
->controller
= must_copy_string(controller
);
1819 file_info
->cgroup
= must_copy_string(path1
);
1820 file_info
->file
= must_copy_string(path2
);
1821 file_info
->type
= LXC_TYPE_CGFILE
;
1822 file_info
->buf
= NULL
;
1823 file_info
->buflen
= 0;
1825 fi
->fh
= (unsigned long)file_info
;
1833 int cg_access(const char *path
, int mode
)
1836 char *last
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
1837 struct cgfs_files
*k
= NULL
;
1838 struct fuse_context
*fc
= fuse_get_context();
1844 controller
= pick_controller_from_path(fc
, path
);
1847 cgroup
= find_cgroup_in_path(path
);
1849 // access("/sys/fs/cgroup/systemd", mode) - rx allowed, w not
1850 if ((mode
& W_OK
) == 0)
1855 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1864 k
= cgfs_get_key(controller
, path1
, path2
);
1866 if ((mode
& W_OK
) == 0)
1874 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1877 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
1881 if (!fc_may_access(fc
, controller
, path1
, path2
, mode
)) {
1893 int cg_release(const char *path
, struct fuse_file_info
*fi
)
1895 do_release_file_info(fi
);
1899 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
1901 static bool wait_for_sock(int sock
, int timeout
)
1903 struct epoll_event ev
;
1904 int epfd
, ret
, now
, starttime
, deltatime
, saved_errno
;
1906 if ((starttime
= time(NULL
)) < 0)
1909 if ((epfd
= epoll_create(1)) < 0) {
1910 fprintf(stderr
, "Failed to create epoll socket: %m\n");
1914 ev
.events
= POLLIN_SET
;
1916 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, sock
, &ev
) < 0) {
1917 fprintf(stderr
, "Failed adding socket to epoll: %m\n");
1923 if ((now
= time(NULL
)) < 0) {
1928 deltatime
= (starttime
+ timeout
) - now
;
1929 if (deltatime
< 0) { // timeout
1934 ret
= epoll_wait(epfd
, &ev
, 1, 1000*deltatime
+ 1);
1935 if (ret
< 0 && errno
== EINTR
)
1937 saved_errno
= errno
;
1941 errno
= saved_errno
;
1947 static int msgrecv(int sockfd
, void *buf
, size_t len
)
1949 if (!wait_for_sock(sockfd
, 2))
1951 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
1954 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
1956 struct msghdr msg
= { 0 };
1958 struct cmsghdr
*cmsg
;
1959 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
1964 if (msgrecv(sock
, buf
, 1) != 1) {
1965 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
1967 return SEND_CREDS_FAIL
;
1971 msg
.msg_control
= cmsgbuf
;
1972 msg
.msg_controllen
= sizeof(cmsgbuf
);
1974 cmsg
= CMSG_FIRSTHDR(&msg
);
1975 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
1976 cmsg
->cmsg_level
= SOL_SOCKET
;
1977 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
1978 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
1980 msg
.msg_name
= NULL
;
1981 msg
.msg_namelen
= 0;
1985 iov
.iov_len
= sizeof(buf
);
1989 if (sendmsg(sock
, &msg
, 0) < 0) {
1990 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
1993 return SEND_CREDS_NOTSK
;
1994 return SEND_CREDS_FAIL
;
1997 return SEND_CREDS_OK
;
2000 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
2002 struct msghdr msg
= { 0 };
2004 struct cmsghdr
*cmsg
;
2005 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
2016 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
2017 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
2021 if (write(sock
, buf
, 1) != 1) {
2022 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
2026 msg
.msg_name
= NULL
;
2027 msg
.msg_namelen
= 0;
2028 msg
.msg_control
= cmsgbuf
;
2029 msg
.msg_controllen
= sizeof(cmsgbuf
);
2032 iov
.iov_len
= sizeof(buf
);
2036 if (!wait_for_sock(sock
, 2)) {
2037 fprintf(stderr
, "Timed out waiting for scm_cred: %s\n",
2041 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
2043 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
2048 cmsg
= CMSG_FIRSTHDR(&msg
);
2050 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
2051 cmsg
->cmsg_level
== SOL_SOCKET
&&
2052 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
2053 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
2060 struct pid_ns_clone_args
{
2064 int (*wrapped
) (int, pid_t
); // pid_from_ns or pid_to_ns
2068 * pid_ns_clone_wrapper - wraps pid_to_ns or pid_from_ns for usage
2069 * with clone(). This simply writes '1' as ACK back to the parent
2070 * before calling the actual wrapped function.
2072 static int pid_ns_clone_wrapper(void *arg
) {
2073 struct pid_ns_clone_args
* args
= (struct pid_ns_clone_args
*) arg
;
2076 close(args
->cpipe
[0]);
2077 if (write(args
->cpipe
[1], &b
, sizeof(char)) < 0) {
2078 fprintf(stderr
, "%s (child): error on write: %s\n",
2079 __func__
, strerror(errno
));
2081 close(args
->cpipe
[1]);
2082 return args
->wrapped(args
->sock
, args
->tpid
);
2086 * pid_to_ns - reads pids from a ucred over a socket, then writes the
2087 * int value back over the socket. This shifts the pid from the
2088 * sender's pidns into tpid's pidns.
2090 static int pid_to_ns(int sock
, pid_t tpid
)
2095 while (recv_creds(sock
, &cred
, &v
)) {
2098 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
2106 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
2107 * in your old pidns. Only children which you clone will be in the target
2108 * pidns. So the pid_to_ns_wrapper does the setns, then clones a child to
2109 * actually convert pids.
2111 * Note: glibc's fork() does not respect pidns, which can lead to failed
2112 * assertions inside glibc (and thus failed forks) if the child's pid in
2113 * the pidns and the parent pid outside are identical. Using clone prevents
2116 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
2118 int newnsfd
= -1, ret
, cpipe
[2];
2123 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2124 if (ret
< 0 || ret
>= sizeof(fnam
))
2126 newnsfd
= open(fnam
, O_RDONLY
);
2129 if (setns(newnsfd
, 0) < 0)
2133 if (pipe(cpipe
) < 0)
2136 struct pid_ns_clone_args args
= {
2140 .wrapped
= &pid_to_ns
2142 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2143 void *stack
= alloca(stack_size
);
2145 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2149 // give the child 1 second to be done forking and
2151 if (!wait_for_sock(cpipe
[0], 1))
2153 ret
= read(cpipe
[0], &v
, 1);
2154 if (ret
!= sizeof(char) || v
!= '1')
2157 if (!wait_for_pid(cpid
))
2163 * To read cgroup files with a particular pid, we will setns into the child
2164 * pidns, open a pipe, fork a child - which will be the first to really be in
2165 * the child ns - which does the cgfs_get_value and writes the data to the pipe.
2167 bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
2169 int sock
[2] = {-1, -1};
2170 char *tmpdata
= NULL
;
2172 pid_t qpid
, cpid
= -1;
2173 bool answer
= false;
2176 size_t sz
= 0, asz
= 0;
2178 if (!cgfs_get_value(contrl
, cg
, file
, &tmpdata
))
2182 * Now we read the pids from returned data one by one, pass
2183 * them into a child in the target namespace, read back the
2184 * translated pids, and put them into our to-return data
2187 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2188 perror("socketpair");
2197 if (!cpid
) // child - exits when done
2198 pid_to_ns_wrapper(sock
[1], tpid
);
2200 char *ptr
= tmpdata
;
2203 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
2205 ret
= send_creds(sock
[0], &cred
, v
, true);
2207 if (ret
== SEND_CREDS_NOTSK
)
2209 if (ret
== SEND_CREDS_FAIL
)
2212 // read converted results
2213 if (!wait_for_sock(sock
[0], 2)) {
2214 fprintf(stderr
, "%s: timed out waiting for pid from child: %s\n",
2215 __func__
, strerror(errno
));
2218 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2219 fprintf(stderr
, "%s: error reading pid from child: %s\n",
2220 __func__
, strerror(errno
));
2223 must_strcat_pid(d
, &sz
, &asz
, qpid
);
2225 ptr
= strchr(ptr
, '\n');
2231 cred
.pid
= getpid();
2233 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
2234 // failed to ask child to exit
2235 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
2236 __func__
, strerror(errno
));
2246 if (sock
[0] != -1) {
2253 int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2254 struct fuse_file_info
*fi
)
2256 struct fuse_context
*fc
= fuse_get_context();
2257 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2258 struct cgfs_files
*k
= NULL
;
2263 if (f
->type
!= LXC_TYPE_CGFILE
) {
2264 fprintf(stderr
, "Internal error: directory cache info used in cg_read\n");
2277 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2283 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) {
2288 if (strcmp(f
->file
, "tasks") == 0 ||
2289 strcmp(f
->file
, "/tasks") == 0 ||
2290 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2291 strcmp(f
->file
, "cgroup.procs") == 0)
2292 // special case - we have to translate the pids
2293 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
2295 r
= cgfs_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
2309 memcpy(buf
, data
, s
);
2310 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
2320 static int pid_from_ns(int sock
, pid_t tpid
)
2330 if (!wait_for_sock(sock
, 2)) {
2331 fprintf(stderr
, "%s: timeout reading from parent\n", __func__
);
2334 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
2335 fprintf(stderr
, "%s: bad read from parent: %s\n",
2336 __func__
, strerror(errno
));
2339 if (vpid
== -1) // done
2343 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
2345 cred
.pid
= getpid();
2346 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
2353 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
2355 int newnsfd
= -1, ret
, cpipe
[2];
2360 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
2361 if (ret
< 0 || ret
>= sizeof(fnam
))
2363 newnsfd
= open(fnam
, O_RDONLY
);
2366 if (setns(newnsfd
, 0) < 0)
2370 if (pipe(cpipe
) < 0)
2373 struct pid_ns_clone_args args
= {
2377 .wrapped
= &pid_from_ns
2379 size_t stack_size
= sysconf(_SC_PAGESIZE
);
2380 void *stack
= alloca(stack_size
);
2382 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
2386 // give the child 1 second to be done forking and
2388 if (!wait_for_sock(cpipe
[0], 1))
2390 ret
= read(cpipe
[0], &v
, 1);
2391 if (ret
!= sizeof(char) || v
!= '1')
2394 if (!wait_for_pid(cpid
))
2400 * Given host @uid, return the uid to which it maps in
2401 * @pid's user namespace, or -1 if none.
2403 bool hostuid_to_ns(uid_t uid
, pid_t pid
, uid_t
*answer
)
2408 sprintf(line
, "/proc/%d/uid_map", pid
);
2409 if ((f
= fopen(line
, "r")) == NULL
) {
2413 *answer
= convert_id_to_ns(f
, uid
);
2422 * get_pid_creds: get the real uid and gid of @pid from
2424 * (XXX should we use euid here?)
2426 void get_pid_creds(pid_t pid
, uid_t
*uid
, gid_t
*gid
)
2435 sprintf(line
, "/proc/%d/status", pid
);
2436 if ((f
= fopen(line
, "r")) == NULL
) {
2437 fprintf(stderr
, "Error opening %s: %s\n", line
, strerror(errno
));
2440 while (fgets(line
, 400, f
)) {
2441 if (strncmp(line
, "Uid:", 4) == 0) {
2442 if (sscanf(line
+4, "%u", &u
) != 1) {
2443 fprintf(stderr
, "bad uid line for pid %u\n", pid
);
2448 } else if (strncmp(line
, "Gid:", 4) == 0) {
2449 if (sscanf(line
+4, "%u", &g
) != 1) {
2450 fprintf(stderr
, "bad gid line for pid %u\n", pid
);
2461 * May the requestor @r move victim @v to a new cgroup?
2462 * This is allowed if
2463 * . they are the same task
2464 * . they are ownedy by the same uid
2465 * . @r is root on the host, or
2466 * . @v's uid is mapped into @r's where @r is root.
2468 bool may_move_pid(pid_t r
, uid_t r_uid
, pid_t v
)
2470 uid_t v_uid
, tmpuid
;
2477 get_pid_creds(v
, &v_uid
, &v_gid
);
2480 if (hostuid_to_ns(r_uid
, r
, &tmpuid
) && tmpuid
== 0
2481 && hostuid_to_ns(v_uid
, r
, &tmpuid
))
2486 static bool do_write_pids(pid_t tpid
, uid_t tuid
, const char *contrl
, const char *cg
,
2487 const char *file
, const char *buf
)
2489 int sock
[2] = {-1, -1};
2490 pid_t qpid
, cpid
= -1;
2491 FILE *pids_file
= NULL
;
2492 bool answer
= false, fail
= false;
2494 pids_file
= open_pids_file(contrl
, cg
);
2499 * write the pids to a socket, have helper in writer's pidns
2500 * call movepid for us
2502 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
2503 perror("socketpair");
2511 if (!cpid
) { // child
2513 pid_from_ns_wrapper(sock
[1], tpid
);
2516 const char *ptr
= buf
;
2517 while (sscanf(ptr
, "%d", &qpid
) == 1) {
2521 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
2522 fprintf(stderr
, "%s: error writing pid to child: %s\n",
2523 __func__
, strerror(errno
));
2527 if (recv_creds(sock
[0], &cred
, &v
)) {
2529 if (!may_move_pid(tpid
, tuid
, cred
.pid
)) {
2533 if (fprintf(pids_file
, "%d", (int) cred
.pid
) < 0)
2538 ptr
= strchr(ptr
, '\n');
2544 /* All good, write the value */
2546 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
2547 fprintf(stderr
, "Warning: failed to ask child to exit\n");
2555 if (sock
[0] != -1) {
2560 if (fclose(pids_file
) != 0)
2566 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2567 struct fuse_file_info
*fi
)
2569 struct fuse_context
*fc
= fuse_get_context();
2570 char *localbuf
= NULL
;
2571 struct cgfs_files
*k
= NULL
;
2572 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2575 if (f
->type
!= LXC_TYPE_CGFILE
) {
2576 fprintf(stderr
, "Internal error: directory cache info used in cg_write\n");
2586 localbuf
= alloca(size
+1);
2587 localbuf
[size
] = '\0';
2588 memcpy(localbuf
, buf
, size
);
2590 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
2595 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
2600 if (strcmp(f
->file
, "tasks") == 0 ||
2601 strcmp(f
->file
, "/tasks") == 0 ||
2602 strcmp(f
->file
, "/cgroup.procs") == 0 ||
2603 strcmp(f
->file
, "cgroup.procs") == 0)
2604 // special case - we have to translate the pids
2605 r
= do_write_pids(fc
->pid
, fc
->uid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2607 r
= cgfs_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
2617 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
2619 struct fuse_context
*fc
= fuse_get_context();
2620 char *cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2621 struct cgfs_files
*k
= NULL
;
2628 if (strcmp(path
, "/cgroup") == 0)
2631 controller
= pick_controller_from_path(fc
, path
);
2634 cgroup
= find_cgroup_in_path(path
);
2636 /* this is just /cgroup/controller */
2639 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2649 if (is_child_cgroup(controller
, path1
, path2
)) {
2650 // get uid, gid, from '/tasks' file and make up a mode
2651 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2652 k
= cgfs_get_key(controller
, cgroup
, "tasks");
2655 k
= cgfs_get_key(controller
, path1
, path2
);
2663 * This being a fuse request, the uid and gid must be valid
2664 * in the caller's namespace. So we can just check to make
2665 * sure that the caller is root in his uid, and privileged
2666 * over the file's current owner.
2668 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
2673 ret
= cgfs_chown_file(controller
, cgroup
, uid
, gid
);
2682 int cg_chmod(const char *path
, mode_t mode
)
2684 struct fuse_context
*fc
= fuse_get_context();
2685 char * cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
2686 struct cgfs_files
*k
= NULL
;
2693 if (strcmp(path
, "/cgroup") == 0)
2696 controller
= pick_controller_from_path(fc
, path
);
2699 cgroup
= find_cgroup_in_path(path
);
2701 /* this is just /cgroup/controller */
2704 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2714 if (is_child_cgroup(controller
, path1
, path2
)) {
2715 // get uid, gid, from '/tasks' file and make up a mode
2716 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
2717 k
= cgfs_get_key(controller
, cgroup
, "tasks");
2720 k
= cgfs_get_key(controller
, path1
, path2
);
2728 * This being a fuse request, the uid and gid must be valid
2729 * in the caller's namespace. So we can just check to make
2730 * sure that the caller is root in his uid, and privileged
2731 * over the file's current owner.
2733 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
2738 if (!cgfs_chmod_file(controller
, cgroup
, mode
)) {
2750 int cg_mkdir(const char *path
, mode_t mode
)
2752 struct fuse_context
*fc
= fuse_get_context();
2753 char *last
= NULL
, *path1
, *cgdir
= NULL
, *controller
, *next
= NULL
;
2761 controller
= pick_controller_from_path(fc
, path
);
2765 cgroup
= find_cgroup_in_path(path
);
2769 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2775 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2778 if (!caller_is_in_ancestor(initpid
, controller
, path1
, &next
)) {
2781 else if (last
&& strcmp(next
, last
) == 0)
2788 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
2792 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
2797 ret
= cgfs_create(controller
, cgroup
, fc
->uid
, fc
->gid
);
2805 int cg_rmdir(const char *path
)
2807 struct fuse_context
*fc
= fuse_get_context();
2808 char *last
= NULL
, *cgdir
= NULL
, *controller
, *next
= NULL
;
2815 controller
= pick_controller_from_path(fc
, path
);
2819 cgroup
= find_cgroup_in_path(path
);
2823 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2829 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2832 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, &next
)) {
2833 if (!last
|| strcmp(next
, last
) == 0)
2840 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
2844 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
2849 if (!cgfs_remove(controller
, cgroup
)) {
2862 static bool startswith(const char *line
, const char *pref
)
2864 if (strncmp(line
, pref
, strlen(pref
)) == 0)
2869 static void get_mem_cached(char *memstat
, unsigned long *v
)
2875 if (startswith(memstat
, "total_cache")) {
2876 sscanf(memstat
+ 11, "%lu", v
);
2880 eol
= strchr(memstat
, '\n');
2887 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
2893 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
2895 size_t len
= strlen(key
);
2899 if (startswith(str
, key
)) {
2900 sscanf(str
+ len
, "%lu", v
);
2903 eol
= strchr(str
, '\n');
2910 static int read_file(const char *path
, char *buf
, size_t size
,
2911 struct file_info
*d
)
2913 size_t linelen
= 0, total_len
= 0, rv
= 0;
2915 char *cache
= d
->buf
;
2916 size_t cache_size
= d
->buflen
;
2917 FILE *f
= fopen(path
, "r");
2921 while (getline(&line
, &linelen
, f
) != -1) {
2922 ssize_t l
= snprintf(cache
, cache_size
, "%s", line
);
2924 perror("Error writing to cache");
2928 if (l
>= cache_size
) {
2929 fprintf(stderr
, "Internal error: truncated write to cache\n");
2938 d
->size
= total_len
;
2939 if (total_len
> size
)
2942 /* read from off 0 */
2943 memcpy(buf
, d
->buf
, total_len
);
2952 * FUSE ops for /proc
2955 static unsigned long get_memlimit(const char *cgroup
)
2957 char *memlimit_str
= NULL
;
2958 unsigned long memlimit
= -1;
2960 if (cgfs_get_value("memory", cgroup
, "memory.limit_in_bytes", &memlimit_str
))
2961 memlimit
= strtoul(memlimit_str
, NULL
, 10);
2968 static unsigned long get_min_memlimit(const char *cgroup
)
2970 char *copy
= strdupa(cgroup
);
2971 unsigned long memlimit
= 0, retlimit
;
2973 retlimit
= get_memlimit(copy
);
2975 while (strcmp(copy
, "/") != 0) {
2976 copy
= dirname(copy
);
2977 memlimit
= get_memlimit(copy
);
2978 if (memlimit
!= -1 && memlimit
< retlimit
)
2979 retlimit
= memlimit
;
2985 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
2986 struct fuse_file_info
*fi
)
2988 struct fuse_context
*fc
= fuse_get_context();
2989 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2991 char *memusage_str
= NULL
, *memstat_str
= NULL
,
2992 *memswlimit_str
= NULL
, *memswusage_str
= NULL
,
2993 *memswlimit_default_str
= NULL
, *memswusage_default_str
= NULL
;
2994 unsigned long memlimit
= 0, memusage
= 0, memswlimit
= 0, memswusage
= 0,
2995 cached
= 0, hosttotal
= 0;
2997 size_t linelen
= 0, total_len
= 0, rv
= 0;
2998 char *cache
= d
->buf
;
2999 size_t cache_size
= d
->buflen
;
3003 if (offset
> d
->size
)
3007 int left
= d
->size
- offset
;
3008 total_len
= left
> size
? size
: left
;
3009 memcpy(buf
, cache
+ offset
, total_len
);
3013 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3016 cg
= get_pid_cgroup(initpid
, "memory");
3018 return read_file("/proc/meminfo", buf
, size
, d
);
3019 prune_init_slice(cg
);
3021 memlimit
= get_min_memlimit(cg
);
3022 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
3024 if (!cgfs_get_value("memory", cg
, "memory.stat", &memstat_str
))
3027 // Following values are allowed to fail, because swapaccount might be turned
3028 // off for current kernel
3029 if(cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
) &&
3030 cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
))
3032 /* If swapaccounting is turned on, then default value is assumed to be that of cgroup / */
3033 if (!cgfs_get_value("memory", "/", "memory.memsw.limit_in_bytes", &memswlimit_default_str
))
3035 if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str
))
3038 memswlimit
= strtoul(memswlimit_str
, NULL
, 10);
3039 memswusage
= strtoul(memswusage_str
, NULL
, 10);
3041 if (!strcmp(memswlimit_str
, memswlimit_default_str
))
3043 if (!strcmp(memswusage_str
, memswusage_default_str
))
3046 memswlimit
= memswlimit
/ 1024;
3047 memswusage
= memswusage
/ 1024;
3050 memusage
= strtoul(memusage_str
, NULL
, 10);
3054 get_mem_cached(memstat_str
, &cached
);
3056 f
= fopen("/proc/meminfo", "r");
3060 while (getline(&line
, &linelen
, f
) != -1) {
3062 char *printme
, lbuf
[100];
3064 memset(lbuf
, 0, 100);
3065 if (startswith(line
, "MemTotal:")) {
3066 sscanf(line
+14, "%lu", &hosttotal
);
3067 if (hosttotal
< memlimit
)
3068 memlimit
= hosttotal
;
3069 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
3071 } else if (startswith(line
, "MemFree:")) {
3072 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
3074 } else if (startswith(line
, "MemAvailable:")) {
3075 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
3077 } else if (startswith(line
, "SwapTotal:") && memswlimit
> 0) {
3078 snprintf(lbuf
, 100, "SwapTotal: %8lu kB\n", memswlimit
- memlimit
);
3080 } else if (startswith(line
, "SwapFree:") && memswlimit
> 0 && memswusage
> 0) {
3081 snprintf(lbuf
, 100, "SwapFree: %8lu kB\n",
3082 (memswlimit
- memlimit
) - (memswusage
- memusage
));
3084 } else if (startswith(line
, "Slab:")) {
3085 snprintf(lbuf
, 100, "Slab: %8lu kB\n", 0UL);
3087 } else if (startswith(line
, "Buffers:")) {
3088 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
3090 } else if (startswith(line
, "Cached:")) {
3091 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
3093 } else if (startswith(line
, "SwapCached:")) {
3094 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
3099 l
= snprintf(cache
, cache_size
, "%s", printme
);
3101 perror("Error writing to cache");
3106 if (l
>= cache_size
) {
3107 fprintf(stderr
, "Internal error: truncated write to cache\n");
3118 d
->size
= total_len
;
3119 if (total_len
> size
) total_len
= size
;
3120 memcpy(buf
, d
->buf
, total_len
);
3129 free(memswlimit_str
);
3130 free(memswusage_str
);
3132 free(memswlimit_default_str
);
3133 free(memswusage_default_str
);
3138 * Read the cpuset.cpus for cg
3139 * Return the answer in a newly allocated string which must be freed
3141 static char *get_cpuset(const char *cg
)
3145 if (!cgfs_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
3150 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
3152 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
3156 if (sscanf(line
, "processor : %d", &cpu
) != 1)
3158 return cpu_in_cpuset(cpu
, cpuset
);
3162 * check whether this is a '^processor" line in /proc/cpuinfo
3164 static bool is_processor_line(const char *line
)
3168 if (sscanf(line
, "processor : %d", &cpu
) == 1)
3173 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
3174 struct fuse_file_info
*fi
)
3176 struct fuse_context
*fc
= fuse_get_context();
3177 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3179 char *cpuset
= NULL
;
3181 size_t linelen
= 0, total_len
= 0, rv
= 0;
3182 bool am_printing
= false, firstline
= true, is_s390x
= false;
3183 int curcpu
= -1, cpu
;
3184 char *cache
= d
->buf
;
3185 size_t cache_size
= d
->buflen
;
3189 if (offset
> d
->size
)
3193 int left
= d
->size
- offset
;
3194 total_len
= left
> size
? size
: left
;
3195 memcpy(buf
, cache
+ offset
, total_len
);
3199 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3202 cg
= get_pid_cgroup(initpid
, "cpuset");
3204 return read_file("proc/cpuinfo", buf
, size
, d
);
3205 prune_init_slice(cg
);
3207 cpuset
= get_cpuset(cg
);
3211 f
= fopen("/proc/cpuinfo", "r");
3215 while (getline(&line
, &linelen
, f
) != -1) {
3219 if (strstr(line
, "IBM/S390") != NULL
) {
3225 if (strncmp(line
, "# processors:", 12) == 0)
3227 if (is_processor_line(line
)) {
3228 am_printing
= cpuline_in_cpuset(line
, cpuset
);
3231 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
3233 perror("Error writing to cache");
3237 if (l
>= cache_size
) {
3238 fprintf(stderr
, "Internal error: truncated write to cache\n");
3247 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
3249 if (!cpu_in_cpuset(cpu
, cpuset
))
3252 p
= strchr(line
, ':');
3256 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
3258 perror("Error writing to cache");
3262 if (l
>= cache_size
) {
3263 fprintf(stderr
, "Internal error: truncated write to cache\n");
3274 l
= snprintf(cache
, cache_size
, "%s", line
);
3276 perror("Error writing to cache");
3280 if (l
>= cache_size
) {
3281 fprintf(stderr
, "Internal error: truncated write to cache\n");
3292 char *origcache
= d
->buf
;
3295 d
->buf
= malloc(d
->buflen
);
3298 cache_size
= d
->buflen
;
3300 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
3301 if (l
< 0 || l
>= cache_size
) {
3308 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
3309 if (l
< 0 || l
>= cache_size
) {
3316 l
= snprintf(cache
, cache_size
, "%s", origcache
);
3318 if (l
< 0 || l
>= cache_size
)
3324 d
->size
= total_len
;
3325 if (total_len
> size
) total_len
= size
;
3327 /* read from off 0 */
3328 memcpy(buf
, d
->buf
, total_len
);
3339 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
3340 struct fuse_file_info
*fi
)
3342 struct fuse_context
*fc
= fuse_get_context();
3343 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3345 char *cpuset
= NULL
;
3347 size_t linelen
= 0, total_len
= 0, rv
= 0;
3348 int curcpu
= -1; /* cpu numbering starts at 0 */
3349 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0;
3350 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
3351 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0;
3352 #define CPUALL_MAX_SIZE BUF_RESERVE_SIZE
3353 char cpuall
[CPUALL_MAX_SIZE
];
3354 /* reserve for cpu all */
3355 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
3356 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
3360 if (offset
> d
->size
)
3364 int left
= d
->size
- offset
;
3365 total_len
= left
> size
? size
: left
;
3366 memcpy(buf
, d
->buf
+ offset
, total_len
);
3370 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3373 cg
= get_pid_cgroup(initpid
, "cpuset");
3375 return read_file("/proc/stat", buf
, size
, d
);
3376 prune_init_slice(cg
);
3378 cpuset
= get_cpuset(cg
);
3382 f
= fopen("/proc/stat", "r");
3387 if (getline(&line
, &linelen
, f
) < 0) {
3388 fprintf(stderr
, "proc_stat_read read first line failed\n");
3392 while (getline(&line
, &linelen
, f
) != -1) {
3395 char cpu_char
[10]; /* That's a lot of cores */
3398 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
3399 /* not a ^cpuN line containing a number N, just print it */
3400 l
= snprintf(cache
, cache_size
, "%s", line
);
3402 perror("Error writing to cache");
3406 if (l
>= cache_size
) {
3407 fprintf(stderr
, "Internal error: truncated write to cache\n");
3417 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
3419 if (!cpu_in_cpuset(cpu
, cpuset
))
3423 c
= strchr(line
, ' ');
3426 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
3428 perror("Error writing to cache");
3433 if (l
>= cache_size
) {
3434 fprintf(stderr
, "Internal error: truncated write to cache\n");
3443 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user
, &nice
, &system
, &idle
, &iowait
, &irq
,
3444 &softirq
, &steal
, &guest
) != 9)
3448 system_sum
+= system
;
3450 iowait_sum
+= iowait
;
3452 softirq_sum
+= softirq
;
3459 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
3460 "cpu ", user_sum
, nice_sum
, system_sum
, idle_sum
, iowait_sum
, irq_sum
, softirq_sum
, steal_sum
, guest_sum
);
3461 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
){
3462 memcpy(cache
, cpuall
, cpuall_len
);
3463 cache
+= cpuall_len
;
3465 /* shouldn't happen */
3466 fprintf(stderr
, "proc_stat_read copy cpuall failed, cpuall_len=%d\n", cpuall_len
);
3470 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
3471 total_len
+= cpuall_len
;
3473 d
->size
= total_len
;
3474 if (total_len
> size
) total_len
= size
;
3476 memcpy(buf
, d
->buf
, total_len
);
3488 static long int getreaperage(pid_t pid
)
3495 qpid
= lookup_initpid_in_store(pid
);
3499 ret
= snprintf(fnam
, 100, "/proc/%d", qpid
);
3500 if (ret
< 0 || ret
>= 100)
3503 if (lstat(fnam
, &sb
) < 0)
3506 return time(NULL
) - sb
.st_ctime
;
3509 static unsigned long get_reaper_busy(pid_t task
)
3511 pid_t initpid
= lookup_initpid_in_store(task
);
3512 char *cgroup
= NULL
, *usage_str
= NULL
;
3513 unsigned long usage
= 0;
3518 cgroup
= get_pid_cgroup(initpid
, "cpuacct");
3521 prune_init_slice(cgroup
);
3522 if (!cgfs_get_value("cpuacct", cgroup
, "cpuacct.usage", &usage_str
))
3524 usage
= strtoul(usage_str
, NULL
, 10);
3525 usage
/= 1000000000;
3536 char *name
, *cwd
= get_current_dir_name();
3542 len
= strlen(cwd
) + strlen("/iwashere") + 1;
3544 snprintf(name
, len
, "%s/iwashere", cwd
);
3546 fd
= creat(name
, 0755);
3553 * We read /proc/uptime and reuse its second field.
3554 * For the first field, we use the mtime for the reaper for
3555 * the calling pid as returned by getreaperage
3557 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
3558 struct fuse_file_info
*fi
)
3560 struct fuse_context
*fc
= fuse_get_context();
3561 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3562 long int reaperage
= getreaperage(fc
->pid
);
3563 unsigned long int busytime
= get_reaper_busy(fc
->pid
), idletime
;
3564 char *cache
= d
->buf
;
3565 ssize_t total_len
= 0;
3572 if (offset
> d
->size
)
3576 int left
= d
->size
- offset
;
3577 total_len
= left
> size
? size
: left
;
3578 memcpy(buf
, cache
+ offset
, total_len
);
3582 idletime
= reaperage
- busytime
;
3583 if (idletime
> reaperage
)
3584 idletime
= reaperage
;
3586 total_len
= snprintf(d
->buf
, d
->size
, "%ld.0 %lu.0\n", reaperage
, idletime
);
3588 perror("Error writing to cache");
3592 d
->size
= (int)total_len
;
3595 if (total_len
> size
) total_len
= size
;
3597 memcpy(buf
, d
->buf
, total_len
);
3601 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
3602 struct fuse_file_info
*fi
)
3605 struct fuse_context
*fc
= fuse_get_context();
3606 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3608 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
3609 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
3610 unsigned long read
= 0, write
= 0;
3611 unsigned long read_merged
= 0, write_merged
= 0;
3612 unsigned long read_sectors
= 0, write_sectors
= 0;
3613 unsigned long read_ticks
= 0, write_ticks
= 0;
3614 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
3615 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
3616 char *cache
= d
->buf
;
3617 size_t cache_size
= d
->buflen
;
3619 size_t linelen
= 0, total_len
= 0, rv
= 0;
3620 unsigned int major
= 0, minor
= 0;
3625 if (offset
> d
->size
)
3629 int left
= d
->size
- offset
;
3630 total_len
= left
> size
? size
: left
;
3631 memcpy(buf
, cache
+ offset
, total_len
);
3635 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3638 cg
= get_pid_cgroup(initpid
, "blkio");
3640 return read_file("/proc/diskstats", buf
, size
, d
);
3641 prune_init_slice(cg
);
3643 if (!cgfs_get_value("blkio", cg
, "blkio.io_serviced_recursive", &io_serviced_str
))
3645 if (!cgfs_get_value("blkio", cg
, "blkio.io_merged_recursive", &io_merged_str
))
3647 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_bytes_recursive", &io_service_bytes_str
))
3649 if (!cgfs_get_value("blkio", cg
, "blkio.io_wait_time_recursive", &io_wait_time_str
))
3651 if (!cgfs_get_value("blkio", cg
, "blkio.io_service_time_recursive", &io_service_time_str
))
3655 f
= fopen("/proc/diskstats", "r");
3659 while (getline(&line
, &linelen
, f
) != -1) {
3663 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
3667 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
3668 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
3669 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
3670 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
3671 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
3672 read_sectors
= read_sectors
/512;
3673 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
3674 write_sectors
= write_sectors
/512;
3676 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
3677 rd_svctm
= rd_svctm
/1000000;
3678 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
3679 rd_wait
= rd_wait
/1000000;
3680 read_ticks
= rd_svctm
+ rd_wait
;
3682 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
3683 wr_svctm
= wr_svctm
/1000000;
3684 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
3685 wr_wait
= wr_wait
/1000000;
3686 write_ticks
= wr_svctm
+ wr_wait
;
3688 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
3689 tot_ticks
= tot_ticks
/1000000;
3691 memset(lbuf
, 0, 256);
3692 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
)
3693 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
3694 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
3695 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
3699 l
= snprintf(cache
, cache_size
, "%s", lbuf
);
3701 perror("Error writing to fuse buf");
3705 if (l
>= cache_size
) {
3706 fprintf(stderr
, "Internal error: truncated write to cache\n");
3716 d
->size
= total_len
;
3717 if (total_len
> size
) total_len
= size
;
3718 memcpy(buf
, d
->buf
, total_len
);
3726 free(io_serviced_str
);
3727 free(io_merged_str
);
3728 free(io_service_bytes_str
);
3729 free(io_wait_time_str
);
3730 free(io_service_time_str
);
3734 static int proc_swaps_read(char *buf
, size_t size
, off_t offset
,
3735 struct fuse_file_info
*fi
)
3737 struct fuse_context
*fc
= fuse_get_context();
3738 struct file_info
*d
= (struct file_info
*)fi
->fh
;
3740 char *memswlimit_str
= NULL
, *memlimit_str
= NULL
, *memusage_str
= NULL
, *memswusage_str
= NULL
,
3741 *memswlimit_default_str
= NULL
, *memswusage_default_str
= NULL
;
3742 unsigned long memswlimit
= 0, memlimit
= 0, memusage
= 0, memswusage
= 0, swap_total
= 0, swap_free
= 0;
3743 ssize_t total_len
= 0, rv
= 0;
3745 char *cache
= d
->buf
;
3748 if (offset
> d
->size
)
3752 int left
= d
->size
- offset
;
3753 total_len
= left
> size
? size
: left
;
3754 memcpy(buf
, cache
+ offset
, total_len
);
3758 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
3761 cg
= get_pid_cgroup(initpid
, "memory");
3763 return read_file("/proc/swaps", buf
, size
, d
);
3764 prune_init_slice(cg
);
3766 if (!cgfs_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
3769 if (!cgfs_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
3772 memlimit
= strtoul(memlimit_str
, NULL
, 10);
3773 memusage
= strtoul(memusage_str
, NULL
, 10);
3775 if (cgfs_get_value("memory", cg
, "memory.memsw.usage_in_bytes", &memswusage_str
) &&
3776 cgfs_get_value("memory", cg
, "memory.memsw.limit_in_bytes", &memswlimit_str
)) {
3778 /* If swap accounting is turned on, then default value is assumed to be that of cgroup / */
3779 if (!cgfs_get_value("memory", "/", "memory.memsw.limit_in_bytes", &memswlimit_default_str
))
3781 if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str
))
3784 memswlimit
= strtoul(memswlimit_str
, NULL
, 10);
3785 memswusage
= strtoul(memswusage_str
, NULL
, 10);
3787 if (!strcmp(memswlimit_str
, memswlimit_default_str
))
3789 if (!strcmp(memswusage_str
, memswusage_default_str
))
3792 swap_total
= (memswlimit
- memlimit
) / 1024;
3793 swap_free
= (memswusage
- memusage
) / 1024;
3796 total_len
= snprintf(d
->buf
, d
->size
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
3798 /* When no mem + swap limit is specified or swapaccount=0*/
3802 FILE *f
= fopen("/proc/meminfo", "r");
3807 while (getline(&line
, &linelen
, f
) != -1) {
3808 if (startswith(line
, "SwapTotal:")) {
3809 sscanf(line
, "SwapTotal: %8lu kB", &swap_total
);
3810 } else if (startswith(line
, "SwapFree:")) {
3811 sscanf(line
, "SwapFree: %8lu kB", &swap_free
);
3819 if (swap_total
> 0) {
3820 l
= snprintf(d
->buf
+ total_len
, d
->size
- total_len
,
3821 "none%*svirtual\t\t%lu\t%lu\t0\n", 36, " ",
3822 swap_total
, swap_free
);
3826 if (total_len
< 0 || l
< 0) {
3827 perror("Error writing to cache");
3833 d
->size
= (int)total_len
;
3835 if (total_len
> size
) total_len
= size
;
3836 memcpy(buf
, d
->buf
, total_len
);
3841 free(memswlimit_str
);
3844 free(memswusage_str
);
3845 free(memswusage_default_str
);
3846 free(memswlimit_default_str
);
3850 static off_t
get_procfile_size(const char *which
)
3852 FILE *f
= fopen(which
, "r");
3855 ssize_t sz
, answer
= 0;
3859 while ((sz
= getline(&line
, &len
, f
)) != -1)
3867 int proc_getattr(const char *path
, struct stat
*sb
)
3869 struct timespec now
;
3871 memset(sb
, 0, sizeof(struct stat
));
3872 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
3874 sb
->st_uid
= sb
->st_gid
= 0;
3875 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
3876 if (strcmp(path
, "/proc") == 0) {
3877 sb
->st_mode
= S_IFDIR
| 00555;
3881 if (strcmp(path
, "/proc/meminfo") == 0 ||
3882 strcmp(path
, "/proc/cpuinfo") == 0 ||
3883 strcmp(path
, "/proc/uptime") == 0 ||
3884 strcmp(path
, "/proc/stat") == 0 ||
3885 strcmp(path
, "/proc/diskstats") == 0 ||
3886 strcmp(path
, "/proc/swaps") == 0) {
3888 sb
->st_mode
= S_IFREG
| 00444;
3896 int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
3897 struct fuse_file_info
*fi
)
3899 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
3900 filler(buf
, "meminfo", NULL
, 0) != 0 ||
3901 filler(buf
, "stat", NULL
, 0) != 0 ||
3902 filler(buf
, "uptime", NULL
, 0) != 0 ||
3903 filler(buf
, "diskstats", NULL
, 0) != 0 ||
3904 filler(buf
, "swaps", NULL
, 0) != 0)
3909 int proc_open(const char *path
, struct fuse_file_info
*fi
)
3912 struct file_info
*info
;
3914 if (strcmp(path
, "/proc/meminfo") == 0)
3915 type
= LXC_TYPE_PROC_MEMINFO
;
3916 else if (strcmp(path
, "/proc/cpuinfo") == 0)
3917 type
= LXC_TYPE_PROC_CPUINFO
;
3918 else if (strcmp(path
, "/proc/uptime") == 0)
3919 type
= LXC_TYPE_PROC_UPTIME
;
3920 else if (strcmp(path
, "/proc/stat") == 0)
3921 type
= LXC_TYPE_PROC_STAT
;
3922 else if (strcmp(path
, "/proc/diskstats") == 0)
3923 type
= LXC_TYPE_PROC_DISKSTATS
;
3924 else if (strcmp(path
, "/proc/swaps") == 0)
3925 type
= LXC_TYPE_PROC_SWAPS
;
3929 info
= malloc(sizeof(*info
));
3933 memset(info
, 0, sizeof(*info
));
3936 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
3938 info
->buf
= malloc(info
->buflen
);
3939 } while (!info
->buf
);
3940 memset(info
->buf
, 0, info
->buflen
);
3941 /* set actual size to buffer size */
3942 info
->size
= info
->buflen
;
3944 fi
->fh
= (unsigned long)info
;
3948 int proc_access(const char *path
, int mask
)
3950 /* these are all read-only */
3951 if ((mask
& ~R_OK
) != 0)
3956 int proc_release(const char *path
, struct fuse_file_info
*fi
)
3958 do_release_file_info(fi
);
3962 int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
3963 struct fuse_file_info
*fi
)
3965 struct file_info
*f
= (struct file_info
*) fi
->fh
;
3968 case LXC_TYPE_PROC_MEMINFO
:
3969 return proc_meminfo_read(buf
, size
, offset
, fi
);
3970 case LXC_TYPE_PROC_CPUINFO
:
3971 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
3972 case LXC_TYPE_PROC_UPTIME
:
3973 return proc_uptime_read(buf
, size
, offset
, fi
);
3974 case LXC_TYPE_PROC_STAT
:
3975 return proc_stat_read(buf
, size
, offset
, fi
);
3976 case LXC_TYPE_PROC_DISKSTATS
:
3977 return proc_diskstats_read(buf
, size
, offset
, fi
);
3978 case LXC_TYPE_PROC_SWAPS
:
3979 return proc_swaps_read(buf
, size
, offset
, fi
);
3985 static void __attribute__((constructor
)) collect_subsystems(void)
3991 if ((f
= fopen("/proc/self/cgroup", "r")) == NULL
) {
3992 fprintf(stderr
, "Error opening /proc/self/cgroup: %s\n", strerror(errno
));
3995 while (getline(&line
, &len
, f
) != -1) {
3998 p
= strchr(line
, ':');
4003 p2
= strrchr(p
, ':');
4008 /* With cgroupv2 /proc/self/cgroup can contain entries of the
4009 * form: 0::/ This will cause lxcfs to fail the cgroup mounts
4010 * because it parses out the empty string "" and later on passes
4011 * it to mount(). Let's skip such entries.
4016 if (!store_hierarchy(line
, p
))
4027 static void __attribute__((destructor
)) free_subsystems(void)
4031 for (i
= 0; i
< num_hierarchies
; i
++)
4033 free(hierarchies
[i
]);