aboutsummaryrefslogtreecommitdiff
path: root/src/handler.cc
blob: 64d63e7436ef748399a3af43cb0a4b0ffc479fef (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

#include "handler.h"

#include <termios.h>
#include <sys/ioctl.h>

namespace ov4
{

sigset_t 
    block_sig_TTOU,
    block_job,
    block_io;

void signal_init()
{
    /* Install the signal handlers */
    
    sigemptyset(&block_job);
    sigemptyset(&block_sig_TTOU);
    sigemptyset(&block_io);

    sigaddset(&block_job, SIGINT);
    sigaddset(&block_job, SIGTSTP);
    sigaddset(&block_job, SIGCHLD);

    sigaddset(&block_sig_TTOU, SIGTTOU);

    sigaddset(&block_io, SIGTTOU);
    sigaddset(&block_io, SIGTTIN);

    Signal(SIGINT,  sigint_handler);   /* ctrl-c */
    Signal(SIGTSTP, sigtstp_handler);  /* ctrl-z */
    Signal(SIGCHLD, sigchld_handler);  /* Terminated or stopped child */
}

/*
 * Signal - wrapper for the sigaction function
 */

handler_t *Signal([[maybe_unused]] int signum, handler_t *handler) 
{
    struct sigaction action, old_action;

    action.sa_handler = handler;  
    sigemptyset(&action.sa_mask); /* block sigs of type being handled */
    action.sa_flags = SA_RESTART; /* restart syscalls if possible */

    if (sigaction(signum, &action, &old_action) < 0)
	unix_error("Signal error");
    return (old_action.sa_handler);
}
    
/* 
 * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
 *     a child job terminates (becomes a zombie), or stops because it
 *     received a SIGSTOP or SIGTSTP signal. The handler reaps all
 *     available zombie children, but doesn't wait for any other
 *     currently running children to terminate.  
 */
void sigchld_handler([[maybe_unused]] int sig) 
{
    int errno_backup = errno;

    sigset_t prev;
    block_handler(&prev);

    int status, pid;
    safe_debug("SIGCHLD\n");

    while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0)
    {
        if (WIFSIGNALED(status) || WIFSTOPPED(status))
        {
            int jid = pid2jid(pid);
            safe_print("Job [");
            safe_print(jid);
            safe_print("] (");
            safe_print(pid);
            safe_print(") ");
            if (!WIFSTOPPED(status) && WTERMSIG(status) == SIGINT)
                safe_print("terminated");
            else if (WIFSTOPPED(status))
                safe_print("stopped");
            else 
                safe_print("idk");
            safe_print(" by signal ");
            if (WIFSTOPPED(status))
                safe_print(WSTOPSIG(status));
            else
                safe_print(WTERMSIG(status));
            safe_print("\n");
        } 

        if (WIFSTOPPED(status))
        {
            safe_debug("suspended pid: ");
            safe_debug(pid);
            safe_debug("\n");
            sigset_t prev_inner;
            block_all(&prev_inner);
            job_suspend(pid);
            sigprocmask(SIG_SETMASK, &prev_inner, nullptr);
        }
        else {
            safe_debug("terminated pid: ");
            safe_debug(pid);
            sigset_t prev_inner;
            block_all(&prev_inner);
            deletejob(pid);
            sigprocmask(SIG_SETMASK, &prev_inner, nullptr);
        }
    }

    sigprocmask(SIG_SETMASK, &prev, nullptr);

    errno = errno_backup;
    return;
}

/* 
 * sigint_handler - The kernel sends a SIGINT to the shell whenver the
 *    user types ctrl-c at the keyboard.  Catch it and send it along
 *    to the foreground job.  
 */
void sigint_handler([[maybe_unused]] int sig) 
{
    int errno_backup = errno;

    sigset_t prev;
    block_handler(&prev);

    int pid = fgpid();
    if (pid != 0)
    {
        kill(-pid, SIGINT);
    } else {
        char *buf = "\n";
        tcflush(STDIN_FILENO, TCIFLUSH);
        ioctl(STDIN_FILENO, TIOCSTI, buf);
    }

    sigprocmask(SIG_SETMASK, &prev, nullptr);

    errno = errno_backup;
    return;
}

/*
 * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
 *     the user types ctrl-z at the keyboard. Catch it and suspend the
 *     foreground job by sending it a SIGTSTP.  
 */
void sigtstp_handler([[maybe_unused]] int sig) 
{
    int errno_backup = errno;

    sigset_t prev;
    block_handler(&prev);

    int pid = fgpid();
    if (pid != 0)
    {
        kill(-pid, SIGTSTP);
    }

    sigprocmask(SIG_SETMASK, &prev, nullptr);

    errno = errno_backup;
    return;
}

int block_all(sigset_t *prev)
{
    sigset_t set;
    sigfillset(&set);
    return sigprocmask(SIG_SETMASK, &set, prev);
}

int block_handler(sigset_t *prev)
{
    return sigprocmask(SIG_BLOCK, &block_job, prev);
}

void sigquit_handler([[maybe_unused]] int sig) 
{
    printf("Terminating after receipt of SIGQUIT signal\n");
    exit(1);
}

}