3 * Copyright © 2014 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 <nih/alloc.h>
29 #include <nih/string.h>
31 #include "cgmanager.h"
35 * a null-terminated, nih-allocated list of the mounted subsystems. We
36 * detect this at startup.
40 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
45 LXC_TYPE_PROC_MEMINFO
,
46 LXC_TYPE_PROC_CPUINFO
,
49 LXC_TYPE_PROC_DISKSTATS
,
57 char *buf
; // unused as of yet
59 int size
; //actual data size
62 /* reserve buffer size, for cpuall in /proc/stat */
63 #define BUF_RESERVE_SIZE 256
65 static char *must_copy_string(void *parent
, const char *str
)
69 return NIH_MUST( nih_strdup(parent
, str
) );
73 * TODO - return value should denote whether child exited with failure
74 * so callers can return errors. Esp read/write of tasks and cgroup.procs
76 static int wait_for_pid(pid_t pid
)
81 ret
= waitpid(pid
, &status
, 0);
89 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
95 * Given a open file * to /proc/pid/{u,g}id_map, and an id
96 * valid in the caller's namespace, return the id mapped into
98 * Returns the mapped id, or -1 on error.
101 convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
103 unsigned int nsuid
, // base id for a range in the idfile's namespace
104 hostuid
, // base id for a range in the caller's namespace
105 count
; // number of ids in this range
109 fseek(idfile
, 0L, SEEK_SET
);
110 while (fgets(line
, 400, idfile
)) {
111 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
114 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
116 * uids wrapped around - unexpected as this is a procfile,
119 fprintf(stderr
, "pid wrapparound at entry %u %u %u in %s\n",
120 nsuid
, hostuid
, count
, line
);
123 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
125 * now since hostuid <= in_id < hostuid+count, and
126 * hostuid+count and nsuid+count do not wrap around,
127 * we know that nsuid+(in_id-hostuid) which must be
128 * less that nsuid+(count) must not wrap around
130 return (in_id
- hostuid
) + nsuid
;
139 * for is_privileged_over,
140 * specify whether we require the calling uid to be root in his
143 #define NS_ROOT_REQD true
144 #define NS_ROOT_OPT false
146 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
148 nih_local
char *fpath
= NULL
;
152 if (victim
== -1 || uid
== -1)
156 * If the request is one not requiring root in the namespace,
157 * then having the same uid suffices. (i.e. uid 1000 has write
158 * access to files owned by uid 1000
160 if (!req_ns_root
&& uid
== victim
)
163 fpath
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/uid_map", pid
) );
164 FILE *f
= fopen(fpath
, "r");
168 /* if caller's not root in his namespace, reject */
169 nsuid
= convert_id_to_ns(f
, uid
);
174 * If victim is not mapped into caller's ns, reject.
175 * XXX I'm not sure this check is needed given that fuse
176 * will be sending requests where the vfs has converted
178 nsuid
= convert_id_to_ns(f
, victim
);
189 static bool perms_include(int fmode
, mode_t req_mode
)
193 switch (req_mode
& O_ACCMODE
) {
201 r
= S_IROTH
| S_IWOTH
;
206 return ((fmode
& r
) == r
);
209 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
213 if (strlen(taskcg
) <= strlen(querycg
)) {
214 fprintf(stderr
, "%s: I was fed bad input\n", __func__
);
218 if (strcmp(querycg
, "/") == 0)
219 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ 1) );
221 start
= NIH_MUST( nih_strdup(NULL
, taskcg
+ strlen(querycg
) + 1) );
222 end
= strchr(start
, '/');
229 * check whether a fuse context may access a cgroup dir or file
231 * If file is not null, it is a cgroup file to check under cg.
232 * If file is null, then we are checking perms on cg itself.
234 * For files we can check the mode of the list_keys result.
235 * For cgroups, we must make assumptions based on the files under the
236 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
239 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
241 nih_local
struct cgm_keys
**list
= NULL
;
250 if (!cgm_list_keys(contrl
, cg
, &list
))
252 for (i
= 0; list
[i
]; i
++) {
253 if (strcmp(list
[i
]->name
, file
) == 0) {
254 struct cgm_keys
*k
= list
[i
];
255 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
256 if (perms_include(k
->mode
>> 6, mode
))
259 if (fc
->gid
== k
->gid
) {
260 if (perms_include(k
->mode
>> 3, mode
))
263 return perms_include(k
->mode
, mode
);
270 static void stripnewline(char *x
)
272 size_t l
= strlen(x
);
273 if (l
&& x
[l
-1] == '\n')
278 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
279 * If caller is in /a, he may act on /a/b, but not on /b.
280 * if the answer is false and nextcg is not NULL, then *nextcg will point
281 * to a nih_alloc'd string containing the next cgroup directory under cg
283 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
285 nih_local
char *fnam
= NULL
;
291 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
292 if (!(f
= fopen(fnam
, "r")))
295 while (getline(&line
, &len
, f
) != -1) {
296 char *c1
, *c2
, *linecmp
;
299 c1
= strchr(line
, ':');
303 c2
= strchr(c1
, ':');
307 if (strcmp(c1
, contrl
) != 0)
312 * callers pass in '/' for root cgroup, otherwise they pass
313 * in a cgroup without leading '/'
315 linecmp
= *cg
== '/' ? c2
: c2
+1;
316 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
318 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
332 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
333 * and needs to be nih_freed.
335 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
340 if (strlen(path
) < 9)
343 ret
= nih_strdup(NULL
, p1
);
346 slash
= strstr(ret
, "/");
350 /* verify that it is a subsystem */
351 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
357 for (i
= 0; list
[i
]; i
++) {
358 if (strcmp(list
[i
], ret
) == 0)
366 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
367 * Note that the returned value may include files (keynames) etc
369 static const char *find_cgroup_in_path(const char *path
)
373 if (strlen(path
) < 9)
375 p1
= strstr(path
+8, "/");
381 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
383 nih_local
char **list
= NULL
;
391 if (!cgm_list_children(contr
, dir
, &list
))
393 for (i
= 0; list
[i
]; i
++) {
394 if (strcmp(list
[i
], f
) == 0)
401 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
403 nih_local
struct cgm_keys
**list
= NULL
;
411 if (!cgm_list_keys(contr
, dir
, &list
))
413 for (i
= 0; list
[i
]; i
++) {
414 if (strcmp(list
[i
]->name
, f
) == 0) {
415 k
= NIH_MUST( nih_alloc(NULL
, (sizeof(*k
))) );
416 k
->name
= NIH_MUST( nih_strdup(k
, list
[i
]->name
) );
417 k
->uid
= list
[i
]->uid
;
418 k
->gid
= list
[i
]->gid
;
419 k
->mode
= list
[i
]->mode
;
427 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
431 *dir
= NIH_MUST( nih_strdup(NULL
, cg
) );
432 *file
= strrchr(cg
, '/');
437 p
= strrchr(*dir
, '/');
442 * FUSE ops for /cgroup
445 static int cg_getattr(const char *path
, struct stat
*sb
)
448 struct fuse_context
*fc
= fuse_get_context();
449 nih_local
char * cgdir
= NULL
;
450 char *fpath
= NULL
, *path1
, *path2
;
451 nih_local
struct cgm_keys
*k
= NULL
;
453 nih_local
char *controller
= NULL
;
459 memset(sb
, 0, sizeof(struct stat
));
461 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
464 sb
->st_uid
= sb
->st_gid
= 0;
465 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
468 if (strcmp(path
, "/cgroup") == 0) {
469 sb
->st_mode
= S_IFDIR
| 00755;
474 controller
= pick_controller_from_path(fc
, path
);
477 cgroup
= find_cgroup_in_path(path
);
479 /* this is just /cgroup/controller, return it as a dir */
480 sb
->st_mode
= S_IFDIR
| 00755;
485 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
495 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
496 * Then check that caller's cgroup is under path if fpath is a child
497 * cgroup, or cgdir if fpath is a file */
499 if (is_child_cgroup(controller
, path1
, path2
)) {
500 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
501 /* this is just /cgroup/controller, return it as a dir */
502 sb
->st_mode
= S_IFDIR
| 00555;
506 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
509 // get uid, gid, from '/tasks' file and make up a mode
510 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
511 sb
->st_mode
= S_IFDIR
| 00755;
512 k
= get_cgroup_key(controller
, cgroup
, "tasks");
514 sb
->st_uid
= sb
->st_gid
= 0;
523 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
524 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
))
526 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
529 sb
->st_mode
= S_IFREG
| k
->mode
;
541 * TODO - cache these results in a table for use in opendir, free
544 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
546 struct fuse_context
*fc
= fuse_get_context();
547 nih_local
struct cgm_keys
**list
= NULL
;
549 struct file_info
*dir_info
;
550 nih_local
char *controller
= NULL
;
555 if (strcmp(path
, "/cgroup") == 0) {
559 // return list of keys for the controller, and list of child cgroups
560 controller
= pick_controller_from_path(fc
, path
);
564 cgroup
= find_cgroup_in_path(path
);
566 /* this is just /cgroup/controller, return its contents */
571 if (cgroup
&& !fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
574 /* we'll free this at cg_releasedir */
575 dir_info
= NIH_MUST( nih_alloc(NULL
, sizeof(*dir_info
)) );
576 dir_info
->controller
= must_copy_string(dir_info
, controller
);
577 dir_info
->cgroup
= must_copy_string(dir_info
, cgroup
);
578 dir_info
->type
= LXC_TYPE_CGDIR
;
579 dir_info
->buf
= NULL
;
580 dir_info
->file
= NULL
;
581 dir_info
->buflen
= 0;
583 fi
->fh
= (unsigned long)dir_info
;
587 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
588 struct fuse_file_info
*fi
)
590 struct file_info
*d
= (struct file_info
*)fi
->fh
;
591 nih_local
struct cgm_keys
**list
= NULL
;
593 nih_local
char *nextcg
= NULL
;
594 struct fuse_context
*fc
= fuse_get_context();
596 if (d
->type
!= LXC_TYPE_CGDIR
) {
597 fprintf(stderr
, "Internal error: file cache info used in readdir\n");
600 if (!d
->cgroup
&& !d
->controller
) {
601 // ls /var/lib/lxcfs/cgroup - just show list of controllers
602 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
608 for (i
= 0; list
[i
]; i
++) {
609 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
616 if (!cgm_list_keys(d
->controller
, d
->cgroup
, &list
))
617 // not a valid cgroup
620 if (!caller_is_in_ancestor(fc
->pid
, d
->controller
, d
->cgroup
, &nextcg
)) {
623 ret
= filler(buf
, nextcg
, NULL
, 0);
630 for (i
= 0; list
[i
]; i
++) {
631 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
636 // now get the list of child cgroups
637 nih_local
char **clist
= NULL
;
639 if (!cgm_list_children(d
->controller
, d
->cgroup
, &clist
))
641 for (i
= 0; clist
[i
]; i
++) {
642 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
649 static void do_release_file_info(struct file_info
*f
)
652 * all file_info fields which are nih_alloc()d with f as parent
653 * will be automatically freed
658 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
660 struct file_info
*d
= (struct file_info
*)fi
->fh
;
662 do_release_file_info(d
);
666 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
668 nih_local
char *controller
= NULL
;
670 char *fpath
= NULL
, *path1
, *path2
;
671 nih_local
char * cgdir
= NULL
;
672 nih_local
struct cgm_keys
*k
= NULL
;
673 struct file_info
*file_info
;
674 struct fuse_context
*fc
= fuse_get_context();
679 controller
= pick_controller_from_path(fc
, path
);
682 cgroup
= find_cgroup_in_path(path
);
686 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
695 k
= get_cgroup_key(controller
, path1
, path2
);
699 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
))
700 // should never get here
703 /* we'll free this at cg_release */
704 file_info
= NIH_MUST( nih_alloc(NULL
, sizeof(*file_info
)) );
705 file_info
->controller
= must_copy_string(file_info
, controller
);
706 file_info
->cgroup
= must_copy_string(file_info
, path1
);
707 file_info
->file
= must_copy_string(file_info
, path2
);
708 file_info
->type
= LXC_TYPE_CGFILE
;
709 file_info
->buf
= NULL
;
710 file_info
->buflen
= 0;
712 fi
->fh
= (unsigned long)file_info
;
716 static int cg_release(const char *path
, struct fuse_file_info
*fi
)
718 struct file_info
*f
= (struct file_info
*)fi
->fh
;
720 do_release_file_info(f
);
724 static int msgrecv(int sockfd
, void *buf
, size_t len
)
730 FD_SET(sockfd
, &rfds
);
734 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) <= 0)
736 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
739 #define SEND_CREDS_OK 0
740 #define SEND_CREDS_NOTSK 1
741 #define SEND_CREDS_FAIL 2
742 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
744 struct msghdr msg
= { 0 };
746 struct cmsghdr
*cmsg
;
747 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
752 if (msgrecv(sock
, buf
, 1) != 1) {
753 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
755 return SEND_CREDS_FAIL
;
759 msg
.msg_control
= cmsgbuf
;
760 msg
.msg_controllen
= sizeof(cmsgbuf
);
762 cmsg
= CMSG_FIRSTHDR(&msg
);
763 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
764 cmsg
->cmsg_level
= SOL_SOCKET
;
765 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
766 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
773 iov
.iov_len
= sizeof(buf
);
777 if (sendmsg(sock
, &msg
, 0) < 0) {
778 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
781 return SEND_CREDS_NOTSK
;
782 return SEND_CREDS_FAIL
;
785 return SEND_CREDS_OK
;
788 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
790 struct msghdr msg
= { 0 };
792 struct cmsghdr
*cmsg
;
793 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
806 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
807 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
811 if (write(sock
, buf
, 1) != 1) {
812 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
818 msg
.msg_control
= cmsgbuf
;
819 msg
.msg_controllen
= sizeof(cmsgbuf
);
822 iov
.iov_len
= sizeof(buf
);
830 if (select(sock
+1, &rfds
, NULL
, NULL
, &tv
) <= 0) {
831 fprintf(stderr
, "Failed to select for scm_cred: %s\n",
835 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
837 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
842 cmsg
= CMSG_FIRSTHDR(&msg
);
844 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
845 cmsg
->cmsg_level
== SOL_SOCKET
&&
846 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
847 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
856 * pid_to_ns - reads pids from a ucred over a socket, then writes the
857 * int value back over the socket. This shifts the pid from the
858 * sender's pidns into tpid's pidns.
860 static void pid_to_ns(int sock
, pid_t tpid
)
865 while (recv_creds(sock
, &cred
, &v
)) {
868 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
875 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
876 * in your old pidns. Only children which you fork will be in the target
877 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
878 * actually convert pids
880 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
882 int newnsfd
= -1, ret
, cpipe
[2];
889 sprintf(fnam
, "/proc/%d/ns/pid", tpid
);
890 newnsfd
= open(fnam
, O_RDONLY
);
893 if (setns(newnsfd
, 0) < 0)
908 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
909 fprintf(stderr
, "%s (child): erorr on write: %s\n",
910 __func__
, strerror(errno
));
913 pid_to_ns(sock
, tpid
);
915 // give the child 1 second to be done forking and
918 FD_SET(cpipe
[0], &s
);
921 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
924 ret
= read(cpipe
[0], &v
, 1);
925 if (ret
!= sizeof(char) || v
!= '1') {
929 if (!wait_for_pid(cpid
))
940 * To read cgroup files with a particular pid, we will setns into the child
941 * pidns, open a pipe, fork a child - which will be the first to really be in
942 * the child ns - which does the cgm_get_value and writes the data to the pipe.
944 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
946 int sock
[2] = {-1, -1};
947 nih_local
char *tmpdata
= NULL
;
949 pid_t qpid
, cpid
= -1;
956 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
960 * Now we read the pids from returned data one by one, pass
961 * them into a child in the target namespace, read back the
962 * translated pids, and put them into our to-return data
965 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
966 perror("socketpair");
975 pid_to_ns_wrapper(sock
[1], tpid
);
980 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
982 ret
= send_creds(sock
[0], &cred
, v
, true);
984 if (ret
== SEND_CREDS_NOTSK
)
986 if (ret
== SEND_CREDS_FAIL
)
989 // read converted results
994 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
996 fprintf(stderr
, "%s: select error waiting for pid from child: %s\n",
997 __func__
, strerror(errno
));
1000 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1001 fprintf(stderr
, "%s: error reading pid from child: %s\n",
1002 __func__
, strerror(errno
));
1005 NIH_MUST( nih_strcat_sprintf(d
, NULL
, "%d\n", qpid
) );
1007 ptr
= strchr(ptr
, '\n');
1013 cred
.pid
= getpid();
1015 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
1016 // failed to ask child to exit
1017 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
1018 __func__
, strerror(errno
));
1027 if (sock
[0] != -1) {
1034 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1035 struct fuse_file_info
*fi
)
1037 struct fuse_context
*fc
= fuse_get_context();
1038 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1039 nih_local
struct cgm_keys
*k
= NULL
;
1041 if (f
->type
!= LXC_TYPE_CGFILE
) {
1042 fprintf(stderr
, "Internal error: directory cache info used in cg_read\n");
1055 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) != NULL
) {
1056 nih_local
char *data
= NULL
;
1060 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
))
1061 // should never get here
1064 if (strcmp(f
->file
, "tasks") == 0 ||
1065 strcmp(f
->file
, "/tasks") == 0 ||
1066 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1067 strcmp(f
->file
, "cgroup.procs") == 0)
1068 // special case - we have to translate the pids
1069 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1071 r
= cgm_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
1081 memcpy(buf
, data
, s
);
1082 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
1091 static void pid_from_ns(int sock
, pid_t tpid
)
1107 ret
= select(sock
+1, &s
, NULL
, NULL
, &tv
);
1109 fprintf(stderr
, "%s: bad select before read from parent: %s\n",
1110 __func__
, strerror(errno
));
1113 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1114 fprintf(stderr
, "%s: bad read from parent: %s\n",
1115 __func__
, strerror(errno
));
1118 if (vpid
== -1) // done
1122 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1124 cred
.pid
= getpid();
1125 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1132 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1134 int newnsfd
= -1, ret
, cpipe
[2];
1141 sprintf(fnam
, "/proc/%d/ns/pid", tpid
);
1142 newnsfd
= open(fnam
, O_RDONLY
);
1145 if (setns(newnsfd
, 0) < 0)
1149 if (pipe(cpipe
) < 0)
1161 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1162 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1163 __func__
, strerror(errno
));
1166 pid_from_ns(sock
, tpid
);
1169 // give the child 1 second to be done forking and
1172 FD_SET(cpipe
[0], &s
);
1175 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1178 ret
= read(cpipe
[0], &v
, 1);
1179 if (ret
!= sizeof(char) || v
!= '1') {
1183 if (!wait_for_pid(cpid
))
1188 kill(cpid
, SIGKILL
);
1193 static bool do_write_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, const char *buf
)
1195 int sock
[2] = {-1, -1};
1196 pid_t qpid
, cpid
= -1;
1197 bool answer
= false, fail
= false;
1200 * write the pids to a socket, have helper in writer's pidns
1201 * call movepid for us
1203 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1204 perror("socketpair");
1213 pid_from_ns_wrapper(sock
[1], tpid
);
1215 const char *ptr
= buf
;
1216 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1220 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1221 fprintf(stderr
, "%s: error writing pid to child: %s\n",
1222 __func__
, strerror(errno
));
1226 if (recv_creds(sock
[0], &cred
, &v
)) {
1228 if (!cgm_move_pid(contrl
, cg
, cred
.pid
))
1233 ptr
= strchr(ptr
, '\n');
1239 /* All good, write the value */
1241 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1242 fprintf(stderr
, "Warning: failed to ask child to exit\n");
1250 if (sock
[0] != -1) {
1257 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1258 struct fuse_file_info
*fi
)
1260 struct fuse_context
*fc
= fuse_get_context();
1261 nih_local
char *localbuf
= NULL
;
1262 nih_local
struct cgm_keys
*k
= NULL
;
1263 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1265 if (f
->type
!= LXC_TYPE_CGFILE
) {
1266 fprintf(stderr
, "Internal error: directory cache info used in cg_write\n");
1276 localbuf
= NIH_MUST( nih_alloc(NULL
, size
+1) );
1277 localbuf
[size
] = '\0';
1278 memcpy(localbuf
, buf
, size
);
1280 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) != NULL
) {
1283 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
))
1286 if (strcmp(f
->file
, "tasks") == 0 ||
1287 strcmp(f
->file
, "/tasks") == 0 ||
1288 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1289 strcmp(f
->file
, "cgroup.procs") == 0)
1290 // special case - we have to translate the pids
1291 r
= do_write_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1293 r
= cgm_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1304 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1306 struct fuse_context
*fc
= fuse_get_context();
1307 nih_local
char * cgdir
= NULL
;
1308 char *fpath
= NULL
, *path1
, *path2
;
1309 nih_local
struct cgm_keys
*k
= NULL
;
1311 nih_local
char *controller
= NULL
;
1317 if (strcmp(path
, "/cgroup") == 0)
1320 controller
= pick_controller_from_path(fc
, path
);
1323 cgroup
= find_cgroup_in_path(path
);
1325 /* this is just /cgroup/controller */
1328 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1338 if (is_child_cgroup(controller
, path1
, path2
)) {
1339 // get uid, gid, from '/tasks' file and make up a mode
1340 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1341 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1344 k
= get_cgroup_key(controller
, path1
, path2
);
1350 * This being a fuse request, the uid and gid must be valid
1351 * in the caller's namespace. So we can just check to make
1352 * sure that the caller is root in his uid, and privileged
1353 * over the file's current owner.
1355 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
))
1358 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
))
1363 int cg_chmod(const char *path
, mode_t mode
)
1365 struct fuse_context
*fc
= fuse_get_context();
1366 nih_local
char * cgdir
= NULL
;
1367 char *fpath
= NULL
, *path1
, *path2
;
1368 nih_local
struct cgm_keys
*k
= NULL
;
1370 nih_local
char *controller
= NULL
;
1375 if (strcmp(path
, "/cgroup") == 0)
1378 controller
= pick_controller_from_path(fc
, path
);
1381 cgroup
= find_cgroup_in_path(path
);
1383 /* this is just /cgroup/controller */
1386 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1396 if (is_child_cgroup(controller
, path1
, path2
)) {
1397 // get uid, gid, from '/tasks' file and make up a mode
1398 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1399 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1402 k
= get_cgroup_key(controller
, path1
, path2
);
1408 * This being a fuse request, the uid and gid must be valid
1409 * in the caller's namespace. So we can just check to make
1410 * sure that the caller is root in his uid, and privileged
1411 * over the file's current owner.
1413 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
))
1416 if (!cgm_chmod_file(controller
, cgroup
, mode
))
1421 int cg_mkdir(const char *path
, mode_t mode
)
1423 struct fuse_context
*fc
= fuse_get_context();
1424 nih_local
struct cgm_keys
**list
= NULL
;
1425 char *fpath
= NULL
, *path1
;
1426 nih_local
char * cgdir
= NULL
;
1428 nih_local
char *controller
= NULL
;
1434 controller
= pick_controller_from_path(fc
, path
);
1438 cgroup
= find_cgroup_in_path(path
);
1442 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1448 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
))
1452 if (!cgm_create(controller
, cgroup
, fc
->uid
, fc
->gid
))
1458 static int cg_rmdir(const char *path
)
1460 struct fuse_context
*fc
= fuse_get_context();
1461 nih_local
struct cgm_keys
**list
= NULL
;
1463 nih_local
char * cgdir
= NULL
;
1465 nih_local
char *controller
= NULL
;
1471 controller
= pick_controller_from_path(fc
, path
);
1475 cgroup
= find_cgroup_in_path(path
);
1479 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1483 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
))
1486 if (!cgm_remove(controller
, cgroup
))
1492 static bool startswith(const char *line
, const char *pref
)
1494 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1499 static void get_mem_cached(char *memstat
, unsigned long *v
)
1505 if (startswith(memstat
, "total_cache")) {
1506 sscanf(memstat
+ 11, "%lu", v
);
1510 eol
= strchr(memstat
, '\n');
1517 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
1523 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
1525 size_t len
= strlen(key
);
1529 if (startswith(str
, key
)) {
1530 sscanf(str
+ len
, "%lu", v
);
1533 eol
= strchr(str
, '\n');
1540 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1542 nih_local
char *fnam
= NULL
;
1544 char *answer
= NULL
;
1548 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
1549 if (!(f
= fopen(fnam
, "r")))
1552 while (getline(&line
, &len
, f
) != -1) {
1556 c1
= strchr(line
, ':');
1560 c2
= strchr(c1
, ':');
1564 if (strcmp(c1
, contrl
) != 0)
1568 answer
= NIH_MUST( nih_strdup(NULL
, c2
) );
1579 * FUSE ops for /proc
1582 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1583 struct fuse_file_info
*fi
)
1585 struct fuse_context
*fc
= fuse_get_context();
1586 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1587 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "memory");
1588 nih_local
char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1589 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1591 size_t linelen
= 0, total_len
= 0;
1592 char *cache
= d
->buf
;
1593 size_t cache_size
= d
->buflen
;
1597 if (offset
> d
->size
)
1599 int left
= d
->size
- offset
;
1600 total_len
= left
> size
? size
: left
;
1601 memcpy(buf
, cache
+ offset
, total_len
);
1608 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1610 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1612 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1614 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1615 memusage
= strtoul(memusage_str
, NULL
, 10);
1618 get_mem_cached(memstat_str
, &cached
);
1620 f
= fopen("/proc/meminfo", "r");
1624 while (getline(&line
, &linelen
, f
) != -1) {
1626 char *printme
, lbuf
[100];
1628 memset(lbuf
, 0, 100);
1629 if (startswith(line
, "MemTotal:")) {
1630 sscanf(line
+14, "%lu", &hosttotal
);
1631 if (hosttotal
< memlimit
)
1632 memlimit
= hosttotal
;
1633 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1635 } else if (startswith(line
, "MemFree:")) {
1636 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1638 } else if (startswith(line
, "MemAvailable:")) {
1639 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1641 } else if (startswith(line
, "Buffers:")) {
1642 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1644 } else if (startswith(line
, "Cached:")) {
1645 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1647 } else if (startswith(line
, "SwapCached:")) {
1648 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1653 l
= snprintf(cache
, cache_size
, "%s", printme
);
1659 d
->size
= total_len
;
1660 if (total_len
> size
) total_len
= size
;
1661 memcpy(buf
, d
->buf
, total_len
);
1669 * Read the cpuset.cpus for cg
1670 * Return the answer in a nih_alloced string
1672 static char *get_cpuset(const char *cg
)
1676 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1682 * Helper functions for cpuset_in-set
1684 char *cpuset_nexttok(const char *c
)
1686 char *r
= strchr(c
+1, ',');
1692 int cpuset_getrange(const char *c
, int *a
, int *b
)
1696 ret
= sscanf(c
, "%d-%d", a
, b
);
1701 * cpusets are in format "1,2-3,4"
1702 * iow, comma-delimited ranges
1704 static bool cpu_in_cpuset(int cpu
, const char *cpuset
)
1708 for (c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
1711 ret
= cpuset_getrange(c
, &a
, &b
);
1712 if (ret
== 1 && cpu
== a
)
1714 if (ret
!= 2) // bad cpuset!
1716 if (cpu
>= a
&& cpu
<= b
)
1723 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1727 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1729 return cpu_in_cpuset(cpu
, cpuset
);
1733 * check whether this is a '^processor" line in /proc/cpuinfo
1735 static bool is_processor_line(const char *line
)
1739 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1744 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1745 struct fuse_file_info
*fi
)
1747 struct fuse_context
*fc
= fuse_get_context();
1748 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1749 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1750 nih_local
char *cpuset
= NULL
;
1752 size_t linelen
= 0, total_len
= 0;
1753 bool am_printing
= false;
1755 char *cache
= d
->buf
;
1756 size_t cache_size
= d
->buflen
;
1760 if (offset
> d
->size
)
1762 int left
= d
->size
- offset
;
1763 total_len
= left
> size
? size
: left
;
1764 memcpy(buf
, cache
+ offset
, total_len
);
1771 cpuset
= get_cpuset(cg
);
1775 f
= fopen("/proc/cpuinfo", "r");
1779 while (getline(&line
, &linelen
, f
) != -1) {
1781 if (is_processor_line(line
)) {
1782 am_printing
= cpuline_in_cpuset(line
, cpuset
);
1785 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
1786 if (l
< cache_size
){
1791 cache
+= cache_size
;
1792 total_len
+= cache_size
;
1800 l
= snprintf(cache
, cache_size
, "%s", line
);
1801 if (l
< cache_size
) {
1806 cache
+= cache_size
;
1807 total_len
+= cache_size
;
1814 d
->size
= total_len
;
1815 if (total_len
> size
) total_len
= size
;
1817 /* read from off 0 */
1818 memcpy(buf
, d
->buf
, total_len
);
1825 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
1826 struct fuse_file_info
*fi
)
1828 struct fuse_context
*fc
= fuse_get_context();
1829 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1830 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1831 nih_local
char *cpuset
= NULL
;
1833 size_t linelen
= 0, total_len
= 0;
1834 int curcpu
= -1; /* cpu numbering starts at 0 */
1835 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0;
1836 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
1837 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0;
1838 #define CPUALL_MAX_SIZE BUF_RESERVE_SIZE
1839 char cpuall
[CPUALL_MAX_SIZE
];
1840 /* reserve for cpu all */
1841 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
1842 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
1846 if (offset
> d
->size
)
1848 int left
= d
->size
- offset
;
1849 total_len
= left
> size
? size
: left
;
1850 memcpy(buf
, d
->buf
+ offset
, total_len
);
1857 cpuset
= get_cpuset(cg
);
1861 f
= fopen("/proc/stat", "r");
1866 if (getline(&line
, &linelen
, f
) < 0) {
1867 fprintf(stderr
, "proc_stat_read read first line failed\n");
1871 while (getline(&line
, &linelen
, f
) != -1) {
1874 char cpu_char
[10]; /* That's a lot of cores */
1877 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
1878 /* not a ^cpuN line containing a number N, just print it */
1879 l
= snprintf(cache
, cache_size
, "%s", line
);
1880 if (l
< cache_size
){
1886 //no more space, break it
1887 cache
+= cache_size
;
1888 total_len
+= cache_size
;
1894 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
1896 if (!cpu_in_cpuset(cpu
, cpuset
))
1900 c
= strchr(line
, ' ');
1903 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
1908 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user
, &nice
, &system
, &idle
, &iowait
, &irq
,
1909 &softirq
, &steal
, &guest
) != 9)
1913 system_sum
+= system
;
1915 iowait_sum
+= iowait
;
1917 softirq_sum
+= softirq
;
1924 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
1925 "cpu ", user_sum
, nice_sum
, system_sum
, idle_sum
, iowait_sum
, irq_sum
, softirq_sum
, steal_sum
, guest_sum
);
1926 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
){
1927 memcpy(cache
, cpuall
, cpuall_len
);
1928 cache
+= cpuall_len
;
1930 /* shouldn't happen */
1931 fprintf(stderr
, "proc_stat_read copy cpuall failed, cpuall_len=%d\n", cpuall_len
);
1935 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
1936 total_len
+= cpuall_len
;
1937 d
->size
= total_len
;
1938 if (total_len
> size
) total_len
= size
;
1940 memcpy(buf
, d
->buf
, total_len
);
1948 * How to guess what to present for uptime?
1949 * One thing we could do would be to take the date on the caller's
1950 * memory.usage_in_bytes file, which should equal the time of creation
1951 * of his cgroup. However, a task could be in a sub-cgroup of the
1952 * container. The same problem exists if we try to look at the ages
1953 * of processes in the caller's cgroup.
1955 * So we'll fork a task that will enter the caller's pidns, mount a
1956 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
1958 * For the second uptime #, we'll do as Stéphane had done, just copy
1959 * the number from /proc/uptime. Not sure how to best emulate 'idle'
1960 * time. Maybe someone can come up with a good algorithm and submit a
1961 * patch. Maybe something based on cpushare info?
1964 /* return age of the reaper for $pid, taken from ctime of its procdir */
1965 static long int get_pid1_time(pid_t pid
)
1968 int fd
, cpipe
[2], ret
;
1975 if (unshare(CLONE_NEWNS
))
1978 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
1979 perror("rslave mount failed");
1983 sprintf(fnam
, "/proc/%d/ns/pid", pid
);
1984 fd
= open(fnam
, O_RDONLY
);
1986 perror("get_pid1_time open of ns/pid");
1990 perror("get_pid1_time setns 1");
1996 if (pipe(cpipe
) < 0)
2007 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
2008 fprintf(stderr
, "%s (child): erorr on write: %s\n",
2009 __func__
, strerror(errno
));
2012 umount2("/proc", MNT_DETACH
);
2013 if (mount("proc", "/proc", "proc", 0, NULL
)) {
2014 perror("get_pid1_time mount");
2017 ret
= lstat("/proc/1", &sb
);
2019 perror("get_pid1_time lstat");
2022 return time(NULL
) - sb
.st_ctime
;
2025 // give the child 1 second to be done forking and
2028 FD_SET(cpipe
[0], &s
);
2031 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2034 ret
= read(cpipe
[0], &v
, 1);
2035 if (ret
!= sizeof(char) || v
!= '1') {
2043 kill(cpid
, SIGKILL
);
2048 static long int getreaperage(pid_t qpid
)
2050 int pid
, mypipe
[2], ret
;
2053 long int mtime
, answer
= 0;
2061 if (!pid
) { // child
2062 mtime
= get_pid1_time(qpid
);
2063 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
2064 fprintf(stderr
, "Warning: bad write from getreaperage\n");
2070 FD_SET(mypipe
[0], &s
);
2073 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2079 fprintf(stderr
, "timed out\n");
2082 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
2094 static long int getprocidle(void)
2096 FILE *f
= fopen("/proc/uptime", "r");
2101 ret
= fscanf(f
, "%ld %ld", &age
, &idle
);
2109 * We read /proc/uptime and reuse its second field.
2110 * For the first field, we use the mtime for the reaper for
2111 * the calling pid as returned by getreaperage
2113 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
2114 struct fuse_file_info
*fi
)
2116 struct fuse_context
*fc
= fuse_get_context();
2117 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2118 long int reaperage
= getreaperage(fc
->pid
);;
2119 long int idletime
= getprocidle();
2120 size_t total_len
= 0;
2123 if (offset
> d
->size
)
2128 total_len
= snprintf(buf
, size
, "%ld %ld\n", reaperage
, idletime
);
2129 d
->size
= total_len
;
2133 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
2134 struct fuse_file_info
*fi
)
2137 struct fuse_context
*fc
= fuse_get_context();
2138 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2139 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "blkio");
2140 nih_local
char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
2141 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
2142 unsigned long read
= 0, write
= 0;
2143 unsigned long read_merged
= 0, write_merged
= 0;
2144 unsigned long read_sectors
= 0, write_sectors
= 0;
2145 unsigned long read_ticks
= 0, write_ticks
= 0;
2146 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
2147 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
2149 size_t linelen
= 0, total_len
= 0;
2150 unsigned int major
= 0, minor
= 0;
2155 if (offset
> d
->size
)
2163 if (!cgm_get_value("blkio", cg
, "blkio.io_serviced", &io_serviced_str
))
2165 if (!cgm_get_value("blkio", cg
, "blkio.io_merged", &io_merged_str
))
2167 if (!cgm_get_value("blkio", cg
, "blkio.io_service_bytes", &io_service_bytes_str
))
2169 if (!cgm_get_value("blkio", cg
, "blkio.io_wait_time", &io_wait_time_str
))
2171 if (!cgm_get_value("blkio", cg
, "blkio.io_service_time", &io_service_time_str
))
2175 f
= fopen("/proc/diskstats", "r");
2179 while (getline(&line
, &linelen
, f
) != -1) {
2181 char *printme
, lbuf
[256];
2183 i
= sscanf(line
, "%u %u %s", &major
, &minor
, dev_name
);
2185 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
2186 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
2187 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
2188 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
2189 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
2190 read_sectors
= read_sectors
/512;
2191 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
2192 write_sectors
= write_sectors
/512;
2194 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
2195 rd_svctm
= rd_svctm
/1000000;
2196 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
2197 rd_wait
= rd_wait
/1000000;
2198 read_ticks
= rd_svctm
+ rd_wait
;
2200 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
2201 wr_svctm
= wr_svctm
/1000000;
2202 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
2203 wr_wait
= wr_wait
/1000000;
2204 write_ticks
= wr_svctm
+ wr_wait
;
2206 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
2207 tot_ticks
= tot_ticks
/1000000;
2212 memset(lbuf
, 0, 256);
2213 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
) {
2214 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2215 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
2216 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
2221 l
= snprintf(buf
, size
, "%s", printme
);
2227 d
->size
= total_len
;
2234 static off_t
get_procfile_size(const char *which
)
2236 FILE *f
= fopen(which
, "r");
2239 ssize_t sz
, answer
= 0;
2243 while ((sz
= getline(&line
, &len
, f
)) != -1)
2251 static int proc_getattr(const char *path
, struct stat
*sb
)
2253 struct timespec now
;
2255 memset(sb
, 0, sizeof(struct stat
));
2256 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
2258 sb
->st_uid
= sb
->st_gid
= 0;
2259 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
2260 if (strcmp(path
, "/proc") == 0) {
2261 sb
->st_mode
= S_IFDIR
| 00555;
2265 if (strcmp(path
, "/proc/meminfo") == 0 ||
2266 strcmp(path
, "/proc/cpuinfo") == 0 ||
2267 strcmp(path
, "/proc/uptime") == 0 ||
2268 strcmp(path
, "/proc/stat") == 0 ||
2269 strcmp(path
, "/proc/diskstats") == 0) {
2271 sb
->st_mode
= S_IFREG
| 00444;
2279 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2280 struct fuse_file_info
*fi
)
2282 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
2283 filler(buf
, "meminfo", NULL
, 0) != 0 ||
2284 filler(buf
, "stat", NULL
, 0) != 0 ||
2285 filler(buf
, "uptime", NULL
, 0) != 0 ||
2286 filler(buf
, "diskstats", NULL
, 0) != 0)
2291 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
2294 struct file_info
*info
;
2296 if (strcmp(path
, "/proc/meminfo") == 0)
2297 type
= LXC_TYPE_PROC_MEMINFO
;
2298 else if (strcmp(path
, "/proc/cpuinfo") == 0)
2299 type
= LXC_TYPE_PROC_CPUINFO
;
2300 else if (strcmp(path
, "/proc/uptime") == 0)
2301 type
= LXC_TYPE_PROC_UPTIME
;
2302 else if (strcmp(path
, "/proc/stat") == 0)
2303 type
= LXC_TYPE_PROC_STAT
;
2304 else if (strcmp(path
, "/proc/diskstats") == 0)
2305 type
= LXC_TYPE_PROC_DISKSTATS
;
2309 info
= NIH_MUST( nih_alloc(NULL
, sizeof(*info
)) );
2310 memset(info
, 0, sizeof(*info
));
2313 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
2314 info
->buf
= NIH_MUST( nih_alloc(info
, info
->buflen
) );
2315 memset(info
->buf
, 0, info
->buflen
);
2316 /* set actual size to buffer size */
2317 info
->size
= info
->buflen
;
2319 fi
->fh
= (unsigned long)info
;
2323 static int proc_release(const char *path
, struct fuse_file_info
*fi
)
2325 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2327 do_release_file_info(f
);
2331 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2332 struct fuse_file_info
*fi
)
2334 struct file_info
*f
= (struct file_info
*) fi
->fh
;
2337 case LXC_TYPE_PROC_MEMINFO
:
2338 return proc_meminfo_read(buf
, size
, offset
, fi
);
2339 case LXC_TYPE_PROC_CPUINFO
:
2340 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
2341 case LXC_TYPE_PROC_UPTIME
:
2342 return proc_uptime_read(buf
, size
, offset
, fi
);
2343 case LXC_TYPE_PROC_STAT
:
2344 return proc_stat_read(buf
, size
, offset
, fi
);
2345 case LXC_TYPE_PROC_DISKSTATS
:
2346 return proc_diskstats_read(buf
, size
, offset
, fi
);
2354 * these just delegate to the /proc and /cgroup ops as
2358 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
2360 if (strcmp(path
, "/") == 0) {
2361 sb
->st_mode
= S_IFDIR
| 00755;
2365 if (strncmp(path
, "/cgroup", 7) == 0) {
2366 return cg_getattr(path
, sb
);
2368 if (strncmp(path
, "/proc", 5) == 0) {
2369 return proc_getattr(path
, sb
);
2374 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
2376 if (strcmp(path
, "/") == 0)
2379 if (strncmp(path
, "/cgroup", 7) == 0) {
2380 return cg_opendir(path
, fi
);
2382 if (strcmp(path
, "/proc") == 0)
2387 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2388 struct fuse_file_info
*fi
)
2390 if (strcmp(path
, "/") == 0) {
2391 if (filler(buf
, "proc", NULL
, 0) != 0 ||
2392 filler(buf
, "cgroup", NULL
, 0) != 0)
2396 if (strncmp(path
, "/cgroup", 7) == 0)
2397 return cg_readdir(path
, buf
, filler
, offset
, fi
);
2398 if (strcmp(path
, "/proc") == 0)
2399 return proc_readdir(path
, buf
, filler
, offset
, fi
);
2403 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
2405 if (strcmp(path
, "/") == 0)
2407 if (strncmp(path
, "/cgroup", 7) == 0) {
2408 return cg_releasedir(path
, fi
);
2410 if (strcmp(path
, "/proc") == 0)
2415 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
2417 if (strncmp(path
, "/cgroup", 7) == 0)
2418 return cg_open(path
, fi
);
2419 if (strncmp(path
, "/proc", 5) == 0)
2420 return proc_open(path
, fi
);
2425 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2426 struct fuse_file_info
*fi
)
2428 if (strncmp(path
, "/cgroup", 7) == 0)
2429 return cg_read(path
, buf
, size
, offset
, fi
);
2430 if (strncmp(path
, "/proc", 5) == 0)
2431 return proc_read(path
, buf
, size
, offset
, fi
);
2436 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2437 struct fuse_file_info
*fi
)
2439 if (strncmp(path
, "/cgroup", 7) == 0) {
2440 return cg_write(path
, buf
, size
, offset
, fi
);
2446 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
2451 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
2453 if (strncmp(path
, "/cgroup", 7) == 0)
2454 return cg_release(path
, fi
);
2455 if (strncmp(path
, "/proc", 5) == 0)
2456 return proc_release(path
, fi
);
2461 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
2466 int lxcfs_mkdir(const char *path
, mode_t mode
)
2468 if (strncmp(path
, "/cgroup", 7) == 0)
2469 return cg_mkdir(path
, mode
);
2474 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
2476 if (strncmp(path
, "/cgroup", 7) == 0)
2477 return cg_chown(path
, uid
, gid
);
2483 * cat first does a truncate before doing ops->write. This doesn't
2484 * really make sense for cgroups. So just return 0 always but do
2487 int lxcfs_truncate(const char *path
, off_t newsize
)
2489 if (strncmp(path
, "/cgroup", 7) == 0)
2494 int lxcfs_rmdir(const char *path
)
2496 if (strncmp(path
, "/cgroup", 7) == 0)
2497 return cg_rmdir(path
);
2501 int lxcfs_chmod(const char *path
, mode_t mode
)
2503 if (strncmp(path
, "/cgroup", 7) == 0)
2504 return cg_chmod(path
, mode
);
2508 const struct fuse_operations lxcfs_ops
= {
2509 .getattr
= lxcfs_getattr
,
2513 .mkdir
= lxcfs_mkdir
,
2515 .rmdir
= lxcfs_rmdir
,
2519 .chmod
= lxcfs_chmod
,
2520 .chown
= lxcfs_chown
,
2521 .truncate
= lxcfs_truncate
,
2526 .release
= lxcfs_release
,
2527 .write
= lxcfs_write
,
2530 .flush
= lxcfs_flush
,
2531 .fsync
= lxcfs_fsync
,
2536 .removexattr
= NULL
,
2538 .opendir
= lxcfs_opendir
,
2539 .readdir
= lxcfs_readdir
,
2540 .releasedir
= lxcfs_releasedir
,
2551 static void usage(const char *me
)
2553 fprintf(stderr
, "Usage:\n");
2554 fprintf(stderr
, "\n");
2555 fprintf(stderr
, "%s mountpoint\n", me
);
2556 fprintf(stderr
, "%s -h\n", me
);
2560 static bool is_help(char *w
)
2562 if (strcmp(w
, "-h") == 0 ||
2563 strcmp(w
, "--help") == 0 ||
2564 strcmp(w
, "-help") == 0 ||
2565 strcmp(w
, "help") == 0)
2570 void swallow_arg(int *argcp
, char *argv
[], char *which
)
2574 for (i
= 1; argv
[i
]; i
++) {
2575 if (strcmp(argv
[i
], which
) != 0)
2577 for (; argv
[i
]; i
++) {
2578 argv
[i
] = argv
[i
+1];
2585 void swallow_option(int *argcp
, char *argv
[], char *opt
, char *v
)
2589 for (i
= 1; argv
[i
]; i
++) {
2592 if (strcmp(argv
[i
], opt
) != 0)
2594 if (strcmp(argv
[i
+1], v
) != 0) {
2595 fprintf(stderr
, "Warning: unexpected fuse option %s\n", v
);
2598 for (; argv
[i
+1]; i
++) {
2599 argv
[i
] = argv
[i
+2];
2606 int main(int argc
, char *argv
[])
2609 struct lxcfs_state
*d
;
2611 * what we pass to fuse_main is:
2612 * argv[0] -s -f -o allow_other,directio argv[1] NULL
2617 /* accomodate older init scripts */
2618 swallow_arg(&argc
, argv
, "-s");
2619 swallow_arg(&argc
, argv
, "-f");
2620 swallow_option(&argc
, argv
, "-o", "allow_other");
2622 if (argc
!= 2 || is_help(argv
[1]))
2625 d
= NIH_MUST( malloc(sizeof(*d
)) );
2627 newargv
[0] = argv
[0];
2631 newargv
[4] = "allow_other,direct_io";
2632 newargv
[5] = argv
[1];
2635 if (!cgm_escape_cgroup())
2636 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
2638 if (!cgm_get_controllers(&d
->subsystems
))
2641 ret
= fuse_main(NARGS
- 1, newargv
, &lxcfs_ops
, d
);