use sigaction(2) for signal handling

References: http://git.suckless.org/dwm/commit/712d6639ff8e863560328131bbb92b248dc9cde7.html
This commit is contained in:
Leonardo Hernández Hernández 2022-09-28 00:54:48 -05:00
parent 8653b27692
commit f7d6a34cd9
No known key found for this signature in database
GPG Key ID: E538897EE11B9624
1 changed files with 20 additions and 11 deletions

31
dwl.c
View File

@ -1885,6 +1885,8 @@ run(char *startup_cmd)
{ {
/* Add a Unix socket to the Wayland display. */ /* Add a Unix socket to the Wayland display. */
const char *socket = wl_display_add_socket_auto(dpy); const char *socket = wl_display_add_socket_auto(dpy);
struct sigaction sa = {.sa_flags = SA_RESTART, .sa_handler = SIG_IGN};
sigemptyset(&sa.sa_mask);
if (!socket) if (!socket)
die("startup: display_add_socket_auto"); die("startup: display_add_socket_auto");
setenv("WAYLAND_DISPLAY", socket, 1); setenv("WAYLAND_DISPLAY", socket, 1);
@ -1913,7 +1915,7 @@ run(char *startup_cmd)
close(piperw[0]); close(piperw[0]);
} }
/* If nobody is reading the status output, don't terminate */ /* If nobody is reading the status output, don't terminate */
signal(SIGPIPE, SIG_IGN); sigaction(SIGPIPE, &sa, NULL);
printstatus(); printstatus();
/* At this point the outputs are initialized, choose initial selmon based on /* At this point the outputs are initialized, choose initial selmon based on
@ -2066,18 +2068,26 @@ setsel(struct wl_listener *listener, void *data)
void void
setup(void) setup(void)
{ {
struct sigaction sa_term = {.sa_flags = SA_RESTART, .sa_handler = quitsignal};
struct sigaction sa_sigchld = {
#ifdef XWAYLAND
.sa_flags = SA_RESTART,
.sa_handler = sigchld,
#else
.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART,
.sa_handler = SIG_IGN,
#endif
};
sigemptyset(&sa_term.sa_mask);
sigemptyset(&sa_sigchld.sa_mask);
/* The Wayland display is managed by libwayland. It handles accepting /* The Wayland display is managed by libwayland. It handles accepting
* clients from the Unix socket, manging Wayland globals, and so on. */ * clients from the Unix socket, manging Wayland globals, and so on. */
dpy = wl_display_create(); dpy = wl_display_create();
/* Set up signal handlers */ /* Set up signal handlers */
#ifdef XWAYLAND sigaction(SIGCHLD, &sa_sigchld, NULL);
sigchld(0); sigaction(SIGINT, &sa_term, NULL);
#else sigaction(SIGTERM, &sa_term, NULL);
signal(SIGCHLD, SIG_IGN);
#endif
signal(SIGINT, quitsignal);
signal(SIGTERM, quitsignal);
/* The backend is a wlroots feature which abstracts the underlying input and /* The backend is a wlroots feature which abstracts the underlying input and
* output hardware. The autocreate option will choose the most suitable * output hardware. The autocreate option will choose the most suitable
@ -2699,12 +2709,11 @@ sigchld(int unused)
{ {
siginfo_t in; siginfo_t in;
/* We should be able to remove this function in favor of a simple /* We should be able to remove this function in favor of a simple
* signal(SIGCHLD, SIG_IGN); * struct sigaction sa = {.sa_handler = SIG_IGN};
* sigaction(SIGCHLD, &sa, NULL);
* but the Xwayland implementation in wlroots currently prevents us from * but the Xwayland implementation in wlroots currently prevents us from
* setting our own disposition for SIGCHLD. * setting our own disposition for SIGCHLD.
*/ */
if (signal(SIGCHLD, sigchld) == SIG_ERR)
die("can't install SIGCHLD handler:");
/* WNOWAIT leaves the child in a waitable state, in case this is the /* WNOWAIT leaves the child in a waitable state, in case this is the
* XWayland process * XWayland process
*/ */