1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #ifndef FUSE_USE_VERSION
11 #define FUSE_USE_VERSION 30
14 #ifndef FUSE_USE_VERSION
15 #define FUSE_USE_VERSION 26
19 #define _FILE_OFFSET_BITS 64
38 #include <linux/sched.h>
39 #include <sys/epoll.h>
40 #include <sys/mount.h>
41 #include <sys/socket.h>
42 #include <linux/limits.h>
45 #include "lxcfs_fuse_compat.h"
47 #include "memory_utils.h"
51 /* Functions to keep track of number of threads using the library */
53 static int users_count
;
54 static pthread_mutex_t user_count_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 static void lock_mutex(pthread_mutex_t
*l
)
59 ret
= pthread_mutex_lock(l
);
61 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
64 static void unlock_mutex(pthread_mutex_t
*l
)
68 ret
= pthread_mutex_unlock(l
);
70 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
73 static inline void users_lock(void)
75 lock_mutex(&user_count_mutex
);
78 static inline void users_unlock(void)
80 unlock_mutex(&user_count_mutex
);
83 static pthread_t loadavg_pid
= 0;
85 /* Returns zero on success */
86 static int start_loadavg(void)
89 pthread_t (*__load_daemon
)(int);
92 __load_daemon
= (pthread_t(*)(int))dlsym(dlopen_handle
, "load_daemon");
95 return log_error(-1, "%s - Failed to start loadavg daemon", error
);
97 loadavg_pid
= __load_daemon(1);
104 /* Returns zero on success */
105 static int stop_loadavg(void)
108 int (*__stop_load_daemon
)(pthread_t
);
110 __stop_load_daemon
= (int (*)(pthread_t
))dlsym(dlopen_handle
, "stop_load_daemon");
113 return log_error(-1, "%s - Failed to stop loadavg daemon", error
);
115 if (__stop_load_daemon(loadavg_pid
))
121 static volatile sig_atomic_t need_reload
;
123 /* do_reload - reload the dynamic library. Done under
124 * lock and when we know the user_count was 0 */
125 static void do_reload(void)
128 char lxcfs_lib_path
[PATH_MAX
];
134 lxcfs_info("Closed liblxcfs.so");
135 dlclose(dlopen_handle
);
138 /* First try loading using ld.so */
140 dlopen_handle
= dlopen("liblxcfs.so", RTLD_NOW
);
142 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
145 lxcfs_debug("Opened liblxcfs.so");
150 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
151 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
153 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
155 if (ret
< 0 || ret
>= sizeof(lxcfs_lib_path
))
156 log_exit("Failed to create path to open liblxcfs");
158 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
160 log_exit("%s - Failed to open liblxcfs.so", dlerror());
162 lxcfs_debug("Opened %s", lxcfs_lib_path
);
169 lxcfs_info("Reloaded LXCFS");
173 static void up_users(void)
176 if (users_count
== 0 && need_reload
)
182 static void down_users(void)
189 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
194 /* Functions to run the library methods */
195 static int do_cg_getattr(const char *path
, struct stat
*sb
)
198 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
201 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
204 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
206 return __cg_getattr(path
, sb
);
209 static int do_proc_getattr(const char *path
, struct stat
*sb
)
212 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
215 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
218 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
220 return __proc_getattr(path
, sb
);
223 static int do_sys_getattr(const char *path
, struct stat
*sb
)
226 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
229 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
232 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
234 return __sys_getattr(path
, sb
);
237 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
238 struct fuse_file_info
*fi
)
241 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
242 struct fuse_file_info
*fi
);
245 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
248 return log_error(-1, "%s - Failed to find cg_read()", error
);
250 return __cg_read(path
, buf
, size
, offset
, fi
);
253 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
254 struct fuse_file_info
*fi
)
257 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
258 off_t offset
, struct fuse_file_info
*fi
);
261 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
264 return log_error(-1, "%s - Failed to find proc_read()", error
);
266 return __proc_read(path
, buf
, size
, offset
, fi
);
269 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
270 struct fuse_file_info
*fi
)
273 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
274 off_t offset
, struct fuse_file_info
*fi
);
277 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
280 return log_error(-1, "%s - Failed to find sys_read()", error
);
282 return __sys_read(path
, buf
, size
, offset
, fi
);
285 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
286 off_t offset
, struct fuse_file_info
*fi
)
289 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
290 off_t offset
, struct fuse_file_info
*fi
);
293 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
296 return log_error(-1, "%s - Failed to find cg_write()", error
);
298 return __cg_write(path
, buf
, size
, offset
, fi
);
301 static int do_sys_write(const char *path
, const char *buf
, size_t size
,
302 off_t offset
, struct fuse_file_info
*fi
)
305 int (*__sys_write
)(const char *path
, const char *buf
, size_t size
,
306 off_t offset
, struct fuse_file_info
*fi
);
309 __sys_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_write");
312 return log_error(-1, "%s - Failed to find sys_write()", error
);
314 return __sys_write(path
, buf
, size
, offset
, fi
);
317 static int do_cg_mkdir(const char *path
, mode_t mode
)
320 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
323 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
326 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
328 return __cg_mkdir(path
, mode
);
331 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
334 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
337 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
340 return log_error(-1, "%s - Failed to find cg_chown()", error
);
342 return __cg_chown(path
, uid
, gid
);
345 static int do_cg_rmdir(const char *path
)
348 int (*__cg_rmdir
)(const char *path
);
351 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
354 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
356 return __cg_rmdir(path
);
359 static int do_cg_chmod(const char *path
, mode_t mode
)
362 int (*__cg_chmod
)(const char *path
, mode_t mode
);
365 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
368 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
370 return __cg_chmod(path
, mode
);
373 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
374 off_t offset
, struct fuse_file_info
*fi
)
377 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
378 off_t offset
, struct fuse_file_info
*fi
);
381 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
384 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
386 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
389 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
390 off_t offset
, struct fuse_file_info
*fi
)
393 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
394 off_t offset
, struct fuse_file_info
*fi
);
397 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
400 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
402 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
405 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
406 off_t offset
, struct fuse_file_info
*fi
)
409 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
410 off_t offset
, struct fuse_file_info
*fi
);
413 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
416 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
418 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
421 static int do_sys_readlink(const char *path
, char *buf
, size_t size
)
424 int (*__sys_readlink
)(const char *path
, char *buf
, size_t size
);
427 __sys_readlink
= (int (*)(const char *, char *, size_t))dlsym(dlopen_handle
, "sys_readlink");
430 return log_error(-1, "%s - Failed to find sys_readlink()", error
);
432 return __sys_readlink(path
, buf
, size
);
435 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
438 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
441 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
444 return log_error(-1, "%s - Failed to find cg_open()", error
);
446 return __cg_open(path
, fi
);
449 static int do_cg_access(const char *path
, int mode
)
452 int (*__cg_access
)(const char *path
, int mode
);
455 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
458 return log_error(-1, "%s - Failed to find cg_access()", error
);
460 return __cg_access(path
, mode
);
463 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
466 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
469 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
472 return log_error(-1, "%s - Failed to find proc_open()", error
);
474 return __proc_open(path
, fi
);
477 static int do_proc_access(const char *path
, int mode
)
480 int (*__proc_access
)(const char *path
, int mode
);
483 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
486 return log_error(-1, "%s - Failed to find proc_access()", error
);
488 return __proc_access(path
, mode
);
491 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
494 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
497 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
500 return log_error(-1, "%s - Failed to find sys_open()", error
);
502 return __sys_open(path
, fi
);
505 static int do_sys_opendir(const char *path
, struct fuse_file_info
*fi
)
508 int (*__sys_opendir
)(const char *path
, struct fuse_file_info
*fi
);
511 __sys_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_opendir");
514 return log_error(-1, "%s - Failed to find sys_opendir()", error
);
516 return __sys_opendir(path
, fi
);
519 static int do_sys_access(const char *path
, int mode
)
522 int (*__sys_access
)(const char *path
, int mode
);
525 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
528 return log_error(-1, "%s - Failed to find sys_access()", error
);
530 return __sys_access(path
, mode
);
533 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
536 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
539 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
542 return log_error(-1, "%s - Failed to find cg_release()", error
);
544 return __cg_release(path
, fi
);
547 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
550 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
553 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
556 return log_error(-1, "%s - Failed to find proc_release()", error
);
558 return __proc_release(path
, fi
);
561 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
564 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
567 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
570 return log_error(-1, "%s - Failed to find sys_release()", error
);
572 return __sys_release(path
, fi
);
575 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
578 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
581 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
584 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
586 return __cg_opendir(path
, fi
);
589 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
592 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
595 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
598 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
600 return __cg_releasedir(path
, fi
);
603 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
606 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
609 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
612 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
614 return __sys_releasedir(path
, fi
);
618 static int lxcfs_getattr(const char *path
, struct stat
*sb
, struct fuse_file_info
*fi
)
620 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
626 if (strcmp(path
, "/") == 0) {
627 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
629 sb
->st_uid
= sb
->st_gid
= 0;
630 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
632 sb
->st_mode
= S_IFDIR
| 00755;
637 if (strncmp(path
, "/cgroup", 7) == 0) {
639 ret
= do_cg_getattr(path
, sb
);
644 if (strncmp(path
, "/proc", 5) == 0) {
646 ret
= do_proc_getattr(path
, sb
);
651 if (strncmp(path
, "/sys", 4) == 0) {
653 ret
= do_sys_getattr(path
, sb
);
661 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
665 if (strcmp(path
, "/") == 0)
668 if (strncmp(path
, "/cgroup", 7) == 0) {
670 ret
= do_cg_opendir(path
, fi
);
675 if (strcmp(path
, "/proc") == 0)
678 if (strncmp(path
, "/sys", 4) == 0) {
680 ret
= do_sys_opendir(path
, fi
);
689 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
690 off_t offset
, struct fuse_file_info
*fi
, enum fuse_readdir_flags flags
)
692 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
693 off_t offset
, struct fuse_file_info
*fi
)
698 if (strcmp(path
, "/") == 0) {
699 if (DIR_FILLER(filler
, buf
, ".", NULL
, 0) != 0 ||
700 DIR_FILLER(filler
, buf
, "..", NULL
, 0) != 0 ||
701 DIR_FILLER(filler
, buf
, "proc", NULL
, 0) != 0 ||
702 DIR_FILLER(filler
, buf
, "sys", NULL
, 0) != 0 ||
703 DIR_FILLER(filler
, buf
, "cgroup", NULL
, 0) != 0)
709 if (strncmp(path
, "/cgroup", 7) == 0) {
711 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
716 if (strcmp(path
, "/proc") == 0) {
718 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
723 if (strncmp(path
, "/sys", 4) == 0) {
725 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
733 static int lxcfs_access(const char *path
, int mode
)
737 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
740 if (strncmp(path
, "/cgroup", 7) == 0) {
742 ret
= do_cg_access(path
, mode
);
747 if (strncmp(path
, "/proc", 5) == 0) {
749 ret
= do_proc_access(path
, mode
);
754 if (strncmp(path
, "/sys", 4) == 0) {
756 ret
= do_sys_access(path
, mode
);
764 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
768 if (strcmp(path
, "/") == 0)
771 if (strncmp(path
, "/cgroup", 7) == 0) {
773 ret
= do_cg_releasedir(path
, fi
);
778 if (strcmp(path
, "/proc") == 0)
781 if (strncmp(path
, "/sys", 4) == 0) {
783 ret
= do_sys_releasedir(path
, fi
);
791 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
795 if (strncmp(path
, "/cgroup", 7) == 0) {
797 ret
= do_cg_open(path
, fi
);
802 if (strncmp(path
, "/proc", 5) == 0) {
804 ret
= do_proc_open(path
, fi
);
809 if (strncmp(path
, "/sys", 4) == 0) {
811 ret
= do_sys_open(path
, fi
);
819 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
820 struct fuse_file_info
*fi
)
824 if (strncmp(path
, "/cgroup", 7) == 0) {
826 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
831 if (strncmp(path
, "/proc", 5) == 0) {
833 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
838 if (strncmp(path
, "/sys", 4) == 0) {
840 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
848 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
849 struct fuse_file_info
*fi
)
853 if (strncmp(path
, "/cgroup", 7) == 0) {
855 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
860 if (strncmp(path
, "/sys", 4) == 0) {
862 ret
= do_sys_write(path
, buf
, size
, offset
, fi
);
870 int lxcfs_readlink(const char *path
, char *buf
, size_t size
)
874 if (strncmp(path
, "/sys", 4) == 0) {
876 ret
= do_sys_readlink(path
, buf
, size
);
884 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
889 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
893 if (strncmp(path
, "/cgroup", 7) == 0) {
895 ret
= do_cg_release(path
, fi
);
900 if (strncmp(path
, "/proc", 5) == 0) {
902 ret
= do_proc_release(path
, fi
);
907 if (strncmp(path
, "/sys", 4) == 0) {
909 ret
= do_sys_release(path
, fi
);
917 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
922 int lxcfs_mkdir(const char *path
, mode_t mode
)
926 if (strncmp(path
, "/cgroup", 7) == 0) {
928 ret
= do_cg_mkdir(path
, mode
);
937 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
, struct fuse_file_info
*fi
)
939 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
944 if (strncmp(path
, "/cgroup", 7) == 0) {
946 ret
= do_cg_chown(path
, uid
, gid
);
951 if (strncmp(path
, "/proc", 5) == 0)
954 if (strncmp(path
, "/sys", 4) == 0)
961 * cat first does a truncate before doing ops->write. This doesn't
962 * really make sense for cgroups. So just return 0 always but do
966 int lxcfs_truncate(const char *path
, off_t newsize
, struct fuse_file_info
*fi
)
968 int lxcfs_truncate(const char *path
, off_t newsize
)
971 if (strncmp(path
, "/cgroup", 7) == 0)
974 if (strncmp(path
, "/sys", 4) == 0)
980 int lxcfs_rmdir(const char *path
)
984 if (strncmp(path
, "/cgroup", 7) == 0) {
986 ret
= do_cg_rmdir(path
);
995 int lxcfs_chmod(const char *path
, mode_t mode
, struct fuse_file_info
*fi
)
997 int lxcfs_chmod(const char *path
, mode_t mode
)
1002 if (strncmp(path
, "/cgroup", 7) == 0) {
1004 ret
= do_cg_chmod(path
, mode
);
1009 if (strncmp(path
, "/proc", 5) == 0)
1012 if (strncmp(path
, "/sys", 4) == 0)
1019 static void *lxcfs_init(struct fuse_conn_info
*conn
, struct fuse_config
*cfg
)
1021 static void *lxcfs_init(struct fuse_conn_info
*conn
)
1025 void *(*__lxcfs_fuse_init
)(struct fuse_conn_info
* conn
, void * cfg
);
1028 __lxcfs_fuse_init
= (void *(*)(struct fuse_conn_info
* conn
, void * cfg
))dlsym(dlopen_handle
, "lxcfs_fuse_init");
1031 return log_error(NULL
, "%s - Failed to find lxcfs_fuse_init()", error
);
1033 return __lxcfs_fuse_init(conn
, NULL
);
1036 const struct fuse_operations lxcfs_ops
= {
1037 .access
= lxcfs_access
,
1038 .chmod
= lxcfs_chmod
,
1039 .chown
= lxcfs_chown
,
1040 .flush
= lxcfs_flush
,
1041 .fsync
= lxcfs_fsync
,
1042 .getattr
= lxcfs_getattr
,
1044 .mkdir
= lxcfs_mkdir
,
1046 .opendir
= lxcfs_opendir
,
1048 .readdir
= lxcfs_readdir
,
1049 .release
= lxcfs_release
,
1050 .releasedir
= lxcfs_releasedir
,
1051 .rmdir
= lxcfs_rmdir
,
1052 .truncate
= lxcfs_truncate
,
1053 .write
= lxcfs_write
,
1054 .readlink
= lxcfs_readlink
,
1071 .removexattr
= NULL
,
1083 lxcfs_info("Usage: lxcfs <directory>\n");
1084 lxcfs_info("lxcfs is a FUSE-based proc, sys and cgroup virtualizing filesystem\n");
1085 lxcfs_info("Options :");
1086 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
1087 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
1088 lxcfs_info(" -n, --help Print help");
1089 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
1090 lxcfs_info(" -o Options to pass directly through fuse");
1091 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
1092 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH
);
1093 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
1094 lxcfs_info(" -v, --version Print lxcfs version");
1095 lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares");
1096 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
1100 static int set_pidfile(char *pidfile
)
1102 __do_close
int fd
= -EBADF
;
1103 char buf
[INTTYPE_TO_STRLEN(long)];
1107 .l_whence
= SEEK_SET
,
1112 fd
= open(pidfile
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| O_CLOEXEC
);
1114 return log_error(-1, "Could not open pidfile %s: %m", pidfile
);
1116 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
1117 if (errno
== EAGAIN
|| errno
== EACCES
)
1118 return log_error(-1, "PID file '%s' is already locked", pidfile
);
1119 lxcfs_error("Warning; unable to lock PID file, proceeding");
1122 if (ftruncate(fd
, 0))
1123 return log_error(-1, "Error truncating PID file '%s': %m", pidfile
);
1125 ret
= snprintf(buf
, sizeof(buf
), "%ld\n", (long)getpid());
1126 if (ret
< 0 || ret
>= sizeof(buf
))
1127 return log_error(-1, "Failed to convert pid to string %m");
1129 if (write(fd
, buf
, ret
) != ret
)
1130 return log_error(-1, "Error writing to PID file '%s': %m", pidfile
);
1135 static const struct option long_options
[] = {
1136 {"debug", no_argument
, 0, 'd' },
1137 {"disable-swap", no_argument
, 0, 'u' },
1138 {"enable-loadavg", no_argument
, 0, 'l' },
1139 {"foreground", no_argument
, 0, 'f' },
1140 {"help", no_argument
, 0, 'h' },
1141 {"version", no_argument
, 0, 'v' },
1143 {"enable-cfs", no_argument
, 0, 0 },
1144 {"enable-pidfd", no_argument
, 0, 0 },
1146 {"pidfile", required_argument
, 0, 'p' },
1150 static int append_comma_separate(char **s
, const char *append
)
1154 size_t append_len
, len
;
1159 append_len
= strlen(append
);
1165 news
= realloc(*s
, len
+ append_len
+ 2);
1168 news
= realloc(NULL
, append_len
+ 1);
1174 ret
= snprintf(news
+ len
, append_len
+ 2, ",%s", append
);
1176 ret
= snprintf(news
, append_len
+ 1, "%s", append
);
1184 int main(int argc
, char *argv
[])
1186 int pidfile_fd
= -EBADF
;
1187 int ret
= EXIT_FAILURE
;
1188 char *pidfile
= NULL
, *token
= NULL
;
1189 char pidfile_buf
[STRLITERALLEN(RUNTIME_PATH
) + STRLITERALLEN("/lxcfs.pid") + 1] = {};
1190 bool debug
= false, foreground
= false;
1192 bool nonempty
= false;
1194 bool load_use
= false;
1196 * what we pass to fuse_main is:
1197 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1200 int c
, idx
, new_argc
;
1202 const char *fuse_opts
= NULL
;
1203 char *new_fuse_opts
= NULL
;
1204 char *const *new_argv
;
1205 struct lxcfs_opts
*opts
;
1207 opts
= malloc(sizeof(struct lxcfs_opts
));
1209 lxcfs_error("Error allocating memory for options");
1213 opts
->swap_off
= false;
1214 opts
->use_pidfd
= false;
1215 opts
->use_cfs
= false;
1218 while ((c
= getopt_long(argc
, argv
, "dulfhvso:p:", long_options
, &idx
)) != -1) {
1221 if (strcmp(long_options
[idx
].name
, "enable-pidfd") == 0)
1222 opts
->use_pidfd
= true;
1223 else if (strcmp(long_options
[idx
].name
, "enable-cfs") == 0)
1224 opts
->use_cfs
= true;
1239 lxcfs_error("Specifying -o multiple times is unsupported");
1249 /* legacy argument: ignore */
1252 opts
->swap_off
= true;
1259 if (foreground
&& debug
)
1260 log_exit("Both --debug and --forgreound specified");
1262 new_argv
= &argv
[optind
];
1263 new_argc
= argc
- optind
;
1264 if (new_argc
!= 1) {
1265 lxcfs_error("Missing mountpoint");
1269 fuse_argv
[fuse_argc
++] = argv
[0];
1271 fuse_argv
[fuse_argc
++] = "-d";
1273 fuse_argv
[fuse_argc
++] = "-f";
1274 fuse_argv
[fuse_argc
++] = "-o";
1276 /* Parse additional fuse options. */
1280 dup
= strdup(fuse_opts
);
1282 lxcfs_error("Failed to copy fuse options");
1286 lxc_iterate_parts(token
, dup
, ",") {
1288 if (strcmp(token
, "allow_other") == 0)
1291 /* default for LXCFS */
1292 if (strcmp(token
, "direct_io") == 0)
1295 /* default for LXCFS */
1296 if (strncmp(token
, "entry_timeout", STRLITERALLEN("entry_timeout")) == 0)
1299 /* default for LXCFS */
1300 if (strncmp(token
, "attr_timeout", STRLITERALLEN("entry_timeout")) == 0)
1303 /* default for LXCFS */
1304 if (strncmp(token
, "allow_other", STRLITERALLEN("allow_other")) == 0)
1307 /* default with fuse3 */
1308 if (strcmp(token
, "nonempty") == 0) {
1315 if (append_comma_separate(&new_fuse_opts
, token
)) {
1316 lxcfs_error("Failed to copy fuse argument \"%s\"", token
);
1324 if (append_comma_separate(&new_fuse_opts
, "allow_other,entry_timeout=0.5,attr_timeout=0.5")) {
1325 lxcfs_error("Failed to copy fuse argument \"allow_other,entry_timeout=0.5,attr_timeout=0.5\"");
1331 if (append_comma_separate(&new_fuse_opts
, "nonempty")) {
1332 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1337 if (append_comma_separate(&new_fuse_opts
, "direct_io")) {
1338 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1344 * We can't use default_permissions since we still support systems that
1345 * don't have kernels with cgroup namespace support. On such kernels
1346 * lxcfs will provide a namespaced cgroup view and needs explicit
1347 * access helpers to make that work.
1348 * Another reason that came to me is that we can't or at least
1349 * shouldn't guarantee that we don't need more complicated access
1350 * helpers for proc and sys virtualization in the future.
1353 fuse_argv
[fuse_argc
++] = new_fuse_opts
;
1354 fuse_argv
[fuse_argc
++] = new_argv
[0];
1355 fuse_argv
[fuse_argc
] = NULL
;
1358 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1359 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1364 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1365 pidfile
= pidfile_buf
;
1368 pidfile_fd
= set_pidfile(pidfile
);
1372 if (load_use
&& start_loadavg() != 0)
1375 if (!fuse_main(fuse_argc
, fuse_argv
, &lxcfs_ops
, opts
))
1383 dlclose(dlopen_handle
);
1386 free(new_fuse_opts
);
1388 close_prot_errno_disarm(pidfile_fd
);