Skip to contentSkip to conversation

share/mainloop: Add handling of NOTIFY_SOCKET

PATCH Applied Luiz Augusto von Dentz 1 participant 11 messages Nov 30, 2018 – Dec 5, 2018

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v3 1/10 share/mainloop: Add handling of NOTIFY_SOCKET

From: Luiz Augusto von Dentz <[email protected]>

This adds handling of systemd NOTIFY_SOCKET so application using mainloop instance do properly notify systemd what is their state.

---
 Makefile.am                  |  8 +++-
 src/shared/mainloop-glib.c   |  6 +++
 src/shared/mainloop-notify.c | 93 ++++++++++++++++++++++++++++++++++++
 src/shared/mainloop-notify.h | 25 ++++++++++
 src/shared/mainloop.c        | 10 ++++
 src/shared/mainloop.h        |  1 +
 6 files changed, 141 insertions(+), 2 deletions(-)
 create mode 100644 src/shared/mainloop-notify.c
 create mode 100644 src/shared/mainloop-notify.h
Makefile.am +6 -2
index 0b26ccc3e..124c32482 100644
@@ -130,12 +130,16 @@ endif
 src_libshared_glib_la_SOURCES = $(shared_sources) \
 				src/shared/io-glib.c \
 				src/shared/timeout-glib.c \
-				src/shared/mainloop-glib.c
+				src/shared/mainloop-glib.c \
+				src/shared/mainloop-notify.h \
+				src/shared/mainloop-notify.c
 
 src_libshared_mainloop_la_SOURCES = $(shared_sources) \
 				src/shared/io-mainloop.c \
 				src/shared/timeout-mainloop.c \
-				src/shared/mainloop.h src/shared/mainloop.c
+				src/shared/mainloop.h src/shared/mainloop.c \
+				src/shared/mainloop-notify.h \
+				src/shared/mainloop-notify.c
 
 if ELL
 src_libshared_ell_la_SOURCES = $(shared_sources) \
src/shared/mainloop-glib.c +6 -0
index 8436969bb..42abfddf8 100644
@@ -36,6 +36,7 @@
 #include <glib.h>
 
 #include "mainloop.h"
+#include "mainloop-notify.h"
 
 static GMainLoop *main_loop;
 static int exit_status;
@@ -43,6 +44,7 @@ static int exit_status;
 void mainloop_init(void)
 {
 	main_loop = g_main_loop_new(NULL, FALSE);
+	mainloop_notify_init();
 }
 
 void mainloop_quit(void)
@@ -51,6 +53,8 @@ void mainloop_quit(void)
 		return;
 
 	g_main_loop_quit(main_loop);
+
+	mainloop_sd_notify("STOPPING=1");
 }
 
 void mainloop_exit_success(void)
@@ -75,6 +79,8 @@ int mainloop_run(void)
 	g_main_loop_unref(main_loop);
 	main_loop = NULL;
 
+	mainloop_notify_exit();
+
 	return exit_status;
 }
 
src/shared/mainloop-notify.c new file +93 -0

93 lines not on this page · raw message

src/shared/mainloop-notify.h new file +25 -0
new file mode 100644
index 000000000..721b5fbed
@@ -0,0 +1,25 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+void mainloop_notify_init(void);
+void mainloop_notify_exit(void);
src/shared/mainloop.c +10 -0
index e6ab9c43d..e9ec6e8cd 100644
@@ -30,13 +30,17 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <signal.h>
 #include <sys/signalfd.h>
 #include <sys/timerfd.h>
 #include <sys/epoll.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include "mainloop.h"
+#include "mainloop-notify.h"
 
 #define MAX_EPOLL_EVENTS 10
 
@@ -83,11 +87,15 @@ void mainloop_init(void)
 		mainloop_list[i] = NULL;
 
 	epoll_terminate = 0;
+
+	mainloop_notify_init();
 }
 
 void mainloop_quit(void)
 {
 	epoll_terminate = 1;
+
+	mainloop_sd_notify("STOPPING=1");
 }
 
 void mainloop_exit_success(void)
@@ -183,6 +191,8 @@ int mainloop_run(void)
 	close(epoll_fd);
 	epoll_fd = 0;
 
+	mainloop_notify_exit();
+
 	return exit_status;
 }
 
src/shared/mainloop.h +1 -0
index b83caabc8..73ed81187 100644
@@ -49,3 +49,4 @@ int mainloop_remove_timeout(int id);
 
 int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback,
 				void *user_data, mainloop_destroy_func destroy);
+int mainloop_sd_notify(const char *state);
Signature · 1 line
2.17.2
author maintainer
#1 lore raw

PATCH v3 2/10 share/mainloop: Add watchdog support

From: Luiz Augusto von Dentz <[email protected]>

This adds watchdog notification support by sending "WATCHDOG=1" twice as frequent as required by WATCHDOG_USEC.

---
 src/shared/mainloop-notify.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
src/shared/mainloop-notify.c +28 -0
index fdc2f25ec..2e3b08f06 100644
@@ -37,13 +37,27 @@
 
 #include "mainloop.h"
 #include "mainloop-notify.h"
+#include "timeout.h"
+
+#define WATCHDOG_TRIGGER_FREQ 2
 
 static int notify_fd = -1;
 
+static unsigned int watchdog;
+
+static bool watchdog_callback(void *user_data)
+{
+	mainloop_sd_notify("WATCHDOG=1");
+
+	return true;
+}
+
 void mainloop_notify_init(void)
 {
 	const char *sock;
 	struct sockaddr_un addr;
+	const char *watchdog_usec;
+	int msec;
 
 	sock = getenv("NOTIFY_SOCKET");
 	if (!sock)
@@ -67,7 +81,19 @@ void mainloop_notify_init(void)
 	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		close(notify_fd);
 		notify_fd = -1;
+		return;
 	}
+
+	watchdog_usec = getenv("WATCHDOG_USEC");
+	if (!watchdog_usec)
+		return;
+
+	msec = atoi(watchdog_usec) / 1000;
+	if (msec < 0)
+		return;
+
+	watchdog = timeout_add(msec / WATCHDOG_TRIGGER_FREQ,
+				watchdog_callback, NULL, NULL);
 }
 
 void mainloop_notify_exit(void)
@@ -76,6 +102,8 @@ void mainloop_notify_exit(void)
 		close(notify_fd);
 		notify_fd = -1;
 	}
+
+	timeout_remove(watchdog);
 }
 
 int mainloop_sd_notify(const char *state)
Signature · 1 line
2.17.2
author maintainer
#2 lore raw

PATCH v3 3/10 tool/btmon-logger: Use mainloop_notify instead of sd_notify

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

mainloop_notify takes care of sending the messages to NOTIFY_SOCKET and includes the handling of WATCHDOG_USEC as well.

---
 tools/btmon-logger.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/btmon-logger.c +4 -6
index c3ba17939..1a42824ee 100644
@@ -48,8 +48,6 @@
 #include "src/shared/mainloop.h"
 #include "src/shared/btsnoop.h"
 
-#include "src/systemd.h"
-
 #define MONITOR_INDEX_NONE 0xffff
 
 struct monitor_hdr {
@@ -283,7 +281,7 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sd_notify(0, "STATUS=Starting up");
+	mainloop_sd_notify("STATUS=Starting up");
 
 	while (true) {
 		int opt;
@@ -375,12 +373,12 @@ int main(int argc, char *argv[])
 
 	printf("Bluetooth monitor logger ver %s\n", VERSION);
 
-	sd_notify(0, "STATUS=Running");
-	sd_notify(0, "READY=1");
+	mainloop_sd_notify("STATUS=Running");
+	mainloop_sd_notify("READY=1");
 
 	exit_status = mainloop_run();
 
-	sd_notify(0, "STATUS=Quitting");
+	mainloop_sd_notify("STATUS=Quitting");
 
 	btsnoop_unref(btsnoop_file);
 
Signature · 1 line
2.17.2
author maintainer
#3 lore raw

PATCH v3 4/10 core: Use mainloop_sd_notify instead of sd_notify

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

mainloop_sd_notify takes care of sending the messages to NOTIFY_SOCKET and includes the handling of WATCHDOG_USEC as well.

---
 src/main.c | 51 +++++++++++----------------------------------------
 1 file changed, 11 insertions(+), 40 deletions(-)
src/main.c +11 -40
index 4716f5388..7ab1349cc 100644
@@ -51,6 +51,7 @@
 #include "backtrace.h"
 
 #include "shared/att-types.h"
+#include "shared/mainloop.h"
 #include "lib/uuid.h"
 #include "hcid.h"
 #include "sdpd.h"
@@ -59,7 +60,6 @@
 #include "dbus-common.h"
 #include "agent.h"
 #include "profile.h"
-#include "systemd.h"
 
 #define BLUEZ_NAME "org.bluez"
 
@@ -486,11 +486,9 @@ static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
 	btd_backtrace(0xffff);
 }
 
-static GMainLoop *event_loop;
-
 void btd_exit(void)
 {
-	g_main_loop_quit(event_loop);
+	mainloop_quit();
 }
 
 static gboolean quit_eventloop(gpointer user_data)
@@ -524,7 +522,7 @@ static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
 			g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
 							quit_eventloop, NULL);
 
-			sd_notify(0, "STATUS=Powering down");
+			mainloop_sd_notify("STATUS=Powering down");
 			adapter_shutdown();
 		}
 
@@ -616,7 +614,7 @@ static void disconnect_dbus(void)
 static void disconnected_dbus(DBusConnection *conn, void *data)
 {
 	info("Disconnected from D-Bus. Exiting.");
-	g_main_loop_quit(event_loop);
+	mainloop_quit();
 }
 
 static int connect_dbus(void)
@@ -644,13 +642,6 @@ static int connect_dbus(void)
 	return 0;
 }
 
-static gboolean watchdog_callback(gpointer user_data)
-{
-	sd_notify(0, "WATCHDOG=1");
-
-	return TRUE;
-}
-
 static gboolean parse_debug(const char *key, const char *value,
 				gpointer user_data, GError **error)
 {
@@ -691,8 +682,7 @@ int main(int argc, char *argv[])
 	uint16_t sdp_mtu = 0;
 	uint32_t sdp_flags = 0;
 	int gdbus_flags = 0;
-	guint signal, watchdog;
-	const char *watchdog_usec;
+	guint signal;
 
 	init_defaults();
 
@@ -719,7 +709,7 @@ int main(int argc, char *argv[])
 
 	btd_backtrace_init();
 
-	event_loop = g_main_loop_new(NULL, FALSE);
+	mainloop_init();
 
 	signal = setup_signalfd();
 
@@ -729,7 +719,7 @@ int main(int argc, char *argv[])
 							G_LOG_FLAG_RECURSION,
 							log_handler, NULL);
 
-	sd_notify(0, "STATUS=Starting up");
+	mainloop_sd_notify("STATUS=Starting up");
 
 	if (option_configfile)
 		main_conf_file_path = option_configfile;
@@ -788,26 +778,12 @@ int main(int argc, char *argv[])
 
 	DBG("Entering main loop");
 
-	sd_notify(0, "STATUS=Running");
-	sd_notify(0, "READY=1");
+	mainloop_sd_notify("STATUS=Running");
+	mainloop_sd_notify("READY=1");
 
-	watchdog_usec = getenv("WATCHDOG_USEC");
-	if (watchdog_usec) {
-		unsigned int seconds;
+	mainloop_run();
 
-		seconds = atoi(watchdog_usec) / (1000 * 1000);
-		info("Watchdog timeout is %d seconds", seconds);
-
-		watchdog = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
-							seconds / 2,
-							watchdog_callback,
-							NULL, NULL);
-	} else
-		watchdog = 0;
-
-	g_main_loop_run(event_loop);
-
-	sd_notify(0, "STATUS=Quitting");
+	mainloop_sd_notify("STATUS=Quitting");
 
 	g_source_remove(signal);
 
@@ -824,8 +800,6 @@ int main(int argc, char *argv[])
 	if (main_opts.mode != BT_MODE_LE)
 		stop_sdp_server();
 
-	g_main_loop_unref(event_loop);
-
 	if (main_conf)
 		g_key_file_free(main_conf);
 
@@ -833,9 +807,6 @@ int main(int argc, char *argv[])
 
 	info("Exit");
 
-	if (watchdog > 0)
-		g_source_remove(watchdog);
-
 	__btd_log_cleanup();
 
 	return 0;
Signature · 1 line
2.17.2
author maintainer
#4 lore raw

PATCH v3 5/10 core: Remove old code related to sd_notify

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This is no longer needed since mainloop instances can handle it now.

---
 Makefile.am    |   1 -
 Makefile.tools |   2 +-
 src/systemd.c  | 107 -------------------------------------------------
 src/systemd.h  |  28 -------------
 4 files changed, 1 insertion(+), 137 deletions(-)
 delete mode 100644 src/systemd.c
 delete mode 100644 src/systemd.h
Makefile.am +0 -1
index 124c32482..a69baea10 100644
@@ -185,7 +185,6 @@ src_bluetoothd_SOURCES = $(builtin_sources) \
 			src/bluetooth.ver \
 			src/main.c src/log.h src/log.c \
 			src/backtrace.h src/backtrace.c \
-			src/systemd.h src/systemd.c \
 			src/rfkill.c src/hcid.h src/sdpd.h \
 			src/sdpd-server.c src/sdpd-request.c \
 			src/sdpd-service.c src/sdpd-database.c \
Makefile.tools +1 -1
index 35412ca61..d3d901bc9 100644
@@ -46,7 +46,7 @@ endif
 if LOGGER
 libexec_PROGRAMS += tools/btmon-logger
 
-tools_btmon_logger_SOURCES = tools/btmon-logger.c src/systemd.c src/systemd.h
+tools_btmon_logger_SOURCES = tools/btmon-logger.c
 tools_btmon_logger_LDADD = src/libshared-mainloop.la
 tools_btmon_logger_DEPENDENCIES = src/libshared-mainloop.la \
 					tools/bluetooth-logger.service
src/systemd.c deleted +0 -107

107 lines not on this page · raw message

src/systemd.h deleted +0 -28
deleted file mode 100644
index 0ef7c82a9..000000000
@@ -1,28 +0,0 @@
-/*
- *
- *  BlueZ - Bluetooth protocol stack for Linux
- *
- *  Copyright (C) 2012  Intel Corporation. All rights reserved.
- *
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- */
-
-#define SD_LISTEN_FDS_START 3
-
-int sd_listen_fds(int unset_environment);
-
-int sd_notify(int unset_environment, const char *state);
Signature · 1 line
2.17.2
author maintainer
#5 lore raw

PATCH v3 6/10 shared/timeout-glib: Check 0 id when removing timeout

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

If the id is 0 that makes it is invalid already.

---
 src/shared/timeout-glib.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/shared/timeout-glib.c +5 -1
index 4163bced7..fd71ca430 100644
@@ -71,8 +71,12 @@ unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
 
 void timeout_remove(unsigned int id)
 {
-	GSource *source = g_main_context_find_source_by_id(NULL, id);
+	GSource *source;
 
+	if (!id)
+		return;
+
+	source = g_main_context_find_source_by_id(NULL, id);
 	if (source)
 		g_source_destroy(source);
 }
Signature · 1 line
2.17.2
author maintainer
#6 lore raw

PATCH v3 7/10 shared/mainloop: Add mainloop_run_with_signal

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This consolidates the handling of signalfd in similar ways as ELL does.

---
 src/shared/mainloop-glib.c   |  1 +
 src/shared/mainloop-notify.c | 85 ++++++++++++++++++++++++++++++++++++
 src/shared/mainloop.h        |  1 +
 3 files changed, 87 insertions(+)
src/shared/mainloop-glib.c +1 -0
index 42abfddf8..e44197315 100644
@@ -37,6 +37,7 @@
 
 #include "mainloop.h"
 #include "mainloop-notify.h"
+#include "io.h"
 
 static GMainLoop *main_loop;
 static int exit_status;
src/shared/mainloop-notify.c +85 -0

107 lines not on this page · raw message

src/shared/mainloop.h +1 -0
index 73ed81187..1ede62797 100644
@@ -36,6 +36,7 @@ void mainloop_quit(void);
 void mainloop_exit_success(void);
 void mainloop_exit_failure(void);
 int mainloop_run(void);
+int mainloop_run_with_signal(mainloop_signal_func func, void *user_data);
 
 int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback,
 				void *user_data, mainloop_destroy_func destroy);
Signature · 1 line
2.17.2
author maintainer
#7 lore raw

PATCH v3 8/10 shared/mainloop: Remove mainloop_set_signal

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This removes mainloop_set_signal and replaces it usage with mainloop_run_with_signal.

---
 android/bluetoothd-snoop.c |  9 +----
 android/system-emulator.c  | 10 +----
 emulator/b1ee.c            |  9 +----
 emulator/hfp.c             | 10 +----
 emulator/main.c            |  9 +----
 monitor/main.c             |  9 +----
 peripheral/main.c          | 10 +----
 src/shared/mainloop.c      | 79 --------------------------------------
 src/shared/shell.c         | 54 ++------------------------
 tools/3dsp.c               |  9 +----
 tools/advtest.c            |  9 +----
 tools/bluemoon.c           |  9 +----
 tools/btattach.c           |  9 +----
 tools/btconfig.c           |  9 +----
 tools/btgatt-client.c      |  9 +----
 tools/btgatt-server.c      |  9 +----
 tools/btinfo.c             |  9 +----
 tools/btmon-logger.c       |  9 +----
 tools/btproxy.c            |  9 +----
 tools/eddystone.c          |  9 +----
 tools/ibeacon.c            |  9 +----
 tools/oobtest.c            |  9 +----
 22 files changed, 24 insertions(+), 292 deletions(-)
android/bluetoothd-snoop.c +1 -8
index 8d9a2d087..25cddee34 100644
@@ -219,7 +219,6 @@ static void set_capabilities(void)
 int main(int argc, char *argv[])
 {
 	const char *path;
-	sigset_t mask;
 
 	__btd_log_init(NULL, 0);
 
@@ -234,12 +233,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	if (!strcmp(DEFAULT_SNOOP_FILE, path))
 		rename(DEFAULT_SNOOP_FILE, DEFAULT_SNOOP_FILE ".old");
 
@@ -250,7 +243,7 @@ int main(int argc, char *argv[])
 
 	info("bluetoothd_snoop: started");
 
-	mainloop_run();
+	mainloop_run_with_signal(signal_callback, NULL);
 
 	close_monitor();
 
android/system-emulator.c +1 -9
index 1545dcea1..8bcf660e2 100644
@@ -216,19 +216,11 @@ static void signal_callback(int signum, void *user_data)
 int main(int argc, char *argv[])
 {
 	const char SYSTEM_SOCKET_PATH[] = "\0android_system";
-	sigset_t mask;
 	struct sockaddr_un addr;
 	int fd;
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-	sigaddset(&mask, SIGCHLD);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Android system emulator ver %s\n", VERSION);
 
 	snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
@@ -254,5 +246,5 @@ int main(int argc, char *argv[])
 	/* Make sure bluetoothd creates files with proper permissions */
 	umask(0177);
 
-	return mainloop_run();
+	return mainloop_run_with_signal(signal_callback, NULL);
 }
emulator/b1ee.c +1 -8
index 1253a3dca..0d5226fcc 100644
@@ -265,7 +265,6 @@ int main(int argc, char *argv[])
 	char *server_port = NULL, *sniffer_port = NULL;
 	int ret = EXIT_FAILURE;
 	ssize_t written;
-	sigset_t mask;
 
 	for (;;) {
 		int opt;
@@ -323,17 +322,11 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	mainloop_add_fd(sniffer_fd, EPOLLIN, sniffer_read_callback, NULL, NULL);
 	mainloop_add_fd(server_fd, EPOLLIN, server_read_callback, NULL, NULL);
 	mainloop_add_fd(vhci_fd, EPOLLIN, vhci_read_callback, NULL, NULL);
 
-	ret = mainloop_run();
+	ret = mainloop_run_with_signal(signal_callback, NULL);
 
 	goto done;
 
emulator/hfp.c +1 -9
index e70054adf..29ec63e7d 100644
@@ -103,18 +103,10 @@ static void signal_callback(int signum, void *user_data)
 
 int main(int argc, char *argv[])
 {
-	sigset_t mask;
-
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	if (!open_connection())
 		return EXIT_FAILURE;
 
-	return mainloop_run();
+	return mainloop_run_with_signal(signal_callback, NULL);
 }
emulator/main.c +1 -8
index 56246a3de..68c53488e 100644
@@ -90,7 +90,6 @@ int main(int argc, char *argv[])
 	int amptest_count = 0;
 	int vhci_count = 0;
 	enum vhci_type vhci_type = VHCI_TYPE_BREDRLE;
-	sigset_t mask;
 	int i;
 
 	mainloop_init();
@@ -154,12 +153,6 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluetooth emulator ver %s\n", VERSION);
 
 	for (i = 0; i < letest_count; i++) {
@@ -227,5 +220,5 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Failed to open monitor server\n");
 	}
 
-	return mainloop_run();
+	return mainloop_run_with_signal(signal_callback, NULL);
 }
monitor/main.c +1 -8
index 5fa87ea3f..8640c023d 100644
@@ -112,7 +112,6 @@ int main(int argc, char *argv[])
 	unsigned short ellisys_port = 0;
 	const char *str;
 	int exit_status;
-	sigset_t mask;
 
 	mainloop_init();
 
@@ -219,12 +218,6 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluetooth monitor ver %s\n", VERSION);
 
 	keys_setup();
@@ -258,7 +251,7 @@ int main(int argc, char *argv[])
 	if (tty && control_tty(tty, tty_speed) < 0)
 		return EXIT_FAILURE;
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	keys_cleanup();
 
peripheral/main.c +1 -9
index d7e10f3d3..93abe2042 100644
@@ -184,7 +184,6 @@ static void signal_callback(int signum, void *user_data)
 
 int main(int argc, char *argv[])
 {
-	sigset_t mask;
 	int exit_status;
 
 	if (getpid() == 1 && getppid() == 0)
@@ -192,13 +191,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-	sigaddset(&mask, SIGCHLD);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluetooth periperhal ver %s\n", VERSION);
 
 	prepare_filesystem();
@@ -235,7 +227,7 @@ int main(int argc, char *argv[])
 	if (is_init)
 		attach_start();
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	if (is_init)
 		attach_stop();
src/shared/mainloop.c +0 -79

104 lines not on this page · raw message

src/shared/shell.c +4 -50

83 lines not on this page · raw message

tools/3dsp.c +1 -8
index 686fe139d..77a70c037 100644
@@ -570,7 +570,6 @@ int main(int argc, char *argv[])
 	uint16_t index = 0;
 	const char *str;
 	bool use_raw = false;
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -624,12 +623,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("3D Synchronization Profile testing ver %s\n", VERSION);
 
 	if (use_raw) {
@@ -654,7 +647,7 @@ int main(int argc, char *argv[])
 	else if (glasses_role)
 		start_glasses();
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(hci_dev);
 
tools/advtest.c +1 -8
index b02301c39..050b570df 100644
@@ -374,7 +374,6 @@ static const struct option main_options[] = {
 
 int main(int argc ,char *argv[])
 {
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -403,12 +402,6 @@ int main(int argc ,char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	mgmt = mgmt_new_default();
 	if (!mgmt) {
 		fprintf(stderr, "Failed to open management socket\n");
@@ -423,7 +416,7 @@ int main(int argc ,char *argv[])
 		goto done;
 	}
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(adv_dev);
 	bt_hci_unref(scan_dev);
tools/bluemoon.c +1 -8
index 6051f3002..800541110 100644
@@ -923,7 +923,6 @@ int main(int argc, char *argv[])
 {
 	const char *str;
 	bool use_raw = false;
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -1000,12 +999,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluemoon configuration utility ver %s\n", VERSION);
 
 	if (check_firmware) {
@@ -1030,7 +1023,7 @@ int main(int argc, char *argv[])
 	bt_hci_send(hci_dev, CMD_READ_VERSION, NULL, 0,
 					read_version_complete, NULL, NULL);
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(hci_dev);
 
tools/btattach.c +1 -8
index 5adbc8d83..6eb99bac3 100644
@@ -231,7 +231,6 @@ int main(int argc, char *argv[])
 {
 	const char *bredr_path = NULL, *amp_path = NULL, *proto = NULL;
 	bool flowctl = true, raw_device = false;
-	sigset_t mask;
 	int exit_status, count = 0, proto_id = HCI_UART_H4;
 	unsigned int speed = B115200;
 
@@ -284,12 +283,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	if (proto) {
 		unsigned int i;
 
@@ -348,7 +341,7 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	return exit_status;
 }
tools/btconfig.c +1 -8
index f171bfbc1..c1ef5834a 100644
@@ -75,7 +75,6 @@ static const struct option main_options[] = {
 
 int main(int argc, char *argv[])
 {
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -105,12 +104,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	mgmt = mgmt_new_default();
 	if (!mgmt) {
 		fprintf(stderr, "Unable to open mgmt_socket\n");
@@ -120,7 +113,7 @@ int main(int argc, char *argv[])
 	if (getenv("MGMT_DEBUG"))
 		mgmt_set_debug(mgmt, mgmt_debug, "mgmt: ", NULL);
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	mgmt_cancel_all(mgmt);
 	mgmt_unregister_all(mgmt);
tools/btgatt-client.c +1 -8
index 51bc3629d..31e849aae 100644
@@ -1518,7 +1518,6 @@ int main(int argc, char *argv[])
 	bdaddr_t src_addr, dst_addr;
 	int dev_id = -1;
 	int fd;
-	sigset_t mask;
 	struct client *cli;
 
 	while ((opt = getopt_long(argc, argv, "+hvs:m:t:d:i:",
@@ -1639,15 +1638,9 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_cb, NULL, NULL);
-
 	print_prompt();
 
-	mainloop_run();
+	mainloop_run_with_signal(signal_cb, NULL);
 
 	printf("\n\nShutting down...\n");
 
tools/btgatt-server.c +1 -8
index 89812bd75..bc32789d9 100644
@@ -1139,7 +1139,6 @@ int main(int argc, char *argv[])
 	int sec = BT_SECURITY_LOW;
 	uint8_t src_type = BDADDR_LE_PUBLIC;
 	uint16_t mtu = 0;
-	sigset_t mask;
 	bool hr_visible = false;
 	struct server *server;
 
@@ -1250,15 +1249,9 @@ int main(int argc, char *argv[])
 
 	printf("Running GATT server\n");
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_cb, NULL, NULL);
-
 	print_prompt();
 
-	mainloop_run();
+	mainloop_run_with_signal(signal_cb, NULL);
 
 	printf("\n\nShutting down...\n");
 
tools/btinfo.c +1 -8
index 8e36577d7..5e609739c 100644
@@ -226,7 +226,6 @@ int main(int argc, char *argv[])
 	const char *str;
 	bool use_raw = false;
 	bool power_down = false;
-	sigset_t mask;
 	int fd, i, exit_status;
 
 	for (;;) {
@@ -284,12 +283,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluetooth information utility ver %s\n", VERSION);
 
 	fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
@@ -345,7 +338,7 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(hci_dev);
 
tools/btmon-logger.c +1 -8
index 1a42824ee..65953d261 100644
@@ -276,7 +276,6 @@ int main(int argc, char *argv[])
 	size_t size_limit = 0;
 	bool parents = false;
 	int exit_status;
-	sigset_t mask;
 	char *endptr;
 
 	mainloop_init();
@@ -365,18 +364,12 @@ int main(int argc, char *argv[])
 
 	drop_capabilities();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Bluetooth monitor logger ver %s\n", VERSION);
 
 	mainloop_sd_notify("STATUS=Running");
 	mainloop_sd_notify("READY=1");
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	mainloop_sd_notify("STATUS=Quitting");
 
tools/btproxy.c +1 -8
index 58fa958e6..fd53ca264 100644
@@ -780,7 +780,6 @@ int main(int argc, char *argv[])
 	bool use_redirect = false;
 	uint8_t type = HCI_PRIMARY;
 	const char *str;
-	sigset_t mask;
 
 	for (;;) {
 		int opt;
@@ -870,12 +869,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	if (connect_address || use_redirect) {
 		int host_fd, dev_fd;
 
@@ -930,5 +923,5 @@ int main(int argc, char *argv[])
 							NULL, NULL);
 	}
 
-	return mainloop_run();
+	return mainloop_run_with_signal(signal_callback, NULL);
 }
tools/eddystone.c +1 -8
index f412c90b0..4764c677c 100644
@@ -244,7 +244,6 @@ int main(int argc, char *argv[])
 {
 	uint16_t index = 0;
 	const char *str;
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -290,12 +289,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Low Energy Eddystone utility ver %s\n", VERSION);
 
 	hci_dev = bt_hci_new_user_channel(index);
@@ -307,7 +300,7 @@ int main(int argc, char *argv[])
 
 	start_eddystone();
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(hci_dev);
 
tools/ibeacon.c +1 -8
index 4b358049c..40078d5ed 100644
@@ -237,7 +237,6 @@ int main(int argc, char *argv[])
 {
 	uint16_t index = 0;
 	const char *str;
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -283,12 +282,6 @@ int main(int argc, char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	printf("Low Energy iBeacon utility ver %s\n", VERSION);
 
 	hci_dev = bt_hci_new_user_channel(index);
@@ -300,7 +293,7 @@ int main(int argc, char *argv[])
 
 	start_ibeacon();
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 	bt_hci_unref(hci_dev);
 
tools/oobtest.c +1 -8
index e77320bab..c228b65b1 100644
@@ -1026,7 +1026,6 @@ static const struct option main_options[] = {
 
 int main(int argc ,char *argv[])
 {
-	sigset_t mask;
 	int exit_status;
 
 	for (;;) {
@@ -1128,12 +1127,6 @@ int main(int argc ,char *argv[])
 
 	mainloop_init();
 
-	sigemptyset(&mask);
-	sigaddset(&mask, SIGINT);
-	sigaddset(&mask, SIGTERM);
-
-	mainloop_set_signal(&mask, signal_callback, NULL, NULL);
-
 	mgmt = mgmt_new_default();
 	if (!mgmt) {
 		fprintf(stderr, "Failed to open management socket\n");
@@ -1148,7 +1141,7 @@ int main(int argc ,char *argv[])
 		goto done;
 	}
 
-	exit_status = mainloop_run();
+	exit_status = mainloop_run_with_signal(signal_callback, NULL);
 
 done:
 	mgmt_unref(mgmt);
Signature · 1 line
2.17.2
author maintainer
#8 lore raw

PATCH v3 9/10 core: Make use of mainloop_run_with_signal

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This don't require setting up signalfd.

---
 src/main.c | 64 +++---------------------------------------------------
 1 file changed, 3 insertions(+), 61 deletions(-)
src/main.c +3 -61
index 7ab1349cc..67eb6ac59 100644
@@ -497,24 +497,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) {
@@ -532,46 +519,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;
@@ -682,7 +629,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();
 
@@ -711,8 +657,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 |
@@ -781,12 +725,10 @@ 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);
 
 	mainloop_sd_notify("STATUS=Quitting");
 
-	g_source_remove(signal);
-
 	plugin_cleanup();
 
 	btd_profile_cleanup();
Signature · 1 line
2.17.2
author maintainer
#9 lore raw

PATCH v3 10/10 shared/tester: Make use of mainloop_run_with_signal

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This don't require setting up signalfd.

---
 src/shared/tester.c | 79 +++++----------------------------------------
 1 file changed, 8 insertions(+), 71 deletions(-)
src/shared/tester.c +8 -71
index 168d0de91..3d79e411d 100644
@@ -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();
 
Signature · 1 line
2.17.2
author maintainer Applied
#10 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 264 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds handling of systemd NOTIFY_SOCKET so application using mainloop instance do properly notify systemd what is their state.

---
 Makefile.am                  |  8 +++-
 src/shared/mainloop-glib.c   |  6 +++
 src/shared/mainloop-notify.c | 93 ++++++++++++++++++++++++++++++++++++
 src/shared/mainloop-notify.h | 25 ++++++++++
 src/shared/mainloop.c        | 10 ++++
 src/shared/mainloop.h        |  1 +
 6 files changed, 141 insertions(+), 2 deletions(-)
 create mode 100644 src/shared/mainloop-notify.c
 create mode 100644 src/shared/mainloop-notify.h
Makefile.am +6 -2
index 0b26ccc3e..124c32482 100644
@@ -130,12 +130,16 @@ endif
 src_libshared_glib_la_SOURCES = $(shared_sources) \
                                src/shared/io-glib.c \
                                src/shared/timeout-glib.c \
-                               src/shared/mainloop-glib.c
+                               src/shared/mainloop-glib.c \
+                               src/shared/mainloop-notify.h \
+                               src/shared/mainloop-notify.c
 
 src_libshared_mainloop_la_SOURCES = $(shared_sources) \
                                src/shared/io-mainloop.c \
                                src/shared/timeout-mainloop.c \
-                               src/shared/mainloop.h src/shared/mainloop.c
+                               src/shared/mainloop.h src/shared/mainloop.c \
+                               src/shared/mainloop-notify.h \
+                               src/shared/mainloop-notify.c
 
 if ELL
 src_libshared_ell_la_SOURCES = $(shared_sources) \
src/shared/mainloop-glib.c +6 -0
index 8436969bb..42abfddf8 100644
@@ -36,6 +36,7 @@
 #include <glib.h>
 
 #include "mainloop.h"
+#include "mainloop-notify.h"
 
 static GMainLoop *main_loop;
 static int exit_status;
@@ -43,6 +44,7 @@ static int exit_status;
 void mainloop_init(void)
 {
        main_loop = g_main_loop_new(NULL, FALSE);
+       mainloop_notify_init();
 }
 
 void mainloop_quit(void)
@@ -51,6 +53,8 @@ void mainloop_quit(void)
                return;
 
        g_main_loop_quit(main_loop);
+
+       mainloop_sd_notify("STOPPING=1");
 }
 
 void mainloop_exit_success(void)
@@ -75,6 +79,8 @@ int mainloop_run(void)
        g_main_loop_unref(main_loop);
        main_loop = NULL;
 
+       mainloop_notify_exit();
+
        return exit_status;
 }
 
src/shared/mainloop-notify.c new file +93 -0
new file mode 100644
index 000000000..fdc2f25ec
@@ -0,0 +1,93 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "mainloop.h"
+#include "mainloop-notify.h"
+
+static int notify_fd = -1;
+
+void mainloop_notify_init(void)
+{
+       const char *sock;
+       struct sockaddr_un addr;
+
+       sock = getenv("NOTIFY_SOCKET");
+       if (!sock)
+               return;
+
+       /* check for abstract socket or absolute path */
+       if (sock[0] != '@' && sock[0] != '/')
+               return;
+
+       notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (notify_fd < 0)
+               return;
+
+       memset(&addr, 0, sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       strncpy(addr.sun_path, sock, sizeof(addr.sun_path) - 1);
+
+       if (addr.sun_path[0] == '@')
+               addr.sun_path[0] = '\0';
+
+       if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+               close(notify_fd);
+               notify_fd = -1;
+       }
+}
+
+void mainloop_notify_exit(void)
+{
+       if (notify_fd > 0) {
+               close(notify_fd);
+               notify_fd = -1;
+       }
+}
+
+int mainloop_sd_notify(const char *state)
+{
+       int err;
+
+       if (notify_fd <= 0)
+               return -ENOTCONN;
+
+       err = send(notify_fd, state, strlen(state), MSG_NOSIGNAL);
+       if (err < 0)
+               return -errno;
+
+       return err;
+}
src/shared/mainloop-notify.h new file +25 -0
new file mode 100644
index 000000000..721b5fbed
@@ -0,0 +1,25 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+void mainloop_notify_init(void);
+void mainloop_notify_exit(void);
src/shared/mainloop.c +10 -0
index e6ab9c43d..e9ec6e8cd 100644
@@ -30,13 +30,17 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <signal.h>
 #include <sys/signalfd.h>
 #include <sys/timerfd.h>
 #include <sys/epoll.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include "mainloop.h"
+#include "mainloop-notify.h"
 
 #define MAX_EPOLL_EVENTS 10
 
@@ -83,11 +87,15 @@ void mainloop_init(void)
                mainloop_list[i] = NULL;
 
        epoll_terminate = 0;
+
+       mainloop_notify_init();
 }
 
 void mainloop_quit(void)
 {
        epoll_terminate = 1;
+
+       mainloop_sd_notify("STOPPING=1");
 }
 
 void mainloop_exit_success(void)
@@ -183,6 +191,8 @@ int mainloop_run(void)
        close(epoll_fd);
        epoll_fd = 0;
 
+       mainloop_notify_exit();
+
        return exit_status;
 }
 
src/shared/mainloop.h +1 -1
index b83caabc8..73ed81187 100644
@@ -49,3 +49,4 @@ int mainloop_remove_timeout(int id);
 
 int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback,
                                void *user_data, mainloop_destroy_func destroy);
+int mainloop_sd_notify(const char *state);
--
Quoted 2 lines

2.17.2

Applied.

Signature · 1 line
Luiz Augusto von Dentz