79da41b03dae4c127cd67df9fe1dc6930e2e375f
[libguestfs.git] / daemon / devsparts.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., 675 Mass Ave, Cambridge, MA 02139, 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 <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "c-ctype.h"
30
31 #include "daemon.h"
32 #include "actions.h"
33
34 typedef int (*block_dev_func_t)(const char *dev,
35                                 char ***r, int *size, int *alloc);
36
37 /* Execute a given function for each discovered block device */
38 static char**
39 foreach_block_device (block_dev_func_t func)
40 {
41   char **r = NULL;
42   int size = 0, alloc = 0;
43
44   DIR *dir;
45   int err = 0;
46
47   dir = opendir ("/sys/block");
48   if (!dir) {
49     reply_with_perror ("opendir: /sys/block");
50     return NULL;
51   }
52
53   while(1) {
54     errno = 0;
55     struct dirent *d = readdir(dir);
56     if(NULL == d) break;
57
58     if (STREQLEN (d->d_name, "sd", 2) ||
59         STREQLEN (d->d_name, "hd", 2) ||
60         STREQLEN (d->d_name, "vd", 2) ||
61         STREQLEN (d->d_name, "sr", 2)) {
62       char dev_path[256];
63       snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
64
65       /* Ignore the root device. */
66       if (is_root_device (dev_path))
67         continue;
68
69       /* RHBZ#514505: Some versions of qemu <= 0.10 add a
70        * CD-ROM device even though we didn't request it.  Try to
71        * detect this by seeing if the device contains media.
72        */
73       int fd = open (dev_path, O_RDONLY);
74       if (fd == -1) {
75         perror (dev_path);
76         continue;
77       }
78       close (fd);
79
80       /* Call the map function for this device */
81       if((*func)(d->d_name, &r, &size, &alloc) != 0) {
82         err = 1;
83         break;
84       }
85     }
86   }
87
88   /* Check readdir didn't fail */
89   if(0 != errno) {
90     reply_with_perror ("readdir: /sys/block");
91     free_stringslen(r, size);
92     closedir (dir);
93     return NULL;
94   }
95
96   /* Close the directory handle */
97   if (closedir (dir) == -1) {
98     reply_with_perror ("closedir: /sys/block");
99     free_stringslen(r, size);
100     return NULL;
101   }
102
103   /* Free the result list on error */
104   if(err) {
105     free_stringslen(r, size);
106     return NULL;
107   }
108
109   /* Sort the devices.  Note that r might be NULL if there are no devices. */
110   if (r != NULL)
111     sort_strings (r, size);
112
113   /* NULL terminate the list */
114   if (add_string (&r, &size, &alloc, NULL) == -1) {
115     return NULL;
116   }
117
118   return r;
119 }
120
121 /* Add a device to the list of devices */
122 static int
123 add_device(const char *device,
124            char ***const r, int *const size, int *const alloc)
125 {
126   char dev_path[256];
127   snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
128
129   if (add_string (r, size, alloc, dev_path) == -1) {
130     return -1;
131   }
132
133   return 0;
134 }
135
136 char **
137 do_list_devices (void)
138 {
139   return foreach_block_device(add_device);
140 }
141
142 static int
143 add_partitions(const char *device,
144                char ***const r, int *const size, int *const alloc)
145 {
146   char devdir[256];
147
148   /* Open the device's directory under /sys/block */
149   snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
150
151   DIR *dir = opendir (devdir);
152   if (!dir) {
153     reply_with_perror ("opendir: %s", devdir);
154     free_stringslen (*r, *size);
155     return -1;
156   }
157
158   /* Look in /sys/block/<device>/ for entries starting with <device>
159    * e.g. /sys/block/sda/sda1
160    */
161   errno = 0;
162   struct dirent *d;
163   while ((d = readdir (dir)) != NULL) {
164     if (STREQLEN (d->d_name, device, strlen (device))) {
165       char part[256];
166       snprintf (part, sizeof part, "/dev/%s", d->d_name);
167
168       if (add_string (r, size, alloc, part) == -1) {
169         closedir (dir);
170         return -1;
171       }
172     }
173   }
174
175   /* Check if readdir failed */
176   if(0 != errno) {
177       reply_with_perror ("readdir: %s", devdir);
178       free_stringslen(*r, *size);
179       closedir (dir);
180       return -1;
181   }
182
183   /* Close the directory handle */
184   if (closedir (dir) == -1) {
185     reply_with_perror ("closedir: /sys/block/%s", device);
186     free_stringslen (*r, *size);
187     return -1;
188   }
189
190   return 0;
191 }
192
193 char **
194 do_list_partitions (void)
195 {
196   return foreach_block_device(add_partitions);
197 }
198
199 char *
200 do_part_to_dev (const char *part)
201 {
202   int err = 1;
203   size_t n = strlen (part);
204
205   while (n >= 1 && c_isdigit (part[n-1])) {
206     err = 0;
207     n--;
208   }
209
210   if (err) {
211     reply_with_error ("device name is not a partition");
212     return NULL;
213   }
214
215   char *r = strndup (part, n);
216   if (r == NULL) {
217     reply_with_perror ("strdup");
218     return NULL;
219   }
220
221   return r;
222 }
223
224 int
225 do_part_to_partnum (const char *part)
226 {
227   int err = 1;
228   size_t n = strlen (part);
229
230   while (n >= 1 && c_isdigit (part[n-1])) {
231     err = 0;
232     n--;
233   }
234
235   if (err) {
236     reply_with_error ("device name is not a partition");
237     return -1;
238   }
239
240   int r;
241   if (sscanf (&part[n], "%d", &r) != 1) {
242     reply_with_error ("could not parse number");
243     return -1;
244   }
245
246   return r;
247 }