daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / fish / options.c
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010-2011 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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "c-ctype.h"
25
26 #include "guestfs.h"
27
28 #include "options.h"
29
30 char
31 add_drives (struct drv *drv, char next_drive)
32 {
33   int r;
34   struct guestfs_add_drive_opts_argv ad_optargs;
35
36   if (next_drive > 'z') {
37     fprintf (stderr,
38              _("%s: too many drives added on the command line\n"),
39              program_name);
40     exit (EXIT_FAILURE);
41   }
42
43   if (drv) {
44     next_drive = add_drives (drv->next, next_drive);
45
46     if (asprintf (&drv->device, "/dev/sd%c", next_drive) == -1) {
47       perror ("asprintf");
48       exit (EXIT_FAILURE);
49     }
50
51     switch (drv->type) {
52     case drv_a:
53       ad_optargs.bitmask = 0;
54       if (read_only) {
55         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
56         ad_optargs.readonly = 1;
57       }
58       if (drv->a.format) {
59         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
60         ad_optargs.format = drv->a.format;
61       }
62       r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
63       if (r == -1)
64         exit (EXIT_FAILURE);
65
66       drv->nr_drives = 1;
67       next_drive++;
68       break;
69
70     case drv_d:
71       r = add_libvirt_drives (drv->d.guest);
72       if (r == -1)
73         exit (EXIT_FAILURE);
74
75       drv->nr_drives = r;
76       next_drive += r;
77       break;
78
79     case drv_N:
80       /* guestfs_add_drive (ie. autodetecting) should be safe here
81        * since we have just created the prepared disk.  At the moment
82        * it will always be "raw" but in a theoretical future we might
83        * create other formats.
84        */
85       /* -N option is not affected by --ro */
86       r = guestfs_add_drive (g, drv->N.filename);
87       if (r == -1)
88         exit (EXIT_FAILURE);
89
90       drv->nr_drives = 1;
91       next_drive++;
92       break;
93
94     default: /* keep GCC happy */
95       abort ();
96     }
97   }
98
99   return next_drive;
100 }
101
102 static void display_mountpoints_on_failure (const char *mp_device);
103 static void canonical_device_name (char *dev);
104
105 /* List is built in reverse order, so mount them in reverse order. */
106 void
107 mount_mps (struct mp *mp)
108 {
109   int r;
110
111   if (mp) {
112     mount_mps (mp->next);
113
114     const char *options;
115     if (mp->options)
116       options = mp->options;
117     else if (read_only)
118       options = "ro";
119     else
120       options = "";
121
122     /* Don't use guestfs_mount here because that will default to mount
123      * options -o sync,noatime.  For more information, see guestfs(3)
124      * section "LIBGUESTFS GOTCHAS".
125      */
126     r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
127     if (r == -1) {
128       display_mountpoints_on_failure (mp->device);
129       exit (EXIT_FAILURE);
130     }
131   }
132 }
133
134 /* If the -m option fails on any command, display a useful error
135  * message listing the mountpoints.
136  */
137 static void
138 display_mountpoints_on_failure (const char *mp_device)
139 {
140   char **fses;
141   size_t i;
142
143   fses = guestfs_list_filesystems (g);
144   if (fses == NULL)
145     return;
146   if (fses[0] == NULL) {
147     free (fses);
148     return;
149   }
150
151   fprintf (stderr,
152            _("%s: '%s' could not be mounted.  Did you mean one of these?\n"),
153            program_name, mp_device);
154
155   for (i = 0; fses[i] != NULL; i += 2) {
156     canonical_device_name (fses[i]);
157     fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
158     free (fses[i]);
159     free (fses[i+1]);
160   }
161
162   free (fses);
163 }
164
165 static void
166 canonical_device_name (char *dev)
167 {
168   if (STRPREFIX (dev, "/dev/") &&
169       (dev[5] == 'h' || dev[5] == 'v') &&
170       dev[6] == 'd' &&
171       c_isalpha (dev[7]) &&
172       (c_isdigit (dev[8]) || dev[8] == '\0'))
173     dev[5] = 's';
174 }
175
176 void
177 free_drives (struct drv *drv)
178 {
179   if (!drv) return;
180   free_drives (drv->next);
181
182   free (drv->device);
183
184   switch (drv->type) {
185   case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
186   case drv_d: /* d.filename is optarg, don't free it */ break;
187   case drv_N:
188     free (drv->N.filename);
189     drv->N.data_free (drv->N.data);
190     break;
191   default: ;                    /* keep GCC happy */
192   }
193   free (drv);
194 }
195
196 void
197 free_mps (struct mp *mp)
198 {
199   if (!mp) return;
200   free_mps (mp->next);
201
202   /* The drive and mountpoint fields are not allocated
203    * from the heap, so we should not free them here.
204    */
205
206   free (mp);
207 }