--- a/contrib/chg/procutil.c Thu Jan 24 11:35:40 2019 -0500
+++ b/contrib/chg/procutil.c Thu Jan 24 10:21:59 2019 -0500
@@ -25,8 +25,9 @@
static void forwardsignal(int sig)
{
assert(peerpid > 0);
- if (kill(peerpid, sig) < 0)
+ if (kill(peerpid, sig) < 0) {
abortmsgerrno("cannot kill %d", peerpid);
+ }
debugmsg("forward signal %d", sig);
}
@@ -34,8 +35,9 @@
{
/* prefer kill(-pgid, sig), fallback to pid if pgid is invalid */
pid_t killpid = peerpgid > 1 ? -peerpgid : peerpid;
- if (kill(killpid, sig) < 0)
+ if (kill(killpid, sig) < 0) {
abortmsgerrno("cannot kill %d", killpid);
+ }
debugmsg("forward signal %d to %d", sig, killpid);
}
@@ -43,28 +45,36 @@
{
sigset_t unblockset, oldset;
struct sigaction sa, oldsa;
- if (sigemptyset(&unblockset) < 0)
+ if (sigemptyset(&unblockset) < 0) {
goto error;
- if (sigaddset(&unblockset, sig) < 0)
+ }
+ if (sigaddset(&unblockset, sig) < 0) {
goto error;
+ }
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
goto error;
+ }
forwardsignal(sig);
- if (raise(sig) < 0) /* resend to self */
+ if (raise(sig) < 0) { /* resend to self */
goto error;
- if (sigaction(sig, &sa, &oldsa) < 0)
+ }
+ if (sigaction(sig, &sa, &oldsa) < 0) {
goto error;
- if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0)
+ }
+ if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) {
goto error;
+ }
/* resent signal will be handled before sigprocmask() returns */
- if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0)
+ if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) {
goto error;
- if (sigaction(sig, &oldsa, NULL) < 0)
+ }
+ if (sigaction(sig, &oldsa, NULL) < 0) {
goto error;
+ }
return;
error:
@@ -73,19 +83,22 @@
static void handlechildsignal(int sig UNUSED_)
{
- if (peerpid == 0 || pagerpid == 0)
+ if (peerpid == 0 || pagerpid == 0) {
return;
+ }
/* if pager exits, notify the server with SIGPIPE immediately.
* otherwise the server won't get SIGPIPE if it does not write
* anything. (issue5278) */
- if (waitpid(pagerpid, NULL, WNOHANG) == pagerpid)
+ if (waitpid(pagerpid, NULL, WNOHANG) == pagerpid) {
kill(peerpid, SIGPIPE);
+ }
}
void setupsignalhandler(pid_t pid, pid_t pgid)
{
- if (pid <= 0)
+ if (pid <= 0) {
return;
+ }
peerpid = pid;
peerpgid = (pgid <= 1 ? 0 : pgid);
@@ -98,42 +111,52 @@
* - SIGINT: usually generated by the terminal */
sa.sa_handler = forwardsignaltogroup;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
+ goto error;
+ }
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGINT, &sa, NULL) < 0)
- goto error;
+ }
/* terminate frontend by double SIGTERM in case of server freeze */
sa.sa_handler = forwardsignal;
sa.sa_flags |= SA_RESETHAND;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
goto error;
+ }
/* notify the worker about window resize events */
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
goto error;
+ }
/* forward user-defined signals */
- if (sigaction(SIGUSR1, &sa, NULL) < 0)
+ if (sigaction(SIGUSR1, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGUSR2, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGUSR2, &sa, NULL) < 0) {
goto error;
+ }
/* propagate job control requests to worker */
sa.sa_handler = forwardsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
goto error;
+ }
sa.sa_handler = handlestopsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
goto error;
+ }
/* get notified when pager exits */
sa.sa_handler = handlechildsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCHLD, &sa, NULL) < 0)
+ if (sigaction(SIGCHLD, &sa, NULL) < 0) {
goto error;
+ }
return;
@@ -147,26 +170,34 @@
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
goto error;
+ }
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGCHLD, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGCHLD, &sa, NULL) < 0) {
goto error;
+ }
/* ignore Ctrl+C while shutting down to make pager exits cleanly */
sa.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sa, NULL) < 0)
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
goto error;
+ }
peerpid = 0;
return;
@@ -180,22 +211,27 @@
pid_t setuppager(const char *pagercmd, const char *envp[])
{
assert(pagerpid == 0);
- if (!pagercmd)
+ if (!pagercmd) {
return 0;
+ }
int pipefds[2];
- if (pipe(pipefds) < 0)
+ if (pipe(pipefds) < 0) {
return 0;
+ }
pid_t pid = fork();
- if (pid < 0)
+ if (pid < 0) {
goto error;
+ }
if (pid > 0) {
close(pipefds[0]);
- if (dup2(pipefds[1], fileno(stdout)) < 0)
+ if (dup2(pipefds[1], fileno(stdout)) < 0) {
goto error;
+ }
if (isatty(fileno(stderr))) {
- if (dup2(pipefds[1], fileno(stderr)) < 0)
+ if (dup2(pipefds[1], fileno(stderr)) < 0) {
goto error;
+ }
}
close(pipefds[1]);
pagerpid = pid;
@@ -222,16 +258,18 @@
void waitpager(void)
{
- if (pagerpid == 0)
+ if (pagerpid == 0) {
return;
+ }
/* close output streams to notify the pager its input ends */
fclose(stdout);
fclose(stderr);
while (1) {
pid_t ret = waitpid(pagerpid, NULL, 0);
- if (ret == -1 && errno == EINTR)
+ if (ret == -1 && errno == EINTR) {
continue;
+ }
break;
}
}