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