| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <poll.h> |
| 4 |
#include <sys/inotify.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-login.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "cgroup-util.h" |
| 11 |
#include "dirent-util.h" |
| 12 |
#include "env-file.h" |
| 13 |
#include "extract-word.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "format-util.h" |
| 16 |
#include "fs-util.h" |
| 17 |
#include "hostname-util.h" |
| 18 |
#include "io-util.h" |
| 19 |
#include "login-util.h" |
| 20 |
#include "parse-util.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "pidfd-util.h" |
| 23 |
#include "pidref.h" |
| 24 |
#include "socket-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "user-util.h" |
| 28 |
|
| 29 |
/* Error codes: |
| 30 |
* |
| 31 |
* invalid input parameters → -EINVAL |
| 32 |
* invalid fd → -EBADF |
| 33 |
* process does not exist → -ESRCH |
| 34 |
* cgroup does not exist → -ENOENT |
| 35 |
* machine, session does not exist → -ENXIO |
| 36 |
* requested metadata on object is missing → -ENODATA |
| 37 |
*/ |
| 38 |
|
| 39 |
_public_ int sd_pid_get_session(pid_t pid, char **ret_session) { |
| 40 |
int r; |
| 41 |
|
| 42 |
assert_return(pid >= 0, -EINVAL); |
| 43 |
|
| 44 |
r = cg_pid_get_session(pid, ret_session); |
| 45 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 46 |
} |
| 47 |
|
| 48 |
_public_ int sd_pid_get_unit(pid_t pid, char **ret_unit) { |
| 49 |
int r; |
| 50 |
|
| 51 |
assert_return(pid >= 0, -EINVAL); |
| 52 |
|
| 53 |
r = cg_pid_get_unit(pid, ret_unit); |
| 54 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 55 |
} |
| 56 |
|
| 57 |
_public_ int sd_pid_get_user_unit(pid_t pid, char **ret_unit) { |
| 58 |
int r; |
| 59 |
|
| 60 |
assert_return(pid >= 0, -EINVAL); |
| 61 |
|
| 62 |
r = cg_pid_get_user_unit(pid, ret_unit); |
| 63 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 64 |
} |
| 65 |
|
| 66 |
_public_ int sd_pid_get_machine_name(pid_t pid, char **ret_machine) { |
| 67 |
int r; |
| 68 |
|
| 69 |
assert_return(pid >= 0, -EINVAL); |
| 70 |
|
| 71 |
r = cg_pid_get_machine_name(pid, ret_machine); |
| 72 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 73 |
} |
| 74 |
|
| 75 |
_public_ int sd_pid_get_slice(pid_t pid, char **ret_slice) { |
| 76 |
int r; |
| 77 |
|
| 78 |
assert_return(pid >= 0, -EINVAL); |
| 79 |
|
| 80 |
r = cg_pid_get_slice(pid, ret_slice); |
| 81 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 82 |
} |
| 83 |
|
| 84 |
_public_ int sd_pid_get_user_slice(pid_t pid, char **ret_slice) { |
| 85 |
int r; |
| 86 |
|
| 87 |
assert_return(pid >= 0, -EINVAL); |
| 88 |
|
| 89 |
r = cg_pid_get_user_slice(pid, ret_slice); |
| 90 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 91 |
} |
| 92 |
|
| 93 |
_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *ret_uid) { |
| 94 |
int r; |
| 95 |
|
| 96 |
assert_return(pid >= 0, -EINVAL); |
| 97 |
|
| 98 |
r = cg_pid_get_owner_uid(pid, ret_uid); |
| 99 |
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
| 100 |
} |
| 101 |
|
| 102 |
_public_ int sd_pid_get_cgroup(pid_t pid, char **ret_cgroup) { |
| 103 |
int r; |
| 104 |
|
| 105 |
assert_return(pid >= 0, -EINVAL); |
| 106 |
|
| 107 |
_cleanup_free_ char *c = NULL; |
| 108 |
r = cg_pid_get_path(pid, &c); |
| 109 |
if (r < 0) |
| 110 |
return r; |
| 111 |
|
| 112 |
if (ret_cgroup) { |
| 113 |
/* The internal APIs return the empty string for the root cgroup, let's return the "/" in the |
| 114 |
* public APIs instead, as that's easier and less ambiguous for people to grok. */ |
| 115 |
if (isempty(c)) { |
| 116 |
r = free_and_strdup(&c, "/"); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
} |
| 120 |
|
| 121 |
*ret_cgroup = TAKE_PTR(c); |
| 122 |
} |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
_public_ int sd_pidfd_get_session(int pidfd, char **ret_session) { |
| 128 |
_cleanup_free_ char *session = NULL; |
| 129 |
pid_t pid; |
| 130 |
int r; |
| 131 |
|
| 132 |
assert_return(pidfd >= 0, -EBADF); |
| 133 |
|
| 134 |
r = pidfd_get_pid(pidfd, &pid); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
|
| 138 |
r = sd_pid_get_session(pid, &session); |
| 139 |
if (r < 0) |
| 140 |
return r; |
| 141 |
|
| 142 |
r = pidfd_verify_pid(pidfd, pid); |
| 143 |
if (r < 0) |
| 144 |
return r; |
| 145 |
|
| 146 |
if (ret_session) |
| 147 |
*ret_session = TAKE_PTR(session); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
_public_ int sd_pidfd_get_unit(int pidfd, char **ret_unit) { |
| 152 |
_cleanup_free_ char *unit = NULL; |
| 153 |
pid_t pid; |
| 154 |
int r; |
| 155 |
|
| 156 |
assert_return(pidfd >= 0, -EBADF); |
| 157 |
|
| 158 |
r = pidfd_get_pid(pidfd, &pid); |
| 159 |
if (r < 0) |
| 160 |
return r; |
| 161 |
|
| 162 |
r = sd_pid_get_unit(pid, &unit); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
|
| 166 |
r = pidfd_verify_pid(pidfd, pid); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
if (ret_unit) |
| 171 |
*ret_unit = TAKE_PTR(unit); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
_public_ int sd_pidfd_get_user_unit(int pidfd, char **ret_unit) { |
| 176 |
_cleanup_free_ char *unit = NULL; |
| 177 |
pid_t pid; |
| 178 |
int r; |
| 179 |
|
| 180 |
assert_return(pidfd >= 0, -EBADF); |
| 181 |
|
| 182 |
r = pidfd_get_pid(pidfd, &pid); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
|
| 186 |
r = sd_pid_get_user_unit(pid, &unit); |
| 187 |
if (r < 0) |
| 188 |
return r; |
| 189 |
|
| 190 |
r = pidfd_verify_pid(pidfd, pid); |
| 191 |
if (r < 0) |
| 192 |
return r; |
| 193 |
|
| 194 |
if (ret_unit) |
| 195 |
*ret_unit = TAKE_PTR(unit); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
_public_ int sd_pidfd_get_machine_name(int pidfd, char **ret_machine) { |
| 200 |
_cleanup_free_ char *name = NULL; |
| 201 |
pid_t pid; |
| 202 |
int r; |
| 203 |
|
| 204 |
assert_return(pidfd >= 0, -EBADF); |
| 205 |
|
| 206 |
r = pidfd_get_pid(pidfd, &pid); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
r = sd_pid_get_machine_name(pid, &name); |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
|
| 214 |
r = pidfd_verify_pid(pidfd, pid); |
| 215 |
if (r < 0) |
| 216 |
return r; |
| 217 |
|
| 218 |
if (ret_machine) |
| 219 |
*ret_machine = TAKE_PTR(name); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
_public_ int sd_pidfd_get_slice(int pidfd, char **ret_slice) { |
| 224 |
_cleanup_free_ char *slice = NULL; |
| 225 |
pid_t pid; |
| 226 |
int r; |
| 227 |
|
| 228 |
assert_return(pidfd >= 0, -EBADF); |
| 229 |
|
| 230 |
r = pidfd_get_pid(pidfd, &pid); |
| 231 |
if (r < 0) |
| 232 |
return r; |
| 233 |
|
| 234 |
r = sd_pid_get_slice(pid, &slice); |
| 235 |
if (r < 0) |
| 236 |
return r; |
| 237 |
|
| 238 |
r = pidfd_verify_pid(pidfd, pid); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
|
| 242 |
if (ret_slice) |
| 243 |
*ret_slice = TAKE_PTR(slice); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
_public_ int sd_pidfd_get_user_slice(int pidfd, char **ret_slice) { |
| 248 |
_cleanup_free_ char *slice = NULL; |
| 249 |
pid_t pid; |
| 250 |
int r; |
| 251 |
|
| 252 |
assert_return(pidfd >= 0, -EBADF); |
| 253 |
|
| 254 |
r = pidfd_get_pid(pidfd, &pid); |
| 255 |
if (r < 0) |
| 256 |
return r; |
| 257 |
|
| 258 |
r = sd_pid_get_user_slice(pid, &slice); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
r = pidfd_verify_pid(pidfd, pid); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
|
| 266 |
if (ret_slice) |
| 267 |
*ret_slice = TAKE_PTR(slice); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
_public_ int sd_pidfd_get_owner_uid(int pidfd, uid_t *ret_uid) { |
| 272 |
uid_t uid; |
| 273 |
pid_t pid; |
| 274 |
int r; |
| 275 |
|
| 276 |
assert_return(pidfd >= 0, -EBADF); |
| 277 |
|
| 278 |
r = pidfd_get_pid(pidfd, &pid); |
| 279 |
if (r < 0) |
| 280 |
return r; |
| 281 |
|
| 282 |
r = sd_pid_get_owner_uid(pid, &uid); |
| 283 |
if (r < 0) |
| 284 |
return r; |
| 285 |
|
| 286 |
r = pidfd_verify_pid(pidfd, pid); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
|
| 290 |
if (ret_uid) |
| 291 |
*ret_uid = uid; |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
_public_ int sd_pidfd_get_cgroup(int pidfd, char **ret_cgroup) { |
| 296 |
_cleanup_free_ char *cgroup = NULL; |
| 297 |
pid_t pid; |
| 298 |
int r; |
| 299 |
|
| 300 |
assert_return(pidfd >= 0, -EBADF); |
| 301 |
|
| 302 |
r = pidfd_get_pid(pidfd, &pid); |
| 303 |
if (r < 0) |
| 304 |
return r; |
| 305 |
|
| 306 |
r = sd_pid_get_cgroup(pid, &cgroup); |
| 307 |
if (r < 0) |
| 308 |
return r; |
| 309 |
|
| 310 |
r = pidfd_verify_pid(pidfd, pid); |
| 311 |
if (r < 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
if (ret_cgroup) |
| 315 |
*ret_cgroup = TAKE_PTR(cgroup); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
_public_ int sd_peer_get_session(int fd, char **ret_session) { |
| 320 |
int r; |
| 321 |
|
| 322 |
assert_return(fd >= 0, -EBADF); |
| 323 |
|
| 324 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 325 |
r = getpeerpidref(fd, &pidref); |
| 326 |
if (r < 0) |
| 327 |
return r; |
| 328 |
|
| 329 |
return cg_pidref_get_session(&pidref, ret_session); |
| 330 |
} |
| 331 |
|
| 332 |
_public_ int sd_peer_get_owner_uid(int fd, uid_t *ret_uid) { |
| 333 |
int r; |
| 334 |
|
| 335 |
assert_return(fd >= 0, -EBADF); |
| 336 |
|
| 337 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 338 |
r = getpeerpidref(fd, &pidref); |
| 339 |
if (r < 0) |
| 340 |
return r; |
| 341 |
|
| 342 |
return cg_pidref_get_owner_uid(&pidref, ret_uid); |
| 343 |
} |
| 344 |
|
| 345 |
_public_ int sd_peer_get_unit(int fd, char **ret_unit) { |
| 346 |
int r; |
| 347 |
|
| 348 |
assert_return(fd >= 0, -EBADF); |
| 349 |
|
| 350 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 351 |
r = getpeerpidref(fd, &pidref); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
|
| 355 |
return cg_pidref_get_unit(&pidref, ret_unit); |
| 356 |
} |
| 357 |
|
| 358 |
_public_ int sd_peer_get_user_unit(int fd, char **ret_unit) { |
| 359 |
struct ucred ucred; |
| 360 |
int r; |
| 361 |
|
| 362 |
assert_return(fd >= 0, -EBADF); |
| 363 |
|
| 364 |
r = getpeercred(fd, &ucred); |
| 365 |
if (r < 0) |
| 366 |
return r; |
| 367 |
|
| 368 |
return cg_pid_get_user_unit(ucred.pid, ret_unit); |
| 369 |
} |
| 370 |
|
| 371 |
_public_ int sd_peer_get_machine_name(int fd, char **ret_machine) { |
| 372 |
struct ucred ucred; |
| 373 |
int r; |
| 374 |
|
| 375 |
assert_return(fd >= 0, -EBADF); |
| 376 |
|
| 377 |
r = getpeercred(fd, &ucred); |
| 378 |
if (r < 0) |
| 379 |
return r; |
| 380 |
|
| 381 |
return cg_pid_get_machine_name(ucred.pid, ret_machine); |
| 382 |
} |
| 383 |
|
| 384 |
_public_ int sd_peer_get_slice(int fd, char **ret_slice) { |
| 385 |
struct ucred ucred; |
| 386 |
int r; |
| 387 |
|
| 388 |
assert_return(fd >= 0, -EBADF); |
| 389 |
|
| 390 |
r = getpeercred(fd, &ucred); |
| 391 |
if (r < 0) |
| 392 |
return r; |
| 393 |
|
| 394 |
return cg_pid_get_slice(ucred.pid, ret_slice); |
| 395 |
} |
| 396 |
|
| 397 |
_public_ int sd_peer_get_user_slice(int fd, char **ret_slice) { |
| 398 |
struct ucred ucred; |
| 399 |
int r; |
| 400 |
|
| 401 |
assert_return(fd >= 0, -EBADF); |
| 402 |
|
| 403 |
r = getpeercred(fd, &ucred); |
| 404 |
if (r < 0) |
| 405 |
return r; |
| 406 |
|
| 407 |
return cg_pid_get_user_slice(ucred.pid, ret_slice); |
| 408 |
} |
| 409 |
|
| 410 |
_public_ int sd_peer_get_cgroup(int fd, char **ret_cgroup) { |
| 411 |
int r; |
| 412 |
|
| 413 |
assert_return(fd >= 0, -EBADF); |
| 414 |
|
| 415 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 416 |
r = getpeerpidref(fd, &pidref); |
| 417 |
if (r < 0) |
| 418 |
return r; |
| 419 |
|
| 420 |
_cleanup_free_ char *c = NULL; |
| 421 |
r = cg_pidref_get_path(&pidref, &c); |
| 422 |
if (r < 0) |
| 423 |
return r; |
| 424 |
|
| 425 |
if (ret_cgroup) { |
| 426 |
/* The internal APIs return the empty string for the root cgroup, let's return the "/" in the |
| 427 |
* public APIs instead, as that's easier and less ambiguous for people to grok. */ |
| 428 |
if (isempty(c)) { |
| 429 |
r = free_and_strdup(&c, "/"); |
| 430 |
if (r < 0) |
| 431 |
return r; |
| 432 |
} |
| 433 |
|
| 434 |
*ret_cgroup = TAKE_PTR(c); |
| 435 |
} |
| 436 |
|
| 437 |
return 0; |
| 438 |
} |
| 439 |
|
| 440 |
static int file_of_uid(uid_t uid, char **ret) { |
| 441 |
|
| 442 |
assert_return(uid_is_valid(uid), -EINVAL); |
| 443 |
assert(ret); |
| 444 |
|
| 445 |
if (asprintf(ret, "/run/systemd/users/" UID_FMT, uid) < 0) |
| 446 |
return -ENOMEM; |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
_public_ int sd_uid_get_state(uid_t uid, char **ret_state) { |
| 452 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 453 |
int r; |
| 454 |
|
| 455 |
r = file_of_uid(uid, &p); |
| 456 |
if (r < 0) |
| 457 |
return r; |
| 458 |
|
| 459 |
r = parse_env_file(/* f= */ NULL, p, "STATE", &s); |
| 460 |
if (r == -ENOENT) |
| 461 |
r = free_and_strdup(&s, "offline"); |
| 462 |
if (r < 0) |
| 463 |
return r; |
| 464 |
if (isempty(s)) |
| 465 |
return -EIO; |
| 466 |
|
| 467 |
if (ret_state) |
| 468 |
*ret_state = TAKE_PTR(s); |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
_public_ int sd_uid_get_display(uid_t uid, char **ret_display) { |
| 473 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 474 |
int r; |
| 475 |
|
| 476 |
r = file_of_uid(uid, &p); |
| 477 |
if (r < 0) |
| 478 |
return r; |
| 479 |
|
| 480 |
r = parse_env_file(/* f= */ NULL, p, "DISPLAY", &s); |
| 481 |
if (r == -ENOENT) |
| 482 |
return -ENODATA; |
| 483 |
if (r < 0) |
| 484 |
return r; |
| 485 |
if (isempty(s)) |
| 486 |
return -ENODATA; |
| 487 |
|
| 488 |
if (ret_display) |
| 489 |
*ret_display = TAKE_PTR(s); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
_public_ int sd_uid_get_login_time(uid_t uid, uint64_t *ret_usec) { |
| 494 |
_cleanup_free_ char *p = NULL, *s = NULL, *rt = NULL; |
| 495 |
int r; |
| 496 |
|
| 497 |
r = file_of_uid(uid, &p); |
| 498 |
if (r < 0) |
| 499 |
return r; |
| 500 |
|
| 501 |
r = parse_env_file(/* f= */ NULL, p, "STATE", &s, "REALTIME", &rt); |
| 502 |
if (r == -ENOENT) |
| 503 |
return -ENXIO; |
| 504 |
if (r < 0) |
| 505 |
return r; |
| 506 |
if (isempty(s) || isempty(rt)) |
| 507 |
return -EIO; |
| 508 |
|
| 509 |
if (!STR_IN_SET(s, "active", "online")) |
| 510 |
return -ENXIO; |
| 511 |
|
| 512 |
return safe_atou64(rt, ret_usec); |
| 513 |
} |
| 514 |
|
| 515 |
static int file_of_seat(const char *seat, char **ret) { |
| 516 |
char *p; |
| 517 |
int r; |
| 518 |
|
| 519 |
assert(ret); |
| 520 |
|
| 521 |
if (seat) { |
| 522 |
if (!filename_is_valid(seat)) |
| 523 |
return -EINVAL; |
| 524 |
|
| 525 |
p = path_join("/run/systemd/seats", seat); |
| 526 |
} else { |
| 527 |
_cleanup_free_ char *buf = NULL; |
| 528 |
|
| 529 |
r = sd_session_get_seat(NULL, &buf); |
| 530 |
if (r < 0) |
| 531 |
return r; |
| 532 |
|
| 533 |
p = path_join("/run/systemd/seats", buf); |
| 534 |
} |
| 535 |
if (!p) |
| 536 |
return -ENOMEM; |
| 537 |
|
| 538 |
*ret = TAKE_PTR(p); |
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
_public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) { |
| 543 |
_cleanup_free_ char *filename = NULL, *content = NULL; |
| 544 |
int r; |
| 545 |
|
| 546 |
assert_return(uid_is_valid(uid), -EINVAL); |
| 547 |
|
| 548 |
r = file_of_seat(seat, &filename); |
| 549 |
if (r < 0) |
| 550 |
return r; |
| 551 |
|
| 552 |
r = parse_env_file(/* f= */ NULL, filename, |
| 553 |
require_active ? "ACTIVE_UID" : "UIDS", |
| 554 |
&content); |
| 555 |
if (r == -ENOENT) |
| 556 |
return 0; |
| 557 |
if (r < 0) |
| 558 |
return r; |
| 559 |
if (isempty(content)) |
| 560 |
return 0; |
| 561 |
|
| 562 |
return string_contains_word(content, NULL, FORMAT_UID(uid)); |
| 563 |
} |
| 564 |
|
| 565 |
static int uid_get_array(uid_t uid, const char *variable, char ***ret_array) { |
| 566 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 567 |
int r; |
| 568 |
|
| 569 |
assert(variable); |
| 570 |
|
| 571 |
r = file_of_uid(uid, &p); |
| 572 |
if (r < 0) |
| 573 |
return r; |
| 574 |
|
| 575 |
r = parse_env_file(/* f= */ NULL, p, variable, &s); |
| 576 |
if (r == -ENOENT || (r >= 0 && isempty(s))) { |
| 577 |
if (ret_array) |
| 578 |
*ret_array = NULL; |
| 579 |
return 0; |
| 580 |
} |
| 581 |
if (r < 0) |
| 582 |
return r; |
| 583 |
|
| 584 |
_cleanup_strv_free_ char **a = strv_split(s, NULL); |
| 585 |
if (!a) |
| 586 |
return -ENOMEM; |
| 587 |
|
| 588 |
strv_uniq(a); |
| 589 |
r = (int) strv_length(a); |
| 590 |
|
| 591 |
if (ret_array) |
| 592 |
*ret_array = TAKE_PTR(a); |
| 593 |
|
| 594 |
return r; |
| 595 |
} |
| 596 |
|
| 597 |
_public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***ret_sessions) { |
| 598 |
return uid_get_array( |
| 599 |
uid, |
| 600 |
require_active == 0 ? "ONLINE_SESSIONS" : |
| 601 |
require_active > 0 ? "ACTIVE_SESSIONS" : |
| 602 |
"SESSIONS", |
| 603 |
ret_sessions); |
| 604 |
} |
| 605 |
|
| 606 |
_public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***ret_seats) { |
| 607 |
return uid_get_array( |
| 608 |
uid, |
| 609 |
require_active == 0 ? "ONLINE_SEATS" : |
| 610 |
require_active > 0 ? "ACTIVE_SEATS" : |
| 611 |
"SEATS", |
| 612 |
ret_seats); |
| 613 |
} |
| 614 |
|
| 615 |
static int file_of_session(const char *session, char **ret) { |
| 616 |
char *p; |
| 617 |
int r; |
| 618 |
|
| 619 |
assert(ret); |
| 620 |
|
| 621 |
if (session) { |
| 622 |
if (!session_id_valid(session)) |
| 623 |
return -EINVAL; |
| 624 |
|
| 625 |
p = path_join("/run/systemd/sessions", session); |
| 626 |
} else { |
| 627 |
_cleanup_free_ char *buf = NULL; |
| 628 |
|
| 629 |
r = sd_pid_get_session(0, &buf); |
| 630 |
if (r < 0) |
| 631 |
return r; |
| 632 |
|
| 633 |
p = path_join("/run/systemd/sessions", buf); |
| 634 |
} |
| 635 |
if (!p) |
| 636 |
return -ENOMEM; |
| 637 |
|
| 638 |
*ret = p; |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
_public_ int sd_session_is_active(const char *session) { |
| 643 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 644 |
int r; |
| 645 |
|
| 646 |
r = file_of_session(session, &p); |
| 647 |
if (r < 0) |
| 648 |
return r; |
| 649 |
|
| 650 |
r = parse_env_file(/* f= */ NULL, p, "ACTIVE", &s); |
| 651 |
if (r == -ENOENT) |
| 652 |
return -ENXIO; |
| 653 |
if (r < 0) |
| 654 |
return r; |
| 655 |
if (isempty(s)) |
| 656 |
return -EIO; |
| 657 |
|
| 658 |
return parse_boolean(s); |
| 659 |
} |
| 660 |
|
| 661 |
_public_ int sd_session_is_remote(const char *session) { |
| 662 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 663 |
int r; |
| 664 |
|
| 665 |
r = file_of_session(session, &p); |
| 666 |
if (r < 0) |
| 667 |
return r; |
| 668 |
|
| 669 |
r = parse_env_file(/* f= */ NULL, p, "REMOTE", &s); |
| 670 |
if (r == -ENOENT) |
| 671 |
return -ENXIO; |
| 672 |
if (r < 0) |
| 673 |
return r; |
| 674 |
if (isempty(s)) |
| 675 |
return -ENODATA; |
| 676 |
|
| 677 |
return parse_boolean(s); |
| 678 |
} |
| 679 |
|
| 680 |
_public_ int sd_session_get_extra_device_access(const char *session, char ***ret_ids) { |
| 681 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 682 |
int r; |
| 683 |
|
| 684 |
r = file_of_session(session, &p); |
| 685 |
if (r < 0) |
| 686 |
return r; |
| 687 |
|
| 688 |
r = parse_env_file(/* f= */ NULL, p, "EXTRA_DEVICE_ACCESS", &s); |
| 689 |
if (r == -ENOENT) |
| 690 |
return -ENXIO; |
| 691 |
if (r < 0) |
| 692 |
return r; |
| 693 |
|
| 694 |
_cleanup_strv_free_ char **ids = NULL; |
| 695 |
size_t n_ids = 0; |
| 696 |
if (!isempty(s)) { |
| 697 |
ids = strv_split(s, /* separators= */ NULL); |
| 698 |
if (!ids) |
| 699 |
return -ENOMEM; |
| 700 |
|
| 701 |
n_ids = strv_length(ids); |
| 702 |
} |
| 703 |
|
| 704 |
if (ret_ids) |
| 705 |
*ret_ids = TAKE_PTR(ids); |
| 706 |
|
| 707 |
return n_ids; |
| 708 |
} |
| 709 |
|
| 710 |
_public_ int sd_session_get_state(const char *session, char **ret_state) { |
| 711 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 712 |
int r; |
| 713 |
|
| 714 |
r = file_of_session(session, &p); |
| 715 |
if (r < 0) |
| 716 |
return r; |
| 717 |
|
| 718 |
r = parse_env_file(/* f= */ NULL, p, "STATE", &s); |
| 719 |
if (r == -ENOENT) |
| 720 |
return -ENXIO; |
| 721 |
if (r < 0) |
| 722 |
return r; |
| 723 |
if (isempty(s)) |
| 724 |
return -EIO; |
| 725 |
|
| 726 |
if (ret_state) |
| 727 |
*ret_state = TAKE_PTR(s); |
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
_public_ int sd_session_get_uid(const char *session, uid_t *ret_uid) { |
| 732 |
int r; |
| 733 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 734 |
|
| 735 |
r = file_of_session(session, &p); |
| 736 |
if (r < 0) |
| 737 |
return r; |
| 738 |
|
| 739 |
r = parse_env_file(/* f= */ NULL, p, "UID", &s); |
| 740 |
if (r == -ENOENT) |
| 741 |
return -ENXIO; |
| 742 |
if (r < 0) |
| 743 |
return r; |
| 744 |
if (isempty(s)) |
| 745 |
return -EIO; |
| 746 |
|
| 747 |
return parse_uid(s, ret_uid); |
| 748 |
} |
| 749 |
|
| 750 |
static int session_get_string(const char *session, const char *field, char **ret_value) { |
| 751 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 752 |
int r; |
| 753 |
|
| 754 |
assert(field); |
| 755 |
|
| 756 |
r = file_of_session(session, &p); |
| 757 |
if (r < 0) |
| 758 |
return r; |
| 759 |
|
| 760 |
r = parse_env_file(/* f= */ NULL, p, field, &s); |
| 761 |
if (r == -ENOENT) |
| 762 |
return -ENXIO; |
| 763 |
if (r < 0) |
| 764 |
return r; |
| 765 |
if (isempty(s)) |
| 766 |
return -ENODATA; |
| 767 |
|
| 768 |
if (ret_value) |
| 769 |
*ret_value = TAKE_PTR(s); |
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
_public_ int sd_session_get_username(const char *session, char **ret_username) { |
| 774 |
return session_get_string(session, "USER", ret_username); |
| 775 |
} |
| 776 |
|
| 777 |
_public_ int sd_session_get_seat(const char *session, char **ret_seat) { |
| 778 |
return session_get_string(session, "SEAT", ret_seat); |
| 779 |
} |
| 780 |
|
| 781 |
_public_ int sd_session_get_start_time(const char *session, uint64_t *ret_usec) { |
| 782 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 783 |
int r; |
| 784 |
|
| 785 |
r = file_of_session(session, &p); |
| 786 |
if (r < 0) |
| 787 |
return r; |
| 788 |
|
| 789 |
r = parse_env_file(/* f= */ NULL, p, "REALTIME", &s); |
| 790 |
if (r == -ENOENT) |
| 791 |
return -ENXIO; |
| 792 |
if (r < 0) |
| 793 |
return r; |
| 794 |
if (isempty(s)) |
| 795 |
return -EIO; |
| 796 |
|
| 797 |
return safe_atou64(s, ret_usec); |
| 798 |
} |
| 799 |
|
| 800 |
_public_ int sd_session_get_tty(const char *session, char **ret_tty) { |
| 801 |
return session_get_string(session, "TTY", ret_tty); |
| 802 |
} |
| 803 |
|
| 804 |
_public_ int sd_session_get_vt(const char *session, unsigned *ret_vtnr) { |
| 805 |
_cleanup_free_ char *vtnr_string = NULL; |
| 806 |
int r; |
| 807 |
|
| 808 |
r = session_get_string(session, "VTNR", &vtnr_string); |
| 809 |
if (r < 0) |
| 810 |
return r; |
| 811 |
|
| 812 |
return safe_atou(vtnr_string, ret_vtnr); |
| 813 |
} |
| 814 |
|
| 815 |
_public_ int sd_session_get_service(const char *session, char **ret_service) { |
| 816 |
return session_get_string(session, "SERVICE", ret_service); |
| 817 |
} |
| 818 |
|
| 819 |
_public_ int sd_session_get_type(const char *session, char **ret_type) { |
| 820 |
return session_get_string(session, "TYPE", ret_type); |
| 821 |
} |
| 822 |
|
| 823 |
_public_ int sd_session_get_class(const char *session, char **ret_clazz) { |
| 824 |
return session_get_string(session, "CLASS", ret_clazz); |
| 825 |
} |
| 826 |
|
| 827 |
_public_ int sd_session_get_desktop(const char *session, char **ret_desktop) { |
| 828 |
return session_get_string(session, "DESKTOP", ret_desktop); |
| 829 |
} |
| 830 |
|
| 831 |
_public_ int sd_session_get_display(const char *session, char **ret_display) { |
| 832 |
return session_get_string(session, "DISPLAY", ret_display); |
| 833 |
} |
| 834 |
|
| 835 |
_public_ int sd_session_get_remote_user(const char *session, char **ret_remote_user) { |
| 836 |
return session_get_string(session, "REMOTE_USER", ret_remote_user); |
| 837 |
} |
| 838 |
|
| 839 |
_public_ int sd_session_get_remote_host(const char *session, char **ret_remote_host) { |
| 840 |
return session_get_string(session, "REMOTE_HOST", ret_remote_host); |
| 841 |
} |
| 842 |
|
| 843 |
_public_ int sd_session_get_leader(const char *session, pid_t *ret_leader) { |
| 844 |
_cleanup_free_ char *leader_string = NULL; |
| 845 |
int r; |
| 846 |
|
| 847 |
r = session_get_string(session, "LEADER", &leader_string); |
| 848 |
if (r < 0) |
| 849 |
return r; |
| 850 |
|
| 851 |
return parse_pid(leader_string, ret_leader); |
| 852 |
} |
| 853 |
|
| 854 |
_public_ int sd_seat_get_active(const char *seat, char **ret_session, uid_t *ret_uid) { |
| 855 |
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL; |
| 856 |
int r; |
| 857 |
|
| 858 |
r = file_of_seat(seat, &p); |
| 859 |
if (r < 0) |
| 860 |
return r; |
| 861 |
|
| 862 |
r = parse_env_file(/* f= */ NULL, p, |
| 863 |
"ACTIVE", &s, |
| 864 |
"ACTIVE_UID", &t); |
| 865 |
if (r == -ENOENT) |
| 866 |
return -ENXIO; |
| 867 |
if (r < 0) |
| 868 |
return r; |
| 869 |
|
| 870 |
if (ret_session && !s) |
| 871 |
return -ENODATA; |
| 872 |
|
| 873 |
if (ret_uid && !t) |
| 874 |
return -ENODATA; |
| 875 |
|
| 876 |
if (ret_uid && t) { |
| 877 |
r = parse_uid(t, ret_uid); |
| 878 |
if (r < 0) |
| 879 |
return r; |
| 880 |
} |
| 881 |
|
| 882 |
if (ret_session && s) |
| 883 |
*ret_session = TAKE_PTR(s); |
| 884 |
|
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
_public_ int sd_seat_get_sessions( |
| 889 |
const char *seat, |
| 890 |
char ***ret_sessions, |
| 891 |
uid_t **ret_uids, |
| 892 |
unsigned *ret_n_uids) { |
| 893 |
|
| 894 |
_cleanup_free_ char *fname = NULL, *session_line = NULL, *uid_line = NULL; |
| 895 |
_cleanup_strv_free_ char **sessions = NULL; |
| 896 |
_cleanup_free_ uid_t *uids = NULL; |
| 897 |
unsigned n_sessions = 0; |
| 898 |
int r; |
| 899 |
|
| 900 |
r = file_of_seat(seat, &fname); |
| 901 |
if (r < 0) |
| 902 |
return r; |
| 903 |
|
| 904 |
r = parse_env_file(/* f= */ NULL, fname, |
| 905 |
"SESSIONS", &session_line, |
| 906 |
"UIDS", &uid_line); |
| 907 |
if (r == -ENOENT) |
| 908 |
return -ENXIO; |
| 909 |
if (r < 0) |
| 910 |
return r; |
| 911 |
|
| 912 |
if (session_line) { |
| 913 |
sessions = strv_split(session_line, NULL); |
| 914 |
if (!sessions) |
| 915 |
return -ENOMEM; |
| 916 |
|
| 917 |
n_sessions = strv_length(sessions); |
| 918 |
}; |
| 919 |
|
| 920 |
if (ret_uids && uid_line) { |
| 921 |
uids = new(uid_t, n_sessions); |
| 922 |
if (!uids) |
| 923 |
return -ENOMEM; |
| 924 |
|
| 925 |
size_t n = 0; |
| 926 |
for (const char *p = uid_line;;) { |
| 927 |
_cleanup_free_ char *word = NULL; |
| 928 |
|
| 929 |
r = extract_first_word(&p, &word, NULL, 0); |
| 930 |
if (r < 0) |
| 931 |
return r; |
| 932 |
if (r == 0) |
| 933 |
break; |
| 934 |
|
| 935 |
r = parse_uid(word, &uids[n++]); |
| 936 |
if (r < 0) |
| 937 |
return r; |
| 938 |
} |
| 939 |
|
| 940 |
if (n != n_sessions) |
| 941 |
return -EUCLEAN; |
| 942 |
} |
| 943 |
|
| 944 |
if (ret_sessions) |
| 945 |
*ret_sessions = TAKE_PTR(sessions); |
| 946 |
if (ret_uids) |
| 947 |
*ret_uids = TAKE_PTR(uids); |
| 948 |
if (ret_n_uids) |
| 949 |
*ret_n_uids = n_sessions; |
| 950 |
|
| 951 |
return n_sessions; |
| 952 |
} |
| 953 |
|
| 954 |
static int seat_get_can(const char *seat, const char *variable) { |
| 955 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 956 |
int r; |
| 957 |
|
| 958 |
assert(variable); |
| 959 |
|
| 960 |
r = file_of_seat(seat, &p); |
| 961 |
if (r < 0) |
| 962 |
return r; |
| 963 |
|
| 964 |
r = parse_env_file(/* f= */ NULL, p, |
| 965 |
variable, &s); |
| 966 |
if (r == -ENOENT) |
| 967 |
return -ENXIO; |
| 968 |
if (r < 0) |
| 969 |
return r; |
| 970 |
if (isempty(s)) |
| 971 |
return -ENODATA; |
| 972 |
|
| 973 |
return parse_boolean(s); |
| 974 |
} |
| 975 |
|
| 976 |
_public_ int sd_seat_can_multi_session(const char *seat) { |
| 977 |
return true; |
| 978 |
} |
| 979 |
|
| 980 |
_public_ int sd_seat_can_tty(const char *seat) { |
| 981 |
return seat_get_can(seat, "CAN_TTY"); |
| 982 |
} |
| 983 |
|
| 984 |
_public_ int sd_seat_can_graphical(const char *seat) { |
| 985 |
return seat_get_can(seat, "CAN_GRAPHICAL"); |
| 986 |
} |
| 987 |
|
| 988 |
_public_ int sd_get_seats(char ***ret_seats) { |
| 989 |
int r; |
| 990 |
|
| 991 |
r = get_files_in_directory("/run/systemd/seats/", ret_seats); |
| 992 |
if (r == -ENOENT) { |
| 993 |
if (ret_seats) |
| 994 |
*ret_seats = NULL; |
| 995 |
return 0; |
| 996 |
} |
| 997 |
return r; |
| 998 |
} |
| 999 |
|
| 1000 |
_public_ int sd_get_sessions(char ***ret_sessions) { |
| 1001 |
int r; |
| 1002 |
|
| 1003 |
r = get_files_in_directory("/run/systemd/sessions/", ret_sessions); |
| 1004 |
if (r == -ENOENT) { |
| 1005 |
if (ret_sessions) |
| 1006 |
*ret_sessions = NULL; |
| 1007 |
return 0; |
| 1008 |
} |
| 1009 |
return r; |
| 1010 |
} |
| 1011 |
|
| 1012 |
_public_ int sd_get_uids(uid_t **ret_users) { |
| 1013 |
_cleanup_closedir_ DIR *d = NULL; |
| 1014 |
_cleanup_free_ uid_t *l = NULL; |
| 1015 |
size_t n = 0; |
| 1016 |
|
| 1017 |
d = opendir("/run/systemd/users/"); |
| 1018 |
if (!d) { |
| 1019 |
if (errno == ENOENT) { |
| 1020 |
if (ret_users) |
| 1021 |
*ret_users = NULL; |
| 1022 |
return 0; |
| 1023 |
} |
| 1024 |
return -errno; |
| 1025 |
} |
| 1026 |
|
| 1027 |
FOREACH_DIRENT_ALL(de, d, return -errno) { |
| 1028 |
uid_t uid; |
| 1029 |
|
| 1030 |
if (!dirent_is_file(de)) |
| 1031 |
continue; |
| 1032 |
|
| 1033 |
if (parse_uid(de->d_name, &uid) < 0) |
| 1034 |
continue; |
| 1035 |
|
| 1036 |
if (ret_users) { |
| 1037 |
if (!GREEDY_REALLOC(l, n + 1)) |
| 1038 |
return -ENOMEM; |
| 1039 |
|
| 1040 |
l[n] = uid; |
| 1041 |
} |
| 1042 |
|
| 1043 |
n++; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (n > INT_MAX) |
| 1047 |
return -EOVERFLOW; |
| 1048 |
|
| 1049 |
if (ret_users) |
| 1050 |
*ret_users = TAKE_PTR(l); |
| 1051 |
|
| 1052 |
return (int) n; |
| 1053 |
} |
| 1054 |
|
| 1055 |
_public_ int sd_get_machine_names(char ***ret_machines) { |
| 1056 |
_cleanup_strv_free_ char **l = NULL; |
| 1057 |
char **a, **b; |
| 1058 |
int r; |
| 1059 |
|
| 1060 |
r = get_files_in_directory("/run/systemd/machines/", &l); |
| 1061 |
if (r == -ENOENT) { |
| 1062 |
if (ret_machines) |
| 1063 |
*ret_machines = NULL; |
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
if (r < 0) |
| 1067 |
return r; |
| 1068 |
|
| 1069 |
if (l) { |
| 1070 |
r = 0; |
| 1071 |
|
| 1072 |
/* Filter out the unit: symlinks */ |
| 1073 |
for (a = b = l; *a; a++) { |
| 1074 |
if (startswith(*a, "unit:") || !hostname_is_valid(*a, 0)) |
| 1075 |
free(*a); |
| 1076 |
else { |
| 1077 |
*b = *a; |
| 1078 |
b++; |
| 1079 |
r++; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
*b = NULL; |
| 1084 |
} |
| 1085 |
|
| 1086 |
if (ret_machines) |
| 1087 |
*ret_machines = TAKE_PTR(l); |
| 1088 |
|
| 1089 |
return r; |
| 1090 |
} |
| 1091 |
|
| 1092 |
_public_ int sd_machine_get_class(const char *machine, char **ret_clazz) { |
| 1093 |
_cleanup_free_ char *c = NULL; |
| 1094 |
int r; |
| 1095 |
|
| 1096 |
if (streq_ptr(machine, ".host")) { |
| 1097 |
c = strdup("host"); |
| 1098 |
if (!c) |
| 1099 |
return -ENOMEM; |
| 1100 |
} else { |
| 1101 |
if (!hostname_is_valid(machine, 0)) |
| 1102 |
return -EINVAL; |
| 1103 |
|
| 1104 |
_cleanup_free_ char *p = path_join("/run/systemd/machines/", machine); |
| 1105 |
if (!p) |
| 1106 |
return -ENOMEM; |
| 1107 |
|
| 1108 |
r = parse_env_file(/* f= */ NULL, p, "CLASS", &c); |
| 1109 |
if (r == -ENOENT) |
| 1110 |
return -ENXIO; |
| 1111 |
if (r < 0) |
| 1112 |
return r; |
| 1113 |
if (!c) |
| 1114 |
return -EIO; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (ret_clazz) |
| 1118 |
*ret_clazz = TAKE_PTR(c); |
| 1119 |
|
| 1120 |
return 0; |
| 1121 |
} |
| 1122 |
|
| 1123 |
_public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) { |
| 1124 |
_cleanup_free_ char *netif_line = NULL, *p = NULL; |
| 1125 |
int r; |
| 1126 |
|
| 1127 |
assert_return(hostname_is_valid(machine, 0), -EINVAL); |
| 1128 |
|
| 1129 |
p = path_join("/run/systemd/machines/", machine); |
| 1130 |
if (!p) |
| 1131 |
return -ENOMEM; |
| 1132 |
|
| 1133 |
r = parse_env_file(/* f= */ NULL, p, "NETIF", &netif_line); |
| 1134 |
if (r == -ENOENT) |
| 1135 |
return -ENXIO; |
| 1136 |
if (r < 0) |
| 1137 |
return r; |
| 1138 |
if (!netif_line) { |
| 1139 |
if (ret_ifindices) |
| 1140 |
*ret_ifindices = NULL; |
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
|
| 1144 |
_cleanup_strv_free_ char **tt = strv_split(netif_line, NULL); |
| 1145 |
if (!tt) |
| 1146 |
return -ENOMEM; |
| 1147 |
|
| 1148 |
_cleanup_free_ int *ifindices = NULL; |
| 1149 |
if (ret_ifindices) { |
| 1150 |
ifindices = new(int, strv_length(tt)); |
| 1151 |
if (!ifindices) |
| 1152 |
return -ENOMEM; |
| 1153 |
} |
| 1154 |
|
| 1155 |
size_t n = 0; |
| 1156 |
for (size_t i = 0; tt[i]; i++) { |
| 1157 |
int ind; |
| 1158 |
|
| 1159 |
ind = parse_ifindex(tt[i]); |
| 1160 |
if (ind < 0) |
| 1161 |
/* Return -EUCLEAN to distinguish from -EINVAL for invalid args */ |
| 1162 |
return ind == -EINVAL ? -EUCLEAN : ind; |
| 1163 |
|
| 1164 |
if (ret_ifindices) |
| 1165 |
ifindices[n] = ind; |
| 1166 |
n++; |
| 1167 |
} |
| 1168 |
|
| 1169 |
if (ret_ifindices) |
| 1170 |
*ret_ifindices = TAKE_PTR(ifindices); |
| 1171 |
|
| 1172 |
return n; |
| 1173 |
} |
| 1174 |
|
| 1175 |
static int MONITOR_TO_FD(sd_login_monitor *m) { |
| 1176 |
return (int) (unsigned long) m - 1; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static sd_login_monitor* FD_TO_MONITOR(int fd) { |
| 1180 |
return (sd_login_monitor*) (unsigned long) (fd + 1); |
| 1181 |
} |
| 1182 |
|
| 1183 |
_public_ int sd_login_monitor_new(const char *category, sd_login_monitor **ret) { |
| 1184 |
_cleanup_close_ int fd = -EBADF; |
| 1185 |
|
| 1186 |
assert_return(ret, -EINVAL); |
| 1187 |
|
| 1188 |
fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
| 1189 |
if (fd < 0) |
| 1190 |
return -errno; |
| 1191 |
|
| 1192 |
static const struct { |
| 1193 |
const char *name; |
| 1194 |
const char *path; |
| 1195 |
} categories[] = { |
| 1196 |
{ "seat", "/run/systemd/seats/" }, |
| 1197 |
{ "session", "/run/systemd/sessions/" }, |
| 1198 |
{ "uid", "/run/systemd/users/" }, |
| 1199 |
{ "machine", "/run/systemd/machines/" }, |
| 1200 |
}; |
| 1201 |
|
| 1202 |
bool good = false; |
| 1203 |
FOREACH_ELEMENT(c, categories) { |
| 1204 |
if (category && !streq(category, c->name)) |
| 1205 |
continue; |
| 1206 |
|
| 1207 |
if (inotify_add_watch(fd, c->path, IN_MOVED_TO|IN_DELETE) < 0) |
| 1208 |
return -errno; |
| 1209 |
|
| 1210 |
good = true; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (!good) |
| 1214 |
return -EINVAL; |
| 1215 |
|
| 1216 |
*ret = FD_TO_MONITOR(TAKE_FD(fd)); |
| 1217 |
return 0; |
| 1218 |
} |
| 1219 |
|
| 1220 |
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) { |
| 1221 |
if (m) |
| 1222 |
(void) close(MONITOR_TO_FD(m)); |
| 1223 |
|
| 1224 |
return NULL; |
| 1225 |
} |
| 1226 |
|
| 1227 |
_public_ int sd_login_monitor_flush(sd_login_monitor *m) { |
| 1228 |
int r; |
| 1229 |
|
| 1230 |
assert_return(m, -EINVAL); |
| 1231 |
|
| 1232 |
r = flush_fd(MONITOR_TO_FD(m)); |
| 1233 |
if (r < 0) |
| 1234 |
return r; |
| 1235 |
|
| 1236 |
return 0; |
| 1237 |
} |
| 1238 |
|
| 1239 |
_public_ int sd_login_monitor_get_fd(sd_login_monitor *m) { |
| 1240 |
|
| 1241 |
assert_return(m, -EINVAL); |
| 1242 |
|
| 1243 |
return MONITOR_TO_FD(m); |
| 1244 |
} |
| 1245 |
|
| 1246 |
_public_ int sd_login_monitor_get_events(sd_login_monitor *m) { |
| 1247 |
|
| 1248 |
assert_return(m, -EINVAL); |
| 1249 |
|
| 1250 |
/* For now we will only return POLLIN here, since we don't |
| 1251 |
* need anything else ever for inotify. However, let's have |
| 1252 |
* this API to keep our options open should we later on need |
| 1253 |
* it. */ |
| 1254 |
return POLLIN; |
| 1255 |
} |
| 1256 |
|
| 1257 |
_public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *ret_timeout_usec) { |
| 1258 |
|
| 1259 |
assert_return(m, -EINVAL); |
| 1260 |
assert_return(ret_timeout_usec, -EINVAL); |
| 1261 |
|
| 1262 |
/* For now we will only return UINT64_MAX, since we don't |
| 1263 |
* need any timeout. However, let's have this API to keep our |
| 1264 |
* options open should we later on need it. */ |
| 1265 |
*ret_timeout_usec = UINT64_MAX; |
| 1266 |
return 0; |
| 1267 |
} |