1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 01:37:35 +00:00

Everywhere: Replace some uses of fork/exec with posix_spawn

It's less code, and it's potentially more efficient once
posix_spawn is a real syscall.
This commit is contained in:
Nico Weber 2020-06-28 13:40:10 -04:00 committed by Andreas Kling
parent 301ac3c7e5
commit 12cbc4ad0d
11 changed files with 65 additions and 101 deletions

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
@ -36,15 +37,14 @@ int main(int argc, char** argv)
return 0;
}
if (!fork()) {
if (execvp(argv[2], &argv[2]) < 0) {
perror("execvp");
exit(1);
}
pid_t child_pid;
if ((errno = posix_spawnp(&child_pid, argv[2], nullptr, nullptr, &argv[2], environ))) {
perror("posix_spawn");
return 1;
}
int status;
if (waitpid(-1, &status, 0) < 0) {
if (waitpid(child_pid, &status, 0) < 0) {
perror("waitpid");
return 1;
}

View file

@ -31,6 +31,7 @@
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -121,20 +122,12 @@ int main(int argc, char** argv)
}
if (access(home_directory.characters(), F_OK) != -1) {
auto child = fork();
if (child < 0) {
perror("fork");
pid_t child;
const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr };
if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
return 12;
}
if (!child) {
int rc = execl("/bin/rm", "rm", "-r", home_directory.characters(), nullptr);
ASSERT(rc < 0);
perror("execl");
exit(127);
}
int wstatus;
if (waitpid(child, &wstatus, 0) < 0) {
perror("waitpid");