2009-07-27 16:40:11 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the GNU General Public License Version 2
|
|
|
|
|
*
|
|
|
|
|
* 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
|
2010-01-27 16:27:15 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-07-27 16:40:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
#include <gudev/gudev.h>
|
|
|
|
|
|
|
|
|
|
#include "egg-debug.h"
|
|
|
|
|
|
2010-01-18 12:26:55 +00:00
|
|
|
#include "up-backend.h"
|
|
|
|
|
#include "up-daemon.h"
|
|
|
|
|
#include "up-marshal.h"
|
|
|
|
|
#include "up-device.h"
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:26:55 +00:00
|
|
|
#include "up-device-supply.h"
|
|
|
|
|
#include "up-device-csr.h"
|
|
|
|
|
#include "up-device-wup.h"
|
|
|
|
|
#include "up-device-hid.h"
|
|
|
|
|
#include "up-input.h"
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
static void up_backend_class_init (UpBackendClass *klass);
|
|
|
|
|
static void up_backend_init (UpBackend *backend);
|
|
|
|
|
static void up_backend_finalize (GObject *object);
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
#define UP_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_BACKEND, UpBackendPrivate))
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
struct UpBackendPrivate
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
2010-01-18 12:35:47 +00:00
|
|
|
UpDaemon *daemon;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDeviceList *device_list;
|
2009-07-27 16:40:11 +01:00
|
|
|
GUdevClient *gudev_client;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDeviceList *managed_devices;
|
2009-07-27 16:40:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
SIGNAL_DEVICE_ADDED,
|
|
|
|
|
SIGNAL_DEVICE_REMOVED,
|
|
|
|
|
SIGNAL_LAST
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static guint signals [SIGNAL_LAST] = { 0 };
|
|
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
static gboolean up_backend_device_add (UpBackend *backend, GUdevDevice *native);
|
|
|
|
|
static void up_backend_device_remove (UpBackend *backend, GUdevDevice *native);
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_device_new:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
2010-01-18 12:49:30 +00:00
|
|
|
static UpDevice *
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_new (UpBackend *backend, GUdevDevice *native)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
|
|
|
|
const gchar *subsys;
|
|
|
|
|
const gchar *native_path;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDevice *device = NULL;
|
2010-01-18 13:15:47 +00:00
|
|
|
UpInput *input;
|
2009-07-27 16:40:11 +01:00
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
subsys = g_udev_device_get_subsystem (native);
|
|
|
|
|
if (g_strcmp0 (subsys, "power_supply") == 0) {
|
|
|
|
|
|
|
|
|
|
/* are we a valid power supply */
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (up_device_supply_new ());
|
|
|
|
|
ret = up_device_coldplug (device, backend->priv->daemon, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
g_object_unref (device);
|
|
|
|
|
|
|
|
|
|
/* no valid power supply object */
|
|
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
|
|
} else if (g_strcmp0 (subsys, "tty") == 0) {
|
|
|
|
|
|
|
|
|
|
/* try to detect a Watts Up? Pro monitor */
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (up_device_wup_new ());
|
|
|
|
|
ret = up_device_coldplug (device, backend->priv->daemon, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
g_object_unref (device);
|
|
|
|
|
|
2009-09-11 12:05:16 +01:00
|
|
|
/* no valid TTY object */
|
2009-07-27 16:40:11 +01:00
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
|
|
} else if (g_strcmp0 (subsys, "usb") == 0) {
|
|
|
|
|
|
|
|
|
|
/* see if this is a CSR mouse or keyboard */
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (up_device_csr_new ());
|
|
|
|
|
ret = up_device_coldplug (device, backend->priv->daemon, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
g_object_unref (device);
|
|
|
|
|
|
|
|
|
|
/* try to detect a HID UPS */
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (up_device_hid_new ());
|
|
|
|
|
ret = up_device_coldplug (device, backend->priv->daemon, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
g_object_unref (device);
|
|
|
|
|
|
2009-09-11 12:05:16 +01:00
|
|
|
/* no valid USB object */
|
2009-07-27 16:40:11 +01:00
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
|
|
} else if (g_strcmp0 (subsys, "input") == 0) {
|
|
|
|
|
|
|
|
|
|
/* check input device */
|
2010-01-18 13:15:47 +00:00
|
|
|
input = up_input_new ();
|
|
|
|
|
ret = up_input_coldplug (input, backend->priv->daemon, native);
|
2009-07-27 16:40:11 +01:00
|
|
|
if (!ret) {
|
|
|
|
|
g_object_unref (input);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* we now have a lid */
|
|
|
|
|
g_object_set (backend->priv->daemon,
|
2009-07-27 17:15:27 +01:00
|
|
|
"lid-is-present", TRUE,
|
2009-07-27 16:40:11 +01:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
/* not a power device */
|
2010-01-18 12:49:30 +00:00
|
|
|
up_device_list_insert (backend->priv->managed_devices, G_OBJECT (native), G_OBJECT (input));
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
/* no valid input object */
|
|
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
native_path = g_udev_device_get_sysfs_path (native);
|
|
|
|
|
egg_warning ("native path %s (%s) ignoring", native_path, subsys);
|
|
|
|
|
}
|
|
|
|
|
out:
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-11 12:05:16 +01:00
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_device_changed:
|
2009-09-11 12:05:16 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_changed (UpBackend *backend, GUdevDevice *native)
|
2009-09-11 12:05:16 +01:00
|
|
|
{
|
|
|
|
|
GObject *object;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDevice *device;
|
2009-09-11 12:05:16 +01:00
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
/* first, check the device and add it if it doesn't exist */
|
2010-01-18 12:49:30 +00:00
|
|
|
object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
|
2009-09-11 12:05:16 +01:00
|
|
|
if (object == NULL) {
|
|
|
|
|
egg_warning ("treating change event as add on %s", g_udev_device_get_sysfs_path (native));
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_add (backend, native);
|
2009-09-11 12:05:16 +01:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* need to refresh device */
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (object);
|
|
|
|
|
ret = up_device_refresh_internal (device);
|
2009-09-11 12:05:16 +01:00
|
|
|
if (!ret) {
|
2010-01-18 12:49:30 +00:00
|
|
|
egg_debug ("no changes on %s", up_device_get_object_path (device));
|
2009-09-11 12:05:16 +01:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
out:
|
2009-09-11 12:34:25 +01:00
|
|
|
if (object != NULL)
|
|
|
|
|
g_object_unref (object);
|
2009-09-11 12:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 16:40:11 +01:00
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_device_add:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static gboolean
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_add (UpBackend *backend, GUdevDevice *native)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
|
|
|
|
GObject *object;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDevice *device;
|
2009-07-27 16:40:11 +01:00
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
|
|
/* does device exist in db? */
|
2010-01-18 12:49:30 +00:00
|
|
|
object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (object != NULL) {
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (object);
|
2009-07-27 16:40:11 +01:00
|
|
|
/* we already have the device; treat as change event */
|
2010-01-18 12:49:30 +00:00
|
|
|
egg_warning ("treating add event as change event on %s", up_device_get_object_path (device));
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_changed (backend, native);
|
2009-07-27 16:40:11 +01:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get the right sort of device */
|
2010-01-18 12:32:06 +00:00
|
|
|
device = up_backend_device_new (backend, native);
|
2009-07-27 16:40:11 +01:00
|
|
|
if (device == NULL) {
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* emit */
|
2009-09-11 12:05:16 +01:00
|
|
|
g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, native, device);
|
2009-07-27 16:40:11 +01:00
|
|
|
out:
|
2009-09-11 12:34:25 +01:00
|
|
|
if (object != NULL)
|
|
|
|
|
g_object_unref (object);
|
2009-07-27 16:40:11 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_device_remove:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_remove (UpBackend *backend, GUdevDevice *native)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
|
|
|
|
GObject *object;
|
2010-01-18 12:49:30 +00:00
|
|
|
UpDevice *device;
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
/* does device exist in db? */
|
2010-01-18 12:49:30 +00:00
|
|
|
object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
|
2009-07-27 16:40:11 +01:00
|
|
|
if (object == NULL) {
|
2009-09-11 14:08:08 +01:00
|
|
|
egg_debug ("ignoring remove event on %s", g_udev_device_get_sysfs_path (native));
|
2009-09-11 12:34:25 +01:00
|
|
|
goto out;
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
2009-09-11 12:34:25 +01:00
|
|
|
|
2010-01-18 12:49:30 +00:00
|
|
|
device = UP_DEVICE (object);
|
2009-09-11 12:34:25 +01:00
|
|
|
/* emit */
|
|
|
|
|
egg_debug ("emitting device-removed: %s", g_udev_device_get_sysfs_path (native));
|
|
|
|
|
g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, native, device);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (object != NULL)
|
|
|
|
|
g_object_unref (object);
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_uevent_signal_handler_cb:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_uevent_signal_handler_cb (GUdevClient *client, const gchar *action,
|
2009-07-27 16:40:11 +01:00
|
|
|
GUdevDevice *device, gpointer user_data)
|
|
|
|
|
{
|
2010-01-18 12:32:06 +00:00
|
|
|
UpBackend *backend = UP_BACKEND (user_data);
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
if (g_strcmp0 (action, "add") == 0) {
|
2009-09-11 12:05:16 +01:00
|
|
|
egg_debug ("SYSFS add %s", g_udev_device_get_sysfs_path (device));
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_add (backend, device);
|
2009-07-27 16:40:11 +01:00
|
|
|
} else if (g_strcmp0 (action, "remove") == 0) {
|
2009-09-11 12:05:16 +01:00
|
|
|
egg_debug ("SYSFS remove %s", g_udev_device_get_sysfs_path (device));
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_remove (backend, device);
|
2009-07-27 16:40:11 +01:00
|
|
|
} else if (g_strcmp0 (action, "change") == 0) {
|
2009-09-11 12:05:16 +01:00
|
|
|
egg_debug ("SYSFS change %s", g_udev_device_get_sysfs_path (device));
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_changed (backend, device);
|
2009-07-27 16:40:11 +01:00
|
|
|
} else {
|
|
|
|
|
egg_warning ("unhandled action '%s' on %s", action, g_udev_device_get_sysfs_path (device));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_coldplug:
|
|
|
|
|
* @backend: The %UpBackend class instance
|
2010-01-18 12:35:47 +00:00
|
|
|
* @daemon: The %UpDaemon controlling instance
|
2009-07-27 16:40:11 +01:00
|
|
|
*
|
|
|
|
|
* Finds all the devices already plugged in, and emits device-add signals for
|
|
|
|
|
* each of them.
|
|
|
|
|
*
|
|
|
|
|
* Return value: %TRUE for success
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2010-01-18 12:35:47 +00:00
|
|
|
up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
|
|
|
|
GUdevDevice *native;
|
|
|
|
|
GList *devices;
|
|
|
|
|
GList *l;
|
|
|
|
|
guint i;
|
|
|
|
|
const gchar *subsystems[] = {"power_supply", "usb", "tty", "input", NULL};
|
|
|
|
|
|
|
|
|
|
backend->priv->daemon = g_object_ref (daemon);
|
2010-01-18 12:35:47 +00:00
|
|
|
backend->priv->device_list = up_daemon_get_device_list (daemon);
|
2009-09-11 09:58:46 +01:00
|
|
|
backend->priv->gudev_client = g_udev_client_new (subsystems);
|
|
|
|
|
g_signal_connect (backend->priv->gudev_client, "uevent",
|
2010-01-18 12:32:06 +00:00
|
|
|
G_CALLBACK (up_backend_uevent_signal_handler_cb), backend);
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
/* add all subsystems */
|
|
|
|
|
for (i=0; subsystems[i] != NULL; i++) {
|
2009-09-11 09:58:46 +01:00
|
|
|
egg_debug ("registering subsystem : %s", subsystems[i]);
|
2009-07-27 16:40:11 +01:00
|
|
|
devices = g_udev_client_query_by_subsystem (backend->priv->gudev_client, subsystems[i]);
|
|
|
|
|
for (l = devices; l != NULL; l = l->next) {
|
|
|
|
|
native = l->data;
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_device_add (backend, native);
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
|
|
|
|
|
g_list_free (devices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 14:16:11 +01:00
|
|
|
/**
|
|
|
|
|
* up_backend_supports_sleep_state:
|
|
|
|
|
**/
|
|
|
|
|
static gboolean
|
|
|
|
|
up_backend_supports_sleep_state (const gchar *state)
|
|
|
|
|
{
|
|
|
|
|
gchar *contents = NULL;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
const gchar *filename = "/sys/power/state";
|
|
|
|
|
|
|
|
|
|
/* see what kernel can do */
|
|
|
|
|
ret = g_file_get_contents (filename, &contents, NULL, &error);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
egg_warning ("failed to open %s: %s", filename, error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* does the kernel advertise this */
|
|
|
|
|
ret = (g_strstr_len (contents, -1, state) != NULL);
|
|
|
|
|
out:
|
|
|
|
|
g_free (contents);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* up_backend_kernel_can_suspend:
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
up_backend_kernel_can_suspend (UpBackend *backend)
|
|
|
|
|
{
|
|
|
|
|
return up_backend_supports_sleep_state ("mem");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* up_backend_kernel_can_hibernate:
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
up_backend_kernel_can_hibernate (UpBackend *backend)
|
|
|
|
|
{
|
|
|
|
|
return up_backend_supports_sleep_state ("disk");
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 14:18:42 +01:00
|
|
|
/**
|
|
|
|
|
* up_backend_has_encrypted_swap:
|
|
|
|
|
*
|
|
|
|
|
* user@local:~$ cat /proc/swaps
|
|
|
|
|
* Filename Type Size Used Priority
|
|
|
|
|
* /dev/mapper/cryptswap1 partition 4803392 35872 -1
|
|
|
|
|
*
|
|
|
|
|
* user@local:~$ cat /etc/crypttab
|
|
|
|
|
* # <target name> <source device> <key file> <options>
|
|
|
|
|
* cryptswap1 /dev/sda5 /dev/urandom swap,cipher=aes-cbc-essiv:sha256
|
|
|
|
|
*
|
|
|
|
|
* Loop over the swap partitions in /proc/swaps, looking for matches in /etc/crypttab
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
up_backend_has_encrypted_swap (UpBackend *backend)
|
|
|
|
|
{
|
|
|
|
|
gchar *contents_swaps = NULL;
|
|
|
|
|
gchar *contents_crypttab = NULL;
|
|
|
|
|
gchar **lines_swaps = NULL;
|
|
|
|
|
gchar **lines_crypttab = NULL;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
gboolean encrypted_swap = FALSE;
|
|
|
|
|
const gchar *filename_swaps = "/proc/swaps";
|
|
|
|
|
const gchar *filename_crypttab = "/etc/crypttab";
|
|
|
|
|
GPtrArray *devices = NULL;
|
|
|
|
|
gchar *device;
|
|
|
|
|
guint i, j;
|
|
|
|
|
|
|
|
|
|
/* get swaps data */
|
|
|
|
|
ret = g_file_get_contents (filename_swaps, &contents_swaps, NULL, &error);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
egg_warning ("failed to open %s: %s", filename_swaps, error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get crypttab data */
|
|
|
|
|
ret = g_file_get_contents (filename_crypttab, &contents_crypttab, NULL, &error);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
egg_warning ("failed to open %s: %s", filename_crypttab, error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* split both into lines */
|
|
|
|
|
lines_swaps = g_strsplit (contents_swaps, "\n", -1);
|
|
|
|
|
lines_crypttab = g_strsplit (contents_crypttab, "\n", -1);
|
|
|
|
|
|
|
|
|
|
/* get valid swap devices */
|
|
|
|
|
devices = g_ptr_array_new_with_free_func (g_free);
|
|
|
|
|
for (i=0; lines_swaps[i] != NULL; i++) {
|
|
|
|
|
|
|
|
|
|
/* is a device? */
|
|
|
|
|
if (lines_swaps[i][0] != '/')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* only look at first parameter */
|
|
|
|
|
g_strdelimit (lines_swaps[i], "\t ", '\0');
|
|
|
|
|
|
|
|
|
|
/* add base device to list */
|
|
|
|
|
device = g_path_get_basename (lines_swaps[i]);
|
|
|
|
|
egg_debug ("adding swap device: %s", device);
|
|
|
|
|
g_ptr_array_add (devices, device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* no swap devices? */
|
|
|
|
|
if (devices->len == 0) {
|
|
|
|
|
egg_debug ("no swap devices");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* find matches in crypttab */
|
|
|
|
|
for (i=0; lines_crypttab[i] != NULL; i++) {
|
|
|
|
|
|
|
|
|
|
/* ignore invalid lines */
|
|
|
|
|
if (lines_crypttab[i][0] == '#' ||
|
|
|
|
|
lines_crypttab[i][0] == '\n' ||
|
|
|
|
|
lines_crypttab[i][0] == '\t' ||
|
|
|
|
|
lines_crypttab[i][0] == '\0')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* only look at first parameter */
|
|
|
|
|
g_strdelimit (lines_crypttab[i], "\t ", '\0');
|
|
|
|
|
|
|
|
|
|
/* is a swap device? */
|
|
|
|
|
for (j=0; j<devices->len; j++) {
|
|
|
|
|
device = g_ptr_array_index (devices, j);
|
|
|
|
|
if (g_strcmp0 (device, lines_crypttab[i]) == 0) {
|
|
|
|
|
egg_debug ("swap device %s is encrypted (so cannot hibernate)", device);
|
|
|
|
|
encrypted_swap = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
egg_debug ("swap device %s is not encrypted (allows hibernate)", device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (devices != NULL)
|
|
|
|
|
g_ptr_array_unref (devices);
|
|
|
|
|
g_free (contents_swaps);
|
|
|
|
|
g_free (contents_crypttab);
|
|
|
|
|
g_strfreev (lines_swaps);
|
|
|
|
|
g_strfreev (lines_crypttab);
|
|
|
|
|
return encrypted_swap;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 16:40:11 +01:00
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_class_init:
|
|
|
|
|
* @klass: The UpBackendClass
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_class_init (UpBackendClass *klass)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2010-01-18 12:32:06 +00:00
|
|
|
object_class->finalize = up_backend_finalize;
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
signals [SIGNAL_DEVICE_ADDED] =
|
|
|
|
|
g_signal_new ("device-added",
|
|
|
|
|
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
|
2010-01-18 12:32:06 +00:00
|
|
|
G_STRUCT_OFFSET (UpBackendClass, device_added),
|
2010-01-18 13:08:31 +00:00
|
|
|
NULL, NULL, up_marshal_VOID__POINTER_POINTER,
|
2009-09-11 12:05:16 +01:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
|
2009-07-27 16:40:11 +01:00
|
|
|
signals [SIGNAL_DEVICE_REMOVED] =
|
|
|
|
|
g_signal_new ("device-removed",
|
|
|
|
|
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
|
2010-01-18 12:32:06 +00:00
|
|
|
G_STRUCT_OFFSET (UpBackendClass, device_removed),
|
2010-01-18 13:08:31 +00:00
|
|
|
NULL, NULL, up_marshal_VOID__POINTER_POINTER,
|
2009-07-27 16:40:11 +01:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
|
|
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (UpBackendPrivate));
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_init:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_init (UpBackend *backend)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
2010-01-18 12:32:06 +00:00
|
|
|
backend->priv = UP_BACKEND_GET_PRIVATE (backend);
|
2009-07-27 16:40:11 +01:00
|
|
|
backend->priv->daemon = NULL;
|
|
|
|
|
backend->priv->device_list = NULL;
|
2010-01-18 12:49:30 +00:00
|
|
|
backend->priv->managed_devices = up_device_list_new ();
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_finalize:
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2010-01-18 12:32:06 +00:00
|
|
|
up_backend_finalize (GObject *object)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
2010-01-18 12:32:06 +00:00
|
|
|
UpBackend *backend;
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
g_return_if_fail (UP_IS_BACKEND (object));
|
2009-07-27 16:40:11 +01:00
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
backend = UP_BACKEND (object);
|
2009-07-27 16:40:11 +01:00
|
|
|
|
|
|
|
|
if (backend->priv->daemon != NULL)
|
|
|
|
|
g_object_unref (backend->priv->daemon);
|
|
|
|
|
if (backend->priv->device_list != NULL)
|
|
|
|
|
g_object_unref (backend->priv->device_list);
|
|
|
|
|
if (backend->priv->gudev_client != NULL)
|
|
|
|
|
g_object_unref (backend->priv->gudev_client);
|
|
|
|
|
|
|
|
|
|
g_object_unref (backend->priv->managed_devices);
|
|
|
|
|
|
2010-01-18 12:32:06 +00:00
|
|
|
G_OBJECT_CLASS (up_backend_parent_class)->finalize (object);
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-18 12:32:06 +00:00
|
|
|
* up_backend_new:
|
2009-07-27 16:40:11 +01:00
|
|
|
*
|
2010-01-18 12:32:06 +00:00
|
|
|
* Return value: a new %UpBackend object.
|
2009-07-27 16:40:11 +01:00
|
|
|
**/
|
2010-01-18 12:32:06 +00:00
|
|
|
UpBackend *
|
|
|
|
|
up_backend_new (void)
|
2009-07-27 16:40:11 +01:00
|
|
|
{
|
2010-01-18 12:32:06 +00:00
|
|
|
UpBackend *backend;
|
|
|
|
|
backend = g_object_new (UP_TYPE_BACKEND, NULL);
|
|
|
|
|
return UP_BACKEND (backend);
|
2009-07-27 16:40:11 +01:00
|
|
|
}
|
|
|
|
|
|