Ejemplo n.º 1
0
GDBusConnection *
_g_bus_get_priv (GBusType            bus_type,
                 GCancellable       *cancellable,
                 GError            **error)
{
  gchar *address;
  GDBusConnection *ret;

  ret = NULL;

  address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
  if (address == NULL)
    goto out;

  ret = g_dbus_connection_new_for_address_sync (address,
                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
                                                G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
                                                NULL, /* GDBusAuthObserver */
                                                cancellable,
                                                error);
  g_free (address);

 out:
  return ret;
}
Ejemplo n.º 2
0
static gpointer
test_auth_client_thread_func (gpointer user_data)
{
  TestAuthData *data = user_data;
  GDBusConnection *c = NULL;
  GError *error = NULL;
  GDBusAuthObserver *auth_observer = NULL;

  auth_observer = g_dbus_auth_observer_new ();

  g_signal_connect (auth_observer,
                    "allow-mechanism",
                    G_CALLBACK (server_on_allow_mechanism),
                    (gpointer) data->allowed_client_mechanism);

  c = g_dbus_connection_new_for_address_sync (data->address,
                                              G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                              auth_observer,
                                              NULL, /* GCancellable */
                                              &error);
  g_assert_no_error (error);
  g_assert (c != NULL);
  g_clear_object (&c);
  g_clear_object (&auth_observer);
  return NULL;
}
Ejemplo n.º 3
0
GDBusConnection *
_get_root_socket_bus_connection (
        GError **error)
{
    gchar address[128];
    g_snprintf (address, 127, TLM_DBUS_ROOT_SOCKET_ADDRESS);
    return g_dbus_connection_new_for_address_sync (address,
            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, NULL, NULL, error);
}
Ejemplo n.º 4
0
static gboolean
photos_thumbnailer_dbus_register (GApplication *application,
                                  GDBusConnection *connection,
                                  const gchar *object_path,
                                  GError **error)
{
  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
  g_autoptr (GDBusAuthObserver) observer = NULL;
  gboolean ret_val = FALSE;

  g_return_val_if_fail (self->skeleton == NULL, FALSE);

  if (!G_APPLICATION_CLASS (photos_thumbnailer_parent_class)->dbus_register (application,
                                                                             connection,
                                                                             object_path,
                                                                             error))
    goto out;

  observer = g_dbus_auth_observer_new ();
  g_signal_connect_swapped (observer,
                            "authorize-authenticated-peer",
                            G_CALLBACK (photos_thumbnailer_authorize_authenticated_peer),
                            self);

  self->connection = g_dbus_connection_new_for_address_sync (self->address,
                                                             G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                                             observer,
                                                             NULL,
                                                             error);
  if (self->connection == NULL)
    goto out;

  self->skeleton = photos_thumbnailer_dbus_skeleton_new ();
  g_signal_connect_swapped (self->skeleton,
                            "handle-cancel",
                            G_CALLBACK (photos_thumbnailer_handle_cancel),
                            self);
  g_signal_connect_swapped (self->skeleton,
                            "handle-generate-thumbnail",
                            G_CALLBACK (photos_thumbnailer_handle_generate_thumbnail),
                            self);

  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
                                         self->connection,
                                         THUMBNAILER_PATH,
                                         error))
    {
      g_clear_object (&self->skeleton);
      goto out;
    }

  ret_val = TRUE;

 out:
  return ret_val;
}
Ejemplo n.º 5
0
 void connect() {
     GError *error = NULL;
     m_conn = g_dbus_connection_new_for_address_sync(
             SERVER_ADDR,
             G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
             NULL, NULL, &error);
     if (m_conn == NULL) {
         g_error("g_dbus_connection_new_for_address_sync() failed: %s", error->message);
         exit(-1);
     }
 }
Ejemplo n.º 6
0
GDBusConnection* WebKitTestBus::getOrCreateConnection()
{
    if (m_connection)
        return m_connection.get();

    g_assert(!m_address.isNull());
    m_connection = adoptGRef(g_dbus_connection_new_for_address_sync(m_address.data(),
        static_cast<GDBusConnectionFlags>(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
        0, 0, 0));
    return m_connection.get();
}
Ejemplo n.º 7
0
static int wrtc_setup(wrtc_t *wrtc)
{
    static srs_client_ops_t ops = {
        .notify_focus   = focus_cb,
        .notify_command = command_cb
    };

    srs_context_t *srs = wrtc->srs;
    char          *cmds[] = {
        (char *)wrtc->config.play,
        (char *)wrtc->config.stop,
        (char *)wrtc->config.pause,
        (char *)wrtc->config.next,
        (char *)wrtc->config.prev
    };
    int         ncmd   = (int)MRP_ARRAY_SIZE(cmds);
    const char *name   = "wrt-media-client";
    const char *appcls = "player";
    const char *id     = "wrt-media-client";

    if (!strcmp(wrtc->config.bus, "session"))
        wrtc->gdbus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
    else if (!strcmp(wrtc->config.bus, "system"))
        wrtc->gdbus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
    else {
        int flags = G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION;
        wrtc->gdbus = g_dbus_connection_new_for_address_sync(wrtc->config.bus,
                                                             flags,
                                                             NULL, NULL, NULL);
    }

    if (wrtc->gdbus == NULL)
        return FALSE;

    wrtc->gnrq = g_bus_own_name(G_BUS_TYPE_SESSION, "org.tizen.srs", 0,
                                NULL, name_acquired_cb, name_lost_cb,
                                wrtc, NULL);

    wrtc->c = client_create(srs, SRS_CLIENT_TYPE_BUILTIN,
                            name, appcls, cmds, ncmd, id, &ops, wrtc);

    if (wrtc->c == NULL) {
        wrtc_cleanup(wrtc);

        return FALSE;
    }

    client_request_focus(wrtc->c, SRS_VOICE_FOCUS_SHARED);

    return TRUE;
}
Ejemplo n.º 8
0
/* Connect to the transaction bus address and return a proxy for the transaction
 * object.
 */
RPMOSTreeTransaction *
rpmostree_transaction_connect (const char *transaction_address,
                               GCancellable *cancellable,
                               GError      **error)
{
  g_autoptr(GDBusConnection) peer_connection =
    g_dbus_connection_new_for_address_sync (transaction_address,
                                            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                            NULL, cancellable, error);

  if (peer_connection == NULL)
    return NULL;

  return rpmostree_transaction_proxy_new_sync (peer_connection,
                                               G_DBUS_PROXY_FLAGS_NONE,
                                               NULL, "/", cancellable, error);
}
Ejemplo n.º 9
0
GDBusConnection *
_get_bus_connection (
        const gchar *seat_id,
        GError **error)
{
    uid_t ui_user_id = getuid ();

    if (ui_user_id == 0) {
        return _get_root_socket_bus_connection (error);
    }

    /* get dbus connection for specific user only */
    gchar address[128];
    g_snprintf (address, 127, "unix:path=%s/%s-%d", TLM_DBUS_SOCKET_PATH,
            seat_id, ui_user_id);
    return g_dbus_connection_new_for_address_sync (address,
            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, NULL, NULL, error);
}
Ejemplo n.º 10
0
int
main (int argc, char *argv[])
{
	GDBusConnection *connection;
	GError *error = NULL;

	nm_g_type_init ();

	connection = g_dbus_connection_new_for_address_sync ("unix:path=" NMRUNDIR "/private-dhcp",
	                                                     G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
	                                                     NULL, NULL, &error);
	if (!connection) {
		g_dbus_error_strip_remote_error (error);
		g_printerr ("Error: could not connect to NetworkManager D-Bus socket: %s\n",
		            error->message);
		g_error_free (error);
		fatal_error ();
	}

	if (!g_dbus_connection_emit_signal (connection,
	                                    NULL,
	                                    "/",
	                                    NM_DHCP_CLIENT_DBUS_IFACE,
	                                    "Event",
	                                    build_signal_parameters (),
	                                    &error)) {
		g_dbus_error_strip_remote_error (error);
		g_printerr ("Error: Could not send DHCP Event signal: %s\n", error->message);
		g_error_free (error);
		fatal_error ();
	}

	if (!g_dbus_connection_flush_sync (connection, NULL, &error)) {
		g_dbus_error_strip_remote_error (error);
		g_printerr ("Error: Could not flush D-Bus connection: %s\n", error->message);
		g_error_free (error);
		fatal_error ();
	}

	g_object_unref (connection);
	return 0;
}
Ejemplo n.º 11
0
/*
 * Raise G_IO_ERROR_NOT_SUPPORTED if the requested user is impossible.
 */
GDBusConnection *
test_try_connect_gdbus_as_user (const char *address,
                                TestUser user,
                                GError **error)
{
  GDBusConnection *conn;

  if (user != TEST_USER_ME && !become_other_user (user, error))
    return NULL;

  conn = g_dbus_connection_new_for_address_sync (address,
      (G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION |
       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT),
      NULL, NULL, error);

  if (user != TEST_USER_ME)
    back_to_root ();

  return conn;
}
Ejemplo n.º 12
0
gint
gtk_module_init (gint   argc,
                 gchar *argv[])
{
	GDBusConnection *dbus;
	gchar *address;
	const gchar *socket;
	GError *error = NULL;

	ENTRY;

	if (!(socket = g_getenv("GDKEVENT_SOCKET"))) {
		CRITICAL(Gdk, "Failed to load gdkevent socket.");
		RETURN(-1);
	}

	address = g_strdup_printf("unix:path=%s", socket);
	dbus = g_dbus_connection_new_for_address_sync(address,
	                                              G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
	                                              NULL, NULL, &error);
	g_free(address);

	if (!dbus) {
		CRITICAL(Gdk, "Failed to load IPC socket: %s", error->message);
		g_error_free(error);
		RETURN(-1);
	}

	connection = dbus;

	gdk_event_handler_set(gdkevent_dispatcher, dbus,
	                      gdkevent_module_unloaded);

	g_get_current_time(&last_time);
	poll_func = g_main_context_get_poll_func(g_main_context_default());
	g_main_context_set_poll_func(g_main_context_default(), gdkevent_poll);

	RETURN(0);
}
Ejemplo n.º 13
0
int
main (int argc, char *argv[])
{
	GOptionContext *opt_context;
	GError *error;
	GDBusConnectionFlags connection_flags;
	GIOChannel *io_channel;
	GDBusProxyFlags proxy_flags;
	GDBusMessage *message;
	GVariant *value;
	gchar *client_ident;
	gchar *input;
	gchar *response;
	gsize *len;
	gint ret,ret_loc;

	ret = 1;
	g_type_init ();
	error = NULL;
	object_path = NULL;

	opt_context = g_option_context_new ("ksr-chat-client() usage:");
	g_option_context_set_summary (opt_context,
									"To connect to server under tcp:host=0.0.0.0 as \"maryl\" and start chatting, use:\n"
									"  \"ksr-chat-client -n maryl -a tcp:host=0.0.0.0");
	g_option_context_add_main_entries (opt_context, opt_entries, NULL);

	if (!g_option_context_parse (opt_context, &argc, &argv, &error))
	{
		g_printerr ("Error parsing options: %s\n", error->message);
		g_error_free (error);
		goto out;
	}

	if (!input_is_valid())
		goto out;

	connection_flags = G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT;

	connection = g_dbus_connection_new_for_address_sync (opt_address,
														   connection_flags,
														   NULL, /* GDBusAuthObserver */
														   NULL, /* GCancellable */
														   &error);

	if (connection == NULL)
	{
		g_printerr ("Error connecting to the DBus : %s\n", error->message);
		g_error_free(error);
		goto out;
	}

	g_print ("Connected to server!\n");

	disallowed_chars = g_regex_new("\n", 0, 0, NULL);
	object_path = g_strdup_printf ("%s/%s", OBJECT_PATH,opt_name);

	// Now register the nickname
	error = NULL;
	value = g_dbus_connection_call_sync (connection,
											   NULL,
											   TMP_OBJECT_PATH,
											   INTERFACE_PATH,
											   "RegisterMe",
											   g_variant_new ("(s)", opt_name),
											   NULL,
											   G_DBUS_CALL_FLAGS_NONE,
											   -1,
											   NULL,
											   &error);

	if (value == NULL)
	{
		g_printerr ("Could not register!\n");
		goto out;
	}
	else
	{
		g_variant_get (value, "(&s)", &response);
		if (g_strcmp0(response,REGISTRATION_RESPONSE_OK) == 0)
			g_print ("Registered a nickname %s , the chat is ready!\n",opt_name);
		else if (g_strcmp0(response,REGISTRATION_RESPONSE_NOT_OK) == 0)
		{
			g_print ("Could not register your nickname %s, please change it!\n",opt_name);
			goto out;
		}
	}

	g_dbus_connection_signal_subscribe(connection,
										NULL,
										INTERFACE_PATH,
										NULL,
										NULL,
										NULL,
										G_DBUS_SIGNAL_FLAGS_NONE,
										handle_signals,
										NULL,
										NULL);

	loop = g_main_loop_new (NULL, FALSE);

	// FD = 0 = stdin
	io_channel = g_io_channel_unix_new(0);

	if (!g_io_add_watch_full (io_channel, G_PRIORITY_HIGH, G_IO_IN, handle_input, NULL, NULL))
			g_error ("Cannot add watch on GIOChannel!\n");

	g_main_loop_run (loop);

	ret = 0;

	out:
	g_option_context_free (opt_context);
	return ret;
}
Ejemplo n.º 14
0
int
main (int argc, char *argv[])
{
	gs_unref_object GDBusConnection *connection = NULL;
	gs_free_error GError *error = NULL;
	gs_unref_variant GVariant *parameters = NULL;
	gs_unref_variant GVariant *result = NULL;
	gboolean success = FALSE;
	guint try_count = 0;
	gint64 time_end;

	nm_g_type_init ();

	/* FIXME: g_dbus_connection_new_for_address_sync() tries to connect to the socket in
	 * non-blocking mode, which can easily fail with EAGAIN, causing the creation of the
	 * socket to fail with "Could not connect: Resource temporarily unavailable".
	 *
	 * We should instead create the GIOStream ourself and block on connecting to
	 * the socket. */
	connection = g_dbus_connection_new_for_address_sync ("unix:path=" NMRUNDIR "/private-dhcp",
	                                                     G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
	                                                     NULL, NULL, &error);
	if (!connection) {
		g_dbus_error_strip_remote_error (error);
		_LOGE ("could not connect to NetworkManager D-Bus socket: %s",
		       error->message);
		goto out;
	}

	parameters = build_signal_parameters ();

	time_end = g_get_monotonic_time () + (200 * 1000L); /* retry for at most 200 milliseconds */

do_notify:
	try_count++;
	result = g_dbus_connection_call_sync (connection,
	                                      NULL,
	                                      NM_DHCP_HELPER_SERVER_OBJECT_PATH,
	                                      NM_DHCP_HELPER_SERVER_INTERFACE_NAME,
	                                      NM_DHCP_HELPER_SERVER_METHOD_NOTIFY,
	                                      parameters,
	                                      NULL,
	                                      G_DBUS_CALL_FLAGS_NONE,
	                                      1000,
	                                      NULL,
	                                      &error);

	if (!result) {
		gs_free char *s_err = NULL;

		s_err = g_dbus_error_get_remote_error (error);
		if (NM_IN_STRSET (s_err, "org.freedesktop.DBus.Error.UnknownMethod")) {
			gint64 remaining_time = time_end - g_get_monotonic_time ();

			/* I am not sure that a race can actually happen, as we register the object
			 * on the server side during GDBusServer:new-connection signal.
			 *
			 * However, there was also a race for subscribing to an event, so let's just
			 * do some retry. */
			if (remaining_time > 0) {
				_LOGi ("failure to call notify: %s (retry %u)", error->message, try_count);
				g_usleep (NM_MIN (NM_CLAMP ((gint64) (100L * (1L << try_count)), 5000, 25000), remaining_time));
				g_clear_error (&error);
				goto do_notify;
			}
		}
		_LOGW ("failure to call notify: %s (try signal via Event)", error->message);
		g_clear_error (&error);

		/* for backward compatibilty, try to emit the signal. There is no stable
		 * API between the dhcp-helper and NetworkManager. However, while upgrading
		 * the NetworkManager package, a newer helper might want to notify an
		 * older server, which still uses the "Event". */
		if (!g_dbus_connection_emit_signal (connection,
		                                    NULL,
		                                    "/",
		                                    NM_DHCP_CLIENT_DBUS_IFACE,
		                                    "Event",
		                                    parameters,
		                                    &error)) {
			g_dbus_error_strip_remote_error (error);
			_LOGE ("could not send DHCP Event signal: %s", error->message);
			goto out;
		}
		/* We were able to send the asynchronous Event. Consider that a success. */
		success = TRUE;
	} else
		success = TRUE;

	if (!g_dbus_connection_flush_sync (connection, NULL, &error)) {
		g_dbus_error_strip_remote_error (error);
		_LOGE ("could not flush D-Bus connection: %s", error->message);
		success = FALSE;
		goto out;
	}

out:
	if (!success)
		kill_pid ();
	return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
Ejemplo n.º 15
0
static gboolean
spawn_bus (State        *state,
           GCancellable *cancellable)
{
        GDBusConnection     *bus_connection = NULL;
        GPtrArray           *arguments = NULL;
        GSubprocessLauncher *launcher = NULL;
        GSubprocess         *subprocess = NULL;
        GInputStream        *input_stream = NULL;
        GDataInputStream    *data_stream = NULL;
        GError              *error = NULL;
        char                *bus_address_fd_string;
        char                *bus_address = NULL;
        gsize                bus_address_size;

        gboolean  is_running = FALSE;
        int       ret;
        int       pipe_fds[2];

        g_debug ("Running session message bus");

        bus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
                                         cancellable,
                                         NULL);

        if (bus_connection != NULL) {
                g_debug ("session message bus already running, not starting another one");
                state->bus_connection = bus_connection;
                return TRUE;
        }

        ret = g_unix_open_pipe (pipe_fds, FD_CLOEXEC, &error);

        if (!ret) {
                g_debug ("could not open pipe: %s", error->message);
                goto out;
        }

        arguments = g_ptr_array_new ();
        launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);

        g_subprocess_launcher_take_fd (launcher, pipe_fds[1], BUS_ADDRESS_FILENO);

        bus_address_fd_string = g_strdup_printf ("%d", BUS_ADDRESS_FILENO);

        g_ptr_array_add (arguments, "dbus-daemon");

        g_ptr_array_add (arguments, "--print-address");
        g_ptr_array_add (arguments, bus_address_fd_string);
        g_ptr_array_add (arguments, "--session");
        g_ptr_array_add (arguments, NULL);

        subprocess = g_subprocess_launcher_spawnv (launcher,
                                                   (const char * const *) arguments->pdata,
                                                   &error);
        g_free (bus_address_fd_string);
        g_clear_object (&launcher);
        g_ptr_array_free (arguments, TRUE);

        if (subprocess == NULL) {
                g_debug ("could not start dbus-daemon: %s", error->message);
                goto out;
        }

        input_stream = g_unix_input_stream_new (pipe_fds[0], TRUE);
        data_stream = g_data_input_stream_new (input_stream);
        g_clear_object (&input_stream);

        bus_address = g_data_input_stream_read_line (data_stream,
                                                     &bus_address_size,
                                                     cancellable,
                                                     &error);

        if (error != NULL) {
                g_debug ("could not read address from session message bus: %s", error->message);
                goto out;
        }

        if (bus_address == NULL) {
                g_debug ("session message bus did not write address");
                goto out;
        }

        state->bus_address = bus_address;

        state->bus_subprocess = g_object_ref (subprocess);

        g_subprocess_wait_async (state->bus_subprocess,
                                 cancellable,
                                 (GAsyncReadyCallback)
                                 on_bus_finished,
                                 state);


        bus_connection = g_dbus_connection_new_for_address_sync (state->bus_address,
                                                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
                                                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
                                                                 NULL,
                                                                 cancellable,
                                                                 &error);

        if (bus_connection == NULL) {
                g_debug ("could not open connection to session bus: %s",
                         error->message);
                goto out;
        }

        state->bus_connection = bus_connection;

        is_running = TRUE;
out:
        g_clear_object (&data_stream);
        g_clear_object (&subprocess);
        g_clear_object (&launcher);
        g_clear_error (&error);

        return is_running;
}
Ejemplo n.º 16
0
GDBusConnection *
_g_dbus_connection_get_sync (const char *dbus_id,
                             GCancellable *cancellable,
                             GError **error)
{
    GDBusConnection *bus;
    ThreadLocalConnections *local;
    GError *local_error;
    GDBusConnection *connection;
    gchar *address1;
    GVfsDBusDaemon *daemon_proxy;
    gboolean res;

    if (g_cancellable_set_error_if_cancelled (cancellable, error))
        return NULL;

    local = g_private_get (&local_connections);
    if (local == NULL)
    {
        local = g_new0 (ThreadLocalConnections, 1);
        local->connections = g_hash_table_new_full (g_str_hash, g_str_equal,
                             g_free, (GDestroyNotify)g_object_unref);
        g_private_set (&local_connections, local);
    }

    if (dbus_id == NULL)
    {
        /* Session bus */

        if (local->session_bus)
        {
            if (! g_dbus_connection_is_closed (local->session_bus))
                return local->session_bus;

            /* Session bus was disconnected, re-connect */
            g_object_unref (local->session_bus);
            local->session_bus = NULL;
        }
    }
    else
    {
        /* Mount daemon connection */

        connection = g_hash_table_lookup (local->connections, dbus_id);
        if (connection != NULL)
        {
            if (g_dbus_connection_is_closed (connection))
            {
                /* The mount for this connection died, we invalidate
                 * the caches, and then caller needs to retry.
                 */

                invalidate_local_connection (dbus_id, error);
                return NULL;
            }

            return connection;
        }
    }

    if (local->session_bus == NULL)
    {
        bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, error);
        if (bus == NULL)
            return NULL;

        local->session_bus = bus;

        if (dbus_id == NULL)
            return bus; /* We actually wanted the session bus, so done */
    }

    daemon_proxy = gvfs_dbus_daemon_proxy_new_sync (local->session_bus,
                   G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                   dbus_id,
                   G_VFS_DBUS_DAEMON_PATH,
                   cancellable,
                   error);
    if (daemon_proxy == NULL)
        return NULL;

    address1 = NULL;
    res = gvfs_dbus_daemon_call_get_connection_sync (daemon_proxy,
            &address1,
            NULL,
            cancellable,
            error);
    g_object_unref (daemon_proxy);

    if (!res)
    {
        g_free (address1);
        return NULL;
    }


    local_error = NULL;
    connection = g_dbus_connection_new_for_address_sync (address1,
                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                 NULL, /* GDBusAuthObserver */
                 cancellable,
                 &local_error);
    g_free (address1);

    if (!connection)
    {
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                     "Error while getting peer-to-peer dbus connection: %s",
                     local_error->message);
        g_error_free (local_error);
        return NULL;
    }

    vfs_connection_setup (connection, FALSE);

    g_hash_table_insert (local->connections, g_strdup (dbus_id), connection);

    return connection;
}
Ejemplo n.º 17
0
int
main (int argc, char *argv[])
{
  gint ret;
  gboolean opt_server;
  gchar *opt_address;
  GOptionContext *opt_context;
  gboolean opt_allow_anonymous;
  GError *error;
  GOptionEntry opt_entries[] =
    {
      { "server", 's', 0, G_OPTION_ARG_NONE, &opt_server, "Start a server instead of a client", NULL },
      { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_address, "D-Bus address to use", NULL },
      { "allow-anonymous", 'n', 0, G_OPTION_ARG_NONE, &opt_allow_anonymous, "Allow anonymous authentication", NULL },
      { NULL}
    };

  ret = 1;

  g_type_init ();

  opt_address = NULL;
  opt_server = FALSE;
  opt_allow_anonymous = FALSE;

  opt_context = g_option_context_new ("peer-to-peer example");
  error = NULL;
  g_option_context_add_main_entries (opt_context, opt_entries, NULL);
  if (!g_option_context_parse (opt_context, &argc, &argv, &error))
    {
      g_printerr ("Error parsing options: %s\n", error->message);
      g_error_free (error);
      goto out;
    }
  if (opt_address == NULL)
    {
      g_printerr ("Incorrect usage, try --help.\n");
      goto out;
    }
  if (!opt_server && opt_allow_anonymous)
    {
      g_printerr ("The --allow-anonymous option only makes sense when used with --server.\n");
      goto out;
    }

  /* We are lazy here - we don't want to manually provide
   * the introspection data structures - so we just build
   * them from XML.
   */
  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
  g_assert (introspection_data != NULL);

  if (opt_server)
    {
      GDBusServer *server;
      gchar *guid;
      GMainLoop *loop;
      GDBusServerFlags server_flags;

      guid = g_dbus_generate_guid ();

      server_flags = G_DBUS_SERVER_FLAGS_NONE;
      if (opt_allow_anonymous)
        server_flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;

      error = NULL;
      server = g_dbus_server_new_sync (opt_address,
                                       server_flags,
                                       guid,
                                       NULL, /* GDBusAuthObserver */
                                       NULL, /* GCancellable */
                                       &error);
      g_dbus_server_start (server);
      g_free (guid);

      if (server == NULL)
        {
          g_printerr ("Error creating server at address %s: %s\n", opt_address, error->message);
          g_error_free (error);
          goto out;
        }
      g_print ("Server is listening at: %s\n", g_dbus_server_get_client_address (server));
      g_signal_connect (server,
                        "new-connection",
                        G_CALLBACK (on_new_connection),
                        NULL);

      loop = g_main_loop_new (NULL, FALSE);
      g_main_loop_run (loop);

      g_object_unref (server);
      g_main_loop_unref (loop);
    }
  else
    {
      GDBusConnection *connection;
      const gchar *greeting_response;
      GVariant *value;
      gchar *greeting;

      error = NULL;
      connection = g_dbus_connection_new_for_address_sync (opt_address,
                                                           G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                                           NULL, /* GDBusAuthObserver */
                                                           NULL, /* GCancellable */
                                                           &error);
      if (connection == NULL)
        {
          g_printerr ("Error connecting to D-Bus address %s: %s\n", opt_address, error->message);
          g_error_free (error);
          goto out;
        }

      g_print ("Connected.\n"
               "Negotiated capabilities: unix-fd-passing=%d\n",
               g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);

      greeting = g_strdup_printf ("Hey, it's %" G_GUINT64_FORMAT " already!", (guint64) time (NULL));
      value = g_dbus_connection_call_sync (connection,
                                           NULL, /* bus_name */
                                           "/org/gtk/GDBus/TestObject",
                                           "org.gtk.GDBus.TestPeerInterface",
                                           "HelloWorld",
                                           g_variant_new ("(s)", greeting),
                                           G_VARIANT_TYPE ("(s)"),
                                           G_DBUS_CALL_FLAGS_NONE,
                                           -1,
                                           NULL,
                                           &error);
      if (value == NULL)
        {
          g_printerr ("Error invoking HelloWorld(): %s\n", error->message);
          g_error_free (error);
          goto out;
        }
      g_variant_get (value, "(&s)", &greeting_response);
      g_print ("Server said: %s\n", greeting_response);
      g_variant_unref (value);

      g_object_unref (connection);
    }
  g_dbus_node_info_unref (introspection_data);

  ret = 0;

 out:
  return ret;
}
Ejemplo n.º 18
0
gpointer dbusSvc_instanceThread(gpointer data)
{
   (void) data;
   /*
    * This is thread in which a GLIB "main" loop will run. The GLIB callback functionality is handled
    * and registered in this context.
    */

   GError *error=NULL;
   GMainLoop *loop;
   GDBusConnection *connection;
   guint id;

   // Add watches for needed DBUS interfaces here

   // Create a new GLIB context which will attach to this thread
   loop = g_main_loop_new(NULL, FALSE);

   if(dbusRemoteIP == NULL)
   {
      // We are using the local DBUS SYSTEM bus

      g_print("[dbusSvc_instanceThread] Local DBUS connection on SYSTEM bus\n");

      id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                          DBUS_NAME,
                          G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE,
                          onBusAcquired,
                          onNameAcquired,
                          onNameLost,
                          loop,
                          NULL);
   }
   else
   {
      // We are using a remote connection to a target DBUS daemon
      gchar *remoteAddress;

      remoteAddress = g_strdup_printf("tcp:host=%s,port=%s",dbusRemoteIP,dbusRemotePort);

      g_print("[dbusSvc_instanceThread] Remote DBUS connection on: %s\n",remoteAddress);

      connection = g_dbus_connection_new_for_address_sync(remoteAddress,
                                                          G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                                          NULL,
                                                          NULL,
                                                          &error);

      g_free(remoteAddress);

      if (error != NULL)
      {
         /*
          * Critical error remote bus was not obtainable
          */
         g_print("[dbusSvc_instanceThread] Failed to get remote bus. DBUS services disabled\n");
         g_error_free(error);
      }
      else
      {

      onBusAcquired(connection,"Remote DBUS over TCP",NULL);

      id = g_bus_own_name_on_connection (connection,
                                    DBUS_NAME,
                                    G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE,
                                    onNameAcquired,
                                    onNameLost,
                                    loop,
                                    NULL);
      }

   }

   // Fire off the GLIB main loop. This will not return until the process is terminated.
   g_main_loop_run(loop);

   // Remove additional client watches here.


   // Release the DBUS name
   g_bus_unown_name(id);

   // Remove the DBUs watcher as we lost the connection anyways.
   dbusMesaInterface_removeDbusNameWatch();

   // Free the main loop object
   g_main_loop_unref(loop);
}