]> git.proxmox.com Git - mirror_lxcfs.git/blob - lxcfs.c
Fix test_proc on s390x
[mirror_lxcfs.git] / lxcfs.c
1 /* lxcfs
2 *
3 * Copyright © 2014-2016 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
5 *
6 * See COPYING file for details.
7 */
8
9 #define FUSE_USE_VERSION 26
10
11 #include <alloca.h>
12 #include <dirent.h>
13 #include <dlfcn.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <fuse.h>
17 #include <libgen.h>
18 #include <pthread.h>
19 #include <sched.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <wait.h>
27 #include <linux/sched.h>
28 #include <sys/epoll.h>
29 #include <sys/mount.h>
30 #include <sys/socket.h>
31 #include <linux/limits.h>
32
33 #include "bindings.h"
34 #include "config.h" // for VERSION
35
36 void *dlopen_handle;
37
38 /* Functions to keep track of number of threads using the library */
39
40 static int users_count;
41 static pthread_mutex_t user_count_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static void lock_mutex(pthread_mutex_t *l)
43 {
44 int ret;
45
46 if ((ret = pthread_mutex_lock(l)) != 0) {
47 lxcfs_error("returned:%d %s\n", ret, strerror(ret));
48 exit(1);
49 }
50 }
51
52 static void unlock_mutex(pthread_mutex_t *l)
53 {
54 int ret;
55
56 if ((ret = pthread_mutex_unlock(l)) != 0) {
57 lxcfs_error("returned:%d %s\n", ret, strerror(ret));
58 exit(1);
59 }
60 }
61
62 static void users_lock(void)
63 {
64 lock_mutex(&user_count_mutex);
65 }
66
67 static void users_unlock(void)
68 {
69 unlock_mutex(&user_count_mutex);
70 }
71
72 static volatile sig_atomic_t need_reload;
73
74 /* do_reload - reload the dynamic library. Done under
75 * lock and when we know the user_count was 0 */
76 static void do_reload(void)
77 {
78 char lxcfs_lib_path[PATH_MAX];
79 if (dlopen_handle) {
80 lxcfs_debug("%s\n", "Closing liblxcfs.so handle.");
81 dlclose(dlopen_handle);
82 }
83
84 /* First try loading using ld.so */
85 dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
86 if (dlopen_handle) {
87 lxcfs_debug("%s\n", "Successfully called dlopen() on liblxcfs.so.");
88 goto good;
89 }
90
91 #ifdef LIBDIR
92 /* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
93 snprintf(lxcfs_lib_path, PATH_MAX, "%s/lxcfs/liblxcfs.so", LIBDIR);
94 #else
95 snprintf(lxcfs_lib_path, PATH_MAX, "/usr/local/lib/lxcfs/liblxcfs.so");
96 #endif
97 dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY);
98 if (!dlopen_handle) {
99 lxcfs_error("Failed to open liblxcfs.so: %s.\n", dlerror());
100 _exit(1);
101 }
102
103 good:
104 if (need_reload)
105 lxcfs_error("%s\n", "lxcfs: reloaded");
106 need_reload = 0;
107 }
108
109 static void up_users(void)
110 {
111 users_lock();
112 if (users_count == 0 && need_reload)
113 do_reload();
114 users_count++;
115 users_unlock();
116 }
117
118 static void down_users(void)
119 {
120 users_lock();
121 users_count--;
122 users_unlock();
123 }
124
125 static void reload_handler(int sig)
126 {
127 need_reload = 1;
128 }
129
130 /* Functions to run the library methods */
131 static int do_cg_getattr(const char *path, struct stat *sb)
132 {
133 int (*cg_getattr)(const char *path, struct stat *sb);
134 char *error;
135 dlerror(); /* Clear any existing error */
136 cg_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "cg_getattr");
137 error = dlerror();
138 if (error != NULL) {
139 lxcfs_error("%s\n", error);
140 return -1;
141 }
142
143 return cg_getattr(path, sb);
144 }
145
146 static int do_proc_getattr(const char *path, struct stat *sb)
147 {
148 int (*proc_getattr)(const char *path, struct stat *sb);
149 char *error;
150 dlerror(); /* Clear any existing error */
151 proc_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "proc_getattr");
152 error = dlerror();
153 if (error != NULL) {
154 lxcfs_error("%s\n", error);
155 return -1;
156 }
157
158 return proc_getattr(path, sb);
159 }
160
161 static int do_cg_read(const char *path, char *buf, size_t size, off_t offset,
162 struct fuse_file_info *fi)
163 {
164 int (*cg_read)(const char *path, char *buf, size_t size, off_t offset,
165 struct fuse_file_info *fi);
166 char *error;
167
168 dlerror(); /* Clear any existing error */
169 cg_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_read");
170 error = dlerror();
171 if (error != NULL) {
172 lxcfs_error("%s\n", error);
173 return -1;
174 }
175
176 return cg_read(path, buf, size, offset, fi);
177 }
178
179 static int do_proc_read(const char *path, char *buf, size_t size, off_t offset,
180 struct fuse_file_info *fi)
181 {
182 int (*proc_read)(const char *path, char *buf, size_t size, off_t offset,
183 struct fuse_file_info *fi);
184 char *error;
185
186 dlerror(); /* Clear any existing error */
187 proc_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_read");
188 error = dlerror();
189 if (error != NULL) {
190 lxcfs_error("%s\n", error);
191 return -1;
192 }
193
194 return proc_read(path, buf, size, offset, fi);
195 }
196
197 static int do_cg_write(const char *path, const char *buf, size_t size, off_t offset,
198 struct fuse_file_info *fi)
199 {
200 int (*cg_write)(const char *path, const char *buf, size_t size, off_t offset,
201 struct fuse_file_info *fi);
202 char *error;
203 dlerror(); /* Clear any existing error */
204 cg_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_write");
205 error = dlerror();
206 if (error != NULL) {
207 lxcfs_error("%s\n", error);
208 return -1;
209 }
210
211 return cg_write(path, buf, size, offset, fi);
212 }
213
214 static int do_cg_mkdir(const char *path, mode_t mode)
215 {
216 int (*cg_mkdir)(const char *path, mode_t mode);
217 char *error;
218 dlerror(); /* Clear any existing error */
219 cg_mkdir = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_mkdir");
220 error = dlerror();
221 if (error != NULL) {
222 lxcfs_error("%s\n", error);
223 return -1;
224 }
225
226 return cg_mkdir(path, mode);
227 }
228
229 static int do_cg_chown(const char *path, uid_t uid, gid_t gid)
230 {
231 int (*cg_chown)(const char *path, uid_t uid, gid_t gid);
232 char *error;
233 dlerror(); /* Clear any existing error */
234 cg_chown = (int (*)(const char *, uid_t, gid_t)) dlsym(dlopen_handle, "cg_chown");
235 error = dlerror();
236 if (error != NULL) {
237 lxcfs_error("%s\n", error);
238 return -1;
239 }
240
241 return cg_chown(path, uid, gid);
242 }
243
244 static int do_cg_rmdir(const char *path)
245 {
246 int (*cg_rmdir)(const char *path);
247 char *error;
248 dlerror(); /* Clear any existing error */
249 cg_rmdir = (int (*)(const char *path)) dlsym(dlopen_handle, "cg_rmdir");
250 error = dlerror();
251 if (error != NULL) {
252 lxcfs_error("%s\n", error);
253 return -1;
254 }
255
256 return cg_rmdir(path);
257 }
258
259 static int do_cg_chmod(const char *path, mode_t mode)
260 {
261 int (*cg_chmod)(const char *path, mode_t mode);
262 char *error;
263 dlerror(); /* Clear any existing error */
264 cg_chmod = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_chmod");
265 error = dlerror();
266 if (error != NULL) {
267 lxcfs_error("%s\n", error);
268 return -1;
269 }
270
271 return cg_chmod(path, mode);
272 }
273
274 static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
275 struct fuse_file_info *fi)
276 {
277 int (*cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
278 struct fuse_file_info *fi);
279 char *error;
280
281 dlerror(); /* Clear any existing error */
282 cg_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_readdir");
283 error = dlerror();
284 if (error != NULL) {
285 lxcfs_error("%s\n", error);
286 return -1;
287 }
288
289 return cg_readdir(path, buf, filler, offset, fi);
290 }
291
292 static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
293 struct fuse_file_info *fi)
294 {
295 int (*proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
296 struct fuse_file_info *fi);
297 char *error;
298
299 dlerror(); /* Clear any existing error */
300 proc_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_readdir");
301 error = dlerror();
302 if (error != NULL) {
303 lxcfs_error("%s\n", error);
304 return -1;
305 }
306
307 return proc_readdir(path, buf, filler, offset, fi);
308 }
309
310 static int do_cg_open(const char *path, struct fuse_file_info *fi)
311 {
312 int (*cg_open)(const char *path, struct fuse_file_info *fi);
313 char *error;
314 dlerror(); /* Clear any existing error */
315 cg_open = (int (*)(const char *, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_open");
316 error = dlerror();
317 if (error != NULL) {
318 lxcfs_error("%s\n", error);
319 return -1;
320 }
321
322 return cg_open(path, fi);
323 }
324
325 static int do_cg_access(const char *path, int mode)
326 {
327 int (*cg_access)(const char *path, int mode);
328 char *error;
329 dlerror(); /* Clear any existing error */
330 cg_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "cg_access");
331 error = dlerror();
332 if (error != NULL) {
333 lxcfs_error("%s\n", error);
334 return -1;
335 }
336
337 return cg_access(path, mode);
338 }
339
340 static int do_proc_open(const char *path, struct fuse_file_info *fi)
341 {
342 int (*proc_open)(const char *path, struct fuse_file_info *fi);
343 char *error;
344 dlerror(); /* Clear any existing error */
345 proc_open = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "proc_open");
346 error = dlerror();
347 if (error != NULL) {
348 lxcfs_error("%s\n", error);
349 return -1;
350 }
351
352 return proc_open(path, fi);
353 }
354
355 static int do_proc_access(const char *path, int mode)
356 {
357 int (*proc_access)(const char *path, int mode);
358 char *error;
359 dlerror(); /* Clear any existing error */
360 proc_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "proc_access");
361 error = dlerror();
362 if (error != NULL) {
363 lxcfs_error("%s\n", error);
364 return -1;
365 }
366
367 return proc_access(path, mode);
368 }
369
370 static int do_cg_release(const char *path, struct fuse_file_info *fi)
371 {
372 int (*cg_release)(const char *path, struct fuse_file_info *fi);
373 char *error;
374 dlerror(); /* Clear any existing error */
375 cg_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_release");
376 error = dlerror();
377 if (error != NULL) {
378 lxcfs_error("%s\n", error);
379 return -1;
380 }
381
382 return cg_release(path, fi);
383 }
384
385 static int do_proc_release(const char *path, struct fuse_file_info *fi)
386 {
387 int (*proc_release)(const char *path, struct fuse_file_info *fi);
388 char *error;
389 dlerror(); /* Clear any existing error */
390 proc_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_release");
391 error = dlerror();
392 if (error != NULL) {
393 lxcfs_error("%s\n", error);
394 return -1;
395 }
396
397 return proc_release(path, fi);
398 }
399
400 static int do_cg_opendir(const char *path, struct fuse_file_info *fi)
401 {
402 int (*cg_opendir)(const char *path, struct fuse_file_info *fi);
403 char *error;
404 dlerror(); /* Clear any existing error */
405 cg_opendir = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "cg_opendir");
406 error = dlerror();
407 if (error != NULL) {
408 lxcfs_error("%s\n", error);
409 return -1;
410 }
411
412 return cg_opendir(path, fi);
413 }
414
415 static int do_cg_releasedir(const char *path, struct fuse_file_info *fi)
416 {
417 int (*cg_releasedir)(const char *path, struct fuse_file_info *fi);
418 char *error;
419 dlerror(); /* Clear any existing error */
420 cg_releasedir = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_releasedir");
421 error = dlerror();
422 if (error != NULL) {
423 lxcfs_error("%s\n", error);
424 return -1;
425 }
426
427 return cg_releasedir(path, fi);
428 }
429
430 /*
431 * FUSE ops for /
432 * these just delegate to the /proc and /cgroup ops as
433 * needed
434 */
435
436 static int lxcfs_getattr(const char *path, struct stat *sb)
437 {
438 int ret;
439 struct timespec now;
440
441 if (strcmp(path, "/") == 0) {
442 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
443 return -EINVAL;
444 sb->st_uid = sb->st_gid = 0;
445 sb->st_atim = sb->st_mtim = sb->st_ctim = now;
446 sb->st_size = 0;
447 sb->st_mode = S_IFDIR | 00755;
448 sb->st_nlink = 2;
449 return 0;
450 }
451
452 if (strncmp(path, "/cgroup", 7) == 0) {
453 up_users();
454 ret = do_cg_getattr(path, sb);
455 down_users();
456 return ret;
457 }
458 if (strncmp(path, "/proc", 5) == 0) {
459 up_users();
460 ret = do_proc_getattr(path, sb);
461 down_users();
462 return ret;
463 }
464 return -ENOENT;
465 }
466
467 static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
468 {
469 int ret;
470 if (strcmp(path, "/") == 0)
471 return 0;
472
473 if (strncmp(path, "/cgroup", 7) == 0) {
474 up_users();
475 ret = do_cg_opendir(path, fi);
476 down_users();
477 return ret;
478 }
479 if (strcmp(path, "/proc") == 0)
480 return 0;
481 return -ENOENT;
482 }
483
484 static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
485 struct fuse_file_info *fi)
486 {
487 int ret;
488 if (strcmp(path, "/") == 0) {
489 if (filler(buf, ".", NULL, 0) != 0 ||
490 filler(buf, "..", NULL, 0) != 0 ||
491 filler(buf, "proc", NULL, 0) != 0 ||
492 filler(buf, "cgroup", NULL, 0) != 0)
493 return -ENOMEM;
494 return 0;
495 }
496 if (strncmp(path, "/cgroup", 7) == 0) {
497 up_users();
498 ret = do_cg_readdir(path, buf, filler, offset, fi);
499 down_users();
500 return ret;
501 }
502 if (strcmp(path, "/proc") == 0) {
503 up_users();
504 ret = do_proc_readdir(path, buf, filler, offset, fi);
505 down_users();
506 return ret;
507 }
508 return -ENOENT;
509 }
510
511 static int lxcfs_access(const char *path, int mode)
512 {
513 int ret;
514
515 if (strcmp(path, "/") == 0 && (mode & W_OK) == 0)
516 return 0;
517
518 if (strncmp(path, "/cgroup", 7) == 0) {
519 up_users();
520 ret = do_cg_access(path, mode);
521 down_users();
522 return ret;
523 }
524 if (strncmp(path, "/proc", 5) == 0) {
525 up_users();
526 ret = do_proc_access(path, mode);
527 down_users();
528 return ret;
529 }
530
531 return -EACCES;
532 }
533
534 static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
535 {
536 int ret;
537 if (strcmp(path, "/") == 0)
538 return 0;
539 if (strncmp(path, "/cgroup", 7) == 0) {
540 up_users();
541 ret = do_cg_releasedir(path, fi);
542 down_users();
543 return ret;
544 }
545 if (strcmp(path, "/proc") == 0)
546 return 0;
547 return -EINVAL;
548 }
549
550 static int lxcfs_open(const char *path, struct fuse_file_info *fi)
551 {
552 int ret;
553 if (strncmp(path, "/cgroup", 7) == 0) {
554 up_users();
555 ret = do_cg_open(path, fi);
556 down_users();
557 return ret;
558 }
559 if (strncmp(path, "/proc", 5) == 0) {
560 up_users();
561 ret = do_proc_open(path, fi);
562 down_users();
563 return ret;
564 }
565
566 return -EACCES;
567 }
568
569 static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
570 struct fuse_file_info *fi)
571 {
572 int ret;
573 if (strncmp(path, "/cgroup", 7) == 0) {
574 up_users();
575 ret = do_cg_read(path, buf, size, offset, fi);
576 down_users();
577 return ret;
578 }
579 if (strncmp(path, "/proc", 5) == 0) {
580 up_users();
581 ret = do_proc_read(path, buf, size, offset, fi);
582 down_users();
583 return ret;
584 }
585
586 return -EINVAL;
587 }
588
589 int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
590 struct fuse_file_info *fi)
591 {
592 int ret;
593 if (strncmp(path, "/cgroup", 7) == 0) {
594 up_users();
595 ret = do_cg_write(path, buf, size, offset, fi);
596 down_users();
597 return ret;
598 }
599
600 return -EINVAL;
601 }
602
603 static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
604 {
605 return 0;
606 }
607
608 static int lxcfs_release(const char *path, struct fuse_file_info *fi)
609 {
610 int ret;
611 if (strncmp(path, "/cgroup", 7) == 0) {
612 up_users();
613 ret = do_cg_release(path, fi);
614 down_users();
615 return ret;
616 }
617 if (strncmp(path, "/proc", 5) == 0) {
618 up_users();
619 ret = do_proc_release(path, fi);
620 down_users();
621 return ret;
622 }
623
624 return -EINVAL;
625 }
626
627 static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
628 {
629 return 0;
630 }
631
632 int lxcfs_mkdir(const char *path, mode_t mode)
633 {
634 int ret;
635 if (strncmp(path, "/cgroup", 7) == 0) {
636 up_users();
637 ret = do_cg_mkdir(path, mode);
638 down_users();
639 return ret;
640 }
641
642 return -EPERM;
643 }
644
645 int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
646 {
647 int ret;
648 if (strncmp(path, "/cgroup", 7) == 0) {
649 up_users();
650 ret = do_cg_chown(path, uid, gid);
651 down_users();
652 return ret;
653 }
654
655 if (strncmp(path, "/proc", 5) == 0)
656 return -EPERM;
657
658 return -ENOENT;
659 }
660
661 /*
662 * cat first does a truncate before doing ops->write. This doesn't
663 * really make sense for cgroups. So just return 0 always but do
664 * nothing.
665 */
666 int lxcfs_truncate(const char *path, off_t newsize)
667 {
668 if (strncmp(path, "/cgroup", 7) == 0)
669 return 0;
670 return -EPERM;
671 }
672
673 int lxcfs_rmdir(const char *path)
674 {
675 int ret;
676 if (strncmp(path, "/cgroup", 7) == 0) {
677 up_users();
678 ret = do_cg_rmdir(path);
679 down_users();
680 return ret;
681 }
682 return -EPERM;
683 }
684
685 int lxcfs_chmod(const char *path, mode_t mode)
686 {
687 int ret;
688 if (strncmp(path, "/cgroup", 7) == 0) {
689 up_users();
690 ret = do_cg_chmod(path, mode);
691 down_users();
692 return ret;
693 }
694
695 if (strncmp(path, "/proc", 5) == 0)
696 return -EPERM;
697
698 return -ENOENT;
699 }
700
701 const struct fuse_operations lxcfs_ops = {
702 .getattr = lxcfs_getattr,
703 .readlink = NULL,
704 .getdir = NULL,
705 .mknod = NULL,
706 .mkdir = lxcfs_mkdir,
707 .unlink = NULL,
708 .rmdir = lxcfs_rmdir,
709 .symlink = NULL,
710 .rename = NULL,
711 .link = NULL,
712 .chmod = lxcfs_chmod,
713 .chown = lxcfs_chown,
714 .truncate = lxcfs_truncate,
715 .utime = NULL,
716
717 .open = lxcfs_open,
718 .read = lxcfs_read,
719 .release = lxcfs_release,
720 .write = lxcfs_write,
721
722 .statfs = NULL,
723 .flush = lxcfs_flush,
724 .fsync = lxcfs_fsync,
725
726 .setxattr = NULL,
727 .getxattr = NULL,
728 .listxattr = NULL,
729 .removexattr = NULL,
730
731 .opendir = lxcfs_opendir,
732 .readdir = lxcfs_readdir,
733 .releasedir = lxcfs_releasedir,
734
735 .fsyncdir = NULL,
736 .init = NULL,
737 .destroy = NULL,
738 .access = lxcfs_access,
739 .create = NULL,
740 .ftruncate = NULL,
741 .fgetattr = NULL,
742 };
743
744 static void usage()
745 {
746 fprintf(stderr, "Usage:\n");
747 fprintf(stderr, "\n");
748 fprintf(stderr, "lxcfs [-f|-d] [-p pidfile] mountpoint\n");
749 fprintf(stderr, " -f running foreground by default; -d enable debug output \n");
750 fprintf(stderr, " Default pidfile is %s/lxcfs.pid\n", RUNTIME_PATH);
751 fprintf(stderr, "lxcfs -h\n");
752 exit(1);
753 }
754
755 static bool is_help(char *w)
756 {
757 if (strcmp(w, "-h") == 0 ||
758 strcmp(w, "--help") == 0 ||
759 strcmp(w, "-help") == 0 ||
760 strcmp(w, "help") == 0)
761 return true;
762 return false;
763 }
764
765 bool swallow_arg(int *argcp, char *argv[], char *which)
766 {
767 int i;
768
769 for (i = 1; argv[i]; i++) {
770 if (strcmp(argv[i], which) != 0)
771 continue;
772 for (; argv[i]; i++) {
773 argv[i] = argv[i+1];
774 }
775 (*argcp)--;
776 return true;
777 }
778 return false;
779 }
780
781 bool swallow_option(int *argcp, char *argv[], char *opt, char **v)
782 {
783 int i;
784
785 for (i = 1; argv[i]; i++) {
786 if (!argv[i+1])
787 continue;
788 if (strcmp(argv[i], opt) != 0)
789 continue;
790 do {
791 *v = strdup(argv[i+1]);
792 } while (!*v);
793 for (; argv[i+1]; i++) {
794 argv[i] = argv[i+2];
795 }
796 (*argcp) -= 2;
797 return true;
798 }
799 return false;
800 }
801
802 static int set_pidfile(char *pidfile)
803 {
804 int fd;
805 char buf[50];
806 struct flock fl;
807
808 fl.l_type = F_WRLCK;
809 fl.l_whence = SEEK_SET;
810 fl.l_start = 0;
811 fl.l_len = 0;
812
813 fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
814 if (fd == -1) {
815 fprintf(stderr, "Could not open pidfile %s: %m\n", pidfile);
816 return -1;
817 }
818
819 if (fcntl(fd, F_SETLK, &fl) == -1) {
820 if (errno == EAGAIN || errno == EACCES) {
821 fprintf(stderr, "PID file '%s' is already locked.\n", pidfile);
822 close(fd);
823 return -1;
824 }
825 fprintf(stderr, "Warning; unable to lock PID file, proceeding.\n");
826 }
827
828 if (ftruncate(fd, 0) == -1) {
829 fprintf(stderr, "Error truncating PID file '%s': %m", pidfile);
830 close(fd);
831 return -1;
832 }
833
834 snprintf(buf, 50, "%ld\n", (long) getpid());
835 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
836 fprintf(stderr, "Error writing to PID file '%s': %m", pidfile);
837 close(fd);
838 return -1;
839 }
840
841 return fd;
842 }
843
844 int main(int argc, char *argv[])
845 {
846 int ret = EXIT_FAILURE;
847 int pidfd = -1;
848 char *pidfile = NULL, *v = NULL;
849 size_t pidfile_len;
850 bool debug = false;
851 /*
852 * what we pass to fuse_main is:
853 * argv[0] -s [-f|-d] -o allow_other,directio argv[1] NULL
854 */
855 int nargs = 5, cnt = 0;
856 char *newargv[6];
857
858 /* accomodate older init scripts */
859 swallow_arg(&argc, argv, "-s");
860 swallow_arg(&argc, argv, "-f");
861 debug = swallow_arg(&argc, argv, "-d");
862 if (swallow_option(&argc, argv, "-o", &v)) {
863 if (strcmp(v, "allow_other") != 0) {
864 fprintf(stderr, "Warning: unexpected fuse option %s\n", v);
865 exit(EXIT_FAILURE);
866 }
867 free(v);
868 v = NULL;
869 }
870 if (swallow_option(&argc, argv, "-p", &v))
871 pidfile = v;
872
873 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
874 fprintf(stderr, "%s\n", VERSION);
875 exit(EXIT_SUCCESS);
876 }
877 if (argc != 2 || is_help(argv[1]))
878 usage();
879
880 do_reload();
881 if (signal(SIGUSR1, reload_handler) == SIG_ERR) {
882 fprintf(stderr, "Error setting USR1 signal handler: %m\n");
883 goto out;
884 }
885
886 newargv[cnt++] = argv[0];
887 if (debug) {
888 newargv[cnt++] = "-d";
889 } else {
890 newargv[cnt++] = "-f";
891 }
892 newargv[cnt++] = "-o";
893 newargv[cnt++] = "allow_other,direct_io,entry_timeout=0.5,attr_timeout=0.5";
894 newargv[cnt++] = argv[1];
895 newargv[cnt++] = NULL;
896
897 if (!pidfile) {
898 pidfile_len = strlen(RUNTIME_PATH) + strlen("/lxcfs.pid") + 1;
899 pidfile = alloca(pidfile_len);
900 snprintf(pidfile, pidfile_len, "%s/lxcfs.pid", RUNTIME_PATH);
901 }
902 if ((pidfd = set_pidfile(pidfile)) < 0)
903 goto out;
904
905 if (!fuse_main(nargs, newargv, &lxcfs_ops, NULL))
906 ret = EXIT_SUCCESS;
907
908 out:
909 if (dlopen_handle)
910 dlclose(dlopen_handle);
911 if (pidfile)
912 unlink(pidfile);
913 if (pidfd > 0)
914 close(pidfd);
915 exit(ret);
916 }