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 */
130 dlopen_handle
= dlopen("liblxcfs.so", RTLD_LAZY
);
132 lxcfs_debug("Opened liblxcfs.so");
137 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
138 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "%s/lxcfs/liblxcfs.so", LIBDIR
);
140 ret
= snprintf(lxcfs_lib_path
, sizeof(lxcfs_lib_path
), "/usr/local/lib/lxcfs/liblxcfs.so");
142 if (ret
< 0 || ret
>= sizeof(lxcfs_lib_path
))
143 log_exit("Failed to create path to open liblxcfs");
145 dlopen_handle
= dlopen(lxcfs_lib_path
, RTLD_LAZY
);
147 log_exit("%s - Failed to open liblxcfs.so", dlerror());
149 lxcfs_debug("Opened %s", lxcfs_lib_path
);
156 lxcfs_info("Reloaded LXCFS");
160 static void up_users(void)
163 if (users_count
== 0 && need_reload
)
169 static void down_users(void)
176 static void sigusr1_reload(int signo
, siginfo_t
*info
, void *extra
)
181 /* Functions to run the library methods */
182 static int do_cg_getattr(const char *path
, struct stat
*sb
)
185 int (*__cg_getattr
)(const char *path
, struct stat
*sb
);
188 __cg_getattr
= (int (*)(const char *, struct stat
*))dlsym(dlopen_handle
, "cg_getattr");
191 return log_error(-1, "%s - Failed to find cg_getattr()", error
);
193 return __cg_getattr(path
, sb
);
196 static int do_proc_getattr(const char *path
, struct stat
*sb
)
199 int (*__proc_getattr
)(const char *path
, struct stat
*sb
);
202 __proc_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "proc_getattr");
205 return log_error(-1, "%s - Failed to find proc_getattr()", error
);
207 return __proc_getattr(path
, sb
);
210 static int do_sys_getattr(const char *path
, struct stat
*sb
)
213 int (*__sys_getattr
)(const char *path
, struct stat
*sb
);
216 __sys_getattr
= (int (*)(const char *, struct stat
*)) dlsym(dlopen_handle
, "sys_getattr");
219 return log_error(-1, "%s - Failed to find sys_getattr()", error
);
221 return __sys_getattr(path
, sb
);
224 static int do_cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
225 struct fuse_file_info
*fi
)
228 int (*__cg_read
)(const char *path
, char *buf
, size_t size
, off_t offset
,
229 struct fuse_file_info
*fi
);
232 __cg_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_read");
235 return log_error(-1, "%s - Failed to find cg_read()", error
);
237 return __cg_read(path
, buf
, size
, offset
, fi
);
240 static int do_proc_read(const char *path
, char *buf
, size_t size
, off_t offset
,
241 struct fuse_file_info
*fi
)
244 int (*__proc_read
)(const char *path
, char *buf
, size_t size
,
245 off_t offset
, struct fuse_file_info
*fi
);
248 __proc_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_read");
251 return log_error(-1, "%s - Failed to find proc_read()", error
);
253 return __proc_read(path
, buf
, size
, offset
, fi
);
256 static int do_sys_read(const char *path
, char *buf
, size_t size
, off_t offset
,
257 struct fuse_file_info
*fi
)
260 int (*__sys_read
)(const char *path
, char *buf
, size_t size
,
261 off_t offset
, struct fuse_file_info
*fi
);
264 __sys_read
= (int (*)(const char *, char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_read");
267 return log_error(-1, "%s - Failed to find sys_read()", error
);
269 return __sys_read(path
, buf
, size
, offset
, fi
);
272 static int do_cg_write(const char *path
, const char *buf
, size_t size
,
273 off_t offset
, struct fuse_file_info
*fi
)
276 int (*__cg_write
)(const char *path
, const char *buf
, size_t size
,
277 off_t offset
, struct fuse_file_info
*fi
);
280 __cg_write
= (int (*)(const char *, const char *, size_t, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_write");
283 return log_error(-1, "%s - Failed to find cg_write()", error
);
285 return __cg_write(path
, buf
, size
, offset
, fi
);
288 static int do_cg_mkdir(const char *path
, mode_t mode
)
291 int (*__cg_mkdir
)(const char *path
, mode_t mode
);
294 __cg_mkdir
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_mkdir");
297 return log_error(-1, "%s - Failed to find cg_mkdir()", error
);
299 return __cg_mkdir(path
, mode
);
302 static int do_cg_chown(const char *path
, uid_t uid
, gid_t gid
)
305 int (*__cg_chown
)(const char *path
, uid_t uid
, gid_t gid
);
308 __cg_chown
= (int (*)(const char *, uid_t
, gid_t
))dlsym(dlopen_handle
, "cg_chown");
311 return log_error(-1, "%s - Failed to find cg_chown()", error
);
313 return __cg_chown(path
, uid
, gid
);
316 static int do_cg_rmdir(const char *path
)
319 int (*__cg_rmdir
)(const char *path
);
322 __cg_rmdir
= (int (*)(const char *path
))dlsym(dlopen_handle
, "cg_rmdir");
325 return log_error(-1, "%s - Failed to find cg_rmdir()", error
);
327 return __cg_rmdir(path
);
330 static int do_cg_chmod(const char *path
, mode_t mode
)
333 int (*__cg_chmod
)(const char *path
, mode_t mode
);
336 __cg_chmod
= (int (*)(const char *, mode_t
))dlsym(dlopen_handle
, "cg_chmod");
339 return log_error(-1, "%s - Failed to find cg_chmod()", error
);
341 return __cg_chmod(path
, mode
);
344 static int do_cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
345 off_t offset
, struct fuse_file_info
*fi
)
348 int (*__cg_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
349 off_t offset
, struct fuse_file_info
*fi
);
352 __cg_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_readdir");
355 return log_error(-1, "%s - Failed to find cg_readdir()", error
);
357 return __cg_readdir(path
, buf
, filler
, offset
, fi
);
360 static int do_proc_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
361 off_t offset
, struct fuse_file_info
*fi
)
364 int (*__proc_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
365 off_t offset
, struct fuse_file_info
*fi
);
368 __proc_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "proc_readdir");
371 return log_error(-1, "%s - Failed to find proc_readdir()", error
);
373 return __proc_readdir(path
, buf
, filler
, offset
, fi
);
376 static int do_sys_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
377 off_t offset
, struct fuse_file_info
*fi
)
380 int (*__sys_readdir
)(const char *path
, void *buf
, fuse_fill_dir_t filler
,
381 off_t offset
, struct fuse_file_info
*fi
);
384 __sys_readdir
= (int (*)(const char *, void *, fuse_fill_dir_t
, off_t
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_readdir");
387 return log_error(-1, "%s - Failed to find sys_readdir()", error
);
389 return __sys_readdir(path
, buf
, filler
, offset
, fi
);
393 static int do_cg_open(const char *path
, struct fuse_file_info
*fi
)
396 int (*__cg_open
)(const char *path
, struct fuse_file_info
*fi
);
399 __cg_open
= (int (*)(const char *, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_open");
402 return log_error(-1, "%s - Failed to find cg_open()", error
);
404 return __cg_open(path
, fi
);
407 static int do_cg_access(const char *path
, int mode
)
410 int (*__cg_access
)(const char *path
, int mode
);
413 __cg_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "cg_access");
416 return log_error(-1, "%s - Failed to find cg_access()", error
);
418 return __cg_access(path
, mode
);
421 static int do_proc_open(const char *path
, struct fuse_file_info
*fi
)
424 int (*__proc_open
)(const char *path
, struct fuse_file_info
*fi
);
427 __proc_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "proc_open");
430 return log_error(-1, "%s - Failed to find proc_open()", error
);
432 return __proc_open(path
, fi
);
435 static int do_proc_access(const char *path
, int mode
)
438 int (*__proc_access
)(const char *path
, int mode
);
441 __proc_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "proc_access");
444 return log_error(-1, "%s - Failed to find proc_access()", error
);
446 return __proc_access(path
, mode
);
449 static int do_sys_open(const char *path
, struct fuse_file_info
*fi
)
452 int (*__sys_open
)(const char *path
, struct fuse_file_info
*fi
);
455 __sys_open
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "sys_open");
458 return log_error(-1, "%s - Failed to find sys_open()", error
);
460 return __sys_open(path
, fi
);
463 static int do_sys_access(const char *path
, int mode
)
466 int (*__sys_access
)(const char *path
, int mode
);
469 __sys_access
= (int (*)(const char *, int mode
))dlsym(dlopen_handle
, "sys_access");
472 return log_error(-1, "%s - Failed to find sys_access()", error
);
474 return __sys_access(path
, mode
);
477 static int do_cg_release(const char *path
, struct fuse_file_info
*fi
)
480 int (*__cg_release
)(const char *path
, struct fuse_file_info
*fi
);
483 __cg_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_release");
486 return log_error(-1, "%s - Failed to find cg_release()", error
);
488 return __cg_release(path
, fi
);
491 static int do_proc_release(const char *path
, struct fuse_file_info
*fi
)
494 int (*__proc_release
)(const char *path
, struct fuse_file_info
*fi
);
497 __proc_release
= (int (*)(const char *path
, struct fuse_file_info
*)) dlsym(dlopen_handle
, "proc_release");
500 return log_error(-1, "%s - Failed to find proc_release()", error
);
502 return __proc_release(path
, fi
);
505 static int do_sys_release(const char *path
, struct fuse_file_info
*fi
)
508 int (*__sys_release
)(const char *path
, struct fuse_file_info
*fi
);
511 __sys_release
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_release");
514 return log_error(-1, "%s - Failed to find sys_release()", error
);
516 return __sys_release(path
, fi
);
519 static int do_cg_opendir(const char *path
, struct fuse_file_info
*fi
)
522 int (*__cg_opendir
)(const char *path
, struct fuse_file_info
*fi
);
525 __cg_opendir
= (int (*)(const char *path
, struct fuse_file_info
*fi
))dlsym(dlopen_handle
, "cg_opendir");
528 return log_error(-1, "%s - Failed to find cg_opendir()", error
);
530 return __cg_opendir(path
, fi
);
533 static int do_cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
536 int (*__cg_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
539 __cg_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "cg_releasedir");
542 return log_error(-1, "%s - Failed to find cg_releasedir()", error
);
544 return __cg_releasedir(path
, fi
);
547 static int do_sys_releasedir(const char *path
, struct fuse_file_info
*fi
)
550 int (*__sys_releasedir
)(const char *path
, struct fuse_file_info
*fi
);
553 __sys_releasedir
= (int (*)(const char *path
, struct fuse_file_info
*))dlsym(dlopen_handle
, "sys_releasedir");
556 return log_error(-1, "%s - Failed to find sys_releasedir()", error
);
558 return __sys_releasedir(path
, fi
);
561 static int lxcfs_getattr(const char *path
, struct stat
*sb
)
566 if (strcmp(path
, "/") == 0) {
567 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
569 sb
->st_uid
= sb
->st_gid
= 0;
570 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
572 sb
->st_mode
= S_IFDIR
| 00755;
577 if (strncmp(path
, "/cgroup", 7) == 0) {
579 ret
= do_cg_getattr(path
, sb
);
584 if (strncmp(path
, "/proc", 5) == 0) {
586 ret
= do_proc_getattr(path
, sb
);
591 if (strncmp(path
, "/sys", 4) == 0) {
593 ret
= do_sys_getattr(path
, sb
);
601 static int lxcfs_opendir(const char *path
, struct fuse_file_info
*fi
)
605 if (strcmp(path
, "/") == 0)
608 if (strncmp(path
, "/cgroup", 7) == 0) {
610 ret
= do_cg_opendir(path
, fi
);
615 if (strcmp(path
, "/proc") == 0)
618 if (strncmp(path
, "/sys", 4) == 0)
624 static int lxcfs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
625 off_t offset
, struct fuse_file_info
*fi
)
629 if (strcmp(path
, "/") == 0) {
630 if (filler(buf
, ".", NULL
, 0) != 0 ||
631 filler(buf
, "..", NULL
, 0) != 0 ||
632 filler(buf
, "proc", NULL
, 0) != 0 ||
633 filler(buf
, "sys", NULL
, 0) != 0 ||
634 filler(buf
, "cgroup", NULL
, 0) != 0)
640 if (strncmp(path
, "/cgroup", 7) == 0) {
642 ret
= do_cg_readdir(path
, buf
, filler
, offset
, fi
);
647 if (strcmp(path
, "/proc") == 0) {
649 ret
= do_proc_readdir(path
, buf
, filler
, offset
, fi
);
654 if (strncmp(path
, "/sys", 4) == 0) {
656 ret
= do_sys_readdir(path
, buf
, filler
, offset
, fi
);
664 static int lxcfs_access(const char *path
, int mode
)
668 if (strcmp(path
, "/") == 0 && (mode
& W_OK
) == 0)
671 if (strncmp(path
, "/cgroup", 7) == 0) {
673 ret
= do_cg_access(path
, mode
);
678 if (strncmp(path
, "/proc", 5) == 0) {
680 ret
= do_proc_access(path
, mode
);
685 if (strncmp(path
, "/sys", 4) == 0) {
687 ret
= do_sys_access(path
, mode
);
695 static int lxcfs_releasedir(const char *path
, struct fuse_file_info
*fi
)
699 if (strcmp(path
, "/") == 0)
702 if (strncmp(path
, "/cgroup", 7) == 0) {
704 ret
= do_cg_releasedir(path
, fi
);
709 if (strcmp(path
, "/proc") == 0)
712 if (strncmp(path
, "/sys", 4) == 0) {
714 ret
= do_sys_releasedir(path
, fi
);
722 static int lxcfs_open(const char *path
, struct fuse_file_info
*fi
)
726 if (strncmp(path
, "/cgroup", 7) == 0) {
728 ret
= do_cg_open(path
, fi
);
733 if (strncmp(path
, "/proc", 5) == 0) {
735 ret
= do_proc_open(path
, fi
);
740 if (strncmp(path
, "/sys", 4) == 0) {
742 ret
= do_sys_open(path
, fi
);
750 static int lxcfs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
751 struct fuse_file_info
*fi
)
755 if (strncmp(path
, "/cgroup", 7) == 0) {
757 ret
= do_cg_read(path
, buf
, size
, offset
, fi
);
762 if (strncmp(path
, "/proc", 5) == 0) {
764 ret
= do_proc_read(path
, buf
, size
, offset
, fi
);
769 if (strncmp(path
, "/sys", 4) == 0) {
771 ret
= do_sys_read(path
, buf
, size
, offset
, fi
);
779 int lxcfs_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
780 struct fuse_file_info
*fi
)
784 if (strncmp(path
, "/cgroup", 7) == 0) {
786 ret
= do_cg_write(path
, buf
, size
, offset
, fi
);
794 static int lxcfs_flush(const char *path
, struct fuse_file_info
*fi
)
799 static int lxcfs_release(const char *path
, struct fuse_file_info
*fi
)
803 if (strncmp(path
, "/cgroup", 7) == 0) {
805 ret
= do_cg_release(path
, fi
);
810 if (strncmp(path
, "/proc", 5) == 0) {
812 ret
= do_proc_release(path
, fi
);
817 if (strncmp(path
, "/sys", 4) == 0) {
819 ret
= do_sys_release(path
, fi
);
827 static int lxcfs_fsync(const char *path
, int datasync
, struct fuse_file_info
*fi
)
832 int lxcfs_mkdir(const char *path
, mode_t mode
)
836 if (strncmp(path
, "/cgroup", 7) == 0) {
838 ret
= do_cg_mkdir(path
, mode
);
846 int lxcfs_chown(const char *path
, uid_t uid
, gid_t gid
)
850 if (strncmp(path
, "/cgroup", 7) == 0) {
852 ret
= do_cg_chown(path
, uid
, gid
);
857 if (strncmp(path
, "/proc", 5) == 0)
860 if (strncmp(path
, "/sys", 4) == 0)
867 * cat first does a truncate before doing ops->write. This doesn't
868 * really make sense for cgroups. So just return 0 always but do
871 int lxcfs_truncate(const char *path
, off_t newsize
)
873 if (strncmp(path
, "/cgroup", 7) == 0)
879 int lxcfs_rmdir(const char *path
)
883 if (strncmp(path
, "/cgroup", 7) == 0) {
885 ret
= do_cg_rmdir(path
);
893 int lxcfs_chmod(const char *path
, mode_t mode
)
897 if (strncmp(path
, "/cgroup", 7) == 0) {
899 ret
= do_cg_chmod(path
, mode
);
904 if (strncmp(path
, "/proc", 5) == 0)
907 if (strncmp(path
, "/sys", 4) == 0)
913 const struct fuse_operations lxcfs_ops
= {
914 .access
= lxcfs_access
,
915 .chmod
= lxcfs_chmod
,
916 .chown
= lxcfs_chown
,
917 .flush
= lxcfs_flush
,
918 .fsync
= lxcfs_fsync
,
919 .getattr
= lxcfs_getattr
,
920 .mkdir
= lxcfs_mkdir
,
922 .opendir
= lxcfs_opendir
,
924 .readdir
= lxcfs_readdir
,
925 .release
= lxcfs_release
,
926 .releasedir
= lxcfs_releasedir
,
927 .rmdir
= lxcfs_rmdir
,
928 .truncate
= lxcfs_truncate
,
929 .write
= lxcfs_write
,
954 lxcfs_info("Usage: lxcfs <directory>\n");
955 lxcfs_info("lxcfs is a FUSE-based proc, sys and cgroup virtualizing filesystem\n");
956 lxcfs_info("Options :");
957 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
958 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
959 lxcfs_info(" -n, --help Print help");
960 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
961 lxcfs_info(" -o Options to pass directly through fuse");
962 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
963 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH
);
964 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
965 lxcfs_info(" -v, --version Print lxcfs version");
966 lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares");
967 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
971 static inline bool is_help(char *w
)
973 return strcmp(w
, "-h") == 0 ||
974 strcmp(w
, "--help") == 0 ||
975 strcmp(w
, "-help") == 0 ||
976 strcmp(w
, "help") == 0;
979 static inline bool is_version(char *w
)
981 return strcmp(w
, "-v") == 0 ||
982 strcmp(w
, "--version") == 0 ||
983 strcmp(w
, "-version") == 0 ||
984 strcmp(w
, "version") == 0;
987 static bool swallow_arg(int *argcp
, char *argv
[], char *which
)
989 for (int i
= 1; argv
[i
]; i
++) {
990 if (strcmp(argv
[i
], which
) != 0)
994 argv
[i
] = argv
[i
+ 1];
1003 static bool swallow_option(int *argcp
, char *argv
[], char *opt
, char **v
)
1005 for (int i
= 1; argv
[i
]; i
++) {
1009 if (strcmp(argv
[i
], opt
) != 0)
1013 *v
= strdup(argv
[i
+ 1]);
1016 for (; argv
[i
+ 1]; i
++)
1017 argv
[i
] = argv
[i
+ 2];
1026 static int set_pidfile(char *pidfile
)
1028 __do_close
int fd
= -EBADF
;
1029 char buf
[INTTYPE_TO_STRLEN(long)];
1033 .l_whence
= SEEK_SET
,
1038 fd
= open(pidfile
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| O_CLOEXEC
);
1040 return log_error(-1, "Could not open pidfile %s: %m", pidfile
);
1042 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
1043 if (errno
== EAGAIN
|| errno
== EACCES
)
1044 return log_error(-1, "PID file '%s' is already locked", pidfile
);
1045 lxcfs_error("Warning; unable to lock PID file, proceeding");
1048 if (ftruncate(fd
, 0))
1049 return log_error(-1, "Error truncating PID file '%s': %m", pidfile
);
1051 ret
= snprintf(buf
, sizeof(buf
), "%ld\n", (long)getpid());
1052 if (ret
< 0 || ret
>= sizeof(buf
))
1053 return log_error(-1, "Failed to convert pid to string %m");
1055 if (write(fd
, buf
, ret
) != ret
)
1056 return log_error(-1, "Error writing to PID file '%s': %m", pidfile
);
1061 int main(int argc
, char *argv
[])
1063 int pidfile_fd
= -EBADF
;
1064 int ret
= EXIT_FAILURE
;
1065 char *pidfile
= NULL
, *saveptr
= NULL
, *token
= NULL
, *v
= NULL
;
1066 char pidfile_buf
[STRLITERALLEN(RUNTIME_PATH
) + STRLITERALLEN("/lxcfs.pid") + 1] = {};
1067 bool debug
= false, foreground
= false, nonempty
= false;
1068 bool load_use
= false;
1070 * what we pass to fuse_main is:
1071 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1073 int nargs
= 5, cnt
= 0;
1075 struct lxcfs_opts
*opts
;
1077 opts
= malloc(sizeof(struct lxcfs_opts
));
1079 lxcfs_error("Error allocating memory for options");
1082 opts
->swap_off
= false;
1083 opts
->use_pidfd
= false;
1084 opts
->use_cfs
= false;
1086 /* accomodate older init scripts */
1087 swallow_arg(&argc
, argv
, "-s");
1089 /* -f / --foreground */
1090 foreground
= swallow_arg(&argc
, argv
, "-f");
1091 if (swallow_arg(&argc
, argv
, "--foreground"))
1095 debug
= swallow_arg(&argc
, argv
, "-d");
1096 if (swallow_arg(&argc
, argv
, "--debug"))
1099 if (foreground
&& debug
)
1100 log_exit("Both --debug and --forgreound specified");
1102 /* -l / --enable-loadavg */
1103 load_use
= swallow_arg(&argc
, argv
, "-l");
1104 if (swallow_arg(&argc
, argv
, "--enable-loadavg"))
1107 /* -u / --disable-swap */
1108 opts
->swap_off
= swallow_arg(&argc
, argv
, "-u");
1109 if (swallow_arg(&argc
, argv
, "--disable-swap"))
1110 opts
->swap_off
= true;
1112 /* --enable-pidfd */
1113 opts
->use_pidfd
= swallow_arg(&argc
, argv
, "--enable-pidfd");
1116 if (swallow_arg(&argc
, argv
, "--enable-cfs"))
1117 opts
->use_cfs
= true;
1119 if (swallow_option(&argc
, argv
, "-o", &v
)) {
1120 /* Parse multiple values */
1121 for (; (token
= strtok_r(v
, ",", &saveptr
)); v
= NULL
) {
1122 if (strcmp(token
, "allow_other") == 0) {
1123 /* Noop. this is the default. Always enabled. */
1124 } else if (strcmp(token
, "nonempty") == 0) {
1127 lxcfs_error("Warning: unexpected fuse option %s", v
);
1136 /* -p / --pidfile */
1137 if (swallow_option(&argc
, argv
, "-p", &v
))
1139 if (!pidfile
&& swallow_option(&argc
, argv
, "--pidfile", &v
))
1142 if (argc
== 2 && is_version(argv
[1])) {
1143 lxcfs_info("%s", VERSION
);
1147 if (argc
!= 2 || is_help(argv
[1]))
1151 if (install_signal_handler(SIGUSR1
, sigusr1_reload
)) {
1152 lxcfs_error("%s - Failed to install SIGUSR1 signal handler", strerror(errno
));
1156 newargv
[cnt
++] = argv
[0];
1158 newargv
[cnt
++] = "-d";
1160 newargv
[cnt
++] = "-f";
1161 newargv
[cnt
++] = "-o";
1164 * We can't use default_permissions since we still support systems that
1165 * don't have kernels with cgroup namespace support. On such kernels
1166 * lxcfs will provide a namespaced cgroup view and needs explicit
1167 * access helpers to make that work.
1168 * Another reason that came to me is that we can't or at least
1169 * shouldn't guarantee that we don't need more complicated access
1170 * helpers for proc and sys virtualization in the future.
1173 newargv
[cnt
++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5,nonempty";
1175 newargv
[cnt
++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5";
1176 newargv
[cnt
++] = argv
[1];
1177 newargv
[cnt
++] = NULL
;
1180 snprintf(pidfile_buf
, sizeof(pidfile_buf
), "%s/lxcfs.pid", RUNTIME_PATH
);
1181 pidfile
= pidfile_buf
;
1184 pidfile_fd
= set_pidfile(pidfile
);
1188 if (load_use
&& start_loadavg() != 0)
1191 if (!fuse_main(nargs
, newargv
, &lxcfs_ops
, opts
))
1198 dlclose(dlopen_handle
);
1202 close_prot_errno_disarm(pidfile_fd
);