summaryrefslogtreecommitdiffstats
path: root/02_exercise/builtins.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/builtins.c')
-rw-r--r--02_exercise/builtins.c27
1 files changed, 6 insertions, 21 deletions
diff --git a/02_exercise/builtins.c b/02_exercise/builtins.c
index 4ab4c7c..1d1cc69 100644
--- a/02_exercise/builtins.c
+++ b/02_exercise/builtins.c
@@ -1,22 +1,16 @@
-#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <stdlib.h>
-
#include "array.h"
#include "builtins.h"
-
-void handle(int sig) {
- /* Do nothing. */
-}
-
+#include "signal_handler.h"
void builtin_wait(process p, bool bind) {
struct sigaction handler;
- handler.sa_handler = handle;
+ handler.sa_handler = signal_handler;
sigemptyset(&handler.sa_mask);
handler.sa_flags = 0;
@@ -43,24 +37,15 @@ void builtin_wait(process p, bool bind) {
pid_t current_pid = arrayPop(pids);
if (bind) {
printf("Resuming %ld...\n", (long) current_pid);
+ sigaction(SIGINT, &handler, NULL);
} else {
printf("Waiting for %ld...\n", (long) current_pid);
}
// install signal handler without SA_RESTART, so waitpid gets interrupted
sigaction(SIGINT, &handler, NULL);
if (waitpid(current_pid, &status, WUNTRACED) < 0) {
- if (EINTR == errno) {
- // cancelled by ctrl-c
- if (bind) {
- kill(current_pid, SIGKILL);
- waitpid(current_pid, &status, WUNTRACED);
- printf("Killed [%ld]\n", (long)current_pid);
- } else {
- kill(current_pid, SIGCONT);
- }
- } else {
- perror("Could not wait for process");
- }
+ sigaction(SIGINT, &handler, NULL);
+ handle_interrupt(current_pid);
} else {
printf("[%i] TERMINATED\n", current_pid);
if (WIFEXITED(status)) {
@@ -77,7 +62,7 @@ void builtin_wait(process p, bool bind) {
WEXITSTATUS(status));
}
}
- signal(SIGINT, SIG_DFL);
+ sigaction(SIGINT, &handler, NULL);
}
}