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