xref: /openbmc/qemu/qga/commands-posix.c (revision 329cefe4)
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 #define MAX_NAME_LEN 128
1199 static GuestDiskStatsInfoList *guest_get_diskstats(Error **errp)
1200 {
1201 #ifdef CONFIG_LINUX
1202     GuestDiskStatsInfoList *head = NULL, **tail = &head;
1203     const char *diskstats = "/proc/diskstats";
1204     FILE *fp;
1205     size_t n;
1206     char *line = NULL;
1207 
1208     fp = fopen(diskstats, "r");
1209     if (fp  == NULL) {
1210         error_setg_errno(errp, errno, "open(\"%s\")", diskstats);
1211         return NULL;
1212     }
1213 
1214     while (getline(&line, &n, fp) != -1) {
1215         g_autofree GuestDiskStatsInfo *diskstatinfo = NULL;
1216         g_autofree GuestDiskStats *diskstat = NULL;
1217         char dev_name[MAX_NAME_LEN];
1218         unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks, dc_ticks, fl_ticks;
1219         unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_ios;
1220         unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec;
1221         unsigned long dc_ios, dc_merges, dc_sec, fl_ios;
1222         unsigned int major, minor;
1223         int i;
1224 
1225         i = sscanf(line, "%u %u %s %lu %lu %lu"
1226                    "%lu %lu %lu %lu %u %u %u %u"
1227                    "%lu %lu %lu %u %lu %u",
1228                    &major, &minor, dev_name,
1229                    &rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios,
1230                    &rd_ticks_or_wr_sec, &wr_ios, &wr_merges, &wr_sec,
1231                    &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks,
1232                    &dc_ios, &dc_merges, &dc_sec, &dc_ticks,
1233                    &fl_ios, &fl_ticks);
1234 
1235         if (i < 7) {
1236             continue;
1237         }
1238 
1239         diskstatinfo = g_new0(GuestDiskStatsInfo, 1);
1240         diskstatinfo->name = g_strdup(dev_name);
1241         diskstatinfo->major = major;
1242         diskstatinfo->minor = minor;
1243 
1244         diskstat = g_new0(GuestDiskStats, 1);
1245         if (i == 7) {
1246             diskstat->has_read_ios = true;
1247             diskstat->read_ios = rd_ios;
1248             diskstat->has_read_sectors = true;
1249             diskstat->read_sectors = rd_merges_or_rd_sec;
1250             diskstat->has_write_ios = true;
1251             diskstat->write_ios = rd_sec_or_wr_ios;
1252             diskstat->has_write_sectors = true;
1253             diskstat->write_sectors = rd_ticks_or_wr_sec;
1254         }
1255         if (i >= 14) {
1256             diskstat->has_read_ios = true;
1257             diskstat->read_ios = rd_ios;
1258             diskstat->has_read_sectors = true;
1259             diskstat->read_sectors = rd_sec_or_wr_ios;
1260             diskstat->has_read_merges = true;
1261             diskstat->read_merges = rd_merges_or_rd_sec;
1262             diskstat->has_read_ticks = true;
1263             diskstat->read_ticks = rd_ticks_or_wr_sec;
1264             diskstat->has_write_ios = true;
1265             diskstat->write_ios = wr_ios;
1266             diskstat->has_write_sectors = true;
1267             diskstat->write_sectors = wr_sec;
1268             diskstat->has_write_merges = true;
1269             diskstat->write_merges = wr_merges;
1270             diskstat->has_write_ticks = true;
1271             diskstat->write_ticks = wr_ticks;
1272             diskstat->has_ios_pgr = true;
1273             diskstat->ios_pgr = ios_pgr;
1274             diskstat->has_total_ticks = true;
1275             diskstat->total_ticks = tot_ticks;
1276             diskstat->has_weight_ticks = true;
1277             diskstat->weight_ticks = rq_ticks;
1278         }
1279         if (i >= 18) {
1280             diskstat->has_discard_ios = true;
1281             diskstat->discard_ios = dc_ios;
1282             diskstat->has_discard_merges = true;
1283             diskstat->discard_merges = dc_merges;
1284             diskstat->has_discard_sectors = true;
1285             diskstat->discard_sectors = dc_sec;
1286             diskstat->has_discard_ticks = true;
1287             diskstat->discard_ticks = dc_ticks;
1288         }
1289         if (i >= 20) {
1290             diskstat->has_flush_ios = true;
1291             diskstat->flush_ios = fl_ios;
1292             diskstat->has_flush_ticks = true;
1293             diskstat->flush_ticks = fl_ticks;
1294         }
1295 
1296         diskstatinfo->stats = g_steal_pointer(&diskstat);
1297         QAPI_LIST_APPEND(tail, diskstatinfo);
1298         diskstatinfo = NULL;
1299     }
1300     free(line);
1301     fclose(fp);
1302     return head;
1303 #else
1304     g_debug("disk stats reporting available only for Linux");
1305     return NULL;
1306 #endif
1307 }
1308 
1309 GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp)
1310 {
1311     return guest_get_diskstats(errp);
1312 }
1313 
1314 GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
1315 {
1316     GuestCpuStatsList *head = NULL, **tail = &head;
1317     const char *cpustats = "/proc/stat";
1318     int clk_tck = sysconf(_SC_CLK_TCK);
1319     FILE *fp;
1320     size_t n;
1321     char *line = NULL;
1322 
1323     fp = fopen(cpustats, "r");
1324     if (fp  == NULL) {
1325         error_setg_errno(errp, errno, "open(\"%s\")", cpustats);
1326         return NULL;
1327     }
1328 
1329     while (getline(&line, &n, fp) != -1) {
1330         GuestCpuStats *cpustat = NULL;
1331         GuestLinuxCpuStats *linuxcpustat;
1332         int i;
1333         unsigned long user, system, idle, iowait, irq, softirq, steal, guest;
1334         unsigned long nice, guest_nice;
1335         char name[64];
1336 
1337         i = sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
1338                    name, &user, &nice, &system, &idle, &iowait, &irq, &softirq,
1339                    &steal, &guest, &guest_nice);
1340 
1341         /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */
1342         if ((i == EOF) || strncmp(name, "cpu", 3) || (name[3] == '\0')) {
1343             continue;
1344         }
1345 
1346         if (i < 5) {
1347             slog("Parsing cpu stat from %s failed, see \"man proc\"", cpustats);
1348             break;
1349         }
1350 
1351         cpustat = g_new0(GuestCpuStats, 1);
1352         cpustat->type = GUEST_CPU_STATS_TYPE_LINUX;
1353 
1354         linuxcpustat = &cpustat->u.q_linux;
1355         linuxcpustat->cpu = atoi(&name[3]);
1356         linuxcpustat->user = user * 1000 / clk_tck;
1357         linuxcpustat->nice = nice * 1000 / clk_tck;
1358         linuxcpustat->system = system * 1000 / clk_tck;
1359         linuxcpustat->idle = idle * 1000 / clk_tck;
1360 
1361         if (i > 5) {
1362             linuxcpustat->has_iowait = true;
1363             linuxcpustat->iowait = iowait * 1000 / clk_tck;
1364         }
1365 
1366         if (i > 6) {
1367             linuxcpustat->has_irq = true;
1368             linuxcpustat->irq = irq * 1000 / clk_tck;
1369             linuxcpustat->has_softirq = true;
1370             linuxcpustat->softirq = softirq * 1000 / clk_tck;
1371         }
1372 
1373         if (i > 8) {
1374             linuxcpustat->has_steal = true;
1375             linuxcpustat->steal = steal * 1000 / clk_tck;
1376         }
1377 
1378         if (i > 9) {
1379             linuxcpustat->has_guest = true;
1380             linuxcpustat->guest = guest * 1000 / clk_tck;
1381         }
1382 
1383         if (i > 10) {
1384             linuxcpustat->has_guest = true;
1385             linuxcpustat->guest = guest * 1000 / clk_tck;
1386             linuxcpustat->has_guestnice = true;
1387             linuxcpustat->guestnice = guest_nice * 1000 / clk_tck;
1388         }
1389 
1390         QAPI_LIST_APPEND(tail, cpustat);
1391     }
1392 
1393     free(line);
1394     fclose(fp);
1395     return head;
1396 }
1397 
1398 #else /* defined(__linux__) */
1399 
1400 void qmp_guest_suspend_disk(Error **errp)
1401 {
1402     error_setg(errp, QERR_UNSUPPORTED);
1403 }
1404 
1405 void qmp_guest_suspend_ram(Error **errp)
1406 {
1407     error_setg(errp, QERR_UNSUPPORTED);
1408 }
1409 
1410 void qmp_guest_suspend_hybrid(Error **errp)
1411 {
1412     error_setg(errp, QERR_UNSUPPORTED);
1413 }
1414 
1415 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1416 {
1417     error_setg(errp, QERR_UNSUPPORTED);
1418     return NULL;
1419 }
1420 
1421 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1422 {
1423     error_setg(errp, QERR_UNSUPPORTED);
1424     return -1;
1425 }
1426 
1427 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
1428 {
1429     error_setg(errp, QERR_UNSUPPORTED);
1430     return NULL;
1431 }
1432 
1433 GuestMemoryBlockResponseList *
1434 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
1435 {
1436     error_setg(errp, QERR_UNSUPPORTED);
1437     return NULL;
1438 }
1439 
1440 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
1441 {
1442     error_setg(errp, QERR_UNSUPPORTED);
1443     return NULL;
1444 }
1445 
1446 #endif
1447 
1448 #ifdef HAVE_GETIFADDRS
1449 static GuestNetworkInterface *
1450 guest_find_interface(GuestNetworkInterfaceList *head,
1451                      const char *name)
1452 {
1453     for (; head; head = head->next) {
1454         if (strcmp(head->value->name, name) == 0) {
1455             return head->value;
1456         }
1457     }
1458 
1459     return NULL;
1460 }
1461 
1462 static int guest_get_network_stats(const char *name,
1463                        GuestNetworkInterfaceStat *stats)
1464 {
1465 #ifdef CONFIG_LINUX
1466     int name_len;
1467     char const *devinfo = "/proc/net/dev";
1468     FILE *fp;
1469     char *line = NULL, *colon;
1470     size_t n = 0;
1471     fp = fopen(devinfo, "r");
1472     if (!fp) {
1473         g_debug("failed to open network stats %s: %s", devinfo,
1474                 g_strerror(errno));
1475         return -1;
1476     }
1477     name_len = strlen(name);
1478     while (getline(&line, &n, fp) != -1) {
1479         long long dummy;
1480         long long rx_bytes;
1481         long long rx_packets;
1482         long long rx_errs;
1483         long long rx_dropped;
1484         long long tx_bytes;
1485         long long tx_packets;
1486         long long tx_errs;
1487         long long tx_dropped;
1488         char *trim_line;
1489         trim_line = g_strchug(line);
1490         if (trim_line[0] == '\0') {
1491             continue;
1492         }
1493         colon = strchr(trim_line, ':');
1494         if (!colon) {
1495             continue;
1496         }
1497         if (colon - name_len  == trim_line &&
1498            strncmp(trim_line, name, name_len) == 0) {
1499             if (sscanf(colon + 1,
1500                 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
1501                   &rx_bytes, &rx_packets, &rx_errs, &rx_dropped,
1502                   &dummy, &dummy, &dummy, &dummy,
1503                   &tx_bytes, &tx_packets, &tx_errs, &tx_dropped,
1504                   &dummy, &dummy, &dummy, &dummy) != 16) {
1505                 continue;
1506             }
1507             stats->rx_bytes = rx_bytes;
1508             stats->rx_packets = rx_packets;
1509             stats->rx_errs = rx_errs;
1510             stats->rx_dropped = rx_dropped;
1511             stats->tx_bytes = tx_bytes;
1512             stats->tx_packets = tx_packets;
1513             stats->tx_errs = tx_errs;
1514             stats->tx_dropped = tx_dropped;
1515             fclose(fp);
1516             g_free(line);
1517             return 0;
1518         }
1519     }
1520     fclose(fp);
1521     g_free(line);
1522     g_debug("/proc/net/dev: Interface '%s' not found", name);
1523 #else /* !CONFIG_LINUX */
1524     g_debug("Network stats reporting available only for Linux");
1525 #endif /* !CONFIG_LINUX */
1526     return -1;
1527 }
1528 
1529 #ifndef CONFIG_BSD
1530 /*
1531  * Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a
1532  * buffer with ETHER_ADDR_LEN length at least.
1533  *
1534  * Returns false in case of an error, otherwise true. "obtained" argument
1535  * is true if a MAC address was obtained successful, otherwise false.
1536  */
1537 bool guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf,
1538                        bool *obtained, Error **errp)
1539 {
1540     struct ifreq ifr;
1541     int sock;
1542 
1543     *obtained = false;
1544 
1545     /* we haven't obtained HW address yet */
1546     sock = socket(PF_INET, SOCK_STREAM, 0);
1547     if (sock == -1) {
1548         error_setg_errno(errp, errno, "failed to create socket");
1549         return false;
1550     }
1551 
1552     memset(&ifr, 0, sizeof(ifr));
1553     pstrcpy(ifr.ifr_name, IF_NAMESIZE, ifa->ifa_name);
1554     if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
1555         /*
1556          * We can't get the hw addr of this interface, but that's not a
1557          * fatal error.
1558          */
1559         if (errno == EADDRNOTAVAIL) {
1560             /* The interface doesn't have a hw addr (e.g. loopback). */
1561             g_debug("failed to get MAC address of %s: %s",
1562                     ifa->ifa_name, strerror(errno));
1563         } else{
1564             g_warning("failed to get MAC address of %s: %s",
1565                       ifa->ifa_name, strerror(errno));
1566         }
1567     } else {
1568 #ifdef CONFIG_SOLARIS
1569         memcpy(buf, &ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
1570 #else
1571         memcpy(buf, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
1572 #endif
1573         *obtained = true;
1574     }
1575     close(sock);
1576     return true;
1577 }
1578 #endif /* CONFIG_BSD */
1579 
1580 /*
1581  * Build information about guest interfaces
1582  */
1583 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1584 {
1585     GuestNetworkInterfaceList *head = NULL, **tail = &head;
1586     struct ifaddrs *ifap, *ifa;
1587 
1588     if (getifaddrs(&ifap) < 0) {
1589         error_setg_errno(errp, errno, "getifaddrs failed");
1590         goto error;
1591     }
1592 
1593     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1594         GuestNetworkInterface *info;
1595         GuestIpAddressList **address_tail;
1596         GuestIpAddress *address_item = NULL;
1597         GuestNetworkInterfaceStat *interface_stat = NULL;
1598         char addr4[INET_ADDRSTRLEN];
1599         char addr6[INET6_ADDRSTRLEN];
1600         unsigned char mac_addr[ETHER_ADDR_LEN];
1601         bool obtained;
1602         void *p;
1603 
1604         g_debug("Processing %s interface", ifa->ifa_name);
1605 
1606         info = guest_find_interface(head, ifa->ifa_name);
1607 
1608         if (!info) {
1609             info = g_malloc0(sizeof(*info));
1610             info->name = g_strdup(ifa->ifa_name);
1611 
1612             QAPI_LIST_APPEND(tail, info);
1613         }
1614 
1615         if (!info->hardware_address) {
1616             if (!guest_get_hw_addr(ifa, mac_addr, &obtained, errp)) {
1617                 goto error;
1618             }
1619             if (obtained) {
1620                 info->hardware_address =
1621                     g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1622                                     (int) mac_addr[0], (int) mac_addr[1],
1623                                     (int) mac_addr[2], (int) mac_addr[3],
1624                                     (int) mac_addr[4], (int) mac_addr[5]);
1625             }
1626         }
1627 
1628         if (ifa->ifa_addr &&
1629             ifa->ifa_addr->sa_family == AF_INET) {
1630             /* interface with IPv4 address */
1631             p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1632             if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
1633                 error_setg_errno(errp, errno, "inet_ntop failed");
1634                 goto error;
1635             }
1636 
1637             address_item = g_malloc0(sizeof(*address_item));
1638             address_item->ip_address = g_strdup(addr4);
1639             address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1640 
1641             if (ifa->ifa_netmask) {
1642                 /* Count the number of set bits in netmask.
1643                  * This is safe as '1' and '0' cannot be shuffled in netmask. */
1644                 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1645                 address_item->prefix = ctpop32(((uint32_t *) p)[0]);
1646             }
1647         } else if (ifa->ifa_addr &&
1648                    ifa->ifa_addr->sa_family == AF_INET6) {
1649             /* interface with IPv6 address */
1650             p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1651             if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
1652                 error_setg_errno(errp, errno, "inet_ntop failed");
1653                 goto error;
1654             }
1655 
1656             address_item = g_malloc0(sizeof(*address_item));
1657             address_item->ip_address = g_strdup(addr6);
1658             address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1659 
1660             if (ifa->ifa_netmask) {
1661                 /* Count the number of set bits in netmask.
1662                  * This is safe as '1' and '0' cannot be shuffled in netmask. */
1663                 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1664                 address_item->prefix =
1665                     ctpop32(((uint32_t *) p)[0]) +
1666                     ctpop32(((uint32_t *) p)[1]) +
1667                     ctpop32(((uint32_t *) p)[2]) +
1668                     ctpop32(((uint32_t *) p)[3]);
1669             }
1670         }
1671 
1672         if (!address_item) {
1673             continue;
1674         }
1675 
1676         address_tail = &info->ip_addresses;
1677         while (*address_tail) {
1678             address_tail = &(*address_tail)->next;
1679         }
1680         QAPI_LIST_APPEND(address_tail, address_item);
1681 
1682         info->has_ip_addresses = true;
1683 
1684         if (!info->statistics) {
1685             interface_stat = g_malloc0(sizeof(*interface_stat));
1686             if (guest_get_network_stats(info->name, interface_stat) == -1) {
1687                 g_free(interface_stat);
1688             } else {
1689                 info->statistics = interface_stat;
1690             }
1691         }
1692     }
1693 
1694     freeifaddrs(ifap);
1695     return head;
1696 
1697 error:
1698     freeifaddrs(ifap);
1699     qapi_free_GuestNetworkInterfaceList(head);
1700     return NULL;
1701 }
1702 
1703 #else
1704 
1705 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1706 {
1707     error_setg(errp, QERR_UNSUPPORTED);
1708     return NULL;
1709 }
1710 
1711 #endif /* HAVE_GETIFADDRS */
1712 
1713 #if !defined(CONFIG_FSFREEZE)
1714 
1715 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1716 {
1717     error_setg(errp, QERR_UNSUPPORTED);
1718     return NULL;
1719 }
1720 
1721 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
1722 {
1723     error_setg(errp, QERR_UNSUPPORTED);
1724 
1725     return 0;
1726 }
1727 
1728 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1729 {
1730     error_setg(errp, QERR_UNSUPPORTED);
1731 
1732     return 0;
1733 }
1734 
1735 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1736                                        strList *mountpoints,
1737                                        Error **errp)
1738 {
1739     error_setg(errp, QERR_UNSUPPORTED);
1740 
1741     return 0;
1742 }
1743 
1744 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
1745 {
1746     error_setg(errp, QERR_UNSUPPORTED);
1747 
1748     return 0;
1749 }
1750 
1751 GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
1752 {
1753     error_setg(errp, QERR_UNSUPPORTED);
1754     return NULL;
1755 }
1756 
1757 GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp)
1758 {
1759     error_setg(errp, QERR_UNSUPPORTED);
1760     return NULL;
1761 }
1762 
1763 GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
1764 {
1765     error_setg(errp, QERR_UNSUPPORTED);
1766     return NULL;
1767 }
1768 
1769 #endif /* CONFIG_FSFREEZE */
1770 
1771 #if !defined(CONFIG_FSTRIM)
1772 GuestFilesystemTrimResponse *
1773 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
1774 {
1775     error_setg(errp, QERR_UNSUPPORTED);
1776     return NULL;
1777 }
1778 #endif
1779 
1780 /* add unsupported commands to the list of blocked RPCs */
1781 GList *ga_command_init_blockedrpcs(GList *blockedrpcs)
1782 {
1783 #if !defined(__linux__)
1784     {
1785         const char *list[] = {
1786             "guest-suspend-disk", "guest-suspend-ram",
1787             "guest-suspend-hybrid", "guest-get-vcpus", "guest-set-vcpus",
1788             "guest-get-memory-blocks", "guest-set-memory-blocks",
1789             "guest-get-memory-block-info",
1790             NULL};
1791         char **p = (char **)list;
1792 
1793         while (*p) {
1794             blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++));
1795         }
1796     }
1797 #endif
1798 
1799 #if !defined(HAVE_GETIFADDRS)
1800     blockedrpcs = g_list_append(blockedrpcs,
1801                               g_strdup("guest-network-get-interfaces"));
1802 #endif
1803 
1804 #if !defined(CONFIG_FSFREEZE)
1805     {
1806         const char *list[] = {
1807             "guest-get-fsinfo", "guest-fsfreeze-status",
1808             "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
1809             "guest-fsfreeze-thaw", "guest-get-fsinfo",
1810             "guest-get-disks", NULL};
1811         char **p = (char **)list;
1812 
1813         while (*p) {
1814             blockedrpcs = g_list_append(blockedrpcs, g_strdup(*p++));
1815         }
1816     }
1817 #endif
1818 
1819 #if !defined(CONFIG_FSTRIM)
1820     blockedrpcs = g_list_append(blockedrpcs, g_strdup("guest-fstrim"));
1821 #endif
1822 
1823     blockedrpcs = g_list_append(blockedrpcs, g_strdup("guest-get-devices"));
1824 
1825     return blockedrpcs;
1826 }
1827 
1828 /* register init/cleanup routines for stateful command groups */
1829 void ga_command_state_init(GAState *s, GACommandState *cs)
1830 {
1831 #if defined(CONFIG_FSFREEZE)
1832     ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
1833 #endif
1834 }
1835 
1836 #ifdef HAVE_UTMPX
1837 
1838 #define QGA_MICRO_SECOND_TO_SECOND 1000000
1839 
1840 static double ga_get_login_time(struct utmpx *user_info)
1841 {
1842     double seconds = (double)user_info->ut_tv.tv_sec;
1843     double useconds = (double)user_info->ut_tv.tv_usec;
1844     useconds /= QGA_MICRO_SECOND_TO_SECOND;
1845     return seconds + useconds;
1846 }
1847 
1848 GuestUserList *qmp_guest_get_users(Error **errp)
1849 {
1850     GHashTable *cache = NULL;
1851     GuestUserList *head = NULL, **tail = &head;
1852     struct utmpx *user_info = NULL;
1853     gpointer value = NULL;
1854     GuestUser *user = NULL;
1855     double login_time = 0;
1856 
1857     cache = g_hash_table_new(g_str_hash, g_str_equal);
1858     setutxent();
1859 
1860     for (;;) {
1861         user_info = getutxent();
1862         if (user_info == NULL) {
1863             break;
1864         } else if (user_info->ut_type != USER_PROCESS) {
1865             continue;
1866         } else if (g_hash_table_contains(cache, user_info->ut_user)) {
1867             value = g_hash_table_lookup(cache, user_info->ut_user);
1868             user = (GuestUser *)value;
1869             login_time = ga_get_login_time(user_info);
1870             /* We're ensuring the earliest login time to be sent */
1871             if (login_time < user->login_time) {
1872                 user->login_time = login_time;
1873             }
1874             continue;
1875         }
1876 
1877         user = g_new0(GuestUser, 1);
1878         user->user = g_strdup(user_info->ut_user);
1879         user->login_time = ga_get_login_time(user_info);
1880 
1881         g_hash_table_insert(cache, user->user, user);
1882 
1883         QAPI_LIST_APPEND(tail, user);
1884     }
1885     endutxent();
1886     g_hash_table_destroy(cache);
1887     return head;
1888 }
1889 
1890 #else
1891 
1892 GuestUserList *qmp_guest_get_users(Error **errp)
1893 {
1894     error_setg(errp, QERR_UNSUPPORTED);
1895     return NULL;
1896 }
1897 
1898 #endif
1899 
1900 /* Replace escaped special characters with their real values. The replacement
1901  * is done in place -- returned value is in the original string.
1902  */
1903 static void ga_osrelease_replace_special(gchar *value)
1904 {
1905     gchar *p, *p2, quote;
1906 
1907     /* Trim the string at first space or semicolon if it is not enclosed in
1908      * single or double quotes. */
1909     if ((value[0] != '"') || (value[0] == '\'')) {
1910         p = strchr(value, ' ');
1911         if (p != NULL) {
1912             *p = 0;
1913         }
1914         p = strchr(value, ';');
1915         if (p != NULL) {
1916             *p = 0;
1917         }
1918         return;
1919     }
1920 
1921     quote = value[0];
1922     p2 = value;
1923     p = value + 1;
1924     while (*p != 0) {
1925         if (*p == '\\') {
1926             p++;
1927             switch (*p) {
1928             case '$':
1929             case '\'':
1930             case '"':
1931             case '\\':
1932             case '`':
1933                 break;
1934             default:
1935                 /* Keep literal backslash followed by whatever is there */
1936                 p--;
1937                 break;
1938             }
1939         } else if (*p == quote) {
1940             *p2 = 0;
1941             break;
1942         }
1943         *(p2++) = *(p++);
1944     }
1945 }
1946 
1947 static GKeyFile *ga_parse_osrelease(const char *fname)
1948 {
1949     gchar *content = NULL;
1950     gchar *content2 = NULL;
1951     GError *err = NULL;
1952     GKeyFile *keys = g_key_file_new();
1953     const char *group = "[os-release]\n";
1954 
1955     if (!g_file_get_contents(fname, &content, NULL, &err)) {
1956         slog("failed to read '%s', error: %s", fname, err->message);
1957         goto fail;
1958     }
1959 
1960     if (!g_utf8_validate(content, -1, NULL)) {
1961         slog("file is not utf-8 encoded: %s", fname);
1962         goto fail;
1963     }
1964     content2 = g_strdup_printf("%s%s", group, content);
1965 
1966     if (!g_key_file_load_from_data(keys, content2, -1, G_KEY_FILE_NONE,
1967                                    &err)) {
1968         slog("failed to parse file '%s', error: %s", fname, err->message);
1969         goto fail;
1970     }
1971 
1972     g_free(content);
1973     g_free(content2);
1974     return keys;
1975 
1976 fail:
1977     g_error_free(err);
1978     g_free(content);
1979     g_free(content2);
1980     g_key_file_free(keys);
1981     return NULL;
1982 }
1983 
1984 GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
1985 {
1986     GuestOSInfo *info = NULL;
1987     struct utsname kinfo;
1988     GKeyFile *osrelease = NULL;
1989     const char *qga_os_release = g_getenv("QGA_OS_RELEASE");
1990 
1991     info = g_new0(GuestOSInfo, 1);
1992 
1993     if (uname(&kinfo) != 0) {
1994         error_setg_errno(errp, errno, "uname failed");
1995     } else {
1996         info->kernel_version = g_strdup(kinfo.version);
1997         info->kernel_release = g_strdup(kinfo.release);
1998         info->machine = g_strdup(kinfo.machine);
1999     }
2000 
2001     if (qga_os_release != NULL) {
2002         osrelease = ga_parse_osrelease(qga_os_release);
2003     } else {
2004         osrelease = ga_parse_osrelease("/etc/os-release");
2005         if (osrelease == NULL) {
2006             osrelease = ga_parse_osrelease("/usr/lib/os-release");
2007         }
2008     }
2009 
2010     if (osrelease != NULL) {
2011         char *value;
2012 
2013 #define GET_FIELD(field, osfield) do { \
2014     value = g_key_file_get_value(osrelease, "os-release", osfield, NULL); \
2015     if (value != NULL) { \
2016         ga_osrelease_replace_special(value); \
2017         info->field = value; \
2018     } \
2019 } while (0)
2020         GET_FIELD(id, "ID");
2021         GET_FIELD(name, "NAME");
2022         GET_FIELD(pretty_name, "PRETTY_NAME");
2023         GET_FIELD(version, "VERSION");
2024         GET_FIELD(version_id, "VERSION_ID");
2025         GET_FIELD(variant, "VARIANT");
2026         GET_FIELD(variant_id, "VARIANT_ID");
2027 #undef GET_FIELD
2028 
2029         g_key_file_free(osrelease);
2030     }
2031 
2032     return info;
2033 }
2034 
2035 GuestDeviceInfoList *qmp_guest_get_devices(Error **errp)
2036 {
2037     error_setg(errp, QERR_UNSUPPORTED);
2038 
2039     return NULL;
2040 }
2041 
2042 #ifndef HOST_NAME_MAX
2043 # ifdef _POSIX_HOST_NAME_MAX
2044 #  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
2045 # else
2046 #  define HOST_NAME_MAX 255
2047 # endif
2048 #endif
2049 
2050 char *qga_get_host_name(Error **errp)
2051 {
2052     long len = -1;
2053     g_autofree char *hostname = NULL;
2054 
2055 #ifdef _SC_HOST_NAME_MAX
2056     len = sysconf(_SC_HOST_NAME_MAX);
2057 #endif /* _SC_HOST_NAME_MAX */
2058 
2059     if (len < 0) {
2060         len = HOST_NAME_MAX;
2061     }
2062 
2063     /* Unfortunately, gethostname() below does not guarantee a
2064      * NULL terminated string. Therefore, allocate one byte more
2065      * to be sure. */
2066     hostname = g_new0(char, len + 1);
2067 
2068     if (gethostname(hostname, len) < 0) {
2069         error_setg_errno(errp, errno,
2070                          "cannot get hostname");
2071         return NULL;
2072     }
2073 
2074     return g_steal_pointer(&hostname);
2075 }
2076