1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/* libguestfs - the guestfsd daemon
* Copyright (C) 2009 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include "c-ctype.h"
#include "daemon.h"
#include "actions.h"
typedef int (*block_dev_func_t)(const char *dev,
char ***r, int *size, int *alloc);
/* Execute a given function for each discovered block device */
static char**
foreach_block_device (block_dev_func_t func)
{
char **r = NULL;
int size = 0, alloc = 0;
DIR *dir;
int err = 0;
dir = opendir ("/sys/block");
if (!dir) {
reply_with_perror ("opendir: /sys/block");
return NULL;
}
while(1) {
errno = 0;
struct dirent *d = readdir(dir);
if(NULL == d) break;
if (STREQLEN (d->d_name, "sd", 2) ||
STREQLEN (d->d_name, "hd", 2) ||
STREQLEN (d->d_name, "vd", 2) ||
STREQLEN (d->d_name, "sr", 2)) {
char dev_path[256];
snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
/* Ignore the root device. */
if (is_root_device (dev_path))
continue;
/* RHBZ#514505: Some versions of qemu <= 0.10 add a
* CD-ROM device even though we didn't request it. Try to
* detect this by seeing if the device contains media.
*/
int fd = open (dev_path, O_RDONLY);
if (fd == -1) {
perror (dev_path);
continue;
}
close (fd);
/* Call the map function for this device */
if((*func)(d->d_name, &r, &size, &alloc) != 0) {
err = 1;
break;
}
}
}
/* Check readdir didn't fail */
if(0 != errno) {
reply_with_perror ("readdir: /sys/block");
free_stringslen(r, size);
return NULL;
}
/* Close the directory handle */
if (closedir (dir) == -1) {
reply_with_perror ("closedir: /sys/block");
free_stringslen(r, size);
return NULL;
}
/* Free the result list on error */
if(err) {
free_stringslen(r, size);
return NULL;
}
/* Sort the devices */
sort_strings (r, size);
/* NULL terminate the list */
if (add_string (&r, &size, &alloc, NULL) == -1) {
return NULL;
}
return r;
}
/* Add a device to the list of devices */
static int
add_device(const char *device,
char ***const r, int *const size, int *const alloc)
{
char dev_path[256];
snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
if (add_string (r, size, alloc, dev_path) == -1) {
return -1;
}
return 0;
}
char **
do_list_devices (void)
{
return foreach_block_device(add_device);
}
static int
add_partitions(const char *device,
char ***const r, int *const size, int *const alloc)
{
char devdir[256];
/* Open the device's directory under /sys/block */
snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
DIR *dir = opendir (devdir);
if (!dir) {
reply_with_perror ("opendir: %s", devdir);
free_stringslen (*r, *size);
return -1;
}
/* Look in /sys/block/<device>/ for entries starting with <device>
* e.g. /sys/block/sda/sda1
*/
errno = 0;
struct dirent *d;
while ((d = readdir (dir)) != NULL) {
if (STREQLEN (d->d_name, device, strlen (device))) {
char part[256];
snprintf (part, sizeof part, "/dev/%s", d->d_name);
if (add_string (r, size, alloc, part) == -1) {
closedir (dir);
return -1;
}
}
}
/* Check if readdir failed */
if(0 != errno) {
reply_with_perror ("readdir: %s", devdir);
free_stringslen(*r, *size);
return -1;
}
/* Close the directory handle */
if (closedir (dir) == -1) {
reply_with_perror ("closedir: /sys/block/%s", device);
free_stringslen (*r, *size);
return -1;
}
return 0;
}
char **
do_list_partitions (void)
{
return foreach_block_device(add_partitions);
}
char *
do_part_to_dev (const char *part)
{
int err = 1;
size_t n = strlen (part);
while (n >= 1 && c_isdigit (part[n-1])) {
err = 0;
n--;
}
if (err) {
reply_with_error ("device name is not a partition");
return NULL;
}
char *r = strndup (part, n);
if (r == NULL) {
reply_with_perror ("strdup");
return NULL;
}
return r;
}
|