daemon: port main() to GDBus

We also change the objects to export not to fetch their own bus object.
This commit is contained in:
Cosimo Cecchi 2015-05-30 14:15:04 -07:00 committed by Richard Hughes
parent 4e2f06d9eb
commit e035f23ae0
7 changed files with 133 additions and 172 deletions

View file

@ -41,7 +41,6 @@
struct UpDaemonPrivate
{
GDBusConnection *connection;
UpExportedDaemon *skeleton;
UpConfig *config;
UpBackend *backend;
@ -449,18 +448,12 @@ up_daemon_get_critical_action (UpExportedDaemon *skeleton,
* up_daemon_register_power_daemon:
**/
static gboolean
up_daemon_register_power_daemon (UpDaemon *daemon)
up_daemon_register_power_daemon (UpDaemon *daemon,
GDBusConnection *connection)
{
GError *error = NULL;
UpDaemonPrivate *priv = daemon->priv;
priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (error != NULL) {
g_critical ("error getting system bus: %s", error->message);
g_error_free (error);
return FALSE;
}
/* export our interface on the bus */
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->skeleton),
connection,
@ -483,7 +476,8 @@ up_daemon_register_power_daemon (UpDaemon *daemon)
* up_daemon_startup:
**/
gboolean
up_daemon_startup (UpDaemon *daemon)
up_daemon_startup (UpDaemon *daemon,
GDBusConnection *connection)
{
gboolean ret;
gboolean on_battery;
@ -1073,8 +1067,6 @@ up_daemon_finalize (GObject *object)
g_clear_pointer (&priv->poll_timeouts, g_hash_table_destroy);
g_clear_pointer (&priv->idle_signals, g_hash_table_destroy);
g_clear_object (&priv->connection);
g_object_unref (priv->skeleton);
g_object_unref (priv->power_devices);
g_object_unref (priv->display_device);

View file

@ -21,7 +21,7 @@
#ifndef __UP_DAEMON_H__
#define __UP_DAEMON_H__
#include <glib-object.h>
#include <gio/gio.h>
#include "up-types.h"
#include "up-device-list.h"
@ -67,7 +67,8 @@ void up_daemon_test (gpointer user_data);
guint up_daemon_get_number_devices_of_type (UpDaemon *daemon,
UpDeviceKind type);
UpDeviceList *up_daemon_get_device_list (UpDaemon *daemon);
gboolean up_daemon_startup (UpDaemon *daemon);
gboolean up_daemon_startup (UpDaemon *daemon,
GDBusConnection *connection);
void up_daemon_shutdown (UpDaemon *daemon);
GDBusConnection *up_daemon_get_dbus_connection (UpDaemon *daemon);
void up_daemon_set_lid_is_closed (UpDaemon *daemon,

View file

@ -47,7 +47,6 @@ struct UpKbdBacklightPrivate
gint fd;
gint brightness;
gint max_brightness;
GDBusConnection *connection;
UpExportedKbdBacklight *skeleton;
};
@ -258,24 +257,8 @@ out:
static void
up_kbd_backlight_init (UpKbdBacklight *kbd_backlight)
{
GError *error = NULL;
kbd_backlight->priv = UP_KBD_BACKLIGHT_GET_PRIVATE (kbd_backlight);
/* find a kbd backlight in sysfs */
if (!up_kbd_backlight_find (kbd_backlight)) {
g_debug ("cannot find a keyboard backlight");
return;
}
kbd_backlight->priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (error != NULL) {
g_warning ("Cannot connect to bus: %s", error->message);
g_error_free (error);
return;
}
/* register on the bus */
kbd_backlight->priv->skeleton = up_exported_kbd_backlight_skeleton_new ();
g_signal_connect (kbd_backlight->priv->skeleton, "handle-get-brightness",
@ -284,17 +267,6 @@ up_kbd_backlight_init (UpKbdBacklight *kbd_backlight)
G_CALLBACK (up_kbd_backlight_get_max_brightness), kbd_backlight);
g_signal_connect (kbd_backlight->priv->skeleton, "handle-set-brightness",
G_CALLBACK (up_kbd_backlight_set_brightness), kbd_backlight);
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
kbd_backlight->priv->connection,
"/org/freedesktop/UPower/KbdBacklight",
&error);
if (error != NULL) {
g_warning ("Cannot export KbdBacklight object to bus: %s", error->message);
g_error_free (error);
return;
}
}
/**
@ -315,9 +287,7 @@ up_kbd_backlight_finalize (GObject *object)
if (kbd_backlight->priv->fd >= 0)
close (kbd_backlight->priv->fd);
g_clear_object (&kbd_backlight->priv->skeleton);
g_clear_object (&kbd_backlight->priv->connection);
g_object_unref (kbd_backlight->priv->skeleton);
G_OBJECT_CLASS (up_kbd_backlight_parent_class)->finalize (object);
}
@ -331,3 +301,25 @@ up_kbd_backlight_new (void)
return g_object_new (UP_TYPE_KBD_BACKLIGHT, NULL);
}
void
up_kbd_backlight_register (UpKbdBacklight *kbd_backlight,
GDBusConnection *connection)
{
GError *error = NULL;
/* find a kbd backlight in sysfs */
if (!up_kbd_backlight_find (kbd_backlight)) {
g_debug ("cannot find a keyboard backlight");
return;
}
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (kbd_backlight->priv->skeleton),
connection,
"/org/freedesktop/UPower/KbdBacklight",
&error);
if (error != NULL) {
g_warning ("Cannot export KbdBacklight object to bus: %s", error->message);
g_error_free (error);
}
}

View file

@ -23,7 +23,7 @@
#ifndef __UP_KBD_BACKLIGHT_H
#define __UP_KBD_BACKLIGHT_H
#include <glib-object.h>
#include <gio/gio.h>
G_BEGIN_DECLS
@ -49,6 +49,8 @@ typedef struct
UpKbdBacklight *up_kbd_backlight_new (void);
GType up_kbd_backlight_get_type (void);
void up_kbd_backlight_register (UpKbdBacklight *kbd_backlight,
GDBusConnection *connection);
G_END_DECLS

View file

@ -29,64 +29,81 @@
#include <sys/types.h>
#include <unistd.h>
#include <gio/gio.h>
#include <glib.h>
#include <glib-unix.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <locale.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "up-daemon.h"
#include "up-kbd-backlight.h"
#include "up-wakeups.h"
#define DEVKIT_POWER_SERVICE_NAME "org.freedesktop.UPower"
static GMainLoop *loop = NULL;
typedef struct UpState {
UpKbdBacklight *kbd_backlight;
UpWakeups *wakeups;
UpDaemon *daemon;
GMainLoop *loop;
} UpState;
static void
up_state_free (UpState *state)
{
up_daemon_shutdown (state->daemon);
g_clear_object (&state->kbd_backlight);
g_clear_object (&state->wakeups);
g_clear_object (&state->daemon);
g_clear_pointer (&state->loop, g_main_loop_unref);
g_free (state);
}
static UpState *
up_state_new (void)
{
UpState *state = g_new0 (UpState, 1);
state->kbd_backlight = up_kbd_backlight_new ();
state->wakeups = up_wakeups_new ();
state->daemon = up_daemon_new ();
state->loop = g_main_loop_new (NULL, FALSE);
return state;
}
/**
* up_main_acquire_name_on_proxy:
* up_main_bus_acquired:
**/
static gboolean
up_main_acquire_name_on_proxy (DBusGProxy *bus_proxy, const gchar *name)
static void
up_main_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
GError *error = NULL;
guint result;
gboolean ret = FALSE;
UpState *state = user_data;
if (bus_proxy == NULL)
goto out;
ret = dbus_g_proxy_call (bus_proxy, "RequestName", &error,
G_TYPE_STRING, name,
G_TYPE_UINT, 0,
G_TYPE_INVALID,
G_TYPE_UINT, &result,
G_TYPE_INVALID);
if (!ret) {
if (error != NULL) {
g_warning ("Failed to acquire %s: %s", name, error->message);
g_error_free (error);
} else {
g_warning ("Failed to acquire %s", name);
}
goto out;
up_kbd_backlight_register (state->kbd_backlight, connection);
up_wakeups_register (state->wakeups, connection);
if (!up_daemon_startup (state->daemon, connection)) {
g_warning ("Could not startup; bailing out");
g_main_loop_quit (state->loop);
}
}
/* already taken */
if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
if (error != NULL) {
g_warning ("Failed to acquire %s: %s", name, error->message);
g_error_free (error);
} else {
g_warning ("Failed to acquire %s", name);
}
ret = FALSE;
goto out;
}
out:
return ret;
/**
* up_main_name_lost:
**/
static void
up_main_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
UpState *state = user_data;
g_debug ("name lost, exiting");
g_main_loop_quit (state->loop);
}
/**
@ -95,8 +112,9 @@ out:
static gboolean
up_main_sigint_cb (gpointer user_data)
{
UpState *state = user_data;
g_debug ("Handling SIGINT");
g_main_loop_quit (loop);
g_main_loop_quit (state->loop);
return FALSE;
}
@ -106,9 +124,9 @@ up_main_sigint_cb (gpointer user_data)
* Exits the main loop, which is helpful for valgrinding.
**/
static gboolean
up_main_timed_exit_cb (GMainLoop *loop)
up_main_timed_exit_cb (UpState *state)
{
g_main_loop_quit (loop);
g_main_loop_quit (state->loop);
return FALSE;
}
@ -154,18 +172,12 @@ gint
main (gint argc, gchar **argv)
{
GError *error = NULL;
UpDaemon *daemon = NULL;
UpKbdBacklight *kbd_backlight = NULL;
UpWakeups *wakeups = NULL;
GOptionContext *context;
DBusGProxy *bus_proxy = NULL;
DBusGConnection *bus;
gboolean ret;
gint retval = 1;
gboolean timed_exit = FALSE;
gboolean immediate_exit = FALSE;
guint timer_id = 0;
gboolean verbose = FALSE;
UpState *state;
const GOptionEntry options[] = {
{ "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
@ -222,75 +234,42 @@ main (gint argc, gchar **argv)
NULL);
}
/* get bus connection */
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (bus == NULL) {
g_warning ("Couldn't connect to system bus: %s", error->message);
g_error_free (error);
goto out;
}
/* get proxy */
bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
if (bus_proxy == NULL) {
g_warning ("Could not construct bus_proxy object; bailing out");
goto out;
}
/* aquire name */
ret = up_main_acquire_name_on_proxy (bus_proxy, DEVKIT_POWER_SERVICE_NAME);
if (!ret) {
g_warning ("Could not acquire name; bailing out");
goto out;
}
/* initialize state */
state = up_state_new ();
/* do stuff on ctrl-c */
g_unix_signal_add_full (G_PRIORITY_DEFAULT,
SIGINT,
up_main_sigint_cb,
loop,
state,
NULL);
/* acquire name */
g_bus_own_name (G_BUS_TYPE_SYSTEM,
DEVKIT_POWER_SERVICE_NAME,
G_BUS_NAME_OWNER_FLAGS_NONE,
up_main_bus_acquired,
NULL,
up_main_name_lost,
state, NULL);
g_debug ("Starting upowerd version %s", PACKAGE_VERSION);
kbd_backlight = up_kbd_backlight_new ();
wakeups = up_wakeups_new ();
daemon = up_daemon_new ();
loop = g_main_loop_new (NULL, FALSE);
ret = up_daemon_startup (daemon);
if (!ret) {
g_warning ("Could not startup; bailing out");
goto out;
}
/* only timeout and close the mainloop if we have specified it on the command line */
if (timed_exit) {
timer_id = g_timeout_add_seconds (30, (GSourceFunc) up_main_timed_exit_cb, loop);
timer_id = g_timeout_add_seconds (30, (GSourceFunc) up_main_timed_exit_cb, state);
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");
}
/* immediatly exit */
if (immediate_exit) {
g_timeout_add (50, (GSourceFunc) up_main_timed_exit_cb, loop);
g_timeout_add (50, (GSourceFunc) up_main_timed_exit_cb, state);
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");
}
/* wait for input or timeout */
g_main_loop_run (loop);
up_daemon_shutdown (daemon);
retval = 0;
out:
if (kbd_backlight != NULL)
g_object_unref (kbd_backlight);
if (wakeups != NULL)
g_object_unref (wakeups);
if (daemon != NULL)
g_object_unref (daemon);
if (loop != NULL)
g_main_loop_unref (loop);
if (bus_proxy != NULL)
g_object_unref (bus_proxy);
return retval;
}
g_main_loop_run (state->loop);
up_state_free (state);
return 0;
}

View file

@ -48,7 +48,6 @@ static gboolean up_wakeups_timerstats_enable (UpWakeups *wakeups);
struct UpWakeupsPrivate
{
GPtrArray *data;
GDBusConnection *connection;
UpExportedWakeups *skeleton;
guint total_old;
guint total_ave;
@ -668,18 +667,9 @@ up_wakeups_class_init (UpWakeupsClass *klass)
static void
up_wakeups_init (UpWakeups *wakeups)
{
GError *error = NULL;
wakeups->priv = UP_WAKEUPS_GET_PRIVATE (wakeups);
wakeups->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
wakeups->priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (error != NULL) {
g_warning ("Cannot connect to bus: %s", error->message);
g_error_free (error);
return;
}
wakeups->priv->skeleton = up_exported_wakeups_skeleton_new ();
/* test if we have an interface */
@ -692,17 +682,6 @@ up_wakeups_init (UpWakeups *wakeups)
G_CALLBACK (up_wakeups_get_data), wakeups);
g_signal_connect (wakeups->priv->skeleton, "handle-get-total",
G_CALLBACK (up_wakeups_get_total), wakeups);
/* register on the bus */
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (wakeups->priv->skeleton),
wakeups->priv->connection,
"/org/freedesktop/UPower/Wakeups",
&error);
if (error != NULL) {
g_critical ("Cannot register wakeups on system bus: %s", error->message);
g_error_free (error);
}
}
/**
@ -725,8 +704,6 @@ up_wakeups_finalize (GObject *object)
g_ptr_array_unref (wakeups->priv->data);
g_object_unref (wakeups->priv->skeleton);
g_clear_object (&wakeups->priv->connection);
G_OBJECT_CLASS (up_wakeups_parent_class)->finalize (object);
}
@ -739,3 +716,19 @@ up_wakeups_new (void)
return g_object_new (UP_TYPE_WAKEUPS, NULL);
}
void
up_wakeups_register (UpWakeups *wakeups,
GDBusConnection *connection)
{
GError *error = NULL;
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (wakeups->priv->skeleton),
connection,
"/org/freedesktop/UPower/Wakeups",
&error);
if (error != NULL) {
g_critical ("Cannot register wakeups on system bus: %s", error->message);
g_error_free (error);
}
}

View file

@ -22,7 +22,7 @@
#ifndef __UP_WAKEUPS_H
#define __UP_WAKEUPS_H
#include <glib-object.h>
#include <gio/gio.h>
G_BEGIN_DECLS
@ -47,7 +47,9 @@ typedef struct
} UpWakeupsClass;
UpWakeups *up_wakeups_new (void);
GType up_wakeups_get_type (void);
GType up_wakeups_get_type (void);
void up_wakeups_register (UpWakeups *wakeups,
GDBusConnection *connection);
G_END_DECLS