X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind-dbus.c;h=e76381b32268a962d336ac5fd08d88647aeaa36e;hp=d8d25b0041559a570d4c90b1f3be03fea9489594;hb=6c605695506cc55fd77241308540c5e1a15d807c;hpb=fb6becb4436ae4078337011b2017ce294e7361cf diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index d8d25b004..e76381b32 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -63,14 +63,6 @@ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ @@ -80,9 +72,6 @@ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ @@ -108,16 +97,6 @@ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ @@ -151,9 +130,6 @@ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ @@ -231,22 +207,12 @@ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ - " \n" \ - " \n" \ " \n" \ " \n" \ " \n" \ @@ -438,8 +404,8 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { int v; if (!seat) - seat = m->vtconsole; - else if (seat != m->vtconsole) + seat = m->seat0; + else if (seat != m->seat0) return -EINVAL; v = vtnr_from_tty(tty); @@ -454,8 +420,8 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { } else if (tty_is_console(tty)) { if (!seat) - seat = m->vtconsole; - else if (seat != m->vtconsole) + seat = m->seat0; + else if (seat != m->seat0) return -EINVAL; if (vtnr != 0) @@ -463,7 +429,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { } if (seat) { - if (seat_can_multi_session(seat)) { + if (seat_has_vts(seat)) { if (vtnr > 63) return -EINVAL; } else { @@ -523,7 +489,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; _cleanup_free_ char *path = NULL; _cleanup_close_ int fifo_fd = -1; - bool exists; + dbus_bool_t exists; /* Session already exists, client is probably * something like "su" which changes uid but is still @@ -588,6 +554,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { * the audit data and let's better register a new * ID */ if (hashmap_get(m->sessions, id)) { + log_warning("Existing logind session ID %s used by new audit session, ignoring", id); audit_id = 0; free(id); @@ -677,6 +644,10 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) { session->create_message = dbus_message_ref(message); + /* Now, let's wait until the slice unit and stuff got + * created. We send the reply back from + * session_send_create_reply().*/ + return 0; fail: @@ -689,148 +660,6 @@ fail: return r; } -static bool valid_machine_name(const char *p) { - size_t l; - - if (!filename_is_safe(p)) - return false; - - if (!ascii_is_valid(p)) - return false; - - l = strlen(p); - - if (l < 1 || l> 64) - return false; - - return true; -} - -static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { - - const char *name, *service, *class, *slice, *root_directory; - _cleanup_free_ char *p = NULL; - DBusMessageIter iter, sub; - MachineClass c; - uint32_t leader; - sd_id128_t id; - Machine *m; - int n, r; - void *v; - - assert(manager); - assert(message); - - if (!dbus_message_iter_init(message, &iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &name); - - if (!valid_machine_name(name) || - !dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || - dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE) - return -EINVAL; - - dbus_message_iter_recurse(&iter, &sub); - dbus_message_iter_get_fixed_array(&sub, &v, &n); - - if (n == 0) - id = SD_ID128_NULL; - else if (n == 16) - memcpy(&id, v, n); - else - return -EINVAL; - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &service); - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &class); - - if (isempty(class)) - c = _MACHINE_CLASS_INVALID; - else { - c = machine_class_from_string(class); - if (c < 0) - return -EINVAL; - } - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &leader); - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &slice); - if (!(isempty(slice) || (unit_name_is_valid(slice, false) && endswith(slice, ".slice"))) || - !dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &root_directory); - - if (!(isempty(root_directory) || path_is_absolute(root_directory))) - return -EINVAL; - - if (hashmap_get(manager->machines, name)) - return -EEXIST; - - if (leader <= 0) { - leader = bus_get_unix_process_id(manager->bus, dbus_message_get_sender(message), NULL); - if (leader == 0) - return -EINVAL; - } - - r = manager_add_machine(manager, name, &m); - if (r < 0) - goto fail; - - m->leader = leader; - m->class = c; - m->id = id; - - if (!isempty(service)) { - m->service = strdup(service); - if (!m->service) { - r = -ENOMEM; - goto fail; - } - } - - if (!isempty(root_directory)) { - m->root_directory = strdup(root_directory); - if (!m->root_directory) { - r = -ENOMEM; - goto fail; - } - } - - r = machine_start(m); - if (r < 0) - goto fail; - - m->create_message = dbus_message_ref(message); - - return 0; - -fail: - if (m) - machine_add_to_gc_queue(m); - - return r; -} - static int bus_manager_inhibit( Manager *m, DBusConnection *connection, @@ -1671,73 +1500,6 @@ static DBusHandlerResult manager_message_handler( 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; @@ -1946,48 +1708,6 @@ static DBusHandlerResult manager_message_handler( 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")) { @@ -2009,12 +1729,6 @@ static DBusHandlerResult manager_message_handler( 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); - } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ReleaseSession")) { const char *name; Session *session; @@ -2201,45 +1915,6 @@ static DBusHandlerResult manager_message_handler( if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillMachine")) { - const char *swho; - int32_t signo; - KillWho who; - const char *name; - Machine *machine; - - 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); - - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); - - r = machine_kill(machine, 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", "TerminateSession")) { const char *name; Session *session; @@ -2309,29 +1984,6 @@ static DBusHandlerResult manager_message_handler( if (!reply) goto oom; - } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateMachine")) { - const char *name; - Machine *machine; - - 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); - - r = machine_stop(machine); - 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", "SetUserLinger")) { uint32_t uid; struct passwd *pw; @@ -2604,7 +2256,7 @@ static DBusHandlerResult manager_message_handler( } HASHMAP_FOREACH(user, m->users, i) - fprintf(f, "", (unsigned long long) user->uid); + fprintf(f, "", (unsigned long long) user->uid); HASHMAP_FOREACH(session, m->sessions, i) { p = bus_path_escape(session->id); @@ -2701,7 +2353,6 @@ DBusHandlerResult bus_message_filter( m->action_what = 0; } else { - Machine *mm; Session *s; User *u; @@ -2718,7 +2369,8 @@ DBusHandlerResult bus_message_filter( dbus_set_error(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); session_send_create_reply(s, &error); } - } + } else + session_save(s); } session_add_to_gc_queue(s); @@ -2736,32 +2388,13 @@ DBusHandlerResult bus_message_filter( u->slice_job = NULL; } + user_save(u); user_add_to_gc_queue(u); } - - mm = hashmap_get(m->machine_units, unit); - if (mm) { - if (streq_ptr(path, mm->scope_job)) { - free(mm->scope_job); - mm->scope_job = NULL; - - if (mm->started) { - if (streq(result, "done")) - machine_send_create_reply(mm, NULL); - else { - dbus_set_error(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); - machine_send_create_reply(mm, &error); - } - } - } - - machine_add_to_gc_queue(mm); - } } } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) { - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; _cleanup_free_ char *unit = NULL; const char *path; @@ -2771,7 +2404,6 @@ DBusHandlerResult bus_message_filter( unit_name_from_dbus_path(path, &unit); if (unit) { - Machine *mm; Session *s; User *u; @@ -2782,10 +2414,74 @@ DBusHandlerResult bus_message_filter( u = hashmap_get(m->user_units, unit); if (u) user_add_to_gc_queue(u); + } + + } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) { + + const char *path, *unit; + Session *session; + User *user; + + if (!dbus_message_get_args(message, &error, + DBUS_TYPE_STRING, &unit, + DBUS_TYPE_OBJECT_PATH, &path, + DBUS_TYPE_INVALID)) { + log_error("Failed to parse UnitRemoved message: %s", bus_error_message(&error)); + goto finish; + } + + session = hashmap_get(m->session_units, unit); + if (session) + session_add_to_gc_queue(session); + + user = hashmap_get(m->user_units, unit); + if (user) + user_add_to_gc_queue(user); + + } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) { + dbus_bool_t b; - mm = hashmap_get(m->machine_units, unit); - if (mm) - machine_add_to_gc_queue(mm); + if (!dbus_message_get_args(message, &error, + DBUS_TYPE_BOOLEAN, &b, + DBUS_TYPE_INVALID)) { + log_error("Failed to parse Reloading message: %s", bus_error_message(&error)); + goto finish; + } + + /* systemd finished reloading, let's recheck all our sessions */ + if (!b) { + Session *session; + Iterator i; + + log_debug("System manager has been reloaded, rechecking sessions..."); + + HASHMAP_FOREACH(session, m->sessions, i) + session_add_to_gc_queue(session); + } + + } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { + const char *name, *old, *new; + char *key; + + if (!dbus_message_get_args(message, &error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old, + DBUS_TYPE_STRING, &new, + DBUS_TYPE_INVALID)) { + log_error("Failed to parse NameOwnerChanged message: %s", bus_error_message(&error)); + goto finish; + } + + /* drop all controllers owned by this name */ + if (*old && !*new && (key = hashmap_remove(m->busnames, old))) { + Session *session; + Iterator i; + + free(key); + + HASHMAP_FOREACH(session, m->sessions, i) + if (session_is_controller(session, old)) + session_drop_controller(session); } } @@ -2851,14 +2547,16 @@ int manager_start_scope( pid_t pid, const char *slice, const char *description, + const char *after, + const char *kill_mode, DBusError *error, char **job) { + const char *timeout_stop_property = "TimeoutStopUSec", *send_sighup_property = "SendSIGHUP", *pids_property = "PIDs"; _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL; DBusMessageIter iter, sub, sub2, sub3, sub4; - const char *timeout_stop_property = "TimeoutStopUSec"; - const char *pids_property = "PIDs"; uint64_t timeout = 500 * USEC_PER_MSEC; + dbus_bool_t send_sighup = true; const char *fail = "fail"; uint32_t u; @@ -2908,6 +2606,32 @@ int manager_start_scope( return log_oom(); } + if (!isempty(after)) { + const char *after_property = "After"; + + if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || + !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &after_property) || + !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "as", &sub3) || + !dbus_message_iter_open_container(&sub3, DBUS_TYPE_ARRAY, "s", &sub4) || + !dbus_message_iter_append_basic(&sub4, DBUS_TYPE_STRING, &after) || + !dbus_message_iter_close_container(&sub3, &sub4) || + !dbus_message_iter_close_container(&sub2, &sub3) || + !dbus_message_iter_close_container(&sub, &sub2)) + return log_oom(); + } + + if (!isempty(kill_mode)) { + const char *kill_mode_property = "KillMode"; + + if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || + !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &kill_mode_property) || + !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "s", &sub3) || + !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &kill_mode) || + !dbus_message_iter_close_container(&sub2, &sub3) || + !dbus_message_iter_close_container(&sub, &sub2)) + return log_oom(); + } + /* cgroup empty notification is not available in containers * currently. To make this less problematic, let's shorten the * stop timeout for sessions, so that we don't wait @@ -2921,6 +2645,16 @@ int manager_start_scope( !dbus_message_iter_close_container(&sub, &sub2)) return log_oom(); + /* Make sure that the session shells are terminated with + * SIGHUP since bash and friends tend to ignore SIGTERM */ + if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || + !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &send_sighup_property) || + !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "b", &sub3) || + !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_BOOLEAN, &send_sighup) || + !dbus_message_iter_close_container(&sub2, &sub3) || + !dbus_message_iter_close_container(&sub, &sub2)) + return log_oom(); + u = pid; if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &pids_property) || @@ -2929,8 +2663,10 @@ int manager_start_scope( !dbus_message_iter_append_basic(&sub4, DBUS_TYPE_UINT32, &u) || !dbus_message_iter_close_container(&sub3, &sub4) || !dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2) || - !dbus_message_iter_close_container(&iter, &sub)) + !dbus_message_iter_close_container(&sub, &sub2)) + return log_oom(); + + if (!dbus_message_iter_close_container(&iter, &sub)) return log_oom(); reply = dbus_connection_send_with_reply_and_block(manager->bus, m, -1, error); @@ -3019,6 +2755,16 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char DBUS_TYPE_STRING, &fail, DBUS_TYPE_INVALID); if (r < 0) { + if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || + dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { + + if (job) + *job = NULL; + + dbus_error_free(error); + return 0; + } + log_error("Failed to stop unit %s: %s", unit, bus_error(error, r)); return r; } @@ -3041,7 +2787,7 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char *job = copy; } - return 0; + return 1; } int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, DBusError *error) { @@ -3106,8 +2852,26 @@ int manager_unit_is_active(Manager *manager, const char *unit) { DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID); - if (r < 0) { + if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY) || + dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED)) { + /* systemd might have droppped off + * momentarily, let's not make this an + * error */ + + dbus_error_free(&error); + return true; + } + + if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || + dbus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) { + /* If the unit is already unloaded then it's + * not active */ + + dbus_error_free(&error); + return false; + } + log_error("Failed to query ActiveState: %s", bus_error(&error, r)); dbus_error_free(&error); return r;