1 /* SPDX-License-Identifier: LGPL-2.1+ */
21 #include <linux/sched.h>
22 #include <sys/epoll.h>
23 #include <sys/mount.h>
24 #include <sys/socket.h>
25 #include <linux/limits.h>
28 #include <fuse3/fuse.h>
34 #include "lxcfs_fuse_compat.h"
36 #include "memory_utils.h"
40 /* Functions to keep track of number of threads using the library */
42 static int users_count
;
43 static pthread_mutex_t user_count_mutex
= PTHREAD_MUTEX_INITIALIZER
;
44 static void lock_mutex(pthread_mutex_t
*l
)
48 ret
= pthread_mutex_lock(l
);
50 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
53 static void unlock_mutex(pthread_mutex_t
*l
)
57 ret
= pthread_mutex_unlock(l
);
59 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
62 static inline void users_lock(void)
64 lock_mutex(&user_count_mutex
);
67 static inline void users_unlock(void)
69 unlock_mutex(&user_count_mutex
);
72 static pthread_t loadavg_pid
= 0;
74 /* Returns zero on success */
75 static int start_loadavg(void)
78 pthread_t (*__load_daemon
)(int);
81 __load_daemon
= (pthread_t(*)(int))dlsym(dlopen_handle
, "load_daemon");
84 return log_error(-1, "%s - Failed to start loadavg daemon", error
);
86 loadavg_pid
= __load_daemon(1);
93 /* Returns zero on success */
94 static int stop_loadavg(void)
97 int (*__stop_load_daemon
)(pthread_t
);
99 __stop_load_daemon
= (int (*)(pthread_t
))dlsym(dlopen_handle
, "stop_load_daemon");
102 return log_error(-1, "%s - Failed to stop loadavg daemon", error
);
104 if (__stop_load_daemon(loadavg_pid
))
110 static volatile sig_atomic_t need_reload
;
112 /* do_reload - reload the dynamic library. Done under
113 * lock and when we know the user_count was 0 */
114 static void do_reload(void)
117 char lxcfs_lib_path
[PATH_MAX
];
123 lxcfs_info("Closed liblxcfs.so");
124 dlclose(dlopen_handle
);
127 /* First try loading using ld.so */
129 dlopen_handle
= dlopen("liblxcfs.so", RTLD_NOW
);
131 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
134 lxcfs_debug("Opened liblxcfs.so");
139 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
140 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
142 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
144 if (ret
< 0 || (size_t)ret
>= sizeof(lxcfs_lib_path
))
145 log_exit("Failed to create path to open liblxcfs");
147 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
149 log_exit("%s - Failed to open liblxcfs.so", dlerror());
151 lxcfs_debug("Opened %s", lxcfs_lib_path
);
158 lxcfs_info("Reloaded LXCFS");
162 static void up_users(void)
165 if (users_count
== 0 && need_reload
)
171 static void down_users(void)
178 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
183 /* Functions to run the library methods */
184 static int do_cg_getattr(const char *path
, struct stat
*sb
)
187 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
190 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
193 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
195 return __cg_getattr(path
, sb
);
198 static int do_proc_getattr(const char *path
, struct stat
*sb
)
201 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
204 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
207 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
209 return __proc_getattr(path
, sb
);
212 static int do_sys_getattr(const char *path
, struct stat
*sb
)
215 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
218 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
221 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
223 return __sys_getattr(path
, sb
);
226 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
227 struct fuse_file_info
*fi
)
230 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
231 struct fuse_file_info
*fi
);
234 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
237 return log_error(-1, "%s - Failed to find cg_read()", error
);
239 return __cg_read(path
, buf
, size
, offset
, fi
);
242 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
243 struct fuse_file_info
*fi
)
246 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
247 off_t offset
, struct fuse_file_info
*fi
);
250 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
253 return log_error(-1, "%s - Failed to find proc_read()", error
);
255 return __proc_read(path
, buf
, size
, offset
, fi
);
258 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
259 struct fuse_file_info
*fi
)
262 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
263 off_t offset
, struct fuse_file_info
*fi
);
266 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
269 return log_error(-1, "%s - Failed to find sys_read()", error
);
271 return __sys_read(path
, buf
, size
, offset
, fi
);
274 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
275 off_t offset
, struct fuse_file_info
*fi
)
278 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
279 off_t offset
, struct fuse_file_info
*fi
);
282 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
285 return log_error(-1, "%s - Failed to find cg_write()", error
);
287 return __cg_write(path
, buf
, size
, offset
, fi
);
290 static int do_sys_write(const char *path
, const char *buf
, size_t size
,
291 off_t offset
, struct fuse_file_info
*fi
)
294 int (*__sys_write
)(const char *path
, const char *buf
, size_t size
,
295 off_t offset
, struct fuse_file_info
*fi
);
298 __sys_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_write");
301 return log_error(-1, "%s - Failed to find sys_write()", error
);
303 return __sys_write(path
, buf
, size
, offset
, fi
);
306 static int do_cg_mkdir(const char *path
, mode_t mode
)
309 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
312 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
315 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
317 return __cg_mkdir(path
, mode
);
320 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
323 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
326 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
329 return log_error(-1, "%s - Failed to find cg_chown()", error
);
331 return __cg_chown(path
, uid
, gid
);
334 static int do_cg_rmdir(const char *path
)
337 int (*__cg_rmdir
)(const char *path
);
340 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
343 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
345 return __cg_rmdir(path
);
348 static int do_cg_chmod(const char *path
, mode_t mode
)
351 int (*__cg_chmod
)(const char *path
, mode_t mode
);
354 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
357 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
359 return __cg_chmod(path
, mode
);
362 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
363 off_t offset
, struct fuse_file_info
*fi
)
366 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
367 off_t offset
, struct fuse_file_info
*fi
);
370 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
373 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
375 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
378 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
379 off_t offset
, struct fuse_file_info
*fi
)
382 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
383 off_t offset
, struct fuse_file_info
*fi
);
386 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
389 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
391 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
394 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
395 off_t offset
, struct fuse_file_info
*fi
)
398 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
399 off_t offset
, struct fuse_file_info
*fi
);
402 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
405 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
407 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
410 static int do_sys_readlink(const char *path
, char *buf
, size_t size
)
413 int (*__sys_readlink
)(const char *path
, char *buf
, size_t size
);
416 __sys_readlink
= (int (*)(const char *, char *, size_t))dlsym(dlopen_handle
, "sys_readlink");
419 return log_error(-1, "%s - Failed to find sys_readlink()", error
);
421 return __sys_readlink(path
, buf
, size
);
424 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
427 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
430 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
433 return log_error(-1, "%s - Failed to find cg_open()", error
);
435 return __cg_open(path
, fi
);
438 static int do_cg_access(const char *path
, int mode
)
441 int (*__cg_access
)(const char *path
, int mode
);
444 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
447 return log_error(-1, "%s - Failed to find cg_access()", error
);
449 return __cg_access(path
, mode
);
452 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
455 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
458 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
461 return log_error(-1, "%s - Failed to find proc_open()", error
);
463 return __proc_open(path
, fi
);
466 static int do_proc_access(const char *path
, int mode
)
469 int (*__proc_access
)(const char *path
, int mode
);
472 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
475 return log_error(-1, "%s - Failed to find proc_access()", error
);
477 return __proc_access(path
, mode
);
480 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
483 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
486 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
489 return log_error(-1, "%s - Failed to find sys_open()", error
);
491 return __sys_open(path
, fi
);
494 static int do_sys_opendir(const char *path
, struct fuse_file_info
*fi
)
497 int (*__sys_opendir
)(const char *path
, struct fuse_file_info
*fi
);
500 __sys_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_opendir");
503 return log_error(-1, "%s - Failed to find sys_opendir()", error
);
505 return __sys_opendir(path
, fi
);
508 static int do_sys_access(const char *path
, int mode
)
511 int (*__sys_access
)(const char *path
, int mode
);
514 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
517 return log_error(-1, "%s - Failed to find sys_access()", error
);
519 return __sys_access(path
, mode
);
522 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
525 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
528 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
531 return log_error(-1, "%s - Failed to find cg_release()", error
);
533 return __cg_release(path
, fi
);
536 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
539 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
542 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
545 return log_error(-1, "%s - Failed to find proc_release()", error
);
547 return __proc_release(path
, fi
);
550 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
553 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
556 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
559 return log_error(-1, "%s - Failed to find sys_release()", error
);
561 return __sys_release(path
, fi
);
564 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
567 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
570 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
573 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
575 return __cg_opendir(path
, fi
);
578 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
581 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
584 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
587 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
589 return __cg_releasedir(path
, fi
);
592 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
595 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
598 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
601 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
603 return __sys_releasedir(path
, fi
);
607 static int lxcfs_getattr(const char *path
, struct stat
*sb
, struct fuse_file_info
*fi
)
609 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
615 if (strcmp(path
, "/") == 0) {
616 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
618 sb
->st_uid
= sb
->st_gid
= 0;
619 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
621 sb
->st_mode
= S_IFDIR
| 00755;
626 if (strncmp(path
, "/cgroup", 7) == 0) {
628 ret
= do_cg_getattr(path
, sb
);
633 if (strncmp(path
, "/proc", 5) == 0) {
635 ret
= do_proc_getattr(path
, sb
);
640 if (strncmp(path
, "/sys", 4) == 0) {
642 ret
= do_sys_getattr(path
, sb
);
650 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
654 if (strcmp(path
, "/") == 0)
657 if (strncmp(path
, "/cgroup", 7) == 0) {
659 ret
= do_cg_opendir(path
, fi
);
664 if (strcmp(path
, "/proc") == 0)
667 if (strncmp(path
, "/sys", 4) == 0) {
669 ret
= do_sys_opendir(path
, fi
);
678 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
679 off_t offset
, struct fuse_file_info
*fi
, enum fuse_readdir_flags flags
)
681 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
682 off_t offset
, struct fuse_file_info
*fi
)
687 if (strcmp(path
, "/") == 0) {
688 if (DIR_FILLER(filler
, buf
, ".", NULL
, 0) != 0 ||
689 DIR_FILLER(filler
, buf
, "..", NULL
, 0) != 0 ||
690 DIR_FILLER(filler
, buf
, "proc", NULL
, 0) != 0 ||
691 DIR_FILLER(filler
, buf
, "sys", NULL
, 0) != 0 ||
692 DIR_FILLER(filler
, buf
, "cgroup", NULL
, 0) != 0)
698 if (strncmp(path
, "/cgroup", 7) == 0) {
700 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
705 if (strcmp(path
, "/proc") == 0) {
707 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
712 if (strncmp(path
, "/sys", 4) == 0) {
714 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
722 static int lxcfs_access(const char *path
, int mode
)
726 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
729 if (strncmp(path
, "/cgroup", 7) == 0) {
731 ret
= do_cg_access(path
, mode
);
736 if (strncmp(path
, "/proc", 5) == 0) {
738 ret
= do_proc_access(path
, mode
);
743 if (strncmp(path
, "/sys", 4) == 0) {
745 ret
= do_sys_access(path
, mode
);
753 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
757 if (strcmp(path
, "/") == 0)
760 if (strncmp(path
, "/cgroup", 7) == 0) {
762 ret
= do_cg_releasedir(path
, fi
);
767 if (strcmp(path
, "/proc") == 0)
770 if (strncmp(path
, "/sys", 4) == 0) {
772 ret
= do_sys_releasedir(path
, fi
);
780 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
784 if (strncmp(path
, "/cgroup", 7) == 0) {
786 ret
= do_cg_open(path
, fi
);
791 if (strncmp(path
, "/proc", 5) == 0) {
793 ret
= do_proc_open(path
, fi
);
798 if (strncmp(path
, "/sys", 4) == 0) {
800 ret
= do_sys_open(path
, fi
);
808 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
809 struct fuse_file_info
*fi
)
813 if (strncmp(path
, "/cgroup", 7) == 0) {
815 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
820 if (strncmp(path
, "/proc", 5) == 0) {
822 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
827 if (strncmp(path
, "/sys", 4) == 0) {
829 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
837 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
838 struct fuse_file_info
*fi
)
842 if (strncmp(path
, "/cgroup", 7) == 0) {
844 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
849 if (strncmp(path
, "/sys", 4) == 0) {
851 ret
= do_sys_write(path
, buf
, size
, offset
, fi
);
859 int lxcfs_readlink(const char *path
, char *buf
, size_t size
)
863 if (strncmp(path
, "/sys", 4) == 0) {
865 ret
= do_sys_readlink(path
, buf
, size
);
873 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
878 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
882 if (strncmp(path
, "/cgroup", 7) == 0) {
884 ret
= do_cg_release(path
, fi
);
889 if (strncmp(path
, "/proc", 5) == 0) {
891 ret
= do_proc_release(path
, fi
);
896 if (strncmp(path
, "/sys", 4) == 0) {
898 ret
= do_sys_release(path
, fi
);
906 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
911 int lxcfs_mkdir(const char *path
, mode_t mode
)
915 if (strncmp(path
, "/cgroup", 7) == 0) {
917 ret
= do_cg_mkdir(path
, mode
);
926 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
, struct fuse_file_info
*fi
)
928 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
933 if (strncmp(path
, "/cgroup", 7) == 0) {
935 ret
= do_cg_chown(path
, uid
, gid
);
940 if (strncmp(path
, "/proc", 5) == 0)
943 if (strncmp(path
, "/sys", 4) == 0)
950 * cat first does a truncate before doing ops->write. This doesn't
951 * really make sense for cgroups. So just return 0 always but do
955 int lxcfs_truncate(const char *path
, off_t newsize
, struct fuse_file_info
*fi
)
957 int lxcfs_truncate(const char *path
, off_t newsize
)
960 if (strncmp(path
, "/cgroup", 7) == 0)
963 if (strncmp(path
, "/sys", 4) == 0)
969 int lxcfs_rmdir(const char *path
)
973 if (strncmp(path
, "/cgroup", 7) == 0) {
975 ret
= do_cg_rmdir(path
);
984 int lxcfs_chmod(const char *path
, mode_t mode
, struct fuse_file_info
*fi
)
986 int lxcfs_chmod(const char *path
, mode_t mode
)
991 if (strncmp(path
, "/cgroup", 7) == 0) {
993 ret
= do_cg_chmod(path
, mode
);
998 if (strncmp(path
, "/proc", 5) == 0)
1001 if (strncmp(path
, "/sys", 4) == 0)
1008 static void *lxcfs_init(struct fuse_conn_info
*conn
, struct fuse_config
*cfg
)
1010 static void *lxcfs_init(struct fuse_conn_info
*conn
)
1014 void *(*__lxcfs_fuse_init
)(struct fuse_conn_info
* conn
, void * cfg
);
1017 __lxcfs_fuse_init
= (void *(*)(struct fuse_conn_info
* conn
, void * cfg
))dlsym(dlopen_handle
, "lxcfs_fuse_init");
1020 return log_error(NULL
, "%s - Failed to find lxcfs_fuse_init()", error
);
1022 return __lxcfs_fuse_init(conn
, NULL
);
1025 const struct fuse_operations lxcfs_ops
= {
1026 .access
= lxcfs_access
,
1027 .chmod
= lxcfs_chmod
,
1028 .chown
= lxcfs_chown
,
1029 .flush
= lxcfs_flush
,
1030 .fsync
= lxcfs_fsync
,
1031 .getattr
= lxcfs_getattr
,
1033 .mkdir
= lxcfs_mkdir
,
1035 .opendir
= lxcfs_opendir
,
1037 .readdir
= lxcfs_readdir
,
1038 .release
= lxcfs_release
,
1039 .releasedir
= lxcfs_releasedir
,
1040 .rmdir
= lxcfs_rmdir
,
1041 .truncate
= lxcfs_truncate
,
1042 .write
= lxcfs_write
,
1043 .readlink
= lxcfs_readlink
,
1060 .removexattr
= NULL
,
1070 static void usage(void)
1072 lxcfs_info("Usage: lxcfs <directory>\n");
1073 lxcfs_info("lxcfs is a FUSE-based proc, sys and cgroup virtualizing filesystem\n");
1074 lxcfs_info("Options :");
1075 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
1076 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
1077 lxcfs_info(" -n, --help Print help");
1078 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
1079 lxcfs_info(" -o Options to pass directly through fuse");
1080 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
1081 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH
);
1082 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
1083 lxcfs_info(" -v, --version Print lxcfs version");
1084 lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares");
1085 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
1089 static int set_pidfile(char *pidfile
)
1091 __do_close
int fd
= -EBADF
;
1092 char buf
[INTTYPE_TO_STRLEN(long)];
1096 .l_whence
= SEEK_SET
,
1101 fd
= open(pidfile
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| O_CLOEXEC
);
1103 return log_error(-1, "Could not open pidfile %s: %m", pidfile
);
1105 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
1106 if (errno
== EAGAIN
|| errno
== EACCES
)
1107 return log_error(-1, "PID file '%s' is already locked", pidfile
);
1108 lxcfs_error("Warning; unable to lock PID file, proceeding");
1111 if (ftruncate(fd
, 0))
1112 return log_error(-1, "Error truncating PID file '%s': %m", pidfile
);
1114 ret
= snprintf(buf
, sizeof(buf
), "%ld\n", (long)getpid());
1115 if (ret
< 0 || (size_t)ret
>= sizeof(buf
))
1116 return log_error(-1, "Failed to convert pid to string %m");
1118 if (write(fd
, buf
, ret
) != ret
)
1119 return log_error(-1, "Error writing to PID file '%s': %m", pidfile
);
1124 static const struct option long_options
[] = {
1125 {"debug", no_argument
, 0, 'd' },
1126 {"disable-swap", no_argument
, 0, 'u' },
1127 {"enable-loadavg", no_argument
, 0, 'l' },
1128 {"foreground", no_argument
, 0, 'f' },
1129 {"help", no_argument
, 0, 'h' },
1130 {"version", no_argument
, 0, 'v' },
1132 {"enable-cfs", no_argument
, 0, 0 },
1133 {"enable-pidfd", no_argument
, 0, 0 },
1135 {"pidfile", required_argument
, 0, 'p' },
1139 static int append_comma_separate(char **s
, const char *append
)
1143 size_t append_len
, len
;
1148 append_len
= strlen(append
);
1154 news
= realloc(*s
, len
+ append_len
+ 2);
1157 news
= realloc(NULL
, append_len
+ 1);
1163 ret
= snprintf(news
+ len
, append_len
+ 2, ",%s", append
);
1165 ret
= snprintf(news
, append_len
+ 1, "%s", append
);
1173 int main(int argc
, char *argv
[])
1175 int pidfile_fd
= -EBADF
;
1176 int ret
= EXIT_FAILURE
;
1177 char *pidfile
= NULL
, *token
= NULL
;
1178 char pidfile_buf
[STRLITERALLEN(RUNTIME_PATH
) + STRLITERALLEN("/lxcfs.pid") + 1] = {};
1179 bool debug
= false, foreground
= false;
1181 bool nonempty
= false;
1183 bool load_use
= false;
1185 * what we pass to fuse_main is:
1186 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1189 int c
, idx
, new_argc
;
1191 const char *fuse_opts
= NULL
;
1192 char *new_fuse_opts
= NULL
;
1193 char *const *new_argv
;
1194 struct lxcfs_opts
*opts
;
1196 opts
= malloc(sizeof(struct lxcfs_opts
));
1198 lxcfs_error("Error allocating memory for options");
1202 opts
->swap_off
= false;
1203 opts
->use_pidfd
= false;
1204 opts
->use_cfs
= false;
1207 while ((c
= getopt_long(argc
, argv
, "dulfhvso:p:", long_options
, &idx
)) != -1) {
1210 if (strcmp(long_options
[idx
].name
, "enable-pidfd") == 0)
1211 opts
->use_pidfd
= true;
1212 else if (strcmp(long_options
[idx
].name
, "enable-cfs") == 0)
1213 opts
->use_cfs
= true;
1228 lxcfs_error("Specifying -o multiple times is unsupported");
1238 /* legacy argument: ignore */
1241 opts
->swap_off
= true;
1244 lxcfs_info("%s", STRINGIFY(PROJECT_VERSION
));
1251 if (foreground
&& debug
)
1252 log_exit("Both --debug and --forgreound specified");
1254 new_argv
= &argv
[optind
];
1255 new_argc
= argc
- optind
;
1257 /* Older LXCFS versions printed help when used without any argument. */
1261 if (new_argc
!= 1) {
1262 lxcfs_error("Missing mountpoint");
1266 fuse_argv
[fuse_argc
++] = argv
[0];
1268 fuse_argv
[fuse_argc
++] = "-d";
1270 fuse_argv
[fuse_argc
++] = "-f";
1271 fuse_argv
[fuse_argc
++] = "-o";
1273 /* Parse additional fuse options. */
1277 dup
= strdup(fuse_opts
);
1279 lxcfs_error("Failed to copy fuse options");
1283 lxc_iterate_parts(token
, dup
, ",") {
1285 if (strcmp(token
, "allow_other") == 0)
1288 /* default for LXCFS */
1289 if (strcmp(token
, "direct_io") == 0)
1292 /* default for LXCFS */
1293 if (strncmp(token
, "entry_timeout", STRLITERALLEN("entry_timeout")) == 0)
1296 /* default for LXCFS */
1297 if (strncmp(token
, "attr_timeout", STRLITERALLEN("entry_timeout")) == 0)
1300 /* default for LXCFS */
1301 if (strncmp(token
, "allow_other", STRLITERALLEN("allow_other")) == 0)
1304 /* default with fuse3 */
1305 if (strcmp(token
, "nonempty") == 0) {
1312 if (append_comma_separate(&new_fuse_opts
, token
)) {
1313 lxcfs_error("Failed to copy fuse argument \"%s\"", token
);
1321 if (append_comma_separate(&new_fuse_opts
, "allow_other,entry_timeout=0.5,attr_timeout=0.5")) {
1322 lxcfs_error("Failed to copy fuse argument \"allow_other,entry_timeout=0.5,attr_timeout=0.5\"");
1328 if (append_comma_separate(&new_fuse_opts
, "nonempty")) {
1329 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1334 if (append_comma_separate(&new_fuse_opts
, "direct_io")) {
1335 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1341 * We can't use default_permissions since we still support systems that
1342 * don't have kernels with cgroup namespace support. On such kernels
1343 * lxcfs will provide a namespaced cgroup view and needs explicit
1344 * access helpers to make that work.
1345 * Another reason that came to me is that we can't or at least
1346 * shouldn't guarantee that we don't need more complicated access
1347 * helpers for proc and sys virtualization in the future.
1350 fuse_argv
[fuse_argc
++] = new_fuse_opts
;
1351 fuse_argv
[fuse_argc
++] = new_argv
[0];
1352 fuse_argv
[fuse_argc
] = NULL
;
1355 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1356 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1361 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1362 pidfile
= pidfile_buf
;
1365 pidfile_fd
= set_pidfile(pidfile
);
1369 if (load_use
&& start_loadavg() != 0)
1372 if (!fuse_main(fuse_argc
, fuse_argv
, &lxcfs_ops
, opts
))
1380 dlclose(dlopen_handle
);
1383 free(new_fuse_opts
);
1385 close_prot_errno_disarm(pidfile_fd
);