daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / lvm-filter.c
1 /* libguestfs - the guestfsd daemon
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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 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 #include <inttypes.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 #include "c-ctype.h"
29 #include "ignore-value.h"
30
31 #include "daemon.h"
32 #include "actions.h"
33
34 /* This runs during daemon start up and creates a complete copy of
35  * /etc/lvm so that we can modify it as we desire.  We set
36  * LVM_SYSTEM_DIR to point to the copy.
37  */
38 static char lvm_system_dir[] = "/tmp/lvmXXXXXX";
39
40 static void rm_lvm_system_dir (void);
41
42 void
43 copy_lvm (void)
44 {
45   struct stat statbuf;
46   char cmd[64];
47   int r;
48
49   /* If /etc/lvm directory doesn't exist (or isn't a directory) assume
50    * that this system doesn't support LVM and do nothing.
51    */
52   r = stat ("/etc/lvm", &statbuf);
53   if (r == -1) {
54     perror ("copy_lvm: stat: /etc/lvm");
55     return;
56   }
57   if (! S_ISDIR (statbuf.st_mode)) {
58     fprintf (stderr, "copy_lvm: warning: /etc/lvm is not a directory\n");
59     return;
60   }
61
62   if (mkdtemp (lvm_system_dir) == NULL) {
63     perror (lvm_system_dir);
64     exit (EXIT_FAILURE);
65   }
66
67   /* Hopefully no dotfiles in there ... XXX */
68   snprintf (cmd, sizeof cmd, "cp -a /etc/lvm/* %s", lvm_system_dir);
69   r = system (cmd);
70   if (r == -1) {
71     perror (cmd);
72     rmdir (lvm_system_dir);
73     exit (EXIT_FAILURE);
74   }
75
76   if (WEXITSTATUS (r) != 0) {
77     fprintf (stderr, "cp command failed with return code %d\n",
78              WEXITSTATUS (r));
79     rmdir (lvm_system_dir);
80     exit (EXIT_FAILURE);
81   }
82
83   /* Set environment variable so we use the copy. */
84   setenv ("LVM_SYSTEM_DIR", lvm_system_dir, 1);
85
86   /* Set a handler to remove the temporary directory at exit. */
87   atexit (rm_lvm_system_dir);
88 }
89
90 static void
91 rm_lvm_system_dir (void)
92 {
93   char cmd[64];
94
95   snprintf (cmd, sizeof cmd, "rm -rf %s", lvm_system_dir);
96   ignore_value (system (cmd));
97 }
98
99 /* Does the current line match the regexp /^\s*filter\s*=/ */
100 static int
101 is_filter_line (const char *line)
102 {
103   while (*line && c_isspace (*line))
104     line++;
105   if (!*line)
106     return 0;
107
108   if (! STRPREFIX (line, "filter"))
109     return 0;
110   line += 6;
111
112   while (*line && c_isspace (*line))
113     line++;
114   if (!*line)
115     return 0;
116
117   if (*line != '=')
118     return 0;
119
120   return 1;
121 }
122
123 /* Rewrite the 'filter = [ ... ]' line in lvm.conf. */
124 static int
125 set_filter (const char *filter)
126 {
127   char lvm_conf[64];
128   snprintf (lvm_conf, sizeof lvm_conf, "%s/lvm.conf", lvm_system_dir);
129
130   char lvm_conf_new[64];
131   snprintf (lvm_conf_new, sizeof lvm_conf, "%s/lvm.conf.new", lvm_system_dir);
132
133   FILE *ifp = fopen (lvm_conf, "r");
134   if (ifp == NULL) {
135     reply_with_perror ("open: %s", lvm_conf);
136     return -1;
137   }
138   FILE *ofp = fopen (lvm_conf_new, "w");
139   if (ofp == NULL) {
140     reply_with_perror ("open: %s", lvm_conf_new);
141     fclose (ifp);
142     return -1;
143   }
144
145   char *line = NULL;
146   size_t len = 0;
147   while (getline (&line, &len, ifp) != -1) {
148     int r;
149     if (is_filter_line (line)) {
150       r = fprintf (ofp, "    filter = [ %s ]\n", filter);
151     } else {
152       r = fprintf (ofp, "%s", line);
153     }
154     if (r < 0) {
155       /* NB. fprintf doesn't set errno on error. */
156       reply_with_error ("%s: write failed", lvm_conf_new);
157       fclose (ifp);
158       fclose (ofp);
159       free (line);
160       unlink (lvm_conf_new);
161       return -1;
162     }
163   }
164
165   free (line);
166
167   if (fclose (ifp) == EOF) {
168     reply_with_perror ("close: %s", lvm_conf);
169     unlink (lvm_conf_new);
170     fclose (ofp);
171     return -1;
172   }
173   if (fclose (ofp) == EOF) {
174     reply_with_perror ("close: %s", lvm_conf_new);
175     unlink (lvm_conf_new);
176     return -1;
177   }
178
179   if (rename (lvm_conf_new, lvm_conf) == -1) {
180     reply_with_perror ("rename: %s", lvm_conf);
181     unlink (lvm_conf_new);
182     return -1;
183   }
184
185   return 0;
186 }
187
188 static int
189 vgchange (const char *vgchange_flag)
190 {
191   char *err;
192   int r = command (NULL, &err, "lvm", "vgchange", vgchange_flag, NULL);
193   if (r == -1) {
194     reply_with_error ("vgchange: %s", err);
195     free (err);
196     return -1;
197   }
198
199   free (err);
200   return 0;
201 }
202
203 /* Deactivate all VGs. */
204 static int
205 deactivate (void)
206 {
207   return vgchange ("-an");
208 }
209
210 /* Reactivate all VGs. */
211 static int
212 reactivate (void)
213 {
214   return vgchange ("-ay");
215 }
216
217 /* Clear the cache and rescan. */
218 static int
219 rescan (void)
220 {
221   char lvm_cache[64];
222   snprintf (lvm_cache, sizeof lvm_cache, "%s/cache/.cache", lvm_system_dir);
223
224   unlink (lvm_cache);
225
226   char *err;
227   int r = command (NULL, &err, "lvm", "vgscan", NULL);
228   if (r == -1) {
229     reply_with_error ("vgscan: %s", err);
230     free (err);
231     return -1;
232   }
233
234   free (err);
235   return 0;
236 }
237
238 /* Construct the new, specific filter string.  We can assume that
239  * the 'devices' array does not contain any regexp metachars,
240  * because it's already been checked by the stub code.
241  */
242 static char *
243 make_filter_string (char *const *devices)
244 {
245   size_t i;
246   size_t len = 64;
247   for (i = 0; devices[i] != NULL; ++i)
248     len += strlen (devices[i]) + 16;
249
250   char *filter = malloc (len);
251   if (filter == NULL) {
252     reply_with_perror ("malloc");
253     return NULL;
254   }
255
256   char *p = filter;
257   for (i = 0; devices[i] != NULL; ++i) {
258     /* Because of the way matching works in LVM, each match clause
259      * should be either:
260      *   "a|^/dev/sda|",      for whole block devices, or
261      *   "a|^/dev/sda1$|",    for single partitions
262      * (the assumption being we have <= 26 block devices XXX).
263      */
264     size_t slen = strlen (devices[i]);
265     char str[slen+16];
266
267     if (c_isdigit (devices[i][slen-1]))
268       snprintf (str, slen+16, "\"a|^%s$|\", ", devices[i]);
269     else
270       snprintf (str, slen+16, "\"a|^%s|\", ", devices[i]);
271
272     strcpy (p, str);
273     p += strlen (str);
274   }
275   strcpy (p, "\"r|.*|\"");
276
277   return filter;                /* Caller must free. */
278 }
279
280 int
281 do_lvm_set_filter (char *const *devices)
282 {
283   char *filter = make_filter_string (devices);
284   if (filter == NULL)
285     return -1;
286
287   if (deactivate () == -1) {
288     free (filter);
289     return -1;
290   }
291
292   int r = set_filter (filter);
293   free (filter);
294   if (r == -1)
295     return -1;
296
297   if (rescan () == -1)
298     return -1;
299
300   return reactivate ();
301 }
302
303 int
304 do_lvm_clear_filter (void)
305 {
306   if (deactivate () == -1)
307     return -1;
308
309   if (set_filter ("\"a/.*/\"") == -1)
310     return -1;
311
312   if (rescan () == -1)
313     return -1;
314
315   return reactivate ();
316 }