From: Claudio Takahasi Date: Thu, 6 Mar 2014 13:44:25 +0000 (-0300) Subject: test: Add signal handling for gatt-service X-Git-Tag: android-x86-4.4-r3~4934 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=c3fd22e8b1c69c1156c66b6f428fd50a23a87249;p=android-x86%2Fexternal-bluetooth-bluez.git test: Add signal handling for gatt-service This patch implements signal handling to run cleanup tasks before exiting. --- diff --git a/test/gatt-service.c b/test/gatt-service.c index 18ab7dacc..ecb5cc38e 100644 --- a/test/gatt-service.c +++ b/test/gatt-service.c @@ -27,6 +27,9 @@ #include #include +#include +#include +#include #include #include @@ -97,9 +100,83 @@ static void create_services(DBusConnection *conn) printf("Registered service: %s\n", service_path); } +static gboolean signal_handler(GIOChannel *channel, GIOCondition cond, + gpointer 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) { + case SIGINT: + case SIGTERM: + if (!__terminated) { + printf("Terminating\n"); + g_main_loop_quit(main_loop); + } + + __terminated = true; + 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); + + 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; +} + int main(int argc, char *argv[]) { DBusConnection *dbus_conn; + guint signal; + + signal = setup_signalfd(); + if (signal == 0) + return -errno; dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL); @@ -114,6 +191,8 @@ int main(int argc, char *argv[]) g_main_loop_run(main_loop); + g_source_remove(signal); + g_slist_free_full(services, g_free); dbus_connection_unref(dbus_conn);