-
Notifications
You must be signed in to change notification settings - Fork 1
/
signals.c
77 lines (67 loc) · 1.53 KB
/
signals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <signal.h>
#include <unistd.h>
#include "jobs.h"
void handle_sigint() {
if (gpidRunning != 0) {
kill(-gpidRunning, SIGINT);
gpidRunning = 0;
}
return;
}
void handle_sigtstp() {
if (gpidRunning != 0) {
kill(-gpidRunning, SIGTSTP);
gpidRunning = 0;
}
return;
}
void handle_sigttou() {
tcsetpgrp(STDIN_FILENO, getpid());
}
void handle_sigchld() {
update_jobs();
}
void handle_signal(int signal) {
switch (signal) {
case SIGINT:
handle_sigint();
break;
case SIGTSTP:
handle_sigtstp();
break;
case SIGTTOU:
handle_sigttou();
break;
case SIGCHLD:
handle_sigchld();
break;
default:
break;
}
}
void setup_handlers() {
struct sigaction action;
action.sa_handler = &handle_signal;
action.sa_flags = SA_RESTART;
sigemptyset(&(action.sa_mask));
sigaddset(&(action.sa_mask), SIGINT);
sigaddset(&(action.sa_mask), SIGCHLD);
sigaddset(&(action.sa_mask), SIGTSTP);
sigaddset(&(action.sa_mask), SIGTTOU);
// sigaddset(&(action.sa_mask), SIGTTIN);
sigaction(SIGINT, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
sigaction(SIGTTOU, &action, NULL);
//sigaction(SIGTTIN, &action, NULL);
}
void reset_handlers() {
struct sigaction action;
action.sa_handler = SIG_DFL;
action.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
sigaction(SIGTTOU, &action, NULL);
sigaction(SIGTTIN, &action, NULL);
}