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