| 1 |
/* |
| 2 |
* Signal handlers for cHg |
| 3 |
* |
| 4 |
* Copyright 2011, 2018 Yuya Nishihara <[email protected]> |
| 5 |
* |
| 6 |
* This software may be used and distributed according to the terms of the |
| 7 |
* GNU General Public License version 2 or any later version. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <signal.h> |
| 13 |
#include <string.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
static pid_t peerpgid = 0; |
| 17 |
static pid_t peerpid = 0; |
| 18 |
|
| 19 |
static void forwardsignal(int sig) |
| 20 |
{ |
| 21 |
assert(peerpid > 0); |
| 22 |
(void)kill(peerpid, sig); |
| 23 |
} |
| 24 |
|
| 25 |
static void forwardsignaltogroup(int sig) |
| 26 |
{ |
| 27 |
/* prefer kill(-pgid, sig), fallback to pid if pgid is invalid */ |
| 28 |
pid_t killpid = peerpgid > 1 ? -peerpgid : peerpid; |
| 29 |
(void)kill(killpid, sig); |
| 30 |
} |
| 31 |
|
| 32 |
static void handlestopsignal(int sig) |
| 33 |
{ |
| 34 |
sigset_t unblockset, oldset; |
| 35 |
struct sigaction sa, oldsa; |
| 36 |
if (sigemptyset(&unblockset) < 0) { |
| 37 |
return; |
| 38 |
} |
| 39 |
if (sigaddset(&unblockset, sig) < 0) { |
| 40 |
return; |
| 41 |
} |
| 42 |
memset(&sa, 0, sizeof(sa)); |
| 43 |
sa.sa_handler = SIG_DFL; |
| 44 |
sa.sa_flags = SA_RESTART; |
| 45 |
if (sigemptyset(&sa.sa_mask) < 0) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
forwardsignal(sig); |
| 50 |
if (raise(sig) < 0) { /* resend to self */ |
| 51 |
return; |
| 52 |
} |
| 53 |
if (sigaction(sig, &sa, &oldsa) < 0) { |
| 54 |
return; |
| 55 |
} |
| 56 |
if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) { |
| 57 |
return; |
| 58 |
} |
| 59 |
/* resent signal will be handled before sigprocmask() returns */ |
| 60 |
if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) { |
| 61 |
return; |
| 62 |
} |
| 63 |
if (sigaction(sig, &oldsa, NULL) < 0) { |
| 64 |
return; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Installs signal handlers. |
| 70 |
* |
| 71 |
* Returns 0 on success, -1 on error and errno is set appropriately. |
| 72 |
* Installed handlers wouldn't be cleaned up on error. |
| 73 |
*/ |
| 74 |
int setupsignalhandler(pid_t pid, pid_t pgid) |
| 75 |
{ |
| 76 |
if (pid <= 0) { |
| 77 |
errno = EINVAL; |
| 78 |
return -1; |
| 79 |
} |
| 80 |
peerpid = pid; |
| 81 |
peerpgid = (pgid <= 1 ? 0 : pgid); |
| 82 |
|
| 83 |
struct sigaction sa; |
| 84 |
memset(&sa, 0, sizeof(sa)); |
| 85 |
|
| 86 |
/* deadly signals meant to be sent to a process group: |
| 87 |
* - SIGHUP: usually generated by the kernel, when termination of a |
| 88 |
* process causes that process group to become orphaned |
| 89 |
* - SIGINT: usually generated by the terminal */ |
| 90 |
sa.sa_handler = forwardsignaltogroup; |
| 91 |
sa.sa_flags = SA_RESTART; |
| 92 |
if (sigemptyset(&sa.sa_mask) < 0) { |
| 93 |
return -1; |
| 94 |
} |
| 95 |
if (sigaction(SIGHUP, &sa, NULL) < 0) { |
| 96 |
return -1; |
| 97 |
} |
| 98 |
if (sigaction(SIGINT, &sa, NULL) < 0) { |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
/* terminate frontend by double SIGTERM in case of server freeze */ |
| 103 |
sa.sa_handler = forwardsignal; |
| 104 |
sa.sa_flags |= SA_RESETHAND; |
| 105 |
if (sigaction(SIGTERM, &sa, NULL) < 0) { |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
/* notify the worker about window resize events */ |
| 110 |
sa.sa_flags = SA_RESTART; |
| 111 |
if (sigaction(SIGWINCH, &sa, NULL) < 0) { |
| 112 |
return -1; |
| 113 |
} |
| 114 |
/* forward user-defined signals */ |
| 115 |
if (sigaction(SIGUSR1, &sa, NULL) < 0) { |
| 116 |
return -1; |
| 117 |
} |
| 118 |
if (sigaction(SIGUSR2, &sa, NULL) < 0) { |
| 119 |
return -1; |
| 120 |
} |
| 121 |
/* propagate job control requests to worker */ |
| 122 |
sa.sa_handler = forwardsignal; |
| 123 |
sa.sa_flags = SA_RESTART; |
| 124 |
if (sigaction(SIGCONT, &sa, NULL) < 0) { |
| 125 |
return -1; |
| 126 |
} |
| 127 |
sa.sa_handler = handlestopsignal; |
| 128 |
sa.sa_flags = SA_RESTART; |
| 129 |
if (sigaction(SIGTSTP, &sa, NULL) < 0) { |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Restores signal handlers to the default, and masks SIGINT. |
| 138 |
* |
| 139 |
* Returns 0 on success, -1 on error and errno is set appropriately. |
| 140 |
*/ |
| 141 |
int restoresignalhandler(void) |
| 142 |
{ |
| 143 |
struct sigaction sa; |
| 144 |
memset(&sa, 0, sizeof(sa)); |
| 145 |
sa.sa_handler = SIG_DFL; |
| 146 |
sa.sa_flags = SA_RESTART; |
| 147 |
if (sigemptyset(&sa.sa_mask) < 0) { |
| 148 |
return -1; |
| 149 |
} |
| 150 |
|
| 151 |
if (sigaction(SIGHUP, &sa, NULL) < 0) { |
| 152 |
return -1; |
| 153 |
} |
| 154 |
if (sigaction(SIGTERM, &sa, NULL) < 0) { |
| 155 |
return -1; |
| 156 |
} |
| 157 |
if (sigaction(SIGWINCH, &sa, NULL) < 0) { |
| 158 |
return -1; |
| 159 |
} |
| 160 |
if (sigaction(SIGCONT, &sa, NULL) < 0) { |
| 161 |
return -1; |
| 162 |
} |
| 163 |
if (sigaction(SIGTSTP, &sa, NULL) < 0) { |
| 164 |
return -1; |
| 165 |
} |
| 166 |
|
| 167 |
/* ignore Ctrl+C while shutting down to make pager exits cleanly */ |
| 168 |
sa.sa_handler = SIG_IGN; |
| 169 |
if (sigaction(SIGINT, &sa, NULL) < 0) { |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
peerpid = 0; |
| 174 |
return 0; |
| 175 |
} |