2011-07-20 00:41:55 +04:00
|
|
|
/*
|
2012-01-20 08:19:27 +04:00
|
|
|
* QEMU Guest Agent POSIX-specific command implementations
|
2011-07-20 00:41:55 +04:00
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
2012-02-29 20:02:23 +04:00
|
|
|
* Michal Privoznik <mprivozn@redhat.com>
|
2011-07-20 00:41:55 +04:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
2012-03-25 22:59:41 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-05-23 22:48:05 +04:00
|
|
|
#include <sys/wait.h>
|
2013-03-07 01:59:30 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
2012-03-25 22:59:41 +04:00
|
|
|
#include "qga/guest-agent-core.h"
|
|
|
|
#include "qga-qmp-commands.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/host-utils.h"
|
2011-07-24 01:14:37 +04:00
|
|
|
|
2012-05-23 22:48:05 +04:00
|
|
|
#ifndef CONFIG_HAS_ENVIRON
|
2012-05-27 19:02:20 +04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#define environ (*_NSGetEnviron())
|
|
|
|
#else
|
2012-05-23 22:48:05 +04:00
|
|
|
extern char **environ;
|
|
|
|
#endif
|
2012-05-27 19:02:20 +04:00
|
|
|
#endif
|
2012-05-23 22:48:05 +04:00
|
|
|
|
2011-07-24 01:14:37 +04:00
|
|
|
#if defined(__linux__)
|
2011-07-20 00:41:55 +04:00
|
|
|
#include <mntent.h>
|
2011-07-22 23:14:17 +04:00
|
|
|
#include <linux/fs.h>
|
2012-02-29 20:02:23 +04:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-06-13 09:41:28 +04:00
|
|
|
#ifdef FIFREEZE
|
2012-03-25 22:59:41 +04:00
|
|
|
#define CONFIG_FSFREEZE
|
|
|
|
#endif
|
2012-06-13 09:41:28 +04:00
|
|
|
#ifdef FITRIM
|
|
|
|
#define CONFIG_FSTRIM
|
|
|
|
#endif
|
2012-03-25 22:59:41 +04:00
|
|
|
#endif
|
|
|
|
|
2012-11-27 17:01:58 +04:00
|
|
|
static void ga_wait_child(pid_t pid, int *status, Error **err)
|
|
|
|
{
|
|
|
|
pid_t rpid;
|
|
|
|
|
|
|
|
*status = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rpid = waitpid(pid, status, 0);
|
|
|
|
} while (rpid == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (rpid == -1) {
|
|
|
|
error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(rpid == pid);
|
|
|
|
}
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
|
|
|
|
{
|
|
|
|
const char *shutdown_flag;
|
2012-11-27 17:01:58 +04:00
|
|
|
Error *local_err = NULL;
|
|
|
|
pid_t pid;
|
2012-05-14 22:25:20 +04:00
|
|
|
int status;
|
2011-07-20 00:41:55 +04:00
|
|
|
|
|
|
|
slog("guest-shutdown called, mode: %s", mode);
|
|
|
|
if (!has_mode || strcmp(mode, "powerdown") == 0) {
|
|
|
|
shutdown_flag = "-P";
|
|
|
|
} else if (strcmp(mode, "halt") == 0) {
|
|
|
|
shutdown_flag = "-H";
|
|
|
|
} else if (strcmp(mode, "reboot") == 0) {
|
|
|
|
shutdown_flag = "-r";
|
|
|
|
} else {
|
2012-11-27 17:01:58 +04:00
|
|
|
error_setg(err,
|
|
|
|
"mode is invalid (valid values are: halt|powerdown|reboot");
|
2011-07-20 00:41:55 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-11 23:19:47 +04:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
2011-07-20 00:41:55 +04:00
|
|
|
/* child, start the shutdown */
|
|
|
|
setsid();
|
2012-05-14 22:25:20 +04:00
|
|
|
reopen_fd_to_null(0);
|
|
|
|
reopen_fd_to_null(1);
|
|
|
|
reopen_fd_to_null(2);
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-05-14 22:25:20 +04:00
|
|
|
execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
|
|
|
|
"hypervisor initiated shutdown", (char*)NULL, environ);
|
|
|
|
_exit(EXIT_FAILURE);
|
2012-05-11 23:19:47 +04:00
|
|
|
} else if (pid < 0) {
|
2012-11-27 17:01:58 +04:00
|
|
|
error_setg_errno(err, errno, "failed to create child process");
|
|
|
|
return;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
2012-05-11 23:19:47 +04:00
|
|
|
|
2012-11-27 17:01:58 +04:00
|
|
|
ga_wait_child(pid, &status, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
error_setg(err, "child process has terminated abnormally");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WEXITSTATUS(status)) {
|
|
|
|
error_setg(err, "child process has failed to shutdown");
|
2012-05-11 23:19:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-18 21:20:07 +04:00
|
|
|
/* succeeded */
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
2013-03-05 13:39:11 +04:00
|
|
|
int64_t qmp_guest_get_time(Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
qemu_timeval tq;
|
|
|
|
int64_t time_ns;
|
|
|
|
|
|
|
|
ret = qemu_gettimeofday(&tq);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, errno, "Failed to get time");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
|
|
|
|
return time_ns;
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:39:12 +04:00
|
|
|
void qmp_guest_set_time(int64_t time_ns, Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* year-2038 will overflow in case time_t is 32bit */
|
|
|
|
if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
|
|
|
|
error_setg(errp, "Time %" PRId64 " is too large", time_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = time_ns / 1000000000;
|
|
|
|
tv.tv_usec = (time_ns % 1000000000) / 1000;
|
|
|
|
|
|
|
|
ret = settimeofday(&tv, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, errno, "Failed to set time to guest");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the Hardware Clock to the current System Time. */
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
setsid();
|
|
|
|
reopen_fd_to_null(0);
|
|
|
|
reopen_fd_to_null(1);
|
|
|
|
reopen_fd_to_null(2);
|
|
|
|
|
|
|
|
execle("/sbin/hwclock", "hwclock", "-w", NULL, environ);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
error_setg_errno(errp, errno, "failed to create child process");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ga_wait_child(pid, &status, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
error_setg(errp, "child process has terminated abnormally");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WEXITSTATUS(status)) {
|
|
|
|
error_setg(errp, "hwclock failed to set hardware clock to system time");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
typedef struct GuestFileHandle {
|
|
|
|
uint64_t id;
|
|
|
|
FILE *fh;
|
|
|
|
QTAILQ_ENTRY(GuestFileHandle) next;
|
|
|
|
} GuestFileHandle;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
QTAILQ_HEAD(, GuestFileHandle) filehandles;
|
|
|
|
} guest_file_state;
|
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
static int64_t guest_file_handle_add(FILE *fh, Error **errp)
|
2011-07-20 00:41:55 +04:00
|
|
|
{
|
|
|
|
GuestFileHandle *gfh;
|
2013-03-01 21:40:27 +04:00
|
|
|
int64_t handle;
|
|
|
|
|
|
|
|
handle = ga_get_fd_handle(ga_state, errp);
|
|
|
|
if (error_is_set(errp)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
gfh = g_malloc0(sizeof(GuestFileHandle));
|
2013-03-01 21:40:27 +04:00
|
|
|
gfh->id = handle;
|
2011-07-20 00:41:55 +04:00
|
|
|
gfh->fh = fh;
|
|
|
|
QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
|
2013-03-01 21:40:27 +04:00
|
|
|
|
|
|
|
return handle;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:01:55 +04:00
|
|
|
static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err)
|
2011-07-20 00:41:55 +04:00
|
|
|
{
|
|
|
|
GuestFileHandle *gfh;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
|
|
|
|
{
|
|
|
|
if (gfh->id == id) {
|
|
|
|
return gfh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 17:01:55 +04:00
|
|
|
error_setg(err, "handle '%" PRId64 "' has not been found", id);
|
2011-07-20 00:41:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
|
|
|
|
{
|
|
|
|
FILE *fh;
|
|
|
|
int fd;
|
2013-03-01 21:40:27 +04:00
|
|
|
int64_t ret = -1, handle;
|
2011-07-20 00:41:55 +04:00
|
|
|
|
|
|
|
if (!has_mode) {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
|
|
|
|
fh = fopen(path, mode);
|
|
|
|
if (!fh) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to open file '%s' (mode: '%s')",
|
|
|
|
path, mode);
|
2011-07-20 00:41:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set fd non-blocking to avoid common use cases (like reading from a
|
|
|
|
* named pipe) from hanging the agent
|
|
|
|
*/
|
|
|
|
fd = fileno(fh);
|
|
|
|
ret = fcntl(fd, F_GETFL);
|
|
|
|
ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
|
|
|
|
if (ret == -1) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
|
|
|
|
path);
|
2011-07-20 00:41:55 +04:00
|
|
|
fclose(fh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-01 21:40:27 +04:00
|
|
|
handle = guest_file_handle_add(fh, err);
|
|
|
|
if (error_is_set(err)) {
|
|
|
|
fclose(fh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
slog("guest-file-open, handle: %d", handle);
|
|
|
|
return handle;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_guest_file_close(int64_t handle, Error **err)
|
|
|
|
{
|
2012-11-27 17:01:55 +04:00
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
|
2011-07-20 00:41:55 +04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
slog("guest-file-close called, handle: %ld", handle);
|
|
|
|
if (!gfh) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fclose(gfh->fh);
|
2012-11-27 17:01:56 +04:00
|
|
|
if (ret == EOF) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to close handle");
|
2011-07-20 00:41:55 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(gfh);
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
|
|
|
|
int64_t count, Error **err)
|
|
|
|
{
|
2012-11-27 17:01:55 +04:00
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
|
2011-07-20 00:41:55 +04:00
|
|
|
GuestFileRead *read_data = NULL;
|
|
|
|
guchar *buf;
|
|
|
|
FILE *fh;
|
|
|
|
size_t read_count;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_count) {
|
|
|
|
count = QGA_READ_COUNT_DEFAULT;
|
|
|
|
} else if (count < 0) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg(err, "value '%" PRId64 "' is invalid for argument count",
|
|
|
|
count);
|
2011-07-20 00:41:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = gfh->fh;
|
2011-08-21 07:09:37 +04:00
|
|
|
buf = g_malloc0(count+1);
|
2011-07-20 00:41:55 +04:00
|
|
|
read_count = fread(buf, 1, count, fh);
|
|
|
|
if (ferror(fh)) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to read file");
|
2011-07-20 00:41:55 +04:00
|
|
|
slog("guest-file-read failed, handle: %ld", handle);
|
|
|
|
} else {
|
|
|
|
buf[read_count] = 0;
|
2011-08-21 07:09:37 +04:00
|
|
|
read_data = g_malloc0(sizeof(GuestFileRead));
|
2011-07-20 00:41:55 +04:00
|
|
|
read_data->count = read_count;
|
|
|
|
read_data->eof = feof(fh);
|
|
|
|
if (read_count) {
|
|
|
|
read_data->buf_b64 = g_base64_encode(buf, read_count);
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2011-07-20 00:41:55 +04:00
|
|
|
clearerr(fh);
|
|
|
|
|
|
|
|
return read_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
|
|
|
|
bool has_count, int64_t count, Error **err)
|
|
|
|
{
|
|
|
|
GuestFileWrite *write_data = NULL;
|
|
|
|
guchar *buf;
|
|
|
|
gsize buf_len;
|
|
|
|
int write_count;
|
2012-11-27 17:01:55 +04:00
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
|
2011-07-20 00:41:55 +04:00
|
|
|
FILE *fh;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = gfh->fh;
|
|
|
|
buf = g_base64_decode(buf_b64, &buf_len);
|
|
|
|
|
|
|
|
if (!has_count) {
|
|
|
|
count = buf_len;
|
|
|
|
} else if (count < 0 || count > buf_len) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg(err, "value '%" PRId64 "' is invalid for argument count",
|
|
|
|
count);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2011-07-20 00:41:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_count = fwrite(buf, 1, count, fh);
|
|
|
|
if (ferror(fh)) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to write to file");
|
2011-07-20 00:41:55 +04:00
|
|
|
slog("guest-file-write failed, handle: %ld", handle);
|
|
|
|
} else {
|
2011-08-21 07:09:37 +04:00
|
|
|
write_data = g_malloc0(sizeof(GuestFileWrite));
|
2011-07-20 00:41:55 +04:00
|
|
|
write_data->count = write_count;
|
|
|
|
write_data->eof = feof(fh);
|
|
|
|
}
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(buf);
|
2011-07-20 00:41:55 +04:00
|
|
|
clearerr(fh);
|
|
|
|
|
|
|
|
return write_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
|
|
|
|
int64_t whence, Error **err)
|
|
|
|
{
|
2012-11-27 17:01:55 +04:00
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
|
2011-07-20 00:41:55 +04:00
|
|
|
GuestFileSeek *seek_data = NULL;
|
|
|
|
FILE *fh;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = gfh->fh;
|
|
|
|
ret = fseek(fh, offset, whence);
|
|
|
|
if (ret == -1) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to seek file");
|
2011-07-20 00:41:55 +04:00
|
|
|
} else {
|
2011-08-21 07:09:37 +04:00
|
|
|
seek_data = g_malloc0(sizeof(GuestFileRead));
|
2011-07-20 00:41:55 +04:00
|
|
|
seek_data->position = ftell(fh);
|
|
|
|
seek_data->eof = feof(fh);
|
|
|
|
}
|
|
|
|
clearerr(fh);
|
|
|
|
|
|
|
|
return seek_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_guest_file_flush(int64_t handle, Error **err)
|
|
|
|
{
|
2012-11-27 17:01:55 +04:00
|
|
|
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
|
2011-07-20 00:41:55 +04:00
|
|
|
FILE *fh;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!gfh) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = gfh->fh;
|
|
|
|
ret = fflush(fh);
|
|
|
|
if (ret == EOF) {
|
2012-11-27 17:01:57 +04:00
|
|
|
error_setg_errno(err, errno, "failed to flush file");
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guest_file_init(void)
|
|
|
|
{
|
|
|
|
QTAILQ_INIT(&guest_file_state.filehandles);
|
|
|
|
}
|
|
|
|
|
2012-03-25 22:59:41 +04:00
|
|
|
/* linux-specific implementations. avoid this if at all possible. */
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
2012-06-13 09:41:28 +04:00
|
|
|
#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
|
2012-06-13 09:41:27 +04:00
|
|
|
typedef struct FsMount {
|
2011-07-20 00:41:55 +04:00
|
|
|
char *dirname;
|
|
|
|
char *devtype;
|
2012-06-13 09:41:27 +04:00
|
|
|
QTAILQ_ENTRY(FsMount) next;
|
|
|
|
} FsMount;
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-06-13 09:41:27 +04:00
|
|
|
typedef QTAILQ_HEAD(, FsMount) FsMountList;
|
2012-04-17 04:52:17 +04:00
|
|
|
|
2012-06-13 09:41:27 +04:00
|
|
|
static void free_fs_mount_list(FsMountList *mounts)
|
2012-04-17 04:52:17 +04:00
|
|
|
{
|
2012-06-13 09:41:27 +04:00
|
|
|
FsMount *mount, *temp;
|
2012-04-17 04:52:17 +04:00
|
|
|
|
|
|
|
if (!mounts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
|
|
|
|
QTAILQ_REMOVE(mounts, mount, next);
|
|
|
|
g_free(mount->dirname);
|
|
|
|
g_free(mount->devtype);
|
|
|
|
g_free(mount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
/*
|
|
|
|
* Walk the mount table and build a list of local file systems
|
|
|
|
*/
|
2012-11-29 21:29:11 +04:00
|
|
|
static void build_fs_mount_list(FsMountList *mounts, Error **err)
|
2011-07-20 00:41:55 +04:00
|
|
|
{
|
|
|
|
struct mntent *ment;
|
2012-06-13 09:41:27 +04:00
|
|
|
FsMount *mount;
|
2012-05-29 19:08:50 +04:00
|
|
|
char const *mtab = "/proc/self/mounts";
|
2011-07-20 00:41:55 +04:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = setmntent(mtab, "r");
|
|
|
|
if (!fp) {
|
2012-11-29 21:29:11 +04:00
|
|
|
error_setg(err, "failed to open mtab file: '%s'", mtab);
|
|
|
|
return;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((ment = getmntent(fp))) {
|
|
|
|
/*
|
|
|
|
* An entry which device name doesn't start with a '/' is
|
|
|
|
* either a dummy file system or a network file system.
|
|
|
|
* Add special handling for smbfs and cifs as is done by
|
|
|
|
* coreutils as well.
|
|
|
|
*/
|
|
|
|
if ((ment->mnt_fsname[0] != '/') ||
|
|
|
|
(strcmp(ment->mnt_type, "smbfs") == 0) ||
|
|
|
|
(strcmp(ment->mnt_type, "cifs") == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-13 09:41:27 +04:00
|
|
|
mount = g_malloc0(sizeof(FsMount));
|
2011-08-21 07:09:37 +04:00
|
|
|
mount->dirname = g_strdup(ment->mnt_dir);
|
|
|
|
mount->devtype = g_strdup(ment->mnt_type);
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-04-17 04:52:17 +04:00
|
|
|
QTAILQ_INSERT_TAIL(mounts, mount, next);
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
endmntent(fp);
|
|
|
|
}
|
2012-06-13 09:41:28 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_FSFREEZE)
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-12-12 07:55:55 +04:00
|
|
|
typedef enum {
|
|
|
|
FSFREEZE_HOOK_THAW = 0,
|
|
|
|
FSFREEZE_HOOK_FREEZE,
|
|
|
|
} FsfreezeHookArg;
|
|
|
|
|
|
|
|
const char *fsfreeze_hook_arg_string[] = {
|
|
|
|
"thaw",
|
|
|
|
"freeze",
|
|
|
|
};
|
|
|
|
|
|
|
|
static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
const char *hook;
|
|
|
|
const char *arg_str = fsfreeze_hook_arg_string[arg];
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
hook = ga_fsfreeze_hook(ga_state);
|
|
|
|
if (!hook) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (access(hook, X_OK) != 0) {
|
|
|
|
error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
slog("executing fsfreeze hook with arg '%s'", arg_str);
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
setsid();
|
|
|
|
reopen_fd_to_null(0);
|
|
|
|
reopen_fd_to_null(1);
|
|
|
|
reopen_fd_to_null(2);
|
|
|
|
|
|
|
|
execle(hook, hook, arg_str, NULL, environ);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
error_setg_errno(err, errno, "failed to create child process");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ga_wait_child(pid, &status, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
error_setg(err, "fsfreeze hook has terminated abnormally");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WEXITSTATUS(status);
|
|
|
|
if (status) {
|
|
|
|
error_setg(err, "fsfreeze hook has failed with status %d", status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
/*
|
|
|
|
* Return status of freeze/thaw
|
|
|
|
*/
|
|
|
|
GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
|
|
|
|
{
|
2012-04-18 04:01:45 +04:00
|
|
|
if (ga_is_frozen(ga_state)) {
|
|
|
|
return GUEST_FSFREEZE_STATUS_FROZEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GUEST_FSFREEZE_STATUS_THAWED;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk list of mounted file systems in the guest, and freeze the ones which
|
|
|
|
* are real local file systems.
|
|
|
|
*/
|
|
|
|
int64_t qmp_guest_fsfreeze_freeze(Error **err)
|
|
|
|
{
|
|
|
|
int ret = 0, i = 0;
|
2012-06-13 09:41:27 +04:00
|
|
|
FsMountList mounts;
|
|
|
|
struct FsMount *mount;
|
2012-11-29 21:29:11 +04:00
|
|
|
Error *local_err = NULL;
|
2011-07-20 00:41:55 +04:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
slog("guest-fsfreeze called");
|
|
|
|
|
2012-12-12 07:55:55 +04:00
|
|
|
execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-17 04:52:17 +04:00
|
|
|
QTAILQ_INIT(&mounts);
|
2012-11-29 21:29:11 +04:00
|
|
|
build_fs_mount_list(&mounts, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
return -1;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cannot risk guest agent blocking itself on a write in this state */
|
2012-04-18 04:01:45 +04:00
|
|
|
ga_set_frozen(ga_state);
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-04-17 04:52:17 +04:00
|
|
|
QTAILQ_FOREACH(mount, &mounts, next) {
|
2011-07-20 00:41:55 +04:00
|
|
|
fd = qemu_open(mount->dirname, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2012-11-27 17:02:00 +04:00
|
|
|
error_setg_errno(err, errno, "failed to open %s", mount->dirname);
|
2011-07-20 00:41:55 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we try to cull filesytems we know won't work in advance, but other
|
|
|
|
* filesytems may not implement fsfreeze for less obvious reasons.
|
2012-04-17 04:52:17 +04:00
|
|
|
* these will report EOPNOTSUPP. we simply ignore these when tallying
|
|
|
|
* the number of frozen filesystems.
|
|
|
|
*
|
|
|
|
* any other error means a failure to freeze a filesystem we
|
|
|
|
* expect to be freezable, so return an error in those cases
|
|
|
|
* and return system to thawed state.
|
2011-07-20 00:41:55 +04:00
|
|
|
*/
|
|
|
|
ret = ioctl(fd, FIFREEZE);
|
2012-04-17 04:52:17 +04:00
|
|
|
if (ret == -1) {
|
|
|
|
if (errno != EOPNOTSUPP) {
|
2012-11-27 17:02:00 +04:00
|
|
|
error_setg_errno(err, errno, "failed to freeze %s",
|
|
|
|
mount->dirname);
|
2012-04-17 04:52:17 +04:00
|
|
|
close(fd);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i++;
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2012-06-13 09:41:27 +04:00
|
|
|
free_fs_mount_list(&mounts);
|
2011-07-20 00:41:55 +04:00
|
|
|
return i;
|
|
|
|
|
|
|
|
error:
|
2012-06-13 09:41:27 +04:00
|
|
|
free_fs_mount_list(&mounts);
|
2012-04-17 04:52:17 +04:00
|
|
|
qmp_guest_fsfreeze_thaw(NULL);
|
2011-07-20 00:41:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk list of frozen file systems in the guest, and thaw them.
|
|
|
|
*/
|
|
|
|
int64_t qmp_guest_fsfreeze_thaw(Error **err)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-06-13 09:41:27 +04:00
|
|
|
FsMountList mounts;
|
|
|
|
FsMount *mount;
|
2012-04-17 04:52:17 +04:00
|
|
|
int fd, i = 0, logged;
|
2012-11-29 21:29:11 +04:00
|
|
|
Error *local_err = NULL;
|
2012-04-17 04:52:17 +04:00
|
|
|
|
|
|
|
QTAILQ_INIT(&mounts);
|
2012-11-29 21:29:11 +04:00
|
|
|
build_fs_mount_list(&mounts, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
2012-04-17 04:52:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-04-17 04:52:17 +04:00
|
|
|
QTAILQ_FOREACH(mount, &mounts, next) {
|
|
|
|
logged = false;
|
2011-07-20 00:41:55 +04:00
|
|
|
fd = qemu_open(mount->dirname, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-17 04:52:17 +04:00
|
|
|
/* we have no way of knowing whether a filesystem was actually unfrozen
|
|
|
|
* as a result of a successful call to FITHAW, only that if an error
|
|
|
|
* was returned the filesystem was *not* unfrozen by that particular
|
|
|
|
* call.
|
|
|
|
*
|
2012-05-09 09:12:04 +04:00
|
|
|
* since multiple preceding FIFREEZEs require multiple calls to FITHAW
|
2012-04-17 04:52:17 +04:00
|
|
|
* to unfreeze, continuing issuing FITHAW until an error is returned,
|
|
|
|
* in which case either the filesystem is in an unfreezable state, or,
|
|
|
|
* more likely, it was thawed previously (and remains so afterward).
|
|
|
|
*
|
|
|
|
* also, since the most recent successful call is the one that did
|
|
|
|
* the actual unfreeze, we can use this to provide an accurate count
|
|
|
|
* of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
|
|
|
|
* may * be useful for determining whether a filesystem was unfrozen
|
|
|
|
* during the freeze/thaw phase by a process other than qemu-ga.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ret = ioctl(fd, FITHAW);
|
|
|
|
if (ret == 0 && !logged) {
|
|
|
|
i++;
|
|
|
|
logged = true;
|
|
|
|
}
|
|
|
|
} while (ret == 0);
|
2011-07-20 00:41:55 +04:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2012-04-18 04:01:45 +04:00
|
|
|
ga_unset_frozen(ga_state);
|
2012-06-13 09:41:27 +04:00
|
|
|
free_fs_mount_list(&mounts);
|
2012-12-12 07:55:55 +04:00
|
|
|
|
|
|
|
execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guest_fsfreeze_cleanup(void)
|
|
|
|
{
|
|
|
|
Error *err = NULL;
|
|
|
|
|
2012-04-18 04:01:45 +04:00
|
|
|
if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
|
2013-01-16 21:15:08 +04:00
|
|
|
qmp_guest_fsfreeze_thaw(&err);
|
|
|
|
if (err) {
|
|
|
|
slog("failed to clean up frozen filesystems: %s",
|
|
|
|
error_get_pretty(err));
|
|
|
|
error_free(err);
|
2011-07-20 00:41:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-25 22:59:41 +04:00
|
|
|
#endif /* CONFIG_FSFREEZE */
|
2011-07-20 00:41:55 +04:00
|
|
|
|
2012-06-13 09:41:28 +04:00
|
|
|
#if defined(CONFIG_FSTRIM)
|
|
|
|
/*
|
|
|
|
* Walk list of mounted file systems in the guest, and trim them.
|
|
|
|
*/
|
|
|
|
void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
FsMountList mounts;
|
|
|
|
struct FsMount *mount;
|
|
|
|
int fd;
|
2012-11-29 21:29:11 +04:00
|
|
|
Error *local_err = NULL;
|
2012-06-13 09:41:28 +04:00
|
|
|
struct fstrim_range r = {
|
|
|
|
.start = 0,
|
|
|
|
.len = -1,
|
|
|
|
.minlen = has_minimum ? minimum : 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
slog("guest-fstrim called");
|
|
|
|
|
|
|
|
QTAILQ_INIT(&mounts);
|
2012-11-29 21:29:11 +04:00
|
|
|
build_fs_mount_list(&mounts, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
2012-06-13 09:41:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(mount, &mounts, next) {
|
|
|
|
fd = qemu_open(mount->dirname, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2012-11-27 17:02:01 +04:00
|
|
|
error_setg_errno(err, errno, "failed to open %s", mount->dirname);
|
2012-06-13 09:41:28 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We try to cull filesytems we know won't work in advance, but other
|
|
|
|
* filesytems may not implement fstrim for less obvious reasons. These
|
|
|
|
* will report EOPNOTSUPP; we simply ignore these errors. Any other
|
|
|
|
* error means an unexpected error, so return it in those cases. In
|
|
|
|
* some other cases ENOTTY will be reported (e.g. CD-ROMs).
|
|
|
|
*/
|
|
|
|
ret = ioctl(fd, FITRIM, &r);
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno != ENOTTY && errno != EOPNOTSUPP) {
|
2012-11-27 17:02:01 +04:00
|
|
|
error_setg_errno(err, errno, "failed to trim %s",
|
|
|
|
mount->dirname);
|
2012-06-13 09:41:28 +04:00
|
|
|
close(fd);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
free_fs_mount_list(&mounts);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FSTRIM */
|
|
|
|
|
|
|
|
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
#define LINUX_SYS_STATE_FILE "/sys/power/state"
|
|
|
|
#define SUSPEND_SUPPORTED 0
|
|
|
|
#define SUSPEND_NOT_SUPPORTED 1
|
|
|
|
|
|
|
|
static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
|
|
|
|
const char *sysfile_str, Error **err)
|
|
|
|
{
|
2012-11-27 17:02:03 +04:00
|
|
|
Error *local_err = NULL;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
char *pmutils_path;
|
2012-11-27 17:02:03 +04:00
|
|
|
pid_t pid;
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
int status;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
|
|
|
pmutils_path = g_find_program_in_path(pmutils_bin);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (!pid) {
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
char buf[32]; /* hopefully big enough */
|
|
|
|
ssize_t ret;
|
|
|
|
int fd;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
|
|
|
setsid();
|
|
|
|
reopen_fd_to_null(0);
|
|
|
|
reopen_fd_to_null(1);
|
|
|
|
reopen_fd_to_null(2);
|
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
if (pmutils_path) {
|
|
|
|
execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
/*
|
|
|
|
* If we get here either pm-utils is not installed or execle() has
|
|
|
|
* failed. Let's try the manual method if the caller wants it.
|
|
|
|
*/
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
if (!sysfile_str) {
|
|
|
|
_exit(SUSPEND_NOT_SUPPORTED);
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
_exit(SUSPEND_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
ret = read(fd, buf, sizeof(buf)-1);
|
|
|
|
if (ret <= 0) {
|
|
|
|
_exit(SUSPEND_NOT_SUPPORTED);
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
buf[ret] = '\0';
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
if (strstr(buf, sysfile_str)) {
|
|
|
|
_exit(SUSPEND_SUPPORTED);
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
_exit(SUSPEND_NOT_SUPPORTED);
|
2012-11-27 17:02:03 +04:00
|
|
|
} else if (pid < 0) {
|
|
|
|
error_setg_errno(err, errno, "failed to create child process");
|
|
|
|
goto out;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:02:03 +04:00
|
|
|
ga_wait_child(pid, &status, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
goto out;
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
2012-11-27 17:02:03 +04:00
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
error_setg(err, "child process has terminated abnormally");
|
|
|
|
goto out;
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:02:03 +04:00
|
|
|
switch (WEXITSTATUS(status)) {
|
|
|
|
case SUSPEND_SUPPORTED:
|
|
|
|
goto out;
|
|
|
|
case SUSPEND_NOT_SUPPORTED:
|
|
|
|
error_setg(err,
|
|
|
|
"the requested suspend mode is not supported by the guest");
|
|
|
|
goto out;
|
|
|
|
default:
|
|
|
|
error_setg(err,
|
|
|
|
"the helper program '%s' returned an unexpected exit status"
|
|
|
|
" code (%d)", pmutils_path, WEXITSTATUS(status));
|
|
|
|
goto out;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:02:03 +04:00
|
|
|
out:
|
|
|
|
g_free(pmutils_path);
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
|
|
|
|
Error **err)
|
|
|
|
{
|
2012-11-27 17:02:04 +04:00
|
|
|
Error *local_err = NULL;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
char *pmutils_path;
|
2012-11-27 17:02:04 +04:00
|
|
|
pid_t pid;
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
int status;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
|
|
|
pmutils_path = g_find_program_in_path(pmutils_bin);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* child */
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
reopen_fd_to_null(0);
|
|
|
|
reopen_fd_to_null(1);
|
|
|
|
reopen_fd_to_null(2);
|
|
|
|
|
|
|
|
if (pmutils_path) {
|
|
|
|
execle(pmutils_path, pmutils_bin, NULL, environ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get here either pm-utils is not installed or execle() has
|
|
|
|
* failed. Let's try the manual method if the caller wants it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!sysfile_str) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(EXIT_SUCCESS);
|
2012-11-27 17:02:04 +04:00
|
|
|
} else if (pid < 0) {
|
|
|
|
error_setg_errno(err, errno, "failed to create child process");
|
|
|
|
goto out;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:02:04 +04:00
|
|
|
ga_wait_child(pid, &status, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
|
|
|
error_propagate(err, local_err);
|
|
|
|
goto out;
|
|
|
|
}
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
|
2012-11-27 17:02:04 +04:00
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
error_setg(err, "child process has terminated abnormally");
|
|
|
|
goto out;
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 17:02:04 +04:00
|
|
|
if (WEXITSTATUS(status)) {
|
|
|
|
error_setg(err, "child process has failed to suspend");
|
|
|
|
goto out;
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
qemu-ga: guest-suspend: make the API synchronous
Currently, qemu-ga has a SIGCHLD handler that automatically reaps terminated
children processes. The idea is to avoid having qemu-ga commands blocked
waiting for children to terminate.
That approach has two problems:
1. qemu-ga is unable to detect errors in the child, meaning that qemu-ga
returns success even if the child fails to perform its task
2. if a command does depend on the child exit status, the command has to
play tricks to bypass the automatic reaper
Case 2 impacts the guest-suspend-* API, because it has to execute an external
program to check for suspend support. Today, to bypass the automatic reaper,
suspend code has to double fork and pass exit status information through a
pipe. Besides being complex, this is prone to race condition bugs. Indeed,
the current code does have such bugs.
Making the guest-suspend-* API synchronous (ie. by dropping the SIGCHLD
handler and calling waitpid() from commands) is a much simpler approach,
which fixes current race conditions bugs and enables commands to detect
errors in the child.
This commit does just that. There's a side effect though, guest-shutdown
will generate zombies if shutting down fails. This will be fixed by the
next commit.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2012-05-11 23:19:46 +04:00
|
|
|
|
2012-11-27 17:02:04 +04:00
|
|
|
out:
|
|
|
|
g_free(pmutils_path);
|
qemu-ga: add guest-suspend-disk
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode()
and guest_suspend(). Both functions are generic enough to be used by
other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils
package if it's available. If it's not available, a manual method,
which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the
parent to catch SIGCHLD signals and a non-blocking call to waitpid()
is done to collect their exit statuses. The statuses, however, are
discarded.
The approach used to query the guest for suspend support deserves some
explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
|
fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple.
The purpose of that approach is to allow qemu-ga to reap its children
(semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from
the first child process, would force us to introduce a more complex way
to reap qemu-ga's children. Like registering PIDs to be reaped and
having a way to wait for them when returning their exit status to
qemu-ga is necessary. The approach explained above avoids that complexity.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2012-02-28 18:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_guest_suspend_disk(Error **err)
|
|
|
|
{
|
|
|
|
bios_supports_mode("pm-is-supported", "--hibernate", "disk", err);
|
|
|
|
if (error_is_set(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
guest_suspend("pm-hibernate", "disk", err);
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:03:04 +04:00
|
|
|
void qmp_guest_suspend_ram(Error **err)
|
|
|
|
{
|
|
|
|
bios_supports_mode("pm-is-supported", "--suspend", "mem", err);
|
|
|
|
if (error_is_set(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
guest_suspend("pm-suspend", "mem", err);
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:03:05 +04:00
|
|
|
void qmp_guest_suspend_hybrid(Error **err)
|
|
|
|
{
|
|
|
|
bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, err);
|
|
|
|
if (error_is_set(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
guest_suspend("pm-suspend-hybrid", NULL, err);
|
|
|
|
}
|
|
|
|
|
2012-02-29 20:02:23 +04:00
|
|
|
static GuestNetworkInterfaceList *
|
|
|
|
guest_find_interface(GuestNetworkInterfaceList *head,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
for (; head; head = head->next) {
|
|
|
|
if (strcmp(head->value->name, name) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build information about guest interfaces
|
|
|
|
*/
|
|
|
|
GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|
|
|
{
|
|
|
|
GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap) < 0) {
|
2012-11-27 17:02:02 +04:00
|
|
|
error_setg_errno(errp, errno, "getifaddrs failed");
|
2012-02-29 20:02:23 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
GuestNetworkInterfaceList *info;
|
|
|
|
GuestIpAddressList **address_list = NULL, *address_item = NULL;
|
|
|
|
char addr4[INET_ADDRSTRLEN];
|
|
|
|
char addr6[INET6_ADDRSTRLEN];
|
|
|
|
int sock;
|
|
|
|
struct ifreq ifr;
|
|
|
|
unsigned char *mac_addr;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
g_debug("Processing %s interface", ifa->ifa_name);
|
|
|
|
|
|
|
|
info = guest_find_interface(head, ifa->ifa_name);
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
info = g_malloc0(sizeof(*info));
|
|
|
|
info->value = g_malloc0(sizeof(*info->value));
|
|
|
|
info->value->name = g_strdup(ifa->ifa_name);
|
|
|
|
|
|
|
|
if (!cur_item) {
|
|
|
|
head = cur_item = info;
|
|
|
|
} else {
|
|
|
|
cur_item->next = info;
|
|
|
|
cur_item = info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info->value->has_hardware_address &&
|
|
|
|
ifa->ifa_flags & SIOCGIFHWADDR) {
|
|
|
|
/* we haven't obtained HW address yet */
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1) {
|
2012-11-27 17:02:02 +04:00
|
|
|
error_setg_errno(errp, errno, "failed to create socket");
|
2012-02-29 20:02:23 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2012-10-04 15:09:58 +04:00
|
|
|
pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
|
2012-02-29 20:02:23 +04:00
|
|
|
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
|
2012-11-27 17:02:02 +04:00
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"failed to get MAC address of %s",
|
|
|
|
ifa->ifa_name);
|
2013-01-16 21:15:09 +04:00
|
|
|
close(sock);
|
2012-02-29 20:02:23 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:15:09 +04:00
|
|
|
close(sock);
|
2012-02-29 20:02:23 +04:00
|
|
|
mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
|
|
|
|
|
2013-01-16 21:37:23 +04:00
|
|
|
info->value->hardware_address =
|
|
|
|
g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
(int) mac_addr[0], (int) mac_addr[1],
|
|
|
|
(int) mac_addr[2], (int) mac_addr[3],
|
|
|
|
(int) mac_addr[4], (int) mac_addr[5]);
|
2012-02-29 20:02:23 +04:00
|
|
|
|
|
|
|
info->value->has_hardware_address = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifa->ifa_addr &&
|
|
|
|
ifa->ifa_addr->sa_family == AF_INET) {
|
|
|
|
/* interface with IPv4 address */
|
|
|
|
p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
|
|
|
|
if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
|
2012-11-27 17:02:02 +04:00
|
|
|
error_setg_errno(errp, errno, "inet_ntop failed");
|
2012-02-29 20:02:23 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:15:09 +04:00
|
|
|
address_item = g_malloc0(sizeof(*address_item));
|
|
|
|
address_item->value = g_malloc0(sizeof(*address_item->value));
|
2012-02-29 20:02:23 +04:00
|
|
|
address_item->value->ip_address = g_strdup(addr4);
|
|
|
|
address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
|
|
|
|
|
|
|
|
if (ifa->ifa_netmask) {
|
|
|
|
/* Count the number of set bits in netmask.
|
|
|
|
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
|
|
|
p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
|
|
|
|
address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
|
|
|
|
}
|
|
|
|
} else if (ifa->ifa_addr &&
|
|
|
|
ifa->ifa_addr->sa_family == AF_INET6) {
|
|
|
|
/* interface with IPv6 address */
|
|
|
|
p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
|
|
|
|
if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
|
2012-11-27 17:02:02 +04:00
|
|
|
error_setg_errno(errp, errno, "inet_ntop failed");
|
2012-02-29 20:02:23 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:15:09 +04:00
|
|
|
address_item = g_malloc0(sizeof(*address_item));
|
|
|
|
address_item->value = g_malloc0(sizeof(*address_item->value));
|
2012-02-29 20:02:23 +04:00
|
|
|
address_item->value->ip_address = g_strdup(addr6);
|
|
|
|
address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
|
|
|
|
|
|
|
|
if (ifa->ifa_netmask) {
|
|
|
|
/* Count the number of set bits in netmask.
|
|
|
|
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
|
|
|
p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
|
|
|
|
address_item->value->prefix =
|
|
|
|
ctpop32(((uint32_t *) p)[0]) +
|
|
|
|
ctpop32(((uint32_t *) p)[1]) +
|
|
|
|
ctpop32(((uint32_t *) p)[2]) +
|
|
|
|
ctpop32(((uint32_t *) p)[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!address_item) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_list = &info->value->ip_addresses;
|
|
|
|
|
|
|
|
while (*address_list && (*address_list)->next) {
|
|
|
|
address_list = &(*address_list)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*address_list) {
|
|
|
|
*address_list = address_item;
|
|
|
|
} else {
|
|
|
|
(*address_list)->next = address_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->value->has_ip_addresses = true;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
return head;
|
|
|
|
|
|
|
|
error:
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
qapi_free_GuestNetworkInterfaceList(head);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:59:30 +04:00
|
|
|
#define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
|
|
|
|
|
|
|
|
static long sysconf_exact(int name, const char *name_str, Error **err)
|
|
|
|
{
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ret = sysconf(name);
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == 0) {
|
|
|
|
error_setg(err, "sysconf(%s): value indefinite", name_str);
|
|
|
|
} else {
|
|
|
|
error_setg_errno(err, errno, "sysconf(%s)", name_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer online/offline status between @vcpu and the guest system.
|
|
|
|
*
|
|
|
|
* On input either @errp or *@errp must be NULL.
|
|
|
|
*
|
|
|
|
* In system-to-@vcpu direction, the following @vcpu fields are accessed:
|
|
|
|
* - R: vcpu->logical_id
|
|
|
|
* - W: vcpu->online
|
|
|
|
* - W: vcpu->can_offline
|
|
|
|
*
|
|
|
|
* In @vcpu-to-system direction, the following @vcpu fields are accessed:
|
|
|
|
* - R: vcpu->logical_id
|
|
|
|
* - R: vcpu->online
|
|
|
|
*
|
|
|
|
* Written members remain unmodified on error.
|
|
|
|
*/
|
|
|
|
static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
char *dirpath;
|
|
|
|
int dirfd;
|
|
|
|
|
|
|
|
dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
|
|
|
|
vcpu->logical_id);
|
|
|
|
dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
|
|
|
|
if (dirfd == -1) {
|
|
|
|
error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
|
|
|
|
} else {
|
|
|
|
static const char fn[] = "online";
|
|
|
|
int fd;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
|
|
|
|
} else if (sys2vcpu) {
|
|
|
|
vcpu->online = true;
|
|
|
|
vcpu->can_offline = false;
|
|
|
|
} else if (!vcpu->online) {
|
|
|
|
error_setg(errp, "logical processor #%" PRId64 " can't be "
|
|
|
|
"offlined", vcpu->logical_id);
|
|
|
|
} /* otherwise pretend successful re-onlining */
|
|
|
|
} else {
|
|
|
|
unsigned char status;
|
|
|
|
|
|
|
|
res = pread(fd, &status, 1, 0);
|
|
|
|
if (res == -1) {
|
|
|
|
error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
|
|
|
|
} else if (res == 0) {
|
|
|
|
error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
|
|
|
|
fn);
|
|
|
|
} else if (sys2vcpu) {
|
|
|
|
vcpu->online = (status != '0');
|
|
|
|
vcpu->can_offline = true;
|
|
|
|
} else if (vcpu->online != (status != '0')) {
|
|
|
|
status = '0' + vcpu->online;
|
|
|
|
if (pwrite(fd, &status, 1, 0) == -1) {
|
|
|
|
error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
|
|
|
|
fn);
|
|
|
|
}
|
|
|
|
} /* otherwise pretend successful re-(on|off)-lining */
|
|
|
|
|
|
|
|
res = close(fd);
|
|
|
|
g_assert(res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = close(dirfd);
|
|
|
|
g_assert(res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(dirpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
|
|
|
|
{
|
|
|
|
int64_t current;
|
|
|
|
GuestLogicalProcessorList *head, **link;
|
|
|
|
long sc_max;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
current = 0;
|
|
|
|
head = NULL;
|
|
|
|
link = &head;
|
|
|
|
sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
|
|
|
|
|
|
|
|
while (local_err == NULL && current < sc_max) {
|
|
|
|
GuestLogicalProcessor *vcpu;
|
|
|
|
GuestLogicalProcessorList *entry;
|
|
|
|
|
|
|
|
vcpu = g_malloc0(sizeof *vcpu);
|
|
|
|
vcpu->logical_id = current++;
|
|
|
|
vcpu->has_can_offline = true; /* lolspeak ftw */
|
|
|
|
transfer_vcpu(vcpu, true, &local_err);
|
|
|
|
|
|
|
|
entry = g_malloc0(sizeof *entry);
|
|
|
|
entry->value = vcpu;
|
|
|
|
|
|
|
|
*link = entry;
|
|
|
|
link = &entry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_err == NULL) {
|
|
|
|
/* there's no guest with zero VCPUs */
|
|
|
|
g_assert(head != NULL);
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
qapi_free_GuestLogicalProcessorList(head);
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:59:31 +04:00
|
|
|
int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
|
|
|
|
{
|
|
|
|
int64_t processed;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
processed = 0;
|
|
|
|
while (vcpus != NULL) {
|
|
|
|
transfer_vcpu(vcpus->value, false, &local_err);
|
|
|
|
if (local_err != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++processed;
|
|
|
|
vcpus = vcpus->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_err != NULL) {
|
|
|
|
if (processed == 0) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
} else {
|
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
2012-03-25 22:59:41 +04:00
|
|
|
#else /* defined(__linux__) */
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
void qmp_guest_suspend_disk(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
void qmp_guest_suspend_ram(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
void qmp_guest_suspend_hybrid(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
2012-04-14 06:07:36 +04:00
|
|
|
error_set(errp, QERR_UNSUPPORTED);
|
|
|
|
return NULL;
|
2012-03-25 22:59:41 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:59:30 +04:00
|
|
|
GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
|
|
|
|
{
|
|
|
|
error_set(errp, QERR_UNSUPPORTED);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:59:31 +04:00
|
|
|
int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
|
|
|
|
{
|
|
|
|
error_set(errp, QERR_UNSUPPORTED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_FSFREEZE)
|
|
|
|
|
|
|
|
GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
2012-04-14 06:07:36 +04:00
|
|
|
|
|
|
|
return 0;
|
2012-03-25 22:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
int64_t qmp_guest_fsfreeze_freeze(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
2012-04-14 06:07:36 +04:00
|
|
|
|
|
|
|
return 0;
|
2012-03-25 22:59:41 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 06:07:36 +04:00
|
|
|
int64_t qmp_guest_fsfreeze_thaw(Error **err)
|
2012-03-25 22:59:41 +04:00
|
|
|
{
|
2012-04-14 06:07:36 +04:00
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
|
|
|
|
|
|
return 0;
|
2012-03-25 22:59:41 +04:00
|
|
|
}
|
2012-06-13 09:41:28 +04:00
|
|
|
#endif /* CONFIG_FSFREEZE */
|
|
|
|
|
|
|
|
#if !defined(CONFIG_FSTRIM)
|
|
|
|
void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
|
|
|
|
{
|
|
|
|
error_set(err, QERR_UNSUPPORTED);
|
|
|
|
}
|
2012-03-25 22:59:41 +04:00
|
|
|
#endif
|
|
|
|
|
2011-07-20 00:41:55 +04:00
|
|
|
/* register init/cleanup routines for stateful command groups */
|
|
|
|
void ga_command_state_init(GAState *s, GACommandState *cs)
|
|
|
|
{
|
2011-07-22 23:14:17 +04:00
|
|
|
#if defined(CONFIG_FSFREEZE)
|
2012-04-18 04:01:45 +04:00
|
|
|
ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
|
2011-07-22 23:14:17 +04:00
|
|
|
#endif
|
2011-07-20 00:41:55 +04:00
|
|
|
ga_command_state_add(cs, guest_file_init, NULL);
|
|
|
|
}
|