Commits

Evan Gates  committed 845b3a6

fix sigchld handler

  • Participants
  • Parent commits c06f2dd

Comments (0)

Files changed (1)

+#include <sys/wait.h>
 #include <unistd.h>
 #include <err.h>
 #include <signal.h>
 char *nrml_prompt = nrml_prompt_default;
 char *scrl_prompt = scrl_prompt_default;
 int pos = 0;
+pid_t child;
 
 void usage(void)
 {
 
 void sigchld(int unused)
 {
-	printf("\n");
-	warnx("subprocess died");
-	kill(getpid(), SIGINT);
+	pid_t pid;
+
+	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
+		if (pid == child) {
+			printf("\n");
+			warnx("subprocess died");
+			kill(getpid(), SIGINT);
+		}
+	}
 }
 
 void my_write(int fd, const char *buf, size_t count)
 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
 		err(1, "failed to install SIGCHLD handler");
 
-	if (forkpty(&mfd, NULL, &set, NULL) == 0) { //child
+	if ((child = forkpty(&mfd, NULL, &set, NULL)) == 0) { //child
 		execvp(argv[i], &argv[i]);
 		err(1, "failed execvp %s", argv[i]);
 	}