]> git.proxmox.com Git - mirror_lxcfs.git/blame - lxcfs.c
fix some error codes
[mirror_lxcfs.git] / lxcfs.c
CommitLineData
758ad80c
SH
1/* lxcfs
2 *
3 * Copyright © 2014 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
5 *
f2799430 6 * See COPYING file for details.
758ad80c
SH
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
2183082c 15#include <stdio.h>
758ad80c
SH
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
27#include <nih/alloc.h>
28#include <nih/string.h>
29
30#include "cgmanager.h"
31
32struct lxcfs_state {
33 /*
34 * a null-terminated, nih-allocated list of the mounted subsystems. We
35 * detect this at startup.
36 */
37 char **subsystems;
38};
39#define LXCFS_DATA ((struct lxcfs_state *) fuse_get_context()->private_data)
40
053a659d
SH
41/*
42 * Given a open file * to /proc/pid/{u,g}id_map, and an id
43 * valid in the caller's namespace, return the id mapped into
44 * pid's namespace.
45 * Returns the mapped id, or -1 on error.
46 */
47unsigned int
48convert_id_to_ns(FILE *idfile, unsigned int in_id)
49{
50 unsigned int nsuid, // base id for a range in the idfile's namespace
51 hostuid, // base id for a range in the caller's namespace
52 count; // number of ids in this range
53 char line[400];
54 int ret;
55
56 fseek(idfile, 0L, SEEK_SET);
57 while (fgets(line, 400, idfile)) {
58 ret = sscanf(line, "%u %u %u\n", &nsuid, &hostuid, &count);
59 if (ret != 3)
60 continue;
61 if (hostuid + count < hostuid || nsuid + count < nsuid) {
62 /*
63 * uids wrapped around - unexpected as this is a procfile,
64 * so just bail.
65 */
66 fprintf(stderr, "pid wrapparound at entry %u %u %u in %s",
67 nsuid, hostuid, count, line);
68 return -1;
69 }
70 if (hostuid <= in_id && hostuid+count > in_id) {
71 /*
72 * now since hostuid <= in_id < hostuid+count, and
73 * hostuid+count and nsuid+count do not wrap around,
74 * we know that nsuid+(in_id-hostuid) which must be
75 * less that nsuid+(count) must not wrap around
76 */
77 return (in_id - hostuid) + nsuid;
78 }
79 }
80
81 // no answer found
82 return -1;
83}
84
341b21ad
SH
85/*
86 * for is_privileged_over,
87 * specify whether we require the calling uid to be root in his
88 * namespace
89 */
90#define NS_ROOT_REQD true
91#define NS_ROOT_OPT false
92
93static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_root)
758ad80c 94{
053a659d
SH
95 nih_local char *fpath = NULL;
96 bool answer = false;
97 uid_t nsuid;
98
341b21ad
SH
99 if (victim == -1 || uid == -1)
100 return false;
101
102 /*
103 * If the request is one not requiring root in the namespace,
104 * then having the same uid suffices. (i.e. uid 1000 has write
105 * access to files owned by uid 1000
106 */
107 if (!req_ns_root && uid == victim)
758ad80c
SH
108 return true;
109
053a659d
SH
110 fpath = NIH_MUST( nih_sprintf(NULL, "/proc/%d/uid_map", pid) );
111 FILE *f = fopen(fpath, "r");
112 if (!f)
113 return false;
114
341b21ad 115 /* if caller's not root in his namespace, reject */
053a659d
SH
116 nsuid = convert_id_to_ns(f, uid);
117 if (nsuid)
118 goto out;
119
341b21ad
SH
120 /*
121 * If victim is not mapped into caller's ns, reject.
122 * XXX I'm not sure this check is needed given that fuse
123 * will be sending requests where the vfs has converted
124 */
053a659d
SH
125 nsuid = convert_id_to_ns(f, victim);
126 if (nsuid == -1)
127 goto out;
128
129 answer = true;
130
131out:
132 fclose(f);
133 return answer;
758ad80c
SH
134}
135
136static bool perms_include(int fmode, mode_t req_mode)
137{
2ad6d2bd
SH
138 mode_t r;
139
140 switch (req_mode & O_ACCMODE) {
141 case O_RDONLY:
142 r = S_IROTH;
143 break;
144 case O_WRONLY:
145 r = S_IWOTH;
146 break;
147 case O_RDWR:
148 r = S_IROTH | S_IWOTH;
149 break;
150 default:
151 return false;
152 }
153 return ((fmode & r) == r);
758ad80c
SH
154}
155
3db25a35
SH
156static char *get_next_cgroup_dir(const char *taskcg, const char *querycg)
157{
158 char *start, *end;
159
160 if (strlen(taskcg) <= strlen(querycg)) {
161 fprintf(stderr, "%s: I was fed bad input\n", __func__);
162 return NULL;
163 }
164
165 if (strcmp(querycg, "/") == 0)
166 start = NIH_MUST( nih_strdup(NULL, taskcg + 1) );
167 else
168 start = NIH_MUST( nih_strdup(NULL, taskcg + strlen(querycg) + 1) );
169 end = strchr(start, '/');
170 if (end)
171 *end = '\0';
172 return start;
173}
174
758ad80c
SH
175/*
176 * check whether a fuse context may access a cgroup dir or file
177 *
178 * If file is not null, it is a cgroup file to check under cg.
179 * If file is null, then we are checking perms on cg itself.
180 *
181 * For files we can check the mode of the list_keys result.
182 * For cgroups, we must make assumptions based on the files under the
183 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
184 * yet.
185 */
186static bool fc_may_access(struct fuse_context *fc, const char *contrl, const char *cg, const char *file, mode_t mode)
187{
188 nih_local struct cgm_keys **list = NULL;
189 int i;
190
191 if (!file)
192 file = "tasks";
193
194 if (*file == '/')
195 file++;
196
197 if (!cgm_list_keys(contrl, cg, &list))
198 return false;
199 for (i = 0; list[i]; i++) {
200 if (strcmp(list[i]->name, file) == 0) {
201 struct cgm_keys *k = list[i];
341b21ad 202 if (is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_OPT)) {
758ad80c
SH
203 if (perms_include(k->mode >> 6, mode))
204 return true;
205 }
206 if (fc->gid == k->gid) {
207 if (perms_include(k->mode >> 3, mode))
208 return true;
209 }
210 return perms_include(k->mode, mode);
211 }
212 }
213
214 return false;
215}
216
3db25a35
SH
217static void stripnewline(char *x)
218{
219 size_t l = strlen(x);
220 if (l && x[l-1] == '\n')
221 x[l-1] = '\0';
222}
223
224/*
225 * If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
226 * If caller is in /a, he may act on /a/b, but not on /b.
227 * if the answer is false and nextcg is not NULL, then *nextcg will point
228 * to a nih_alloc'd string containing the next cgroup directory under cg
229 */
230static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg, char **nextcg)
231{
232 nih_local char *fnam = NULL;
233 FILE *f;
234 bool answer = false;
235 char *line = NULL;
236 size_t len = 0;
237
238 fnam = NIH_MUST( nih_sprintf(NULL, "/proc/%d/cgroup", pid) );
239 if (!(f = fopen(fnam, "r")))
240 return false;
241
242 while (getline(&line, &len, f) != -1) {
243 char *c1, *c2, *linecmp;
244 if (!line[0])
245 continue;
246 c1 = strchr(line, ':');
247 if (!c1)
248 goto out;
249 c1++;
250 c2 = strchr(c1, ':');
251 if (!c2)
252 goto out;
253 *c2 = '\0';
254 if (strcmp(c1, contrl) != 0)
255 continue;
256 c2++;
257 stripnewline(c2);
258 /*
259 * callers pass in '/' for root cgroup, otherwise they pass
260 * in a cgroup without leading '/'
261 */
262 linecmp = *cg == '/' ? c2 : c2+1;
263 if (strncmp(linecmp, cg, strlen(linecmp)) != 0) {
264 if (nextcg)
265 *nextcg = get_next_cgroup_dir(linecmp, cg);
266 goto out;
267 }
268 answer = true;
269 goto out;
270 }
271
272out:
273 fclose(f);
274 free(line);
275 return answer;
276}
277
758ad80c
SH
278/*
279 * given /cgroup/freezer/a/b, return "freezer". this will be nih-allocated
280 * and needs to be nih_freed.
281 */
282static char *pick_controller_from_path(struct fuse_context *fc, const char *path)
283{
284 const char *p1;
285 char *ret, *slash;
286
287 if (strlen(path) < 9)
288 return NULL;
289 p1 = path+8;
290 ret = nih_strdup(NULL, p1);
291 if (!ret)
292 return ret;
293 slash = strstr(ret, "/");
294 if (slash)
295 *slash = '\0';
296
297 /* verify that it is a subsystem */
298 char **list = LXCFS_DATA ? LXCFS_DATA->subsystems : NULL;
299 int i;
300 if (!list) {
301 nih_free(ret);
302 return NULL;
303 }
304 for (i = 0; list[i]; i++) {
305 if (strcmp(list[i], ret) == 0)
306 return ret;
307 }
308 nih_free(ret);
309 return NULL;
310}
311
312/*
313 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
314 * Note that the returned value may include files (keynames) etc
315 */
316static const char *find_cgroup_in_path(const char *path)
317{
318 const char *p1;
319
320 if (strlen(path) < 9)
321 return NULL;
322 p1 = strstr(path+8, "/");
323 if (!p1)
324 return NULL;
325 return p1+1;
326}
327
328static bool is_child_cgroup(const char *contr, const char *dir, const char *f)
329{
330 nih_local char **list = NULL;
331 int i;
332
333 if (!f)
334 return false;
335 if (*f == '/')
336 f++;
337
338 if (!cgm_list_children(contr, dir, &list))
339 return false;
340 for (i = 0; list[i]; i++) {
341 if (strcmp(list[i], f) == 0)
342 return true;
343 }
344
345 return false;
346}
347
348static struct cgm_keys *get_cgroup_key(const char *contr, const char *dir, const char *f)
349{
350 nih_local struct cgm_keys **list = NULL;
351 struct cgm_keys *k;
352 int i;
353
354 if (!f)
355 return NULL;
356 if (*f == '/')
357 f++;
358 if (!cgm_list_keys(contr, dir, &list))
359 return NULL;
360 for (i = 0; list[i]; i++) {
361 if (strcmp(list[i]->name, f) == 0) {
362 k = NIH_MUST( nih_alloc(NULL, (sizeof(*k))) );
363 k->name = NIH_MUST( nih_strdup(k, list[i]->name) );
364 k->uid = list[i]->uid;
365 k->gid = list[i]->gid;
366 k->mode = list[i]->mode;
367 return k;
368 }
369 }
370
371 return NULL;
372}
373
374static void get_cgdir_and_path(const char *cg, char **dir, char **file)
375{
758ad80c
SH
376 char *p;
377
378 *dir = NIH_MUST( nih_strdup(NULL, cg) );
379 *file = strrchr(cg, '/');
380 if (!*file) {
381 *file = NULL;
382 return;
383 }
384 p = strrchr(*dir, '/');
385 *p = '\0';
386}
387
99978832
SH
388static size_t get_file_size(const char *contrl, const char *cg, const char *f)
389{
390 nih_local char *data = NULL;
391 size_t s;
392 if (!cgm_get_value(contrl, cg, f, &data))
393 return -EINVAL;
394 s = strlen(data);
395 return s;
396}
2ad6d2bd 397
758ad80c 398/*
2ad6d2bd 399 * FUSE ops for /cgroup
758ad80c 400 */
2ad6d2bd 401
758ad80c
SH
402static int cg_getattr(const char *path, struct stat *sb)
403{
404 struct timespec now;
405 struct fuse_context *fc = fuse_get_context();
406 nih_local char * cgdir = NULL;
407 char *fpath = NULL, *path1, *path2;
408 nih_local struct cgm_keys *k = NULL;
409 const char *cgroup;
410 nih_local char *controller = NULL;
411
412
413 if (!fc)
414 return -EIO;
415
416 memset(sb, 0, sizeof(struct stat));
417
418 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
419 return -EINVAL;
420
421 sb->st_uid = sb->st_gid = 0;
422 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
423 sb->st_size = 0;
424
425 if (strcmp(path, "/cgroup") == 0) {
426 sb->st_mode = S_IFDIR | 00755;
427 sb->st_nlink = 2;
428 return 0;
429 }
430
431 controller = pick_controller_from_path(fc, path);
432 if (!controller)
433 return -EIO;
758ad80c
SH
434 cgroup = find_cgroup_in_path(path);
435 if (!cgroup) {
436 /* this is just /cgroup/controller, return it as a dir */
437 sb->st_mode = S_IFDIR | 00755;
438 sb->st_nlink = 2;
439 return 0;
440 }
341b21ad 441
758ad80c
SH
442 get_cgdir_and_path(cgroup, &cgdir, &fpath);
443
444 if (!fpath) {
445 path1 = "/";
446 path2 = cgdir;
447 } else {
448 path1 = cgdir;
449 path2 = fpath;
450 }
451
758ad80c
SH
452 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
453 * Then check that caller's cgroup is under path if fpath is a child
454 * cgroup, or cgdir if fpath is a file */
455
456 if (is_child_cgroup(controller, path1, path2)) {
f9a05025
SH
457 if (!caller_is_in_ancestor(fc->pid, controller, cgroup, NULL)) {
458 /* this is just /cgroup/controller, return it as a dir */
459 sb->st_mode = S_IFDIR | 00555;
460 sb->st_nlink = 2;
461 return 0;
462 }
758ad80c 463 if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY))
f9a05025 464 return -EACCES;
758ad80c 465
053a659d
SH
466 // get uid, gid, from '/tasks' file and make up a mode
467 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
468 sb->st_mode = S_IFDIR | 00755;
469 k = get_cgroup_key(controller, cgroup, "tasks");
470 if (!k) {
053a659d
SH
471 sb->st_uid = sb->st_gid = 0;
472 } else {
053a659d
SH
473 sb->st_uid = k->uid;
474 sb->st_gid = k->gid;
475 }
758ad80c
SH
476 sb->st_nlink = 2;
477 return 0;
478 }
479
480 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
3db25a35
SH
481 if (!caller_is_in_ancestor(fc->pid, controller, path1, NULL))
482 return -ENOENT;
758ad80c 483 if (!fc_may_access(fc, controller, path1, path2, O_RDONLY))
f9a05025 484 return -EACCES;
758ad80c 485
758ad80c 486 sb->st_mode = S_IFREG | k->mode;
053a659d 487 sb->st_nlink = 1;
758ad80c
SH
488 sb->st_uid = k->uid;
489 sb->st_gid = k->gid;
99978832 490 sb->st_size = get_file_size(controller, path1, path2);
758ad80c
SH
491 return 0;
492 }
493
ab54b798 494 return -ENOENT;
758ad80c 495}
2183082c 496
758ad80c 497static int cg_opendir(const char *path, struct fuse_file_info *fi)
2183082c 498{
758ad80c
SH
499 return 0;
500}
501
758ad80c
SH
502static int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
503 struct fuse_file_info *fi)
504{
505 struct fuse_context *fc = fuse_get_context();
506
507 if (!fc)
508 return -EIO;
509
510 if (strcmp(path, "/cgroup") == 0) {
511 // get list of controllers
512 char **list = LXCFS_DATA ? LXCFS_DATA->subsystems : NULL;
513 int i;
514
515 if (!list)
516 return -EIO;
517 /* TODO - collect the list of controllers at fuse_init */
518 for (i = 0; list[i]; i++) {
519 if (filler(buf, list[i], NULL, 0) != 0) {
520 return -EIO;
521 }
522 }
523 return 0;
524 }
525
526 // return list of keys for the controller, and list of child cgroups
527 nih_local struct cgm_keys **list = NULL;
528 const char *cgroup;
529 nih_local char *controller = NULL;
530 int i;
3db25a35 531 nih_local char *nextcg = NULL;
758ad80c
SH
532
533 controller = pick_controller_from_path(fc, path);
534 if (!controller)
535 return -EIO;
536
537 cgroup = find_cgroup_in_path(path);
538 if (!cgroup) {
539 /* this is just /cgroup/controller, return its contents */
540 cgroup = "/";
541 }
542
543 if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY))
f9a05025 544 return -EACCES;
758ad80c
SH
545
546 if (!cgm_list_keys(controller, cgroup, &list))
3db25a35 547 // not a valid cgroup
758ad80c 548 return -EINVAL;
3db25a35
SH
549
550 if (!caller_is_in_ancestor(fc->pid, controller, cgroup, &nextcg)) {
551 if (nextcg) {
552 int ret;
553 ret = filler(buf, nextcg, NULL, 0);
554 if (ret != 0)
555 return -EIO;
556 }
557 return 0;
558 }
559
758ad80c 560 for (i = 0; list[i]; i++) {
758ad80c
SH
561 if (filler(buf, list[i]->name, NULL, 0) != 0) {
562 return -EIO;
563 }
564 }
565
566 // now get the list of child cgroups
567 nih_local char **clist;
568
569 if (!cgm_list_children(controller, cgroup, &clist))
570 return 0;
571 for (i = 0; clist[i]; i++) {
758ad80c
SH
572 if (filler(buf, clist[i], NULL, 0) != 0) {
573 return -EIO;
574 }
575 }
576 return 0;
577}
578
579static int cg_releasedir(const char *path, struct fuse_file_info *fi)
580{
581 return 0;
582}
583
99978832
SH
584static int cg_open(const char *path, struct fuse_file_info *fi)
585{
586 nih_local char *controller = NULL;
587 const char *cgroup;
588 char *fpath = NULL, *path1, *path2;
589 nih_local char * cgdir = NULL;
590 nih_local struct cgm_keys *k = NULL;
591 struct fuse_context *fc = fuse_get_context();
592
593 if (!fc)
594 return -EIO;
595
596 controller = pick_controller_from_path(fc, path);
597 if (!controller)
598 return -EIO;
599 cgroup = find_cgroup_in_path(path);
600 if (!cgroup)
601 return -EINVAL;
602
603 get_cgdir_and_path(cgroup, &cgdir, &fpath);
604 if (!fpath) {
605 path1 = "/";
606 path2 = cgdir;
607 } else {
608 path1 = cgdir;
609 path2 = fpath;
610 }
611
612 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
613 if (!fc_may_access(fc, controller, path1, path2, fi->flags))
f9a05025
SH
614 // should never get here
615 return -EACCES;
99978832
SH
616
617 /* TODO - we want to cache this info for read/write */
618 return 0;
619 }
620
621 return -EINVAL;
622}
623
624static int cg_read(const char *path, char *buf, size_t size, off_t offset,
625 struct fuse_file_info *fi)
626{
627 nih_local char *controller = NULL;
628 const char *cgroup;
629 char *fpath = NULL, *path1, *path2;
630 struct fuse_context *fc = fuse_get_context();
631 nih_local char * cgdir = NULL;
632 nih_local struct cgm_keys *k = NULL;
633
634 if (offset)
635 return -EIO;
636
637 if (!fc)
638 return -EIO;
639
640 controller = pick_controller_from_path(fc, path);
641 if (!controller)
f9a05025 642 return -EINVAL;
99978832
SH
643 cgroup = find_cgroup_in_path(path);
644 if (!cgroup)
645 return -EINVAL;
646
647 get_cgdir_and_path(cgroup, &cgdir, &fpath);
648 if (!fpath) {
649 path1 = "/";
650 path2 = cgdir;
651 } else {
652 path1 = cgdir;
653 path2 = fpath;
654 }
655
656 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
657 nih_local char *data = NULL;
658 int s;
659
2ad6d2bd 660 if (!fc_may_access(fc, controller, path1, path2, O_RDONLY))
f9a05025
SH
661 // should never get here
662 return -EACCES;
99978832
SH
663
664 if (!cgm_get_value(controller, path1, path2, &data))
665 return -EINVAL;
666
667 s = strlen(data);
668 if (s > size)
669 s = size;
670 memcpy(buf, data, s);
671
99978832
SH
672 return s;
673 }
674
675 return -EINVAL;
676}
677
2ad6d2bd
SH
678int cg_write(const char *path, const char *buf, size_t size, off_t offset,
679 struct fuse_file_info *fi)
680{
681 nih_local char *controller = NULL;
682 const char *cgroup;
683 char *fpath = NULL, *path1, *path2;
684 struct fuse_context *fc = fuse_get_context();
685 nih_local char * cgdir = NULL;
686 nih_local struct cgm_keys *k = NULL;
687
2ad6d2bd 688 if (offset)
f9a05025 689 return -EINVAL;
2ad6d2bd
SH
690
691 if (!fc)
692 return -EIO;
693
694 controller = pick_controller_from_path(fc, path);
695 if (!controller)
f9a05025 696 return -EINVAL;
2ad6d2bd
SH
697 cgroup = find_cgroup_in_path(path);
698 if (!cgroup)
699 return -EINVAL;
700
701 get_cgdir_and_path(cgroup, &cgdir, &fpath);
702 if (!fpath) {
703 path1 = "/";
704 path2 = cgdir;
705 } else {
706 path1 = cgdir;
707 path2 = fpath;
708 }
709
710 if ((k = get_cgroup_key(controller, path1, path2)) != NULL) {
711 if (!fc_may_access(fc, controller, path1, path2, O_WRONLY))
f9a05025 712 return -EACCES;
2ad6d2bd
SH
713
714 if (!cgm_set_value(controller, path1, path2, buf))
715 return -EINVAL;
716
717 return size;
718 }
719
720 return -EINVAL;
721}
722
341b21ad
SH
723int cg_chown(const char *path, uid_t uid, gid_t gid)
724{
725 struct fuse_context *fc = fuse_get_context();
726 nih_local char * cgdir = NULL;
727 char *fpath = NULL, *path1, *path2;
728 nih_local struct cgm_keys *k = NULL;
729 const char *cgroup;
730 nih_local char *controller = NULL;
731
732
733 if (!fc)
734 return -EIO;
735
736 if (strcmp(path, "/cgroup") == 0)
737 return -EINVAL;
738
739 controller = pick_controller_from_path(fc, path);
740 if (!controller)
f9a05025 741 return -EINVAL;
341b21ad
SH
742 cgroup = find_cgroup_in_path(path);
743 if (!cgroup)
744 /* this is just /cgroup/controller */
745 return -EINVAL;
746
747 get_cgdir_and_path(cgroup, &cgdir, &fpath);
748
749 if (!fpath) {
750 path1 = "/";
751 path2 = cgdir;
752 } else {
753 path1 = cgdir;
754 path2 = fpath;
755 }
756
757 if (is_child_cgroup(controller, path1, path2)) {
758 // get uid, gid, from '/tasks' file and make up a mode
759 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
760 k = get_cgroup_key(controller, cgroup, "tasks");
761
762 } else
763 k = get_cgroup_key(controller, path1, path2);
764
765 if (!k)
766 return -EINVAL;
767
768 /*
769 * This being a fuse request, the uid and gid must be valid
770 * in the caller's namespace. So we can just check to make
771 * sure that the caller is root in his uid, and privileged
772 * over the file's current owner.
773 */
774 if (!is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_REQD))
f9a05025 775 return -EACCES;
341b21ad
SH
776
777 if (!cgm_chown_file(controller, cgroup, uid, gid))
778 return -EINVAL;
779 return 0;
780}
2ad6d2bd 781
fd2e4e03
SH
782int cg_chmod(const char *path, mode_t mode)
783{
0a1bb5ea
SH
784 struct fuse_context *fc = fuse_get_context();
785 nih_local char * cgdir = NULL;
786 char *fpath = NULL, *path1, *path2;
787 nih_local struct cgm_keys *k = NULL;
788 const char *cgroup;
789 nih_local char *controller = NULL;
790
791 if (!fc)
792 return -EIO;
793
794 if (strcmp(path, "/cgroup") == 0)
795 return -EINVAL;
796
797 controller = pick_controller_from_path(fc, path);
798 if (!controller)
f9a05025 799 return -EINVAL;
0a1bb5ea
SH
800 cgroup = find_cgroup_in_path(path);
801 if (!cgroup)
802 /* this is just /cgroup/controller */
803 return -EINVAL;
804
805 get_cgdir_and_path(cgroup, &cgdir, &fpath);
806
807 if (!fpath) {
808 path1 = "/";
809 path2 = cgdir;
810 } else {
811 path1 = cgdir;
812 path2 = fpath;
813 }
814
815 if (is_child_cgroup(controller, path1, path2)) {
816 // get uid, gid, from '/tasks' file and make up a mode
817 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
818 k = get_cgroup_key(controller, cgroup, "tasks");
819
820 } else
821 k = get_cgroup_key(controller, path1, path2);
822
823 if (!k)
824 return -EINVAL;
825
826 /*
827 * This being a fuse request, the uid and gid must be valid
828 * in the caller's namespace. So we can just check to make
829 * sure that the caller is root in his uid, and privileged
830 * over the file's current owner.
831 */
832 if (!is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_OPT))
833 return -EPERM;
834
835 if (!cgm_chmod_file(controller, cgroup, mode))
836 return -EINVAL;
837 return 0;
fd2e4e03
SH
838}
839
ab54b798
SH
840int cg_mkdir(const char *path, mode_t mode)
841{
842 struct fuse_context *fc = fuse_get_context();
843 nih_local struct cgm_keys **list = NULL;
844 char *fpath = NULL, *path1;
845 nih_local char * cgdir = NULL;
846 const char *cgroup;
847 nih_local char *controller = NULL;
848
ab54b798
SH
849 if (!fc)
850 return -EIO;
851
852
853 controller = pick_controller_from_path(fc, path);
854 if (!controller)
f9a05025 855 return -EINVAL;
ab54b798
SH
856
857 cgroup = find_cgroup_in_path(path);
858 if (!cgroup)
f9a05025 859 return -EINVAL;
ab54b798
SH
860
861 get_cgdir_and_path(cgroup, &cgdir, &fpath);
862 if (!fpath)
863 path1 = "/";
864 else
865 path1 = cgdir;
866
867 if (!fc_may_access(fc, controller, path1, NULL, O_RDWR))
f9a05025 868 return -EACCES;
ab54b798
SH
869
870
871 if (!cgm_create(controller, cgroup, fc->uid, fc->gid))
872 return -EINVAL;
873
874 return 0;
875}
876
50d8d5b5
SH
877static int cg_rmdir(const char *path)
878{
879 struct fuse_context *fc = fuse_get_context();
880 nih_local struct cgm_keys **list = NULL;
881 char *fpath = NULL;
882 nih_local char * cgdir = NULL;
883 const char *cgroup;
884 nih_local char *controller = NULL;
885
886 if (!fc)
887 return -EIO;
888
889
890 controller = pick_controller_from_path(fc, path);
891 if (!controller)
f9a05025 892 return -EINVAL;
50d8d5b5
SH
893
894 cgroup = find_cgroup_in_path(path);
895 if (!cgroup)
f9a05025 896 return -EINVAL;
50d8d5b5
SH
897
898 get_cgdir_and_path(cgroup, &cgdir, &fpath);
899 if (!fpath)
900 return -EINVAL;
901
902 if (!fc_may_access(fc, controller, cgdir, NULL, O_WRONLY))
f9a05025 903 return -EACCES;
50d8d5b5
SH
904
905 if (!cgm_remove(controller, cgroup))
906 return -EINVAL;
907
908 return 0;
909}
910
758ad80c 911/*
2ad6d2bd 912 * FUSE ops for /proc
758ad80c 913 */
758ad80c
SH
914
915static int proc_getattr(const char *path, struct stat *sb)
916{
917 if (strcmp(path, "/proc") != 0)
918 return -EINVAL;
919 sb->st_mode = S_IFDIR | 00755;
920 sb->st_nlink = 2;
921 return 0;
922}
923
2ad6d2bd
SH
924/*
925 * FUSE ops for /
926 * these just delegate to the /proc and /cgroup ops as
927 * needed
928 */
758ad80c
SH
929
930static int lxcfs_getattr(const char *path, struct stat *sb)
931{
932 if (strcmp(path, "/") == 0) {
933 sb->st_mode = S_IFDIR | 00755;
934 sb->st_nlink = 2;
935 return 0;
936 }
937 if (strncmp(path, "/cgroup", 7) == 0) {
938 return cg_getattr(path, sb);
939 }
940 if (strncmp(path, "/proc", 7) == 0) {
941 return proc_getattr(path, sb);
942 }
943 return -EINVAL;
944}
945
946static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
947{
948 if (strcmp(path, "/") == 0)
949 return 0;
950
951 if (strncmp(path, "/cgroup", 7) == 0) {
952 return cg_opendir(path, fi);
953 }
954 return -EINVAL;
955}
956
957static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
958 struct fuse_file_info *fi)
959{
960 if (strcmp(path, "/") == 0) {
961 if (filler(buf, "proc", NULL, 0) != 0 ||
962 filler(buf, "cgroup", NULL, 0) != 0)
963 return -EINVAL;
964 return 0;
965 }
966 if (strncmp(path, "/cgroup", 7) == 0) {
967 return cg_readdir(path, buf, filler, offset, fi);
968 }
969 return -EINVAL;
970}
971
972static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
973{
974 if (strcmp(path, "/") == 0)
975 return 0;
976 if (strncmp(path, "/cgroup", 7) == 0) {
977 return cg_releasedir(path, fi);
978 }
979 return -EINVAL;
980}
981
99978832
SH
982static int lxcfs_open(const char *path, struct fuse_file_info *fi)
983{
984 if (strncmp(path, "/cgroup", 7) == 0) {
985 return cg_open(path, fi);
986 }
987
988 return -EINVAL;
989}
990
991static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
992 struct fuse_file_info *fi)
993{
994 if (strncmp(path, "/cgroup", 7) == 0) {
995 return cg_read(path, buf, size, offset, fi);
996 }
997
998 return -EINVAL;
999}
1000
2ad6d2bd
SH
1001int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
1002 struct fuse_file_info *fi)
1003{
1004 if (strncmp(path, "/cgroup", 7) == 0) {
1005 return cg_write(path, buf, size, offset, fi);
1006 }
1007
1008 return -EINVAL;
1009}
1010
99978832
SH
1011static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
1012{
1013 return 0;
1014}
1015
1016static int lxcfs_release(const char *path, struct fuse_file_info *fi)
758ad80c 1017{
99978832
SH
1018 return 0;
1019}
1020
1021static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
1022{
1023 return 0;
758ad80c
SH
1024}
1025
ab54b798
SH
1026int lxcfs_mkdir(const char *path, mode_t mode)
1027{
1028 if (strncmp(path, "/cgroup", 7) == 0)
1029 return cg_mkdir(path, mode);
1030
1031 return -EINVAL;
1032}
1033
341b21ad
SH
1034int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
1035{
1036 if (strncmp(path, "/cgroup", 7) == 0)
1037 return cg_chown(path, uid, gid);
1038
1039 return -EINVAL;
1040}
1041
2ad6d2bd
SH
1042/*
1043 * cat first does a truncate before doing ops->write. This doesn't
1044 * really make sense for cgroups. So just return 0 always but do
1045 * nothing.
1046 */
1047int lxcfs_truncate(const char *path, off_t newsize)
1048{
1049 if (strncmp(path, "/cgroup", 7) == 0)
1050 return 0;
1051 return -EINVAL;
1052}
1053
50d8d5b5
SH
1054int lxcfs_rmdir(const char *path)
1055{
1056 if (strncmp(path, "/cgroup", 7) == 0)
1057 return cg_rmdir(path);
1058 return -EINVAL;
1059}
1060
fd2e4e03
SH
1061int lxcfs_chmod(const char *path, mode_t mode)
1062{
1063 if (strncmp(path, "/cgroup", 7) == 0)
1064 return cg_chmod(path, mode);
1065 return -EINVAL;
1066}
1067
758ad80c
SH
1068const struct fuse_operations lxcfs_ops = {
1069 .getattr = lxcfs_getattr,
1070 .readlink = NULL,
1071 .getdir = NULL,
1072 .mknod = NULL,
ab54b798 1073 .mkdir = lxcfs_mkdir,
758ad80c 1074 .unlink = NULL,
50d8d5b5 1075 .rmdir = lxcfs_rmdir,
758ad80c
SH
1076 .symlink = NULL,
1077 .rename = NULL,
1078 .link = NULL,
fd2e4e03 1079 .chmod = lxcfs_chmod,
341b21ad 1080 .chown = lxcfs_chown,
2ad6d2bd 1081 .truncate = lxcfs_truncate,
758ad80c 1082 .utime = NULL,
99978832
SH
1083
1084 .open = lxcfs_open,
1085 .read = lxcfs_read,
1086 .release = lxcfs_release,
2ad6d2bd 1087 .write = lxcfs_write,
99978832 1088
758ad80c 1089 .statfs = NULL,
99978832
SH
1090 .flush = lxcfs_flush,
1091 .fsync = lxcfs_fsync,
758ad80c
SH
1092
1093 .setxattr = NULL,
1094 .getxattr = NULL,
1095 .listxattr = NULL,
1096 .removexattr = NULL,
1097
1098 .opendir = lxcfs_opendir,
1099 .readdir = lxcfs_readdir,
1100 .releasedir = lxcfs_releasedir,
1101
1102 .fsyncdir = NULL,
1103 .init = NULL,
1104 .destroy = NULL,
1105 .access = NULL,
1106 .create = NULL,
1107 .ftruncate = NULL,
1108 .fgetattr = NULL,
1109};
1110
99978832 1111static void usage(const char *me)
758ad80c
SH
1112{
1113 fprintf(stderr, "Usage:\n");
1114 fprintf(stderr, "\n");
1115 fprintf(stderr, "%s [FUSE and mount options] mountpoint\n", me);
1116 exit(1);
1117}
1118
99978832 1119static bool is_help(char *w)
758ad80c
SH
1120{
1121 if (strcmp(w, "-h") == 0 ||
1122 strcmp(w, "--help") == 0 ||
1123 strcmp(w, "-help") == 0 ||
1124 strcmp(w, "help") == 0)
1125 return true;
1126 return false;
1127}
1128
1129int main(int argc, char *argv[])
1130{
1131 int ret;
1132 struct lxcfs_state *d;
1133
1134 if (argc < 2 || is_help(argv[1]))
1135 usage(argv[0]);
1136
1137 d = malloc(sizeof(*d));
1138 if (!d)
1139 return -1;
1140
1141 if (!cgm_escape_cgroup())
1142 fprintf(stderr, "WARNING: failed to escape to root cgroup\n");
1143
1144 if (!cgm_get_controllers(&d->subsystems))
1145 return -1;
1146
1147 ret = fuse_main(argc, argv, &lxcfs_ops, d);
1148
1149 return ret;
2183082c 1150}