1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-bus.h"
5 
6 #include "dissect-image.h"
7 #include "unit.h"
8 
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);
10 
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) {                          \
19                                                                         \
20                 type v;                                                 \
21                 int r;                                                  \
22                                                                         \
23                 assert(p);                                              \
24                                                                         \
25                 r = sd_bus_message_read(message, bus_type, &v);         \
26                 if (r < 0)                                              \
27                         return r;                                       \
28                                                                         \
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                 }                                                       \
34                                                                         \
35                 return 1;                                               \
36         }
37 
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) {                          \
46                                                                         \
47                 type v;                                                 \
48                 int r;                                                  \
49                                                                         \
50                 assert(p);                                              \
51                                                                         \
52                 r = sd_bus_message_read(message, bus_type, &v);         \
53                 if (r < 0)                                              \
54                         return r;                                       \
55                                                                         \
56                 if (!check(v))                                          \
57                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
58                                                  "Invalid %s setting: " fmt, name, v); \
59                                                                         \
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                 }                                                       \
65                                                                         \
66                 return 1;                                               \
67         }
68 
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) {                          \
77                                                                         \
78                 const char *s;                                          \
79                 type v;                                                 \
80                 int r;                                                  \
81                                                                         \
82                 assert(p);                                              \
83                                                                         \
84                 r = sd_bus_message_read(message, bus_type, &v);         \
85                 if (r < 0)                                              \
86                         return r;                                       \
87                                                                         \
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); \
92                                                                         \
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                 }                                                       \
98                                                                         \
99                 return 1;                                               \
100         }
101 
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) {                          \
110                                                                         \
111                 _cleanup_free_ char *s = NULL;                          \
112                 type v;                                                 \
113                 int r;                                                  \
114                                                                         \
115                 assert(p);                                              \
116                                                                         \
117                 r = sd_bus_message_read(message, bus_type, &v);         \
118                 if (r < 0)                                              \
119                         return r;                                       \
120                                                                         \
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;                                       \
127                                                                         \
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));         \
133                 }                                                       \
134                                                                         \
135                 return 1;                                               \
136         }
137 
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) {                          \
146                                                                         \
147                 const char *s;                                          \
148                 type v;                                                 \
149                 int r;                                                  \
150                                                                         \
151                 assert(p);                                              \
152                                                                         \
153                 r = sd_bus_message_read(message, "s", &s);              \
154                 if (r < 0)                                              \
155                         return r;                                       \
156                                                                         \
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); \
161                                                                         \
162                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {                    \
163                         *p = v;                                         \
164                         unit_write_settingf(u, flags, name,             \
165                                             "%s=%s", name, s);          \
166                 }                                                       \
167                                                                         \
168                 return 1;                                               \
169         }
170 
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) {                          \
179                                                                         \
180                 const char *s;                                          \
181                 type v;                                                 \
182                 int r;                                                  \
183                                                                         \
184                 assert(p);                                              \
185                                                                         \
186                 r = sd_bus_message_read(message, "s", &s);              \
187                 if (r < 0)                                              \
188                         return r;                                       \
189                                                                         \
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); \
194                                                                         \
195                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {                    \
196                         *p = v;                                         \
197                         unit_write_settingf(u, flags, name,             \
198                                             "%s=%s", name, strempty(s)); \
199                 }                                                       \
200                                                                         \
201                 return 1;                                               \
202         }
203 
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) {                          \
212                                                                         \
213                 const char *v;                                          \
214                 int r;                                                  \
215                                                                         \
216                 assert(p);                                              \
217                                                                         \
218                 r = sd_bus_message_read(message, "s", &v);              \
219                 if (r < 0)                                              \
220                         return r;                                       \
221                                                                         \
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); \
225                                                                         \
226                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {                    \
227                         r = free_and_strdup(p, empty_to_null(v));       \
228                         if (r < 0)                                      \
229                                 return r;                               \
230                                                                         \
231                         unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, \
232                                             "%s=%s", name, strempty(v)); \
233                 }                                                       \
234                                                                         \
235                 return 1;                                               \
236         }
237 
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_usec_internal(Unit *u, const char *name, usec_t *p, bool fix_0, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
bus_set_transient_usec(Unit * u,const char * name,usec_t * p,sd_bus_message * message,UnitWriteFlags flags,sd_bus_error * error)245 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) {
246         return bus_set_transient_usec_internal(u, name, p, false, message, flags, error);
247 }
bus_set_transient_usec_fix_0(Unit * u,const char * name,usec_t * p,sd_bus_message * message,UnitWriteFlags flags,sd_bus_error * error)248 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) {
249         return bus_set_transient_usec_internal(u, name, p, true, message, flags, error);
250 }
251 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);
252 
253 int bus_read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator);
254