HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/login/logind-dbus.c

180,924 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 38cb6ce3dd8e7c377cf4004e698626c8561e1006

Lines 1–1000 of 4,785

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
 
#include "sd-bus.h"
#include "sd-device.h"
#include "sd-messages.h"
 
#include "alloc-util.h"
#include "audit-util.h"
#include "bitfield.h"
#include "bootspec.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-get-properties.h"
#include "bus-locator.h"
#include "bus-object.h"
#include "bus-polkit.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "device-util.h"
#include "dirent-util.h"
#include "efi-api.h"
#include "efi-loader.h"
#include "efivars.h"
#include "env-file.h"
#include "env-util.h"
#include "errno-util.h"
#include "escape.h"
#include "event-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "login-util.h"
#include "logind-session.h"
#include "logind.h"
#include "logind-action.h"
#include "logind-dbus.h"
#include "logind-polkit.h"
#include "logind-seat.h"
#include "logind-seat-dbus.h"
#include "logind-session-dbus.h"
#include "logind-shutdown.h"
#include "logind-user.h"
#include "logind-user-dbus.h"
#include "logind-utmp.h"
#include "mkdir.h"
#include "os-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "reboot-util.h"
#include "serialize.h"
#include "signal-util.h"
#include "sleep-config.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "tmpfile-util.h"
#include "unit-def.h"
#include "user-record.h"
#include "user-util.h"
#include "virt.h"
#include "wall.h"
 
/* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
 * character limit for the wall message.
 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
 * There is no real technical need for that but doesn't make sense
 * to store arbitrary amounts either. As we are not stingy here, we
 * allow 4k.
 */
#define WALL_MESSAGE_MAX 4096U
 
static int get_sender_session(
                Manager *m,
                sd_bus_message *message,
                bool consult_display,
                sd_bus_error *error,
                Session **ret) {
 
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
        Session *session = NULL;
        const char *name;
        int r;
 
        assert(m);
        assert(ret);
 
        /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
         * and returns that. If not and 'consult_display' is true, this returns the display session of the
         * owning user of the caller. */
 
        r = sd_bus_query_sender_creds(message,
                                      SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT|
                                      (consult_display ? SD_BUS_CREDS_OWNER_UID : 0), &creds);
        if (r < 0)
                return r;
 
        r = sd_bus_creds_get_session(creds, &name);
        if (r < 0) {
                if (r != -ENXIO)
                        return r;
 
                if (consult_display) {
                        uid_t uid;
 
                        r = sd_bus_creds_get_owner_uid(creds, &uid);
                        if (r < 0) {
                                if (r != -ENXIO)
                                        return r;
                        } else {
                                User *user;
 
                                user = hashmap_get(m->users, UID_TO_PTR(uid));
                                if (user)
                                        session = user->display;
                        }
                }
        } else
                session = hashmap_get(m->sessions, name);
 
        if (!session)
                return sd_bus_error_set(error, BUS_ERROR_NO_SESSION_FOR_PID,
                                        consult_display ?
                                        "Caller does not belong to any known session and doesn't own any suitable session." :
                                        "Caller does not belong to any known session.");
 
        *ret = session;
        return 0;
}
 
int manager_get_session_from_creds(
                Manager *m,
                sd_bus_message *message,
                const char *name,
                sd_bus_error *error,
                Session **ret) {
 
        Session *session;
 
        assert(m);
        assert(ret);
 
        if (session_is_self(name)) /* the caller's own session */
                return get_sender_session(m, message, false, error, ret);
        if (session_is_auto(name)) /* The caller's own session if they have one, otherwise their user's display session */
                return get_sender_session(m, message, true, error, ret);
 
        session = hashmap_get(m->sessions, name);
        if (!session)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
 
        *ret = session;
        return 0;
}
 
static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
        uid_t uid;
        User *user;
        int r;
 
        assert(ret);
 
        /* Note that we get the owner UID of the session, not the actual client UID here! */
        r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
        if (r < 0)
                return r;
 
        r = sd_bus_creds_get_owner_uid(creds, &uid);
        if (r < 0) {
                if (r != -ENXIO)
                        return r;
 
                user = NULL;
        } else
                user = hashmap_get(m->users, UID_TO_PTR(uid));
 
        if (!user)
                return sd_bus_error_set(error, BUS_ERROR_NO_USER_FOR_PID,
                                        "Caller does not belong to any logged in or lingering user");
 
        *ret = user;
        return 0;
}
 
int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
        User *user;
 
        assert(m);
        assert(ret);
 
        if (!uid_is_valid(uid))
                return get_sender_user(m, message, error, ret);
 
        user = hashmap_get(m->users, UID_TO_PTR(uid));
        if (!user)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER,
                                         "User ID "UID_FMT" is not logged in or lingering", uid);
 
        *ret = user;
        return 0;
}
 
int manager_get_seat_from_creds(
                Manager *m,
                sd_bus_message *message,
                const char *name,
                sd_bus_error *error,
                Seat **ret) {
 
        Seat *seat;
        int r;
 
        assert(m);
        assert(ret);
 
        if (seat_is_self(name) || seat_is_auto(name)) {
                Session *session;
 
                /* Use these special seat names as session names */
                r = manager_get_session_from_creds(m, message, name, error, &session);
                if (r < 0)
                        return r;
 
                seat = session->seat;
                if (!seat)
                        return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session '%s' has no seat.", session->id);
        } else {
                seat = hashmap_get(m->seats, name);
                if (!seat)
                        return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
        }
 
        *ret = seat;
        return 0;
}
 
static int return_test_polkit(
                sd_bus_message *message,
                const char *action,
                const char **details,
                uid_t good_user,
                sd_bus_error *e) {
 
        const char *result;
        bool challenge;
        int r;
 
        r = bus_test_polkit(message, action, details, good_user, &challenge, e);
        if (r < 0)
                return r;
 
        if (r > 0)
                result = "yes";
        else if (challenge)
                result = "challenge";
        else
                result = "no";
 
        return sd_bus_reply_method_return(message, "s", result);
}
 
static int property_get_idle_hint(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, /* ret_timestamp= */ NULL));
}
 
static int property_get_idle_since_hint(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        dual_timestamp t;
 
        assert(bus);
        assert(reply);
 
        manager_get_idle_hint(m, &t);
 
        return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
}
 
static int property_get_inhibited(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        InhibitWhat w;
 
        assert(bus);
        assert(reply);
 
        if (streq(property, "BlockInhibited"))
                w = manager_inhibit_what(m, INHIBIT_BLOCK);
        else if (streq(property, "BlockWeakInhibited"))
                w = manager_inhibit_what(m, INHIBIT_BLOCK_WEAK);
        else if (streq(property, "DelayInhibited"))
                w = manager_inhibit_what(m, INHIBIT_DELAY);
        else
                assert_not_reached();
 
        return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
}
 
static int property_get_preparing(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        bool b = false;
 
        assert(bus);
        assert(reply);
 
        if (m->delayed_action) {
                if (streq(property, "PreparingForShutdown"))
                        b = m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN;
                else
                        b = m->delayed_action->inhibit_what & INHIBIT_SLEEP;
        }
 
        return sd_bus_message_append(reply, "b", b);
}
 
static int property_get_preparing_shutdown_with_metadata(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        if (!m->delayed_action || !(m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN))
                return sd_bus_message_append(reply, "a{sv}", 1, "preparing", "b", false);
 
        return sd_bus_message_append(
                        reply,
                        "a{sv}",
                        2,
                        "preparing",
                        "b",
                        true,
                        "type",
                        "s",
                        handle_action_to_string(m->delayed_action->handle));
}
 
static int property_get_sleep_operations(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_strv_free_ char **actions = NULL;
        int r;
 
        assert(bus);
        assert(reply);
 
        r = handle_action_get_enabled_sleep_actions(m->handle_action_sleep_mask, &actions);
        if (r < 0)
                return r;
 
        return sd_bus_message_append_strv(reply, actions);
}
 
static int property_get_scheduled_shutdown(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        int r;
 
        assert(bus);
        assert(reply);
 
        r = sd_bus_message_open_container(reply, 'r', "st");
        if (r < 0)
                return r;
 
        if (m->delayed_action) {
                usec_t t = m->scheduled_shutdown_timeout; /* fall back to the schedule time on failure below */
 
                if (m->inhibit_timeout_source) {
                        r = sd_event_source_get_time(m->inhibit_timeout_source, &t);
                        if (r < 0)
                                log_debug_errno(r, "Failed to get time of inhibit timeout event source, ignoring: %m");
                }
 
                r = sd_bus_message_append(
                                reply, "st",
                                handle_action_to_string(m->delayed_action->handle), t);
        } else
                r = sd_bus_message_append(
                                reply, "st",
                                handle_action_to_string(m->scheduled_shutdown_action),
                                m->scheduled_shutdown_timeout);
        if (r < 0)
                return r;
 
        return sd_bus_message_close_container(reply);
}
 
static int property_get_maintenance_time(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
 
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_free_ char *s = NULL;
        int r;
 
        assert(bus);
        assert(reply);
 
        if (m->maintenance_time) {
                r = calendar_spec_to_string(m->maintenance_time, &s);
                if (r < 0)
                        return log_error_errno(r, "Failed to format calendar specification: %m");
        }
 
        return sd_bus_message_append(reply, "s", s);
}
 
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power());
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
 
static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *p = NULL;
        Manager *m = ASSERT_PTR(userdata);
        const char *name;
        Session *session;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        r = manager_get_session_from_creds(m, message, name, error, &session);
        if (r < 0)
                return r;
 
        p = session_bus_path(session);
        if (!p)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", p);
}
 
/* Get login session of a process.  This is not what you are looking for these days,
 * as apps may instead belong to a user service unit.  This includes terminal
 * emulators and hence command-line apps. */
static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_free_ char *p = NULL;
        Session *session = NULL;
        pid_t pid;
        int r;
 
        assert(message);
 
        assert_cc(sizeof(pid_t) == sizeof(uint32_t));
 
        r = sd_bus_message_read(message, "u", &pid);
        if (r < 0)
                return r;
        if (pid < 0)
                return -EINVAL;
 
        if (pid == 0) {
                r = manager_get_session_from_creds(m, message, NULL, error, &session);
                if (r < 0)
                        return r;
        } else {
                r = manager_get_session_by_pidref(m, &PIDREF_MAKE_FROM_PID(pid), &session);
                if (r < 0)
                        return r;
 
                if (!session)
                        return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
                                                 "PID "PID_FMT" does not belong to any known session", pid);
        }
 
        p = session_bus_path(session);
        if (!p)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", p);
}
 
static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *p = NULL;
        Manager *m = ASSERT_PTR(userdata);
        uint32_t uid;
        User *user;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "u", &uid);
        if (r < 0)
                return r;
 
        r = manager_get_user_from_creds(m, message, uid, error, &user);
        if (r < 0)
                return r;
 
        p = user_bus_path(user);
        if (!p)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", p);
}
 
static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *p = NULL;
        Manager *m = ASSERT_PTR(userdata);
        User *user = NULL;
        pid_t pid;
        int r;
 
        assert(message);
 
        assert_cc(sizeof(pid_t) == sizeof(uint32_t));
 
        r = sd_bus_message_read(message, "u", &pid);
        if (r < 0)
                return r;
        if (pid < 0)
                return -EINVAL;
 
        if (pid == 0) {
                r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
                if (r < 0)
                        return r;
        } else {
                r = manager_get_user_by_pid(m, pid, &user);
                if (r < 0)
                        return r;
                if (!user)
                        return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
                                                 "PID "PID_FMT" does not belong to any logged in user or lingering user",
                                                 pid);
        }
 
        p = user_bus_path(user);
        if (!p)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", p);
}
 
static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *p = NULL;
        Manager *m = ASSERT_PTR(userdata);
        const char *name;
        Seat *seat;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        r = manager_get_seat_from_creds(m, message, name, error, &seat);
        if (r < 0)
                return r;
 
        p = seat_bus_path(seat);
        if (!p)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", p);
}
 
static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Session *session;
        int r;
 
        assert(message);
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(susso)");
        if (r < 0)
                return r;
 
        HASHMAP_FOREACH(session, m->sessions) {
                _cleanup_free_ char *p = NULL;
 
                p = session_bus_path(session);
                if (!p)
                        return -ENOMEM;
 
                r = sd_bus_message_append(reply, "(susso)",
                                          session->id,
                                          (uint32_t) session->user->user_record->uid,
                                          session->user->user_record->user_name,
                                          session->seat ? session->seat->id : "",
                                          p);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int method_list_sessions_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        int r;
 
        assert(message);
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(sussussbto)");
        if (r < 0)
                return r;
 
        Session *s;
        HASHMAP_FOREACH(s, m->sessions) {
                _cleanup_free_ char *path = NULL;
                dual_timestamp idle_ts;
                bool idle;
 
                assert(s->user);
 
                path = session_bus_path(s);
                if (!path)
                        return -ENOMEM;
 
                idle = session_get_idle_hint(s, &idle_ts);
 
                r = sd_bus_message_append(reply, "(sussussbto)",
                                          s->id,
                                          (uint32_t) s->user->user_record->uid,
                                          s->user->user_record->user_name,
                                          s->seat ? s->seat->id : "",
                                          (uint32_t) s->leader.pid,
                                          session_class_to_string(s->class),
                                          s->tty,
                                          idle,
                                          idle_ts.monotonic,
                                          path);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        Manager *m = ASSERT_PTR(userdata);
        User *user;
        int r;
 
        assert(message);
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(uso)");
        if (r < 0)
                return r;
 
        HASHMAP_FOREACH(user, m->users) {
                _cleanup_free_ char *p = NULL;
 
                p = user_bus_path(user);
                if (!p)
                        return -ENOMEM;
 
                r = sd_bus_message_append(reply, "(uso)",
                                          (uint32_t) user->user_record->uid,
                                          user->user_record->user_name,
                                          p);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Seat *seat;
        int r;
 
        assert(message);
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(so)");
        if (r < 0)
                return r;
 
        HASHMAP_FOREACH(seat, m->seats) {
                _cleanup_free_ char *p = NULL;
 
                p = seat_bus_path(seat);
                if (!p)
                        return -ENOMEM;
 
                r = sd_bus_message_append(reply, "(so)", seat->id, p);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Inhibitor *inhibitor;
        int r;
 
        assert(message);
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
        if (r < 0)
                return r;
 
        HASHMAP_FOREACH(inhibitor, m->inhibitors) {
 
                r = sd_bus_message_append(reply, "(ssssuu)",
                                          strempty(inhibit_what_to_string(inhibitor->what)),
                                          strempty(inhibitor->who),
                                          strempty(inhibitor->why),
                                          strempty(inhibit_mode_to_string(inhibitor->mode)),
                                          (uint32_t) inhibitor->uid,
                                          (uint32_t) inhibitor->pid.pid);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int manager_choose_session_id(
                Manager *m,
                const PidRef *leader,
                char **ret_id) {
 
        int r;
 
        assert(m);
        assert(pidref_is_set(leader));
        assert(ret_id);
 
        /* Try to keep our session IDs and the audit session IDs in sync */
        _cleanup_free_ char *id = NULL;
        uint32_t audit_id = AUDIT_SESSION_INVALID;
        r = audit_session_from_pid(leader, &audit_id);
        if (r < 0) {
                if (r != -ENODATA)
                        log_debug_errno(r, "Failed to read audit session ID of process " PID_FMT ", ignoring: %m", leader->pid);
        } else {
                if (asprintf(&id, "%"PRIu32, audit_id) < 0)
                        return -ENOMEM;
 
                /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
                 * not trust the audit data and let's better register a new ID */
                if (hashmap_contains(m->sessions, id)) {
                        log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
                        id = mfree(id);
                }
        }
 
        if (!id)
                do {
                        id = mfree(id);
 
                        if (asprintf(&id, "c%" PRIu64, ++m->session_counter) < 0)
                                return -ENOMEM;
 
                } while (hashmap_contains(m->sessions, id));
 
        /* The generated names should not clash with 'auto' or 'self' */
        assert(!session_is_self(id));
        assert(!session_is_auto(id));
 
        *ret_id = TAKE_PTR(id);
        return 0;
}
 
int manager_create_session(
                Manager *m,
                uid_t uid,
                PidRef *leader, /* consumed */
                const char *service,
                SessionType type,
                SessionClass class,
                const char *desktop,
                Seat *seat,
                unsigned vtnr,
                const char *tty,
                const char *display,
                bool remote,
                const char *remote_user,
                const char *remote_host,
                char * const *extra_device_access,
                Session **ret_session) {
 
        bool mangle_class = false;
        int r;
 
        assert(m);
        assert(uid_is_valid(uid));
        assert(pidref_is_set(leader));
        assert(ret_session);
 
        /* Returns:
         *    -EBUSY         → client is already in a session
         *    -EADDRNOTAVAIL → VT is already taken
         *    -EUSERS        → limit of sessions reached
         */
 
        if (type == _SESSION_TYPE_INVALID) {
                if (!isempty(display))
                        type = SESSION_X11;
                else if (!isempty(tty))
                        type = SESSION_TTY;
                else
                        type = SESSION_UNSPECIFIED;
        }
 
        if (class == _SESSION_CLASS_INVALID) {
                if (type == SESSION_UNSPECIFIED)
                        class = SESSION_BACKGROUND;
                else
                        class = SESSION_USER;
 
                /* If we determined the class automatically, then let's later potentially change it to early
                 * or light flavours, once we learn the disposition of the user */
                mangle_class = true;
        }
 
        /* Check if we are already in a logind session, and if so refuse. */
        r = manager_get_session_by_pidref(m, leader, /* ret= */ NULL);
        if (r < 0)
                return log_debug_errno(
                                r,
                                "Failed to check if process " PID_FMT " is already in a session: %m",
                                leader->pid);
        if (r > 0)
                return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "Client is already in a session.");
 
        /* Old gdm and lightdm start the user-session on the same VT as the greeter session. But they destroy
         * the greeter session after the user-session and want the user-session to take over the VT. We need
         * to support this for backwards-compatibility, so make sure we allow new sessions on a VT that a
         * greeter is running on. Furthermore, to allow re-logins, we have to allow a greeter to take over a
         * used VT for the exact same reasons. */
        if (class != SESSION_GREETER &&
            vtnr > 0 &&
            vtnr < MALLOC_ELEMENTSOF(m->seat0->positions) &&
            m->seat0->positions[vtnr] &&
            m->seat0->positions[vtnr]->class != SESSION_GREETER)
                return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), "VT already occupied by a session.");
 
        if (hashmap_size(m->sessions) >= m->sessions_max)
                return log_debug_errno(SYNTHETIC_ERRNO(EUSERS), "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
 
        _cleanup_free_ char *id = NULL;
        r = manager_choose_session_id(m, leader, &id);
        if (r < 0)
                return r;
 
        /* If we are not watching utmp already, try again */
        manager_reconnect_utmp(m);
 
        User *user = NULL;
        Session *session = NULL;
 
        r = manager_add_user_by_uid(m, uid, &user);
        if (r < 0)
                goto fail;
 
        /* If we picked the session class on our own, and the user is not a regular one, and the session is
         * not a graphical one then do not pull in session manager by default. For root make a special
         * exception: for TTY logins leave the service manager on, but relax the /run/nologin
         * restrictions. */
        if (mangle_class &&
            IN_SET(user_record_disposition(user->user_record), USER_INTRINSIC, USER_SYSTEM, USER_DYNAMIC)) {
 
                if (class == SESSION_USER) {
                        if (user_record_is_root(user->user_record))
                                class = SESSION_USER_EARLY;
                        else if (SESSION_TYPE_IS_GRAPHICAL(type))
                                class = SESSION_USER;
                        else
                                class = SESSION_USER_LIGHT;
 
                } else if (class == SESSION_BACKGROUND)
                        class = SESSION_BACKGROUND_LIGHT;
        }
 
        r = manager_add_session(m, id, &session);
        if (r < 0)
                goto fail;
 
        session_set_user(session, user);
        r = session_set_leader_consume(session, TAKE_PIDREF(*leader));