blob: f6f3e3cd8ee13c83c4b5602cfba5be00af7ea092 [file] [log] [blame]
Michael Rothe3d4d252011-07-19 15:41:55 -05001/*
Michael Roth42074a92012-01-19 22:19:27 -06002 * QEMU Guest Agent POSIX-specific command implementations
Michael Rothe3d4d252011-07-19 15:41:55 -05003 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
Michal Privoznik3424fc92012-02-29 17:02:23 +01008 * Michal Privoznik <mprivozn@redhat.com>
Michael Rothe3d4d252011-07-19 15:41:55 -05009 *
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
14#include <glib.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050015#include <sys/types.h>
16#include <sys/ioctl.h>
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030017#include <sys/wait.h>
Laszlo Ersekd2baff62013-03-06 22:59:30 +010018#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -040021#include <dirent.h>
Laszlo Ersekc689b4f2013-04-24 13:13:18 +020022#include <stdio.h>
23#include <string.h>
24#include <sys/stat.h>
Laszlo Ersekd2baff62013-03-06 22:59:30 +010025#include <inttypes.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050026#include "qga/guest-agent-core.h"
27#include "qga-qmp-commands.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010028#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/queue.h"
30#include "qemu/host-utils.h"
Michael Rothe3d4d252011-07-19 15:41:55 -050031
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030032#ifndef CONFIG_HAS_ENVIRON
Andreas Färbereecae142012-05-27 17:02:20 +020033#ifdef __APPLE__
34#include <crt_externs.h>
35#define environ (*_NSGetEnviron())
36#else
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030037extern char **environ;
38#endif
Andreas Färbereecae142012-05-27 17:02:20 +020039#endif
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030040
Michael Rothe72c3f22012-03-25 13:59:41 -050041#if defined(__linux__)
42#include <mntent.h>
43#include <linux/fs.h>
44#include <ifaddrs.h>
45#include <arpa/inet.h>
46#include <sys/socket.h>
47#include <net/if.h>
Michael Rothe72c3f22012-03-25 13:59:41 -050048
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020049#ifdef FIFREEZE
Michael Rothe72c3f22012-03-25 13:59:41 -050050#define CONFIG_FSFREEZE
51#endif
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020052#ifdef FITRIM
53#define CONFIG_FSTRIM
54#endif
Michael Rothe72c3f22012-03-25 13:59:41 -050055#endif
56
Markus Armbruster77dbc812014-05-02 13:26:30 +020057static void ga_wait_child(pid_t pid, int *status, Error **errp)
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020058{
59 pid_t rpid;
60
61 *status = 0;
62
63 do {
64 rpid = waitpid(pid, status, 0);
65 } while (rpid == -1 && errno == EINTR);
66
67 if (rpid == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +020068 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
69 pid);
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020070 return;
71 }
72
73 g_assert(rpid == pid);
74}
75
Markus Armbruster77dbc812014-05-02 13:26:30 +020076void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -050077{
Michael Rothe3d4d252011-07-19 15:41:55 -050078 const char *shutdown_flag;
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020079 Error *local_err = NULL;
80 pid_t pid;
Luiz Capitulino36748382012-05-14 15:25:20 -030081 int status;
Michael Rothe3d4d252011-07-19 15:41:55 -050082
83 slog("guest-shutdown called, mode: %s", mode);
84 if (!has_mode || strcmp(mode, "powerdown") == 0) {
85 shutdown_flag = "-P";
86 } else if (strcmp(mode, "halt") == 0) {
87 shutdown_flag = "-H";
88 } else if (strcmp(mode, "reboot") == 0) {
89 shutdown_flag = "-r";
90 } else {
Markus Armbruster77dbc812014-05-02 13:26:30 +020091 error_setg(errp,
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020092 "mode is invalid (valid values are: halt|powerdown|reboot");
Michael Rothe3d4d252011-07-19 15:41:55 -050093 return;
94 }
95
Luiz Capitulinod5dd3492012-05-11 16:19:47 -030096 pid = fork();
97 if (pid == 0) {
Michael Rothe3d4d252011-07-19 15:41:55 -050098 /* child, start the shutdown */
99 setsid();
Luiz Capitulino36748382012-05-14 15:25:20 -0300100 reopen_fd_to_null(0);
101 reopen_fd_to_null(1);
102 reopen_fd_to_null(2);
Michael Rothe3d4d252011-07-19 15:41:55 -0500103
whitearchey485e7412013-11-06 10:54:04 +0900104 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
Luiz Capitulino36748382012-05-14 15:25:20 -0300105 "hypervisor initiated shutdown", (char*)NULL, environ);
106 _exit(EXIT_FAILURE);
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300107 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200108 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300109 return;
110 }
111
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200112 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100113 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200114 error_propagate(errp, local_err);
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200115 return;
116 }
117
118 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200119 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200120 return;
121 }
122
123 if (WEXITSTATUS(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200124 error_setg(errp, "child process has failed to shutdown");
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200125 return;
126 }
127
Peter Maydell085d8132013-03-18 17:20:07 +0000128 /* succeeded */
Michael Rothe3d4d252011-07-19 15:41:55 -0500129}
130
Lei Li6912e6a2013-03-05 17:39:11 +0800131int64_t qmp_guest_get_time(Error **errp)
132{
133 int ret;
134 qemu_timeval tq;
135 int64_t time_ns;
136
137 ret = qemu_gettimeofday(&tq);
138 if (ret < 0) {
139 error_setg_errno(errp, errno, "Failed to get time");
140 return -1;
141 }
142
143 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
144 return time_ns;
145}
146
Michal Privoznik2c958922014-01-31 11:29:51 +0100147void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
Lei Lia1bca572013-03-05 17:39:12 +0800148{
149 int ret;
150 int status;
151 pid_t pid;
152 Error *local_err = NULL;
153 struct timeval tv;
154
Michal Privoznik2c958922014-01-31 11:29:51 +0100155 /* If user has passed a time, validate and set it. */
156 if (has_time) {
157 /* year-2038 will overflow in case time_t is 32bit */
158 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
159 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
160 return;
161 }
162
163 tv.tv_sec = time_ns / 1000000000;
164 tv.tv_usec = (time_ns % 1000000000) / 1000;
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 }
Lei Lia1bca572013-03-05 17:39:12 +0800171 }
172
Michal Privoznik2c958922014-01-31 11:29:51 +0100173 /* 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
Amos Kong1634df52014-04-04 23:25:02 +0800176 * hardware clock (RTC). */
Lei Lia1bca572013-03-05 17:39:12 +0800177 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
Michal Privoznik2c958922014-01-31 11:29:51 +0100184 /* 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);
Lei Lia1bca572013-03-05 17:39:12 +0800188 _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);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100195 if (local_err) {
Lei Lia1bca572013-03-05 17:39:12 +0800196 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
Michael Rothe3d4d252011-07-19 15:41:55 -0500211typedef struct GuestFileHandle {
212 uint64_t id;
213 FILE *fh;
214 QTAILQ_ENTRY(GuestFileHandle) next;
215} GuestFileHandle;
216
217static struct {
218 QTAILQ_HEAD(, GuestFileHandle) filehandles;
219} guest_file_state;
220
Michael Roth39097da2013-03-01 11:40:27 -0600221static int64_t guest_file_handle_add(FILE *fh, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500222{
223 GuestFileHandle *gfh;
Michael Roth39097da2013-03-01 11:40:27 -0600224 int64_t handle;
225
226 handle = ga_get_fd_handle(ga_state, errp);
Markus Armbrustera903f402014-05-02 13:26:33 +0200227 if (handle < 0) {
228 return -1;
Michael Roth39097da2013-03-01 11:40:27 -0600229 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500230
Anthony Liguori7267c092011-08-20 22:09:37 -0500231 gfh = g_malloc0(sizeof(GuestFileHandle));
Michael Roth39097da2013-03-01 11:40:27 -0600232 gfh->id = handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500233 gfh->fh = fh;
234 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
Michael Roth39097da2013-03-01 11:40:27 -0600235
236 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500237}
238
Markus Armbruster77dbc812014-05-02 13:26:30 +0200239static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500240{
241 GuestFileHandle *gfh;
242
243 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
244 {
245 if (gfh->id == id) {
246 return gfh;
247 }
248 }
249
Markus Armbruster77dbc812014-05-02 13:26:30 +0200250 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
Michael Rothe3d4d252011-07-19 15:41:55 -0500251 return NULL;
252}
253
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200254typedef const char * const ccpc;
255
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200256#ifndef O_BINARY
257#define O_BINARY 0
258#endif
259
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200260/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
261static const struct {
262 ccpc *forms;
263 int oflag_base;
264} guest_file_open_modes[] = {
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200265 { (ccpc[]){ "r", NULL }, O_RDONLY },
266 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
267 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
268 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
269 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
270 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
271 { (ccpc[]){ "r+", NULL }, O_RDWR },
272 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
273 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
274 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
275 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
276 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200277};
278
279static int
Markus Armbruster77dbc812014-05-02 13:26:30 +0200280find_open_flag(const char *mode_str, Error **errp)
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200281{
282 unsigned mode;
283
284 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
285 ccpc *form;
286
287 form = guest_file_open_modes[mode].forms;
288 while (*form != NULL && strcmp(*form, mode_str) != 0) {
289 ++form;
290 }
291 if (*form != NULL) {
292 break;
293 }
294 }
295
296 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200297 error_setg(errp, "invalid file open mode '%s'", mode_str);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200298 return -1;
299 }
300 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
301}
302
303#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
304 S_IRGRP | S_IWGRP | \
305 S_IROTH | S_IWOTH)
306
307static FILE *
Markus Armbruster77dbc812014-05-02 13:26:30 +0200308safe_open_or_create(const char *path, const char *mode, Error **errp)
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200309{
310 Error *local_err = NULL;
311 int oflag;
312
313 oflag = find_open_flag(mode, &local_err);
314 if (local_err == NULL) {
315 int fd;
316
317 /* If the caller wants / allows creation of a new file, we implement it
318 * with a two step process: open() + (open() / fchmod()).
319 *
320 * First we insist on creating the file exclusively as a new file. If
321 * that succeeds, we're free to set any file-mode bits on it. (The
322 * motivation is that we want to set those file-mode bits independently
323 * of the current umask.)
324 *
325 * If the exclusive creation fails because the file already exists
326 * (EEXIST is not possible for any other reason), we just attempt to
327 * open the file, but in this case we won't be allowed to change the
328 * file-mode bits on the preexistent file.
329 *
330 * The pathname should never disappear between the two open()s in
331 * practice. If it happens, then someone very likely tried to race us.
332 * In this case just go ahead and report the ENOENT from the second
333 * open() to the caller.
334 *
335 * If the caller wants to open a preexistent file, then the first
336 * open() is decisive and its third argument is ignored, and the second
337 * open() and the fchmod() are never called.
338 */
339 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
340 if (fd == -1 && errno == EEXIST) {
341 oflag &= ~(unsigned)O_CREAT;
342 fd = open(path, oflag);
343 }
344
345 if (fd == -1) {
346 error_setg_errno(&local_err, errno, "failed to open file '%s' "
347 "(mode: '%s')", path, mode);
348 } else {
349 qemu_set_cloexec(fd);
350
351 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
352 error_setg_errno(&local_err, errno, "failed to set permission "
353 "0%03o on new file '%s' (mode: '%s')",
354 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
355 } else {
356 FILE *f;
357
358 f = fdopen(fd, mode);
359 if (f == NULL) {
360 error_setg_errno(&local_err, errno, "failed to associate "
361 "stdio stream with file descriptor %d, "
362 "file '%s' (mode: '%s')", fd, path, mode);
363 } else {
364 return f;
365 }
366 }
367
368 close(fd);
Laszlo Ersek2b720012013-05-08 17:31:36 +0200369 if (oflag & O_CREAT) {
370 unlink(path);
371 }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200372 }
373 }
374
Markus Armbruster77dbc812014-05-02 13:26:30 +0200375 error_propagate(errp, local_err);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200376 return NULL;
377}
378
Markus Armbruster77dbc812014-05-02 13:26:30 +0200379int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
380 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500381{
382 FILE *fh;
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200383 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500384 int fd;
Michael Roth39097da2013-03-01 11:40:27 -0600385 int64_t ret = -1, handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500386
387 if (!has_mode) {
388 mode = "r";
389 }
390 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200391 fh = safe_open_or_create(path, mode, &local_err);
392 if (local_err != NULL) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200393 error_propagate(errp, local_err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500394 return -1;
395 }
396
397 /* set fd non-blocking to avoid common use cases (like reading from a
398 * named pipe) from hanging the agent
399 */
400 fd = fileno(fh);
401 ret = fcntl(fd, F_GETFL);
402 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
403 if (ret == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200404 error_setg_errno(errp, errno, "failed to make file '%s' non-blocking",
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200405 path);
Michael Rothe3d4d252011-07-19 15:41:55 -0500406 fclose(fh);
407 return -1;
408 }
409
Markus Armbruster77dbc812014-05-02 13:26:30 +0200410 handle = guest_file_handle_add(fh, errp);
Markus Armbrustera903f402014-05-02 13:26:33 +0200411 if (handle < 0) {
Michael Roth39097da2013-03-01 11:40:27 -0600412 fclose(fh);
413 return -1;
414 }
415
Stefan Weild607a522013-11-17 19:19:52 +0100416 slog("guest-file-open, handle: %" PRId64, handle);
Michael Roth39097da2013-03-01 11:40:27 -0600417 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500418}
419
Markus Armbruster77dbc812014-05-02 13:26:30 +0200420void qmp_guest_file_close(int64_t handle, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500421{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200422 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500423 int ret;
424
Stefan Weild607a522013-11-17 19:19:52 +0100425 slog("guest-file-close called, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500426 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500427 return;
428 }
429
430 ret = fclose(gfh->fh);
Luiz Capitulino3ac4b7c2012-11-27 11:01:56 -0200431 if (ret == EOF) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200432 error_setg_errno(errp, errno, "failed to close handle");
Michael Rothe3d4d252011-07-19 15:41:55 -0500433 return;
434 }
435
436 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500437 g_free(gfh);
Michael Rothe3d4d252011-07-19 15:41:55 -0500438}
439
440struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
Markus Armbruster77dbc812014-05-02 13:26:30 +0200441 int64_t count, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500442{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200443 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500444 GuestFileRead *read_data = NULL;
445 guchar *buf;
446 FILE *fh;
447 size_t read_count;
448
449 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500450 return NULL;
451 }
452
453 if (!has_count) {
454 count = QGA_READ_COUNT_DEFAULT;
455 } else if (count < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200456 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200457 count);
Michael Rothe3d4d252011-07-19 15:41:55 -0500458 return NULL;
459 }
460
461 fh = gfh->fh;
Anthony Liguori7267c092011-08-20 22:09:37 -0500462 buf = g_malloc0(count+1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500463 read_count = fread(buf, 1, count, fh);
464 if (ferror(fh)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200465 error_setg_errno(errp, errno, "failed to read file");
Stefan Weild607a522013-11-17 19:19:52 +0100466 slog("guest-file-read failed, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500467 } else {
468 buf[read_count] = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500469 read_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500470 read_data->count = read_count;
471 read_data->eof = feof(fh);
472 if (read_count) {
473 read_data->buf_b64 = g_base64_encode(buf, read_count);
474 }
475 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500476 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500477 clearerr(fh);
478
479 return read_data;
480}
481
482GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
Markus Armbruster77dbc812014-05-02 13:26:30 +0200483 bool has_count, int64_t count,
484 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500485{
486 GuestFileWrite *write_data = NULL;
487 guchar *buf;
488 gsize buf_len;
489 int write_count;
Markus Armbruster77dbc812014-05-02 13:26:30 +0200490 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500491 FILE *fh;
492
493 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500494 return NULL;
495 }
496
497 fh = gfh->fh;
498 buf = g_base64_decode(buf_b64, &buf_len);
499
500 if (!has_count) {
501 count = buf_len;
502 } else if (count < 0 || count > buf_len) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200503 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200504 count);
Anthony Liguori7267c092011-08-20 22:09:37 -0500505 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500506 return NULL;
507 }
508
509 write_count = fwrite(buf, 1, count, fh);
510 if (ferror(fh)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200511 error_setg_errno(errp, errno, "failed to write to file");
Stefan Weild607a522013-11-17 19:19:52 +0100512 slog("guest-file-write failed, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500513 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500514 write_data = g_malloc0(sizeof(GuestFileWrite));
Michael Rothe3d4d252011-07-19 15:41:55 -0500515 write_data->count = write_count;
516 write_data->eof = feof(fh);
517 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500518 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500519 clearerr(fh);
520
521 return write_data;
522}
523
524struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
Markus Armbruster77dbc812014-05-02 13:26:30 +0200525 int64_t whence, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500526{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200527 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500528 GuestFileSeek *seek_data = NULL;
529 FILE *fh;
530 int ret;
531
532 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500533 return NULL;
534 }
535
536 fh = gfh->fh;
537 ret = fseek(fh, offset, whence);
538 if (ret == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200539 error_setg_errno(errp, errno, "failed to seek file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500540 } else {
Markus Armbruster10b7c5d2014-02-21 13:36:49 +0100541 seek_data = g_new0(GuestFileSeek, 1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500542 seek_data->position = ftell(fh);
543 seek_data->eof = feof(fh);
544 }
545 clearerr(fh);
546
547 return seek_data;
548}
549
Markus Armbruster77dbc812014-05-02 13:26:30 +0200550void qmp_guest_file_flush(int64_t handle, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500551{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200552 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500553 FILE *fh;
554 int ret;
555
556 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500557 return;
558 }
559
560 fh = gfh->fh;
561 ret = fflush(fh);
562 if (ret == EOF) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200563 error_setg_errno(errp, errno, "failed to flush file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500564 }
565}
566
567static void guest_file_init(void)
568{
569 QTAILQ_INIT(&guest_file_state.filehandles);
570}
571
Michael Rothe72c3f22012-03-25 13:59:41 -0500572/* linux-specific implementations. avoid this if at all possible. */
573#if defined(__linux__)
574
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200575#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
Paolo Bonziniaf022032012-06-13 07:41:27 +0200576typedef struct FsMount {
Michael Rothe3d4d252011-07-19 15:41:55 -0500577 char *dirname;
578 char *devtype;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400579 unsigned int devmajor, devminor;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200580 QTAILQ_ENTRY(FsMount) next;
581} FsMount;
Michael Rothe3d4d252011-07-19 15:41:55 -0500582
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -0400583typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
Michael Roth9e8aded432012-04-16 19:52:17 -0500584
Paolo Bonziniaf022032012-06-13 07:41:27 +0200585static void free_fs_mount_list(FsMountList *mounts)
Michael Roth9e8aded432012-04-16 19:52:17 -0500586{
Paolo Bonziniaf022032012-06-13 07:41:27 +0200587 FsMount *mount, *temp;
Michael Roth9e8aded432012-04-16 19:52:17 -0500588
589 if (!mounts) {
590 return;
591 }
592
593 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
594 QTAILQ_REMOVE(mounts, mount, next);
595 g_free(mount->dirname);
596 g_free(mount->devtype);
597 g_free(mount);
598 }
599}
600
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400601static int dev_major_minor(const char *devpath,
602 unsigned int *devmajor, unsigned int *devminor)
603{
604 struct stat st;
605
606 *devmajor = 0;
607 *devminor = 0;
608
609 if (stat(devpath, &st) < 0) {
610 slog("failed to stat device file '%s': %s", devpath, strerror(errno));
611 return -1;
612 }
613 if (S_ISDIR(st.st_mode)) {
614 /* It is bind mount */
615 return -2;
616 }
617 if (S_ISBLK(st.st_mode)) {
618 *devmajor = major(st.st_rdev);
619 *devminor = minor(st.st_rdev);
620 return 0;
621 }
622 return -1;
623}
624
Michael Rothe3d4d252011-07-19 15:41:55 -0500625/*
626 * Walk the mount table and build a list of local file systems
627 */
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400628static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500629{
630 struct mntent *ment;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200631 FsMount *mount;
Michael Roth9e2fa412012-05-29 10:08:50 -0500632 char const *mtab = "/proc/self/mounts";
Michael Rothe3d4d252011-07-19 15:41:55 -0500633 FILE *fp;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400634 unsigned int devmajor, devminor;
Michael Rothe3d4d252011-07-19 15:41:55 -0500635
Michael Rothe3d4d252011-07-19 15:41:55 -0500636 fp = setmntent(mtab, "r");
637 if (!fp) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200638 error_setg(errp, "failed to open mtab file: '%s'", mtab);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200639 return;
Michael Rothe3d4d252011-07-19 15:41:55 -0500640 }
641
642 while ((ment = getmntent(fp))) {
643 /*
644 * An entry which device name doesn't start with a '/' is
645 * either a dummy file system or a network file system.
646 * Add special handling for smbfs and cifs as is done by
647 * coreutils as well.
648 */
649 if ((ment->mnt_fsname[0] != '/') ||
650 (strcmp(ment->mnt_type, "smbfs") == 0) ||
651 (strcmp(ment->mnt_type, "cifs") == 0)) {
652 continue;
653 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400654 if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
655 /* Skip bind mounts */
656 continue;
657 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500658
Paolo Bonziniaf022032012-06-13 07:41:27 +0200659 mount = g_malloc0(sizeof(FsMount));
Anthony Liguori7267c092011-08-20 22:09:37 -0500660 mount->dirname = g_strdup(ment->mnt_dir);
661 mount->devtype = g_strdup(ment->mnt_type);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400662 mount->devmajor = devmajor;
663 mount->devminor = devminor;
Michael Rothe3d4d252011-07-19 15:41:55 -0500664
Michael Roth9e8aded432012-04-16 19:52:17 -0500665 QTAILQ_INSERT_TAIL(mounts, mount, next);
Michael Rothe3d4d252011-07-19 15:41:55 -0500666 }
667
668 endmntent(fp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500669}
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400670
671static void decode_mntname(char *name, int len)
672{
673 int i, j = 0;
674 for (i = 0; i <= len; i++) {
675 if (name[i] != '\\') {
676 name[j++] = name[i];
677 } else if (name[i + 1] == '\\') {
678 name[j++] = '\\';
679 i++;
680 } else if (name[i + 1] >= '0' && name[i + 1] <= '3' &&
681 name[i + 2] >= '0' && name[i + 2] <= '7' &&
682 name[i + 3] >= '0' && name[i + 3] <= '7') {
683 name[j++] = (name[i + 1] - '0') * 64 +
684 (name[i + 2] - '0') * 8 +
685 (name[i + 3] - '0');
686 i += 3;
687 } else {
688 name[j++] = name[i];
689 }
690 }
691}
692
693static void build_fs_mount_list(FsMountList *mounts, Error **errp)
694{
695 FsMount *mount;
696 char const *mountinfo = "/proc/self/mountinfo";
697 FILE *fp;
698 char *line = NULL, *dash;
699 size_t n;
700 char check;
701 unsigned int devmajor, devminor;
702 int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
703
704 fp = fopen(mountinfo, "r");
705 if (!fp) {
706 build_fs_mount_list_from_mtab(mounts, errp);
707 return;
708 }
709
710 while (getline(&line, &n, fp) != -1) {
711 ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c",
712 &devmajor, &devminor, &dir_s, &dir_e, &check);
713 if (ret < 3) {
714 continue;
715 }
716 dash = strstr(line + dir_e, " - ");
717 if (!dash) {
718 continue;
719 }
720 ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
721 &type_s, &type_e, &dev_s, &dev_e, &check);
722 if (ret < 1) {
723 continue;
724 }
725 line[dir_e] = 0;
726 dash[type_e] = 0;
727 dash[dev_e] = 0;
728 decode_mntname(line + dir_s, dir_e - dir_s);
729 decode_mntname(dash + dev_s, dev_e - dev_s);
730 if (devmajor == 0) {
731 /* btrfs reports major number = 0 */
732 if (strcmp("btrfs", dash + type_s) != 0 ||
733 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
734 continue;
735 }
736 }
737
738 mount = g_malloc0(sizeof(FsMount));
739 mount->dirname = g_strdup(line + dir_s);
740 mount->devtype = g_strdup(dash + type_s);
741 mount->devmajor = devmajor;
742 mount->devminor = devminor;
743
744 QTAILQ_INSERT_TAIL(mounts, mount, next);
745 }
746 free(line);
747
748 fclose(fp);
749}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200750#endif
751
752#if defined(CONFIG_FSFREEZE)
Michael Rothe3d4d252011-07-19 15:41:55 -0500753
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400754static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
755{
756 char *path;
757 char *dpath;
758 char *driver = NULL;
759 char buf[PATH_MAX];
760 ssize_t len;
761
762 path = g_strndup(syspath, pathlen);
763 dpath = g_strdup_printf("%s/driver", path);
764 len = readlink(dpath, buf, sizeof(buf) - 1);
765 if (len != -1) {
766 buf[len] = 0;
767 driver = g_strdup(basename(buf));
768 }
769 g_free(dpath);
770 g_free(path);
771 return driver;
772}
773
774static int compare_uint(const void *_a, const void *_b)
775{
776 unsigned int a = *(unsigned int *)_a;
777 unsigned int b = *(unsigned int *)_b;
778
779 return a < b ? -1 : a > b ? 1 : 0;
780}
781
782/* Walk the specified sysfs and build a sorted list of host or ata numbers */
783static int build_hosts(char const *syspath, char const *host, bool ata,
784 unsigned int *hosts, int hosts_max, Error **errp)
785{
786 char *path;
787 DIR *dir;
788 struct dirent *entry;
789 int i = 0;
790
791 path = g_strndup(syspath, host - syspath);
792 dir = opendir(path);
793 if (!dir) {
794 error_setg_errno(errp, errno, "opendir(\"%s\")", path);
795 g_free(path);
796 return -1;
797 }
798
799 while (i < hosts_max) {
800 entry = readdir(dir);
801 if (!entry) {
802 break;
803 }
804 if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
805 ++i;
806 } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
807 ++i;
808 }
809 }
810
811 qsort(hosts, i, sizeof(hosts[0]), compare_uint);
812
813 g_free(path);
814 closedir(dir);
815 return i;
816}
817
818/* Store disk device info specified by @sysfs into @fs */
819static void build_guest_fsinfo_for_real_device(char const *syspath,
820 GuestFilesystemInfo *fs,
821 Error **errp)
822{
823 unsigned int pci[4], host, hosts[8], tgt[3];
824 int i, nhosts = 0, pcilen;
825 GuestDiskAddress *disk;
826 GuestPCIAddress *pciaddr;
827 GuestDiskAddressList *list = NULL;
828 bool has_ata = false, has_host = false, has_tgt = false;
829 char *p, *q, *driver = NULL;
830
831 p = strstr(syspath, "/devices/pci");
832 if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
833 pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
834 g_debug("only pci device is supported: sysfs path \"%s\"", syspath);
835 return;
836 }
837
838 driver = get_pci_driver(syspath, (p + 12 + pcilen) - syspath, errp);
839 if (!driver) {
840 goto cleanup;
841 }
842
843 p = strstr(syspath, "/target");
844 if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
845 tgt, tgt + 1, tgt + 2) == 3) {
846 has_tgt = true;
847 }
848
849 p = strstr(syspath, "/ata");
850 if (p) {
851 q = p + 4;
852 has_ata = true;
853 } else {
854 p = strstr(syspath, "/host");
855 q = p + 5;
856 }
857 if (p && sscanf(q, "%u", &host) == 1) {
858 has_host = true;
859 nhosts = build_hosts(syspath, p, has_ata, hosts,
860 sizeof(hosts) / sizeof(hosts[0]), errp);
861 if (nhosts < 0) {
862 goto cleanup;
863 }
864 }
865
866 pciaddr = g_malloc0(sizeof(*pciaddr));
867 pciaddr->domain = pci[0];
868 pciaddr->bus = pci[1];
869 pciaddr->slot = pci[2];
870 pciaddr->function = pci[3];
871
872 disk = g_malloc0(sizeof(*disk));
873 disk->pci_controller = pciaddr;
874
875 list = g_malloc0(sizeof(*list));
876 list->value = disk;
877
878 if (strcmp(driver, "ata_piix") == 0) {
879 /* a host per ide bus, target*:0:<unit>:0 */
880 if (!has_host || !has_tgt) {
881 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
882 goto cleanup;
883 }
884 for (i = 0; i < nhosts; i++) {
885 if (host == hosts[i]) {
886 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
887 disk->bus = i;
888 disk->unit = tgt[1];
889 break;
890 }
891 }
892 if (i >= nhosts) {
893 g_debug("no host for '%s' (driver '%s')", syspath, driver);
894 goto cleanup;
895 }
896 } else if (strcmp(driver, "sym53c8xx") == 0) {
897 /* scsi(LSI Logic): target*:0:<unit>:0 */
898 if (!has_tgt) {
899 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
900 goto cleanup;
901 }
902 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
903 disk->unit = tgt[1];
904 } else if (strcmp(driver, "virtio-pci") == 0) {
905 if (has_tgt) {
906 /* virtio-scsi: target*:0:0:<unit> */
907 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
908 disk->unit = tgt[2];
909 } else {
910 /* virtio-blk: 1 disk per 1 device */
911 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
912 }
913 } else if (strcmp(driver, "ahci") == 0) {
914 /* ahci: 1 host per 1 unit */
915 if (!has_host || !has_tgt) {
916 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
917 goto cleanup;
918 }
919 for (i = 0; i < nhosts; i++) {
920 if (host == hosts[i]) {
921 disk->unit = i;
922 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
923 break;
924 }
925 }
926 if (i >= nhosts) {
927 g_debug("no host for '%s' (driver '%s')", syspath, driver);
928 goto cleanup;
929 }
930 } else {
931 g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
932 goto cleanup;
933 }
934
935 list->next = fs->disk;
936 fs->disk = list;
937 g_free(driver);
938 return;
939
940cleanup:
941 if (list) {
942 qapi_free_GuestDiskAddressList(list);
943 }
944 g_free(driver);
945}
946
947static void build_guest_fsinfo_for_device(char const *devpath,
948 GuestFilesystemInfo *fs,
949 Error **errp);
950
951/* Store a list of slave devices of virtual volume specified by @syspath into
952 * @fs */
953static void build_guest_fsinfo_for_virtual_device(char const *syspath,
954 GuestFilesystemInfo *fs,
955 Error **errp)
956{
957 DIR *dir;
958 char *dirpath;
zhanghailiange668d1b2014-09-19 11:09:10 +0800959 struct dirent *entry;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400960
961 dirpath = g_strdup_printf("%s/slaves", syspath);
962 dir = opendir(dirpath);
963 if (!dir) {
964 error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
965 g_free(dirpath);
966 return;
967 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400968
969 for (;;) {
zhanghailiange668d1b2014-09-19 11:09:10 +0800970 errno = 0;
971 entry = readdir(dir);
972 if (entry == NULL) {
973 if (errno) {
974 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
975 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400976 break;
977 }
978
zhanghailiange668d1b2014-09-19 11:09:10 +0800979 if (entry->d_type == DT_LNK) {
980 char *path;
981
982 g_debug(" slave device '%s'", entry->d_name);
983 path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
984 build_guest_fsinfo_for_device(path, fs, errp);
985 g_free(path);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400986
987 if (*errp) {
988 break;
989 }
990 }
991 }
992
zhanghailiange668d1b2014-09-19 11:09:10 +0800993 g_free(dirpath);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400994 closedir(dir);
995}
996
997/* Dispatch to functions for virtual/real device */
998static void build_guest_fsinfo_for_device(char const *devpath,
999 GuestFilesystemInfo *fs,
1000 Error **errp)
1001{
1002 char *syspath = realpath(devpath, NULL);
1003
1004 if (!syspath) {
1005 error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1006 return;
1007 }
1008
1009 if (!fs->name) {
1010 fs->name = g_strdup(basename(syspath));
1011 }
1012
1013 g_debug(" parse sysfs path '%s'", syspath);
1014
1015 if (strstr(syspath, "/devices/virtual/block/")) {
1016 build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1017 } else {
1018 build_guest_fsinfo_for_real_device(syspath, fs, errp);
1019 }
1020
1021 free(syspath);
1022}
1023
1024/* Return a list of the disk device(s)' info which @mount lies on */
1025static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1026 Error **errp)
1027{
1028 GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1029 char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1030 mount->devmajor, mount->devminor);
1031
1032 fs->mountpoint = g_strdup(mount->dirname);
1033 fs->type = g_strdup(mount->devtype);
1034 build_guest_fsinfo_for_device(devpath, fs, errp);
1035
1036 g_free(devpath);
1037 return fs;
1038}
1039
1040GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1041{
1042 FsMountList mounts;
1043 struct FsMount *mount;
1044 GuestFilesystemInfoList *new, *ret = NULL;
1045 Error *local_err = NULL;
1046
1047 QTAILQ_INIT(&mounts);
1048 build_fs_mount_list(&mounts, &local_err);
1049 if (local_err) {
1050 error_propagate(errp, local_err);
1051 return NULL;
1052 }
1053
1054 QTAILQ_FOREACH(mount, &mounts, next) {
1055 g_debug("Building guest fsinfo for '%s'", mount->dirname);
1056
1057 new = g_malloc0(sizeof(*ret));
1058 new->value = build_guest_fsinfo(mount, &local_err);
1059 new->next = ret;
1060 ret = new;
1061 if (local_err) {
1062 error_propagate(errp, local_err);
1063 qapi_free_GuestFilesystemInfoList(ret);
1064 ret = NULL;
1065 break;
1066 }
1067 }
1068
1069 free_fs_mount_list(&mounts);
1070 return ret;
1071}
1072
1073
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001074typedef enum {
1075 FSFREEZE_HOOK_THAW = 0,
1076 FSFREEZE_HOOK_FREEZE,
1077} FsfreezeHookArg;
1078
Stefan Weil13a439e2014-07-07 21:07:29 +02001079static const char *fsfreeze_hook_arg_string[] = {
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001080 "thaw",
1081 "freeze",
1082};
1083
Markus Armbruster77dbc812014-05-02 13:26:30 +02001084static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001085{
1086 int status;
1087 pid_t pid;
1088 const char *hook;
1089 const char *arg_str = fsfreeze_hook_arg_string[arg];
1090 Error *local_err = NULL;
1091
1092 hook = ga_fsfreeze_hook(ga_state);
1093 if (!hook) {
1094 return;
1095 }
1096 if (access(hook, X_OK) != 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001097 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001098 return;
1099 }
1100
1101 slog("executing fsfreeze hook with arg '%s'", arg_str);
1102 pid = fork();
1103 if (pid == 0) {
1104 setsid();
1105 reopen_fd_to_null(0);
1106 reopen_fd_to_null(1);
1107 reopen_fd_to_null(2);
1108
1109 execle(hook, hook, arg_str, NULL, environ);
1110 _exit(EXIT_FAILURE);
1111 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001112 error_setg_errno(errp, errno, "failed to create child process");
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001113 return;
1114 }
1115
1116 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001117 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001118 error_propagate(errp, local_err);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001119 return;
1120 }
1121
1122 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001123 error_setg(errp, "fsfreeze hook has terminated abnormally");
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001124 return;
1125 }
1126
1127 status = WEXITSTATUS(status);
1128 if (status) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001129 error_setg(errp, "fsfreeze hook has failed with status %d", status);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001130 return;
1131 }
1132}
1133
Michael Rothe3d4d252011-07-19 15:41:55 -05001134/*
1135 * Return status of freeze/thaw
1136 */
Markus Armbruster77dbc812014-05-02 13:26:30 +02001137GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001138{
Michael Rothf22d85e2012-04-17 19:01:45 -05001139 if (ga_is_frozen(ga_state)) {
1140 return GUEST_FSFREEZE_STATUS_FROZEN;
1141 }
1142
1143 return GUEST_FSFREEZE_STATUS_THAWED;
Michael Rothe3d4d252011-07-19 15:41:55 -05001144}
1145
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001146int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1147{
1148 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1149}
1150
Michael Rothe3d4d252011-07-19 15:41:55 -05001151/*
1152 * Walk list of mounted file systems in the guest, and freeze the ones which
1153 * are real local file systems.
1154 */
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001155int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1156 strList *mountpoints,
1157 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001158{
1159 int ret = 0, i = 0;
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001160 strList *list;
Paolo Bonziniaf022032012-06-13 07:41:27 +02001161 FsMountList mounts;
1162 struct FsMount *mount;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001163 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -05001164 int fd;
Michael Rothe3d4d252011-07-19 15:41:55 -05001165
1166 slog("guest-fsfreeze called");
1167
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001168 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001169 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001170 error_propagate(errp, local_err);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001171 return -1;
1172 }
1173
Michael Roth9e8aded432012-04-16 19:52:17 -05001174 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001175 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001176 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001177 error_propagate(errp, local_err);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001178 return -1;
Michael Rothe3d4d252011-07-19 15:41:55 -05001179 }
1180
1181 /* cannot risk guest agent blocking itself on a write in this state */
Michael Rothf22d85e2012-04-17 19:01:45 -05001182 ga_set_frozen(ga_state);
Michael Rothe3d4d252011-07-19 15:41:55 -05001183
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -04001184 QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001185 /* To issue fsfreeze in the reverse order of mounts, check if the
1186 * mount is listed in the list here */
1187 if (has_mountpoints) {
1188 for (list = mountpoints; list; list = list->next) {
1189 if (strcmp(list->value, mount->dirname) == 0) {
1190 break;
1191 }
1192 }
1193 if (!list) {
1194 continue;
1195 }
1196 }
1197
Michael Rothe3d4d252011-07-19 15:41:55 -05001198 fd = qemu_open(mount->dirname, O_RDONLY);
1199 if (fd == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001200 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
Michael Rothe3d4d252011-07-19 15:41:55 -05001201 goto error;
1202 }
1203
1204 /* we try to cull filesytems we know won't work in advance, but other
1205 * filesytems may not implement fsfreeze for less obvious reasons.
Michael Roth9e8aded432012-04-16 19:52:17 -05001206 * these will report EOPNOTSUPP. we simply ignore these when tallying
1207 * the number of frozen filesystems.
1208 *
1209 * any other error means a failure to freeze a filesystem we
1210 * expect to be freezable, so return an error in those cases
1211 * and return system to thawed state.
Michael Rothe3d4d252011-07-19 15:41:55 -05001212 */
1213 ret = ioctl(fd, FIFREEZE);
Michael Roth9e8aded432012-04-16 19:52:17 -05001214 if (ret == -1) {
1215 if (errno != EOPNOTSUPP) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001216 error_setg_errno(errp, errno, "failed to freeze %s",
Luiz Capitulino617fbbc2012-11-27 11:02:00 -02001217 mount->dirname);
Michael Roth9e8aded432012-04-16 19:52:17 -05001218 close(fd);
1219 goto error;
1220 }
1221 } else {
1222 i++;
Michael Rothe3d4d252011-07-19 15:41:55 -05001223 }
1224 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -05001225 }
1226
Paolo Bonziniaf022032012-06-13 07:41:27 +02001227 free_fs_mount_list(&mounts);
Michael Rothe3d4d252011-07-19 15:41:55 -05001228 return i;
1229
1230error:
Paolo Bonziniaf022032012-06-13 07:41:27 +02001231 free_fs_mount_list(&mounts);
Michael Roth9e8aded432012-04-16 19:52:17 -05001232 qmp_guest_fsfreeze_thaw(NULL);
Michael Rothe3d4d252011-07-19 15:41:55 -05001233 return 0;
1234}
1235
1236/*
1237 * Walk list of frozen file systems in the guest, and thaw them.
1238 */
Markus Armbruster77dbc812014-05-02 13:26:30 +02001239int64_t qmp_guest_fsfreeze_thaw(Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001240{
1241 int ret;
Paolo Bonziniaf022032012-06-13 07:41:27 +02001242 FsMountList mounts;
1243 FsMount *mount;
Michael Roth9e8aded432012-04-16 19:52:17 -05001244 int fd, i = 0, logged;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001245 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -05001246
Michael Roth9e8aded432012-04-16 19:52:17 -05001247 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001248 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001249 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001250 error_propagate(errp, local_err);
Michael Roth9e8aded432012-04-16 19:52:17 -05001251 return 0;
1252 }
1253
1254 QTAILQ_FOREACH(mount, &mounts, next) {
1255 logged = false;
Michael Rothe3d4d252011-07-19 15:41:55 -05001256 fd = qemu_open(mount->dirname, O_RDONLY);
1257 if (fd == -1) {
Michael Rothe3d4d252011-07-19 15:41:55 -05001258 continue;
1259 }
Michael Roth9e8aded432012-04-16 19:52:17 -05001260 /* we have no way of knowing whether a filesystem was actually unfrozen
1261 * as a result of a successful call to FITHAW, only that if an error
1262 * was returned the filesystem was *not* unfrozen by that particular
1263 * call.
1264 *
Jim Meyeringa31f0532012-05-09 05:12:04 +00001265 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
Michael Roth9e8aded432012-04-16 19:52:17 -05001266 * to unfreeze, continuing issuing FITHAW until an error is returned,
1267 * in which case either the filesystem is in an unfreezable state, or,
1268 * more likely, it was thawed previously (and remains so afterward).
1269 *
1270 * also, since the most recent successful call is the one that did
1271 * the actual unfreeze, we can use this to provide an accurate count
1272 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1273 * may * be useful for determining whether a filesystem was unfrozen
1274 * during the freeze/thaw phase by a process other than qemu-ga.
1275 */
1276 do {
1277 ret = ioctl(fd, FITHAW);
1278 if (ret == 0 && !logged) {
1279 i++;
1280 logged = true;
1281 }
1282 } while (ret == 0);
Michael Rothe3d4d252011-07-19 15:41:55 -05001283 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -05001284 }
1285
Michael Rothf22d85e2012-04-17 19:01:45 -05001286 ga_unset_frozen(ga_state);
Paolo Bonziniaf022032012-06-13 07:41:27 +02001287 free_fs_mount_list(&mounts);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001288
Markus Armbruster77dbc812014-05-02 13:26:30 +02001289 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001290
Michael Rothe3d4d252011-07-19 15:41:55 -05001291 return i;
1292}
1293
Michael Rothe3d4d252011-07-19 15:41:55 -05001294static void guest_fsfreeze_cleanup(void)
1295{
Michael Rothe3d4d252011-07-19 15:41:55 -05001296 Error *err = NULL;
1297
Michael Rothf22d85e2012-04-17 19:01:45 -05001298 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
Markus Armbruster6f686742013-01-16 18:15:08 +01001299 qmp_guest_fsfreeze_thaw(&err);
1300 if (err) {
1301 slog("failed to clean up frozen filesystems: %s",
1302 error_get_pretty(err));
1303 error_free(err);
Michael Rothe3d4d252011-07-19 15:41:55 -05001304 }
1305 }
1306}
Michael Rothe72c3f22012-03-25 13:59:41 -05001307#endif /* CONFIG_FSFREEZE */
Michael Rothe3d4d252011-07-19 15:41:55 -05001308
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001309#if defined(CONFIG_FSTRIM)
1310/*
1311 * Walk list of mounted file systems in the guest, and trim them.
1312 */
Markus Armbruster77dbc812014-05-02 13:26:30 +02001313void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001314{
1315 int ret = 0;
1316 FsMountList mounts;
1317 struct FsMount *mount;
1318 int fd;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001319 Error *local_err = NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001320 struct fstrim_range r = {
1321 .start = 0,
1322 .len = -1,
1323 .minlen = has_minimum ? minimum : 0,
1324 };
1325
1326 slog("guest-fstrim called");
1327
1328 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001329 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001330 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001331 error_propagate(errp, local_err);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001332 return;
1333 }
1334
1335 QTAILQ_FOREACH(mount, &mounts, next) {
1336 fd = qemu_open(mount->dirname, O_RDONLY);
1337 if (fd == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001338 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001339 goto error;
1340 }
1341
1342 /* We try to cull filesytems we know won't work in advance, but other
1343 * filesytems may not implement fstrim for less obvious reasons. These
1344 * will report EOPNOTSUPP; we simply ignore these errors. Any other
1345 * error means an unexpected error, so return it in those cases. In
1346 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
1347 */
1348 ret = ioctl(fd, FITRIM, &r);
1349 if (ret == -1) {
1350 if (errno != ENOTTY && errno != EOPNOTSUPP) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001351 error_setg_errno(errp, errno, "failed to trim %s",
Luiz Capitulino071673b2012-11-27 11:02:01 -02001352 mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001353 close(fd);
1354 goto error;
1355 }
1356 }
1357 close(fd);
1358 }
1359
1360error:
1361 free_fs_mount_list(&mounts);
1362}
1363#endif /* CONFIG_FSTRIM */
1364
1365
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001366#define LINUX_SYS_STATE_FILE "/sys/power/state"
1367#define SUSPEND_SUPPORTED 0
1368#define SUSPEND_NOT_SUPPORTED 1
1369
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001370static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
Markus Armbruster77dbc812014-05-02 13:26:30 +02001371 const char *sysfile_str, Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001372{
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001373 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001374 char *pmutils_path;
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001375 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001376 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001377
1378 pmutils_path = g_find_program_in_path(pmutils_bin);
1379
1380 pid = fork();
1381 if (!pid) {
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001382 char buf[32]; /* hopefully big enough */
1383 ssize_t ret;
1384 int fd;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001385
1386 setsid();
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001387 reopen_fd_to_null(0);
1388 reopen_fd_to_null(1);
1389 reopen_fd_to_null(2);
1390
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001391 if (pmutils_path) {
1392 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
1393 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001394
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001395 /*
1396 * If we get here either pm-utils is not installed or execle() has
1397 * failed. Let's try the manual method if the caller wants it.
1398 */
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001399
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001400 if (!sysfile_str) {
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001401 _exit(SUSPEND_NOT_SUPPORTED);
1402 }
1403
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001404 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1405 if (fd < 0) {
1406 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001407 }
1408
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001409 ret = read(fd, buf, sizeof(buf)-1);
1410 if (ret <= 0) {
1411 _exit(SUSPEND_NOT_SUPPORTED);
1412 }
1413 buf[ret] = '\0';
1414
1415 if (strstr(buf, sysfile_str)) {
1416 _exit(SUSPEND_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001417 }
1418
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001419 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001420 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001421 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001422 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001423 }
1424
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001425 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001426 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001427 error_propagate(errp, local_err);
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001428 goto out;
1429 }
1430
1431 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001432 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001433 goto out;
1434 }
1435
1436 switch (WEXITSTATUS(status)) {
1437 case SUSPEND_SUPPORTED:
1438 goto out;
1439 case SUSPEND_NOT_SUPPORTED:
Markus Armbruster77dbc812014-05-02 13:26:30 +02001440 error_setg(errp,
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001441 "the requested suspend mode is not supported by the guest");
1442 goto out;
1443 default:
Markus Armbruster77dbc812014-05-02 13:26:30 +02001444 error_setg(errp,
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001445 "the helper program '%s' returned an unexpected exit status"
1446 " code (%d)", pmutils_path, WEXITSTATUS(status));
1447 goto out;
1448 }
1449
1450out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001451 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001452}
1453
1454static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
Markus Armbruster77dbc812014-05-02 13:26:30 +02001455 Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001456{
Luiz Capitulino7b376082012-11-27 11:02:04 -02001457 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001458 char *pmutils_path;
Luiz Capitulino7b376082012-11-27 11:02:04 -02001459 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001460 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001461
1462 pmutils_path = g_find_program_in_path(pmutils_bin);
1463
1464 pid = fork();
1465 if (pid == 0) {
1466 /* child */
1467 int fd;
1468
1469 setsid();
1470 reopen_fd_to_null(0);
1471 reopen_fd_to_null(1);
1472 reopen_fd_to_null(2);
1473
1474 if (pmutils_path) {
1475 execle(pmutils_path, pmutils_bin, NULL, environ);
1476 }
1477
1478 /*
1479 * If we get here either pm-utils is not installed or execle() has
1480 * failed. Let's try the manual method if the caller wants it.
1481 */
1482
1483 if (!sysfile_str) {
1484 _exit(EXIT_FAILURE);
1485 }
1486
1487 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1488 if (fd < 0) {
1489 _exit(EXIT_FAILURE);
1490 }
1491
1492 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1493 _exit(EXIT_FAILURE);
1494 }
1495
1496 _exit(EXIT_SUCCESS);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001497 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001498 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001499 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001500 }
1501
Luiz Capitulino7b376082012-11-27 11:02:04 -02001502 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001503 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001504 error_propagate(errp, local_err);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001505 goto out;
1506 }
1507
1508 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001509 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001510 goto out;
1511 }
1512
1513 if (WEXITSTATUS(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001514 error_setg(errp, "child process has failed to suspend");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001515 goto out;
1516 }
1517
1518out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001519 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001520}
1521
Markus Armbruster77dbc812014-05-02 13:26:30 +02001522void qmp_guest_suspend_disk(Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001523{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001524 Error *local_err = NULL;
1525
1526 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err);
1527 if (local_err) {
1528 error_propagate(errp, local_err);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001529 return;
1530 }
1531
Markus Armbruster77dbc812014-05-02 13:26:30 +02001532 guest_suspend("pm-hibernate", "disk", errp);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001533}
1534
Markus Armbruster77dbc812014-05-02 13:26:30 +02001535void qmp_guest_suspend_ram(Error **errp)
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001536{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001537 Error *local_err = NULL;
1538
1539 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err);
1540 if (local_err) {
1541 error_propagate(errp, local_err);
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001542 return;
1543 }
1544
Markus Armbruster77dbc812014-05-02 13:26:30 +02001545 guest_suspend("pm-suspend", "mem", errp);
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001546}
1547
Markus Armbruster77dbc812014-05-02 13:26:30 +02001548void qmp_guest_suspend_hybrid(Error **errp)
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001549{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001550 Error *local_err = NULL;
1551
1552 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL,
1553 &local_err);
1554 if (local_err) {
1555 error_propagate(errp, local_err);
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001556 return;
1557 }
1558
Markus Armbruster77dbc812014-05-02 13:26:30 +02001559 guest_suspend("pm-suspend-hybrid", NULL, errp);
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001560}
1561
Michal Privoznik3424fc92012-02-29 17:02:23 +01001562static GuestNetworkInterfaceList *
1563guest_find_interface(GuestNetworkInterfaceList *head,
1564 const char *name)
1565{
1566 for (; head; head = head->next) {
1567 if (strcmp(head->value->name, name) == 0) {
1568 break;
1569 }
1570 }
1571
1572 return head;
1573}
1574
1575/*
1576 * Build information about guest interfaces
1577 */
1578GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1579{
1580 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1581 struct ifaddrs *ifap, *ifa;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001582
1583 if (getifaddrs(&ifap) < 0) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001584 error_setg_errno(errp, errno, "getifaddrs failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001585 goto error;
1586 }
1587
1588 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1589 GuestNetworkInterfaceList *info;
1590 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1591 char addr4[INET_ADDRSTRLEN];
1592 char addr6[INET6_ADDRSTRLEN];
1593 int sock;
1594 struct ifreq ifr;
1595 unsigned char *mac_addr;
1596 void *p;
1597
1598 g_debug("Processing %s interface", ifa->ifa_name);
1599
1600 info = guest_find_interface(head, ifa->ifa_name);
1601
1602 if (!info) {
1603 info = g_malloc0(sizeof(*info));
1604 info->value = g_malloc0(sizeof(*info->value));
1605 info->value->name = g_strdup(ifa->ifa_name);
1606
1607 if (!cur_item) {
1608 head = cur_item = info;
1609 } else {
1610 cur_item->next = info;
1611 cur_item = info;
1612 }
1613 }
1614
1615 if (!info->value->has_hardware_address &&
1616 ifa->ifa_flags & SIOCGIFHWADDR) {
1617 /* we haven't obtained HW address yet */
1618 sock = socket(PF_INET, SOCK_STREAM, 0);
1619 if (sock == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001620 error_setg_errno(errp, errno, "failed to create socket");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001621 goto error;
1622 }
1623
1624 memset(&ifr, 0, sizeof(ifr));
Jim Meyering1ab516e2012-10-04 13:09:58 +02001625 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001626 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001627 error_setg_errno(errp, errno,
1628 "failed to get MAC address of %s",
1629 ifa->ifa_name);
Markus Armbruster10a21582013-01-16 18:15:09 +01001630 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001631 goto error;
1632 }
1633
Markus Armbruster10a21582013-01-16 18:15:09 +01001634 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001635 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1636
Stefan Weile4ada482013-01-16 18:37:23 +01001637 info->value->hardware_address =
1638 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1639 (int) mac_addr[0], (int) mac_addr[1],
1640 (int) mac_addr[2], (int) mac_addr[3],
1641 (int) mac_addr[4], (int) mac_addr[5]);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001642
1643 info->value->has_hardware_address = true;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001644 }
1645
1646 if (ifa->ifa_addr &&
1647 ifa->ifa_addr->sa_family == AF_INET) {
1648 /* interface with IPv4 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001649 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1650 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001651 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001652 goto error;
1653 }
1654
Markus Armbruster10a21582013-01-16 18:15:09 +01001655 address_item = g_malloc0(sizeof(*address_item));
1656 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001657 address_item->value->ip_address = g_strdup(addr4);
1658 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1659
1660 if (ifa->ifa_netmask) {
1661 /* Count the number of set bits in netmask.
1662 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1663 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1664 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1665 }
1666 } else if (ifa->ifa_addr &&
1667 ifa->ifa_addr->sa_family == AF_INET6) {
1668 /* interface with IPv6 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001669 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1670 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001671 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001672 goto error;
1673 }
1674
Markus Armbruster10a21582013-01-16 18:15:09 +01001675 address_item = g_malloc0(sizeof(*address_item));
1676 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001677 address_item->value->ip_address = g_strdup(addr6);
1678 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1679
1680 if (ifa->ifa_netmask) {
1681 /* Count the number of set bits in netmask.
1682 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1683 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1684 address_item->value->prefix =
1685 ctpop32(((uint32_t *) p)[0]) +
1686 ctpop32(((uint32_t *) p)[1]) +
1687 ctpop32(((uint32_t *) p)[2]) +
1688 ctpop32(((uint32_t *) p)[3]);
1689 }
1690 }
1691
1692 if (!address_item) {
1693 continue;
1694 }
1695
1696 address_list = &info->value->ip_addresses;
1697
1698 while (*address_list && (*address_list)->next) {
1699 address_list = &(*address_list)->next;
1700 }
1701
1702 if (!*address_list) {
1703 *address_list = address_item;
1704 } else {
1705 (*address_list)->next = address_item;
1706 }
1707
1708 info->value->has_ip_addresses = true;
1709
1710
1711 }
1712
1713 freeifaddrs(ifap);
1714 return head;
1715
1716error:
1717 freeifaddrs(ifap);
1718 qapi_free_GuestNetworkInterfaceList(head);
1719 return NULL;
1720}
1721
Markus Armbruster77dbc812014-05-02 13:26:30 +02001722#define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001723
Markus Armbruster77dbc812014-05-02 13:26:30 +02001724static long sysconf_exact(int name, const char *name_str, Error **errp)
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001725{
1726 long ret;
1727
1728 errno = 0;
1729 ret = sysconf(name);
1730 if (ret == -1) {
1731 if (errno == 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001732 error_setg(errp, "sysconf(%s): value indefinite", name_str);
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001733 } else {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001734 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001735 }
1736 }
1737 return ret;
1738}
1739
1740/* Transfer online/offline status between @vcpu and the guest system.
1741 *
1742 * On input either @errp or *@errp must be NULL.
1743 *
1744 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1745 * - R: vcpu->logical_id
1746 * - W: vcpu->online
1747 * - W: vcpu->can_offline
1748 *
1749 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1750 * - R: vcpu->logical_id
1751 * - R: vcpu->online
1752 *
1753 * Written members remain unmodified on error.
1754 */
1755static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1756 Error **errp)
1757{
1758 char *dirpath;
1759 int dirfd;
1760
1761 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1762 vcpu->logical_id);
1763 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1764 if (dirfd == -1) {
1765 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1766 } else {
1767 static const char fn[] = "online";
1768 int fd;
1769 int res;
1770
1771 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1772 if (fd == -1) {
1773 if (errno != ENOENT) {
1774 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1775 } else if (sys2vcpu) {
1776 vcpu->online = true;
1777 vcpu->can_offline = false;
1778 } else if (!vcpu->online) {
1779 error_setg(errp, "logical processor #%" PRId64 " can't be "
1780 "offlined", vcpu->logical_id);
1781 } /* otherwise pretend successful re-onlining */
1782 } else {
1783 unsigned char status;
1784
1785 res = pread(fd, &status, 1, 0);
1786 if (res == -1) {
1787 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1788 } else if (res == 0) {
1789 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1790 fn);
1791 } else if (sys2vcpu) {
1792 vcpu->online = (status != '0');
1793 vcpu->can_offline = true;
1794 } else if (vcpu->online != (status != '0')) {
1795 status = '0' + vcpu->online;
1796 if (pwrite(fd, &status, 1, 0) == -1) {
1797 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1798 fn);
1799 }
1800 } /* otherwise pretend successful re-(on|off)-lining */
1801
1802 res = close(fd);
1803 g_assert(res == 0);
1804 }
1805
1806 res = close(dirfd);
1807 g_assert(res == 0);
1808 }
1809
1810 g_free(dirpath);
1811}
1812
1813GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1814{
1815 int64_t current;
1816 GuestLogicalProcessorList *head, **link;
1817 long sc_max;
1818 Error *local_err = NULL;
1819
1820 current = 0;
1821 head = NULL;
1822 link = &head;
1823 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1824
1825 while (local_err == NULL && current < sc_max) {
1826 GuestLogicalProcessor *vcpu;
1827 GuestLogicalProcessorList *entry;
1828
1829 vcpu = g_malloc0(sizeof *vcpu);
1830 vcpu->logical_id = current++;
1831 vcpu->has_can_offline = true; /* lolspeak ftw */
1832 transfer_vcpu(vcpu, true, &local_err);
1833
1834 entry = g_malloc0(sizeof *entry);
1835 entry->value = vcpu;
1836
1837 *link = entry;
1838 link = &entry->next;
1839 }
1840
1841 if (local_err == NULL) {
1842 /* there's no guest with zero VCPUs */
1843 g_assert(head != NULL);
1844 return head;
1845 }
1846
1847 qapi_free_GuestLogicalProcessorList(head);
1848 error_propagate(errp, local_err);
1849 return NULL;
1850}
1851
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001852int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1853{
1854 int64_t processed;
1855 Error *local_err = NULL;
1856
1857 processed = 0;
1858 while (vcpus != NULL) {
1859 transfer_vcpu(vcpus->value, false, &local_err);
1860 if (local_err != NULL) {
1861 break;
1862 }
1863 ++processed;
1864 vcpus = vcpus->next;
1865 }
1866
1867 if (local_err != NULL) {
1868 if (processed == 0) {
1869 error_propagate(errp, local_err);
1870 } else {
1871 error_free(local_err);
1872 }
1873 }
1874
1875 return processed;
1876}
1877
Michael Rothe72c3f22012-03-25 13:59:41 -05001878#else /* defined(__linux__) */
1879
Markus Armbruster77dbc812014-05-02 13:26:30 +02001880void qmp_guest_suspend_disk(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05001881{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001882 error_set(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05001883}
1884
Markus Armbruster77dbc812014-05-02 13:26:30 +02001885void qmp_guest_suspend_ram(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05001886{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001887 error_set(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05001888}
1889
Markus Armbruster77dbc812014-05-02 13:26:30 +02001890void qmp_guest_suspend_hybrid(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05001891{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001892 error_set(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05001893}
1894
1895GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1896{
1897 error_set(errp, QERR_UNSUPPORTED);
1898 return NULL;
1899}
1900
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001901GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1902{
1903 error_set(errp, QERR_UNSUPPORTED);
1904 return NULL;
1905}
1906
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001907int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1908{
1909 error_set(errp, QERR_UNSUPPORTED);
1910 return -1;
1911}
1912
Michael Rothe72c3f22012-03-25 13:59:41 -05001913#endif
1914
Michael Rothd35d4cb2012-04-13 21:07:36 -05001915#if !defined(CONFIG_FSFREEZE)
1916
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001917GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1918{
1919 error_set(errp, QERR_UNSUPPORTED);
1920 return NULL;
1921}
1922
Markus Armbruster77dbc812014-05-02 13:26:30 +02001923GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05001924{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001925 error_set(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05001926
1927 return 0;
1928}
1929
Markus Armbruster77dbc812014-05-02 13:26:30 +02001930int64_t qmp_guest_fsfreeze_freeze(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05001931{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001932 error_set(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05001933
1934 return 0;
1935}
1936
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001937int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1938 strList *mountpoints,
1939 Error **errp)
1940{
1941 error_set(errp, QERR_UNSUPPORTED);
1942
1943 return 0;
1944}
1945
Markus Armbruster77dbc812014-05-02 13:26:30 +02001946int64_t qmp_guest_fsfreeze_thaw(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05001947{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001948 error_set(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05001949
1950 return 0;
1951}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001952#endif /* CONFIG_FSFREEZE */
Michael Rothd35d4cb2012-04-13 21:07:36 -05001953
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001954#if !defined(CONFIG_FSTRIM)
Markus Armbruster77dbc812014-05-02 13:26:30 +02001955void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001956{
Markus Armbruster77dbc812014-05-02 13:26:30 +02001957 error_set(errp, QERR_UNSUPPORTED);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001958}
Michael Rothd35d4cb2012-04-13 21:07:36 -05001959#endif
1960
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04001961/* add unsupported commands to the blacklist */
1962GList *ga_command_blacklist_init(GList *blacklist)
1963{
1964#if !defined(__linux__)
1965 {
1966 const char *list[] = {
1967 "guest-suspend-disk", "guest-suspend-ram",
1968 "guest-suspend-hybrid", "guest-network-get-interfaces",
1969 "guest-get-vcpus", "guest-set-vcpus", NULL};
1970 char **p = (char **)list;
1971
1972 while (*p) {
1973 blacklist = g_list_append(blacklist, *p++);
1974 }
1975 }
1976#endif
1977
1978#if !defined(CONFIG_FSFREEZE)
1979 {
1980 const char *list[] = {
1981 "guest-get-fsinfo", "guest-fsfreeze-status",
1982 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
1983 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
1984 char **p = (char **)list;
1985
1986 while (*p) {
1987 blacklist = g_list_append(blacklist, *p++);
1988 }
1989 }
1990#endif
1991
1992#if !defined(CONFIG_FSTRIM)
1993 blacklist = g_list_append(blacklist, (char *)"guest-fstrim");
1994#endif
1995
1996 return blacklist;
1997}
1998
Michael Rothe3d4d252011-07-19 15:41:55 -05001999/* register init/cleanup routines for stateful command groups */
2000void ga_command_state_init(GAState *s, GACommandState *cs)
2001{
Anthony Liguori7006b9c2011-07-22 14:14:17 -05002002#if defined(CONFIG_FSFREEZE)
Michael Rothf22d85e2012-04-17 19:01:45 -05002003 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
Anthony Liguori7006b9c2011-07-22 14:14:17 -05002004#endif
Michael Rothe3d4d252011-07-19 15:41:55 -05002005 ga_command_state_add(cs, guest_file_init, NULL);
2006}