Anonymous avatar Anonymous committed 845b3a6

fix sigchld handler

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]);
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.