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>
27 #include "lxcfs_fuse.h"
30 #include "lxcfs_fuse_compat.h"
32 #include "memory_utils.h"
36 /* Functions to keep track of number of threads using the library */
38 static int users_count
;
39 static pthread_mutex_t user_count_mutex
= PTHREAD_MUTEX_INITIALIZER
;
40 static void lock_mutex(pthread_mutex_t
*l
)
44 ret
= pthread_mutex_lock(l
);
46 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
49 static void unlock_mutex(pthread_mutex_t
*l
)
53 ret
= pthread_mutex_unlock(l
);
55 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
58 static inline void users_lock(void)
60 lock_mutex(&user_count_mutex
);
63 static inline void users_unlock(void)
65 unlock_mutex(&user_count_mutex
);
68 static pthread_t loadavg_pid
= 0;
70 /* Returns zero on success */
71 static int start_loadavg(void)
74 pthread_t (*__load_daemon
)(int);
77 __load_daemon
= (pthread_t(*)(int))dlsym(dlopen_handle
, "load_daemon");
80 return log_error(-1, "%s - Failed to start loadavg daemon", error
);
82 loadavg_pid
= __load_daemon(1);
89 /* Returns zero on success */
90 static int stop_loadavg(void)
93 int (*__stop_load_daemon
)(pthread_t
);
95 __stop_load_daemon
= (int (*)(pthread_t
))dlsym(dlopen_handle
, "stop_load_daemon");
98 return log_error(-1, "%s - Failed to stop loadavg daemon", error
);
100 if (__stop_load_daemon(loadavg_pid
))
106 static volatile sig_atomic_t need_reload
;
108 static int lxcfs_init_library(void)
111 void *(*__lxcfs_fuse_init
)(struct fuse_conn_info
* conn
, void * cfg
);
114 __lxcfs_fuse_init
= (void *(*)(struct fuse_conn_info
* conn
, void * cfg
))dlsym(dlopen_handle
, "lxcfs_fuse_init");
117 return log_error(-1, "%s - Failed to find lxcfs_fuse_init()", error
);
119 __lxcfs_fuse_init(NULL
, NULL
);
124 /* do_reload - reload the dynamic library. Done under
125 * lock and when we know the user_count was 0 */
126 static void do_reload(bool reinit
)
129 char lxcfs_lib_path
[PATH_MAX
];
135 lxcfs_info("Closed liblxcfs.so");
136 dlclose(dlopen_handle
);
139 /* First try loading using ld.so */
141 dlopen_handle
= dlopen("liblxcfs.so", RTLD_NOW
);
143 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
146 lxcfs_debug("Opened liblxcfs.so");
151 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
152 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
154 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
156 if (ret
< 0 || (size_t)ret
>= sizeof(lxcfs_lib_path
))
157 log_exit("Failed to create path to open liblxcfs");
159 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
161 log_exit("%s - Failed to open liblxcfs.so", dlerror());
163 lxcfs_debug("Opened %s", lxcfs_lib_path
);
166 /* initialize the library */
167 if (reinit
&& lxcfs_init_library() < 0) {
168 log_exit("Failed to initialize liblxcfs.so");
175 lxcfs_info("Reloaded LXCFS");
179 static void up_users(void)
182 if (users_count
== 0 && need_reload
)
188 static void down_users(void)
195 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
200 /* Functions to run the library methods */
201 static int do_cg_getattr(const char *path
, struct stat
*sb
)
204 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
207 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
210 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
212 return __cg_getattr(path
, sb
);
215 static int do_proc_getattr(const char *path
, struct stat
*sb
)
218 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
221 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
224 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
226 return __proc_getattr(path
, sb
);
229 static int do_sys_getattr(const char *path
, struct stat
*sb
)
232 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
235 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
238 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
240 return __sys_getattr(path
, sb
);
243 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
244 struct fuse_file_info
*fi
)
247 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
248 struct fuse_file_info
*fi
);
251 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
254 return log_error(-1, "%s - Failed to find cg_read()", error
);
256 return __cg_read(path
, buf
, size
, offset
, fi
);
259 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
260 struct fuse_file_info
*fi
)
263 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
264 off_t offset
, struct fuse_file_info
*fi
);
267 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
270 return log_error(-1, "%s - Failed to find proc_read()", error
);
272 return __proc_read(path
, buf
, size
, offset
, fi
);
275 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
276 struct fuse_file_info
*fi
)
279 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
280 off_t offset
, struct fuse_file_info
*fi
);
283 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
286 return log_error(-1, "%s - Failed to find sys_read()", error
);
288 return __sys_read(path
, buf
, size
, offset
, fi
);
291 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
292 off_t offset
, struct fuse_file_info
*fi
)
295 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
296 off_t offset
, struct fuse_file_info
*fi
);
299 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
302 return log_error(-1, "%s - Failed to find cg_write()", error
);
304 return __cg_write(path
, buf
, size
, offset
, fi
);
307 static int do_sys_write(const char *path
, const char *buf
, size_t size
,
308 off_t offset
, struct fuse_file_info
*fi
)
311 int (*__sys_write
)(const char *path
, const char *buf
, size_t size
,
312 off_t offset
, struct fuse_file_info
*fi
);
315 __sys_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_write");
318 return log_error(-1, "%s - Failed to find sys_write()", error
);
320 return __sys_write(path
, buf
, size
, offset
, fi
);
323 static int do_cg_mkdir(const char *path
, mode_t mode
)
326 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
329 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
332 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
334 return __cg_mkdir(path
, mode
);
337 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
340 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
343 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
346 return log_error(-1, "%s - Failed to find cg_chown()", error
);
348 return __cg_chown(path
, uid
, gid
);
351 static int do_cg_rmdir(const char *path
)
354 int (*__cg_rmdir
)(const char *path
);
357 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
360 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
362 return __cg_rmdir(path
);
365 static int do_cg_chmod(const char *path
, mode_t mode
)
368 int (*__cg_chmod
)(const char *path
, mode_t mode
);
371 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
374 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
376 return __cg_chmod(path
, mode
);
379 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
380 off_t offset
, struct fuse_file_info
*fi
)
383 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
384 off_t offset
, struct fuse_file_info
*fi
);
387 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
390 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
392 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
395 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
396 off_t offset
, struct fuse_file_info
*fi
)
399 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
400 off_t offset
, struct fuse_file_info
*fi
);
403 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
406 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
408 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
411 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
412 off_t offset
, struct fuse_file_info
*fi
)
415 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
416 off_t offset
, struct fuse_file_info
*fi
);
419 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
422 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
424 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
427 static int do_sys_readlink(const char *path
, char *buf
, size_t size
)
430 int (*__sys_readlink
)(const char *path
, char *buf
, size_t size
);
433 __sys_readlink
= (int (*)(const char *, char *, size_t))dlsym(dlopen_handle
, "sys_readlink");
436 return log_error(-1, "%s - Failed to find sys_readlink()", error
);
438 return __sys_readlink(path
, buf
, size
);
441 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
444 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
447 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
450 return log_error(-1, "%s - Failed to find cg_open()", error
);
452 return __cg_open(path
, fi
);
455 static int do_cg_access(const char *path
, int mode
)
458 int (*__cg_access
)(const char *path
, int mode
);
461 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
464 return log_error(-1, "%s - Failed to find cg_access()", error
);
466 return __cg_access(path
, mode
);
469 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
472 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
475 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
478 return log_error(-1, "%s - Failed to find proc_open()", error
);
480 return __proc_open(path
, fi
);
483 static int do_proc_access(const char *path
, int mode
)
486 int (*__proc_access
)(const char *path
, int mode
);
489 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
492 return log_error(-1, "%s - Failed to find proc_access()", error
);
494 return __proc_access(path
, mode
);
497 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
500 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
503 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
506 return log_error(-1, "%s - Failed to find sys_open()", error
);
508 return __sys_open(path
, fi
);
511 static int do_sys_opendir(const char *path
, struct fuse_file_info
*fi
)
514 int (*__sys_opendir
)(const char *path
, struct fuse_file_info
*fi
);
517 __sys_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_opendir");
520 return log_error(-1, "%s - Failed to find sys_opendir()", error
);
522 return __sys_opendir(path
, fi
);
525 static int do_sys_access(const char *path
, int mode
)
528 int (*__sys_access
)(const char *path
, int mode
);
531 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
534 return log_error(-1, "%s - Failed to find sys_access()", error
);
536 return __sys_access(path
, mode
);
539 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
542 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
545 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
548 return log_error(-1, "%s - Failed to find cg_release()", error
);
550 return __cg_release(path
, fi
);
553 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
556 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
559 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
562 return log_error(-1, "%s - Failed to find proc_release()", error
);
564 return __proc_release(path
, fi
);
567 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
570 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
573 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
576 return log_error(-1, "%s - Failed to find sys_release()", error
);
578 return __sys_release(path
, fi
);
581 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
584 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
587 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
590 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
592 return __cg_opendir(path
, fi
);
595 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
598 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
601 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
604 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
606 return __cg_releasedir(path
, fi
);
609 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
612 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
615 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
618 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
620 return __sys_releasedir(path
, fi
);
624 static int lxcfs_getattr(const char *path
, struct stat
*sb
, struct fuse_file_info
*fi
)
626 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
632 if (strcmp(path
, "/") == 0) {
633 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
635 sb
->st_uid
= sb
->st_gid
= 0;
636 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
638 sb
->st_mode
= S_IFDIR
| 00755;
643 if (strncmp(path
, "/cgroup", 7) == 0) {
645 ret
= do_cg_getattr(path
, sb
);
650 if (strncmp(path
, "/proc", 5) == 0) {
652 ret
= do_proc_getattr(path
, sb
);
657 if (strncmp(path
, "/sys", 4) == 0) {
659 ret
= do_sys_getattr(path
, sb
);
667 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
671 if (strcmp(path
, "/") == 0)
674 if (strncmp(path
, "/cgroup", 7) == 0) {
676 ret
= do_cg_opendir(path
, fi
);
681 if (strcmp(path
, "/proc") == 0)
684 if (strncmp(path
, "/sys", 4) == 0) {
686 ret
= do_sys_opendir(path
, fi
);
695 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
696 off_t offset
, struct fuse_file_info
*fi
, enum fuse_readdir_flags flags
)
698 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
699 off_t offset
, struct fuse_file_info
*fi
)
704 if (strcmp(path
, "/") == 0) {
705 if (dir_filler(filler
, buf
, ".", 0) != 0 ||
706 dir_filler(filler
, buf
, "..", 0) != 0 ||
707 dir_filler(filler
, buf
, "proc", 0) != 0 ||
708 dir_filler(filler
, buf
, "sys", 0) != 0 ||
709 dir_filler(filler
, buf
, "cgroup", 0) != 0)
715 if (strncmp(path
, "/cgroup", 7) == 0) {
717 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
722 if (strcmp(path
, "/proc") == 0) {
724 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
729 if (strncmp(path
, "/sys", 4) == 0) {
731 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
739 static int lxcfs_access(const char *path
, int mode
)
743 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
746 if (strncmp(path
, "/cgroup", 7) == 0) {
748 ret
= do_cg_access(path
, mode
);
753 if (strncmp(path
, "/proc", 5) == 0) {
755 ret
= do_proc_access(path
, mode
);
760 if (strncmp(path
, "/sys", 4) == 0) {
762 ret
= do_sys_access(path
, mode
);
770 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
774 if (strcmp(path
, "/") == 0)
777 if (strncmp(path
, "/cgroup", 7) == 0) {
779 ret
= do_cg_releasedir(path
, fi
);
784 if (strcmp(path
, "/proc") == 0)
787 if (strncmp(path
, "/sys", 4) == 0) {
789 ret
= do_sys_releasedir(path
, fi
);
797 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
801 if (strncmp(path
, "/cgroup", 7) == 0) {
803 ret
= do_cg_open(path
, fi
);
808 if (strncmp(path
, "/proc", 5) == 0) {
810 ret
= do_proc_open(path
, fi
);
815 if (strncmp(path
, "/sys", 4) == 0) {
817 ret
= do_sys_open(path
, fi
);
825 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
826 struct fuse_file_info
*fi
)
830 if (strncmp(path
, "/cgroup", 7) == 0) {
832 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
837 if (strncmp(path
, "/proc", 5) == 0) {
839 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
844 if (strncmp(path
, "/sys", 4) == 0) {
846 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
854 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
855 struct fuse_file_info
*fi
)
859 if (strncmp(path
, "/cgroup", 7) == 0) {
861 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
866 if (strncmp(path
, "/sys", 4) == 0) {
868 ret
= do_sys_write(path
, buf
, size
, offset
, fi
);
876 int lxcfs_readlink(const char *path
, char *buf
, size_t size
)
880 if (strncmp(path
, "/sys", 4) == 0) {
882 ret
= do_sys_readlink(path
, buf
, size
);
890 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
895 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
899 if (strncmp(path
, "/cgroup", 7) == 0) {
901 ret
= do_cg_release(path
, fi
);
906 if (strncmp(path
, "/proc", 5) == 0) {
908 ret
= do_proc_release(path
, fi
);
913 if (strncmp(path
, "/sys", 4) == 0) {
915 ret
= do_sys_release(path
, fi
);
923 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
928 int lxcfs_mkdir(const char *path
, mode_t mode
)
932 if (strncmp(path
, "/cgroup", 7) == 0) {
934 ret
= do_cg_mkdir(path
, mode
);
943 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
, struct fuse_file_info
*fi
)
945 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
950 if (strncmp(path
, "/cgroup", 7) == 0) {
952 ret
= do_cg_chown(path
, uid
, gid
);
957 if (strncmp(path
, "/proc", 5) == 0)
960 if (strncmp(path
, "/sys", 4) == 0)
967 * cat first does a truncate before doing ops->write. This doesn't
968 * really make sense for cgroups. So just return 0 always but do
972 int lxcfs_truncate(const char *path
, off_t newsize
, struct fuse_file_info
*fi
)
974 int lxcfs_truncate(const char *path
, off_t newsize
)
977 if (strncmp(path
, "/cgroup", 7) == 0)
980 if (strncmp(path
, "/sys", 4) == 0)
986 int lxcfs_rmdir(const char *path
)
990 if (strncmp(path
, "/cgroup", 7) == 0) {
992 ret
= do_cg_rmdir(path
);
1001 int lxcfs_chmod(const char *path
, mode_t mode
, struct fuse_file_info
*fi
)
1003 int lxcfs_chmod(const char *path
, mode_t mode
)
1008 if (strncmp(path
, "/cgroup", 7) == 0) {
1010 ret
= do_cg_chmod(path
, mode
);
1015 if (strncmp(path
, "/proc", 5) == 0)
1018 if (strncmp(path
, "/sys", 4) == 0)
1025 static void *lxcfs_init(struct fuse_conn_info
*conn
, struct fuse_config
*cfg
)
1027 static void *lxcfs_init(struct fuse_conn_info
*conn
)
1030 if (lxcfs_init_library() < 0)
1033 return fuse_get_context()->private_data
;
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
,
1081 static void usage(void)
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 || (size_t)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;
1255 lxcfs_info("%s", STRINGIFY(PROJECT_VERSION
));
1262 if (foreground
&& debug
)
1263 log_exit("Both --debug and --forgreound specified");
1265 new_argv
= &argv
[optind
];
1266 new_argc
= argc
- optind
;
1268 /* Older LXCFS versions printed help when used without any argument. */
1272 if (new_argc
!= 1) {
1273 lxcfs_error("Missing mountpoint");
1277 fuse_argv
[fuse_argc
++] = argv
[0];
1279 fuse_argv
[fuse_argc
++] = "-d";
1281 fuse_argv
[fuse_argc
++] = "-f";
1282 fuse_argv
[fuse_argc
++] = "-o";
1284 /* Parse additional fuse options. */
1288 dup
= strdup(fuse_opts
);
1290 lxcfs_error("Failed to copy fuse options");
1294 lxc_iterate_parts(token
, dup
, ",") {
1296 if (strcmp(token
, "allow_other") == 0)
1299 /* default for LXCFS */
1300 if (strcmp(token
, "direct_io") == 0)
1303 /* default for LXCFS */
1304 if (strncmp(token
, "entry_timeout", STRLITERALLEN("entry_timeout")) == 0)
1307 /* default for LXCFS */
1308 if (strncmp(token
, "attr_timeout", STRLITERALLEN("entry_timeout")) == 0)
1311 /* default for LXCFS */
1312 if (strncmp(token
, "allow_other", STRLITERALLEN("allow_other")) == 0)
1315 /* default with fuse3 */
1316 if (strcmp(token
, "nonempty") == 0) {
1323 if (append_comma_separate(&new_fuse_opts
, token
)) {
1324 lxcfs_error("Failed to copy fuse argument \"%s\"", token
);
1332 if (append_comma_separate(&new_fuse_opts
, "allow_other,entry_timeout=0.5,attr_timeout=0.5")) {
1333 lxcfs_error("Failed to copy fuse argument \"allow_other,entry_timeout=0.5,attr_timeout=0.5\"");
1339 if (append_comma_separate(&new_fuse_opts
, "nonempty")) {
1340 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1345 if (append_comma_separate(&new_fuse_opts
, "direct_io")) {
1346 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1352 * We can't use default_permissions since we still support systems that
1353 * don't have kernels with cgroup namespace support. On such kernels
1354 * lxcfs will provide a namespaced cgroup view and needs explicit
1355 * access helpers to make that work.
1356 * Another reason that came to me is that we can't or at least
1357 * shouldn't guarantee that we don't need more complicated access
1358 * helpers for proc and sys virtualization in the future.
1361 fuse_argv
[fuse_argc
++] = new_fuse_opts
;
1362 fuse_argv
[fuse_argc
++] = new_argv
[0];
1363 fuse_argv
[fuse_argc
] = NULL
;
1366 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1367 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1372 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1373 pidfile
= pidfile_buf
;
1376 pidfile_fd
= set_pidfile(pidfile
);
1380 if (load_use
&& start_loadavg() != 0)
1383 if (!fuse_main(fuse_argc
, fuse_argv
, &lxcfs_ops
, opts
))
1391 dlclose(dlopen_handle
);
1394 free(new_fuse_opts
);
1396 close_prot_errno_disarm(pidfile_fd
);