1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
11 #define _FILE_OFFSET_BITS 64
29 #include <linux/sched.h>
30 #include <sys/epoll.h>
31 #include <sys/mount.h>
32 #include <sys/socket.h>
33 #include <linux/limits.h>
38 #include "memory_utils.h"
42 /* Functions to keep track of number of threads using the library */
44 static int users_count
;
45 static pthread_mutex_t user_count_mutex
= PTHREAD_MUTEX_INITIALIZER
;
46 static void lock_mutex(pthread_mutex_t
*l
)
50 ret
= pthread_mutex_lock(l
);
52 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
55 static void unlock_mutex(pthread_mutex_t
*l
)
59 ret
= pthread_mutex_unlock(l
);
61 log_exit("%s - returned: %d\n", strerror(ret
), ret
);
64 static inline void users_lock(void)
66 lock_mutex(&user_count_mutex
);
69 static inline void users_unlock(void)
71 unlock_mutex(&user_count_mutex
);
74 static pthread_t loadavg_pid
= 0;
76 /* Returns zero on success */
77 static int start_loadavg(void)
80 pthread_t (*__load_daemon
)(int);
83 __load_daemon
= (pthread_t(*)(int))dlsym(dlopen_handle
, "load_daemon");
86 return log_error(-1, "%s - Failed to start loadavg daemon", error
);
88 loadavg_pid
= __load_daemon(1);
95 /* Returns zero on success */
96 static int stop_loadavg(void)
99 int (*__stop_load_daemon
)(pthread_t
);
101 __stop_load_daemon
= (int (*)(pthread_t
))dlsym(dlopen_handle
, "stop_load_daemon");
104 return log_error(-1, "%s - Failed to stop loadavg daemon", error
);
106 if (__stop_load_daemon(loadavg_pid
))
112 static volatile sig_atomic_t need_reload
;
114 /* do_reload - reload the dynamic library. Done under
115 * lock and when we know the user_count was 0 */
116 static void do_reload(void)
119 char lxcfs_lib_path
[PATH_MAX
];
125 lxcfs_info("Closed liblxcfs.so");
126 dlclose(dlopen_handle
);
129 /* First try loading using ld.so */
131 dlopen_handle
= dlopen("liblxcfs.so", RTLD_NOW
);
133 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
136 lxcfs_debug("Opened liblxcfs.so");
141 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
142 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
144 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
146 if (ret
< 0 || ret
>= sizeof(lxcfs_lib_path
))
147 log_exit("Failed to create path to open liblxcfs");
149 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
151 log_exit("%s - Failed to open liblxcfs.so", dlerror());
153 lxcfs_debug("Opened %s", lxcfs_lib_path
);
160 lxcfs_info("Reloaded LXCFS");
164 static void up_users(void)
167 if (users_count
== 0 && need_reload
)
173 static void down_users(void)
180 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
185 /* Functions to run the library methods */
186 static int do_cg_getattr(const char *path
, struct stat
*sb
)
189 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
192 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
195 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
197 return __cg_getattr(path
, sb
);
200 static int do_proc_getattr(const char *path
, struct stat
*sb
)
203 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
206 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
209 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
211 return __proc_getattr(path
, sb
);
214 static int do_sys_getattr(const char *path
, struct stat
*sb
)
217 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
220 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
223 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
225 return __sys_getattr(path
, sb
);
228 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
229 struct fuse_file_info
*fi
)
232 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
233 struct fuse_file_info
*fi
);
236 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
239 return log_error(-1, "%s - Failed to find cg_read()", error
);
241 return __cg_read(path
, buf
, size
, offset
, fi
);
244 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
245 struct fuse_file_info
*fi
)
248 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
249 off_t offset
, struct fuse_file_info
*fi
);
252 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
255 return log_error(-1, "%s - Failed to find proc_read()", error
);
257 return __proc_read(path
, buf
, size
, offset
, fi
);
260 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
261 struct fuse_file_info
*fi
)
264 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
265 off_t offset
, struct fuse_file_info
*fi
);
268 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
271 return log_error(-1, "%s - Failed to find sys_read()", error
);
273 return __sys_read(path
, buf
, size
, offset
, fi
);
276 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
277 off_t offset
, struct fuse_file_info
*fi
)
280 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
281 off_t offset
, struct fuse_file_info
*fi
);
284 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
287 return log_error(-1, "%s - Failed to find cg_write()", error
);
289 return __cg_write(path
, buf
, size
, offset
, fi
);
292 static int do_cg_mkdir(const char *path
, mode_t mode
)
295 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
298 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
301 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
303 return __cg_mkdir(path
, mode
);
306 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
309 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
312 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
315 return log_error(-1, "%s - Failed to find cg_chown()", error
);
317 return __cg_chown(path
, uid
, gid
);
320 static int do_cg_rmdir(const char *path
)
323 int (*__cg_rmdir
)(const char *path
);
326 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
329 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
331 return __cg_rmdir(path
);
334 static int do_cg_chmod(const char *path
, mode_t mode
)
337 int (*__cg_chmod
)(const char *path
, mode_t mode
);
340 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
343 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
345 return __cg_chmod(path
, mode
);
348 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
349 off_t offset
, struct fuse_file_info
*fi
)
352 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
353 off_t offset
, struct fuse_file_info
*fi
);
356 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
359 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
361 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
364 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
365 off_t offset
, struct fuse_file_info
*fi
)
368 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
369 off_t offset
, struct fuse_file_info
*fi
);
372 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
375 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
377 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
380 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
381 off_t offset
, struct fuse_file_info
*fi
)
384 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
385 off_t offset
, struct fuse_file_info
*fi
);
388 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
391 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
393 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
397 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
400 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
403 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
406 return log_error(-1, "%s - Failed to find cg_open()", error
);
408 return __cg_open(path
, fi
);
411 static int do_cg_access(const char *path
, int mode
)
414 int (*__cg_access
)(const char *path
, int mode
);
417 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
420 return log_error(-1, "%s - Failed to find cg_access()", error
);
422 return __cg_access(path
, mode
);
425 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
428 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
431 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
434 return log_error(-1, "%s - Failed to find proc_open()", error
);
436 return __proc_open(path
, fi
);
439 static int do_proc_access(const char *path
, int mode
)
442 int (*__proc_access
)(const char *path
, int mode
);
445 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
448 return log_error(-1, "%s - Failed to find proc_access()", error
);
450 return __proc_access(path
, mode
);
453 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
456 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
459 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
462 return log_error(-1, "%s - Failed to find sys_open()", error
);
464 return __sys_open(path
, fi
);
467 static int do_sys_access(const char *path
, int mode
)
470 int (*__sys_access
)(const char *path
, int mode
);
473 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
476 return log_error(-1, "%s - Failed to find sys_access()", error
);
478 return __sys_access(path
, mode
);
481 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
484 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
487 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
490 return log_error(-1, "%s - Failed to find cg_release()", error
);
492 return __cg_release(path
, fi
);
495 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
498 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
501 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
504 return log_error(-1, "%s - Failed to find proc_release()", error
);
506 return __proc_release(path
, fi
);
509 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
512 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
515 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
518 return log_error(-1, "%s - Failed to find sys_release()", error
);
520 return __sys_release(path
, fi
);
523 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
526 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
529 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
532 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
534 return __cg_opendir(path
, fi
);
537 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
540 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
543 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
546 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
548 return __cg_releasedir(path
, fi
);
551 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
554 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
557 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
560 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
562 return __sys_releasedir(path
, fi
);
565 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
570 if (strcmp(path
, "/") == 0) {
571 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
573 sb
->st_uid
= sb
->st_gid
= 0;
574 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
576 sb
->st_mode
= S_IFDIR
| 00755;
581 if (strncmp(path
, "/cgroup", 7) == 0) {
583 ret
= do_cg_getattr(path
, sb
);
588 if (strncmp(path
, "/proc", 5) == 0) {
590 ret
= do_proc_getattr(path
, sb
);
595 if (strncmp(path
, "/sys", 4) == 0) {
597 ret
= do_sys_getattr(path
, sb
);
605 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
609 if (strcmp(path
, "/") == 0)
612 if (strncmp(path
, "/cgroup", 7) == 0) {
614 ret
= do_cg_opendir(path
, fi
);
619 if (strcmp(path
, "/proc") == 0)
622 if (strncmp(path
, "/sys", 4) == 0)
628 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
629 off_t offset
, struct fuse_file_info
*fi
)
633 if (strcmp(path
, "/") == 0) {
634 if (filler(buf
, ".", NULL
, 0) != 0 ||
635 filler(buf
, "..", NULL
, 0) != 0 ||
636 filler(buf
, "proc", NULL
, 0) != 0 ||
637 filler(buf
, "sys", NULL
, 0) != 0 ||
638 filler(buf
, "cgroup", NULL
, 0) != 0)
644 if (strncmp(path
, "/cgroup", 7) == 0) {
646 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
651 if (strcmp(path
, "/proc") == 0) {
653 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
658 if (strncmp(path
, "/sys", 4) == 0) {
660 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
668 static int lxcfs_access(const char *path
, int mode
)
672 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
675 if (strncmp(path
, "/cgroup", 7) == 0) {
677 ret
= do_cg_access(path
, mode
);
682 if (strncmp(path
, "/proc", 5) == 0) {
684 ret
= do_proc_access(path
, mode
);
689 if (strncmp(path
, "/sys", 4) == 0) {
691 ret
= do_sys_access(path
, mode
);
699 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
703 if (strcmp(path
, "/") == 0)
706 if (strncmp(path
, "/cgroup", 7) == 0) {
708 ret
= do_cg_releasedir(path
, fi
);
713 if (strcmp(path
, "/proc") == 0)
716 if (strncmp(path
, "/sys", 4) == 0) {
718 ret
= do_sys_releasedir(path
, fi
);
726 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
730 if (strncmp(path
, "/cgroup", 7) == 0) {
732 ret
= do_cg_open(path
, fi
);
737 if (strncmp(path
, "/proc", 5) == 0) {
739 ret
= do_proc_open(path
, fi
);
744 if (strncmp(path
, "/sys", 4) == 0) {
746 ret
= do_sys_open(path
, fi
);
754 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
755 struct fuse_file_info
*fi
)
759 if (strncmp(path
, "/cgroup", 7) == 0) {
761 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
766 if (strncmp(path
, "/proc", 5) == 0) {
768 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
773 if (strncmp(path
, "/sys", 4) == 0) {
775 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
783 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
784 struct fuse_file_info
*fi
)
788 if (strncmp(path
, "/cgroup", 7) == 0) {
790 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
798 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
803 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
807 if (strncmp(path
, "/cgroup", 7) == 0) {
809 ret
= do_cg_release(path
, fi
);
814 if (strncmp(path
, "/proc", 5) == 0) {
816 ret
= do_proc_release(path
, fi
);
821 if (strncmp(path
, "/sys", 4) == 0) {
823 ret
= do_sys_release(path
, fi
);
831 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
836 int lxcfs_mkdir(const char *path
, mode_t mode
)
840 if (strncmp(path
, "/cgroup", 7) == 0) {
842 ret
= do_cg_mkdir(path
, mode
);
850 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
854 if (strncmp(path
, "/cgroup", 7) == 0) {
856 ret
= do_cg_chown(path
, uid
, gid
);
861 if (strncmp(path
, "/proc", 5) == 0)
864 if (strncmp(path
, "/sys", 4) == 0)
871 * cat first does a truncate before doing ops->write. This doesn't
872 * really make sense for cgroups. So just return 0 always but do
875 int lxcfs_truncate(const char *path
, off_t newsize
)
877 if (strncmp(path
, "/cgroup", 7) == 0)
883 int lxcfs_rmdir(const char *path
)
887 if (strncmp(path
, "/cgroup", 7) == 0) {
889 ret
= do_cg_rmdir(path
);
897 int lxcfs_chmod(const char *path
, mode_t mode
)
901 if (strncmp(path
, "/cgroup", 7) == 0) {
903 ret
= do_cg_chmod(path
, mode
);
908 if (strncmp(path
, "/proc", 5) == 0)
911 if (strncmp(path
, "/sys", 4) == 0)
917 const struct fuse_operations lxcfs_ops
= {
918 .access
= lxcfs_access
,
919 .chmod
= lxcfs_chmod
,
920 .chown
= lxcfs_chown
,
921 .flush
= lxcfs_flush
,
922 .fsync
= lxcfs_fsync
,
923 .getattr
= lxcfs_getattr
,
924 .mkdir
= lxcfs_mkdir
,
926 .opendir
= lxcfs_opendir
,
928 .readdir
= lxcfs_readdir
,
929 .release
= lxcfs_release
,
930 .releasedir
= lxcfs_releasedir
,
931 .rmdir
= lxcfs_rmdir
,
932 .truncate
= lxcfs_truncate
,
933 .write
= lxcfs_write
,
958 lxcfs_info("Usage: lxcfs <directory>\n");
959 lxcfs_info("lxcfs is a FUSE-based proc, sys and cgroup virtualizing filesystem\n");
960 lxcfs_info("Options :");
961 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
962 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
963 lxcfs_info(" -n, --help Print help");
964 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
965 lxcfs_info(" -o Options to pass directly through fuse");
966 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
967 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH
);
968 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
969 lxcfs_info(" -v, --version Print lxcfs version");
970 lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares");
971 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
975 static inline bool is_help(char *w
)
977 return strcmp(w
, "-h") == 0 ||
978 strcmp(w
, "--help") == 0 ||
979 strcmp(w
, "-help") == 0 ||
980 strcmp(w
, "help") == 0;
983 static inline bool is_version(char *w
)
985 return strcmp(w
, "-v") == 0 ||
986 strcmp(w
, "--version") == 0 ||
987 strcmp(w
, "-version") == 0 ||
988 strcmp(w
, "version") == 0;
991 static bool swallow_arg(int *argcp
, char *argv
[], char *which
)
993 for (int i
= 1; argv
[i
]; i
++) {
994 if (strcmp(argv
[i
], which
) != 0)
998 argv
[i
] = argv
[i
+ 1];
1007 static bool swallow_option(int *argcp
, char *argv
[], char *opt
, char **v
)
1009 for (int i
= 1; argv
[i
]; i
++) {
1013 if (strcmp(argv
[i
], opt
) != 0)
1017 *v
= strdup(argv
[i
+ 1]);
1020 for (; argv
[i
+ 1]; i
++)
1021 argv
[i
] = argv
[i
+ 2];
1030 static int set_pidfile(char *pidfile
)
1032 __do_close
int fd
= -EBADF
;
1033 char buf
[INTTYPE_TO_STRLEN(long)];
1037 .l_whence
= SEEK_SET
,
1042 fd
= open(pidfile
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| O_CLOEXEC
);
1044 return log_error(-1, "Could not open pidfile %s: %m", pidfile
);
1046 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
1047 if (errno
== EAGAIN
|| errno
== EACCES
)
1048 return log_error(-1, "PID file '%s' is already locked", pidfile
);
1049 lxcfs_error("Warning; unable to lock PID file, proceeding");
1052 if (ftruncate(fd
, 0))
1053 return log_error(-1, "Error truncating PID file '%s': %m", pidfile
);
1055 ret
= snprintf(buf
, sizeof(buf
), "%ld\n", (long)getpid());
1056 if (ret
< 0 || ret
>= sizeof(buf
))
1057 return log_error(-1, "Failed to convert pid to string %m");
1059 if (write(fd
, buf
, ret
) != ret
)
1060 return log_error(-1, "Error writing to PID file '%s': %m", pidfile
);
1065 int main(int argc
, char *argv
[])
1067 int pidfile_fd
= -EBADF
;
1068 int ret
= EXIT_FAILURE
;
1069 char *pidfile
= NULL
, *saveptr
= NULL
, *token
= NULL
, *v
= NULL
;
1070 char pidfile_buf
[STRLITERALLEN(RUNTIME_PATH
) + STRLITERALLEN("/lxcfs.pid") + 1] = {};
1071 bool debug
= false, foreground
= false, nonempty
= false;
1072 bool load_use
= false;
1074 * what we pass to fuse_main is:
1075 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1077 int nargs
= 5, cnt
= 0;
1079 struct lxcfs_opts
*opts
;
1081 opts
= malloc(sizeof(struct lxcfs_opts
));
1083 lxcfs_error("Error allocating memory for options");
1086 opts
->swap_off
= false;
1087 opts
->use_pidfd
= false;
1088 opts
->use_cfs
= false;
1090 /* accomodate older init scripts */
1091 swallow_arg(&argc
, argv
, "-s");
1093 /* -f / --foreground */
1094 foreground
= swallow_arg(&argc
, argv
, "-f");
1095 if (swallow_arg(&argc
, argv
, "--foreground"))
1099 debug
= swallow_arg(&argc
, argv
, "-d");
1100 if (swallow_arg(&argc
, argv
, "--debug"))
1103 if (foreground
&& debug
)
1104 log_exit("Both --debug and --forgreound specified");
1106 /* -l / --enable-loadavg */
1107 load_use
= swallow_arg(&argc
, argv
, "-l");
1108 if (swallow_arg(&argc
, argv
, "--enable-loadavg"))
1111 /* -u / --disable-swap */
1112 opts
->swap_off
= swallow_arg(&argc
, argv
, "-u");
1113 if (swallow_arg(&argc
, argv
, "--disable-swap"))
1114 opts
->swap_off
= true;
1116 /* --enable-pidfd */
1117 opts
->use_pidfd
= swallow_arg(&argc
, argv
, "--enable-pidfd");
1120 if (swallow_arg(&argc
, argv
, "--enable-cfs"))
1121 opts
->use_cfs
= true;
1123 if (swallow_option(&argc
, argv
, "-o", &v
)) {
1124 /* Parse multiple values */
1125 for (; (token
= strtok_r(v
, ",", &saveptr
)); v
= NULL
) {
1126 if (strcmp(token
, "allow_other") == 0) {
1127 /* Noop. this is the default. Always enabled. */
1128 } else if (strcmp(token
, "nonempty") == 0) {
1131 lxcfs_error("Warning: unexpected fuse option %s", v
);
1140 /* -p / --pidfile */
1141 if (swallow_option(&argc
, argv
, "-p", &v
))
1143 if (!pidfile
&& swallow_option(&argc
, argv
, "--pidfile", &v
))
1146 if (argc
== 2 && is_version(argv
[1])) {
1147 lxcfs_info("%s", VERSION
);
1151 if (argc
!= 2 || is_help(argv
[1]))
1155 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1156 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1160 newargv
[cnt
++] = argv
[0];
1162 newargv
[cnt
++] = "-d";
1164 newargv
[cnt
++] = "-f";
1165 newargv
[cnt
++] = "-o";
1168 * We can't use default_permissions since we still support systems that
1169 * don't have kernels with cgroup namespace support. On such kernels
1170 * lxcfs will provide a namespaced cgroup view and needs explicit
1171 * access helpers to make that work.
1172 * Another reason that came to me is that we can't or at least
1173 * shouldn't guarantee that we don't need more complicated access
1174 * helpers for proc and sys virtualization in the future.
1177 newargv
[cnt
++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5,nonempty";
1179 newargv
[cnt
++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5";
1180 newargv
[cnt
++] = argv
[1];
1181 newargv
[cnt
++] = NULL
;
1184 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1185 pidfile
= pidfile_buf
;
1188 pidfile_fd
= set_pidfile(pidfile
);
1192 if (load_use
&& start_loadavg() != 0)
1195 if (!fuse_main(nargs
, newargv
, &lxcfs_ops
, opts
))
1202 dlclose(dlopen_handle
);
1206 close_prot_errno_disarm(pidfile_fd
);