Services: Use Core::Process::spawn() for common process spawn pattern

This commit is contained in:
MacDue 2022-05-26 23:26:57 +01:00 committed by Linus Groh
parent e547f5887e
commit 5e5a055455
3 changed files with 12 additions and 35 deletions

View file

@ -8,6 +8,7 @@
#include <Kernel/API/InodeWatcherEvent.h>
#include <LibCore/FileWatcher.h>
#include <LibCore/MappedFile.h>
#include <LibCore/Process.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <serenity.h>
@ -33,22 +34,12 @@ static void wait_until_coredump_is_ready(String const& coredump_path)
static void launch_crash_reporter(String const& coredump_path, bool unlink_on_exit)
{
pid_t child;
char const* argv[4] = { "CrashReporter" };
if (unlink_on_exit) {
argv[1] = "--unlink";
argv[2] = coredump_path.characters();
argv[3] = nullptr;
} else {
argv[1] = coredump_path.characters();
argv[2] = nullptr;
}
if ((errno = posix_spawn(&child, "/bin/CrashReporter", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
} else {
if (disown(child) < 0)
perror("disown");
}
auto pid = Core::Process::spawn("/bin/CrashReporter",
unlink_on_exit
? Array { "--unlink", coredump_path.characters() }.span()
: Array { coredump_path.characters() }.span());
if (pid.is_error())
warnln("Failed to launch CrashReporter");
}
ErrorOr<int> serenity_main(Main::Arguments)

View file

@ -13,6 +13,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibDesktop/AppFile.h>
#include <errno.h>
#include <serenity.h>
@ -183,20 +184,7 @@ bool Launcher::open_with_handler_name(const URL& url, String const& handler_name
bool spawn(String executable, Vector<String> const& arguments)
{
Vector<char const*> argv { executable.characters() };
for (auto& arg : arguments)
argv.append(arg.characters());
argv.append(nullptr);
pid_t child_pid;
if ((errno = posix_spawn(&child_pid, executable.characters(), nullptr, nullptr, const_cast<char**>(argv.data()), environ))) {
perror("posix_spawn");
return false;
} else {
if (disown(child_pid) < 0)
perror("disown");
}
return true;
return !Core::Process::spawn(executable, arguments).is_error();
}
Handler Launcher::get_handler_for_executable(Handler::Type handler_type, String const& executable) const

View file

@ -7,6 +7,7 @@
#include <AK/JsonObject.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <WindowServer/KeymapSwitcher.h>
#include <spawn.h>
#include <unistd.h>
@ -101,12 +102,9 @@ String KeymapSwitcher::get_current_keymap() const
void KeymapSwitcher::setkeymap(const AK::String& keymap)
{
pid_t child_pid;
char const* argv[] = { "/bin/keymap", "-m", keymap.characters(), nullptr };
if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
if (Core::Process::spawn("/bin/keymap", Array { "-m", keymap.characters() }).is_error())
dbgln("Failed to call /bin/keymap, error: {} ({})", errno, strerror(errno));
}
if (on_keymap_change)
on_keymap_change(keymap);
}