Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Created October 21, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdamjan/a06602fd5bffc5ae500b59bb07729130 to your computer and use it in GitHub Desktop.
Save gdamjan/a06602fd5bffc5ae500b59bb07729130 to your computer and use it in GitHub Desktop.
signal handling wl_event_loop after daemon() is broken
/*
* compile with: gcc test.c -o test -l wayland-server
*
*/
#define _DEFAULT_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-server.h>
static struct wl_event_loop *event_loop;
static int handle_signal(int sig, void *data) {
switch (sig) {
case SIGTERM:
fprintf(stderr, "got signal \"SIGTERM\", terminating.\n");
wl_event_loop_destroy(event_loop);
exit(0);
break;
default:
fprintf(stderr, "got signal \"%s\", ignoring.\n", strsignal(sig));
return 0;
}
}
int main(int argc, char *argv[]) {
event_loop = wl_event_loop_create();
wl_event_loop_add_signal(event_loop, SIGHUP, handle_signal, NULL);
wl_event_loop_add_signal(event_loop, SIGINT, handle_signal, NULL);
wl_event_loop_add_signal(event_loop, SIGTERM, handle_signal, NULL);
daemon(1, 1); // comment this out and in, signal handling is different
fprintf(stderr, "%s started. pid: %d\n", argv[0], getpid());
while (wl_event_loop_dispatch(event_loop, -1) != 1) {
// event loop just runs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment