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