diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2015-01-15 11:53:03 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2015-02-07 12:57:48 +0100 |
commit | a92b6f286bc8a9cd487a88e841e78c627ed98f85 (patch) | |
tree | c0be165d4f258752f153173c063cb500bcce1e62 | |
parent | c6ba09403a404d191e388db3b54d0145f057075c (diff) |
suspend: imported suspend/resume support from NetworkManager
We're going to make this optional during build, not mandatory as was the case
in NM.
-rw-r--r-- | configure.ac | 22 | ||||
-rw-r--r-- | src/Makefile.am | 10 | ||||
-rw-r--r-- | src/mm-sleep-monitor-systemd.c | 247 | ||||
-rw-r--r-- | src/mm-sleep-monitor-upower.c | 137 | ||||
-rw-r--r-- | src/mm-sleep-monitor.h | 43 |
5 files changed, 459 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index 22417cc1..49a95024 100644 --- a/configure.ac +++ b/configure.ac @@ -150,6 +150,27 @@ if test "x$with_systemdsystemunitdir" != xno; then fi AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$SYSTEMD_UNIT_DIR" -a "$SYSTEMD_UNIT_DIR" != xno ]) +# suspend/resume support +AC_ARG_WITH(suspend-resume, AS_HELP_STRING([--with-suspend-resume=no|upower|systemd], [Build ModemManager with specific suspend/resume support])) +if test "x$with_suspend_resume" = "x"; then + with_suspend_resume="none" +fi + +case $with_suspend_resume in + none) ;; + upower) ;; + systemd) + PKG_CHECK_MODULES(SYSTEMD_INHIBIT, [libsystemd >= 209],, + [PKG_CHECK_MODULES(SYSTEMD_INHIBIT, [libsystemd-login >= 183])]) + ;; + *) + AC_MSG_ERROR(--with-suspend-resume must be one of [none, upower, systemd]) + ;; +esac + +AM_CONDITIONAL(SUSPEND_RESUME_UPOWER, test "x$with_suspend_resume" = "xupower") +AM_CONDITIONAL(SUSPEND_RESUME_SYSTEMD, test "x$with_suspend_resume" = "xsystemd") + # PolicyKit PKG_CHECK_MODULES(POLKIT, [polkit-gobject-1 >= 0.97], [have_polkit=yes],[have_polkit=no]) AC_ARG_WITH(polkit, @@ -322,4 +343,5 @@ echo " Documentation: ${enable_gtk_doc} MBIM support: ${with_mbim} QMI support: ${with_qmi} + suspend/resume support: ${with_suspend_resume} " diff --git a/src/Makefile.am b/src/Makefile.am index 54297c0c..8a8cdc50 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -240,6 +240,16 @@ ModemManager_LDADD += $(POLKIT_LIBS) ModemManager_CPPFLAGS += $(POLKIT_CFLAGS) endif +# Additional suspend/resume support via systemd +if SUSPEND_RESUME_SYSTEMD +ModemManager_SOURCES += mm-sleep-monitor.h mm-sleep-monitor-systemd.c +endif + +# Additional suspend/resume support via upower +if SUSPEND_RESUME_UPOWER +ModemManager_SOURCES += mm-sleep-monitor.h mm-sleep-monitor-upower.c +endif + # Additional QMI support in ModemManager if WITH_QMI ModemManager_SOURCES += \ diff --git a/src/mm-sleep-monitor-systemd.c b/src/mm-sleep-monitor-systemd.c new file mode 100644 index 00000000..c5aae5aa --- /dev/null +++ b/src/mm-sleep-monitor-systemd.c @@ -0,0 +1,247 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* 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. + * + * (C) Copyright 2012 Red Hat, Inc. + * Author: Matthias Clasen <mclasen@redhat.com> + */ + +#include "config.h" + +#include <errno.h> +#include <string.h> +#include <sys/stat.h> +#include <glib/gi18n.h> +#include <gio/gio.h> +#include <gio/gunixfdlist.h> + +#include "mm-log.h" +#include "mm-utils.h" +#include "mm-sleep-monitor.h" + +#define SD_NAME "org.freedesktop.login1" +#define SD_PATH "/org/freedesktop/login1" +#define SD_INTERFACE "org.freedesktop.login1.Manager" + + +struct _MMSleepMonitor { + GObject parent_instance; + + GDBusProxy *sd_proxy; + gint inhibit_fd; +}; + +struct _MMSleepMonitorClass { + GObjectClass parent_class; + + void (*sleeping) (MMSleepMonitor *monitor); + void (*resuming) (MMSleepMonitor *monitor); +}; + + +enum { + SLEEPING, + RESUMING, + LAST_SIGNAL, +}; +static guint signals[LAST_SIGNAL] = {0}; + +G_DEFINE_TYPE (MMSleepMonitor, mm_sleep_monitor, G_TYPE_OBJECT); + +/********************************************************************/ + +static gboolean +drop_inhibitor (MMSleepMonitor *self) +{ + if (self->inhibit_fd >= 0) { + mm_dbg ("[sleep-monitor] dropping systemd sleep inhibitor"); + close (self->inhibit_fd); + self->inhibit_fd = -1; + return TRUE; + } + return FALSE; +} + +static void +inhibit_done (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GDBusProxy *sd_proxy = G_DBUS_PROXY (source); + MMSleepMonitor *self = user_data; + GError *error = NULL; + GVariant *res; + GUnixFDList *fd_list; + + res = g_dbus_proxy_call_with_unix_fd_list_finish (sd_proxy, &fd_list, result, &error); + if (!res) { + mm_warn ("[sleep-monitor] inhibit failed: %s", error->message); + g_error_free (error); + } else { + if (!fd_list || g_unix_fd_list_get_length (fd_list) != 1) + mm_warn ("[sleep-monitor] didn't get a single fd back"); + + self->inhibit_fd = g_unix_fd_list_get (fd_list, 0, NULL); + + mm_dbg ("[sleep-monitor] inhibitor fd is %d", self->inhibit_fd); + g_object_unref (fd_list); + g_variant_unref (res); + } +} + +static void +take_inhibitor (MMSleepMonitor *self) +{ + g_assert (self->inhibit_fd == -1); + + mm_dbg ("[sleep-monitor] taking systemd sleep inhibitor"); + g_dbus_proxy_call_with_unix_fd_list (self->sd_proxy, + "Inhibit", + g_variant_new ("(ssss)", + "sleep", + "ModemManager", + _("ModemManager needs to reset devices"), + "delay"), + 0, + G_MAXINT, + NULL, + NULL, + inhibit_done, + self); +} + +static void +signal_cb (GDBusProxy *proxy, + const gchar *sendername, + const gchar *signalname, + GVariant *args, + gpointer data) +{ + MMSleepMonitor *self = data; + gboolean is_about_to_suspend; + + if (strcmp (signalname, "PrepareForSleep") != 0) + return; + + g_variant_get (args, "(b)", &is_about_to_suspend); + mm_dbg ("[sleep-monitor] received PrepareForSleep signal: %d", is_about_to_suspend); + + if (is_about_to_suspend) { + g_signal_emit (self, signals[SLEEPING], 0); + drop_inhibitor (self); + } else { + take_inhibitor (self); + g_signal_emit (self, signals[RESUMING], 0); + } +} + +static void +name_owner_cb (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + GDBusProxy *proxy = G_DBUS_PROXY (object); + MMSleepMonitor *self = MM_SLEEP_MONITOR (user_data); + char *owner; + + g_assert (proxy == self->sd_proxy); + + owner = g_dbus_proxy_get_name_owner (proxy); + if (owner) + take_inhibitor (self); + else + drop_inhibitor (self); + g_free (owner); +} + +static void +on_proxy_acquired (GObject *object, + GAsyncResult *res, + MMSleepMonitor *self) +{ + GError *error = NULL; + char *owner; + + self->sd_proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + if (!self->sd_proxy) { + mm_warn ("[sleep-monitor] failed to acquire logind proxy: %s", error->message); + g_clear_error (&error); + return; + } + + g_signal_connect (self->sd_proxy, "notify::g-name-owner", G_CALLBACK (name_owner_cb), self); + g_signal_connect (self->sd_proxy, "g-signal", G_CALLBACK (signal_cb), self); + + owner = g_dbus_proxy_get_name_owner (self->sd_proxy); + if (owner) + take_inhibitor (self); + g_free (owner); +} + +static void +mm_sleep_monitor_init (MMSleepMonitor *self) +{ + self->inhibit_fd = -1; + g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | + G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, + NULL, + SD_NAME, SD_PATH, SD_INTERFACE, + NULL, + (GAsyncReadyCallback) on_proxy_acquired, self); +} + +static void +finalize (GObject *object) +{ + MMSleepMonitor *self = MM_SLEEP_MONITOR (object); + + drop_inhibitor (self); + if (self->sd_proxy) + g_object_unref (self->sd_proxy); + + if (G_OBJECT_CLASS (mm_sleep_monitor_parent_class)->finalize != NULL) + G_OBJECT_CLASS (mm_sleep_monitor_parent_class)->finalize (object); +} + +static void +mm_sleep_monitor_class_init (MMSleepMonitorClass *klass) +{ + GObjectClass *gobject_class; + + gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = finalize; + + signals[SLEEPING] = g_signal_new (MM_SLEEP_MONITOR_SLEEPING, + MM_TYPE_SLEEP_MONITOR, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MMSleepMonitorClass, sleeping), + NULL, /* accumulator */ + NULL, /* accumulator data */ + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + signals[RESUMING] = g_signal_new (MM_SLEEP_MONITOR_RESUMING, + MM_TYPE_SLEEP_MONITOR, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MMSleepMonitorClass, resuming), + NULL, /* accumulator */ + NULL, /* accumulator data */ + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +MM_DEFINE_SINGLETON_GETTER (MMSleepMonitor, mm_sleep_monitor_get, MM_TYPE_SLEEP_MONITOR); + +/* ---------------------------------------------------------------------------------------------------- */ diff --git a/src/mm-sleep-monitor-upower.c b/src/mm-sleep-monitor-upower.c new file mode 100644 index 00000000..37760fec --- /dev/null +++ b/src/mm-sleep-monitor-upower.c @@ -0,0 +1,137 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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. + * + * (C) Copyright 2012 Red Hat, Inc. + * Author: Matthias Clasen <mclasen@redhat.com> + */ + +#include "config.h" + +#include <errno.h> +#include <string.h> +#include <sys/stat.h> +#include <dbus/dbus-glib.h> +#include <gio/gio.h> + +#include "mm-log.h" +#include "mm-utils.h" +#include "mm-sleep-monitor.h" + +#define UPOWER_DBUS_SERVICE "org.freedesktop.UPower" + +struct _MMSleepMonitor { + GObject parent_instance; + + DBusGProxy *upower_proxy; +}; + +struct _MMSleepMonitorClass { + GObjectClass parent_class; + + void (*sleeping) (MMSleepMonitor *monitor); + void (*resuming) (MMSleepMonitor *monitor); +}; + + +enum { + SLEEPING, + RESUMING, + LAST_SIGNAL, +}; +static guint signals[LAST_SIGNAL] = {0}; + +G_DEFINE_TYPE (MMSleepMonitor, mm_sleep_monitor, G_TYPE_OBJECT); + +/********************************************************************/ + +static void +upower_sleeping_cb (DBusGProxy *proxy, gpointer user_data) +{ + mm_dbg ("[sleep-monitor] received UPower sleeping signal"); + g_signal_emit (user_data, signals[SLEEPING], 0); +} + +static void +upower_resuming_cb (DBusGProxy *proxy, gpointer user_data) +{ + mm_dbg ("[sleep-monitor] received UPower resuming signal"); + g_signal_emit (user_data, signals[RESUMING], 0); +} + +static void +mm_sleep_monitor_init (MMSleepMonitor *self) +{ + DBusGConnection *bus; + + bus = mm_dbus_manager_get_connection (mm_dbus_manager_get ()); + self->upower_proxy = dbus_g_proxy_new_for_name (bus, + UPOWER_DBUS_SERVICE, + "/org/freedesktop/UPower", + "org.freedesktop.UPower"); + if (self->upower_proxy) { + dbus_g_proxy_add_signal (self->upower_proxy, "Sleeping", G_TYPE_INVALID); + dbus_g_proxy_connect_signal (self->upower_proxy, "Sleeping", + G_CALLBACK (upower_sleeping_cb), + self, NULL); + + dbus_g_proxy_add_signal (self->upower_proxy, "Resuming", G_TYPE_INVALID); + dbus_g_proxy_connect_signal (self->upower_proxy, "Resuming", + G_CALLBACK (upower_resuming_cb), + self, NULL); + } else + mm_warn ("could not initialize UPower D-Bus proxy"); +} + +static void +finalize (GObject *object) +{ + MMSleepMonitor *self = MM_SLEEP_MONITOR (object); + + if (self->upower_proxy) + g_object_unref (self->upower_proxy); + + if (G_OBJECT_CLASS (mm_sleep_monitor_parent_class)->finalize != NULL) + G_OBJECT_CLASS (mm_sleep_monitor_parent_class)->finalize (object); +} + +static void +mm_sleep_monitor_class_init (MMSleepMonitorClass *klass) +{ + GObjectClass *gobject_class; + + gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = finalize; + + signals[SLEEPING] = g_signal_new (MM_SLEEP_MONITOR_SLEEPING, + MM_TYPE_SLEEP_MONITOR, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MMSleepMonitorClass, sleeping), + NULL, /* accumulator */ + NULL, /* accumulator data */ + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + signals[RESUMING] = g_signal_new (MM_SLEEP_MONITOR_RESUMING, + MM_TYPE_SLEEP_MONITOR, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MMSleepMonitorClass, resuming), + NULL, /* accumulator */ + NULL, /* accumulator data */ + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +MM_DEFINE_SINGLETON_GETTER (MMSleepMonitor, mm_sleep_monitor_get, MM_TYPE_SLEEP_MONITOR); diff --git a/src/mm-sleep-monitor.h b/src/mm-sleep-monitor.h new file mode 100644 index 00000000..2fac5517 --- /dev/null +++ b/src/mm-sleep-monitor.h @@ -0,0 +1,43 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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. + * + * (C) Copyright 2012 Red Hat, Inc. + * Author: Matthias Clasen <mclasen@redhat.com> + * Original code imported from NetworkManager. + */ + +#ifndef __MM_SLEEP_MONITOR_H__ +#define __MM_SLEEP_MONITOR_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define MM_TYPE_SLEEP_MONITOR (mm_sleep_monitor_get_type ()) +#define MM_SLEEP_MONITOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MM_TYPE_SLEEP_MONITOR, MMSleepMonitor)) +#define MM_SLEEP_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), MM_TYPE_SLEEP_MONITOR, MMSleepMonitorClass)) +#define MM_SLEEP_MONITOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MM_TYPE_SLEEP_MONITOR, MMSleepMonitorClass)) +#define MM_IS_SLEEP_MONITOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MM_TYPE_SLEEP_MONITOR)) +#define MM_IS_SLEEP_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MM_TYPE_SLEEP_MONITOR)) + +#define MM_SLEEP_MONITOR_SLEEPING "sleeping" +#define MM_SLEEP_MONITOR_RESUMING "resuming" + +typedef struct _MMSleepMonitor MMSleepMonitor; +typedef struct _MMSleepMonitorClass MMSleepMonitorClass; + +GType mm_sleep_monitor_get_type (void) G_GNUC_CONST; +MMSleepMonitor *mm_sleep_monitor_get (void); + +G_END_DECLS + +#endif /* __MM_SLEEP_MONITOR_H__ */ |