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