Fix exec_before/after leaving a zombie process behind

This commit is contained in:
Tobias Langendorf 2021-08-26 02:31:21 +02:00 committed by Simon Ser
parent 5f5a29ccfd
commit 9ef1d6aa2b
1 changed files with 23 additions and 13 deletions

View File

@ -19,10 +19,15 @@ static const char object_path[] = "/org/freedesktop/portal/desktop";
static const char interface_name[] = "org.freedesktop.impl.portal.ScreenCast"; static const char interface_name[] = "org.freedesktop.impl.portal.ScreenCast";
void exec_with_shell(char *command) { void exec_with_shell(char *command) {
pid_t pid = fork(); pid_t pid1 = fork();
if (pid < 0) { if (pid1 < 0) {
perror("fork"); perror("fork");
} else if (pid == 0) { return;
} else if (pid1 == 0) {
pid_t pid2 = fork();
if (pid2 < 0) {
perror("fork");
} else if (pid2 == 0) {
char *const argv[] = { char *const argv[] = {
"sh", "sh",
"-c", "-c",
@ -30,9 +35,14 @@ void exec_with_shell(char *command) {
NULL, NULL,
}; };
execvp("sh", argv); execvp("sh", argv);
perror("execvp"); perror("execvp");
exit(127); _exit(127);
}
_exit(0);
}
int stat;
if (waitpid(pid1, &stat, 0) < 0) {
perror("waitpid");
} }
} }