c8f0256a351cdb0f69375ddf32f801f120898d12
[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     return NULL;
93   }
94
95   /* Close the directory handle */
96   if (closedir (dir) == -1) {
97     reply_with_perror ("closedir: /sys/block");
98     free_stringslen(r, size);
99     return NULL;
100   }
101
102   /* Free the result list on error */
103   if(err) {
104     free_stringslen(r, size);
105     return NULL;
106   }
107
108   /* Sort the devices.  Note that r might be NULL if there are no devices. */
109   if (r != NULL)
110     sort_strings (r, size);
111
112   /* NULL terminate the list */
113   if (add_string (&r, &size, &alloc, NULL) == -1) {
114     return NULL;
115   }
116
117   return r;
118 }
119
120 /* Add a device to the list of devices */
121 static int
122 add_device(const char *device,
123            char ***const r, int *const size, int *const alloc)
124 {
125   char dev_path[256];
126   snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
127
128   if (add_string (r, size, alloc, dev_path) == -1) {
129     return -1;
130   }
131
132   return 0;
133 }
134
135 char **
136 do_list_devices (void)
137 {
138   return foreach_block_device(add_device);
139 }
140
141 static int
142 add_partitions(const char *device,
143                char ***const r, int *const size, int *const alloc)
144 {
145   char devdir[256];
146
147   /* Open the device's directory under /sys/block */
148   snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
149
150   DIR *dir = opendir (devdir);
151   if (!dir) {
152     reply_with_perror ("opendir: %s", devdir);
153     free_stringslen (*r, *size);
154     return -1;
155   }
156
157   /* Look in /sys/block/<device>/ for entries starting with <device>
158    * e.g. /sys/block/sda/sda1
159    */
160   errno = 0;
161   struct dirent *d;
162   while ((d = readdir (dir)) != NULL) {
163     if (STREQLEN (d->d_name, device, strlen (device))) {
164       char part[256];
165       snprintf (part, sizeof part, "/dev/%s", d->d_name);
166
167       if (add_string (r, size, alloc, part) == -1) {
168         closedir (dir);
169         return -1;
170       }
171     }
172   }
173
174   /* Check if readdir failed */
175   if(0 != errno) {
176       reply_with_perror ("readdir: %s", devdir);
177       free_stringslen(*r, *size);
178       return -1;
179   }
180
181   /* Close the directory handle */
182   if (closedir (dir) == -1) {
183     reply_with_perror ("closedir: /sys/block/%s", device);
184     free_stringslen (*r, *size);
185     return -1;
186   }
187
188   return 0;
189 }
190
191 char **
192 do_list_partitions (void)
193 {
194   return foreach_block_device(add_partitions);
195 }
196
197 char *
198 do_part_to_dev (const char *part)
199 {
200   int err = 1;
201   size_t n = strlen (part);
202
203   while (n >= 1 && c_isdigit (part[n-1])) {
204     err = 0;
205     n--;
206   }
207
208   if (err) {
209     reply_with_error ("device name is not a partition");
210     return NULL;
211   }
212
213   char *r = strndup (part, n);
214   if (r == NULL) {
215     reply_with_perror ("strdup");
216     return NULL;
217   }
218
219   return r;
220 }