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