- DBusError error;
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- int r;
-
- assert(connection);
- assert(message);
- assert(m);
-
- dbus_error_init(&error);
-
- if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
- const char *name;
- char *p;
- Session *session;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
- uint32_t pid;
- char *p;
- Session *session;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = manager_get_session_by_pid(m, pid, &session);
- if (r <= 0)
- return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
- uint32_t uid;
- char *p;
- User *user;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &uid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
- if (!user)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = user_bus_path(user);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUserByPID")) {
- uint32_t pid;
- char *p;
- User *user;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = manager_get_user_by_pid(m, pid, &user);
- if (r <= 0)
- return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = user_bus_path(user);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
- const char *name;
- char *p;
- Seat *seat;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- seat = hashmap_get(m->seats, name);
- if (!seat)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = seat_bus_path(seat);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
- char *p;
- Session *session;
- Iterator i;
- DBusMessageIter iter, sub;
- const char *empty = "";
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(session, m->sessions, i) {
- DBusMessageIter sub2;
- uint32_t uid;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- uid = session->user->uid;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
- free(p);
- goto oom;
- }
-
- free(p);
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
- User *user;
- Iterator i;
- DBusMessageIter iter, sub;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(user, m->users, i) {
- _cleanup_free_ char *p = NULL;
- DBusMessageIter sub2;
- uint32_t uid;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- uid = user->uid;
-
- p = user_bus_path(user);
- if (!p)
- goto oom;
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
- free(p);
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
- Seat *seat;
- Iterator i;
- DBusMessageIter iter, sub;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(seat, m->seats, i) {
- _cleanup_free_ char *p = NULL;
- DBusMessageIter sub2;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- p = seat_bus_path(seat);
- if (!p)
- goto oom;
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
- free(p);
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListInhibitors")) {
- Inhibitor *inhibitor;
- Iterator i;
- DBusMessageIter iter, sub;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssuu)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
- DBusMessageIter sub2;
- dbus_uint32_t uid, pid;
- const char *what, *who, *why, *mode;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- what = strempty(inhibit_what_to_string(inhibitor->what));
- who = strempty(inhibitor->who);
- why = strempty(inhibitor->why);
- mode = strempty(inhibit_mode_to_string(inhibitor->mode));
- uid = (dbus_uint32_t) inhibitor->uid;
- pid = (dbus_uint32_t) inhibitor->pid;
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &what) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &who) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &why) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &mode) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &pid))
- goto oom;
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Inhibit")) {
-
- r = bus_manager_inhibit(m, connection, message, &error, &reply);
-
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
-
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
-
- r = bus_manager_create_session(m, message);
-
- /* Don't delay the work on OOM here, since it might be
- * triggered by a low RLIMIT_NOFILE here (since we
- * send a dupped fd to the client), and we'd rather
- * see this fail quickly then be retried later */
-
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ReleaseSession")) {
- const char *name;
- Session *session;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- /* We use the FIFO to detect stray sessions where the
- process invoking PAM dies abnormally. We need to make
- sure that that process is not killed if at the clean
- end of the session it closes the FIFO. Hence, with
- this call explicitly turn off the FIFO logic, so that
- the PAM code can finish clean up on its own */
- session_remove_fifo(session);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
- const char *name;
- Session *session;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- r = session_activate(session);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSessionOnSeat")) {
- const char *session_name, *seat_name;
- Session *session;
- Seat *seat;
-
- /* Same as ActivateSession() but refuses to work if
- * the seat doesn't match */
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &session_name,
- DBUS_TYPE_STRING, &seat_name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, session_name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- seat = hashmap_get(m->seats, seat_name);
- if (!seat)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- if (session->seat != seat)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = session_activate(session);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
- dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
- const char *name;
- Session *session;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, NULL, -ENOENT);
-
- if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
- goto oom;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSessions") ||
- dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSessions")) {
-
- r = session_send_lock_all(m, streq(dbus_message_get_member(message), "LockSessions"));
- if (r < 0)
- bus_send_error_reply(connection, message, NULL, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
- const char *swho;
- int32_t signo;
- KillWho who;
- const char *name;
- Session *session;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &swho,
- DBUS_TYPE_INT32, &signo,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- if (isempty(swho))
- who = KILL_ALL;
- else {
- who = kill_who_from_string(swho);
- if (who < 0)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
- }
-
- if (signo <= 0 || signo >= _NSIG)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- r = session_kill(session, who, signo);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
- uint32_t uid;
- User *user;
- int32_t signo;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &uid,
- DBUS_TYPE_INT32, &signo,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- if (signo <= 0 || signo >= _NSIG)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
- if (!user)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- r = user_kill(user, signo);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);