|
Packit |
3ff832 |
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
|
|
Packit |
3ff832 |
/* vim:set et sts=4: */
|
|
Packit |
3ff832 |
/* bus - The Input Bus
|
|
Packit |
3ff832 |
* Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
|
|
Packit Service |
57bd60 |
* Copyright (C) 2008-2010 Red Hat, Inc.
|
|
Packit |
3ff832 |
*
|
|
Packit |
3ff832 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
3ff832 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
3ff832 |
* License as published by the Free Software Foundation; either
|
|
Packit |
3ff832 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
3ff832 |
*
|
|
Packit |
3ff832 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
3ff832 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
3ff832 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
3ff832 |
* Lesser General Public License for more details.
|
|
Packit |
3ff832 |
*
|
|
Packit |
3ff832 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
3ff832 |
* License along with this library; if not, write to the Free Software
|
|
Packit |
3ff832 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
Packit |
3ff832 |
* USA
|
|
Packit |
3ff832 |
*/
|
|
Packit |
3ff832 |
#include "server.h"
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
#include <gio/gio.h>
|
|
Packit |
3ff832 |
#include <stdlib.h>
|
|
Packit |
3ff832 |
#include <string.h>
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
#include "dbusimpl.h"
|
|
Packit |
3ff832 |
#include "ibusimpl.h"
|
|
Packit |
3ff832 |
#include "global.h"
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
static GDBusServer *server = NULL;
|
|
Packit |
3ff832 |
static GMainLoop *mainloop = NULL;
|
|
Packit |
3ff832 |
static BusDBusImpl *dbus = NULL;
|
|
Packit |
3ff832 |
static BusIBusImpl *ibus = NULL;
|
|
Packit |
3ff832 |
static gchar *address = NULL;
|
|
Packit |
3ff832 |
static gboolean _restart = FALSE;
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
static void
|
|
Packit |
3ff832 |
_restart_server (void)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
gchar *exe;
|
|
Packit |
3ff832 |
gint fd;
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
exe = g_strdup_printf ("/proc/%d/exe", getpid ());
|
|
Packit |
3ff832 |
exe = g_file_read_link (exe, NULL);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
if (exe == NULL)
|
|
Packit |
3ff832 |
exe = BINDIR "/ibus-daemon";
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* close all fds except stdin, stdout, stderr */
|
|
Packit |
3ff832 |
for (fd = 3; fd <= sysconf (_SC_OPEN_MAX); fd ++) {
|
|
Packit |
3ff832 |
close (fd);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
_restart = FALSE;
|
|
Packit |
3ff832 |
execv (exe, g_argv);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* If the server binary is replaced while the server is running,
|
|
Packit |
3ff832 |
* "readlink /proc/[pid]/exe" might return a path with " (deleted)"
|
|
Packit |
3ff832 |
* suffix. */
|
|
Packit |
3ff832 |
const gchar suffix[] = " (deleted)";
|
|
Packit |
3ff832 |
if (g_str_has_suffix (exe, suffix)) {
|
|
Packit |
3ff832 |
exe [strlen (exe) - sizeof (suffix) + 1] = '\0';
|
|
Packit |
3ff832 |
execv (exe, g_argv);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
g_warning ("execv %s failed!", g_argv[0]);
|
|
Packit |
3ff832 |
exit (-1);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/**
|
|
Packit |
3ff832 |
* bus_new_connection_cb:
|
|
Packit Service |
57bd60 |
* @user_data: always NULL.
|
|
Packit Service |
57bd60 |
* @returns: TRUE when the function can handle the connection.
|
|
Packit |
3ff832 |
*
|
|
Packit |
3ff832 |
* Handle incoming connections.
|
|
Packit |
3ff832 |
*/
|
|
Packit |
3ff832 |
static gboolean
|
|
Packit Service |
57bd60 |
bus_new_connection_cb (GDBusServer *server,
|
|
Packit Service |
57bd60 |
GDBusConnection *dbus_connection,
|
|
Packit Service |
57bd60 |
gpointer user_data)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
BusConnection *connection = bus_connection_new (dbus_connection);
|
|
Packit |
3ff832 |
bus_dbus_impl_new_connection (dbus, connection);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
if (g_object_is_floating (connection)) {
|
|
Packit |
3ff832 |
/* bus_dbus_impl_new_connection couldn't handle the connection. just delete the connection and return TRUE
|
|
Packit |
3ff832 |
* (so that other connection handler will not handle the deleted connection.) */
|
|
Packit |
3ff832 |
ibus_object_destroy ((IBusObject *)connection);
|
|
Packit |
3ff832 |
g_object_unref (connection);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
return TRUE;
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
static void
|
|
Packit Service |
57bd60 |
_server_connect_start_portal_cb (GObject *source_object,
|
|
Packit Service |
57bd60 |
GAsyncResult *res,
|
|
Packit Service |
57bd60 |
gpointer user_data)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
GVariant *result;
|
|
Packit |
3ff832 |
GError *error = NULL;
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
|
|
Packit |
3ff832 |
res,
|
|
Packit |
3ff832 |
&error);
|
|
Packit |
3ff832 |
if (result != NULL) {
|
|
Packit |
3ff832 |
g_variant_unref (result);
|
|
Packit |
3ff832 |
} else {
|
|
Packit |
3ff832 |
g_print ("portal is not running: %s\n", error->message);
|
|
Packit |
3ff832 |
g_error_free (error);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
static void
|
|
Packit Service |
57bd60 |
bus_acquired_handler (GDBusConnection *connection,
|
|
Packit Service |
57bd60 |
const gchar *name,
|
|
Packit Service |
57bd60 |
gpointer user_data)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
g_dbus_connection_call (connection,
|
|
Packit |
3ff832 |
IBUS_SERVICE_PORTAL,
|
|
Packit |
3ff832 |
IBUS_PATH_IBUS,
|
|
Packit |
3ff832 |
"org.freedesktop.DBus.Peer",
|
|
Packit |
3ff832 |
"Ping",
|
|
Packit |
3ff832 |
g_variant_new ("()"),
|
|
Packit |
3ff832 |
G_VARIANT_TYPE ("()"),
|
|
Packit |
3ff832 |
G_DBUS_CALL_FLAGS_NONE,
|
|
Packit |
3ff832 |
-1,
|
|
Packit |
3ff832 |
NULL /* cancellable */,
|
|
Packit |
3ff832 |
(GAsyncReadyCallback)
|
|
Packit |
3ff832 |
_server_connect_start_portal_cb,
|
|
Packit |
3ff832 |
NULL);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
void
|
|
Packit |
3ff832 |
bus_server_init (void)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
GError *error = NULL;
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
dbus = bus_dbus_impl_get_default ();
|
|
Packit |
3ff832 |
ibus = bus_ibus_impl_get_default ();
|
|
Packit |
3ff832 |
bus_dbus_impl_register_object (dbus, (IBusService *)ibus);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* init server */
|
|
Packit Service |
57bd60 |
GDBusServerFlags flags = G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
|
|
Packit Service |
57bd60 |
gchar *guid = g_dbus_generate_guid ();
|
|
Packit Service |
57bd60 |
if (!g_str_has_prefix (g_address, "unix:tmpdir=")) {
|
|
Packit Service |
57bd60 |
g_error ("Your socket address does not have the format unix:tmpdir=$DIR; %s",
|
|
Packit Service |
57bd60 |
g_address);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
server = g_dbus_server_new_sync (
|
|
Packit |
3ff832 |
g_address, /* the place where the socket file lives, e.g. /tmp, abstract namespace, etc. */
|
|
Packit |
3ff832 |
flags, guid,
|
|
Packit Service |
57bd60 |
NULL /* observer */,
|
|
Packit |
3ff832 |
NULL /* cancellable */,
|
|
Packit |
3ff832 |
&error);
|
|
Packit |
3ff832 |
if (server == NULL) {
|
|
Packit |
3ff832 |
g_error ("g_dbus_server_new_sync() is failed with address %s "
|
|
Packit |
3ff832 |
"and guid %s: %s",
|
|
Packit |
3ff832 |
g_address, guid, error->message);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
g_free (guid);
|
|
Packit |
3ff832 |
|
|
Packit Service |
57bd60 |
g_signal_connect (server, "new-connection", G_CALLBACK (bus_new_connection_cb), NULL);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
g_dbus_server_start (server);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
address = g_strdup_printf ("%s,guid=%s",
|
|
Packit |
3ff832 |
g_dbus_server_get_client_address (server),
|
|
Packit |
3ff832 |
g_dbus_server_get_guid (server));
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* write address to file */
|
|
Packit |
3ff832 |
ibus_write_address (address);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* own a session bus name so that third parties can easily track our life-cycle */
|
|
Packit |
3ff832 |
g_bus_own_name (G_BUS_TYPE_SESSION, IBUS_SERVICE_IBUS,
|
|
Packit |
3ff832 |
G_BUS_NAME_OWNER_FLAGS_NONE,
|
|
Packit |
3ff832 |
bus_acquired_handler,
|
|
Packit |
3ff832 |
NULL, NULL, NULL, NULL);
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
const gchar *
|
|
Packit |
3ff832 |
bus_server_get_address (void)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
return address;
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
void
|
|
Packit |
3ff832 |
bus_server_run (void)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
g_return_if_fail (server);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* create and run main loop */
|
|
Packit |
3ff832 |
mainloop = g_main_loop_new (NULL, FALSE);
|
|
Packit |
3ff832 |
g_main_loop_run (mainloop);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* bus_server_quit is called. stop server */
|
|
Packit |
3ff832 |
g_dbus_server_stop (server);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
ibus_object_destroy ((IBusObject *)dbus);
|
|
Packit |
3ff832 |
ibus_object_destroy ((IBusObject *)ibus);
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* release resources */
|
|
Packit |
3ff832 |
g_object_unref (server);
|
|
Packit |
3ff832 |
g_main_loop_unref (mainloop);
|
|
Packit |
3ff832 |
mainloop = NULL;
|
|
Packit |
3ff832 |
g_free (address);
|
|
Packit |
3ff832 |
address = NULL;
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* When _ibus_exit() is called, bus_ibus_impl_destroy() needs
|
|
Packit |
3ff832 |
* to be called so that waitpid() prevents the processes from
|
|
Packit |
3ff832 |
* becoming the daemons. So we run execv() after
|
|
Packit |
3ff832 |
* ibus_object_destroy(ibus) is called here. */
|
|
Packit |
3ff832 |
if (_restart) {
|
|
Packit |
3ff832 |
_restart_server ();
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
/* should not reach here */
|
|
Packit |
3ff832 |
g_assert_not_reached ();
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
}
|
|
Packit |
3ff832 |
|
|
Packit |
3ff832 |
void
|
|
Packit |
3ff832 |
bus_server_quit (gboolean restart)
|
|
Packit |
3ff832 |
{
|
|
Packit |
3ff832 |
_restart = restart;
|
|
Packit |
3ff832 |
if (mainloop)
|
|
Packit |
3ff832 |
g_main_loop_quit (mainloop);
|
|
Packit |
3ff832 |
}
|