fuse: Add more tracing for calls into guestmount functions.
[libguestfs.git] / fuse / guestmount.c
1 /* guestmount - mount guests using libguestfs and FUSE
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * Derived from the example program 'fusexmp.c':
19  * Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
20  *
21  * This program can be distributed under the terms of the GNU GPL.
22  * See the file COPYING.
23  */
24
25 #define FUSE_USE_VERSION 26
26
27 #include <config.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <time.h>
41 #include <assert.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <locale.h>
45
46 #include <fuse.h>
47 #include <guestfs.h>
48
49 #include "progname.h"
50
51 #include "guestmount.h"
52 #include "options.h"
53 #include "dircache.h"
54
55 /* See <attr/xattr.h> */
56 #ifndef ENOATTR
57 #define ENOATTR ENODATA
58 #endif
59
60 guestfs_h *g = NULL;
61 int read_only = 0;
62 int verbose = 0;
63 int inspector = 0;
64 int keys_from_stdin = 0;
65 int echo_keys = 0;
66 const char *libvirt_uri;
67 int dir_cache_timeout = 60;
68 static int trace_calls = 0;
69
70 #define TRACE_CALL(fs,...)                                              \
71   if (trace_calls) {                                                    \
72     fprintf (stderr, "%s: %s (" fs ")\n",                               \
73              program_name, __func__, __VA_ARGS__);                      \
74   }
75
76 static int
77 error (void)
78 {
79   return -guestfs_last_errno (g);
80 }
81
82 static struct guestfs_xattr_list *
83 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
84 {
85   struct guestfs_xattr_list *xattrs;
86
87   xattrs = malloc (sizeof *xattrs);
88   if (xattrs == NULL) {
89     perror ("malloc");
90     return NULL;
91   }
92
93   xattrs->len = num;
94   xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
95   if (xattrs->val == NULL) {
96     perror ("malloc");
97     free (xattrs);
98     return NULL;
99   }
100
101   size_t i;
102   for (i = 0; i < num; ++i) {
103     xattrs->val[i].attrname = strdup (first[i].attrname);
104     xattrs->val[i].attrval_len = first[i].attrval_len;
105     xattrs->val[i].attrval = malloc (first[i].attrval_len);
106     memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
107   }
108
109   return xattrs;
110 }
111
112 static int
113 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
114             off_t offset, struct fuse_file_info *fi)
115 {
116   TRACE_CALL ("%s, %p, %ld", path, buf, (long) offset);
117
118   time_t now;
119   time (&now);
120
121   dir_cache_remove_all_expired (now);
122
123   struct guestfs_dirent_list *ents;
124
125   ents = guestfs_readdir (g, path);
126   if (ents == NULL)
127     return error ();
128
129   size_t i;
130   for (i = 0; i < ents->len; ++i) {
131     struct stat stat;
132     memset (&stat, 0, sizeof stat);
133
134     stat.st_ino = ents->val[i].ino;
135     switch (ents->val[i].ftyp) {
136     case 'b': stat.st_mode = S_IFBLK; break;
137     case 'c': stat.st_mode = S_IFCHR; break;
138     case 'd': stat.st_mode = S_IFDIR; break;
139     case 'f': stat.st_mode = S_IFIFO; break;
140     case 'l': stat.st_mode = S_IFLNK; break;
141     case 'r': stat.st_mode = S_IFREG; break;
142     case 's': stat.st_mode = S_IFSOCK; break;
143     case 'u':
144     case '?':
145     default:  stat.st_mode = 0;
146     }
147
148     /* Copied from the example, which also ignores 'offset'.  I'm
149      * not quite sure how this is ever supposed to work on large
150      * directories. XXX
151      */
152     if (filler (buf, ents->val[i].name, &stat, 0))
153       break;
154   }
155
156   /* Now prepopulate the directory caches.  This step is just an
157    * optimization, don't worry if it fails.
158    */
159   char **names = malloc ((ents->len + 1) * sizeof (char *));
160   if (names) {
161     for (i = 0; i < ents->len; ++i)
162       names[i] = ents->val[i].name;
163     names[i] = NULL;
164
165     struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
166     if (ss) {
167       for (i = 0; i < ss->len; ++i) {
168         if (ss->val[i].ino >= 0) {
169           struct stat statbuf;
170
171           statbuf.st_dev = ss->val[i].dev;
172           statbuf.st_ino = ss->val[i].ino;
173           statbuf.st_mode = ss->val[i].mode;
174           statbuf.st_nlink = ss->val[i].nlink;
175           statbuf.st_uid = ss->val[i].uid;
176           statbuf.st_gid = ss->val[i].gid;
177           statbuf.st_rdev = ss->val[i].rdev;
178           statbuf.st_size = ss->val[i].size;
179           statbuf.st_blksize = ss->val[i].blksize;
180           statbuf.st_blocks = ss->val[i].blocks;
181           statbuf.st_atime = ss->val[i].atime;
182           statbuf.st_mtime = ss->val[i].mtime;
183           statbuf.st_ctime = ss->val[i].ctime;
184
185           lsc_insert (path, names[i], now, &statbuf);
186         }
187       }
188       guestfs_free_stat_list (ss);
189     }
190
191     struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
192     if (xattrs) {
193       size_t ni, num;
194       struct guestfs_xattr *first;
195       struct guestfs_xattr_list *copy;
196       for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
197         assert (strlen (xattrs->val[i].attrname) == 0);
198         if (xattrs->val[i].attrval_len > 0) {
199           ++i;
200           first = &xattrs->val[i];
201           num = 0;
202           for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
203             num++;
204
205           copy = copy_xattr_list (first, num);
206           if (copy)
207             xac_insert (path, names[ni], now, copy);
208
209           i--;
210         }
211       }
212       guestfs_free_xattr_list (xattrs);
213     }
214
215     char **links = guestfs_readlinklist (g, path, names);
216     if (links) {
217       for (i = 0; names[i] != NULL; ++i) {
218         if (links[i][0])
219           /* Note that rlc_insert owns the string links[i] after this, */
220           rlc_insert (path, names[i], now, links[i]);
221         else
222           /* which is why we have to free links[i] here. */
223           free (links[i]);
224       }
225       free (links);             /* free the array, not the strings */
226     }
227
228     free (names);
229   }
230
231   guestfs_free_dirent_list (ents);
232
233   return 0;
234 }
235
236 static int
237 fg_getattr (const char *path, struct stat *statbuf)
238 {
239   TRACE_CALL ("%s, %p", path, statbuf);
240
241   const struct stat *buf;
242
243   buf = lsc_lookup (path);
244   if (buf) {
245     memcpy (statbuf, buf, sizeof *statbuf);
246     return 0;
247   }
248
249   struct guestfs_stat *r;
250
251   r = guestfs_lstat (g, path);
252   if (r == NULL)
253     return error ();
254
255   statbuf->st_dev = r->dev;
256   statbuf->st_ino = r->ino;
257   statbuf->st_mode = r->mode;
258   statbuf->st_nlink = r->nlink;
259   statbuf->st_uid = r->uid;
260   statbuf->st_gid = r->gid;
261   statbuf->st_rdev = r->rdev;
262   statbuf->st_size = r->size;
263   statbuf->st_blksize = r->blksize;
264   statbuf->st_blocks = r->blocks;
265   statbuf->st_atime = r->atime;
266   statbuf->st_mtime = r->mtime;
267   statbuf->st_ctime = r->ctime;
268
269   guestfs_free_stat (r);
270
271   return 0;
272 }
273
274 /* Nautilus loves to use access(2) to test everything about a file,
275  * such as whether it's executable.  Therefore treat this a lot like
276  * fg_getattr.
277  */
278 static int
279 fg_access (const char *path, int mask)
280 {
281   TRACE_CALL ("%s, %d", path, mask);
282
283   struct stat statbuf;
284   int r;
285
286   if (read_only && (mask & W_OK))
287     return -EROFS;
288
289   r = fg_getattr (path, &statbuf);
290   if (r < 0 || mask == F_OK)
291     return r;
292
293   struct fuse_context *fuse = fuse_get_context ();
294   int ok = 1;
295
296   if (mask & R_OK)
297     ok = ok &&
298       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
299        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
300        : statbuf.st_mode & S_IROTH);
301   if (mask & W_OK)
302     ok = ok &&
303       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
304        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
305        : statbuf.st_mode & S_IWOTH);
306   if (mask & X_OK)
307     ok = ok &&
308       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
309        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
310        : statbuf.st_mode & S_IXOTH);
311
312   return ok ? 0 : -EACCES;
313 }
314
315 static int
316 fg_readlink (const char *path, char *buf, size_t size)
317 {
318   TRACE_CALL ("%s, %p, %zu", path, buf, size);
319
320   const char *r;
321   int free_it = 0;
322
323   r = rlc_lookup (path);
324   if (!r) {
325     r = guestfs_readlink (g, path);
326     if (r == NULL)
327       return error ();
328     free_it = 1;
329   }
330
331   /* Note this is different from the real readlink(2) syscall.  FUSE wants
332    * the string to be always nul-terminated, even if truncated.
333    */
334   size_t len = strlen (r);
335   if (len > size - 1)
336     len = size - 1;
337
338   memcpy (buf, r, len);
339   buf[len] = '\0';
340
341   if (free_it) {
342     char *tmp = (char *) r;
343     free (tmp);
344   }
345
346   return 0;
347 }
348
349 static int
350 fg_mknod (const char *path, mode_t mode, dev_t rdev)
351 {
352   TRACE_CALL ("%s, 0%o, 0x%lx", path, mode, (long) rdev);
353
354   int r;
355
356   if (read_only) return -EROFS;
357
358   dir_cache_invalidate (path);
359
360   r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
361   if (r == -1)
362     return error ();
363
364   return 0;
365 }
366
367 static int
368 fg_mkdir (const char *path, mode_t mode)
369 {
370   TRACE_CALL ("%s, 0%o", path, mode);
371
372   int r;
373
374   if (read_only) return -EROFS;
375
376   dir_cache_invalidate (path);
377
378   r = guestfs_mkdir_mode (g, path, mode);
379   if (r == -1)
380     return error ();
381
382   return 0;
383 }
384
385 static int
386 fg_unlink (const char *path)
387 {
388   TRACE_CALL ("%s", path);
389
390   int r;
391
392   if (read_only) return -EROFS;
393
394   dir_cache_invalidate (path);
395
396   r = guestfs_rm (g, path);
397   if (r == -1)
398     return error ();
399
400   return 0;
401 }
402
403 static int
404 fg_rmdir (const char *path)
405 {
406   TRACE_CALL ("%s", path);
407
408   int r;
409
410   if (read_only) return -EROFS;
411
412   dir_cache_invalidate (path);
413
414   r = guestfs_rmdir (g, path);
415   if (r == -1)
416     return error ();
417
418   return 0;
419 }
420
421 static int
422 fg_symlink (const char *from, const char *to)
423 {
424   TRACE_CALL ("%s, %s", from, to);
425
426   int r;
427
428   if (read_only) return -EROFS;
429
430   dir_cache_invalidate (to);
431
432   r = guestfs_ln_s (g, from, to);
433   if (r == -1)
434     return error ();
435
436   return 0;
437 }
438
439 static int
440 fg_rename (const char *from, const char *to)
441 {
442   TRACE_CALL ("%s, %s", from, to);
443
444   int r;
445
446   if (read_only) return -EROFS;
447
448   dir_cache_invalidate (from);
449   dir_cache_invalidate (to);
450
451   /* XXX It's not clear how close the 'mv' command is to the
452    * rename syscall.  We might need to add the rename syscall
453    * to the guestfs(3) API.
454    */
455   r = guestfs_mv (g, from, to);
456   if (r == -1)
457     return error ();
458
459   return 0;
460 }
461
462 static int
463 fg_link (const char *from, const char *to)
464 {
465   TRACE_CALL ("%s, %s", from, to);
466
467   int r;
468
469   if (read_only) return -EROFS;
470
471   dir_cache_invalidate (from);
472   dir_cache_invalidate (to);
473
474   r = guestfs_ln (g, from, to);
475   if (r == -1)
476     return error ();
477
478   return 0;
479 }
480
481 static int
482 fg_chmod (const char *path, mode_t mode)
483 {
484   TRACE_CALL ("%s, 0%o", path, mode);
485
486   int r;
487
488   if (read_only) return -EROFS;
489
490   dir_cache_invalidate (path);
491
492   r = guestfs_chmod (g, mode, path);
493   if (r == -1)
494     return error ();
495
496   return 0;
497 }
498
499 static int
500 fg_chown (const char *path, uid_t uid, gid_t gid)
501 {
502   TRACE_CALL ("%s, %ld, %ld", path, (long) uid, (long) gid);
503
504   int r;
505
506   if (read_only) return -EROFS;
507
508   dir_cache_invalidate (path);
509
510   r = guestfs_lchown (g, uid, gid, path);
511   if (r == -1)
512     return error ();
513
514   return 0;
515 }
516
517 static int
518 fg_truncate (const char *path, off_t size)
519 {
520   TRACE_CALL ("%s, %ld", path, (long) size);
521
522   int r;
523
524   if (read_only) return -EROFS;
525
526   dir_cache_invalidate (path);
527
528   r = guestfs_truncate_size (g, path, size);
529   if (r == -1)
530     return error ();
531
532   return 0;
533 }
534
535 static int
536 fg_utimens (const char *path, const struct timespec ts[2])
537 {
538   TRACE_CALL ("%s, [{ %ld, %ld }, { %ld, %ld }]",
539               path, ts[0].tv_sec, ts[0].tv_nsec, ts[1].tv_sec, ts[1].tv_nsec);
540
541   int r;
542
543   if (read_only) return -EROFS;
544
545   dir_cache_invalidate (path);
546
547   time_t atsecs = ts[0].tv_sec;
548   long atnsecs = ts[0].tv_nsec;
549   time_t mtsecs = ts[1].tv_sec;
550   long mtnsecs = ts[1].tv_nsec;
551
552 #ifdef UTIME_NOW
553   if (atnsecs == UTIME_NOW)
554     atnsecs = -1;
555 #endif
556 #ifdef UTIME_OMIT
557   if (atnsecs == UTIME_OMIT)
558     atnsecs = -2;
559 #endif
560 #ifdef UTIME_NOW
561   if (mtnsecs == UTIME_NOW)
562     mtnsecs = -1;
563 #endif
564 #ifdef UTIME_OMIT
565   if (mtnsecs == UTIME_OMIT)
566     mtnsecs = -2;
567 #endif
568
569   r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
570   if (r == -1)
571     return error ();
572
573   return 0;
574 }
575
576 /* All this function needs to do is to check that the requested open
577  * flags are valid.  See the notes in <fuse/fuse.h>.
578  */
579 static int
580 fg_open (const char *path, struct fuse_file_info *fi)
581 {
582   TRACE_CALL ("%s, 0%o", path, fi->flags);
583              
584   int flags = fi->flags & 3;
585
586   if (read_only && flags != O_RDONLY)
587     return -EROFS;
588
589   return 0;
590 }
591
592 static int
593 fg_read (const char *path, char *buf, size_t size, off_t offset,
594          struct fuse_file_info *fi)
595 {
596   TRACE_CALL ("%s, %p, %zu, %ld", path, buf, size, offset);
597              
598   char *r;
599   size_t rsize;
600
601   if (verbose)
602     fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
603              path, size, offset);
604
605   /* The guestfs protocol limits size to somewhere over 2MB.  We just
606    * reduce the requested size here accordingly and push the problem
607    * up to every user.  http://www.jwz.org/doc/worse-is-better.html
608    */
609   const size_t limit = 2 * 1024 * 1024;
610   if (size > limit)
611     size = limit;
612
613   r = guestfs_pread (g, path, size, offset, &rsize);
614   if (r == NULL)
615     return error ();
616
617   /* This should never happen, but at least it stops us overflowing
618    * the output buffer if it does happen.
619    */
620   if (rsize > size)
621     rsize = size;
622
623   memcpy (buf, r, rsize);
624   free (r);
625
626   return rsize;
627 }
628
629 static int
630 fg_write (const char *path, const char *buf, size_t size,
631           off_t offset, struct fuse_file_info *fi)
632 {
633   TRACE_CALL ("%s, %p, %zu, %ld", path, buf, size, offset);
634
635   if (read_only) return -EROFS;
636
637   dir_cache_invalidate (path);
638
639   /* See fg_read. */
640   const size_t limit = 2 * 1024 * 1024;
641   if (size > limit)
642     size = limit;
643
644   int r;
645   r = guestfs_pwrite (g, path, buf, size, offset);
646   if (r == -1)
647     return error ();
648
649   return r;
650 }
651
652 static int
653 fg_statfs (const char *path, struct statvfs *stbuf)
654 {
655   TRACE_CALL ("%s, %p", path, stbuf);
656
657   struct guestfs_statvfs *r;
658
659   r = guestfs_statvfs (g, path);
660   if (r == NULL)
661     return error ();
662
663   stbuf->f_bsize = r->bsize;
664   stbuf->f_frsize = r->frsize;
665   stbuf->f_blocks = r->blocks;
666   stbuf->f_bfree = r->bfree;
667   stbuf->f_bavail = r->bavail;
668   stbuf->f_files = r->files;
669   stbuf->f_ffree = r->ffree;
670   stbuf->f_favail = r->favail;
671   stbuf->f_fsid = r->fsid;
672   stbuf->f_flag = r->flag;
673   stbuf->f_namemax = r->namemax;
674
675   guestfs_free_statvfs (r);
676
677   return 0;
678 }
679
680 static int
681 fg_release (const char *path, struct fuse_file_info *fi)
682 {
683   TRACE_CALL ("%s", path);
684
685   /* Just a stub. This method is optional and can safely be left
686    * unimplemented.
687    */
688   return 0;
689 }
690
691 /* Emulate this by calling sync. */
692 static int fg_fsync(const char *path, int isdatasync,
693                      struct fuse_file_info *fi)
694 {
695   TRACE_CALL ("%s, %d", path, isdatasync);
696
697   int r;
698
699   r = guestfs_sync (g);
700   if (r == -1)
701     return error ();
702
703   return 0;
704 }
705
706 static int
707 fg_setxattr (const char *path, const char *name, const char *value,
708              size_t size, int flags)
709 {
710   TRACE_CALL ("%s, %s, %p, %zu", path, name, value, size);
711
712   int r;
713
714   if (read_only) return -EROFS;
715
716   dir_cache_invalidate (path);
717
718   /* XXX Underlying guestfs(3) API doesn't understand the flags. */
719   r = guestfs_lsetxattr (g, name, value, size, path);
720   if (r == -1)
721     return error ();
722
723   return 0;
724 }
725
726 /* The guestfs(3) API for getting xattrs is much easier to use
727  * than the real syscall.  Unfortunately we now have to emulate
728  * the real syscall using that API :-(
729  */
730 static int
731 fg_getxattr (const char *path, const char *name, char *value,
732              size_t size)
733 {
734   TRACE_CALL ("%s, %s, %p, %zu", path, name, value, size);
735
736   const struct guestfs_xattr_list *xattrs;
737   int free_attrs = 0;
738
739   xattrs = xac_lookup (path);
740   if (xattrs == NULL) {
741     xattrs = guestfs_lgetxattrs (g, path);
742     if (xattrs == NULL)
743       return error ();
744     free_attrs = 1;
745   }
746
747   size_t i;
748   int r = -ENOATTR;
749   for (i = 0; i < xattrs->len; ++i) {
750     if (STREQ (xattrs->val[i].attrname, name)) {
751       size_t sz = xattrs->val[i].attrval_len;
752       if (sz > size)
753         sz = size;
754       memcpy (value, xattrs->val[i].attrval, sz);
755       r = 0;
756       break;
757     }
758   }
759
760   if (free_attrs)
761     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
762
763   return r;
764 }
765
766 /* Ditto as above. */
767 static int
768 fg_listxattr (const char *path, char *list, size_t size)
769 {
770   TRACE_CALL ("%s, %p, %zu", path, list, size);
771
772   const struct guestfs_xattr_list *xattrs;
773   int free_attrs = 0;
774
775   xattrs = xac_lookup (path);
776   if (xattrs == NULL) {
777     xattrs = guestfs_lgetxattrs (g, path);
778     if (xattrs == NULL)
779       return error ();
780     free_attrs = 1;
781   }
782
783   size_t i;
784   ssize_t copied = 0;
785   for (i = 0; i < xattrs->len; ++i) {
786     size_t len = strlen (xattrs->val[i].attrname) + 1;
787     if (size >= len) {
788       memcpy (list, xattrs->val[i].attrname, len);
789       size -= len;
790       list += len;
791       copied += len;
792     } else {
793       copied = -ERANGE;
794       break;
795     }
796   }
797
798   if (free_attrs)
799     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
800
801   return copied;
802 }
803
804 static int
805 fg_removexattr(const char *path, const char *name)
806 {
807   TRACE_CALL ("%s, %s", path, name);
808
809   int r;
810
811   if (read_only) return -EROFS;
812
813   dir_cache_invalidate (path);
814
815   r = guestfs_lremovexattr (g, name, path);
816   if (r == -1)
817     return error ();
818
819   return 0;
820 }
821
822 static struct fuse_operations fg_operations = {
823   .getattr      = fg_getattr,
824   .access       = fg_access,
825   .readlink     = fg_readlink,
826   .readdir      = fg_readdir,
827   .mknod        = fg_mknod,
828   .mkdir        = fg_mkdir,
829   .symlink      = fg_symlink,
830   .unlink       = fg_unlink,
831   .rmdir        = fg_rmdir,
832   .rename       = fg_rename,
833   .link         = fg_link,
834   .chmod        = fg_chmod,
835   .chown        = fg_chown,
836   .truncate     = fg_truncate,
837   .utimens      = fg_utimens,
838   .open         = fg_open,
839   .read         = fg_read,
840   .write        = fg_write,
841   .statfs       = fg_statfs,
842   .release      = fg_release,
843   .fsync        = fg_fsync,
844   .setxattr     = fg_setxattr,
845   .getxattr     = fg_getxattr,
846   .listxattr    = fg_listxattr,
847   .removexattr  = fg_removexattr,
848 };
849
850 static void __attribute__((noreturn))
851 fuse_help (void)
852 {
853   const char *tmp_argv[] = { program_name, "--help", NULL };
854   fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
855   exit (EXIT_SUCCESS);
856 }
857
858 static void __attribute__((noreturn))
859 usage (int status)
860 {
861   if (status != EXIT_SUCCESS)
862     fprintf (stderr, _("Try `%s --help' for more information.\n"),
863              program_name);
864   else {
865     fprintf (stdout,
866            _("%s: FUSE module for libguestfs\n"
867              "%s lets you mount a virtual machine filesystem\n"
868              "Copyright (C) 2009-2010 Red Hat Inc.\n"
869              "Usage:\n"
870              "  %s [--options] [-- [--FUSE-options]] mountpoint\n"
871              "Options:\n"
872              "  -a|--add image       Add image\n"
873              "  -c|--connect uri     Specify libvirt URI for -d option\n"
874              "  --dir-cache-timeout  Set readdir cache timeout (default 5 sec)\n"
875              "  -d|--domain guest    Add disks from libvirt guest\n"
876              "  --echo-keys          Don't turn off echo for passphrases\n"
877              "  --format[=raw|..]    Force disk format for -a option\n"
878              "  --fuse-help          Display extra FUSE options\n"
879              "  -i|--inspector       Automatically mount filesystems\n"
880              "  --help               Display help message and exit\n"
881              "  --keys-from-stdin    Read passphrases from stdin\n"
882              "  -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
883              "  -n|--no-sync         Don't autosync\n"
884              "  -o|--option opt      Pass extra option to FUSE\n"
885              "  -r|--ro              Mount read-only\n"
886              "  --selinux            Enable SELinux support\n"
887              "  -v|--verbose         Verbose messages\n"
888              "  -V|--version         Display version and exit\n"
889              "  -x|--trace           Trace guestfs API calls\n"
890              ),
891              program_name, program_name, program_name);
892   }
893   exit (status);
894 }
895
896 int
897 main (int argc, char *argv[])
898 {
899   setlocale (LC_ALL, "");
900   bindtextdomain (PACKAGE, LOCALEBASEDIR);
901   textdomain (PACKAGE);
902
903   enum { HELP_OPTION = CHAR_MAX + 1 };
904
905   /* The command line arguments are broadly compatible with (a subset
906    * of) guestfish.  Thus we have to deal mainly with -a, -m and --ro.
907    */
908   static const char *options = "a:c:d:im:no:rv?Vwx";
909   static const struct option long_options[] = {
910     { "add", 1, 0, 'a' },
911     { "connect", 1, 0, 'c' },
912     { "dir-cache-timeout", 1, 0, 0 },
913     { "domain", 1, 0, 'd' },
914     { "echo-keys", 0, 0, 0 },
915     { "format", 2, 0, 0 },
916     { "fuse-help", 0, 0, 0 },
917     { "help", 0, 0, HELP_OPTION },
918     { "inspector", 0, 0, 'i' },
919     { "keys-from-stdin", 0, 0, 0 },
920     { "mount", 1, 0, 'm' },
921     { "no-sync", 0, 0, 'n' },
922     { "option", 1, 0, 'o' },
923     { "ro", 0, 0, 'r' },
924     { "rw", 0, 0, 'w' },
925     { "selinux", 0, 0, 0 },
926     { "trace", 0, 0, 'x' },
927     { "verbose", 0, 0, 'v' },
928     { "version", 0, 0, 'V' },
929     { 0, 0, 0, 0 }
930   };
931
932   struct drv *drvs = NULL;
933   struct drv *drv;
934   struct mp *mps = NULL;
935   struct mp *mp;
936   char *p;
937   const char *format = NULL;
938   int c, r;
939   int option_index;
940   struct sigaction sa;
941
942   int fuse_argc = 0;
943   const char **fuse_argv = NULL;
944
945 #define ADD_FUSE_ARG(str)                                               \
946   do {                                                                  \
947     fuse_argc ++;                                                       \
948     fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *));   \
949     if (!fuse_argv) {                                                   \
950       perror ("realloc");                                               \
951       exit (EXIT_FAILURE);                                                         \
952     }                                                                   \
953     fuse_argv[fuse_argc-1] = (str);                                     \
954     fuse_argv[fuse_argc] = NULL;                                        \
955   } while (0)
956
957   /* LC_ALL=C is required so we can parse error messages. */
958   setenv ("LC_ALL", "C", 1);
959
960   /* Set global program name that is not polluted with libtool artifacts.  */
961   set_program_name (argv[0]);
962
963   memset (&sa, 0, sizeof sa);
964   sa.sa_handler = SIG_IGN;
965   sa.sa_flags = SA_RESTART;
966   sigaction (SIGPIPE, &sa, NULL);
967
968   /* Various initialization. */
969   init_dir_caches ();
970
971   g = guestfs_create ();
972   if (g == NULL) {
973     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
974     exit (EXIT_FAILURE);
975   }
976
977   guestfs_set_recovery_proc (g, 0);
978
979   ADD_FUSE_ARG (program_name);
980   /* MUST be single-threaded.  You cannot have two threads accessing the
981    * same libguestfs handle, and opening more than one handle is likely
982    * to be very expensive.
983    */
984   ADD_FUSE_ARG ("-s");
985
986   /* If developing, add ./appliance to the path.  Note that libtools
987    * interferes with this because uninstalled guestfish is a shell
988    * script that runs the real program with an absolute path.  Detect
989    * that too.
990    *
991    * BUT if LIBGUESTFS_PATH environment variable is already set by
992    * the user, then don't override it.
993    */
994   if (getenv ("LIBGUESTFS_PATH") == NULL &&
995       argv[0] &&
996       (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
997     guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
998
999   for (;;) {
1000     c = getopt_long (argc, argv, options, long_options, &option_index);
1001     if (c == -1) break;
1002
1003     switch (c) {
1004     case 0:                     /* options which are long only */
1005       if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
1006         dir_cache_timeout = atoi (optarg);
1007       else if (STREQ (long_options[option_index].name, "fuse-help"))
1008         fuse_help ();
1009       else if (STREQ (long_options[option_index].name, "selinux"))
1010         guestfs_set_selinux (g, 1);
1011       else if (STREQ (long_options[option_index].name, "format")) {
1012         if (!optarg || STREQ (optarg, ""))
1013           format = NULL;
1014         else
1015           format = optarg;
1016       } else if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
1017         keys_from_stdin = 1;
1018       } else if (STREQ (long_options[option_index].name, "echo-keys")) {
1019         echo_keys = 1;
1020       } else {
1021         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
1022                  program_name, long_options[option_index].name, option_index);
1023         exit (EXIT_FAILURE);
1024       }
1025       break;
1026
1027     case 'a':
1028       OPTION_a;
1029       break;
1030
1031     case 'c':
1032       OPTION_c;
1033       break;
1034
1035     case 'd':
1036       OPTION_d;
1037       break;
1038
1039     case 'i':
1040       OPTION_i;
1041       break;
1042
1043     case 'm':
1044       OPTION_m;
1045       break;
1046
1047     case 'n':
1048       OPTION_n;
1049       break;
1050
1051     case 'o':
1052       ADD_FUSE_ARG ("-o");
1053       ADD_FUSE_ARG (optarg);
1054       break;
1055
1056     case 'r':
1057       OPTION_r;
1058       break;
1059
1060     case 'v':
1061       OPTION_v;
1062       break;
1063
1064     case 'V':
1065       OPTION_V;
1066       break;
1067
1068     case 'w':
1069       OPTION_w;
1070       break;
1071
1072     case 'x':
1073       OPTION_x;
1074       ADD_FUSE_ARG ("-f");
1075       guestfs_set_recovery_proc (g, 1);
1076       trace_calls = 1;
1077       break;
1078
1079     case HELP_OPTION:
1080       usage (EXIT_SUCCESS);
1081
1082     default:
1083       usage (EXIT_FAILURE);
1084     }
1085   }
1086
1087   /* Check we have the right options. */
1088   if (!drvs || !(mps || inspector)) {
1089     fprintf (stderr,
1090              _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1091              program_name);
1092     exit (EXIT_FAILURE);
1093   }
1094
1095   /* We'd better have a mountpoint. */
1096   if (optind+1 != argc) {
1097     fprintf (stderr,
1098              _("%s: you must specify a mountpoint in the host filesystem\n"),
1099              program_name);
1100     exit (EXIT_FAILURE);
1101   }
1102
1103   /* Do the guest drives and mountpoints. */
1104   add_drives (drvs, 'a');
1105   if (guestfs_launch (g) == -1)
1106     exit (EXIT_FAILURE);
1107   if (inspector)
1108     inspect_mount ();
1109   mount_mps (mps);
1110
1111   free_drives (drvs);
1112   free_mps (mps);
1113
1114   /* FUSE example does this, not clear if it's necessary, but ... */
1115   if (guestfs_umask (g, 0) == -1)
1116     exit (EXIT_FAILURE);
1117
1118   /* At the last minute, remove the libguestfs error handler.  In code
1119    * above this point, the default error handler has been used which
1120    * sends all errors to stderr.  Now before entering FUSE itself we
1121    * want to silence errors so we can convert them (see error()
1122    * function above).
1123    */
1124   guestfs_set_error_handler (g, NULL, NULL);
1125
1126   /* Finish off FUSE args. */
1127   ADD_FUSE_ARG (argv[optind]);
1128
1129   /*
1130     It says about the line containing the for-statement:
1131     error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1132
1133   if (verbose) {
1134     fprintf (stderr, "guestmount: invoking FUSE with args [");
1135     for (i = 0; i < fuse_argc; ++i) {
1136       if (i > 0) fprintf (stderr, ", ");
1137       fprintf (stderr, "%s", fuse_argv[i]);
1138     }
1139     fprintf (stderr, "]\n");
1140   }
1141   */
1142
1143   r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1144
1145   /* Cleanup. */
1146   guestfs_close (g);
1147   free_dir_caches ();
1148
1149   exit (r == -1 ? 1 : 0);
1150 }