$OpenBSD$

Index: src/flb_engine.c
--- src/flb_engine.c.orig
+++ src/flb_engine.c
@@ -52,6 +52,8 @@
 #include <fluent-bit/stream_processor/flb_sp.h>
 #endif
 
+static int flb_engine_handle_signal(struct flb_config *config);
+
 int flb_engine_destroy_tasks(struct mk_list *tasks)
 {
     int c = 0;
@@ -523,7 +525,10 @@ int flb_engine_start(struct flb_config *config)
     flb_engine_started(config);
 
     while (1) {
-        mk_event_wait(evl);
+        if (mk_event_wait(evl) == -1 && errno == EINTR &&
+            flb_engine_handle_signal(config) == 0)
+                    return (0);
+
         mk_event_foreach(event, evl) {
             if (event->type == FLB_ENGINE_EV_CORE) {
                 ret = flb_engine_handle_event(event->fd, event->mask, config);
@@ -595,6 +600,32 @@ int flb_engine_start(struct flb_config *config)
             flb_upstream_conn_timeouts(config);
         }
     }
+}
+
+sig_atomic_t flb_engine_signo = 0;
+static int flb_engine_handle_signal(struct flb_config *config)
+{
+    /* Signal handlers */
+    switch (flb_engine_signo) {
+    case SIGINT:
+#ifndef _WIN32
+    case SIGQUIT:
+    case SIGHUP:
+#endif
+        flb_engine_shutdown(config);
+#ifdef FLB_HAVE_MTRACE
+        /* Stop tracing malloc and free */
+        muntrace();
+#endif
+        break;
+    case SIGTERM:
+        flb_engine_exit(config);
+        break;
+    default:
+        return (-1);
+    }
+
+    return (0);
 }
 
 /* Release all resources associated to the engine */
