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