summaryrefslogtreecommitdiff
path: root/libnm/nm-dbus-helpers.c
blob: 5d6cd9f74df8a47184703f0f2d640d3349d6df67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2013 Red Hat, Inc.
 */

#include <string.h>
#include <config.h>
#include <gio/gio.h>
#include "nm-dbus-helpers-private.h"
#include "nm-dbus-interface.h"

#define NM_DBUS_PRIVATE_CONNECTION_TAG "libnm-private-connection"

GDBusConnection *
_nm_dbus_new_connection (GError **error)
{
	GDBusConnection *connection = NULL;

	/* If running as root try the private bus first */
	if (0 == geteuid ()) {
		connection = g_dbus_connection_new_for_address_sync ("unix:path=" NMRUNDIR "/private",
		                                                     G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
		                                                     NULL, NULL, NULL);
		if (connection) {
			/* Mark this connection as private */
			g_object_set_data (G_OBJECT (connection),
			                   NM_DBUS_PRIVATE_CONNECTION_TAG,
			                   GUINT_TO_POINTER (TRUE));
			return connection;
		}
	}

	return g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
}

gboolean
_nm_dbus_is_connection_private (GDBusConnection *connection)
{
	return !!g_object_get_data (G_OBJECT (connection), NM_DBUS_PRIVATE_CONNECTION_TAG);
}

GDBusProxy *
_nm_dbus_new_proxy_for_connection (GDBusConnection *connection,
                                   const char *path,
                                   const char *interface)
{
	GDBusProxy *proxy;
	const char *name;
	GError *error = NULL;

	if (_nm_dbus_is_connection_private (connection))
		name = NULL;
	else
		name = NM_DBUS_SERVICE;

	proxy = g_dbus_proxy_new_sync (connection,
	                               G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
	                               NULL,
	                               name, path, interface,
	                               NULL,
	                               &error);
	if (error) {
		/* FIXME */
		g_warning ("Could not create proxy for %s on %s: %s",
		           path, interface, error->message);
		g_clear_error (&error);
	}

	return proxy;
}

void
_nm_dbus_register_error_domain (GQuark domain,
                                const char *interface,
                                GType enum_type)
{
	GEnumClass *enum_class;
	GEnumValue *e;
	char *error_name;
	int i;

	enum_class = g_type_class_ref (enum_type);
	for (i = 0; i < enum_class->n_values; i++) {
		e = &enum_class->values[i];
		error_name = g_strdup_printf ("%s.%s", interface, e->value_nick);
		g_dbus_error_register_error (domain, e->value, error_name);
		g_free (error_name);
	}

	g_type_class_unref (enum_class);
}