blob: 2ae37255d45031c343eb8835bb3d1eb7536e0d17 [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
Peter Maydell4459bf32016-01-29 17:49:58 +000014#include "qemu/osdep.h"
Michael Rothe3d4d252011-07-19 15:41:55 -050015#include <glib.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050016#include <sys/ioctl.h>
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030017#include <sys/wait.h>
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -040018#include <dirent.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050019#include "qga/guest-agent-core.h"
20#include "qga-qmp-commands.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010021#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/queue.h"
23#include "qemu/host-utils.h"
Denis V. Lunev12505392015-10-28 18:13:55 +030024#include "qemu/sockets.h"
Daniel P. Berrange920639c2015-11-23 15:37:07 +000025#include "qemu/base64.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Michael Rothe3d4d252011-07-19 15:41:55 -050027
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030028#ifndef CONFIG_HAS_ENVIRON
Andreas Färbereecae142012-05-27 17:02:20 +020029#ifdef __APPLE__
30#include <crt_externs.h>
31#define environ (*_NSGetEnviron())
32#else
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030033extern char **environ;
34#endif
Andreas Färbereecae142012-05-27 17:02:20 +020035#endif
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030036
Michael Rothe72c3f22012-03-25 13:59:41 -050037#if defined(__linux__)
38#include <mntent.h>
39#include <linux/fs.h>
40#include <ifaddrs.h>
41#include <arpa/inet.h>
42#include <sys/socket.h>
43#include <net/if.h>
Michael Rothe72c3f22012-03-25 13:59:41 -050044
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020045#ifdef FIFREEZE
Michael Rothe72c3f22012-03-25 13:59:41 -050046#define CONFIG_FSFREEZE
47#endif
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020048#ifdef FITRIM
49#define CONFIG_FSTRIM
50#endif
Michael Rothe72c3f22012-03-25 13:59:41 -050051#endif
52
Markus Armbruster77dbc812014-05-02 13:26:30 +020053static void ga_wait_child(pid_t pid, int *status, Error **errp)
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020054{
55 pid_t rpid;
56
57 *status = 0;
58
59 do {
60 rpid = waitpid(pid, status, 0);
61 } while (rpid == -1 && errno == EINTR);
62
63 if (rpid == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +020064 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
65 pid);
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020066 return;
67 }
68
69 g_assert(rpid == pid);
70}
71
Markus Armbruster77dbc812014-05-02 13:26:30 +020072void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -050073{
Michael Rothe3d4d252011-07-19 15:41:55 -050074 const char *shutdown_flag;
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020075 Error *local_err = NULL;
76 pid_t pid;
Luiz Capitulino36748382012-05-14 15:25:20 -030077 int status;
Michael Rothe3d4d252011-07-19 15:41:55 -050078
79 slog("guest-shutdown called, mode: %s", mode);
80 if (!has_mode || strcmp(mode, "powerdown") == 0) {
81 shutdown_flag = "-P";
82 } else if (strcmp(mode, "halt") == 0) {
83 shutdown_flag = "-H";
84 } else if (strcmp(mode, "reboot") == 0) {
85 shutdown_flag = "-r";
86 } else {
Markus Armbruster77dbc812014-05-02 13:26:30 +020087 error_setg(errp,
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020088 "mode is invalid (valid values are: halt|powerdown|reboot");
Michael Rothe3d4d252011-07-19 15:41:55 -050089 return;
90 }
91
Luiz Capitulinod5dd3492012-05-11 16:19:47 -030092 pid = fork();
93 if (pid == 0) {
Michael Rothe3d4d252011-07-19 15:41:55 -050094 /* child, start the shutdown */
95 setsid();
Luiz Capitulino36748382012-05-14 15:25:20 -030096 reopen_fd_to_null(0);
97 reopen_fd_to_null(1);
98 reopen_fd_to_null(2);
Michael Rothe3d4d252011-07-19 15:41:55 -050099
whitearchey485e7412013-11-06 10:54:04 +0900100 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
Luiz Capitulino36748382012-05-14 15:25:20 -0300101 "hypervisor initiated shutdown", (char*)NULL, environ);
102 _exit(EXIT_FAILURE);
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300103 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200104 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300105 return;
106 }
107
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200108 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100109 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200110 error_propagate(errp, local_err);
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200111 return;
112 }
113
114 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200115 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200116 return;
117 }
118
119 if (WEXITSTATUS(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200120 error_setg(errp, "child process has failed to shutdown");
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200121 return;
122 }
123
Peter Maydell085d8132013-03-18 17:20:07 +0000124 /* succeeded */
Michael Rothe3d4d252011-07-19 15:41:55 -0500125}
126
Lei Li6912e6a2013-03-05 17:39:11 +0800127int64_t qmp_guest_get_time(Error **errp)
128{
129 int ret;
130 qemu_timeval tq;
131 int64_t time_ns;
132
133 ret = qemu_gettimeofday(&tq);
134 if (ret < 0) {
135 error_setg_errno(errp, errno, "Failed to get time");
136 return -1;
137 }
138
139 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
140 return time_ns;
141}
142
Michal Privoznik2c958922014-01-31 11:29:51 +0100143void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
Lei Lia1bca572013-03-05 17:39:12 +0800144{
145 int ret;
146 int status;
147 pid_t pid;
148 Error *local_err = NULL;
149 struct timeval tv;
150
Michal Privoznik2c958922014-01-31 11:29:51 +0100151 /* If user has passed a time, validate and set it. */
152 if (has_time) {
Marc-André Lureau00d2f372015-07-05 16:28:58 +0200153 GDate date = { 0, };
154
Michal Privoznik2c958922014-01-31 11:29:51 +0100155 /* year-2038 will overflow in case time_t is 32bit */
156 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
157 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
158 return;
159 }
160
161 tv.tv_sec = time_ns / 1000000000;
162 tv.tv_usec = (time_ns % 1000000000) / 1000;
Marc-André Lureau00d2f372015-07-05 16:28:58 +0200163 g_date_set_time_t(&date, tv.tv_sec);
164 if (date.year < 1970 || date.year >= 2070) {
165 error_setg_errno(errp, errno, "Invalid time");
166 return;
167 }
Michal Privoznik2c958922014-01-31 11:29:51 +0100168
169 ret = settimeofday(&tv, NULL);
170 if (ret < 0) {
171 error_setg_errno(errp, errno, "Failed to set time to guest");
172 return;
173 }
Lei Lia1bca572013-03-05 17:39:12 +0800174 }
175
Michal Privoznik2c958922014-01-31 11:29:51 +0100176 /* Now, if user has passed a time to set and the system time is set, we
177 * just need to synchronize the hardware clock. However, if no time was
178 * passed, user is requesting the opposite: set the system time from the
Amos Kong1634df52014-04-04 23:25:02 +0800179 * hardware clock (RTC). */
Lei Lia1bca572013-03-05 17:39:12 +0800180 pid = fork();
181 if (pid == 0) {
182 setsid();
183 reopen_fd_to_null(0);
184 reopen_fd_to_null(1);
185 reopen_fd_to_null(2);
186
Michal Privoznik2c958922014-01-31 11:29:51 +0100187 /* Use '/sbin/hwclock -w' to set RTC from the system time,
188 * or '/sbin/hwclock -s' to set the system time from RTC. */
189 execle("/sbin/hwclock", "hwclock", has_time ? "-w" : "-s",
190 NULL, environ);
Lei Lia1bca572013-03-05 17:39:12 +0800191 _exit(EXIT_FAILURE);
192 } else if (pid < 0) {
193 error_setg_errno(errp, errno, "failed to create child process");
194 return;
195 }
196
197 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100198 if (local_err) {
Lei Lia1bca572013-03-05 17:39:12 +0800199 error_propagate(errp, local_err);
200 return;
201 }
202
203 if (!WIFEXITED(status)) {
204 error_setg(errp, "child process has terminated abnormally");
205 return;
206 }
207
208 if (WEXITSTATUS(status)) {
209 error_setg(errp, "hwclock failed to set hardware clock to system time");
210 return;
211 }
212}
213
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100214typedef enum {
215 RW_STATE_NEW,
216 RW_STATE_READING,
217 RW_STATE_WRITING,
218} RwState;
219
Michael Rothe3d4d252011-07-19 15:41:55 -0500220typedef struct GuestFileHandle {
221 uint64_t id;
222 FILE *fh;
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100223 RwState state;
Michael Rothe3d4d252011-07-19 15:41:55 -0500224 QTAILQ_ENTRY(GuestFileHandle) next;
225} GuestFileHandle;
226
227static struct {
228 QTAILQ_HEAD(, GuestFileHandle) filehandles;
Denis V. Lunevb4fe97c2015-10-13 18:41:19 +0300229} guest_file_state = {
230 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
231};
Michael Rothe3d4d252011-07-19 15:41:55 -0500232
Michael Roth39097da2013-03-01 11:40:27 -0600233static int64_t guest_file_handle_add(FILE *fh, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500234{
235 GuestFileHandle *gfh;
Michael Roth39097da2013-03-01 11:40:27 -0600236 int64_t handle;
237
238 handle = ga_get_fd_handle(ga_state, errp);
Markus Armbrustera903f402014-05-02 13:26:33 +0200239 if (handle < 0) {
240 return -1;
Michael Roth39097da2013-03-01 11:40:27 -0600241 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500242
Markus Armbrusterf3a06402015-09-14 13:50:44 +0200243 gfh = g_new0(GuestFileHandle, 1);
Michael Roth39097da2013-03-01 11:40:27 -0600244 gfh->id = handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500245 gfh->fh = fh;
246 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
Michael Roth39097da2013-03-01 11:40:27 -0600247
248 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500249}
250
Markus Armbruster77dbc812014-05-02 13:26:30 +0200251static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500252{
253 GuestFileHandle *gfh;
254
255 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
256 {
257 if (gfh->id == id) {
258 return gfh;
259 }
260 }
261
Markus Armbruster77dbc812014-05-02 13:26:30 +0200262 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
Michael Rothe3d4d252011-07-19 15:41:55 -0500263 return NULL;
264}
265
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200266typedef const char * const ccpc;
267
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200268#ifndef O_BINARY
269#define O_BINARY 0
270#endif
271
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200272/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
273static const struct {
274 ccpc *forms;
275 int oflag_base;
276} guest_file_open_modes[] = {
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200277 { (ccpc[]){ "r", NULL }, O_RDONLY },
278 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
279 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
280 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
281 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
282 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
283 { (ccpc[]){ "r+", NULL }, O_RDWR },
284 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
285 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
286 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
287 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
288 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200289};
290
291static int
Markus Armbruster77dbc812014-05-02 13:26:30 +0200292find_open_flag(const char *mode_str, Error **errp)
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200293{
294 unsigned mode;
295
296 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
297 ccpc *form;
298
299 form = guest_file_open_modes[mode].forms;
300 while (*form != NULL && strcmp(*form, mode_str) != 0) {
301 ++form;
302 }
303 if (*form != NULL) {
304 break;
305 }
306 }
307
308 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200309 error_setg(errp, "invalid file open mode '%s'", mode_str);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200310 return -1;
311 }
312 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
313}
314
315#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
316 S_IRGRP | S_IWGRP | \
317 S_IROTH | S_IWOTH)
318
319static FILE *
Markus Armbruster77dbc812014-05-02 13:26:30 +0200320safe_open_or_create(const char *path, const char *mode, Error **errp)
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200321{
322 Error *local_err = NULL;
323 int oflag;
324
325 oflag = find_open_flag(mode, &local_err);
326 if (local_err == NULL) {
327 int fd;
328
329 /* If the caller wants / allows creation of a new file, we implement it
330 * with a two step process: open() + (open() / fchmod()).
331 *
332 * First we insist on creating the file exclusively as a new file. If
333 * that succeeds, we're free to set any file-mode bits on it. (The
334 * motivation is that we want to set those file-mode bits independently
335 * of the current umask.)
336 *
337 * If the exclusive creation fails because the file already exists
338 * (EEXIST is not possible for any other reason), we just attempt to
339 * open the file, but in this case we won't be allowed to change the
340 * file-mode bits on the preexistent file.
341 *
342 * The pathname should never disappear between the two open()s in
343 * practice. If it happens, then someone very likely tried to race us.
344 * In this case just go ahead and report the ENOENT from the second
345 * open() to the caller.
346 *
347 * If the caller wants to open a preexistent file, then the first
348 * open() is decisive and its third argument is ignored, and the second
349 * open() and the fchmod() are never called.
350 */
351 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
352 if (fd == -1 && errno == EEXIST) {
353 oflag &= ~(unsigned)O_CREAT;
354 fd = open(path, oflag);
355 }
356
357 if (fd == -1) {
358 error_setg_errno(&local_err, errno, "failed to open file '%s' "
359 "(mode: '%s')", path, mode);
360 } else {
361 qemu_set_cloexec(fd);
362
363 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
364 error_setg_errno(&local_err, errno, "failed to set permission "
365 "0%03o on new file '%s' (mode: '%s')",
366 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
367 } else {
368 FILE *f;
369
370 f = fdopen(fd, mode);
371 if (f == NULL) {
372 error_setg_errno(&local_err, errno, "failed to associate "
373 "stdio stream with file descriptor %d, "
374 "file '%s' (mode: '%s')", fd, path, mode);
375 } else {
376 return f;
377 }
378 }
379
380 close(fd);
Laszlo Ersek2b720012013-05-08 17:31:36 +0200381 if (oflag & O_CREAT) {
382 unlink(path);
383 }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200384 }
385 }
386
Markus Armbruster77dbc812014-05-02 13:26:30 +0200387 error_propagate(errp, local_err);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200388 return NULL;
389}
390
Markus Armbruster77dbc812014-05-02 13:26:30 +0200391int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
392 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500393{
394 FILE *fh;
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200395 Error *local_err = NULL;
Simon Zolin85b6f6f2015-02-06 20:59:54 +0300396 int64_t handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500397
398 if (!has_mode) {
399 mode = "r";
400 }
401 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200402 fh = safe_open_or_create(path, mode, &local_err);
403 if (local_err != NULL) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200404 error_propagate(errp, local_err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500405 return -1;
406 }
407
408 /* set fd non-blocking to avoid common use cases (like reading from a
409 * named pipe) from hanging the agent
410 */
Denis V. Lunev12505392015-10-28 18:13:55 +0300411 qemu_set_nonblock(fileno(fh));
Michael Rothe3d4d252011-07-19 15:41:55 -0500412
Markus Armbruster77dbc812014-05-02 13:26:30 +0200413 handle = guest_file_handle_add(fh, errp);
Markus Armbrustera903f402014-05-02 13:26:33 +0200414 if (handle < 0) {
Michael Roth39097da2013-03-01 11:40:27 -0600415 fclose(fh);
416 return -1;
417 }
418
Stefan Weild607a522013-11-17 19:19:52 +0100419 slog("guest-file-open, handle: %" PRId64, handle);
Michael Roth39097da2013-03-01 11:40:27 -0600420 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500421}
422
Markus Armbruster77dbc812014-05-02 13:26:30 +0200423void qmp_guest_file_close(int64_t handle, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500424{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200425 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500426 int ret;
427
Stefan Weild607a522013-11-17 19:19:52 +0100428 slog("guest-file-close called, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500429 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500430 return;
431 }
432
433 ret = fclose(gfh->fh);
Luiz Capitulino3ac4b7c2012-11-27 11:01:56 -0200434 if (ret == EOF) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200435 error_setg_errno(errp, errno, "failed to close handle");
Michael Rothe3d4d252011-07-19 15:41:55 -0500436 return;
437 }
438
439 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500440 g_free(gfh);
Michael Rothe3d4d252011-07-19 15:41:55 -0500441}
442
443struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
Markus Armbruster77dbc812014-05-02 13:26:30 +0200444 int64_t count, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500445{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200446 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500447 GuestFileRead *read_data = NULL;
448 guchar *buf;
449 FILE *fh;
450 size_t read_count;
451
452 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500453 return NULL;
454 }
455
456 if (!has_count) {
457 count = QGA_READ_COUNT_DEFAULT;
458 } else if (count < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200459 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200460 count);
Michael Rothe3d4d252011-07-19 15:41:55 -0500461 return NULL;
462 }
463
464 fh = gfh->fh;
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100465
466 /* explicitly flush when switching from writing to reading */
467 if (gfh->state == RW_STATE_WRITING) {
468 int ret = fflush(fh);
469 if (ret == EOF) {
470 error_setg_errno(errp, errno, "failed to flush file");
471 return NULL;
472 }
473 gfh->state = RW_STATE_NEW;
474 }
475
Anthony Liguori7267c092011-08-20 22:09:37 -0500476 buf = g_malloc0(count+1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500477 read_count = fread(buf, 1, count, fh);
478 if (ferror(fh)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200479 error_setg_errno(errp, errno, "failed to read file");
Stefan Weild607a522013-11-17 19:19:52 +0100480 slog("guest-file-read failed, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500481 } else {
482 buf[read_count] = 0;
Markus Armbrusterf3a06402015-09-14 13:50:44 +0200483 read_data = g_new0(GuestFileRead, 1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500484 read_data->count = read_count;
485 read_data->eof = feof(fh);
486 if (read_count) {
487 read_data->buf_b64 = g_base64_encode(buf, read_count);
488 }
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100489 gfh->state = RW_STATE_READING;
Michael Rothe3d4d252011-07-19 15:41:55 -0500490 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500491 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500492 clearerr(fh);
493
494 return read_data;
495}
496
497GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
Markus Armbruster77dbc812014-05-02 13:26:30 +0200498 bool has_count, int64_t count,
499 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500500{
501 GuestFileWrite *write_data = NULL;
502 guchar *buf;
503 gsize buf_len;
504 int write_count;
Markus Armbruster77dbc812014-05-02 13:26:30 +0200505 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500506 FILE *fh;
507
508 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500509 return NULL;
510 }
511
512 fh = gfh->fh;
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100513
514 if (gfh->state == RW_STATE_READING) {
515 int ret = fseek(fh, 0, SEEK_CUR);
516 if (ret == -1) {
517 error_setg_errno(errp, errno, "failed to seek file");
518 return NULL;
519 }
520 gfh->state = RW_STATE_NEW;
521 }
522
Daniel P. Berrange920639c2015-11-23 15:37:07 +0000523 buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
524 if (!buf) {
525 return NULL;
526 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500527
528 if (!has_count) {
529 count = buf_len;
530 } else if (count < 0 || count > buf_len) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200531 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200532 count);
Anthony Liguori7267c092011-08-20 22:09:37 -0500533 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500534 return NULL;
535 }
536
537 write_count = fwrite(buf, 1, count, fh);
538 if (ferror(fh)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200539 error_setg_errno(errp, errno, "failed to write to file");
Stefan Weild607a522013-11-17 19:19:52 +0100540 slog("guest-file-write failed, handle: %" PRId64, handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500541 } else {
Markus Armbrusterf3a06402015-09-14 13:50:44 +0200542 write_data = g_new0(GuestFileWrite, 1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500543 write_data->count = write_count;
544 write_data->eof = feof(fh);
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100545 gfh->state = RW_STATE_WRITING;
Michael Rothe3d4d252011-07-19 15:41:55 -0500546 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500547 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500548 clearerr(fh);
549
550 return write_data;
551}
552
553struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
Eric Blake0b4b4932016-02-09 14:27:16 -0700554 GuestFileWhence *whence_code,
555 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500556{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200557 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500558 GuestFileSeek *seek_data = NULL;
559 FILE *fh;
560 int ret;
Eric Blake0a982b12015-11-25 10:37:15 -0700561 int whence;
Eric Blake0b4b4932016-02-09 14:27:16 -0700562 Error *err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500563
564 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500565 return NULL;
566 }
567
Eric Blake0a982b12015-11-25 10:37:15 -0700568 /* We stupidly exposed 'whence':'int' in our qapi */
Eric Blake0b4b4932016-02-09 14:27:16 -0700569 whence = ga_parse_whence(whence_code, &err);
570 if (err) {
571 error_propagate(errp, err);
Eric Blake0a982b12015-11-25 10:37:15 -0700572 return NULL;
573 }
574
Michael Rothe3d4d252011-07-19 15:41:55 -0500575 fh = gfh->fh;
576 ret = fseek(fh, offset, whence);
577 if (ret == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200578 error_setg_errno(errp, errno, "failed to seek file");
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100579 if (errno == ESPIPE) {
580 /* file is non-seekable, stdio shouldn't be buffering anyways */
581 gfh->state = RW_STATE_NEW;
582 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500583 } else {
Markus Armbruster10b7c5d2014-02-21 13:36:49 +0100584 seek_data = g_new0(GuestFileSeek, 1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500585 seek_data->position = ftell(fh);
586 seek_data->eof = feof(fh);
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100587 gfh->state = RW_STATE_NEW;
Michael Rothe3d4d252011-07-19 15:41:55 -0500588 }
589 clearerr(fh);
590
591 return seek_data;
592}
593
Markus Armbruster77dbc812014-05-02 13:26:30 +0200594void qmp_guest_file_flush(int64_t handle, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500595{
Markus Armbruster77dbc812014-05-02 13:26:30 +0200596 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500597 FILE *fh;
598 int ret;
599
600 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500601 return;
602 }
603
604 fh = gfh->fh;
605 ret = fflush(fh);
606 if (ret == EOF) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200607 error_setg_errno(errp, errno, "failed to flush file");
Marc-André Lureau895b00f2015-11-25 13:59:11 +0100608 } else {
609 gfh->state = RW_STATE_NEW;
Michael Rothe3d4d252011-07-19 15:41:55 -0500610 }
611}
612
Michael Rothe72c3f22012-03-25 13:59:41 -0500613/* linux-specific implementations. avoid this if at all possible. */
614#if defined(__linux__)
615
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200616#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
Paolo Bonziniaf022032012-06-13 07:41:27 +0200617typedef struct FsMount {
Michael Rothe3d4d252011-07-19 15:41:55 -0500618 char *dirname;
619 char *devtype;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400620 unsigned int devmajor, devminor;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200621 QTAILQ_ENTRY(FsMount) next;
622} FsMount;
Michael Rothe3d4d252011-07-19 15:41:55 -0500623
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -0400624typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
Michael Roth9e8aded432012-04-16 19:52:17 -0500625
Paolo Bonziniaf022032012-06-13 07:41:27 +0200626static void free_fs_mount_list(FsMountList *mounts)
Michael Roth9e8aded432012-04-16 19:52:17 -0500627{
Paolo Bonziniaf022032012-06-13 07:41:27 +0200628 FsMount *mount, *temp;
Michael Roth9e8aded432012-04-16 19:52:17 -0500629
630 if (!mounts) {
631 return;
632 }
633
634 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
635 QTAILQ_REMOVE(mounts, mount, next);
636 g_free(mount->dirname);
637 g_free(mount->devtype);
638 g_free(mount);
639 }
640}
641
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400642static int dev_major_minor(const char *devpath,
643 unsigned int *devmajor, unsigned int *devminor)
644{
645 struct stat st;
646
647 *devmajor = 0;
648 *devminor = 0;
649
650 if (stat(devpath, &st) < 0) {
651 slog("failed to stat device file '%s': %s", devpath, strerror(errno));
652 return -1;
653 }
654 if (S_ISDIR(st.st_mode)) {
655 /* It is bind mount */
656 return -2;
657 }
658 if (S_ISBLK(st.st_mode)) {
659 *devmajor = major(st.st_rdev);
660 *devminor = minor(st.st_rdev);
661 return 0;
662 }
663 return -1;
664}
665
Michael Rothe3d4d252011-07-19 15:41:55 -0500666/*
667 * Walk the mount table and build a list of local file systems
668 */
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400669static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500670{
671 struct mntent *ment;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200672 FsMount *mount;
Michael Roth9e2fa412012-05-29 10:08:50 -0500673 char const *mtab = "/proc/self/mounts";
Michael Rothe3d4d252011-07-19 15:41:55 -0500674 FILE *fp;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400675 unsigned int devmajor, devminor;
Michael Rothe3d4d252011-07-19 15:41:55 -0500676
Michael Rothe3d4d252011-07-19 15:41:55 -0500677 fp = setmntent(mtab, "r");
678 if (!fp) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200679 error_setg(errp, "failed to open mtab file: '%s'", mtab);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200680 return;
Michael Rothe3d4d252011-07-19 15:41:55 -0500681 }
682
683 while ((ment = getmntent(fp))) {
684 /*
685 * An entry which device name doesn't start with a '/' is
686 * either a dummy file system or a network file system.
687 * Add special handling for smbfs and cifs as is done by
688 * coreutils as well.
689 */
690 if ((ment->mnt_fsname[0] != '/') ||
691 (strcmp(ment->mnt_type, "smbfs") == 0) ||
692 (strcmp(ment->mnt_type, "cifs") == 0)) {
693 continue;
694 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400695 if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
696 /* Skip bind mounts */
697 continue;
698 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500699
Markus Armbrusterf3a06402015-09-14 13:50:44 +0200700 mount = g_new0(FsMount, 1);
Anthony Liguori7267c092011-08-20 22:09:37 -0500701 mount->dirname = g_strdup(ment->mnt_dir);
702 mount->devtype = g_strdup(ment->mnt_type);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400703 mount->devmajor = devmajor;
704 mount->devminor = devminor;
Michael Rothe3d4d252011-07-19 15:41:55 -0500705
Michael Roth9e8aded432012-04-16 19:52:17 -0500706 QTAILQ_INSERT_TAIL(mounts, mount, next);
Michael Rothe3d4d252011-07-19 15:41:55 -0500707 }
708
709 endmntent(fp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500710}
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400711
712static void decode_mntname(char *name, int len)
713{
714 int i, j = 0;
715 for (i = 0; i <= len; i++) {
716 if (name[i] != '\\') {
717 name[j++] = name[i];
718 } else if (name[i + 1] == '\\') {
719 name[j++] = '\\';
720 i++;
721 } else if (name[i + 1] >= '0' && name[i + 1] <= '3' &&
722 name[i + 2] >= '0' && name[i + 2] <= '7' &&
723 name[i + 3] >= '0' && name[i + 3] <= '7') {
724 name[j++] = (name[i + 1] - '0') * 64 +
725 (name[i + 2] - '0') * 8 +
726 (name[i + 3] - '0');
727 i += 3;
728 } else {
729 name[j++] = name[i];
730 }
731 }
732}
733
734static void build_fs_mount_list(FsMountList *mounts, Error **errp)
735{
736 FsMount *mount;
737 char const *mountinfo = "/proc/self/mountinfo";
738 FILE *fp;
739 char *line = NULL, *dash;
740 size_t n;
741 char check;
742 unsigned int devmajor, devminor;
743 int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
744
745 fp = fopen(mountinfo, "r");
746 if (!fp) {
747 build_fs_mount_list_from_mtab(mounts, errp);
748 return;
749 }
750
751 while (getline(&line, &n, fp) != -1) {
752 ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c",
753 &devmajor, &devminor, &dir_s, &dir_e, &check);
754 if (ret < 3) {
755 continue;
756 }
757 dash = strstr(line + dir_e, " - ");
758 if (!dash) {
759 continue;
760 }
761 ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
762 &type_s, &type_e, &dev_s, &dev_e, &check);
763 if (ret < 1) {
764 continue;
765 }
766 line[dir_e] = 0;
767 dash[type_e] = 0;
768 dash[dev_e] = 0;
769 decode_mntname(line + dir_s, dir_e - dir_s);
770 decode_mntname(dash + dev_s, dev_e - dev_s);
771 if (devmajor == 0) {
772 /* btrfs reports major number = 0 */
773 if (strcmp("btrfs", dash + type_s) != 0 ||
774 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
775 continue;
776 }
777 }
778
Markus Armbrusterf3a06402015-09-14 13:50:44 +0200779 mount = g_new0(FsMount, 1);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400780 mount->dirname = g_strdup(line + dir_s);
781 mount->devtype = g_strdup(dash + type_s);
782 mount->devmajor = devmajor;
783 mount->devminor = devminor;
784
785 QTAILQ_INSERT_TAIL(mounts, mount, next);
786 }
787 free(line);
788
789 fclose(fp);
790}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200791#endif
792
793#if defined(CONFIG_FSFREEZE)
Michael Rothe3d4d252011-07-19 15:41:55 -0500794
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -0400795static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
796{
797 char *path;
798 char *dpath;
799 char *driver = NULL;
800 char buf[PATH_MAX];
801 ssize_t len;
802
803 path = g_strndup(syspath, pathlen);
804 dpath = g_strdup_printf("%s/driver", path);
805 len = readlink(dpath, buf, sizeof(buf) - 1);
806 if (len != -1) {
807 buf[len] = 0;
808 driver = g_strdup(basename(buf));
809 }
810 g_free(dpath);
811 g_free(path);
812 return driver;
813}
814
815static int compare_uint(const void *_a, const void *_b)
816{
817 unsigned int a = *(unsigned int *)_a;
818 unsigned int b = *(unsigned int *)_b;
819
820 return a < b ? -1 : a > b ? 1 : 0;
821}
822
823/* Walk the specified sysfs and build a sorted list of host or ata numbers */
824static int build_hosts(char const *syspath, char const *host, bool ata,
825 unsigned int *hosts, int hosts_max, Error **errp)
826{
827 char *path;
828 DIR *dir;
829 struct dirent *entry;
830 int i = 0;
831
832 path = g_strndup(syspath, host - syspath);
833 dir = opendir(path);
834 if (!dir) {
835 error_setg_errno(errp, errno, "opendir(\"%s\")", path);
836 g_free(path);
837 return -1;
838 }
839
840 while (i < hosts_max) {
841 entry = readdir(dir);
842 if (!entry) {
843 break;
844 }
845 if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
846 ++i;
847 } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
848 ++i;
849 }
850 }
851
852 qsort(hosts, i, sizeof(hosts[0]), compare_uint);
853
854 g_free(path);
855 closedir(dir);
856 return i;
857}
858
859/* Store disk device info specified by @sysfs into @fs */
860static void build_guest_fsinfo_for_real_device(char const *syspath,
861 GuestFilesystemInfo *fs,
862 Error **errp)
863{
864 unsigned int pci[4], host, hosts[8], tgt[3];
865 int i, nhosts = 0, pcilen;
866 GuestDiskAddress *disk;
867 GuestPCIAddress *pciaddr;
868 GuestDiskAddressList *list = NULL;
869 bool has_ata = false, has_host = false, has_tgt = false;
870 char *p, *q, *driver = NULL;
871
872 p = strstr(syspath, "/devices/pci");
873 if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
874 pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
875 g_debug("only pci device is supported: sysfs path \"%s\"", syspath);
876 return;
877 }
878
879 driver = get_pci_driver(syspath, (p + 12 + pcilen) - syspath, errp);
880 if (!driver) {
881 goto cleanup;
882 }
883
884 p = strstr(syspath, "/target");
885 if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
886 tgt, tgt + 1, tgt + 2) == 3) {
887 has_tgt = true;
888 }
889
890 p = strstr(syspath, "/ata");
891 if (p) {
892 q = p + 4;
893 has_ata = true;
894 } else {
895 p = strstr(syspath, "/host");
896 q = p + 5;
897 }
898 if (p && sscanf(q, "%u", &host) == 1) {
899 has_host = true;
900 nhosts = build_hosts(syspath, p, has_ata, hosts,
901 sizeof(hosts) / sizeof(hosts[0]), errp);
902 if (nhosts < 0) {
903 goto cleanup;
904 }
905 }
906
907 pciaddr = g_malloc0(sizeof(*pciaddr));
908 pciaddr->domain = pci[0];
909 pciaddr->bus = pci[1];
910 pciaddr->slot = pci[2];
911 pciaddr->function = pci[3];
912
913 disk = g_malloc0(sizeof(*disk));
914 disk->pci_controller = pciaddr;
915
916 list = g_malloc0(sizeof(*list));
917 list->value = disk;
918
919 if (strcmp(driver, "ata_piix") == 0) {
920 /* a host per ide bus, target*:0:<unit>:0 */
921 if (!has_host || !has_tgt) {
922 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
923 goto cleanup;
924 }
925 for (i = 0; i < nhosts; i++) {
926 if (host == hosts[i]) {
927 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
928 disk->bus = i;
929 disk->unit = tgt[1];
930 break;
931 }
932 }
933 if (i >= nhosts) {
934 g_debug("no host for '%s' (driver '%s')", syspath, driver);
935 goto cleanup;
936 }
937 } else if (strcmp(driver, "sym53c8xx") == 0) {
938 /* scsi(LSI Logic): target*:0:<unit>:0 */
939 if (!has_tgt) {
940 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
941 goto cleanup;
942 }
943 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
944 disk->unit = tgt[1];
945 } else if (strcmp(driver, "virtio-pci") == 0) {
946 if (has_tgt) {
947 /* virtio-scsi: target*:0:0:<unit> */
948 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
949 disk->unit = tgt[2];
950 } else {
951 /* virtio-blk: 1 disk per 1 device */
952 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
953 }
954 } else if (strcmp(driver, "ahci") == 0) {
955 /* ahci: 1 host per 1 unit */
956 if (!has_host || !has_tgt) {
957 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
958 goto cleanup;
959 }
960 for (i = 0; i < nhosts; i++) {
961 if (host == hosts[i]) {
962 disk->unit = i;
963 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
964 break;
965 }
966 }
967 if (i >= nhosts) {
968 g_debug("no host for '%s' (driver '%s')", syspath, driver);
969 goto cleanup;
970 }
971 } else {
972 g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
973 goto cleanup;
974 }
975
976 list->next = fs->disk;
977 fs->disk = list;
978 g_free(driver);
979 return;
980
981cleanup:
982 if (list) {
983 qapi_free_GuestDiskAddressList(list);
984 }
985 g_free(driver);
986}
987
988static void build_guest_fsinfo_for_device(char const *devpath,
989 GuestFilesystemInfo *fs,
990 Error **errp);
991
992/* Store a list of slave devices of virtual volume specified by @syspath into
993 * @fs */
994static void build_guest_fsinfo_for_virtual_device(char const *syspath,
995 GuestFilesystemInfo *fs,
996 Error **errp)
997{
998 DIR *dir;
999 char *dirpath;
zhanghailiange668d1b2014-09-19 11:09:10 +08001000 struct dirent *entry;
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001001
1002 dirpath = g_strdup_printf("%s/slaves", syspath);
1003 dir = opendir(dirpath);
1004 if (!dir) {
1005 error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
1006 g_free(dirpath);
1007 return;
1008 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001009
1010 for (;;) {
zhanghailiange668d1b2014-09-19 11:09:10 +08001011 errno = 0;
1012 entry = readdir(dir);
1013 if (entry == NULL) {
1014 if (errno) {
1015 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
1016 }
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001017 break;
1018 }
1019
zhanghailiange668d1b2014-09-19 11:09:10 +08001020 if (entry->d_type == DT_LNK) {
1021 char *path;
1022
1023 g_debug(" slave device '%s'", entry->d_name);
1024 path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
1025 build_guest_fsinfo_for_device(path, fs, errp);
1026 g_free(path);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001027
1028 if (*errp) {
1029 break;
1030 }
1031 }
1032 }
1033
zhanghailiange668d1b2014-09-19 11:09:10 +08001034 g_free(dirpath);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04001035 closedir(dir);
1036}
1037
1038/* Dispatch to functions for virtual/real device */
1039static void build_guest_fsinfo_for_device(char const *devpath,
1040 GuestFilesystemInfo *fs,
1041 Error **errp)
1042{
1043 char *syspath = realpath(devpath, NULL);
1044
1045 if (!syspath) {
1046 error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1047 return;
1048 }
1049
1050 if (!fs->name) {
1051 fs->name = g_strdup(basename(syspath));
1052 }
1053
1054 g_debug(" parse sysfs path '%s'", syspath);
1055
1056 if (strstr(syspath, "/devices/virtual/block/")) {
1057 build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1058 } else {
1059 build_guest_fsinfo_for_real_device(syspath, fs, errp);
1060 }
1061
1062 free(syspath);
1063}
1064
1065/* Return a list of the disk device(s)' info which @mount lies on */
1066static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1067 Error **errp)
1068{
1069 GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1070 char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1071 mount->devmajor, mount->devminor);
1072
1073 fs->mountpoint = g_strdup(mount->dirname);
1074 fs->type = g_strdup(mount->devtype);
1075 build_guest_fsinfo_for_device(devpath, fs, errp);
1076
1077 g_free(devpath);
1078 return fs;
1079}
1080
1081GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1082{
1083 FsMountList mounts;
1084 struct FsMount *mount;
1085 GuestFilesystemInfoList *new, *ret = NULL;
1086 Error *local_err = NULL;
1087
1088 QTAILQ_INIT(&mounts);
1089 build_fs_mount_list(&mounts, &local_err);
1090 if (local_err) {
1091 error_propagate(errp, local_err);
1092 return NULL;
1093 }
1094
1095 QTAILQ_FOREACH(mount, &mounts, next) {
1096 g_debug("Building guest fsinfo for '%s'", mount->dirname);
1097
1098 new = g_malloc0(sizeof(*ret));
1099 new->value = build_guest_fsinfo(mount, &local_err);
1100 new->next = ret;
1101 ret = new;
1102 if (local_err) {
1103 error_propagate(errp, local_err);
1104 qapi_free_GuestFilesystemInfoList(ret);
1105 ret = NULL;
1106 break;
1107 }
1108 }
1109
1110 free_fs_mount_list(&mounts);
1111 return ret;
1112}
1113
1114
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001115typedef enum {
1116 FSFREEZE_HOOK_THAW = 0,
1117 FSFREEZE_HOOK_FREEZE,
1118} FsfreezeHookArg;
1119
Stefan Weil13a439e2014-07-07 21:07:29 +02001120static const char *fsfreeze_hook_arg_string[] = {
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001121 "thaw",
1122 "freeze",
1123};
1124
Markus Armbruster77dbc812014-05-02 13:26:30 +02001125static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001126{
1127 int status;
1128 pid_t pid;
1129 const char *hook;
1130 const char *arg_str = fsfreeze_hook_arg_string[arg];
1131 Error *local_err = NULL;
1132
1133 hook = ga_fsfreeze_hook(ga_state);
1134 if (!hook) {
1135 return;
1136 }
1137 if (access(hook, X_OK) != 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001138 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001139 return;
1140 }
1141
1142 slog("executing fsfreeze hook with arg '%s'", arg_str);
1143 pid = fork();
1144 if (pid == 0) {
1145 setsid();
1146 reopen_fd_to_null(0);
1147 reopen_fd_to_null(1);
1148 reopen_fd_to_null(2);
1149
1150 execle(hook, hook, arg_str, NULL, environ);
1151 _exit(EXIT_FAILURE);
1152 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001153 error_setg_errno(errp, errno, "failed to create child process");
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001154 return;
1155 }
1156
1157 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001158 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001159 error_propagate(errp, local_err);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001160 return;
1161 }
1162
1163 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001164 error_setg(errp, "fsfreeze hook has terminated abnormally");
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001165 return;
1166 }
1167
1168 status = WEXITSTATUS(status);
1169 if (status) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001170 error_setg(errp, "fsfreeze hook has failed with status %d", status);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001171 return;
1172 }
1173}
1174
Michael Rothe3d4d252011-07-19 15:41:55 -05001175/*
1176 * Return status of freeze/thaw
1177 */
Markus Armbruster77dbc812014-05-02 13:26:30 +02001178GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001179{
Michael Rothf22d85e2012-04-17 19:01:45 -05001180 if (ga_is_frozen(ga_state)) {
1181 return GUEST_FSFREEZE_STATUS_FROZEN;
1182 }
1183
1184 return GUEST_FSFREEZE_STATUS_THAWED;
Michael Rothe3d4d252011-07-19 15:41:55 -05001185}
1186
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001187int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1188{
1189 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1190}
1191
Michael Rothe3d4d252011-07-19 15:41:55 -05001192/*
1193 * Walk list of mounted file systems in the guest, and freeze the ones which
1194 * are real local file systems.
1195 */
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001196int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1197 strList *mountpoints,
1198 Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001199{
1200 int ret = 0, i = 0;
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001201 strList *list;
Paolo Bonziniaf022032012-06-13 07:41:27 +02001202 FsMountList mounts;
1203 struct FsMount *mount;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001204 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -05001205 int fd;
Michael Rothe3d4d252011-07-19 15:41:55 -05001206
1207 slog("guest-fsfreeze called");
1208
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001209 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001210 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001211 error_propagate(errp, local_err);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001212 return -1;
1213 }
1214
Michael Roth9e8aded432012-04-16 19:52:17 -05001215 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001216 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001217 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001218 error_propagate(errp, local_err);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001219 return -1;
Michael Rothe3d4d252011-07-19 15:41:55 -05001220 }
1221
1222 /* cannot risk guest agent blocking itself on a write in this state */
Michael Rothf22d85e2012-04-17 19:01:45 -05001223 ga_set_frozen(ga_state);
Michael Rothe3d4d252011-07-19 15:41:55 -05001224
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -04001225 QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04001226 /* To issue fsfreeze in the reverse order of mounts, check if the
1227 * mount is listed in the list here */
1228 if (has_mountpoints) {
1229 for (list = mountpoints; list; list = list->next) {
1230 if (strcmp(list->value, mount->dirname) == 0) {
1231 break;
1232 }
1233 }
1234 if (!list) {
1235 continue;
1236 }
1237 }
1238
Michael Rothe3d4d252011-07-19 15:41:55 -05001239 fd = qemu_open(mount->dirname, O_RDONLY);
1240 if (fd == -1) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001241 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
Michael Rothe3d4d252011-07-19 15:41:55 -05001242 goto error;
1243 }
1244
1245 /* we try to cull filesytems we know won't work in advance, but other
1246 * filesytems may not implement fsfreeze for less obvious reasons.
Michael Roth9e8aded432012-04-16 19:52:17 -05001247 * these will report EOPNOTSUPP. we simply ignore these when tallying
1248 * the number of frozen filesystems.
1249 *
1250 * any other error means a failure to freeze a filesystem we
1251 * expect to be freezable, so return an error in those cases
1252 * and return system to thawed state.
Michael Rothe3d4d252011-07-19 15:41:55 -05001253 */
1254 ret = ioctl(fd, FIFREEZE);
Michael Roth9e8aded432012-04-16 19:52:17 -05001255 if (ret == -1) {
1256 if (errno != EOPNOTSUPP) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001257 error_setg_errno(errp, errno, "failed to freeze %s",
Luiz Capitulino617fbbc2012-11-27 11:02:00 -02001258 mount->dirname);
Michael Roth9e8aded432012-04-16 19:52:17 -05001259 close(fd);
1260 goto error;
1261 }
1262 } else {
1263 i++;
Michael Rothe3d4d252011-07-19 15:41:55 -05001264 }
1265 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -05001266 }
1267
Paolo Bonziniaf022032012-06-13 07:41:27 +02001268 free_fs_mount_list(&mounts);
Michael Rothe3d4d252011-07-19 15:41:55 -05001269 return i;
1270
1271error:
Paolo Bonziniaf022032012-06-13 07:41:27 +02001272 free_fs_mount_list(&mounts);
Michael Roth9e8aded432012-04-16 19:52:17 -05001273 qmp_guest_fsfreeze_thaw(NULL);
Michael Rothe3d4d252011-07-19 15:41:55 -05001274 return 0;
1275}
1276
1277/*
1278 * Walk list of frozen file systems in the guest, and thaw them.
1279 */
Markus Armbruster77dbc812014-05-02 13:26:30 +02001280int64_t qmp_guest_fsfreeze_thaw(Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -05001281{
1282 int ret;
Paolo Bonziniaf022032012-06-13 07:41:27 +02001283 FsMountList mounts;
1284 FsMount *mount;
Michael Roth9e8aded432012-04-16 19:52:17 -05001285 int fd, i = 0, logged;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001286 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -05001287
Michael Roth9e8aded432012-04-16 19:52:17 -05001288 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001289 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001290 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001291 error_propagate(errp, local_err);
Michael Roth9e8aded432012-04-16 19:52:17 -05001292 return 0;
1293 }
1294
1295 QTAILQ_FOREACH(mount, &mounts, next) {
1296 logged = false;
Michael Rothe3d4d252011-07-19 15:41:55 -05001297 fd = qemu_open(mount->dirname, O_RDONLY);
1298 if (fd == -1) {
Michael Rothe3d4d252011-07-19 15:41:55 -05001299 continue;
1300 }
Michael Roth9e8aded432012-04-16 19:52:17 -05001301 /* we have no way of knowing whether a filesystem was actually unfrozen
1302 * as a result of a successful call to FITHAW, only that if an error
1303 * was returned the filesystem was *not* unfrozen by that particular
1304 * call.
1305 *
Jim Meyeringa31f0532012-05-09 05:12:04 +00001306 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
Michael Roth9e8aded432012-04-16 19:52:17 -05001307 * to unfreeze, continuing issuing FITHAW until an error is returned,
1308 * in which case either the filesystem is in an unfreezable state, or,
1309 * more likely, it was thawed previously (and remains so afterward).
1310 *
1311 * also, since the most recent successful call is the one that did
1312 * the actual unfreeze, we can use this to provide an accurate count
1313 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1314 * may * be useful for determining whether a filesystem was unfrozen
1315 * during the freeze/thaw phase by a process other than qemu-ga.
1316 */
1317 do {
1318 ret = ioctl(fd, FITHAW);
1319 if (ret == 0 && !logged) {
1320 i++;
1321 logged = true;
1322 }
1323 } while (ret == 0);
Michael Rothe3d4d252011-07-19 15:41:55 -05001324 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -05001325 }
1326
Michael Rothf22d85e2012-04-17 19:01:45 -05001327 ga_unset_frozen(ga_state);
Paolo Bonziniaf022032012-06-13 07:41:27 +02001328 free_fs_mount_list(&mounts);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001329
Markus Armbruster77dbc812014-05-02 13:26:30 +02001330 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +09001331
Michael Rothe3d4d252011-07-19 15:41:55 -05001332 return i;
1333}
1334
Michael Rothe3d4d252011-07-19 15:41:55 -05001335static void guest_fsfreeze_cleanup(void)
1336{
Michael Rothe3d4d252011-07-19 15:41:55 -05001337 Error *err = NULL;
1338
Michael Rothf22d85e2012-04-17 19:01:45 -05001339 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
Markus Armbruster6f686742013-01-16 18:15:08 +01001340 qmp_guest_fsfreeze_thaw(&err);
1341 if (err) {
1342 slog("failed to clean up frozen filesystems: %s",
1343 error_get_pretty(err));
1344 error_free(err);
Michael Rothe3d4d252011-07-19 15:41:55 -05001345 }
1346 }
1347}
Michael Rothe72c3f22012-03-25 13:59:41 -05001348#endif /* CONFIG_FSFREEZE */
Michael Rothe3d4d252011-07-19 15:41:55 -05001349
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001350#if defined(CONFIG_FSTRIM)
1351/*
1352 * Walk list of mounted file systems in the guest, and trim them.
1353 */
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001354GuestFilesystemTrimResponse *
1355qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001356{
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001357 GuestFilesystemTrimResponse *response;
1358 GuestFilesystemTrimResultList *list;
1359 GuestFilesystemTrimResult *result;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001360 int ret = 0;
1361 FsMountList mounts;
1362 struct FsMount *mount;
1363 int fd;
Luiz Capitulino261551d2012-11-29 15:29:11 -02001364 Error *local_err = NULL;
Justin Ossevoort73a652a2015-05-11 08:58:44 +02001365 struct fstrim_range r;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001366
1367 slog("guest-fstrim called");
1368
1369 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -02001370 build_fs_mount_list(&mounts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001371 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001372 error_propagate(errp, local_err);
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001373 return NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001374 }
1375
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001376 response = g_malloc0(sizeof(*response));
1377
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001378 QTAILQ_FOREACH(mount, &mounts, next) {
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001379 result = g_malloc0(sizeof(*result));
1380 result->path = g_strdup(mount->dirname);
1381
1382 list = g_malloc0(sizeof(*list));
1383 list->value = result;
1384 list->next = response->paths;
1385 response->paths = list;
1386
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001387 fd = qemu_open(mount->dirname, O_RDONLY);
1388 if (fd == -1) {
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001389 result->error = g_strdup_printf("failed to open: %s",
1390 strerror(errno));
1391 result->has_error = true;
1392 continue;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001393 }
1394
1395 /* We try to cull filesytems we know won't work in advance, but other
1396 * filesytems may not implement fstrim for less obvious reasons. These
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001397 * will report EOPNOTSUPP; while in some other cases ENOTTY will be
1398 * reported (e.g. CD-ROMs).
1399 * Any other error means an unexpected error.
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001400 */
Justin Ossevoort73a652a2015-05-11 08:58:44 +02001401 r.start = 0;
1402 r.len = -1;
1403 r.minlen = has_minimum ? minimum : 0;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001404 ret = ioctl(fd, FITRIM, &r);
1405 if (ret == -1) {
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001406 result->has_error = true;
1407 if (errno == ENOTTY || errno == EOPNOTSUPP) {
1408 result->error = g_strdup("trim not supported");
1409 } else {
1410 result->error = g_strdup_printf("failed to trim: %s",
1411 strerror(errno));
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001412 }
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001413 close(fd);
1414 continue;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001415 }
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001416
1417 result->has_minimum = true;
1418 result->minimum = r.minlen;
1419 result->has_trimmed = true;
1420 result->trimmed = r.len;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001421 close(fd);
1422 }
1423
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001424 free_fs_mount_list(&mounts);
Justin Ossevoorte82855d2015-05-11 08:58:45 +02001425 return response;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001426}
1427#endif /* CONFIG_FSTRIM */
1428
1429
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001430#define LINUX_SYS_STATE_FILE "/sys/power/state"
1431#define SUSPEND_SUPPORTED 0
1432#define SUSPEND_NOT_SUPPORTED 1
1433
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001434static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
Markus Armbruster77dbc812014-05-02 13:26:30 +02001435 const char *sysfile_str, Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001436{
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001437 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001438 char *pmutils_path;
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001439 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001440 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001441
1442 pmutils_path = g_find_program_in_path(pmutils_bin);
1443
1444 pid = fork();
1445 if (!pid) {
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001446 char buf[32]; /* hopefully big enough */
1447 ssize_t ret;
1448 int fd;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001449
1450 setsid();
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001451 reopen_fd_to_null(0);
1452 reopen_fd_to_null(1);
1453 reopen_fd_to_null(2);
1454
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001455 if (pmutils_path) {
1456 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
1457 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001458
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001459 /*
1460 * If we get here either pm-utils is not installed or execle() has
1461 * failed. Let's try the manual method if the caller wants it.
1462 */
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001463
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001464 if (!sysfile_str) {
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001465 _exit(SUSPEND_NOT_SUPPORTED);
1466 }
1467
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001468 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1469 if (fd < 0) {
1470 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001471 }
1472
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001473 ret = read(fd, buf, sizeof(buf)-1);
1474 if (ret <= 0) {
1475 _exit(SUSPEND_NOT_SUPPORTED);
1476 }
1477 buf[ret] = '\0';
1478
1479 if (strstr(buf, sysfile_str)) {
1480 _exit(SUSPEND_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001481 }
1482
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001483 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001484 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001485 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001486 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001487 }
1488
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001489 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001490 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001491 error_propagate(errp, local_err);
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001492 goto out;
1493 }
1494
1495 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001496 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001497 goto out;
1498 }
1499
1500 switch (WEXITSTATUS(status)) {
1501 case SUSPEND_SUPPORTED:
1502 goto out;
1503 case SUSPEND_NOT_SUPPORTED:
Markus Armbruster77dbc812014-05-02 13:26:30 +02001504 error_setg(errp,
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001505 "the requested suspend mode is not supported by the guest");
1506 goto out;
1507 default:
Markus Armbruster77dbc812014-05-02 13:26:30 +02001508 error_setg(errp,
Luiz Capitulino6b26e832012-11-27 11:02:03 -02001509 "the helper program '%s' returned an unexpected exit status"
1510 " code (%d)", pmutils_path, WEXITSTATUS(status));
1511 goto out;
1512 }
1513
1514out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001515 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001516}
1517
1518static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
Markus Armbruster77dbc812014-05-02 13:26:30 +02001519 Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001520{
Luiz Capitulino7b376082012-11-27 11:02:04 -02001521 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001522 char *pmutils_path;
Luiz Capitulino7b376082012-11-27 11:02:04 -02001523 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -03001524 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001525
1526 pmutils_path = g_find_program_in_path(pmutils_bin);
1527
1528 pid = fork();
1529 if (pid == 0) {
1530 /* child */
1531 int fd;
1532
1533 setsid();
1534 reopen_fd_to_null(0);
1535 reopen_fd_to_null(1);
1536 reopen_fd_to_null(2);
1537
1538 if (pmutils_path) {
1539 execle(pmutils_path, pmutils_bin, NULL, environ);
1540 }
1541
1542 /*
1543 * If we get here either pm-utils is not installed or execle() has
1544 * failed. Let's try the manual method if the caller wants it.
1545 */
1546
1547 if (!sysfile_str) {
1548 _exit(EXIT_FAILURE);
1549 }
1550
1551 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1552 if (fd < 0) {
1553 _exit(EXIT_FAILURE);
1554 }
1555
1556 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1557 _exit(EXIT_FAILURE);
1558 }
1559
1560 _exit(EXIT_SUCCESS);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001561 } else if (pid < 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001562 error_setg_errno(errp, errno, "failed to create child process");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001563 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001564 }
1565
Luiz Capitulino7b376082012-11-27 11:02:04 -02001566 ga_wait_child(pid, &status, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001567 if (local_err) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001568 error_propagate(errp, local_err);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001569 goto out;
1570 }
1571
1572 if (!WIFEXITED(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001573 error_setg(errp, "child process has terminated abnormally");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001574 goto out;
1575 }
1576
1577 if (WEXITSTATUS(status)) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001578 error_setg(errp, "child process has failed to suspend");
Luiz Capitulino7b376082012-11-27 11:02:04 -02001579 goto out;
1580 }
1581
1582out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001583 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001584}
1585
Markus Armbruster77dbc812014-05-02 13:26:30 +02001586void qmp_guest_suspend_disk(Error **errp)
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001587{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001588 Error *local_err = NULL;
1589
1590 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err);
1591 if (local_err) {
1592 error_propagate(errp, local_err);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001593 return;
1594 }
1595
Markus Armbruster77dbc812014-05-02 13:26:30 +02001596 guest_suspend("pm-hibernate", "disk", errp);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001597}
1598
Markus Armbruster77dbc812014-05-02 13:26:30 +02001599void qmp_guest_suspend_ram(Error **errp)
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001600{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001601 Error *local_err = NULL;
1602
1603 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err);
1604 if (local_err) {
1605 error_propagate(errp, local_err);
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001606 return;
1607 }
1608
Markus Armbruster77dbc812014-05-02 13:26:30 +02001609 guest_suspend("pm-suspend", "mem", errp);
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001610}
1611
Markus Armbruster77dbc812014-05-02 13:26:30 +02001612void qmp_guest_suspend_hybrid(Error **errp)
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001613{
Markus Armbruster0f230bf2014-05-02 13:26:38 +02001614 Error *local_err = NULL;
1615
1616 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL,
1617 &local_err);
1618 if (local_err) {
1619 error_propagate(errp, local_err);
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001620 return;
1621 }
1622
Markus Armbruster77dbc812014-05-02 13:26:30 +02001623 guest_suspend("pm-suspend-hybrid", NULL, errp);
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001624}
1625
Michal Privoznik3424fc92012-02-29 17:02:23 +01001626static GuestNetworkInterfaceList *
1627guest_find_interface(GuestNetworkInterfaceList *head,
1628 const char *name)
1629{
1630 for (; head; head = head->next) {
1631 if (strcmp(head->value->name, name) == 0) {
1632 break;
1633 }
1634 }
1635
1636 return head;
1637}
1638
1639/*
1640 * Build information about guest interfaces
1641 */
1642GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1643{
1644 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1645 struct ifaddrs *ifap, *ifa;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001646
1647 if (getifaddrs(&ifap) < 0) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001648 error_setg_errno(errp, errno, "getifaddrs failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001649 goto error;
1650 }
1651
1652 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1653 GuestNetworkInterfaceList *info;
1654 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1655 char addr4[INET_ADDRSTRLEN];
1656 char addr6[INET6_ADDRSTRLEN];
1657 int sock;
1658 struct ifreq ifr;
1659 unsigned char *mac_addr;
1660 void *p;
1661
1662 g_debug("Processing %s interface", ifa->ifa_name);
1663
1664 info = guest_find_interface(head, ifa->ifa_name);
1665
1666 if (!info) {
1667 info = g_malloc0(sizeof(*info));
1668 info->value = g_malloc0(sizeof(*info->value));
1669 info->value->name = g_strdup(ifa->ifa_name);
1670
1671 if (!cur_item) {
1672 head = cur_item = info;
1673 } else {
1674 cur_item->next = info;
1675 cur_item = info;
1676 }
1677 }
1678
1679 if (!info->value->has_hardware_address &&
1680 ifa->ifa_flags & SIOCGIFHWADDR) {
1681 /* we haven't obtained HW address yet */
1682 sock = socket(PF_INET, SOCK_STREAM, 0);
1683 if (sock == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001684 error_setg_errno(errp, errno, "failed to create socket");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001685 goto error;
1686 }
1687
1688 memset(&ifr, 0, sizeof(ifr));
Jim Meyering1ab516e2012-10-04 13:09:58 +02001689 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001690 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001691 error_setg_errno(errp, errno,
1692 "failed to get MAC address of %s",
1693 ifa->ifa_name);
Markus Armbruster10a21582013-01-16 18:15:09 +01001694 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001695 goto error;
1696 }
1697
Markus Armbruster10a21582013-01-16 18:15:09 +01001698 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001699 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1700
Stefan Weile4ada482013-01-16 18:37:23 +01001701 info->value->hardware_address =
1702 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1703 (int) mac_addr[0], (int) mac_addr[1],
1704 (int) mac_addr[2], (int) mac_addr[3],
1705 (int) mac_addr[4], (int) mac_addr[5]);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001706
1707 info->value->has_hardware_address = true;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001708 }
1709
1710 if (ifa->ifa_addr &&
1711 ifa->ifa_addr->sa_family == AF_INET) {
1712 /* interface with IPv4 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001713 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1714 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001715 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001716 goto error;
1717 }
1718
Markus Armbruster10a21582013-01-16 18:15:09 +01001719 address_item = g_malloc0(sizeof(*address_item));
1720 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001721 address_item->value->ip_address = g_strdup(addr4);
1722 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1723
1724 if (ifa->ifa_netmask) {
1725 /* Count the number of set bits in netmask.
1726 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1727 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1728 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1729 }
1730 } else if (ifa->ifa_addr &&
1731 ifa->ifa_addr->sa_family == AF_INET6) {
1732 /* interface with IPv6 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001733 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1734 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001735 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001736 goto error;
1737 }
1738
Markus Armbruster10a21582013-01-16 18:15:09 +01001739 address_item = g_malloc0(sizeof(*address_item));
1740 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001741 address_item->value->ip_address = g_strdup(addr6);
1742 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1743
1744 if (ifa->ifa_netmask) {
1745 /* Count the number of set bits in netmask.
1746 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1747 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1748 address_item->value->prefix =
1749 ctpop32(((uint32_t *) p)[0]) +
1750 ctpop32(((uint32_t *) p)[1]) +
1751 ctpop32(((uint32_t *) p)[2]) +
1752 ctpop32(((uint32_t *) p)[3]);
1753 }
1754 }
1755
1756 if (!address_item) {
1757 continue;
1758 }
1759
1760 address_list = &info->value->ip_addresses;
1761
1762 while (*address_list && (*address_list)->next) {
1763 address_list = &(*address_list)->next;
1764 }
1765
1766 if (!*address_list) {
1767 *address_list = address_item;
1768 } else {
1769 (*address_list)->next = address_item;
1770 }
1771
1772 info->value->has_ip_addresses = true;
1773
1774
1775 }
1776
1777 freeifaddrs(ifap);
1778 return head;
1779
1780error:
1781 freeifaddrs(ifap);
1782 qapi_free_GuestNetworkInterfaceList(head);
1783 return NULL;
1784}
1785
Markus Armbruster77dbc812014-05-02 13:26:30 +02001786#define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001787
Markus Armbruster77dbc812014-05-02 13:26:30 +02001788static long sysconf_exact(int name, const char *name_str, Error **errp)
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001789{
1790 long ret;
1791
1792 errno = 0;
1793 ret = sysconf(name);
1794 if (ret == -1) {
1795 if (errno == 0) {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001796 error_setg(errp, "sysconf(%s): value indefinite", name_str);
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001797 } else {
Markus Armbruster77dbc812014-05-02 13:26:30 +02001798 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001799 }
1800 }
1801 return ret;
1802}
1803
1804/* Transfer online/offline status between @vcpu and the guest system.
1805 *
1806 * On input either @errp or *@errp must be NULL.
1807 *
1808 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1809 * - R: vcpu->logical_id
1810 * - W: vcpu->online
1811 * - W: vcpu->can_offline
1812 *
1813 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1814 * - R: vcpu->logical_id
1815 * - R: vcpu->online
1816 *
1817 * Written members remain unmodified on error.
1818 */
1819static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1820 Error **errp)
1821{
1822 char *dirpath;
1823 int dirfd;
1824
1825 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1826 vcpu->logical_id);
1827 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1828 if (dirfd == -1) {
1829 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1830 } else {
1831 static const char fn[] = "online";
1832 int fd;
1833 int res;
1834
1835 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1836 if (fd == -1) {
1837 if (errno != ENOENT) {
1838 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1839 } else if (sys2vcpu) {
1840 vcpu->online = true;
1841 vcpu->can_offline = false;
1842 } else if (!vcpu->online) {
1843 error_setg(errp, "logical processor #%" PRId64 " can't be "
1844 "offlined", vcpu->logical_id);
1845 } /* otherwise pretend successful re-onlining */
1846 } else {
1847 unsigned char status;
1848
1849 res = pread(fd, &status, 1, 0);
1850 if (res == -1) {
1851 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1852 } else if (res == 0) {
1853 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1854 fn);
1855 } else if (sys2vcpu) {
1856 vcpu->online = (status != '0');
1857 vcpu->can_offline = true;
1858 } else if (vcpu->online != (status != '0')) {
1859 status = '0' + vcpu->online;
1860 if (pwrite(fd, &status, 1, 0) == -1) {
1861 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1862 fn);
1863 }
1864 } /* otherwise pretend successful re-(on|off)-lining */
1865
1866 res = close(fd);
1867 g_assert(res == 0);
1868 }
1869
1870 res = close(dirfd);
1871 g_assert(res == 0);
1872 }
1873
1874 g_free(dirpath);
1875}
1876
1877GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1878{
1879 int64_t current;
1880 GuestLogicalProcessorList *head, **link;
1881 long sc_max;
1882 Error *local_err = NULL;
1883
1884 current = 0;
1885 head = NULL;
1886 link = &head;
1887 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1888
1889 while (local_err == NULL && current < sc_max) {
1890 GuestLogicalProcessor *vcpu;
1891 GuestLogicalProcessorList *entry;
1892
1893 vcpu = g_malloc0(sizeof *vcpu);
1894 vcpu->logical_id = current++;
1895 vcpu->has_can_offline = true; /* lolspeak ftw */
1896 transfer_vcpu(vcpu, true, &local_err);
1897
1898 entry = g_malloc0(sizeof *entry);
1899 entry->value = vcpu;
1900
1901 *link = entry;
1902 link = &entry->next;
1903 }
1904
1905 if (local_err == NULL) {
1906 /* there's no guest with zero VCPUs */
1907 g_assert(head != NULL);
1908 return head;
1909 }
1910
1911 qapi_free_GuestLogicalProcessorList(head);
1912 error_propagate(errp, local_err);
1913 return NULL;
1914}
1915
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001916int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1917{
1918 int64_t processed;
1919 Error *local_err = NULL;
1920
1921 processed = 0;
1922 while (vcpus != NULL) {
1923 transfer_vcpu(vcpus->value, false, &local_err);
1924 if (local_err != NULL) {
1925 break;
1926 }
1927 ++processed;
1928 vcpus = vcpus->next;
1929 }
1930
1931 if (local_err != NULL) {
1932 if (processed == 0) {
1933 error_propagate(errp, local_err);
1934 } else {
1935 error_free(local_err);
1936 }
1937 }
1938
1939 return processed;
1940}
1941
Daniel P. Berrange215a2772015-02-11 11:26:12 +00001942void qmp_guest_set_user_password(const char *username,
1943 const char *password,
1944 bool crypted,
1945 Error **errp)
1946{
1947 Error *local_err = NULL;
1948 char *passwd_path = NULL;
1949 pid_t pid;
1950 int status;
1951 int datafd[2] = { -1, -1 };
1952 char *rawpasswddata = NULL;
1953 size_t rawpasswdlen;
1954 char *chpasswddata = NULL;
1955 size_t chpasswdlen;
1956
Daniel P. Berrange920639c2015-11-23 15:37:07 +00001957 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
1958 if (!rawpasswddata) {
1959 return;
1960 }
Daniel P. Berrange215a2772015-02-11 11:26:12 +00001961 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
1962 rawpasswddata[rawpasswdlen] = '\0';
1963
1964 if (strchr(rawpasswddata, '\n')) {
1965 error_setg(errp, "forbidden characters in raw password");
1966 goto out;
1967 }
1968
1969 if (strchr(username, '\n') ||
1970 strchr(username, ':')) {
1971 error_setg(errp, "forbidden characters in username");
1972 goto out;
1973 }
1974
1975 chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata);
1976 chpasswdlen = strlen(chpasswddata);
1977
1978 passwd_path = g_find_program_in_path("chpasswd");
1979
1980 if (!passwd_path) {
1981 error_setg(errp, "cannot find 'passwd' program in PATH");
1982 goto out;
1983 }
1984
1985 if (pipe(datafd) < 0) {
1986 error_setg(errp, "cannot create pipe FDs");
1987 goto out;
1988 }
1989
1990 pid = fork();
1991 if (pid == 0) {
1992 close(datafd[1]);
1993 /* child */
1994 setsid();
1995 dup2(datafd[0], 0);
1996 reopen_fd_to_null(1);
1997 reopen_fd_to_null(2);
1998
1999 if (crypted) {
2000 execle(passwd_path, "chpasswd", "-e", NULL, environ);
2001 } else {
2002 execle(passwd_path, "chpasswd", NULL, environ);
2003 }
2004 _exit(EXIT_FAILURE);
2005 } else if (pid < 0) {
2006 error_setg_errno(errp, errno, "failed to create child process");
2007 goto out;
2008 }
2009 close(datafd[0]);
2010 datafd[0] = -1;
2011
2012 if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) {
2013 error_setg_errno(errp, errno, "cannot write new account password");
2014 goto out;
2015 }
2016 close(datafd[1]);
2017 datafd[1] = -1;
2018
2019 ga_wait_child(pid, &status, &local_err);
2020 if (local_err) {
2021 error_propagate(errp, local_err);
2022 goto out;
2023 }
2024
2025 if (!WIFEXITED(status)) {
2026 error_setg(errp, "child process has terminated abnormally");
2027 goto out;
2028 }
2029
2030 if (WEXITSTATUS(status)) {
2031 error_setg(errp, "child process has failed to set user password");
2032 goto out;
2033 }
2034
2035out:
2036 g_free(chpasswddata);
2037 g_free(rawpasswddata);
2038 g_free(passwd_path);
2039 if (datafd[0] != -1) {
2040 close(datafd[0]);
2041 }
2042 if (datafd[1] != -1) {
2043 close(datafd[1]);
2044 }
2045}
2046
zhanghailiangbd240fc2015-01-22 10:40:03 +08002047static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
2048 int size, Error **errp)
2049{
2050 int fd;
2051 int res;
2052
2053 errno = 0;
2054 fd = openat(dirfd, pathname, O_RDONLY);
2055 if (fd == -1) {
2056 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2057 return;
2058 }
2059
2060 res = pread(fd, buf, size, 0);
2061 if (res == -1) {
2062 error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname);
2063 } else if (res == 0) {
2064 error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname);
2065 }
2066 close(fd);
2067}
2068
2069static void ga_write_sysfs_file(int dirfd, const char *pathname,
2070 const char *buf, int size, Error **errp)
2071{
2072 int fd;
2073
2074 errno = 0;
2075 fd = openat(dirfd, pathname, O_WRONLY);
2076 if (fd == -1) {
2077 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2078 return;
2079 }
2080
2081 if (pwrite(fd, buf, size, 0) == -1) {
2082 error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
2083 }
2084
2085 close(fd);
2086}
2087
2088/* Transfer online/offline status between @mem_blk and the guest system.
2089 *
2090 * On input either @errp or *@errp must be NULL.
2091 *
2092 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2093 * - R: mem_blk->phys_index
2094 * - W: mem_blk->online
2095 * - W: mem_blk->can_offline
2096 *
2097 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2098 * - R: mem_blk->phys_index
2099 * - R: mem_blk->online
2100 *- R: mem_blk->can_offline
2101 * Written members remain unmodified on error.
2102 */
2103static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
2104 GuestMemoryBlockResponse *result,
2105 Error **errp)
2106{
2107 char *dirpath;
2108 int dirfd;
2109 char *status;
2110 Error *local_err = NULL;
2111
2112 if (!sys2memblk) {
2113 DIR *dp;
2114
2115 if (!result) {
2116 error_setg(errp, "Internal error, 'result' should not be NULL");
2117 return;
2118 }
2119 errno = 0;
2120 dp = opendir("/sys/devices/system/memory/");
2121 /* if there is no 'memory' directory in sysfs,
2122 * we think this VM does not support online/offline memory block,
2123 * any other solution?
2124 */
2125 if (!dp && errno == ENOENT) {
2126 result->response =
2127 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2128 goto out1;
2129 }
2130 closedir(dp);
2131 }
2132
2133 dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
2134 mem_blk->phys_index);
2135 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2136 if (dirfd == -1) {
2137 if (sys2memblk) {
2138 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2139 } else {
2140 if (errno == ENOENT) {
2141 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
2142 } else {
2143 result->response =
2144 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2145 }
2146 }
2147 g_free(dirpath);
2148 goto out1;
2149 }
2150 g_free(dirpath);
2151
2152 status = g_malloc0(10);
2153 ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
2154 if (local_err) {
2155 /* treat with sysfs file that not exist in old kernel */
2156 if (errno == ENOENT) {
2157 error_free(local_err);
2158 if (sys2memblk) {
2159 mem_blk->online = true;
2160 mem_blk->can_offline = false;
2161 } else if (!mem_blk->online) {
2162 result->response =
2163 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2164 }
2165 } else {
2166 if (sys2memblk) {
2167 error_propagate(errp, local_err);
2168 } else {
2169 result->response =
2170 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2171 }
2172 }
2173 goto out2;
2174 }
2175
2176 if (sys2memblk) {
2177 char removable = '0';
2178
2179 mem_blk->online = (strncmp(status, "online", 6) == 0);
2180
2181 ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err);
2182 if (local_err) {
Veres Lajos67cc32e2015-09-08 22:45:14 +01002183 /* if no 'removable' file, it doesn't support offline mem blk */
zhanghailiangbd240fc2015-01-22 10:40:03 +08002184 if (errno == ENOENT) {
2185 error_free(local_err);
2186 mem_blk->can_offline = false;
2187 } else {
2188 error_propagate(errp, local_err);
2189 }
2190 } else {
2191 mem_blk->can_offline = (removable != '0');
2192 }
2193 } else {
2194 if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
2195 char *new_state = mem_blk->online ? g_strdup("online") :
2196 g_strdup("offline");
2197
2198 ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
2199 &local_err);
2200 g_free(new_state);
2201 if (local_err) {
2202 error_free(local_err);
2203 result->response =
2204 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2205 goto out2;
2206 }
2207
2208 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
2209 result->has_error_code = false;
2210 } /* otherwise pretend successful re-(on|off)-lining */
2211 }
2212 g_free(status);
2213 close(dirfd);
2214 return;
2215
2216out2:
2217 g_free(status);
2218 close(dirfd);
2219out1:
2220 if (!sys2memblk) {
2221 result->has_error_code = true;
2222 result->error_code = errno;
2223 }
2224}
2225
zhanghailianga065aaa2015-01-22 10:40:02 +08002226GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2227{
zhanghailiangbd240fc2015-01-22 10:40:03 +08002228 GuestMemoryBlockList *head, **link;
2229 Error *local_err = NULL;
2230 struct dirent *de;
2231 DIR *dp;
2232
2233 head = NULL;
2234 link = &head;
2235
2236 dp = opendir("/sys/devices/system/memory/");
2237 if (!dp) {
Michael Rothf693fe62015-10-19 16:38:25 -05002238 /* it's ok if this happens to be a system that doesn't expose
2239 * memory blocks via sysfs, but otherwise we should report
2240 * an error
2241 */
2242 if (errno != ENOENT) {
2243 error_setg_errno(errp, errno, "Can't open directory"
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01002244 "\"/sys/devices/system/memory/\"");
Michael Rothf693fe62015-10-19 16:38:25 -05002245 }
zhanghailiangbd240fc2015-01-22 10:40:03 +08002246 return NULL;
2247 }
2248
2249 /* Note: the phys_index of memory block may be discontinuous,
2250 * this is because a memblk is the unit of the Sparse Memory design, which
2251 * allows discontinuous memory ranges (ex. NUMA), so here we should
2252 * traverse the memory block directory.
2253 */
2254 while ((de = readdir(dp)) != NULL) {
2255 GuestMemoryBlock *mem_blk;
2256 GuestMemoryBlockList *entry;
2257
2258 if ((strncmp(de->d_name, "memory", 6) != 0) ||
2259 !(de->d_type & DT_DIR)) {
2260 continue;
2261 }
2262
2263 mem_blk = g_malloc0(sizeof *mem_blk);
2264 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2265 mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10);
2266 mem_blk->has_can_offline = true; /* lolspeak ftw */
2267 transfer_memory_block(mem_blk, true, NULL, &local_err);
2268
2269 entry = g_malloc0(sizeof *entry);
2270 entry->value = mem_blk;
2271
2272 *link = entry;
2273 link = &entry->next;
2274 }
2275
2276 closedir(dp);
2277 if (local_err == NULL) {
2278 /* there's no guest with zero memory blocks */
2279 if (head == NULL) {
2280 error_setg(errp, "guest reported zero memory blocks!");
2281 }
2282 return head;
2283 }
2284
2285 qapi_free_GuestMemoryBlockList(head);
2286 error_propagate(errp, local_err);
zhanghailianga065aaa2015-01-22 10:40:02 +08002287 return NULL;
2288}
2289
2290GuestMemoryBlockResponseList *
2291qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2292{
zhanghailiang32ca7922015-01-22 10:40:04 +08002293 GuestMemoryBlockResponseList *head, **link;
2294 Error *local_err = NULL;
2295
2296 head = NULL;
2297 link = &head;
2298
2299 while (mem_blks != NULL) {
2300 GuestMemoryBlockResponse *result;
2301 GuestMemoryBlockResponseList *entry;
2302 GuestMemoryBlock *current_mem_blk = mem_blks->value;
2303
2304 result = g_malloc0(sizeof(*result));
2305 result->phys_index = current_mem_blk->phys_index;
2306 transfer_memory_block(current_mem_blk, false, result, &local_err);
2307 if (local_err) { /* should never happen */
2308 goto err;
2309 }
2310 entry = g_malloc0(sizeof *entry);
2311 entry->value = result;
2312
2313 *link = entry;
2314 link = &entry->next;
2315 mem_blks = mem_blks->next;
2316 }
2317
2318 return head;
2319err:
2320 qapi_free_GuestMemoryBlockResponseList(head);
2321 error_propagate(errp, local_err);
zhanghailianga065aaa2015-01-22 10:40:02 +08002322 return NULL;
2323}
2324
2325GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2326{
zhanghailiangef82b602015-01-22 10:40:05 +08002327 Error *local_err = NULL;
2328 char *dirpath;
2329 int dirfd;
2330 char *buf;
2331 GuestMemoryBlockInfo *info;
2332
2333 dirpath = g_strdup_printf("/sys/devices/system/memory/");
2334 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2335 if (dirfd == -1) {
2336 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2337 g_free(dirpath);
2338 return NULL;
2339 }
2340 g_free(dirpath);
2341
2342 buf = g_malloc0(20);
2343 ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
Shannon Zhao8ce1ee42015-03-14 17:52:15 +08002344 close(dirfd);
zhanghailiangef82b602015-01-22 10:40:05 +08002345 if (local_err) {
2346 g_free(buf);
2347 error_propagate(errp, local_err);
2348 return NULL;
2349 }
2350
2351 info = g_new0(GuestMemoryBlockInfo, 1);
2352 info->size = strtol(buf, NULL, 16); /* the unit is bytes */
2353
2354 g_free(buf);
2355
2356 return info;
zhanghailianga065aaa2015-01-22 10:40:02 +08002357}
2358
Michael Rothe72c3f22012-03-25 13:59:41 -05002359#else /* defined(__linux__) */
2360
Markus Armbruster77dbc812014-05-02 13:26:30 +02002361void qmp_guest_suspend_disk(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05002362{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002363 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05002364}
2365
Markus Armbruster77dbc812014-05-02 13:26:30 +02002366void qmp_guest_suspend_ram(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05002367{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002368 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05002369}
2370
Markus Armbruster77dbc812014-05-02 13:26:30 +02002371void qmp_guest_suspend_hybrid(Error **errp)
Michael Rothe72c3f22012-03-25 13:59:41 -05002372{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002373 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05002374}
2375
2376GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
2377{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002378 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothe72c3f22012-03-25 13:59:41 -05002379 return NULL;
2380}
2381
Laszlo Ersekd2baff62013-03-06 22:59:30 +01002382GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2383{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002384 error_setg(errp, QERR_UNSUPPORTED);
Laszlo Ersekd2baff62013-03-06 22:59:30 +01002385 return NULL;
2386}
2387
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01002388int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2389{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002390 error_setg(errp, QERR_UNSUPPORTED);
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01002391 return -1;
2392}
2393
Daniel P. Berrange215a2772015-02-11 11:26:12 +00002394void qmp_guest_set_user_password(const char *username,
2395 const char *password,
2396 bool crypted,
2397 Error **errp)
2398{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002399 error_setg(errp, QERR_UNSUPPORTED);
Daniel P. Berrange215a2772015-02-11 11:26:12 +00002400}
2401
zhanghailianga065aaa2015-01-22 10:40:02 +08002402GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2403{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002404 error_setg(errp, QERR_UNSUPPORTED);
zhanghailianga065aaa2015-01-22 10:40:02 +08002405 return NULL;
2406}
2407
2408GuestMemoryBlockResponseList *
2409qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2410{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002411 error_setg(errp, QERR_UNSUPPORTED);
zhanghailianga065aaa2015-01-22 10:40:02 +08002412 return NULL;
2413}
2414
2415GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2416{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002417 error_setg(errp, QERR_UNSUPPORTED);
zhanghailianga065aaa2015-01-22 10:40:02 +08002418 return NULL;
2419}
2420
Michael Rothe72c3f22012-03-25 13:59:41 -05002421#endif
2422
Michael Rothd35d4cb2012-04-13 21:07:36 -05002423#if !defined(CONFIG_FSFREEZE)
2424
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04002425GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
2426{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002427 error_setg(errp, QERR_UNSUPPORTED);
Tomoki Sekiyama46d4c572014-06-30 17:51:34 -04002428 return NULL;
2429}
2430
Markus Armbruster77dbc812014-05-02 13:26:30 +02002431GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05002432{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002433 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05002434
2435 return 0;
2436}
2437
Markus Armbruster77dbc812014-05-02 13:26:30 +02002438int64_t qmp_guest_fsfreeze_freeze(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05002439{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002440 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05002441
2442 return 0;
2443}
2444
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04002445int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
2446 strList *mountpoints,
2447 Error **errp)
2448{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002449 error_setg(errp, QERR_UNSUPPORTED);
Tomoki Sekiyamae99bce22014-06-30 17:51:27 -04002450
2451 return 0;
2452}
2453
Markus Armbruster77dbc812014-05-02 13:26:30 +02002454int64_t qmp_guest_fsfreeze_thaw(Error **errp)
Michael Rothd35d4cb2012-04-13 21:07:36 -05002455{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002456 error_setg(errp, QERR_UNSUPPORTED);
Michael Rothd35d4cb2012-04-13 21:07:36 -05002457
2458 return 0;
2459}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02002460#endif /* CONFIG_FSFREEZE */
Michael Rothd35d4cb2012-04-13 21:07:36 -05002461
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02002462#if !defined(CONFIG_FSTRIM)
Justin Ossevoorte82855d2015-05-11 08:58:45 +02002463GuestFilesystemTrimResponse *
2464qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02002465{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002466 error_setg(errp, QERR_UNSUPPORTED);
Justin Ossevoorte82855d2015-05-11 08:58:45 +02002467 return NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02002468}
Michael Rothd35d4cb2012-04-13 21:07:36 -05002469#endif
2470
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04002471/* add unsupported commands to the blacklist */
2472GList *ga_command_blacklist_init(GList *blacklist)
2473{
2474#if !defined(__linux__)
2475 {
2476 const char *list[] = {
2477 "guest-suspend-disk", "guest-suspend-ram",
2478 "guest-suspend-hybrid", "guest-network-get-interfaces",
zhanghailiang0dd38a02015-01-22 10:40:06 +08002479 "guest-get-vcpus", "guest-set-vcpus",
2480 "guest-get-memory-blocks", "guest-set-memory-blocks",
2481 "guest-get-memory-block-size", NULL};
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04002482 char **p = (char **)list;
2483
2484 while (*p) {
Marc-André Lureau4bca81c2015-08-27 01:34:50 +02002485 blacklist = g_list_append(blacklist, g_strdup(*p++));
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04002486 }
2487 }
2488#endif
2489
2490#if !defined(CONFIG_FSFREEZE)
2491 {
2492 const char *list[] = {
2493 "guest-get-fsinfo", "guest-fsfreeze-status",
2494 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2495 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
2496 char **p = (char **)list;
2497
2498 while (*p) {
Marc-André Lureau4bca81c2015-08-27 01:34:50 +02002499 blacklist = g_list_append(blacklist, g_strdup(*p++));
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04002500 }
2501 }
2502#endif
2503
2504#if !defined(CONFIG_FSTRIM)
Marc-André Lureau4bca81c2015-08-27 01:34:50 +02002505 blacklist = g_list_append(blacklist, g_strdup("guest-fstrim"));
Tomoki Sekiyama1281c082014-06-30 17:51:40 -04002506#endif
2507
2508 return blacklist;
2509}
2510
Michael Rothe3d4d252011-07-19 15:41:55 -05002511/* register init/cleanup routines for stateful command groups */
2512void ga_command_state_init(GAState *s, GACommandState *cs)
2513{
Anthony Liguori7006b9c2011-07-22 14:14:17 -05002514#if defined(CONFIG_FSFREEZE)
Michael Rothf22d85e2012-04-17 19:01:45 -05002515 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
Anthony Liguori7006b9c2011-07-22 14:14:17 -05002516#endif
Michael Rothe3d4d252011-07-19 15:41:55 -05002517}