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 /* do_reload - reload the dynamic library. Done under
109 * lock and when we know the user_count was 0 */
110 static void do_reload(void)
113 char lxcfs_lib_path
[PATH_MAX
];
119 lxcfs_info("Closed liblxcfs.so");
120 dlclose(dlopen_handle
);
123 /* First try loading using ld.so */
125 dlopen_handle
= dlopen("liblxcfs.so", RTLD_NOW
);
127 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
130 lxcfs_debug("Opened liblxcfs.so");
135 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
136 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
138 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
140 if (ret
< 0 || (size_t)ret
>= sizeof(lxcfs_lib_path
))
141 log_exit("Failed to create path to open liblxcfs");
143 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
145 log_exit("%s - Failed to open liblxcfs.so", dlerror());
147 lxcfs_debug("Opened %s", lxcfs_lib_path
);
154 lxcfs_info("Reloaded LXCFS");
158 static void up_users(void)
161 if (users_count
== 0 && need_reload
)
167 static void down_users(void)
174 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
179 /* Functions to run the library methods */
180 static int do_cg_getattr(const char *path
, struct stat
*sb
)
183 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
186 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
189 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
191 return __cg_getattr(path
, sb
);
194 static int do_proc_getattr(const char *path
, struct stat
*sb
)
197 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
200 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
203 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
205 return __proc_getattr(path
, sb
);
208 static int do_sys_getattr(const char *path
, struct stat
*sb
)
211 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
214 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
217 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
219 return __sys_getattr(path
, sb
);
222 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
223 struct fuse_file_info
*fi
)
226 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
227 struct fuse_file_info
*fi
);
230 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
233 return log_error(-1, "%s - Failed to find cg_read()", error
);
235 return __cg_read(path
, buf
, size
, offset
, fi
);
238 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
239 struct fuse_file_info
*fi
)
242 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
243 off_t offset
, struct fuse_file_info
*fi
);
246 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
249 return log_error(-1, "%s - Failed to find proc_read()", error
);
251 return __proc_read(path
, buf
, size
, offset
, fi
);
254 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
255 struct fuse_file_info
*fi
)
258 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
259 off_t offset
, struct fuse_file_info
*fi
);
262 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
265 return log_error(-1, "%s - Failed to find sys_read()", error
);
267 return __sys_read(path
, buf
, size
, offset
, fi
);
270 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
271 off_t offset
, struct fuse_file_info
*fi
)
274 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
275 off_t offset
, struct fuse_file_info
*fi
);
278 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
281 return log_error(-1, "%s - Failed to find cg_write()", error
);
283 return __cg_write(path
, buf
, size
, offset
, fi
);
286 static int do_sys_write(const char *path
, const char *buf
, size_t size
,
287 off_t offset
, struct fuse_file_info
*fi
)
290 int (*__sys_write
)(const char *path
, const char *buf
, size_t size
,
291 off_t offset
, struct fuse_file_info
*fi
);
294 __sys_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_write");
297 return log_error(-1, "%s - Failed to find sys_write()", error
);
299 return __sys_write(path
, buf
, size
, offset
, fi
);
302 static int do_cg_mkdir(const char *path
, mode_t mode
)
305 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
308 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
311 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
313 return __cg_mkdir(path
, mode
);
316 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
319 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
322 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
325 return log_error(-1, "%s - Failed to find cg_chown()", error
);
327 return __cg_chown(path
, uid
, gid
);
330 static int do_cg_rmdir(const char *path
)
333 int (*__cg_rmdir
)(const char *path
);
336 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
339 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
341 return __cg_rmdir(path
);
344 static int do_cg_chmod(const char *path
, mode_t mode
)
347 int (*__cg_chmod
)(const char *path
, mode_t mode
);
350 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
353 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
355 return __cg_chmod(path
, mode
);
358 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
359 off_t offset
, struct fuse_file_info
*fi
)
362 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
363 off_t offset
, struct fuse_file_info
*fi
);
366 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
369 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
371 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
374 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
375 off_t offset
, struct fuse_file_info
*fi
)
378 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
379 off_t offset
, struct fuse_file_info
*fi
);
382 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
385 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
387 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
390 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
391 off_t offset
, struct fuse_file_info
*fi
)
394 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
395 off_t offset
, struct fuse_file_info
*fi
);
398 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
401 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
403 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
406 static int do_sys_readlink(const char *path
, char *buf
, size_t size
)
409 int (*__sys_readlink
)(const char *path
, char *buf
, size_t size
);
412 __sys_readlink
= (int (*)(const char *, char *, size_t))dlsym(dlopen_handle
, "sys_readlink");
415 return log_error(-1, "%s - Failed to find sys_readlink()", error
);
417 return __sys_readlink(path
, buf
, size
);
420 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
423 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
426 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
429 return log_error(-1, "%s - Failed to find cg_open()", error
);
431 return __cg_open(path
, fi
);
434 static int do_cg_access(const char *path
, int mode
)
437 int (*__cg_access
)(const char *path
, int mode
);
440 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
443 return log_error(-1, "%s - Failed to find cg_access()", error
);
445 return __cg_access(path
, mode
);
448 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
451 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
454 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
457 return log_error(-1, "%s - Failed to find proc_open()", error
);
459 return __proc_open(path
, fi
);
462 static int do_proc_access(const char *path
, int mode
)
465 int (*__proc_access
)(const char *path
, int mode
);
468 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
471 return log_error(-1, "%s - Failed to find proc_access()", error
);
473 return __proc_access(path
, mode
);
476 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
479 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
482 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
485 return log_error(-1, "%s - Failed to find sys_open()", error
);
487 return __sys_open(path
, fi
);
490 static int do_sys_opendir(const char *path
, struct fuse_file_info
*fi
)
493 int (*__sys_opendir
)(const char *path
, struct fuse_file_info
*fi
);
496 __sys_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_opendir");
499 return log_error(-1, "%s - Failed to find sys_opendir()", error
);
501 return __sys_opendir(path
, fi
);
504 static int do_sys_access(const char *path
, int mode
)
507 int (*__sys_access
)(const char *path
, int mode
);
510 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
513 return log_error(-1, "%s - Failed to find sys_access()", error
);
515 return __sys_access(path
, mode
);
518 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
521 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
524 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
527 return log_error(-1, "%s - Failed to find cg_release()", error
);
529 return __cg_release(path
, fi
);
532 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
535 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
538 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
541 return log_error(-1, "%s - Failed to find proc_release()", error
);
543 return __proc_release(path
, fi
);
546 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
549 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
552 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
555 return log_error(-1, "%s - Failed to find sys_release()", error
);
557 return __sys_release(path
, fi
);
560 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
563 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
566 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
569 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
571 return __cg_opendir(path
, fi
);
574 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
577 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
580 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
583 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
585 return __cg_releasedir(path
, fi
);
588 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
591 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
594 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
597 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
599 return __sys_releasedir(path
, fi
);
603 static int lxcfs_getattr(const char *path
, struct stat
*sb
, struct fuse_file_info
*fi
)
605 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
611 if (strcmp(path
, "/") == 0) {
612 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
614 sb
->st_uid
= sb
->st_gid
= 0;
615 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
617 sb
->st_mode
= S_IFDIR
| 00755;
622 if (strncmp(path
, "/cgroup", 7) == 0) {
624 ret
= do_cg_getattr(path
, sb
);
629 if (strncmp(path
, "/proc", 5) == 0) {
631 ret
= do_proc_getattr(path
, sb
);
636 if (strncmp(path
, "/sys", 4) == 0) {
638 ret
= do_sys_getattr(path
, sb
);
646 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
650 if (strcmp(path
, "/") == 0)
653 if (strncmp(path
, "/cgroup", 7) == 0) {
655 ret
= do_cg_opendir(path
, fi
);
660 if (strcmp(path
, "/proc") == 0)
663 if (strncmp(path
, "/sys", 4) == 0) {
665 ret
= do_sys_opendir(path
, fi
);
674 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
675 off_t offset
, struct fuse_file_info
*fi
, enum fuse_readdir_flags flags
)
677 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
678 off_t offset
, struct fuse_file_info
*fi
)
683 if (strcmp(path
, "/") == 0) {
684 if (dir_filler(filler
, buf
, ".", 0) != 0 ||
685 dir_filler(filler
, buf
, "..", 0) != 0 ||
686 dir_filler(filler
, buf
, "proc", 0) != 0 ||
687 dir_filler(filler
, buf
, "sys", 0) != 0 ||
688 dir_filler(filler
, buf
, "cgroup", 0) != 0)
694 if (strncmp(path
, "/cgroup", 7) == 0) {
696 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
701 if (strcmp(path
, "/proc") == 0) {
703 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
708 if (strncmp(path
, "/sys", 4) == 0) {
710 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
718 static int lxcfs_access(const char *path
, int mode
)
722 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
725 if (strncmp(path
, "/cgroup", 7) == 0) {
727 ret
= do_cg_access(path
, mode
);
732 if (strncmp(path
, "/proc", 5) == 0) {
734 ret
= do_proc_access(path
, mode
);
739 if (strncmp(path
, "/sys", 4) == 0) {
741 ret
= do_sys_access(path
, mode
);
749 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
753 if (strcmp(path
, "/") == 0)
756 if (strncmp(path
, "/cgroup", 7) == 0) {
758 ret
= do_cg_releasedir(path
, fi
);
763 if (strcmp(path
, "/proc") == 0)
766 if (strncmp(path
, "/sys", 4) == 0) {
768 ret
= do_sys_releasedir(path
, fi
);
776 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
780 if (strncmp(path
, "/cgroup", 7) == 0) {
782 ret
= do_cg_open(path
, fi
);
787 if (strncmp(path
, "/proc", 5) == 0) {
789 ret
= do_proc_open(path
, fi
);
794 if (strncmp(path
, "/sys", 4) == 0) {
796 ret
= do_sys_open(path
, fi
);
804 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
805 struct fuse_file_info
*fi
)
809 if (strncmp(path
, "/cgroup", 7) == 0) {
811 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
816 if (strncmp(path
, "/proc", 5) == 0) {
818 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
823 if (strncmp(path
, "/sys", 4) == 0) {
825 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
833 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
834 struct fuse_file_info
*fi
)
838 if (strncmp(path
, "/cgroup", 7) == 0) {
840 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
845 if (strncmp(path
, "/sys", 4) == 0) {
847 ret
= do_sys_write(path
, buf
, size
, offset
, fi
);
855 int lxcfs_readlink(const char *path
, char *buf
, size_t size
)
859 if (strncmp(path
, "/sys", 4) == 0) {
861 ret
= do_sys_readlink(path
, buf
, size
);
869 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
874 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
878 if (strncmp(path
, "/cgroup", 7) == 0) {
880 ret
= do_cg_release(path
, fi
);
885 if (strncmp(path
, "/proc", 5) == 0) {
887 ret
= do_proc_release(path
, fi
);
892 if (strncmp(path
, "/sys", 4) == 0) {
894 ret
= do_sys_release(path
, fi
);
902 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
907 int lxcfs_mkdir(const char *path
, mode_t mode
)
911 if (strncmp(path
, "/cgroup", 7) == 0) {
913 ret
= do_cg_mkdir(path
, mode
);
922 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
, struct fuse_file_info
*fi
)
924 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
929 if (strncmp(path
, "/cgroup", 7) == 0) {
931 ret
= do_cg_chown(path
, uid
, gid
);
936 if (strncmp(path
, "/proc", 5) == 0)
939 if (strncmp(path
, "/sys", 4) == 0)
946 * cat first does a truncate before doing ops->write. This doesn't
947 * really make sense for cgroups. So just return 0 always but do
951 int lxcfs_truncate(const char *path
, off_t newsize
, struct fuse_file_info
*fi
)
953 int lxcfs_truncate(const char *path
, off_t newsize
)
956 if (strncmp(path
, "/cgroup", 7) == 0)
959 if (strncmp(path
, "/sys", 4) == 0)
965 int lxcfs_rmdir(const char *path
)
969 if (strncmp(path
, "/cgroup", 7) == 0) {
971 ret
= do_cg_rmdir(path
);
980 int lxcfs_chmod(const char *path
, mode_t mode
, struct fuse_file_info
*fi
)
982 int lxcfs_chmod(const char *path
, mode_t mode
)
987 if (strncmp(path
, "/cgroup", 7) == 0) {
989 ret
= do_cg_chmod(path
, mode
);
994 if (strncmp(path
, "/proc", 5) == 0)
997 if (strncmp(path
, "/sys", 4) == 0)
1004 static void *lxcfs_init(struct fuse_conn_info
*conn
, struct fuse_config
*cfg
)
1006 static void *lxcfs_init(struct fuse_conn_info
*conn
)
1010 void *(*__lxcfs_fuse_init
)(struct fuse_conn_info
* conn
, void * cfg
);
1013 __lxcfs_fuse_init
= (void *(*)(struct fuse_conn_info
* conn
, void * cfg
))dlsym(dlopen_handle
, "lxcfs_fuse_init");
1016 return log_error(NULL
, "%s - Failed to find lxcfs_fuse_init()", error
);
1018 return __lxcfs_fuse_init(conn
, NULL
);
1021 const struct fuse_operations lxcfs_ops
= {
1022 .access
= lxcfs_access
,
1023 .chmod
= lxcfs_chmod
,
1024 .chown
= lxcfs_chown
,
1025 .flush
= lxcfs_flush
,
1026 .fsync
= lxcfs_fsync
,
1027 .getattr
= lxcfs_getattr
,
1029 .mkdir
= lxcfs_mkdir
,
1031 .opendir
= lxcfs_opendir
,
1033 .readdir
= lxcfs_readdir
,
1034 .release
= lxcfs_release
,
1035 .releasedir
= lxcfs_releasedir
,
1036 .rmdir
= lxcfs_rmdir
,
1037 .truncate
= lxcfs_truncate
,
1038 .write
= lxcfs_write
,
1039 .readlink
= lxcfs_readlink
,
1056 .removexattr
= NULL
,
1066 static void usage(void)
1068 lxcfs_info("Usage: lxcfs <directory>\n");
1069 lxcfs_info("lxcfs is a FUSE-based proc, sys and cgroup virtualizing filesystem\n");
1070 lxcfs_info("Options :");
1071 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
1072 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
1073 lxcfs_info(" -n, --help Print help");
1074 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
1075 lxcfs_info(" -o Options to pass directly through fuse");
1076 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
1077 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH
);
1078 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
1079 lxcfs_info(" -v, --version Print lxcfs version");
1080 lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares");
1081 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
1085 static int set_pidfile(char *pidfile
)
1087 __do_close
int fd
= -EBADF
;
1088 char buf
[INTTYPE_TO_STRLEN(long)];
1092 .l_whence
= SEEK_SET
,
1097 fd
= open(pidfile
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| O_CLOEXEC
);
1099 return log_error(-1, "Could not open pidfile %s: %m", pidfile
);
1101 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
1102 if (errno
== EAGAIN
|| errno
== EACCES
)
1103 return log_error(-1, "PID file '%s' is already locked", pidfile
);
1104 lxcfs_error("Warning; unable to lock PID file, proceeding");
1107 if (ftruncate(fd
, 0))
1108 return log_error(-1, "Error truncating PID file '%s': %m", pidfile
);
1110 ret
= snprintf(buf
, sizeof(buf
), "%ld\n", (long)getpid());
1111 if (ret
< 0 || (size_t)ret
>= sizeof(buf
))
1112 return log_error(-1, "Failed to convert pid to string %m");
1114 if (write(fd
, buf
, ret
) != ret
)
1115 return log_error(-1, "Error writing to PID file '%s': %m", pidfile
);
1120 static const struct option long_options
[] = {
1121 {"debug", no_argument
, 0, 'd' },
1122 {"disable-swap", no_argument
, 0, 'u' },
1123 {"enable-loadavg", no_argument
, 0, 'l' },
1124 {"foreground", no_argument
, 0, 'f' },
1125 {"help", no_argument
, 0, 'h' },
1126 {"version", no_argument
, 0, 'v' },
1128 {"enable-cfs", no_argument
, 0, 0 },
1129 {"enable-pidfd", no_argument
, 0, 0 },
1131 {"pidfile", required_argument
, 0, 'p' },
1135 static int append_comma_separate(char **s
, const char *append
)
1139 size_t append_len
, len
;
1144 append_len
= strlen(append
);
1150 news
= realloc(*s
, len
+ append_len
+ 2);
1153 news
= realloc(NULL
, append_len
+ 1);
1159 ret
= snprintf(news
+ len
, append_len
+ 2, ",%s", append
);
1161 ret
= snprintf(news
, append_len
+ 1, "%s", append
);
1169 int main(int argc
, char *argv
[])
1171 int pidfile_fd
= -EBADF
;
1172 int ret
= EXIT_FAILURE
;
1173 char *pidfile
= NULL
, *token
= NULL
;
1174 char pidfile_buf
[STRLITERALLEN(RUNTIME_PATH
) + STRLITERALLEN("/lxcfs.pid") + 1] = {};
1175 bool debug
= false, foreground
= false;
1177 bool nonempty
= false;
1179 bool load_use
= false;
1181 * what we pass to fuse_main is:
1182 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1185 int c
, idx
, new_argc
;
1187 const char *fuse_opts
= NULL
;
1188 char *new_fuse_opts
= NULL
;
1189 char *const *new_argv
;
1190 struct lxcfs_opts
*opts
;
1192 opts
= malloc(sizeof(struct lxcfs_opts
));
1194 lxcfs_error("Error allocating memory for options");
1198 opts
->swap_off
= false;
1199 opts
->use_pidfd
= false;
1200 opts
->use_cfs
= false;
1203 while ((c
= getopt_long(argc
, argv
, "dulfhvso:p:", long_options
, &idx
)) != -1) {
1206 if (strcmp(long_options
[idx
].name
, "enable-pidfd") == 0)
1207 opts
->use_pidfd
= true;
1208 else if (strcmp(long_options
[idx
].name
, "enable-cfs") == 0)
1209 opts
->use_cfs
= true;
1224 lxcfs_error("Specifying -o multiple times is unsupported");
1234 /* legacy argument: ignore */
1237 opts
->swap_off
= true;
1240 lxcfs_info("%s", STRINGIFY(PROJECT_VERSION
));
1247 if (foreground
&& debug
)
1248 log_exit("Both --debug and --forgreound specified");
1250 new_argv
= &argv
[optind
];
1251 new_argc
= argc
- optind
;
1253 /* Older LXCFS versions printed help when used without any argument. */
1257 if (new_argc
!= 1) {
1258 lxcfs_error("Missing mountpoint");
1262 fuse_argv
[fuse_argc
++] = argv
[0];
1264 fuse_argv
[fuse_argc
++] = "-d";
1266 fuse_argv
[fuse_argc
++] = "-f";
1267 fuse_argv
[fuse_argc
++] = "-o";
1269 /* Parse additional fuse options. */
1273 dup
= strdup(fuse_opts
);
1275 lxcfs_error("Failed to copy fuse options");
1279 lxc_iterate_parts(token
, dup
, ",") {
1281 if (strcmp(token
, "allow_other") == 0)
1284 /* default for LXCFS */
1285 if (strcmp(token
, "direct_io") == 0)
1288 /* default for LXCFS */
1289 if (strncmp(token
, "entry_timeout", STRLITERALLEN("entry_timeout")) == 0)
1292 /* default for LXCFS */
1293 if (strncmp(token
, "attr_timeout", STRLITERALLEN("entry_timeout")) == 0)
1296 /* default for LXCFS */
1297 if (strncmp(token
, "allow_other", STRLITERALLEN("allow_other")) == 0)
1300 /* default with fuse3 */
1301 if (strcmp(token
, "nonempty") == 0) {
1308 if (append_comma_separate(&new_fuse_opts
, token
)) {
1309 lxcfs_error("Failed to copy fuse argument \"%s\"", token
);
1317 if (append_comma_separate(&new_fuse_opts
, "allow_other,entry_timeout=0.5,attr_timeout=0.5")) {
1318 lxcfs_error("Failed to copy fuse argument \"allow_other,entry_timeout=0.5,attr_timeout=0.5\"");
1324 if (append_comma_separate(&new_fuse_opts
, "nonempty")) {
1325 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1330 if (append_comma_separate(&new_fuse_opts
, "direct_io")) {
1331 lxcfs_error("Failed to copy fuse argument \"nonempty\"");
1337 * We can't use default_permissions since we still support systems that
1338 * don't have kernels with cgroup namespace support. On such kernels
1339 * lxcfs will provide a namespaced cgroup view and needs explicit
1340 * access helpers to make that work.
1341 * Another reason that came to me is that we can't or at least
1342 * shouldn't guarantee that we don't need more complicated access
1343 * helpers for proc and sys virtualization in the future.
1346 fuse_argv
[fuse_argc
++] = new_fuse_opts
;
1347 fuse_argv
[fuse_argc
++] = new_argv
[0];
1348 fuse_argv
[fuse_argc
] = NULL
;
1351 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1352 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1357 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1358 pidfile
= pidfile_buf
;
1361 pidfile_fd
= set_pidfile(pidfile
);
1365 if (load_use
&& start_loadavg() != 0)
1368 if (!fuse_main(fuse_argc
, fuse_argv
, &lxcfs_ops
, opts
))
1376 dlclose(dlopen_handle
);
1379 free(new_fuse_opts
);
1381 close_prot_errno_disarm(pidfile_fd
);