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>
34 #include <nih/alloc.h>
36 #include "cgmanager.h"
40 * a null-terminated, nih-allocated list of the mounted subsystems. We
41 * detect this at startup.
45 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
51 char *buf
; // unused as of yet
55 static char *must_copy_string(const char *str
)
59 return NIH_MUST( nih_strdup(NULL
, str
) );
63 * TODO - return value should denote whether child exited with failure
64 * so callers can return errors. Esp read/write of tasks and cgroup.procs
66 static int wait_for_pid(pid_t pid
)
71 ret
= waitpid(pid
, &status
, 0);
79 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
85 * Given a open file * to /proc/pid/{u,g}id_map, and an id
86 * valid in the caller's namespace, return the id mapped into
88 * Returns the mapped id, or -1 on error.
91 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
93 unsigned int nsuid
, // base id for a range in the idfile's namespace
94 hostuid
, // base id for a range in the caller's namespace
95 count
; // number of ids in this range
99 fseek(idfile
, 0L, SEEK_SET
);
100 while (fgets(line
, 400, idfile
)) {
101 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
104 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
106 * uids wrapped around - unexpected as this is a procfile,
109 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s\n",
110 nsuid
, hostuid
, count
, line
);
113 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
115 * now since hostuid <= in_id < hostuid+count, and
116 * hostuid+count and nsuid+count do not wrap around,
117 * we know that nsuid+(in_id-hostuid) which must be
118 * less that nsuid+(count) must not wrap around
120 return (in_id
- hostuid
) + nsuid
;
129 * for is_privileged_over,
130 * specify whether we require the calling uid to be root in his
133 #define NS_ROOT_REQD true
134 #define NS_ROOT_OPT false
136 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
138 nih_local
char *fpath
= NULL
;
142 if (victim
== -1 || uid
== -1)
146 * If the request is one not requiring root in the namespace,
147 * then having the same uid suffices. (i.e. uid 1000 has write
148 * access to files owned by uid 1000
150 if (!req_ns_root
&& uid
== victim
)
153 fpath
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/uid_map", pid
) );
154 FILE *f
= fopen(fpath
, "r");
158 /* if caller's not root in his namespace, reject */
159 nsuid
= convert_id_to_ns(f
, uid
);
164 * If victim is not mapped into caller's ns, reject.
165 * XXX I'm not sure this check is needed given that fuse
166 * will be sending requests where the vfs has converted
168 nsuid
= convert_id_to_ns(f
, victim
);
179 static bool perms_include(int fmode
, mode_t req_mode
)
183 switch (req_mode
& O_ACCMODE
) {
191 r
= S_IROTH
| S_IWOTH
;
196 return ((fmode
& r
) == r
);
199 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
203 if (strlen(taskcg
) <= strlen(querycg
)) {
204 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
208 if (strcmp(querycg
, "/") == 0)
209 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ 1) );
211 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ strlen(querycg
) + 1) );
212 end
= strchr(start
, '/');
219 * check whether a fuse context may access a cgroup dir or file
221 * If file is not null, it is a cgroup file to check under cg.
222 * If file is null, then we are checking perms on cg itself.
224 * For files we can check the mode of the list_keys result.
225 * For cgroups, we must make assumptions based on the files under the
226 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
229 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
231 nih_local
struct cgm_keys
**list
= NULL
;
240 if (!cgm_list_keys(contrl
, cg
, &list
))
242 for (i
= 0; list
[i
]; i
++) {
243 if (strcmp(list
[i
]->name
, file
) == 0) {
244 struct cgm_keys
*k
= list
[i
];
245 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
246 if (perms_include(k
->mode
>> 6, mode
))
249 if (fc
->gid
== k
->gid
) {
250 if (perms_include(k
->mode
>> 3, mode
))
253 return perms_include(k
->mode
, mode
);
260 static void stripnewline(char *x
)
262 size_t l
= strlen(x
);
263 if (l
&& x
[l
-1] == '\n')
268 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
269 * If caller is in /a, he may act on /a/b, but not on /b.
270 * if the answer is false and nextcg is not NULL, then *nextcg will point
271 * to a nih_alloc'd string containing the next cgroup directory under cg
273 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
275 nih_local
char *fnam
= NULL
;
281 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
282 if (!(f
= fopen(fnam
, "r")))
285 while (getline(&line
, &len
, f
) != -1) {
286 char *c1
, *c2
, *linecmp
;
289 c1
= strchr(line
, ':');
293 c2
= strchr(c1
, ':');
297 if (strcmp(c1
, contrl
) != 0)
302 * callers pass in '/' for root cgroup, otherwise they pass
303 * in a cgroup without leading '/'
305 linecmp
= *cg
== '/' ? c2
: c2
+1;
306 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
308 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
322 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
323 * and needs to be nih_freed.
325 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
330 if (strlen(path
) < 9)
333 ret
= nih_strdup(NULL
, p1
);
336 slash
= strstr(ret
, "/");
340 /* verify that it is a subsystem */
341 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
347 for (i
= 0; list
[i
]; i
++) {
348 if (strcmp(list
[i
], ret
) == 0)
356 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
357 * Note that the returned value may include files (keynames) etc
359 static const char *find_cgroup_in_path(const char *path
)
363 if (strlen(path
) < 9)
365 p1
= strstr(path
+8, "/");
371 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
373 nih_local
char **list
= NULL
;
381 if (!cgm_list_children(contr
, dir
, &list
))
383 for (i
= 0; list
[i
]; i
++) {
384 if (strcmp(list
[i
], f
) == 0)
391 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
393 nih_local
struct cgm_keys
**list
= NULL
;
401 if (!cgm_list_keys(contr
, dir
, &list
))
403 for (i
= 0; list
[i
]; i
++) {
404 if (strcmp(list
[i
]->name
, f
) == 0) {
405 k
= NIH_MUST( nih_alloc(NULL
, (sizeof(*k
))) );
406 k
->name
= NIH_MUST( nih_strdup(k
, list
[i
]->name
) );
407 k
->uid
= list
[i
]->uid
;
408 k
->gid
= list
[i
]->gid
;
409 k
->mode
= list
[i
]->mode
;
417 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
421 *dir
= NIH_MUST( nih_strdup(NULL
, cg
) );
422 *file
= strrchr(cg
, '/');
427 p
= strrchr(*dir
, '/');
431 static size_t get_file_size(const char *contrl
, const char *cg
, const char *f
)
433 nih_local
char *data
= NULL
;
435 if (!cgm_get_value(contrl
, cg
, f
, &data
))
442 * FUSE ops for /cgroup
445 static int cg_getattr(const char *path
, struct stat
*sb
)
448 struct fuse_context
*fc
= fuse_get_context();
449 nih_local
char * cgdir
= NULL
;
450 char *fpath
= NULL
, *path1
, *path2
;
451 nih_local
struct cgm_keys
*k
= NULL
;
453 nih_local
char *controller
= NULL
;
459 memset(sb
, 0, sizeof(struct stat
));
461 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
464 sb
->st_uid
= sb
->st_gid
= 0;
465 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
468 if (strcmp(path
, "/cgroup") == 0) {
469 sb
->st_mode
= S_IFDIR
| 00755;
474 controller
= pick_controller_from_path(fc
, path
);
477 cgroup
= find_cgroup_in_path(path
);
479 /* this is just /cgroup/controller, return it as a dir */
480 sb
->st_mode
= S_IFDIR
| 00755;
485 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
495 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
496 * Then check that caller's cgroup is under path if fpath is a child
497 * cgroup, or cgdir if fpath is a file */
499 if (is_child_cgroup(controller
, path1
, path2
)) {
500 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
501 /* this is just /cgroup/controller, return it as a dir */
502 sb
->st_mode
= S_IFDIR
| 00555;
506 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
509 // get uid, gid, from '/tasks' file and make up a mode
510 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
511 sb
->st_mode
= S_IFDIR
| 00755;
512 k
= get_cgroup_key(controller
, cgroup
, "tasks");
514 sb
->st_uid
= sb
->st_gid
= 0;
523 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
524 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
))
526 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
529 sb
->st_mode
= S_IFREG
| k
->mode
;
533 sb
->st_size
= get_file_size(controller
, path1
, path2
);
541 * TODO - cache these results in a table for use in opendir, free
544 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
546 struct fuse_context
*fc
= fuse_get_context();
547 nih_local
struct cgm_keys
**list
= NULL
;
549 struct file_info
*dir_info
;
550 nih_local
char *controller
= NULL
;
555 if (strcmp(path
, "/cgroup") == 0) {
559 // return list of keys for the controller, and list of child cgroups
560 controller
= pick_controller_from_path(fc
, path
);
564 cgroup
= find_cgroup_in_path(path
);
566 /* this is just /cgroup/controller, return its contents */
571 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
574 /* we'll free this at cg_releasedir */
575 dir_info
= NIH_MUST( nih_alloc(NULL
, sizeof(*dir_info
)) );
576 dir_info
->controller
= must_copy_string(controller
);
577 dir_info
->cgroup
= must_copy_string(cgroup
);
578 dir_info
->isdir
= true;
579 dir_info
->buf
= NULL
;
580 dir_info
->buflen
= 0;
582 fi
->fh
= (unsigned long)dir_info
;
586 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
587 struct fuse_file_info
*fi
)
589 struct file_info
*d
= (struct file_info
*)fi
->fh
;
590 nih_local
struct cgm_keys
**list
= NULL
;
592 nih_local
char *nextcg
= NULL
;
593 struct fuse_context
*fc
= fuse_get_context();
595 if (!d
->cgroup
&& !d
->controller
) {
596 // ls /var/lib/lxcfs/cgroup - just show list of controllers
597 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
603 for (i
= 0; list
[i
]; i
++) {
604 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
611 if (!cgm_list_keys(d
->controller
, d
->cgroup
, &list
))
612 // not a valid cgroup
615 if (!caller_is_in_ancestor(fc
->pid
, d
->controller
, d
->cgroup
, &nextcg
)) {
618 ret
= filler(buf
, nextcg
, NULL
, 0);
625 for (i
= 0; list
[i
]; i
++) {
626 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
631 // now get the list of child cgroups
632 nih_local
char **clist
= NULL
;
634 if (!cgm_list_children(d
->controller
, d
->cgroup
, &clist
))
636 for (i
= 0; clist
[i
]; i
++) {
637 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
644 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
646 struct file_info
*d
= (struct file_info
*)fi
->fh
;
649 nih_free(d
->controller
);
658 * TODO - cache info here for read/write, release in cg_release.
660 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
662 nih_local
char *controller
= NULL
;
664 char *fpath
= NULL
, *path1
, *path2
;
665 nih_local
char * cgdir
= NULL
;
666 nih_local
struct cgm_keys
*k
= NULL
;
667 struct fuse_context
*fc
= fuse_get_context();
672 controller
= pick_controller_from_path(fc
, path
);
675 cgroup
= find_cgroup_in_path(path
);
679 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
688 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
689 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
))
690 // should never get here
699 static int msgrecv(int sockfd
, void *buf
, size_t len
)
705 FD_SET(sockfd
, &rfds
);
709 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) <= 0)
711 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
714 #define SEND_CREDS_OK 0
715 #define SEND_CREDS_NOTSK 1
716 #define SEND_CREDS_FAIL 2
717 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
719 struct msghdr msg
= { 0 };
721 struct cmsghdr
*cmsg
;
722 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
727 if (msgrecv(sock
, buf
, 1) != 1) {
728 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
730 return SEND_CREDS_FAIL
;
734 msg
.msg_control
= cmsgbuf
;
735 msg
.msg_controllen
= sizeof(cmsgbuf
);
737 cmsg
= CMSG_FIRSTHDR(&msg
);
738 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
739 cmsg
->cmsg_level
= SOL_SOCKET
;
740 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
741 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
748 iov
.iov_len
= sizeof(buf
);
752 if (sendmsg(sock
, &msg
, 0) < 0) {
753 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
756 return SEND_CREDS_NOTSK
;
757 return SEND_CREDS_FAIL
;
760 return SEND_CREDS_OK
;
763 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
765 struct msghdr msg
= { 0 };
767 struct cmsghdr
*cmsg
;
768 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
781 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
782 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
786 if (write(sock
, buf
, 1) != 1) {
787 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
793 msg
.msg_control
= cmsgbuf
;
794 msg
.msg_controllen
= sizeof(cmsgbuf
);
797 iov
.iov_len
= sizeof(buf
);
805 if (select(sock
+1, &rfds
, NULL
, NULL
, &tv
) <= 0) {
806 fprintf(stderr
, "Failed to select for scm_cred: %s\n",
810 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
812 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
817 cmsg
= CMSG_FIRSTHDR(&msg
);
819 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
820 cmsg
->cmsg_level
== SOL_SOCKET
&&
821 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
822 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
831 * pid_to_ns - reads pids from a ucred over a socket, then writes the
832 * int value back over the socket. This shifts the pid from the
833 * sender's pidns into tpid's pidns.
835 static void pid_to_ns(int sock
, pid_t tpid
)
840 while (recv_creds(sock
, &cred
, &v
)) {
843 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
850 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
851 * in your old pidns. Only children which you fork will be in the target
852 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
853 * actually convert pids
855 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
857 int newnsfd
= -1, ret
, cpipe
[2];
864 sprintf(fnam
, "/proc/%d/ns/pid", tpid
);
865 newnsfd
= open(fnam
, O_RDONLY
);
868 if (setns(newnsfd
, 0) < 0)
883 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
884 fprintf(stderr
, "%s (child): erorr on write: %s\n",
885 __func__
, strerror(errno
));
888 pid_to_ns(sock
, tpid
);
890 // give the child 1 second to be done forking and
893 FD_SET(cpipe
[0], &s
);
896 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
899 ret
= read(cpipe
[0], &v
, 1);
900 if (ret
!= sizeof(char) || v
!= '1') {
904 if (!wait_for_pid(cpid
))
915 * To read cgroup files with a particular pid, we will setns into the child
916 * pidns, open a pipe, fork a child - which will be the first to really be in
917 * the child ns - which does the cgm_get_value and writes the data to the pipe.
919 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
921 int sock
[2] = {-1, -1};
922 nih_local
char *tmpdata
= NULL
;
924 pid_t qpid
, cpid
= -1;
931 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
935 * Now we read the pids from returned data one by one, pass
936 * them into a child in the target namespace, read back the
937 * translated pids, and put them into our to-return data
940 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
941 perror("socketpair");
950 pid_to_ns_wrapper(sock
[1], tpid
);
955 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
957 ret
= send_creds(sock
[0], &cred
, v
, true);
959 if (ret
== SEND_CREDS_NOTSK
)
961 if (ret
== SEND_CREDS_FAIL
)
964 // read converted results
969 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
971 fprintf(stderr
, "%s: select error waiting for pid from child: %s\n",
972 __func__
, strerror(errno
));
975 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
976 fprintf(stderr
, "%s: error reading pid from child: %s\n",
977 __func__
, strerror(errno
));
980 NIH_MUST( nih_strcat_sprintf(d
, NULL
, "%d\n", qpid
) );
982 ptr
= strchr(ptr
, '\n');
990 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
991 // failed to ask child to exit
992 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
993 __func__
, strerror(errno
));
1002 if (sock
[0] != -1) {
1009 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1010 struct fuse_file_info
*fi
)
1012 nih_local
char *controller
= NULL
;
1014 char *fpath
= NULL
, *path1
, *path2
;
1015 struct fuse_context
*fc
= fuse_get_context();
1016 nih_local
char * cgdir
= NULL
;
1017 nih_local
struct cgm_keys
*k
= NULL
;
1025 controller
= pick_controller_from_path(fc
, path
);
1028 cgroup
= find_cgroup_in_path(path
);
1032 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1041 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
1042 nih_local
char *data
= NULL
;
1046 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
1047 // should never get here
1050 if (strcmp(path2
, "tasks") == 0 ||
1051 strcmp(path2
, "/tasks") == 0 ||
1052 strcmp(path2
, "/cgroup.procs") == 0 ||
1053 strcmp(path2
, "cgroup.procs") == 0)
1054 // special case - we have to translate the pids
1055 r
= do_read_pids(fc
->pid
, controller
, path1
, path2
, &data
);
1057 r
= cgm_get_value(controller
, path1
, path2
, &data
);
1067 memcpy(buf
, data
, s
);
1075 static void pid_from_ns(int sock
, pid_t tpid
)
1091 ret
= select(sock
+1, &s
, NULL
, NULL
, &tv
);
1093 fprintf(stderr
, "%s: bad select before read from parent: %s\n",
1094 __func__
, strerror(errno
));
1097 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1098 fprintf(stderr
, "%s: bad read from parent: %s\n",
1099 __func__
, strerror(errno
));
1102 if (vpid
== -1) // done
1106 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1108 cred
.pid
= getpid();
1109 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1116 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1118 int newnsfd
= -1, ret
, cpipe
[2];
1125 sprintf(fnam
, "/proc/%d/ns/pid", tpid
);
1126 newnsfd
= open(fnam
, O_RDONLY
);
1129 if (setns(newnsfd
, 0) < 0)
1133 if (pipe(cpipe
) < 0)
1145 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1146 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1147 __func__
, strerror(errno
));
1150 pid_from_ns(sock
, tpid
);
1153 // give the child 1 second to be done forking and
1156 FD_SET(cpipe
[0], &s
);
1159 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1162 ret
= read(cpipe
[0], &v
, 1);
1163 if (ret
!= sizeof(char) || v
!= '1') {
1167 if (!wait_for_pid(cpid
))
1172 kill(cpid
, SIGKILL
);
1177 static bool do_write_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, const char *buf
)
1179 int sock
[2] = {-1, -1};
1180 pid_t qpid
, cpid
= -1;
1181 bool answer
= false, fail
= false;
1184 * write the pids to a socket, have helper in writer's pidns
1185 * call movepid for us
1187 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1188 perror("socketpair");
1197 pid_from_ns_wrapper(sock
[1], tpid
);
1199 const char *ptr
= buf
;
1200 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1204 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1205 fprintf(stderr
, "%s: error writing pid to child: %s\n",
1206 __func__
, strerror(errno
));
1210 if (recv_creds(sock
[0], &cred
, &v
)) {
1212 if (!cgm_move_pid(contrl
, cg
, cred
.pid
))
1217 ptr
= strchr(ptr
, '\n');
1223 /* All good, write the value */
1225 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1226 fprintf(stderr
, "Warning: failed to ask child to exit\n");
1234 if (sock
[0] != -1) {
1241 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1242 struct fuse_file_info
*fi
)
1244 nih_local
char *controller
= NULL
;
1246 char *fpath
= NULL
, *path1
, *path2
;
1247 struct fuse_context
*fc
= fuse_get_context();
1248 nih_local
char * cgdir
= NULL
;
1249 nih_local
struct cgm_keys
*k
= NULL
;
1250 nih_local
char *localbuf
= NULL
;
1258 localbuf
= NIH_MUST( nih_alloc(NULL
, size
+1) );
1259 localbuf
[size
] = '\0';
1260 memcpy(localbuf
, buf
, size
);
1261 controller
= pick_controller_from_path(fc
, path
);
1264 cgroup
= find_cgroup_in_path(path
);
1268 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1277 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
1280 if (!fc_may_access(fc
, controller
, path1
, path2
, O_WRONLY
))
1283 if (strcmp(path2
, "tasks") == 0 ||
1284 strcmp(path2
, "/tasks") == 0 ||
1285 strcmp(path2
, "/cgroup.procs") == 0 ||
1286 strcmp(path2
, "cgroup.procs") == 0)
1287 // special case - we have to translate the pids
1288 r
= do_write_pids(fc
->pid
, controller
, path1
, path2
, localbuf
);
1290 r
= cgm_set_value(controller
, path1
, path2
, localbuf
);
1301 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1303 struct fuse_context
*fc
= fuse_get_context();
1304 nih_local
char * cgdir
= NULL
;
1305 char *fpath
= NULL
, *path1
, *path2
;
1306 nih_local
struct cgm_keys
*k
= NULL
;
1308 nih_local
char *controller
= NULL
;
1314 if (strcmp(path
, "/cgroup") == 0)
1317 controller
= pick_controller_from_path(fc
, path
);
1320 cgroup
= find_cgroup_in_path(path
);
1322 /* this is just /cgroup/controller */
1325 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1335 if (is_child_cgroup(controller
, path1
, path2
)) {
1336 // get uid, gid, from '/tasks' file and make up a mode
1337 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1338 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1341 k
= get_cgroup_key(controller
, path1
, path2
);
1347 * This being a fuse request, the uid and gid must be valid
1348 * in the caller's namespace. So we can just check to make
1349 * sure that the caller is root in his uid, and privileged
1350 * over the file's current owner.
1352 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
))
1355 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
))
1360 int cg_chmod(const char *path
, mode_t mode
)
1362 struct fuse_context
*fc
= fuse_get_context();
1363 nih_local
char * cgdir
= NULL
;
1364 char *fpath
= NULL
, *path1
, *path2
;
1365 nih_local
struct cgm_keys
*k
= NULL
;
1367 nih_local
char *controller
= NULL
;
1372 if (strcmp(path
, "/cgroup") == 0)
1375 controller
= pick_controller_from_path(fc
, path
);
1378 cgroup
= find_cgroup_in_path(path
);
1380 /* this is just /cgroup/controller */
1383 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1393 if (is_child_cgroup(controller
, path1
, path2
)) {
1394 // get uid, gid, from '/tasks' file and make up a mode
1395 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1396 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1399 k
= get_cgroup_key(controller
, path1
, path2
);
1405 * This being a fuse request, the uid and gid must be valid
1406 * in the caller's namespace. So we can just check to make
1407 * sure that the caller is root in his uid, and privileged
1408 * over the file's current owner.
1410 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
))
1413 if (!cgm_chmod_file(controller
, cgroup
, mode
))
1418 int cg_mkdir(const char *path
, mode_t mode
)
1420 struct fuse_context
*fc
= fuse_get_context();
1421 nih_local
struct cgm_keys
**list
= NULL
;
1422 char *fpath
= NULL
, *path1
;
1423 nih_local
char * cgdir
= NULL
;
1425 nih_local
char *controller
= NULL
;
1431 controller
= pick_controller_from_path(fc
, path
);
1435 cgroup
= find_cgroup_in_path(path
);
1439 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1445 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
))
1449 if (!cgm_create(controller
, cgroup
, fc
->uid
, fc
->gid
))
1455 static int cg_rmdir(const char *path
)
1457 struct fuse_context
*fc
= fuse_get_context();
1458 nih_local
struct cgm_keys
**list
= NULL
;
1460 nih_local
char * cgdir
= NULL
;
1462 nih_local
char *controller
= NULL
;
1468 controller
= pick_controller_from_path(fc
, path
);
1472 cgroup
= find_cgroup_in_path(path
);
1476 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1480 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
))
1483 if (!cgm_remove(controller
, cgroup
))
1489 static bool startswith(const char *line
, const char *pref
)
1491 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1496 static void get_mem_cached(char *memstat
, unsigned long *v
)
1502 if (startswith(memstat
, "total_cache")) {
1503 sscanf(memstat
+ 11, "%lu", v
);
1507 eol
= strchr(memstat
, '\n');
1514 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
1520 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
1522 size_t len
= strlen(key
);
1526 if (startswith(str
, key
)) {
1527 sscanf(str
+ len
, "%lu", v
);
1530 eol
= strchr(str
, '\n');
1537 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1539 nih_local
char *fnam
= NULL
;
1541 char *answer
= NULL
;
1545 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
1546 if (!(f
= fopen(fnam
, "r")))
1549 while (getline(&line
, &len
, f
) != -1) {
1553 c1
= strchr(line
, ':');
1557 c2
= strchr(c1
, ':');
1561 if (strcmp(c1
, contrl
) != 0)
1565 answer
= NIH_MUST( nih_strdup(NULL
, c2
) );
1576 * FUSE ops for /proc
1579 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1580 struct fuse_file_info
*fi
)
1582 struct fuse_context
*fc
= fuse_get_context();
1583 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "memory");
1584 nih_local
char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1585 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1587 size_t linelen
= 0, total_len
= 0;
1596 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1598 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1600 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1602 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1603 memusage
= strtoul(memusage_str
, NULL
, 10);
1606 get_mem_cached(memstat_str
, &cached
);
1608 f
= fopen("/proc/meminfo", "r");
1612 while (getline(&line
, &linelen
, f
) != -1) {
1614 char *printme
, lbuf
[100];
1616 memset(lbuf
, 0, 100);
1617 if (startswith(line
, "MemTotal:")) {
1618 sscanf(line
+14, "%lu", &hosttotal
);
1619 if (hosttotal
< memlimit
)
1620 memlimit
= hosttotal
;
1621 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1623 } else if (startswith(line
, "MemFree:")) {
1624 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1626 } else if (startswith(line
, "MemAvailable:")) {
1627 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1629 } else if (startswith(line
, "Buffers:")) {
1630 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1632 } else if (startswith(line
, "Cached:")) {
1633 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1635 } else if (startswith(line
, "SwapCached:")) {
1636 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1640 l
= snprintf(buf
, size
, "%s", printme
);
1652 * Read the cpuset.cpus for cg
1653 * Return the answer in a nih_alloced string
1655 static char *get_cpuset(const char *cg
)
1659 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1665 * Helper functions for cpuset_in-set
1667 char *cpuset_nexttok(const char *c
)
1669 char *r
= strchr(c
+1, ',');
1675 int cpuset_getrange(const char *c
, int *a
, int *b
)
1679 ret
= sscanf(c
, "%d-%d", a
, b
);
1684 * cpusets are in format "1,2-3,4"
1685 * iow, comma-delimited ranges
1687 static bool cpu_in_cpuset(int cpu
, const char *cpuset
)
1691 for (c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
1694 ret
= cpuset_getrange(c
, &a
, &b
);
1695 if (ret
== 1 && cpu
== a
)
1697 if (ret
!= 2) // bad cpuset!
1699 if (cpu
>= a
&& cpu
<= b
)
1706 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1710 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1712 return cpu_in_cpuset(cpu
, cpuset
);
1716 * check whether this is a '^processor" line in /proc/cpuinfo
1718 static bool is_processor_line(const char *line
)
1722 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1727 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1728 struct fuse_file_info
*fi
)
1730 struct fuse_context
*fc
= fuse_get_context();
1731 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1732 nih_local
char *cpuset
= NULL
;
1734 size_t linelen
= 0, total_len
= 0;
1735 bool am_printing
= false;
1745 cpuset
= get_cpuset(cg
);
1749 f
= fopen("/proc/cpuinfo", "r");
1753 while (getline(&line
, &linelen
, f
) != -1) {
1755 if (is_processor_line(line
)) {
1756 am_printing
= cpuline_in_cpuset(line
, cpuset
);
1759 l
= snprintf(buf
, size
, "processor : %d\n", curcpu
);
1767 l
= snprintf(buf
, size
, "%s", line
);
1779 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
1780 struct fuse_file_info
*fi
)
1782 struct fuse_context
*fc
= fuse_get_context();
1783 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1784 nih_local
char *cpuset
= NULL
;
1786 size_t linelen
= 0, total_len
= 0;
1787 int curcpu
= -1; /* cpu numbering starts at 0 */
1796 cpuset
= get_cpuset(cg
);
1800 f
= fopen("/proc/stat", "r");
1804 while (getline(&line
, &linelen
, f
) != -1) {
1807 char cpu_char
[10]; /* That's a lot of cores */
1810 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
1811 /* not a ^cpuN line containing a number N, just print it */
1812 l
= snprintf(buf
, size
, "%s", line
);
1819 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
1821 if (!cpu_in_cpuset(cpu
, cpuset
))
1825 c
= strchr(line
, ' ');
1828 l
= snprintf(buf
, size
, "cpu%d %s", curcpu
, c
);
1840 * How to guess what to present for uptime?
1841 * One thing we could do would be to take the date on the caller's
1842 * memory.usage_in_bytes file, which should equal the time of creation
1843 * of his cgroup. However, a task could be in a sub-cgroup of the
1844 * container. The same problem exists if we try to look at the ages
1845 * of processes in the caller's cgroup.
1847 * So we'll fork a task that will enter the caller's pidns, mount a
1848 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
1850 * For the second uptime #, we'll do as Stéphane had done, just copy
1851 * the number from /proc/uptime. Not sure how to best emulate 'idle'
1852 * time. Maybe someone can come up with a good algorithm and submit a
1853 * patch. Maybe something based on cpushare info?
1856 /* return age of the reaper for $pid, taken from ctime of its procdir */
1857 static long int get_pid1_time(pid_t pid
)
1860 int fd
, cpipe
[2], ret
;
1867 if (unshare(CLONE_NEWNS
))
1870 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
1871 perror("rslave mount failed");
1875 sprintf(fnam
, "/proc/%d/ns/pid", pid
);
1876 fd
= open(fnam
, O_RDONLY
);
1878 perror("get_pid1_time open of ns/pid");
1882 perror("get_pid1_time setns 1");
1888 if (pipe(cpipe
) < 0)
1899 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1900 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1901 __func__
, strerror(errno
));
1904 umount2("/proc", MNT_DETACH
);
1905 if (mount("proc", "/proc", "proc", 0, NULL
)) {
1906 perror("get_pid1_time mount");
1909 ret
= lstat("/proc/1", &sb
);
1911 perror("get_pid1_time lstat");
1914 return time(NULL
) - sb
.st_ctime
;
1917 // give the child 1 second to be done forking and
1920 FD_SET(cpipe
[0], &s
);
1923 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1926 ret
= read(cpipe
[0], &v
, 1);
1927 if (ret
!= sizeof(char) || v
!= '1') {
1935 kill(cpid
, SIGKILL
);
1940 static long int getreaperage(pid_t qpid
)
1942 int pid
, mypipe
[2], ret
;
1945 long int mtime
, answer
= 0;
1953 if (!pid
) { // child
1954 mtime
= get_pid1_time(qpid
);
1955 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
1956 fprintf(stderr
, "Warning: bad write from getreaperage\n");
1962 FD_SET(mypipe
[0], &s
);
1965 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1971 fprintf(stderr
, "timed out\n");
1974 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
1986 static long int getprocidle(void)
1988 FILE *f
= fopen("/proc/uptime", "r");
1993 ret
= fscanf(f
, "%ld %ld", &age
, &idle
);
2001 * We read /proc/uptime and reuse its second field.
2002 * For the first field, we use the mtime for the reaper for
2003 * the calling pid as returned by getreaperage
2005 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
2006 struct fuse_file_info
*fi
)
2008 struct fuse_context
*fc
= fuse_get_context();
2009 long int reaperage
= getreaperage(fc
->pid
);;
2010 long int idletime
= getprocidle();
2014 return snprintf(buf
, size
, "%ld %ld\n", reaperage
, idletime
);
2017 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
2018 struct fuse_file_info
*fi
)
2021 struct fuse_context
*fc
= fuse_get_context();
2022 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "blkio");
2023 nih_local
char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
2024 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
2025 unsigned long read
= 0, write
= 0;
2026 unsigned long read_merged
= 0, write_merged
= 0;
2027 unsigned long read_sectors
= 0, write_sectors
= 0;
2028 unsigned long read_ticks
= 0, write_ticks
= 0;
2029 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
2030 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
2032 size_t linelen
= 0, total_len
= 0;
2033 unsigned int major
= 0, minor
= 0;
2043 if (!cgm_get_value("blkio", cg
, "blkio.io_serviced", &io_serviced_str
))
2045 if (!cgm_get_value("blkio", cg
, "blkio.io_merged", &io_merged_str
))
2047 if (!cgm_get_value("blkio", cg
, "blkio.io_service_bytes", &io_service_bytes_str
))
2049 if (!cgm_get_value("blkio", cg
, "blkio.io_wait_time", &io_wait_time_str
))
2051 if (!cgm_get_value("blkio", cg
, "blkio.io_service_time", &io_service_time_str
))
2055 f
= fopen("/proc/diskstats", "r");
2059 while (getline(&line
, &linelen
, f
) != -1) {
2061 char *printme
, lbuf
[256];
2063 i
= sscanf(line
, "%u %u %s", &major
, &minor
, dev_name
);
2065 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
2066 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
2067 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
2068 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
2069 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
2070 read_sectors
= read_sectors
/512;
2071 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
2072 write_sectors
= write_sectors
/512;
2074 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
2075 rd_svctm
= rd_svctm
/1000000;
2076 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
2077 rd_wait
= rd_wait
/1000000;
2078 read_ticks
= rd_svctm
+ rd_wait
;
2080 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
2081 wr_svctm
= wr_svctm
/1000000;
2082 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
2083 wr_wait
= wr_wait
/1000000;
2084 write_ticks
= wr_svctm
+ wr_wait
;
2086 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
2087 tot_ticks
= tot_ticks
/1000000;
2092 memset(lbuf
, 0, 256);
2093 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
) {
2094 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2095 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
2096 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
2101 l
= snprintf(buf
, size
, "%s", printme
);
2112 static off_t
get_procfile_size(const char *which
)
2114 FILE *f
= fopen(which
, "r");
2117 ssize_t sz
, answer
= 0;
2121 while ((sz
= getline(&line
, &len
, f
)) != -1)
2129 static int proc_getattr(const char *path
, struct stat
*sb
)
2131 struct timespec now
;
2133 memset(sb
, 0, sizeof(struct stat
));
2134 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
2136 sb
->st_uid
= sb
->st_gid
= 0;
2137 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
2138 if (strcmp(path
, "/proc") == 0) {
2139 sb
->st_mode
= S_IFDIR
| 00555;
2143 if (strcmp(path
, "/proc/meminfo") == 0 ||
2144 strcmp(path
, "/proc/cpuinfo") == 0 ||
2145 strcmp(path
, "/proc/uptime") == 0 ||
2146 strcmp(path
, "/proc/stat") == 0 ||
2147 strcmp(path
, "/proc/diskstats") == 0) {
2148 sb
->st_size
= get_procfile_size(path
);
2149 sb
->st_mode
= S_IFREG
| 00444;
2157 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2158 struct fuse_file_info
*fi
)
2160 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
2161 filler(buf
, "meminfo", NULL
, 0) != 0 ||
2162 filler(buf
, "stat", NULL
, 0) != 0 ||
2163 filler(buf
, "uptime", NULL
, 0) != 0 ||
2164 filler(buf
, "diskstats", NULL
, 0) != 0)
2169 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
2171 if (strcmp(path
, "/proc/meminfo") == 0 ||
2172 strcmp(path
, "/proc/cpuinfo") == 0 ||
2173 strcmp(path
, "/proc/uptime") == 0 ||
2174 strcmp(path
, "/proc/stat") == 0 ||
2175 strcmp(path
, "/proc/diskstats") == 0)
2180 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2181 struct fuse_file_info
*fi
)
2183 if (strcmp(path
, "/proc/meminfo") == 0)
2184 return proc_meminfo_read(buf
, size
, offset
, fi
);
2185 if (strcmp(path
, "/proc/cpuinfo") == 0)
2186 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
2187 if (strcmp(path
, "/proc/uptime") == 0)
2188 return proc_uptime_read(buf
, size
, offset
, fi
);
2189 if (strcmp(path
, "/proc/stat") == 0)
2190 return proc_stat_read(buf
, size
, offset
, fi
);
2191 if (strcmp(path
, "/proc/diskstats") == 0)
2192 return proc_diskstats_read(buf
, size
, offset
, fi
);
2198 * these just delegate to the /proc and /cgroup ops as
2202 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
2204 if (strcmp(path
, "/") == 0) {
2205 sb
->st_mode
= S_IFDIR
| 00755;
2209 if (strncmp(path
, "/cgroup", 7) == 0) {
2210 return cg_getattr(path
, sb
);
2212 if (strncmp(path
, "/proc", 5) == 0) {
2213 return proc_getattr(path
, sb
);
2218 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
2220 if (strcmp(path
, "/") == 0)
2223 if (strncmp(path
, "/cgroup", 7) == 0) {
2224 return cg_opendir(path
, fi
);
2226 if (strcmp(path
, "/proc") == 0)
2231 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2232 struct fuse_file_info
*fi
)
2234 if (strcmp(path
, "/") == 0) {
2235 if (filler(buf
, "proc", NULL
, 0) != 0 ||
2236 filler(buf
, "cgroup", NULL
, 0) != 0)
2240 if (strncmp(path
, "/cgroup", 7) == 0)
2241 return cg_readdir(path
, buf
, filler
, offset
, fi
);
2242 if (strcmp(path
, "/proc") == 0)
2243 return proc_readdir(path
, buf
, filler
, offset
, fi
);
2247 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
2249 if (strcmp(path
, "/") == 0)
2251 if (strncmp(path
, "/cgroup", 7) == 0) {
2252 return cg_releasedir(path
, fi
);
2254 if (strcmp(path
, "/proc") == 0)
2259 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
2261 if (strncmp(path
, "/cgroup", 7) == 0)
2262 return cg_open(path
, fi
);
2263 if (strncmp(path
, "/proc", 5) == 0)
2264 return proc_open(path
, fi
);
2269 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2270 struct fuse_file_info
*fi
)
2272 if (strncmp(path
, "/cgroup", 7) == 0)
2273 return cg_read(path
, buf
, size
, offset
, fi
);
2274 if (strncmp(path
, "/proc", 5) == 0)
2275 return proc_read(path
, buf
, size
, offset
, fi
);
2280 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2281 struct fuse_file_info
*fi
)
2283 if (strncmp(path
, "/cgroup", 7) == 0) {
2284 return cg_write(path
, buf
, size
, offset
, fi
);
2290 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
2295 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
2300 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
2305 int lxcfs_mkdir(const char *path
, mode_t mode
)
2307 if (strncmp(path
, "/cgroup", 7) == 0)
2308 return cg_mkdir(path
, mode
);
2313 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
2315 if (strncmp(path
, "/cgroup", 7) == 0)
2316 return cg_chown(path
, uid
, gid
);
2322 * cat first does a truncate before doing ops->write. This doesn't
2323 * really make sense for cgroups. So just return 0 always but do
2326 int lxcfs_truncate(const char *path
, off_t newsize
)
2328 if (strncmp(path
, "/cgroup", 7) == 0)
2333 int lxcfs_rmdir(const char *path
)
2335 if (strncmp(path
, "/cgroup", 7) == 0)
2336 return cg_rmdir(path
);
2340 int lxcfs_chmod(const char *path
, mode_t mode
)
2342 if (strncmp(path
, "/cgroup", 7) == 0)
2343 return cg_chmod(path
, mode
);
2347 const struct fuse_operations lxcfs_ops
= {
2348 .getattr
= lxcfs_getattr
,
2352 .mkdir
= lxcfs_mkdir
,
2354 .rmdir
= lxcfs_rmdir
,
2358 .chmod
= lxcfs_chmod
,
2359 .chown
= lxcfs_chown
,
2360 .truncate
= lxcfs_truncate
,
2365 .release
= lxcfs_release
,
2366 .write
= lxcfs_write
,
2369 .flush
= lxcfs_flush
,
2370 .fsync
= lxcfs_fsync
,
2375 .removexattr
= NULL
,
2377 .opendir
= lxcfs_opendir
,
2378 .readdir
= lxcfs_readdir
,
2379 .releasedir
= lxcfs_releasedir
,
2390 static void usage(const char *me
)
2392 fprintf(stderr
, "Usage:\n");
2393 fprintf(stderr
, "\n");
2394 fprintf(stderr
, "%s [FUSE and mount options] mountpoint\n", me
);
2398 static bool is_help(char *w
)
2400 if (strcmp(w
, "-h") == 0 ||
2401 strcmp(w
, "--help") == 0 ||
2402 strcmp(w
, "-help") == 0 ||
2403 strcmp(w
, "help") == 0)
2408 int main(int argc
, char *argv
[])
2411 struct lxcfs_state
*d
;
2413 if (argc
< 2 || is_help(argv
[1]))
2416 d
= malloc(sizeof(*d
));
2420 if (!cgm_escape_cgroup())
2421 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
2423 if (!cgm_get_controllers(&d
->subsystems
))
2426 ret
= fuse_main(argc
, argv
, &lxcfs_ops
, d
);