Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle the shellspawn zombie #1559

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/shellspawn/shellspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ along with Darling. If not, see <http://www.gnu.org/licenses/>.
#define DBG 0

int g_serverSocket = -1;
struct sigaction sigchld_oldaction;

void setupSocket(void);
void listenForConnections(void);
void spawnShell(int fd);
void setupSigchild(void);
void restoreSigchild(void);
void reapAll(void);

int main(int argc, const char** argv)
{
// in order to read the exit status of the process,
// we have to allow it to become a zombie, which is prevented
// when we set the SIGCHLD signal to SA_NOCLDWAIT
//setupSigchild();
// shellspawn (daemon) --fork()--> shellspawn (child) --fork()--> exec /bin/bash
// in order to read the exit status of the shell process,
// we have to allow it to become a zombie, therefore we need to
// restore the sigaction of SIGCHLD of the child shellspawn
setupSigchild();
setupSocket();
listenForConnections();

Expand Down Expand Up @@ -106,14 +109,14 @@ void listenForConnections(void)

if (fork() == 0)
{
restoreSigchild();
fcntl(sock, F_SETFD, FD_CLOEXEC);
spawnShell(sock);
exit(EXIT_SUCCESS);
}
else
{
close(sock);
reapAll();
}
}
}
Expand Down Expand Up @@ -433,7 +436,12 @@ void setupSigchild(void)
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
};
sigaction(SIGCHLD, &sigchld_action, NULL);
sigaction(SIGCHLD, &sigchld_action, &sigchld_oldaction);
}

void restoreSigchild(void)
{
sigaction(SIGCHLD, &sigchld_oldaction, NULL);
}

void reapAll(void)
Expand Down
Loading