3 * Copyright © 2014,2015 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
6 * See COPYING file for details.
9 #define FUSE_USE_VERSION 26
23 #include <linux/sched.h>
24 #include <sys/socket.h>
25 #include <sys/mount.h>
28 #include "cgmanager.h"
29 #include "config.h" // for VERSION
33 * a null-terminated list of the mounted subsystems. We
34 * detect this at startup.
38 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
43 LXC_TYPE_PROC_MEMINFO
,
44 LXC_TYPE_PROC_CPUINFO
,
47 LXC_TYPE_PROC_DISKSTATS
,
55 char *buf
; // unused as of yet
57 int size
; //actual data size
61 /* reserve buffer size, for cpuall in /proc/stat */
62 #define BUF_RESERVE_SIZE 256
66 * src: a pointer to a char* in which ot append the pid.
67 * sz: the number of characters printed so far, minus trailing \0.
68 * asz: the allocated size so far
69 * pid: the pid to append
71 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
76 sprintf(tmp
, "%d\n", (int)pid
);
80 d
= malloc(BUF_RESERVE_SIZE
);
83 *asz
= BUF_RESERVE_SIZE
;
84 } else if (strlen(tmp
) + sz
+ 1 >= asz
) {
86 d
= realloc(d
, *asz
+ BUF_RESERVE_SIZE
);
89 *asz
+= BUF_RESERVE_SIZE
;
91 memcpy(d
+*sz
, tmp
, strlen(tmp
));
96 static char *must_copy_string(void *parent
, const char *str
)
108 static int wait_for_pid(pid_t pid
)
113 ret
= waitpid(pid
, &status
, 0);
121 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
127 * Given a open file * to /proc/pid/{u,g}id_map, and an id
128 * valid in the caller's namespace, return the id mapped into
130 * Returns the mapped id, or -1 on error.
133 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
135 unsigned int nsuid
, // base id for a range in the idfile's namespace
136 hostuid
, // base id for a range in the caller's namespace
137 count
; // number of ids in this range
141 fseek(idfile
, 0L, SEEK_SET
);
142 while (fgets(line
, 400, idfile
)) {
143 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
146 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
148 * uids wrapped around - unexpected as this is a procfile,
151 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s\n",
152 nsuid
, hostuid
, count
, line
);
155 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
157 * now since hostuid <= in_id < hostuid+count, and
158 * hostuid+count and nsuid+count do not wrap around,
159 * we know that nsuid+(in_id-hostuid) which must be
160 * less that nsuid+(count) must not wrap around
162 return (in_id
- hostuid
) + nsuid
;
171 * for is_privileged_over,
172 * specify whether we require the calling uid to be root in his
175 #define NS_ROOT_REQD true
176 #define NS_ROOT_OPT false
180 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
187 if (victim
== -1 || uid
== -1)
191 * If the request is one not requiring root in the namespace,
192 * then having the same uid suffices. (i.e. uid 1000 has write
193 * access to files owned by uid 1000
195 if (!req_ns_root
&& uid
== victim
)
198 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
199 if (ret
< 0 || ret
>= PROCLEN
)
201 FILE *f
= fopen(fpath
, "r");
205 /* if caller's not root in his namespace, reject */
206 nsuid
= convert_id_to_ns(f
, uid
);
211 * If victim is not mapped into caller's ns, reject.
212 * XXX I'm not sure this check is needed given that fuse
213 * will be sending requests where the vfs has converted
215 nsuid
= convert_id_to_ns(f
, victim
);
226 static bool perms_include(int fmode
, mode_t req_mode
)
230 switch (req_mode
& O_ACCMODE
) {
238 r
= S_IROTH
| S_IWOTH
;
243 return ((fmode
& r
) == r
);
246 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
250 if (strlen(taskcg
) <= strlen(querycg
)) {
251 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
255 if (strcmp(querycg
, "/") == 0)
256 start
= strdup(taskcg
+ 1);
258 start
= strdup(taskcg
+ strlen(querycg
) + 1);
261 end
= strchr(start
, '/');
267 static void stripnewline(char *x
)
269 size_t l
= strlen(x
);
270 if (l
&& x
[l
-1] == '\n')
274 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
283 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
284 if (ret
< 0 || ret
>= PROCLEN
)
286 if (!(f
= fopen(fnam
, "r")))
289 while (getline(&line
, &len
, f
) != -1) {
293 c1
= strchr(line
, ':');
297 c2
= strchr(c1
, ':');
301 if (strcmp(c1
, contrl
) != 0)
318 * check whether a fuse context may access a cgroup dir or file
320 * If file is not null, it is a cgroup file to check under cg.
321 * If file is null, then we are checking perms on cg itself.
323 * For files we can check the mode of the list_keys result.
324 * For cgroups, we must make assumptions based on the files under the
325 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
328 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
330 struct cgm_keys
**list
= NULL
;
340 if (!cgm_list_keys(contrl
, cg
, &list
))
342 for (i
= 0; list
[i
]; i
++) {
343 if (strcmp(list
[i
]->name
, file
) == 0) {
344 struct cgm_keys
*k
= list
[i
];
345 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
346 if (perms_include(k
->mode
>> 6, mode
)) {
351 if (fc
->gid
== k
->gid
) {
352 if (perms_include(k
->mode
>> 3, mode
)) {
357 ret
= perms_include(k
->mode
, mode
);
367 #define INITSCOPE "/init.scope"
368 static void prune_init_slice(char *cg
)
371 point
= cg
+ strlen(cg
) - strlen(INITSCOPE
);
374 if (strcmp(point
, INITSCOPE
) == 0) {
383 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
384 * If caller is in /a, he may act on /a/b, but not on /b.
385 * if the answer is false and nextcg is not NULL, then *nextcg will point
386 * to a string containing the next cgroup directory under cg, which must be
387 * freed by the caller.
389 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
398 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
399 if (ret
< 0 || ret
>= PROCLEN
)
401 if (!(f
= fopen(fnam
, "r")))
404 while (getline(&line
, &len
, f
) != -1) {
405 char *c1
, *c2
, *linecmp
;
408 c1
= strchr(line
, ':');
412 c2
= strchr(c1
, ':');
416 if (strcmp(c1
, contrl
) != 0)
420 prune_init_slice(c2
);
422 * callers pass in '/' for root cgroup, otherwise they pass
423 * in a cgroup without leading '/'
425 linecmp
= *cg
== '/' ? c2
: c2
+1;
426 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
428 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
442 * given /cgroup/freezer/a/b, return "freezer".
443 * the returned char* should NOT be freed.
445 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
450 if (strlen(path
) < 9)
452 if (*(path
+7) != '/')
458 slash
= strstr(contr
, "/");
462 /* verify that it is a subsystem */
463 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
467 for (i
= 0; list
[i
]; i
++) {
468 if (strcmp(list
[i
], contr
) == 0)
475 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
476 * Note that the returned value may include files (keynames) etc
478 static const char *find_cgroup_in_path(const char *path
)
482 if (strlen(path
) < 9)
484 p1
= strstr(path
+8, "/");
490 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
501 if (!cgm_list_children(contr
, dir
, &list
))
503 for (i
= 0; list
[i
]; i
++) {
504 if (strcmp(list
[i
], f
) == 0) {
511 for (i
= 0; list
[i
]; i
++)
517 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
519 struct cgm_keys
**list
= NULL
;
520 struct cgm_keys
*k
= NULL
;
527 if (!cgm_list_keys(contr
, dir
, &list
))
529 for (i
= 0; list
[i
]; i
++) {
530 if (strcmp(list
[i
]->name
, f
) == 0) {
532 // free all the keys we are not returning
534 for (j
= 0; list
[j
]; j
++) {
548 * dir should be freed, file not
550 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
557 *file
= strrchr(cg
, '/');
562 p
= strrchr(*dir
, '/');
567 * FUSE ops for /cgroup
570 static int cg_getattr(const char *path
, struct stat
*sb
)
573 struct fuse_context
*fc
= fuse_get_context();
575 char *fpath
= NULL
, *path1
, *path2
;
576 struct cgm_keys
*k
= NULL
;
578 const char *controller
= NULL
;
585 memset(sb
, 0, sizeof(struct stat
));
587 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
590 sb
->st_uid
= sb
->st_gid
= 0;
591 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
594 if (strcmp(path
, "/cgroup") == 0) {
595 sb
->st_mode
= S_IFDIR
| 00755;
600 controller
= pick_controller_from_path(fc
, path
);
603 cgroup
= find_cgroup_in_path(path
);
605 /* this is just /cgroup/controller, return it as a dir */
606 sb
->st_mode
= S_IFDIR
| 00755;
611 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
621 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
622 * Then check that caller's cgroup is under path if fpath is a child
623 * cgroup, or cgdir if fpath is a file */
625 if (is_child_cgroup(controller
, path1
, path2
)) {
626 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
627 /* this is just /cgroup/controller, return it as a dir */
628 sb
->st_mode
= S_IFDIR
| 00555;
633 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
638 // get uid, gid, from '/tasks' file and make up a mode
639 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
640 sb
->st_mode
= S_IFDIR
| 00755;
641 k
= get_cgroup_key(controller
, cgroup
, "tasks");
643 sb
->st_uid
= sb
->st_gid
= 0;
654 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
655 sb
->st_mode
= S_IFREG
| k
->mode
;
661 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
)) {
665 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
)) {
678 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
680 struct fuse_context
*fc
= fuse_get_context();
682 struct file_info
*dir_info
;
683 char *controller
= NULL
;
688 if (strcmp(path
, "/cgroup") == 0) {
692 // return list of keys for the controller, and list of child cgroups
693 controller
= pick_controller_from_path(fc
, path
);
697 cgroup
= find_cgroup_in_path(path
);
699 /* this is just /cgroup/controller, return its contents */
704 if (cgroup
&& !fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
708 /* we'll free this at cg_releasedir */
709 dir_info
= malloc(sizeof(*dir_info
));
712 dir_info
->controller
= must_copy_string(dir_info
, controller
);
713 dir_info
->cgroup
= must_copy_string(dir_info
, cgroup
);
714 dir_info
->type
= LXC_TYPE_CGDIR
;
715 dir_info
->buf
= NULL
;
716 dir_info
->file
= NULL
;
717 dir_info
->buflen
= 0;
719 fi
->fh
= (unsigned long)dir_info
;
723 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
724 struct fuse_file_info
*fi
)
726 struct file_info
*d
= (struct file_info
*)fi
->fh
;
727 struct cgm_keys
**list
= NULL
;
730 struct fuse_context
*fc
= fuse_get_context();
733 if (d
->type
!= LXC_TYPE_CGDIR
) {
734 fprintf(stderr
, "Internal error: file cache info used in readdir\n");
737 if (!d
->cgroup
&& !d
->controller
) {
738 // ls /var/lib/lxcfs/cgroup - just show list of controllers
739 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
745 for (i
= 0; list
[i
]; i
++) {
746 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
753 if (!cgm_list_keys(d
->controller
, d
->cgroup
, &list
)) {
754 // not a valid cgroup
759 if (!caller_is_in_ancestor(fc
->pid
, d
->controller
, d
->cgroup
, &nextcg
)) {
762 ret
= filler(buf
, nextcg
, NULL
, 0);
773 for (i
= 0; list
[i
]; i
++) {
774 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
780 // now get the list of child cgroups
782 if (!cgm_list_children(d
->controller
, d
->cgroup
, &clist
)) {
786 for (i
= 0; clist
[i
]; i
++) {
787 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
797 for (i
= 0; clist
[i
]; i
++)
804 static void do_release_file_info(struct file_info
*f
)
815 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
817 struct file_info
*d
= (struct file_info
*)fi
->fh
;
819 do_release_file_info(d
);
823 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
826 char *fpath
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
827 struct cgm_keys
*k
= NULL
;
828 struct file_info
*file_info
;
829 struct fuse_context
*fc
= fuse_get_context();
835 controller
= pick_controller_from_path(fc
, path
);
838 cgroup
= find_cgroup_in_path(path
);
842 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
851 k
= get_cgroup_key(controller
, path1
, path2
);
858 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
859 // should never get here
864 /* we'll free this at cg_release */
865 file_info
= malloc(sizeof(*file_info
));
870 file_info
->controller
= must_copy_string(file_info
, controller
);
871 file_info
->cgroup
= must_copy_string(file_info
, path1
);
872 file_info
->file
= must_copy_string(file_info
, path2
);
873 file_info
->type
= LXC_TYPE_CGFILE
;
874 file_info
->buf
= NULL
;
875 file_info
->buflen
= 0;
877 fi
->fh
= (unsigned long)file_info
;
885 static int cg_release(const char *path
, struct fuse_file_info
*fi
)
887 struct file_info
*f
= (struct file_info
*)fi
->fh
;
889 do_release_file_info(f
);
893 static int msgrecv(int sockfd
, void *buf
, size_t len
)
899 FD_SET(sockfd
, &rfds
);
903 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) <= 0)
905 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
908 #define SEND_CREDS_OK 0
909 #define SEND_CREDS_NOTSK 1
910 #define SEND_CREDS_FAIL 2
911 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
913 struct msghdr msg
= { 0 };
915 struct cmsghdr
*cmsg
;
916 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
921 if (msgrecv(sock
, buf
, 1) != 1) {
922 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
924 return SEND_CREDS_FAIL
;
928 msg
.msg_control
= cmsgbuf
;
929 msg
.msg_controllen
= sizeof(cmsgbuf
);
931 cmsg
= CMSG_FIRSTHDR(&msg
);
932 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
933 cmsg
->cmsg_level
= SOL_SOCKET
;
934 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
935 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
942 iov
.iov_len
= sizeof(buf
);
946 if (sendmsg(sock
, &msg
, 0) < 0) {
947 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
950 return SEND_CREDS_NOTSK
;
951 return SEND_CREDS_FAIL
;
954 return SEND_CREDS_OK
;
957 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
959 struct msghdr msg
= { 0 };
961 struct cmsghdr
*cmsg
;
962 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
975 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
976 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
980 if (write(sock
, buf
, 1) != 1) {
981 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
987 msg
.msg_control
= cmsgbuf
;
988 msg
.msg_controllen
= sizeof(cmsgbuf
);
991 iov
.iov_len
= sizeof(buf
);
999 if (select(sock
+1, &rfds
, NULL
, NULL
, &tv
) <= 0) {
1000 fprintf(stderr
, "Failed to select for scm_cred: %s\n",
1004 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
1006 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
1011 cmsg
= CMSG_FIRSTHDR(&msg
);
1013 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
1014 cmsg
->cmsg_level
== SOL_SOCKET
&&
1015 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1016 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
1025 * pid_to_ns - reads pids from a ucred over a socket, then writes the
1026 * int value back over the socket. This shifts the pid from the
1027 * sender's pidns into tpid's pidns.
1029 static void pid_to_ns(int sock
, pid_t tpid
)
1034 while (recv_creds(sock
, &cred
, &v
)) {
1037 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
1044 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
1045 * in your old pidns. Only children which you fork will be in the target
1046 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
1047 * actually convert pids
1049 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
1051 int newnsfd
= -1, ret
, cpipe
[2];
1058 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1059 if (ret
< 0 || ret
>= sizeof(fnam
))
1061 newnsfd
= open(fnam
, O_RDONLY
);
1064 if (setns(newnsfd
, 0) < 0)
1068 if (pipe(cpipe
) < 0)
1079 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1080 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1081 __func__
, strerror(errno
));
1084 pid_to_ns(sock
, tpid
);
1086 // give the child 1 second to be done forking and
1089 FD_SET(cpipe
[0], &s
);
1092 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1095 ret
= read(cpipe
[0], &v
, 1);
1096 if (ret
!= sizeof(char) || v
!= '1') {
1100 if (!wait_for_pid(cpid
))
1105 kill(cpid
, SIGKILL
);
1111 * To read cgroup files with a particular pid, we will setns into the child
1112 * pidns, open a pipe, fork a child - which will be the first to really be in
1113 * the child ns - which does the cgm_get_value and writes the data to the pipe.
1115 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
1117 int sock
[2] = {-1, -1};
1118 char *tmpdata
= NULL
;
1120 pid_t qpid
, cpid
= -1;
1121 bool answer
= false;
1125 size_t sz
= 0, asz
= 0;
1128 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
1132 * Now we read the pids from returned data one by one, pass
1133 * them into a child in the target namespace, read back the
1134 * translated pids, and put them into our to-return data
1137 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1138 perror("socketpair");
1148 pid_to_ns_wrapper(sock
[1], tpid
);
1150 char *ptr
= tmpdata
;
1153 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
1155 ret
= send_creds(sock
[0], &cred
, v
, true);
1157 if (ret
== SEND_CREDS_NOTSK
)
1159 if (ret
== SEND_CREDS_FAIL
)
1162 // read converted results
1164 FD_SET(sock
[0], &s
);
1167 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
1169 fprintf(stderr
, "%s: select error waiting for pid from child: %s\n",
1170 __func__
, strerror(errno
));
1173 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1174 fprintf(stderr
, "%s: error reading pid from child: %s\n",
1175 __func__
, strerror(errno
));
1178 must_strcat_pid(d
, &sz
, &asz
, qpid
);
1180 ptr
= strchr(ptr
, '\n');
1186 cred
.pid
= getpid();
1188 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
1189 // failed to ask child to exit
1190 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
1191 __func__
, strerror(errno
));
1201 if (sock
[0] != -1) {
1208 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1209 struct fuse_file_info
*fi
)
1211 struct fuse_context
*fc
= fuse_get_context();
1212 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1213 struct cgm_keys
*k
= NULL
;
1218 if (f
->type
!= LXC_TYPE_CGFILE
) {
1219 fprintf(stderr
, "Internal error: directory cache info used in cg_read\n");
1232 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1238 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) { // should never get here
1243 if (strcmp(f
->file
, "tasks") == 0 ||
1244 strcmp(f
->file
, "/tasks") == 0 ||
1245 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1246 strcmp(f
->file
, "cgroup.procs") == 0)
1247 // special case - we have to translate the pids
1248 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1250 r
= cgm_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
1264 memcpy(buf
, data
, s
);
1265 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
1275 static void pid_from_ns(int sock
, pid_t tpid
)
1291 ret
= select(sock
+1, &s
, NULL
, NULL
, &tv
);
1293 fprintf(stderr
, "%s: bad select before read from parent: %s\n",
1294 __func__
, strerror(errno
));
1297 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1298 fprintf(stderr
, "%s: bad read from parent: %s\n",
1299 __func__
, strerror(errno
));
1302 if (vpid
== -1) // done
1306 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1308 cred
.pid
= getpid();
1309 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1316 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1318 int newnsfd
= -1, ret
, cpipe
[2];
1325 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1326 if (ret
< 0 || ret
>= sizeof(fnam
))
1328 newnsfd
= open(fnam
, O_RDONLY
);
1331 if (setns(newnsfd
, 0) < 0)
1335 if (pipe(cpipe
) < 0)
1347 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1348 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1349 __func__
, strerror(errno
));
1352 pid_from_ns(sock
, tpid
);
1355 // give the child 1 second to be done forking and
1358 FD_SET(cpipe
[0], &s
);
1361 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1364 ret
= read(cpipe
[0], &v
, 1);
1365 if (ret
!= sizeof(char) || v
!= '1') {
1369 if (!wait_for_pid(cpid
))
1374 kill(cpid
, SIGKILL
);
1379 static bool do_write_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, const char *buf
)
1381 int sock
[2] = {-1, -1};
1382 pid_t qpid
, cpid
= -1;
1383 bool answer
= false, fail
= false;
1386 * write the pids to a socket, have helper in writer's pidns
1387 * call movepid for us
1389 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1390 perror("socketpair");
1399 pid_from_ns_wrapper(sock
[1], tpid
);
1401 const char *ptr
= buf
;
1402 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1406 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1407 fprintf(stderr
, "%s: error writing pid to child: %s\n",
1408 __func__
, strerror(errno
));
1412 if (recv_creds(sock
[0], &cred
, &v
)) {
1414 if (!cgm_move_pid(contrl
, cg
, cred
.pid
))
1419 ptr
= strchr(ptr
, '\n');
1425 /* All good, write the value */
1427 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1428 fprintf(stderr
, "Warning: failed to ask child to exit\n");
1436 if (sock
[0] != -1) {
1443 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1444 struct fuse_file_info
*fi
)
1446 struct fuse_context
*fc
= fuse_get_context();
1447 char *localbuf
= NULL
;
1448 struct cgm_keys
*k
= NULL
;
1449 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1452 if (f
->type
!= LXC_TYPE_CGFILE
) {
1453 fprintf(stderr
, "Internal error: directory cache info used in cg_write\n");
1463 localbuf
= alloca(size
+1);
1464 localbuf
[size
] = '\0';
1465 memcpy(localbuf
, buf
, size
);
1467 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1472 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
1477 if (strcmp(f
->file
, "tasks") == 0 ||
1478 strcmp(f
->file
, "/tasks") == 0 ||
1479 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1480 strcmp(f
->file
, "cgroup.procs") == 0)
1481 // special case - we have to translate the pids
1482 r
= do_write_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1484 r
= cgm_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1494 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1496 struct fuse_context
*fc
= fuse_get_context();
1497 char *cgdir
= NULL
, *fpath
= NULL
, *path1
, *path2
, *controller
;
1498 struct cgm_keys
*k
= NULL
;
1505 if (strcmp(path
, "/cgroup") == 0)
1508 controller
= pick_controller_from_path(fc
, path
);
1511 cgroup
= find_cgroup_in_path(path
);
1513 /* this is just /cgroup/controller */
1516 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1526 if (is_child_cgroup(controller
, path1
, path2
)) {
1527 // get uid, gid, from '/tasks' file and make up a mode
1528 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1529 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1532 k
= get_cgroup_key(controller
, path1
, path2
);
1540 * This being a fuse request, the uid and gid must be valid
1541 * in the caller's namespace. So we can just check to make
1542 * sure that the caller is root in his uid, and privileged
1543 * over the file's current owner.
1545 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
1550 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
)) {
1564 int cg_chmod(const char *path
, mode_t mode
)
1566 struct fuse_context
*fc
= fuse_get_context();
1567 char * cgdir
= NULL
, *fpath
= NULL
, *path1
, *path2
, *controller
;
1568 struct cgm_keys
*k
= NULL
;
1575 if (strcmp(path
, "/cgroup") == 0)
1578 controller
= pick_controller_from_path(fc
, path
);
1581 cgroup
= find_cgroup_in_path(path
);
1583 /* this is just /cgroup/controller */
1586 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1596 if (is_child_cgroup(controller
, path1
, path2
)) {
1597 // get uid, gid, from '/tasks' file and make up a mode
1598 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1599 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1602 k
= get_cgroup_key(controller
, path1
, path2
);
1610 * This being a fuse request, the uid and gid must be valid
1611 * in the caller's namespace. So we can just check to make
1612 * sure that the caller is root in his uid, and privileged
1613 * over the file's current owner.
1615 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1620 if (!cgm_chmod_file(controller
, cgroup
, mode
)) {
1632 #define LXCFS_MKDIR_PATH LIBEXECDIR "/lxcfs/lxcfs_mkdir"
1634 int cg_mkdir(const char *path
, mode_t mode
)
1636 struct fuse_context
*fc
= fuse_get_context();
1637 char *fpath
= NULL
, *path1
, *cgdir
= NULL
, *controller
;
1645 controller
= pick_controller_from_path(fc
, path
);
1649 cgroup
= find_cgroup_in_path(path
);
1653 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1659 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
1663 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
)) {
1668 if (fc
->uid
== 0 && fc
->gid
== 0) {
1669 if (!cgm_create(controller
, cgroup
)) {
1675 * exec a helper so as to get a clean dbus connection
1676 * 17 for lxcfs_mkdir, and spaces and newline and \0. 50 for two ints.
1679 size_t len
= strlen(cgroup
) + strlen(controller
) + 17 + 50;
1680 char *cmd
= alloca(len
);
1681 ret
= snprintf(cmd
, len
, "%s %d %d %s %s\n", LXCFS_MKDIR_PATH
,
1682 fc
->uid
, fc
->gid
, controller
, cgroup
);
1683 if (ret
< 0 || ret
>= len
) {
1699 static int cg_rmdir(const char *path
)
1701 struct fuse_context
*fc
= fuse_get_context();
1702 char *fpath
= NULL
, *cgdir
= NULL
, *controller
;
1709 controller
= pick_controller_from_path(fc
, path
);
1713 cgroup
= find_cgroup_in_path(path
);
1717 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1723 fprintf(stderr
, "rmdir: verifying access to %s:%s (req path %s)\n",
1724 controller
, cgdir
, path
);
1725 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
1729 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
1734 if (!cgm_remove(controller
, cgroup
)) {
1746 static bool startswith(const char *line
, const char *pref
)
1748 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1753 static void get_mem_cached(char *memstat
, unsigned long *v
)
1759 if (startswith(memstat
, "total_cache")) {
1760 sscanf(memstat
+ 11, "%lu", v
);
1764 eol
= strchr(memstat
, '\n');
1771 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
1777 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
1779 size_t len
= strlen(key
);
1783 if (startswith(str
, key
)) {
1784 sscanf(str
+ len
, "%lu", v
);
1787 eol
= strchr(str
, '\n');
1794 static int read_file(const char *path
, char *buf
, size_t size
,
1795 struct file_info
*d
)
1797 size_t linelen
= 0, total_len
= 0, rv
= 0;
1799 char *cache
= d
->buf
;
1800 size_t cache_size
= d
->buflen
;
1801 FILE *f
= fopen(path
, "r");
1805 while (getline(&line
, &linelen
, f
) != -1) {
1806 size_t l
= snprintf(cache
, cache_size
, "%s", line
);
1808 perror("Error writing to cache");
1812 if (l
>= cache_size
) {
1813 fprintf(stderr
, "Internal error: truncated write to cache\n");
1817 if (l
< cache_size
) {
1822 cache
+= cache_size
;
1823 total_len
+= cache_size
;
1829 d
->size
= total_len
;
1830 if (total_len
> size
) total_len
= size
;
1832 /* read from off 0 */
1833 memcpy(buf
, d
->buf
, total_len
);
1842 * FUSE ops for /proc
1845 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1846 struct fuse_file_info
*fi
)
1848 struct fuse_context
*fc
= fuse_get_context();
1849 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1851 char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1852 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1854 size_t linelen
= 0, total_len
= 0, rv
= 0;
1855 char *cache
= d
->buf
;
1856 size_t cache_size
= d
->buflen
;
1860 if (offset
> d
->size
)
1864 int left
= d
->size
- offset
;
1865 total_len
= left
> size
? size
: left
;
1866 memcpy(buf
, cache
+ offset
, total_len
);
1870 cg
= get_pid_cgroup(fc
->pid
, "memory");
1872 return read_file("/proc/meminfo", buf
, size
, d
);
1874 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1876 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1878 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1880 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1881 memusage
= strtoul(memusage_str
, NULL
, 10);
1884 get_mem_cached(memstat_str
, &cached
);
1886 f
= fopen("/proc/meminfo", "r");
1890 while (getline(&line
, &linelen
, f
) != -1) {
1892 char *printme
, lbuf
[100];
1894 memset(lbuf
, 0, 100);
1895 if (startswith(line
, "MemTotal:")) {
1896 sscanf(line
+14, "%lu", &hosttotal
);
1897 if (hosttotal
< memlimit
)
1898 memlimit
= hosttotal
;
1899 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1901 } else if (startswith(line
, "MemFree:")) {
1902 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1904 } else if (startswith(line
, "MemAvailable:")) {
1905 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1907 } else if (startswith(line
, "Buffers:")) {
1908 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1910 } else if (startswith(line
, "Cached:")) {
1911 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1913 } else if (startswith(line
, "SwapCached:")) {
1914 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1919 l
= snprintf(cache
, cache_size
, "%s", printme
);
1921 perror("Error writing to cache");
1926 if (l
>= cache_size
) {
1927 fprintf(stderr
, "Internal error: truncated write to cache\n");
1938 d
->size
= total_len
;
1939 if (total_len
> size
) total_len
= size
;
1940 memcpy(buf
, d
->buf
, total_len
);
1955 * Read the cpuset.cpus for cg
1956 * Return the answer in a newly allocated string which must be freed
1958 static char *get_cpuset(const char *cg
)
1962 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1967 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
1969 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1973 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1975 return cpu_in_cpuset(cpu
, cpuset
);
1979 * check whether this is a '^processor" line in /proc/cpuinfo
1981 static bool is_processor_line(const char *line
)
1985 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1990 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1991 struct fuse_file_info
*fi
)
1993 struct fuse_context
*fc
= fuse_get_context();
1994 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1996 char *cpuset
= NULL
;
1998 size_t linelen
= 0, total_len
= 0, rv
= 0;
1999 bool am_printing
= false;
2001 char *cache
= d
->buf
;
2002 size_t cache_size
= d
->buflen
;
2006 if (offset
> d
->size
)
2010 int left
= d
->size
- offset
;
2011 total_len
= left
> size
? size
: left
;
2012 memcpy(buf
, cache
+ offset
, total_len
);
2016 cg
= get_pid_cgroup(fc
->pid
, "cpuset");
2018 return read_file("proc/cpuinfo", buf
, size
, d
);
2020 cpuset
= get_cpuset(cg
);
2024 f
= fopen("/proc/cpuinfo", "r");
2028 while (getline(&line
, &linelen
, f
) != -1) {
2030 if (is_processor_line(line
)) {
2031 am_printing
= cpuline_in_cpuset(line
, cpuset
);
2034 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
2036 perror("Error writing to cache");
2040 if (l
>= cache_size
) {
2041 fprintf(stderr
, "Internal error: truncated write to cache\n");
2045 if (l
< cache_size
){
2050 cache
+= cache_size
;
2051 total_len
+= cache_size
;
2059 l
= snprintf(cache
, cache_size
, "%s", line
);
2061 perror("Error writing to cache");
2065 if (l
>= cache_size
) {
2066 fprintf(stderr
, "Internal error: truncated write to cache\n");
2070 if (l
< cache_size
) {
2075 cache
+= cache_size
;
2076 total_len
+= cache_size
;
2084 d
->size
= total_len
;
2085 if (total_len
> size
) total_len
= size
;
2087 /* read from off 0 */
2088 memcpy(buf
, d
->buf
, total_len
);
2099 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
2100 struct fuse_file_info
*fi
)
2102 struct fuse_context
*fc
= fuse_get_context();
2103 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2105 char *cpuset
= NULL
;
2107 size_t linelen
= 0, total_len
= 0, rv
= 0;
2108 int curcpu
= -1; /* cpu numbering starts at 0 */
2109 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0;
2110 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
2111 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0;
2112 #define CPUALL_MAX_SIZE BUF_RESERVE_SIZE
2113 char cpuall
[CPUALL_MAX_SIZE
];
2114 /* reserve for cpu all */
2115 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
2116 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
2120 if (offset
> d
->size
)
2124 int left
= d
->size
- offset
;
2125 total_len
= left
> size
? size
: left
;
2126 memcpy(buf
, d
->buf
+ offset
, total_len
);
2130 cg
= get_pid_cgroup(fc
->pid
, "cpuset");
2132 return read_file("/proc/stat", buf
, size
, d
);
2134 cpuset
= get_cpuset(cg
);
2138 f
= fopen("/proc/stat", "r");
2143 if (getline(&line
, &linelen
, f
) < 0) {
2144 fprintf(stderr
, "proc_stat_read read first line failed\n");
2148 while (getline(&line
, &linelen
, f
) != -1) {
2151 char cpu_char
[10]; /* That's a lot of cores */
2154 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
2155 /* not a ^cpuN line containing a number N, just print it */
2156 l
= snprintf(cache
, cache_size
, "%s", line
);
2158 perror("Error writing to cache");
2162 if (l
>= cache_size
) {
2163 fprintf(stderr
, "Internal error: truncated write to cache\n");
2167 if (l
< cache_size
) {
2173 //no more space, break it
2174 cache
+= cache_size
;
2175 total_len
+= cache_size
;
2181 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
2183 if (!cpu_in_cpuset(cpu
, cpuset
))
2187 c
= strchr(line
, ' ');
2190 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
2192 perror("Error writing to cache");
2197 if (l
>= cache_size
) {
2198 fprintf(stderr
, "Internal error: truncated write to cache\n");
2207 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user
, &nice
, &system
, &idle
, &iowait
, &irq
,
2208 &softirq
, &steal
, &guest
) != 9)
2212 system_sum
+= system
;
2214 iowait_sum
+= iowait
;
2216 softirq_sum
+= softirq
;
2223 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2224 "cpu ", user_sum
, nice_sum
, system_sum
, idle_sum
, iowait_sum
, irq_sum
, softirq_sum
, steal_sum
, guest_sum
);
2225 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
){
2226 memcpy(cache
, cpuall
, cpuall_len
);
2227 cache
+= cpuall_len
;
2229 /* shouldn't happen */
2230 fprintf(stderr
, "proc_stat_read copy cpuall failed, cpuall_len=%d\n", cpuall_len
);
2234 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
2235 total_len
+= cpuall_len
;
2237 d
->size
= total_len
;
2238 if (total_len
> size
) total_len
= size
;
2240 memcpy(buf
, d
->buf
, total_len
);
2253 * How to guess what to present for uptime?
2254 * One thing we could do would be to take the date on the caller's
2255 * memory.usage_in_bytes file, which should equal the time of creation
2256 * of his cgroup. However, a task could be in a sub-cgroup of the
2257 * container. The same problem exists if we try to look at the ages
2258 * of processes in the caller's cgroup.
2260 * So we'll fork a task that will enter the caller's pidns, mount a
2261 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
2263 * For the second uptime #, we'll do as Stéphane had done, just copy
2264 * the number from /proc/uptime. Not sure how to best emulate 'idle'
2265 * time. Maybe someone can come up with a good algorithm and submit a
2266 * patch. Maybe something based on cpushare info?
2269 /* return age of the reaper for $pid, taken from ctime of its procdir */
2270 static long int get_pid1_time(pid_t pid
)
2273 int fd
, cpipe
[2], ret
;
2280 if (unshare(CLONE_NEWNS
))
2283 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
2284 perror("rslave mount failed");
2288 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", pid
);
2289 if (ret
< 0 || ret
>= sizeof(fnam
))
2292 fd
= open(fnam
, O_RDONLY
);
2294 perror("get_pid1_time open of ns/pid");
2298 perror("get_pid1_time setns 1");
2304 if (pipe(cpipe
) < 0)
2315 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
2316 fprintf(stderr
, "%s (child): erorr on write: %s\n",
2317 __func__
, strerror(errno
));
2320 umount2("/proc", MNT_DETACH
);
2321 if (mount("proc", "/proc", "proc", 0, NULL
)) {
2322 perror("get_pid1_time mount");
2325 ret
= lstat("/proc/1", &sb
);
2327 perror("get_pid1_time lstat");
2330 return time(NULL
) - sb
.st_ctime
;
2333 // give the child 1 second to be done forking and
2336 FD_SET(cpipe
[0], &s
);
2339 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2342 ret
= read(cpipe
[0], &v
, 1);
2343 if (ret
!= sizeof(char) || v
!= '1') {
2351 kill(cpid
, SIGKILL
);
2356 static long int getreaperage(pid_t qpid
)
2358 int pid
, mypipe
[2], ret
;
2361 long int mtime
, answer
= 0;
2369 if (!pid
) { // child
2370 mtime
= get_pid1_time(qpid
);
2371 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
2372 fprintf(stderr
, "Warning: bad write from getreaperage\n");
2378 FD_SET(mypipe
[0], &s
);
2381 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2387 fprintf(stderr
, "timed out\n");
2390 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
2402 static unsigned long int getprocidle(void)
2404 FILE *f
= fopen("/proc/uptime", "r");
2405 unsigned long int age
, idle
;
2406 unsigned long int age_nsec
, idle_nsec
;
2411 ret
= fscanf(f
, "%lu.%02lu %lu.%02lu", &age
, &age_nsec
, &idle
, &idle_nsec
);
2419 * We read /proc/uptime and reuse its second field.
2420 * For the first field, we use the mtime for the reaper for
2421 * the calling pid as returned by getreaperage
2423 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
2424 struct fuse_file_info
*fi
)
2426 struct fuse_context
*fc
= fuse_get_context();
2427 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2428 long int reaperage
= getreaperage(fc
->pid
);;
2429 unsigned long int idletime
= getprocidle();
2430 char *cache
= d
->buf
;
2431 size_t total_len
= 0;
2434 if (offset
> d
->size
)
2438 int left
= d
->size
- offset
;
2439 total_len
= left
> size
? size
: left
;
2440 memcpy(buf
, cache
+ offset
, total_len
);
2444 total_len
= snprintf(d
->buf
, d
->size
, "%ld.0 %lu.0\n", reaperage
, idletime
);
2446 perror("Error writing to cache");
2450 d
->size
= (int)total_len
;
2453 if (total_len
> size
) total_len
= size
;
2455 memcpy(buf
, d
->buf
, total_len
);
2459 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
2460 struct fuse_file_info
*fi
)
2463 struct fuse_context
*fc
= fuse_get_context();
2464 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2466 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
2467 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
2468 unsigned long read
= 0, write
= 0;
2469 unsigned long read_merged
= 0, write_merged
= 0;
2470 unsigned long read_sectors
= 0, write_sectors
= 0;
2471 unsigned long read_ticks
= 0, write_ticks
= 0;
2472 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
2473 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
2474 char *cache
= d
->buf
;
2475 size_t cache_size
= d
->buflen
;
2477 size_t linelen
= 0, total_len
= 0, rv
= 0;
2478 unsigned int major
= 0, minor
= 0;
2483 if (offset
> d
->size
)
2487 int left
= d
->size
- offset
;
2488 total_len
= left
> size
? size
: left
;
2489 memcpy(buf
, cache
+ offset
, total_len
);
2493 cg
= get_pid_cgroup(fc
->pid
, "blkio");
2495 return read_file("/proc/diskstats", buf
, size
, d
);
2497 if (!cgm_get_value("blkio", cg
, "blkio.io_serviced", &io_serviced_str
))
2499 if (!cgm_get_value("blkio", cg
, "blkio.io_merged", &io_merged_str
))
2501 if (!cgm_get_value("blkio", cg
, "blkio.io_service_bytes", &io_service_bytes_str
))
2503 if (!cgm_get_value("blkio", cg
, "blkio.io_wait_time", &io_wait_time_str
))
2505 if (!cgm_get_value("blkio", cg
, "blkio.io_service_time", &io_service_time_str
))
2509 f
= fopen("/proc/diskstats", "r");
2513 while (getline(&line
, &linelen
, f
) != -1) {
2515 char *printme
, lbuf
[256];
2517 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
2519 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
2520 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
2521 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
2522 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
2523 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
2524 read_sectors
= read_sectors
/512;
2525 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
2526 write_sectors
= write_sectors
/512;
2528 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
2529 rd_svctm
= rd_svctm
/1000000;
2530 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
2531 rd_wait
= rd_wait
/1000000;
2532 read_ticks
= rd_svctm
+ rd_wait
;
2534 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
2535 wr_svctm
= wr_svctm
/1000000;
2536 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
2537 wr_wait
= wr_wait
/1000000;
2538 write_ticks
= wr_svctm
+ wr_wait
;
2540 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
2541 tot_ticks
= tot_ticks
/1000000;
2546 memset(lbuf
, 0, 256);
2547 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
) {
2548 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2549 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
2550 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
2555 l
= snprintf(cache
, cache_size
, "%s", printme
);
2557 perror("Error writing to fuse buf");
2561 if (l
>= cache_size
) {
2562 fprintf(stderr
, "Internal error: truncated write to cache\n");
2572 d
->size
= total_len
;
2573 if (total_len
> size
) total_len
= size
;
2574 memcpy(buf
, d
->buf
, total_len
);
2582 free(io_serviced_str
);
2583 free(io_merged_str
);
2584 free(io_service_bytes_str
);
2585 free(io_wait_time_str
);
2586 free(io_service_time_str
);
2590 static off_t
get_procfile_size(const char *which
)
2592 FILE *f
= fopen(which
, "r");
2595 ssize_t sz
, answer
= 0;
2599 while ((sz
= getline(&line
, &len
, f
)) != -1)
2607 static int proc_getattr(const char *path
, struct stat
*sb
)
2609 struct timespec now
;
2611 memset(sb
, 0, sizeof(struct stat
));
2612 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
2614 sb
->st_uid
= sb
->st_gid
= 0;
2615 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
2616 if (strcmp(path
, "/proc") == 0) {
2617 sb
->st_mode
= S_IFDIR
| 00555;
2621 if (strcmp(path
, "/proc/meminfo") == 0 ||
2622 strcmp(path
, "/proc/cpuinfo") == 0 ||
2623 strcmp(path
, "/proc/uptime") == 0 ||
2624 strcmp(path
, "/proc/stat") == 0 ||
2625 strcmp(path
, "/proc/diskstats") == 0) {
2627 sb
->st_mode
= S_IFREG
| 00444;
2635 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2636 struct fuse_file_info
*fi
)
2638 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
2639 filler(buf
, "meminfo", NULL
, 0) != 0 ||
2640 filler(buf
, "stat", NULL
, 0) != 0 ||
2641 filler(buf
, "uptime", NULL
, 0) != 0 ||
2642 filler(buf
, "diskstats", NULL
, 0) != 0)
2647 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
2650 struct file_info
*info
;
2652 if (strcmp(path
, "/proc/meminfo") == 0)
2653 type
= LXC_TYPE_PROC_MEMINFO
;
2654 else if (strcmp(path
, "/proc/cpuinfo") == 0)
2655 type
= LXC_TYPE_PROC_CPUINFO
;
2656 else if (strcmp(path
, "/proc/uptime") == 0)
2657 type
= LXC_TYPE_PROC_UPTIME
;
2658 else if (strcmp(path
, "/proc/stat") == 0)
2659 type
= LXC_TYPE_PROC_STAT
;
2660 else if (strcmp(path
, "/proc/diskstats") == 0)
2661 type
= LXC_TYPE_PROC_DISKSTATS
;
2665 info
= malloc(sizeof(*info
));
2669 memset(info
, 0, sizeof(*info
));
2672 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
2674 info
->buf
= malloc(info
->buflen
);
2675 } while (!info
->buf
);
2676 memset(info
->buf
, 0, info
->buflen
);
2677 /* set actual size to buffer size */
2678 info
->size
= info
->buflen
;
2680 fi
->fh
= (unsigned long)info
;
2684 static int proc_release(const char *path
, struct fuse_file_info
*fi
)
2686 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2688 do_release_file_info(f
);
2692 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2693 struct fuse_file_info
*fi
)
2695 struct file_info
*f
= (struct file_info
*) fi
->fh
;
2698 case LXC_TYPE_PROC_MEMINFO
:
2699 return proc_meminfo_read(buf
, size
, offset
, fi
);
2700 case LXC_TYPE_PROC_CPUINFO
:
2701 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
2702 case LXC_TYPE_PROC_UPTIME
:
2703 return proc_uptime_read(buf
, size
, offset
, fi
);
2704 case LXC_TYPE_PROC_STAT
:
2705 return proc_stat_read(buf
, size
, offset
, fi
);
2706 case LXC_TYPE_PROC_DISKSTATS
:
2707 return proc_diskstats_read(buf
, size
, offset
, fi
);
2715 * these just delegate to the /proc and /cgroup ops as
2719 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
2721 if (strcmp(path
, "/") == 0) {
2722 sb
->st_mode
= S_IFDIR
| 00755;
2726 if (strncmp(path
, "/cgroup", 7) == 0) {
2727 return cg_getattr(path
, sb
);
2729 if (strncmp(path
, "/proc", 5) == 0) {
2730 return proc_getattr(path
, sb
);
2735 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
2737 if (strcmp(path
, "/") == 0)
2740 if (strncmp(path
, "/cgroup", 7) == 0) {
2741 return cg_opendir(path
, fi
);
2743 if (strcmp(path
, "/proc") == 0)
2748 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2749 struct fuse_file_info
*fi
)
2751 if (strcmp(path
, "/") == 0) {
2752 if (filler(buf
, "proc", NULL
, 0) != 0 ||
2753 filler(buf
, "cgroup", NULL
, 0) != 0)
2757 if (strncmp(path
, "/cgroup", 7) == 0)
2758 return cg_readdir(path
, buf
, filler
, offset
, fi
);
2759 if (strcmp(path
, "/proc") == 0)
2760 return proc_readdir(path
, buf
, filler
, offset
, fi
);
2764 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
2766 if (strcmp(path
, "/") == 0)
2768 if (strncmp(path
, "/cgroup", 7) == 0) {
2769 return cg_releasedir(path
, fi
);
2771 if (strcmp(path
, "/proc") == 0)
2776 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
2778 if (strncmp(path
, "/cgroup", 7) == 0)
2779 return cg_open(path
, fi
);
2780 if (strncmp(path
, "/proc", 5) == 0)
2781 return proc_open(path
, fi
);
2786 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2787 struct fuse_file_info
*fi
)
2789 if (strncmp(path
, "/cgroup", 7) == 0)
2790 return cg_read(path
, buf
, size
, offset
, fi
);
2791 if (strncmp(path
, "/proc", 5) == 0)
2792 return proc_read(path
, buf
, size
, offset
, fi
);
2797 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2798 struct fuse_file_info
*fi
)
2800 if (strncmp(path
, "/cgroup", 7) == 0) {
2801 return cg_write(path
, buf
, size
, offset
, fi
);
2807 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
2812 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
2814 if (strncmp(path
, "/cgroup", 7) == 0)
2815 return cg_release(path
, fi
);
2816 if (strncmp(path
, "/proc", 5) == 0)
2817 return proc_release(path
, fi
);
2822 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
2827 int lxcfs_mkdir(const char *path
, mode_t mode
)
2829 if (strncmp(path
, "/cgroup", 7) == 0)
2830 return cg_mkdir(path
, mode
);
2835 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
2837 if (strncmp(path
, "/cgroup", 7) == 0)
2838 return cg_chown(path
, uid
, gid
);
2844 * cat first does a truncate before doing ops->write. This doesn't
2845 * really make sense for cgroups. So just return 0 always but do
2848 int lxcfs_truncate(const char *path
, off_t newsize
)
2850 if (strncmp(path
, "/cgroup", 7) == 0)
2855 int lxcfs_rmdir(const char *path
)
2857 if (strncmp(path
, "/cgroup", 7) == 0)
2858 return cg_rmdir(path
);
2862 int lxcfs_chmod(const char *path
, mode_t mode
)
2864 if (strncmp(path
, "/cgroup", 7) == 0)
2865 return cg_chmod(path
, mode
);
2869 const struct fuse_operations lxcfs_ops
= {
2870 .getattr
= lxcfs_getattr
,
2874 .mkdir
= lxcfs_mkdir
,
2876 .rmdir
= lxcfs_rmdir
,
2880 .chmod
= lxcfs_chmod
,
2881 .chown
= lxcfs_chown
,
2882 .truncate
= lxcfs_truncate
,
2887 .release
= lxcfs_release
,
2888 .write
= lxcfs_write
,
2891 .flush
= lxcfs_flush
,
2892 .fsync
= lxcfs_fsync
,
2897 .removexattr
= NULL
,
2899 .opendir
= lxcfs_opendir
,
2900 .readdir
= lxcfs_readdir
,
2901 .releasedir
= lxcfs_releasedir
,
2912 static void usage(const char *me
)
2914 fprintf(stderr
, "Usage:\n");
2915 fprintf(stderr
, "\n");
2916 fprintf(stderr
, "%s mountpoint\n", me
);
2917 fprintf(stderr
, "%s -h\n", me
);
2921 static bool is_help(char *w
)
2923 if (strcmp(w
, "-h") == 0 ||
2924 strcmp(w
, "--help") == 0 ||
2925 strcmp(w
, "-help") == 0 ||
2926 strcmp(w
, "help") == 0)
2931 void swallow_arg(int *argcp
, char *argv
[], char *which
)
2935 for (i
= 1; argv
[i
]; i
++) {
2936 if (strcmp(argv
[i
], which
) != 0)
2938 for (; argv
[i
]; i
++) {
2939 argv
[i
] = argv
[i
+1];
2946 void swallow_option(int *argcp
, char *argv
[], char *opt
, char *v
)
2950 for (i
= 1; argv
[i
]; i
++) {
2953 if (strcmp(argv
[i
], opt
) != 0)
2955 if (strcmp(argv
[i
+1], v
) != 0) {
2956 fprintf(stderr
, "Warning: unexpected fuse option %s\n", v
);
2959 for (; argv
[i
+1]; i
++) {
2960 argv
[i
] = argv
[i
+2];
2967 int main(int argc
, char *argv
[])
2970 struct lxcfs_state
*d
= NULL
;
2972 * what we pass to fuse_main is:
2973 * argv[0] -s -f -o allow_other,directio argv[1] NULL
2975 int nargs
= 5, cnt
= 0;
2978 /* accomodate older init scripts */
2979 swallow_arg(&argc
, argv
, "-s");
2980 swallow_arg(&argc
, argv
, "-f");
2981 swallow_option(&argc
, argv
, "-o", "allow_other");
2983 if (argc
== 2 && strcmp(argv
[1], "--version") == 0) {
2984 fprintf(stderr
, "%s\n", VERSION
);
2987 if (argc
!= 2 || is_help(argv
[1]))
2991 d
= malloc(sizeof(*d
));
2994 newargv
[cnt
++] = argv
[0];
2995 newargv
[cnt
++] = "-f";
2996 newargv
[cnt
++] = "-o";
2997 newargv
[cnt
++] = "allow_other,direct_io";
2998 newargv
[cnt
++] = argv
[1];
2999 newargv
[cnt
++] = NULL
;
3001 if (!cgm_escape_cgroup())
3002 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
3004 if (!cgm_get_controllers(&d
->subsystems
))
3007 ret
= fuse_main(nargs
, newargv
, &lxcfs_ops
, d
);
3008 cgm_dbus_disconnect();