udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / dbus-util.h
blob9464b25516d96ed94e4eb39fd18b89a72261f4e0
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
4 #include "sd-bus.h"
6 #include "dissect-image.h"
7 #include "unit.h"
9 int bus_property_get_triggered_unit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
11 #define BUS_DEFINE_SET_TRANSIENT(function, bus_type, type, cast_type, fmt) \
12 int bus_set_transient_##function( \
13 Unit *u, \
14 const char *name, \
15 cast_type *p, \
16 sd_bus_message *message, \
17 UnitWriteFlags flags, \
18 sd_bus_error *error) { \
20 type v; \
21 int r; \
23 assert(p); \
25 r = sd_bus_message_read(message, bus_type, &v); \
26 if (r < 0) \
27 return r; \
29 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
30 *p = (cast_type) v; \
31 unit_write_settingf(u, flags, name, \
32 "%s=" fmt, name, v); \
33 } \
35 return 1; \
38 #define BUS_DEFINE_SET_TRANSIENT_IS_VALID(function, bus_type, type, cast_type, fmt, check) \
39 int bus_set_transient_##function( \
40 Unit *u, \
41 const char *name, \
42 cast_type *p, \
43 sd_bus_message *message, \
44 UnitWriteFlags flags, \
45 sd_bus_error *error) { \
47 type v; \
48 int r; \
50 assert(p); \
52 r = sd_bus_message_read(message, bus_type, &v); \
53 if (r < 0) \
54 return r; \
56 if (!check(v)) \
57 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
58 "Invalid %s setting: " fmt, name, v); \
60 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
61 *p = (cast_type) v; \
62 unit_write_settingf(u, flags, name, \
63 "%s=" fmt, name, v); \
64 } \
66 return 1; \
69 #define BUS_DEFINE_SET_TRANSIENT_TO_STRING(function, bus_type, type, cast_type, fmt, to_string) \
70 int bus_set_transient_##function( \
71 Unit *u, \
72 const char *name, \
73 cast_type *p, \
74 sd_bus_message *message, \
75 UnitWriteFlags flags, \
76 sd_bus_error *error) { \
78 const char *s; \
79 type v; \
80 int r; \
82 assert(p); \
84 r = sd_bus_message_read(message, bus_type, &v); \
85 if (r < 0) \
86 return r; \
88 s = to_string(v); \
89 if (!s) \
90 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
91 "Invalid %s setting: " fmt, name, v); \
93 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
94 *p = (cast_type) v; \
95 unit_write_settingf(u, flags, name, \
96 "%s=%s", name, s); \
97 } \
99 return 1; \
102 #define BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(function, bus_type, type, cast_type, fmt, to_string) \
103 int bus_set_transient_##function( \
104 Unit *u, \
105 const char *name, \
106 cast_type *p, \
107 sd_bus_message *message, \
108 UnitWriteFlags flags, \
109 sd_bus_error *error) { \
111 _cleanup_free_ char *s = NULL; \
112 type v; \
113 int r; \
115 assert(p); \
117 r = sd_bus_message_read(message, bus_type, &v); \
118 if (r < 0) \
119 return r; \
121 r = to_string(v, &s); \
122 if (r == -EINVAL) \
123 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
124 "Invalid %s setting: " fmt, name, v); \
125 if (r < 0) \
126 return r; \
128 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
129 *p = (cast_type) v; \
130 unit_write_settingf(u, flags, name, \
131 "%s=%s", \
132 name, strempty(s)); \
135 return 1; \
138 #define BUS_DEFINE_SET_TRANSIENT_PARSE(function, type, parse) \
139 int bus_set_transient_##function( \
140 Unit *u, \
141 const char *name, \
142 type *p, \
143 sd_bus_message *message, \
144 UnitWriteFlags flags, \
145 sd_bus_error *error) { \
147 const char *s; \
148 type v; \
149 int r; \
151 assert(p); \
153 r = sd_bus_message_read(message, "s", &s); \
154 if (r < 0) \
155 return r; \
157 v = parse(s); \
158 if (v < 0) \
159 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
160 "Invalid %s setting: %s", name, s); \
162 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
163 *p = v; \
164 unit_write_settingf(u, flags, name, \
165 "%s=%s", name, s); \
168 return 1; \
171 #define BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(function, type, parse) \
172 int bus_set_transient_##function( \
173 Unit *u, \
174 const char *name, \
175 type *p, \
176 sd_bus_message *message, \
177 UnitWriteFlags flags, \
178 sd_bus_error *error) { \
180 const char *s; \
181 type v; \
182 int r; \
184 assert(p); \
186 r = sd_bus_message_read(message, "s", &s); \
187 if (r < 0) \
188 return r; \
190 r = parse(s, &v); \
191 if (r < 0) \
192 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
193 "Invalid %s setting: %s", name, s); \
195 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
196 *p = v; \
197 unit_write_settingf(u, flags, name, \
198 "%s=%s", name, strempty(s)); \
201 return 1; \
204 #define BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(function, check) \
205 int bus_set_transient_##function( \
206 Unit *u, \
207 const char *name, \
208 char **p, \
209 sd_bus_message *message, \
210 UnitWriteFlags flags, \
211 sd_bus_error *error) { \
213 const char *v; \
214 int r; \
216 assert(p); \
218 r = sd_bus_message_read(message, "s", &v); \
219 if (r < 0) \
220 return r; \
222 if (!isempty(v) && !check(v)) \
223 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
224 "Invalid %s setting: %s", name, v); \
226 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
227 r = free_and_strdup(p, empty_to_null(v)); \
228 if (r < 0) \
229 return r; \
231 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, \
232 "%s=%s", name, strempty(v)); \
235 return 1; \
238 int bus_set_transient_mode_t(Unit *u, const char *name, mode_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
239 int bus_set_transient_unsigned(Unit *u, const char *name, unsigned *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
240 int bus_set_transient_user_relaxed(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
241 int bus_set_transient_path(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
242 int bus_set_transient_string(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
243 int bus_set_transient_bool(Unit *u, const char *name, bool *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
244 int bus_set_transient_tristate(Unit *u, const char *name, int *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
245 int bus_set_transient_usec_internal(Unit *u, const char *name, usec_t *p, bool fix_0, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
246 static inline int bus_set_transient_usec(Unit *u, const char *name, usec_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error) {
247 return bus_set_transient_usec_internal(u, name, p, false, message, flags, error);
249 static inline int bus_set_transient_usec_fix_0(Unit *u, const char *name, usec_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error) {
250 return bus_set_transient_usec_internal(u, name, p, true, message, flags, error);
252 int bus_verify_manage_units_async_full(Unit *u, const char *verb, int capability, const char *polkit_message, bool interactive, sd_bus_message *call, sd_bus_error *error);
254 int bus_read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator);
256 int bus_property_get_activation_details(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);