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)
342 if (*(path
+7) != '/')
345 ret
= nih_strdup(NULL
, p1
);
348 slash
= strstr(ret
, "/");
352 /* verify that it is a subsystem */
353 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
359 for (i
= 0; list
[i
]; i
++) {
360 if (strcmp(list
[i
], ret
) == 0)
368 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
369 * Note that the returned value may include files (keynames) etc
371 static const char *find_cgroup_in_path(const char *path
)
375 if (strlen(path
) < 9)
377 p1
= strstr(path
+8, "/");
383 static bool is_child_cgroup(const char *contr
, const char *dir
, const char *f
)
385 nih_local
char **list
= NULL
;
393 if (!cgm_list_children(contr
, dir
, &list
))
395 for (i
= 0; list
[i
]; i
++) {
396 if (strcmp(list
[i
], f
) == 0)
403 static struct cgm_keys
*get_cgroup_key(const char *contr
, const char *dir
, const char *f
)
405 nih_local
struct cgm_keys
**list
= NULL
;
413 if (!cgm_list_keys(contr
, dir
, &list
))
415 for (i
= 0; list
[i
]; i
++) {
416 if (strcmp(list
[i
]->name
, f
) == 0) {
417 k
= NIH_MUST( nih_alloc(NULL
, (sizeof(*k
))) );
418 k
->name
= NIH_MUST( nih_strdup(k
, list
[i
]->name
) );
419 k
->uid
= list
[i
]->uid
;
420 k
->gid
= list
[i
]->gid
;
421 k
->mode
= list
[i
]->mode
;
429 static void get_cgdir_and_path(const char *cg
, char **dir
, char **file
)
433 *dir
= NIH_MUST( nih_strdup(NULL
, cg
) );
434 *file
= strrchr(cg
, '/');
439 p
= strrchr(*dir
, '/');
444 * FUSE ops for /cgroup
447 static int cg_getattr(const char *path
, struct stat
*sb
)
450 struct fuse_context
*fc
= fuse_get_context();
451 nih_local
char * cgdir
= NULL
;
452 char *fpath
= NULL
, *path1
, *path2
;
453 nih_local
struct cgm_keys
*k
= NULL
;
455 nih_local
char *controller
= NULL
;
461 memset(sb
, 0, sizeof(struct stat
));
463 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
466 sb
->st_uid
= sb
->st_gid
= 0;
467 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
470 if (strcmp(path
, "/cgroup") == 0) {
471 sb
->st_mode
= S_IFDIR
| 00755;
476 controller
= pick_controller_from_path(fc
, path
);
479 cgroup
= find_cgroup_in_path(path
);
481 /* this is just /cgroup/controller, return it as a dir */
482 sb
->st_mode
= S_IFDIR
| 00755;
487 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
497 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
498 * Then check that caller's cgroup is under path if fpath is a child
499 * cgroup, or cgdir if fpath is a file */
501 if (is_child_cgroup(controller
, path1
, path2
)) {
502 if (!caller_is_in_ancestor(fc
->pid
, controller
, cgroup
, NULL
)) {
503 /* this is just /cgroup/controller, return it as a dir */
504 sb
->st_mode
= S_IFDIR
| 00555;
508 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
511 // get uid, gid, from '/tasks' file and make up a mode
512 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
513 sb
->st_mode
= S_IFDIR
| 00755;
514 k
= get_cgroup_key(controller
, cgroup
, "tasks");
516 sb
->st_uid
= sb
->st_gid
= 0;
525 if ((k
= get_cgroup_key(controller
, path1
, path2
)) != NULL
) {
526 if (!caller_is_in_ancestor(fc
->pid
, controller
, path1
, NULL
))
528 if (!fc_may_access(fc
, controller
, path1
, path2
, O_RDONLY
))
531 sb
->st_mode
= S_IFREG
| k
->mode
;
543 * TODO - cache these results in a table for use in opendir, free
546 static int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
548 struct fuse_context
*fc
= fuse_get_context();
549 nih_local
struct cgm_keys
**list
= NULL
;
551 struct file_info
*dir_info
;
552 nih_local
char *controller
= NULL
;
557 if (strcmp(path
, "/cgroup") == 0) {
561 // return list of keys for the controller, and list of child cgroups
562 controller
= pick_controller_from_path(fc
, path
);
566 cgroup
= find_cgroup_in_path(path
);
568 /* this is just /cgroup/controller, return its contents */
573 if (cgroup
&& !fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
576 /* we'll free this at cg_releasedir */
577 dir_info
= NIH_MUST( nih_alloc(NULL
, sizeof(*dir_info
)) );
578 dir_info
->controller
= must_copy_string(dir_info
, controller
);
579 dir_info
->cgroup
= must_copy_string(dir_info
, cgroup
);
580 dir_info
->type
= LXC_TYPE_CGDIR
;
581 dir_info
->buf
= NULL
;
582 dir_info
->file
= NULL
;
583 dir_info
->buflen
= 0;
585 fi
->fh
= (unsigned long)dir_info
;
589 static int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
590 struct fuse_file_info
*fi
)
592 struct file_info
*d
= (struct file_info
*)fi
->fh
;
593 nih_local
struct cgm_keys
**list
= NULL
;
595 nih_local
char *nextcg
= NULL
;
596 struct fuse_context
*fc
= fuse_get_context();
598 if (d
->type
!= LXC_TYPE_CGDIR
) {
599 fprintf(stderr
, "Internal error: file cache info used in readdir\n");
602 if (!d
->cgroup
&& !d
->controller
) {
603 // ls /var/lib/lxcfs/cgroup - just show list of controllers
604 char **list
= LXCFS_DATA
? LXCFS_DATA
->subsystems
: NULL
;
610 for (i
= 0; list
[i
]; i
++) {
611 if (filler(buf
, list
[i
], NULL
, 0) != 0) {
618 if (!cgm_list_keys(d
->controller
, d
->cgroup
, &list
))
619 // not a valid cgroup
622 if (!caller_is_in_ancestor(fc
->pid
, d
->controller
, d
->cgroup
, &nextcg
)) {
625 ret
= filler(buf
, nextcg
, NULL
, 0);
632 for (i
= 0; list
[i
]; i
++) {
633 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
638 // now get the list of child cgroups
639 nih_local
char **clist
= NULL
;
641 if (!cgm_list_children(d
->controller
, d
->cgroup
, &clist
))
643 for (i
= 0; clist
[i
]; i
++) {
644 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
651 static void do_release_file_info(struct file_info
*f
)
654 * all file_info fields which are nih_alloc()d with f as parent
655 * will be automatically freed
660 static int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
662 struct file_info
*d
= (struct file_info
*)fi
->fh
;
664 do_release_file_info(d
);
668 static int cg_open(const char *path
, struct fuse_file_info
*fi
)
670 nih_local
char *controller
= NULL
;
672 char *fpath
= NULL
, *path1
, *path2
;
673 nih_local
char * cgdir
= NULL
;
674 nih_local
struct cgm_keys
*k
= NULL
;
675 struct file_info
*file_info
;
676 struct fuse_context
*fc
= fuse_get_context();
681 controller
= pick_controller_from_path(fc
, path
);
684 cgroup
= find_cgroup_in_path(path
);
688 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
697 k
= get_cgroup_key(controller
, path1
, path2
);
701 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
))
702 // should never get here
705 /* we'll free this at cg_release */
706 file_info
= NIH_MUST( nih_alloc(NULL
, sizeof(*file_info
)) );
707 file_info
->controller
= must_copy_string(file_info
, controller
);
708 file_info
->cgroup
= must_copy_string(file_info
, path1
);
709 file_info
->file
= must_copy_string(file_info
, path2
);
710 file_info
->type
= LXC_TYPE_CGFILE
;
711 file_info
->buf
= NULL
;
712 file_info
->buflen
= 0;
714 fi
->fh
= (unsigned long)file_info
;
718 static int cg_release(const char *path
, struct fuse_file_info
*fi
)
720 struct file_info
*f
= (struct file_info
*)fi
->fh
;
722 do_release_file_info(f
);
726 static int msgrecv(int sockfd
, void *buf
, size_t len
)
732 FD_SET(sockfd
, &rfds
);
736 if (select(sockfd
+1, &rfds
, NULL
, NULL
, &tv
) <= 0)
738 return recv(sockfd
, buf
, len
, MSG_DONTWAIT
);
741 #define SEND_CREDS_OK 0
742 #define SEND_CREDS_NOTSK 1
743 #define SEND_CREDS_FAIL 2
744 static int send_creds(int sock
, struct ucred
*cred
, char v
, bool pingfirst
)
746 struct msghdr msg
= { 0 };
748 struct cmsghdr
*cmsg
;
749 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
754 if (msgrecv(sock
, buf
, 1) != 1) {
755 fprintf(stderr
, "%s: Error getting reply from server over socketpair\n",
757 return SEND_CREDS_FAIL
;
761 msg
.msg_control
= cmsgbuf
;
762 msg
.msg_controllen
= sizeof(cmsgbuf
);
764 cmsg
= CMSG_FIRSTHDR(&msg
);
765 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
766 cmsg
->cmsg_level
= SOL_SOCKET
;
767 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
768 memcpy(CMSG_DATA(cmsg
), cred
, sizeof(*cred
));
775 iov
.iov_len
= sizeof(buf
);
779 if (sendmsg(sock
, &msg
, 0) < 0) {
780 fprintf(stderr
, "%s: failed at sendmsg: %s\n", __func__
,
783 return SEND_CREDS_NOTSK
;
784 return SEND_CREDS_FAIL
;
787 return SEND_CREDS_OK
;
790 static bool recv_creds(int sock
, struct ucred
*cred
, char *v
)
792 struct msghdr msg
= { 0 };
794 struct cmsghdr
*cmsg
;
795 char cmsgbuf
[CMSG_SPACE(sizeof(*cred
))];
808 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &optval
, sizeof(optval
)) == -1) {
809 fprintf(stderr
, "Failed to set passcred: %s\n", strerror(errno
));
813 if (write(sock
, buf
, 1) != 1) {
814 fprintf(stderr
, "Failed to start write on scm fd: %s\n", strerror(errno
));
820 msg
.msg_control
= cmsgbuf
;
821 msg
.msg_controllen
= sizeof(cmsgbuf
);
824 iov
.iov_len
= sizeof(buf
);
832 if (select(sock
+1, &rfds
, NULL
, NULL
, &tv
) <= 0) {
833 fprintf(stderr
, "Failed to select for scm_cred: %s\n",
837 ret
= recvmsg(sock
, &msg
, MSG_DONTWAIT
);
839 fprintf(stderr
, "Failed to receive scm_cred: %s\n",
844 cmsg
= CMSG_FIRSTHDR(&msg
);
846 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
847 cmsg
->cmsg_level
== SOL_SOCKET
&&
848 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
849 memcpy(cred
, CMSG_DATA(cmsg
), sizeof(*cred
));
858 * pid_to_ns - reads pids from a ucred over a socket, then writes the
859 * int value back over the socket. This shifts the pid from the
860 * sender's pidns into tpid's pidns.
862 static void pid_to_ns(int sock
, pid_t tpid
)
867 while (recv_creds(sock
, &cred
, &v
)) {
870 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
877 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
878 * in your old pidns. Only children which you fork will be in the target
879 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
880 * actually convert pids
882 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
884 int newnsfd
= -1, ret
, cpipe
[2];
891 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
892 if (ret
< 0 || ret
>= sizeof(fnam
))
894 newnsfd
= open(fnam
, O_RDONLY
);
897 if (setns(newnsfd
, 0) < 0)
912 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
913 fprintf(stderr
, "%s (child): erorr on write: %s\n",
914 __func__
, strerror(errno
));
917 pid_to_ns(sock
, tpid
);
919 // give the child 1 second to be done forking and
922 FD_SET(cpipe
[0], &s
);
925 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
928 ret
= read(cpipe
[0], &v
, 1);
929 if (ret
!= sizeof(char) || v
!= '1') {
933 if (!wait_for_pid(cpid
))
944 * To read cgroup files with a particular pid, we will setns into the child
945 * pidns, open a pipe, fork a child - which will be the first to really be in
946 * the child ns - which does the cgm_get_value and writes the data to the pipe.
948 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, char **d
)
950 int sock
[2] = {-1, -1};
951 nih_local
char *tmpdata
= NULL
;
953 pid_t qpid
, cpid
= -1;
960 if (!cgm_get_value(contrl
, cg
, file
, &tmpdata
))
964 * Now we read the pids from returned data one by one, pass
965 * them into a child in the target namespace, read back the
966 * translated pids, and put them into our to-return data
969 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
970 perror("socketpair");
979 pid_to_ns_wrapper(sock
[1], tpid
);
984 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
986 ret
= send_creds(sock
[0], &cred
, v
, true);
988 if (ret
== SEND_CREDS_NOTSK
)
990 if (ret
== SEND_CREDS_FAIL
)
993 // read converted results
998 ret
= select(sock
[0]+1, &s
, NULL
, NULL
, &tv
);
1000 fprintf(stderr
, "%s: select error waiting for pid from child: %s\n",
1001 __func__
, strerror(errno
));
1004 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1005 fprintf(stderr
, "%s: error reading pid from child: %s\n",
1006 __func__
, strerror(errno
));
1009 NIH_MUST( nih_strcat_sprintf(d
, NULL
, "%d\n", qpid
) );
1011 ptr
= strchr(ptr
, '\n');
1017 cred
.pid
= getpid();
1019 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
1020 // failed to ask child to exit
1021 fprintf(stderr
, "%s: failed to ask child to exit: %s\n",
1022 __func__
, strerror(errno
));
1031 if (sock
[0] != -1) {
1038 static int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1039 struct fuse_file_info
*fi
)
1041 struct fuse_context
*fc
= fuse_get_context();
1042 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1043 nih_local
struct cgm_keys
*k
= NULL
;
1045 if (f
->type
!= LXC_TYPE_CGFILE
) {
1046 fprintf(stderr
, "Internal error: directory cache info used in cg_read\n");
1059 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) != NULL
) {
1060 nih_local
char *data
= NULL
;
1064 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
))
1065 // should never get here
1068 if (strcmp(f
->file
, "tasks") == 0 ||
1069 strcmp(f
->file
, "/tasks") == 0 ||
1070 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1071 strcmp(f
->file
, "cgroup.procs") == 0)
1072 // special case - we have to translate the pids
1073 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1075 r
= cgm_get_value(f
->controller
, f
->cgroup
, f
->file
, &data
);
1085 memcpy(buf
, data
, s
);
1086 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
1095 static void pid_from_ns(int sock
, pid_t tpid
)
1111 ret
= select(sock
+1, &s
, NULL
, NULL
, &tv
);
1113 fprintf(stderr
, "%s: bad select before read from parent: %s\n",
1114 __func__
, strerror(errno
));
1117 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1118 fprintf(stderr
, "%s: bad read from parent: %s\n",
1119 __func__
, strerror(errno
));
1122 if (vpid
== -1) // done
1126 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1128 cred
.pid
= getpid();
1129 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1136 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1138 int newnsfd
= -1, ret
, cpipe
[2];
1145 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1146 if (ret
< 0 || ret
>= sizeof(fnam
))
1148 newnsfd
= open(fnam
, O_RDONLY
);
1151 if (setns(newnsfd
, 0) < 0)
1155 if (pipe(cpipe
) < 0)
1167 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
1168 fprintf(stderr
, "%s (child): erorr on write: %s\n",
1169 __func__
, strerror(errno
));
1172 pid_from_ns(sock
, tpid
);
1175 // give the child 1 second to be done forking and
1178 FD_SET(cpipe
[0], &s
);
1181 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
1184 ret
= read(cpipe
[0], &v
, 1);
1185 if (ret
!= sizeof(char) || v
!= '1') {
1189 if (!wait_for_pid(cpid
))
1194 kill(cpid
, SIGKILL
);
1199 static bool do_write_pids(pid_t tpid
, const char *contrl
, const char *cg
, const char *file
, const char *buf
)
1201 int sock
[2] = {-1, -1};
1202 pid_t qpid
, cpid
= -1;
1203 bool answer
= false, fail
= false;
1206 * write the pids to a socket, have helper in writer's pidns
1207 * call movepid for us
1209 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1210 perror("socketpair");
1219 pid_from_ns_wrapper(sock
[1], tpid
);
1221 const char *ptr
= buf
;
1222 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1226 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1227 fprintf(stderr
, "%s: error writing pid to child: %s\n",
1228 __func__
, strerror(errno
));
1232 if (recv_creds(sock
[0], &cred
, &v
)) {
1234 if (!cgm_move_pid(contrl
, cg
, cred
.pid
))
1239 ptr
= strchr(ptr
, '\n');
1245 /* All good, write the value */
1247 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1248 fprintf(stderr
, "Warning: failed to ask child to exit\n");
1256 if (sock
[0] != -1) {
1263 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1264 struct fuse_file_info
*fi
)
1266 struct fuse_context
*fc
= fuse_get_context();
1267 nih_local
char *localbuf
= NULL
;
1268 nih_local
struct cgm_keys
*k
= NULL
;
1269 struct file_info
*f
= (struct file_info
*)fi
->fh
;
1271 if (f
->type
!= LXC_TYPE_CGFILE
) {
1272 fprintf(stderr
, "Internal error: directory cache info used in cg_write\n");
1282 localbuf
= NIH_MUST( nih_alloc(NULL
, size
+1) );
1283 localbuf
[size
] = '\0';
1284 memcpy(localbuf
, buf
, size
);
1286 if ((k
= get_cgroup_key(f
->controller
, f
->cgroup
, f
->file
)) != NULL
) {
1289 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
))
1292 if (strcmp(f
->file
, "tasks") == 0 ||
1293 strcmp(f
->file
, "/tasks") == 0 ||
1294 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1295 strcmp(f
->file
, "cgroup.procs") == 0)
1296 // special case - we have to translate the pids
1297 r
= do_write_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1299 r
= cgm_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1310 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1312 struct fuse_context
*fc
= fuse_get_context();
1313 nih_local
char * cgdir
= NULL
;
1314 char *fpath
= NULL
, *path1
, *path2
;
1315 nih_local
struct cgm_keys
*k
= NULL
;
1317 nih_local
char *controller
= NULL
;
1323 if (strcmp(path
, "/cgroup") == 0)
1326 controller
= pick_controller_from_path(fc
, path
);
1329 cgroup
= find_cgroup_in_path(path
);
1331 /* this is just /cgroup/controller */
1334 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1344 if (is_child_cgroup(controller
, path1
, path2
)) {
1345 // get uid, gid, from '/tasks' file and make up a mode
1346 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1347 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1350 k
= get_cgroup_key(controller
, path1
, path2
);
1356 * This being a fuse request, the uid and gid must be valid
1357 * in the caller's namespace. So we can just check to make
1358 * sure that the caller is root in his uid, and privileged
1359 * over the file's current owner.
1361 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
))
1364 if (!cgm_chown_file(controller
, cgroup
, uid
, gid
))
1369 int cg_chmod(const char *path
, mode_t mode
)
1371 struct fuse_context
*fc
= fuse_get_context();
1372 nih_local
char * cgdir
= NULL
;
1373 char *fpath
= NULL
, *path1
, *path2
;
1374 nih_local
struct cgm_keys
*k
= NULL
;
1376 nih_local
char *controller
= NULL
;
1381 if (strcmp(path
, "/cgroup") == 0)
1384 controller
= pick_controller_from_path(fc
, path
);
1387 cgroup
= find_cgroup_in_path(path
);
1389 /* this is just /cgroup/controller */
1392 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1402 if (is_child_cgroup(controller
, path1
, path2
)) {
1403 // get uid, gid, from '/tasks' file and make up a mode
1404 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1405 k
= get_cgroup_key(controller
, cgroup
, "tasks");
1408 k
= get_cgroup_key(controller
, path1
, path2
);
1414 * This being a fuse request, the uid and gid must be valid
1415 * in the caller's namespace. So we can just check to make
1416 * sure that the caller is root in his uid, and privileged
1417 * over the file's current owner.
1419 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
))
1422 if (!cgm_chmod_file(controller
, cgroup
, mode
))
1427 int cg_mkdir(const char *path
, mode_t mode
)
1429 struct fuse_context
*fc
= fuse_get_context();
1430 nih_local
struct cgm_keys
**list
= NULL
;
1431 char *fpath
= NULL
, *path1
;
1432 nih_local
char * cgdir
= NULL
;
1434 nih_local
char *controller
= NULL
;
1440 controller
= pick_controller_from_path(fc
, path
);
1444 cgroup
= find_cgroup_in_path(path
);
1448 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1454 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
))
1458 if (!cgm_create(controller
, cgroup
, fc
->uid
, fc
->gid
))
1464 static int cg_rmdir(const char *path
)
1466 struct fuse_context
*fc
= fuse_get_context();
1467 nih_local
struct cgm_keys
**list
= NULL
;
1469 nih_local
char * cgdir
= NULL
;
1471 nih_local
char *controller
= NULL
;
1477 controller
= pick_controller_from_path(fc
, path
);
1481 cgroup
= find_cgroup_in_path(path
);
1485 get_cgdir_and_path(cgroup
, &cgdir
, &fpath
);
1489 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
))
1492 if (!cgm_remove(controller
, cgroup
))
1498 static bool startswith(const char *line
, const char *pref
)
1500 if (strncmp(line
, pref
, strlen(pref
)) == 0)
1505 static void get_mem_cached(char *memstat
, unsigned long *v
)
1511 if (startswith(memstat
, "total_cache")) {
1512 sscanf(memstat
+ 11, "%lu", v
);
1516 eol
= strchr(memstat
, '\n');
1523 static void get_blkio_io_value(char *str
, unsigned major
, unsigned minor
, char *iotype
, unsigned long *v
)
1529 snprintf(key
, 32, "%u:%u %s", major
, minor
, iotype
);
1531 size_t len
= strlen(key
);
1535 if (startswith(str
, key
)) {
1536 sscanf(str
+ len
, "%lu", v
);
1539 eol
= strchr(str
, '\n');
1546 static char *get_pid_cgroup(pid_t pid
, const char *contrl
)
1548 nih_local
char *fnam
= NULL
;
1550 char *answer
= NULL
;
1554 fnam
= NIH_MUST( nih_sprintf(NULL
, "/proc/%d/cgroup", pid
) );
1555 if (!(f
= fopen(fnam
, "r")))
1558 while (getline(&line
, &len
, f
) != -1) {
1562 c1
= strchr(line
, ':');
1566 c2
= strchr(c1
, ':');
1570 if (strcmp(c1
, contrl
) != 0)
1574 answer
= NIH_MUST( nih_strdup(NULL
, c2
) );
1585 * FUSE ops for /proc
1588 static int proc_meminfo_read(char *buf
, size_t size
, off_t offset
,
1589 struct fuse_file_info
*fi
)
1591 struct fuse_context
*fc
= fuse_get_context();
1592 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1593 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "memory");
1594 nih_local
char *memlimit_str
= NULL
, *memusage_str
= NULL
, *memstat_str
= NULL
;
1595 unsigned long memlimit
= 0, memusage
= 0, cached
= 0, hosttotal
= 0;
1597 size_t linelen
= 0, total_len
= 0;
1598 char *cache
= d
->buf
;
1599 size_t cache_size
= d
->buflen
;
1603 if (offset
> d
->size
)
1605 int left
= d
->size
- offset
;
1606 total_len
= left
> size
? size
: left
;
1607 memcpy(buf
, cache
+ offset
, total_len
);
1614 if (!cgm_get_value("memory", cg
, "memory.limit_in_bytes", &memlimit_str
))
1616 if (!cgm_get_value("memory", cg
, "memory.usage_in_bytes", &memusage_str
))
1618 if (!cgm_get_value("memory", cg
, "memory.stat", &memstat_str
))
1620 memlimit
= strtoul(memlimit_str
, NULL
, 10);
1621 memusage
= strtoul(memusage_str
, NULL
, 10);
1624 get_mem_cached(memstat_str
, &cached
);
1626 f
= fopen("/proc/meminfo", "r");
1630 while (getline(&line
, &linelen
, f
) != -1) {
1632 char *printme
, lbuf
[100];
1634 memset(lbuf
, 0, 100);
1635 if (startswith(line
, "MemTotal:")) {
1636 sscanf(line
+14, "%lu", &hosttotal
);
1637 if (hosttotal
< memlimit
)
1638 memlimit
= hosttotal
;
1639 snprintf(lbuf
, 100, "MemTotal: %8lu kB\n", memlimit
);
1641 } else if (startswith(line
, "MemFree:")) {
1642 snprintf(lbuf
, 100, "MemFree: %8lu kB\n", memlimit
- memusage
);
1644 } else if (startswith(line
, "MemAvailable:")) {
1645 snprintf(lbuf
, 100, "MemAvailable: %8lu kB\n", memlimit
- memusage
);
1647 } else if (startswith(line
, "Buffers:")) {
1648 snprintf(lbuf
, 100, "Buffers: %8lu kB\n", 0UL);
1650 } else if (startswith(line
, "Cached:")) {
1651 snprintf(lbuf
, 100, "Cached: %8lu kB\n", cached
);
1653 } else if (startswith(line
, "SwapCached:")) {
1654 snprintf(lbuf
, 100, "SwapCached: %8lu kB\n", 0UL);
1659 l
= snprintf(cache
, cache_size
, "%s", printme
);
1665 d
->size
= total_len
;
1666 if (total_len
> size
) total_len
= size
;
1667 memcpy(buf
, d
->buf
, total_len
);
1675 * Read the cpuset.cpus for cg
1676 * Return the answer in a nih_alloced string
1678 static char *get_cpuset(const char *cg
)
1682 if (!cgm_get_value("cpuset", cg
, "cpuset.cpus", &answer
))
1688 * Helper functions for cpuset_in-set
1690 char *cpuset_nexttok(const char *c
)
1692 char *r
= strchr(c
+1, ',');
1698 int cpuset_getrange(const char *c
, int *a
, int *b
)
1702 ret
= sscanf(c
, "%d-%d", a
, b
);
1707 * cpusets are in format "1,2-3,4"
1708 * iow, comma-delimited ranges
1710 static bool cpu_in_cpuset(int cpu
, const char *cpuset
)
1714 for (c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
1717 ret
= cpuset_getrange(c
, &a
, &b
);
1718 if (ret
== 1 && cpu
== a
)
1720 if (ret
!= 2) // bad cpuset!
1722 if (cpu
>= a
&& cpu
<= b
)
1729 static bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
1733 if (sscanf(line
, "processor : %d", &cpu
) != 1)
1735 return cpu_in_cpuset(cpu
, cpuset
);
1739 * check whether this is a '^processor" line in /proc/cpuinfo
1741 static bool is_processor_line(const char *line
)
1745 if (sscanf(line
, "processor : %d", &cpu
) == 1)
1750 static int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
1751 struct fuse_file_info
*fi
)
1753 struct fuse_context
*fc
= fuse_get_context();
1754 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1755 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1756 nih_local
char *cpuset
= NULL
;
1758 size_t linelen
= 0, total_len
= 0;
1759 bool am_printing
= false;
1761 char *cache
= d
->buf
;
1762 size_t cache_size
= d
->buflen
;
1766 if (offset
> d
->size
)
1768 int left
= d
->size
- offset
;
1769 total_len
= left
> size
? size
: left
;
1770 memcpy(buf
, cache
+ offset
, total_len
);
1777 cpuset
= get_cpuset(cg
);
1781 f
= fopen("/proc/cpuinfo", "r");
1785 while (getline(&line
, &linelen
, f
) != -1) {
1787 if (is_processor_line(line
)) {
1788 am_printing
= cpuline_in_cpuset(line
, cpuset
);
1791 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
1792 if (l
< cache_size
){
1797 cache
+= cache_size
;
1798 total_len
+= cache_size
;
1806 l
= snprintf(cache
, cache_size
, "%s", line
);
1807 if (l
< cache_size
) {
1812 cache
+= cache_size
;
1813 total_len
+= cache_size
;
1820 d
->size
= total_len
;
1821 if (total_len
> size
) total_len
= size
;
1823 /* read from off 0 */
1824 memcpy(buf
, d
->buf
, total_len
);
1831 static int proc_stat_read(char *buf
, size_t size
, off_t offset
,
1832 struct fuse_file_info
*fi
)
1834 struct fuse_context
*fc
= fuse_get_context();
1835 struct file_info
*d
= (struct file_info
*)fi
->fh
;
1836 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "cpuset");
1837 nih_local
char *cpuset
= NULL
;
1839 size_t linelen
= 0, total_len
= 0;
1840 int curcpu
= -1; /* cpu numbering starts at 0 */
1841 unsigned long user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0, softirq
= 0, steal
= 0, guest
= 0;
1842 unsigned long user_sum
= 0, nice_sum
= 0, system_sum
= 0, idle_sum
= 0, iowait_sum
= 0,
1843 irq_sum
= 0, softirq_sum
= 0, steal_sum
= 0, guest_sum
= 0;
1844 #define CPUALL_MAX_SIZE BUF_RESERVE_SIZE
1845 char cpuall
[CPUALL_MAX_SIZE
];
1846 /* reserve for cpu all */
1847 char *cache
= d
->buf
+ CPUALL_MAX_SIZE
;
1848 size_t cache_size
= d
->buflen
- CPUALL_MAX_SIZE
;
1852 if (offset
> d
->size
)
1854 int left
= d
->size
- offset
;
1855 total_len
= left
> size
? size
: left
;
1856 memcpy(buf
, d
->buf
+ offset
, total_len
);
1863 cpuset
= get_cpuset(cg
);
1867 f
= fopen("/proc/stat", "r");
1872 if (getline(&line
, &linelen
, f
) < 0) {
1873 fprintf(stderr
, "proc_stat_read read first line failed\n");
1877 while (getline(&line
, &linelen
, f
) != -1) {
1880 char cpu_char
[10]; /* That's a lot of cores */
1883 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1) {
1884 /* not a ^cpuN line containing a number N, just print it */
1885 l
= snprintf(cache
, cache_size
, "%s", line
);
1886 if (l
< cache_size
){
1892 //no more space, break it
1893 cache
+= cache_size
;
1894 total_len
+= cache_size
;
1900 if (sscanf(cpu_char
, "%d", &cpu
) != 1)
1902 if (!cpu_in_cpuset(cpu
, cpuset
))
1906 c
= strchr(line
, ' ');
1909 l
= snprintf(cache
, cache_size
, "cpu%d%s", curcpu
, c
);
1914 if (sscanf(line
, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user
, &nice
, &system
, &idle
, &iowait
, &irq
,
1915 &softirq
, &steal
, &guest
) != 9)
1919 system_sum
+= system
;
1921 iowait_sum
+= iowait
;
1923 softirq_sum
+= softirq
;
1930 int cpuall_len
= snprintf(cpuall
, CPUALL_MAX_SIZE
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
1931 "cpu ", user_sum
, nice_sum
, system_sum
, idle_sum
, iowait_sum
, irq_sum
, softirq_sum
, steal_sum
, guest_sum
);
1932 if (cpuall_len
> 0 && cpuall_len
< CPUALL_MAX_SIZE
){
1933 memcpy(cache
, cpuall
, cpuall_len
);
1934 cache
+= cpuall_len
;
1936 /* shouldn't happen */
1937 fprintf(stderr
, "proc_stat_read copy cpuall failed, cpuall_len=%d\n", cpuall_len
);
1941 memmove(cache
, d
->buf
+ CPUALL_MAX_SIZE
, total_len
);
1942 total_len
+= cpuall_len
;
1943 d
->size
= total_len
;
1944 if (total_len
> size
) total_len
= size
;
1946 memcpy(buf
, d
->buf
, total_len
);
1954 * How to guess what to present for uptime?
1955 * One thing we could do would be to take the date on the caller's
1956 * memory.usage_in_bytes file, which should equal the time of creation
1957 * of his cgroup. However, a task could be in a sub-cgroup of the
1958 * container. The same problem exists if we try to look at the ages
1959 * of processes in the caller's cgroup.
1961 * So we'll fork a task that will enter the caller's pidns, mount a
1962 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
1964 * For the second uptime #, we'll do as Stéphane had done, just copy
1965 * the number from /proc/uptime. Not sure how to best emulate 'idle'
1966 * time. Maybe someone can come up with a good algorithm and submit a
1967 * patch. Maybe something based on cpushare info?
1970 /* return age of the reaper for $pid, taken from ctime of its procdir */
1971 static long int get_pid1_time(pid_t pid
)
1974 int fd
, cpipe
[2], ret
;
1981 if (unshare(CLONE_NEWNS
))
1984 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
1985 perror("rslave mount failed");
1989 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", pid
);
1990 if (ret
< 0 || ret
>= sizeof(fnam
))
1993 fd
= open(fnam
, O_RDONLY
);
1995 perror("get_pid1_time open of ns/pid");
1999 perror("get_pid1_time setns 1");
2005 if (pipe(cpipe
) < 0)
2016 if (write(cpipe
[1], &b
, sizeof(char)) < 0) {
2017 fprintf(stderr
, "%s (child): erorr on write: %s\n",
2018 __func__
, strerror(errno
));
2021 umount2("/proc", MNT_DETACH
);
2022 if (mount("proc", "/proc", "proc", 0, NULL
)) {
2023 perror("get_pid1_time mount");
2026 ret
= lstat("/proc/1", &sb
);
2028 perror("get_pid1_time lstat");
2031 return time(NULL
) - sb
.st_ctime
;
2034 // give the child 1 second to be done forking and
2037 FD_SET(cpipe
[0], &s
);
2040 ret
= select(cpipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2043 ret
= read(cpipe
[0], &v
, 1);
2044 if (ret
!= sizeof(char) || v
!= '1') {
2052 kill(cpid
, SIGKILL
);
2057 static long int getreaperage(pid_t qpid
)
2059 int pid
, mypipe
[2], ret
;
2062 long int mtime
, answer
= 0;
2070 if (!pid
) { // child
2071 mtime
= get_pid1_time(qpid
);
2072 if (write(mypipe
[1], &mtime
, sizeof(mtime
)) != sizeof(mtime
))
2073 fprintf(stderr
, "Warning: bad write from getreaperage\n");
2079 FD_SET(mypipe
[0], &s
);
2082 ret
= select(mypipe
[0]+1, &s
, NULL
, NULL
, &tv
);
2088 fprintf(stderr
, "timed out\n");
2091 if (read(mypipe
[0], &mtime
, sizeof(mtime
)) != sizeof(mtime
)) {
2103 static long int getprocidle(void)
2105 FILE *f
= fopen("/proc/uptime", "r");
2110 ret
= fscanf(f
, "%ld %ld", &age
, &idle
);
2118 * We read /proc/uptime and reuse its second field.
2119 * For the first field, we use the mtime for the reaper for
2120 * the calling pid as returned by getreaperage
2122 static int proc_uptime_read(char *buf
, size_t size
, off_t offset
,
2123 struct fuse_file_info
*fi
)
2125 struct fuse_context
*fc
= fuse_get_context();
2126 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2127 long int reaperage
= getreaperage(fc
->pid
);;
2128 long int idletime
= getprocidle();
2129 size_t total_len
= 0;
2132 if (offset
> d
->size
)
2137 total_len
= snprintf(buf
, size
, "%ld %ld\n", reaperage
, idletime
);
2138 d
->size
= total_len
;
2142 static int proc_diskstats_read(char *buf
, size_t size
, off_t offset
,
2143 struct fuse_file_info
*fi
)
2146 struct fuse_context
*fc
= fuse_get_context();
2147 struct file_info
*d
= (struct file_info
*)fi
->fh
;
2148 nih_local
char *cg
= get_pid_cgroup(fc
->pid
, "blkio");
2149 nih_local
char *io_serviced_str
= NULL
, *io_merged_str
= NULL
, *io_service_bytes_str
= NULL
,
2150 *io_wait_time_str
= NULL
, *io_service_time_str
= NULL
;
2151 unsigned long read
= 0, write
= 0;
2152 unsigned long read_merged
= 0, write_merged
= 0;
2153 unsigned long read_sectors
= 0, write_sectors
= 0;
2154 unsigned long read_ticks
= 0, write_ticks
= 0;
2155 unsigned long ios_pgr
= 0, tot_ticks
= 0, rq_ticks
= 0;
2156 unsigned long rd_svctm
= 0, wr_svctm
= 0, rd_wait
= 0, wr_wait
= 0;
2158 size_t linelen
= 0, total_len
= 0;
2159 unsigned int major
= 0, minor
= 0;
2164 if (offset
> d
->size
)
2172 if (!cgm_get_value("blkio", cg
, "blkio.io_serviced", &io_serviced_str
))
2174 if (!cgm_get_value("blkio", cg
, "blkio.io_merged", &io_merged_str
))
2176 if (!cgm_get_value("blkio", cg
, "blkio.io_service_bytes", &io_service_bytes_str
))
2178 if (!cgm_get_value("blkio", cg
, "blkio.io_wait_time", &io_wait_time_str
))
2180 if (!cgm_get_value("blkio", cg
, "blkio.io_service_time", &io_service_time_str
))
2184 f
= fopen("/proc/diskstats", "r");
2188 while (getline(&line
, &linelen
, f
) != -1) {
2190 char *printme
, lbuf
[256];
2192 i
= sscanf(line
, "%u %u %71s", &major
, &minor
, dev_name
);
2194 get_blkio_io_value(io_serviced_str
, major
, minor
, "Read", &read
);
2195 get_blkio_io_value(io_serviced_str
, major
, minor
, "Write", &write
);
2196 get_blkio_io_value(io_merged_str
, major
, minor
, "Read", &read_merged
);
2197 get_blkio_io_value(io_merged_str
, major
, minor
, "Write", &write_merged
);
2198 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Read", &read_sectors
);
2199 read_sectors
= read_sectors
/512;
2200 get_blkio_io_value(io_service_bytes_str
, major
, minor
, "Write", &write_sectors
);
2201 write_sectors
= write_sectors
/512;
2203 get_blkio_io_value(io_service_time_str
, major
, minor
, "Read", &rd_svctm
);
2204 rd_svctm
= rd_svctm
/1000000;
2205 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Read", &rd_wait
);
2206 rd_wait
= rd_wait
/1000000;
2207 read_ticks
= rd_svctm
+ rd_wait
;
2209 get_blkio_io_value(io_service_time_str
, major
, minor
, "Write", &wr_svctm
);
2210 wr_svctm
= wr_svctm
/1000000;
2211 get_blkio_io_value(io_wait_time_str
, major
, minor
, "Write", &wr_wait
);
2212 wr_wait
= wr_wait
/1000000;
2213 write_ticks
= wr_svctm
+ wr_wait
;
2215 get_blkio_io_value(io_service_time_str
, major
, minor
, "Total", &tot_ticks
);
2216 tot_ticks
= tot_ticks
/1000000;
2221 memset(lbuf
, 0, 256);
2222 if (read
|| write
|| read_merged
|| write_merged
|| read_sectors
|| write_sectors
|| read_ticks
|| write_ticks
) {
2223 snprintf(lbuf
, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2224 major
, minor
, dev_name
, read
, read_merged
, read_sectors
, read_ticks
,
2225 write
, write_merged
, write_sectors
, write_ticks
, ios_pgr
, tot_ticks
, rq_ticks
);
2230 l
= snprintf(buf
, size
, "%s", printme
);
2236 d
->size
= total_len
;
2243 static off_t
get_procfile_size(const char *which
)
2245 FILE *f
= fopen(which
, "r");
2248 ssize_t sz
, answer
= 0;
2252 while ((sz
= getline(&line
, &len
, f
)) != -1)
2260 static int proc_getattr(const char *path
, struct stat
*sb
)
2262 struct timespec now
;
2264 memset(sb
, 0, sizeof(struct stat
));
2265 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
2267 sb
->st_uid
= sb
->st_gid
= 0;
2268 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
2269 if (strcmp(path
, "/proc") == 0) {
2270 sb
->st_mode
= S_IFDIR
| 00555;
2274 if (strcmp(path
, "/proc/meminfo") == 0 ||
2275 strcmp(path
, "/proc/cpuinfo") == 0 ||
2276 strcmp(path
, "/proc/uptime") == 0 ||
2277 strcmp(path
, "/proc/stat") == 0 ||
2278 strcmp(path
, "/proc/diskstats") == 0) {
2280 sb
->st_mode
= S_IFREG
| 00444;
2288 static int proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2289 struct fuse_file_info
*fi
)
2291 if (filler(buf
, "cpuinfo", NULL
, 0) != 0 ||
2292 filler(buf
, "meminfo", NULL
, 0) != 0 ||
2293 filler(buf
, "stat", NULL
, 0) != 0 ||
2294 filler(buf
, "uptime", NULL
, 0) != 0 ||
2295 filler(buf
, "diskstats", NULL
, 0) != 0)
2300 static int proc_open(const char *path
, struct fuse_file_info
*fi
)
2303 struct file_info
*info
;
2305 if (strcmp(path
, "/proc/meminfo") == 0)
2306 type
= LXC_TYPE_PROC_MEMINFO
;
2307 else if (strcmp(path
, "/proc/cpuinfo") == 0)
2308 type
= LXC_TYPE_PROC_CPUINFO
;
2309 else if (strcmp(path
, "/proc/uptime") == 0)
2310 type
= LXC_TYPE_PROC_UPTIME
;
2311 else if (strcmp(path
, "/proc/stat") == 0)
2312 type
= LXC_TYPE_PROC_STAT
;
2313 else if (strcmp(path
, "/proc/diskstats") == 0)
2314 type
= LXC_TYPE_PROC_DISKSTATS
;
2318 info
= NIH_MUST( nih_alloc(NULL
, sizeof(*info
)) );
2319 memset(info
, 0, sizeof(*info
));
2322 info
->buflen
= get_procfile_size(path
) + BUF_RESERVE_SIZE
;
2323 info
->buf
= NIH_MUST( nih_alloc(info
, info
->buflen
) );
2324 memset(info
->buf
, 0, info
->buflen
);
2325 /* set actual size to buffer size */
2326 info
->size
= info
->buflen
;
2328 fi
->fh
= (unsigned long)info
;
2332 static int proc_release(const char *path
, struct fuse_file_info
*fi
)
2334 struct file_info
*f
= (struct file_info
*)fi
->fh
;
2336 do_release_file_info(f
);
2340 static int proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2341 struct fuse_file_info
*fi
)
2343 struct file_info
*f
= (struct file_info
*) fi
->fh
;
2346 case LXC_TYPE_PROC_MEMINFO
:
2347 return proc_meminfo_read(buf
, size
, offset
, fi
);
2348 case LXC_TYPE_PROC_CPUINFO
:
2349 return proc_cpuinfo_read(buf
, size
, offset
, fi
);
2350 case LXC_TYPE_PROC_UPTIME
:
2351 return proc_uptime_read(buf
, size
, offset
, fi
);
2352 case LXC_TYPE_PROC_STAT
:
2353 return proc_stat_read(buf
, size
, offset
, fi
);
2354 case LXC_TYPE_PROC_DISKSTATS
:
2355 return proc_diskstats_read(buf
, size
, offset
, fi
);
2363 * these just delegate to the /proc and /cgroup ops as
2367 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
2369 if (strcmp(path
, "/") == 0) {
2370 sb
->st_mode
= S_IFDIR
| 00755;
2374 if (strncmp(path
, "/cgroup", 7) == 0) {
2375 return cg_getattr(path
, sb
);
2377 if (strncmp(path
, "/proc", 5) == 0) {
2378 return proc_getattr(path
, sb
);
2383 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
2385 if (strcmp(path
, "/") == 0)
2388 if (strncmp(path
, "/cgroup", 7) == 0) {
2389 return cg_opendir(path
, fi
);
2391 if (strcmp(path
, "/proc") == 0)
2396 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
, off_t offset
,
2397 struct fuse_file_info
*fi
)
2399 if (strcmp(path
, "/") == 0) {
2400 if (filler(buf
, "proc", NULL
, 0) != 0 ||
2401 filler(buf
, "cgroup", NULL
, 0) != 0)
2405 if (strncmp(path
, "/cgroup", 7) == 0)
2406 return cg_readdir(path
, buf
, filler
, offset
, fi
);
2407 if (strcmp(path
, "/proc") == 0)
2408 return proc_readdir(path
, buf
, filler
, offset
, fi
);
2412 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
2414 if (strcmp(path
, "/") == 0)
2416 if (strncmp(path
, "/cgroup", 7) == 0) {
2417 return cg_releasedir(path
, fi
);
2419 if (strcmp(path
, "/proc") == 0)
2424 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
2426 if (strncmp(path
, "/cgroup", 7) == 0)
2427 return cg_open(path
, fi
);
2428 if (strncmp(path
, "/proc", 5) == 0)
2429 return proc_open(path
, fi
);
2434 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
2435 struct fuse_file_info
*fi
)
2437 if (strncmp(path
, "/cgroup", 7) == 0)
2438 return cg_read(path
, buf
, size
, offset
, fi
);
2439 if (strncmp(path
, "/proc", 5) == 0)
2440 return proc_read(path
, buf
, size
, offset
, fi
);
2445 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
2446 struct fuse_file_info
*fi
)
2448 if (strncmp(path
, "/cgroup", 7) == 0) {
2449 return cg_write(path
, buf
, size
, offset
, fi
);
2455 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
2460 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
2462 if (strncmp(path
, "/cgroup", 7) == 0)
2463 return cg_release(path
, fi
);
2464 if (strncmp(path
, "/proc", 5) == 0)
2465 return proc_release(path
, fi
);
2470 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
2475 int lxcfs_mkdir(const char *path
, mode_t mode
)
2477 if (strncmp(path
, "/cgroup", 7) == 0)
2478 return cg_mkdir(path
, mode
);
2483 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
2485 if (strncmp(path
, "/cgroup", 7) == 0)
2486 return cg_chown(path
, uid
, gid
);
2492 * cat first does a truncate before doing ops->write. This doesn't
2493 * really make sense for cgroups. So just return 0 always but do
2496 int lxcfs_truncate(const char *path
, off_t newsize
)
2498 if (strncmp(path
, "/cgroup", 7) == 0)
2503 int lxcfs_rmdir(const char *path
)
2505 if (strncmp(path
, "/cgroup", 7) == 0)
2506 return cg_rmdir(path
);
2510 int lxcfs_chmod(const char *path
, mode_t mode
)
2512 if (strncmp(path
, "/cgroup", 7) == 0)
2513 return cg_chmod(path
, mode
);
2517 const struct fuse_operations lxcfs_ops
= {
2518 .getattr
= lxcfs_getattr
,
2522 .mkdir
= lxcfs_mkdir
,
2524 .rmdir
= lxcfs_rmdir
,
2528 .chmod
= lxcfs_chmod
,
2529 .chown
= lxcfs_chown
,
2530 .truncate
= lxcfs_truncate
,
2535 .release
= lxcfs_release
,
2536 .write
= lxcfs_write
,
2539 .flush
= lxcfs_flush
,
2540 .fsync
= lxcfs_fsync
,
2545 .removexattr
= NULL
,
2547 .opendir
= lxcfs_opendir
,
2548 .readdir
= lxcfs_readdir
,
2549 .releasedir
= lxcfs_releasedir
,
2560 static void usage(const char *me
)
2562 fprintf(stderr
, "Usage:\n");
2563 fprintf(stderr
, "\n");
2564 fprintf(stderr
, "%s mountpoint\n", me
);
2565 fprintf(stderr
, "%s -h\n", me
);
2569 static bool is_help(char *w
)
2571 if (strcmp(w
, "-h") == 0 ||
2572 strcmp(w
, "--help") == 0 ||
2573 strcmp(w
, "-help") == 0 ||
2574 strcmp(w
, "help") == 0)
2579 void swallow_arg(int *argcp
, char *argv
[], char *which
)
2583 for (i
= 1; argv
[i
]; i
++) {
2584 if (strcmp(argv
[i
], which
) != 0)
2586 for (; argv
[i
]; i
++) {
2587 argv
[i
] = argv
[i
+1];
2594 void swallow_option(int *argcp
, char *argv
[], char *opt
, char *v
)
2598 for (i
= 1; argv
[i
]; i
++) {
2601 if (strcmp(argv
[i
], opt
) != 0)
2603 if (strcmp(argv
[i
+1], v
) != 0) {
2604 fprintf(stderr
, "Warning: unexpected fuse option %s\n", v
);
2607 for (; argv
[i
+1]; i
++) {
2608 argv
[i
] = argv
[i
+2];
2615 int main(int argc
, char *argv
[])
2618 struct lxcfs_state
*d
= NULL
;
2620 * what we pass to fuse_main is:
2621 * argv[0] -s -f -o allow_other,directio argv[1] NULL
2626 /* accomodate older init scripts */
2627 swallow_arg(&argc
, argv
, "-s");
2628 swallow_arg(&argc
, argv
, "-f");
2629 swallow_option(&argc
, argv
, "-o", "allow_other");
2631 if (argc
!= 2 || is_help(argv
[1]))
2634 d
= NIH_MUST( malloc(sizeof(*d
)) );
2636 newargv
[0] = argv
[0];
2640 newargv
[4] = "allow_other,direct_io";
2641 newargv
[5] = argv
[1];
2644 if (!cgm_escape_cgroup())
2645 fprintf(stderr
, "WARNING: failed to escape to root cgroup\n");
2647 if (!cgm_get_controllers(&d
->subsystems
))
2650 ret
= fuse_main(NARGS
- 1, newargv
, &lxcfs_ops
, d
);