Linux-Bluetooth Archive on lore.kernel.org
 help / Atom feed
* [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET
@ 2018-11-26 16:24 Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 2/6] share/mainloop: Add watchdog support Luiz Augusto von Dentz
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

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

diff --git a/Makefile.am b/Makefile.am
index 0b26ccc3e..124c32482 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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) \
diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
index 8436969bb..9d588e8c5 100644
--- a/src/shared/mainloop-glib.c
+++ b/src/shared/mainloop-glib.c
@@ -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;
 }
 
diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c
new file mode 100644
index 000000000..fef333c24
--- /dev/null
+++ b/src/shared/mainloop-notify.c
@@ -0,0 +1,104 @@
+/*
+ *
+ *  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 char *notify_sock;
+static int notify_fd = -1;
+
+void mainloop_notify_init(void)
+{
+	const char *sock;
+
+	sock = getenv("NOTIFY_SOCKET");
+	if (!sock)
+		return;
+
+	/* check for abstract socket or absolute path */
+	if (sock[0] != '@' && sock[0] != '/')
+		return;
+
+	notify_sock = strdup(sock);
+
+	notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+}
+
+void mainloop_notify_exit(void)
+{
+	if (notify_fd > 0) {
+		close(notify_fd);
+		notify_fd = -1;
+	}
+
+	free(notify_sock);
+	notify_sock = NULL;
+}
+
+int mainloop_notify(const char *state)
+{
+	struct sockaddr_un addr;
+	struct msghdr msghdr;
+	struct iovec iovec;
+
+	if (notify_fd <= 0)
+		return -ENOTCONN;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, notify_sock, sizeof(addr.sun_path) - 1);
+
+	if (addr.sun_path[0] == '@')
+		addr.sun_path[0] = '\0';
+
+	memset(&iovec, 0, sizeof(iovec));
+	iovec.iov_base = (char *) state;
+	iovec.iov_len = strlen(state);
+
+	memset(&msghdr, 0, sizeof(msghdr));
+	msghdr.msg_name = &addr;
+	msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) +
+							strlen(notify_sock);
+
+	if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
+		msghdr.msg_namelen = sizeof(struct sockaddr_un);
+
+	msghdr.msg_iov = &iovec;
+	msghdr.msg_iovlen = 1;
+
+	return sendmsg(notify_fd, &msghdr, MSG_NOSIGNAL);
+}
diff --git a/src/shared/mainloop-notify.h b/src/shared/mainloop-notify.h
new file mode 100644
index 000000000..721b5fbed
--- /dev/null
+++ b/src/shared/mainloop-notify.h
@@ -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);
diff --git a/src/shared/mainloop.c b/src/shared/mainloop.c
index e6ab9c43d..9255e6e0e 100644
--- a/src/shared/mainloop.c
+++ b/src/shared/mainloop.c
@@ -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;
 }
 
diff --git a/src/shared/mainloop.h b/src/shared/mainloop.h
index b83caabc8..7ae6b3c90 100644
--- a/src/shared/mainloop.h
+++ b/src/shared/mainloop.h
@@ -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);
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 2/6] share/mainloop: Add watchdog support
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
@ 2018-11-26 16:24 ` Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 3/6] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

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(+)

diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c
index fef333c24..cb895086f 100644
--- a/src/shared/mainloop-notify.c
+++ b/src/shared/mainloop-notify.c
@@ -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)
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 3/6] tool/btmon-logger: Use mainloop_notify instead of sd_notify
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 2/6] share/mainloop: Add watchdog support Luiz Augusto von Dentz
@ 2018-11-26 16:24 ` Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 4/6] core: " Luiz Augusto von Dentz
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

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(-)

diff --git a/tools/btmon-logger.c b/tools/btmon-logger.c
index c3ba17939..65c497dbb 100644
--- a/tools/btmon-logger.c
+++ b/tools/btmon-logger.c
@@ -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);
 
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 4/6] core: Use mainloop_notify instead of sd_notify
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 2/6] share/mainloop: Add watchdog support Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 3/6] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz
@ 2018-11-26 16:24 ` " Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 5/6] core: Remove old code related to sd_notify Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

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(-)

diff --git a/src/main.c b/src/main.c
index 4716f5388..f2e0dd050 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 5/6] core: Remove old code related to sd_notify
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2018-11-26 16:24 ` [PATCH BlueZ 4/6] core: " Luiz Augusto von Dentz
@ 2018-11-26 16:24 ` Luiz Augusto von Dentz
  2018-11-26 16:24 ` [PATCH BlueZ 6/6] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz
  2018-11-26 17:54 ` [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Marcel Holtmann
  5 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

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

diff --git a/Makefile.am b/Makefile.am
index 124c32482..a69baea10 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/Makefile.tools b/Makefile.tools
index 35412ca61..d3d901bc9 100644
--- a/Makefile.tools
+++ b/Makefile.tools
@@ -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
diff --git a/src/systemd.c b/src/systemd.c
deleted file mode 100644
index 0a4b35d06..000000000
--- a/src/systemd.c
+++ /dev/null
@@ -1,107 +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
- *
- */
-
-#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 "systemd.h"
-
-int sd_listen_fds(int unset_environment)
-{
-	return 0;
-}
-
-int sd_notify(int unset_environment, const char *state)
-{
-	const char *sock;
-	struct sockaddr_un addr;
-	struct msghdr msghdr;
-	struct iovec iovec;
-	int fd, err;
-
-	if (!state) {
-		err = -EINVAL;
-		goto done;
-	}
-
-	sock = getenv("NOTIFY_SOCKET");
-	if (!sock)
-		return 0;
-
-	/* check for abstract socket or absolute path */
-	if (sock[0] != '@' && sock[0] != '/') {
-		err = -EINVAL;
-		goto done;
-	}
-
-	fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-	if (fd < 0) {
-		err = -errno;
-		goto done;
-	}
-
-	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';
-
-	memset(&iovec, 0, sizeof(iovec));
-	iovec.iov_base = (char *) state;
-	iovec.iov_len = strlen(state);
-
-	memset(&msghdr, 0, sizeof(msghdr));
-	msghdr.msg_name = &addr;
-	msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) +
-								strlen(sock);
-
-	if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
-		msghdr.msg_namelen = sizeof(struct sockaddr_un);
-
-	msghdr.msg_iov = &iovec;
-	msghdr.msg_iovlen = 1;
-
-	if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0)
-		err = -errno;
-	else
-		err = 1;
-
-	close(fd);
-
-done:
-	if (unset_environment)
-		unsetenv("NOTIFY_SOCKET");
-
-	return err;
-}
diff --git a/src/systemd.h b/src/systemd.h
deleted file mode 100644
index 0ef7c82a9..000000000
--- a/src/systemd.h
+++ /dev/null
@@ -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);
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 6/6] shared/timeout-glib: Check 0 id when removing timeout
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
                   ` (3 preceding siblings ...)
  2018-11-26 16:24 ` [PATCH BlueZ 5/6] core: Remove old code related to sd_notify Luiz Augusto von Dentz
@ 2018-11-26 16:24 ` Luiz Augusto von Dentz
  2018-11-26 17:54 ` [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Marcel Holtmann
  5 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-26 16:24 UTC (permalink / raw)
  To: linux-bluetooth

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

If the id is 0 that makes it is invalid already.
---
 src/shared/timeout-glib.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/shared/timeout-glib.c b/src/shared/timeout-glib.c
index 4163bced7..fd71ca430 100644
--- a/src/shared/timeout-glib.c
+++ b/src/shared/timeout-glib.c
@@ -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);
 }
-- 
2.17.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET
  2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
                   ` (4 preceding siblings ...)
  2018-11-26 16:24 ` [PATCH BlueZ 6/6] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz
@ 2018-11-26 17:54 ` Marcel Holtmann
  2018-11-27  9:51   ` Luiz Augusto von Dentz
  5 siblings, 1 reply; 10+ messages in thread
From: Marcel Holtmann @ 2018-11-26 17:54 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth

Hi Luiz,

> 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
> 
> diff --git a/Makefile.am b/Makefile.am
> index 0b26ccc3e..124c32482 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -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) \
> diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
> index 8436969bb..9d588e8c5 100644
> --- a/src/shared/mainloop-glib.c
> +++ b/src/shared/mainloop-glib.c
> @@ -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");
> +

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET
  2018-11-26 17:54 ` [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Marcel Holtmann
@ 2018-11-27  9:51   ` Luiz Augusto von Dentz
  2018-11-27 13:30     ` Marcel Holtmann
  0 siblings, 1 reply; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-27  9:51 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: linux-bluetooth

Hi Marcel,
On Mon, Nov 26, 2018 at 7:55 PM Marcel Holtmann <marcel@holtmann.org> wrote:
>
> Hi Luiz,
>
> > 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
> >
> > diff --git a/Makefile.am b/Makefile.am
> > index 0b26ccc3e..124c32482 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -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) \
> > diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
> > index 8436969bb..9d588e8c5 100644
> > --- a/src/shared/mainloop-glib.c
> > +++ b/src/shared/mainloop-glib.c
> > @@ -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");
> > +
>
> 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.

> 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.

> 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).

> Regards
>
> Marcel
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET
  2018-11-27  9:51   ` Luiz Augusto von Dentz
@ 2018-11-27 13:30     ` Marcel Holtmann
  2018-11-27 15:19       ` Luiz Augusto von Dentz
  0 siblings, 1 reply; 10+ messages in thread
From: Marcel Holtmann @ 2018-11-27 13:30 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth

Hi Luiz,

>>> 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
>>> 
>>> diff --git a/Makefile.am b/Makefile.am
>>> index 0b26ccc3e..124c32482 100644
>>> --- a/Makefile.am
>>> +++ b/Makefile.am
>>> @@ -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) \
>>> diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
>>> index 8436969bb..9d588e8c5 100644
>>> --- a/src/shared/mainloop-glib.c
>>> +++ b/src/shared/mainloop-glib.c
>>> @@ -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");
>>> +
>> 
>> 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.

>> 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.

>> 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).

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET
  2018-11-27 13:30     ` Marcel Holtmann
@ 2018-11-27 15:19       ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-27 15:19 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: linux-bluetooth

Hi Marcel,

On Tue, Nov 27, 2018 at 3:30 PM Marcel Holtmann <marcel@holtmann.org> wrote:
>
> Hi Luiz,
>
> >>> 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
> >>>
> >>> diff --git a/Makefile.am b/Makefile.am
> >>> index 0b26ccc3e..124c32482 100644
> >>> --- a/Makefile.am
> >>> +++ b/Makefile.am
> >>> @@ -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) \
> >>> diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
> >>> index 8436969bb..9d588e8c5 100644
> >>> --- a/src/shared/mainloop-glib.c
> >>> +++ b/src/shared/mainloop-glib.c
> >>> @@ -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");
> >>> +
> >>
> >> 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.

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.

> >> 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.
>
> >> 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).
>
> 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.

> Regards
>
> Marcel
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, back to index

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-26 16:24 [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
2018-11-26 16:24 ` [PATCH BlueZ 2/6] share/mainloop: Add watchdog support Luiz Augusto von Dentz
2018-11-26 16:24 ` [PATCH BlueZ 3/6] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz
2018-11-26 16:24 ` [PATCH BlueZ 4/6] core: " Luiz Augusto von Dentz
2018-11-26 16:24 ` [PATCH BlueZ 5/6] core: Remove old code related to sd_notify Luiz Augusto von Dentz
2018-11-26 16:24 ` [PATCH BlueZ 6/6] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz
2018-11-26 17:54 ` [PATCH BlueZ 1/6] share/mainloop: Add handling of NOTIFY_SOCKET Marcel Holtmann
2018-11-27  9:51   ` Luiz Augusto von Dentz
2018-11-27 13:30     ` Marcel Holtmann
2018-11-27 15:19       ` Luiz Augusto von Dentz

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