1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/* virt-p2v
* Copyright (C) 2016 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* This file is used to inhibit power saving, sleep, suspend etc during
* the conversion.
*
* The method it uses is to send a D-Bus message to logind, as
* described here:
*
* https://www.freedesktop.org/wiki/Software/systemd/inhibit/
*
* If virt-p2v is compiled without D-Bus support then this does nothing.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_DBUS)
#include <dbus/dbus.h>
#endif
#include "p2v.h"
/**
* Inhibit all forms of power saving. A file descriptor is returned,
* and when the file descriptor is closed the inhibit is stopped.
*
* If the function returns C<-1> then C<Inhibit> operation could not
* be performed (eg. if we are compiled without D-Bus support, or there
* is some error contacting logind). This is not usually fatal from
* the point of view of the caller, conversion can continue.
*/
int
inhibit_power_saving (void)
{
#if defined(HAVE_DBUS) && defined(DBUS_TYPE_UNIX_FD)
DBusError err;
DBusConnection *conn = NULL;
DBusMessage *msg = NULL;
DBusMessageIter args;
DBusPendingCall *pending = NULL;
const char *what = "shutdown:sleep:idle";
const char *who = "virt-p2v";
const char *why = "virt-p2v conversion is running";
const char *mode = "block";
int fd = -1;
dbus_error_init (&err);
conn = dbus_bus_get (DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set (&err)) {
fprintf (stderr, "inhibit_power_saving: dbus: cannot connect to system bus: %s\n", err.message);
goto out;
}
if (conn == NULL)
goto out;
msg = dbus_message_new_method_call ("org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit");
if (msg == NULL) {
fprintf (stderr, "inhibit_power_saving: dbus: cannot create message\n");
goto out;
}
dbus_message_iter_init_append (msg, &args);
if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &what) ||
!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &who) ||
!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &why) ||
!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &mode)) {
fprintf (stderr, "inhibit_power_saving: dbus: cannot add message arguments\n");
goto out;
}
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) {
fprintf (stderr, "inhibit_power_saving: dbus: cannot send Inhibit message to logind\n");
goto out;
}
if (pending == NULL)
goto out;
dbus_connection_flush (conn);
dbus_message_unref (msg);
msg = NULL;
dbus_pending_call_block (pending);
msg = dbus_pending_call_steal_reply (pending);
if (msg == NULL) {
fprintf (stderr, "inhibit_power_saving: dbus: could not read message reply\n");
goto out;
}
dbus_pending_call_unref (pending);
pending = NULL;
if (!dbus_message_iter_init (msg, &args)) {
fprintf (stderr, "inhibit_power_saving: dbus: message reply has no return value\n");
goto out;
}
if (dbus_message_iter_get_arg_type (&args) != DBUS_TYPE_UNIX_FD) {
fprintf (stderr, "inhibit_power_saving: dbus: message reply is not a file descriptor\n");
goto out;
}
dbus_message_iter_get_basic (&args, &fd);
#ifdef DEBUG_STDERR
fprintf (stderr, "inhibit_power_saving: dbus: Inhibit() call returned file descriptor %d\n", fd);
#endif
out:
if (pending != NULL)
dbus_pending_call_unref (pending);
if (msg != NULL)
dbus_message_unref (msg);
/* This is the system bus connection, so unref-ing it does not
* actually close it.
*/
if (conn != NULL)
dbus_connection_unref (conn);
dbus_error_free (&err);
return fd;
#else /* !dbus */
#ifdef DEBUG_STDERR
fprintf (stderr, "warning: virt-p2v compiled without D-Bus support.\n");
#endif
return -1;
#endif
}
|