Skip to content

Commit

Permalink
Merge pull request #1559 from everything411/shellspawn-zombie
Browse files Browse the repository at this point in the history
Correctly handle the shellspawn zombie
  • Loading branch information
CuriousTommy authored Jan 10, 2025
2 parents d210844 + 980a02f commit a458d15
Showing 1 changed file with 14 additions and 6 deletions.
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

0 comments on commit a458d15

Please sign in to comment.