daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / blkid.c
1 /* libguestfs - the guestfsd daemon
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., 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 <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29
30 static char *
31 get_blkid_tag (const char *device, const char *tag)
32 {
33   char *out, *err;
34   int r;
35
36   r = commandr (&out, &err,
37                 "blkid",
38                 /* Adding -c option kills all caching, even on RHEL 5. */
39                 "-c", "/dev/null",
40                 "-o", "value", "-s", tag, device, NULL);
41   if (r != 0 && r != 2) {
42     if (r >= 0)
43       reply_with_error ("%s: %s (blkid returned %d)", device, err, r);
44     else
45       reply_with_error ("%s: %s", device, err);
46     free (out);
47     free (err);
48     return NULL;
49   }
50
51   free (err);
52
53   if (r == 2) {                 /* means UUID etc not found */
54     free (out);
55     out = strdup ("");
56     if (out == NULL)
57       reply_with_perror ("strdup");
58     return out;
59   }
60
61   /* Trim trailing \n if present. */
62   size_t len = strlen (out);
63   if (len > 0 && out[len-1] == '\n')
64     out[len-1] = '\0';
65
66   return out;                   /* caller frees */
67 }
68
69 char *
70 do_vfs_type (const char *device)
71 {
72   return get_blkid_tag (device, "TYPE");
73 }
74
75 char *
76 do_vfs_label (const char *device)
77 {
78   return get_blkid_tag (device, "LABEL");
79 }
80
81 char *
82 do_vfs_uuid (const char *device)
83 {
84   return get_blkid_tag (device, "UUID");
85 }
86
87 /* RHEL5 blkid doesn't have the -p(partition info) option and the
88  * -i(I/O limits) option so we must test for these options the first
89  * time the function is called.
90  */
91 static int
92 test_blkid_p_opt(void)
93 {
94   static int result;
95   char *err = NULL;
96
97   int r = commandr(NULL, &err, "blkid", "-p", "/dev/null", NULL);
98   if (r == -1) {
99     reply_with_error("could not run 'blkid' command: %s", err);
100     free(err);
101     return -1;
102   }
103
104   if (strstr(err, "invalid option --"))
105     result = 0;
106   else
107     result = 1;
108   free(err);
109   return result;
110 }
111
112 static char **
113 blkid_with_p_opt(const char *device)
114 {
115   int r;
116   char *out = NULL, *err = NULL;
117   char **lines = NULL;
118   char **ret = NULL;
119   int size = 0, alloc = 0;
120
121   r = command(&out, &err, "blkid", "-c", "/dev/null",
122                "-p", "-i", "-o", "export", device, NULL);
123   if (r == -1) {
124     reply_with_error("%s", err);
125     goto error;
126   }
127
128   /* Split the command output into lines */
129   lines = split_lines(out);
130   if (lines == NULL) {
131     reply_with_perror("malloc");
132     goto error;
133   }
134
135   /* Parse the output of blkid -p -i -o export:
136    * UUID=b6d83437-c6b4-4bf0-8381-ef3fc3578590
137    * VERSION=1.0
138    * TYPE=ext2
139    * USAGE=filesystem
140    * MINIMUM_IO_SIZE=512
141    * PHYSICAL_SECTOR_SIZE=512
142    * LOGICAL_SECTOR_SIZE=512
143    * PART_ENTRY_SCHEME=dos
144    * PART_ENTRY_TYPE=0x83
145    * PART_ENTRY_NUMBER=6
146    * PART_ENTRY_OFFSET=642875153
147    * PART_ENTRY_SIZE=104857600
148    * PART_ENTRY_DISK=8:0
149    */
150   for (char **i = lines; *i != NULL; i++) {
151     char *line = *i;
152
153     /* Skip blank lines (shouldn't happen) */
154     if (line[0] == '\0') continue;
155
156     /* Split the line in 2 at the equals sign */
157     char *eq = strchr(line, '=');
158     if (eq) {
159       *eq = '\0'; eq++;
160
161       /* Add the key/value pair to the output */
162       if (add_string(&ret, &size, &alloc, line) == -1 ||
163           add_string(&ret, &size, &alloc, eq) == -1) goto error;
164     } else {
165       fprintf(stderr, "blkid: unexpected blkid output ignored: %s", line);
166     }
167   }
168
169   if (add_string(&ret, &size, &alloc, NULL) == -1) goto error;
170
171   free(out);
172   free(err);
173   free(lines);
174
175   return ret;
176
177 error:
178   free(out);
179   free(err);
180   if (lines) free(lines);
181   if (ret) free_strings(ret);
182
183   return NULL;
184 }
185
186 static char **
187 blkid_without_p_opt(const char *device)
188 {
189   char **ret = NULL;
190   int size = 0, alloc = 0;
191
192   if (add_string(&ret, &size, &alloc, "TYPE") == -1) goto error;
193   if (add_string(&ret, &size, &alloc, get_blkid_tag(device, "TYPE")) == -1)
194     goto error;
195   if (add_string(&ret, &size, &alloc, "LABEL") == -1) goto error;
196   if (add_string(&ret, &size, &alloc, get_blkid_tag(device, "LABEL")) == -1)
197     goto error;
198   if (add_string(&ret, &size, &alloc, "UUID") == -1) goto error;
199   if (add_string(&ret, &size, &alloc, get_blkid_tag(device, "UUID")) == -1)
200     goto error;
201   if (add_string_nodup(&ret, &size, &alloc, NULL) == -1) goto error;
202
203   return ret;
204 error:
205   if (ret) free_strings(ret);
206   return NULL;
207 }
208
209 char **
210 do_blkid(const char *device)
211 {
212   int r;
213   char *out = NULL, *err = NULL;
214   char **lines = NULL;
215
216   char **ret = NULL;
217   int size = 0, alloc = 0;
218   int blkid_has_p_opt;
219
220   if ((blkid_has_p_opt = test_blkid_p_opt()) == -1)
221     return NULL;
222   else if (blkid_has_p_opt)
223     return blkid_with_p_opt(device);
224   else
225     return blkid_without_p_opt(device);
226 }