Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2 10/10] shared/tester: Make use of mainloop_run_with_signal
Date: Wed, 28 Nov 2018 17:00:23 +0200
Message-ID: <20181128150023.1576-10-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20181128150023.1576-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This don't require setting up signalfd.
---
 src/shared/tester.c | 79 +++++----------------------------------------
 1 file changed, 8 insertions(+), 71 deletions(-)

diff --git a/src/shared/tester.c b/src/shared/tester.c
index 168d0de91..3d79e411d 100644
--- a/src/shared/tester.c
+++ b/src/shared/tester.c
@@ -44,6 +44,7 @@
 #include <valgrind/memcheck.h>
 #endif
 
+#include "src/shared/mainloop.h"
 #include "src/shared/util.h"
 #include "src/shared/tester.h"
 #include "src/shared/log.h"
@@ -105,7 +106,6 @@ struct test_case {
 	void *user_data;
 };
 
-static GMainLoop *main_loop;
 static char *tester_name;
 
 static GList *test_list;
@@ -463,7 +463,7 @@ static void next_test_case(void)
 	if (!test_current) {
 		g_timer_stop(test_timer);
 
-		g_main_loop_quit(main_loop);
+		mainloop_quit();
 		return;
 	}
 
@@ -778,73 +778,19 @@ void tester_wait(unsigned int seconds, tester_wait_func_t func,
 	print_progress(test->name, COLOR_BLACK, "waiting %u seconds", seconds);
 }
 
-static gboolean signal_handler(GIOChannel *channel, GIOCondition condition,
-							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 (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
-		g_main_loop_quit(main_loop);
-		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)
-			g_main_loop_quit(main_loop);
+			mainloop_quit();
 
 		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;
 }
 
 bool tester_use_quiet(void)
@@ -897,7 +843,7 @@ void tester_init(int *argc, char ***argv)
 		exit(EXIT_SUCCESS);
 	}
 
-	main_loop = g_main_loop_new(NULL, FALSE);
+	mainloop_init();
 
 	tester_name = strrchr(*argv[0], '/');
 	if (!tester_name)
@@ -911,25 +857,16 @@ void tester_init(int *argc, char ***argv)
 
 int tester_run(void)
 {
-	guint signal;
 	int ret;
 
-	if (!main_loop)
-		return EXIT_FAILURE;
-
 	if (option_list) {
-		g_main_loop_unref(main_loop);
+		mainloop_quit();
 		return EXIT_SUCCESS;
 	}
 
-	signal = setup_signalfd();
-
 	g_idle_add(start_tester, NULL);
-	g_main_loop_run(main_loop);
-
-	g_source_remove(signal);
 
-	g_main_loop_unref(main_loop);
+	mainloop_run_with_signal(signal_callback, NULL);
 
 	ret = tester_summarize();
 
-- 
2.17.2


      parent reply index

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 04/10] core: Use mainloop_sd_notify " Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 05/10] core: Remove old code related to sd_notify Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 09/10] core: Make use of mainloop_run_with_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` Luiz Augusto von Dentz [this message]

Reply instructions:

You may reply publically 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=20181128150023.1576-10-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --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

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org linux-bluetooth@archiver.kernel.org
	public-inbox-index linux-bluetooth


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/ public-inbox