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