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 "qga-qapi-commands.h" 20 #include "qapi/error.h" 21 #include "qapi/qmp/qerror.h" 22 #include "qemu/host-utils.h" 23 #include "qemu/sockets.h" 24 #include "qemu/base64.h" 25 #include "qemu/cutils.h" 26 #include "commands-common.h" 27 #include "cutils.h" 28 29 #ifdef HAVE_UTMPX 30 #include <utmpx.h> 31 #endif 32 33 #ifdef HAVE_GETIFADDRS 34 #include <arpa/inet.h> 35 #include <sys/socket.h> 36 #include <net/if.h> 37 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(CONFIG_SOLARIS) 38 #include <net/if_arp.h> 39 #include <netinet/if_ether.h> 40 #if !defined(ETHER_ADDR_LEN) && defined(ETHERADDRL) 41 #define ETHER_ADDR_LEN ETHERADDRL 42 #endif 43 #else 44 #include <net/ethernet.h> 45 #endif 46 #ifdef CONFIG_SOLARIS 47 #include <sys/sockio.h> 48 #endif 49 #endif 50 51 static void ga_wait_child(pid_t pid, int *status, Error **errp) 52 { 53 pid_t rpid; 54 55 *status = 0; 56 57 rpid = RETRY_ON_EINTR(waitpid(pid, status, 0)); 58 59 if (rpid == -1) { 60 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)", 61 pid); 62 return; 63 } 64 65 g_assert(rpid == pid); 66 } 67 68 static ssize_t ga_pipe_read_str(int fd[2], char **str) 69 { 70 ssize_t n, len = 0; 71 char buf[1024]; 72 73 close(fd[1]); 74 fd[1] = -1; 75 while ((n = read(fd[0], buf, sizeof(buf))) != 0) { 76 if (n < 0) { 77 if (errno == EINTR) { 78 continue; 79 } else { 80 len = -errno; 81 break; 82 } 83 } 84 *str = g_realloc(*str, len + n + 1); 85 memcpy(*str + len, buf, n); 86 len += n; 87 *str[len] = '\0'; 88 } 89 close(fd[0]); 90 fd[0] = -1; 91 92 return len; 93 } 94 95 /* 96 * Helper to run command with input/output redirection, 97 * sending string to stdin and taking error message from 98 * stdout/err. 99 */ 100 static int ga_run_command(const char *argv[], const char *in_str, 101 const char *action, Error **errp) 102 { 103 pid_t pid; 104 int status; 105 int retcode = -1; 106 int infd[2] = { -1, -1 }; 107 int outfd[2] = { -1, -1 }; 108 char *str = NULL; 109 ssize_t len = 0; 110 111 if ((in_str && !g_unix_open_pipe(infd, FD_CLOEXEC, NULL)) || 112 !g_unix_open_pipe(outfd, FD_CLOEXEC, NULL)) { 113 error_setg(errp, "cannot create pipe FDs"); 114 goto out; 115 } 116 117 pid = fork(); 118 if (pid == 0) { 119 char *cherr = NULL; 120 121 setsid(); 122 123 if (in_str) { 124 /* Redirect stdin to infd. */ 125 close(infd[1]); 126 dup2(infd[0], 0); 127 close(infd[0]); 128 } else { 129 reopen_fd_to_null(0); 130 } 131 132 /* Redirect stdout/stderr to outfd. */ 133 close(outfd[0]); 134 dup2(outfd[1], 1); 135 dup2(outfd[1], 2); 136 close(outfd[1]); 137 138 execvp(argv[0], (char *const *)argv); 139 140 /* Write the cause of failed exec to pipe for the parent to read it. */ 141 cherr = g_strdup_printf("failed to exec '%s'", argv[0]); 142 perror(cherr); 143 g_free(cherr); 144 _exit(EXIT_FAILURE); 145 } else if (pid < 0) { 146 error_setg_errno(errp, errno, "failed to create child process"); 147 goto out; 148 } 149 150 if (in_str) { 151 close(infd[0]); 152 infd[0] = -1; 153 if (qemu_write_full(infd[1], in_str, strlen(in_str)) != 154 strlen(in_str)) { 155 error_setg_errno(errp, errno, "%s: cannot write to stdin pipe", 156 action); 157 goto out; 158 } 159 close(infd[1]); 160 infd[1] = -1; 161 } 162 163 len = ga_pipe_read_str(outfd, &str); 164 if (len < 0) { 165 error_setg_errno(errp, -len, "%s: cannot read from stdout/stderr pipe", 166 action); 167 goto out; 168 } 169 170 ga_wait_child(pid, &status, errp); 171 if (*errp) { 172 goto out; 173 } 174 175 if (!WIFEXITED(status)) { 176 if (len) { 177 error_setg(errp, "child process has terminated abnormally: %s", 178 str); 179 } else { 180 error_setg(errp, "child process has terminated abnormally"); 181 } 182 goto out; 183 } 184 185 retcode = WEXITSTATUS(status); 186 187 if (WEXITSTATUS(status)) { 188 if (len) { 189 error_setg(errp, "child process has failed to %s: %s", 190 action, str); 191 } else { 192 error_setg(errp, "child process has failed to %s: exit status %d", 193 action, WEXITSTATUS(status)); 194 } 195 goto out; 196 } 197 198 out: 199 g_free(str); 200 201 if (infd[0] != -1) { 202 close(infd[0]); 203 } 204 if (infd[1] != -1) { 205 close(infd[1]); 206 } 207 if (outfd[0] != -1) { 208 close(outfd[0]); 209 } 210 if (outfd[1] != -1) { 211 close(outfd[1]); 212 } 213 214 return retcode; 215 } 216 217 void qmp_guest_shutdown(const char *mode, Error **errp) 218 { 219 const char *shutdown_flag; 220 Error *local_err = NULL; 221 222 #ifdef CONFIG_SOLARIS 223 const char *powerdown_flag = "-i5"; 224 const char *halt_flag = "-i0"; 225 const char *reboot_flag = "-i6"; 226 #elif defined(CONFIG_BSD) 227 const char *powerdown_flag = "-p"; 228 const char *halt_flag = "-h"; 229 const char *reboot_flag = "-r"; 230 #else 231 const char *powerdown_flag = "-P"; 232 const char *halt_flag = "-H"; 233 const char *reboot_flag = "-r"; 234 #endif 235 236 slog("guest-shutdown called, mode: %s", mode); 237 if (!mode || strcmp(mode, "powerdown") == 0) { 238 shutdown_flag = powerdown_flag; 239 } else if (strcmp(mode, "halt") == 0) { 240 shutdown_flag = halt_flag; 241 } else if (strcmp(mode, "reboot") == 0) { 242 shutdown_flag = reboot_flag; 243 } else { 244 error_setg(errp, 245 "mode is invalid (valid values are: halt|powerdown|reboot"); 246 return; 247 } 248 249 const char *argv[] = {"/sbin/shutdown", 250 #ifdef CONFIG_SOLARIS 251 shutdown_flag, "-g0", "-y", 252 #elif defined(CONFIG_BSD) 253 shutdown_flag, "+0", 254 #else 255 "-h", shutdown_flag, "+0", 256 #endif 257 "hypervisor initiated shutdown", (char *) NULL}; 258 259 ga_run_command(argv, NULL, "shutdown", &local_err); 260 if (local_err) { 261 error_propagate(errp, local_err); 262 return; 263 } 264 265 /* succeeded */ 266 } 267 268 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) 269 { 270 int ret; 271 Error *local_err = NULL; 272 struct timeval tv; 273 const char *argv[] = {"/sbin/hwclock", has_time ? "-w" : "-s", NULL}; 274 275 /* If user has passed a time, validate and set it. */ 276 if (has_time) { 277 GDate date = { 0, }; 278 279 /* year-2038 will overflow in case time_t is 32bit */ 280 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) { 281 error_setg(errp, "Time %" PRId64 " is too large", time_ns); 282 return; 283 } 284 285 tv.tv_sec = time_ns / 1000000000; 286 tv.tv_usec = (time_ns % 1000000000) / 1000; 287 g_date_set_time_t(&date, tv.tv_sec); 288 if (date.year < 1970 || date.year >= 2070) { 289 error_setg_errno(errp, errno, "Invalid time"); 290 return; 291 } 292 293 ret = settimeofday(&tv, NULL); 294 if (ret < 0) { 295 error_setg_errno(errp, errno, "Failed to set time to guest"); 296 return; 297 } 298 } 299 300 /* Now, if user has passed a time to set and the system time is set, we 301 * just need to synchronize the hardware clock. However, if no time was 302 * passed, user is requesting the opposite: set the system time from the 303 * hardware clock (RTC). */ 304 ga_run_command(argv, NULL, "set hardware clock to system time", 305 &local_err); 306 if (local_err) { 307 error_propagate(errp, local_err); 308 return; 309 } 310 } 311 312 typedef enum { 313 RW_STATE_NEW, 314 RW_STATE_READING, 315 RW_STATE_WRITING, 316 } RwState; 317 318 struct GuestFileHandle { 319 uint64_t id; 320 FILE *fh; 321 RwState state; 322 QTAILQ_ENTRY(GuestFileHandle) next; 323 }; 324 325 static struct { 326 QTAILQ_HEAD(, GuestFileHandle) filehandles; 327 } guest_file_state = { 328 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles), 329 }; 330 331 static int64_t guest_file_handle_add(FILE *fh, Error **errp) 332 { 333 GuestFileHandle *gfh; 334 int64_t handle; 335 336 handle = ga_get_fd_handle(ga_state, errp); 337 if (handle < 0) { 338 return -1; 339 } 340 341 gfh = g_new0(GuestFileHandle, 1); 342 gfh->id = handle; 343 gfh->fh = fh; 344 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next); 345 346 return handle; 347 } 348 349 GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp) 350 { 351 GuestFileHandle *gfh; 352 353 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) 354 { 355 if (gfh->id == id) { 356 return gfh; 357 } 358 } 359 360 error_setg(errp, "handle '%" PRId64 "' has not been found", id); 361 return NULL; 362 } 363 364 typedef const char * const ccpc; 365 366 #ifndef O_BINARY 367 #define O_BINARY 0 368 #endif 369 370 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */ 371 static const struct { 372 ccpc *forms; 373 int oflag_base; 374 } guest_file_open_modes[] = { 375 { (ccpc[]){ "r", NULL }, O_RDONLY }, 376 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY }, 377 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC }, 378 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY }, 379 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND }, 380 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY }, 381 { (ccpc[]){ "r+", NULL }, O_RDWR }, 382 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY }, 383 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC }, 384 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY }, 385 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND }, 386 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY } 387 }; 388 389 static int 390 find_open_flag(const char *mode_str, Error **errp) 391 { 392 unsigned mode; 393 394 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { 395 ccpc *form; 396 397 form = guest_file_open_modes[mode].forms; 398 while (*form != NULL && strcmp(*form, mode_str) != 0) { 399 ++form; 400 } 401 if (*form != NULL) { 402 break; 403 } 404 } 405 406 if (mode == ARRAY_SIZE(guest_file_open_modes)) { 407 error_setg(errp, "invalid file open mode '%s'", mode_str); 408 return -1; 409 } 410 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK; 411 } 412 413 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \ 414 S_IRGRP | S_IWGRP | \ 415 S_IROTH | S_IWOTH) 416 417 static FILE * 418 safe_open_or_create(const char *path, const char *mode, Error **errp) 419 { 420 int oflag; 421 int fd = -1; 422 FILE *f = NULL; 423 424 oflag = find_open_flag(mode, errp); 425 if (oflag < 0) { 426 goto end; 427 } 428 429 /* If the caller wants / allows creation of a new file, we implement it 430 * with a two step process: open() + (open() / fchmod()). 431 * 432 * First we insist on creating the file exclusively as a new file. If 433 * that succeeds, we're free to set any file-mode bits on it. (The 434 * motivation is that we want to set those file-mode bits independently 435 * of the current umask.) 436 * 437 * If the exclusive creation fails because the file already exists 438 * (EEXIST is not possible for any other reason), we just attempt to 439 * open the file, but in this case we won't be allowed to change the 440 * file-mode bits on the preexistent file. 441 * 442 * The pathname should never disappear between the two open()s in 443 * practice. If it happens, then someone very likely tried to race us. 444 * In this case just go ahead and report the ENOENT from the second 445 * open() to the caller. 446 * 447 * If the caller wants to open a preexistent file, then the first 448 * open() is decisive and its third argument is ignored, and the second 449 * open() and the fchmod() are never called. 450 */ 451 fd = qga_open_cloexec(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0); 452 if (fd == -1 && errno == EEXIST) { 453 oflag &= ~(unsigned)O_CREAT; 454 fd = qga_open_cloexec(path, oflag, 0); 455 } 456 if (fd == -1) { 457 error_setg_errno(errp, errno, 458 "failed to open file '%s' (mode: '%s')", 459 path, mode); 460 goto end; 461 } 462 463 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) { 464 error_setg_errno(errp, errno, "failed to set permission " 465 "0%03o on new file '%s' (mode: '%s')", 466 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode); 467 goto end; 468 } 469 470 f = fdopen(fd, mode); 471 if (f == NULL) { 472 error_setg_errno(errp, errno, "failed to associate stdio stream with " 473 "file descriptor %d, file '%s' (mode: '%s')", 474 fd, path, mode); 475 } 476 477 end: 478 if (f == NULL && fd != -1) { 479 close(fd); 480 if (oflag & O_CREAT) { 481 unlink(path); 482 } 483 } 484 return f; 485 } 486 487 int64_t qmp_guest_file_open(const char *path, const char *mode, 488 Error **errp) 489 { 490 FILE *fh; 491 Error *local_err = NULL; 492 int64_t handle; 493 494 if (!mode) { 495 mode = "r"; 496 } 497 slog("guest-file-open called, filepath: %s, mode: %s", path, mode); 498 fh = safe_open_or_create(path, mode, &local_err); 499 if (local_err != NULL) { 500 error_propagate(errp, local_err); 501 return -1; 502 } 503 504 /* set fd non-blocking to avoid common use cases (like reading from a 505 * named pipe) from hanging the agent 506 */ 507 if (!g_unix_set_fd_nonblocking(fileno(fh), true, NULL)) { 508 fclose(fh); 509 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 510 return -1; 511 } 512 513 handle = guest_file_handle_add(fh, errp); 514 if (handle < 0) { 515 fclose(fh); 516 return -1; 517 } 518 519 slog("guest-file-open, handle: %" PRId64, handle); 520 return handle; 521 } 522 523 void qmp_guest_file_close(int64_t handle, Error **errp) 524 { 525 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 526 int ret; 527 528 slog("guest-file-close called, handle: %" PRId64, handle); 529 if (!gfh) { 530 return; 531 } 532 533 ret = fclose(gfh->fh); 534 if (ret == EOF) { 535 error_setg_errno(errp, errno, "failed to close handle"); 536 return; 537 } 538 539 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next); 540 g_free(gfh); 541 } 542 543 GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh, 544 int64_t count, Error **errp) 545 { 546 GuestFileRead *read_data = NULL; 547 guchar *buf; 548 FILE *fh = gfh->fh; 549 size_t read_count; 550 551 /* explicitly flush when switching from writing to reading */ 552 if (gfh->state == RW_STATE_WRITING) { 553 int ret = fflush(fh); 554 if (ret == EOF) { 555 error_setg_errno(errp, errno, "failed to flush file"); 556 return NULL; 557 } 558 gfh->state = RW_STATE_NEW; 559 } 560 561 buf = g_malloc0(count + 1); 562 read_count = fread(buf, 1, count, fh); 563 if (ferror(fh)) { 564 error_setg_errno(errp, errno, "failed to read file"); 565 } else { 566 buf[read_count] = 0; 567 read_data = g_new0(GuestFileRead, 1); 568 read_data->count = read_count; 569 read_data->eof = feof(fh); 570 if (read_count) { 571 read_data->buf_b64 = g_base64_encode(buf, read_count); 572 } 573 gfh->state = RW_STATE_READING; 574 } 575 g_free(buf); 576 clearerr(fh); 577 578 return read_data; 579 } 580 581 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, 582 bool has_count, int64_t count, 583 Error **errp) 584 { 585 GuestFileWrite *write_data = NULL; 586 guchar *buf; 587 gsize buf_len; 588 int write_count; 589 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 590 FILE *fh; 591 592 if (!gfh) { 593 return NULL; 594 } 595 596 fh = gfh->fh; 597 598 if (gfh->state == RW_STATE_READING) { 599 int ret = fseek(fh, 0, SEEK_CUR); 600 if (ret == -1) { 601 error_setg_errno(errp, errno, "failed to seek file"); 602 return NULL; 603 } 604 gfh->state = RW_STATE_NEW; 605 } 606 607 buf = qbase64_decode(buf_b64, -1, &buf_len, errp); 608 if (!buf) { 609 return NULL; 610 } 611 612 if (!has_count) { 613 count = buf_len; 614 } else if (count < 0 || count > buf_len) { 615 error_setg(errp, "value '%" PRId64 "' is invalid for argument count", 616 count); 617 g_free(buf); 618 return NULL; 619 } 620 621 write_count = fwrite(buf, 1, count, fh); 622 if (ferror(fh)) { 623 error_setg_errno(errp, errno, "failed to write to file"); 624 slog("guest-file-write failed, handle: %" PRId64, handle); 625 } else { 626 write_data = g_new0(GuestFileWrite, 1); 627 write_data->count = write_count; 628 write_data->eof = feof(fh); 629 gfh->state = RW_STATE_WRITING; 630 } 631 g_free(buf); 632 clearerr(fh); 633 634 return write_data; 635 } 636 637 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset, 638 GuestFileWhence *whence_code, 639 Error **errp) 640 { 641 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 642 GuestFileSeek *seek_data = NULL; 643 FILE *fh; 644 int ret; 645 int whence; 646 Error *err = NULL; 647 648 if (!gfh) { 649 return NULL; 650 } 651 652 /* We stupidly exposed 'whence':'int' in our qapi */ 653 whence = ga_parse_whence(whence_code, &err); 654 if (err) { 655 error_propagate(errp, err); 656 return NULL; 657 } 658 659 fh = gfh->fh; 660 ret = fseek(fh, offset, whence); 661 if (ret == -1) { 662 error_setg_errno(errp, errno, "failed to seek file"); 663 if (errno == ESPIPE) { 664 /* file is non-seekable, stdio shouldn't be buffering anyways */ 665 gfh->state = RW_STATE_NEW; 666 } 667 } else { 668 seek_data = g_new0(GuestFileSeek, 1); 669 seek_data->position = ftell(fh); 670 seek_data->eof = feof(fh); 671 gfh->state = RW_STATE_NEW; 672 } 673 clearerr(fh); 674 675 return seek_data; 676 } 677 678 void qmp_guest_file_flush(int64_t handle, Error **errp) 679 { 680 GuestFileHandle *gfh = guest_file_handle_find(handle, errp); 681 FILE *fh; 682 int ret; 683 684 if (!gfh) { 685 return; 686 } 687 688 fh = gfh->fh; 689 ret = fflush(fh); 690 if (ret == EOF) { 691 error_setg_errno(errp, errno, "failed to flush file"); 692 } else { 693 gfh->state = RW_STATE_NEW; 694 } 695 } 696 697 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) 698 void free_fs_mount_list(FsMountList *mounts) 699 { 700 FsMount *mount, *temp; 701 702 if (!mounts) { 703 return; 704 } 705 706 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) { 707 QTAILQ_REMOVE(mounts, mount, next); 708 g_free(mount->dirname); 709 g_free(mount->devtype); 710 g_free(mount); 711 } 712 } 713 #endif 714 715 #if defined(CONFIG_FSFREEZE) 716 typedef enum { 717 FSFREEZE_HOOK_THAW = 0, 718 FSFREEZE_HOOK_FREEZE, 719 } FsfreezeHookArg; 720 721 static const char *fsfreeze_hook_arg_string[] = { 722 "thaw", 723 "freeze", 724 }; 725 726 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp) 727 { 728 const char *hook; 729 const char *arg_str = fsfreeze_hook_arg_string[arg]; 730 Error *local_err = NULL; 731 732 hook = ga_fsfreeze_hook(ga_state); 733 if (!hook) { 734 return; 735 } 736 737 const char *argv[] = {hook, arg_str, NULL}; 738 739 slog("executing fsfreeze hook with arg '%s'", arg_str); 740 ga_run_command(argv, NULL, "execute fsfreeze hook", &local_err); 741 if (local_err) { 742 error_propagate(errp, local_err); 743 return; 744 } 745 } 746 747 /* 748 * Return status of freeze/thaw 749 */ 750 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) 751 { 752 if (ga_is_frozen(ga_state)) { 753 return GUEST_FSFREEZE_STATUS_FROZEN; 754 } 755 756 return GUEST_FSFREEZE_STATUS_THAWED; 757 } 758 759 int64_t qmp_guest_fsfreeze_freeze(Error **errp) 760 { 761 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); 762 } 763 764 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, 765 strList *mountpoints, 766 Error **errp) 767 { 768 int ret; 769 FsMountList mounts; 770 Error *local_err = NULL; 771 772 slog("guest-fsfreeze called"); 773 774 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err); 775 if (local_err) { 776 error_propagate(errp, local_err); 777 return -1; 778 } 779 780 QTAILQ_INIT(&mounts); 781 if (!build_fs_mount_list(&mounts, &local_err)) { 782 error_propagate(errp, local_err); 783 return -1; 784 } 785 786 /* cannot risk guest agent blocking itself on a write in this state */ 787 ga_set_frozen(ga_state); 788 789 ret = qmp_guest_fsfreeze_do_freeze_list(has_mountpoints, mountpoints, 790 mounts, errp); 791 792 free_fs_mount_list(&mounts); 793 /* We may not issue any FIFREEZE here. 794 * Just unset ga_state here and ready for the next call. 795 */ 796 if (ret == 0) { 797 ga_unset_frozen(ga_state); 798 } else if (ret < 0) { 799 qmp_guest_fsfreeze_thaw(NULL); 800 } 801 return ret; 802 } 803 804 int64_t qmp_guest_fsfreeze_thaw(Error **errp) 805 { 806 int ret; 807 808 ret = qmp_guest_fsfreeze_do_thaw(errp); 809 if (ret >= 0) { 810 ga_unset_frozen(ga_state); 811 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); 812 } else { 813 ret = 0; 814 } 815 816 return ret; 817 } 818 819 static void guest_fsfreeze_cleanup(void) 820 { 821 Error *err = NULL; 822 823 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { 824 qmp_guest_fsfreeze_thaw(&err); 825 if (err) { 826 slog("failed to clean up frozen filesystems: %s", 827 error_get_pretty(err)); 828 error_free(err); 829 } 830 } 831 } 832 #endif 833 834 #if defined(__linux__) || defined(__FreeBSD__) 835 void qmp_guest_set_user_password(const char *username, 836 const char *password, 837 bool crypted, 838 Error **errp) 839 { 840 Error *local_err = NULL; 841 g_autofree char *rawpasswddata = NULL; 842 size_t rawpasswdlen; 843 844 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp); 845 if (!rawpasswddata) { 846 return; 847 } 848 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); 849 rawpasswddata[rawpasswdlen] = '\0'; 850 851 if (strchr(rawpasswddata, '\n')) { 852 error_setg(errp, "forbidden characters in raw password"); 853 return; 854 } 855 856 if (strchr(username, '\n') || 857 strchr(username, ':')) { 858 error_setg(errp, "forbidden characters in username"); 859 return; 860 } 861 862 #ifdef __FreeBSD__ 863 g_autofree char *chpasswddata = g_strdup(rawpasswddata); 864 const char *crypt_flag = crypted ? "-H" : "-h"; 865 const char *argv[] = {"pw", "usermod", "-n", username, 866 crypt_flag, "0", NULL}; 867 #else 868 g_autofree char *chpasswddata = g_strdup_printf("%s:%s\n", username, 869 rawpasswddata); 870 const char *crypt_flag = crypted ? "-e" : NULL; 871 const char *argv[] = {"chpasswd", crypt_flag, NULL}; 872 #endif 873 874 ga_run_command(argv, chpasswddata, "set user password", &local_err); 875 if (local_err) { 876 error_propagate(errp, local_err); 877 return; 878 } 879 } 880 #else /* __linux__ || __FreeBSD__ */ 881 void qmp_guest_set_user_password(const char *username, 882 const char *password, 883 bool crypted, 884 Error **errp) 885 { 886 error_setg(errp, QERR_UNSUPPORTED); 887 } 888 #endif /* __linux__ || __FreeBSD__ */ 889 890 #ifdef __linux__ 891 static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf, 892 int size, Error **errp) 893 { 894 int fd; 895 int res; 896 897 errno = 0; 898 fd = openat(dirfd, pathname, O_RDONLY); 899 if (fd == -1) { 900 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); 901 return; 902 } 903 904 res = pread(fd, buf, size, 0); 905 if (res == -1) { 906 error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname); 907 } else if (res == 0) { 908 error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname); 909 } 910 close(fd); 911 } 912 913 static void ga_write_sysfs_file(int dirfd, const char *pathname, 914 const char *buf, int size, Error **errp) 915 { 916 int fd; 917 918 errno = 0; 919 fd = openat(dirfd, pathname, O_WRONLY); 920 if (fd == -1) { 921 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); 922 return; 923 } 924 925 if (pwrite(fd, buf, size, 0) == -1) { 926 error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname); 927 } 928 929 close(fd); 930 } 931 932 /* Transfer online/offline status between @mem_blk and the guest system. 933 * 934 * On input either @errp or *@errp must be NULL. 935 * 936 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed: 937 * - R: mem_blk->phys_index 938 * - W: mem_blk->online 939 * - W: mem_blk->can_offline 940 * 941 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed: 942 * - R: mem_blk->phys_index 943 * - R: mem_blk->online 944 *- R: mem_blk->can_offline 945 * Written members remain unmodified on error. 946 */ 947 static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk, 948 GuestMemoryBlockResponse *result, 949 Error **errp) 950 { 951 char *dirpath; 952 int dirfd; 953 char *status; 954 Error *local_err = NULL; 955 956 if (!sys2memblk) { 957 DIR *dp; 958 959 if (!result) { 960 error_setg(errp, "Internal error, 'result' should not be NULL"); 961 return; 962 } 963 errno = 0; 964 dp = opendir("/sys/devices/system/memory/"); 965 /* if there is no 'memory' directory in sysfs, 966 * we think this VM does not support online/offline memory block, 967 * any other solution? 968 */ 969 if (!dp) { 970 if (errno == ENOENT) { 971 result->response = 972 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED; 973 } 974 goto out1; 975 } 976 closedir(dp); 977 } 978 979 dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/", 980 mem_blk->phys_index); 981 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); 982 if (dirfd == -1) { 983 if (sys2memblk) { 984 error_setg_errno(errp, errno, "open(\"%s\")", dirpath); 985 } else { 986 if (errno == ENOENT) { 987 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND; 988 } else { 989 result->response = 990 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; 991 } 992 } 993 g_free(dirpath); 994 goto out1; 995 } 996 g_free(dirpath); 997 998 status = g_malloc0(10); 999 ga_read_sysfs_file(dirfd, "state", status, 10, &local_err); 1000 if (local_err) { 1001 /* treat with sysfs file that not exist in old kernel */ 1002 if (errno == ENOENT) { 1003 error_free(local_err); 1004 if (sys2memblk) { 1005 mem_blk->online = true; 1006 mem_blk->can_offline = false; 1007 } else if (!mem_blk->online) { 1008 result->response = 1009 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED; 1010 } 1011 } else { 1012 if (sys2memblk) { 1013 error_propagate(errp, local_err); 1014 } else { 1015 error_free(local_err); 1016 result->response = 1017 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; 1018 } 1019 } 1020 goto out2; 1021 } 1022 1023 if (sys2memblk) { 1024 char removable = '0'; 1025 1026 mem_blk->online = (strncmp(status, "online", 6) == 0); 1027 1028 ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err); 1029 if (local_err) { 1030 /* if no 'removable' file, it doesn't support offline mem blk */ 1031 if (errno == ENOENT) { 1032 error_free(local_err); 1033 mem_blk->can_offline = false; 1034 } else { 1035 error_propagate(errp, local_err); 1036 } 1037 } else { 1038 mem_blk->can_offline = (removable != '0'); 1039 } 1040 } else { 1041 if (mem_blk->online != (strncmp(status, "online", 6) == 0)) { 1042 const char *new_state = mem_blk->online ? "online" : "offline"; 1043 1044 ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state), 1045 &local_err); 1046 if (local_err) { 1047 error_free(local_err); 1048 result->response = 1049 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; 1050 goto out2; 1051 } 1052 1053 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS; 1054 result->has_error_code = false; 1055 } /* otherwise pretend successful re-(on|off)-lining */ 1056 } 1057 g_free(status); 1058 close(dirfd); 1059 return; 1060 1061 out2: 1062 g_free(status); 1063 close(dirfd); 1064 out1: 1065 if (!sys2memblk) { 1066 result->has_error_code = true; 1067 result->error_code = errno; 1068 } 1069 } 1070 1071 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) 1072 { 1073 GuestMemoryBlockList *head, **tail; 1074 Error *local_err = NULL; 1075 struct dirent *de; 1076 DIR *dp; 1077 1078 head = NULL; 1079 tail = &head; 1080 1081 dp = opendir("/sys/devices/system/memory/"); 1082 if (!dp) { 1083 /* it's ok if this happens to be a system that doesn't expose 1084 * memory blocks via sysfs, but otherwise we should report 1085 * an error 1086 */ 1087 if (errno != ENOENT) { 1088 error_setg_errno(errp, errno, "Can't open directory" 1089 "\"/sys/devices/system/memory/\""); 1090 } 1091 return NULL; 1092 } 1093 1094 /* Note: the phys_index of memory block may be discontinuous, 1095 * this is because a memblk is the unit of the Sparse Memory design, which 1096 * allows discontinuous memory ranges (ex. NUMA), so here we should 1097 * traverse the memory block directory. 1098 */ 1099 while ((de = readdir(dp)) != NULL) { 1100 GuestMemoryBlock *mem_blk; 1101 1102 if ((strncmp(de->d_name, "memory", 6) != 0) || 1103 !(de->d_type & DT_DIR)) { 1104 continue; 1105 } 1106 1107 mem_blk = g_malloc0(sizeof *mem_blk); 1108 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */ 1109 mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10); 1110 mem_blk->has_can_offline = true; /* lolspeak ftw */ 1111 transfer_memory_block(mem_blk, true, NULL, &local_err); 1112 if (local_err) { 1113 break; 1114 } 1115 1116 QAPI_LIST_APPEND(tail, mem_blk); 1117 } 1118 1119 closedir(dp); 1120 if (local_err == NULL) { 1121 /* there's no guest with zero memory blocks */ 1122 if (head == NULL) { 1123 error_setg(errp, "guest reported zero memory blocks!"); 1124 } 1125 return head; 1126 } 1127 1128 qapi_free_GuestMemoryBlockList(head); 1129 error_propagate(errp, local_err); 1130 return NULL; 1131 } 1132 1133 GuestMemoryBlockResponseList * 1134 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) 1135 { 1136 GuestMemoryBlockResponseList *head, **tail; 1137 Error *local_err = NULL; 1138 1139 head = NULL; 1140 tail = &head; 1141 1142 while (mem_blks != NULL) { 1143 GuestMemoryBlockResponse *result; 1144 GuestMemoryBlock *current_mem_blk = mem_blks->value; 1145 1146 result = g_malloc0(sizeof(*result)); 1147 result->phys_index = current_mem_blk->phys_index; 1148 transfer_memory_block(current_mem_blk, false, result, &local_err); 1149 if (local_err) { /* should never happen */ 1150 goto err; 1151 } 1152 1153 QAPI_LIST_APPEND(tail, result); 1154 mem_blks = mem_blks->next; 1155 } 1156 1157 return head; 1158 err: 1159 qapi_free_GuestMemoryBlockResponseList(head); 1160 error_propagate(errp, local_err); 1161 return NULL; 1162 } 1163 1164 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) 1165 { 1166 Error *local_err = NULL; 1167 char *dirpath; 1168 int dirfd; 1169 char *buf; 1170 GuestMemoryBlockInfo *info; 1171 1172 dirpath = g_strdup_printf("/sys/devices/system/memory/"); 1173 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); 1174 if (dirfd == -1) { 1175 error_setg_errno(errp, errno, "open(\"%s\")", dirpath); 1176 g_free(dirpath); 1177 return NULL; 1178 } 1179 g_free(dirpath); 1180 1181 buf = g_malloc0(20); 1182 ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err); 1183 close(dirfd); 1184 if (local_err) { 1185 g_free(buf); 1186 error_propagate(errp, local_err); 1187 return NULL; 1188 } 1189 1190 info = g_new0(GuestMemoryBlockInfo, 1); 1191 info->size = strtol(buf, NULL, 16); /* the unit is bytes */ 1192 1193 g_free(buf); 1194 1195 return info; 1196 } 1197 1198 1199 #else /* defined(__linux__) */ 1200 1201 void qmp_guest_suspend_disk(Error **errp) 1202 { 1203 error_setg(errp, QERR_UNSUPPORTED); 1204 } 1205 1206 void qmp_guest_suspend_ram(Error **errp) 1207 { 1208 error_setg(errp, QERR_UNSUPPORTED); 1209 } 1210 1211 void qmp_guest_suspend_hybrid(Error **errp) 1212 { 1213 error_setg(errp, QERR_UNSUPPORTED); 1214 } 1215 1216 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) 1217 { 1218 error_setg(errp, QERR_UNSUPPORTED); 1219 return NULL; 1220 } 1221 1222 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) 1223 { 1224 error_setg(errp, QERR_UNSUPPORTED); 1225 return -1; 1226 } 1227 1228 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) 1229 { 1230 error_setg(errp, QERR_UNSUPPORTED); 1231 return NULL; 1232 } 1233 1234 GuestMemoryBlockResponseList * 1235 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) 1236 { 1237 error_setg(errp, QERR_UNSUPPORTED); 1238 return NULL; 1239 } 1240 1241 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) 1242 { 1243 error_setg(errp, QERR_UNSUPPORTED); 1244 return NULL; 1245 } 1246 1247 #endif 1248 1249 #ifdef HAVE_GETIFADDRS 1250 static GuestNetworkInterface * 1251 guest_find_interface(GuestNetworkInterfaceList *head, 1252 const char *name) 1253 { 1254 for (; head; head = head->next) { 1255 if (strcmp(head->value->name, name) == 0) { 1256 return head->value; 1257 } 1258 } 1259 1260 return NULL; 1261 } 1262 1263 static int guest_get_network_stats(const char *name, 1264 GuestNetworkInterfaceStat *stats) 1265 { 1266 #ifdef CONFIG_LINUX 1267 int name_len; 1268 char const *devinfo = "/proc/net/dev"; 1269 FILE *fp; 1270 char *line = NULL, *colon; 1271 size_t n = 0; 1272 fp = fopen(devinfo, "r"); 1273 if (!fp) { 1274 g_debug("failed to open network stats %s: %s", devinfo, 1275 g_strerror(errno)); 1276 return -1; 1277 } 1278 name_len = strlen(name); 1279 while (getline(&line, &n, fp) != -1) { 1280 long long dummy; 1281 long long rx_bytes; 1282 long long rx_packets; 1283 long long rx_errs; 1284 long long rx_dropped; 1285 long long tx_bytes; 1286 long long tx_packets; 1287 long long tx_errs; 1288 long long tx_dropped; 1289 char *trim_line; 1290 trim_line = g_strchug(line); 1291 if (trim_line[0] == '\0') { 1292 continue; 1293 } 1294 colon = strchr(trim_line, ':'); 1295 if (!colon) { 1296 continue; 1297 } 1298 if (colon - name_len == trim_line && 1299 strncmp(trim_line, name, name_len) == 0) { 1300 if (sscanf(colon + 1, 1301 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", 1302 &rx_bytes, &rx_packets, &rx_errs, &rx_dropped, 1303 &dummy, &dummy, &dummy, &dummy, 1304 &tx_bytes, &tx_packets, &tx_errs, &tx_dropped, 1305 &dummy, &dummy, &dummy, &dummy) != 16) { 1306 continue; 1307 } 1308 stats->rx_bytes = rx_bytes; 1309 stats->rx_packets = rx_packets; 1310 stats->rx_errs = rx_errs; 1311 stats->rx_dropped = rx_dropped; 1312 stats->tx_bytes = tx_bytes; 1313 stats->tx_packets = tx_packets; 1314 stats->tx_errs = tx_errs; 1315 stats->tx_dropped = tx_dropped; 1316 fclose(fp); 1317 g_free(line); 1318 return 0; 1319 } 1320 } 1321 fclose(fp); 1322 g_free(line); 1323 g_debug("/proc/net/dev: Interface '%s' not found", name); 1324 #else /* !CONFIG_LINUX */ 1325 g_debug("Network stats reporting available only for Linux"); 1326 #endif /* !CONFIG_LINUX */ 1327 return -1; 1328 } 1329 1330 #ifndef CONFIG_BSD 1331 /* 1332 * Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a 1333 * buffer with ETHER_ADDR_LEN length at least. 1334 * 1335 * Returns false in case of an error, otherwise true. "obtained" argument 1336 * is true if a MAC address was obtained successful, otherwise false. 1337 */ 1338 bool guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, 1339 bool *obtained, Error **errp) 1340 { 1341 struct ifreq ifr; 1342 int sock; 1343 1344 *obtained = false; 1345 1346 /* we haven't obtained HW address yet */ 1347 sock = socket(PF_INET, SOCK_STREAM, 0); 1348 if (sock == -1) { 1349 error_setg_errno(errp, errno, "failed to create socket"); 1350 return false; 1351 } 1352 1353 memset(&ifr, 0, sizeof(ifr)); 1354 pstrcpy(ifr.ifr_name, IF_NAMESIZE, ifa->ifa_name); 1355 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) { 1356 /* 1357 * We can't get the hw addr of this interface, but that's not a 1358 * fatal error. 1359 */ 1360 if (errno == EADDRNOTAVAIL) { 1361 /* The interface doesn't have a hw addr (e.g. loopback). */ 1362 g_debug("failed to get MAC address of %s: %s", 1363 ifa->ifa_name, strerror(errno)); 1364 } else{ 1365 g_warning("failed to get MAC address of %s: %s", 1366 ifa->ifa_name, strerror(errno)); 1367 } 1368 } else { 1369 #ifdef CONFIG_SOLARIS 1370 memcpy(buf, &ifr.ifr_addr.sa_data, ETHER_ADDR_LEN); 1371 #else 1372 memcpy(buf, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); 1373 #endif 1374 *obtained = true; 1375 } 1376 close(sock); 1377 return true; 1378 } 1379 #endif /* CONFIG_BSD */ 1380 1381 /* 1382 * Build information about guest interfaces 1383 */ 1384 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) 1385 { 1386 GuestNetworkInterfaceList *head = NULL, **tail = &head; 1387 struct ifaddrs *ifap, *ifa; 1388 1389 if (getifaddrs(&ifap) < 0) { 1390 error_setg_errno(errp, errno, "getifaddrs failed"); 1391 goto error; 1392 } 1393 1394 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1395 GuestNetworkInterface *info; 1396 GuestIpAddressList **address_tail; 1397 GuestIpAddress *address_item = NULL; 1398 GuestNetworkInterfaceStat *interface_stat = NULL; 1399 char addr4[INET_ADDRSTRLEN]; 1400 char addr6[INET6_ADDRSTRLEN]; 1401 unsigned char mac_addr[ETHER_ADDR_LEN]; 1402 bool obtained; 1403 void *p; 1404 1405 g_debug("Processing %s interface", ifa->ifa_name); 1406 1407 info = guest_find_interface(head, ifa->ifa_name); 1408 1409 if (!info) { 1410 info = g_malloc0(sizeof(*info)); 1411 info->name = g_strdup(ifa->ifa_name); 1412 1413 QAPI_LIST_APPEND(tail, info); 1414 } 1415 1416 if (!info->hardware_address) { 1417 if (!guest_get_hw_addr(ifa, mac_addr, &obtained, errp)) { 1418 goto error; 1419 } 1420 if (obtained) { 1421 info->hardware_address = 1422 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", 1423 (int) mac_addr[0], (int) mac_addr[1], 1424 (int) mac_addr[2], (int) mac_addr[3], 1425 (int) mac_addr[4], (int) mac_addr[5]); 1426 } 1427 } 1428 1429 if (ifa->ifa_addr && 1430 ifa->ifa_addr->sa_family == AF_INET) { 1431 /* interface with IPv4 address */ 1432 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; 1433 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) { 1434 error_setg_errno(errp, errno, "inet_ntop failed"); 1435 goto error; 1436 } 1437 1438 address_item = g_malloc0(sizeof(*address_item)); 1439 address_item->ip_address = g_strdup(addr4); 1440 address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4; 1441 1442 if (ifa->ifa_netmask) { 1443 /* Count the number of set bits in netmask. 1444 * This is safe as '1' and '0' cannot be shuffled in netmask. */ 1445 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr; 1446 address_item->prefix = ctpop32(((uint32_t *) p)[0]); 1447 } 1448 } else if (ifa->ifa_addr && 1449 ifa->ifa_addr->sa_family == AF_INET6) { 1450 /* interface with IPv6 address */ 1451 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; 1452 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) { 1453 error_setg_errno(errp, errno, "inet_ntop failed"); 1454 goto error; 1455 } 1456 1457 address_item = g_malloc0(sizeof(*address_item)); 1458 address_item->ip_address = g_strdup(addr6); 1459 address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6; 1460 1461 if (ifa->ifa_netmask) { 1462 /* Count the number of set bits in netmask. 1463 * This is safe as '1' and '0' cannot be shuffled in netmask. */ 1464 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr; 1465 address_item->prefix = 1466 ctpop32(((uint32_t *) p)[0]) + 1467 ctpop32(((uint32_t *) p)[1]) + 1468 ctpop32(((uint32_t *) p)[2]) + 1469 ctpop32(((uint32_t *) p)[3]); 1470 } 1471 } 1472 1473 if (!address_item) { 1474 continue; 1475 } 1476 1477 address_tail = &info->ip_addresses; 1478 while (*address_tail) { 1479 address_tail = &(*address_tail)->next; 1480 } 1481 QAPI_LIST_APPEND(address_tail, address_item); 1482 1483 info->has_ip_addresses = true; 1484 1485 if (!info->statistics) { 1486 interface_stat = g_malloc0(sizeof(*interface_stat)); 1487 if (guest_get_network_stats(info->name, interface_stat) == -1) { 1488 g_free(interface_stat); 1489 } else { 1490 info->statistics = interface_stat; 1491 } 1492 } 1493 } 1494 1495 freeifaddrs(ifap); 1496 return head; 1497 1498 error: 1499 freeifaddrs(ifap); 1500 qapi_free_GuestNetworkInterfaceList(head); 1501 return NULL; 1502 } 1503 1504 #else 1505 1506 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) 1507 { 1508 error_setg(errp, QERR_UNSUPPORTED); 1509 return NULL; 1510 } 1511 1512 #endif /* HAVE_GETIFADDRS */ 1513 1514 #if !defined(CONFIG_FSFREEZE) 1515 1516 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) 1517 { 1518 error_setg(errp, QERR_UNSUPPORTED); 1519 return NULL; 1520 } 1521 1522 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) 1523 { 1524 error_setg(errp, QERR_UNSUPPORTED); 1525 1526 return 0; 1527 } 1528 1529 int64_t qmp_guest_fsfreeze_freeze(Error **errp) 1530 { 1531 error_setg(errp, QERR_UNSUPPORTED); 1532 1533 return 0; 1534 } 1535 1536 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, 1537 strList *mountpoints, 1538 Error **errp) 1539 { 1540 error_setg(errp, QERR_UNSUPPORTED); 1541 1542 return 0; 1543 } 1544 1545 int64_t qmp_guest_fsfreeze_thaw(Error **errp) 1546 { 1547 error_setg(errp, QERR_UNSUPPORTED); 1548 1549 return 0; 1550 } 1551 1552 GuestDiskInfoList *qmp_guest_get_disks(Error **errp) 1553 { 1554 error_setg(errp, QERR_UNSUPPORTED); 1555 return NULL; 1556 } 1557 1558 GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp) 1559 { 1560 error_setg(errp, QERR_UNSUPPORTED); 1561 return NULL; 1562 } 1563 1564 GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) 1565 { 1566 error_setg(errp, QERR_UNSUPPORTED); 1567 return NULL; 1568 } 1569 1570 #endif /* CONFIG_FSFREEZE */ 1571 1572 #if !defined(CONFIG_FSTRIM) 1573 GuestFilesystemTrimResponse * 1574 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) 1575 { 1576 error_setg(errp, QERR_UNSUPPORTED); 1577 return NULL; 1578 } 1579 #endif 1580 1581 /* add unsupported commands to the list of blocked RPCs */ 1582 GList *ga_command_init_blockedrpcs(GList *blockedrpcs) 1583 { 1584 #if !defined(__linux__) 1585 { 1586 const char *list[] = { 1587 "guest-suspend-disk", "guest-suspend-ram", 1588 "guest-suspend-hybrid", "guest-get-vcpus", "guest-set-vcpus", 1589 "guest-get-memory-blocks", "guest-set-memory-blocks", 1590 "guest-get-memory-block-info", 1591 NULL}; 1592 char **p = (char **)list; 1593 1594 while (*p) { 1595 blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++)); 1596 } 1597 } 1598 #endif 1599 1600 #if !defined(HAVE_GETIFADDRS) 1601 blockedrpcs = g_list_append(blockedrpcs, 1602 g_strdup("guest-network-get-interfaces")); 1603 #endif 1604 1605 #if !defined(CONFIG_FSFREEZE) 1606 { 1607 const char *list[] = { 1608 "guest-get-fsinfo", "guest-fsfreeze-status", 1609 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list", 1610 "guest-fsfreeze-thaw", "guest-get-fsinfo", 1611 "guest-get-disks", NULL}; 1612 char **p = (char **)list; 1613 1614 while (*p) { 1615 blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++)); 1616 } 1617 } 1618 #endif 1619 1620 #if !defined(CONFIG_FSTRIM) 1621 blockedrpcs = g_list_append(blockedrpcs, g_strdup("guest-fstrim")); 1622 #endif 1623 1624 blockedrpcs = g_list_append(blockedrpcs, g_strdup("guest-get-devices")); 1625 1626 return blockedrpcs; 1627 } 1628 1629 /* register init/cleanup routines for stateful command groups */ 1630 void ga_command_state_init(GAState *s, GACommandState *cs) 1631 { 1632 #if defined(CONFIG_FSFREEZE) 1633 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); 1634 #endif 1635 } 1636 1637 #ifdef HAVE_UTMPX 1638 1639 #define QGA_MICRO_SECOND_TO_SECOND 1000000 1640 1641 static double ga_get_login_time(struct utmpx *user_info) 1642 { 1643 double seconds = (double)user_info->ut_tv.tv_sec; 1644 double useconds = (double)user_info->ut_tv.tv_usec; 1645 useconds /= QGA_MICRO_SECOND_TO_SECOND; 1646 return seconds + useconds; 1647 } 1648 1649 GuestUserList *qmp_guest_get_users(Error **errp) 1650 { 1651 GHashTable *cache = NULL; 1652 GuestUserList *head = NULL, **tail = &head; 1653 struct utmpx *user_info = NULL; 1654 gpointer value = NULL; 1655 GuestUser *user = NULL; 1656 double login_time = 0; 1657 1658 cache = g_hash_table_new(g_str_hash, g_str_equal); 1659 setutxent(); 1660 1661 for (;;) { 1662 user_info = getutxent(); 1663 if (user_info == NULL) { 1664 break; 1665 } else if (user_info->ut_type != USER_PROCESS) { 1666 continue; 1667 } else if (g_hash_table_contains(cache, user_info->ut_user)) { 1668 value = g_hash_table_lookup(cache, user_info->ut_user); 1669 user = (GuestUser *)value; 1670 login_time = ga_get_login_time(user_info); 1671 /* We're ensuring the earliest login time to be sent */ 1672 if (login_time < user->login_time) { 1673 user->login_time = login_time; 1674 } 1675 continue; 1676 } 1677 1678 user = g_new0(GuestUser, 1); 1679 user->user = g_strdup(user_info->ut_user); 1680 user->login_time = ga_get_login_time(user_info); 1681 1682 g_hash_table_insert(cache, user->user, user); 1683 1684 QAPI_LIST_APPEND(tail, user); 1685 } 1686 endutxent(); 1687 g_hash_table_destroy(cache); 1688 return head; 1689 } 1690 1691 #else 1692 1693 GuestUserList *qmp_guest_get_users(Error **errp) 1694 { 1695 error_setg(errp, QERR_UNSUPPORTED); 1696 return NULL; 1697 } 1698 1699 #endif 1700 1701 /* Replace escaped special characters with their real values. The replacement 1702 * is done in place -- returned value is in the original string. 1703 */ 1704 static void ga_osrelease_replace_special(gchar *value) 1705 { 1706 gchar *p, *p2, quote; 1707 1708 /* Trim the string at first space or semicolon if it is not enclosed in 1709 * single or double quotes. */ 1710 if ((value[0] != '"') || (value[0] == '\'')) { 1711 p = strchr(value, ' '); 1712 if (p != NULL) { 1713 *p = 0; 1714 } 1715 p = strchr(value, ';'); 1716 if (p != NULL) { 1717 *p = 0; 1718 } 1719 return; 1720 } 1721 1722 quote = value[0]; 1723 p2 = value; 1724 p = value + 1; 1725 while (*p != 0) { 1726 if (*p == '\\') { 1727 p++; 1728 switch (*p) { 1729 case '$': 1730 case '\'': 1731 case '"': 1732 case '\\': 1733 case '`': 1734 break; 1735 default: 1736 /* Keep literal backslash followed by whatever is there */ 1737 p--; 1738 break; 1739 } 1740 } else if (*p == quote) { 1741 *p2 = 0; 1742 break; 1743 } 1744 *(p2++) = *(p++); 1745 } 1746 } 1747 1748 static GKeyFile *ga_parse_osrelease(const char *fname) 1749 { 1750 gchar *content = NULL; 1751 gchar *content2 = NULL; 1752 GError *err = NULL; 1753 GKeyFile *keys = g_key_file_new(); 1754 const char *group = "[os-release]\n"; 1755 1756 if (!g_file_get_contents(fname, &content, NULL, &err)) { 1757 slog("failed to read '%s', error: %s", fname, err->message); 1758 goto fail; 1759 } 1760 1761 if (!g_utf8_validate(content, -1, NULL)) { 1762 slog("file is not utf-8 encoded: %s", fname); 1763 goto fail; 1764 } 1765 content2 = g_strdup_printf("%s%s", group, content); 1766 1767 if (!g_key_file_load_from_data(keys, content2, -1, G_KEY_FILE_NONE, 1768 &err)) { 1769 slog("failed to parse file '%s', error: %s", fname, err->message); 1770 goto fail; 1771 } 1772 1773 g_free(content); 1774 g_free(content2); 1775 return keys; 1776 1777 fail: 1778 g_error_free(err); 1779 g_free(content); 1780 g_free(content2); 1781 g_key_file_free(keys); 1782 return NULL; 1783 } 1784 1785 GuestOSInfo *qmp_guest_get_osinfo(Error **errp) 1786 { 1787 GuestOSInfo *info = NULL; 1788 struct utsname kinfo; 1789 GKeyFile *osrelease = NULL; 1790 const char *qga_os_release = g_getenv("QGA_OS_RELEASE"); 1791 1792 info = g_new0(GuestOSInfo, 1); 1793 1794 if (uname(&kinfo) != 0) { 1795 error_setg_errno(errp, errno, "uname failed"); 1796 } else { 1797 info->kernel_version = g_strdup(kinfo.version); 1798 info->kernel_release = g_strdup(kinfo.release); 1799 info->machine = g_strdup(kinfo.machine); 1800 } 1801 1802 if (qga_os_release != NULL) { 1803 osrelease = ga_parse_osrelease(qga_os_release); 1804 } else { 1805 osrelease = ga_parse_osrelease("/etc/os-release"); 1806 if (osrelease == NULL) { 1807 osrelease = ga_parse_osrelease("/usr/lib/os-release"); 1808 } 1809 } 1810 1811 if (osrelease != NULL) { 1812 char *value; 1813 1814 #define GET_FIELD(field, osfield) do { \ 1815 value = g_key_file_get_value(osrelease, "os-release", osfield, NULL); \ 1816 if (value != NULL) { \ 1817 ga_osrelease_replace_special(value); \ 1818 info->field = value; \ 1819 } \ 1820 } while (0) 1821 GET_FIELD(id, "ID"); 1822 GET_FIELD(name, "NAME"); 1823 GET_FIELD(pretty_name, "PRETTY_NAME"); 1824 GET_FIELD(version, "VERSION"); 1825 GET_FIELD(version_id, "VERSION_ID"); 1826 GET_FIELD(variant, "VARIANT"); 1827 GET_FIELD(variant_id, "VARIANT_ID"); 1828 #undef GET_FIELD 1829 1830 g_key_file_free(osrelease); 1831 } 1832 1833 return info; 1834 } 1835 1836 GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) 1837 { 1838 error_setg(errp, QERR_UNSUPPORTED); 1839 1840 return NULL; 1841 } 1842 1843 #ifndef HOST_NAME_MAX 1844 # ifdef _POSIX_HOST_NAME_MAX 1845 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX 1846 # else 1847 # define HOST_NAME_MAX 255 1848 # endif 1849 #endif 1850 1851 char *qga_get_host_name(Error **errp) 1852 { 1853 long len = -1; 1854 g_autofree char *hostname = NULL; 1855 1856 #ifdef _SC_HOST_NAME_MAX 1857 len = sysconf(_SC_HOST_NAME_MAX); 1858 #endif /* _SC_HOST_NAME_MAX */ 1859 1860 if (len < 0) { 1861 len = HOST_NAME_MAX; 1862 } 1863 1864 /* Unfortunately, gethostname() below does not guarantee a 1865 * NULL terminated string. Therefore, allocate one byte more 1866 * to be sure. */ 1867 hostname = g_new0(char, len + 1); 1868 1869 if (gethostname(hostname, len) < 0) { 1870 error_setg_errno(errp, errno, 1871 "cannot get hostname"); 1872 return NULL; 1873 } 1874 1875 return g_steal_pointer(&hostname); 1876 } 1877