]>
Commit | Line | Data |
---|---|---|
e3d4d252 | 1 | /* |
42074a9d | 2 | * QEMU Guest Agent POSIX-specific command implementations |
e3d4d252 MR |
3 | * |
4 | * Copyright IBM Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Michael Roth <[email protected]> | |
3424fc9f | 8 | * Michal Privoznik <[email protected]> |
e3d4d252 MR |
9 | * |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
4459bf38 | 14 | #include "qemu/osdep.h" |
e72c3f2e | 15 | #include <sys/ioctl.h> |
2c02cbf6 | 16 | #include <sys/wait.h> |
46d4c572 | 17 | #include <dirent.h> |
e72c3f2e MR |
18 | #include "qga/guest-agent-core.h" |
19 | #include "qga-qmp-commands.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/qerror.h" |
1de7afc9 PB |
21 | #include "qemu/queue.h" |
22 | #include "qemu/host-utils.h" | |
12505396 | 23 | #include "qemu/sockets.h" |
920639ca | 24 | #include "qemu/base64.h" |
f348b6d1 | 25 | #include "qemu/cutils.h" |
4eb36d40 | 26 | |
2c02cbf6 | 27 | #ifndef CONFIG_HAS_ENVIRON |
eecae147 AF |
28 | #ifdef __APPLE__ |
29 | #include <crt_externs.h> | |
30 | #define environ (*_NSGetEnviron()) | |
31 | #else | |
2c02cbf6 LC |
32 | extern char **environ; |
33 | #endif | |
eecae147 | 34 | #endif |
2c02cbf6 | 35 | |
4eb36d40 | 36 | #if defined(__linux__) |
e3d4d252 | 37 | #include <mntent.h> |
7006b9cf | 38 | #include <linux/fs.h> |
3424fc9f MP |
39 | #include <ifaddrs.h> |
40 | #include <arpa/inet.h> | |
41 | #include <sys/socket.h> | |
42 | #include <net/if.h> | |
e3d4d252 | 43 | |
eab5fd59 | 44 | #ifdef FIFREEZE |
e72c3f2e MR |
45 | #define CONFIG_FSFREEZE |
46 | #endif | |
eab5fd59 PB |
47 | #ifdef FITRIM |
48 | #define CONFIG_FSTRIM | |
49 | #endif | |
e72c3f2e MR |
50 | #endif |
51 | ||
77dbc81b | 52 | static void ga_wait_child(pid_t pid, int *status, Error **errp) |
d220a6df LC |
53 | { |
54 | pid_t rpid; | |
55 | ||
56 | *status = 0; | |
57 | ||
58 | do { | |
59 | rpid = waitpid(pid, status, 0); | |
60 | } while (rpid == -1 && errno == EINTR); | |
61 | ||
62 | if (rpid == -1) { | |
77dbc81b MA |
63 | error_setg_errno(errp, errno, "failed to wait for child (pid: %d)", |
64 | pid); | |
d220a6df LC |
65 | return; |
66 | } | |
67 | ||
68 | g_assert(rpid == pid); | |
69 | } | |
70 | ||
77dbc81b | 71 | void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp) |
e3d4d252 | 72 | { |
e3d4d252 | 73 | const char *shutdown_flag; |
d220a6df LC |
74 | Error *local_err = NULL; |
75 | pid_t pid; | |
3674838c | 76 | int status; |
e3d4d252 MR |
77 | |
78 | slog("guest-shutdown called, mode: %s", mode); | |
79 | if (!has_mode || strcmp(mode, "powerdown") == 0) { | |
80 | shutdown_flag = "-P"; | |
81 | } else if (strcmp(mode, "halt") == 0) { | |
82 | shutdown_flag = "-H"; | |
83 | } else if (strcmp(mode, "reboot") == 0) { | |
84 | shutdown_flag = "-r"; | |
85 | } else { | |
77dbc81b | 86 | error_setg(errp, |
d220a6df | 87 | "mode is invalid (valid values are: halt|powerdown|reboot"); |
e3d4d252 MR |
88 | return; |
89 | } | |
90 | ||
d5dd3498 LC |
91 | pid = fork(); |
92 | if (pid == 0) { | |
e3d4d252 MR |
93 | /* child, start the shutdown */ |
94 | setsid(); | |
3674838c LC |
95 | reopen_fd_to_null(0); |
96 | reopen_fd_to_null(1); | |
97 | reopen_fd_to_null(2); | |
e3d4d252 | 98 | |
485e741c | 99 | execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0", |
3674838c LC |
100 | "hypervisor initiated shutdown", (char*)NULL, environ); |
101 | _exit(EXIT_FAILURE); | |
d5dd3498 | 102 | } else if (pid < 0) { |
77dbc81b | 103 | error_setg_errno(errp, errno, "failed to create child process"); |
d220a6df | 104 | return; |
e3d4d252 | 105 | } |
d5dd3498 | 106 | |
d220a6df | 107 | ga_wait_child(pid, &status, &local_err); |
84d18f06 | 108 | if (local_err) { |
77dbc81b | 109 | error_propagate(errp, local_err); |
d220a6df LC |
110 | return; |
111 | } | |
112 | ||
113 | if (!WIFEXITED(status)) { | |
77dbc81b | 114 | error_setg(errp, "child process has terminated abnormally"); |
d220a6df LC |
115 | return; |
116 | } | |
117 | ||
118 | if (WEXITSTATUS(status)) { | |
77dbc81b | 119 | error_setg(errp, "child process has failed to shutdown"); |
d5dd3498 LC |
120 | return; |
121 | } | |
122 | ||
085d8134 | 123 | /* succeeded */ |
e3d4d252 MR |
124 | } |
125 | ||
6912e6a9 LL |
126 | int64_t qmp_guest_get_time(Error **errp) |
127 | { | |
128 | int ret; | |
129 | qemu_timeval tq; | |
6912e6a9 LL |
130 | |
131 | ret = qemu_gettimeofday(&tq); | |
132 | if (ret < 0) { | |
133 | error_setg_errno(errp, errno, "Failed to get time"); | |
134 | return -1; | |
135 | } | |
136 | ||
9be38598 | 137 | return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000; |
6912e6a9 LL |
138 | } |
139 | ||
2c958923 | 140 | void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) |
a1bca57f LL |
141 | { |
142 | int ret; | |
143 | int status; | |
144 | pid_t pid; | |
145 | Error *local_err = NULL; | |
146 | struct timeval tv; | |
147 | ||
2c958923 MP |
148 | /* If user has passed a time, validate and set it. */ |
149 | if (has_time) { | |
00d2f370 MAL |
150 | GDate date = { 0, }; |
151 | ||
2c958923 MP |
152 | /* year-2038 will overflow in case time_t is 32bit */ |
153 | if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) { | |
154 | error_setg(errp, "Time %" PRId64 " is too large", time_ns); | |
155 | return; | |
156 | } | |
157 | ||
158 | tv.tv_sec = time_ns / 1000000000; | |
159 | tv.tv_usec = (time_ns % 1000000000) / 1000; | |
00d2f370 MAL |
160 | g_date_set_time_t(&date, tv.tv_sec); |
161 | if (date.year < 1970 || date.year >= 2070) { | |
162 | error_setg_errno(errp, errno, "Invalid time"); | |
163 | return; | |
164 | } | |
2c958923 MP |
165 | |
166 | ret = settimeofday(&tv, NULL); | |
167 | if (ret < 0) { | |
168 | error_setg_errno(errp, errno, "Failed to set time to guest"); | |
169 | return; | |
170 | } | |
a1bca57f LL |
171 | } |
172 | ||
2c958923 MP |
173 | /* Now, if user has passed a time to set and the system time is set, we |
174 | * just need to synchronize the hardware clock. However, if no time was | |
175 | * passed, user is requesting the opposite: set the system time from the | |
1634df56 | 176 | * hardware clock (RTC). */ |
a1bca57f LL |
177 | pid = fork(); |
178 | if (pid == 0) { | |
179 | setsid(); | |
180 | reopen_fd_to_null(0); | |
181 | reopen_fd_to_null(1); | |
182 | reopen_fd_to_null(2); | |
183 | ||
2c958923 MP |
184 | /* Use '/sbin/hwclock -w' to set RTC from the system time, |
185 | * or '/sbin/hwclock -s' to set the system time from RTC. */ | |
186 | execle("/sbin/hwclock", "hwclock", has_time ? "-w" : "-s", | |
187 | NULL, environ); | |
a1bca57f LL |
188 | _exit(EXIT_FAILURE); |
189 | } else if (pid < 0) { | |
190 | error_setg_errno(errp, errno, "failed to create child process"); | |
191 | return; | |
192 | } | |
193 | ||
194 | ga_wait_child(pid, &status, &local_err); | |
84d18f06 | 195 | if (local_err) { |
a1bca57f LL |
196 | error_propagate(errp, local_err); |
197 | return; | |
198 | } | |
199 | ||
200 | if (!WIFEXITED(status)) { | |
201 | error_setg(errp, "child process has terminated abnormally"); | |
202 | return; | |
203 | } | |
204 | ||
205 | if (WEXITSTATUS(status)) { | |
206 | error_setg(errp, "hwclock failed to set hardware clock to system time"); | |
207 | return; | |
208 | } | |
209 | } | |
210 | ||
895b00f6 MAL |
211 | typedef enum { |
212 | RW_STATE_NEW, | |
213 | RW_STATE_READING, | |
214 | RW_STATE_WRITING, | |
215 | } RwState; | |
216 | ||
e3d4d252 MR |
217 | typedef struct GuestFileHandle { |
218 | uint64_t id; | |
219 | FILE *fh; | |
895b00f6 | 220 | RwState state; |
e3d4d252 MR |
221 | QTAILQ_ENTRY(GuestFileHandle) next; |
222 | } GuestFileHandle; | |
223 | ||
224 | static struct { | |
225 | QTAILQ_HEAD(, GuestFileHandle) filehandles; | |
b4fe97c8 DL |
226 | } guest_file_state = { |
227 | .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles), | |
228 | }; | |
e3d4d252 | 229 | |
39097daf | 230 | static int64_t guest_file_handle_add(FILE *fh, Error **errp) |
e3d4d252 MR |
231 | { |
232 | GuestFileHandle *gfh; | |
39097daf MR |
233 | int64_t handle; |
234 | ||
235 | handle = ga_get_fd_handle(ga_state, errp); | |
a903f40c MA |
236 | if (handle < 0) { |
237 | return -1; | |
39097daf | 238 | } |
e3d4d252 | 239 | |
f3a06403 | 240 | gfh = g_new0(GuestFileHandle, 1); |
39097daf | 241 | gfh->id = handle; |
e3d4d252 MR |
242 | gfh->fh = fh; |
243 | QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); | |
39097daf MR |
244 | |
245 | return handle; | |
e3d4d252 MR |
246 | } |
247 | ||
77dbc81b | 248 | static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) |
e3d4d252 MR |
249 | { |
250 | GuestFileHandle *gfh; | |
251 | ||
252 | QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) | |
253 | { | |
254 | if (gfh->id == id) { | |
255 | return gfh; | |
256 | } | |
257 | } | |
258 | ||
77dbc81b | 259 | error_setg(errp, "handle '%" PRId64 "' has not been found", id); |
e3d4d252 MR |
260 | return NULL; |
261 | } | |
262 | ||
c689b4f1 LE |
263 | typedef const char * const ccpc; |
264 | ||
8fe6bbca LE |
265 | #ifndef O_BINARY |
266 | #define O_BINARY 0 | |
267 | #endif | |
268 | ||
c689b4f1 LE |
269 | /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */ |
270 | static const struct { | |
271 | ccpc *forms; | |
272 | int oflag_base; | |
273 | } guest_file_open_modes[] = { | |
8fe6bbca LE |
274 | { (ccpc[]){ "r", NULL }, O_RDONLY }, |
275 | { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY }, | |
276 | { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC }, | |
277 | { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY }, | |
278 | { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND }, | |
279 | { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY }, | |
280 | { (ccpc[]){ "r+", NULL }, O_RDWR }, | |
281 | { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY }, | |
282 | { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC }, | |
283 | { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY }, | |
284 | { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND }, | |
285 | { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY } | |
c689b4f1 LE |
286 | }; |
287 | ||
288 | static int | |
77dbc81b | 289 | find_open_flag(const char *mode_str, Error **errp) |
c689b4f1 LE |
290 | { |
291 | unsigned mode; | |
292 | ||
293 | for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { | |
294 | ccpc *form; | |
295 | ||
296 | form = guest_file_open_modes[mode].forms; | |
297 | while (*form != NULL && strcmp(*form, mode_str) != 0) { | |
298 | ++form; | |
299 | } | |
300 | if (*form != NULL) { | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | if (mode == ARRAY_SIZE(guest_file_open_modes)) { | |
77dbc81b | 306 | error_setg(errp, "invalid file open mode '%s'", mode_str); |
c689b4f1 LE |
307 | return -1; |
308 | } | |
309 | return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK; | |
310 | } | |
311 | ||
312 | #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \ | |
313 | S_IRGRP | S_IWGRP | \ | |
314 | S_IROTH | S_IWOTH) | |
315 | ||
316 | static FILE * | |
77dbc81b | 317 | safe_open_or_create(const char *path, const char *mode, Error **errp) |
c689b4f1 LE |
318 | { |
319 | Error *local_err = NULL; | |
320 | int oflag; | |
321 | ||
322 | oflag = find_open_flag(mode, &local_err); | |
323 | if (local_err == NULL) { | |
324 | int fd; | |
325 | ||
326 | /* If the caller wants / allows creation of a new file, we implement it | |
327 | * with a two step process: open() + (open() / fchmod()). | |
328 | * | |
329 | * First we insist on creating the file exclusively as a new file. If | |
330 | * that succeeds, we're free to set any file-mode bits on it. (The | |
331 | * motivation is that we want to set those file-mode bits independently | |
332 | * of the current umask.) | |
333 | * | |
334 | * If the exclusive creation fails because the file already exists | |
335 | * (EEXIST is not possible for any other reason), we just attempt to | |
336 | * open the file, but in this case we won't be allowed to change the | |
337 | * file-mode bits on the preexistent file. | |
338 | * | |
339 | * The pathname should never disappear between the two open()s in | |
340 | * practice. If it happens, then someone very likely tried to race us. | |
341 | * In this case just go ahead and report the ENOENT from the second | |
342 | * open() to the caller. | |
343 | * | |
344 | * If the caller wants to open a preexistent file, then the first | |
345 | * open() is decisive and its third argument is ignored, and the second | |
346 | * open() and the fchmod() are never called. | |
347 | */ | |
348 | fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0); | |
349 | if (fd == -1 && errno == EEXIST) { | |
350 | oflag &= ~(unsigned)O_CREAT; | |
351 | fd = open(path, oflag); | |
352 | } | |
353 | ||
354 | if (fd == -1) { | |
355 | error_setg_errno(&local_err, errno, "failed to open file '%s' " | |
356 | "(mode: '%s')", path, mode); | |
357 | } else { | |
358 | qemu_set_cloexec(fd); | |
359 | ||
360 | if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) { | |
361 | error_setg_errno(&local_err, errno, "failed to set permission " | |
362 | "0%03o on new file '%s' (mode: '%s')", | |
363 | (unsigned)DEFAULT_NEW_FILE_MODE, path, mode); | |
364 | } else { | |
365 | FILE *f; | |
366 | ||
367 | f = fdopen(fd, mode); | |
368 | if (f == NULL) { | |
369 | error_setg_errno(&local_err, errno, "failed to associate " | |
370 | "stdio stream with file descriptor %d, " | |
371 | "file '%s' (mode: '%s')", fd, path, mode); | |
372 | } else { | |
373 | return f; | |
374 | } | |
375 | } | |
376 | ||
377 | close(fd); | |
2b720018 LE |
378 | if (oflag & O_CREAT) { |
379 | unlink(path); | |
380 | } | |
c689b4f1 LE |
381 | } |
382 | } | |
383 | ||
77dbc81b | 384 | error_propagate(errp, local_err); |
c689b4f1 LE |
385 | return NULL; |
386 | } | |
387 | ||
77dbc81b MA |
388 | int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, |
389 | Error **errp) | |
e3d4d252 MR |
390 | { |
391 | FILE *fh; | |
c689b4f1 | 392 | Error *local_err = NULL; |
85b6f6f5 | 393 | int64_t handle; |
e3d4d252 MR |
394 | |
395 | if (!has_mode) { | |
396 | mode = "r"; | |
397 | } | |
398 | slog("guest-file-open called, filepath: %s, mode: %s", path, mode); | |
c689b4f1 LE |
399 | fh = safe_open_or_create(path, mode, &local_err); |
400 | if (local_err != NULL) { | |
77dbc81b | 401 | error_propagate(errp, local_err); |
e3d4d252 MR |
402 | return -1; |
403 | } | |
404 | ||
405 | /* set fd non-blocking to avoid common use cases (like reading from a | |
406 | * named pipe) from hanging the agent | |
407 | */ | |
12505396 | 408 | qemu_set_nonblock(fileno(fh)); |
e3d4d252 | 409 | |
77dbc81b | 410 | handle = guest_file_handle_add(fh, errp); |
a903f40c | 411 | if (handle < 0) { |
39097daf MR |
412 | fclose(fh); |
413 | return -1; | |
414 | } | |
415 | ||
d607a523 | 416 | slog("guest-file-open, handle: %" PRId64, handle); |
39097daf | 417 | return handle; |
e3d4d252 MR |
418 | } |
419 | ||
77dbc81b | 420 | void qmp_guest_file_close(int64_t handle, Error **errp) |
e3d4d252 | 421 | { |
77dbc81b | 422 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
e3d4d252 MR |
423 | int ret; |
424 | ||
d607a523 | 425 | slog("guest-file-close called, handle: %" PRId64, handle); |
e3d4d252 | 426 | if (!gfh) { |
e3d4d252 MR |
427 | return; |
428 | } | |
429 | ||
430 | ret = fclose(gfh->fh); | |
3ac4b7c5 | 431 | if (ret == EOF) { |
77dbc81b | 432 | error_setg_errno(errp, errno, "failed to close handle"); |
e3d4d252 MR |
433 | return; |
434 | } | |
435 | ||
436 | QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); | |
7267c094 | 437 | g_free(gfh); |
e3d4d252 MR |
438 | } |
439 | ||
440 | struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, | |
77dbc81b | 441 | int64_t count, Error **errp) |
e3d4d252 | 442 | { |
77dbc81b | 443 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
e3d4d252 MR |
444 | GuestFileRead *read_data = NULL; |
445 | guchar *buf; | |
446 | FILE *fh; | |
447 | size_t read_count; | |
448 | ||
449 | if (!gfh) { | |
e3d4d252 MR |
450 | return NULL; |
451 | } | |
452 | ||
453 | if (!has_count) { | |
454 | count = QGA_READ_COUNT_DEFAULT; | |
455 | } else if (count < 0) { | |
77dbc81b | 456 | error_setg(errp, "value '%" PRId64 "' is invalid for argument count", |
db3edb66 | 457 | count); |
e3d4d252 MR |
458 | return NULL; |
459 | } | |
460 | ||
461 | fh = gfh->fh; | |
895b00f6 MAL |
462 | |
463 | /* explicitly flush when switching from writing to reading */ | |
464 | if (gfh->state == RW_STATE_WRITING) { | |
465 | int ret = fflush(fh); | |
466 | if (ret == EOF) { | |
467 | error_setg_errno(errp, errno, "failed to flush file"); | |
468 | return NULL; | |
469 | } | |
470 | gfh->state = RW_STATE_NEW; | |
471 | } | |
472 | ||
7267c094 | 473 | buf = g_malloc0(count+1); |
e3d4d252 MR |
474 | read_count = fread(buf, 1, count, fh); |
475 | if (ferror(fh)) { | |
77dbc81b | 476 | error_setg_errno(errp, errno, "failed to read file"); |
d607a523 | 477 | slog("guest-file-read failed, handle: %" PRId64, handle); |
e3d4d252 MR |
478 | } else { |
479 | buf[read_count] = 0; | |
f3a06403 | 480 | read_data = g_new0(GuestFileRead, 1); |
e3d4d252 MR |
481 | read_data->count = read_count; |
482 | read_data->eof = feof(fh); | |
483 | if (read_count) { | |
484 | read_data->buf_b64 = g_base64_encode(buf, read_count); | |
485 | } | |
895b00f6 | 486 | gfh->state = RW_STATE_READING; |
e3d4d252 | 487 | } |
7267c094 | 488 | g_free(buf); |
e3d4d252 MR |
489 | clearerr(fh); |
490 | ||
491 | return read_data; | |
492 | } | |
493 | ||
494 | GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, | |
77dbc81b MA |
495 | bool has_count, int64_t count, |
496 | Error **errp) | |
e3d4d252 MR |
497 | { |
498 | GuestFileWrite *write_data = NULL; | |
499 | guchar *buf; | |
500 | gsize buf_len; | |
501 | int write_count; | |
77dbc81b | 502 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
e3d4d252 MR |
503 | FILE *fh; |
504 | ||
505 | if (!gfh) { | |
e3d4d252 MR |
506 | return NULL; |
507 | } | |
508 | ||
509 | fh = gfh->fh; | |
895b00f6 MAL |
510 | |
511 | if (gfh->state == RW_STATE_READING) { | |
512 | int ret = fseek(fh, 0, SEEK_CUR); | |
513 | if (ret == -1) { | |
514 | error_setg_errno(errp, errno, "failed to seek file"); | |
515 | return NULL; | |
516 | } | |
517 | gfh->state = RW_STATE_NEW; | |
518 | } | |
519 | ||
920639ca DB |
520 | buf = qbase64_decode(buf_b64, -1, &buf_len, errp); |
521 | if (!buf) { | |
522 | return NULL; | |
523 | } | |
e3d4d252 MR |
524 | |
525 | if (!has_count) { | |
526 | count = buf_len; | |
527 | } else if (count < 0 || count > buf_len) { | |
77dbc81b | 528 | error_setg(errp, "value '%" PRId64 "' is invalid for argument count", |
db3edb66 | 529 | count); |
7267c094 | 530 | g_free(buf); |
e3d4d252 MR |
531 | return NULL; |
532 | } | |
533 | ||
534 | write_count = fwrite(buf, 1, count, fh); | |
535 | if (ferror(fh)) { | |
77dbc81b | 536 | error_setg_errno(errp, errno, "failed to write to file"); |
d607a523 | 537 | slog("guest-file-write failed, handle: %" PRId64, handle); |
e3d4d252 | 538 | } else { |
f3a06403 | 539 | write_data = g_new0(GuestFileWrite, 1); |
e3d4d252 MR |
540 | write_data->count = write_count; |
541 | write_data->eof = feof(fh); | |
895b00f6 | 542 | gfh->state = RW_STATE_WRITING; |
e3d4d252 | 543 | } |
7267c094 | 544 | g_free(buf); |
e3d4d252 MR |
545 | clearerr(fh); |
546 | ||
547 | return write_data; | |
548 | } | |
549 | ||
550 | struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, | |
0b4b4938 EB |
551 | GuestFileWhence *whence_code, |
552 | Error **errp) | |
e3d4d252 | 553 | { |
77dbc81b | 554 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
e3d4d252 MR |
555 | GuestFileSeek *seek_data = NULL; |
556 | FILE *fh; | |
557 | int ret; | |
0a982b1b | 558 | int whence; |
0b4b4938 | 559 | Error *err = NULL; |
e3d4d252 MR |
560 | |
561 | if (!gfh) { | |
e3d4d252 MR |
562 | return NULL; |
563 | } | |
564 | ||
0a982b1b | 565 | /* We stupidly exposed 'whence':'int' in our qapi */ |
0b4b4938 EB |
566 | whence = ga_parse_whence(whence_code, &err); |
567 | if (err) { | |
568 | error_propagate(errp, err); | |
0a982b1b EB |
569 | return NULL; |
570 | } | |
571 | ||
e3d4d252 MR |
572 | fh = gfh->fh; |
573 | ret = fseek(fh, offset, whence); | |
574 | if (ret == -1) { | |
77dbc81b | 575 | error_setg_errno(errp, errno, "failed to seek file"); |
895b00f6 MAL |
576 | if (errno == ESPIPE) { |
577 | /* file is non-seekable, stdio shouldn't be buffering anyways */ | |
578 | gfh->state = RW_STATE_NEW; | |
579 | } | |
e3d4d252 | 580 | } else { |
10b7c5dd | 581 | seek_data = g_new0(GuestFileSeek, 1); |
e3d4d252 MR |
582 | seek_data->position = ftell(fh); |
583 | seek_data->eof = feof(fh); | |
895b00f6 | 584 | gfh->state = RW_STATE_NEW; |
e3d4d252 MR |
585 | } |
586 | clearerr(fh); | |
587 | ||
588 | return seek_data; | |
589 | } | |
590 | ||
77dbc81b | 591 | void qmp_guest_file_flush(int64_t handle, Error **errp) |
e3d4d252 | 592 | { |
77dbc81b | 593 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
e3d4d252 MR |
594 | FILE *fh; |
595 | int ret; | |
596 | ||
597 | if (!gfh) { | |
e3d4d252 MR |
598 | return; |
599 | } | |
600 | ||
601 | fh = gfh->fh; | |
602 | ret = fflush(fh); | |
603 | if (ret == EOF) { | |
77dbc81b | 604 | error_setg_errno(errp, errno, "failed to flush file"); |
895b00f6 MAL |
605 | } else { |
606 | gfh->state = RW_STATE_NEW; | |
e3d4d252 MR |
607 | } |
608 | } | |
609 | ||
e72c3f2e MR |
610 | /* linux-specific implementations. avoid this if at all possible. */ |
611 | #if defined(__linux__) | |
612 | ||
eab5fd59 | 613 | #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) |
af02203f | 614 | typedef struct FsMount { |
e3d4d252 MR |
615 | char *dirname; |
616 | char *devtype; | |
46d4c572 | 617 | unsigned int devmajor, devminor; |
af02203f PB |
618 | QTAILQ_ENTRY(FsMount) next; |
619 | } FsMount; | |
e3d4d252 | 620 | |
e5d9adbd | 621 | typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList; |
9e8aded4 | 622 | |
af02203f | 623 | static void free_fs_mount_list(FsMountList *mounts) |
9e8aded4 | 624 | { |
af02203f | 625 | FsMount *mount, *temp; |
9e8aded4 MR |
626 | |
627 | if (!mounts) { | |
628 | return; | |
629 | } | |
630 | ||
631 | QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) { | |
632 | QTAILQ_REMOVE(mounts, mount, next); | |
633 | g_free(mount->dirname); | |
634 | g_free(mount->devtype); | |
635 | g_free(mount); | |
636 | } | |
637 | } | |
638 | ||
46d4c572 TS |
639 | static int dev_major_minor(const char *devpath, |
640 | unsigned int *devmajor, unsigned int *devminor) | |
641 | { | |
642 | struct stat st; | |
643 | ||
644 | *devmajor = 0; | |
645 | *devminor = 0; | |
646 | ||
647 | if (stat(devpath, &st) < 0) { | |
648 | slog("failed to stat device file '%s': %s", devpath, strerror(errno)); | |
649 | return -1; | |
650 | } | |
651 | if (S_ISDIR(st.st_mode)) { | |
652 | /* It is bind mount */ | |
653 | return -2; | |
654 | } | |
655 | if (S_ISBLK(st.st_mode)) { | |
656 | *devmajor = major(st.st_rdev); | |
657 | *devminor = minor(st.st_rdev); | |
658 | return 0; | |
659 | } | |
660 | return -1; | |
661 | } | |
662 | ||
e3d4d252 MR |
663 | /* |
664 | * Walk the mount table and build a list of local file systems | |
665 | */ | |
46d4c572 | 666 | static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp) |
e3d4d252 MR |
667 | { |
668 | struct mntent *ment; | |
af02203f | 669 | FsMount *mount; |
9e2fa418 | 670 | char const *mtab = "/proc/self/mounts"; |
e3d4d252 | 671 | FILE *fp; |
46d4c572 | 672 | unsigned int devmajor, devminor; |
e3d4d252 | 673 | |
e3d4d252 MR |
674 | fp = setmntent(mtab, "r"); |
675 | if (!fp) { | |
77dbc81b | 676 | error_setg(errp, "failed to open mtab file: '%s'", mtab); |
261551d1 | 677 | return; |
e3d4d252 MR |
678 | } |
679 | ||
680 | while ((ment = getmntent(fp))) { | |
681 | /* | |
682 | * An entry which device name doesn't start with a '/' is | |
683 | * either a dummy file system or a network file system. | |
684 | * Add special handling for smbfs and cifs as is done by | |
685 | * coreutils as well. | |
686 | */ | |
687 | if ((ment->mnt_fsname[0] != '/') || | |
688 | (strcmp(ment->mnt_type, "smbfs") == 0) || | |
689 | (strcmp(ment->mnt_type, "cifs") == 0)) { | |
690 | continue; | |
691 | } | |
46d4c572 TS |
692 | if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) { |
693 | /* Skip bind mounts */ | |
694 | continue; | |
695 | } | |
e3d4d252 | 696 | |
f3a06403 | 697 | mount = g_new0(FsMount, 1); |
7267c094 AL |
698 | mount->dirname = g_strdup(ment->mnt_dir); |
699 | mount->devtype = g_strdup(ment->mnt_type); | |
46d4c572 TS |
700 | mount->devmajor = devmajor; |
701 | mount->devminor = devminor; | |
e3d4d252 | 702 | |
9e8aded4 | 703 | QTAILQ_INSERT_TAIL(mounts, mount, next); |
e3d4d252 MR |
704 | } |
705 | ||
706 | endmntent(fp); | |
e3d4d252 | 707 | } |
46d4c572 TS |
708 | |
709 | static void decode_mntname(char *name, int len) | |
710 | { | |
711 | int i, j = 0; | |
712 | for (i = 0; i <= len; i++) { | |
713 | if (name[i] != '\\') { | |
714 | name[j++] = name[i]; | |
715 | } else if (name[i + 1] == '\\') { | |
716 | name[j++] = '\\'; | |
717 | i++; | |
718 | } else if (name[i + 1] >= '0' && name[i + 1] <= '3' && | |
719 | name[i + 2] >= '0' && name[i + 2] <= '7' && | |
720 | name[i + 3] >= '0' && name[i + 3] <= '7') { | |
721 | name[j++] = (name[i + 1] - '0') * 64 + | |
722 | (name[i + 2] - '0') * 8 + | |
723 | (name[i + 3] - '0'); | |
724 | i += 3; | |
725 | } else { | |
726 | name[j++] = name[i]; | |
727 | } | |
728 | } | |
729 | } | |
730 | ||
731 | static void build_fs_mount_list(FsMountList *mounts, Error **errp) | |
732 | { | |
733 | FsMount *mount; | |
734 | char const *mountinfo = "/proc/self/mountinfo"; | |
735 | FILE *fp; | |
736 | char *line = NULL, *dash; | |
737 | size_t n; | |
738 | char check; | |
739 | unsigned int devmajor, devminor; | |
740 | int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e; | |
741 | ||
742 | fp = fopen(mountinfo, "r"); | |
743 | if (!fp) { | |
744 | build_fs_mount_list_from_mtab(mounts, errp); | |
745 | return; | |
746 | } | |
747 | ||
748 | while (getline(&line, &n, fp) != -1) { | |
749 | ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c", | |
750 | &devmajor, &devminor, &dir_s, &dir_e, &check); | |
751 | if (ret < 3) { | |
752 | continue; | |
753 | } | |
754 | dash = strstr(line + dir_e, " - "); | |
755 | if (!dash) { | |
756 | continue; | |
757 | } | |
758 | ret = sscanf(dash, " - %n%*s%n %n%*s%n%c", | |
759 | &type_s, &type_e, &dev_s, &dev_e, &check); | |
760 | if (ret < 1) { | |
761 | continue; | |
762 | } | |
763 | line[dir_e] = 0; | |
764 | dash[type_e] = 0; | |
765 | dash[dev_e] = 0; | |
766 | decode_mntname(line + dir_s, dir_e - dir_s); | |
767 | decode_mntname(dash + dev_s, dev_e - dev_s); | |
768 | if (devmajor == 0) { | |
769 | /* btrfs reports major number = 0 */ | |
770 | if (strcmp("btrfs", dash + type_s) != 0 || | |
771 | dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) { | |
772 | continue; | |
773 | } | |
774 | } | |
775 | ||
f3a06403 | 776 | mount = g_new0(FsMount, 1); |
46d4c572 TS |
777 | mount->dirname = g_strdup(line + dir_s); |
778 | mount->devtype = g_strdup(dash + type_s); | |
779 | mount->devmajor = devmajor; | |
780 | mount->devminor = devminor; | |
781 | ||
782 | QTAILQ_INSERT_TAIL(mounts, mount, next); | |
783 | } | |
784 | free(line); | |
785 | ||
786 | fclose(fp); | |
787 | } | |
eab5fd59 PB |
788 | #endif |
789 | ||
790 | #if defined(CONFIG_FSFREEZE) | |
e3d4d252 | 791 | |
46d4c572 TS |
792 | static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) |
793 | { | |
794 | char *path; | |
795 | char *dpath; | |
796 | char *driver = NULL; | |
797 | char buf[PATH_MAX]; | |
798 | ssize_t len; | |
799 | ||
800 | path = g_strndup(syspath, pathlen); | |
801 | dpath = g_strdup_printf("%s/driver", path); | |
802 | len = readlink(dpath, buf, sizeof(buf) - 1); | |
803 | if (len != -1) { | |
804 | buf[len] = 0; | |
805 | driver = g_strdup(basename(buf)); | |
806 | } | |
807 | g_free(dpath); | |
808 | g_free(path); | |
809 | return driver; | |
810 | } | |
811 | ||
812 | static int compare_uint(const void *_a, const void *_b) | |
813 | { | |
814 | unsigned int a = *(unsigned int *)_a; | |
815 | unsigned int b = *(unsigned int *)_b; | |
816 | ||
817 | return a < b ? -1 : a > b ? 1 : 0; | |
818 | } | |
819 | ||
820 | /* Walk the specified sysfs and build a sorted list of host or ata numbers */ | |
821 | static int build_hosts(char const *syspath, char const *host, bool ata, | |
822 | unsigned int *hosts, int hosts_max, Error **errp) | |
823 | { | |
824 | char *path; | |
825 | DIR *dir; | |
826 | struct dirent *entry; | |
827 | int i = 0; | |
828 | ||
829 | path = g_strndup(syspath, host - syspath); | |
830 | dir = opendir(path); | |
831 | if (!dir) { | |
832 | error_setg_errno(errp, errno, "opendir(\"%s\")", path); | |
833 | g_free(path); | |
834 | return -1; | |
835 | } | |
836 | ||
837 | while (i < hosts_max) { | |
838 | entry = readdir(dir); | |
839 | if (!entry) { | |
840 | break; | |
841 | } | |
842 | if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) { | |
843 | ++i; | |
844 | } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) { | |
845 | ++i; | |
846 | } | |
847 | } | |
848 | ||
849 | qsort(hosts, i, sizeof(hosts[0]), compare_uint); | |
850 | ||
851 | g_free(path); | |
852 | closedir(dir); | |
853 | return i; | |
854 | } | |
855 | ||
856 | /* Store disk device info specified by @sysfs into @fs */ | |
857 | static void build_guest_fsinfo_for_real_device(char const *syspath, | |
858 | GuestFilesystemInfo *fs, | |
859 | Error **errp) | |
860 | { | |
861 | unsigned int pci[4], host, hosts[8], tgt[3]; | |
862 | int i, nhosts = 0, pcilen; | |
863 | GuestDiskAddress *disk; | |
864 | GuestPCIAddress *pciaddr; | |
865 | GuestDiskAddressList *list = NULL; | |
866 | bool has_ata = false, has_host = false, has_tgt = false; | |
867 | char *p, *q, *driver = NULL; | |
868 | ||
869 | p = strstr(syspath, "/devices/pci"); | |
870 | if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n", | |
871 | pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) { | |
872 | g_debug("only pci device is supported: sysfs path \"%s\"", syspath); | |
873 | return; | |
874 | } | |
875 | ||
876 | driver = get_pci_driver(syspath, (p + 12 + pcilen) - syspath, errp); | |
877 | if (!driver) { | |
878 | goto cleanup; | |
879 | } | |
880 | ||
881 | p = strstr(syspath, "/target"); | |
882 | if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u", | |
883 | tgt, tgt + 1, tgt + 2) == 3) { | |
884 | has_tgt = true; | |
885 | } | |
886 | ||
887 | p = strstr(syspath, "/ata"); | |
888 | if (p) { | |
889 | q = p + 4; | |
890 | has_ata = true; | |
891 | } else { | |
892 | p = strstr(syspath, "/host"); | |
893 | q = p + 5; | |
894 | } | |
895 | if (p && sscanf(q, "%u", &host) == 1) { | |
896 | has_host = true; | |
897 | nhosts = build_hosts(syspath, p, has_ata, hosts, | |
898 | sizeof(hosts) / sizeof(hosts[0]), errp); | |
899 | if (nhosts < 0) { | |
900 | goto cleanup; | |
901 | } | |
902 | } | |
903 | ||
904 | pciaddr = g_malloc0(sizeof(*pciaddr)); | |
905 | pciaddr->domain = pci[0]; | |
906 | pciaddr->bus = pci[1]; | |
907 | pciaddr->slot = pci[2]; | |
908 | pciaddr->function = pci[3]; | |
909 | ||
910 | disk = g_malloc0(sizeof(*disk)); | |
911 | disk->pci_controller = pciaddr; | |
912 | ||
913 | list = g_malloc0(sizeof(*list)); | |
914 | list->value = disk; | |
915 | ||
916 | if (strcmp(driver, "ata_piix") == 0) { | |
917 | /* a host per ide bus, target*:0:<unit>:0 */ | |
918 | if (!has_host || !has_tgt) { | |
919 | g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver); | |
920 | goto cleanup; | |
921 | } | |
922 | for (i = 0; i < nhosts; i++) { | |
923 | if (host == hosts[i]) { | |
924 | disk->bus_type = GUEST_DISK_BUS_TYPE_IDE; | |
925 | disk->bus = i; | |
926 | disk->unit = tgt[1]; | |
927 | break; | |
928 | } | |
929 | } | |
930 | if (i >= nhosts) { | |
931 | g_debug("no host for '%s' (driver '%s')", syspath, driver); | |
932 | goto cleanup; | |
933 | } | |
934 | } else if (strcmp(driver, "sym53c8xx") == 0) { | |
935 | /* scsi(LSI Logic): target*:0:<unit>:0 */ | |
936 | if (!has_tgt) { | |
937 | g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver); | |
938 | goto cleanup; | |
939 | } | |
940 | disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI; | |
941 | disk->unit = tgt[1]; | |
942 | } else if (strcmp(driver, "virtio-pci") == 0) { | |
943 | if (has_tgt) { | |
944 | /* virtio-scsi: target*:0:0:<unit> */ | |
945 | disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI; | |
946 | disk->unit = tgt[2]; | |
947 | } else { | |
948 | /* virtio-blk: 1 disk per 1 device */ | |
949 | disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO; | |
950 | } | |
951 | } else if (strcmp(driver, "ahci") == 0) { | |
952 | /* ahci: 1 host per 1 unit */ | |
953 | if (!has_host || !has_tgt) { | |
954 | g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver); | |
955 | goto cleanup; | |
956 | } | |
957 | for (i = 0; i < nhosts; i++) { | |
958 | if (host == hosts[i]) { | |
959 | disk->unit = i; | |
960 | disk->bus_type = GUEST_DISK_BUS_TYPE_SATA; | |
961 | break; | |
962 | } | |
963 | } | |
964 | if (i >= nhosts) { | |
965 | g_debug("no host for '%s' (driver '%s')", syspath, driver); | |
966 | goto cleanup; | |
967 | } | |
968 | } else { | |
969 | g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath); | |
970 | goto cleanup; | |
971 | } | |
972 | ||
973 | list->next = fs->disk; | |
974 | fs->disk = list; | |
975 | g_free(driver); | |
976 | return; | |
977 | ||
978 | cleanup: | |
979 | if (list) { | |
980 | qapi_free_GuestDiskAddressList(list); | |
981 | } | |
982 | g_free(driver); | |
983 | } | |
984 | ||
985 | static void build_guest_fsinfo_for_device(char const *devpath, | |
986 | GuestFilesystemInfo *fs, | |
987 | Error **errp); | |
988 | ||
989 | /* Store a list of slave devices of virtual volume specified by @syspath into | |
990 | * @fs */ | |
991 | static void build_guest_fsinfo_for_virtual_device(char const *syspath, | |
992 | GuestFilesystemInfo *fs, | |
993 | Error **errp) | |
994 | { | |
995 | DIR *dir; | |
996 | char *dirpath; | |
e668d1b8 | 997 | struct dirent *entry; |
46d4c572 TS |
998 | |
999 | dirpath = g_strdup_printf("%s/slaves", syspath); | |
1000 | dir = opendir(dirpath); | |
1001 | if (!dir) { | |
8251a72f MR |
1002 | if (errno != ENOENT) { |
1003 | error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath); | |
1004 | } | |
46d4c572 TS |
1005 | g_free(dirpath); |
1006 | return; | |
1007 | } | |
46d4c572 TS |
1008 | |
1009 | for (;;) { | |
e668d1b8 HZ |
1010 | errno = 0; |
1011 | entry = readdir(dir); | |
1012 | if (entry == NULL) { | |
1013 | if (errno) { | |
1014 | error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath); | |
1015 | } | |
46d4c572 TS |
1016 | break; |
1017 | } | |
1018 | ||
e668d1b8 HZ |
1019 | if (entry->d_type == DT_LNK) { |
1020 | char *path; | |
1021 | ||
1022 | g_debug(" slave device '%s'", entry->d_name); | |
1023 | path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name); | |
1024 | build_guest_fsinfo_for_device(path, fs, errp); | |
1025 | g_free(path); | |
46d4c572 TS |
1026 | |
1027 | if (*errp) { | |
1028 | break; | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | ||
e668d1b8 | 1033 | g_free(dirpath); |
46d4c572 TS |
1034 | closedir(dir); |
1035 | } | |
1036 | ||
1037 | /* Dispatch to functions for virtual/real device */ | |
1038 | static void build_guest_fsinfo_for_device(char const *devpath, | |
1039 | GuestFilesystemInfo *fs, | |
1040 | Error **errp) | |
1041 | { | |
1042 | char *syspath = realpath(devpath, NULL); | |
1043 | ||
1044 | if (!syspath) { | |
1045 | error_setg_errno(errp, errno, "realpath(\"%s\")", devpath); | |
1046 | return; | |
1047 | } | |
1048 | ||
1049 | if (!fs->name) { | |
1050 | fs->name = g_strdup(basename(syspath)); | |
1051 | } | |
1052 | ||
1053 | g_debug(" parse sysfs path '%s'", syspath); | |
1054 | ||
1055 | if (strstr(syspath, "/devices/virtual/block/")) { | |
1056 | build_guest_fsinfo_for_virtual_device(syspath, fs, errp); | |
1057 | } else { | |
1058 | build_guest_fsinfo_for_real_device(syspath, fs, errp); | |
1059 | } | |
1060 | ||
1061 | free(syspath); | |
1062 | } | |
1063 | ||
1064 | /* Return a list of the disk device(s)' info which @mount lies on */ | |
1065 | static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount, | |
1066 | Error **errp) | |
1067 | { | |
1068 | GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs)); | |
1069 | char *devpath = g_strdup_printf("/sys/dev/block/%u:%u", | |
1070 | mount->devmajor, mount->devminor); | |
1071 | ||
1072 | fs->mountpoint = g_strdup(mount->dirname); | |
1073 | fs->type = g_strdup(mount->devtype); | |
1074 | build_guest_fsinfo_for_device(devpath, fs, errp); | |
1075 | ||
1076 | g_free(devpath); | |
1077 | return fs; | |
1078 | } | |
1079 | ||
1080 | GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) | |
1081 | { | |
1082 | FsMountList mounts; | |
1083 | struct FsMount *mount; | |
1084 | GuestFilesystemInfoList *new, *ret = NULL; | |
1085 | Error *local_err = NULL; | |
1086 | ||
1087 | QTAILQ_INIT(&mounts); | |
1088 | build_fs_mount_list(&mounts, &local_err); | |
1089 | if (local_err) { | |
1090 | error_propagate(errp, local_err); | |
1091 | return NULL; | |
1092 | } | |
1093 | ||
1094 | QTAILQ_FOREACH(mount, &mounts, next) { | |
1095 | g_debug("Building guest fsinfo for '%s'", mount->dirname); | |
1096 | ||
1097 | new = g_malloc0(sizeof(*ret)); | |
1098 | new->value = build_guest_fsinfo(mount, &local_err); | |
1099 | new->next = ret; | |
1100 | ret = new; | |
1101 | if (local_err) { | |
1102 | error_propagate(errp, local_err); | |
1103 | qapi_free_GuestFilesystemInfoList(ret); | |
1104 | ret = NULL; | |
1105 | break; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | free_fs_mount_list(&mounts); | |
1110 | return ret; | |
1111 | } | |
1112 | ||
1113 | ||
ec0f694c TS |
1114 | typedef enum { |
1115 | FSFREEZE_HOOK_THAW = 0, | |
1116 | FSFREEZE_HOOK_FREEZE, | |
1117 | } FsfreezeHookArg; | |
1118 | ||
13a439ec | 1119 | static const char *fsfreeze_hook_arg_string[] = { |
ec0f694c TS |
1120 | "thaw", |
1121 | "freeze", | |
1122 | }; | |
1123 | ||
77dbc81b | 1124 | static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp) |
ec0f694c TS |
1125 | { |
1126 | int status; | |
1127 | pid_t pid; | |
1128 | const char *hook; | |
1129 | const char *arg_str = fsfreeze_hook_arg_string[arg]; | |
1130 | Error *local_err = NULL; | |
1131 | ||
1132 | hook = ga_fsfreeze_hook(ga_state); | |
1133 | if (!hook) { | |
1134 | return; | |
1135 | } | |
1136 | if (access(hook, X_OK) != 0) { | |
77dbc81b | 1137 | error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook); |
ec0f694c TS |
1138 | return; |
1139 | } | |
1140 | ||
1141 | slog("executing fsfreeze hook with arg '%s'", arg_str); | |
1142 | pid = fork(); | |
1143 | if (pid == 0) { | |
1144 | setsid(); | |
1145 | reopen_fd_to_null(0); | |
1146 | reopen_fd_to_null(1); | |
1147 | reopen_fd_to_null(2); | |
1148 | ||
1149 | execle(hook, hook, arg_str, NULL, environ); | |
1150 | _exit(EXIT_FAILURE); | |
1151 | } else if (pid < 0) { | |
77dbc81b | 1152 | error_setg_errno(errp, errno, "failed to create child process"); |
ec0f694c TS |
1153 | return; |
1154 | } | |
1155 | ||
1156 | ga_wait_child(pid, &status, &local_err); | |
84d18f06 | 1157 | if (local_err) { |
77dbc81b | 1158 | error_propagate(errp, local_err); |
ec0f694c TS |
1159 | return; |
1160 | } | |
1161 | ||
1162 | if (!WIFEXITED(status)) { | |
77dbc81b | 1163 | error_setg(errp, "fsfreeze hook has terminated abnormally"); |
ec0f694c TS |
1164 | return; |
1165 | } | |
1166 | ||
1167 | status = WEXITSTATUS(status); | |
1168 | if (status) { | |
77dbc81b | 1169 | error_setg(errp, "fsfreeze hook has failed with status %d", status); |
ec0f694c TS |
1170 | return; |
1171 | } | |
1172 | } | |
1173 | ||
e3d4d252 MR |
1174 | /* |
1175 | * Return status of freeze/thaw | |
1176 | */ | |
77dbc81b | 1177 | GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) |
e3d4d252 | 1178 | { |
f22d85e9 MR |
1179 | if (ga_is_frozen(ga_state)) { |
1180 | return GUEST_FSFREEZE_STATUS_FROZEN; | |
1181 | } | |
1182 | ||
1183 | return GUEST_FSFREEZE_STATUS_THAWED; | |
e3d4d252 MR |
1184 | } |
1185 | ||
e99bce20 TS |
1186 | int64_t qmp_guest_fsfreeze_freeze(Error **errp) |
1187 | { | |
1188 | return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); | |
1189 | } | |
1190 | ||
e3d4d252 MR |
1191 | /* |
1192 | * Walk list of mounted file systems in the guest, and freeze the ones which | |
1193 | * are real local file systems. | |
1194 | */ | |
e99bce20 TS |
1195 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
1196 | strList *mountpoints, | |
1197 | Error **errp) | |
e3d4d252 MR |
1198 | { |
1199 | int ret = 0, i = 0; | |
e99bce20 | 1200 | strList *list; |
af02203f PB |
1201 | FsMountList mounts; |
1202 | struct FsMount *mount; | |
261551d1 | 1203 | Error *local_err = NULL; |
e3d4d252 | 1204 | int fd; |
e3d4d252 MR |
1205 | |
1206 | slog("guest-fsfreeze called"); | |
1207 | ||
ec0f694c | 1208 | execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err); |
84d18f06 | 1209 | if (local_err) { |
77dbc81b | 1210 | error_propagate(errp, local_err); |
ec0f694c TS |
1211 | return -1; |
1212 | } | |
1213 | ||
9e8aded4 | 1214 | QTAILQ_INIT(&mounts); |
261551d1 | 1215 | build_fs_mount_list(&mounts, &local_err); |
84d18f06 | 1216 | if (local_err) { |
77dbc81b | 1217 | error_propagate(errp, local_err); |
261551d1 | 1218 | return -1; |
e3d4d252 MR |
1219 | } |
1220 | ||
1221 | /* cannot risk guest agent blocking itself on a write in this state */ | |
f22d85e9 | 1222 | ga_set_frozen(ga_state); |
e3d4d252 | 1223 | |
e5d9adbd | 1224 | QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) { |
e99bce20 TS |
1225 | /* To issue fsfreeze in the reverse order of mounts, check if the |
1226 | * mount is listed in the list here */ | |
1227 | if (has_mountpoints) { | |
1228 | for (list = mountpoints; list; list = list->next) { | |
1229 | if (strcmp(list->value, mount->dirname) == 0) { | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | if (!list) { | |
1234 | continue; | |
1235 | } | |
1236 | } | |
1237 | ||
e3d4d252 MR |
1238 | fd = qemu_open(mount->dirname, O_RDONLY); |
1239 | if (fd == -1) { | |
77dbc81b | 1240 | error_setg_errno(errp, errno, "failed to open %s", mount->dirname); |
e3d4d252 MR |
1241 | goto error; |
1242 | } | |
1243 | ||
e35916ac MT |
1244 | /* we try to cull filesystems we know won't work in advance, but other |
1245 | * filesystems may not implement fsfreeze for less obvious reasons. | |
9e8aded4 MR |
1246 | * these will report EOPNOTSUPP. we simply ignore these when tallying |
1247 | * the number of frozen filesystems. | |
ce2eb6c4 PL |
1248 | * if a filesystem is mounted more than once (aka bind mount) a |
1249 | * consecutive attempt to freeze an already frozen filesystem will | |
1250 | * return EBUSY. | |
9e8aded4 MR |
1251 | * |
1252 | * any other error means a failure to freeze a filesystem we | |
1253 | * expect to be freezable, so return an error in those cases | |
1254 | * and return system to thawed state. | |
e3d4d252 MR |
1255 | */ |
1256 | ret = ioctl(fd, FIFREEZE); | |
9e8aded4 | 1257 | if (ret == -1) { |
ce2eb6c4 | 1258 | if (errno != EOPNOTSUPP && errno != EBUSY) { |
77dbc81b | 1259 | error_setg_errno(errp, errno, "failed to freeze %s", |
617fbbc1 | 1260 | mount->dirname); |
9e8aded4 MR |
1261 | close(fd); |
1262 | goto error; | |
1263 | } | |
1264 | } else { | |
1265 | i++; | |
e3d4d252 MR |
1266 | } |
1267 | close(fd); | |
e3d4d252 MR |
1268 | } |
1269 | ||
af02203f | 1270 | free_fs_mount_list(&mounts); |
e3d4d252 MR |
1271 | return i; |
1272 | ||
1273 | error: | |
af02203f | 1274 | free_fs_mount_list(&mounts); |
9e8aded4 | 1275 | qmp_guest_fsfreeze_thaw(NULL); |
e3d4d252 MR |
1276 | return 0; |
1277 | } | |
1278 | ||
1279 | /* | |
1280 | * Walk list of frozen file systems in the guest, and thaw them. | |
1281 | */ | |
77dbc81b | 1282 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
e3d4d252 MR |
1283 | { |
1284 | int ret; | |
af02203f PB |
1285 | FsMountList mounts; |
1286 | FsMount *mount; | |
9e8aded4 | 1287 | int fd, i = 0, logged; |
261551d1 | 1288 | Error *local_err = NULL; |
9e8aded4 MR |
1289 | |
1290 | QTAILQ_INIT(&mounts); | |
261551d1 | 1291 | build_fs_mount_list(&mounts, &local_err); |
84d18f06 | 1292 | if (local_err) { |
77dbc81b | 1293 | error_propagate(errp, local_err); |
9e8aded4 MR |
1294 | return 0; |
1295 | } | |
e3d4d252 | 1296 | |
9e8aded4 MR |
1297 | QTAILQ_FOREACH(mount, &mounts, next) { |
1298 | logged = false; | |
e3d4d252 MR |
1299 | fd = qemu_open(mount->dirname, O_RDONLY); |
1300 | if (fd == -1) { | |
e3d4d252 MR |
1301 | continue; |
1302 | } | |
9e8aded4 MR |
1303 | /* we have no way of knowing whether a filesystem was actually unfrozen |
1304 | * as a result of a successful call to FITHAW, only that if an error | |
1305 | * was returned the filesystem was *not* unfrozen by that particular | |
1306 | * call. | |
1307 | * | |
a31f0531 | 1308 | * since multiple preceding FIFREEZEs require multiple calls to FITHAW |
9e8aded4 MR |
1309 | * to unfreeze, continuing issuing FITHAW until an error is returned, |
1310 | * in which case either the filesystem is in an unfreezable state, or, | |
1311 | * more likely, it was thawed previously (and remains so afterward). | |
1312 | * | |
1313 | * also, since the most recent successful call is the one that did | |
1314 | * the actual unfreeze, we can use this to provide an accurate count | |
1315 | * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which | |
1316 | * may * be useful for determining whether a filesystem was unfrozen | |
1317 | * during the freeze/thaw phase by a process other than qemu-ga. | |
1318 | */ | |
1319 | do { | |
1320 | ret = ioctl(fd, FITHAW); | |
1321 | if (ret == 0 && !logged) { | |
1322 | i++; | |
1323 | logged = true; | |
1324 | } | |
1325 | } while (ret == 0); | |
e3d4d252 | 1326 | close(fd); |
e3d4d252 MR |
1327 | } |
1328 | ||
f22d85e9 | 1329 | ga_unset_frozen(ga_state); |
af02203f | 1330 | free_fs_mount_list(&mounts); |
ec0f694c | 1331 | |
77dbc81b | 1332 | execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); |
ec0f694c | 1333 | |
e3d4d252 MR |
1334 | return i; |
1335 | } | |
1336 | ||
e3d4d252 MR |
1337 | static void guest_fsfreeze_cleanup(void) |
1338 | { | |
e3d4d252 MR |
1339 | Error *err = NULL; |
1340 | ||
f22d85e9 | 1341 | if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { |
6f686749 MA |
1342 | qmp_guest_fsfreeze_thaw(&err); |
1343 | if (err) { | |
1344 | slog("failed to clean up frozen filesystems: %s", | |
1345 | error_get_pretty(err)); | |
1346 | error_free(err); | |
e3d4d252 MR |
1347 | } |
1348 | } | |
1349 | } | |
e72c3f2e | 1350 | #endif /* CONFIG_FSFREEZE */ |
e3d4d252 | 1351 | |
eab5fd59 PB |
1352 | #if defined(CONFIG_FSTRIM) |
1353 | /* | |
1354 | * Walk list of mounted file systems in the guest, and trim them. | |
1355 | */ | |
e82855d9 JO |
1356 | GuestFilesystemTrimResponse * |
1357 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 1358 | { |
e82855d9 JO |
1359 | GuestFilesystemTrimResponse *response; |
1360 | GuestFilesystemTrimResultList *list; | |
1361 | GuestFilesystemTrimResult *result; | |
eab5fd59 PB |
1362 | int ret = 0; |
1363 | FsMountList mounts; | |
1364 | struct FsMount *mount; | |
1365 | int fd; | |
261551d1 | 1366 | Error *local_err = NULL; |
73a652a1 | 1367 | struct fstrim_range r; |
eab5fd59 PB |
1368 | |
1369 | slog("guest-fstrim called"); | |
1370 | ||
1371 | QTAILQ_INIT(&mounts); | |
261551d1 | 1372 | build_fs_mount_list(&mounts, &local_err); |
84d18f06 | 1373 | if (local_err) { |
77dbc81b | 1374 | error_propagate(errp, local_err); |
e82855d9 | 1375 | return NULL; |
eab5fd59 PB |
1376 | } |
1377 | ||
e82855d9 JO |
1378 | response = g_malloc0(sizeof(*response)); |
1379 | ||
eab5fd59 | 1380 | QTAILQ_FOREACH(mount, &mounts, next) { |
e82855d9 JO |
1381 | result = g_malloc0(sizeof(*result)); |
1382 | result->path = g_strdup(mount->dirname); | |
1383 | ||
1384 | list = g_malloc0(sizeof(*list)); | |
1385 | list->value = result; | |
1386 | list->next = response->paths; | |
1387 | response->paths = list; | |
1388 | ||
eab5fd59 PB |
1389 | fd = qemu_open(mount->dirname, O_RDONLY); |
1390 | if (fd == -1) { | |
e82855d9 JO |
1391 | result->error = g_strdup_printf("failed to open: %s", |
1392 | strerror(errno)); | |
1393 | result->has_error = true; | |
1394 | continue; | |
eab5fd59 PB |
1395 | } |
1396 | ||
e35916ac MT |
1397 | /* We try to cull filesystems we know won't work in advance, but other |
1398 | * filesystems may not implement fstrim for less obvious reasons. | |
1399 | * These will report EOPNOTSUPP; while in some other cases ENOTTY | |
1400 | * will be reported (e.g. CD-ROMs). | |
e82855d9 | 1401 | * Any other error means an unexpected error. |
eab5fd59 | 1402 | */ |
73a652a1 JO |
1403 | r.start = 0; |
1404 | r.len = -1; | |
1405 | r.minlen = has_minimum ? minimum : 0; | |
eab5fd59 PB |
1406 | ret = ioctl(fd, FITRIM, &r); |
1407 | if (ret == -1) { | |
e82855d9 JO |
1408 | result->has_error = true; |
1409 | if (errno == ENOTTY || errno == EOPNOTSUPP) { | |
1410 | result->error = g_strdup("trim not supported"); | |
1411 | } else { | |
1412 | result->error = g_strdup_printf("failed to trim: %s", | |
1413 | strerror(errno)); | |
eab5fd59 | 1414 | } |
e82855d9 JO |
1415 | close(fd); |
1416 | continue; | |
eab5fd59 | 1417 | } |
e82855d9 JO |
1418 | |
1419 | result->has_minimum = true; | |
1420 | result->minimum = r.minlen; | |
1421 | result->has_trimmed = true; | |
1422 | result->trimmed = r.len; | |
eab5fd59 PB |
1423 | close(fd); |
1424 | } | |
1425 | ||
eab5fd59 | 1426 | free_fs_mount_list(&mounts); |
e82855d9 | 1427 | return response; |
eab5fd59 PB |
1428 | } |
1429 | #endif /* CONFIG_FSTRIM */ | |
1430 | ||
1431 | ||
11d0f125 LC |
1432 | #define LINUX_SYS_STATE_FILE "/sys/power/state" |
1433 | #define SUSPEND_SUPPORTED 0 | |
1434 | #define SUSPEND_NOT_SUPPORTED 1 | |
1435 | ||
11d0f125 | 1436 | static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg, |
77dbc81b | 1437 | const char *sysfile_str, Error **errp) |
11d0f125 | 1438 | { |
6b26e837 | 1439 | Error *local_err = NULL; |
11d0f125 | 1440 | char *pmutils_path; |
6b26e837 | 1441 | pid_t pid; |
dc8764f0 | 1442 | int status; |
11d0f125 LC |
1443 | |
1444 | pmutils_path = g_find_program_in_path(pmutils_bin); | |
1445 | ||
1446 | pid = fork(); | |
1447 | if (!pid) { | |
dc8764f0 LC |
1448 | char buf[32]; /* hopefully big enough */ |
1449 | ssize_t ret; | |
1450 | int fd; | |
11d0f125 LC |
1451 | |
1452 | setsid(); | |
11d0f125 LC |
1453 | reopen_fd_to_null(0); |
1454 | reopen_fd_to_null(1); | |
1455 | reopen_fd_to_null(2); | |
1456 | ||
dc8764f0 LC |
1457 | if (pmutils_path) { |
1458 | execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ); | |
1459 | } | |
11d0f125 | 1460 | |
dc8764f0 LC |
1461 | /* |
1462 | * If we get here either pm-utils is not installed or execle() has | |
1463 | * failed. Let's try the manual method if the caller wants it. | |
1464 | */ | |
11d0f125 | 1465 | |
dc8764f0 LC |
1466 | if (!sysfile_str) { |
1467 | _exit(SUSPEND_NOT_SUPPORTED); | |
1468 | } | |
11d0f125 | 1469 | |
dc8764f0 LC |
1470 | fd = open(LINUX_SYS_STATE_FILE, O_RDONLY); |
1471 | if (fd < 0) { | |
11d0f125 LC |
1472 | _exit(SUSPEND_NOT_SUPPORTED); |
1473 | } | |
1474 | ||
dc8764f0 LC |
1475 | ret = read(fd, buf, sizeof(buf)-1); |
1476 | if (ret <= 0) { | |
1477 | _exit(SUSPEND_NOT_SUPPORTED); | |
11d0f125 | 1478 | } |
dc8764f0 | 1479 | buf[ret] = '\0'; |
11d0f125 | 1480 | |
dc8764f0 LC |
1481 | if (strstr(buf, sysfile_str)) { |
1482 | _exit(SUSPEND_SUPPORTED); | |
11d0f125 LC |
1483 | } |
1484 | ||
dc8764f0 | 1485 | _exit(SUSPEND_NOT_SUPPORTED); |
6b26e837 | 1486 | } else if (pid < 0) { |
77dbc81b | 1487 | error_setg_errno(errp, errno, "failed to create child process"); |
6b26e837 | 1488 | goto out; |
11d0f125 LC |
1489 | } |
1490 | ||
6b26e837 | 1491 | ga_wait_child(pid, &status, &local_err); |
84d18f06 | 1492 | if (local_err) { |
77dbc81b | 1493 | error_propagate(errp, local_err); |
6b26e837 LC |
1494 | goto out; |
1495 | } | |
11d0f125 | 1496 | |
6b26e837 | 1497 | if (!WIFEXITED(status)) { |
77dbc81b | 1498 | error_setg(errp, "child process has terminated abnormally"); |
6b26e837 | 1499 | goto out; |
dc8764f0 LC |
1500 | } |
1501 | ||
6b26e837 LC |
1502 | switch (WEXITSTATUS(status)) { |
1503 | case SUSPEND_SUPPORTED: | |
1504 | goto out; | |
1505 | case SUSPEND_NOT_SUPPORTED: | |
77dbc81b | 1506 | error_setg(errp, |
6b26e837 LC |
1507 | "the requested suspend mode is not supported by the guest"); |
1508 | goto out; | |
1509 | default: | |
77dbc81b | 1510 | error_setg(errp, |
6b26e837 LC |
1511 | "the helper program '%s' returned an unexpected exit status" |
1512 | " code (%d)", pmutils_path, WEXITSTATUS(status)); | |
1513 | goto out; | |
11d0f125 LC |
1514 | } |
1515 | ||
6b26e837 LC |
1516 | out: |
1517 | g_free(pmutils_path); | |
11d0f125 LC |
1518 | } |
1519 | ||
1520 | static void guest_suspend(const char *pmutils_bin, const char *sysfile_str, | |
77dbc81b | 1521 | Error **errp) |
11d0f125 | 1522 | { |
7b376087 | 1523 | Error *local_err = NULL; |
11d0f125 | 1524 | char *pmutils_path; |
7b376087 | 1525 | pid_t pid; |
dc8764f0 | 1526 | int status; |
11d0f125 LC |
1527 | |
1528 | pmutils_path = g_find_program_in_path(pmutils_bin); | |
1529 | ||
1530 | pid = fork(); | |
1531 | if (pid == 0) { | |
1532 | /* child */ | |
1533 | int fd; | |
1534 | ||
1535 | setsid(); | |
1536 | reopen_fd_to_null(0); | |
1537 | reopen_fd_to_null(1); | |
1538 | reopen_fd_to_null(2); | |
1539 | ||
1540 | if (pmutils_path) { | |
1541 | execle(pmutils_path, pmutils_bin, NULL, environ); | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * If we get here either pm-utils is not installed or execle() has | |
1546 | * failed. Let's try the manual method if the caller wants it. | |
1547 | */ | |
1548 | ||
1549 | if (!sysfile_str) { | |
1550 | _exit(EXIT_FAILURE); | |
1551 | } | |
1552 | ||
1553 | fd = open(LINUX_SYS_STATE_FILE, O_WRONLY); | |
1554 | if (fd < 0) { | |
1555 | _exit(EXIT_FAILURE); | |
1556 | } | |
1557 | ||
1558 | if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) { | |
1559 | _exit(EXIT_FAILURE); | |
1560 | } | |
1561 | ||
1562 | _exit(EXIT_SUCCESS); | |
7b376087 | 1563 | } else if (pid < 0) { |
77dbc81b | 1564 | error_setg_errno(errp, errno, "failed to create child process"); |
7b376087 | 1565 | goto out; |
11d0f125 LC |
1566 | } |
1567 | ||
7b376087 | 1568 | ga_wait_child(pid, &status, &local_err); |
84d18f06 | 1569 | if (local_err) { |
77dbc81b | 1570 | error_propagate(errp, local_err); |
7b376087 LC |
1571 | goto out; |
1572 | } | |
11d0f125 | 1573 | |
7b376087 | 1574 | if (!WIFEXITED(status)) { |
77dbc81b | 1575 | error_setg(errp, "child process has terminated abnormally"); |
7b376087 | 1576 | goto out; |
dc8764f0 LC |
1577 | } |
1578 | ||
7b376087 | 1579 | if (WEXITSTATUS(status)) { |
77dbc81b | 1580 | error_setg(errp, "child process has failed to suspend"); |
7b376087 | 1581 | goto out; |
11d0f125 | 1582 | } |
dc8764f0 | 1583 | |
7b376087 LC |
1584 | out: |
1585 | g_free(pmutils_path); | |
11d0f125 LC |
1586 | } |
1587 | ||
77dbc81b | 1588 | void qmp_guest_suspend_disk(Error **errp) |
11d0f125 | 1589 | { |
0f230bf7 MA |
1590 | Error *local_err = NULL; |
1591 | ||
1592 | bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err); | |
1593 | if (local_err) { | |
1594 | error_propagate(errp, local_err); | |
11d0f125 LC |
1595 | return; |
1596 | } | |
1597 | ||
77dbc81b | 1598 | guest_suspend("pm-hibernate", "disk", errp); |
11d0f125 LC |
1599 | } |
1600 | ||
77dbc81b | 1601 | void qmp_guest_suspend_ram(Error **errp) |
fbf42210 | 1602 | { |
0f230bf7 MA |
1603 | Error *local_err = NULL; |
1604 | ||
1605 | bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err); | |
1606 | if (local_err) { | |
1607 | error_propagate(errp, local_err); | |
fbf42210 LC |
1608 | return; |
1609 | } | |
1610 | ||
77dbc81b | 1611 | guest_suspend("pm-suspend", "mem", errp); |
fbf42210 LC |
1612 | } |
1613 | ||
77dbc81b | 1614 | void qmp_guest_suspend_hybrid(Error **errp) |
95f4f404 | 1615 | { |
0f230bf7 MA |
1616 | Error *local_err = NULL; |
1617 | ||
1618 | bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, | |
1619 | &local_err); | |
1620 | if (local_err) { | |
1621 | error_propagate(errp, local_err); | |
95f4f404 LC |
1622 | return; |
1623 | } | |
1624 | ||
77dbc81b | 1625 | guest_suspend("pm-suspend-hybrid", NULL, errp); |
95f4f404 LC |
1626 | } |
1627 | ||
3424fc9f MP |
1628 | static GuestNetworkInterfaceList * |
1629 | guest_find_interface(GuestNetworkInterfaceList *head, | |
1630 | const char *name) | |
1631 | { | |
1632 | for (; head; head = head->next) { | |
1633 | if (strcmp(head->value->name, name) == 0) { | |
1634 | break; | |
1635 | } | |
1636 | } | |
1637 | ||
1638 | return head; | |
1639 | } | |
1640 | ||
1641 | /* | |
1642 | * Build information about guest interfaces | |
1643 | */ | |
1644 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) | |
1645 | { | |
1646 | GuestNetworkInterfaceList *head = NULL, *cur_item = NULL; | |
1647 | struct ifaddrs *ifap, *ifa; | |
3424fc9f MP |
1648 | |
1649 | if (getifaddrs(&ifap) < 0) { | |
878a0ae0 | 1650 | error_setg_errno(errp, errno, "getifaddrs failed"); |
3424fc9f MP |
1651 | goto error; |
1652 | } | |
1653 | ||
1654 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | |
1655 | GuestNetworkInterfaceList *info; | |
1656 | GuestIpAddressList **address_list = NULL, *address_item = NULL; | |
1657 | char addr4[INET_ADDRSTRLEN]; | |
1658 | char addr6[INET6_ADDRSTRLEN]; | |
1659 | int sock; | |
1660 | struct ifreq ifr; | |
1661 | unsigned char *mac_addr; | |
1662 | void *p; | |
1663 | ||
1664 | g_debug("Processing %s interface", ifa->ifa_name); | |
1665 | ||
1666 | info = guest_find_interface(head, ifa->ifa_name); | |
1667 | ||
1668 | if (!info) { | |
1669 | info = g_malloc0(sizeof(*info)); | |
1670 | info->value = g_malloc0(sizeof(*info->value)); | |
1671 | info->value->name = g_strdup(ifa->ifa_name); | |
1672 | ||
1673 | if (!cur_item) { | |
1674 | head = cur_item = info; | |
1675 | } else { | |
1676 | cur_item->next = info; | |
1677 | cur_item = info; | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | if (!info->value->has_hardware_address && | |
1682 | ifa->ifa_flags & SIOCGIFHWADDR) { | |
1683 | /* we haven't obtained HW address yet */ | |
1684 | sock = socket(PF_INET, SOCK_STREAM, 0); | |
1685 | if (sock == -1) { | |
878a0ae0 | 1686 | error_setg_errno(errp, errno, "failed to create socket"); |
3424fc9f MP |
1687 | goto error; |
1688 | } | |
1689 | ||
1690 | memset(&ifr, 0, sizeof(ifr)); | |
1ab516ed | 1691 | pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name); |
3424fc9f | 1692 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) { |
878a0ae0 LC |
1693 | error_setg_errno(errp, errno, |
1694 | "failed to get MAC address of %s", | |
1695 | ifa->ifa_name); | |
10a2158f | 1696 | close(sock); |
3424fc9f MP |
1697 | goto error; |
1698 | } | |
1699 | ||
10a2158f | 1700 | close(sock); |
3424fc9f MP |
1701 | mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data; |
1702 | ||
e4ada482 SW |
1703 | info->value->hardware_address = |
1704 | g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
1705 | (int) mac_addr[0], (int) mac_addr[1], | |
1706 | (int) mac_addr[2], (int) mac_addr[3], | |
1707 | (int) mac_addr[4], (int) mac_addr[5]); | |
3424fc9f MP |
1708 | |
1709 | info->value->has_hardware_address = true; | |
3424fc9f MP |
1710 | } |
1711 | ||
1712 | if (ifa->ifa_addr && | |
1713 | ifa->ifa_addr->sa_family == AF_INET) { | |
1714 | /* interface with IPv4 address */ | |
3424fc9f MP |
1715 | p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; |
1716 | if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) { | |
878a0ae0 | 1717 | error_setg_errno(errp, errno, "inet_ntop failed"); |
3424fc9f MP |
1718 | goto error; |
1719 | } | |
1720 | ||
10a2158f MA |
1721 | address_item = g_malloc0(sizeof(*address_item)); |
1722 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
3424fc9f MP |
1723 | address_item->value->ip_address = g_strdup(addr4); |
1724 | address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4; | |
1725 | ||
1726 | if (ifa->ifa_netmask) { | |
1727 | /* Count the number of set bits in netmask. | |
1728 | * This is safe as '1' and '0' cannot be shuffled in netmask. */ | |
1729 | p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr; | |
1730 | address_item->value->prefix = ctpop32(((uint32_t *) p)[0]); | |
1731 | } | |
1732 | } else if (ifa->ifa_addr && | |
1733 | ifa->ifa_addr->sa_family == AF_INET6) { | |
1734 | /* interface with IPv6 address */ | |
3424fc9f MP |
1735 | p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; |
1736 | if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) { | |
878a0ae0 | 1737 | error_setg_errno(errp, errno, "inet_ntop failed"); |
3424fc9f MP |
1738 | goto error; |
1739 | } | |
1740 | ||
10a2158f MA |
1741 | address_item = g_malloc0(sizeof(*address_item)); |
1742 | address_item->value = g_malloc0(sizeof(*address_item->value)); | |
3424fc9f MP |
1743 | address_item->value->ip_address = g_strdup(addr6); |
1744 | address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6; | |
1745 | ||
1746 | if (ifa->ifa_netmask) { | |
1747 | /* Count the number of set bits in netmask. | |
1748 | * This is safe as '1' and '0' cannot be shuffled in netmask. */ | |
1749 | p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr; | |
1750 | address_item->value->prefix = | |
1751 | ctpop32(((uint32_t *) p)[0]) + | |
1752 | ctpop32(((uint32_t *) p)[1]) + | |
1753 | ctpop32(((uint32_t *) p)[2]) + | |
1754 | ctpop32(((uint32_t *) p)[3]); | |
1755 | } | |
1756 | } | |
1757 | ||
1758 | if (!address_item) { | |
1759 | continue; | |
1760 | } | |
1761 | ||
1762 | address_list = &info->value->ip_addresses; | |
1763 | ||
1764 | while (*address_list && (*address_list)->next) { | |
1765 | address_list = &(*address_list)->next; | |
1766 | } | |
1767 | ||
1768 | if (!*address_list) { | |
1769 | *address_list = address_item; | |
1770 | } else { | |
1771 | (*address_list)->next = address_item; | |
1772 | } | |
1773 | ||
1774 | info->value->has_ip_addresses = true; | |
1775 | ||
1776 | ||
1777 | } | |
1778 | ||
1779 | freeifaddrs(ifap); | |
1780 | return head; | |
1781 | ||
1782 | error: | |
1783 | freeifaddrs(ifap); | |
1784 | qapi_free_GuestNetworkInterfaceList(head); | |
1785 | return NULL; | |
1786 | } | |
1787 | ||
77dbc81b | 1788 | #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp)) |
d2baff62 | 1789 | |
77dbc81b | 1790 | static long sysconf_exact(int name, const char *name_str, Error **errp) |
d2baff62 LE |
1791 | { |
1792 | long ret; | |
1793 | ||
1794 | errno = 0; | |
1795 | ret = sysconf(name); | |
1796 | if (ret == -1) { | |
1797 | if (errno == 0) { | |
77dbc81b | 1798 | error_setg(errp, "sysconf(%s): value indefinite", name_str); |
d2baff62 | 1799 | } else { |
77dbc81b | 1800 | error_setg_errno(errp, errno, "sysconf(%s)", name_str); |
d2baff62 LE |
1801 | } |
1802 | } | |
1803 | return ret; | |
1804 | } | |
1805 | ||
1806 | /* Transfer online/offline status between @vcpu and the guest system. | |
1807 | * | |
1808 | * On input either @errp or *@errp must be NULL. | |
1809 | * | |
1810 | * In system-to-@vcpu direction, the following @vcpu fields are accessed: | |
1811 | * - R: vcpu->logical_id | |
1812 | * - W: vcpu->online | |
1813 | * - W: vcpu->can_offline | |
1814 | * | |
1815 | * In @vcpu-to-system direction, the following @vcpu fields are accessed: | |
1816 | * - R: vcpu->logical_id | |
1817 | * - R: vcpu->online | |
1818 | * | |
1819 | * Written members remain unmodified on error. | |
1820 | */ | |
1821 | static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu, | |
1822 | Error **errp) | |
1823 | { | |
1824 | char *dirpath; | |
1825 | int dirfd; | |
1826 | ||
1827 | dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/", | |
1828 | vcpu->logical_id); | |
1829 | dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); | |
1830 | if (dirfd == -1) { | |
1831 | error_setg_errno(errp, errno, "open(\"%s\")", dirpath); | |
1832 | } else { | |
1833 | static const char fn[] = "online"; | |
1834 | int fd; | |
1835 | int res; | |
1836 | ||
1837 | fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR); | |
1838 | if (fd == -1) { | |
1839 | if (errno != ENOENT) { | |
1840 | error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn); | |
1841 | } else if (sys2vcpu) { | |
1842 | vcpu->online = true; | |
1843 | vcpu->can_offline = false; | |
1844 | } else if (!vcpu->online) { | |
1845 | error_setg(errp, "logical processor #%" PRId64 " can't be " | |
1846 | "offlined", vcpu->logical_id); | |
1847 | } /* otherwise pretend successful re-onlining */ | |
1848 | } else { | |
1849 | unsigned char status; | |
1850 | ||
1851 | res = pread(fd, &status, 1, 0); | |
1852 | if (res == -1) { | |
1853 | error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn); | |
1854 | } else if (res == 0) { | |
1855 | error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath, | |
1856 | fn); | |
1857 | } else if (sys2vcpu) { | |
1858 | vcpu->online = (status != '0'); | |
1859 | vcpu->can_offline = true; | |
1860 | } else if (vcpu->online != (status != '0')) { | |
1861 | status = '0' + vcpu->online; | |
1862 | if (pwrite(fd, &status, 1, 0) == -1) { | |
1863 | error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath, | |
1864 | fn); | |
1865 | } | |
1866 | } /* otherwise pretend successful re-(on|off)-lining */ | |
1867 | ||
1868 | res = close(fd); | |
1869 | g_assert(res == 0); | |
1870 | } | |
1871 | ||
1872 | res = close(dirfd); | |
1873 | g_assert(res == 0); | |
1874 | } | |
1875 | ||
1876 | g_free(dirpath); | |
1877 | } | |
1878 | ||
1879 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) | |
1880 | { | |
1881 | int64_t current; | |
1882 | GuestLogicalProcessorList *head, **link; | |
1883 | long sc_max; | |
1884 | Error *local_err = NULL; | |
1885 | ||
1886 | current = 0; | |
1887 | head = NULL; | |
1888 | link = &head; | |
1889 | sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err); | |
1890 | ||
1891 | while (local_err == NULL && current < sc_max) { | |
1892 | GuestLogicalProcessor *vcpu; | |
1893 | GuestLogicalProcessorList *entry; | |
1894 | ||
1895 | vcpu = g_malloc0(sizeof *vcpu); | |
1896 | vcpu->logical_id = current++; | |
1897 | vcpu->has_can_offline = true; /* lolspeak ftw */ | |
1898 | transfer_vcpu(vcpu, true, &local_err); | |
1899 | ||
1900 | entry = g_malloc0(sizeof *entry); | |
1901 | entry->value = vcpu; | |
1902 | ||
1903 | *link = entry; | |
1904 | link = &entry->next; | |
1905 | } | |
1906 | ||
1907 | if (local_err == NULL) { | |
1908 | /* there's no guest with zero VCPUs */ | |
1909 | g_assert(head != NULL); | |
1910 | return head; | |
1911 | } | |
1912 | ||
1913 | qapi_free_GuestLogicalProcessorList(head); | |
1914 | error_propagate(errp, local_err); | |
1915 | return NULL; | |
1916 | } | |
1917 | ||
cbb65fc2 LE |
1918 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) |
1919 | { | |
1920 | int64_t processed; | |
1921 | Error *local_err = NULL; | |
1922 | ||
1923 | processed = 0; | |
1924 | while (vcpus != NULL) { | |
1925 | transfer_vcpu(vcpus->value, false, &local_err); | |
1926 | if (local_err != NULL) { | |
1927 | break; | |
1928 | } | |
1929 | ++processed; | |
1930 | vcpus = vcpus->next; | |
1931 | } | |
1932 | ||
1933 | if (local_err != NULL) { | |
1934 | if (processed == 0) { | |
1935 | error_propagate(errp, local_err); | |
1936 | } else { | |
1937 | error_free(local_err); | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | return processed; | |
1942 | } | |
1943 | ||
215a2771 DB |
1944 | void qmp_guest_set_user_password(const char *username, |
1945 | const char *password, | |
1946 | bool crypted, | |
1947 | Error **errp) | |
1948 | { | |
1949 | Error *local_err = NULL; | |
1950 | char *passwd_path = NULL; | |
1951 | pid_t pid; | |
1952 | int status; | |
1953 | int datafd[2] = { -1, -1 }; | |
1954 | char *rawpasswddata = NULL; | |
1955 | size_t rawpasswdlen; | |
1956 | char *chpasswddata = NULL; | |
1957 | size_t chpasswdlen; | |
1958 | ||
920639ca DB |
1959 | rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp); |
1960 | if (!rawpasswddata) { | |
1961 | return; | |
1962 | } | |
215a2771 DB |
1963 | rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); |
1964 | rawpasswddata[rawpasswdlen] = '\0'; | |
1965 | ||
1966 | if (strchr(rawpasswddata, '\n')) { | |
1967 | error_setg(errp, "forbidden characters in raw password"); | |
1968 | goto out; | |
1969 | } | |
1970 | ||
1971 | if (strchr(username, '\n') || | |
1972 | strchr(username, ':')) { | |
1973 | error_setg(errp, "forbidden characters in username"); | |
1974 | goto out; | |
1975 | } | |
1976 | ||
1977 | chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata); | |
1978 | chpasswdlen = strlen(chpasswddata); | |
1979 | ||
1980 | passwd_path = g_find_program_in_path("chpasswd"); | |
1981 | ||
1982 | if (!passwd_path) { | |
1983 | error_setg(errp, "cannot find 'passwd' program in PATH"); | |
1984 | goto out; | |
1985 | } | |
1986 | ||
1987 | if (pipe(datafd) < 0) { | |
1988 | error_setg(errp, "cannot create pipe FDs"); | |
1989 | goto out; | |
1990 | } | |
1991 | ||
1992 | pid = fork(); | |
1993 | if (pid == 0) { | |
1994 | close(datafd[1]); | |
1995 | /* child */ | |
1996 | setsid(); | |
1997 | dup2(datafd[0], 0); | |
1998 | reopen_fd_to_null(1); | |
1999 | reopen_fd_to_null(2); | |
2000 | ||
2001 | if (crypted) { | |
2002 | execle(passwd_path, "chpasswd", "-e", NULL, environ); | |
2003 | } else { | |
2004 | execle(passwd_path, "chpasswd", NULL, environ); | |
2005 | } | |
2006 | _exit(EXIT_FAILURE); | |
2007 | } else if (pid < 0) { | |
2008 | error_setg_errno(errp, errno, "failed to create child process"); | |
2009 | goto out; | |
2010 | } | |
2011 | close(datafd[0]); | |
2012 | datafd[0] = -1; | |
2013 | ||
2014 | if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) { | |
2015 | error_setg_errno(errp, errno, "cannot write new account password"); | |
2016 | goto out; | |
2017 | } | |
2018 | close(datafd[1]); | |
2019 | datafd[1] = -1; | |
2020 | ||
2021 | ga_wait_child(pid, &status, &local_err); | |
2022 | if (local_err) { | |
2023 | error_propagate(errp, local_err); | |
2024 | goto out; | |
2025 | } | |
2026 | ||
2027 | if (!WIFEXITED(status)) { | |
2028 | error_setg(errp, "child process has terminated abnormally"); | |
2029 | goto out; | |
2030 | } | |
2031 | ||
2032 | if (WEXITSTATUS(status)) { | |
2033 | error_setg(errp, "child process has failed to set user password"); | |
2034 | goto out; | |
2035 | } | |
2036 | ||
2037 | out: | |
2038 | g_free(chpasswddata); | |
2039 | g_free(rawpasswddata); | |
2040 | g_free(passwd_path); | |
2041 | if (datafd[0] != -1) { | |
2042 | close(datafd[0]); | |
2043 | } | |
2044 | if (datafd[1] != -1) { | |
2045 | close(datafd[1]); | |
2046 | } | |
2047 | } | |
2048 | ||
bd240fca HZ |
2049 | static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf, |
2050 | int size, Error **errp) | |
2051 | { | |
2052 | int fd; | |
2053 | int res; | |
2054 | ||
2055 | errno = 0; | |
2056 | fd = openat(dirfd, pathname, O_RDONLY); | |
2057 | if (fd == -1) { | |
2058 | error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); | |
2059 | return; | |
2060 | } | |
2061 | ||
2062 | res = pread(fd, buf, size, 0); | |
2063 | if (res == -1) { | |
2064 | error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname); | |
2065 | } else if (res == 0) { | |
2066 | error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname); | |
2067 | } | |
2068 | close(fd); | |
2069 | } | |
2070 | ||
2071 | static void ga_write_sysfs_file(int dirfd, const char *pathname, | |
2072 | const char *buf, int size, Error **errp) | |
2073 | { | |
2074 | int fd; | |
2075 | ||
2076 | errno = 0; | |
2077 | fd = openat(dirfd, pathname, O_WRONLY); | |
2078 | if (fd == -1) { | |
2079 | error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); | |
2080 | return; | |
2081 | } | |
2082 | ||
2083 | if (pwrite(fd, buf, size, 0) == -1) { | |
2084 | error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname); | |
2085 | } | |
2086 | ||
2087 | close(fd); | |
2088 | } | |
2089 | ||
2090 | /* Transfer online/offline status between @mem_blk and the guest system. | |
2091 | * | |
2092 | * On input either @errp or *@errp must be NULL. | |
2093 | * | |
2094 | * In system-to-@mem_blk direction, the following @mem_blk fields are accessed: | |
2095 | * - R: mem_blk->phys_index | |
2096 | * - W: mem_blk->online | |
2097 | * - W: mem_blk->can_offline | |
2098 | * | |
2099 | * In @mem_blk-to-system direction, the following @mem_blk fields are accessed: | |
2100 | * - R: mem_blk->phys_index | |
2101 | * - R: mem_blk->online | |
2102 | *- R: mem_blk->can_offline | |
2103 | * Written members remain unmodified on error. | |
2104 | */ | |
2105 | static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk, | |
2106 | GuestMemoryBlockResponse *result, | |
2107 | Error **errp) | |
2108 | { | |
2109 | char *dirpath; | |
2110 | int dirfd; | |
2111 | char *status; | |
2112 | Error *local_err = NULL; | |
2113 | ||
2114 | if (!sys2memblk) { | |
2115 | DIR *dp; | |
2116 | ||
2117 | if (!result) { | |
2118 | error_setg(errp, "Internal error, 'result' should not be NULL"); | |
2119 | return; | |
2120 | } | |
2121 | errno = 0; | |
2122 | dp = opendir("/sys/devices/system/memory/"); | |
2123 | /* if there is no 'memory' directory in sysfs, | |
2124 | * we think this VM does not support online/offline memory block, | |
2125 | * any other solution? | |
2126 | */ | |
2127 | if (!dp && errno == ENOENT) { | |
2128 | result->response = | |
2129 | GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED; | |
2130 | goto out1; | |
2131 | } | |
2132 | closedir(dp); | |
2133 | } | |
2134 | ||
2135 | dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/", | |
2136 | mem_blk->phys_index); | |
2137 | dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); | |
2138 | if (dirfd == -1) { | |
2139 | if (sys2memblk) { | |
2140 | error_setg_errno(errp, errno, "open(\"%s\")", dirpath); | |
2141 | } else { | |
2142 | if (errno == ENOENT) { | |
2143 | result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND; | |
2144 | } else { | |
2145 | result->response = | |
2146 | GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; | |
2147 | } | |
2148 | } | |
2149 | g_free(dirpath); | |
2150 | goto out1; | |
2151 | } | |
2152 | g_free(dirpath); | |
2153 | ||
2154 | status = g_malloc0(10); | |
2155 | ga_read_sysfs_file(dirfd, "state", status, 10, &local_err); | |
2156 | if (local_err) { | |
2157 | /* treat with sysfs file that not exist in old kernel */ | |
2158 | if (errno == ENOENT) { | |
2159 | error_free(local_err); | |
2160 | if (sys2memblk) { | |
2161 | mem_blk->online = true; | |
2162 | mem_blk->can_offline = false; | |
2163 | } else if (!mem_blk->online) { | |
2164 | result->response = | |
2165 | GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED; | |
2166 | } | |
2167 | } else { | |
2168 | if (sys2memblk) { | |
2169 | error_propagate(errp, local_err); | |
2170 | } else { | |
2171 | result->response = | |
2172 | GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; | |
2173 | } | |
2174 | } | |
2175 | goto out2; | |
2176 | } | |
2177 | ||
2178 | if (sys2memblk) { | |
2179 | char removable = '0'; | |
2180 | ||
2181 | mem_blk->online = (strncmp(status, "online", 6) == 0); | |
2182 | ||
2183 | ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err); | |
2184 | if (local_err) { | |
67cc32eb | 2185 | /* if no 'removable' file, it doesn't support offline mem blk */ |
bd240fca HZ |
2186 | if (errno == ENOENT) { |
2187 | error_free(local_err); | |
2188 | mem_blk->can_offline = false; | |
2189 | } else { | |
2190 | error_propagate(errp, local_err); | |
2191 | } | |
2192 | } else { | |
2193 | mem_blk->can_offline = (removable != '0'); | |
2194 | } | |
2195 | } else { | |
2196 | if (mem_blk->online != (strncmp(status, "online", 6) == 0)) { | |
2197 | char *new_state = mem_blk->online ? g_strdup("online") : | |
2198 | g_strdup("offline"); | |
2199 | ||
2200 | ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state), | |
2201 | &local_err); | |
2202 | g_free(new_state); | |
2203 | if (local_err) { | |
2204 | error_free(local_err); | |
2205 | result->response = | |
2206 | GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; | |
2207 | goto out2; | |
2208 | } | |
2209 | ||
2210 | result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS; | |
2211 | result->has_error_code = false; | |
2212 | } /* otherwise pretend successful re-(on|off)-lining */ | |
2213 | } | |
2214 | g_free(status); | |
2215 | close(dirfd); | |
2216 | return; | |
2217 | ||
2218 | out2: | |
2219 | g_free(status); | |
2220 | close(dirfd); | |
2221 | out1: | |
2222 | if (!sys2memblk) { | |
2223 | result->has_error_code = true; | |
2224 | result->error_code = errno; | |
2225 | } | |
2226 | } | |
2227 | ||
a065aaa9 HZ |
2228 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
2229 | { | |
bd240fca HZ |
2230 | GuestMemoryBlockList *head, **link; |
2231 | Error *local_err = NULL; | |
2232 | struct dirent *de; | |
2233 | DIR *dp; | |
2234 | ||
2235 | head = NULL; | |
2236 | link = &head; | |
2237 | ||
2238 | dp = opendir("/sys/devices/system/memory/"); | |
2239 | if (!dp) { | |
f693fe6e MR |
2240 | /* it's ok if this happens to be a system that doesn't expose |
2241 | * memory blocks via sysfs, but otherwise we should report | |
2242 | * an error | |
2243 | */ | |
2244 | if (errno != ENOENT) { | |
2245 | error_setg_errno(errp, errno, "Can't open directory" | |
9af9e0fe | 2246 | "\"/sys/devices/system/memory/\""); |
f693fe6e | 2247 | } |
bd240fca HZ |
2248 | return NULL; |
2249 | } | |
2250 | ||
2251 | /* Note: the phys_index of memory block may be discontinuous, | |
2252 | * this is because a memblk is the unit of the Sparse Memory design, which | |
2253 | * allows discontinuous memory ranges (ex. NUMA), so here we should | |
2254 | * traverse the memory block directory. | |
2255 | */ | |
2256 | while ((de = readdir(dp)) != NULL) { | |
2257 | GuestMemoryBlock *mem_blk; | |
2258 | GuestMemoryBlockList *entry; | |
2259 | ||
2260 | if ((strncmp(de->d_name, "memory", 6) != 0) || | |
2261 | !(de->d_type & DT_DIR)) { | |
2262 | continue; | |
2263 | } | |
2264 | ||
2265 | mem_blk = g_malloc0(sizeof *mem_blk); | |
2266 | /* The d_name is "memoryXXX", phys_index is block id, same as XXX */ | |
2267 | mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10); | |
2268 | mem_blk->has_can_offline = true; /* lolspeak ftw */ | |
2269 | transfer_memory_block(mem_blk, true, NULL, &local_err); | |
2270 | ||
2271 | entry = g_malloc0(sizeof *entry); | |
2272 | entry->value = mem_blk; | |
2273 | ||
2274 | *link = entry; | |
2275 | link = &entry->next; | |
2276 | } | |
2277 | ||
2278 | closedir(dp); | |
2279 | if (local_err == NULL) { | |
2280 | /* there's no guest with zero memory blocks */ | |
2281 | if (head == NULL) { | |
2282 | error_setg(errp, "guest reported zero memory blocks!"); | |
2283 | } | |
2284 | return head; | |
2285 | } | |
2286 | ||
2287 | qapi_free_GuestMemoryBlockList(head); | |
2288 | error_propagate(errp, local_err); | |
a065aaa9 HZ |
2289 | return NULL; |
2290 | } | |
2291 | ||
2292 | GuestMemoryBlockResponseList * | |
2293 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
2294 | { | |
32ca7927 HZ |
2295 | GuestMemoryBlockResponseList *head, **link; |
2296 | Error *local_err = NULL; | |
2297 | ||
2298 | head = NULL; | |
2299 | link = &head; | |
2300 | ||
2301 | while (mem_blks != NULL) { | |
2302 | GuestMemoryBlockResponse *result; | |
2303 | GuestMemoryBlockResponseList *entry; | |
2304 | GuestMemoryBlock *current_mem_blk = mem_blks->value; | |
2305 | ||
2306 | result = g_malloc0(sizeof(*result)); | |
2307 | result->phys_index = current_mem_blk->phys_index; | |
2308 | transfer_memory_block(current_mem_blk, false, result, &local_err); | |
2309 | if (local_err) { /* should never happen */ | |
2310 | goto err; | |
2311 | } | |
2312 | entry = g_malloc0(sizeof *entry); | |
2313 | entry->value = result; | |
2314 | ||
2315 | *link = entry; | |
2316 | link = &entry->next; | |
2317 | mem_blks = mem_blks->next; | |
2318 | } | |
2319 | ||
2320 | return head; | |
2321 | err: | |
2322 | qapi_free_GuestMemoryBlockResponseList(head); | |
2323 | error_propagate(errp, local_err); | |
a065aaa9 HZ |
2324 | return NULL; |
2325 | } | |
2326 | ||
2327 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
2328 | { | |
ef82b60b HZ |
2329 | Error *local_err = NULL; |
2330 | char *dirpath; | |
2331 | int dirfd; | |
2332 | char *buf; | |
2333 | GuestMemoryBlockInfo *info; | |
2334 | ||
2335 | dirpath = g_strdup_printf("/sys/devices/system/memory/"); | |
2336 | dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); | |
2337 | if (dirfd == -1) { | |
2338 | error_setg_errno(errp, errno, "open(\"%s\")", dirpath); | |
2339 | g_free(dirpath); | |
2340 | return NULL; | |
2341 | } | |
2342 | g_free(dirpath); | |
2343 | ||
2344 | buf = g_malloc0(20); | |
2345 | ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err); | |
8ce1ee46 | 2346 | close(dirfd); |
ef82b60b HZ |
2347 | if (local_err) { |
2348 | g_free(buf); | |
2349 | error_propagate(errp, local_err); | |
2350 | return NULL; | |
2351 | } | |
2352 | ||
2353 | info = g_new0(GuestMemoryBlockInfo, 1); | |
2354 | info->size = strtol(buf, NULL, 16); /* the unit is bytes */ | |
2355 | ||
2356 | g_free(buf); | |
2357 | ||
2358 | return info; | |
a065aaa9 HZ |
2359 | } |
2360 | ||
e72c3f2e MR |
2361 | #else /* defined(__linux__) */ |
2362 | ||
77dbc81b | 2363 | void qmp_guest_suspend_disk(Error **errp) |
e72c3f2e | 2364 | { |
c6bd8c70 | 2365 | error_setg(errp, QERR_UNSUPPORTED); |
e72c3f2e MR |
2366 | } |
2367 | ||
77dbc81b | 2368 | void qmp_guest_suspend_ram(Error **errp) |
e72c3f2e | 2369 | { |
c6bd8c70 | 2370 | error_setg(errp, QERR_UNSUPPORTED); |
e72c3f2e MR |
2371 | } |
2372 | ||
77dbc81b | 2373 | void qmp_guest_suspend_hybrid(Error **errp) |
e72c3f2e | 2374 | { |
c6bd8c70 | 2375 | error_setg(errp, QERR_UNSUPPORTED); |
e72c3f2e MR |
2376 | } |
2377 | ||
d35d4cb5 | 2378 | GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) |
e72c3f2e | 2379 | { |
c6bd8c70 | 2380 | error_setg(errp, QERR_UNSUPPORTED); |
d35d4cb5 | 2381 | return NULL; |
e72c3f2e MR |
2382 | } |
2383 | ||
d2baff62 LE |
2384 | GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) |
2385 | { | |
c6bd8c70 | 2386 | error_setg(errp, QERR_UNSUPPORTED); |
d2baff62 LE |
2387 | return NULL; |
2388 | } | |
2389 | ||
cbb65fc2 LE |
2390 | int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) |
2391 | { | |
c6bd8c70 | 2392 | error_setg(errp, QERR_UNSUPPORTED); |
cbb65fc2 LE |
2393 | return -1; |
2394 | } | |
2395 | ||
215a2771 DB |
2396 | void qmp_guest_set_user_password(const char *username, |
2397 | const char *password, | |
2398 | bool crypted, | |
2399 | Error **errp) | |
2400 | { | |
c6bd8c70 | 2401 | error_setg(errp, QERR_UNSUPPORTED); |
215a2771 DB |
2402 | } |
2403 | ||
a065aaa9 HZ |
2404 | GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) |
2405 | { | |
c6bd8c70 | 2406 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
2407 | return NULL; |
2408 | } | |
2409 | ||
2410 | GuestMemoryBlockResponseList * | |
2411 | qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) | |
2412 | { | |
c6bd8c70 | 2413 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
2414 | return NULL; |
2415 | } | |
2416 | ||
2417 | GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) | |
2418 | { | |
c6bd8c70 | 2419 | error_setg(errp, QERR_UNSUPPORTED); |
a065aaa9 HZ |
2420 | return NULL; |
2421 | } | |
2422 | ||
d35d4cb5 MR |
2423 | #endif |
2424 | ||
2425 | #if !defined(CONFIG_FSFREEZE) | |
2426 | ||
46d4c572 TS |
2427 | GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) |
2428 | { | |
c6bd8c70 | 2429 | error_setg(errp, QERR_UNSUPPORTED); |
46d4c572 TS |
2430 | return NULL; |
2431 | } | |
2432 | ||
77dbc81b | 2433 | GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) |
e72c3f2e | 2434 | { |
c6bd8c70 | 2435 | error_setg(errp, QERR_UNSUPPORTED); |
d35d4cb5 MR |
2436 | |
2437 | return 0; | |
e72c3f2e MR |
2438 | } |
2439 | ||
77dbc81b | 2440 | int64_t qmp_guest_fsfreeze_freeze(Error **errp) |
e72c3f2e | 2441 | { |
c6bd8c70 | 2442 | error_setg(errp, QERR_UNSUPPORTED); |
d35d4cb5 MR |
2443 | |
2444 | return 0; | |
e72c3f2e MR |
2445 | } |
2446 | ||
e99bce20 TS |
2447 | int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, |
2448 | strList *mountpoints, | |
2449 | Error **errp) | |
2450 | { | |
c6bd8c70 | 2451 | error_setg(errp, QERR_UNSUPPORTED); |
e99bce20 TS |
2452 | |
2453 | return 0; | |
2454 | } | |
2455 | ||
77dbc81b | 2456 | int64_t qmp_guest_fsfreeze_thaw(Error **errp) |
e72c3f2e | 2457 | { |
c6bd8c70 | 2458 | error_setg(errp, QERR_UNSUPPORTED); |
d35d4cb5 MR |
2459 | |
2460 | return 0; | |
e72c3f2e | 2461 | } |
eab5fd59 PB |
2462 | #endif /* CONFIG_FSFREEZE */ |
2463 | ||
2464 | #if !defined(CONFIG_FSTRIM) | |
e82855d9 JO |
2465 | GuestFilesystemTrimResponse * |
2466 | qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) | |
eab5fd59 | 2467 | { |
c6bd8c70 | 2468 | error_setg(errp, QERR_UNSUPPORTED); |
e82855d9 | 2469 | return NULL; |
eab5fd59 | 2470 | } |
e72c3f2e MR |
2471 | #endif |
2472 | ||
1281c08a TS |
2473 | /* add unsupported commands to the blacklist */ |
2474 | GList *ga_command_blacklist_init(GList *blacklist) | |
2475 | { | |
2476 | #if !defined(__linux__) | |
2477 | { | |
2478 | const char *list[] = { | |
2479 | "guest-suspend-disk", "guest-suspend-ram", | |
2480 | "guest-suspend-hybrid", "guest-network-get-interfaces", | |
0dd38a03 HZ |
2481 | "guest-get-vcpus", "guest-set-vcpus", |
2482 | "guest-get-memory-blocks", "guest-set-memory-blocks", | |
2483 | "guest-get-memory-block-size", NULL}; | |
1281c08a TS |
2484 | char **p = (char **)list; |
2485 | ||
2486 | while (*p) { | |
4bca81ce | 2487 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
2488 | } |
2489 | } | |
2490 | #endif | |
2491 | ||
2492 | #if !defined(CONFIG_FSFREEZE) | |
2493 | { | |
2494 | const char *list[] = { | |
2495 | "guest-get-fsinfo", "guest-fsfreeze-status", | |
2496 | "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list", | |
2497 | "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL}; | |
2498 | char **p = (char **)list; | |
2499 | ||
2500 | while (*p) { | |
4bca81ce | 2501 | blacklist = g_list_append(blacklist, g_strdup(*p++)); |
1281c08a TS |
2502 | } |
2503 | } | |
2504 | #endif | |
2505 | ||
2506 | #if !defined(CONFIG_FSTRIM) | |
4bca81ce | 2507 | blacklist = g_list_append(blacklist, g_strdup("guest-fstrim")); |
1281c08a TS |
2508 | #endif |
2509 | ||
2510 | return blacklist; | |
2511 | } | |
2512 | ||
e3d4d252 MR |
2513 | /* register init/cleanup routines for stateful command groups */ |
2514 | void ga_command_state_init(GAState *s, GACommandState *cs) | |
2515 | { | |
7006b9cf | 2516 | #if defined(CONFIG_FSFREEZE) |
f22d85e9 | 2517 | ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); |
7006b9cf | 2518 | #endif |
e3d4d252 | 2519 | } |