1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Copyright (C) 2006 - 2013 Red Hat, Inc.
19 * Copyright (C) 2006 - 2008 Novell, Inc.
22 #include "nm-default.h"
26 #include <sys/types.h>
30 #include "nm-dbus-interface.h"
31 #include "nm-bus-manager.h"
32 #include "nm-core-internal.h"
33 #include "nm-dbus-compat.h"
34 #include "nm-exported-object.h"
35 #include "NetworkManagerUtils.h"
37 #define _NMLOG_DOMAIN LOGD_CORE
38 #define _NMLOG_PREFIX_NAME "bus-manager"
39 #define _NMLOG(level, ...) \
41 nm_log ((level), _NMLOG_DOMAIN, \
42 "%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
43 _NMLOG_PREFIX_NAME": " \
44 _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
48 DBUS_CONNECTION_CHANGED = 0,
49 PRIVATE_CONNECTION_NEW,
50 PRIVATE_CONNECTION_DISCONNECTED,
54 static guint signals[NUMBER_OF_SIGNALS];
56 G_DEFINE_TYPE(NMBusManager, nm_bus_manager, G_TYPE_OBJECT)
58 #define NM_BUS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
59 NM_TYPE_BUS_MANAGER, \
62 typedef struct _PrivateServer PrivateServer;
65 GDBusConnection *connection;
66 GDBusObjectManagerServer *obj_manager;
69 GSList *private_servers;
75 } NMBusManagerPrivate;
77 static gboolean nm_bus_manager_init_bus (NMBusManager *self);
78 static void nm_bus_manager_cleanup (NMBusManager *self);
79 static void start_reconnection_timeout (NMBusManager *self);
81 /* The base path for our GDBusObjectManagerServers. They do not contain
82 * "NetworkManager" because GDBusObjectManagerServer requires that all
83 * exported objects be *below* the base path, and eg the Manager object
84 * is the base path already.
86 #define OBJECT_MANAGER_SERVER_BASE_PATH "/org/freedesktop"
88 NM_DEFINE_SINGLETON_REGISTER (NMBusManager);
91 nm_bus_manager_get (void)
93 if (G_UNLIKELY (!singleton_instance)) {
94 nm_bus_manager_setup (g_object_new (NM_TYPE_BUS_MANAGER, NULL));
95 if (!nm_bus_manager_init_bus (singleton_instance))
96 start_reconnection_timeout (singleton_instance);
98 return singleton_instance;
102 nm_bus_manager_setup (NMBusManager *instance)
104 static char already_setup = FALSE;
106 g_assert (NM_IS_BUS_MANAGER (instance));
107 g_assert (!already_setup);
108 g_assert (!singleton_instance);
110 already_setup = TRUE;
111 singleton_instance = instance;
112 nm_singleton_instance_register ();
113 _LOGD ("setup %s singleton (%p)", "NMBusManager", singleton_instance);
116 /**************************************************************/
118 struct _PrivateServer {
124 /* With peer bus connections, we'll get a new connection for each
125 * client. For each connection we create an ObjectManager for
126 * that connection to handle exporting our objects. This table
127 * maps GDBusObjectManager :: 'fake sender'.
129 * Note that even for connections that don't export any objects
130 * we'll still create GDBusObjectManager since that's where we store
131 * the pointer to the GDBusConnection.
133 GHashTable *obj_managers;
135 NMBusManager *manager;
139 GDBusConnection *connection;
140 PrivateServer *server;
141 gboolean remote_peer_vanished;
142 } CloseConnectionInfo;
145 close_connection_in_idle (gpointer user_data)
147 CloseConnectionInfo *info = user_data;
148 PrivateServer *server = info->server;
150 GDBusObjectManagerServer *manager;
152 /* Emit this for the manager */
153 g_signal_emit (server->manager,
154 signals[PRIVATE_CONNECTION_DISCONNECTED],
158 /* FIXME: there's a bug (754730) in GLib for which the connection
159 * is marked as closed when the remote peer vanishes but its
160 * resources are not cleaned up. Work around it by explicitly
161 * closing the connection in that case. */
162 if (info->remote_peer_vanished)
163 g_dbus_connection_close (info->connection, NULL, NULL, NULL);
165 g_hash_table_iter_init (&iter, server->obj_managers);
166 while (g_hash_table_iter_next (&iter, (gpointer) &manager, NULL)) {
167 if (g_dbus_object_manager_server_get_connection (manager) == info->connection) {
168 g_hash_table_iter_remove (&iter);
173 g_object_unref (server->manager);
174 g_slice_free (CloseConnectionInfo, info);
176 return G_SOURCE_REMOVE;
180 private_server_closed_connection (GDBusConnection *conn,
181 gboolean remote_peer_vanished,
185 PrivateServer *s = user_data;
186 CloseConnectionInfo *info;
188 /* Clean up after the connection */
189 _LOGD ("(%s) closed connection %p on private socket", s->tag, conn);
191 info = g_slice_new0 (CloseConnectionInfo);
192 info->connection = conn;
194 info->remote_peer_vanished = remote_peer_vanished;
196 g_object_ref (s->manager);
198 /* Delay the close of connection to ensure that D-Bus signals
200 g_idle_add (close_connection_in_idle, info);
204 private_server_new_connection (GDBusServer *server,
205 GDBusConnection *conn,
208 PrivateServer *s = user_data;
209 static guint32 counter = 0;
210 GDBusObjectManagerServer *manager;
213 g_signal_connect (conn, "closed", G_CALLBACK (private_server_closed_connection), s);
215 /* Fake a sender since private connections don't have one */
216 sender = g_strdup_printf ("x:y:%d", counter++);
218 manager = g_dbus_object_manager_server_new (OBJECT_MANAGER_SERVER_BASE_PATH);
219 g_dbus_object_manager_server_set_connection (manager, conn);
220 g_hash_table_insert (s->obj_managers, manager, sender);
222 _LOGD ("(%s) accepted connection %p on private socket", s->tag, conn);
224 /* Emit this for the manager */
225 g_signal_emit (s->manager,
226 signals[PRIVATE_CONNECTION_NEW],
234 private_server_manager_destroy (GDBusObjectManagerServer *manager)
236 GDBusConnection *connection = g_dbus_object_manager_server_get_connection (manager);
238 if (!g_dbus_connection_is_closed (connection))
239 g_dbus_connection_close (connection, NULL, NULL, NULL);
240 g_dbus_object_manager_server_set_connection (manager, NULL);
241 g_object_unref (manager);
245 private_server_authorize (GDBusAuthObserver *observer,
247 GCredentials *credentials,
250 return g_credentials_get_unix_user (credentials, NULL) == 0;
253 static PrivateServer *
254 private_server_new (const char *path,
256 NMBusManager *manager)
259 GDBusAuthObserver *auth_observer;
261 GError *error = NULL;
262 char *address, *guid;
265 address = g_strdup_printf ("unix:path=%s", path);
267 _LOGD ("(%s) creating private socket %s", tag, address);
269 guid = g_dbus_generate_guid ();
270 auth_observer = g_dbus_auth_observer_new ();
271 g_signal_connect (auth_observer, "authorize-authenticated-peer",
272 G_CALLBACK (private_server_authorize), NULL);
273 server = g_dbus_server_new_sync (address,
274 G_DBUS_SERVER_FLAGS_NONE,
279 g_object_unref (auth_observer);
282 _LOGW ("(%s) failed to set up private socket %s: %s",
283 tag, address, error->message);
284 g_error_free (error);
289 s = g_malloc0 (sizeof (*s));
290 s->address = address;
292 g_signal_connect (server, "new-connection",
293 G_CALLBACK (private_server_new_connection), s);
295 s->obj_managers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
296 (GDestroyNotify) private_server_manager_destroy,
298 s->manager = manager;
299 s->detail = g_quark_from_string (tag);
300 s->tag = g_quark_to_string (s->detail);
302 g_dbus_server_start (server);
308 private_server_free (gpointer ptr)
310 PrivateServer *s = ptr;
314 g_hash_table_destroy (s->obj_managers);
316 g_dbus_server_stop (s->server);
317 g_object_unref (s->server);
319 memset (s, 0, sizeof (*s));
324 nm_bus_manager_private_server_register (NMBusManager *self,
328 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
332 g_return_if_fail (self != NULL);
333 g_return_if_fail (path != NULL);
334 g_return_if_fail (tag != NULL);
336 /* Only one instance per tag; but don't warn */
337 for (iter = priv->private_servers; iter; iter = g_slist_next (iter)) {
339 if (g_strcmp0 (tag, s->tag) == 0)
343 s = private_server_new (path, tag, self);
345 priv->private_servers = g_slist_append (priv->private_servers, s);
349 private_server_get_connection_owner (PrivateServer *s, GDBusConnection *connection)
352 GDBusObjectManagerServer *manager;
355 g_return_val_if_fail (s != NULL, NULL);
356 g_return_val_if_fail (connection != NULL, NULL);
358 g_hash_table_iter_init (&iter, s->obj_managers);
359 while (g_hash_table_iter_next (&iter, (gpointer) &manager, (gpointer) &owner)) {
360 if (g_dbus_object_manager_server_get_connection (manager) == connection)
366 static GDBusConnection *
367 private_server_get_connection_by_owner (PrivateServer *s, const char *owner)
370 GDBusObjectManagerServer *manager;
371 const char *priv_sender;
373 g_hash_table_iter_init (&iter, s->obj_managers);
374 while (g_hash_table_iter_next (&iter, (gpointer) &manager, (gpointer) &priv_sender)) {
375 if (g_strcmp0 (owner, priv_sender) == 0)
376 return g_dbus_object_manager_server_get_connection (manager);
381 /**************************************************************/
384 _bus_get_unix_pid (NMBusManager *self,
389 guint32 unix_pid = G_MAXUINT32;
390 gs_unref_variant GVariant *ret = NULL;
392 ret = _nm_dbus_proxy_call_sync (NM_BUS_MANAGER_GET_PRIVATE (self)->proxy,
393 "GetConnectionUnixProcessID",
394 g_variant_new ("(s)", sender),
395 G_VARIANT_TYPE ("(u)"),
396 G_DBUS_CALL_FLAGS_NONE, 2000,
401 g_variant_get (ret, "(u)", &unix_pid);
403 *out_pid = (gulong) unix_pid;
408 _bus_get_unix_user (NMBusManager *self,
413 guint32 unix_uid = G_MAXUINT32;
414 gs_unref_variant GVariant *ret = NULL;
416 ret = _nm_dbus_proxy_call_sync (NM_BUS_MANAGER_GET_PRIVATE (self)->proxy,
417 "GetConnectionUnixUser",
418 g_variant_new ("(s)", sender),
419 G_VARIANT_TYPE ("(u)"),
420 G_DBUS_CALL_FLAGS_NONE, 2000,
425 g_variant_get (ret, "(u)", &unix_uid);
427 *out_user = (gulong) unix_uid;
432 * _get_caller_info():
434 * Given a GDBus method invocation, or a GDBusConnection + GDBusMessage,
435 * return the sender and the UID of the sender.
438 _get_caller_info (NMBusManager *self,
439 GDBusMethodInvocation *context,
440 GDBusConnection *connection,
441 GDBusMessage *message,
446 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
451 connection = g_dbus_method_invocation_get_connection (context);
453 /* only bus connections will have a sender */
454 sender = g_dbus_method_invocation_get_sender (context);
457 sender = g_dbus_message_get_sender (message);
459 g_assert (connection);
462 /* Might be a private connection, for which we fake a sender */
463 for (iter = priv->private_servers; iter; iter = g_slist_next (iter)) {
464 PrivateServer *s = iter->data;
466 sender = private_server_get_connection_owner (s, connection);
471 *out_sender = g_strdup (sender);
475 creds = g_dbus_connection_get_peer_credentials (connection);
479 pid = g_credentials_get_unix_pid (creds, NULL);
481 *out_pid = G_MAXULONG;
485 *out_pid = G_MAXULONG;
493 /* Bus connections always have a sender */
496 if (!_bus_get_unix_user (self, sender, out_uid, NULL)) {
497 *out_uid = G_MAXULONG;
503 if (!_bus_get_unix_pid (self, sender, out_pid, NULL)) {
504 *out_pid = G_MAXULONG;
510 *out_sender = g_strdup (sender);
516 nm_bus_manager_get_caller_info (NMBusManager *self,
517 GDBusMethodInvocation *context,
522 return _get_caller_info (self, context, NULL, NULL, out_sender, out_uid, out_pid);
526 nm_bus_manager_get_caller_info_from_message (NMBusManager *self,
527 GDBusConnection *connection,
528 GDBusMessage *message,
533 return _get_caller_info (self, NULL, connection, message, out_sender, out_uid, out_pid);
537 nm_bus_manager_get_unix_user (NMBusManager *self,
541 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
543 GError *error = NULL;
545 g_return_val_if_fail (sender != NULL, FALSE);
546 g_return_val_if_fail (out_uid != NULL, FALSE);
548 /* Check if it's a private connection sender, which we fake */
549 for (iter = priv->private_servers; iter; iter = iter->next) {
550 if (private_server_get_connection_by_owner (iter->data, sender)) {
556 /* Otherwise, a bus connection */
557 if (!_bus_get_unix_user (self, sender, out_uid, &error)) {
558 _LOGW ("failed to get unix user for dbus sender '%s': %s",
559 sender, error->message);
560 g_error_free (error);
567 /**************************************************************/
570 nm_bus_manager_init (NMBusManager *self)
572 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
574 priv->obj_manager = g_dbus_object_manager_server_new (OBJECT_MANAGER_SERVER_BASE_PATH);
578 nm_bus_manager_dispose (GObject *object)
580 NMBusManager *self = NM_BUS_MANAGER (object);
581 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
582 GList *exported, *iter;
584 g_slist_free_full (priv->private_servers, private_server_free);
585 priv->private_servers = NULL;
587 nm_bus_manager_cleanup (self);
589 if (priv->obj_manager) {
590 /* The ObjectManager owns the last reference to many exported
591 * objects, and when that reference is dropped the objects unregister
592 * themselves via nm_bus_manager_unregister_object(). By that time
593 * priv->obj_manager is already NULL and that prints warnings. Unregister
594 * them before clearing the ObjectManager instead.
596 exported = g_dbus_object_manager_get_objects ((GDBusObjectManager *) priv->obj_manager);
597 for (iter = exported; iter; iter = iter->next) {
598 nm_bus_manager_unregister_object (self, iter->data);
599 g_object_unref (iter->data);
601 g_list_free (exported);
602 g_clear_object (&priv->obj_manager);
605 nm_clear_g_source (&priv->reconnect_id);
607 G_OBJECT_CLASS (nm_bus_manager_parent_class)->dispose (object);
611 nm_bus_manager_class_init (NMBusManagerClass *klass)
613 GObjectClass *object_class = G_OBJECT_CLASS (klass);
615 g_type_class_add_private (klass, sizeof (NMBusManagerPrivate));
617 object_class->dispose = nm_bus_manager_dispose;
619 signals[DBUS_CONNECTION_CHANGED] =
620 g_signal_new (NM_BUS_MANAGER_DBUS_CONNECTION_CHANGED,
621 G_OBJECT_CLASS_TYPE (object_class),
623 G_STRUCT_OFFSET (NMBusManagerClass, dbus_connection_changed),
625 G_TYPE_NONE, 1, G_TYPE_POINTER);
627 signals[PRIVATE_CONNECTION_NEW] =
628 g_signal_new (NM_BUS_MANAGER_PRIVATE_CONNECTION_NEW,
629 G_OBJECT_CLASS_TYPE (object_class),
630 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
632 G_TYPE_NONE, 2, G_TYPE_DBUS_CONNECTION, G_TYPE_DBUS_OBJECT_MANAGER_SERVER);
634 signals[PRIVATE_CONNECTION_DISCONNECTED] =
635 g_signal_new (NM_BUS_MANAGER_PRIVATE_CONNECTION_DISCONNECTED,
636 G_OBJECT_CLASS_TYPE (object_class),
637 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
638 G_STRUCT_OFFSET (NMBusManagerClass, private_connection_disconnected),
640 G_TYPE_NONE, 1, G_TYPE_POINTER);
644 /* Only cleanup a specific dbus connection, not all our private data */
646 nm_bus_manager_cleanup (NMBusManager *self)
648 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
650 g_clear_object (&priv->proxy);
652 if (priv->connection) {
653 g_signal_handler_disconnect (priv->connection, priv->bus_closed_id);
654 priv->bus_closed_id = 0;
655 g_clear_object (&priv->connection);
658 g_dbus_object_manager_server_set_connection (priv->obj_manager, NULL);
659 priv->started = FALSE;
663 nm_bus_manager_reconnect (gpointer user_data)
665 NMBusManager *self = NM_BUS_MANAGER (user_data);
666 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
668 g_assert (self != NULL);
670 if (nm_bus_manager_init_bus (self)) {
671 if (nm_bus_manager_start_service (self)) {
672 _LOGI ("reconnected to the system bus");
673 g_signal_emit (self, signals[DBUS_CONNECTION_CHANGED],
674 0, priv->connection);
675 priv->reconnect_id = 0;
681 nm_bus_manager_cleanup (self);
686 start_reconnection_timeout (NMBusManager *self)
688 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
690 if (priv->reconnect_id)
691 g_source_remove (priv->reconnect_id);
693 /* Schedule timeout for reconnection attempts */
694 priv->reconnect_id = g_timeout_add_seconds (3, nm_bus_manager_reconnect, self);
698 closed_cb (GDBusConnection *connection,
699 gboolean remote_peer_vanished,
703 NMBusManager *self = NM_BUS_MANAGER (user_data);
705 /* Clean up existing connection */
706 _LOGW ("disconnected by the system bus");
708 nm_bus_manager_cleanup (self);
710 g_signal_emit (G_OBJECT (self), signals[DBUS_CONNECTION_CHANGED], 0, NULL);
712 start_reconnection_timeout (self);
716 nm_bus_manager_init_bus (NMBusManager *self)
718 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
719 GError *error = NULL;
721 if (priv->connection) {
722 _LOGW ("DBus Manager already has a valid connection");
726 priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
727 if (!priv->connection) {
728 /* Log with 'info' severity; there won't be a bus daemon in minimal
729 * environments (eg, initrd) where we only want to use the private
732 _LOGI ("could not connect to the system bus (%s); only the "
733 "private D-Bus socket will be available",
735 g_error_free (error);
739 g_dbus_connection_set_exit_on_close (priv->connection, FALSE);
740 priv->bus_closed_id = g_signal_connect (priv->connection, "closed",
741 G_CALLBACK (closed_cb), self);
743 priv->proxy = g_dbus_proxy_new_sync (priv->connection,
744 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
745 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
752 g_clear_object (&priv->connection);
753 _LOGW ("could not create org.freedesktop.DBus proxy (%s); only the "
754 "private D-Bus socket will be available",
756 g_error_free (error);
760 g_dbus_object_manager_server_set_connection (priv->obj_manager, priv->connection);
764 /* Register our service on the bus; shouldn't be called until
765 * all necessary message handlers have been registered, because
766 * when we register on the bus, clients may start to call.
769 nm_bus_manager_start_service (NMBusManager *self)
771 NMBusManagerPrivate *priv;
772 gs_unref_variant GVariant *ret = NULL;
776 g_return_val_if_fail (NM_IS_BUS_MANAGER (self), FALSE);
778 priv = NM_BUS_MANAGER_GET_PRIVATE (self);
781 _LOGE ("service has already started");
785 /* Pointless to request a name when we aren't connected to the bus */
789 ret = _nm_dbus_proxy_call_sync (priv->proxy,
791 g_variant_new ("(su)",
793 DBUS_NAME_FLAG_DO_NOT_QUEUE),
794 G_VARIANT_TYPE ("(u)"),
795 G_DBUS_CALL_FLAGS_NONE, -1,
798 _LOGE ("could not acquire the NetworkManager service: '%s'", err->message);
803 g_variant_get (ret, "(u)", &result);
805 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
806 _LOGE ("could not acquire the NetworkManager service as it is already taken");
810 priv->started = TRUE;
811 return priv->started;
815 nm_bus_manager_get_connection (NMBusManager *self)
817 g_return_val_if_fail (NM_IS_BUS_MANAGER (self), NULL);
819 return NM_BUS_MANAGER_GET_PRIVATE (self)->connection;
823 nm_bus_manager_register_object (NMBusManager *self,
824 GDBusObjectSkeleton *object)
826 NMBusManagerPrivate *priv;
828 g_return_if_fail (NM_IS_BUS_MANAGER (self));
829 g_return_if_fail (NM_IS_EXPORTED_OBJECT (object));
831 priv = NM_BUS_MANAGER_GET_PRIVATE (self);
833 #if NM_MORE_ASSERTS >= 1
834 #if GLIB_CHECK_VERSION(2,34,0)
835 if (g_dbus_object_manager_server_is_exported (priv->obj_manager, object))
836 g_return_if_reached ();
840 g_dbus_object_manager_server_export (priv->obj_manager, object);
843 GDBusObjectSkeleton *
844 nm_bus_manager_get_registered_object (NMBusManager *self,
847 NMBusManagerPrivate *priv = NM_BUS_MANAGER_GET_PRIVATE (self);
849 return G_DBUS_OBJECT_SKELETON (g_dbus_object_manager_get_object ((GDBusObjectManager *) priv->obj_manager, path));
853 nm_bus_manager_unregister_object (NMBusManager *self,
854 GDBusObjectSkeleton *object)
856 NMBusManagerPrivate *priv;
857 gs_free char *path = NULL;
859 g_return_if_fail (NM_IS_BUS_MANAGER (self));
860 g_return_if_fail (NM_IS_EXPORTED_OBJECT (object));
862 priv = NM_BUS_MANAGER_GET_PRIVATE (self);
864 #if NM_MORE_ASSERTS >= 1
865 #if GLIB_CHECK_VERSION(2,34,0)
866 if (!g_dbus_object_manager_server_is_exported (priv->obj_manager, object))
867 g_return_if_reached ();
871 g_object_get (G_OBJECT (object), "g-object-path", &path, NULL);
872 g_return_if_fail (path != NULL);
874 g_dbus_object_manager_server_unexport (priv->obj_manager, path);
878 nm_bus_manager_connection_get_private_name (NMBusManager *self,
879 GDBusConnection *connection)
881 NMBusManagerPrivate *priv;
885 g_return_val_if_fail (NM_IS_BUS_MANAGER (self), FALSE);
886 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
888 if (g_dbus_connection_get_unique_name (connection)) {
889 /* Shortcut. The connection is not a private connection. */
893 priv = NM_BUS_MANAGER_GET_PRIVATE (self);
894 for (iter = priv->private_servers; iter; iter = g_slist_next (iter)) {
895 PrivateServer *s = iter->data;
897 if ((owner = private_server_get_connection_owner (s, connection)))
900 g_return_val_if_reached (NULL);
904 * nm_bus_manager_new_proxy:
905 * @self: the #NMBusManager
906 * @connection: the GDBusConnection for which this connection should be created
907 * @proxy_type: the type of #GDBusProxy to create
908 * @name: any name on the message bus
909 * @path: name of the object instance to call methods on
910 * @iface: name of the interface to call methods on
912 * Creates a new proxy (of type @proxy_type) for a name on a given bus. Since
913 * the process which called the D-Bus method could be coming from a private
914 * connection or the system bus connection, different proxies must be created
915 * for each case. This function abstracts that.
917 * Returns: a #GDBusProxy capable of calling D-Bus methods of the calling process
920 nm_bus_manager_new_proxy (NMBusManager *self,
921 GDBusConnection *connection,
929 GError *error = NULL;
931 g_return_val_if_fail (g_type_is_a (proxy_type, G_TYPE_DBUS_PROXY), NULL);
932 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
934 /* Might be a private connection, for which @name is fake */
935 owner = nm_bus_manager_connection_get_private_name (self, connection);
937 g_return_val_if_fail (!g_strcmp0 (owner, name), NULL);
941 proxy = g_initable_new (proxy_type, NULL, &error,
942 "g-connection", connection,
943 "g-flags", (G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
944 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS),
946 "g-object-path", path,
947 "g-interface-name", iface,
950 _LOGW ("could not create proxy for %s on connection %s: %s",
951 iface, name, error->message);
952 g_error_free (error);