3 * Copyright © 2014 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
6 * See COPYING file for details.
10 * NOTES - make sure to run this as -s to avoid threading.
11 * TODO - can we enforce that here from the code?
13 #define FUSE_USE_VERSION 26
27 #include <nih/alloc.h>
28 #include <nih/string.h>
30 #include "cgmanager.h"
34 * a null-terminated, nih-allocated list of the mounted subsystems. We
35 * detect this at startup.
39 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
42 * Given a open file * to /proc/pid/{u,g}id_map, and an id
43 * valid in the caller's namespace, return the id mapped into
45 * Returns the mapped id, or -1 on error.
48 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
50 unsigned int nsuid
, // base id for a range in the idfile's namespace
51 hostuid
, // base id for a range in the caller's namespace
52 count
; // number of ids in this range
56 fseek(idfile
, 0L, SEEK_SET
);
57 while (fgets(line
, 400, idfile
)) {
58 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
61 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
63 * uids wrapped around - unexpected as this is a procfile,
66 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s",
67 nsuid
, hostuid
, count
, line
);
70 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
72 * now since hostuid <= in_id < hostuid+count, and
73 * hostuid+count and nsuid+count do not wrap around,
74 * we know that nsuid+(in_id-hostuid) which must be
75 * less that nsuid+(count) must not wrap around
77 return (in_id
- hostuid
) + nsuid
;
86 * for is_privileged_over,
87 * specify whether we require the calling uid to be root in his
90 #define NS_ROOT_REQD true
91 #define NS_ROOT_OPT false
93 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
95 nih_local
char *fpath
= NULL
;
99 if (victim
== -1 || uid
== -1)
103 * If the request is one not requiring root in the namespace,
104 * then having the same uid suffices. (i.e. uid 1000 has write
105 * access to files owned by uid 1000
107 if (!req_ns_root
&& uid
== victim
)
110 fpath
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/uid_map", pid
) );
111 FILE *f
= fopen(fpath
, "r");
115 /* if caller's not root in his namespace, reject */
116 nsuid
= convert_id_to_ns(f
, uid
);
121 * If victim is not mapped into caller's ns, reject.
122 * XXX I'm not sure this check is needed given that fuse
123 * will be sending requests where the vfs has converted
125 nsuid
= convert_id_to_ns(f
, victim
);
136 static bool perms_include(int fmode
, mode_t req_mode
)
140 switch (req_mode
& O_ACCMODE
) {
148 r
= S_IROTH
| S_IWOTH
;
153 return ((fmode
& r
) == r
);
156 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
160 if (strlen(taskcg
) <= strlen(querycg
)) {
161 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
165 if (strcmp(querycg
, "/") == 0)
166 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ 1) );
168 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ strlen(querycg
) + 1) );
169 end
= strchr(start
, '/');
176 * check whether a fuse context may access a cgroup dir or file
178 * If file is not null, it is a cgroup file to check under cg.
179 * If file is null, then we are checking perms on cg itself.
181 * For files we can check the mode of the list_keys result.
182 * For cgroups, we must make assumptions based on the files under the
183 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
186 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
188 nih_local
struct cgm_keys
**list
= NULL
;
197 if (!cgm_list_keys(contrl
, cg
, &list
))
199 for (i
= 0; list
[i
]; i
++) {
200 if (strcmp(list
[i
]->name
, file
) == 0) {
201 struct cgm_keys
*k
= list
[i
];
202 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
203 if (perms_include(k
->mode
>> 6, mode
))
206 if (fc
->gid
== k
->gid
) {
207 if (perms_include(k
->mode
>> 3, mode
))
210 return perms_include(k
->mode
, mode
);
217 static void stripnewline(char *x
)
219 size_t l
= strlen(x
);
220 if (l
&& x
[l
-1] == '\n')
225 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
226 * If caller is in /a, he may act on /a/b, but not on /b.
227 * if the answer is false and nextcg is not NULL, then *nextcg will point
228 * to a nih_alloc'd string containing the next cgroup directory under cg
230 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
232 nih_local
char *fnam
= NULL
;
238 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
239 if (!(f
= fopen(fnam
, "r")))
242 while (getline(&line
, &len
, f
) != -1) {
243 char *c1
, *c2
, *linecmp
;
246 c1
= strchr(line
, ':');
250 c2
= strchr(c1
, ':');
254 if (strcmp(c1
, contrl
) != 0)
259 * callers pass in '/' for root cgroup, otherwise they pass
260 * in a cgroup without leading '/'
262 linecmp
= *cg
== '/' ? c2
: c2
+1;
263 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
265 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
279 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
280 * and needs to be nih_freed.
282 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
287 if (strlen(path
) < 9)
290 ret
= nih_strdup(NULL
, p1
);
293 slash
= strstr(ret
, "/");
297 /* verify that it is a subsystem */
298 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
304 for (i
= 0; list
[i
]; i
++) {
305 if (strcmp(list
[i
], ret
) == 0)
313 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
314 * Note that the returned value may include files (keynames) etc
316 static const char *find_cgroup_in_path(const char *path
)
320 if (strlen(path
) < 9)
322 p1
= strstr(path
+8, "/");
328 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
330 nih_local
char **list
= NULL
;
338 if (!cgm_list_children(contr
, dir
, &list
))
340 for (i
= 0; list
[i
]; i
++) {
341 if (strcmp(list
[i
], f
) == 0)
348 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
350 nih_local
struct cgm_keys
**list
= NULL
;
358 if (!cgm_list_keys(contr
, dir
, &list
))
360 for (i
= 0; list
[i
]; i
++) {
361 if (strcmp(list
[i
]->name
, f
) == 0) {
362 k
= NIH_MUST( nih_alloc(NULL
, (sizeof(*k
))) );
363 k
->name
= NIH_MUST( nih_strdup(k
, list
[i
]->name
) );
364 k
->uid
= list
[i
]->uid
;
365 k
->gid
= list
[i
]->gid
;
366 k
->mode
= list
[i
]->mode
;
374 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
378 *dir
= NIH_MUST( nih_strdup(NULL
, cg
) );
379 *file
= strrchr(cg
, '/');
384 p
= strrchr(*dir
, '/');
388 static size_t get_file_size(const char *contrl
, const char *cg
, const char *f
)
390 nih_local
char *data
= NULL
;
392 if (!cgm_get_value(contrl
, cg
, f
, &data
))
399 * FUSE ops for /cgroup
402 static int cg_getattr(const char *path
, struct stat
*sb
)
405 struct fuse_context
*fc
= fuse_get_context();
406 nih_local
char * cgdir
= NULL
;
407 char *fpath
= NULL
, *path1
, *path2
;
408 nih_local
struct cgm_keys
*k
= NULL
;
410 nih_local
char *controller
= NULL
;
416 memset(sb
, 0, sizeof(struct stat
));
418 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
421 sb
->st_uid
= sb
->st_gid
= 0;
422 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
425 if (strcmp(path
, "/cgroup") == 0) {
426 sb
->st_mode
= S_IFDIR
| 00755;
431 controller
= pick_controller_from_path(fc
, path
);
434 cgroup
= find_cgroup_in_path(path
);
436 /* this is just /cgroup/controller, return it as a dir */
437 sb
->st_mode
= S_IFDIR
| 00755;
442 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
452 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
453 * Then check that caller's cgroup is under path if fpath is a child
454 * cgroup, or cgdir if fpath is a file */
456 if (is_child_cgroup(controller
, path1
, path2
)) {
457 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
458 /* this is just /cgroup/controller, return it as a dir */
459 sb
->st_mode
= S_IFDIR
| 00555;
463 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
466 // get uid, gid, from '/tasks' file and make up a mode
467 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
468 sb
->st_mode
= S_IFDIR
| 00755;
469 k
= get_cgroup_key(controller
, cgroup
, "tasks");
471 sb
->st_uid
= sb
->st_gid
= 0;
480 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
481 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
))
483 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
486 sb
->st_mode
= S_IFREG
| k
->mode
;
490 sb
->st_size
= get_file_size(controller
, path1
, path2
);
498 * TODO - cache these results in a table for use in opendir, free
501 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
503 struct fuse_context
*fc
= fuse_get_context();
504 nih_local
struct cgm_keys
**list
= NULL
;
506 nih_local
char *controller
= NULL
;
507 nih_local
char *nextcg
= NULL
;
512 if (strcmp(path
, "/cgroup") == 0)
515 // return list of keys for the controller, and list of child cgroups
516 controller
= pick_controller_from_path(fc
, path
);
520 cgroup
= find_cgroup_in_path(path
);
522 /* this is just /cgroup/controller, return its contents */
526 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
531 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
532 struct fuse_file_info
*fi
)
534 struct fuse_context
*fc
= fuse_get_context();
539 if (strcmp(path
, "/cgroup") == 0) {
540 // get list of controllers
541 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
547 for (i
= 0; list
[i
]; i
++) {
548 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
555 // return list of keys for the controller, and list of child cgroups
556 nih_local
struct cgm_keys
**list
= NULL
;
558 nih_local
char *controller
= NULL
;
560 nih_local
char *nextcg
= NULL
;
562 controller
= pick_controller_from_path(fc
, path
);
566 cgroup
= find_cgroup_in_path(path
);
568 /* this is just /cgroup/controller, return its contents */
572 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
575 if (!cgm_list_keys(controller
, cgroup
, &list
))
576 // not a valid cgroup
579 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, &nextcg
)) {
582 ret
= filler(buf
, nextcg
, NULL
, 0);
589 for (i
= 0; list
[i
]; i
++) {
590 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
595 // now get the list of child cgroups
596 nih_local
char **clist
;
598 if (!cgm_list_children(controller
, cgroup
, &clist
))
600 for (i
= 0; clist
[i
]; i
++) {
601 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
608 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
614 * TODO - cache info here for read/write, release in cg_release.
616 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
618 nih_local
char *controller
= NULL
;
620 char *fpath
= NULL
, *path1
, *path2
;
621 nih_local
char * cgdir
= NULL
;
622 nih_local
struct cgm_keys
*k
= NULL
;
623 struct fuse_context
*fc
= fuse_get_context();
628 controller
= pick_controller_from_path(fc
, path
);
631 cgroup
= find_cgroup_in_path(path
);
635 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
644 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
645 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
))
646 // should never get here
655 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
656 struct fuse_file_info
*fi
)
658 nih_local
char *controller
= NULL
;
660 char *fpath
= NULL
, *path1
, *path2
;
661 struct fuse_context
*fc
= fuse_get_context();
662 nih_local
char * cgdir
= NULL
;
663 nih_local
struct cgm_keys
*k
= NULL
;
671 controller
= pick_controller_from_path(fc
, path
);
674 cgroup
= find_cgroup_in_path(path
);
678 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
687 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
688 nih_local
char *data
= NULL
;
691 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
692 // should never get here
695 if (!cgm_get_value(controller
, path1
, path2
, &data
))
701 memcpy(buf
, data
, s
);
709 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
710 struct fuse_file_info
*fi
)
712 nih_local
char *controller
= NULL
;
714 char *fpath
= NULL
, *path1
, *path2
;
715 struct fuse_context
*fc
= fuse_get_context();
716 nih_local
char * cgdir
= NULL
;
717 nih_local
struct cgm_keys
*k
= NULL
;
725 controller
= pick_controller_from_path(fc
, path
);
728 cgroup
= find_cgroup_in_path(path
);
732 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
741 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
742 if (!fc_may_access(fc
, controller
, path1
, path2
, O_WRONLY
))
745 if (!cgm_set_value(controller
, path1
, path2
, buf
))
754 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
756 struct fuse_context
*fc
= fuse_get_context();
757 nih_local
char * cgdir
= NULL
;
758 char *fpath
= NULL
, *path1
, *path2
;
759 nih_local
struct cgm_keys
*k
= NULL
;
761 nih_local
char *controller
= NULL
;
767 if (strcmp(path
, "/cgroup") == 0)
770 controller
= pick_controller_from_path(fc
, path
);
773 cgroup
= find_cgroup_in_path(path
);
775 /* this is just /cgroup/controller */
778 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
788 if (is_child_cgroup(controller
, path1
, path2
)) {
789 // get uid, gid, from '/tasks' file and make up a mode
790 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
791 k
= get_cgroup_key(controller
, cgroup
, "tasks");
794 k
= get_cgroup_key(controller
, path1
, path2
);
800 * This being a fuse request, the uid and gid must be valid
801 * in the caller's namespace. So we can just check to make
802 * sure that the caller is root in his uid, and privileged
803 * over the file's current owner.
805 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
))
808 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
))
813 int cg_chmod(const char *path
, mode_t mode
)
815 struct fuse_context
*fc
= fuse_get_context();
816 nih_local
char * cgdir
= NULL
;
817 char *fpath
= NULL
, *path1
, *path2
;
818 nih_local
struct cgm_keys
*k
= NULL
;
820 nih_local
char *controller
= NULL
;
825 if (strcmp(path
, "/cgroup") == 0)
828 controller
= pick_controller_from_path(fc
, path
);
831 cgroup
= find_cgroup_in_path(path
);
833 /* this is just /cgroup/controller */
836 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
846 if (is_child_cgroup(controller
, path1
, path2
)) {
847 // get uid, gid, from '/tasks' file and make up a mode
848 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
849 k
= get_cgroup_key(controller
, cgroup
, "tasks");
852 k
= get_cgroup_key(controller
, path1
, path2
);
858 * This being a fuse request, the uid and gid must be valid
859 * in the caller's namespace. So we can just check to make
860 * sure that the caller is root in his uid, and privileged
861 * over the file's current owner.
863 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
))
866 if (!cgm_chmod_file(controller
, cgroup
, mode
))
871 int cg_mkdir(const char *path
, mode_t mode
)
873 struct fuse_context
*fc
= fuse_get_context();
874 nih_local
struct cgm_keys
**list
= NULL
;
875 char *fpath
= NULL
, *path1
;
876 nih_local
char * cgdir
= NULL
;
878 nih_local
char *controller
= NULL
;
884 controller
= pick_controller_from_path(fc
, path
);
888 cgroup
= find_cgroup_in_path(path
);
892 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
898 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
))
902 if (!cgm_create(controller
, cgroup
, fc
->uid
, fc
->gid
))
908 static int cg_rmdir(const char *path
)
910 struct fuse_context
*fc
= fuse_get_context();
911 nih_local
struct cgm_keys
**list
= NULL
;
913 nih_local
char * cgdir
= NULL
;
915 nih_local
char *controller
= NULL
;
921 controller
= pick_controller_from_path(fc
, path
);
925 cgroup
= find_cgroup_in_path(path
);
929 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
933 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
))
936 if (!cgm_remove(controller
, cgroup
))
942 static bool startswith(const char *line
, const char *pref
)
944 if (strncmp(line
, pref
, strlen(pref
)) == 0)
949 static void get_mem_cached(char *memstat
, unsigned long *v
)
955 if (startswith(memstat
, "total_cache")) {
956 sscanf(memstat
+ 11, "%lu", v
);
960 eol
= strchr(memstat
, '\n');
967 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
969 nih_local
char *fnam
= NULL
;
975 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
976 if (!(f
= fopen(fnam
, "r")))
979 while (getline(&line
, &len
, f
) != -1) {
983 c1
= strchr(line
, ':');
987 c2
= strchr(c1
, ':');
991 if (strcmp(c1
, contrl
) != 0)
995 answer
= NIH_MUST( nih_strdup(NULL
, c2
) );
1006 * FUSE ops for /proc
1009 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1010 struct fuse_file_info
*fi
)
1012 struct fuse_context
*fc
= fuse_get_context();
1013 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "memory");
1014 nih_local
char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1015 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1017 size_t linelen
= 0, total_len
= 0;
1026 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1028 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1030 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1032 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1033 memusage
= strtoul(memusage_str
, NULL
, 10);
1036 get_mem_cached(memstat_str
, &cached
);
1038 f
= fopen("/proc/meminfo", "r");
1042 while (getline(&line
, &linelen
, f
) != -1) {
1044 char *printme
, lbuf
[100];
1046 memset(lbuf
, 0, 100);
1047 if (startswith(line
, "MemTotal:")) {
1048 sscanf(line
+14, "%lu", &hosttotal
);
1049 if (hosttotal
< memlimit
)
1050 memlimit
= hosttotal
;
1051 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1053 } else if (startswith(line
, "MemFree:")) {
1054 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1056 } else if (startswith(line
, "MemAvailable:")) {
1057 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1059 } else if (startswith(line
, "Buffers:")) {
1060 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1062 } else if (startswith(line
, "Cached:")) {
1063 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1065 } else if (startswith(line
, "SwapCached:")) {
1066 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1070 l
= snprintf(buf
, size
, "%s", printme
);
1080 * Read the cpuset.cpus for cg
1081 * Return the answer in a nih_alloced string
1083 static char *get_cpuset(const char *cg
)
1087 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1093 * Helper functions for cpuset_in-set
1095 char *cpuset_nexttok(const char *c
)
1097 char *r
= strchr(c
+1, ',');
1103 int cpuset_getrange(const char *c
, int *a
, int *b
)
1107 ret
= sscanf(c
, "%d-%d", a
, b
);
1112 * cpusets are in format "1,2-3,4"
1113 * iow, comma-delimited ranges
1115 static bool cpu_in_cpuset(int cpu
, const char *cpuset
)
1119 for (c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
1122 ret
= cpuset_getrange(c
, &a
, &b
);
1123 if (ret
== 1 && cpu
== a
)
1125 if (ret
!= 2) // bad cpuset!
1127 if (cpu
>= a
&& cpu
<= b
)
1134 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1138 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1140 return cpu_in_cpuset(cpu
, cpuset
);
1144 * check whether this is a '^processor" line in /proc/cpuinfo
1146 static bool is_processor_line(const char *line
)
1150 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1155 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1156 struct fuse_file_info
*fi
)
1158 struct fuse_context
*fc
= fuse_get_context();
1159 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1160 nih_local
char *cpuset
= NULL
;
1162 size_t linelen
= 0, total_len
= 0;
1163 bool am_printing
= false;
1173 cpuset
= get_cpuset(cg
);
1177 f
= fopen("/proc/cpuinfo", "r");
1181 while (getline(&line
, &linelen
, f
) != -1) {
1183 if (is_processor_line(line
)) {
1184 am_printing
= cpuline_in_cpuset(line
, cpuset
);
1187 l
= snprintf(buf
, size
, "processor : %d\n", curcpu
);
1195 l
= snprintf(buf
, size
, "%s", line
);
1205 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
1206 struct fuse_file_info
*fi
)
1208 struct fuse_context
*fc
= fuse_get_context();
1209 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1210 nih_local
char *cpuset
= NULL
;
1212 size_t linelen
= 0, total_len
= 0;
1222 cpuset
= get_cpuset(cg
);
1226 f
= fopen("/proc/stat", "r");
1230 while (getline(&line
, &linelen
, f
) != -1) {
1235 if (sscanf(line
, "cpu%d", &cpu
) != 1) {
1236 /* not a ^cpu line, just print it */
1237 l
= snprintf(buf
, size
, "%s", line
);
1243 if (!cpu_in_cpuset(cpu
, cpuset
))
1247 c
= strchr(line
, ' ');
1250 l
= snprintf(buf
, size
, "cpu%d %s", curcpu
, c
);
1259 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
1260 struct fuse_file_info
*fi
)
1265 static off_t
get_procfile_size(const char *which
)
1267 FILE *f
= fopen(which
, "r");
1270 ssize_t sz
, answer
= 0;
1274 while ((sz
= getline(&line
, &len
, f
)) != -1)
1281 static int proc_getattr(const char *path
, struct stat
*sb
)
1283 struct timespec now
;
1285 memset(sb
, 0, sizeof(struct stat
));
1286 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1288 sb
->st_uid
= sb
->st_gid
= 0;
1289 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1290 if (strcmp(path
, "/proc") == 0) {
1291 sb
->st_mode
= S_IFDIR
| 00555;
1295 if (strcmp(path
, "/proc/meminfo") == 0 ||
1296 strcmp(path
, "/proc/cpuinfo") == 0 ||
1297 strcmp(path
, "/proc/uptime") == 0 ||
1298 strcmp(path
, "/proc/stat") == 0) {
1300 sb
->st_size
= get_procfile_size(path
);
1301 sb
->st_mode
= S_IFREG
| 00444;
1309 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1310 struct fuse_file_info
*fi
)
1312 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
1313 filler(buf
, "meminfo", NULL
, 0) != 0 ||
1314 filler(buf
, "stat", NULL
, 0) != 0 ||
1315 filler(buf
, "uptime", NULL
, 0) != 0)
1320 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
1322 if (strcmp(path
, "/proc/meminfo") == 0 ||
1323 strcmp(path
, "/proc/cpuinfo") == 0 ||
1324 strcmp(path
, "/proc/uptime") == 0 ||
1325 strcmp(path
, "/proc/stat") == 0)
1330 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1331 struct fuse_file_info
*fi
)
1333 if (strcmp(path
, "/proc/meminfo") == 0)
1334 return proc_meminfo_read(buf
, size
, offset
, fi
);
1335 if (strcmp(path
, "/proc/cpuinfo") == 0)
1336 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
1337 if (strcmp(path
, "/proc/uptime") == 0)
1338 return proc_uptime_read(buf
, size
, offset
, fi
);
1339 if (strcmp(path
, "/proc/stat") == 0)
1340 return proc_stat_read(buf
, size
, offset
, fi
);
1346 * these just delegate to the /proc and /cgroup ops as
1350 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
1352 if (strcmp(path
, "/") == 0) {
1353 sb
->st_mode
= S_IFDIR
| 00755;
1357 if (strncmp(path
, "/cgroup", 7) == 0) {
1358 return cg_getattr(path
, sb
);
1360 if (strncmp(path
, "/proc", 5) == 0) {
1361 return proc_getattr(path
, sb
);
1366 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
1368 if (strcmp(path
, "/") == 0)
1371 if (strncmp(path
, "/cgroup", 7) == 0) {
1372 return cg_opendir(path
, fi
);
1374 if (strcmp(path
, "/proc") == 0)
1379 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1380 struct fuse_file_info
*fi
)
1382 if (strcmp(path
, "/") == 0) {
1383 if (filler(buf
, "proc", NULL
, 0) != 0 ||
1384 filler(buf
, "cgroup", NULL
, 0) != 0)
1388 if (strncmp(path
, "/cgroup", 7) == 0)
1389 return cg_readdir(path
, buf
, filler
, offset
, fi
);
1390 if (strcmp(path
, "/proc") == 0)
1391 return proc_readdir(path
, buf
, filler
, offset
, fi
);
1395 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
1397 if (strcmp(path
, "/") == 0)
1399 if (strncmp(path
, "/cgroup", 7) == 0) {
1400 return cg_releasedir(path
, fi
);
1402 if (strcmp(path
, "/proc") == 0)
1407 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
1409 if (strncmp(path
, "/cgroup", 7) == 0)
1410 return cg_open(path
, fi
);
1411 if (strncmp(path
, "/proc", 5) == 0)
1412 return proc_open(path
, fi
);
1417 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1418 struct fuse_file_info
*fi
)
1420 if (strncmp(path
, "/cgroup", 7) == 0)
1421 return cg_read(path
, buf
, size
, offset
, fi
);
1422 if (strncmp(path
, "/proc", 5) == 0)
1423 return proc_read(path
, buf
, size
, offset
, fi
);
1428 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1429 struct fuse_file_info
*fi
)
1431 if (strncmp(path
, "/cgroup", 7) == 0) {
1432 return cg_write(path
, buf
, size
, offset
, fi
);
1438 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
1443 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
1448 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
1453 int lxcfs_mkdir(const char *path
, mode_t mode
)
1455 if (strncmp(path
, "/cgroup", 7) == 0)
1456 return cg_mkdir(path
, mode
);
1461 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
1463 if (strncmp(path
, "/cgroup", 7) == 0)
1464 return cg_chown(path
, uid
, gid
);
1470 * cat first does a truncate before doing ops->write. This doesn't
1471 * really make sense for cgroups. So just return 0 always but do
1474 int lxcfs_truncate(const char *path
, off_t newsize
)
1476 if (strncmp(path
, "/cgroup", 7) == 0)
1481 int lxcfs_rmdir(const char *path
)
1483 if (strncmp(path
, "/cgroup", 7) == 0)
1484 return cg_rmdir(path
);
1488 int lxcfs_chmod(const char *path
, mode_t mode
)
1490 if (strncmp(path
, "/cgroup", 7) == 0)
1491 return cg_chmod(path
, mode
);
1495 const struct fuse_operations lxcfs_ops
= {
1496 .getattr
= lxcfs_getattr
,
1500 .mkdir
= lxcfs_mkdir
,
1502 .rmdir
= lxcfs_rmdir
,
1506 .chmod
= lxcfs_chmod
,
1507 .chown
= lxcfs_chown
,
1508 .truncate
= lxcfs_truncate
,
1513 .release
= lxcfs_release
,
1514 .write
= lxcfs_write
,
1517 .flush
= lxcfs_flush
,
1518 .fsync
= lxcfs_fsync
,
1523 .removexattr
= NULL
,
1525 .opendir
= lxcfs_opendir
,
1526 .readdir
= lxcfs_readdir
,
1527 .releasedir
= lxcfs_releasedir
,
1538 static void usage(const char *me
)
1540 fprintf(stderr
, "Usage:\n");
1541 fprintf(stderr
, "\n");
1542 fprintf(stderr
, "%s [FUSE and mount options] mountpoint\n", me
);
1546 static bool is_help(char *w
)
1548 if (strcmp(w
, "-h") == 0 ||
1549 strcmp(w
, "--help") == 0 ||
1550 strcmp(w
, "-help") == 0 ||
1551 strcmp(w
, "help") == 0)
1556 int main(int argc
, char *argv
[])
1559 struct lxcfs_state
*d
;
1561 if (argc
< 2 || is_help(argv
[1]))
1564 d
= malloc(sizeof(*d
));
1568 if (!cgm_escape_cgroup())
1569 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
1571 if (!cgm_get_controllers(&d
->subsystems
))
1574 ret
= fuse_main(argc
, argv
, &lxcfs_ops
, d
);