]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-util.h
Merge pull request #8377 from sourcejedi/logind_restart_is_sorely_lacking_in_testing3
[thirdparty/systemd.git] / src / core / dbus-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include "sd-bus.h"
24 #include "unit.h"
25
26 #define BUS_DEFINE_SET_TRANSIENT(function, bus_type, type, cast_type, fmt) \
27 int bus_set_transient_##function( \
28 Unit *u, \
29 const char *name, \
30 cast_type *p, \
31 sd_bus_message *message, \
32 UnitWriteFlags flags, \
33 sd_bus_error *error) { \
34 \
35 type v; \
36 int r; \
37 \
38 assert(p); \
39 \
40 r = sd_bus_message_read(message, bus_type, &v); \
41 if (r < 0) \
42 return r; \
43 \
44 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
45 *p = (cast_type) v; \
46 unit_write_settingf(u, flags, name, \
47 "%s=" fmt, name, v); \
48 } \
49 \
50 return 1; \
51 }
52
53 #define BUS_DEFINE_SET_TRANSIENT_IS_VALID(function, bus_type, type, cast_type, fmt, check) \
54 int bus_set_transient_##function( \
55 Unit *u, \
56 const char *name, \
57 cast_type *p, \
58 sd_bus_message *message, \
59 UnitWriteFlags flags, \
60 sd_bus_error *error) { \
61 \
62 type v; \
63 int r; \
64 \
65 assert(p); \
66 \
67 r = sd_bus_message_read(message, bus_type, &v); \
68 if (r < 0) \
69 return r; \
70 \
71 if (!check(v)) \
72 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
73 "Invalid %s setting: " fmt, name, v); \
74 \
75 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
76 *p = (cast_type) v; \
77 unit_write_settingf(u, flags, name, \
78 "%s=" fmt, name, v); \
79 } \
80 \
81 return 1; \
82 }
83
84 #define BUS_DEFINE_SET_TRANSIENT_TO_STRING(function, bus_type, type, cast_type, fmt, to_string) \
85 int bus_set_transient_##function( \
86 Unit *u, \
87 const char *name, \
88 cast_type *p, \
89 sd_bus_message *message, \
90 UnitWriteFlags flags, \
91 sd_bus_error *error) { \
92 \
93 const char *s; \
94 type v; \
95 int r; \
96 \
97 assert(p); \
98 \
99 r = sd_bus_message_read(message, bus_type, &v); \
100 if (r < 0) \
101 return r; \
102 \
103 s = to_string(v); \
104 if (!s) \
105 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
106 "Invalid %s setting: " fmt, name, v); \
107 \
108 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
109 *p = (cast_type) v; \
110 unit_write_settingf(u, flags, name, \
111 "%s=%s", name, s); \
112 } \
113 \
114 return 1; \
115 }
116
117 #define BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(function, bus_type, type, cast_type, fmt, to_string) \
118 int bus_set_transient_##function( \
119 Unit *u, \
120 const char *name, \
121 cast_type *p, \
122 sd_bus_message *message, \
123 UnitWriteFlags flags, \
124 sd_bus_error *error) { \
125 \
126 _cleanup_free_ char *s = NULL; \
127 type v; \
128 int r; \
129 \
130 assert(p); \
131 \
132 r = sd_bus_message_read(message, bus_type, &v); \
133 if (r < 0) \
134 return r; \
135 \
136 r = to_string(v, &s); \
137 if (r == -EINVAL) \
138 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
139 "Invalid %s setting: " fmt, name, v); \
140 if (r < 0) \
141 return r; \
142 \
143 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
144 *p = (cast_type) v; \
145 unit_write_settingf(u, flags, name, \
146 "%s=%s", name, s); \
147 } \
148 \
149 return 1; \
150 }
151
152 #define BUS_DEFINE_SET_TRANSIENT_PARSE(function, type, parse) \
153 int bus_set_transient_##function( \
154 Unit *u, \
155 const char *name, \
156 type *p, \
157 sd_bus_message *message, \
158 UnitWriteFlags flags, \
159 sd_bus_error *error) { \
160 \
161 const char *s; \
162 type v; \
163 int r; \
164 \
165 assert(p); \
166 \
167 r = sd_bus_message_read(message, "s", &s); \
168 if (r < 0) \
169 return r; \
170 \
171 v = parse(s); \
172 if (v < 0) \
173 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
174 "Invalid %s setting: %s", name, s); \
175 \
176 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
177 *p = v; \
178 unit_write_settingf(u, flags, name, \
179 "%s=%s", name, s); \
180 } \
181 \
182 return 1; \
183 }
184
185 #define BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(function, type, parse) \
186 int bus_set_transient_##function( \
187 Unit *u, \
188 const char *name, \
189 type *p, \
190 sd_bus_message *message, \
191 UnitWriteFlags flags, \
192 sd_bus_error *error) { \
193 \
194 const char *s; \
195 type v; \
196 int r; \
197 \
198 assert(p); \
199 \
200 r = sd_bus_message_read(message, "s", &s); \
201 if (r < 0) \
202 return r; \
203 \
204 r = parse(s, &v); \
205 if (r < 0) \
206 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
207 "Invalid %s setting: %s", name, s); \
208 \
209 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
210 *p = v; \
211 unit_write_settingf(u, flags, name, \
212 "%s=%s", name, strempty(s)); \
213 } \
214 \
215 return 1; \
216 }
217
218 #define BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(function, check) \
219 int bus_set_transient_##function( \
220 Unit *u, \
221 const char *name, \
222 char **p, \
223 sd_bus_message *message, \
224 UnitWriteFlags flags, \
225 sd_bus_error *error) { \
226 \
227 const char *v; \
228 int r; \
229 \
230 assert(p); \
231 \
232 r = sd_bus_message_read(message, "s", &v); \
233 if (r < 0) \
234 return r; \
235 \
236 if (!isempty(v) && !check(v)) \
237 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
238 "Invalid %s setting: %s", name, v); \
239 \
240 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
241 r = free_and_strdup(p, empty_to_null(v)); \
242 if (r < 0) \
243 return r; \
244 \
245 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, \
246 "%s=%s", name, strempty(v)); \
247 } \
248 \
249 return 1; \
250 }
251
252 int bus_set_transient_mode_t(Unit *u, const char *name, mode_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
253 int bus_set_transient_unsigned(Unit *u, const char *name, unsigned *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
254 int bus_set_transient_user(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
255 int bus_set_transient_path(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
256 int bus_set_transient_string(Unit *u, const char *name, char **p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
257 int bus_set_transient_bool(Unit *u, const char *name, bool *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
258 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);
259 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) {
260 return bus_set_transient_usec_internal(u, name, p, false, message, flags, error);
261 }
262 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) {
263 return bus_set_transient_usec_internal(u, name, p, true, message, flags, error);
264 }