- 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", "GetMachine")) {
- Machine *machine;
- const char *name;
- char *p;
- 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);
-
- machine = hashmap_get(m->machines, name);
- if (!machine)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = machine_bus_path(machine);
- 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", "GetMachineByPID")) {
- uint32_t pid;
- char *p;
- Machine *machine;
- 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_machine_by_pid(m, pid, &machine);
- 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 = machine_bus_path(machine);
- 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", "ListMachines")) {
- Machine *machine;
- 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, "(ssso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(machine, m->machines, i) {
- _cleanup_free_ char *p = NULL;
- DBusMessageIter sub2;
- const char *class;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- p = machine_bus_path(machine);
- if (!p)
- goto oom;
-
- class = strempty(machine_class_to_string(machine->class));
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->name) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &class) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->service) ||
- !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", "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", "CreateMachine")) {
-
- r = bus_manager_create_machine(m, message);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);