#include <stdio.h>
 #include <string.h>
 #include <getopt.h>
+#include <signal.h>
 
 static const struct option longopts[] = {
        { "help",       no_argument,    NULL,   'h' },
        printf("  -l, --active-low:\tset the line active state to low\n");
 }
 
+static volatile bool do_run = true;
+
+static void sighandler(int signum UNUSED)
+{
+       do_run = false;
+}
+
 static int event_callback(int type, const struct timespec *ts,
                          void *data UNUSED)
 {
                printf("GPIO EVENT: %s [%8ld.%09ld]\n",
                       evname, ts->tv_sec, ts->tv_nsec);
 
+       if (!do_run)
+               return GPIOD_EVENT_CB_STOP;
+
        return GPIOD_EVENT_CB_OK;
 }
 
        timeout.tv_sec = 0;
        timeout.tv_nsec = 500000000;
 
+       signal(SIGINT, sighandler);
+       signal(SIGTERM, sighandler);
+
        status = gpiod_simple_event_loop(device, offset, active_low,
                                         &timeout, event_callback, NULL);
        if (status < 0)