All of lore.kernel.org
 help / color / mirror / Atom feed
From: Claudio Takahasi <claudio.takahasi@openbossa.org>
To: linux-bluetooth@vger.kernel.org
Cc: claudio.takahasi@openbossa.org
Subject: [PATCH BlueZ v7 09/11] test: Add signal handling for gatt-service
Date: Wed, 19 Feb 2014 15:51:31 -0300	[thread overview]
Message-ID: <1392835893-6723-10-git-send-email-claudio.takahasi@openbossa.org> (raw)
In-Reply-To: <1392835893-6723-1-git-send-email-claudio.takahasi@openbossa.org>

This patch implements signal handling to run cleanup tasks before
exiting.
---
 test/gatt-service.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/test/gatt-service.c b/test/gatt-service.c
index 769fd37..4059336 100644
--- a/test/gatt-service.c
+++ b/test/gatt-service.c
@@ -27,6 +27,9 @@
 
 #include <errno.h>
 #include <stdio.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <sys/signalfd.h>
 
 #include <glib.h>
 #include <dbus/dbus.h>
@@ -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);
 
-- 
1.8.3.1


  parent reply	other threads:[~2014-02-19 18:51 UTC|newest]

Thread overview: 123+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-21 13:26 [PATCH BlueZ v2 00/18] GATT API: External Services Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 01/18] doc: Add GATT API Claudio Takahasi
2014-01-21 16:50   ` Marcel Holtmann
2014-01-21 19:40     ` Claudio Takahasi
2014-01-21 19:48       ` Johan Hedberg
2014-01-21 19:51         ` Marcel Holtmann
2014-01-21 13:26 ` [PATCH BlueZ v2 02/18] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 03/18] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 04/18] gatt: Add registering external service Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 05/18] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 06/18] gatt: Add helper for creating GATT services Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 07/18] gatt: Add external services tracking Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 08/18] gatt: Add server unix socket Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 09/18] gattrib: Use default ATT LE MTU for non-standard sockets Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 10/18] gatt: Register ATT command/event handler Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 11/18] gatt: Add Discover All Primary Services Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 12/18] test: Add external service GATT skeleton Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 13/18] gitignore: Add test/gatt-service Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 14/18] test: Add signal handling for gatt-service Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 15/18] test: Add registering external service Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 16/18] gatttool: Add unix socket connect Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 17/18] gatttool: Add unix socket support for interactive mode Claudio Takahasi
2014-01-21 13:26 ` [PATCH BlueZ v2 18/18] bluetooth.conf: Add ObjectManager interface Claudio Takahasi
2014-01-22 13:40 ` [PATCH BlueZ v3 00/18] GATT API: External Services Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 01/18] doc: Add GATT API Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 02/18] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 03/18] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 04/18] gatt: Add registering external service Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 05/18] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 06/18] gatt: Add helper for creating GATT services Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 07/18] gatt: Add external services tracking Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 08/18] gatt: Add server unix socket Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 09/18] gattrib: Use default ATT LE MTU for non-standard sockets Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 10/18] gatt: Register ATT command/event handler Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 11/18] gatt: Add Discover All Primary Services Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 12/18] test: Add external service GATT skeleton Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 13/18] gitignore: Add test/gatt-service Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 14/18] test: Add signal handling for gatt-service Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 15/18] test: Add registering external service Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 16/18] gatttool: Add unix socket connect Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 17/18] gatttool: Add unix socket support for interactive mode Claudio Takahasi
2014-01-22 13:40   ` [PATCH BlueZ v3 18/18] bluetooth.conf: Add ObjectManager interface Claudio Takahasi
2014-01-27 18:00   ` [PATCH BlueZ v4 00/18] GATT API: External Services Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 01/18] doc: Add GATT API Claudio Takahasi
2014-01-27 18:15       ` Marcel Holtmann
2014-01-27 18:54         ` Claudio Takahasi
2014-01-27 19:08           ` Marcel Holtmann
2014-01-27 18:00     ` [PATCH BlueZ v4 02/18] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 03/18] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 04/18] gatt: Add registering external service Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 05/18] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 06/18] gatt: Add helper for creating GATT services Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 07/18] gatt: Add external services tracking Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 08/18] gatt: Add server unix socket Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 09/18] gattrib: Use default ATT LE MTU for non-standard sockets Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 10/18] gatt: Register ATT command/event handler Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 11/18] gatt: Add Discover All Primary Services Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 12/18] test: Add external service GATT skeleton Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 13/18] gitignore: Add test/gatt-service Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 14/18] test: Add signal handling for gatt-service Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 15/18] test: Add registering external service Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 16/18] gatttool: Add unix socket connect Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 17/18] gatttool: Add unix socket support for interactive mode Claudio Takahasi
2014-01-27 18:00     ` [PATCH BlueZ v4 18/18] bluetooth.conf: Add ObjectManager interface Claudio Takahasi
2014-01-27 20:35     ` [PATCH BlueZ v5 00/18] GATT API: External Services Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 01/18] doc: Add experimental GATT API Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 02/18] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 03/18] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 04/18] gatt: Add registering external service Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 05/18] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 06/18] gatt: Add helper for creating GATT services Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 07/18] gatt: Add external services tracking Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 08/18] gatt: Add server unix socket Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 09/18] gattrib: Use default ATT LE MTU for non-standard sockets Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 10/18] gatt: Register ATT command/event handler Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 11/18] gatt: Add Discover All Primary Services Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 12/18] test: Add external service GATT skeleton Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 13/18] gitignore: Add test/gatt-service Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 14/18] test: Add signal handling for gatt-service Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 15/18] test: Add registering external service Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 16/18] gatttool: Add unix socket connect Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 17/18] gatttool: Add unix socket support for interactive mode Claudio Takahasi
2014-01-27 20:35       ` [PATCH BlueZ v5 18/18] bluetooth.conf: Add ObjectManager interface Claudio Takahasi
2014-02-04 17:53       ` [PATCH BlueZ v6 00/18] GATT API: External Services Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 01/18] doc: Add experimental GATT API Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 02/18] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 03/18] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 04/18] gatt: Add registering external service Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 05/18] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 06/18] gatt: Add helper for creating GATT services Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 07/18] gatt: Add external services tracking Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 08/18] gatt: Add server unix socket Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 09/18] gattrib: Use default ATT LE MTU for non-standard sockets Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 10/18] gatt: Register ATT command/event handler Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 11/18] gatt: Add Discover All Primary Services Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 12/18] test: Add external service GATT skeleton Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 13/18] gitignore: Add test/gatt-service Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 14/18] test: Add signal handling for gatt-service Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 15/18] test: Add registering external service Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 16/18] gatttool: Add unix socket connect Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 17/18] gatttool: Add unix socket support for interactive mode Claudio Takahasi
2014-02-04 17:53         ` [PATCH BlueZ v6 18/18] bluetooth.conf: Add ObjectManager interface Claudio Takahasi
2014-02-18 13:41         ` [PATCH BlueZ v6 00/18] GATT API: External Services Claudio Takahasi
2014-02-18 17:44         ` Marcel Holtmann
2014-02-18 18:08           ` Claudio Takahasi
2014-02-18 18:17             ` Marcel Holtmann
2014-02-19 18:51               ` [PATCH BlueZ v7 00/11] " Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 01/11] doc: Add experimental GATT API Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 02/11] gatt: Add stub for gatt.{c, h} files Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 03/11] gatt: Register Manager D-Bus Interface Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 04/11] gatt: Add registering external service Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 05/11] lib: Move GATT UUID to uuid.h Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 06/11] gatt: Add helper for creating GATT services Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 07/11] gatt: Add external services tracking Claudio Takahasi
2014-02-24  8:33                   ` Johan Hedberg
2014-02-24 12:49                     ` Anderson Lizardo
2014-02-24 12:51                       ` Anderson Lizardo
2014-02-25  6:26                         ` Johan Hedberg
2014-02-27 21:47                           ` Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 08/11] test: Add external service GATT skeleton Claudio Takahasi
2014-02-19 18:51                 ` Claudio Takahasi [this message]
2014-02-19 18:51                 ` [PATCH BlueZ v7 10/11] test: Add registering external service Claudio Takahasi
2014-02-19 18:51                 ` [PATCH BlueZ v7 11/11] bluetooth.conf: Add ObjectManager interface Claudio Takahasi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392835893-6723-10-git-send-email-claudio.takahasi@openbossa.org \
    --to=claudio.takahasi@openbossa.org \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.