-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.c
263 lines (233 loc) · 6.45 KB
/
utils.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* virt-p2v
* Copyright (C) 2015 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, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <locale.h>
#include <libintl.h>
#include "ignore-value.h"
#include "p2v.h"
#define CHOMP(line,len) \
do { \
if ((len) > 0 && (line)[(len)-1] == '\n') { \
(line)[(len)-1] = '\0'; \
len--; \
} \
} while (0)
/**
* Return size of a block device, from F</sys/block/I<dev>/size>.
*
* This function always succeeds, or else exits (since we expect
* C<dev> to always be valid and the C<size> file to always exist).
*/
uint64_t
get_blockdev_size (const char *dev)
{
CLEANUP_FCLOSE FILE *fp = NULL;
CLEANUP_FREE char *path = NULL;
CLEANUP_FREE char *size_str = NULL;
size_t len;
uint64_t size;
if (asprintf (&path, "/sys/block/%s/size", dev) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fp = fopen (path, "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, "fopen: %s", path);
if (getline (&size_str, &len, fp) == -1)
error (EXIT_FAILURE, errno, "getline: %s", path);
if (sscanf (size_str, "%" SCNu64, &size) != 1)
error (EXIT_FAILURE, 0, "cannot parse %s: %s", path, size_str);
size /= 2*1024*1024; /* size from kernel is given in sectors? */
return size;
}
/**
* Return model of a block device, from F</sys/block/I<dev>/device/model>.
*
* Returns C<NULL> if the file was not found. The caller must
* free the returned string.
*/
char *
get_blockdev_model (const char *dev)
{
CLEANUP_FCLOSE FILE *fp = NULL;
CLEANUP_FREE char *path = NULL;
char *model = NULL;
size_t len = 0;
ssize_t n;
if (asprintf (&path, "/sys/block/%s/device/model", dev) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fp = fopen (path, "r");
if (fp == NULL) {
perror (path);
return NULL;
}
if ((n = getline (&model, &len, fp)) == -1) {
perror (path);
free (model);
return NULL;
}
CHOMP (model, n);
return model;
}
/**
* Return the serial number of a block device.
*
* This is found using the lsblk command.
*
* Returns C<NULL> if we could not get the serial number. The caller
* must free the returned string.
*/
char *
get_blockdev_serial (const char *dev)
{
CLEANUP_PCLOSE FILE *fp = NULL;
CLEANUP_FREE char *cmd = NULL;
char *serial = NULL;
size_t len = 0;
ssize_t n;
if (asprintf (&cmd, "lsblk -o serial /dev/%s --nodeps --noheadings",
dev) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fp = popen (cmd, "r");
if (fp == NULL) {
perror (cmd);
return NULL;
}
if ((n = getline (&serial, &len, fp)) == -1) {
perror (cmd);
free (serial);
return NULL;
}
CHOMP (serial, n);
return serial;
}
/**
* Return contents of F</sys/class/net/I<if_name>/address> (if found).
*/
char *
get_if_addr (const char *if_name)
{
CLEANUP_FCLOSE FILE *fp = NULL;
CLEANUP_FREE char *path = NULL;
char *content = NULL;
size_t len = 0;
ssize_t n;
if (asprintf (&path, "/sys/class/net/%s/address", if_name) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fp = fopen (path, "r");
if (fp == NULL)
return NULL;
if ((n = getline (&content, &len, fp)) == -1) {
perror (path);
free (content);
return NULL;
}
CHOMP (content, n);
return content;
}
/**
* Return contents of F</sys/class/net/I<if_name>/device/vendor> (if
* found), mapped to the PCI vendor. See:
* L<http://pjwelsh.blogspot.co.uk/2011/11/howto-get-network-card-vendor-device-or.html>
*/
char *
get_if_vendor (const char *if_name, int truncate)
{
CLEANUP_FCLOSE FILE *fp = NULL;
CLEANUP_FREE char *path = NULL;
char *line = NULL;
size_t len = 0;
ssize_t n;
char vendor[5];
if (asprintf (&path, "/sys/class/net/%s/device/vendor", if_name) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fp = fopen (path, "r");
if (fp == NULL) {
perror (path);
return NULL;
}
if ((n = getline (&line, &len, fp)) == -1) {
perror (path);
free (line);
return NULL;
}
/* Vendor is (always?) a 16 bit quantity (as defined by PCI),
* something like "0x8086" (for Intel Corp).
*/
CHOMP (line, n);
if (line[0] != '0' || line[1] != 'x' || strlen (&line[2]) != 4) {
free (line);
return NULL;
}
strcpy (vendor, &line[2]);
fclose (fp);
fp = fopen ("/usr/share/hwdata/pci.ids", "r");
if (fp == NULL) {
perror ("/usr/share/hwdata/pci.ids");
free (line);
return NULL;
}
while ((n = getline (&line, &len, fp)) != -1) {
CHOMP (line, n);
if (STRPREFIX (line, vendor)) {
/* Find the start of the name after the vendor ID and whitespace. */
size_t i = 4;
n -= 4;
while (n > 0 && isspace (line[i])) {
i++;
n--;
}
memmove (&line[0], &line[i], n+1 /* copy trailing \0 */);
/* Truncate? */
if (truncate > 0 && n > truncate)
line[n] = '\0';
return line;
}
}
free (line);
return NULL;
}
/* XXX We could make this configurable. */
#define NETWORK_ONLINE_COMMAND "nm-online -t 30"
/**
* Wait for the network to come online, but don't error out if that
* fails. The caller will call C<test_connection> immediately after
* this which will fail if the network didn't come online.
*/
void
wait_network_online (const struct config *config)
{
#ifdef DEBUG_STDERR
fprintf (stderr, "waiting for the network to come online ...\n");
fprintf (stderr, "%s\n", NETWORK_ONLINE_COMMAND);
fflush (stderr);
#endif
ignore_value (system (NETWORK_ONLINE_COMMAND));
}
int
compare_strings (const void *vp1, const void *vp2)
{
char * const *p1 = (char * const *) vp1;
char * const *p2 = (char * const *) vp2;
return strcmp (*p1, *p2);
}