-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <unistd.h>
-#include <string.h>
#include <errno.h>
-#include <sys/inotify.h>
#include <poll.h>
+#include <string.h>
+#include <sys/inotify.h>
+#include <unistd.h>
-#include "util.h"
+#include "sd-login.h"
+
+#include "alloc-util.h"
#include "cgroup-util.h"
-#include "macro.h"
-#include "strv.h"
+#include "dirent-util.h"
+#include "escape.h"
+#include "fd-util.h"
#include "fileio.h"
-#include "login-util.h"
-#include "formats-util.h"
+#include "format-util.h"
+#include "fs-util.h"
#include "hostname-util.h"
-#include "sd-login.h"
+#include "io-util.h"
+#include "login-util.h"
+#include "macro.h"
+#include "parse-util.h"
+#include "path-util.h"
+#include "socket-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "user-util.h"
+#include "util.h"
/* Error codes:
*
*/
_public_ int sd_pid_get_session(pid_t pid, char **session) {
+ int r;
assert_return(pid >= 0, -EINVAL);
assert_return(session, -EINVAL);
- return cg_pid_get_session(pid, session);
+ r = cg_pid_get_session(pid, session);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
}
_public_ int sd_pid_get_unit(pid_t pid, char **unit) {
+#if 0 /// UNNEEDED by elogind
+ int r;
+#endif // 0
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
-/// elogind does not support systemd units
-#if 0
- return cg_pid_get_unit(pid, unit);
+#if 0 /// elogind does not support systemd units
+ r = cg_pid_get_unit(pid, unit);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
#else
return -ESRCH;
#endif // 0
}
_public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
+#if 0 /// UNNEEDED by elogind
+ int r;
+#endif // 0
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
-/// elogind does not support systemd units
-#if 0
- return cg_pid_get_user_unit(pid, unit);
+#if 0 /// elogind does not support systemd units
+ r = cg_pid_get_user_unit(pid, unit);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
#else
return -ESRCH;
#endif // 0
}
_public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
+#if 0 /// UNNEEDED by elogind
+ int r;
+#endif // 0
assert_return(pid >= 0, -EINVAL);
assert_return(name, -EINVAL);
-/// elogind does not support systemd units
-#if 0
- return cg_pid_get_machine_name(pid, name);
+#if 0 /// elogind does not support systemd units
+ r = cg_pid_get_machine_name(pid, name);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
#else
return -ESRCH;
#endif // 0
}
_public_ int sd_pid_get_slice(pid_t pid, char **slice) {
+ int r;
assert_return(pid >= 0, -EINVAL);
assert_return(slice, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
- return cg_pid_get_slice(pid, slice);
-#else
- return -ESRCH;
-#endif // 0
+ r = cg_pid_get_slice(pid, slice);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
}
_public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
+ int r;
assert_return(pid >= 0, -EINVAL);
assert_return(slice, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
- return cg_pid_get_user_slice(pid, slice);
-#else
- return -ESRCH;
-#endif // 0
+ r = cg_pid_get_user_slice(pid, slice);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
}
_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
+ int r;
assert_return(pid >= 0, -EINVAL);
assert_return(uid, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
- return cg_pid_get_owner_uid(pid, uid);
-#else
- return -ESRCH;
-#endif // 0
+ r = cg_pid_get_owner_uid(pid, uid);
+ return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
}
_public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
/* The internal APIs return the empty string for the root
* cgroup, let's return the "/" in the public APIs instead, as
- * that's easier and less ambigious for people to grok. */
+ * that's easier and less ambiguous for people to grok. */
if (isempty(c)) {
free(c);
c = strdup("/");
if (r < 0)
return r;
-/// elogind does not support systemd scopes
-#if 0
return cg_pid_get_session(ucred.pid, session);
-#else
- return -ESRCH;
-#endif // 0
}
_public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
return cg_pid_get_owner_uid(ucred.pid, uid);
-#else
- return -ESRCH;
-#endif // 0
}
_public_ int sd_peer_get_unit(int fd, char **unit) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_unit(ucred.pid, unit);
#else
return -ESRCH;
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_user_unit(ucred.pid, unit);
#else
return -ESRCH;
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_machine_name(ucred.pid, machine);
#else
return -ESRCH;
if (r < 0)
return r;
-/// elogind does not support systemd slices
-#if 0
return cg_pid_get_slice(ucred.pid, slice);
-#else
- return -ESRCH;
-#endif // 0
}
_public_ int sd_peer_get_user_slice(int fd, char **slice) {
if (r < 0)
return r;
-/// elogind does not support systemd slices
-#if 0
return cg_pid_get_user_slice(ucred.pid, slice);
-#else
- return -ESRCH;
-#endif // 0
}
_public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
if (r == -ENOENT) {
free(s);
s = strdup("offline");
return -ENOMEM;
}
- if (r < 0) {
+ else if (r < 0) {
free(s);
return r;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "DISPLAY", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
if (isempty(s))
return -ENODATA;
- *session = s;
- s = NULL;
+ *session = TAKE_PTR(s);
return 0;
}
if (!p)
return -ENOMEM;
- *_p = p;
- p = NULL;
+ *_p = TAKE_PTR(p);
return 0;
}
variable = require_active ? "ACTIVE_UID" : "UIDS";
- r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
if (r == -ENOENT)
return 0;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
if (r == -ENOENT || (r >= 0 && isempty(s))) {
if (array)
*array = NULL;
return -ENOMEM;
strv_uniq(a);
- r = strv_length(a);
+ r = (int) strv_length(a);
if (array)
*array = a;
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "ACTIVE", &s, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "REMOTE", &s, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (isempty(s))
return -EIO;
- *state = s;
- s = NULL;
+ *state = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "UID", &s, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, field, &s, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, field, &s, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (isempty(s))
return -ENODATA;
- *value = s;
- s = NULL;
+ *value = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
+ r = parse_env_file(NULL, p, NEWLINE,
"ACTIVE", &s,
"ACTIVE_UID", &t,
NULL);
return r;
}
- if (session && s) {
- *session = s;
- s = NULL;
- }
+ if (session && s)
+ *session = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
+ r = parse_env_file(NULL, p, NEWLINE,
"SESSIONS", &s,
- "ACTIVE_SESSIONS", &t,
+ "UIDS", &t,
NULL);
if (r == -ENOENT)
return -ENXIO;
r = parse_uid(k, b + i);
if (r < 0)
- continue;
+ return r;
i++;
}
}
}
- r = strv_length(a);
+ r = (int) strv_length(a);
- if (sessions) {
- *sessions = a;
- a = NULL;
- }
+ if (sessions)
+ *sessions = TAKE_PTR(a);
- if (uids) {
- *uids = b;
- b = NULL;
- }
+ if (uids)
+ *uids = TAKE_PTR(b);
if (n_uids)
*n_uids = n;
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
+ r = parse_env_file(NULL, p, NEWLINE,
variable, &s,
NULL);
if (r == -ENOENT)
}
_public_ int sd_get_seats(char ***seats) {
- return get_files_in_directory("/run/systemd/seats/", seats);
+ int r;
+
+ r = get_files_in_directory("/run/systemd/seats/", seats);
+ if (r == -ENOENT) {
+ if (seats)
+ *seats = NULL;
+ return 0;
+ }
+ return r;
}
_public_ int sd_get_sessions(char ***sessions) {
- return get_files_in_directory("/run/systemd/sessions/", sessions);
+ int r;
+
+ r = get_files_in_directory("/run/systemd/sessions/", sessions);
+ if (r == -ENOENT) {
+ if (sessions)
+ *sessions = NULL;
+ return 0;
+ }
+ return r;
}
_public_ int sd_get_uids(uid_t **users) {
_cleanup_closedir_ DIR *d;
+ struct dirent *de;
int r = 0;
unsigned n = 0;
_cleanup_free_ uid_t *l = NULL;
d = opendir("/run/systemd/users/");
- if (!d)
+ if (!d) {
+ if (errno == ENOENT) {
+ if (users)
+ *users = NULL;
+ return 0;
+ }
return -errno;
+ }
- for (;;) {
- struct dirent *de;
+ FOREACH_DIRENT_ALL(de, d, return -errno) {
int k;
uid_t uid;
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
-
- if (!de)
- break;
-
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
r++;
}
- if (users) {
- *users = l;
- l = NULL;
- }
+ if (users)
+ *users = TAKE_PTR(l);
return r;
}
_public_ int sd_get_machine_names(char ***machines) {
- char **l = NULL, **a, **b;
+ _cleanup_strv_free_ char **l = NULL;
+ char **a, **b;
int r;
- assert_return(machines, -EINVAL);
-
r = get_files_in_directory("/run/systemd/machines/", &l);
+ if (r == -ENOENT) {
+ if (machines)
+ *machines = NULL;
+ return 0;
+ }
if (r < 0)
return r;
r = 0;
/* Filter out the unit: symlinks */
- for (a = l, b = l; *a; a++) {
+ for (a = b = l; *a; a++) {
if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
free(*a);
else {
*b = NULL;
}
- *machines = l;
+ if (machines)
+ *machines = TAKE_PTR(l);
+
return r;
}
assert_return(class, -EINVAL);
p = strjoina("/run/systemd/machines/", machine);
- r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "CLASS", &c, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (!c)
return -EIO;
- *class = c;
- c = NULL;
+ *class = TAKE_PTR(c);
return 0;
}
assert_return(ifindices, -EINVAL);
p = strjoina("/run/systemd/machines/", machine);
- r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
+ r = parse_env_file(NULL, p, NEWLINE, "NETIF", &netif, NULL);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
*(char*) (mempcpy(buf, word, l)) = 0;
- if (safe_atoi(buf, &ifi) < 0)
- continue;
- if (ifi <= 0)
+ if (parse_ifindex(buf, &ifi) < 0)
continue;
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
}
_public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
- int fd, k;
+ _cleanup_close_ int fd = -1;
bool good = false;
+ int k;
assert_return(m, -EINVAL);
if (!category || streq(category, "seat")) {
k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
- if (k < 0) {
- safe_close(fd);
+ if (k < 0)
return -errno;
- }
good = true;
}
if (!category || streq(category, "session")) {
k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
- if (k < 0) {
- safe_close(fd);
+ if (k < 0)
return -errno;
- }
good = true;
}
if (!category || streq(category, "uid")) {
k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
- if (k < 0) {
- safe_close(fd);
+ if (k < 0)
return -errno;
- }
good = true;
}
if (!category || streq(category, "machine")) {
k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
- if (k < 0) {
- safe_close(fd);
+ if (k < 0)
return -errno;
- }
good = true;
}
- if (!good) {
- close_nointr(fd);
+ if (!good)
return -EINVAL;
- }
*m = FD_TO_MONITOR(fd);
+ fd = -1;
+
return 0;
}
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
int fd;
- assert_return(m, NULL);
+ if (!m)
+ return NULL;
fd = MONITOR_TO_FD(m);
close_nointr(fd);
}
_public_ int sd_login_monitor_flush(sd_login_monitor *m) {
+ int r;
assert_return(m, -EINVAL);
- return flush_fd(MONITOR_TO_FD(m));
+ r = flush_fd(MONITOR_TO_FD(m));
+ if (r < 0)
+ return r;
+
+ return 0;
}
_public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {