chiark / gitweb /
cgroup: additional validity checks for cgroup attribute names
[elogind.git] / src / shared / dbus-common.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
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
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
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
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <dbus/dbus.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27
28 #ifndef DBUS_ERROR_UNKNOWN_OBJECT
29 #define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject"
30 #endif
31
32 #ifndef DBUS_ERROR_UNKNOWN_INTERFACE
33 #define DBUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface"
34 #endif
35
36 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
37 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
38 #endif
39
40 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
41 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
42 #endif
43
44 #define BUS_PROPERTIES_INTERFACE                                        \
45         " <interface name=\"org.freedesktop.DBus.Properties\">\n"       \
46         "  <method name=\"Get\">\n"                                     \
47         "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"    \
48         "   <arg name=\"property\" direction=\"in\" type=\"s\"/>\n"     \
49         "   <arg name=\"value\" direction=\"out\" type=\"v\"/>\n"       \
50         "  </method>\n"                                                 \
51         "  <method name=\"GetAll\">\n"                                  \
52         "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"    \
53         "   <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>\n" \
54         "  </method>\n"                                                 \
55         "  <method name=\"Set\">\n"                                     \
56         "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"    \
57         "   <arg name=\"property\" direction=\"in\" type=\"s\"/>\n"     \
58         "   <arg name=\"value\" direction=\"in\" type=\"v\"/>\n"        \
59         "  </method>\n"                                                 \
60         "  <signal name=\"PropertiesChanged\">\n"                       \
61         "   <arg type=\"s\" name=\"interface\"/>\n"                     \
62         "   <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"        \
63         "   <arg type=\"as\" name=\"invalidated_properties\"/>\n"       \
64         "  </signal>\n"                                                 \
65         " </interface>\n"
66
67 #define BUS_INTROSPECTABLE_INTERFACE                                    \
68         " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"   \
69         "  <method name=\"Introspect\">\n"                              \
70         "   <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"        \
71         "  </method>\n"                                                 \
72         " </interface>\n"
73
74 #define BUS_PEER_INTERFACE                                              \
75         "<interface name=\"org.freedesktop.DBus.Peer\">\n"              \
76         " <method name=\"Ping\"/>\n"                                    \
77         " <method name=\"GetMachineId\">\n"                             \
78         "  <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n" \
79         " </method>\n"                                                  \
80         "</interface>\n"
81
82 #define BUS_GENERIC_INTERFACES_LIST             \
83         "org.freedesktop.DBus.Properties\0"     \
84         "org.freedesktop.DBus.Introspectable\0" \
85         "org.freedesktop.DBus.Peer\0"
86
87 int bus_check_peercred(DBusConnection *c);
88
89 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private_bus, DBusError *error);
90
91 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error);
92 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error);
93
94 const char *bus_error_message(const DBusError *error);
95 const char *bus_error_message_or_strerror(const DBusError *error, int err);
96
97 typedef int (*BusPropertyCallback)(DBusMessageIter *iter, const char *property, void *data);
98 typedef int (*BusPropertySetCallback)(DBusMessageIter *iter, const char *property, void *data);
99
100 typedef struct BusProperty {
101         const char *property;            /* name of the property */
102         BusPropertyCallback append;      /* Function that is called to serialize this property */
103         const char *signature;
104         const uint16_t offset;           /* Offset from BusBoundProperties::base address to the property data.
105                                           * uint16_t is sufficient, because we have no structs too big.
106                                           * -Werror=overflow will catch it if this does not hold. */
107         bool indirect;                   /* data is indirect, ie. not base+offset, but *(base+offset) */
108         BusPropertySetCallback set;      /* Optional: Function that is called to set this property */
109 } BusProperty;
110
111 typedef struct BusBoundProperties {
112         const char *interface;           /* interface of the properties */
113         const BusProperty *properties;   /* array of properties, ended by a NULL-filled element */
114         const void *const base;          /* base pointer to which the offset must be added to reach data */
115 } BusBoundProperties;
116
117 dbus_bool_t bus_maybe_send_reply (DBusConnection   *c,
118                                   DBusMessage *message,
119                                   DBusMessage *reply);
120
121 DBusHandlerResult bus_send_error_reply(
122                 DBusConnection *c,
123                 DBusMessage *message,
124                 DBusError *bus_error,
125                 int error);
126
127 DBusHandlerResult bus_default_message_handler(
128                 DBusConnection *c,
129                 DBusMessage *message,
130                 const char *introspection,
131                 const char *interfaces,
132                 const BusBoundProperties *bound_properties);
133
134 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data);
135 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data);
136 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data);
137 int bus_property_append_tristate_false(DBusMessageIter *i, const char *property, void *data);
138 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data);
139 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data);
140 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data);
141 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data);
142 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data);
143 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data);
144
145 #define bus_property_append_int bus_property_append_int32
146 #define bus_property_append_pid bus_property_append_uint32
147 #define bus_property_append_uid bus_property_append_uint32
148 #define bus_property_append_gid bus_property_append_uint32
149 #define bus_property_append_mode bus_property_append_uint32
150 #define bus_property_append_unsigned bus_property_append_uint32
151 #define bus_property_append_usec bus_property_append_uint64
152
153 int bus_property_set_uint64(DBusMessageIter *i, const char *property, void *data);
154 #define bus_property_set_usec bus_property_set_uint64
155
156 #define DEFINE_BUS_PROPERTY_APPEND_ENUM(function,name,type)             \
157         int function(DBusMessageIter *i, const char *property, void *data) { \
158                 const char *value;                                      \
159                 type *field = data;                                     \
160                                                                         \
161                 assert(i);                                              \
162                 assert(property);                                       \
163                                                                         \
164                 value = strempty(name##_to_string(*field));             \
165                                                                         \
166                 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &value)) \
167                         return -ENOMEM;                                 \
168                                                                         \
169                 return 0;                                               \
170         }
171
172 #define DEFINE_BUS_PROPERTY_SET_ENUM(function,name,type)                \
173         int function(DBusMessageIter *i, const char *property, void *data) { \
174                 const char *value;                                      \
175                 type f, *field = data;                                  \
176                                                                         \
177                 assert(i);                                              \
178                 assert(property);                                       \
179                                                                         \
180                 dbus_message_iter_get_basic(i, &value);                 \
181                                                                         \
182                 f = name##_from_string(value);                          \
183                 if (f < 0)                                              \
184                         return f;                                       \
185                                                                         \
186                 *field = f;                                             \
187                 return 0;                                               \
188         }
189
190 const char *bus_errno_to_dbus(int error);
191
192 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties);
193 DBusMessage* bus_properties_changed_one_new(const char *path, const char *interface, const char *property);
194
195 uint32_t bus_flags_to_events(DBusWatch *bus_watch);
196 unsigned bus_events_to_flags(uint32_t events);
197
198 int bus_parse_strv(DBusMessage *m, char ***_l);
199 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l);
200
201 int bus_append_strv_iter(DBusMessageIter *iter, char **l);
202
203 int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next);
204
205 int generic_print_property(const char *name, DBusMessageIter *iter, bool all);
206
207 void bus_async_unregister_and_exit(DBusConnection *bus, const char *name);
208
209 DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata);
210
211 pid_t bus_get_unix_process_id(DBusConnection *connection, const char *name, DBusError *error);
212
213 bool bus_error_is_no_service(const DBusError *error);
214 int bus_method_call_with_reply(DBusConnection *bus,
215                                const char *destination,
216                                const char *path,
217                                const char *interface,
218                                const char *method,
219                                DBusMessage **return_reply,
220                                DBusError *return_error,
221                                int first_arg_type, ...);
222
223 const char *bus_message_get_sender_with_fallback(DBusMessage *m);
224
225 void bus_message_unrefp(DBusMessage **reply);
226
227 #define _cleanup_dbus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
228 #define _cleanup_dbus_error_free_ __attribute__((cleanup(dbus_error_free)))