chiark / gitweb /
sd-bus: rename "connection name" to "description" for the sd-bus API too
[elogind.git] / src / libsystemd / sd-bus / bus-util.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 2013 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 "sd-event.h"
25 #include "sd-bus.h"
26 #include "hashmap.h"
27 #include "time-util.h"
28 #include "util.h"
29
30 typedef enum BusTransport {
31         BUS_TRANSPORT_LOCAL,
32         BUS_TRANSPORT_REMOTE,
33         BUS_TRANSPORT_CONTAINER,
34         _BUS_TRANSPORT_MAX,
35         _BUS_TRANSPORT_INVALID = -1
36 } BusTransport;
37
38 typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata);
39
40 struct bus_properties_map {
41         const char *member;
42         const char *signature;
43         bus_property_set_t set;
44         size_t offset;
45 };
46
47 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata);
48
49 int bus_message_map_all_properties(sd_bus *bus,
50                                    sd_bus_message *m,
51                                    const struct bus_properties_map *map,
52                                    void *userdata);
53 int bus_message_map_properties_changed(sd_bus *bus,
54                                        sd_bus_message *m,
55                                        const struct bus_properties_map *map,
56                                        void *userdata);
57 int bus_map_all_properties(sd_bus *bus,
58                            const char *destination,
59                            const char *path,
60                            const struct bus_properties_map *map,
61                            void *userdata);
62
63 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name);
64
65 typedef bool (*check_idle_t)(void *userdata);
66
67 int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata);
68
69 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error);
70
71 int bus_check_peercred(sd_bus *c);
72
73 int bus_verify_polkit(sd_bus_message *call, int capability, const char *action, bool interactive, bool *_challenge, sd_bus_error *e);
74
75 int bus_verify_polkit_async(sd_bus_message *call, int capability, const char *action, bool interactive, Hashmap **registry, sd_bus_error *error);
76 void bus_verify_polkit_async_registry_free(Hashmap *registry);
77
78 int bus_open_system_systemd(sd_bus **_bus);
79 int bus_open_user_systemd(sd_bus **_bus);
80
81 int bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus);
82 int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
83
84 int bus_print_property(const char *name, sd_bus_message *property, bool all);
85 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all);
86
87 int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
88
89 #define bus_property_get_usec ((sd_bus_property_get_t) NULL)
90 #define bus_property_set_usec ((sd_bus_property_set_t) NULL)
91
92 assert_cc(sizeof(int) == sizeof(int32_t));
93 #define bus_property_get_int ((sd_bus_property_get_t) NULL)
94
95 assert_cc(sizeof(unsigned) == sizeof(unsigned));
96 #define bus_property_get_unsigned ((sd_bus_property_get_t) NULL)
97
98 /* On 64bit machines we can use the default serializer for size_t and
99  * friends, otherwise we need to cast this manually */
100 #if __SIZEOF_SIZE_T__ == 8
101 #define bus_property_get_size ((sd_bus_property_get_t) NULL)
102 #else
103 int bus_property_get_size(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
104 #endif
105
106 #if __SIZEOF_LONG__ == 8
107 #define bus_property_get_long ((sd_bus_property_get_t) NULL)
108 #define bus_property_get_ulong ((sd_bus_property_get_t) NULL)
109 #else
110 int bus_property_get_long(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
111 int bus_property_get_ulong(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
112 #endif
113
114 /* uid_t and friends on Linux 32 bit. This means we can just use the
115  * default serializer for 32bit unsigned, for serializing it, and map
116  * it to NULL here */
117 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
118 #define bus_property_get_uid ((sd_bus_property_get_t) NULL)
119
120 assert_cc(sizeof(gid_t) == sizeof(uint32_t));
121 #define bus_property_get_gid ((sd_bus_property_get_t) NULL)
122
123 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
124 #define bus_property_get_pid ((sd_bus_property_get_t) NULL)
125
126 assert_cc(sizeof(mode_t) == sizeof(uint32_t));
127 #define bus_property_get_mode ((sd_bus_property_get_t) NULL)
128
129 int bus_log_parse_error(int r);
130 int bus_log_create_error(int r);
131
132 typedef struct UnitInfo {
133         const char *machine;
134         const char *id;
135         const char *description;
136         const char *load_state;
137         const char *active_state;
138         const char *sub_state;
139         const char *following;
140         const char *unit_path;
141         uint32_t job_id;
142         const char *job_type;
143         const char *job_path;
144 } UnitInfo;
145
146 int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u);
147
148 static inline void sd_bus_close_unrefp(sd_bus **bus) {
149         if (*bus) {
150                 sd_bus_flush(*bus);
151                 sd_bus_close(*bus);
152                 sd_bus_unref(*bus);
153         }
154 }
155
156 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref);
157 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_slot*, sd_bus_slot_unref);
158 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref);
159 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_creds*, sd_bus_creds_unref);
160 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_track*, sd_bus_track_unref);
161
162 #define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
163 #define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
164 #define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
165 #define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
166 #define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
167 #define _cleanup_bus_track_unref_ _cleanup_(sd_bus_slot_unrefp)
168 #define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free)
169
170 #define BUS_DEFINE_PROPERTY_GET_ENUM(function, name, type)              \
171         int function(sd_bus *bus,                                       \
172                      const char *path,                                  \
173                      const char *interface,                             \
174                      const char *property,                              \
175                      sd_bus_message *reply,                             \
176                      void *userdata,                                    \
177                      sd_bus_error *error) {                             \
178                                                                         \
179                 const char *value;                                      \
180                 type *field = userdata;                                 \
181                 int r;                                                  \
182                                                                         \
183                 assert(bus);                                            \
184                 assert(reply);                                          \
185                 assert(field);                                          \
186                                                                         \
187                 value = strempty(name##_to_string(*field));             \
188                                                                         \
189                 r = sd_bus_message_append_basic(reply, 's', value);     \
190                 if (r < 0)                                              \
191                         return r;                                       \
192                                                                         \
193                 return 1;                                               \
194         }                                                               \
195         struct __useless_struct_to_allow_trailing_semicolon__
196
197 #define BUS_PROPERTY_DUAL_TIMESTAMP(name, offset, flags) \
198         SD_BUS_PROPERTY(name, "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, realtime), (flags)), \
199         SD_BUS_PROPERTY(name "Monotonic", "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, monotonic), (flags))
200
201 int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
202
203 int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignment);