]> git.proxmox.com Git - mirror_lxcfs.git/blob - lxcfs.c
Merge pull request #329 from brauner/master
[mirror_lxcfs.git] / lxcfs.c
1 /* lxcfs
2 *
3 * Copyright © 2014-2016 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
5 *
6 * See COPYING file for details.
7 */
8
9 #ifndef _GNU_SOURCE
10 #define _GNU_SOURCE
11 #endif
12
13 #ifndef FUSE_USE_VERSION
14 #define FUSE_USE_VERSION 26
15 #endif
16
17 #define _FILE_OFFSET_BITS 64
18
19 #include <alloca.h>
20 #include <dirent.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fuse.h>
25 #include <libgen.h>
26 #include <pthread.h>
27 #include <sched.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <wait.h>
35 #include <linux/sched.h>
36 #include <sys/epoll.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
39 #include <linux/limits.h>
40
41 #include "bindings.h"
42 #include "config.h"
43
44 void *dlopen_handle;
45
46 /* Functions to keep track of number of threads using the library */
47
48 static int users_count;
49 static pthread_mutex_t user_count_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static void lock_mutex(pthread_mutex_t *l)
51 {
52 int ret;
53
54 if ((ret = pthread_mutex_lock(l)) != 0) {
55 lxcfs_error("returned:%d %s\n", ret, strerror(ret));
56 exit(1);
57 }
58 }
59
60 static void unlock_mutex(pthread_mutex_t *l)
61 {
62 int ret;
63
64 if ((ret = pthread_mutex_unlock(l)) != 0) {
65 lxcfs_error("returned:%d %s\n", ret, strerror(ret));
66 exit(1);
67 }
68 }
69
70 static void users_lock(void)
71 {
72 lock_mutex(&user_count_mutex);
73 }
74
75 static void users_unlock(void)
76 {
77 unlock_mutex(&user_count_mutex);
78 }
79
80 static pthread_t loadavg_pid = 0;
81
82 /* Returns zero on success */
83 static int start_loadavg(void) {
84 char *error;
85 pthread_t (*__load_daemon)(int);
86
87 dlerror(); /* Clear any existing error */
88
89 __load_daemon = (pthread_t (*)(int)) dlsym(dlopen_handle, "load_daemon");
90 error = dlerror();
91 if (error != NULL) {
92 lxcfs_error("load_daemon fails:%s\n", error);
93 return -1;
94 }
95 loadavg_pid = __load_daemon(1);
96 if (loadavg_pid == 0)
97 return -1;
98
99 return 0;
100 }
101
102 /* Returns zero on success */
103 static int stop_loadavg(void) {
104 char *error;
105 int (*__stop_load_daemon)(pthread_t);
106
107 __stop_load_daemon = (int (*)(pthread_t)) dlsym(dlopen_handle, "stop_load_daemon");
108 error = dlerror();
109 if (error != NULL) {
110 lxcfs_error("stop_load_daemon error: %s\n", error);
111 return -1;
112 }
113
114 if (__stop_load_daemon(loadavg_pid) != 0)
115 return -1;
116
117 return 0;
118 }
119
120 static volatile sig_atomic_t need_reload;
121
122 /* do_reload - reload the dynamic library. Done under
123 * lock and when we know the user_count was 0 */
124 static void do_reload(void)
125 {
126 char lxcfs_lib_path[PATH_MAX];
127
128 if (loadavg_pid > 0)
129 stop_loadavg();
130
131 if (dlopen_handle) {
132 lxcfs_debug("%s\n", "Closing liblxcfs.so handle.");
133 dlclose(dlopen_handle);
134 }
135
136 /* First try loading using ld.so */
137 dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
138 if (dlopen_handle) {
139 lxcfs_debug("%s\n", "Successfully called dlopen() on liblxcfs.so.");
140 goto good;
141 }
142
143 #ifdef LIBDIR
144 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
145 snprintf(lxcfs_lib_path, PATH_MAX, "%s/lxcfs/liblxcfs.so", LIBDIR);
146 #else
147 snprintf(lxcfs_lib_path, PATH_MAX, "/usr/local/lib/lxcfs/liblxcfs.so");
148 #endif
149 dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY);
150 if (!dlopen_handle) {
151 lxcfs_error("Failed to open liblxcfs.so: %s.\n", dlerror());
152 _exit(1);
153 }
154
155 good:
156 if (loadavg_pid > 0)
157 start_loadavg();
158
159 if (need_reload)
160 lxcfs_error("%s\n", "lxcfs: reloaded");
161 need_reload = 0;
162 }
163
164 static void up_users(void)
165 {
166 users_lock();
167 if (users_count == 0 && need_reload)
168 do_reload();
169 users_count++;
170 users_unlock();
171 }
172
173 static void down_users(void)
174 {
175 users_lock();
176 users_count--;
177 users_unlock();
178 }
179
180 static void reload_handler(int sig)
181 {
182 need_reload = 1;
183 }
184
185 /* Functions to run the library methods */
186 static int do_cg_getattr(const char *path, struct stat *sb)
187 {
188 int (*__cg_getattr)(const char *path, struct stat *sb);
189 char *error;
190 dlerror(); /* Clear any existing error */
191 __cg_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "cg_getattr");
192 error = dlerror();
193 if (error != NULL) {
194 lxcfs_error("%s\n", error);
195 return -1;
196 }
197
198 return __cg_getattr(path, sb);
199 }
200
201 static int do_proc_getattr(const char *path, struct stat *sb)
202 {
203 int (*__proc_getattr)(const char *path, struct stat *sb);
204 char *error;
205 dlerror(); /* Clear any existing error */
206 __proc_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "proc_getattr");
207 error = dlerror();
208 if (error != NULL) {
209 lxcfs_error("%s\n", error);
210 return -1;
211 }
212
213 return __proc_getattr(path, sb);
214 }
215
216 static int do_sys_getattr(const char *path, struct stat *sb)
217 {
218 int (*__sys_getattr)(const char *path, struct stat *sb);
219 char *error;
220 dlerror(); /* Clear any existing error */
221 __sys_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "sys_getattr");
222 error = dlerror();
223 if (error != NULL) {
224 lxcfs_error("%s\n", error);
225 return -1;
226 }
227
228 return __sys_getattr(path, sb);
229 }
230
231 static int do_cg_read(const char *path, char *buf, size_t size, off_t offset,
232 struct fuse_file_info *fi)
233 {
234 int (*__cg_read)(const char *path, char *buf, size_t size, off_t offset,
235 struct fuse_file_info *fi);
236 char *error;
237
238 dlerror(); /* Clear any existing error */
239 __cg_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_read");
240 error = dlerror();
241 if (error != NULL) {
242 lxcfs_error("%s\n", error);
243 return -1;
244 }
245
246 return __cg_read(path, buf, size, offset, fi);
247 }
248
249 static int do_proc_read(const char *path, char *buf, size_t size, off_t offset,
250 struct fuse_file_info *fi)
251 {
252 int (*__proc_read)(const char *path, char *buf, size_t size,
253 off_t offset, struct fuse_file_info *fi);
254 char *error;
255
256 dlerror(); /* Clear any existing error */
257 __proc_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_read");
258 error = dlerror();
259 if (error != NULL) {
260 lxcfs_error("%s\n", error);
261 return -1;
262 }
263
264 return __proc_read(path, buf, size, offset, fi);
265 }
266
267 static int do_sys_read(const char *path, char *buf, size_t size, off_t offset,
268 struct fuse_file_info *fi)
269 {
270 int (*__sys_read)(const char *path, char *buf, size_t size,
271 off_t offset, struct fuse_file_info *fi);
272 char *error;
273
274 dlerror(); /* Clear any existing error */
275 __sys_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "sys_read");
276 error = dlerror();
277 if (error != NULL) {
278 lxcfs_error("%s\n", error);
279 return -1;
280 }
281
282 return __sys_read(path, buf, size, offset, fi);
283 }
284
285 static int do_cg_write(const char *path, const char *buf, size_t size, off_t offset,
286 struct fuse_file_info *fi)
287 {
288 int (*__cg_write)(const char *path, const char *buf, size_t size,
289 off_t offset, struct fuse_file_info *fi);
290 char *error;
291 dlerror(); /* Clear any existing error */
292 __cg_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_write");
293 error = dlerror();
294 if (error != NULL) {
295 lxcfs_error("%s\n", error);
296 return -1;
297 }
298
299 return __cg_write(path, buf, size, offset, fi);
300 }
301
302 static int do_cg_mkdir(const char *path, mode_t mode)
303 {
304 int (*__cg_mkdir)(const char *path, mode_t mode);
305 char *error;
306
307 dlerror(); /* Clear any existing error */
308 __cg_mkdir = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_mkdir");
309 error = dlerror();
310 if (error != NULL) {
311 lxcfs_error("%s\n", error);
312 return -1;
313 }
314
315 return __cg_mkdir(path, mode);
316 }
317
318 static int do_cg_chown(const char *path, uid_t uid, gid_t gid)
319 {
320 int (*__cg_chown)(const char *path, uid_t uid, gid_t gid);
321 char *error;
322
323 dlerror(); /* Clear any existing error */
324 __cg_chown = (int (*)(const char *, uid_t, gid_t)) dlsym(dlopen_handle, "cg_chown");
325 error = dlerror();
326 if (error != NULL) {
327 lxcfs_error("%s\n", error);
328 return -1;
329 }
330
331 return __cg_chown(path, uid, gid);
332 }
333
334 static int do_cg_rmdir(const char *path)
335 {
336 int (*__cg_rmdir)(const char *path);
337 char *error;
338
339 dlerror(); /* Clear any existing error */
340 __cg_rmdir = (int (*)(const char *path)) dlsym(dlopen_handle, "cg_rmdir");
341 error = dlerror();
342 if (error != NULL) {
343 lxcfs_error("%s\n", error);
344 return -1;
345 }
346
347 return __cg_rmdir(path);
348 }
349
350 static int do_cg_chmod(const char *path, mode_t mode)
351 {
352 int (*__cg_chmod)(const char *path, mode_t mode);
353 char *error;
354
355 dlerror(); /* Clear any existing error */
356 __cg_chmod = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_chmod");
357 error = dlerror();
358 if (error != NULL) {
359 lxcfs_error("%s\n", error);
360 return -1;
361 }
362
363 return __cg_chmod(path, mode);
364 }
365
366 static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
367 off_t offset, struct fuse_file_info *fi)
368 {
369 int (*__cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
370 off_t offset, struct fuse_file_info *fi);
371 char *error;
372
373 dlerror(); /* Clear any existing error */
374 __cg_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_readdir");
375 error = dlerror();
376 if (error != NULL) {
377 lxcfs_error("%s\n", error);
378 return -1;
379 }
380
381 return __cg_readdir(path, buf, filler, offset, fi);
382 }
383
384 static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
385 off_t offset, struct fuse_file_info *fi)
386 {
387 int (*__proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
388 off_t offset, struct fuse_file_info *fi);
389 char *error;
390
391 dlerror(); /* Clear any existing error */
392 __proc_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_readdir");
393 error = dlerror();
394 if (error != NULL) {
395 lxcfs_error("%s\n", error);
396 return -1;
397 }
398
399 return __proc_readdir(path, buf, filler, offset, fi);
400 }
401
402 static int do_sys_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
403 off_t offset, struct fuse_file_info *fi)
404 {
405 int (*__sys_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
406 struct fuse_file_info *fi);
407 char *error;
408
409 dlerror(); /* Clear any existing error */
410 __sys_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "sys_readdir");
411 error = dlerror();
412 if (error != NULL) {
413 lxcfs_error("%s\n", error);
414 return -1;
415 }
416
417 return __sys_readdir(path, buf, filler, offset, fi);
418 }
419
420
421 static int do_cg_open(const char *path, struct fuse_file_info *fi)
422 {
423 int (*__cg_open)(const char *path, struct fuse_file_info *fi);
424 char *error;
425 dlerror(); /* Clear any existing error */
426 __cg_open = (int (*)(const char *, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_open");
427 error = dlerror();
428 if (error != NULL) {
429 lxcfs_error("%s\n", error);
430 return -1;
431 }
432
433 return __cg_open(path, fi);
434 }
435
436 static int do_cg_access(const char *path, int mode)
437 {
438 int (*__cg_access)(const char *path, int mode);
439 char *error;
440 dlerror(); /* Clear any existing error */
441 __cg_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "cg_access");
442 error = dlerror();
443 if (error != NULL) {
444 lxcfs_error("%s\n", error);
445 return -1;
446 }
447
448 return __cg_access(path, mode);
449 }
450
451 static int do_proc_open(const char *path, struct fuse_file_info *fi)
452 {
453 int (*__proc_open)(const char *path, struct fuse_file_info *fi);
454 char *error;
455 dlerror(); /* Clear any existing error */
456 __proc_open = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "proc_open");
457 error = dlerror();
458 if (error != NULL) {
459 lxcfs_error("%s\n", error);
460 return -1;
461 }
462
463 return __proc_open(path, fi);
464 }
465
466 static int do_proc_access(const char *path, int mode)
467 {
468 int (*__proc_access)(const char *path, int mode);
469 char *error;
470 dlerror(); /* Clear any existing error */
471 __proc_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "proc_access");
472 error = dlerror();
473 if (error != NULL) {
474 lxcfs_error("%s\n", error);
475 return -1;
476 }
477
478 return __proc_access(path, mode);
479 }
480
481 static int do_sys_open(const char *path, struct fuse_file_info *fi)
482 {
483 int (*__sys_open)(const char *path, struct fuse_file_info *fi);
484 char *error;
485 dlerror(); /* Clear any existing error */
486 __sys_open = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "sys_open");
487 error = dlerror();
488 if (error != NULL) {
489 lxcfs_error("%s\n", error);
490 return -1;
491 }
492
493 return __sys_open(path, fi);
494 }
495
496 static int do_sys_access(const char *path, int mode)
497 {
498 int (*__sys_access)(const char *path, int mode);
499 char *error;
500 dlerror(); /* Clear any existing error */
501 __sys_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "sys_access");
502 error = dlerror();
503 if (error != NULL) {
504 lxcfs_error("%s\n", error);
505 return -1;
506 }
507
508 return __sys_access(path, mode);
509 }
510
511 static int do_cg_release(const char *path, struct fuse_file_info *fi)
512 {
513 int (*__cg_release)(const char *path, struct fuse_file_info *fi);
514 char *error;
515 dlerror(); /* Clear any existing error */
516 __cg_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_release");
517 error = dlerror();
518 if (error != NULL) {
519 lxcfs_error("%s\n", error);
520 return -1;
521 }
522
523 return __cg_release(path, fi);
524 }
525
526 static int do_proc_release(const char *path, struct fuse_file_info *fi)
527 {
528 int (*__proc_release)(const char *path, struct fuse_file_info *fi);
529 char *error;
530 dlerror(); /* Clear any existing error */
531 __proc_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_release");
532 error = dlerror();
533 if (error != NULL) {
534 lxcfs_error("%s\n", error);
535 return -1;
536 }
537
538 return __proc_release(path, fi);
539 }
540
541 static int do_sys_release(const char *path, struct fuse_file_info *fi)
542 {
543 int (*__sys_release)(const char *path, struct fuse_file_info *fi);
544 char *error;
545 dlerror(); /* Clear any existing error */
546 __sys_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "sys_release");
547 error = dlerror();
548 if (error != NULL) {
549 lxcfs_error("%s\n", error);
550 return -1;
551 }
552
553 return __sys_release(path, fi);
554 }
555
556 static int do_cg_opendir(const char *path, struct fuse_file_info *fi)
557 {
558 int (*__cg_opendir)(const char *path, struct fuse_file_info *fi);
559 char *error;
560 dlerror(); /* Clear any existing error */
561 __cg_opendir = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "cg_opendir");
562 error = dlerror();
563 if (error != NULL) {
564 lxcfs_error("%s\n", error);
565 return -1;
566 }
567
568 return __cg_opendir(path, fi);
569 }
570
571 static int do_cg_releasedir(const char *path, struct fuse_file_info *fi)
572 {
573 int (*__cg_releasedir)(const char *path, struct fuse_file_info *fi);
574 char *error;
575 dlerror(); /* Clear any existing error */
576 __cg_releasedir = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_releasedir");
577 error = dlerror();
578 if (error != NULL) {
579 lxcfs_error("%s\n", error);
580 return -1;
581 }
582
583 return __cg_releasedir(path, fi);
584 }
585
586 static int do_sys_releasedir(const char *path, struct fuse_file_info *fi)
587 {
588 int (*__sys_releasedir)(const char *path, struct fuse_file_info *fi);
589 char *error;
590 dlerror(); /* Clear any existing error */
591 __sys_releasedir = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "sys_releasedir");
592 error = dlerror();
593 if (error != NULL) {
594 lxcfs_error("%s\n", error);
595 return -1;
596 }
597
598 return __sys_releasedir(path, fi);
599 }
600
601 /*
602 * FUSE ops for /
603 * these just delegate to the /proc and /cgroup ops as
604 * needed
605 */
606
607 static int lxcfs_getattr(const char *path, struct stat *sb)
608 {
609 int ret;
610 struct timespec now;
611
612 if (strcmp(path, "/") == 0) {
613 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
614 return -EINVAL;
615 sb->st_uid = sb->st_gid = 0;
616 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
617 sb->st_size = 0;
618 sb->st_mode = S_IFDIR | 00755;
619 sb->st_nlink = 2;
620 return 0;
621 }
622
623 if (strncmp(path, "/cgroup", 7) == 0) {
624 up_users();
625 ret = do_cg_getattr(path, sb);
626 down_users();
627 return ret;
628 }
629 if (strncmp(path, "/proc", 5) == 0) {
630 up_users();
631 ret = do_proc_getattr(path, sb);
632 down_users();
633 return ret;
634 }
635 if (strncmp(path, "/sys", 4) == 0) {
636 up_users();
637 ret = do_sys_getattr(path, sb);
638 down_users();
639 return ret;
640 }
641 return -ENOENT;
642 }
643
644 static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
645 {
646 int ret;
647 if (strcmp(path, "/") == 0)
648 return 0;
649
650 if (strncmp(path, "/cgroup", 7) == 0) {
651 up_users();
652 ret = do_cg_opendir(path, fi);
653 down_users();
654 return ret;
655 }
656 if (strcmp(path, "/proc") == 0)
657 return 0;
658 if (strncmp(path, "/sys", 4) == 0)
659 return 0;
660
661 return -ENOENT;
662 }
663
664 static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
665 struct fuse_file_info *fi)
666 {
667 int ret;
668 if (strcmp(path, "/") == 0) {
669 if (filler(buf, ".", NULL, 0) != 0 ||
670 filler(buf, "..", NULL, 0) != 0 ||
671 filler(buf, "proc", NULL, 0) != 0 ||
672 filler(buf, "sys", NULL, 0) != 0 ||
673 filler(buf, "cgroup", NULL, 0) != 0)
674 return -ENOMEM;
675 return 0;
676 }
677 if (strncmp(path, "/cgroup", 7) == 0) {
678 up_users();
679 ret = do_cg_readdir(path, buf, filler, offset, fi);
680 down_users();
681 return ret;
682 }
683 if (strcmp(path, "/proc") == 0) {
684 up_users();
685 ret = do_proc_readdir(path, buf, filler, offset, fi);
686 down_users();
687 return ret;
688 }
689
690 if (strncmp(path, "/sys", 4) == 0) {
691 up_users();
692 ret = do_sys_readdir(path, buf, filler, offset, fi);
693 down_users();
694 return ret;
695 }
696
697 return -ENOENT;
698 }
699
700 static int lxcfs_access(const char *path, int mode)
701 {
702 int ret;
703
704 if (strcmp(path, "/") == 0 && (mode & W_OK) == 0)
705 return 0;
706
707 if (strncmp(path, "/cgroup", 7) == 0) {
708 up_users();
709 ret = do_cg_access(path, mode);
710 down_users();
711 return ret;
712 }
713 if (strncmp(path, "/proc", 5) == 0) {
714 up_users();
715 ret = do_proc_access(path, mode);
716 down_users();
717 return ret;
718 }
719 if (strncmp(path, "/sys", 4) == 0) {
720 up_users();
721 ret = do_sys_access(path, mode);
722 down_users();
723 return ret;
724 }
725
726
727 return -EACCES;
728 }
729
730 static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
731 {
732 int ret;
733 if (strcmp(path, "/") == 0)
734 return 0;
735 if (strncmp(path, "/cgroup", 7) == 0) {
736 up_users();
737 ret = do_cg_releasedir(path, fi);
738 down_users();
739 return ret;
740 }
741 if (strcmp(path, "/proc") == 0)
742 return 0;
743 if (strncmp(path, "/sys", 4) == 0){
744 up_users();
745 ret = do_sys_releasedir(path, fi);
746 down_users();
747 return ret;
748 }
749
750 return -EINVAL;
751 }
752
753 static int lxcfs_open(const char *path, struct fuse_file_info *fi)
754 {
755 int ret;
756 if (strncmp(path, "/cgroup", 7) == 0) {
757 up_users();
758 ret = do_cg_open(path, fi);
759 down_users();
760 return ret;
761 }
762 if (strncmp(path, "/proc", 5) == 0) {
763 up_users();
764 ret = do_proc_open(path, fi);
765 down_users();
766 return ret;
767 }
768 if (strncmp(path, "/sys", 4) == 0) {
769 up_users();
770 ret = do_sys_open(path, fi);
771 down_users();
772 return ret;
773 }
774
775
776 return -EACCES;
777 }
778
779 static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
780 struct fuse_file_info *fi)
781 {
782 int ret;
783 if (strncmp(path, "/cgroup", 7) == 0) {
784 up_users();
785 ret = do_cg_read(path, buf, size, offset, fi);
786 down_users();
787 return ret;
788 }
789 if (strncmp(path, "/proc", 5) == 0) {
790 up_users();
791 ret = do_proc_read(path, buf, size, offset, fi);
792 down_users();
793 return ret;
794 }
795 if (strncmp(path, "/sys", 4) == 0) {
796 up_users();
797 ret = do_sys_read(path, buf, size, offset, fi);
798 down_users();
799 return ret;
800 }
801
802
803 return -EINVAL;
804 }
805
806 int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
807 struct fuse_file_info *fi)
808 {
809 int ret;
810 if (strncmp(path, "/cgroup", 7) == 0) {
811 up_users();
812 ret = do_cg_write(path, buf, size, offset, fi);
813 down_users();
814 return ret;
815 }
816
817 return -EINVAL;
818 }
819
820 static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
821 {
822 return 0;
823 }
824
825 static int lxcfs_release(const char *path, struct fuse_file_info *fi)
826 {
827 int ret;
828 if (strncmp(path, "/cgroup", 7) == 0) {
829 up_users();
830 ret = do_cg_release(path, fi);
831 down_users();
832 return ret;
833 }
834 if (strncmp(path, "/proc", 5) == 0) {
835 up_users();
836 ret = do_proc_release(path, fi);
837 down_users();
838 return ret;
839 }
840 if (strncmp(path, "/sys", 4) == 0) {
841 up_users();
842 ret = do_sys_release(path, fi);
843 down_users();
844 return ret;
845 }
846
847
848 return -EINVAL;
849 }
850
851 static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
852 {
853 return 0;
854 }
855
856 int lxcfs_mkdir(const char *path, mode_t mode)
857 {
858 int ret;
859 if (strncmp(path, "/cgroup", 7) == 0) {
860 up_users();
861 ret = do_cg_mkdir(path, mode);
862 down_users();
863 return ret;
864 }
865
866 return -EPERM;
867 }
868
869 int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
870 {
871 int ret;
872 if (strncmp(path, "/cgroup", 7) == 0) {
873 up_users();
874 ret = do_cg_chown(path, uid, gid);
875 down_users();
876 return ret;
877 }
878
879 if (strncmp(path, "/proc", 5) == 0)
880 return -EPERM;
881
882 if (strncmp(path, "/sys", 4) == 0)
883 return -EPERM;
884 return -ENOENT;
885 }
886
887 /*
888 * cat first does a truncate before doing ops->write. This doesn't
889 * really make sense for cgroups. So just return 0 always but do
890 * nothing.
891 */
892 int lxcfs_truncate(const char *path, off_t newsize)
893 {
894 if (strncmp(path, "/cgroup", 7) == 0)
895 return 0;
896 return -EPERM;
897 }
898
899 int lxcfs_rmdir(const char *path)
900 {
901 int ret;
902 if (strncmp(path, "/cgroup", 7) == 0) {
903 up_users();
904 ret = do_cg_rmdir(path);
905 down_users();
906 return ret;
907 }
908 return -EPERM;
909 }
910
911 int lxcfs_chmod(const char *path, mode_t mode)
912 {
913 int ret;
914 if (strncmp(path, "/cgroup", 7) == 0) {
915 up_users();
916 ret = do_cg_chmod(path, mode);
917 down_users();
918 return ret;
919 }
920
921 if (strncmp(path, "/proc", 5) == 0)
922 return -EPERM;
923
924 if (strncmp(path, "/sys", 4) == 0)
925 return -EPERM;
926
927 return -ENOENT;
928 }
929
930 const struct fuse_operations lxcfs_ops = {
931 .getattr = lxcfs_getattr,
932 .readlink = NULL,
933 .getdir = NULL,
934 .mknod = NULL,
935 .mkdir = lxcfs_mkdir,
936 .unlink = NULL,
937 .rmdir = lxcfs_rmdir,
938 .symlink = NULL,
939 .rename = NULL,
940 .link = NULL,
941 .chmod = lxcfs_chmod,
942 .chown = lxcfs_chown,
943 .truncate = lxcfs_truncate,
944 .utime = NULL,
945
946 .open = lxcfs_open,
947 .read = lxcfs_read,
948 .release = lxcfs_release,
949 .write = lxcfs_write,
950
951 .statfs = NULL,
952 .flush = lxcfs_flush,
953 .fsync = lxcfs_fsync,
954
955 .setxattr = NULL,
956 .getxattr = NULL,
957 .listxattr = NULL,
958 .removexattr = NULL,
959
960 .opendir = lxcfs_opendir,
961 .readdir = lxcfs_readdir,
962 .releasedir = lxcfs_releasedir,
963
964 .fsyncdir = NULL,
965 .init = NULL,
966 .destroy = NULL,
967 .access = lxcfs_access,
968 .create = NULL,
969 .ftruncate = NULL,
970 .fgetattr = NULL,
971 };
972
973 static void usage()
974 {
975 fprintf(stderr, "Usage:\n");
976 fprintf(stderr, "\n");
977 fprintf(stderr, "lxcfs [-f|-d] -u -l -n [-p pidfile] mountpoint\n");
978 fprintf(stderr, " -f running foreground by default; -d enable debug output \n");
979 fprintf(stderr, " -l use loadavg \n");
980 fprintf(stderr, " -u no swap \n");
981 fprintf(stderr, " Default pidfile is %s/lxcfs.pid\n", RUNTIME_PATH);
982 fprintf(stderr, "lxcfs -h\n");
983 fprintf(stderr, "lxcfs -v\n");
984 exit(1);
985 }
986
987 static bool is_help(char *w)
988 {
989 if (strcmp(w, "-h") == 0 ||
990 strcmp(w, "--help") == 0 ||
991 strcmp(w, "-help") == 0 ||
992 strcmp(w, "help") == 0)
993 return true;
994 return false;
995 }
996
997 static bool is_version(char *w)
998 {
999 if (strcmp(w, "-v") == 0 ||
1000 strcmp(w, "--version") == 0 ||
1001 strcmp(w, "-version") == 0 ||
1002 strcmp(w, "version") == 0)
1003 return true;
1004 return false;
1005 }
1006
1007 bool swallow_arg(int *argcp, char *argv[], char *which)
1008 {
1009 int i;
1010
1011 for (i = 1; argv[i]; i++) {
1012 if (strcmp(argv[i], which) != 0)
1013 continue;
1014 for (; argv[i]; i++) {
1015 argv[i] = argv[i+1];
1016 }
1017 (*argcp)--;
1018 return true;
1019 }
1020 return false;
1021 }
1022
1023 bool swallow_option(int *argcp, char *argv[], char *opt, char **v)
1024 {
1025 int i;
1026
1027 for (i = 1; argv[i]; i++) {
1028 if (!argv[i+1])
1029 continue;
1030 if (strcmp(argv[i], opt) != 0)
1031 continue;
1032 do {
1033 *v = strdup(argv[i+1]);
1034 } while (!*v);
1035 for (; argv[i+1]; i++) {
1036 argv[i] = argv[i+2];
1037 }
1038 (*argcp) -= 2;
1039 return true;
1040 }
1041 return false;
1042 }
1043
1044 static int set_pidfile(char *pidfile)
1045 {
1046 int fd;
1047 char buf[50];
1048 struct flock fl;
1049
1050 fl.l_type = F_WRLCK;
1051 fl.l_whence = SEEK_SET;
1052 fl.l_start = 0;
1053 fl.l_len = 0;
1054
1055 fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1056 if (fd == -1) {
1057 fprintf(stderr, "Could not open pidfile %s: %m\n", pidfile);
1058 return -1;
1059 }
1060
1061 if (fcntl(fd, F_SETLK, &fl) == -1) {
1062 if (errno == EAGAIN || errno == EACCES) {
1063 fprintf(stderr, "PID file '%s' is already locked.\n", pidfile);
1064 close(fd);
1065 return -1;
1066 }
1067 fprintf(stderr, "Warning; unable to lock PID file, proceeding.\n");
1068 }
1069
1070 if (ftruncate(fd, 0) == -1) {
1071 fprintf(stderr, "Error truncating PID file '%s': %m", pidfile);
1072 close(fd);
1073 return -1;
1074 }
1075
1076 snprintf(buf, 50, "%ld\n", (long) getpid());
1077 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
1078 fprintf(stderr, "Error writing to PID file '%s': %m", pidfile);
1079 close(fd);
1080 return -1;
1081 }
1082
1083 return fd;
1084 }
1085
1086 int main(int argc, char *argv[])
1087 {
1088 int ret = EXIT_FAILURE;
1089 int pidfd = -1;
1090 char *pidfile = NULL, *saveptr = NULL, *token = NULL, *v = NULL;
1091 size_t pidfile_len;
1092 bool debug = false, nonempty = false;
1093 bool load_use = false;
1094 /*
1095 * what we pass to fuse_main is:
1096 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1097 */
1098 int nargs = 5, cnt = 0;
1099 char *newargv[6];
1100 struct lxcfs_opts *opts;
1101
1102 opts = malloc(sizeof(struct lxcfs_opts));
1103 if (opts == NULL) {
1104 fprintf(stderr, "Error allocating memory for options.\n");
1105 goto out;
1106 }
1107 opts->swap_off = false;
1108 opts->use_pidfd = false;
1109
1110 /* accomodate older init scripts */
1111 swallow_arg(&argc, argv, "-s");
1112 swallow_arg(&argc, argv, "-f");
1113 debug = swallow_arg(&argc, argv, "-d");
1114 if (swallow_arg(&argc, argv, "-l"))
1115 load_use = true;
1116 if (swallow_arg(&argc, argv, "-u"))
1117 opts->swap_off = true;
1118
1119 if (swallow_arg(&argc, argv, "--pidfd"))
1120 opts->use_pidfd = true;
1121
1122 if (swallow_option(&argc, argv, "-o", &v)) {
1123 /* Parse multiple values */
1124 for (; (token = strtok_r(v, ",", &saveptr)); v = NULL) {
1125 if (strcmp(token, "allow_other") == 0) {
1126 /* Noop. this is the default. Always enabled. */
1127 } else if (strcmp(token, "nonempty") == 0) {
1128 nonempty = true;
1129 } else {
1130 free(v);
1131 fprintf(stderr, "Warning: unexpected fuse option %s\n", v);
1132 exit(EXIT_FAILURE);
1133 }
1134 }
1135 free(v);
1136 v = NULL;
1137 }
1138 if (swallow_option(&argc, argv, "-p", &v))
1139 pidfile = v;
1140
1141 if (argc == 2 && is_version(argv[1])) {
1142 fprintf(stderr, "%s\n", VERSION);
1143 exit(EXIT_SUCCESS);
1144 }
1145
1146 if (argc != 2 || is_help(argv[1]))
1147 usage();
1148
1149 do_reload();
1150 if (signal(SIGUSR1, reload_handler) == SIG_ERR) {
1151 fprintf(stderr, "Error setting USR1 signal handler: %m\n");
1152 goto out;
1153 }
1154
1155 newargv[cnt++] = argv[0];
1156 if (debug)
1157 newargv[cnt++] = "-d";
1158 else
1159 newargv[cnt++] = "-f";
1160 newargv[cnt++] = "-o";
1161 if (nonempty)
1162 newargv[cnt++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5,nonempty";
1163 else
1164 newargv[cnt++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5";
1165 newargv[cnt++] = argv[1];
1166 newargv[cnt++] = NULL;
1167
1168 if (!pidfile) {
1169 pidfile_len = strlen(RUNTIME_PATH) + strlen("/lxcfs.pid") + 1;
1170 pidfile = alloca(pidfile_len);
1171 snprintf(pidfile, pidfile_len, "%s/lxcfs.pid", RUNTIME_PATH);
1172 }
1173 if ((pidfd = set_pidfile(pidfile)) < 0)
1174 goto out;
1175
1176 if (load_use && start_loadavg() != 0)
1177 goto out;
1178
1179 if (!fuse_main(nargs, newargv, &lxcfs_ops, opts))
1180 ret = EXIT_SUCCESS;
1181 if (load_use)
1182 stop_loadavg();
1183
1184 out:
1185 if (dlopen_handle)
1186 dlclose(dlopen_handle);
1187 if (pidfile)
1188 unlink(pidfile);
1189 if (pidfd > 0)
1190 close(pidfd);
1191 exit(ret);
1192 }