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 <linux/sched.h>
28 #include <sys/socket.h>
29 #include <sys/mount.h>
32 #include <nih/alloc.h>
33 #include <nih/string.h>
35 #include "cgmanager.h"
39 * a null-terminated, nih-allocated list of the mounted subsystems. We
40 * detect this at startup.
44 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
46 static int wait_for_pid(pid_t pid
)
51 ret
= waitpid(pid
, &status
, 0);
59 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
65 * Given a open file * to /proc/pid/{u,g}id_map, and an id
66 * valid in the caller's namespace, return the id mapped into
68 * Returns the mapped id, or -1 on error.
71 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
73 unsigned int nsuid
, // base id for a range in the idfile's namespace
74 hostuid
, // base id for a range in the caller's namespace
75 count
; // number of ids in this range
79 fseek(idfile
, 0L, SEEK_SET
);
80 while (fgets(line
, 400, idfile
)) {
81 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
84 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
86 * uids wrapped around - unexpected as this is a procfile,
89 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s",
90 nsuid
, hostuid
, count
, line
);
93 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
95 * now since hostuid <= in_id < hostuid+count, and
96 * hostuid+count and nsuid+count do not wrap around,
97 * we know that nsuid+(in_id-hostuid) which must be
98 * less that nsuid+(count) must not wrap around
100 return (in_id
- hostuid
) + nsuid
;
109 * for is_privileged_over,
110 * specify whether we require the calling uid to be root in his
113 #define NS_ROOT_REQD true
114 #define NS_ROOT_OPT false
116 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
118 nih_local
char *fpath
= NULL
;
122 if (victim
== -1 || uid
== -1)
126 * If the request is one not requiring root in the namespace,
127 * then having the same uid suffices. (i.e. uid 1000 has write
128 * access to files owned by uid 1000
130 if (!req_ns_root
&& uid
== victim
)
133 fpath
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/uid_map", pid
) );
134 FILE *f
= fopen(fpath
, "r");
138 /* if caller's not root in his namespace, reject */
139 nsuid
= convert_id_to_ns(f
, uid
);
144 * If victim is not mapped into caller's ns, reject.
145 * XXX I'm not sure this check is needed given that fuse
146 * will be sending requests where the vfs has converted
148 nsuid
= convert_id_to_ns(f
, victim
);
159 static bool perms_include(int fmode
, mode_t req_mode
)
163 switch (req_mode
& O_ACCMODE
) {
171 r
= S_IROTH
| S_IWOTH
;
176 return ((fmode
& r
) == r
);
179 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
183 if (strlen(taskcg
) <= strlen(querycg
)) {
184 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
188 if (strcmp(querycg
, "/") == 0)
189 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ 1) );
191 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ strlen(querycg
) + 1) );
192 end
= strchr(start
, '/');
199 * check whether a fuse context may access a cgroup dir or file
201 * If file is not null, it is a cgroup file to check under cg.
202 * If file is null, then we are checking perms on cg itself.
204 * For files we can check the mode of the list_keys result.
205 * For cgroups, we must make assumptions based on the files under the
206 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
209 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
211 nih_local
struct cgm_keys
**list
= NULL
;
220 if (!cgm_list_keys(contrl
, cg
, &list
))
222 for (i
= 0; list
[i
]; i
++) {
223 if (strcmp(list
[i
]->name
, file
) == 0) {
224 struct cgm_keys
*k
= list
[i
];
225 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
226 if (perms_include(k
->mode
>> 6, mode
))
229 if (fc
->gid
== k
->gid
) {
230 if (perms_include(k
->mode
>> 3, mode
))
233 return perms_include(k
->mode
, mode
);
240 static void stripnewline(char *x
)
242 size_t l
= strlen(x
);
243 if (l
&& x
[l
-1] == '\n')
248 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
249 * If caller is in /a, he may act on /a/b, but not on /b.
250 * if the answer is false and nextcg is not NULL, then *nextcg will point
251 * to a nih_alloc'd string containing the next cgroup directory under cg
253 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
255 nih_local
char *fnam
= NULL
;
261 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
262 if (!(f
= fopen(fnam
, "r")))
265 while (getline(&line
, &len
, f
) != -1) {
266 char *c1
, *c2
, *linecmp
;
269 c1
= strchr(line
, ':');
273 c2
= strchr(c1
, ':');
277 if (strcmp(c1
, contrl
) != 0)
282 * callers pass in '/' for root cgroup, otherwise they pass
283 * in a cgroup without leading '/'
285 linecmp
= *cg
== '/' ? c2
: c2
+1;
286 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
288 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
302 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
303 * and needs to be nih_freed.
305 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
310 if (strlen(path
) < 9)
313 ret
= nih_strdup(NULL
, p1
);
316 slash
= strstr(ret
, "/");
320 /* verify that it is a subsystem */
321 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
327 for (i
= 0; list
[i
]; i
++) {
328 if (strcmp(list
[i
], ret
) == 0)
336 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
337 * Note that the returned value may include files (keynames) etc
339 static const char *find_cgroup_in_path(const char *path
)
343 if (strlen(path
) < 9)
345 p1
= strstr(path
+8, "/");
351 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
353 nih_local
char **list
= NULL
;
361 if (!cgm_list_children(contr
, dir
, &list
))
363 for (i
= 0; list
[i
]; i
++) {
364 if (strcmp(list
[i
], f
) == 0)
371 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
373 nih_local
struct cgm_keys
**list
= NULL
;
381 if (!cgm_list_keys(contr
, dir
, &list
))
383 for (i
= 0; list
[i
]; i
++) {
384 if (strcmp(list
[i
]->name
, f
) == 0) {
385 k
= NIH_MUST( nih_alloc(NULL
, (sizeof(*k
))) );
386 k
->name
= NIH_MUST( nih_strdup(k
, list
[i
]->name
) );
387 k
->uid
= list
[i
]->uid
;
388 k
->gid
= list
[i
]->gid
;
389 k
->mode
= list
[i
]->mode
;
397 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
401 *dir
= NIH_MUST( nih_strdup(NULL
, cg
) );
402 *file
= strrchr(cg
, '/');
407 p
= strrchr(*dir
, '/');
411 static size_t get_file_size(const char *contrl
, const char *cg
, const char *f
)
413 nih_local
char *data
= NULL
;
415 if (!cgm_get_value(contrl
, cg
, f
, &data
))
422 * FUSE ops for /cgroup
425 static int cg_getattr(const char *path
, struct stat
*sb
)
428 struct fuse_context
*fc
= fuse_get_context();
429 nih_local
char * cgdir
= NULL
;
430 char *fpath
= NULL
, *path1
, *path2
;
431 nih_local
struct cgm_keys
*k
= NULL
;
433 nih_local
char *controller
= NULL
;
439 memset(sb
, 0, sizeof(struct stat
));
441 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
444 sb
->st_uid
= sb
->st_gid
= 0;
445 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
448 if (strcmp(path
, "/cgroup") == 0) {
449 sb
->st_mode
= S_IFDIR
| 00755;
454 controller
= pick_controller_from_path(fc
, path
);
457 cgroup
= find_cgroup_in_path(path
);
459 /* this is just /cgroup/controller, return it as a dir */
460 sb
->st_mode
= S_IFDIR
| 00755;
465 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
475 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
476 * Then check that caller's cgroup is under path if fpath is a child
477 * cgroup, or cgdir if fpath is a file */
479 if (is_child_cgroup(controller
, path1
, path2
)) {
480 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
481 /* this is just /cgroup/controller, return it as a dir */
482 sb
->st_mode
= S_IFDIR
| 00555;
486 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
489 // get uid, gid, from '/tasks' file and make up a mode
490 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
491 sb
->st_mode
= S_IFDIR
| 00755;
492 k
= get_cgroup_key(controller
, cgroup
, "tasks");
494 sb
->st_uid
= sb
->st_gid
= 0;
503 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
504 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
))
506 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
509 sb
->st_mode
= S_IFREG
| k
->mode
;
513 sb
->st_size
= get_file_size(controller
, path1
, path2
);
521 * TODO - cache these results in a table for use in opendir, free
524 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
526 struct fuse_context
*fc
= fuse_get_context();
527 nih_local
struct cgm_keys
**list
= NULL
;
529 nih_local
char *controller
= NULL
;
530 nih_local
char *nextcg
= NULL
;
535 if (strcmp(path
, "/cgroup") == 0)
538 // return list of keys for the controller, and list of child cgroups
539 controller
= pick_controller_from_path(fc
, path
);
543 cgroup
= find_cgroup_in_path(path
);
545 /* this is just /cgroup/controller, return its contents */
549 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
554 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
555 struct fuse_file_info
*fi
)
557 struct fuse_context
*fc
= fuse_get_context();
562 if (strcmp(path
, "/cgroup") == 0) {
563 // get list of controllers
564 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
570 for (i
= 0; list
[i
]; i
++) {
571 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
578 // return list of keys for the controller, and list of child cgroups
579 nih_local
struct cgm_keys
**list
= NULL
;
581 nih_local
char *controller
= NULL
;
583 nih_local
char *nextcg
= NULL
;
585 controller
= pick_controller_from_path(fc
, path
);
589 cgroup
= find_cgroup_in_path(path
);
591 /* this is just /cgroup/controller, return its contents */
595 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
598 if (!cgm_list_keys(controller
, cgroup
, &list
))
599 // not a valid cgroup
602 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, &nextcg
)) {
605 ret
= filler(buf
, nextcg
, NULL
, 0);
612 for (i
= 0; list
[i
]; i
++) {
613 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
618 // now get the list of child cgroups
619 nih_local
char **clist
;
621 if (!cgm_list_children(controller
, cgroup
, &clist
))
623 for (i
= 0; clist
[i
]; i
++) {
624 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
631 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
637 * TODO - cache info here for read/write, release in cg_release.
639 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
641 nih_local
char *controller
= NULL
;
643 char *fpath
= NULL
, *path1
, *path2
;
644 nih_local
char * cgdir
= NULL
;
645 nih_local
struct cgm_keys
*k
= NULL
;
646 struct fuse_context
*fc
= fuse_get_context();
651 controller
= pick_controller_from_path(fc
, path
);
654 cgroup
= find_cgroup_in_path(path
);
658 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
667 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
668 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
))
669 // should never get here
678 static int msgrecv(int sockfd
, void *buf
, size_t len
)
684 FD_SET(sockfd
, &rfds
);
688 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) < 0)
690 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
693 static bool send_creds(int sock
, struct ucred
*cred
, char v
)
695 struct msghdr msg
= { 0 };
697 struct cmsghdr
*cmsg
;
698 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
702 if (msgrecv(sock
, buf
, 1) != 1) {
703 printf("%s: Error getting reply from server over socketpair",
708 msg
.msg_control
= cmsgbuf
;
709 msg
.msg_controllen
= sizeof(cmsgbuf
);
711 cmsg
= CMSG_FIRSTHDR(&msg
);
712 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
713 cmsg
->cmsg_level
= SOL_SOCKET
;
714 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
715 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
722 iov
.iov_len
= sizeof(buf
);
726 if (sendmsg(sock
, &msg
, 0) < 0) {
727 printf("%s: failed at sendmsg: %s", __func__
,
737 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
739 struct msghdr msg
= { 0 };
741 struct cmsghdr
*cmsg
;
742 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
753 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
754 printf("Failed to set passcred: %s", strerror(errno
));
758 if (write(sock
, buf
, 1) != 1) {
759 printf("Failed to start write on scm fd: %s", strerror(errno
));
765 msg
.msg_control
= cmsgbuf
;
766 msg
.msg_controllen
= sizeof(cmsgbuf
);
769 iov
.iov_len
= sizeof(buf
);
773 // retry logic is not ideal, especially as we are not
774 // threaded. Sleep at most 1 second waiting for the client
775 // to send us the scm_cred
776 ret
= recvmsg(sock
, &msg
, 0);
778 printf("Failed to receive scm_cred: %s",
783 cmsg
= CMSG_FIRSTHDR(&msg
);
785 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
786 cmsg
->cmsg_level
== SOL_SOCKET
&&
787 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
788 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
797 * pidreader - reads pids from a ucred over a socket, then writes the
798 * int value back over the socket
800 static void pidreader(int sock
, pid_t tpid
)
805 while (recv_creds(sock
, &cred
, &v
)) {
808 printf("CCC: child received %d\n", cred
.pid
);
809 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
816 * pidreader_wrapper: when you setns into a pidns, you yourself remain
817 * in your old pidns. Only children which you fork will be in the target
818 * pidns. So the pidreader_wrapper does the setns, then forks a child to
819 * actually convert pids
821 static void pidreader_wrapper(int sock
, pid_t tpid
)
827 sprintf(fnam
, "/proc/%d/ns/pid", tpid
);
828 newnsfd
= open(fnam
, O_RDONLY
);
831 if (setns(newnsfd
, 0) < 0)
840 pidreader(sock
, tpid
);
841 if (!wait_for_pid(cpid
))
847 * To read cgroup files with a particular pid, we will setns into the child
848 * pidns, open a pipe, fork a child - which will be the first to really be in
849 * the child ns - which does the cgm_get_value and writes the data to the pipe.
851 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
853 int sock
[2] = {-1, -1};
854 nih_local
char *tmpdata
= NULL
;
856 pid_t qpid
, cpid
= -1;
863 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
867 * Now we read the pids from returned data one by one, pass
868 * them into a child in the target namespace, read back the
869 * translated pids, and put them into our to-return data
872 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
873 perror("socketpair");
882 pidreader_wrapper(sock
[1], tpid
);
887 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
889 printf("AAA: sending %d\n", qpid
);
890 if (!send_creds(sock
[0], &cred
, v
))
893 // read converted results
898 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
903 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
908 printf("BBB: read %d\n", qpid
);
909 NIH_MUST( nih_strcat_sprintf(d
, NULL
, "%d\n", qpid
) );
910 ptr
= strchr(ptr
, '\n');
918 if (!send_creds(sock
[0], &cred
, v
)) {
919 // failed to ask child to exit
936 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
937 struct fuse_file_info
*fi
)
939 nih_local
char *controller
= NULL
;
941 char *fpath
= NULL
, *path1
, *path2
;
942 struct fuse_context
*fc
= fuse_get_context();
943 nih_local
char * cgdir
= NULL
;
944 nih_local
struct cgm_keys
*k
= NULL
;
952 controller
= pick_controller_from_path(fc
, path
);
955 cgroup
= find_cgroup_in_path(path
);
959 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
968 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
969 nih_local
char *data
= NULL
;
972 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
973 // should never get here
976 printf("XXX path2 is .%s.\n", path2
);
977 if (strcmp(path2
, "tasks") == 0 ||
978 strcmp(path2
, "/tasks") == 0 ||
979 strcmp(path2
, "/cgroup.procs") == 0 ||
980 strcmp(path2
, "cgroup.procs") == 0)
981 // special case - we have to translate the pids
982 ret
= do_read_pids(fc
->pid
, controller
, path1
, path2
, &data
);
984 ret
= cgm_get_value(controller
, path1
, path2
, &data
);
992 memcpy(buf
, data
, s
);
1000 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1001 struct fuse_file_info
*fi
)
1003 nih_local
char *controller
= NULL
;
1005 char *fpath
= NULL
, *path1
, *path2
;
1006 struct fuse_context
*fc
= fuse_get_context();
1007 nih_local
char * cgdir
= NULL
;
1008 nih_local
struct cgm_keys
*k
= NULL
;
1016 controller
= pick_controller_from_path(fc
, path
);
1019 cgroup
= find_cgroup_in_path(path
);
1023 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1032 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
1033 if (!fc_may_access(fc
, controller
, path1
, path2
, O_WRONLY
))
1036 if (!cgm_set_value(controller
, path1
, path2
, buf
))
1045 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1047 struct fuse_context
*fc
= fuse_get_context();
1048 nih_local
char * cgdir
= NULL
;
1049 char *fpath
= NULL
, *path1
, *path2
;
1050 nih_local
struct cgm_keys
*k
= NULL
;
1052 nih_local
char *controller
= NULL
;
1058 if (strcmp(path
, "/cgroup") == 0)
1061 controller
= pick_controller_from_path(fc
, path
);
1064 cgroup
= find_cgroup_in_path(path
);
1066 /* this is just /cgroup/controller */
1069 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1079 if (is_child_cgroup(controller
, path1
, path2
)) {
1080 // get uid, gid, from '/tasks' file and make up a mode
1081 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1082 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1085 k
= get_cgroup_key(controller
, path1
, path2
);
1091 * This being a fuse request, the uid and gid must be valid
1092 * in the caller's namespace. So we can just check to make
1093 * sure that the caller is root in his uid, and privileged
1094 * over the file's current owner.
1096 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
))
1099 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
))
1104 int cg_chmod(const char *path
, mode_t mode
)
1106 struct fuse_context
*fc
= fuse_get_context();
1107 nih_local
char * cgdir
= NULL
;
1108 char *fpath
= NULL
, *path1
, *path2
;
1109 nih_local
struct cgm_keys
*k
= NULL
;
1111 nih_local
char *controller
= NULL
;
1116 if (strcmp(path
, "/cgroup") == 0)
1119 controller
= pick_controller_from_path(fc
, path
);
1122 cgroup
= find_cgroup_in_path(path
);
1124 /* this is just /cgroup/controller */
1127 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1137 if (is_child_cgroup(controller
, path1
, path2
)) {
1138 // get uid, gid, from '/tasks' file and make up a mode
1139 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1140 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1143 k
= get_cgroup_key(controller
, path1
, path2
);
1149 * This being a fuse request, the uid and gid must be valid
1150 * in the caller's namespace. So we can just check to make
1151 * sure that the caller is root in his uid, and privileged
1152 * over the file's current owner.
1154 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
))
1157 if (!cgm_chmod_file(controller
, cgroup
, mode
))
1162 int cg_mkdir(const char *path
, mode_t mode
)
1164 struct fuse_context
*fc
= fuse_get_context();
1165 nih_local
struct cgm_keys
**list
= NULL
;
1166 char *fpath
= NULL
, *path1
;
1167 nih_local
char * cgdir
= NULL
;
1169 nih_local
char *controller
= NULL
;
1175 controller
= pick_controller_from_path(fc
, path
);
1179 cgroup
= find_cgroup_in_path(path
);
1183 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1189 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
))
1193 if (!cgm_create(controller
, cgroup
, fc
->uid
, fc
->gid
))
1199 static int cg_rmdir(const char *path
)
1201 struct fuse_context
*fc
= fuse_get_context();
1202 nih_local
struct cgm_keys
**list
= NULL
;
1204 nih_local
char * cgdir
= NULL
;
1206 nih_local
char *controller
= NULL
;
1212 controller
= pick_controller_from_path(fc
, path
);
1216 cgroup
= find_cgroup_in_path(path
);
1220 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1224 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
))
1227 if (!cgm_remove(controller
, cgroup
))
1233 static bool startswith(const char *line
, const char *pref
)
1235 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1240 static void get_mem_cached(char *memstat
, unsigned long *v
)
1246 if (startswith(memstat
, "total_cache")) {
1247 sscanf(memstat
+ 11, "%lu", v
);
1251 eol
= strchr(memstat
, '\n');
1258 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1260 nih_local
char *fnam
= NULL
;
1262 char *answer
= NULL
;
1266 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
1267 if (!(f
= fopen(fnam
, "r")))
1270 while (getline(&line
, &len
, f
) != -1) {
1274 c1
= strchr(line
, ':');
1278 c2
= strchr(c1
, ':');
1282 if (strcmp(c1
, contrl
) != 0)
1286 answer
= NIH_MUST( nih_strdup(NULL
, c2
) );
1297 * FUSE ops for /proc
1300 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1301 struct fuse_file_info
*fi
)
1303 struct fuse_context
*fc
= fuse_get_context();
1304 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "memory");
1305 nih_local
char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1306 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1308 size_t linelen
= 0, total_len
= 0;
1317 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1319 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1321 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1323 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1324 memusage
= strtoul(memusage_str
, NULL
, 10);
1327 get_mem_cached(memstat_str
, &cached
);
1329 f
= fopen("/proc/meminfo", "r");
1333 while (getline(&line
, &linelen
, f
) != -1) {
1335 char *printme
, lbuf
[100];
1337 memset(lbuf
, 0, 100);
1338 if (startswith(line
, "MemTotal:")) {
1339 sscanf(line
+14, "%lu", &hosttotal
);
1340 if (hosttotal
< memlimit
)
1341 memlimit
= hosttotal
;
1342 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1344 } else if (startswith(line
, "MemFree:")) {
1345 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1347 } else if (startswith(line
, "MemAvailable:")) {
1348 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1350 } else if (startswith(line
, "Buffers:")) {
1351 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1353 } else if (startswith(line
, "Cached:")) {
1354 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1356 } else if (startswith(line
, "SwapCached:")) {
1357 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1361 l
= snprintf(buf
, size
, "%s", printme
);
1371 * Read the cpuset.cpus for cg
1372 * Return the answer in a nih_alloced string
1374 static char *get_cpuset(const char *cg
)
1378 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1384 * Helper functions for cpuset_in-set
1386 char *cpuset_nexttok(const char *c
)
1388 char *r
= strchr(c
+1, ',');
1394 int cpuset_getrange(const char *c
, int *a
, int *b
)
1398 ret
= sscanf(c
, "%d-%d", a
, b
);
1403 * cpusets are in format "1,2-3,4"
1404 * iow, comma-delimited ranges
1406 static bool cpu_in_cpuset(int cpu
, const char *cpuset
)
1410 for (c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
1413 ret
= cpuset_getrange(c
, &a
, &b
);
1414 if (ret
== 1 && cpu
== a
)
1416 if (ret
!= 2) // bad cpuset!
1418 if (cpu
>= a
&& cpu
<= b
)
1425 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1429 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1431 return cpu_in_cpuset(cpu
, cpuset
);
1435 * check whether this is a '^processor" line in /proc/cpuinfo
1437 static bool is_processor_line(const char *line
)
1441 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1446 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1447 struct fuse_file_info
*fi
)
1449 struct fuse_context
*fc
= fuse_get_context();
1450 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1451 nih_local
char *cpuset
= NULL
;
1453 size_t linelen
= 0, total_len
= 0;
1454 bool am_printing
= false;
1464 cpuset
= get_cpuset(cg
);
1468 f
= fopen("/proc/cpuinfo", "r");
1472 while (getline(&line
, &linelen
, f
) != -1) {
1474 if (is_processor_line(line
)) {
1475 am_printing
= cpuline_in_cpuset(line
, cpuset
);
1478 l
= snprintf(buf
, size
, "processor : %d\n", curcpu
);
1486 l
= snprintf(buf
, size
, "%s", line
);
1496 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
1497 struct fuse_file_info
*fi
)
1499 struct fuse_context
*fc
= fuse_get_context();
1500 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1501 nih_local
char *cpuset
= NULL
;
1503 size_t linelen
= 0, total_len
= 0;
1513 cpuset
= get_cpuset(cg
);
1517 f
= fopen("/proc/stat", "r");
1521 while (getline(&line
, &linelen
, f
) != -1) {
1526 if (sscanf(line
, "cpu%d", &cpu
) != 1) {
1527 /* not a ^cpu line, just print it */
1528 l
= snprintf(buf
, size
, "%s", line
);
1534 if (!cpu_in_cpuset(cpu
, cpuset
))
1538 c
= strchr(line
, ' ');
1541 l
= snprintf(buf
, size
, "cpu%d %s", curcpu
, c
);
1551 * How to guess what to present for uptime?
1552 * One thing we could do would be to take the date on the caller's
1553 * memory.usage_in_bytes file, which should equal the time of creation
1554 * of his cgroup. However, a task could be in a sub-cgroup of the
1555 * container. The same problem exists if we try to look at the ages
1556 * of processes in the caller's cgroup.
1558 * So we'll fork a task that will enter the caller's pidns, mount a
1559 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
1561 * For the second uptime #, we'll do as Stéphane had done, just copy
1562 * the number from /proc/uptime. Not sure how to best emulate 'idle'
1563 * time. Maybe someone can come up with a good algorithm and submit a
1564 * patch. Maybe something based on cpushare info?
1567 /* return age of the reaper for $pid, taken from ctime of its procdir */
1568 static long int get_pid1_time(pid_t pid
)
1576 if (unshare(CLONE_NEWNS
))
1579 sprintf(fnam
, "/proc/%d/ns/pid", pid
);
1580 fd
= open(fnam
, O_RDONLY
);
1582 perror("get_pid1_time open of ns/pid");
1586 perror("get_pid1_time setns 1");
1596 // child will do the writing for us
1601 umount2("/proc", MNT_DETACH
);
1603 if (mount("proc", "/proc", "proc", 0, NULL
)) {
1604 perror("get_pid1_time mount");
1607 ret
= lstat("/proc/1", &sb
);
1609 perror("get_pid1_time lstat");
1612 return time(NULL
) - sb
.st_ctime
;
1615 static long int getreaperage(pid_t qpid
)
1617 int pid
, mypipe
[2], ret
;
1620 long int mtime
, answer
= 0;
1628 if (!pid
) { // child
1629 mtime
= get_pid1_time(qpid
);
1630 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
1631 fprintf(stderr
, "Warning: bad write from getreaperage\n");
1637 FD_SET(mypipe
[0], &s
);
1640 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1646 printf("timed out\n");
1649 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
1661 static long int getprocidle(void)
1663 FILE *f
= fopen("/proc/uptime", "r");
1667 if (fscanf(f
, "%ld %ld", &age
, &idle
) != 2)
1673 * We read /proc/uptime and reuse its second field.
1674 * For the first field, we use the mtime for the reaper for
1675 * the calling pid as returned by getreaperage
1677 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
1678 struct fuse_file_info
*fi
)
1680 struct fuse_context
*fc
= fuse_get_context();
1681 long int reaperage
= getreaperage(fc
->pid
);;
1682 long int idletime
= getprocidle();
1686 return snprintf(buf
, size
, "%ld %ld\n", reaperage
, idletime
);
1689 static off_t
get_procfile_size(const char *which
)
1691 FILE *f
= fopen(which
, "r");
1694 ssize_t sz
, answer
= 0;
1698 while ((sz
= getline(&line
, &len
, f
)) != -1)
1705 static int proc_getattr(const char *path
, struct stat
*sb
)
1707 struct timespec now
;
1709 memset(sb
, 0, sizeof(struct stat
));
1710 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
1712 sb
->st_uid
= sb
->st_gid
= 0;
1713 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
1714 if (strcmp(path
, "/proc") == 0) {
1715 sb
->st_mode
= S_IFDIR
| 00555;
1719 if (strcmp(path
, "/proc/meminfo") == 0 ||
1720 strcmp(path
, "/proc/cpuinfo") == 0 ||
1721 strcmp(path
, "/proc/uptime") == 0 ||
1722 strcmp(path
, "/proc/stat") == 0) {
1724 sb
->st_size
= get_procfile_size(path
);
1725 sb
->st_mode
= S_IFREG
| 00444;
1733 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1734 struct fuse_file_info
*fi
)
1736 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
1737 filler(buf
, "meminfo", NULL
, 0) != 0 ||
1738 filler(buf
, "stat", NULL
, 0) != 0 ||
1739 filler(buf
, "uptime", NULL
, 0) != 0)
1744 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
1746 if (strcmp(path
, "/proc/meminfo") == 0 ||
1747 strcmp(path
, "/proc/cpuinfo") == 0 ||
1748 strcmp(path
, "/proc/uptime") == 0 ||
1749 strcmp(path
, "/proc/stat") == 0)
1754 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1755 struct fuse_file_info
*fi
)
1757 if (strcmp(path
, "/proc/meminfo") == 0)
1758 return proc_meminfo_read(buf
, size
, offset
, fi
);
1759 if (strcmp(path
, "/proc/cpuinfo") == 0)
1760 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
1761 if (strcmp(path
, "/proc/uptime") == 0)
1762 return proc_uptime_read(buf
, size
, offset
, fi
);
1763 if (strcmp(path
, "/proc/stat") == 0)
1764 return proc_stat_read(buf
, size
, offset
, fi
);
1770 * these just delegate to the /proc and /cgroup ops as
1774 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
1776 if (strcmp(path
, "/") == 0) {
1777 sb
->st_mode
= S_IFDIR
| 00755;
1781 if (strncmp(path
, "/cgroup", 7) == 0) {
1782 return cg_getattr(path
, sb
);
1784 if (strncmp(path
, "/proc", 5) == 0) {
1785 return proc_getattr(path
, sb
);
1790 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
1792 if (strcmp(path
, "/") == 0)
1795 if (strncmp(path
, "/cgroup", 7) == 0) {
1796 return cg_opendir(path
, fi
);
1798 if (strcmp(path
, "/proc") == 0)
1803 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
1804 struct fuse_file_info
*fi
)
1806 if (strcmp(path
, "/") == 0) {
1807 if (filler(buf
, "proc", NULL
, 0) != 0 ||
1808 filler(buf
, "cgroup", NULL
, 0) != 0)
1812 if (strncmp(path
, "/cgroup", 7) == 0)
1813 return cg_readdir(path
, buf
, filler
, offset
, fi
);
1814 if (strcmp(path
, "/proc") == 0)
1815 return proc_readdir(path
, buf
, filler
, offset
, fi
);
1819 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
1821 if (strcmp(path
, "/") == 0)
1823 if (strncmp(path
, "/cgroup", 7) == 0) {
1824 return cg_releasedir(path
, fi
);
1826 if (strcmp(path
, "/proc") == 0)
1831 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
1833 if (strncmp(path
, "/cgroup", 7) == 0)
1834 return cg_open(path
, fi
);
1835 if (strncmp(path
, "/proc", 5) == 0)
1836 return proc_open(path
, fi
);
1841 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1842 struct fuse_file_info
*fi
)
1844 if (strncmp(path
, "/cgroup", 7) == 0)
1845 return cg_read(path
, buf
, size
, offset
, fi
);
1846 if (strncmp(path
, "/proc", 5) == 0)
1847 return proc_read(path
, buf
, size
, offset
, fi
);
1852 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1853 struct fuse_file_info
*fi
)
1855 if (strncmp(path
, "/cgroup", 7) == 0) {
1856 return cg_write(path
, buf
, size
, offset
, fi
);
1862 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
1867 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
1872 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
1877 int lxcfs_mkdir(const char *path
, mode_t mode
)
1879 if (strncmp(path
, "/cgroup", 7) == 0)
1880 return cg_mkdir(path
, mode
);
1885 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
1887 if (strncmp(path
, "/cgroup", 7) == 0)
1888 return cg_chown(path
, uid
, gid
);
1894 * cat first does a truncate before doing ops->write. This doesn't
1895 * really make sense for cgroups. So just return 0 always but do
1898 int lxcfs_truncate(const char *path
, off_t newsize
)
1900 if (strncmp(path
, "/cgroup", 7) == 0)
1905 int lxcfs_rmdir(const char *path
)
1907 if (strncmp(path
, "/cgroup", 7) == 0)
1908 return cg_rmdir(path
);
1912 int lxcfs_chmod(const char *path
, mode_t mode
)
1914 if (strncmp(path
, "/cgroup", 7) == 0)
1915 return cg_chmod(path
, mode
);
1919 const struct fuse_operations lxcfs_ops
= {
1920 .getattr
= lxcfs_getattr
,
1924 .mkdir
= lxcfs_mkdir
,
1926 .rmdir
= lxcfs_rmdir
,
1930 .chmod
= lxcfs_chmod
,
1931 .chown
= lxcfs_chown
,
1932 .truncate
= lxcfs_truncate
,
1937 .release
= lxcfs_release
,
1938 .write
= lxcfs_write
,
1941 .flush
= lxcfs_flush
,
1942 .fsync
= lxcfs_fsync
,
1947 .removexattr
= NULL
,
1949 .opendir
= lxcfs_opendir
,
1950 .readdir
= lxcfs_readdir
,
1951 .releasedir
= lxcfs_releasedir
,
1962 static void usage(const char *me
)
1964 fprintf(stderr
, "Usage:\n");
1965 fprintf(stderr
, "\n");
1966 fprintf(stderr
, "%s [FUSE and mount options] mountpoint\n", me
);
1970 static bool is_help(char *w
)
1972 if (strcmp(w
, "-h") == 0 ||
1973 strcmp(w
, "--help") == 0 ||
1974 strcmp(w
, "-help") == 0 ||
1975 strcmp(w
, "help") == 0)
1980 int main(int argc
, char *argv
[])
1983 struct lxcfs_state
*d
;
1985 if (argc
< 2 || is_help(argv
[1]))
1988 d
= malloc(sizeof(*d
));
1992 if (!cgm_escape_cgroup())
1993 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
1995 if (!cgm_get_controllers(&d
->subsystems
))
1998 ret
= fuse_main(argc
, argv
, &lxcfs_ops
, d
);