HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-login/sd-login.c
Git blob: 1331190c2e3b91cd459fd9f1223f8692d9c30414
Lines 1001–1268 of 1,268
int r;
r = get_files_in_directory("/run/systemd/sessions/", ret_sessions);
if (r == -ENOENT) {
if (ret_sessions)
*ret_sessions = NULL;
return 0;
}
return r;
}
_public_ int sd_get_uids(uid_t **ret_users) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ uid_t *l = NULL;
size_t n = 0;
d = opendir("/run/systemd/users/");
if (!d) {
if (errno == ENOENT) {
if (ret_users)
*ret_users = NULL;
return 0;
}
return -errno;
}
FOREACH_DIRENT_ALL(de, d, return -errno) {
uid_t uid;
if (!dirent_is_file(de))
continue;
if (parse_uid(de->d_name, &uid) < 0)
continue;
if (ret_users) {
if (!GREEDY_REALLOC(l, n + 1))
return -ENOMEM;
l[n] = uid;
}
n++;
}
if (n > INT_MAX)
return -EOVERFLOW;
if (ret_users)
*ret_users = TAKE_PTR(l);
return (int) n;
}
_public_ int sd_get_machine_names(char ***ret_machines) {
_cleanup_strv_free_ char **l = NULL;
char **a, **b;
int r;
r = get_files_in_directory("/run/systemd/machines/", &l);
if (r == -ENOENT) {
if (ret_machines)
*ret_machines = NULL;
return 0;
}
if (r < 0)
return r;
if (l) {
r = 0;
/* Filter out the unit: symlinks */
for (a = b = l; *a; a++) {
if (startswith(*a, "unit:") || !hostname_is_valid(*a, 0))
free(*a);
else {
*b = *a;
b++;
r++;
}
}
*b = NULL;
}
if (ret_machines)
*ret_machines = TAKE_PTR(l);
return r;
}
_public_ int sd_machine_get_class(const char *machine, char **ret_clazz) {
_cleanup_free_ char *c = NULL;
int r;
if (streq_ptr(machine, ".host")) {
c = strdup("host");
if (!c)
return -ENOMEM;
} else {
if (!hostname_is_valid(machine, 0))
return -EINVAL;
_cleanup_free_ char *p = path_join("/run/systemd/machines/", machine);
if (!p)
return -ENOMEM;
r = parse_env_file(/* f= */ NULL, p, "CLASS", &c);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (!c)
return -EIO;
}
if (ret_clazz)
*ret_clazz = TAKE_PTR(c);
return 0;
}
_public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) {
_cleanup_free_ char *netif_line = NULL, *p = NULL;
int r;
assert_return(hostname_is_valid(machine, 0), -EINVAL);
p = path_join("/run/systemd/machines/", machine);
if (!p)
return -ENOMEM;
r = parse_env_file(/* f= */ NULL, p, "NETIF", &netif_line);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (!netif_line) {
if (ret_ifindices)
*ret_ifindices = NULL;
return 0;
}
_cleanup_strv_free_ char **tt = strv_split(netif_line, NULL);
if (!tt)
return -ENOMEM;
_cleanup_free_ int *ifindices = NULL;
if (ret_ifindices) {
ifindices = new(int, strv_length(tt));
if (!ifindices)
return -ENOMEM;
}
size_t n = 0;
for (size_t i = 0; tt[i]; i++) {
int ind;
ind = parse_ifindex(tt[i]);
if (ind < 0)
/* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
return ind == -EINVAL ? -EUCLEAN : ind;
if (ret_ifindices)
ifindices[n] = ind;
n++;
}
if (ret_ifindices)
*ret_ifindices = TAKE_PTR(ifindices);
return n;
}
static int MONITOR_TO_FD(sd_login_monitor *m) {
return (int) (unsigned long) m - 1;
}
static sd_login_monitor* FD_TO_MONITOR(int fd) {
return (sd_login_monitor*) (unsigned long) (fd + 1);
}
_public_ int sd_login_monitor_new(const char *category, sd_login_monitor **ret) {
_cleanup_close_ int fd = -EBADF;
assert_return(ret, -EINVAL);
fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (fd < 0)
return -errno;
static const struct {
const char *name;
const char *path;
} categories[] = {
{ "seat", "/run/systemd/seats/" },
{ "session", "/run/systemd/sessions/" },
{ "uid", "/run/systemd/users/" },
{ "machine", "/run/systemd/machines/" },
};
bool good = false;
FOREACH_ELEMENT(c, categories) {
if (category && !streq(category, c->name))
continue;
if (inotify_add_watch(fd, c->path, IN_MOVED_TO|IN_DELETE) < 0)
return -errno;
good = true;
}
if (!good)
return -EINVAL;
*ret = FD_TO_MONITOR(TAKE_FD(fd));
return 0;
}
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
if (m)
(void) close(MONITOR_TO_FD(m));
return NULL;
}
_public_ int sd_login_monitor_flush(sd_login_monitor *m) {
int r;
assert_return(m, -EINVAL);
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) {
assert_return(m, -EINVAL);
return MONITOR_TO_FD(m);
}
_public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
assert_return(m, -EINVAL);
/* For now we will only return POLLIN here, since we don't
* need anything else ever for inotify. However, let's have
* this API to keep our options open should we later on need
* it. */
return POLLIN;
}
_public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *ret_timeout_usec) {
assert_return(m, -EINVAL);
assert_return(ret_timeout_usec, -EINVAL);
/* For now we will only return UINT64_MAX, since we don't
* need any timeout. However, let's have this API to keep our
* options open should we later on need it. */
*ret_timeout_usec = UINT64_MAX;
return 0;
}