mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 12:17:34 +00:00
LibCore: Add Core::Process::spawn()
This is a simple wrapper around posix_spawn() that will help us simplify a bunch of very verbose posix_spawn() invocations. This first version only supports the simplest case: executing an executable without passing arguments or doing anything fancy. More features can be added to cover more cases. :^)
This commit is contained in:
parent
ad3ae7e0e8
commit
6e65b36973
3 changed files with 55 additions and 0 deletions
|
@ -21,6 +21,7 @@ set(SOURCES
|
||||||
NetworkResponse.cpp
|
NetworkResponse.cpp
|
||||||
Notifier.cpp
|
Notifier.cpp
|
||||||
Object.cpp
|
Object.cpp
|
||||||
|
Process.cpp
|
||||||
ProcessStatisticsReader.cpp
|
ProcessStatisticsReader.cpp
|
||||||
Property.cpp
|
Property.cpp
|
||||||
Socket.cpp
|
Socket.cpp
|
||||||
|
|
36
Userland/Libraries/LibCore/Process.cpp
Normal file
36
Userland/Libraries/LibCore/Process.cpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <LibCore/Process.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <spawn.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifdef __serenity__
|
||||||
|
# include <serenity.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace Core {
|
||||||
|
|
||||||
|
pid_t Process::spawn(StringView path)
|
||||||
|
{
|
||||||
|
String path_string = path;
|
||||||
|
|
||||||
|
pid_t pid;
|
||||||
|
char const* argv[] = { path_string.characters(), nullptr };
|
||||||
|
if ((errno = posix_spawn(&pid, path_string.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
||||||
|
perror("Process::spawn posix_spawn");
|
||||||
|
} else {
|
||||||
|
#ifdef __serenity__
|
||||||
|
if (disown(pid) < 0)
|
||||||
|
perror("Process::spawn disown");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
Userland/Libraries/LibCore/Process.h
Normal file
18
Userland/Libraries/LibCore/Process.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
|
||||||
|
namespace Core {
|
||||||
|
|
||||||
|
class Process {
|
||||||
|
public:
|
||||||
|
static pid_t spawn(StringView path);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue