fix comments; move declarations
[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 "daemon.h"
30 #include "actions.h"
31
32 char **
33 do_list_devices (void)
34 {
35   char **r = NULL;
36   int size = 0, alloc = 0;
37   DIR *dir;
38   struct dirent *d;
39   char buf[256];
40
41   dir = opendir ("/sys/block");
42   if (!dir) {
43     reply_with_perror ("opendir: /sys/block");
44     return NULL;
45   }
46
47   while ((d = readdir (dir)) != NULL) {
48     if (strncmp (d->d_name, "sd", 2) == 0 ||
49         strncmp (d->d_name, "hd", 2) == 0 ||
50         strncmp (d->d_name, "vd", 2) == 0) {
51       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
52
53       /* RHBZ#514505: Some versions of qemu <= 0.10 add a
54        * CD-ROM device even though we didn't request it.  Try to
55        * detect this by seeing if the device contains media.
56        */
57       int fd = open (buf, O_RDONLY);
58       if (fd == -1) {
59         perror (buf);
60         continue;
61       }
62       close (fd);
63
64       if (add_string (&r, &size, &alloc, buf) == -1) {
65         closedir (dir);
66         return NULL;
67       }
68     }
69   }
70
71   if (add_string (&r, &size, &alloc, NULL) == -1) {
72     closedir (dir);
73     return NULL;
74   }
75
76   if (closedir (dir) == -1) {
77     reply_with_perror ("closedir: /sys/block");
78     free_strings (r);
79     return NULL;
80   }
81
82   sort_strings (r, size-1);
83   return r;
84 }
85
86 char **
87 do_list_partitions (void)
88 {
89   char **r = NULL;
90   int size = 0, alloc = 0;
91   DIR *dir, *dir2;
92   struct dirent *d;
93   char buf[256], devname[256];
94
95   dir = opendir ("/sys/block");
96   if (!dir) {
97     reply_with_perror ("opendir: /sys/block");
98     return NULL;
99   }
100
101   while ((d = readdir (dir)) != NULL) {
102     if (strncmp (d->d_name, "sd", 2) == 0 ||
103         strncmp (d->d_name, "hd", 2) == 0 ||
104         strncmp (d->d_name, "vd", 2) == 0) {
105       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
106
107       /* RHBZ#514505: Some versions of qemu <= 0.10 add a
108        * CD-ROM device even though we didn't request it.  Try to
109        * detect this by seeing if the device contains media.
110        */
111       int fd = open (buf, O_RDONLY);
112       if (fd == -1) {
113         perror (buf);
114         continue;
115       }
116       close (fd);
117
118       strncpy (devname, d->d_name, sizeof devname);
119       devname[sizeof devname - 1] = '\0';
120
121       snprintf (buf, sizeof buf, "/sys/block/%s", devname);
122
123       dir2 = opendir (buf);
124       if (!dir2) {
125         reply_with_perror ("opendir: %s", buf);
126         free_stringslen (r, size);
127         return NULL;
128       }
129       while ((d = readdir (dir2)) != NULL) {
130         if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
131           snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
132
133           if (add_string (&r, &size, &alloc, buf) == -1) {
134             closedir (dir2);
135             closedir (dir);
136             return NULL;
137           }
138         }
139       }
140
141       if (closedir (dir2) == -1) {
142         reply_with_perror ("closedir: /sys/block/%s", devname);
143         free_stringslen (r, size);
144         return NULL;
145       }
146     }
147   }
148
149   if (add_string (&r, &size, &alloc, NULL) == -1) {
150     closedir (dir);
151     return NULL;
152   }
153
154   if (closedir (dir) == -1) {
155     reply_with_perror ("closedir: /sys/block");
156     free_strings (r);
157     return NULL;
158   }
159
160   sort_strings (r, size-1);
161   return r;
162 }
163
164 int
165 do_mkfs (char *fstype, char *device)
166 {
167   char *err;
168   int r;
169
170   IS_DEVICE (device, -1);
171
172   r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
173   if (r == -1) {
174     reply_with_error ("mkfs: %s", err);
175     free (err);
176     return -1;
177   }
178
179   free (err);
180   return 0;
181 }