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