core: Make use of mainloop_run_with_signal
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 28 Nov 2018 14:27:38 +0000 (16:27 +0200)
committerhimanshu <h.himanshu@samsung.com>
Wed, 22 Jan 2020 06:53:08 +0000 (12:23 +0530)
This don't require setting up signalfd.

Change-Id: I662835f2fb18e3d30aca89dc2150b977a46db939
Signed-off-by: himanshu <h.himanshu@samsung.com>
src/main.c

index d6b3713..08bdc8e 100755 (executable)
@@ -534,24 +534,11 @@ static gboolean quit_eventloop(gpointer user_data)
        return FALSE;
 }
 
-static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
-                                                       gpointer user_data)
+static void signal_callback(int signum, void *user_data)
 {
        static bool terminated = false;
-       struct signalfd_siginfo si;
-       ssize_t result;
-       int fd;
 
-       if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
-               return FALSE;
-
-       fd = g_io_channel_unix_get_fd(channel);
-
-       result = read(fd, &si, sizeof(si));
-       if (result != sizeof(si))
-               return FALSE;
-
-       switch (si.ssi_signo) {
+       switch (signum) {
        case SIGINT:
        case SIGTERM:
                if (!terminated) {
@@ -569,46 +556,6 @@ static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
                __btd_toggle_debug();
                break;
        }
-
-       return TRUE;
-}
-
-static guint setup_signalfd(void)
-{
-       GIOChannel *channel;
-       guint source;
-       sigset_t mask;
-       int fd;
-
-       sigemptyset(&mask);
-       sigaddset(&mask, SIGINT);
-       sigaddset(&mask, SIGTERM);
-       sigaddset(&mask, SIGUSR2);
-
-       if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
-               perror("Failed to set signal mask");
-               return 0;
-       }
-
-       fd = signalfd(-1, &mask, 0);
-       if (fd < 0) {
-               perror("Failed to create signal descriptor");
-               return 0;
-       }
-
-       channel = g_io_channel_unix_new(fd);
-
-       g_io_channel_set_close_on_unref(channel, TRUE);
-       g_io_channel_set_encoding(channel, NULL, NULL);
-       g_io_channel_set_buffered(channel, FALSE);
-
-       source = g_io_add_watch(channel,
-                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-                               signal_handler, NULL);
-
-       g_io_channel_unref(channel);
-
-       return source;
 }
 
 static char *option_debug = NULL;
@@ -719,7 +666,6 @@ int main(int argc, char *argv[])
        uint16_t sdp_mtu = 0;
        uint32_t sdp_flags = 0;
        int gdbus_flags = 0;
-       guint signal;
 
        init_defaults();
 
@@ -748,8 +694,6 @@ int main(int argc, char *argv[])
 
        mainloop_init();
 
-       signal = setup_signalfd();
-
        __btd_log_init(option_debug, option_detach);
 
        g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
@@ -818,7 +762,7 @@ int main(int argc, char *argv[])
        mainloop_sd_notify("STATUS=Running");
        mainloop_sd_notify("READY=1");
 
-       mainloop_run();
+       mainloop_run_with_signal(signal_callback, NULL);
 
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        sd_notify(0, "STOPPING=1");
@@ -826,8 +770,6 @@ int main(int argc, char *argv[])
 
        mainloop_sd_notify("STATUS=Quitting");
 
-       g_source_remove(signal);
-
        plugin_cleanup();
 
        btd_profile_cleanup();