]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/lxcfs.c
Merge pull request #364 from brauner/master
[mirror_lxcfs.git] / src / lxcfs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE
5 #endif
6
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
9 #endif
10
11 #define _FILE_OFFSET_BITS 64
12
13 #include <alloca.h>
14 #include <dirent.h>
15 #include <dlfcn.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <fuse.h>
19 #include <libgen.h>
20 #include <pthread.h>
21 #include <sched.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <wait.h>
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>
34
35 #include "bindings.h"
36 #include "config.h"
37 #include "macro.h"
38 #include "memory_utils.h"
39
40 void *dlopen_handle;
41
42 /* Functions to keep track of number of threads using the library */
43
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)
47 {
48 int ret;
49
50 ret = pthread_mutex_lock(l);
51 if (ret)
52 log_exit("%s - returned: %d\n", strerror(ret), ret);
53 }
54
55 static void unlock_mutex(pthread_mutex_t *l)
56 {
57 int ret;
58
59 ret = pthread_mutex_unlock(l);
60 if (ret)
61 log_exit("%s - returned: %d\n", strerror(ret), ret);
62 }
63
64 static inline void users_lock(void)
65 {
66 lock_mutex(&user_count_mutex);
67 }
68
69 static inline void users_unlock(void)
70 {
71 unlock_mutex(&user_count_mutex);
72 }
73
74 static pthread_t loadavg_pid = 0;
75
76 /* Returns zero on success */
77 static int start_loadavg(void)
78 {
79 char *error;
80 pthread_t (*__load_daemon)(int);
81
82 dlerror();
83 __load_daemon = (pthread_t(*)(int))dlsym(dlopen_handle, "load_daemon");
84 error = dlerror();
85 if (error)
86 return log_error(-1, "%s - Failed to start loadavg daemon", error);
87
88 loadavg_pid = __load_daemon(1);
89 if (!loadavg_pid)
90 return -1;
91
92 return 0;
93 }
94
95 /* Returns zero on success */
96 static int stop_loadavg(void)
97 {
98 char *error;
99 int (*__stop_load_daemon)(pthread_t);
100
101 __stop_load_daemon = (int (*)(pthread_t))dlsym(dlopen_handle, "stop_load_daemon");
102 error = dlerror();
103 if (error)
104 return log_error(-1, "%s - Failed to stop loadavg daemon", error);
105
106 if (__stop_load_daemon(loadavg_pid))
107 return -1;
108
109 return 0;
110 }
111
112 static volatile sig_atomic_t need_reload;
113
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)
117 {
118 int ret;
119 char lxcfs_lib_path[PATH_MAX];
120
121 if (loadavg_pid > 0)
122 stop_loadavg();
123
124 if (dlopen_handle) {
125 lxcfs_info("Closed liblxcfs.so");
126 dlclose(dlopen_handle);
127 }
128
129 /* First try loading using ld.so */
130 dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
131 if (dlopen_handle) {
132 lxcfs_debug("Opened liblxcfs.so");
133 goto good;
134 }
135
136 #ifdef LIBDIR
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);
139 #else
140 ret = snprintf(lxcfs_lib_path, sizeof(lxcfs_lib_path), "/usr/local/lib/lxcfs/liblxcfs.so");
141 #endif
142 if (ret < 0 || ret >= sizeof(lxcfs_lib_path))
143 log_exit("Failed to create path to open liblxcfs");
144
145 dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY);
146 if (!dlopen_handle)
147 log_exit("%s - Failed to open liblxcfs.so", dlerror());
148 else
149 lxcfs_debug("Opened %s", lxcfs_lib_path);
150
151 good:
152 if (loadavg_pid > 0)
153 start_loadavg();
154
155 if (need_reload)
156 lxcfs_info("Reloaded LXCFS");
157 need_reload = 0;
158 }
159
160 static void up_users(void)
161 {
162 users_lock();
163 if (users_count == 0 && need_reload)
164 do_reload();
165 users_count++;
166 users_unlock();
167 }
168
169 static void down_users(void)
170 {
171 users_lock();
172 users_count--;
173 users_unlock();
174 }
175
176 static void reload_handler(int sig)
177 {
178 need_reload = 1;
179 }
180
181 /* Functions to run the library methods */
182 static int do_cg_getattr(const char *path, struct stat *sb)
183 {
184 char *error;
185 int (*__cg_getattr)(const char *path, struct stat *sb);
186
187 dlerror();
188 __cg_getattr = (int (*)(const char *, struct stat *))dlsym(dlopen_handle, "cg_getattr");
189 error = dlerror();
190 if (error)
191 return log_error(-1, "%s - Failed to find cg_getattr()", error);
192
193 return __cg_getattr(path, sb);
194 }
195
196 static int do_proc_getattr(const char *path, struct stat *sb)
197 {
198 char *error;
199 int (*__proc_getattr)(const char *path, struct stat *sb);
200
201 dlerror();
202 __proc_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "proc_getattr");
203 error = dlerror();
204 if (error)
205 return log_error(-1, "%s - Failed to find proc_getattr()", error);
206
207 return __proc_getattr(path, sb);
208 }
209
210 static int do_sys_getattr(const char *path, struct stat *sb)
211 {
212 char *error;
213 int (*__sys_getattr)(const char *path, struct stat *sb);
214
215 dlerror();
216 __sys_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "sys_getattr");
217 error = dlerror();
218 if (error)
219 return log_error(-1, "%s - Failed to find sys_getattr()", error);
220
221 return __sys_getattr(path, sb);
222 }
223
224 static int do_cg_read(const char *path, char *buf, size_t size, off_t offset,
225 struct fuse_file_info *fi)
226 {
227 char *error;
228 int (*__cg_read)(const char *path, char *buf, size_t size, off_t offset,
229 struct fuse_file_info *fi);
230
231 dlerror();
232 __cg_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_read");
233 error = dlerror();
234 if (error)
235 return log_error(-1, "%s - Failed to find cg_read()", error);
236
237 return __cg_read(path, buf, size, offset, fi);
238 }
239
240 static int do_proc_read(const char *path, char *buf, size_t size, off_t offset,
241 struct fuse_file_info *fi)
242 {
243 char *error;
244 int (*__proc_read)(const char *path, char *buf, size_t size,
245 off_t offset, struct fuse_file_info *fi);
246
247 dlerror();
248 __proc_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "proc_read");
249 error = dlerror();
250 if (error)
251 return log_error(-1, "%s - Failed to find proc_read()", error);
252
253 return __proc_read(path, buf, size, offset, fi);
254 }
255
256 static int do_sys_read(const char *path, char *buf, size_t size, off_t offset,
257 struct fuse_file_info *fi)
258 {
259 char *error;
260 int (*__sys_read)(const char *path, char *buf, size_t size,
261 off_t offset, struct fuse_file_info *fi);
262
263 dlerror();
264 __sys_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "sys_read");
265 error = dlerror();
266 if (error)
267 return log_error(-1, "%s - Failed to find sys_read()", error);
268
269 return __sys_read(path, buf, size, offset, fi);
270 }
271
272 static int do_cg_write(const char *path, const char *buf, size_t size,
273 off_t offset, struct fuse_file_info *fi)
274 {
275 char *error;
276 int (*__cg_write)(const char *path, const char *buf, size_t size,
277 off_t offset, struct fuse_file_info *fi);
278
279 dlerror();
280 __cg_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_write");
281 error = dlerror();
282 if (error)
283 return log_error(-1, "%s - Failed to find cg_write()", error);
284
285 return __cg_write(path, buf, size, offset, fi);
286 }
287
288 static int do_cg_mkdir(const char *path, mode_t mode)
289 {
290 char *error;
291 int (*__cg_mkdir)(const char *path, mode_t mode);
292
293 dlerror();
294 __cg_mkdir = (int (*)(const char *, mode_t))dlsym(dlopen_handle, "cg_mkdir");
295 error = dlerror();
296 if (error)
297 return log_error(-1, "%s - Failed to find cg_mkdir()", error);
298
299 return __cg_mkdir(path, mode);
300 }
301
302 static int do_cg_chown(const char *path, uid_t uid, gid_t gid)
303 {
304 char *error;
305 int (*__cg_chown)(const char *path, uid_t uid, gid_t gid);
306
307 dlerror();
308 __cg_chown = (int (*)(const char *, uid_t, gid_t))dlsym(dlopen_handle, "cg_chown");
309 error = dlerror();
310 if (error)
311 return log_error(-1, "%s - Failed to find cg_chown()", error);
312
313 return __cg_chown(path, uid, gid);
314 }
315
316 static int do_cg_rmdir(const char *path)
317 {
318 char *error;
319 int (*__cg_rmdir)(const char *path);
320
321 dlerror();
322 __cg_rmdir = (int (*)(const char *path))dlsym(dlopen_handle, "cg_rmdir");
323 error = dlerror();
324 if (error)
325 return log_error(-1, "%s - Failed to find cg_rmdir()", error);
326
327 return __cg_rmdir(path);
328 }
329
330 static int do_cg_chmod(const char *path, mode_t mode)
331 {
332 char *error;
333 int (*__cg_chmod)(const char *path, mode_t mode);
334
335 dlerror();
336 __cg_chmod = (int (*)(const char *, mode_t))dlsym(dlopen_handle, "cg_chmod");
337 error = dlerror();
338 if (error)
339 return log_error(-1, "%s - Failed to find cg_chmod()", error);
340
341 return __cg_chmod(path, mode);
342 }
343
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)
346 {
347 char *error;
348 int (*__cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
349 off_t offset, struct fuse_file_info *fi);
350
351 dlerror();
352 __cg_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_readdir");
353 error = dlerror();
354 if (error)
355 return log_error(-1, "%s - Failed to find cg_readdir()", error);
356
357 return __cg_readdir(path, buf, filler, offset, fi);
358 }
359
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)
362 {
363 char *error;
364 int (*__proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
365 off_t offset, struct fuse_file_info *fi);
366
367 dlerror();
368 __proc_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "proc_readdir");
369 error = dlerror();
370 if (error)
371 return log_error(-1, "%s - Failed to find proc_readdir()", error);
372
373 return __proc_readdir(path, buf, filler, offset, fi);
374 }
375
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)
378 {
379 char *error;
380 int (*__sys_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
381 off_t offset, struct fuse_file_info *fi);
382
383 dlerror();
384 __sys_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "sys_readdir");
385 error = dlerror();
386 if (error)
387 return log_error(-1, "%s - Failed to find sys_readdir()", error);
388
389 return __sys_readdir(path, buf, filler, offset, fi);
390 }
391
392
393 static int do_cg_open(const char *path, struct fuse_file_info *fi)
394 {
395 char *error;
396 int (*__cg_open)(const char *path, struct fuse_file_info *fi);
397
398 dlerror();
399 __cg_open = (int (*)(const char *, struct fuse_file_info *))dlsym(dlopen_handle, "cg_open");
400 error = dlerror();
401 if (error)
402 return log_error(-1, "%s - Failed to find cg_open()", error);
403
404 return __cg_open(path, fi);
405 }
406
407 static int do_cg_access(const char *path, int mode)
408 {
409 char *error;
410 int (*__cg_access)(const char *path, int mode);
411
412 dlerror();
413 __cg_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "cg_access");
414 error = dlerror();
415 if (error)
416 return log_error(-1, "%s - Failed to find cg_access()", error);
417
418 return __cg_access(path, mode);
419 }
420
421 static int do_proc_open(const char *path, struct fuse_file_info *fi)
422 {
423 char *error;
424 int (*__proc_open)(const char *path, struct fuse_file_info *fi);
425
426 dlerror();
427 __proc_open = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "proc_open");
428 error = dlerror();
429 if (error)
430 return log_error(-1, "%s - Failed to find proc_open()", error);
431
432 return __proc_open(path, fi);
433 }
434
435 static int do_proc_access(const char *path, int mode)
436 {
437 char *error;
438 int (*__proc_access)(const char *path, int mode);
439
440 dlerror();
441 __proc_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "proc_access");
442 error = dlerror();
443 if (error)
444 return log_error(-1, "%s - Failed to find proc_access()", error);
445
446 return __proc_access(path, mode);
447 }
448
449 static int do_sys_open(const char *path, struct fuse_file_info *fi)
450 {
451 char *error;
452 int (*__sys_open)(const char *path, struct fuse_file_info *fi);
453
454 dlerror();
455 __sys_open = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "sys_open");
456 error = dlerror();
457 if (error)
458 return log_error(-1, "%s - Failed to find sys_open()", error);
459
460 return __sys_open(path, fi);
461 }
462
463 static int do_sys_access(const char *path, int mode)
464 {
465 char *error;
466 int (*__sys_access)(const char *path, int mode);
467
468 dlerror();
469 __sys_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "sys_access");
470 error = dlerror();
471 if (error)
472 return log_error(-1, "%s - Failed to find sys_access()", error);
473
474 return __sys_access(path, mode);
475 }
476
477 static int do_cg_release(const char *path, struct fuse_file_info *fi)
478 {
479 char *error;
480 int (*__cg_release)(const char *path, struct fuse_file_info *fi);
481
482 dlerror();
483 __cg_release = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "cg_release");
484 error = dlerror();
485 if (error)
486 return log_error(-1, "%s - Failed to find cg_release()", error);
487
488 return __cg_release(path, fi);
489 }
490
491 static int do_proc_release(const char *path, struct fuse_file_info *fi)
492 {
493 char *error;
494 int (*__proc_release)(const char *path, struct fuse_file_info *fi);
495
496 dlerror();
497 __proc_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_release");
498 error = dlerror();
499 if (error)
500 return log_error(-1, "%s - Failed to find proc_release()", error);
501
502 return __proc_release(path, fi);
503 }
504
505 static int do_sys_release(const char *path, struct fuse_file_info *fi)
506 {
507 char *error;
508 int (*__sys_release)(const char *path, struct fuse_file_info *fi);
509
510 dlerror();
511 __sys_release = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "sys_release");
512 error = dlerror();
513 if (error)
514 return log_error(-1, "%s - Failed to find sys_release()", error);
515
516 return __sys_release(path, fi);
517 }
518
519 static int do_cg_opendir(const char *path, struct fuse_file_info *fi)
520 {
521 char *error;
522 int (*__cg_opendir)(const char *path, struct fuse_file_info *fi);
523
524 dlerror();
525 __cg_opendir = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "cg_opendir");
526 error = dlerror();
527 if (error)
528 return log_error(-1, "%s - Failed to find cg_opendir()", error);
529
530 return __cg_opendir(path, fi);
531 }
532
533 static int do_cg_releasedir(const char *path, struct fuse_file_info *fi)
534 {
535 char *error;
536 int (*__cg_releasedir)(const char *path, struct fuse_file_info *fi);
537
538 dlerror();
539 __cg_releasedir = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "cg_releasedir");
540 error = dlerror();
541 if (error)
542 return log_error(-1, "%s - Failed to find cg_releasedir()", error);
543
544 return __cg_releasedir(path, fi);
545 }
546
547 static int do_sys_releasedir(const char *path, struct fuse_file_info *fi)
548 {
549 char *error;
550 int (*__sys_releasedir)(const char *path, struct fuse_file_info *fi);
551
552 dlerror();
553 __sys_releasedir = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "sys_releasedir");
554 error = dlerror();
555 if (error)
556 return log_error(-1, "%s - Failed to find sys_releasedir()", error);
557
558 return __sys_releasedir(path, fi);
559 }
560
561 static int lxcfs_getattr(const char *path, struct stat *sb)
562 {
563 int ret;
564 struct timespec now;
565
566 if (strcmp(path, "/") == 0) {
567 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
568 return -EINVAL;
569 sb->st_uid = sb->st_gid = 0;
570 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
571 sb->st_size = 0;
572 sb->st_mode = S_IFDIR | 00755;
573 sb->st_nlink = 2;
574 return 0;
575 }
576
577 if (strncmp(path, "/cgroup", 7) == 0) {
578 up_users();
579 ret = do_cg_getattr(path, sb);
580 down_users();
581 return ret;
582 }
583
584 if (strncmp(path, "/proc", 5) == 0) {
585 up_users();
586 ret = do_proc_getattr(path, sb);
587 down_users();
588 return ret;
589 }
590
591 if (strncmp(path, "/sys", 4) == 0) {
592 up_users();
593 ret = do_sys_getattr(path, sb);
594 down_users();
595 return ret;
596 }
597
598 return -ENOENT;
599 }
600
601 static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
602 {
603 int ret;
604
605 if (strcmp(path, "/") == 0)
606 return 0;
607
608 if (strncmp(path, "/cgroup", 7) == 0) {
609 up_users();
610 ret = do_cg_opendir(path, fi);
611 down_users();
612 return ret;
613 }
614
615 if (strcmp(path, "/proc") == 0)
616 return 0;
617
618 if (strncmp(path, "/sys", 4) == 0)
619 return 0;
620
621 return -ENOENT;
622 }
623
624 static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
625 off_t offset, struct fuse_file_info *fi)
626 {
627 int ret;
628
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)
635 return -ENOMEM;
636
637 return 0;
638 }
639
640 if (strncmp(path, "/cgroup", 7) == 0) {
641 up_users();
642 ret = do_cg_readdir(path, buf, filler, offset, fi);
643 down_users();
644 return ret;
645 }
646
647 if (strcmp(path, "/proc") == 0) {
648 up_users();
649 ret = do_proc_readdir(path, buf, filler, offset, fi);
650 down_users();
651 return ret;
652 }
653
654 if (strncmp(path, "/sys", 4) == 0) {
655 up_users();
656 ret = do_sys_readdir(path, buf, filler, offset, fi);
657 down_users();
658 return ret;
659 }
660
661 return -ENOENT;
662 }
663
664 static int lxcfs_access(const char *path, int mode)
665 {
666 int ret;
667
668 if (strcmp(path, "/") == 0 && (mode & W_OK) == 0)
669 return 0;
670
671 if (strncmp(path, "/cgroup", 7) == 0) {
672 up_users();
673 ret = do_cg_access(path, mode);
674 down_users();
675 return ret;
676 }
677
678 if (strncmp(path, "/proc", 5) == 0) {
679 up_users();
680 ret = do_proc_access(path, mode);
681 down_users();
682 return ret;
683 }
684
685 if (strncmp(path, "/sys", 4) == 0) {
686 up_users();
687 ret = do_sys_access(path, mode);
688 down_users();
689 return ret;
690 }
691
692 return -EACCES;
693 }
694
695 static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
696 {
697 int ret;
698
699 if (strcmp(path, "/") == 0)
700 return 0;
701
702 if (strncmp(path, "/cgroup", 7) == 0) {
703 up_users();
704 ret = do_cg_releasedir(path, fi);
705 down_users();
706 return ret;
707 }
708
709 if (strcmp(path, "/proc") == 0)
710 return 0;
711
712 if (strncmp(path, "/sys", 4) == 0) {
713 up_users();
714 ret = do_sys_releasedir(path, fi);
715 down_users();
716 return ret;
717 }
718
719 return -EINVAL;
720 }
721
722 static int lxcfs_open(const char *path, struct fuse_file_info *fi)
723 {
724 int ret;
725
726 if (strncmp(path, "/cgroup", 7) == 0) {
727 up_users();
728 ret = do_cg_open(path, fi);
729 down_users();
730 return ret;
731 }
732
733 if (strncmp(path, "/proc", 5) == 0) {
734 up_users();
735 ret = do_proc_open(path, fi);
736 down_users();
737 return ret;
738 }
739
740 if (strncmp(path, "/sys", 4) == 0) {
741 up_users();
742 ret = do_sys_open(path, fi);
743 down_users();
744 return ret;
745 }
746
747 return -EACCES;
748 }
749
750 static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
751 struct fuse_file_info *fi)
752 {
753 int ret;
754
755 if (strncmp(path, "/cgroup", 7) == 0) {
756 up_users();
757 ret = do_cg_read(path, buf, size, offset, fi);
758 down_users();
759 return ret;
760 }
761
762 if (strncmp(path, "/proc", 5) == 0) {
763 up_users();
764 ret = do_proc_read(path, buf, size, offset, fi);
765 down_users();
766 return ret;
767 }
768
769 if (strncmp(path, "/sys", 4) == 0) {
770 up_users();
771 ret = do_sys_read(path, buf, size, offset, fi);
772 down_users();
773 return ret;
774 }
775
776 return -EINVAL;
777 }
778
779 int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
780 struct fuse_file_info *fi)
781 {
782 int ret;
783
784 if (strncmp(path, "/cgroup", 7) == 0) {
785 up_users();
786 ret = do_cg_write(path, buf, size, offset, fi);
787 down_users();
788 return ret;
789 }
790
791 return -EINVAL;
792 }
793
794 static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
795 {
796 return 0;
797 }
798
799 static int lxcfs_release(const char *path, struct fuse_file_info *fi)
800 {
801 int ret;
802
803 if (strncmp(path, "/cgroup", 7) == 0) {
804 up_users();
805 ret = do_cg_release(path, fi);
806 down_users();
807 return ret;
808 }
809
810 if (strncmp(path, "/proc", 5) == 0) {
811 up_users();
812 ret = do_proc_release(path, fi);
813 down_users();
814 return ret;
815 }
816
817 if (strncmp(path, "/sys", 4) == 0) {
818 up_users();
819 ret = do_sys_release(path, fi);
820 down_users();
821 return ret;
822 }
823
824 return -EINVAL;
825 }
826
827 static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
828 {
829 return 0;
830 }
831
832 int lxcfs_mkdir(const char *path, mode_t mode)
833 {
834 int ret;
835
836 if (strncmp(path, "/cgroup", 7) == 0) {
837 up_users();
838 ret = do_cg_mkdir(path, mode);
839 down_users();
840 return ret;
841 }
842
843 return -EPERM;
844 }
845
846 int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
847 {
848 int ret;
849
850 if (strncmp(path, "/cgroup", 7) == 0) {
851 up_users();
852 ret = do_cg_chown(path, uid, gid);
853 down_users();
854 return ret;
855 }
856
857 if (strncmp(path, "/proc", 5) == 0)
858 return -EPERM;
859
860 if (strncmp(path, "/sys", 4) == 0)
861 return -EPERM;
862
863 return -ENOENT;
864 }
865
866 /*
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
869 * nothing.
870 */
871 int lxcfs_truncate(const char *path, off_t newsize)
872 {
873 if (strncmp(path, "/cgroup", 7) == 0)
874 return 0;
875
876 return -EPERM;
877 }
878
879 int lxcfs_rmdir(const char *path)
880 {
881 int ret;
882
883 if (strncmp(path, "/cgroup", 7) == 0) {
884 up_users();
885 ret = do_cg_rmdir(path);
886 down_users();
887 return ret;
888 }
889
890 return -EPERM;
891 }
892
893 int lxcfs_chmod(const char *path, mode_t mode)
894 {
895 int ret;
896
897 if (strncmp(path, "/cgroup", 7) == 0) {
898 up_users();
899 ret = do_cg_chmod(path, mode);
900 down_users();
901 return ret;
902 }
903
904 if (strncmp(path, "/proc", 5) == 0)
905 return -EPERM;
906
907 if (strncmp(path, "/sys", 4) == 0)
908 return -EPERM;
909
910 return -ENOENT;
911 }
912
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,
921 .open = lxcfs_open,
922 .opendir = lxcfs_opendir,
923 .read = lxcfs_read,
924 .readdir = lxcfs_readdir,
925 .release = lxcfs_release,
926 .releasedir = lxcfs_releasedir,
927 .rmdir = lxcfs_rmdir,
928 .truncate = lxcfs_truncate,
929 .write = lxcfs_write,
930
931 .create = NULL,
932 .destroy = NULL,
933 .fgetattr = NULL,
934 .fsyncdir = NULL,
935 .ftruncate = NULL,
936 .getdir = NULL,
937 .getxattr = NULL,
938 .init = NULL,
939 .link = NULL,
940 .listxattr = NULL,
941 .mknod = NULL,
942 .readlink = NULL,
943 .rename = NULL,
944 .removexattr = NULL,
945 .setxattr = NULL,
946 .statfs = NULL,
947 .symlink = NULL,
948 .unlink = NULL,
949 .utime = NULL,
950 };
951
952 static void usage()
953 {
954 lxcfs_info("Usage: lxcfs <directory>\n");
955 lxcfs_info("lxcfs set up fuse- and cgroup-based virtualizing filesystem\n");
956 lxcfs_info("Options :");
957 lxcfs_info(" -d, --debug Run lxcfs with debugging enabled");
958 lxcfs_info(" --disable-cfs Disable cpu virtualization via cpu shares");
959 lxcfs_info(" -f, --foreground Run lxcfs in the foreground");
960 lxcfs_info(" -n, --help Print help");
961 lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization");
962 lxcfs_info(" -o Options to pass directly through fuse");
963 lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid");
964 lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH);
965 lxcfs_info(" -u, --disable-swap Disable swap virtualization");
966 lxcfs_info(" -v, --version Print lxcfs version");
967 lxcfs_info(" --enable-pidfd Use pidfd for process tracking");
968 exit(EXIT_FAILURE);
969 }
970
971 static inline bool is_help(char *w)
972 {
973 return strcmp(w, "-h") == 0 ||
974 strcmp(w, "--help") == 0 ||
975 strcmp(w, "-help") == 0 ||
976 strcmp(w, "help") == 0;
977 }
978
979 static inline bool is_version(char *w)
980 {
981 return strcmp(w, "-v") == 0 ||
982 strcmp(w, "--version") == 0 ||
983 strcmp(w, "-version") == 0 ||
984 strcmp(w, "version") == 0;
985 }
986
987 static bool swallow_arg(int *argcp, char *argv[], char *which)
988 {
989 for (int i = 1; argv[i]; i++) {
990 if (strcmp(argv[i], which) != 0)
991 continue;
992
993 for (; argv[i]; i++)
994 argv[i] = argv[i + 1];
995
996 (*argcp)--;
997 return true;
998 }
999
1000 return false;
1001 }
1002
1003 static bool swallow_option(int *argcp, char *argv[], char *opt, char **v)
1004 {
1005 for (int i = 1; argv[i]; i++) {
1006 if (!argv[i + 1])
1007 continue;
1008
1009 if (strcmp(argv[i], opt) != 0)
1010 continue;
1011
1012 do {
1013 *v = strdup(argv[i + 1]);
1014 } while (!*v);
1015
1016 for (; argv[i + 1]; i++)
1017 argv[i] = argv[i + 2];
1018
1019 (*argcp) -= 2;
1020 return true;
1021 }
1022
1023 return false;
1024 }
1025
1026 static int set_pidfile(char *pidfile)
1027 {
1028 __do_close_prot_errno int fd = -EBADF;
1029 char buf[INTTYPE_TO_STRLEN(long)];
1030 int ret;
1031 struct flock fl = {
1032 fl.l_type = F_WRLCK,
1033 fl.l_whence = SEEK_SET,
1034 fl.l_start = 0,
1035 fl.l_len = 0,
1036 };
1037
1038 fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | O_CLOEXEC);
1039 if (fd < 0)
1040 return log_error(-1, "Could not open pidfile %s: %m", pidfile);
1041
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");
1046 }
1047
1048 if (ftruncate(fd, 0))
1049 return log_error(-1, "Error truncating PID file '%s': %m", pidfile);
1050
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");
1054
1055 if (write(fd, buf, ret) != ret)
1056 return log_error(-1, "Error writing to PID file '%s': %m", pidfile);
1057
1058 return move_fd(fd);
1059 }
1060
1061 int main(int argc, char *argv[])
1062 {
1063 __do_close_prot_errno 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;
1069 /*
1070 * what we pass to fuse_main is:
1071 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
1072 */
1073 int nargs = 5, cnt = 0;
1074 char *newargv[6];
1075 struct lxcfs_opts *opts;
1076
1077 opts = malloc(sizeof(struct lxcfs_opts));
1078 if (opts == NULL) {
1079 lxcfs_error("Error allocating memory for options");
1080 goto out;
1081 }
1082 opts->swap_off = false;
1083 opts->use_pidfd = false;
1084 opts->use_cfs = false;
1085
1086 /* accomodate older init scripts */
1087 swallow_arg(&argc, argv, "-s");
1088
1089 /* -f / --foreground */
1090 foreground = swallow_arg(&argc, argv, "-f");
1091 if (swallow_arg(&argc, argv, "--foreground"))
1092 foreground = true;
1093
1094 /* -d / --debug */
1095 debug = swallow_arg(&argc, argv, "-d");
1096 if (swallow_arg(&argc, argv, "--debug"))
1097 debug = true;
1098
1099 if (foreground && debug)
1100 log_exit("Both --debug and --forgreound specified");
1101
1102 /* -l / --enable-loadavg */
1103 load_use = swallow_arg(&argc, argv, "-l");
1104 if (swallow_arg(&argc, argv, "--enable-loadavg"))
1105 load_use = true;
1106
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;
1111
1112 /* --enable-pidfd */
1113 opts->use_pidfd = swallow_arg(&argc, argv, "--enable-pidfd");
1114
1115 /* --enable-cfs */
1116 if (swallow_arg(&argc, argv, "--enable-cfs"))
1117 opts->use_cfs = true;
1118
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) {
1125 nonempty = true;
1126 } else {
1127 free(v);
1128 lxcfs_error("Warning: unexpected fuse option %s", v);
1129 exit(EXIT_FAILURE);
1130 }
1131 }
1132 free(v);
1133 v = NULL;
1134 }
1135
1136 /* -p / --pidfile */
1137 if (swallow_option(&argc, argv, "-p", &v))
1138 pidfile = v;
1139 if (!pidfile && swallow_option(&argc, argv, "--pidfile", &v))
1140 pidfile = v;
1141
1142 if (argc == 2 && is_version(argv[1])) {
1143 lxcfs_info("%s", VERSION);
1144 exit(EXIT_SUCCESS);
1145 }
1146
1147 if (argc != 2 || is_help(argv[1]))
1148 usage();
1149
1150 do_reload();
1151 if (signal(SIGUSR1, reload_handler) == SIG_ERR) {
1152 lxcfs_error("Error setting USR1 signal handler: %m");
1153 goto out;
1154 }
1155
1156 newargv[cnt++] = argv[0];
1157 if (debug)
1158 newargv[cnt++] = "-d";
1159 else
1160 newargv[cnt++] = "-f";
1161 newargv[cnt++] = "-o";
1162 if (nonempty)
1163 newargv[cnt++] = "default_permissions,allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5,nonempty";
1164 else
1165 newargv[cnt++] = "default_permissions,allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5";
1166 newargv[cnt++] = argv[1];
1167 newargv[cnt++] = NULL;
1168
1169 if (!pidfile) {
1170 snprintf(pidfile_buf, sizeof(pidfile_buf), "%s/lxcfs.pid", RUNTIME_PATH);
1171 pidfile = pidfile_buf;
1172 }
1173
1174 pidfile_fd = set_pidfile(pidfile);
1175 if (pidfile_fd < 0)
1176 goto out;
1177
1178 if (load_use && start_loadavg() != 0)
1179 goto out;
1180
1181 if (!fuse_main(nargs, newargv, &lxcfs_ops, opts))
1182 ret = EXIT_SUCCESS;
1183 if (load_use)
1184 stop_loadavg();
1185
1186 out:
1187 if (dlopen_handle)
1188 dlclose(dlopen_handle);
1189 if (pidfile)
1190 unlink(pidfile);
1191 exit(ret);
1192 }