blob: d7da8506152b1c90d6cc91f6878eb8e81d53b673 [file] [log] [blame]
Michael Rothe3d4d252011-07-19 15:41:55 -05001/*
Michael Roth42074a92012-01-19 22:19:27 -06002 * QEMU Guest Agent POSIX-specific command implementations
Michael Rothe3d4d252011-07-19 15:41:55 -05003 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
Michal Privoznik3424fc92012-02-29 17:02:23 +01008 * Michal Privoznik <mprivozn@redhat.com>
Michael Rothe3d4d252011-07-19 15:41:55 -05009 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14#include <glib.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050015#include <sys/types.h>
16#include <sys/ioctl.h>
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030017#include <sys/wait.h>
Laszlo Ersekd2baff62013-03-06 22:59:30 +010018#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <inttypes.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050022#include "qga/guest-agent-core.h"
23#include "qga-qmp-commands.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010024#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/queue.h"
26#include "qemu/host-utils.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
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020053static void ga_wait_child(pid_t pid, int *status, Error **err)
54{
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) {
64 error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
65 return;
66 }
67
68 g_assert(rpid == pid);
69}
70
Michael Rothe3d4d252011-07-19 15:41:55 -050071void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
72{
Michael Rothe3d4d252011-07-19 15:41:55 -050073 const char *shutdown_flag;
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020074 Error *local_err = NULL;
75 pid_t pid;
Luiz Capitulino36748382012-05-14 15:25:20 -030076 int status;
Michael Rothe3d4d252011-07-19 15:41:55 -050077
78 slog("guest-shutdown called, mode: %s", mode);
79 if (!has_mode || strcmp(mode, "powerdown") == 0) {
80 shutdown_flag = "-P";
81 } else if (strcmp(mode, "halt") == 0) {
82 shutdown_flag = "-H";
83 } else if (strcmp(mode, "reboot") == 0) {
84 shutdown_flag = "-r";
85 } else {
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020086 error_setg(err,
87 "mode is invalid (valid values are: halt|powerdown|reboot");
Michael Rothe3d4d252011-07-19 15:41:55 -050088 return;
89 }
90
Luiz Capitulinod5dd3492012-05-11 16:19:47 -030091 pid = fork();
92 if (pid == 0) {
Michael Rothe3d4d252011-07-19 15:41:55 -050093 /* child, start the shutdown */
94 setsid();
Luiz Capitulino36748382012-05-14 15:25:20 -030095 reopen_fd_to_null(0);
96 reopen_fd_to_null(1);
97 reopen_fd_to_null(2);
Michael Rothe3d4d252011-07-19 15:41:55 -050098
Luiz Capitulino36748382012-05-14 15:25:20 -030099 execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
100 "hypervisor initiated shutdown", (char*)NULL, environ);
101 _exit(EXIT_FAILURE);
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300102 } else if (pid < 0) {
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200103 error_setg_errno(err, errno, "failed to create child process");
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300104 return;
105 }
106
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200107 ga_wait_child(pid, &status, &local_err);
108 if (error_is_set(&local_err)) {
109 error_propagate(err, local_err);
110 return;
111 }
112
113 if (!WIFEXITED(status)) {
114 error_setg(err, "child process has terminated abnormally");
115 return;
116 }
117
118 if (WEXITSTATUS(status)) {
119 error_setg(err, "child process has failed to shutdown");
120 return;
121 }
122
123 /* succeded */
Michael Rothe3d4d252011-07-19 15:41:55 -0500124}
125
Lei Li6912e6a2013-03-05 17:39:11 +0800126int64_t qmp_guest_get_time(Error **errp)
127{
128 int ret;
129 qemu_timeval tq;
130 int64_t time_ns;
131
132 ret = qemu_gettimeofday(&tq);
133 if (ret < 0) {
134 error_setg_errno(errp, errno, "Failed to get time");
135 return -1;
136 }
137
138 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
139 return time_ns;
140}
141
Lei Lia1bca572013-03-05 17:39:12 +0800142void qmp_guest_set_time(int64_t time_ns, Error **errp)
143{
144 int ret;
145 int status;
146 pid_t pid;
147 Error *local_err = NULL;
148 struct timeval tv;
149
150 /* year-2038 will overflow in case time_t is 32bit */
151 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
152 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
153 return;
154 }
155
156 tv.tv_sec = time_ns / 1000000000;
157 tv.tv_usec = (time_ns % 1000000000) / 1000;
158
159 ret = settimeofday(&tv, NULL);
160 if (ret < 0) {
161 error_setg_errno(errp, errno, "Failed to set time to guest");
162 return;
163 }
164
165 /* Set the Hardware Clock to the current System Time. */
166 pid = fork();
167 if (pid == 0) {
168 setsid();
169 reopen_fd_to_null(0);
170 reopen_fd_to_null(1);
171 reopen_fd_to_null(2);
172
173 execle("/sbin/hwclock", "hwclock", "-w", NULL, environ);
174 _exit(EXIT_FAILURE);
175 } else if (pid < 0) {
176 error_setg_errno(errp, errno, "failed to create child process");
177 return;
178 }
179
180 ga_wait_child(pid, &status, &local_err);
181 if (error_is_set(&local_err)) {
182 error_propagate(errp, local_err);
183 return;
184 }
185
186 if (!WIFEXITED(status)) {
187 error_setg(errp, "child process has terminated abnormally");
188 return;
189 }
190
191 if (WEXITSTATUS(status)) {
192 error_setg(errp, "hwclock failed to set hardware clock to system time");
193 return;
194 }
195}
196
Michael Rothe3d4d252011-07-19 15:41:55 -0500197typedef struct GuestFileHandle {
198 uint64_t id;
199 FILE *fh;
200 QTAILQ_ENTRY(GuestFileHandle) next;
201} GuestFileHandle;
202
203static struct {
204 QTAILQ_HEAD(, GuestFileHandle) filehandles;
205} guest_file_state;
206
Michael Roth39097da2013-03-01 11:40:27 -0600207static int64_t guest_file_handle_add(FILE *fh, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500208{
209 GuestFileHandle *gfh;
Michael Roth39097da2013-03-01 11:40:27 -0600210 int64_t handle;
211
212 handle = ga_get_fd_handle(ga_state, errp);
213 if (error_is_set(errp)) {
214 return 0;
215 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500216
Anthony Liguori7267c092011-08-20 22:09:37 -0500217 gfh = g_malloc0(sizeof(GuestFileHandle));
Michael Roth39097da2013-03-01 11:40:27 -0600218 gfh->id = handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500219 gfh->fh = fh;
220 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
Michael Roth39097da2013-03-01 11:40:27 -0600221
222 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500223}
224
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200225static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err)
Michael Rothe3d4d252011-07-19 15:41:55 -0500226{
227 GuestFileHandle *gfh;
228
229 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
230 {
231 if (gfh->id == id) {
232 return gfh;
233 }
234 }
235
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200236 error_setg(err, "handle '%" PRId64 "' has not been found", id);
Michael Rothe3d4d252011-07-19 15:41:55 -0500237 return NULL;
238}
239
240int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
241{
242 FILE *fh;
243 int fd;
Michael Roth39097da2013-03-01 11:40:27 -0600244 int64_t ret = -1, handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500245
246 if (!has_mode) {
247 mode = "r";
248 }
249 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
250 fh = fopen(path, mode);
251 if (!fh) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200252 error_setg_errno(err, errno, "failed to open file '%s' (mode: '%s')",
253 path, mode);
Michael Rothe3d4d252011-07-19 15:41:55 -0500254 return -1;
255 }
256
257 /* set fd non-blocking to avoid common use cases (like reading from a
258 * named pipe) from hanging the agent
259 */
260 fd = fileno(fh);
261 ret = fcntl(fd, F_GETFL);
262 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
263 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200264 error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
265 path);
Michael Rothe3d4d252011-07-19 15:41:55 -0500266 fclose(fh);
267 return -1;
268 }
269
Michael Roth39097da2013-03-01 11:40:27 -0600270 handle = guest_file_handle_add(fh, err);
271 if (error_is_set(err)) {
272 fclose(fh);
273 return -1;
274 }
275
276 slog("guest-file-open, handle: %d", handle);
277 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500278}
279
280void qmp_guest_file_close(int64_t handle, Error **err)
281{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200282 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500283 int ret;
284
285 slog("guest-file-close called, handle: %ld", handle);
286 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500287 return;
288 }
289
290 ret = fclose(gfh->fh);
Luiz Capitulino3ac4b7c2012-11-27 11:01:56 -0200291 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200292 error_setg_errno(err, errno, "failed to close handle");
Michael Rothe3d4d252011-07-19 15:41:55 -0500293 return;
294 }
295
296 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500297 g_free(gfh);
Michael Rothe3d4d252011-07-19 15:41:55 -0500298}
299
300struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
301 int64_t count, Error **err)
302{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200303 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500304 GuestFileRead *read_data = NULL;
305 guchar *buf;
306 FILE *fh;
307 size_t read_count;
308
309 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500310 return NULL;
311 }
312
313 if (!has_count) {
314 count = QGA_READ_COUNT_DEFAULT;
315 } else if (count < 0) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200316 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
317 count);
Michael Rothe3d4d252011-07-19 15:41:55 -0500318 return NULL;
319 }
320
321 fh = gfh->fh;
Anthony Liguori7267c092011-08-20 22:09:37 -0500322 buf = g_malloc0(count+1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500323 read_count = fread(buf, 1, count, fh);
324 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200325 error_setg_errno(err, errno, "failed to read file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500326 slog("guest-file-read failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500327 } else {
328 buf[read_count] = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500329 read_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500330 read_data->count = read_count;
331 read_data->eof = feof(fh);
332 if (read_count) {
333 read_data->buf_b64 = g_base64_encode(buf, read_count);
334 }
335 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500336 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500337 clearerr(fh);
338
339 return read_data;
340}
341
342GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
343 bool has_count, int64_t count, Error **err)
344{
345 GuestFileWrite *write_data = NULL;
346 guchar *buf;
347 gsize buf_len;
348 int write_count;
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200349 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500350 FILE *fh;
351
352 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500353 return NULL;
354 }
355
356 fh = gfh->fh;
357 buf = g_base64_decode(buf_b64, &buf_len);
358
359 if (!has_count) {
360 count = buf_len;
361 } else if (count < 0 || count > buf_len) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200362 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
363 count);
Anthony Liguori7267c092011-08-20 22:09:37 -0500364 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500365 return NULL;
366 }
367
368 write_count = fwrite(buf, 1, count, fh);
369 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200370 error_setg_errno(err, errno, "failed to write to file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500371 slog("guest-file-write failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500372 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500373 write_data = g_malloc0(sizeof(GuestFileWrite));
Michael Rothe3d4d252011-07-19 15:41:55 -0500374 write_data->count = write_count;
375 write_data->eof = feof(fh);
376 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500377 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500378 clearerr(fh);
379
380 return write_data;
381}
382
383struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
384 int64_t whence, Error **err)
385{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200386 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500387 GuestFileSeek *seek_data = NULL;
388 FILE *fh;
389 int ret;
390
391 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500392 return NULL;
393 }
394
395 fh = gfh->fh;
396 ret = fseek(fh, offset, whence);
397 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200398 error_setg_errno(err, errno, "failed to seek file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500399 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500400 seek_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500401 seek_data->position = ftell(fh);
402 seek_data->eof = feof(fh);
403 }
404 clearerr(fh);
405
406 return seek_data;
407}
408
409void qmp_guest_file_flush(int64_t handle, Error **err)
410{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200411 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500412 FILE *fh;
413 int ret;
414
415 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500416 return;
417 }
418
419 fh = gfh->fh;
420 ret = fflush(fh);
421 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200422 error_setg_errno(err, errno, "failed to flush file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500423 }
424}
425
426static void guest_file_init(void)
427{
428 QTAILQ_INIT(&guest_file_state.filehandles);
429}
430
Michael Rothe72c3f22012-03-25 13:59:41 -0500431/* linux-specific implementations. avoid this if at all possible. */
432#if defined(__linux__)
433
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200434#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
Paolo Bonziniaf022032012-06-13 07:41:27 +0200435typedef struct FsMount {
Michael Rothe3d4d252011-07-19 15:41:55 -0500436 char *dirname;
437 char *devtype;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200438 QTAILQ_ENTRY(FsMount) next;
439} FsMount;
Michael Rothe3d4d252011-07-19 15:41:55 -0500440
Paolo Bonziniaf022032012-06-13 07:41:27 +0200441typedef QTAILQ_HEAD(, FsMount) FsMountList;
Michael Roth9e8aded432012-04-16 19:52:17 -0500442
Paolo Bonziniaf022032012-06-13 07:41:27 +0200443static void free_fs_mount_list(FsMountList *mounts)
Michael Roth9e8aded432012-04-16 19:52:17 -0500444{
Paolo Bonziniaf022032012-06-13 07:41:27 +0200445 FsMount *mount, *temp;
Michael Roth9e8aded432012-04-16 19:52:17 -0500446
447 if (!mounts) {
448 return;
449 }
450
451 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
452 QTAILQ_REMOVE(mounts, mount, next);
453 g_free(mount->dirname);
454 g_free(mount->devtype);
455 g_free(mount);
456 }
457}
458
Michael Rothe3d4d252011-07-19 15:41:55 -0500459/*
460 * Walk the mount table and build a list of local file systems
461 */
Luiz Capitulino261551d2012-11-29 15:29:11 -0200462static void build_fs_mount_list(FsMountList *mounts, Error **err)
Michael Rothe3d4d252011-07-19 15:41:55 -0500463{
464 struct mntent *ment;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200465 FsMount *mount;
Michael Roth9e2fa412012-05-29 10:08:50 -0500466 char const *mtab = "/proc/self/mounts";
Michael Rothe3d4d252011-07-19 15:41:55 -0500467 FILE *fp;
468
Michael Rothe3d4d252011-07-19 15:41:55 -0500469 fp = setmntent(mtab, "r");
470 if (!fp) {
Luiz Capitulino261551d2012-11-29 15:29:11 -0200471 error_setg(err, "failed to open mtab file: '%s'", mtab);
472 return;
Michael Rothe3d4d252011-07-19 15:41:55 -0500473 }
474
475 while ((ment = getmntent(fp))) {
476 /*
477 * An entry which device name doesn't start with a '/' is
478 * either a dummy file system or a network file system.
479 * Add special handling for smbfs and cifs as is done by
480 * coreutils as well.
481 */
482 if ((ment->mnt_fsname[0] != '/') ||
483 (strcmp(ment->mnt_type, "smbfs") == 0) ||
484 (strcmp(ment->mnt_type, "cifs") == 0)) {
485 continue;
486 }
487
Paolo Bonziniaf022032012-06-13 07:41:27 +0200488 mount = g_malloc0(sizeof(FsMount));
Anthony Liguori7267c092011-08-20 22:09:37 -0500489 mount->dirname = g_strdup(ment->mnt_dir);
490 mount->devtype = g_strdup(ment->mnt_type);
Michael Rothe3d4d252011-07-19 15:41:55 -0500491
Michael Roth9e8aded432012-04-16 19:52:17 -0500492 QTAILQ_INSERT_TAIL(mounts, mount, next);
Michael Rothe3d4d252011-07-19 15:41:55 -0500493 }
494
495 endmntent(fp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500496}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200497#endif
498
499#if defined(CONFIG_FSFREEZE)
Michael Rothe3d4d252011-07-19 15:41:55 -0500500
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900501typedef enum {
502 FSFREEZE_HOOK_THAW = 0,
503 FSFREEZE_HOOK_FREEZE,
504} FsfreezeHookArg;
505
506const char *fsfreeze_hook_arg_string[] = {
507 "thaw",
508 "freeze",
509};
510
511static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
512{
513 int status;
514 pid_t pid;
515 const char *hook;
516 const char *arg_str = fsfreeze_hook_arg_string[arg];
517 Error *local_err = NULL;
518
519 hook = ga_fsfreeze_hook(ga_state);
520 if (!hook) {
521 return;
522 }
523 if (access(hook, X_OK) != 0) {
524 error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
525 return;
526 }
527
528 slog("executing fsfreeze hook with arg '%s'", arg_str);
529 pid = fork();
530 if (pid == 0) {
531 setsid();
532 reopen_fd_to_null(0);
533 reopen_fd_to_null(1);
534 reopen_fd_to_null(2);
535
536 execle(hook, hook, arg_str, NULL, environ);
537 _exit(EXIT_FAILURE);
538 } else if (pid < 0) {
539 error_setg_errno(err, errno, "failed to create child process");
540 return;
541 }
542
543 ga_wait_child(pid, &status, &local_err);
544 if (error_is_set(&local_err)) {
545 error_propagate(err, local_err);
546 return;
547 }
548
549 if (!WIFEXITED(status)) {
550 error_setg(err, "fsfreeze hook has terminated abnormally");
551 return;
552 }
553
554 status = WEXITSTATUS(status);
555 if (status) {
556 error_setg(err, "fsfreeze hook has failed with status %d", status);
557 return;
558 }
559}
560
Michael Rothe3d4d252011-07-19 15:41:55 -0500561/*
562 * Return status of freeze/thaw
563 */
564GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
565{
Michael Rothf22d85e2012-04-17 19:01:45 -0500566 if (ga_is_frozen(ga_state)) {
567 return GUEST_FSFREEZE_STATUS_FROZEN;
568 }
569
570 return GUEST_FSFREEZE_STATUS_THAWED;
Michael Rothe3d4d252011-07-19 15:41:55 -0500571}
572
573/*
574 * Walk list of mounted file systems in the guest, and freeze the ones which
575 * are real local file systems.
576 */
577int64_t qmp_guest_fsfreeze_freeze(Error **err)
578{
579 int ret = 0, i = 0;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200580 FsMountList mounts;
581 struct FsMount *mount;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200582 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500583 int fd;
Michael Rothe3d4d252011-07-19 15:41:55 -0500584
585 slog("guest-fsfreeze called");
586
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900587 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
588 if (error_is_set(&local_err)) {
589 error_propagate(err, local_err);
590 return -1;
591 }
592
Michael Roth9e8aded432012-04-16 19:52:17 -0500593 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200594 build_fs_mount_list(&mounts, &local_err);
595 if (error_is_set(&local_err)) {
596 error_propagate(err, local_err);
597 return -1;
Michael Rothe3d4d252011-07-19 15:41:55 -0500598 }
599
600 /* cannot risk guest agent blocking itself on a write in this state */
Michael Rothf22d85e2012-04-17 19:01:45 -0500601 ga_set_frozen(ga_state);
Michael Rothe3d4d252011-07-19 15:41:55 -0500602
Michael Roth9e8aded432012-04-16 19:52:17 -0500603 QTAILQ_FOREACH(mount, &mounts, next) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500604 fd = qemu_open(mount->dirname, O_RDONLY);
605 if (fd == -1) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200606 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Michael Rothe3d4d252011-07-19 15:41:55 -0500607 goto error;
608 }
609
610 /* we try to cull filesytems we know won't work in advance, but other
611 * filesytems may not implement fsfreeze for less obvious reasons.
Michael Roth9e8aded432012-04-16 19:52:17 -0500612 * these will report EOPNOTSUPP. we simply ignore these when tallying
613 * the number of frozen filesystems.
614 *
615 * any other error means a failure to freeze a filesystem we
616 * expect to be freezable, so return an error in those cases
617 * and return system to thawed state.
Michael Rothe3d4d252011-07-19 15:41:55 -0500618 */
619 ret = ioctl(fd, FIFREEZE);
Michael Roth9e8aded432012-04-16 19:52:17 -0500620 if (ret == -1) {
621 if (errno != EOPNOTSUPP) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200622 error_setg_errno(err, errno, "failed to freeze %s",
623 mount->dirname);
Michael Roth9e8aded432012-04-16 19:52:17 -0500624 close(fd);
625 goto error;
626 }
627 } else {
628 i++;
Michael Rothe3d4d252011-07-19 15:41:55 -0500629 }
630 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500631 }
632
Paolo Bonziniaf022032012-06-13 07:41:27 +0200633 free_fs_mount_list(&mounts);
Michael Rothe3d4d252011-07-19 15:41:55 -0500634 return i;
635
636error:
Paolo Bonziniaf022032012-06-13 07:41:27 +0200637 free_fs_mount_list(&mounts);
Michael Roth9e8aded432012-04-16 19:52:17 -0500638 qmp_guest_fsfreeze_thaw(NULL);
Michael Rothe3d4d252011-07-19 15:41:55 -0500639 return 0;
640}
641
642/*
643 * Walk list of frozen file systems in the guest, and thaw them.
644 */
645int64_t qmp_guest_fsfreeze_thaw(Error **err)
646{
647 int ret;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200648 FsMountList mounts;
649 FsMount *mount;
Michael Roth9e8aded432012-04-16 19:52:17 -0500650 int fd, i = 0, logged;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200651 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500652
Michael Roth9e8aded432012-04-16 19:52:17 -0500653 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200654 build_fs_mount_list(&mounts, &local_err);
655 if (error_is_set(&local_err)) {
656 error_propagate(err, local_err);
Michael Roth9e8aded432012-04-16 19:52:17 -0500657 return 0;
658 }
659
660 QTAILQ_FOREACH(mount, &mounts, next) {
661 logged = false;
Michael Rothe3d4d252011-07-19 15:41:55 -0500662 fd = qemu_open(mount->dirname, O_RDONLY);
663 if (fd == -1) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500664 continue;
665 }
Michael Roth9e8aded432012-04-16 19:52:17 -0500666 /* we have no way of knowing whether a filesystem was actually unfrozen
667 * as a result of a successful call to FITHAW, only that if an error
668 * was returned the filesystem was *not* unfrozen by that particular
669 * call.
670 *
Jim Meyeringa31f0532012-05-09 05:12:04 +0000671 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
Michael Roth9e8aded432012-04-16 19:52:17 -0500672 * to unfreeze, continuing issuing FITHAW until an error is returned,
673 * in which case either the filesystem is in an unfreezable state, or,
674 * more likely, it was thawed previously (and remains so afterward).
675 *
676 * also, since the most recent successful call is the one that did
677 * the actual unfreeze, we can use this to provide an accurate count
678 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
679 * may * be useful for determining whether a filesystem was unfrozen
680 * during the freeze/thaw phase by a process other than qemu-ga.
681 */
682 do {
683 ret = ioctl(fd, FITHAW);
684 if (ret == 0 && !logged) {
685 i++;
686 logged = true;
687 }
688 } while (ret == 0);
Michael Rothe3d4d252011-07-19 15:41:55 -0500689 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500690 }
691
Michael Rothf22d85e2012-04-17 19:01:45 -0500692 ga_unset_frozen(ga_state);
Paolo Bonziniaf022032012-06-13 07:41:27 +0200693 free_fs_mount_list(&mounts);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900694
695 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
696
Michael Rothe3d4d252011-07-19 15:41:55 -0500697 return i;
698}
699
Michael Rothe3d4d252011-07-19 15:41:55 -0500700static void guest_fsfreeze_cleanup(void)
701{
Michael Rothe3d4d252011-07-19 15:41:55 -0500702 Error *err = NULL;
703
Michael Rothf22d85e2012-04-17 19:01:45 -0500704 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
Markus Armbruster6f686742013-01-16 18:15:08 +0100705 qmp_guest_fsfreeze_thaw(&err);
706 if (err) {
707 slog("failed to clean up frozen filesystems: %s",
708 error_get_pretty(err));
709 error_free(err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500710 }
711 }
712}
Michael Rothe72c3f22012-03-25 13:59:41 -0500713#endif /* CONFIG_FSFREEZE */
Michael Rothe3d4d252011-07-19 15:41:55 -0500714
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200715#if defined(CONFIG_FSTRIM)
716/*
717 * Walk list of mounted file systems in the guest, and trim them.
718 */
719void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
720{
721 int ret = 0;
722 FsMountList mounts;
723 struct FsMount *mount;
724 int fd;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200725 Error *local_err = NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200726 struct fstrim_range r = {
727 .start = 0,
728 .len = -1,
729 .minlen = has_minimum ? minimum : 0,
730 };
731
732 slog("guest-fstrim called");
733
734 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200735 build_fs_mount_list(&mounts, &local_err);
736 if (error_is_set(&local_err)) {
737 error_propagate(err, local_err);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200738 return;
739 }
740
741 QTAILQ_FOREACH(mount, &mounts, next) {
742 fd = qemu_open(mount->dirname, O_RDONLY);
743 if (fd == -1) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200744 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200745 goto error;
746 }
747
748 /* We try to cull filesytems we know won't work in advance, but other
749 * filesytems may not implement fstrim for less obvious reasons. These
750 * will report EOPNOTSUPP; we simply ignore these errors. Any other
751 * error means an unexpected error, so return it in those cases. In
752 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
753 */
754 ret = ioctl(fd, FITRIM, &r);
755 if (ret == -1) {
756 if (errno != ENOTTY && errno != EOPNOTSUPP) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200757 error_setg_errno(err, errno, "failed to trim %s",
758 mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200759 close(fd);
760 goto error;
761 }
762 }
763 close(fd);
764 }
765
766error:
767 free_fs_mount_list(&mounts);
768}
769#endif /* CONFIG_FSTRIM */
770
771
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300772#define LINUX_SYS_STATE_FILE "/sys/power/state"
773#define SUSPEND_SUPPORTED 0
774#define SUSPEND_NOT_SUPPORTED 1
775
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300776static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
777 const char *sysfile_str, Error **err)
778{
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200779 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300780 char *pmutils_path;
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200781 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300782 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300783
784 pmutils_path = g_find_program_in_path(pmutils_bin);
785
786 pid = fork();
787 if (!pid) {
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300788 char buf[32]; /* hopefully big enough */
789 ssize_t ret;
790 int fd;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300791
792 setsid();
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300793 reopen_fd_to_null(0);
794 reopen_fd_to_null(1);
795 reopen_fd_to_null(2);
796
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300797 if (pmutils_path) {
798 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
799 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300800
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300801 /*
802 * If we get here either pm-utils is not installed or execle() has
803 * failed. Let's try the manual method if the caller wants it.
804 */
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300805
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300806 if (!sysfile_str) {
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300807 _exit(SUSPEND_NOT_SUPPORTED);
808 }
809
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300810 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
811 if (fd < 0) {
812 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300813 }
814
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300815 ret = read(fd, buf, sizeof(buf)-1);
816 if (ret <= 0) {
817 _exit(SUSPEND_NOT_SUPPORTED);
818 }
819 buf[ret] = '\0';
820
821 if (strstr(buf, sysfile_str)) {
822 _exit(SUSPEND_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300823 }
824
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300825 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200826 } else if (pid < 0) {
827 error_setg_errno(err, errno, "failed to create child process");
828 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300829 }
830
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200831 ga_wait_child(pid, &status, &local_err);
832 if (error_is_set(&local_err)) {
833 error_propagate(err, local_err);
834 goto out;
835 }
836
837 if (!WIFEXITED(status)) {
838 error_setg(err, "child process has terminated abnormally");
839 goto out;
840 }
841
842 switch (WEXITSTATUS(status)) {
843 case SUSPEND_SUPPORTED:
844 goto out;
845 case SUSPEND_NOT_SUPPORTED:
846 error_setg(err,
847 "the requested suspend mode is not supported by the guest");
848 goto out;
849 default:
850 error_setg(err,
851 "the helper program '%s' returned an unexpected exit status"
852 " code (%d)", pmutils_path, WEXITSTATUS(status));
853 goto out;
854 }
855
856out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300857 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300858}
859
860static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
861 Error **err)
862{
Luiz Capitulino7b376082012-11-27 11:02:04 -0200863 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300864 char *pmutils_path;
Luiz Capitulino7b376082012-11-27 11:02:04 -0200865 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300866 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300867
868 pmutils_path = g_find_program_in_path(pmutils_bin);
869
870 pid = fork();
871 if (pid == 0) {
872 /* child */
873 int fd;
874
875 setsid();
876 reopen_fd_to_null(0);
877 reopen_fd_to_null(1);
878 reopen_fd_to_null(2);
879
880 if (pmutils_path) {
881 execle(pmutils_path, pmutils_bin, NULL, environ);
882 }
883
884 /*
885 * If we get here either pm-utils is not installed or execle() has
886 * failed. Let's try the manual method if the caller wants it.
887 */
888
889 if (!sysfile_str) {
890 _exit(EXIT_FAILURE);
891 }
892
893 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
894 if (fd < 0) {
895 _exit(EXIT_FAILURE);
896 }
897
898 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
899 _exit(EXIT_FAILURE);
900 }
901
902 _exit(EXIT_SUCCESS);
Luiz Capitulino7b376082012-11-27 11:02:04 -0200903 } else if (pid < 0) {
904 error_setg_errno(err, errno, "failed to create child process");
905 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300906 }
907
Luiz Capitulino7b376082012-11-27 11:02:04 -0200908 ga_wait_child(pid, &status, &local_err);
909 if (error_is_set(&local_err)) {
910 error_propagate(err, local_err);
911 goto out;
912 }
913
914 if (!WIFEXITED(status)) {
915 error_setg(err, "child process has terminated abnormally");
916 goto out;
917 }
918
919 if (WEXITSTATUS(status)) {
920 error_setg(err, "child process has failed to suspend");
921 goto out;
922 }
923
924out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300925 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300926}
927
928void qmp_guest_suspend_disk(Error **err)
929{
930 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err);
931 if (error_is_set(err)) {
932 return;
933 }
934
935 guest_suspend("pm-hibernate", "disk", err);
936}
937
Luiz Capitulinofbf42212012-02-28 11:03:04 -0300938void qmp_guest_suspend_ram(Error **err)
939{
940 bios_supports_mode("pm-is-supported", "--suspend", "mem", err);
941 if (error_is_set(err)) {
942 return;
943 }
944
945 guest_suspend("pm-suspend", "mem", err);
946}
947
Luiz Capitulino95f4f402012-02-28 11:03:05 -0300948void qmp_guest_suspend_hybrid(Error **err)
949{
950 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, err);
951 if (error_is_set(err)) {
952 return;
953 }
954
955 guest_suspend("pm-suspend-hybrid", NULL, err);
956}
957
Michal Privoznik3424fc92012-02-29 17:02:23 +0100958static GuestNetworkInterfaceList *
959guest_find_interface(GuestNetworkInterfaceList *head,
960 const char *name)
961{
962 for (; head; head = head->next) {
963 if (strcmp(head->value->name, name) == 0) {
964 break;
965 }
966 }
967
968 return head;
969}
970
971/*
972 * Build information about guest interfaces
973 */
974GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
975{
976 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
977 struct ifaddrs *ifap, *ifa;
Michal Privoznik3424fc92012-02-29 17:02:23 +0100978
979 if (getifaddrs(&ifap) < 0) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -0200980 error_setg_errno(errp, errno, "getifaddrs failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +0100981 goto error;
982 }
983
984 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
985 GuestNetworkInterfaceList *info;
986 GuestIpAddressList **address_list = NULL, *address_item = NULL;
987 char addr4[INET_ADDRSTRLEN];
988 char addr6[INET6_ADDRSTRLEN];
989 int sock;
990 struct ifreq ifr;
991 unsigned char *mac_addr;
992 void *p;
993
994 g_debug("Processing %s interface", ifa->ifa_name);
995
996 info = guest_find_interface(head, ifa->ifa_name);
997
998 if (!info) {
999 info = g_malloc0(sizeof(*info));
1000 info->value = g_malloc0(sizeof(*info->value));
1001 info->value->name = g_strdup(ifa->ifa_name);
1002
1003 if (!cur_item) {
1004 head = cur_item = info;
1005 } else {
1006 cur_item->next = info;
1007 cur_item = info;
1008 }
1009 }
1010
1011 if (!info->value->has_hardware_address &&
1012 ifa->ifa_flags & SIOCGIFHWADDR) {
1013 /* we haven't obtained HW address yet */
1014 sock = socket(PF_INET, SOCK_STREAM, 0);
1015 if (sock == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001016 error_setg_errno(errp, errno, "failed to create socket");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001017 goto error;
1018 }
1019
1020 memset(&ifr, 0, sizeof(ifr));
Jim Meyering1ab516e2012-10-04 13:09:58 +02001021 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001022 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001023 error_setg_errno(errp, errno,
1024 "failed to get MAC address of %s",
1025 ifa->ifa_name);
Markus Armbruster10a21582013-01-16 18:15:09 +01001026 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001027 goto error;
1028 }
1029
Markus Armbruster10a21582013-01-16 18:15:09 +01001030 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001031 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1032
Stefan Weile4ada482013-01-16 18:37:23 +01001033 info->value->hardware_address =
1034 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1035 (int) mac_addr[0], (int) mac_addr[1],
1036 (int) mac_addr[2], (int) mac_addr[3],
1037 (int) mac_addr[4], (int) mac_addr[5]);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001038
1039 info->value->has_hardware_address = true;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001040 }
1041
1042 if (ifa->ifa_addr &&
1043 ifa->ifa_addr->sa_family == AF_INET) {
1044 /* interface with IPv4 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001045 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1046 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001047 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001048 goto error;
1049 }
1050
Markus Armbruster10a21582013-01-16 18:15:09 +01001051 address_item = g_malloc0(sizeof(*address_item));
1052 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001053 address_item->value->ip_address = g_strdup(addr4);
1054 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1055
1056 if (ifa->ifa_netmask) {
1057 /* Count the number of set bits in netmask.
1058 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1059 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1060 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1061 }
1062 } else if (ifa->ifa_addr &&
1063 ifa->ifa_addr->sa_family == AF_INET6) {
1064 /* interface with IPv6 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001065 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1066 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001067 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001068 goto error;
1069 }
1070
Markus Armbruster10a21582013-01-16 18:15:09 +01001071 address_item = g_malloc0(sizeof(*address_item));
1072 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001073 address_item->value->ip_address = g_strdup(addr6);
1074 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1075
1076 if (ifa->ifa_netmask) {
1077 /* Count the number of set bits in netmask.
1078 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1079 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1080 address_item->value->prefix =
1081 ctpop32(((uint32_t *) p)[0]) +
1082 ctpop32(((uint32_t *) p)[1]) +
1083 ctpop32(((uint32_t *) p)[2]) +
1084 ctpop32(((uint32_t *) p)[3]);
1085 }
1086 }
1087
1088 if (!address_item) {
1089 continue;
1090 }
1091
1092 address_list = &info->value->ip_addresses;
1093
1094 while (*address_list && (*address_list)->next) {
1095 address_list = &(*address_list)->next;
1096 }
1097
1098 if (!*address_list) {
1099 *address_list = address_item;
1100 } else {
1101 (*address_list)->next = address_item;
1102 }
1103
1104 info->value->has_ip_addresses = true;
1105
1106
1107 }
1108
1109 freeifaddrs(ifap);
1110 return head;
1111
1112error:
1113 freeifaddrs(ifap);
1114 qapi_free_GuestNetworkInterfaceList(head);
1115 return NULL;
1116}
1117
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001118#define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1119
1120static long sysconf_exact(int name, const char *name_str, Error **err)
1121{
1122 long ret;
1123
1124 errno = 0;
1125 ret = sysconf(name);
1126 if (ret == -1) {
1127 if (errno == 0) {
1128 error_setg(err, "sysconf(%s): value indefinite", name_str);
1129 } else {
1130 error_setg_errno(err, errno, "sysconf(%s)", name_str);
1131 }
1132 }
1133 return ret;
1134}
1135
1136/* Transfer online/offline status between @vcpu and the guest system.
1137 *
1138 * On input either @errp or *@errp must be NULL.
1139 *
1140 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1141 * - R: vcpu->logical_id
1142 * - W: vcpu->online
1143 * - W: vcpu->can_offline
1144 *
1145 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1146 * - R: vcpu->logical_id
1147 * - R: vcpu->online
1148 *
1149 * Written members remain unmodified on error.
1150 */
1151static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1152 Error **errp)
1153{
1154 char *dirpath;
1155 int dirfd;
1156
1157 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1158 vcpu->logical_id);
1159 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1160 if (dirfd == -1) {
1161 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1162 } else {
1163 static const char fn[] = "online";
1164 int fd;
1165 int res;
1166
1167 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1168 if (fd == -1) {
1169 if (errno != ENOENT) {
1170 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1171 } else if (sys2vcpu) {
1172 vcpu->online = true;
1173 vcpu->can_offline = false;
1174 } else if (!vcpu->online) {
1175 error_setg(errp, "logical processor #%" PRId64 " can't be "
1176 "offlined", vcpu->logical_id);
1177 } /* otherwise pretend successful re-onlining */
1178 } else {
1179 unsigned char status;
1180
1181 res = pread(fd, &status, 1, 0);
1182 if (res == -1) {
1183 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1184 } else if (res == 0) {
1185 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1186 fn);
1187 } else if (sys2vcpu) {
1188 vcpu->online = (status != '0');
1189 vcpu->can_offline = true;
1190 } else if (vcpu->online != (status != '0')) {
1191 status = '0' + vcpu->online;
1192 if (pwrite(fd, &status, 1, 0) == -1) {
1193 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1194 fn);
1195 }
1196 } /* otherwise pretend successful re-(on|off)-lining */
1197
1198 res = close(fd);
1199 g_assert(res == 0);
1200 }
1201
1202 res = close(dirfd);
1203 g_assert(res == 0);
1204 }
1205
1206 g_free(dirpath);
1207}
1208
1209GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1210{
1211 int64_t current;
1212 GuestLogicalProcessorList *head, **link;
1213 long sc_max;
1214 Error *local_err = NULL;
1215
1216 current = 0;
1217 head = NULL;
1218 link = &head;
1219 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1220
1221 while (local_err == NULL && current < sc_max) {
1222 GuestLogicalProcessor *vcpu;
1223 GuestLogicalProcessorList *entry;
1224
1225 vcpu = g_malloc0(sizeof *vcpu);
1226 vcpu->logical_id = current++;
1227 vcpu->has_can_offline = true; /* lolspeak ftw */
1228 transfer_vcpu(vcpu, true, &local_err);
1229
1230 entry = g_malloc0(sizeof *entry);
1231 entry->value = vcpu;
1232
1233 *link = entry;
1234 link = &entry->next;
1235 }
1236
1237 if (local_err == NULL) {
1238 /* there's no guest with zero VCPUs */
1239 g_assert(head != NULL);
1240 return head;
1241 }
1242
1243 qapi_free_GuestLogicalProcessorList(head);
1244 error_propagate(errp, local_err);
1245 return NULL;
1246}
1247
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001248int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1249{
1250 int64_t processed;
1251 Error *local_err = NULL;
1252
1253 processed = 0;
1254 while (vcpus != NULL) {
1255 transfer_vcpu(vcpus->value, false, &local_err);
1256 if (local_err != NULL) {
1257 break;
1258 }
1259 ++processed;
1260 vcpus = vcpus->next;
1261 }
1262
1263 if (local_err != NULL) {
1264 if (processed == 0) {
1265 error_propagate(errp, local_err);
1266 } else {
1267 error_free(local_err);
1268 }
1269 }
1270
1271 return processed;
1272}
1273
Michael Rothe72c3f22012-03-25 13:59:41 -05001274#else /* defined(__linux__) */
1275
Michael Rothe72c3f22012-03-25 13:59:41 -05001276void qmp_guest_suspend_disk(Error **err)
1277{
1278 error_set(err, QERR_UNSUPPORTED);
1279}
1280
1281void qmp_guest_suspend_ram(Error **err)
1282{
1283 error_set(err, QERR_UNSUPPORTED);
1284}
1285
1286void qmp_guest_suspend_hybrid(Error **err)
1287{
1288 error_set(err, QERR_UNSUPPORTED);
1289}
1290
1291GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1292{
1293 error_set(errp, QERR_UNSUPPORTED);
1294 return NULL;
1295}
1296
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001297GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1298{
1299 error_set(errp, QERR_UNSUPPORTED);
1300 return NULL;
1301}
1302
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001303int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1304{
1305 error_set(errp, QERR_UNSUPPORTED);
1306 return -1;
1307}
1308
Michael Rothe72c3f22012-03-25 13:59:41 -05001309#endif
1310
Michael Rothd35d4cb2012-04-13 21:07:36 -05001311#if !defined(CONFIG_FSFREEZE)
1312
1313GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
1314{
1315 error_set(err, QERR_UNSUPPORTED);
1316
1317 return 0;
1318}
1319
1320int64_t qmp_guest_fsfreeze_freeze(Error **err)
1321{
1322 error_set(err, QERR_UNSUPPORTED);
1323
1324 return 0;
1325}
1326
1327int64_t qmp_guest_fsfreeze_thaw(Error **err)
1328{
1329 error_set(err, QERR_UNSUPPORTED);
1330
1331 return 0;
1332}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001333#endif /* CONFIG_FSFREEZE */
Michael Rothd35d4cb2012-04-13 21:07:36 -05001334
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001335#if !defined(CONFIG_FSTRIM)
1336void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
1337{
1338 error_set(err, QERR_UNSUPPORTED);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001339}
Michael Rothd35d4cb2012-04-13 21:07:36 -05001340#endif
1341
Michael Rothe3d4d252011-07-19 15:41:55 -05001342/* register init/cleanup routines for stateful command groups */
1343void ga_command_state_init(GAState *s, GACommandState *cs)
1344{
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001345#if defined(CONFIG_FSFREEZE)
Michael Rothf22d85e2012-04-17 19:01:45 -05001346 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001347#endif
Michael Rothe3d4d252011-07-19 15:41:55 -05001348 ga_command_state_add(cs, guest_file_init, NULL);
1349}