Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp signal handling. #1480

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/bspwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
Expand Down Expand Up @@ -87,7 +86,7 @@ bool auto_raise;
bool sticky_still;
bool hide_sticky;
bool record_history;
bool running;
volatile sig_atomic_t running;
bool restart;
bool randr;

Expand All @@ -104,6 +103,7 @@ int main(int argc, char *argv[])
xcb_generic_event_t *event;
char *end;
int opt;
struct sigaction sigact;

while ((opt = getopt(argc, argv, "hvc:s:o:")) != -1) {
switch (opt) {
Expand Down Expand Up @@ -194,11 +194,20 @@ int main(int argc, char *argv[])

fcntl(sock_fd, F_SETFD, FD_CLOEXEC | fcntl(sock_fd, F_GETFD));

signal(SIGINT, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGCHLD, sig_handler);
signal(SIGPIPE, SIG_IGN);
sigact.sa_handler = sig_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_RESTART;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGHUP, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
/* We avoid using SIG_IGN with SIGPIPE because that would be preserved across
exec. */
sigaction(SIGPIPE, &sigact, NULL);

sigact.sa_handler = SIG_IGN;
sigact.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &sigact, NULL);

run_config(run_level);
running = true;

Expand Down Expand Up @@ -527,11 +536,7 @@ bool check_connection (xcb_connection_t *dpy)

void sig_handler(int sig)
{
if (sig == SIGCHLD) {
signal(sig, sig_handler);
while (waitpid(-1, 0, WNOHANG) > 0)
;
} else if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
running = false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/bspwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef BSPWM_BSPWM_H
#define BSPWM_BSPWM_H

#include <signal.h>
#include "types.h"

#define WM_NAME "bspwm"
Expand Down Expand Up @@ -84,7 +85,7 @@ extern bool auto_raise;
extern bool sticky_still;
extern bool hide_sticky;
extern bool record_history;
extern bool running;
extern volatile sig_atomic_t running;
extern bool restart;
extern bool randr;

Expand Down