diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index e2ab5eba2e6..9109343643b 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -98,9 +98,9 @@ ErrorOr Account::self([[maybe_unused]] Read options) return Account(*pwd, spwd, extra_gids); } -ErrorOr Account::from_name(char const* username, [[maybe_unused]] Read options) +ErrorOr Account::from_name(StringView username, [[maybe_unused]] Read options) { - auto pwd = TRY(Core::System::getpwnam({ username, strlen(username) })); + auto pwd = TRY(Core::System::getpwnam(username)); if (!pwd.has_value()) return Error::from_string_literal("No such user"); diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index e75de6f71a1..f34a698ba03 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -32,11 +32,9 @@ public: PasswdOnly }; - // FIXME: Convert the methods below to take StringViews instead. - static String parse_path_with_uid(StringView general_path, Optional force_uid = {}); static ErrorOr self(Read options = Read::All); - static ErrorOr from_name(char const* username, Read options = Read::All); + static ErrorOr from_name(StringView username, Read options = Read::All); static ErrorOr from_uid(uid_t uid, Read options = Read::All); bool authenticate(SecretString const& password) const; @@ -51,11 +49,11 @@ public: // You must call sync to apply changes. void set_password(SecretString const& password); void set_password_enabled(bool enabled); - void set_home_directory(char const* home_directory) { m_home_directory = home_directory; } + void set_home_directory(StringView home_directory) { m_home_directory = home_directory; } void set_uid(uid_t uid) { m_uid = uid; } void set_gid(gid_t gid) { m_gid = gid; } - void set_shell(char const* shell) { m_shell = shell; } - void set_gecos(char const* gecos) { m_gecos = gecos; } + void set_shell(StringView shell) { m_shell = shell; } + void set_gecos(StringView gecos) { m_gecos = gecos; } void delete_password(); // A null password means that this account was missing from /etc/shadow. diff --git a/Userland/Services/LoginServer/main.cpp b/Userland/Services/LoginServer/main.cpp index 34fc819e2f4..0efd279042a 100644 --- a/Userland/Services/LoginServer/main.cpp +++ b/Userland/Services/LoginServer/main.cpp @@ -80,7 +80,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto fail_message = "Can't log in: invalid username or password."sv; - auto account = Core::Account::from_name(username.characters()); + auto account = Core::Account::from_name(username); if (account.is_error()) { window->set_fail_message(fail_message); dbgln("failed graphical login for user {}: {}", username, account.error()); @@ -99,13 +99,13 @@ ErrorOr serenity_main(Main::Arguments arguments) login(account.value(), *window); }; - char const* auto_login = nullptr; + StringView auto_login; Core::ArgsParser args_parser; args_parser.add_option(auto_login, "automatically log in with no prompt", "auto-login", 'a', "username"); args_parser.parse(arguments); - if (!auto_login) { + if (auto_login.is_empty()) { window->show(); } else { auto account = Core::Account::from_name(auto_login); diff --git a/Userland/Services/SystemServer/Service.cpp b/Userland/Services/SystemServer/Service.cpp index c8a4da259b1..673f0683c52 100644 --- a/Userland/Services/SystemServer/Service.cpp +++ b/Userland/Services/SystemServer/Service.cpp @@ -299,7 +299,7 @@ Service::Service(Core::ConfigFile const& config, StringView name) m_user = config.read_entry(name, "User"); if (!m_user.is_null()) { - auto result = Core::Account::from_name(m_user.characters(), Core::Account::Read::PasswdOnly); + auto result = Core::Account::from_name(m_user, Core::Account::Read::PasswdOnly); if (result.is_error()) warnln("Failed to resolve user {}: {}", m_user, result.error()); else diff --git a/Userland/Utilities/groups.cpp b/Userland/Utilities/groups.cpp index 09eddadac01..8bd261b58ea 100644 --- a/Userland/Utilities/groups.cpp +++ b/Userland/Utilities/groups.cpp @@ -47,8 +47,8 @@ ErrorOr serenity_main(Main::Arguments arguments) print_account_gids(account); } - for (auto username : usernames) { - auto result = Core::Account::from_name(username.characters(), Core::Account::Read::PasswdOnly); + for (auto const& username : usernames) { + auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly); if (result.is_error()) { warnln("{} '{}'", result.error(), username); continue; diff --git a/Userland/Utilities/id.cpp b/Userland/Utilities/id.cpp index b5044bb6ae2..5b0f662a759 100644 --- a/Userland/Utilities/id.cpp +++ b/Userland/Utilities/id.cpp @@ -53,7 +53,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (auto user_id = user_str.to_uint(); user_id.has_value()) account = TRY(Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly)); else - account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly)); + account = TRY(Core::Account::from_name(user_str, Core::Account::Read::PasswdOnly)); } else { account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly)); } diff --git a/Userland/Utilities/passwd.cpp b/Userland/Utilities/passwd.cpp index e1d8585e785..b8300a4b5e3 100644 --- a/Userland/Utilities/passwd.cpp +++ b/Userland/Utilities/passwd.cpp @@ -46,7 +46,7 @@ ErrorOr serenity_main(Main::Arguments arguments) // target_account is the account we are changing the password of. auto target_account = TRY(!username.is_empty() - ? Core::Account::from_name(username.characters()) + ? Core::Account::from_name(username) : Core::Account::from_uid(current_uid)); setpwent(); diff --git a/Userland/Utilities/su.cpp b/Userland/Utilities/su.cpp index 52b07df2149..b39a415fec9 100644 --- a/Userland/Utilities/su.cpp +++ b/Userland/Utilities/su.cpp @@ -21,7 +21,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!TRY(Core::System::isatty(STDIN_FILENO))) return Error::from_string_literal("Standard input is not a terminal"); - char const* user = nullptr; + StringView user; Core::ArgsParser args_parser; args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No); @@ -30,7 +30,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (geteuid() != 0) return Error::from_string_literal("Not running as root :("); - auto account = TRY(user ? Core::Account::from_name(user) : Core::Account::from_uid(0)); + auto account = TRY(user.is_empty() ? Core::Account::from_uid(0) : Core::Account::from_name(user)); TRY(Core::System::pledge("stdio tty exec id")); @@ -51,7 +51,6 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::setenv("HOME"sv, account.home_directory(), true)); - execl(account.shell().characters(), account.shell().characters(), nullptr); - perror("execl"); + TRY(Core::System::exec(account.shell(), Array { account.shell().view() }, Core::System::SearchInPath::No)); return 1; } diff --git a/Userland/Utilities/userdel.cpp b/Userland/Utilities/userdel.cpp index 381e38e2810..373bc4e27ef 100644 --- a/Userland/Utilities/userdel.cpp +++ b/Userland/Utilities/userdel.cpp @@ -33,7 +33,7 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::unveil("/etc/", "rwc")); TRY(Core::System::unveil("/bin/rm", "x")); - char const* username = nullptr; + StringView username; bool remove_home = false; Core::ArgsParser args_parser; diff --git a/Userland/Utilities/usermod.cpp b/Userland/Utilities/usermod.cpp index 5896ad91b8d..5dce973db0d 100644 --- a/Userland/Utilities/usermod.cpp +++ b/Userland/Utilities/usermod.cpp @@ -30,11 +30,11 @@ ErrorOr serenity_main(Main::Arguments arguments) int gid = 0; bool lock = false; bool unlock = false; - char const* new_home_directory = nullptr; + StringView new_home_directory; bool move_home = false; - char const* shell = nullptr; - char const* gecos = nullptr; - char const* username = nullptr; + StringView shell; + StringView gecos; + StringView username; auto args_parser = Core::ArgsParser(); args_parser.set_general_help("Modify a user account"); @@ -62,7 +62,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (move_home) { TRY(Core::System::unveil(target_account.home_directory(), "c"sv)); - TRY(Core::System::unveil({ new_home_directory, strlen(new_home_directory) }, "wc"sv)); + TRY(Core::System::unveil(new_home_directory, "wc"sv)); } unveil(nullptr, nullptr); @@ -98,11 +98,11 @@ ErrorOr serenity_main(Main::Arguments arguments) target_account.set_password_enabled(true); } - if (new_home_directory) { + if (!new_home_directory.is_empty()) { if (move_home) { - int rc = rename(target_account.home_directory().characters(), new_home_directory); - if (rc < 0) { - if (errno == EXDEV) { + auto maybe_error = Core::System::rename(target_account.home_directory(), new_home_directory); + if (maybe_error.is_error()) { + if (maybe_error.error().code() == EXDEV) { auto result = Core::File::copy_file_or_directory( new_home_directory, target_account.home_directory().characters(), Core::File::RecursionMode::Allowed, @@ -113,11 +113,11 @@ ErrorOr serenity_main(Main::Arguments arguments) warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast(result.error())); return 1; } - rc = unlink(target_account.home_directory().characters()); - if (rc < 0) - warnln("usermod: unlink {} : {}", target_account.home_directory().characters(), strerror(errno)); + maybe_error = Core::System::unlink(target_account.home_directory()); + if (maybe_error.is_error()) + warnln("usermod: unlink {} : {}", target_account.home_directory(), maybe_error.error().code()); } else { - warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), strerror(errno)); + warnln("usermod: could not move directory {} : {}", target_account.home_directory(), maybe_error.error().code()); } } } @@ -125,11 +125,11 @@ ErrorOr serenity_main(Main::Arguments arguments) target_account.set_home_directory(new_home_directory); } - if (shell) { + if (!shell.is_empty()) { target_account.set_shell(shell); } - if (gecos) { + if (!gecos.is_empty()) { target_account.set_gecos(gecos); }