]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/dbus-common.h
move bus_method_call_with_reply() to dbus-common
[thirdparty/systemd.git] / src / shared / dbus-common.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
9a1ac7b9 2
c2f1db8f 3#pragma once
9a1ac7b9
LP
4
5/***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
9a1ac7b9
LP
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 18 Lesser General Public License for more details.
9a1ac7b9 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
9a1ac7b9
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
24#include <dbus/dbus.h>
25
bfebab7f
LP
26#ifndef DBUS_ERROR_UNKNOWN_OBJECT
27#define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject"
28#endif
29
30#ifndef DBUS_ERROR_UNKNOWN_INTERFACE
31#define DBUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface"
32#endif
33
34#ifndef DBUS_ERROR_UNKNOWN_PROPERTY
35#define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
36#endif
37
38#ifndef DBUS_ERROR_PROPERTY_READ_ONLY
39#define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
40#endif
41
42#define BUS_PROPERTIES_INTERFACE \
43 " <interface name=\"org.freedesktop.DBus.Properties\">\n" \
44 " <method name=\"Get\">\n" \
45 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" \
46 " <arg name=\"property\" direction=\"in\" type=\"s\"/>\n" \
47 " <arg name=\"value\" direction=\"out\" type=\"v\"/>\n" \
48 " </method>\n" \
49 " <method name=\"GetAll\">\n" \
50 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" \
51 " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>\n" \
52 " </method>\n" \
53 " <method name=\"Set\">\n" \
54 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" \
55 " <arg name=\"property\" direction=\"in\" type=\"s\"/>\n" \
56 " <arg name=\"value\" direction=\"in\" type=\"v\"/>\n" \
57 " </method>\n" \
58 " <signal name=\"PropertiesChanged\">\n" \
59 " <arg type=\"s\" name=\"interface\"/>\n" \
60 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n" \
61 " <arg type=\"as\" name=\"invalidated_properties\"/>\n" \
62 " </signal>\n" \
63 " </interface>\n"
64
65#define BUS_INTROSPECTABLE_INTERFACE \
66 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n" \
67 " <method name=\"Introspect\">\n" \
68 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n" \
69 " </method>\n" \
70 " </interface>\n"
71
72#define BUS_PEER_INTERFACE \
73 "<interface name=\"org.freedesktop.DBus.Peer\">\n" \
74 " <method name=\"Ping\"/>\n" \
75 " <method name=\"GetMachineId\">\n" \
76 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n" \
77 " </method>\n" \
78 "</interface>\n"
79
80#define BUS_GENERIC_INTERFACES_LIST \
81 "org.freedesktop.DBus.Properties\0" \
82 "org.freedesktop.DBus.Introspectable\0" \
83 "org.freedesktop.DBus.Peer\0"
84
9a1ac7b9
LP
85int bus_check_peercred(DBusConnection *c);
86
f4579ce7 87int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private_bus, DBusError *error);
9a1ac7b9 88
a8f11321
LP
89int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error);
90int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error);
91
4cf5d675 92const char *bus_error_message(const DBusError *error);
eecd1362 93const char *bus_error_message_or_strerror(const DBusError *error, int err);
4cf5d675 94
bfebab7f 95typedef int (*BusPropertyCallback)(DBusMessageIter *iter, const char *property, void *data);
9612f07c 96typedef int (*BusPropertySetCallback)(DBusMessageIter *iter, const char *property, void *data);
bfebab7f
LP
97
98typedef struct BusProperty {
bfebab7f
LP
99 const char *property; /* name of the property */
100 BusPropertyCallback append; /* Function that is called to serialize this property */
101 const char *signature;
d200735e
MS
102 const uint16_t offset; /* Offset from BusBoundProperties::base address to the property data.
103 * uint16_t is sufficient, because we have no structs too big.
104 * -Werror=overflow will catch it if this does not hold. */
105 bool indirect; /* data is indirect, ie. not base+offset, but *(base+offset) */
bfebab7f
LP
106 BusPropertySetCallback set; /* Optional: Function that is called to set this property */
107} BusProperty;
108
d200735e
MS
109typedef struct BusBoundProperties {
110 const char *interface; /* interface of the properties */
111 const BusProperty *properties; /* array of properties, ended by a NULL-filled element */
112 const void *const base; /* base pointer to which the offset must be added to reach data */
113} BusBoundProperties;
114
bfebab7f
LP
115DBusHandlerResult bus_send_error_reply(
116 DBusConnection *c,
117 DBusMessage *message,
118 DBusError *bus_error,
119 int error);
120
121DBusHandlerResult bus_default_message_handler(
122 DBusConnection *c,
123 DBusMessage *message,
124 const char *introspection,
125 const char *interfaces,
d200735e 126 const BusBoundProperties *bound_properties);
bfebab7f
LP
127
128int bus_property_append_string(DBusMessageIter *i, const char *property, void *data);
129int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data);
130int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data);
aa001cd6 131int bus_property_append_tristate_false(DBusMessageIter *i, const char *property, void *data);
bfebab7f
LP
132int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data);
133int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data);
134int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data);
135int bus_property_append_size(DBusMessageIter *i, const char *property, void *data);
136int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data);
916abb21 137int bus_property_append_long(DBusMessageIter *i, const char *property, void *data);
bfebab7f
LP
138
139#define bus_property_append_int bus_property_append_int32
140#define bus_property_append_pid bus_property_append_uint32
bd253d1b
LP
141#define bus_property_append_uid bus_property_append_uint32
142#define bus_property_append_gid bus_property_append_uint32
bfebab7f
LP
143#define bus_property_append_mode bus_property_append_uint32
144#define bus_property_append_unsigned bus_property_append_uint32
145#define bus_property_append_usec bus_property_append_uint64
146
c757a65b
LP
147int bus_property_set_uint64(DBusMessageIter *i, const char *property, void *data);
148#define bus_property_set_usec bus_property_set_uint64
149
bfebab7f
LP
150#define DEFINE_BUS_PROPERTY_APPEND_ENUM(function,name,type) \
151 int function(DBusMessageIter *i, const char *property, void *data) { \
152 const char *value; \
153 type *field = data; \
154 \
155 assert(i); \
156 assert(property); \
157 \
6e476bc9 158 value = strempty(name##_to_string(*field)); \
bfebab7f
LP
159 \
160 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &value)) \
161 return -ENOMEM; \
162 \
163 return 0; \
164 }
165
c57c09ee
MO
166#define DEFINE_BUS_PROPERTY_SET_ENUM(function,name,type) \
167 int function(DBusMessageIter *i, const char *property, void *data) { \
168 const char *value; \
6e476bc9 169 type f, *field = data; \
c57c09ee
MO
170 \
171 assert(i); \
172 assert(property); \
173 \
174 dbus_message_iter_get_basic(i, &value); \
175 \
6e476bc9
LP
176 f = name##_from_string(value); \
177 if (f < 0) \
178 return f; \
c57c09ee 179 \
6e476bc9 180 *field = f; \
c57c09ee
MO
181 return 0; \
182 }
183
bfebab7f
LP
184const char *bus_errno_to_dbus(int error);
185
186DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties);
d4e7373b 187DBusMessage* bus_properties_changed_one_new(const char *path, const char *interface, const char *property);
bfebab7f 188
3df5bf61
LP
189uint32_t bus_flags_to_events(DBusWatch *bus_watch);
190unsigned bus_events_to_flags(uint32_t events);
191
8d0e38a2
LP
192int bus_parse_strv(DBusMessage *m, char ***_l);
193int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l);
194
98a28fef
LP
195int bus_append_strv_iter(DBusMessageIter *iter, char **l);
196
abca4822
LP
197int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next);
198
a4c279f8
LP
199int generic_print_property(const char *name, DBusMessageIter *iter, bool all);
200
ad740100
LP
201void bus_async_unregister_and_exit(DBusConnection *bus, const char *name);
202
203DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata);
204
f8e2fb7b 205pid_t bus_get_unix_process_id(DBusConnection *connection, const char *name, DBusError *error);
c67de56f
SP
206
207bool bus_error_is_no_service(const DBusError *error);
208int bus_method_call_with_reply(DBusConnection *bus,
209 const char *destination,
210 const char *path,
211 const char *interface,
212 const char *method,
213 DBusMessage **return_reply,
214 DBusError *return_error,
215 int first_arg_type, ...);