Skip to contentSkip to conversation

share/mainloop: Add handling of NOTIFY_SOCKET

PATCH Superseded Luiz Augusto von Dentz 2 participants 10 messages Nov 26, 2018 – Nov 27, 2018

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/6 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   |   8 +++
 src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
 src/shared/mainloop-notify.h |  25 +++++++++
 src/shared/mainloop.c        |  12 ++++
 src/shared/mainloop.h        |   1 +
 6 files changed, 156 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 +8 -0
index 8436969bb..9d588e8c5 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)
@@ -70,11 +72,17 @@ int mainloop_run(void)
 	if (!main_loop)
 		return -EINVAL;
 
+	mainloop_notify("READY=1");
+
 	g_main_loop_run(main_loop);
 
+	mainloop_notify("STOPPING=1");
+
 	g_main_loop_unref(main_loop);
 	main_loop = NULL;
 
+	mainloop_notify_exit();
+
 	return exit_status;
 }
 
src/shared/mainloop-notify.c new file +104 -0

104 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 +12 -0
index e6ab9c43d..9255e6e0e 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,6 +87,8 @@ void mainloop_init(void)
 		mainloop_list[i] = NULL;
 
 	epoll_terminate = 0;
+
+	mainloop_notify_init();
 }
 
 void mainloop_quit(void)
@@ -141,6 +147,8 @@ int mainloop_run(void)
 		}
 	}
 
+	mainloop_notify("READY=1");
+
 	while (!epoll_terminate) {
 		struct epoll_event events[MAX_EPOLL_EVENTS];
 		int n, nfds;
@@ -180,9 +188,13 @@ int mainloop_run(void)
 		}
 	}
 
+	mainloop_notify("STOPPING=1");
+
 	close(epoll_fd);
 	epoll_fd = 0;
 
+	mainloop_notify_exit();
+
 	return exit_status;
 }
 
src/shared/mainloop.h +1 -0
index b83caabc8..7ae6b3c90 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_notify(const char *state);
Signature · 1 line
2.17.2
author maintainer
#1 lore raw

PATCH 2/6 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 | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
src/shared/mainloop-notify.c +27 -0
index fef333c24..cb895086f 100644
@@ -37,13 +37,27 @@
 
 #include "mainloop.h"
 #include "mainloop-notify.h"
+#include "timeout.h"
+
+#define WATCHDOG_THRESHOLD 2
 
 static char *notify_sock;
 static int notify_fd = -1;
 
+static unsigned int watchdog;
+
+static bool watchdog_callback(void *user_data)
+{
+	mainloop_notify("WATCHDOG=1");
+
+	return true;
+}
+
 void mainloop_notify_init(void)
 {
 	const char *sock;
+	const char *watchdog_usec;
+	int msec;
 
 	sock = getenv("NOTIFY_SOCKET");
 	if (!sock)
@@ -56,6 +70,17 @@ void mainloop_notify_init(void)
 	notify_sock = strdup(sock);
 
 	notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+
+	watchdog_usec = getenv("WATCHDOG_USEC");
+	if (!watchdog_usec)
+		return;
+
+	msec = atoi(watchdog_usec) / 1000;
+	if (msec < 0)
+		return;
+
+	watchdog = timeout_add(msec / WATCHDOG_THRESHOLD,
+				watchdog_callback, NULL, NULL);
 }
 
 void mainloop_notify_exit(void)
@@ -67,6 +92,8 @@ void mainloop_notify_exit(void)
 
 	free(notify_sock);
 	notify_sock = NULL;
+
+	timeout_remove(watchdog);
 }
 
 int mainloop_notify(const char *state)
Signature · 1 line
2.17.2
author maintainer
#2 lore raw

PATCH 3/6 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 | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/btmon-logger.c +3 -6
index c3ba17939..65c497dbb 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_notify("STATUS=Starting up");
 
 	while (true) {
 		int opt;
@@ -375,12 +373,11 @@ 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_notify("STATUS=Running");
 
 	exit_status = mainloop_run();
 
-	sd_notify(0, "STATUS=Quitting");
+	mainloop_notify("STATUS=Quitting");
 
 	btsnoop_unref(btsnoop_file);
 
Signature · 1 line
2.17.2
author maintainer
#3 lore raw

PATCH 4/6 core: 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.

---
 src/main.c | 50 ++++++++++----------------------------------------
 1 file changed, 10 insertions(+), 40 deletions(-)
src/main.c +10 -40
index 4716f5388..f2e0dd050 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_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_notify("STATUS=Starting up");
 
 	if (option_configfile)
 		main_conf_file_path = option_configfile;
@@ -788,26 +778,11 @@ int main(int argc, char *argv[])
 
 	DBG("Entering main loop");
 
-	sd_notify(0, "STATUS=Running");
-	sd_notify(0, "READY=1");
+	mainloop_notify("STATUS=Running");
 
-	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_notify("STATUS=Quitting");
 
 	g_source_remove(signal);
 
@@ -824,8 +799,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 +806,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 5/6 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 6/6 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
maintainer
#6 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 57 lines

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   |   8 +++
src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
src/shared/mainloop-notify.h |  25 +++++++++
src/shared/mainloop.c        |  12 ++++
src/shared/mainloop.h        |   1 +
6 files changed, 156 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 +2 -0
index 8436969bb..9d588e8c5 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)

src/shared/mainloop-glib.c int mainloop_run(void) · line 80

Quoted 9 lines
src/shared/mainloop-glib.c +4 -0
@@ -70,11 +72,17 @@ int mainloop_run(void)
 	if (!main_loop)
 		return -EINVAL;
 
+	mainloop_notify("READY=1");
+
 	g_main_loop_run(main_loop);
 
+	mainloop_notify("STOPPING=1");
+

I actually think this is too simple. Frankly what we want is some generic code that runs the mainloops and handles the terminations signals and also brings you onto D-Bus. And only then signal READY=1.

If you look at iwd and wired/dbus.c then I have started something in that direction with dbus_app_run. That needs to be a bit more unified and turned into l_dbus_run or some similar name.

My thinking really is that the main() function should be just deal with argument parsing and then getting you on the system or session bus. It should not be bothered with all the signal setup or the duplicated code for handling the asynchronous shutdown. And if you have that, then you do a nice integration with NOTIFY_SOCKET.

Regards

Marcel

author maintainer
#7 lore raw

Hi Marcel,

Quoted 63 lines · Marcel Holtmann

Hi Luiz,

Quoted 60 lines

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   |   8 +++
src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
src/shared/mainloop-notify.h |  25 +++++++++
src/shared/mainloop.c        |  12 ++++
src/shared/mainloop.h        |   1 +
6 files changed, 156 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 +2 -0
index 8436969bb..9d588e8c5 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)
@@ -70,11 +72,17 @@ int mainloop_run(void)
      if (!main_loop)
              return -EINVAL;
 
Quoted 8 lines

src/shared/mainloop-glib.c int mainloop_run(void) · line 80

Quoted 6 lines
src/shared/mainloop-glib.c +4 -0
+     mainloop_notify("READY=1");
+
      g_main_loop_run(main_loop);
 
+     mainloop_notify("STOPPING=1");
+

I actually think this is too simple. Frankly what we want is some generic code that runs the mainloops and handles the terminations signals and also brings you onto D-Bus. And only then signal READY=1.

That was the intention, though things like btmon-logger does not need to be on D-Bus beside mainloop_run is normally called after D-Bus setup since we want to confirm we can claim the name.

Quoted 1 line

If you look at iwd and wired/dbus.c then I have started something in that direction with dbus_app_run. That needs to be a bit more unified and turned into l_dbus_run or some similar name.

Right, Im not sure if that applies to our internal g_dbus though but for meshd it definitely makes sense.

Quoted 1 line

My thinking really is that the main() function should be just deal with argument parsing and then getting you on the system or session bus. It should not be bothered with all the signal setup or the duplicated code for handling the asynchronous shutdown. And if you have that, then you do a nice integration with NOTIFY_SOCKET.

I just wanted to tackle READY=1 and STOPPING=1 when the mainloop start and stop respectively, those I think make sense regardless of what type of service (D-Bus daemon, btproxy, btattach, etc) but perhaps you are saying that those things may actually need to be notified in different places depending on the service.

For READY=1 I can see the point if the service does require do to something asynchronous before it is considered ready, bluetoothd currently don't require that but maybe in ell we are doing something different, for STOPPING=1 that only indicates we are starting to shutdown so that doesn't rule out doing it asynchronously:

https://www.freedesktop.org/software/systemd/man/sd_notify.html

STATUS= is free format and given the example I was assuming if would notify things asynchronously if we have to, thus why I created mainloop_notify, perhaps we should rename it to mainloop_notify_status?

Watchdog I guess it is pretty safe to do the handling along with the mainloop since timeout handling is already done there anyway, anyway notifying READY or STOPPING multiple times probably don't make any sense, they cannot be undone, we could perhaps have a flag indicating if the mainloop shall handle those, maybe via an option like -s/--service=[ready, stopping, watchdog] passed to mainloop_init(int argc, char *argv[]) that way the user can inform that he wants to run the tool as a service and optionally include what states it should control (by default it would be all).

Quoted 4 lines

Regards

Marcel

Signature · 1 line
Luiz Augusto von Dentz
maintainer
#8 lore raw

Hi Luiz,

Quoted 64 lines
Quoted 64 lines
Quoted 64 lines

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   |   8 +++
src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
src/shared/mainloop-notify.h |  25 +++++++++
src/shared/mainloop.c        |  12 ++++
src/shared/mainloop.h        |   1 +
6 files changed, 156 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 +4 -0
index 8436969bb..9d588e8c5 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)
@@ -70,11 +72,17 @@ int mainloop_run(void)
     if (!main_loop)
             return -EINVAL;
 
+     mainloop_notify("READY=1");
+
     g_main_loop_run(main_loop);
 
Quoted 8 lines
Quoted 4 lines

src/shared/mainloop-glib.c int mainloop_run(void) · line 80

Quoted 2 lines
src/shared/mainloop-glib.c +2 -0
+     mainloop_notify("STOPPING=1");
+

I actually think this is too simple. Frankly what we want is some generic code that runs the mainloops and handles the terminations signals and also brings you onto D-Bus. And only then signal READY=1.

That was the intention, though things like btmon-logger does not need to be on D-Bus beside mainloop_run is normally called after D-Bus setup since we want to confirm we can claim the name.

I think it is fine to have a version that does not connect to D-Bus, but the name claiming in case of D-Bus based application needs to be handled correctly as well. The background is that we want to start other activities like connecting to mgmt socket etc. only _after_ we successfully claimed the name. Claiming the name is an asynchronous operation (at least in ELL D-Bus) so the mainloop needs to be running.

Quoted 4 lines
Quoted 1 line

If you look at iwd and wired/dbus.c then I have started something in that direction with dbus_app_run. That needs to be a bit more unified and turned into l_dbus_run or some similar name.

Right, Im not sure if that applies to our internal g_dbus though but for meshd it definitely makes sense.

Our internal gdbus is something that needs to move towards ELL D-Bus really quickly. We have to remove the dependency on libdbus even if we keep gdbus API in place for a while.

Quoted 21 lines
Quoted 1 line

My thinking really is that the main() function should be just deal with argument parsing and then getting you on the system or session bus. It should not be bothered with all the signal setup or the duplicated code for handling the asynchronous shutdown. And if you have that, then you do a nice integration with NOTIFY_SOCKET.

I just wanted to tackle READY=1 and STOPPING=1 when the mainloop start and stop respectively, those I think make sense regardless of what type of service (D-Bus daemon, btproxy, btattach, etc) but perhaps you are saying that those things may actually need to be notified in different places depending on the service.

For READY=1 I can see the point if the service does require do to something asynchronous before it is considered ready, bluetoothd currently don't require that but maybe in ell we are doing something different, for STOPPING=1 that only indicates we are starting to shutdown so that doesn't rule out doing it asynchronously:

https://www.freedesktop.org/software/systemd/man/sd_notify.html

STATUS= is free format and given the example I was assuming if would notify things asynchronously if we have to, thus why I created mainloop_notify, perhaps we should rename it to mainloop_notify_status?

Quoted 9 lines

Watchdog I guess it is pretty safe to do the handling along with the mainloop since timeout handling is already done there anyway, anyway notifying READY or STOPPING multiple times probably don't make any sense, they cannot be undone, we could perhaps have a flag indicating if the mainloop shall handle those, maybe via an option like -s/--service=[ready, stopping, watchdog] passed to mainloop_init(int argc, char *argv[]) that way the user can inform that he wants to run the tool as a service and optionally include what states it should control (by default it would be all).

The watchdog needs to be done within the mainloop since you have to have timeout handling active.

We can argue about if certain things are useful as a short term quick solution, but in the end we want a proper solution and not just hack it into the codebase. I rather have no systemd integration than a half-baked one.

Regards

Marcel

author maintainer
#9 lore raw

Hi Marcel,

Quoted 72 lines · Marcel Holtmann

Hi Luiz,

Quoted 69 lines
Quoted 68 lines
Quoted 57 lines

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   |   8 +++
src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
src/shared/mainloop-notify.h |  25 +++++++++
src/shared/mainloop.c        |  12 ++++
src/shared/mainloop.h        |   1 +
6 files changed, 156 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 +2 -0
index 8436969bb..9d588e8c5 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)

src/shared/mainloop-glib.c int mainloop_run(void) · line 80

Quoted 9 lines
src/shared/mainloop-glib.c +4 -0
@@ -70,11 +72,17 @@ int mainloop_run(void)
     if (!main_loop)
             return -EINVAL;
 
+     mainloop_notify("READY=1");
+
     g_main_loop_run(main_loop);
 
+     mainloop_notify("STOPPING=1");
+

I actually think this is too simple. Frankly what we want is some generic code that runs the mainloops and handles the terminations signals and also brings you onto D-Bus. And only then signal READY=1.

Quoted 5 lines
Quoted 3 lines

That was the intention, though things like btmon-logger does not need to be on D-Bus beside mainloop_run is normally called after D-Bus setup since we want to confirm we can claim the name.

I think it is fine to have a version that does not connect to D-Bus, but the name claiming in case of D-Bus based application needs to be handled correctly as well. The background is that we want to start other activities like connecting to mgmt socket etc. only _after_ we successfully claimed the name. Claiming the name is an asynchronous operation (at least in ELL D-Bus) so the mainloop needs to be running.

As I said Im fine not triggering the READY=1 directly at mainloop_run, the name claim is done blocking in gdbus/libdbus but Id understand this may change once we switch over to ELL.

Quoted 38 lines
Quoted 4 lines
Quoted 1 line

If you look at iwd and wired/dbus.c then I have started something in that direction with dbus_app_run. That needs to be a bit more unified and turned into l_dbus_run or some similar name.

Right, Im not sure if that applies to our internal g_dbus though but for meshd it definitely makes sense.

Our internal gdbus is something that needs to move towards ELL D-Bus really quickly. We have to remove the dependency on libdbus even if we keep gdbus API in place for a while.

Quoted 21 lines
Quoted 1 line

My thinking really is that the main() function should be just deal with argument parsing and then getting you on the system or session bus. It should not be bothered with all the signal setup or the duplicated code for handling the asynchronous shutdown. And if you have that, then you do a nice integration with NOTIFY_SOCKET.

I just wanted to tackle READY=1 and STOPPING=1 when the mainloop start and stop respectively, those I think make sense regardless of what type of service (D-Bus daemon, btproxy, btattach, etc) but perhaps you are saying that those things may actually need to be notified in different places depending on the service.

For READY=1 I can see the point if the service does require do to something asynchronous before it is considered ready, bluetoothd currently don't require that but maybe in ell we are doing something different, for STOPPING=1 that only indicates we are starting to shutdown so that doesn't rule out doing it asynchronously:

https://www.freedesktop.org/software/systemd/man/sd_notify.html

STATUS= is free format and given the example I was assuming if would notify things asynchronously if we have to, thus why I created mainloop_notify, perhaps we should rename it to mainloop_notify_status?

Quoted 9 lines

Watchdog I guess it is pretty safe to do the handling along with the mainloop since timeout handling is already done there anyway, anyway notifying READY or STOPPING multiple times probably don't make any sense, they cannot be undone, we could perhaps have a flag indicating if the mainloop shall handle those, maybe via an option like -s/--service=[ready, stopping, watchdog] passed to mainloop_init(int argc, char *argv[]) that way the user can inform that he wants to run the tool as a service and optionally include what states it should control (by default it would be all).

Quoted 3 lines

The watchdog needs to be done within the mainloop since you have to have timeout handling active.

We can argue about if certain things are useful as a short term quick solution, but in the end we want a proper solution and not just hack it into the codebase. I rather have no systemd integration than a half-baked one.

Well Im trying to agree here on the API so we can have it similar in ELL thus making the transition a bit simpler, I was already intending to switch everything to mainloop abstraction, since I suppose the problem with ELL is not exactly the API but the dependency which may not be available in many distros. Some people already experience this on the likes of rpi when attempting to use meshd, though we hope that makes distro guys attempt to create packages for it once we have the D-Bus APIs implemented.

Quoted 4 lines

Regards

Marcel

Signature · 1 line
Luiz Augusto von Dentz