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
|
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <alpm.h>
#include "conf.h"
#include "sighandler.h"
#include "util.h"
static ssize_t xwrite(int fd, const void *buf, size_t count)
{
ssize_t ret;
do {
ret = write(fd, buf, count);
} while(ret == -1 && errno == EINTR);
return ret;
}
static void soft_interrupt_handler(int signum)
{
if(signum == SIGINT) {
const char msg[] = "\nInterrupt signal received\n";
xwrite(STDERR_FILENO, msg, ARRAYSIZE(msg) - 1);
} else {
const char msg[] = "\nHangup signal received\n";
xwrite(STDERR_FILENO, msg, ARRAYSIZE(msg) - 1);
}
if(alpm_trans_interrupt(config->handle) == 0) {
return;
}
alpm_unlock(config->handle);
xwrite(STDOUT_FILENO, "\n", 1);
_Exit(128 + signum);
}
void install_soft_interrupt_handler(void)
{
struct sigaction new_action;
new_action.sa_handler = soft_interrupt_handler;
new_action.sa_flags = SA_RESTART;
sigemptyset(&new_action.sa_mask);
sigaddset(&new_action.sa_mask, SIGINT);
sigaddset(&new_action.sa_mask, SIGHUP);
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGHUP, &new_action, NULL);
}
void remove_soft_interrupt_handler(void)
{
struct sigaction new_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_handler = SIG_DFL;
new_action.sa_flags = 0;
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGHUP, &new_action, NULL);
}
static void segv_handler(int signum)
{
const char msg[] = "\nerror: segmentation fault\n"
"Please submit a full bug report with --debug if appropriate.\n";
xwrite(STDERR_FILENO, msg, sizeof(msg) - 1);
_Exit(signum);
}
void install_segv_handler(void)
{
struct sigaction new_action;
new_action.sa_handler = segv_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = SA_RESTART;
sigaction(SIGSEGV, &new_action, NULL);
}
static void winch_handler(int signum)
{
(void)signum;
columns_cache_reset();
}
void install_winch_handler(void)
{
struct sigaction new_action;
new_action.sa_handler = winch_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = SA_RESTART;
sigaction(SIGWINCH, &new_action, NULL);
}
|