sleep-monitor-systemd: merge branch 'th/sleep-monitor-sd-cleanup'
Some cleanup of "nm-sleep-monitor-systemd.c"
This commit is contained in:
@@ -55,17 +55,20 @@ struct _NMSleepMonitor {
|
|||||||
GObject parent_instance;
|
GObject parent_instance;
|
||||||
|
|
||||||
GDBusProxy *sd_proxy;
|
GDBusProxy *sd_proxy;
|
||||||
|
|
||||||
|
/* used both during construction of sd_proxy and during Inhibit call. */
|
||||||
|
GCancellable *cancellable;
|
||||||
|
|
||||||
gint inhibit_fd;
|
gint inhibit_fd;
|
||||||
|
|
||||||
|
gulong sig_id_1;
|
||||||
|
gulong sig_id_2;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _NMSleepMonitorClass {
|
struct _NMSleepMonitorClass {
|
||||||
GObjectClass parent_class;
|
GObjectClass parent_class;
|
||||||
|
|
||||||
void (*sleeping) (NMSleepMonitor *monitor);
|
|
||||||
void (*resuming) (NMSleepMonitor *monitor);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
SLEEPING,
|
SLEEPING,
|
||||||
RESUMING,
|
RESUMING,
|
||||||
@@ -75,18 +78,47 @@ static guint signals[LAST_SIGNAL] = {0};
|
|||||||
|
|
||||||
G_DEFINE_TYPE (NMSleepMonitor, nm_sleep_monitor, G_TYPE_OBJECT);
|
G_DEFINE_TYPE (NMSleepMonitor, nm_sleep_monitor, G_TYPE_OBJECT);
|
||||||
|
|
||||||
/********************************************************************/
|
NM_DEFINE_SINGLETON_GETTER (NMSleepMonitor, nm_sleep_monitor_get, NM_TYPE_SLEEP_MONITOR);
|
||||||
|
|
||||||
static gboolean
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef SUSPEND_RESUME_SYSTEMD
|
||||||
|
#define _NMLOG_PREFIX_NAME "sleep-monitor-sd"
|
||||||
|
#else
|
||||||
|
#define _NMLOG_PREFIX_NAME "sleep-monitor-ck"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define _NMLOG_DOMAIN LOGD_SUSPEND
|
||||||
|
#define _NMLOG(level, ...) \
|
||||||
|
G_STMT_START { \
|
||||||
|
const NMLogLevel __level = (level); \
|
||||||
|
\
|
||||||
|
if (nm_logging_enabled (__level, _NMLOG_DOMAIN)) { \
|
||||||
|
char __prefix[20]; \
|
||||||
|
const NMSleepMonitor *const __self = (self); \
|
||||||
|
\
|
||||||
|
_nm_log (__level, _NMLOG_DOMAIN, 0, \
|
||||||
|
"%s%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
|
||||||
|
_NMLOG_PREFIX_NAME, \
|
||||||
|
(!__self || __self == singleton_instance \
|
||||||
|
? "" \
|
||||||
|
: nm_sprintf_buf (__prefix, "[%p]", __self)) \
|
||||||
|
_NM_UTILS_MACRO_REST (__VA_ARGS__)); \
|
||||||
|
} \
|
||||||
|
} G_STMT_END
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
static void
|
||||||
drop_inhibitor (NMSleepMonitor *self)
|
drop_inhibitor (NMSleepMonitor *self)
|
||||||
{
|
{
|
||||||
if (self->inhibit_fd >= 0) {
|
if (self->inhibit_fd >= 0) {
|
||||||
nm_log_dbg (LOGD_SUSPEND, "Dropping systemd sleep inhibitor");
|
_LOGD ("Dropping systemd sleep inhibitor %d", self->inhibit_fd);
|
||||||
close (self->inhibit_fd);
|
close (self->inhibit_fd);
|
||||||
self->inhibit_fd = -1;
|
self->inhibit_fd = -1;
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
return FALSE;
|
|
||||||
|
nm_clear_g_cancellable (&self->cancellable);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -96,44 +128,51 @@ inhibit_done (GObject *source,
|
|||||||
{
|
{
|
||||||
GDBusProxy *sd_proxy = G_DBUS_PROXY (source);
|
GDBusProxy *sd_proxy = G_DBUS_PROXY (source);
|
||||||
NMSleepMonitor *self = user_data;
|
NMSleepMonitor *self = user_data;
|
||||||
GError *error = NULL;
|
gs_free_error GError *error = NULL;
|
||||||
GVariant *res;
|
gs_unref_variant GVariant *res = NULL;
|
||||||
GUnixFDList *fd_list;
|
gs_unref_object GUnixFDList *fd_list = NULL;
|
||||||
|
|
||||||
res = g_dbus_proxy_call_with_unix_fd_list_finish (sd_proxy, &fd_list, result, &error);
|
res = g_dbus_proxy_call_with_unix_fd_list_finish (sd_proxy, &fd_list, result, &error);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
g_dbus_error_strip_remote_error (error);
|
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
|
||||||
nm_log_warn (LOGD_SUSPEND, "Inhibit failed: %s", error->message);
|
g_clear_object (&self->cancellable);
|
||||||
g_error_free (error);
|
_LOGW ("Inhibit failed: %s", error->message);
|
||||||
} else {
|
}
|
||||||
if (!fd_list || g_unix_fd_list_get_length (fd_list) != 1)
|
return;
|
||||||
nm_log_warn (LOGD_SUSPEND, "Didn't get a single fd back");
|
|
||||||
|
|
||||||
self->inhibit_fd = g_unix_fd_list_get (fd_list, 0, NULL);
|
|
||||||
|
|
||||||
nm_log_dbg (LOGD_SUSPEND, "Inhibitor fd is %d", self->inhibit_fd);
|
|
||||||
g_object_unref (fd_list);
|
|
||||||
g_variant_unref (res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_clear_object (&self->cancellable);
|
||||||
|
|
||||||
|
if (!fd_list || g_unix_fd_list_get_length (fd_list) != 1) {
|
||||||
|
_LOGW ("Didn't get a single fd back");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->inhibit_fd = g_unix_fd_list_get (fd_list, 0, NULL);
|
||||||
|
_LOGD ("Inhibitor fd is %d", self->inhibit_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
take_inhibitor (NMSleepMonitor *self)
|
take_inhibitor (NMSleepMonitor *self)
|
||||||
{
|
{
|
||||||
g_assert (self->inhibit_fd == -1);
|
g_return_if_fail (NM_IS_SLEEP_MONITOR (self));
|
||||||
|
g_return_if_fail (G_IS_DBUS_PROXY (self->sd_proxy));
|
||||||
|
|
||||||
nm_log_dbg (LOGD_SUSPEND, "Taking systemd sleep inhibitor");
|
drop_inhibitor (self);
|
||||||
|
|
||||||
|
_LOGD ("Taking systemd sleep inhibitor");
|
||||||
|
self->cancellable = g_cancellable_new ();
|
||||||
g_dbus_proxy_call_with_unix_fd_list (self->sd_proxy,
|
g_dbus_proxy_call_with_unix_fd_list (self->sd_proxy,
|
||||||
"Inhibit",
|
"Inhibit",
|
||||||
g_variant_new ("(ssss)",
|
g_variant_new ("(ssss)",
|
||||||
"sleep",
|
"sleep",
|
||||||
"NetworkManager",
|
"NetworkManager",
|
||||||
_("NetworkManager needs to turn off networks"),
|
"NetworkManager needs to turn off networks",
|
||||||
"delay"),
|
"delay"),
|
||||||
0,
|
0,
|
||||||
G_MAXINT,
|
G_MAXINT,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
self->cancellable,
|
||||||
inhibit_done,
|
inhibit_done,
|
||||||
self);
|
self);
|
||||||
}
|
}
|
||||||
@@ -145,7 +184,7 @@ prepare_for_sleep_cb (GDBusProxy *proxy,
|
|||||||
{
|
{
|
||||||
NMSleepMonitor *self = data;
|
NMSleepMonitor *self = data;
|
||||||
|
|
||||||
nm_log_dbg (LOGD_SUSPEND, "Received PrepareForSleep signal: %d", is_about_to_suspend);
|
_LOGD ("Received PrepareForSleep signal: %d", is_about_to_suspend);
|
||||||
|
|
||||||
if (is_about_to_suspend) {
|
if (is_about_to_suspend) {
|
||||||
g_signal_emit (self, signals[SLEEPING], 0);
|
g_signal_emit (self, signals[SLEEPING], 0);
|
||||||
@@ -182,17 +221,23 @@ on_proxy_acquired (GObject *object,
|
|||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
char *owner;
|
char *owner;
|
||||||
|
GDBusProxy *sd_proxy;
|
||||||
|
|
||||||
self->sd_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
|
sd_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
|
||||||
if (!self->sd_proxy) {
|
if (!sd_proxy) {
|
||||||
nm_log_warn (LOGD_SUSPEND, "Failed to acquire logind proxy: %s", error->message);
|
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
||||||
|
_LOGW ("Failed to acquire logind proxy: %s", error->message);
|
||||||
g_clear_error (&error);
|
g_clear_error (&error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
self->sd_proxy = sd_proxy;
|
||||||
|
g_clear_object (&self->cancellable);
|
||||||
|
|
||||||
g_signal_connect (self->sd_proxy, "notify::g-name-owner", G_CALLBACK (name_owner_cb), self);
|
self->sig_id_1 = g_signal_connect (self->sd_proxy, "notify::g-name-owner",
|
||||||
_nm_dbus_signal_connect (self->sd_proxy, "PrepareForSleep", G_VARIANT_TYPE ("(b)"),
|
G_CALLBACK (name_owner_cb), self);
|
||||||
G_CALLBACK (prepare_for_sleep_cb), self);
|
self->sig_id_2 = _nm_dbus_signal_connect (self->sd_proxy, "PrepareForSleep",
|
||||||
|
G_VARIANT_TYPE ("(b)"),
|
||||||
|
G_CALLBACK (prepare_for_sleep_cb), self);
|
||||||
|
|
||||||
owner = g_dbus_proxy_get_name_owner (self->sd_proxy);
|
owner = g_dbus_proxy_get_name_owner (self->sd_proxy);
|
||||||
if (owner)
|
if (owner)
|
||||||
@@ -204,26 +249,31 @@ static void
|
|||||||
nm_sleep_monitor_init (NMSleepMonitor *self)
|
nm_sleep_monitor_init (NMSleepMonitor *self)
|
||||||
{
|
{
|
||||||
self->inhibit_fd = -1;
|
self->inhibit_fd = -1;
|
||||||
|
self->cancellable = g_cancellable_new ();
|
||||||
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
|
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
|
||||||
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
|
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
|
||||||
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
|
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
|
||||||
NULL,
|
NULL,
|
||||||
SUSPEND_DBUS_NAME, SUSPEND_DBUS_PATH, SUSPEND_DBUS_INTERFACE,
|
SUSPEND_DBUS_NAME, SUSPEND_DBUS_PATH, SUSPEND_DBUS_INTERFACE,
|
||||||
NULL,
|
self->cancellable,
|
||||||
(GAsyncReadyCallback) on_proxy_acquired, self);
|
(GAsyncReadyCallback) on_proxy_acquired, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
finalize (GObject *object)
|
dispose (GObject *object)
|
||||||
{
|
{
|
||||||
NMSleepMonitor *self = NM_SLEEP_MONITOR (object);
|
NMSleepMonitor *self = NM_SLEEP_MONITOR (object);
|
||||||
|
|
||||||
|
/* drop_inhibitor() also clears our "cancellable" */
|
||||||
drop_inhibitor (self);
|
drop_inhibitor (self);
|
||||||
if (self->sd_proxy)
|
|
||||||
g_object_unref (self->sd_proxy);
|
|
||||||
|
|
||||||
if (G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize != NULL)
|
if (self->sd_proxy) {
|
||||||
G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize (object);
|
nm_clear_g_signal_handler (self->sd_proxy, &self->sig_id_1);
|
||||||
|
nm_clear_g_signal_handler (self->sd_proxy, &self->sig_id_2);
|
||||||
|
g_clear_object (&self->sd_proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->dispose (object);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -233,26 +283,19 @@ nm_sleep_monitor_class_init (NMSleepMonitorClass *klass)
|
|||||||
|
|
||||||
gobject_class = G_OBJECT_CLASS (klass);
|
gobject_class = G_OBJECT_CLASS (klass);
|
||||||
|
|
||||||
gobject_class->finalize = finalize;
|
gobject_class->dispose = dispose;
|
||||||
|
|
||||||
signals[SLEEPING] = g_signal_new (NM_SLEEP_MONITOR_SLEEPING,
|
signals[SLEEPING] = g_signal_new (NM_SLEEP_MONITOR_SLEEPING,
|
||||||
NM_TYPE_SLEEP_MONITOR,
|
NM_TYPE_SLEEP_MONITOR,
|
||||||
G_SIGNAL_RUN_LAST,
|
G_SIGNAL_RUN_LAST,
|
||||||
G_STRUCT_OFFSET (NMSleepMonitorClass, sleeping),
|
0, NULL, NULL,
|
||||||
NULL, /* accumulator */
|
|
||||||
NULL, /* accumulator data */
|
|
||||||
g_cclosure_marshal_VOID__VOID,
|
g_cclosure_marshal_VOID__VOID,
|
||||||
G_TYPE_NONE, 0);
|
G_TYPE_NONE, 0);
|
||||||
signals[RESUMING] = g_signal_new (NM_SLEEP_MONITOR_RESUMING,
|
signals[RESUMING] = g_signal_new (NM_SLEEP_MONITOR_RESUMING,
|
||||||
NM_TYPE_SLEEP_MONITOR,
|
NM_TYPE_SLEEP_MONITOR,
|
||||||
G_SIGNAL_RUN_LAST,
|
G_SIGNAL_RUN_LAST,
|
||||||
G_STRUCT_OFFSET (NMSleepMonitorClass, resuming),
|
0, NULL, NULL,
|
||||||
NULL, /* accumulator */
|
|
||||||
NULL, /* accumulator data */
|
|
||||||
g_cclosure_marshal_VOID__VOID,
|
g_cclosure_marshal_VOID__VOID,
|
||||||
G_TYPE_NONE, 0);
|
G_TYPE_NONE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
NM_DEFINE_SINGLETON_GETTER (NMSleepMonitor, nm_sleep_monitor_get, NM_TYPE_SLEEP_MONITOR);
|
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------------- */
|
|
||||||
|
Reference in New Issue
Block a user