| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-event.h" |
| 4 |
#include "sd-varlink.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "path-util.h" |
| 11 |
#include "pidref.h" |
| 12 |
#include "recurse-dir.h" |
| 13 |
#include "set.h" |
| 14 |
#include "socket-util.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "varlink-internal.h" |
| 17 |
#include "varlink-util.h" |
| 18 |
#include "version.h" |
| 19 |
|
| 20 |
int varlink_get_peer_pidref(sd_varlink *v, PidRef *ret) { |
| 21 |
int r; |
| 22 |
|
| 23 |
assert_return(v, -EINVAL); |
| 24 |
assert_return(ret, -EINVAL); |
| 25 |
|
| 26 |
/* Returns r > 0 if we acquired the pidref via SO_PEERPIDFD (i.e. if we can use it for |
| 27 |
* authentication). Returns == 0 if we didn't, and the pidref should not be used for |
| 28 |
* authentication. */ |
| 29 |
|
| 30 |
int pidfd = sd_varlink_get_peer_pidfd(v); |
| 31 |
if (pidfd < 0) { |
| 32 |
if (!ERRNO_IS_NEG_IOCTL_NOT_SUPPORTED(pidfd)) |
| 33 |
return pidfd; |
| 34 |
|
| 35 |
pid_t pid; |
| 36 |
r = sd_varlink_get_peer_pid(v, &pid); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
|
| 40 |
r = pidref_set_pid(ret, pid); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
return 0; /* didn't get pidfd securely */ |
| 45 |
} |
| 46 |
|
| 47 |
r = pidref_set_pidfd(ret, pidfd); |
| 48 |
if (r < 0) |
| 49 |
return r; |
| 50 |
|
| 51 |
return 1; /* got pidfd securely */ |
| 52 |
} |
| 53 |
|
| 54 |
int varlink_call_and_log( |
| 55 |
sd_varlink *v, |
| 56 |
const char *method, |
| 57 |
sd_json_variant *parameters, |
| 58 |
sd_json_variant **ret_parameters) { |
| 59 |
|
| 60 |
sd_json_variant *reply = NULL; |
| 61 |
const char *error_id = NULL; |
| 62 |
int r; |
| 63 |
|
| 64 |
assert_return(v, -EINVAL); |
| 65 |
assert_return(method, -EINVAL); |
| 66 |
|
| 67 |
r = sd_varlink_call(v, method, parameters, &reply, &error_id); |
| 68 |
if (r < 0) |
| 69 |
return log_error_errno(r, "Failed to issue %s() varlink call: %m", method); |
| 70 |
if (error_id) { |
| 71 |
r = sd_varlink_error_to_errno(error_id, reply); /* If this is a system errno style error, output it with %m */ |
| 72 |
if (r != -EBADR) |
| 73 |
return log_error_errno(r, "Failed to issue %s() varlink call: %m", method); |
| 74 |
|
| 75 |
return log_error_errno(r, "Failed to issue %s() varlink call: %s", method, error_id); |
| 76 |
} |
| 77 |
|
| 78 |
if (ret_parameters) |
| 79 |
*ret_parameters = TAKE_PTR(reply); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int varlink_callb_and_log( |
| 85 |
sd_varlink *v, |
| 86 |
const char *method, |
| 87 |
sd_json_variant **ret_parameters, |
| 88 |
...) { |
| 89 |
|
| 90 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; |
| 91 |
va_list ap; |
| 92 |
int r; |
| 93 |
|
| 94 |
assert_return(v, -EINVAL); |
| 95 |
assert_return(method, -EINVAL); |
| 96 |
|
| 97 |
va_start(ap, ret_parameters); |
| 98 |
r = sd_json_buildv(¶meters, ap); |
| 99 |
va_end(ap); |
| 100 |
if (r < 0) |
| 101 |
return log_error_errno(r, "Failed to build JSON message: %m"); |
| 102 |
|
| 103 |
return varlink_call_and_log(v, method, parameters, ret_parameters); |
| 104 |
} |
| 105 |
|
| 106 |
int varlink_many_notify(Set *s, sd_json_variant *parameters) { |
| 107 |
sd_varlink *link; |
| 108 |
int r = 1; |
| 109 |
|
| 110 |
if (set_isempty(s)) |
| 111 |
return 0; |
| 112 |
|
| 113 |
SET_FOREACH(link, s) |
| 114 |
RET_GATHER(r, sd_varlink_notify(link, parameters)); |
| 115 |
|
| 116 |
return r; |
| 117 |
} |
| 118 |
|
| 119 |
int varlink_many_notifyb(Set *s, ...) { |
| 120 |
int r; |
| 121 |
|
| 122 |
/* Equivalent to varlink_notifyb(), but does this for each entry of the supplied set of Varlink connections */ |
| 123 |
|
| 124 |
if (set_isempty(s)) |
| 125 |
return 0; |
| 126 |
|
| 127 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; |
| 128 |
va_list ap; |
| 129 |
va_start(ap, s); |
| 130 |
r = sd_json_buildv(¶meters, ap); |
| 131 |
va_end(ap); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
|
| 135 |
return varlink_many_notify(s, parameters); |
| 136 |
} |
| 137 |
|
| 138 |
int varlink_many_reply(Set *s, sd_json_variant *parameters) { |
| 139 |
if (set_isempty(s)) |
| 140 |
return 0; |
| 141 |
|
| 142 |
int r = 1; |
| 143 |
sd_varlink *link; |
| 144 |
SET_FOREACH(link, s) |
| 145 |
RET_GATHER(r, sd_varlink_reply(link, parameters)); |
| 146 |
|
| 147 |
return r; |
| 148 |
} |
| 149 |
|
| 150 |
int varlink_many_error(Set *s, const char *error_id, sd_json_variant *parameters) { |
| 151 |
if (set_isempty(s)) |
| 152 |
return 0; |
| 153 |
|
| 154 |
int r = 1; |
| 155 |
sd_varlink *link; |
| 156 |
SET_FOREACH(link, s) |
| 157 |
RET_GATHER(r, sd_varlink_error(link, error_id, parameters)); |
| 158 |
|
| 159 |
return r; |
| 160 |
} |
| 161 |
|
| 162 |
int varlink_set_info_systemd(sd_varlink_server *server) { |
| 163 |
_cleanup_free_ char *product = NULL; |
| 164 |
|
| 165 |
product = strjoin("systemd (", program_invocation_short_name, ")"); |
| 166 |
if (!product) |
| 167 |
return -ENOMEM; |
| 168 |
|
| 169 |
return sd_varlink_server_set_info( |
| 170 |
server, |
| 171 |
"The systemd Project", |
| 172 |
product, |
| 173 |
PROJECT_VERSION_FULL " (" GIT_VERSION ")", |
| 174 |
"https://systemd.io/"); |
| 175 |
} |
| 176 |
|
| 177 |
int varlink_server_new( |
| 178 |
sd_varlink_server **ret, |
| 179 |
sd_varlink_server_flags_t flags, |
| 180 |
void *userdata) { |
| 181 |
|
| 182 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL; |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(ret); |
| 186 |
|
| 187 |
r = sd_varlink_server_new(&s, flags|SD_VARLINK_SERVER_FD_PASSING_INPUT_STRICT); |
| 188 |
if (r < 0) |
| 189 |
return log_debug_errno(r, "Failed to allocate varlink server object: %m"); |
| 190 |
|
| 191 |
r = varlink_set_info_systemd(s); |
| 192 |
if (r < 0) |
| 193 |
return log_debug_errno(r, "Failed to configure varlink server object: %m"); |
| 194 |
|
| 195 |
sd_varlink_server_set_userdata(s, userdata); |
| 196 |
|
| 197 |
*ret = TAKE_PTR(s); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
int varlink_check_privileged_peer(sd_varlink *vl) { |
| 202 |
int r; |
| 203 |
|
| 204 |
assert(vl); |
| 205 |
|
| 206 |
uid_t uid; |
| 207 |
r = sd_varlink_get_peer_uid(vl, &uid); |
| 208 |
if (r < 0) |
| 209 |
return log_debug_errno(r, "Failed to get peer UID: %m"); |
| 210 |
|
| 211 |
if (uid != 0) |
| 212 |
return sd_varlink_error(vl, SD_VARLINK_ERROR_PERMISSION_DENIED, /* parameters= */ NULL); |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 218 |
varlink_hash_ops, |
| 219 |
void, |
| 220 |
trivial_hash_func, |
| 221 |
trivial_compare_func, |
| 222 |
sd_varlink, |
| 223 |
sd_varlink_unref); |
| 224 |
|
| 225 |
static int varlink_finish_idle(Set *s) { |
| 226 |
int r; |
| 227 |
|
| 228 |
sd_varlink *vl; |
| 229 |
bool fully_idle = true; |
| 230 |
SET_FOREACH(vl, s) { |
| 231 |
r = sd_varlink_is_idle(vl); |
| 232 |
if (r < 0) |
| 233 |
return r; |
| 234 |
if (r == 0) |
| 235 |
fully_idle = false; |
| 236 |
else { |
| 237 |
/* Idle? Then we can close the connection, and release some resources. */ |
| 238 |
assert_se(set_remove(s, vl) == vl); |
| 239 |
vl = sd_varlink_close_unref(vl); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return fully_idle; |
| 244 |
} |
| 245 |
|
| 246 |
#define VARLINK_EXECUTE_SOCKETS_MAX 255 |
| 247 |
|
| 248 |
ssize_t varlink_execute_directory( |
| 249 |
const char *path, |
| 250 |
const char *method, |
| 251 |
sd_json_variant *parameters, |
| 252 |
bool more, |
| 253 |
usec_t timeout_usec, |
| 254 |
sd_varlink_reply_t reply, |
| 255 |
void *userdata) { |
| 256 |
|
| 257 |
int r; |
| 258 |
|
| 259 |
assert(path); |
| 260 |
assert(method); |
| 261 |
|
| 262 |
/* Invokes the specified method on all Varlink sockets in the specified directory. Any reply |
| 263 |
* will be dispatched to the reply callback. Blocks until the last reply has come in. |
| 264 |
* |
| 265 |
* Returns how many sockets were contacted. |
| 266 |
* |
| 267 |
* Usecase for all of this: hook directories, where components can link their sockets into to get |
| 268 |
* notified about certain system events. */ |
| 269 |
|
| 270 |
_cleanup_close_ int fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
| 271 |
if (fd < 0) |
| 272 |
return log_debug_errno(errno, "Failed to open '%s': %m", path); |
| 273 |
|
| 274 |
_cleanup_free_ DirectoryEntries *dentries = NULL; |
| 275 |
r = readdir_all(fd, RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &dentries); |
| 276 |
if (r < 0) |
| 277 |
return log_debug_errno(r, "Failed to enumerate '%s': %m", path); |
| 278 |
|
| 279 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 280 |
_cleanup_(set_freep) Set *links = NULL; |
| 281 |
size_t t = 0; |
| 282 |
FOREACH_ARRAY(dp, dentries->entries, dentries->n_entries) { |
| 283 |
struct dirent *de = *dp; |
| 284 |
|
| 285 |
if (de->d_type != DT_SOCK) |
| 286 |
continue; |
| 287 |
|
| 288 |
t++; |
| 289 |
|
| 290 |
_cleanup_free_ char *j = path_join(path, de->d_name); |
| 291 |
if (!j) |
| 292 |
return log_oom_debug(); |
| 293 |
|
| 294 |
if (set_size(links) >= VARLINK_EXECUTE_SOCKETS_MAX) { |
| 295 |
log_debug("Too many sockets (%zu) in directory, skipping '%s'.", t, j); |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
_cleanup_close_ int socket_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 300 |
if (socket_fd < 0) |
| 301 |
return log_debug_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m"); |
| 302 |
|
| 303 |
r = connect_unix_path(socket_fd, fd, de->d_name); |
| 304 |
if (r < 0) { |
| 305 |
log_debug_errno(r, "Failed to connect to '%s', ignoring: %m", j); |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
if (!event) { |
| 310 |
r = sd_event_new(&event); |
| 311 |
if (r < 0) |
| 312 |
return log_debug_errno(r, "Failed to allocate event loop: %m"); |
| 313 |
} |
| 314 |
|
| 315 |
_cleanup_(sd_varlink_unrefp) sd_varlink *link = NULL; |
| 316 |
r = sd_varlink_connect_fd(&link, socket_fd); |
| 317 |
if (r < 0) |
| 318 |
return log_debug_errno(r, "Failed to allocate Varlink connection: %m"); |
| 319 |
|
| 320 |
TAKE_FD(socket_fd); |
| 321 |
|
| 322 |
r = sd_varlink_attach_event(link, event, /* priority= */ 0); |
| 323 |
if (r < 0) |
| 324 |
return log_debug_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 325 |
|
| 326 |
sd_varlink_set_userdata(link, userdata); |
| 327 |
|
| 328 |
r = sd_varlink_bind_reply(link, reply); |
| 329 |
if (r < 0) |
| 330 |
return log_debug_errno(r, "Failed to bind reply callback: %m"); |
| 331 |
|
| 332 |
r = sd_varlink_set_description(link, j); |
| 333 |
if (r < 0) |
| 334 |
return log_debug_errno(r, "Failed to set description: %m"); |
| 335 |
|
| 336 |
r = sd_varlink_set_relative_timeout(link, timeout_usec); |
| 337 |
if (r < 0) |
| 338 |
return log_debug_errno(r, "Failed to set relative timeout: %m"); |
| 339 |
|
| 340 |
if (more) |
| 341 |
r = sd_varlink_observe(link, method, parameters); |
| 342 |
else |
| 343 |
r = sd_varlink_invoke(link, method, parameters); |
| 344 |
if (r < 0) |
| 345 |
return log_debug_errno(r, "Failed to enqueue message on Varlink connection: %m"); |
| 346 |
|
| 347 |
if (set_ensure_consume(&links, &varlink_hash_ops, TAKE_PTR(link)) < 0) |
| 348 |
return log_oom_debug(); |
| 349 |
} |
| 350 |
|
| 351 |
size_t c = set_size(links); |
| 352 |
|
| 353 |
for (;;) { |
| 354 |
if (event) { |
| 355 |
int state = sd_event_get_state(event); |
| 356 |
if (state < 0) |
| 357 |
return state; |
| 358 |
if (state == SD_EVENT_FINISHED) { |
| 359 |
int x; |
| 360 |
r = sd_event_get_exit_code(event, &x); |
| 361 |
if (r < 0) |
| 362 |
return r; |
| 363 |
if (x != 0) |
| 364 |
return x; |
| 365 |
|
| 366 |
break; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
r = varlink_finish_idle(links); |
| 371 |
if (r < 0) |
| 372 |
return r; |
| 373 |
if (r > 0) |
| 374 |
break; /* idle, we are done */ |
| 375 |
|
| 376 |
assert(event); |
| 377 |
|
| 378 |
r = sd_event_run(event, /* timeout= */ UINT64_MAX); |
| 379 |
if (r < 0) |
| 380 |
return r; |
| 381 |
} |
| 382 |
|
| 383 |
return (ssize_t) c; |
| 384 |
} |