Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET
@ 2018-11-28 15:00 Luiz Augusto von Dentz
  2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-28 15:00 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   |   6 ++
 src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++
 src/shared/mainloop-notify.h |  25 +++++++++
 src/shared/mainloop.c        |  10 ++++
 src/shared/mainloop.h        |   1 +
 6 files changed, 152 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..4403d02d8 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)
@@ -72,9 +74,13 @@ int mainloop_run(void)
 
 	g_main_loop_run(main_loop);
 
+	mainloop_sd_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..81b0e15ba
--- /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_sd_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..d7200a837 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)
@@ -180,9 +186,13 @@ int mainloop_run(void)
 		}
 	}
 
+	mainloop_sd_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..73ed81187 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_sd_notify(const char *state);
-- 
2.17.2


^ 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-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 04/10] core: Use mainloop_sd_notify " Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 05/10] core: Remove old code related to sd_notify Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 09/10] core: Make use of mainloop_run_with_signal Luiz Augusto von Dentz
2018-11-28 15:00 ` [PATCH v2 10/10] shared/tester: " 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

Example config snippet for mirrors

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