]> git.proxmox.com Git - mirror_lxcfs.git/blob - lxcfs.c
cache opendir work and re-use it at readdir
[mirror_lxcfs.git] / lxcfs.c
1 /* lxcfs
2 *
3 * Copyright © 2014 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
5 *
6 * See COPYING file for details.
7 */
8
9 /*
10 * NOTES - make sure to run this as -s to avoid threading.
11 * TODO - can we enforce that here from the code?
12 */
13 #define FUSE_USE_VERSION 26
14
15 #include <stdio.h>
16 #include <dirent.h>
17 #include <fcntl.h>
18 #include <fuse.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <time.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <libgen.h>
26 #include <sched.h>
27 #include <linux/sched.h>
28 #include <sys/socket.h>
29 #include <sys/mount.h>
30 #include <wait.h>
31
32 #include <nih/alloc.h>
33 #include <nih/string.h>
34 #include <nih/alloc.h>
35
36 #include "cgmanager.h"
37
38 struct lxcfs_state {
39 /*
40 * a null-terminated, nih-allocated list of the mounted subsystems. We
41 * detect this at startup.
42 */
43 char **subsystems;
44 };
45 #define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
46
47 struct file_info {
48 char *controller;
49 char *cgroup;
50 bool isdir;
51 char *buf; // unused as of yet
52 int buflen;
53 };
54
55 static char *must_copy_string(const char *str)
56 {
57 if (!str)
58 return NULL;
59 return NIH_MUST( nih_strdup(NULL, str) );
60 }
61
62 /*
63 * TODO - return value should denote whether child exited with failure
64 * so callers can return errors. Esp read/write of tasks and cgroup.procs
65 */
66 static int wait_for_pid(pid_t pid)
67 {
68 int status, ret;
69
70 again:
71 ret = waitpid(pid, &status, 0);
72 if (ret == -1) {
73 if (errno == EINTR)
74 goto again;
75 return -1;
76 }
77 if (ret != pid)
78 goto again;
79 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
80 return -1;
81 return 0;
82 }
83
84 /*
85 * Given a open file * to /proc/pid/{u,g}id_map, and an id
86 * valid in the caller's namespace, return the id mapped into
87 * pid's namespace.
88 * Returns the mapped id, or -1 on error.
89 */
90 unsigned int
91 convert_id_to_ns(FILE *idfile, unsigned int in_id)
92 {
93 unsigned int nsuid, // base id for a range in the idfile's namespace
94 hostuid, // base id for a range in the caller's namespace
95 count; // number of ids in this range
96 char line[400];
97 int ret;
98
99 fseek(idfile, 0L, SEEK_SET);
100 while (fgets(line, 400, idfile)) {
101 ret = sscanf(line, "%u %u %u\n", &nsuid, &hostuid, &count);
102 if (ret != 3)
103 continue;
104 if (hostuid + count < hostuid || nsuid + count < nsuid) {
105 /*
106 * uids wrapped around - unexpected as this is a procfile,
107 * so just bail.
108 */
109 fprintf(stderr, "pid wrapparound at entry %u %u %u in %s\n",
110 nsuid, hostuid, count, line);
111 return -1;
112 }
113 if (hostuid <= in_id && hostuid+count > in_id) {
114 /*
115 * now since hostuid <= in_id < hostuid+count, and
116 * hostuid+count and nsuid+count do not wrap around,
117 * we know that nsuid+(in_id-hostuid) which must be
118 * less that nsuid+(count) must not wrap around
119 */
120 return (in_id - hostuid) + nsuid;
121 }
122 }
123
124 // no answer found
125 return -1;
126 }
127
128 /*
129 * for is_privileged_over,
130 * specify whether we require the calling uid to be root in his
131 * namespace
132 */
133 #define NS_ROOT_REQD true
134 #define NS_ROOT_OPT false
135
136 static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_root)
137 {
138 nih_local char *fpath = NULL;
139 bool answer = false;
140 uid_t nsuid;
141
142 if (victim == -1 || uid == -1)
143 return false;
144
145 /*
146 * If the request is one not requiring root in the namespace,
147 * then having the same uid suffices. (i.e. uid 1000 has write
148 * access to files owned by uid 1000
149 */
150 if (!req_ns_root && uid == victim)
151 return true;
152
153 fpath = NIH_MUST( nih_sprintf(NULL, "/proc/%d/uid_map", pid) );
154 FILE *f = fopen(fpath, "r");
155 if (!f)
156 return false;
157
158 /* if caller's not root in his namespace, reject */
159 nsuid = convert_id_to_ns(f, uid);
160 if (nsuid)
161 goto out;
162
163 /*
164 * If victim is not mapped into caller's ns, reject.
165 * XXX I'm not sure this check is needed given that fuse
166 * will be sending requests where the vfs has converted
167 */
168 nsuid = convert_id_to_ns(f, victim);
169 if (nsuid == -1)
170 goto out;
171
172 answer = true;
173
174 out:
175 fclose(f);
176 return answer;
177 }
178
179 static bool perms_include(int fmode, mode_t req_mode)
180 {
181 mode_t r;
182
183 switch (req_mode & O_ACCMODE) {
184 case O_RDONLY:
185 r = S_IROTH;
186 break;
187 case O_WRONLY:
188 r = S_IWOTH;
189 break;
190 case O_RDWR:
191 r = S_IROTH | S_IWOTH;
192 break;
193 default:
194 return false;
195 }
196 return ((fmode & r) == r);
197 }
198
199 static char *get_next_cgroup_dir(const char *taskcg, const char *querycg)
200 {
201 char *start, *end;
202
203 if (strlen(taskcg) <= strlen(querycg)) {
204 fprintf(stderr, "%s: I was fed bad input\n", __func__);
205 return NULL;
206 }
207
208 if (strcmp(querycg, "/") == 0)
209 start = NIH_MUST( nih_strdup(NULL, taskcg + 1) );
210 else
211 start = NIH_MUST( nih_strdup(NULL, taskcg + strlen(querycg) + 1) );
212 end = strchr(start, '/');
213 if (end)
214 *end = '\0';
215 return start;
216 }
217
218 /*
219 * check whether a fuse context may access a cgroup dir or file
220 *
221 * If file is not null, it is a cgroup file to check under cg.
222 * If file is null, then we are checking perms on cg itself.
223 *
224 * For files we can check the mode of the list_keys result.
225 * For cgroups, we must make assumptions based on the files under the
226 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
227 * yet.
228 */
229 static bool fc_may_access(struct fuse_context *fc, const char *contrl, const char *cg, const char *file, mode_t mode)
230 {
231 nih_local struct cgm_keys **list = NULL;
232 int i;
233
234 if (!file)
235 file = "tasks";
236
237 if (*file == '/')
238 file++;
239
240 if (!cgm_list_keys(contrl, cg, &list))
241 return false;
242 for (i = 0; list[i]; i++) {
243 if (strcmp(list[i]->name, file) == 0) {
244 struct cgm_keys *k = list[i];
245 if (is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_OPT)) {
246 if (perms_include(k->mode >> 6, mode))
247 return true;
248 }
249 if (fc->gid == k->gid) {
250 if (perms_include(k->mode >> 3, mode))
251 return true;
252 }
253 return perms_include(k->mode, mode);
254 }
255 }
256
257 return false;
258 }
259
260 static void stripnewline(char *x)
261 {
262 size_t l = strlen(x);
263 if (l && x[l-1] == '\n')
264 x[l-1] = '\0';
265 }
266
267 /*
268 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
269 * If caller is in /a, he may act on /a/b, but not on /b.
270 * if the answer is false and nextcg is not NULL, then *nextcg will point
271 * to a nih_alloc'd string containing the next cgroup directory under cg
272 */
273 static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg, char **nextcg)
274 {
275 nih_local char *fnam = NULL;
276 FILE *f;
277 bool answer = false;
278 char *line = NULL;
279 size_t len = 0;
280
281 fnam = NIH_MUST( nih_sprintf(NULL, "/proc/%d/cgroup", pid) );
282 if (!(f = fopen(fnam, "r")))
283 return false;
284
285 while (getline(&line, &len, f) != -1) {
286 char *c1, *c2, *linecmp;
287 if (!line[0])
288 continue;
289 c1 = strchr(line, ':');
290 if (!c1)
291 goto out;
292 c1++;
293 c2 = strchr(c1, ':');
294 if (!c2)
295 goto out;
296 *c2 = '\0';
297 if (strcmp(c1, contrl) != 0)
298 continue;
299 c2++;
300 stripnewline(c2);
301 /*
302 * callers pass in '/' for root cgroup, otherwise they pass
303 * in a cgroup without leading '/'
304 */
305 linecmp = *cg == '/' ? c2 : c2+1;
306 if (strncmp(linecmp, cg, strlen(linecmp)) != 0) {
307 if (nextcg)
308 *nextcg = get_next_cgroup_dir(linecmp, cg);
309 goto out;
310 }
311 answer = true;
312 goto out;
313 }
314
315 out:
316 fclose(f);
317 free(line);
318 return answer;
319 }
320
321 /*
322 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
323 * and needs to be nih_freed.
324 */
325 static char *pick_controller_from_path(struct fuse_context *fc, const char *path)
326 {
327 const char *p1;
328 char *ret, *slash;
329
330 if (strlen(path) < 9)
331 return NULL;
332 p1 = path+8;
333 ret = nih_strdup(NULL, p1);
334 if (!ret)
335 return ret;
336 slash = strstr(ret, "/");
337 if (slash)
338 *slash = '\0';
339
340 /* verify that it is a subsystem */
341 char **list = LXCFS_DATA ? LXCFS_DATA->subsystems : NULL;
342 int i;
343 if (!list) {
344 nih_free(ret);
345 return NULL;
346 }
347 for (i = 0; list[i]; i++) {
348 if (strcmp(list[i], ret) == 0)
349 return ret;
350 }
351 nih_free(ret);
352 return NULL;
353 }
354
355 /*
356 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
357 * Note that the returned value may include files (keynames) etc
358 */
359 static const char *find_cgroup_in_path(const char *path)
360 {
361 const char *p1;
362
363 if (strlen(path) < 9)
364 return NULL;
365 p1 = strstr(path+8, "/");
366 if (!p1)
367 return NULL;
368 return p1+1;
369 }
370
371 static bool is_child_cgroup(const char *contr, const char *dir, const char *f)
372 {
373 nih_local char **list = NULL;
374 int i;
375
376 if (!f)
377 return false;
378 if (*f == '/')
379 f++;
380
381 if (!cgm_list_children(contr, dir, &list))
382 return false;
383 for (i = 0; list[i]; i++) {
384 if (strcmp(list[i], f) == 0)
385 return true;
386 }
387
388 return false;
389 }
390
391 static struct cgm_keys *get_cgroup_key(const char *contr, const char *dir, const char *f)
392 {
393 nih_local struct cgm_keys **list = NULL;
394 struct cgm_keys *k;
395 int i;
396
397 if (!f)
398 return NULL;
399 if (*f == '/')
400 f++;
401 if (!cgm_list_keys(contr, dir, &list))
402 return NULL;
403 for (i = 0; list[i]; i++) {
404 if (strcmp(list[i]->name, f) == 0) {
405 k = NIH_MUST( nih_alloc(NULL, (sizeof(*k))) );
406 k->name = NIH_MUST( nih_strdup(k, list[i]->name) );
407 k->uid = list[i]->uid;
408 k->gid = list[i]->gid;
409 k->mode = list[i]->mode;
410 return k;
411 }
412 }
413
414 return NULL;
415 }
416
417 static void get_cgdir_and_path(const char *cg, char **dir, char **file)
418 {
419 char *p;
420
421 *dir = NIH_MUST( nih_strdup(NULL, cg) );
422 *file = strrchr(cg, '/');
423 if (!*file) {
424 *file = NULL;
425 return;
426 }
427 p = strrchr(*dir, '/');
428 *p = '\0';
429 }
430
431 static size_t get_file_size(const char *contrl, const char *cg, const char *f)
432 {
433 nih_local char *data = NULL;
434 size_t s;
435 if (!cgm_get_value(contrl, cg, f, &data))
436 return -EINVAL;
437 s = strlen(data);
438 return s;
439 }
440
441 /*
442 * FUSE ops for /cgroup
443 */
444
445 static int cg_getattr(const char *path, struct stat *sb)
446 {
447 struct timespec now;
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;
452 const char *cgroup;
453 nih_local char *controller = NULL;
454
455
456 if (!fc)
457 return -EIO;
458
459 memset(sb, 0, sizeof(struct stat));
460
461 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
462 return -EINVAL;
463
464 sb->st_uid = sb->st_gid = 0;
465 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
466 sb->st_size = 0;
467
468 if (strcmp(path, "/cgroup") == 0) {
469 sb->st_mode = S_IFDIR | 00755;
470 sb->st_nlink = 2;
471 return 0;
472 }
473
474 controller = pick_controller_from_path(fc, path);
475 if (!controller)
476 return -EIO;
477 cgroup = find_cgroup_in_path(path);
478 if (!cgroup) {
479 /* this is just /cgroup/controller, return it as a dir */
480 sb->st_mode = S_IFDIR | 00755;
481 sb->st_nlink = 2;
482 return 0;
483 }
484
485 get_cgdir_and_path(cgroup, &cgdir, &fpath);
486
487 if (!fpath) {
488 path1 = "/";
489 path2 = cgdir;
490 } else {
491 path1 = cgdir;
492 path2 = fpath;
493 }
494
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 */
498
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;
503 sb->st_nlink = 2;
504 return 0;
505 }
506 if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY))
507 return -EACCES;
508
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");
513 if (!k) {
514 sb->st_uid = sb->st_gid = 0;
515 } else {
516 sb->st_uid = k->uid;
517 sb->st_gid = k->gid;
518 }
519 sb->st_nlink = 2;
520 return 0;
521 }
522
523 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
524 if (!caller_is_in_ancestor(fc->pid, controller, path1, NULL))
525 return -ENOENT;
526 if (!fc_may_access(fc, controller, path1, path2, O_RDONLY))
527 return -EACCES;
528
529 sb->st_mode = S_IFREG | k->mode;
530 sb->st_nlink = 1;
531 sb->st_uid = k->uid;
532 sb->st_gid = k->gid;
533 sb->st_size = get_file_size(controller, path1, path2);
534 return 0;
535 }
536
537 return -ENOENT;
538 }
539
540 /*
541 * TODO - cache these results in a table for use in opendir, free
542 * in releasedir
543 */
544 static int cg_opendir(const char *path, struct fuse_file_info *fi)
545 {
546 struct fuse_context *fc = fuse_get_context();
547 nih_local struct cgm_keys **list = NULL;
548 const char *cgroup;
549 struct file_info *dir_info;
550 nih_local char *controller = NULL;
551
552 if (!fc)
553 return -EIO;
554
555 if (strcmp(path, "/cgroup") == 0) {
556 cgroup = NULL;
557 controller = NULL;
558 } else {
559 // return list of keys for the controller, and list of child cgroups
560 controller = pick_controller_from_path(fc, path);
561 if (!controller)
562 return -EIO;
563
564 cgroup = find_cgroup_in_path(path);
565 if (!cgroup) {
566 /* this is just /cgroup/controller, return its contents */
567 cgroup = "/";
568 }
569 }
570
571 if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY))
572 return -EACCES;
573
574 /* we'll free this at cg_releasedir */
575 dir_info = NIH_MUST( nih_alloc(NULL, sizeof(*dir_info)) );
576 dir_info->controller = must_copy_string(controller);
577 dir_info->cgroup = must_copy_string(cgroup);
578 dir_info->isdir = true;
579 dir_info->buf = NULL;
580 dir_info->buflen = 0;
581
582 fi->fh = (unsigned long)dir_info;
583 return 0;
584 }
585
586 static int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
587 struct fuse_file_info *fi)
588 {
589 struct file_info *d = (struct file_info *)fi->fh;
590 nih_local struct cgm_keys **list = NULL;
591 int i;
592 nih_local char *nextcg = NULL;
593 struct fuse_context *fc = fuse_get_context();
594
595 if (!d->cgroup && !d->controller) {
596 // ls /var/lib/lxcfs/cgroup - just show list of controllers
597 char **list = LXCFS_DATA ? LXCFS_DATA->subsystems : NULL;
598 int i;
599
600 if (!list)
601 return -EIO;
602
603 for (i = 0; list[i]; i++) {
604 if (filler(buf, list[i], NULL, 0) != 0) {
605 return -EIO;
606 }
607 }
608 return 0;
609 }
610
611 if (!cgm_list_keys(d->controller, d->cgroup, &list))
612 // not a valid cgroup
613 return -EINVAL;
614
615 if (!caller_is_in_ancestor(fc->pid, d->controller, d->cgroup, &nextcg)) {
616 if (nextcg) {
617 int ret;
618 ret = filler(buf, nextcg, NULL, 0);
619 if (ret != 0)
620 return -EIO;
621 }
622 return 0;
623 }
624
625 for (i = 0; list[i]; i++) {
626 if (filler(buf, list[i]->name, NULL, 0) != 0) {
627 return -EIO;
628 }
629 }
630
631 // now get the list of child cgroups
632 nih_local char **clist = NULL;
633
634 if (!cgm_list_children(d->controller, d->cgroup, &clist))
635 return 0;
636 for (i = 0; clist[i]; i++) {
637 if (filler(buf, clist[i], NULL, 0) != 0) {
638 return -EIO;
639 }
640 }
641 return 0;
642 }
643
644 static int cg_releasedir(const char *path, struct fuse_file_info *fi)
645 {
646 struct file_info *d = (struct file_info *)fi->fh;
647
648 if (d->controller)
649 nih_free(d->controller);
650 if (d->cgroup)
651 nih_free(d->cgroup);
652 free(d->buf);
653 nih_free(d);
654 return 0;
655 }
656
657 /*
658 * TODO - cache info here for read/write, release in cg_release.
659 */
660 static int cg_open(const char *path, struct fuse_file_info *fi)
661 {
662 nih_local char *controller = NULL;
663 const char *cgroup;
664 char *fpath = NULL, *path1, *path2;
665 nih_local char * cgdir = NULL;
666 nih_local struct cgm_keys *k = NULL;
667 struct fuse_context *fc = fuse_get_context();
668
669 if (!fc)
670 return -EIO;
671
672 controller = pick_controller_from_path(fc, path);
673 if (!controller)
674 return -EIO;
675 cgroup = find_cgroup_in_path(path);
676 if (!cgroup)
677 return -EINVAL;
678
679 get_cgdir_and_path(cgroup, &cgdir, &fpath);
680 if (!fpath) {
681 path1 = "/";
682 path2 = cgdir;
683 } else {
684 path1 = cgdir;
685 path2 = fpath;
686 }
687
688 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
689 if (!fc_may_access(fc, controller, path1, path2, fi->flags))
690 // should never get here
691 return -EACCES;
692
693 return 0;
694 }
695
696 return -EINVAL;
697 }
698
699 static int msgrecv(int sockfd, void *buf, size_t len)
700 {
701 struct timeval tv;
702 fd_set rfds;
703
704 FD_ZERO(&rfds);
705 FD_SET(sockfd, &rfds);
706 tv.tv_sec = 2;
707 tv.tv_usec = 0;
708
709 if (select(sockfd+1, &rfds, NULL, NULL, &tv) <= 0)
710 return -1;
711 return recv(sockfd, buf, len, MSG_DONTWAIT);
712 }
713
714 #define SEND_CREDS_OK 0
715 #define SEND_CREDS_NOTSK 1
716 #define SEND_CREDS_FAIL 2
717 static int send_creds(int sock, struct ucred *cred, char v, bool pingfirst)
718 {
719 struct msghdr msg = { 0 };
720 struct iovec iov;
721 struct cmsghdr *cmsg;
722 char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
723 char buf[1];
724 buf[0] = 'p';
725
726 if (pingfirst) {
727 if (msgrecv(sock, buf, 1) != 1) {
728 fprintf(stderr, "%s: Error getting reply from server over socketpair\n",
729 __func__);
730 return SEND_CREDS_FAIL;
731 }
732 }
733
734 msg.msg_control = cmsgbuf;
735 msg.msg_controllen = sizeof(cmsgbuf);
736
737 cmsg = CMSG_FIRSTHDR(&msg);
738 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
739 cmsg->cmsg_level = SOL_SOCKET;
740 cmsg->cmsg_type = SCM_CREDENTIALS;
741 memcpy(CMSG_DATA(cmsg), cred, sizeof(*cred));
742
743 msg.msg_name = NULL;
744 msg.msg_namelen = 0;
745
746 buf[0] = v;
747 iov.iov_base = buf;
748 iov.iov_len = sizeof(buf);
749 msg.msg_iov = &iov;
750 msg.msg_iovlen = 1;
751
752 if (sendmsg(sock, &msg, 0) < 0) {
753 fprintf(stderr, "%s: failed at sendmsg: %s\n", __func__,
754 strerror(errno));
755 if (errno == 3)
756 return SEND_CREDS_NOTSK;
757 return SEND_CREDS_FAIL;
758 }
759
760 return SEND_CREDS_OK;
761 }
762
763 static bool recv_creds(int sock, struct ucred *cred, char *v)
764 {
765 struct msghdr msg = { 0 };
766 struct iovec iov;
767 struct cmsghdr *cmsg;
768 char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
769 char buf[1];
770 int ret;
771 int optval = 1;
772 struct timeval tv;
773 fd_set rfds;
774
775 *v = '1';
776
777 cred->pid = -1;
778 cred->uid = -1;
779 cred->gid = -1;
780
781 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1) {
782 fprintf(stderr, "Failed to set passcred: %s\n", strerror(errno));
783 return false;
784 }
785 buf[0] = '1';
786 if (write(sock, buf, 1) != 1) {
787 fprintf(stderr, "Failed to start write on scm fd: %s\n", strerror(errno));
788 return false;
789 }
790
791 msg.msg_name = NULL;
792 msg.msg_namelen = 0;
793 msg.msg_control = cmsgbuf;
794 msg.msg_controllen = sizeof(cmsgbuf);
795
796 iov.iov_base = buf;
797 iov.iov_len = sizeof(buf);
798 msg.msg_iov = &iov;
799 msg.msg_iovlen = 1;
800
801 FD_ZERO(&rfds);
802 FD_SET(sock, &rfds);
803 tv.tv_sec = 2;
804 tv.tv_usec = 0;
805 if (select(sock+1, &rfds, NULL, NULL, &tv) <= 0) {
806 fprintf(stderr, "Failed to select for scm_cred: %s\n",
807 strerror(errno));
808 return false;
809 }
810 ret = recvmsg(sock, &msg, MSG_DONTWAIT);
811 if (ret < 0) {
812 fprintf(stderr, "Failed to receive scm_cred: %s\n",
813 strerror(errno));
814 return false;
815 }
816
817 cmsg = CMSG_FIRSTHDR(&msg);
818
819 if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) &&
820 cmsg->cmsg_level == SOL_SOCKET &&
821 cmsg->cmsg_type == SCM_CREDENTIALS) {
822 memcpy(cred, CMSG_DATA(cmsg), sizeof(*cred));
823 }
824 *v = buf[0];
825
826 return true;
827 }
828
829
830 /*
831 * pid_to_ns - reads pids from a ucred over a socket, then writes the
832 * int value back over the socket. This shifts the pid from the
833 * sender's pidns into tpid's pidns.
834 */
835 static void pid_to_ns(int sock, pid_t tpid)
836 {
837 char v = '0';
838 struct ucred cred;
839
840 while (recv_creds(sock, &cred, &v)) {
841 if (v == '1')
842 exit(0);
843 if (write(sock, &cred.pid, sizeof(pid_t)) != sizeof(pid_t))
844 exit(1);
845 }
846 exit(0);
847 }
848
849 /*
850 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
851 * in your old pidns. Only children which you fork will be in the target
852 * pidns. So the pid_to_ns_wrapper does the setns, then forks a child to
853 * actually convert pids
854 */
855 static void pid_to_ns_wrapper(int sock, pid_t tpid)
856 {
857 int newnsfd = -1, ret, cpipe[2];
858 char fnam[100];
859 pid_t cpid;
860 struct timeval tv;
861 fd_set s;
862 char v;
863
864 sprintf(fnam, "/proc/%d/ns/pid", tpid);
865 newnsfd = open(fnam, O_RDONLY);
866 if (newnsfd < 0)
867 exit(1);
868 if (setns(newnsfd, 0) < 0)
869 exit(1);
870 close(newnsfd);
871
872 if (pipe(cpipe) < 0)
873 exit(1);
874
875 loop:
876 cpid = fork();
877 if (cpid < 0)
878 exit(1);
879
880 if (!cpid) {
881 char b = '1';
882 close(cpipe[0]);
883 if (write(cpipe[1], &b, sizeof(char)) < 0) {
884 fprintf(stderr, "%s (child): erorr on write: %s\n",
885 __func__, strerror(errno));
886 }
887 close(cpipe[1]);
888 pid_to_ns(sock, tpid);
889 }
890 // give the child 1 second to be done forking and
891 // write it's ack
892 FD_ZERO(&s);
893 FD_SET(cpipe[0], &s);
894 tv.tv_sec = 1;
895 tv.tv_usec = 0;
896 ret = select(cpipe[0]+1, &s, NULL, NULL, &tv);
897 if (ret <= 0)
898 goto again;
899 ret = read(cpipe[0], &v, 1);
900 if (ret != sizeof(char) || v != '1') {
901 goto again;
902 }
903
904 if (!wait_for_pid(cpid))
905 exit(1);
906 exit(0);
907
908 again:
909 kill(cpid, SIGKILL);
910 wait_for_pid(cpid);
911 goto loop;
912 }
913
914 /*
915 * To read cgroup files with a particular pid, we will setns into the child
916 * pidns, open a pipe, fork a child - which will be the first to really be in
917 * the child ns - which does the cgm_get_value and writes the data to the pipe.
918 */
919 static bool do_read_pids(pid_t tpid, const char *contrl, const char *cg, const char *file, char **d)
920 {
921 int sock[2] = {-1, -1};
922 nih_local char *tmpdata = NULL;
923 int ret;
924 pid_t qpid, cpid = -1;
925 bool answer = false;
926 char v = '0';
927 struct ucred cred;
928 struct timeval tv;
929 fd_set s;
930
931 if (!cgm_get_value(contrl, cg, file, &tmpdata))
932 return false;
933
934 /*
935 * Now we read the pids from returned data one by one, pass
936 * them into a child in the target namespace, read back the
937 * translated pids, and put them into our to-return data
938 */
939
940 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0) {
941 perror("socketpair");
942 exit(1);
943 }
944
945 cpid = fork();
946 if (cpid == -1)
947 goto out;
948
949 if (!cpid) // child
950 pid_to_ns_wrapper(sock[1], tpid);
951
952 char *ptr = tmpdata;
953 cred.uid = 0;
954 cred.gid = 0;
955 while (sscanf(ptr, "%d\n", &qpid) == 1) {
956 cred.pid = qpid;
957 ret = send_creds(sock[0], &cred, v, true);
958
959 if (ret == SEND_CREDS_NOTSK)
960 goto next;
961 if (ret == SEND_CREDS_FAIL)
962 goto out;
963
964 // read converted results
965 FD_ZERO(&s);
966 FD_SET(sock[0], &s);
967 tv.tv_sec = 2;
968 tv.tv_usec = 0;
969 ret = select(sock[0]+1, &s, NULL, NULL, &tv);
970 if (ret <= 0) {
971 fprintf(stderr, "%s: select error waiting for pid from child: %s\n",
972 __func__, strerror(errno));
973 goto out;
974 }
975 if (read(sock[0], &qpid, sizeof(qpid)) != sizeof(qpid)) {
976 fprintf(stderr, "%s: error reading pid from child: %s\n",
977 __func__, strerror(errno));
978 goto out;
979 }
980 NIH_MUST( nih_strcat_sprintf(d, NULL, "%d\n", qpid) );
981 next:
982 ptr = strchr(ptr, '\n');
983 if (!ptr)
984 break;
985 ptr++;
986 }
987
988 cred.pid = getpid();
989 v = '1';
990 if (send_creds(sock[0], &cred, v, true) != SEND_CREDS_OK) {
991 // failed to ask child to exit
992 fprintf(stderr, "%s: failed to ask child to exit: %s\n",
993 __func__, strerror(errno));
994 goto out;
995 }
996
997 answer = true;
998
999 out:
1000 if (cpid != -1)
1001 wait_for_pid(cpid);
1002 if (sock[0] != -1) {
1003 close(sock[0]);
1004 close(sock[1]);
1005 }
1006 return answer;
1007 }
1008
1009 static int cg_read(const char *path, char *buf, size_t size, off_t offset,
1010 struct fuse_file_info *fi)
1011 {
1012 nih_local char *controller = NULL;
1013 const char *cgroup;
1014 char *fpath = NULL, *path1, *path2;
1015 struct fuse_context *fc = fuse_get_context();
1016 nih_local char * cgdir = NULL;
1017 nih_local struct cgm_keys *k = NULL;
1018
1019 if (offset)
1020 return -EIO;
1021
1022 if (!fc)
1023 return -EIO;
1024
1025 controller = pick_controller_from_path(fc, path);
1026 if (!controller)
1027 return -EINVAL;
1028 cgroup = find_cgroup_in_path(path);
1029 if (!cgroup)
1030 return -EINVAL;
1031
1032 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1033 if (!fpath) {
1034 path1 = "/";
1035 path2 = cgdir;
1036 } else {
1037 path1 = cgdir;
1038 path2 = fpath;
1039 }
1040
1041 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
1042 nih_local char *data = NULL;
1043 int s;
1044 bool r;
1045
1046 if (!fc_may_access(fc, controller, path1, path2, O_RDONLY))
1047 // should never get here
1048 return -EACCES;
1049
1050 if (strcmp(path2, "tasks") == 0 ||
1051 strcmp(path2, "/tasks") == 0 ||
1052 strcmp(path2, "/cgroup.procs") == 0 ||
1053 strcmp(path2, "cgroup.procs") == 0)
1054 // special case - we have to translate the pids
1055 r = do_read_pids(fc->pid, controller, path1, path2, &data);
1056 else
1057 r = cgm_get_value(controller, path1, path2, &data);
1058
1059 if (!r)
1060 return -EINVAL;
1061
1062 if (!data)
1063 return 0;
1064 s = strlen(data);
1065 if (s > size)
1066 s = size;
1067 memcpy(buf, data, s);
1068
1069 return s;
1070 }
1071
1072 return -EINVAL;
1073 }
1074
1075 static void pid_from_ns(int sock, pid_t tpid)
1076 {
1077 pid_t vpid;
1078 struct ucred cred;
1079 char v;
1080 struct timeval tv;
1081 fd_set s;
1082 int ret;
1083
1084 cred.uid = 0;
1085 cred.gid = 0;
1086 while (1) {
1087 FD_ZERO(&s);
1088 FD_SET(sock, &s);
1089 tv.tv_sec = 2;
1090 tv.tv_usec = 0;
1091 ret = select(sock+1, &s, NULL, NULL, &tv);
1092 if (ret <= 0) {
1093 fprintf(stderr, "%s: bad select before read from parent: %s\n",
1094 __func__, strerror(errno));
1095 exit(1);
1096 }
1097 if ((ret = read(sock, &vpid, sizeof(pid_t))) != sizeof(pid_t)) {
1098 fprintf(stderr, "%s: bad read from parent: %s\n",
1099 __func__, strerror(errno));
1100 exit(1);
1101 }
1102 if (vpid == -1) // done
1103 break;
1104 v = '0';
1105 cred.pid = vpid;
1106 if (send_creds(sock, &cred, v, true) != SEND_CREDS_OK) {
1107 v = '1';
1108 cred.pid = getpid();
1109 if (send_creds(sock, &cred, v, false) != SEND_CREDS_OK)
1110 exit(1);
1111 }
1112 }
1113 exit(0);
1114 }
1115
1116 static void pid_from_ns_wrapper(int sock, pid_t tpid)
1117 {
1118 int newnsfd = -1, ret, cpipe[2];
1119 char fnam[100];
1120 pid_t cpid;
1121 fd_set s;
1122 struct timeval tv;
1123 char v;
1124
1125 sprintf(fnam, "/proc/%d/ns/pid", tpid);
1126 newnsfd = open(fnam, O_RDONLY);
1127 if (newnsfd < 0)
1128 exit(1);
1129 if (setns(newnsfd, 0) < 0)
1130 exit(1);
1131 close(newnsfd);
1132
1133 if (pipe(cpipe) < 0)
1134 exit(1);
1135
1136 loop:
1137 cpid = fork();
1138
1139 if (cpid < 0)
1140 exit(1);
1141
1142 if (!cpid) {
1143 char b = '1';
1144 close(cpipe[0]);
1145 if (write(cpipe[1], &b, sizeof(char)) < 0) {
1146 fprintf(stderr, "%s (child): erorr on write: %s\n",
1147 __func__, strerror(errno));
1148 }
1149 close(cpipe[1]);
1150 pid_from_ns(sock, tpid);
1151 }
1152
1153 // give the child 1 second to be done forking and
1154 // write it's ack
1155 FD_ZERO(&s);
1156 FD_SET(cpipe[0], &s);
1157 tv.tv_sec = 1;
1158 tv.tv_usec = 0;
1159 ret = select(cpipe[0]+1, &s, NULL, NULL, &tv);
1160 if (ret <= 0)
1161 goto again;
1162 ret = read(cpipe[0], &v, 1);
1163 if (ret != sizeof(char) || v != '1') {
1164 goto again;
1165 }
1166
1167 if (!wait_for_pid(cpid))
1168 exit(1);
1169 exit(0);
1170
1171 again:
1172 kill(cpid, SIGKILL);
1173 wait_for_pid(cpid);
1174 goto loop;
1175 }
1176
1177 static bool do_write_pids(pid_t tpid, const char *contrl, const char *cg, const char *file, const char *buf)
1178 {
1179 int sock[2] = {-1, -1};
1180 pid_t qpid, cpid = -1;
1181 bool answer = false, fail = false;
1182
1183 /*
1184 * write the pids to a socket, have helper in writer's pidns
1185 * call movepid for us
1186 */
1187 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0) {
1188 perror("socketpair");
1189 exit(1);
1190 }
1191
1192 cpid = fork();
1193 if (cpid == -1)
1194 goto out;
1195
1196 if (!cpid) // child
1197 pid_from_ns_wrapper(sock[1], tpid);
1198
1199 const char *ptr = buf;
1200 while (sscanf(ptr, "%d", &qpid) == 1) {
1201 struct ucred cred;
1202 char v;
1203
1204 if (write(sock[0], &qpid, sizeof(qpid)) != sizeof(qpid)) {
1205 fprintf(stderr, "%s: error writing pid to child: %s\n",
1206 __func__, strerror(errno));
1207 goto out;
1208 }
1209
1210 if (recv_creds(sock[0], &cred, &v)) {
1211 if (v == '0') {
1212 if (!cgm_move_pid(contrl, cg, cred.pid))
1213 fail = true;
1214 }
1215 }
1216
1217 ptr = strchr(ptr, '\n');
1218 if (!ptr)
1219 break;
1220 ptr++;
1221 }
1222
1223 /* All good, write the value */
1224 qpid = -1;
1225 if (write(sock[0], &qpid ,sizeof(qpid)) != sizeof(qpid))
1226 fprintf(stderr, "Warning: failed to ask child to exit\n");
1227
1228 if (!fail)
1229 answer = true;
1230
1231 out:
1232 if (cpid != -1)
1233 wait_for_pid(cpid);
1234 if (sock[0] != -1) {
1235 close(sock[0]);
1236 close(sock[1]);
1237 }
1238 return answer;
1239 }
1240
1241 int cg_write(const char *path, const char *buf, size_t size, off_t offset,
1242 struct fuse_file_info *fi)
1243 {
1244 nih_local char *controller = NULL;
1245 const char *cgroup;
1246 char *fpath = NULL, *path1, *path2;
1247 struct fuse_context *fc = fuse_get_context();
1248 nih_local char * cgdir = NULL;
1249 nih_local struct cgm_keys *k = NULL;
1250 nih_local char *localbuf = NULL;
1251
1252 if (offset)
1253 return -EINVAL;
1254
1255 if (!fc)
1256 return -EIO;
1257
1258 localbuf = NIH_MUST( nih_alloc(NULL, size+1) );
1259 localbuf[size] = '\0';
1260 memcpy(localbuf, buf, size);
1261 controller = pick_controller_from_path(fc, path);
1262 if (!controller)
1263 return -EINVAL;
1264 cgroup = find_cgroup_in_path(path);
1265 if (!cgroup)
1266 return -EINVAL;
1267
1268 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1269 if (!fpath) {
1270 path1 = "/";
1271 path2 = cgdir;
1272 } else {
1273 path1 = cgdir;
1274 path2 = fpath;
1275 }
1276
1277 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
1278 bool r;
1279
1280 if (!fc_may_access(fc, controller, path1, path2, O_WRONLY))
1281 return -EACCES;
1282
1283 if (strcmp(path2, "tasks") == 0 ||
1284 strcmp(path2, "/tasks") == 0 ||
1285 strcmp(path2, "/cgroup.procs") == 0 ||
1286 strcmp(path2, "cgroup.procs") == 0)
1287 // special case - we have to translate the pids
1288 r = do_write_pids(fc->pid, controller, path1, path2, localbuf);
1289 else
1290 r = cgm_set_value(controller, path1, path2, localbuf);
1291
1292 if (!r)
1293 return -EINVAL;
1294
1295 return size;
1296 }
1297
1298 return -EINVAL;
1299 }
1300
1301 int cg_chown(const char *path, uid_t uid, gid_t gid)
1302 {
1303 struct fuse_context *fc = fuse_get_context();
1304 nih_local char * cgdir = NULL;
1305 char *fpath = NULL, *path1, *path2;
1306 nih_local struct cgm_keys *k = NULL;
1307 const char *cgroup;
1308 nih_local char *controller = NULL;
1309
1310
1311 if (!fc)
1312 return -EIO;
1313
1314 if (strcmp(path, "/cgroup") == 0)
1315 return -EINVAL;
1316
1317 controller = pick_controller_from_path(fc, path);
1318 if (!controller)
1319 return -EINVAL;
1320 cgroup = find_cgroup_in_path(path);
1321 if (!cgroup)
1322 /* this is just /cgroup/controller */
1323 return -EINVAL;
1324
1325 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1326
1327 if (!fpath) {
1328 path1 = "/";
1329 path2 = cgdir;
1330 } else {
1331 path1 = cgdir;
1332 path2 = fpath;
1333 }
1334
1335 if (is_child_cgroup(controller, path1, path2)) {
1336 // get uid, gid, from '/tasks' file and make up a mode
1337 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1338 k = get_cgroup_key(controller, cgroup, "tasks");
1339
1340 } else
1341 k = get_cgroup_key(controller, path1, path2);
1342
1343 if (!k)
1344 return -EINVAL;
1345
1346 /*
1347 * This being a fuse request, the uid and gid must be valid
1348 * in the caller's namespace. So we can just check to make
1349 * sure that the caller is root in his uid, and privileged
1350 * over the file's current owner.
1351 */
1352 if (!is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_REQD))
1353 return -EACCES;
1354
1355 if (!cgm_chown_file(controller, cgroup, uid, gid))
1356 return -EINVAL;
1357 return 0;
1358 }
1359
1360 int cg_chmod(const char *path, mode_t mode)
1361 {
1362 struct fuse_context *fc = fuse_get_context();
1363 nih_local char * cgdir = NULL;
1364 char *fpath = NULL, *path1, *path2;
1365 nih_local struct cgm_keys *k = NULL;
1366 const char *cgroup;
1367 nih_local char *controller = NULL;
1368
1369 if (!fc)
1370 return -EIO;
1371
1372 if (strcmp(path, "/cgroup") == 0)
1373 return -EINVAL;
1374
1375 controller = pick_controller_from_path(fc, path);
1376 if (!controller)
1377 return -EINVAL;
1378 cgroup = find_cgroup_in_path(path);
1379 if (!cgroup)
1380 /* this is just /cgroup/controller */
1381 return -EINVAL;
1382
1383 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1384
1385 if (!fpath) {
1386 path1 = "/";
1387 path2 = cgdir;
1388 } else {
1389 path1 = cgdir;
1390 path2 = fpath;
1391 }
1392
1393 if (is_child_cgroup(controller, path1, path2)) {
1394 // get uid, gid, from '/tasks' file and make up a mode
1395 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1396 k = get_cgroup_key(controller, cgroup, "tasks");
1397
1398 } else
1399 k = get_cgroup_key(controller, path1, path2);
1400
1401 if (!k)
1402 return -EINVAL;
1403
1404 /*
1405 * This being a fuse request, the uid and gid must be valid
1406 * in the caller's namespace. So we can just check to make
1407 * sure that the caller is root in his uid, and privileged
1408 * over the file's current owner.
1409 */
1410 if (!is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_OPT))
1411 return -EPERM;
1412
1413 if (!cgm_chmod_file(controller, cgroup, mode))
1414 return -EINVAL;
1415 return 0;
1416 }
1417
1418 int cg_mkdir(const char *path, mode_t mode)
1419 {
1420 struct fuse_context *fc = fuse_get_context();
1421 nih_local struct cgm_keys **list = NULL;
1422 char *fpath = NULL, *path1;
1423 nih_local char * cgdir = NULL;
1424 const char *cgroup;
1425 nih_local char *controller = NULL;
1426
1427 if (!fc)
1428 return -EIO;
1429
1430
1431 controller = pick_controller_from_path(fc, path);
1432 if (!controller)
1433 return -EINVAL;
1434
1435 cgroup = find_cgroup_in_path(path);
1436 if (!cgroup)
1437 return -EINVAL;
1438
1439 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1440 if (!fpath)
1441 path1 = "/";
1442 else
1443 path1 = cgdir;
1444
1445 if (!fc_may_access(fc, controller, path1, NULL, O_RDWR))
1446 return -EACCES;
1447
1448
1449 if (!cgm_create(controller, cgroup, fc->uid, fc->gid))
1450 return -EINVAL;
1451
1452 return 0;
1453 }
1454
1455 static int cg_rmdir(const char *path)
1456 {
1457 struct fuse_context *fc = fuse_get_context();
1458 nih_local struct cgm_keys **list = NULL;
1459 char *fpath = NULL;
1460 nih_local char * cgdir = NULL;
1461 const char *cgroup;
1462 nih_local char *controller = NULL;
1463
1464 if (!fc)
1465 return -EIO;
1466
1467
1468 controller = pick_controller_from_path(fc, path);
1469 if (!controller)
1470 return -EINVAL;
1471
1472 cgroup = find_cgroup_in_path(path);
1473 if (!cgroup)
1474 return -EINVAL;
1475
1476 get_cgdir_and_path(cgroup, &cgdir, &fpath);
1477 if (!fpath)
1478 return -EINVAL;
1479
1480 if (!fc_may_access(fc, controller, cgdir, NULL, O_WRONLY))
1481 return -EACCES;
1482
1483 if (!cgm_remove(controller, cgroup))
1484 return -EINVAL;
1485
1486 return 0;
1487 }
1488
1489 static bool startswith(const char *line, const char *pref)
1490 {
1491 if (strncmp(line, pref, strlen(pref)) == 0)
1492 return true;
1493 return false;
1494 }
1495
1496 static void get_mem_cached(char *memstat, unsigned long *v)
1497 {
1498 char *eol;
1499
1500 *v = 0;
1501 while (*memstat) {
1502 if (startswith(memstat, "total_cache")) {
1503 sscanf(memstat + 11, "%lu", v);
1504 *v /= 1024;
1505 return;
1506 }
1507 eol = strchr(memstat, '\n');
1508 if (!eol)
1509 return;
1510 memstat = eol+1;
1511 }
1512 }
1513
1514 static void get_blkio_io_value(char *str, unsigned major, unsigned minor, char *iotype, unsigned long *v)
1515 {
1516 char *eol;
1517 char key[32];
1518
1519 memset(key, 0, 32);
1520 snprintf(key, 32, "%u:%u %s", major, minor, iotype);
1521
1522 size_t len = strlen(key);
1523 *v = 0;
1524
1525 while (*str) {
1526 if (startswith(str, key)) {
1527 sscanf(str + len, "%lu", v);
1528 return;
1529 }
1530 eol = strchr(str, '\n');
1531 if (!eol)
1532 return;
1533 str = eol+1;
1534 }
1535 }
1536
1537 static char *get_pid_cgroup(pid_t pid, const char *contrl)
1538 {
1539 nih_local char *fnam = NULL;
1540 FILE *f;
1541 char *answer = NULL;
1542 char *line = NULL;
1543 size_t len = 0;
1544
1545 fnam = NIH_MUST( nih_sprintf(NULL, "/proc/%d/cgroup", pid) );
1546 if (!(f = fopen(fnam, "r")))
1547 return false;
1548
1549 while (getline(&line, &len, f) != -1) {
1550 char *c1, *c2;
1551 if (!line[0])
1552 continue;
1553 c1 = strchr(line, ':');
1554 if (!c1)
1555 goto out;
1556 c1++;
1557 c2 = strchr(c1, ':');
1558 if (!c2)
1559 goto out;
1560 *c2 = '\0';
1561 if (strcmp(c1, contrl) != 0)
1562 continue;
1563 c2++;
1564 stripnewline(c2);
1565 answer = NIH_MUST( nih_strdup(NULL, c2) );
1566 goto out;
1567 }
1568
1569 out:
1570 fclose(f);
1571 free(line);
1572 return answer;
1573 }
1574
1575 /*
1576 * FUSE ops for /proc
1577 */
1578
1579 static int proc_meminfo_read(char *buf, size_t size, off_t offset,
1580 struct fuse_file_info *fi)
1581 {
1582 struct fuse_context *fc = fuse_get_context();
1583 nih_local char *cg = get_pid_cgroup(fc->pid, "memory");
1584 nih_local char *memlimit_str = NULL, *memusage_str = NULL, *memstat_str = NULL;
1585 unsigned long memlimit = 0, memusage = 0, cached = 0, hosttotal = 0;
1586 char *line = NULL;
1587 size_t linelen = 0, total_len = 0;
1588 FILE *f;
1589
1590 if (offset)
1591 return -EINVAL;
1592
1593 if (!cg)
1594 return 0;
1595
1596 if (!cgm_get_value("memory", cg, "memory.limit_in_bytes", &memlimit_str))
1597 return 0;
1598 if (!cgm_get_value("memory", cg, "memory.usage_in_bytes", &memusage_str))
1599 return 0;
1600 if (!cgm_get_value("memory", cg, "memory.stat", &memstat_str))
1601 return 0;
1602 memlimit = strtoul(memlimit_str, NULL, 10);
1603 memusage = strtoul(memusage_str, NULL, 10);
1604 memlimit /= 1024;
1605 memusage /= 1024;
1606 get_mem_cached(memstat_str, &cached);
1607
1608 f = fopen("/proc/meminfo", "r");
1609 if (!f)
1610 return 0;
1611
1612 while (getline(&line, &linelen, f) != -1) {
1613 size_t l;
1614 char *printme, lbuf[100];
1615
1616 memset(lbuf, 0, 100);
1617 if (startswith(line, "MemTotal:")) {
1618 sscanf(line+14, "%lu", &hosttotal);
1619 if (hosttotal < memlimit)
1620 memlimit = hosttotal;
1621 snprintf(lbuf, 100, "MemTotal: %8lu kB\n", memlimit);
1622 printme = lbuf;
1623 } else if (startswith(line, "MemFree:")) {
1624 snprintf(lbuf, 100, "MemFree: %8lu kB\n", memlimit - memusage);
1625 printme = lbuf;
1626 } else if (startswith(line, "MemAvailable:")) {
1627 snprintf(lbuf, 100, "MemAvailable: %8lu kB\n", memlimit - memusage);
1628 printme = lbuf;
1629 } else if (startswith(line, "Buffers:")) {
1630 snprintf(lbuf, 100, "Buffers: %8lu kB\n", 0UL);
1631 printme = lbuf;
1632 } else if (startswith(line, "Cached:")) {
1633 snprintf(lbuf, 100, "Cached: %8lu kB\n", cached);
1634 printme = lbuf;
1635 } else if (startswith(line, "SwapCached:")) {
1636 snprintf(lbuf, 100, "SwapCached: %8lu kB\n", 0UL);
1637 printme = lbuf;
1638 } else
1639 printme = line;
1640 l = snprintf(buf, size, "%s", printme);
1641 buf += l;
1642 size -= l;
1643 total_len += l;
1644 }
1645
1646 fclose(f);
1647 free(line);
1648 return total_len;
1649 }
1650
1651 /*
1652 * Read the cpuset.cpus for cg
1653 * Return the answer in a nih_alloced string
1654 */
1655 static char *get_cpuset(const char *cg)
1656 {
1657 char *answer;
1658
1659 if (!cgm_get_value("cpuset", cg, "cpuset.cpus", &answer))
1660 return NULL;
1661 return answer;
1662 }
1663
1664 /*
1665 * Helper functions for cpuset_in-set
1666 */
1667 char *cpuset_nexttok(const char *c)
1668 {
1669 char *r = strchr(c+1, ',');
1670 if (r)
1671 return r+1;
1672 return NULL;
1673 }
1674
1675 int cpuset_getrange(const char *c, int *a, int *b)
1676 {
1677 int ret;
1678
1679 ret = sscanf(c, "%d-%d", a, b);
1680 return ret;
1681 }
1682
1683 /*
1684 * cpusets are in format "1,2-3,4"
1685 * iow, comma-delimited ranges
1686 */
1687 static bool cpu_in_cpuset(int cpu, const char *cpuset)
1688 {
1689 const char *c;
1690
1691 for (c = cpuset; c; c = cpuset_nexttok(c)) {
1692 int a, b, ret;
1693
1694 ret = cpuset_getrange(c, &a, &b);
1695 if (ret == 1 && cpu == a)
1696 return true;
1697 if (ret != 2) // bad cpuset!
1698 return false;
1699 if (cpu >= a && cpu <= b)
1700 return true;
1701 }
1702
1703 return false;
1704 }
1705
1706 static bool cpuline_in_cpuset(const char *line, const char *cpuset)
1707 {
1708 int cpu;
1709
1710 if (sscanf(line, "processor : %d", &cpu) != 1)
1711 return false;
1712 return cpu_in_cpuset(cpu, cpuset);
1713 }
1714
1715 /*
1716 * check whether this is a '^processor" line in /proc/cpuinfo
1717 */
1718 static bool is_processor_line(const char *line)
1719 {
1720 int cpu;
1721
1722 if (sscanf(line, "processor : %d", &cpu) == 1)
1723 return true;
1724 return false;
1725 }
1726
1727 static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
1728 struct fuse_file_info *fi)
1729 {
1730 struct fuse_context *fc = fuse_get_context();
1731 nih_local char *cg = get_pid_cgroup(fc->pid, "cpuset");
1732 nih_local char *cpuset = NULL;
1733 char *line = NULL;
1734 size_t linelen = 0, total_len = 0;
1735 bool am_printing = false;
1736 int curcpu = -1;
1737 FILE *f;
1738
1739 if (offset)
1740 return -EINVAL;
1741
1742 if (!cg)
1743 return 0;
1744
1745 cpuset = get_cpuset(cg);
1746 if (!cpuset)
1747 return 0;
1748
1749 f = fopen("/proc/cpuinfo", "r");
1750 if (!f)
1751 return 0;
1752
1753 while (getline(&line, &linelen, f) != -1) {
1754 size_t l;
1755 if (is_processor_line(line)) {
1756 am_printing = cpuline_in_cpuset(line, cpuset);
1757 if (am_printing) {
1758 curcpu ++;
1759 l = snprintf(buf, size, "processor : %d\n", curcpu);
1760 buf += l;
1761 size -= l;
1762 total_len += l;
1763 }
1764 continue;
1765 }
1766 if (am_printing) {
1767 l = snprintf(buf, size, "%s", line);
1768 buf += l;
1769 size -= l;
1770 total_len += l;
1771 }
1772 }
1773
1774 fclose(f);
1775 free(line);
1776 return total_len;
1777 }
1778
1779 static int proc_stat_read(char *buf, size_t size, off_t offset,
1780 struct fuse_file_info *fi)
1781 {
1782 struct fuse_context *fc = fuse_get_context();
1783 nih_local char *cg = get_pid_cgroup(fc->pid, "cpuset");
1784 nih_local char *cpuset = NULL;
1785 char *line = NULL;
1786 size_t linelen = 0, total_len = 0;
1787 int curcpu = -1; /* cpu numbering starts at 0 */
1788 FILE *f;
1789
1790 if (offset)
1791 return -EINVAL;
1792
1793 if (!cg)
1794 return 0;
1795
1796 cpuset = get_cpuset(cg);
1797 if (!cpuset)
1798 return 0;
1799
1800 f = fopen("/proc/stat", "r");
1801 if (!f)
1802 return 0;
1803
1804 while (getline(&line, &linelen, f) != -1) {
1805 size_t l;
1806 int cpu;
1807 char cpu_char[10]; /* That's a lot of cores */
1808 char *c;
1809
1810 if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) {
1811 /* not a ^cpuN line containing a number N, just print it */
1812 l = snprintf(buf, size, "%s", line);
1813 buf += l;
1814 size -= l;
1815 total_len += l;
1816 continue;
1817 }
1818
1819 if (sscanf(cpu_char, "%d", &cpu) != 1)
1820 continue;
1821 if (!cpu_in_cpuset(cpu, cpuset))
1822 continue;
1823 curcpu ++;
1824
1825 c = strchr(line, ' ');
1826 if (!c)
1827 continue;
1828 l = snprintf(buf, size, "cpu%d %s", curcpu, c);
1829 buf += l;
1830 size -= l;
1831 total_len += l;
1832 }
1833
1834 fclose(f);
1835 free(line);
1836 return total_len;
1837 }
1838
1839 /*
1840 * How to guess what to present for uptime?
1841 * One thing we could do would be to take the date on the caller's
1842 * memory.usage_in_bytes file, which should equal the time of creation
1843 * of his cgroup. However, a task could be in a sub-cgroup of the
1844 * container. The same problem exists if we try to look at the ages
1845 * of processes in the caller's cgroup.
1846 *
1847 * So we'll fork a task that will enter the caller's pidns, mount a
1848 * fresh procfs, get the age of /proc/1, and pass that back over a pipe.
1849 *
1850 * For the second uptime #, we'll do as Stéphane had done, just copy
1851 * the number from /proc/uptime. Not sure how to best emulate 'idle'
1852 * time. Maybe someone can come up with a good algorithm and submit a
1853 * patch. Maybe something based on cpushare info?
1854 */
1855
1856 /* return age of the reaper for $pid, taken from ctime of its procdir */
1857 static long int get_pid1_time(pid_t pid)
1858 {
1859 char fnam[100];
1860 int fd, cpipe[2], ret;
1861 struct stat sb;
1862 pid_t cpid;
1863 struct timeval tv;
1864 fd_set s;
1865 char v;
1866
1867 if (unshare(CLONE_NEWNS))
1868 return 0;
1869
1870 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
1871 perror("rslave mount failed");
1872 return 0;
1873 }
1874
1875 sprintf(fnam, "/proc/%d/ns/pid", pid);
1876 fd = open(fnam, O_RDONLY);
1877 if (fd < 0) {
1878 perror("get_pid1_time open of ns/pid");
1879 return 0;
1880 }
1881 if (setns(fd, 0)) {
1882 perror("get_pid1_time setns 1");
1883 close(fd);
1884 return 0;
1885 }
1886 close(fd);
1887
1888 if (pipe(cpipe) < 0)
1889 exit(1);
1890
1891 loop:
1892 cpid = fork();
1893 if (cpid < 0)
1894 return 0;
1895
1896 if (!cpid) {
1897 char b = '1';
1898 close(cpipe[0]);
1899 if (write(cpipe[1], &b, sizeof(char)) < 0) {
1900 fprintf(stderr, "%s (child): erorr on write: %s\n",
1901 __func__, strerror(errno));
1902 }
1903 close(cpipe[1]);
1904 umount2("/proc", MNT_DETACH);
1905 if (mount("proc", "/proc", "proc", 0, NULL)) {
1906 perror("get_pid1_time mount");
1907 return 0;
1908 }
1909 ret = lstat("/proc/1", &sb);
1910 if (ret) {
1911 perror("get_pid1_time lstat");
1912 return 0;
1913 }
1914 return time(NULL) - sb.st_ctime;
1915 }
1916
1917 // give the child 1 second to be done forking and
1918 // write it's ack
1919 FD_ZERO(&s);
1920 FD_SET(cpipe[0], &s);
1921 tv.tv_sec = 1;
1922 tv.tv_usec = 0;
1923 ret = select(cpipe[0]+1, &s, NULL, NULL, &tv);
1924 if (ret <= 0)
1925 goto again;
1926 ret = read(cpipe[0], &v, 1);
1927 if (ret != sizeof(char) || v != '1') {
1928 goto again;
1929 }
1930
1931 wait_for_pid(cpid);
1932 exit(0);
1933
1934 again:
1935 kill(cpid, SIGKILL);
1936 wait_for_pid(cpid);
1937 goto loop;
1938 }
1939
1940 static long int getreaperage(pid_t qpid)
1941 {
1942 int pid, mypipe[2], ret;
1943 struct timeval tv;
1944 fd_set s;
1945 long int mtime, answer = 0;
1946
1947 if (pipe(mypipe)) {
1948 return 0;
1949 }
1950
1951 pid = fork();
1952
1953 if (!pid) { // child
1954 mtime = get_pid1_time(qpid);
1955 if (write(mypipe[1], &mtime, sizeof(mtime)) != sizeof(mtime))
1956 fprintf(stderr, "Warning: bad write from getreaperage\n");
1957 exit(0);
1958 }
1959
1960 close(mypipe[1]);
1961 FD_ZERO(&s);
1962 FD_SET(mypipe[0], &s);
1963 tv.tv_sec = 1;
1964 tv.tv_usec = 0;
1965 ret = select(mypipe[0]+1, &s, NULL, NULL, &tv);
1966 if (ret <= 0) {
1967 perror("select");
1968 goto out;
1969 }
1970 if (!ret) {
1971 fprintf(stderr, "timed out\n");
1972 goto out;
1973 }
1974 if (read(mypipe[0], &mtime, sizeof(mtime)) != sizeof(mtime)) {
1975 perror("read");
1976 goto out;
1977 }
1978 answer = mtime;
1979
1980 out:
1981 wait_for_pid(pid);
1982 close(mypipe[0]);
1983 return answer;
1984 }
1985
1986 static long int getprocidle(void)
1987 {
1988 FILE *f = fopen("/proc/uptime", "r");
1989 long int age, idle;
1990 int ret;
1991 if (!f)
1992 return 0;
1993 ret = fscanf(f, "%ld %ld", &age, &idle);
1994 fclose(f);
1995 if (ret != 2)
1996 return 0;
1997 return idle;
1998 }
1999
2000 /*
2001 * We read /proc/uptime and reuse its second field.
2002 * For the first field, we use the mtime for the reaper for
2003 * the calling pid as returned by getreaperage
2004 */
2005 static int proc_uptime_read(char *buf, size_t size, off_t offset,
2006 struct fuse_file_info *fi)
2007 {
2008 struct fuse_context *fc = fuse_get_context();
2009 long int reaperage = getreaperage(fc->pid);;
2010 long int idletime = getprocidle();
2011
2012 if (offset)
2013 return -EINVAL;
2014 return snprintf(buf, size, "%ld %ld\n", reaperage, idletime);
2015 }
2016
2017 static int proc_diskstats_read(char *buf, size_t size, off_t offset,
2018 struct fuse_file_info *fi)
2019 {
2020 char dev_name[72];
2021 struct fuse_context *fc = fuse_get_context();
2022 nih_local char *cg = get_pid_cgroup(fc->pid, "blkio");
2023 nih_local char *io_serviced_str = NULL, *io_merged_str = NULL, *io_service_bytes_str = NULL,
2024 *io_wait_time_str = NULL, *io_service_time_str = NULL;
2025 unsigned long read = 0, write = 0;
2026 unsigned long read_merged = 0, write_merged = 0;
2027 unsigned long read_sectors = 0, write_sectors = 0;
2028 unsigned long read_ticks = 0, write_ticks = 0;
2029 unsigned long ios_pgr = 0, tot_ticks = 0, rq_ticks = 0;
2030 unsigned long rd_svctm = 0, wr_svctm = 0, rd_wait = 0, wr_wait = 0;
2031 char *line = NULL;
2032 size_t linelen = 0, total_len = 0;
2033 unsigned int major = 0, minor = 0;
2034 int i = 0;
2035 FILE *f;
2036
2037 if (offset)
2038 return -EINVAL;
2039
2040 if (!cg)
2041 return 0;
2042
2043 if (!cgm_get_value("blkio", cg, "blkio.io_serviced", &io_serviced_str))
2044 return 0;
2045 if (!cgm_get_value("blkio", cg, "blkio.io_merged", &io_merged_str))
2046 return 0;
2047 if (!cgm_get_value("blkio", cg, "blkio.io_service_bytes", &io_service_bytes_str))
2048 return 0;
2049 if (!cgm_get_value("blkio", cg, "blkio.io_wait_time", &io_wait_time_str))
2050 return 0;
2051 if (!cgm_get_value("blkio", cg, "blkio.io_service_time", &io_service_time_str))
2052 return 0;
2053
2054
2055 f = fopen("/proc/diskstats", "r");
2056 if (!f)
2057 return 0;
2058
2059 while (getline(&line, &linelen, f) != -1) {
2060 size_t l;
2061 char *printme, lbuf[256];
2062
2063 i = sscanf(line, "%u %u %s", &major, &minor, dev_name);
2064 if(i == 3){
2065 get_blkio_io_value(io_serviced_str, major, minor, "Read", &read);
2066 get_blkio_io_value(io_serviced_str, major, minor, "Write", &write);
2067 get_blkio_io_value(io_merged_str, major, minor, "Read", &read_merged);
2068 get_blkio_io_value(io_merged_str, major, minor, "Write", &write_merged);
2069 get_blkio_io_value(io_service_bytes_str, major, minor, "Read", &read_sectors);
2070 read_sectors = read_sectors/512;
2071 get_blkio_io_value(io_service_bytes_str, major, minor, "Write", &write_sectors);
2072 write_sectors = write_sectors/512;
2073
2074 get_blkio_io_value(io_service_time_str, major, minor, "Read", &rd_svctm);
2075 rd_svctm = rd_svctm/1000000;
2076 get_blkio_io_value(io_wait_time_str, major, minor, "Read", &rd_wait);
2077 rd_wait = rd_wait/1000000;
2078 read_ticks = rd_svctm + rd_wait;
2079
2080 get_blkio_io_value(io_service_time_str, major, minor, "Write", &wr_svctm);
2081 wr_svctm = wr_svctm/1000000;
2082 get_blkio_io_value(io_wait_time_str, major, minor, "Write", &wr_wait);
2083 wr_wait = wr_wait/1000000;
2084 write_ticks = wr_svctm + wr_wait;
2085
2086 get_blkio_io_value(io_service_time_str, major, minor, "Total", &tot_ticks);
2087 tot_ticks = tot_ticks/1000000;
2088 }else{
2089 continue;
2090 }
2091
2092 memset(lbuf, 0, 256);
2093 if (read || write || read_merged || write_merged || read_sectors || write_sectors || read_ticks || write_ticks) {
2094 snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
2095 major, minor, dev_name, read, read_merged, read_sectors, read_ticks,
2096 write, write_merged, write_sectors, write_ticks, ios_pgr, tot_ticks, rq_ticks);
2097 printme = lbuf;
2098 } else
2099 continue;
2100
2101 l = snprintf(buf, size, "%s", printme);
2102 buf += l;
2103 size -= l;
2104 total_len += l;
2105 }
2106
2107 fclose(f);
2108 free(line);
2109 return total_len;
2110 }
2111
2112 static off_t get_procfile_size(const char *which)
2113 {
2114 FILE *f = fopen(which, "r");
2115 char *line = NULL;
2116 size_t len = 0;
2117 ssize_t sz, answer = 0;
2118 if (!f)
2119 return 0;
2120
2121 while ((sz = getline(&line, &len, f)) != -1)
2122 answer += sz;
2123 fclose (f);
2124 free(line);
2125
2126 return answer;
2127 }
2128
2129 static int proc_getattr(const char *path, struct stat *sb)
2130 {
2131 struct timespec now;
2132
2133 memset(sb, 0, sizeof(struct stat));
2134 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
2135 return -EINVAL;
2136 sb->st_uid = sb->st_gid = 0;
2137 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
2138 if (strcmp(path, "/proc") == 0) {
2139 sb->st_mode = S_IFDIR | 00555;
2140 sb->st_nlink = 2;
2141 return 0;
2142 }
2143 if (strcmp(path, "/proc/meminfo") == 0 ||
2144 strcmp(path, "/proc/cpuinfo") == 0 ||
2145 strcmp(path, "/proc/uptime") == 0 ||
2146 strcmp(path, "/proc/stat") == 0 ||
2147 strcmp(path, "/proc/diskstats") == 0) {
2148 sb->st_size = get_procfile_size(path);
2149 sb->st_mode = S_IFREG | 00444;
2150 sb->st_nlink = 1;
2151 return 0;
2152 }
2153
2154 return -ENOENT;
2155 }
2156
2157 static int proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
2158 struct fuse_file_info *fi)
2159 {
2160 if (filler(buf, "cpuinfo", NULL, 0) != 0 ||
2161 filler(buf, "meminfo", NULL, 0) != 0 ||
2162 filler(buf, "stat", NULL, 0) != 0 ||
2163 filler(buf, "uptime", NULL, 0) != 0 ||
2164 filler(buf, "diskstats", NULL, 0) != 0)
2165 return -EINVAL;
2166 return 0;
2167 }
2168
2169 static int proc_open(const char *path, struct fuse_file_info *fi)
2170 {
2171 if (strcmp(path, "/proc/meminfo") == 0 ||
2172 strcmp(path, "/proc/cpuinfo") == 0 ||
2173 strcmp(path, "/proc/uptime") == 0 ||
2174 strcmp(path, "/proc/stat") == 0 ||
2175 strcmp(path, "/proc/diskstats") == 0)
2176 return 0;
2177 return -ENOENT;
2178 }
2179
2180 static int proc_read(const char *path, char *buf, size_t size, off_t offset,
2181 struct fuse_file_info *fi)
2182 {
2183 if (strcmp(path, "/proc/meminfo") == 0)
2184 return proc_meminfo_read(buf, size, offset, fi);
2185 if (strcmp(path, "/proc/cpuinfo") == 0)
2186 return proc_cpuinfo_read(buf, size, offset, fi);
2187 if (strcmp(path, "/proc/uptime") == 0)
2188 return proc_uptime_read(buf, size, offset, fi);
2189 if (strcmp(path, "/proc/stat") == 0)
2190 return proc_stat_read(buf, size, offset, fi);
2191 if (strcmp(path, "/proc/diskstats") == 0)
2192 return proc_diskstats_read(buf, size, offset, fi);
2193 return -EINVAL;
2194 }
2195
2196 /*
2197 * FUSE ops for /
2198 * these just delegate to the /proc and /cgroup ops as
2199 * needed
2200 */
2201
2202 static int lxcfs_getattr(const char *path, struct stat *sb)
2203 {
2204 if (strcmp(path, "/") == 0) {
2205 sb->st_mode = S_IFDIR | 00755;
2206 sb->st_nlink = 2;
2207 return 0;
2208 }
2209 if (strncmp(path, "/cgroup", 7) == 0) {
2210 return cg_getattr(path, sb);
2211 }
2212 if (strncmp(path, "/proc", 5) == 0) {
2213 return proc_getattr(path, sb);
2214 }
2215 return -EINVAL;
2216 }
2217
2218 static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
2219 {
2220 if (strcmp(path, "/") == 0)
2221 return 0;
2222
2223 if (strncmp(path, "/cgroup", 7) == 0) {
2224 return cg_opendir(path, fi);
2225 }
2226 if (strcmp(path, "/proc") == 0)
2227 return 0;
2228 return -ENOENT;
2229 }
2230
2231 static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
2232 struct fuse_file_info *fi)
2233 {
2234 if (strcmp(path, "/") == 0) {
2235 if (filler(buf, "proc", NULL, 0) != 0 ||
2236 filler(buf, "cgroup", NULL, 0) != 0)
2237 return -EINVAL;
2238 return 0;
2239 }
2240 if (strncmp(path, "/cgroup", 7) == 0)
2241 return cg_readdir(path, buf, filler, offset, fi);
2242 if (strcmp(path, "/proc") == 0)
2243 return proc_readdir(path, buf, filler, offset, fi);
2244 return -EINVAL;
2245 }
2246
2247 static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
2248 {
2249 if (strcmp(path, "/") == 0)
2250 return 0;
2251 if (strncmp(path, "/cgroup", 7) == 0) {
2252 return cg_releasedir(path, fi);
2253 }
2254 if (strcmp(path, "/proc") == 0)
2255 return 0;
2256 return -EINVAL;
2257 }
2258
2259 static int lxcfs_open(const char *path, struct fuse_file_info *fi)
2260 {
2261 if (strncmp(path, "/cgroup", 7) == 0)
2262 return cg_open(path, fi);
2263 if (strncmp(path, "/proc", 5) == 0)
2264 return proc_open(path, fi);
2265
2266 return -EINVAL;
2267 }
2268
2269 static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
2270 struct fuse_file_info *fi)
2271 {
2272 if (strncmp(path, "/cgroup", 7) == 0)
2273 return cg_read(path, buf, size, offset, fi);
2274 if (strncmp(path, "/proc", 5) == 0)
2275 return proc_read(path, buf, size, offset, fi);
2276
2277 return -EINVAL;
2278 }
2279
2280 int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
2281 struct fuse_file_info *fi)
2282 {
2283 if (strncmp(path, "/cgroup", 7) == 0) {
2284 return cg_write(path, buf, size, offset, fi);
2285 }
2286
2287 return -EINVAL;
2288 }
2289
2290 static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
2291 {
2292 return 0;
2293 }
2294
2295 static int lxcfs_release(const char *path, struct fuse_file_info *fi)
2296 {
2297 return 0;
2298 }
2299
2300 static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
2301 {
2302 return 0;
2303 }
2304
2305 int lxcfs_mkdir(const char *path, mode_t mode)
2306 {
2307 if (strncmp(path, "/cgroup", 7) == 0)
2308 return cg_mkdir(path, mode);
2309
2310 return -EINVAL;
2311 }
2312
2313 int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
2314 {
2315 if (strncmp(path, "/cgroup", 7) == 0)
2316 return cg_chown(path, uid, gid);
2317
2318 return -EINVAL;
2319 }
2320
2321 /*
2322 * cat first does a truncate before doing ops->write. This doesn't
2323 * really make sense for cgroups. So just return 0 always but do
2324 * nothing.
2325 */
2326 int lxcfs_truncate(const char *path, off_t newsize)
2327 {
2328 if (strncmp(path, "/cgroup", 7) == 0)
2329 return 0;
2330 return -EINVAL;
2331 }
2332
2333 int lxcfs_rmdir(const char *path)
2334 {
2335 if (strncmp(path, "/cgroup", 7) == 0)
2336 return cg_rmdir(path);
2337 return -EINVAL;
2338 }
2339
2340 int lxcfs_chmod(const char *path, mode_t mode)
2341 {
2342 if (strncmp(path, "/cgroup", 7) == 0)
2343 return cg_chmod(path, mode);
2344 return -EINVAL;
2345 }
2346
2347 const struct fuse_operations lxcfs_ops = {
2348 .getattr = lxcfs_getattr,
2349 .readlink = NULL,
2350 .getdir = NULL,
2351 .mknod = NULL,
2352 .mkdir = lxcfs_mkdir,
2353 .unlink = NULL,
2354 .rmdir = lxcfs_rmdir,
2355 .symlink = NULL,
2356 .rename = NULL,
2357 .link = NULL,
2358 .chmod = lxcfs_chmod,
2359 .chown = lxcfs_chown,
2360 .truncate = lxcfs_truncate,
2361 .utime = NULL,
2362
2363 .open = lxcfs_open,
2364 .read = lxcfs_read,
2365 .release = lxcfs_release,
2366 .write = lxcfs_write,
2367
2368 .statfs = NULL,
2369 .flush = lxcfs_flush,
2370 .fsync = lxcfs_fsync,
2371
2372 .setxattr = NULL,
2373 .getxattr = NULL,
2374 .listxattr = NULL,
2375 .removexattr = NULL,
2376
2377 .opendir = lxcfs_opendir,
2378 .readdir = lxcfs_readdir,
2379 .releasedir = lxcfs_releasedir,
2380
2381 .fsyncdir = NULL,
2382 .init = NULL,
2383 .destroy = NULL,
2384 .access = NULL,
2385 .create = NULL,
2386 .ftruncate = NULL,
2387 .fgetattr = NULL,
2388 };
2389
2390 static void usage(const char *me)
2391 {
2392 fprintf(stderr, "Usage:\n");
2393 fprintf(stderr, "\n");
2394 fprintf(stderr, "%s [FUSE and mount options] mountpoint\n", me);
2395 exit(1);
2396 }
2397
2398 static bool is_help(char *w)
2399 {
2400 if (strcmp(w, "-h") == 0 ||
2401 strcmp(w, "--help") == 0 ||
2402 strcmp(w, "-help") == 0 ||
2403 strcmp(w, "help") == 0)
2404 return true;
2405 return false;
2406 }
2407
2408 int main(int argc, char *argv[])
2409 {
2410 int ret;
2411 struct lxcfs_state *d;
2412
2413 if (argc < 2 || is_help(argv[1]))
2414 usage(argv[0]);
2415
2416 d = malloc(sizeof(*d));
2417 if (!d)
2418 return -1;
2419
2420 if (!cgm_escape_cgroup())
2421 fprintf(stderr, "WARNING: failed to escape to root cgroup\n");
2422
2423 if (!cgm_get_controllers(&d->subsystems))
2424 return -1;
2425
2426 ret = fuse_main(argc, argv, &lxcfs_ops, d);
2427
2428 return ret;
2429 }