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
60 /* reserve buffer size, for cpuall in /proc/stat */
61 #define BUF_RESERVE_SIZE 256
65 * src: a pointer to a char* in which ot append the pid.
66 * sz: the number of characters printed so far, minus trailing \0.
67 * asz: the allocated size so far
68 * pid: the pid to append
70 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
75 sprintf(tmp
, "%d\n", (int)pid
);
79 d
= malloc(BUF_RESERVE_SIZE
);
82 *asz
= BUF_RESERVE_SIZE
;
83 } else if (strlen(tmp
) + sz
+ 1 >= asz
) {
85 d
= realloc(d
, *asz
+ BUF_RESERVE_SIZE
);
88 *asz
+= BUF_RESERVE_SIZE
;
90 memcpy(d
+*sz
, tmp
, strlen(tmp
));
95 static char *must_copy_string(void *parent
, const char *str
)
107 static int wait_for_pid(pid_t pid
)
112 ret
= waitpid(pid
, &status
, 0);
120 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
126 * Given a open file * to /proc/pid/{u,g}id_map, and an id
127 * valid in the caller's namespace, return the id mapped into
129 * Returns the mapped id, or -1 on error.
132 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
134 unsigned int nsuid
, // base id for a range in the idfile's namespace
135 hostuid
, // base id for a range in the caller's namespace
136 count
; // number of ids in this range
140 fseek(idfile
, 0L, SEEK_SET
);
141 while (fgets(line
, 400, idfile
)) {
142 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
145 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
147 * uids wrapped around - unexpected as this is a procfile,
150 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s\n",
151 nsuid
, hostuid
, count
, line
);
154 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
156 * now since hostuid <= in_id < hostuid+count, and
157 * hostuid+count and nsuid+count do not wrap around,
158 * we know that nsuid+(in_id-hostuid) which must be
159 * less that nsuid+(count) must not wrap around
161 return (in_id
- hostuid
) + nsuid
;
170 * for is_privileged_over,
171 * specify whether we require the calling uid to be root in his
174 #define NS_ROOT_REQD true
175 #define NS_ROOT_OPT false
179 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
186 if (victim
== -1 || uid
== -1)
190 * If the request is one not requiring root in the namespace,
191 * then having the same uid suffices. (i.e. uid 1000 has write
192 * access to files owned by uid 1000
194 if (!req_ns_root
&& uid
== victim
)
197 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
198 if (ret
< 0 || ret
>= PROCLEN
)
200 FILE *f
= fopen(fpath
, "r");
204 /* if caller's not root in his namespace, reject */
205 nsuid
= convert_id_to_ns(f
, uid
);
210 * If victim is not mapped into caller's ns, reject.
211 * XXX I'm not sure this check is needed given that fuse
212 * will be sending requests where the vfs has converted
214 nsuid
= convert_id_to_ns(f
, victim
);
225 static bool perms_include(int fmode
, mode_t req_mode
)
229 switch (req_mode
& O_ACCMODE
) {
237 r
= S_IROTH
| S_IWOTH
;
242 return ((fmode
& r
) == r
);
245 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
249 if (strlen(taskcg
) <= strlen(querycg
)) {
250 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
254 if (strcmp(querycg
, "/") == 0)
255 start
= strdup(taskcg
+ 1);
257 start
= strdup(taskcg
+ strlen(querycg
) + 1);
260 end
= strchr(start
, '/');
266 static void stripnewline(char *x
)
268 size_t l
= strlen(x
);
269 if (l
&& x
[l
-1] == '\n')
273 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
282 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
283 if (ret
< 0 || ret
>= PROCLEN
)
285 if (!(f
= fopen(fnam
, "r")))
288 while (getline(&line
, &len
, f
) != -1) {
292 c1
= strchr(line
, ':');
296 c2
= strchr(c1
, ':');
300 if (strcmp(c1
, contrl
) != 0)
317 * check whether a fuse context may access a cgroup dir or file
319 * If file is not null, it is a cgroup file to check under cg.
320 * If file is null, then we are checking perms on cg itself.
322 * For files we can check the mode of the list_keys result.
323 * For cgroups, we must make assumptions based on the files under the
324 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
327 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
329 struct cgm_keys
**list
= NULL
;
339 if (!cgm_list_keys(contrl
, cg
, &list
))
341 for (i
= 0; list
[i
]; i
++) {
342 if (strcmp(list
[i
]->name
, file
) == 0) {
343 struct cgm_keys
*k
= list
[i
];
344 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
345 if (perms_include(k
->mode
>> 6, mode
)) {
350 if (fc
->gid
== k
->gid
) {
351 if (perms_include(k
->mode
>> 3, mode
)) {
356 ret
= perms_include(k
->mode
, mode
);
366 #define INITSCOPE "/init.scope"
367 static void prune_init_slice(char *cg
)
370 point
= cg
+ strlen(cg
) - strlen(INITSCOPE
);
373 if (strcmp(point
, INITSCOPE
) == 0) {
382 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
383 * If caller is in /a, he may act on /a/b, but not on /b.
384 * if the answer is false and nextcg is not NULL, then *nextcg will point
385 * to a string containing the next cgroup directory under cg, which must be
386 * freed by the caller.
388 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
397 ret
= snprintf(fnam
, PROCLEN
, "/proc/%d/cgroup", pid
);
398 if (ret
< 0 || ret
>= PROCLEN
)
400 if (!(f
= fopen(fnam
, "r")))
403 while (getline(&line
, &len
, f
) != -1) {
404 char *c1
, *c2
, *linecmp
;
407 c1
= strchr(line
, ':');
411 c2
= strchr(c1
, ':');
415 if (strcmp(c1
, contrl
) != 0)
419 prune_init_slice(c2
);
421 * callers pass in '/' for root cgroup, otherwise they pass
422 * in a cgroup without leading '/'
424 linecmp
= *cg
== '/' ? c2
: c2
+1;
425 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
427 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
441 * given /cgroup/freezer/a/b, return "freezer".
442 * the returned char* should NOT be freed.
444 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
449 if (strlen(path
) < 9)
451 if (*(path
+7) != '/')
457 slash
= strstr(contr
, "/");
461 /* verify that it is a subsystem */
462 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
466 for (i
= 0; list
[i
]; i
++) {
467 if (strcmp(list
[i
], contr
) == 0)
474 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
475 * Note that the returned value may include files (keynames) etc
477 static const char *find_cgroup_in_path(const char *path
)
481 if (strlen(path
) < 9)
483 p1
= strstr(path
+8, "/");
489 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
500 if (!cgm_list_children(contr
, dir
, &list
))
502 for (i
= 0; list
[i
]; i
++) {
503 if (strcmp(list
[i
], f
) == 0) {
510 for (i
= 0; list
[i
]; i
++)
516 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
518 struct cgm_keys
**list
= NULL
;
519 struct cgm_keys
*k
= NULL
;
526 if (!cgm_list_keys(contr
, dir
, &list
))
528 for (i
= 0; list
[i
]; i
++) {
529 if (strcmp(list
[i
]->name
, f
) == 0) {
531 // free all the keys we are not returning
533 for (j
= 0; list
[j
]; j
++) {
547 * dir should be freed, file not
549 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
556 *file
= strrchr(cg
, '/');
561 p
= strrchr(*dir
, '/');
566 * FUSE ops for /cgroup
569 static int cg_getattr(const char *path
, struct stat
*sb
)
572 struct fuse_context
*fc
= fuse_get_context();
574 char *fpath
= NULL
, *path1
, *path2
;
575 struct cgm_keys
*k
= NULL
;
577 const char *controller
= NULL
;
584 memset(sb
, 0, sizeof(struct stat
));
586 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
589 sb
->st_uid
= sb
->st_gid
= 0;
590 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
593 if (strcmp(path
, "/cgroup") == 0) {
594 sb
->st_mode
= S_IFDIR
| 00755;
599 controller
= pick_controller_from_path(fc
, path
);
602 cgroup
= find_cgroup_in_path(path
);
604 /* this is just /cgroup/controller, return it as a dir */
605 sb
->st_mode
= S_IFDIR
| 00755;
610 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
620 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
621 * Then check that caller's cgroup is under path if fpath is a child
622 * cgroup, or cgdir if fpath is a file */
624 if (is_child_cgroup(controller
, path1
, path2
)) {
625 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
626 /* this is just /cgroup/controller, return it as a dir */
627 sb
->st_mode
= S_IFDIR
| 00555;
632 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
637 // get uid, gid, from '/tasks' file and make up a mode
638 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
639 sb
->st_mode
= S_IFDIR
| 00755;
640 k
= get_cgroup_key(controller
, cgroup
, "tasks");
642 sb
->st_uid
= sb
->st_gid
= 0;
653 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
654 sb
->st_mode
= S_IFREG
| k
->mode
;
660 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
)) {
664 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
)) {
677 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
679 struct fuse_context
*fc
= fuse_get_context();
681 struct file_info
*dir_info
;
682 char *controller
= NULL
;
687 if (strcmp(path
, "/cgroup") == 0) {
691 // return list of keys for the controller, and list of child cgroups
692 controller
= pick_controller_from_path(fc
, path
);
696 cgroup
= find_cgroup_in_path(path
);
698 /* this is just /cgroup/controller, return its contents */
703 if (cgroup
&& !fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
707 /* we'll free this at cg_releasedir */
708 dir_info
= malloc(sizeof(*dir_info
));
711 dir_info
->controller
= must_copy_string(dir_info
, controller
);
712 dir_info
->cgroup
= must_copy_string(dir_info
, cgroup
);
713 dir_info
->type
= LXC_TYPE_CGDIR
;
714 dir_info
->buf
= NULL
;
715 dir_info
->file
= NULL
;
716 dir_info
->buflen
= 0;
718 fi
->fh
= (unsigned long)dir_info
;
722 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
723 struct fuse_file_info
*fi
)
725 struct file_info
*d
= (struct file_info
*)fi
->fh
;
726 struct cgm_keys
**list
= NULL
;
729 struct fuse_context
*fc
= fuse_get_context();
732 if (d
->type
!= LXC_TYPE_CGDIR
) {
733 fprintf(stderr
, "Internal error: file cache info used in readdir\n");
736 if (!d
->cgroup
&& !d
->controller
) {
737 // ls /var/lib/lxcfs/cgroup - just show list of controllers
738 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
744 for (i
= 0; list
[i
]; i
++) {
745 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
752 if (!cgm_list_keys(d
->controller
, d
->cgroup
, &list
)) {
753 // not a valid cgroup
758 if (!caller_is_in_ancestor(fc
->pid
, d
->controller
, d
->cgroup
, &nextcg
)) {
761 ret
= filler(buf
, nextcg
, NULL
, 0);
772 for (i
= 0; list
[i
]; i
++) {
773 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
779 // now get the list of child cgroups
781 if (!cgm_list_children(d
->controller
, d
->cgroup
, &clist
)) {
785 for (i
= 0; clist
[i
]; i
++) {
786 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
796 for (i
= 0; clist
[i
]; i
++)
803 static void do_release_file_info(struct file_info
*f
)
814 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
816 struct file_info
*d
= (struct file_info
*)fi
->fh
;
818 do_release_file_info(d
);
822 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
825 char *fpath
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
826 struct cgm_keys
*k
= NULL
;
827 struct file_info
*file_info
;
828 struct fuse_context
*fc
= fuse_get_context();
834 controller
= pick_controller_from_path(fc
, path
);
837 cgroup
= find_cgroup_in_path(path
);
841 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
850 k
= get_cgroup_key(controller
, path1
, path2
);
857 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
858 // should never get here
863 /* we'll free this at cg_release */
864 file_info
= malloc(sizeof(*file_info
));
869 file_info
->controller
= must_copy_string(file_info
, controller
);
870 file_info
->cgroup
= must_copy_string(file_info
, path1
);
871 file_info
->file
= must_copy_string(file_info
, path2
);
872 file_info
->type
= LXC_TYPE_CGFILE
;
873 file_info
->buf
= NULL
;
874 file_info
->buflen
= 0;
876 fi
->fh
= (unsigned long)file_info
;
884 static int cg_release(const char *path
, struct fuse_file_info
*fi
)
886 struct file_info
*f
= (struct file_info
*)fi
->fh
;
888 do_release_file_info(f
);
892 static int msgrecv(int sockfd
, void *buf
, size_t len
)
898 FD_SET(sockfd
, &rfds
);
902 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) <= 0)
904 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
907 #define SEND_CREDS_OK 0
908 #define SEND_CREDS_NOTSK 1
909 #define SEND_CREDS_FAIL 2
910 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
912 struct msghdr msg
= { 0 };
914 struct cmsghdr
*cmsg
;
915 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
920 if (msgrecv(sock
, buf
, 1) != 1) {
921 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
923 return SEND_CREDS_FAIL
;
927 msg
.msg_control
= cmsgbuf
;
928 msg
.msg_controllen
= sizeof(cmsgbuf
);
930 cmsg
= CMSG_FIRSTHDR(&msg
);
931 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
932 cmsg
->cmsg_level
= SOL_SOCKET
;
933 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
934 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
941 iov
.iov_len
= sizeof(buf
);
945 if (sendmsg(sock
, &msg
, 0) < 0) {
946 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
949 return SEND_CREDS_NOTSK
;
950 return SEND_CREDS_FAIL
;
953 return SEND_CREDS_OK
;
956 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
958 struct msghdr msg
= { 0 };
960 struct cmsghdr
*cmsg
;
961 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
974 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
975 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
979 if (write(sock
, buf
, 1) != 1) {
980 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
986 msg
.msg_control
= cmsgbuf
;
987 msg
.msg_controllen
= sizeof(cmsgbuf
);
990 iov
.iov_len
= sizeof(buf
);
998 if (select(sock
+1, &rfds
, NULL
, NULL
, &tv
) <= 0) {
999 fprintf(stderr
, "Failed to select for scm_cred: %s\n",
1003 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
1005 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
1010 cmsg
= CMSG_FIRSTHDR(&msg
);
1012 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
1013 cmsg
->cmsg_level
== SOL_SOCKET
&&
1014 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1015 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
1024 * pid_to_ns - reads pids from a ucred over a socket, then writes the
1025 * int value back over the socket. This shifts the pid from the
1026 * sender's pidns into tpid's pidns.
1028 static void pid_to_ns(int sock
, pid_t tpid
)
1033 while (recv_creds(sock
, &cred
, &v
)) {
1036 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
1043 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
1044 * in your old pidns. Only children which you fork will be in the target
1045 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
1046 * actually convert pids
1048 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
1050 int newnsfd
= -1, ret
, cpipe
[2];
1057 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1058 if (ret
< 0 || ret
>= sizeof(fnam
))
1060 newnsfd
= open(fnam
, O_RDONLY
);
1063 if (setns(newnsfd
, 0) < 0)
1067 if (pipe(cpipe
) < 0)
1078 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1079 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1080 __func__
, strerror(errno
));
1083 pid_to_ns(sock
, tpid
);
1085 // give the child 1 second to be done forking and
1088 FD_SET(cpipe
[0], &s
);
1091 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1094 ret
= read(cpipe
[0], &v
, 1);
1095 if (ret
!= sizeof(char) || v
!= '1') {
1099 if (!wait_for_pid(cpid
))
1104 kill(cpid
, SIGKILL
);
1110 * To read cgroup files with a particular pid, we will setns into the child
1111 * pidns, open a pipe, fork a child - which will be the first to really be in
1112 * the child ns - which does the cgm_get_value and writes the data to the pipe.
1114 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
1116 int sock
[2] = {-1, -1};
1117 char *tmpdata
= NULL
;
1119 pid_t qpid
, cpid
= -1;
1120 bool answer
= false;
1124 size_t sz
= 0, asz
= 0;
1127 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
1131 * Now we read the pids from returned data one by one, pass
1132 * them into a child in the target namespace, read back the
1133 * translated pids, and put them into our to-return data
1136 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1137 perror("socketpair");
1147 pid_to_ns_wrapper(sock
[1], tpid
);
1149 char *ptr
= tmpdata
;
1152 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
1154 ret
= send_creds(sock
[0], &cred
, v
, true);
1156 if (ret
== SEND_CREDS_NOTSK
)
1158 if (ret
== SEND_CREDS_FAIL
)
1161 // read converted results
1163 FD_SET(sock
[0], &s
);
1166 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
1168 fprintf(stderr
, "%s: select error waiting for pid from child: %s\n",
1169 __func__
, strerror(errno
));
1172 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1173 fprintf(stderr
, "%s: error reading pid from child: %s\n",
1174 __func__
, strerror(errno
));
1177 must_strcat_pid(d
, &sz
, &asz
, qpid
);
1179 ptr
= strchr(ptr
, '\n');
1185 cred
.pid
= getpid();
1187 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
1188 // failed to ask child to exit
1189 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
1190 __func__
, strerror(errno
));
1200 if (sock
[0] != -1) {
1207 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1208 struct fuse_file_info
*fi
)
1210 struct fuse_context
*fc
= fuse_get_context();
1211 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1212 struct cgm_keys
*k
= NULL
;
1217 if (f
->type
!= LXC_TYPE_CGFILE
) {
1218 fprintf(stderr
, "Internal error: directory cache info used in cg_read\n");
1231 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1237 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) { // should never get here
1242 if (strcmp(f
->file
, "tasks") == 0 ||
1243 strcmp(f
->file
, "/tasks") == 0 ||
1244 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1245 strcmp(f
->file
, "cgroup.procs") == 0)
1246 // special case - we have to translate the pids
1247 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1249 r
= cgm_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
1263 memcpy(buf
, data
, s
);
1264 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
1274 static void pid_from_ns(int sock
, pid_t tpid
)
1290 ret
= select(sock
+1, &s
, NULL
, NULL
, &tv
);
1292 fprintf(stderr
, "%s: bad select before read from parent: %s\n",
1293 __func__
, strerror(errno
));
1296 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1297 fprintf(stderr
, "%s: bad read from parent: %s\n",
1298 __func__
, strerror(errno
));
1301 if (vpid
== -1) // done
1305 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1307 cred
.pid
= getpid();
1308 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1315 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1317 int newnsfd
= -1, ret
, cpipe
[2];
1324 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1325 if (ret
< 0 || ret
>= sizeof(fnam
))
1327 newnsfd
= open(fnam
, O_RDONLY
);
1330 if (setns(newnsfd
, 0) < 0)
1334 if (pipe(cpipe
) < 0)
1346 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1347 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1348 __func__
, strerror(errno
));
1351 pid_from_ns(sock
, tpid
);
1354 // give the child 1 second to be done forking and
1357 FD_SET(cpipe
[0], &s
);
1360 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1363 ret
= read(cpipe
[0], &v
, 1);
1364 if (ret
!= sizeof(char) || v
!= '1') {
1368 if (!wait_for_pid(cpid
))
1373 kill(cpid
, SIGKILL
);
1378 static bool do_write_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, const char *buf
)
1380 int sock
[2] = {-1, -1};
1381 pid_t qpid
, cpid
= -1;
1382 bool answer
= false, fail
= false;
1385 * write the pids to a socket, have helper in writer's pidns
1386 * call movepid for us
1388 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1389 perror("socketpair");
1398 pid_from_ns_wrapper(sock
[1], tpid
);
1400 const char *ptr
= buf
;
1401 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1405 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1406 fprintf(stderr
, "%s: error writing pid to child: %s\n",
1407 __func__
, strerror(errno
));
1411 if (recv_creds(sock
[0], &cred
, &v
)) {
1413 if (!cgm_move_pid(contrl
, cg
, cred
.pid
))
1418 ptr
= strchr(ptr
, '\n');
1424 /* All good, write the value */
1426 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1427 fprintf(stderr
, "Warning: failed to ask child to exit\n");
1435 if (sock
[0] != -1) {
1442 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1443 struct fuse_file_info
*fi
)
1445 struct fuse_context
*fc
= fuse_get_context();
1446 char *localbuf
= NULL
;
1447 struct cgm_keys
*k
= NULL
;
1448 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1451 if (f
->type
!= LXC_TYPE_CGFILE
) {
1452 fprintf(stderr
, "Internal error: directory cache info used in cg_write\n");
1462 localbuf
= alloca(size
+1);
1463 localbuf
[size
] = '\0';
1464 memcpy(localbuf
, buf
, size
);
1466 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1471 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
1476 if (strcmp(f
->file
, "tasks") == 0 ||
1477 strcmp(f
->file
, "/tasks") == 0 ||
1478 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1479 strcmp(f
->file
, "cgroup.procs") == 0)
1480 // special case - we have to translate the pids
1481 r
= do_write_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1483 r
= cgm_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1493 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1495 struct fuse_context
*fc
= fuse_get_context();
1496 char *cgdir
= NULL
, *fpath
= NULL
, *path1
, *path2
, *controller
;
1497 struct cgm_keys
*k
= NULL
;
1504 if (strcmp(path
, "/cgroup") == 0)
1507 controller
= pick_controller_from_path(fc
, path
);
1510 cgroup
= find_cgroup_in_path(path
);
1512 /* this is just /cgroup/controller */
1515 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1525 if (is_child_cgroup(controller
, path1
, path2
)) {
1526 // get uid, gid, from '/tasks' file and make up a mode
1527 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1528 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1531 k
= get_cgroup_key(controller
, path1
, path2
);
1539 * This being a fuse request, the uid and gid must be valid
1540 * in the caller's namespace. So we can just check to make
1541 * sure that the caller is root in his uid, and privileged
1542 * over the file's current owner.
1544 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
1549 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
)) {
1563 int cg_chmod(const char *path
, mode_t mode
)
1565 struct fuse_context
*fc
= fuse_get_context();
1566 char * cgdir
= NULL
, *fpath
= NULL
, *path1
, *path2
, *controller
;
1567 struct cgm_keys
*k
= NULL
;
1574 if (strcmp(path
, "/cgroup") == 0)
1577 controller
= pick_controller_from_path(fc
, path
);
1580 cgroup
= find_cgroup_in_path(path
);
1582 /* this is just /cgroup/controller */
1585 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1595 if (is_child_cgroup(controller
, path1
, path2
)) {
1596 // get uid, gid, from '/tasks' file and make up a mode
1597 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1598 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1601 k
= get_cgroup_key(controller
, path1
, path2
);
1609 * This being a fuse request, the uid and gid must be valid
1610 * in the caller's namespace. So we can just check to make
1611 * sure that the caller is root in his uid, and privileged
1612 * over the file's current owner.
1614 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
1619 if (!cgm_chmod_file(controller
, cgroup
, mode
)) {
1631 #define LXCFS_MKDIR_PATH LIBEXECDIR "/lxcfs/lxcfs_mkdir"
1633 int cg_mkdir(const char *path
, mode_t mode
)
1635 struct fuse_context
*fc
= fuse_get_context();
1636 char *fpath
= NULL
, *path1
, *cgdir
= NULL
, *controller
;
1644 controller
= pick_controller_from_path(fc
, path
);
1648 cgroup
= find_cgroup_in_path(path
);
1652 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1658 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
1662 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
)) {
1667 if (fc
->uid
== 0 && fc
->gid
== 0) {
1668 if (!cgm_create(controller
, cgroup
)) {
1674 * exec a helper so as to get a clean dbus connection
1675 * 17 for lxcfs_mkdir, and spaces and newline and \0. 50 for two ints.
1678 size_t len
= strlen(cgroup
) + strlen(controller
) + 17 + 50;
1679 char *cmd
= alloca(len
);
1680 ret
= snprintf(cmd
, len
, "%s %d %d %s %s\n", LXCFS_MKDIR_PATH
,
1681 fc
->uid
, fc
->gid
, controller
, cgroup
);
1682 if (ret
< 0 || ret
>= len
) {
1698 static int cg_rmdir(const char *path
)
1700 struct fuse_context
*fc
= fuse_get_context();
1701 char *fpath
= NULL
, *cgdir
= NULL
, *controller
;
1708 controller
= pick_controller_from_path(fc
, path
);
1712 cgroup
= find_cgroup_in_path(path
);
1716 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1722 fprintf(stderr
, "rmdir: verifying access to %s:%s (req path %s)\n",
1723 controller
, cgdir
, path
);
1724 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
1728 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
1733 if (!cgm_remove(controller
, cgroup
)) {
1745 static bool startswith(const char *line
, const char *pref
)
1747 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1752 static void get_mem_cached(char *memstat
, unsigned long *v
)
1758 if (startswith(memstat
, "total_cache")) {
1759 sscanf(memstat
+ 11, "%lu", v
);
1763 eol
= strchr(memstat
, '\n');
1770 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
1776 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
1778 size_t len
= strlen(key
);
1782 if (startswith(str
, key
)) {
1783 sscanf(str
+ len
, "%lu", v
);
1786 eol
= strchr(str
, '\n');
1793 static int read_file(const char *path
, char *buf
, size_t size
,
1794 struct file_info
*d
)
1796 size_t linelen
= 0, total_len
= 0, rv
= 0;
1798 char *cache
= d
->buf
;
1799 size_t cache_size
= d
->buflen
;
1800 FILE *f
= fopen(path
, "r");
1804 while (getline(&line
, &linelen
, f
) != -1) {
1805 size_t l
= snprintf(cache
, cache_size
, "%s", line
);
1807 perror("Error writing to cache");
1811 if (l
>= cache_size
) {
1812 fprintf(stderr
, "Internal error: truncated write to cache\n");
1816 if (l
< cache_size
) {
1821 cache
+= cache_size
;
1822 total_len
+= cache_size
;
1828 d
->size
= total_len
;
1829 if (total_len
> size
) total_len
= size
;
1831 /* read from off 0 */
1832 memcpy(buf
, d
->buf
, total_len
);
1841 * FUSE ops for /proc
1844 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1845 struct fuse_file_info
*fi
)
1847 struct fuse_context
*fc
= fuse_get_context();
1848 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1850 char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1851 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1853 size_t linelen
= 0, total_len
= 0, rv
= 0;
1854 char *cache
= d
->buf
;
1855 size_t cache_size
= d
->buflen
;
1859 if (offset
> d
->size
)
1861 int left
= d
->size
- offset
;
1862 total_len
= left
> size
? size
: left
;
1863 memcpy(buf
, cache
+ offset
, total_len
);
1867 cg
= get_pid_cgroup(fc
->pid
, "memory");
1869 return read_file("/proc/meminfo", buf
, size
, d
);
1871 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1873 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1875 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1877 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1878 memusage
= strtoul(memusage_str
, NULL
, 10);
1881 get_mem_cached(memstat_str
, &cached
);
1883 f
= fopen("/proc/meminfo", "r");
1887 while (getline(&line
, &linelen
, f
) != -1) {
1889 char *printme
, lbuf
[100];
1891 memset(lbuf
, 0, 100);
1892 if (startswith(line
, "MemTotal:")) {
1893 sscanf(line
+14, "%lu", &hosttotal
);
1894 if (hosttotal
< memlimit
)
1895 memlimit
= hosttotal
;
1896 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1898 } else if (startswith(line
, "MemFree:")) {
1899 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1901 } else if (startswith(line
, "MemAvailable:")) {
1902 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1904 } else if (startswith(line
, "Buffers:")) {
1905 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1907 } else if (startswith(line
, "Cached:")) {
1908 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1910 } else if (startswith(line
, "SwapCached:")) {
1911 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1916 l
= snprintf(cache
, cache_size
, "%s", printme
);
1918 perror("Error writing to cache");
1923 if (l
>= cache_size
) {
1924 fprintf(stderr
, "Internal error: truncated write to cache\n");
1934 d
->size
= total_len
;
1935 if (total_len
> size
) total_len
= size
;
1936 memcpy(buf
, d
->buf
, total_len
);
1951 * Read the cpuset.cpus for cg
1952 * Return the answer in a newly allocated string which must be freed
1954 static char *get_cpuset(const char *cg
)
1958 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1963 bool cpu_in_cpuset(int cpu
, const char *cpuset
);
1965 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1969 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1971 return cpu_in_cpuset(cpu
, cpuset
);
1975 * check whether this is a '^processor" line in /proc/cpuinfo
1977 static bool is_processor_line(const char *line
)
1981 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1986 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1987 struct fuse_file_info
*fi
)
1989 struct fuse_context
*fc
= fuse_get_context();
1990 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1992 char *cpuset
= NULL
;
1994 size_t linelen
= 0, total_len
= 0, rv
= 0;
1995 bool am_printing
= false;
1997 char *cache
= d
->buf
;
1998 size_t cache_size
= d
->buflen
;
2002 if (offset
> d
->size
)
2004 int left
= d
->size
- offset
;
2005 total_len
= left
> size
? size
: left
;
2006 memcpy(buf
, cache
+ offset
, total_len
);
2010 cg
= get_pid_cgroup(fc
->pid
, "cpuset");
2012 return read_file("proc/cpuinfo", buf
, size
, d
);
2014 cpuset
= get_cpuset(cg
);
2018 f
= fopen("/proc/cpuinfo", "r");
2022 while (getline(&line
, &linelen
, f
) != -1) {
2024 if (is_processor_line(line
)) {
2025 am_printing
= cpuline_in_cpuset(line
, cpuset
);
2028 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
2030 perror("Error writing to cache");
2034 if (l
>= cache_size
) {
2035 fprintf(stderr
, "Internal error: truncated write to cache\n");
2039 if (l
< cache_size
){
2044 cache
+= cache_size
;
2045 total_len
+= cache_size
;
2053 l
= snprintf(cache
, cache_size
, "%s", line
);
2055 perror("Error writing to cache");
2059 if (l
>= cache_size
) {
2060 fprintf(stderr
, "Internal error: truncated write to cache\n");
2064 if (l
< cache_size
) {
2069 cache
+= cache_size
;
2070 total_len
+= cache_size
;
2077 d
->size
= total_len
;
2078 if (total_len
> size
) total_len
= size
;
2080 /* read from off 0 */
2081 memcpy(buf
, d
->buf
, total_len
);
2092 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
2093 struct fuse_file_info
*fi
)
2095 struct fuse_context
*fc
= fuse_get_context();
2096 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2098 char *cpuset
= NULL
;
2100 size_t linelen
= 0, total_len
= 0, rv
= 0;
2101 int curcpu
= -1; /* cpu numbering starts at 0 */
2102 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0;
2103 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
2104 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0;
2105 #define CPUALL_MAX_SIZE BUF_RESERVE_SIZE
2106 char cpuall
[CPUALL_MAX_SIZE
];
2107 /* reserve for cpu all */
2108 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
2109 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
2113 if (offset
> d
->size
)
2115 int left
= d
->size
- offset
;
2116 total_len
= left
> size
? size
: left
;
2117 memcpy(buf
, d
->buf
+ offset
, total_len
);
2121 cg
= get_pid_cgroup(fc
->pid
, "cpuset");
2123 return read_file("/proc/stat", buf
, size
, d
);
2125 cpuset
= get_cpuset(cg
);
2129 f
= fopen("/proc/stat", "r");
2134 if (getline(&line
, &linelen
, f
) < 0) {
2135 fprintf(stderr
, "proc_stat_read read first line failed\n");
2139 while (getline(&line
, &linelen
, f
) != -1) {
2142 char cpu_char
[10]; /* That's a lot of cores */
2145 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
2146 /* not a ^cpuN line containing a number N, just print it */
2147 l
= snprintf(cache
, cache_size
, "%s", line
);
2149 perror("Error writing to cache");
2153 if (l
>= cache_size
) {
2154 fprintf(stderr
, "Internal error: truncated write to cache\n");
2158 if (l
< cache_size
) {
2164 //no more space, break it
2165 cache
+= cache_size
;
2166 total_len
+= cache_size
;
2172 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
2174 if (!cpu_in_cpuset(cpu
, cpuset
))
2178 c
= strchr(line
, ' ');
2181 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
2183 perror("Error writing to cache");
2188 if (l
>= cache_size
) {
2189 fprintf(stderr
, "Internal error: truncated write to cache\n");
2198 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user
, &nice
, &system
, &idle
, &iowait
, &irq
,
2199 &softirq
, &steal
, &guest
) != 9)
2203 system_sum
+= system
;
2205 iowait_sum
+= iowait
;
2207 softirq_sum
+= softirq
;
2214 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2215 "cpu ", user_sum
, nice_sum
, system_sum
, idle_sum
, iowait_sum
, irq_sum
, softirq_sum
, steal_sum
, guest_sum
);
2216 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
){
2217 memcpy(cache
, cpuall
, cpuall_len
);
2218 cache
+= cpuall_len
;
2220 /* shouldn't happen */
2221 fprintf(stderr
, "proc_stat_read copy cpuall failed, cpuall_len=%d\n", cpuall_len
);
2225 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
2226 total_len
+= cpuall_len
;
2227 d
->size
= total_len
;
2228 if (total_len
> size
) total_len
= size
;
2230 memcpy(buf
, d
->buf
, total_len
);
2243 * How to guess what to present for uptime?
2244 * One thing we could do would be to take the date on the caller's
2245 * memory.usage_in_bytes file, which should equal the time of creation
2246 * of his cgroup. However, a task could be in a sub-cgroup of the
2247 * container. The same problem exists if we try to look at the ages
2248 * of processes in the caller's cgroup.
2250 * So we'll fork a task that will enter the caller's pidns, mount a
2251 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
2253 * For the second uptime #, we'll do as Stéphane had done, just copy
2254 * the number from /proc/uptime. Not sure how to best emulate 'idle'
2255 * time. Maybe someone can come up with a good algorithm and submit a
2256 * patch. Maybe something based on cpushare info?
2259 /* return age of the reaper for $pid, taken from ctime of its procdir */
2260 static long int get_pid1_time(pid_t pid
)
2263 int fd
, cpipe
[2], ret
;
2270 if (unshare(CLONE_NEWNS
))
2273 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
2274 perror("rslave mount failed");
2278 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", pid
);
2279 if (ret
< 0 || ret
>= sizeof(fnam
))
2282 fd
= open(fnam
, O_RDONLY
);
2284 perror("get_pid1_time open of ns/pid");
2288 perror("get_pid1_time setns 1");
2294 if (pipe(cpipe
) < 0)
2305 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
2306 fprintf(stderr
, "%s (child): erorr on write: %s\n",
2307 __func__
, strerror(errno
));
2310 umount2("/proc", MNT_DETACH
);
2311 if (mount("proc", "/proc", "proc", 0, NULL
)) {
2312 perror("get_pid1_time mount");
2315 ret
= lstat("/proc/1", &sb
);
2317 perror("get_pid1_time lstat");
2320 return time(NULL
) - sb
.st_ctime
;
2323 // give the child 1 second to be done forking and
2326 FD_SET(cpipe
[0], &s
);
2329 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2332 ret
= read(cpipe
[0], &v
, 1);
2333 if (ret
!= sizeof(char) || v
!= '1') {
2341 kill(cpid
, SIGKILL
);
2346 static long int getreaperage(pid_t qpid
)
2348 int pid
, mypipe
[2], ret
;
2351 long int mtime
, answer
= 0;
2359 if (!pid
) { // child
2360 mtime
= get_pid1_time(qpid
);
2361 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
2362 fprintf(stderr
, "Warning: bad write from getreaperage\n");
2368 FD_SET(mypipe
[0], &s
);
2371 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2377 fprintf(stderr
, "timed out\n");
2380 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
2392 static unsigned long int getprocidle(void)
2394 FILE *f
= fopen("/proc/uptime", "r");
2395 unsigned long int age
, idle
;
2396 unsigned long int age_nsec
, idle_nsec
;
2401 ret
= fscanf(f
, "%lu.%02lu %lu.%02lu", &age
, &age_nsec
, &idle
, &idle_nsec
);
2409 * We read /proc/uptime and reuse its second field.
2410 * For the first field, we use the mtime for the reaper for
2411 * the calling pid as returned by getreaperage
2413 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
2414 struct fuse_file_info
*fi
)
2416 struct fuse_context
*fc
= fuse_get_context();
2417 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2418 long int reaperage
= getreaperage(fc
->pid
);;
2419 unsigned long int idletime
= getprocidle();
2420 size_t total_len
= 0;
2423 if (offset
> d
->size
)
2428 total_len
= snprintf(buf
, size
, "%ld.0 %lu.0\n", reaperage
, idletime
);
2430 perror("Error writing to cache");
2433 if (total_len
>= size
){
2438 d
->size
= total_len
;
2442 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
2443 struct fuse_file_info
*fi
)
2446 struct fuse_context
*fc
= fuse_get_context();
2447 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2449 char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
2450 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
2451 unsigned long read
= 0, write
= 0;
2452 unsigned long read_merged
= 0, write_merged
= 0;
2453 unsigned long read_sectors
= 0, write_sectors
= 0;
2454 unsigned long read_ticks
= 0, write_ticks
= 0;
2455 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
2456 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
2458 size_t linelen
= 0, total_len
= 0, rv
= 0;
2459 unsigned int major
= 0, minor
= 0;
2464 if (offset
> d
->size
)
2469 cg
= get_pid_cgroup(fc
->pid
, "blkio");
2471 return read_file("/proc/diskstats", buf
, size
, d
);
2473 if (!cgm_get_value("blkio", cg
, "blkio.io_serviced", &io_serviced_str
))
2475 if (!cgm_get_value("blkio", cg
, "blkio.io_merged", &io_merged_str
))
2477 if (!cgm_get_value("blkio", cg
, "blkio.io_service_bytes", &io_service_bytes_str
))
2479 if (!cgm_get_value("blkio", cg
, "blkio.io_wait_time", &io_wait_time_str
))
2481 if (!cgm_get_value("blkio", cg
, "blkio.io_service_time", &io_service_time_str
))
2485 f
= fopen("/proc/diskstats", "r");
2489 while (getline(&line
, &linelen
, f
) != -1) {
2491 char *printme
, lbuf
[256];
2493 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
2495 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
2496 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
2497 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
2498 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
2499 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
2500 read_sectors
= read_sectors
/512;
2501 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
2502 write_sectors
= write_sectors
/512;
2504 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
2505 rd_svctm
= rd_svctm
/1000000;
2506 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
2507 rd_wait
= rd_wait
/1000000;
2508 read_ticks
= rd_svctm
+ rd_wait
;
2510 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
2511 wr_svctm
= wr_svctm
/1000000;
2512 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
2513 wr_wait
= wr_wait
/1000000;
2514 write_ticks
= wr_svctm
+ wr_wait
;
2516 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
2517 tot_ticks
= tot_ticks
/1000000;
2522 memset(lbuf
, 0, 256);
2523 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
) {
2524 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2525 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
2526 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
2531 l
= snprintf(buf
, size
, "%s", printme
);
2533 perror("Error writing to fuse buf");
2538 fprintf(stderr
, "Internal error: truncated write to cache\n");
2547 d
->size
= total_len
;
2554 free(io_serviced_str
);
2555 free(io_merged_str
);
2556 free(io_service_bytes_str
);
2557 free(io_wait_time_str
);
2558 free(io_service_time_str
);
2562 static off_t
get_procfile_size(const char *which
)
2564 FILE *f
= fopen(which
, "r");
2567 ssize_t sz
, answer
= 0;
2571 while ((sz
= getline(&line
, &len
, f
)) != -1)
2579 static int proc_getattr(const char *path
, struct stat
*sb
)
2581 struct timespec now
;
2583 memset(sb
, 0, sizeof(struct stat
));
2584 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
2586 sb
->st_uid
= sb
->st_gid
= 0;
2587 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
2588 if (strcmp(path
, "/proc") == 0) {
2589 sb
->st_mode
= S_IFDIR
| 00555;
2593 if (strcmp(path
, "/proc/meminfo") == 0 ||
2594 strcmp(path
, "/proc/cpuinfo") == 0 ||
2595 strcmp(path
, "/proc/uptime") == 0 ||
2596 strcmp(path
, "/proc/stat") == 0 ||
2597 strcmp(path
, "/proc/diskstats") == 0) {
2599 sb
->st_mode
= S_IFREG
| 00444;
2607 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2608 struct fuse_file_info
*fi
)
2610 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
2611 filler(buf
, "meminfo", NULL
, 0) != 0 ||
2612 filler(buf
, "stat", NULL
, 0) != 0 ||
2613 filler(buf
, "uptime", NULL
, 0) != 0 ||
2614 filler(buf
, "diskstats", NULL
, 0) != 0)
2619 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
2622 struct file_info
*info
;
2624 if (strcmp(path
, "/proc/meminfo") == 0)
2625 type
= LXC_TYPE_PROC_MEMINFO
;
2626 else if (strcmp(path
, "/proc/cpuinfo") == 0)
2627 type
= LXC_TYPE_PROC_CPUINFO
;
2628 else if (strcmp(path
, "/proc/uptime") == 0)
2629 type
= LXC_TYPE_PROC_UPTIME
;
2630 else if (strcmp(path
, "/proc/stat") == 0)
2631 type
= LXC_TYPE_PROC_STAT
;
2632 else if (strcmp(path
, "/proc/diskstats") == 0)
2633 type
= LXC_TYPE_PROC_DISKSTATS
;
2637 info
= malloc(sizeof(*info
));
2641 memset(info
, 0, sizeof(*info
));
2644 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
2646 info
->buf
= malloc(info
->buflen
);
2647 } while (!info
->buf
);
2648 memset(info
->buf
, 0, info
->buflen
);
2649 /* set actual size to buffer size */
2650 info
->size
= info
->buflen
;
2652 fi
->fh
= (unsigned long)info
;
2656 static int proc_release(const char *path
, struct fuse_file_info
*fi
)
2658 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2660 do_release_file_info(f
);
2664 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2665 struct fuse_file_info
*fi
)
2667 struct file_info
*f
= (struct file_info
*) fi
->fh
;
2670 case LXC_TYPE_PROC_MEMINFO
:
2671 return proc_meminfo_read(buf
, size
, offset
, fi
);
2672 case LXC_TYPE_PROC_CPUINFO
:
2673 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
2674 case LXC_TYPE_PROC_UPTIME
:
2675 return proc_uptime_read(buf
, size
, offset
, fi
);
2676 case LXC_TYPE_PROC_STAT
:
2677 return proc_stat_read(buf
, size
, offset
, fi
);
2678 case LXC_TYPE_PROC_DISKSTATS
:
2679 return proc_diskstats_read(buf
, size
, offset
, fi
);
2687 * these just delegate to the /proc and /cgroup ops as
2691 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
2693 if (strcmp(path
, "/") == 0) {
2694 sb
->st_mode
= S_IFDIR
| 00755;
2698 if (strncmp(path
, "/cgroup", 7) == 0) {
2699 return cg_getattr(path
, sb
);
2701 if (strncmp(path
, "/proc", 5) == 0) {
2702 return proc_getattr(path
, sb
);
2707 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
2709 if (strcmp(path
, "/") == 0)
2712 if (strncmp(path
, "/cgroup", 7) == 0) {
2713 return cg_opendir(path
, fi
);
2715 if (strcmp(path
, "/proc") == 0)
2720 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2721 struct fuse_file_info
*fi
)
2723 if (strcmp(path
, "/") == 0) {
2724 if (filler(buf
, "proc", NULL
, 0) != 0 ||
2725 filler(buf
, "cgroup", NULL
, 0) != 0)
2729 if (strncmp(path
, "/cgroup", 7) == 0)
2730 return cg_readdir(path
, buf
, filler
, offset
, fi
);
2731 if (strcmp(path
, "/proc") == 0)
2732 return proc_readdir(path
, buf
, filler
, offset
, fi
);
2736 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
2738 if (strcmp(path
, "/") == 0)
2740 if (strncmp(path
, "/cgroup", 7) == 0) {
2741 return cg_releasedir(path
, fi
);
2743 if (strcmp(path
, "/proc") == 0)
2748 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
2750 if (strncmp(path
, "/cgroup", 7) == 0)
2751 return cg_open(path
, fi
);
2752 if (strncmp(path
, "/proc", 5) == 0)
2753 return proc_open(path
, fi
);
2758 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2759 struct fuse_file_info
*fi
)
2761 if (strncmp(path
, "/cgroup", 7) == 0)
2762 return cg_read(path
, buf
, size
, offset
, fi
);
2763 if (strncmp(path
, "/proc", 5) == 0)
2764 return proc_read(path
, buf
, size
, offset
, fi
);
2769 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2770 struct fuse_file_info
*fi
)
2772 if (strncmp(path
, "/cgroup", 7) == 0) {
2773 return cg_write(path
, buf
, size
, offset
, fi
);
2779 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
2784 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
2786 if (strncmp(path
, "/cgroup", 7) == 0)
2787 return cg_release(path
, fi
);
2788 if (strncmp(path
, "/proc", 5) == 0)
2789 return proc_release(path
, fi
);
2794 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
2799 int lxcfs_mkdir(const char *path
, mode_t mode
)
2801 if (strncmp(path
, "/cgroup", 7) == 0)
2802 return cg_mkdir(path
, mode
);
2807 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
2809 if (strncmp(path
, "/cgroup", 7) == 0)
2810 return cg_chown(path
, uid
, gid
);
2816 * cat first does a truncate before doing ops->write. This doesn't
2817 * really make sense for cgroups. So just return 0 always but do
2820 int lxcfs_truncate(const char *path
, off_t newsize
)
2822 if (strncmp(path
, "/cgroup", 7) == 0)
2827 int lxcfs_rmdir(const char *path
)
2829 if (strncmp(path
, "/cgroup", 7) == 0)
2830 return cg_rmdir(path
);
2834 int lxcfs_chmod(const char *path
, mode_t mode
)
2836 if (strncmp(path
, "/cgroup", 7) == 0)
2837 return cg_chmod(path
, mode
);
2841 const struct fuse_operations lxcfs_ops
= {
2842 .getattr
= lxcfs_getattr
,
2846 .mkdir
= lxcfs_mkdir
,
2848 .rmdir
= lxcfs_rmdir
,
2852 .chmod
= lxcfs_chmod
,
2853 .chown
= lxcfs_chown
,
2854 .truncate
= lxcfs_truncate
,
2859 .release
= lxcfs_release
,
2860 .write
= lxcfs_write
,
2863 .flush
= lxcfs_flush
,
2864 .fsync
= lxcfs_fsync
,
2869 .removexattr
= NULL
,
2871 .opendir
= lxcfs_opendir
,
2872 .readdir
= lxcfs_readdir
,
2873 .releasedir
= lxcfs_releasedir
,
2884 static void usage(const char *me
)
2886 fprintf(stderr
, "Usage:\n");
2887 fprintf(stderr
, "\n");
2888 fprintf(stderr
, "%s mountpoint\n", me
);
2889 fprintf(stderr
, "%s -h\n", me
);
2893 static bool is_help(char *w
)
2895 if (strcmp(w
, "-h") == 0 ||
2896 strcmp(w
, "--help") == 0 ||
2897 strcmp(w
, "-help") == 0 ||
2898 strcmp(w
, "help") == 0)
2903 void swallow_arg(int *argcp
, char *argv
[], char *which
)
2907 for (i
= 1; argv
[i
]; i
++) {
2908 if (strcmp(argv
[i
], which
) != 0)
2910 for (; argv
[i
]; i
++) {
2911 argv
[i
] = argv
[i
+1];
2918 void swallow_option(int *argcp
, char *argv
[], char *opt
, char *v
)
2922 for (i
= 1; argv
[i
]; i
++) {
2925 if (strcmp(argv
[i
], opt
) != 0)
2927 if (strcmp(argv
[i
+1], v
) != 0) {
2928 fprintf(stderr
, "Warning: unexpected fuse option %s\n", v
);
2931 for (; argv
[i
+1]; i
++) {
2932 argv
[i
] = argv
[i
+2];
2939 int main(int argc
, char *argv
[])
2942 struct lxcfs_state
*d
= NULL
;
2944 * what we pass to fuse_main is:
2945 * argv[0] -s -f -o allow_other,directio argv[1] NULL
2947 int nargs
= 5, cnt
= 0;
2950 /* accomodate older init scripts */
2951 swallow_arg(&argc
, argv
, "-s");
2952 swallow_arg(&argc
, argv
, "-f");
2953 swallow_option(&argc
, argv
, "-o", "allow_other");
2955 if (argc
== 2 && strcmp(argv
[1], "--version") == 0) {
2956 fprintf(stderr
, "%s\n", VERSION
);
2959 if (argc
!= 2 || is_help(argv
[1]))
2963 d
= malloc(sizeof(*d
));
2966 newargv
[cnt
++] = argv
[0];
2967 newargv
[cnt
++] = "-f";
2968 newargv
[cnt
++] = "-o";
2969 newargv
[cnt
++] = "allow_other,direct_io";
2970 newargv
[cnt
++] = argv
[1];
2971 newargv
[cnt
++] = NULL
;
2973 if (!cgm_escape_cgroup())
2974 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
2976 if (!cgm_get_controllers(&d
->subsystems
))
2979 ret
= fuse_main(nargs
, newargv
, &lxcfs_ops
, d
);
2980 cgm_dbus_disconnect();