Skip to contentSkip to conversation

main: Add support for $NOTIFY_SOCKET

PATCH Applied Luiz Augusto von Dentz 2 participants 4 messages Nov 30, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 main: Add support for $NOTIFY_SOCKET

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

This adds support for sending states to the service manager using the socket set in $NOTIFY_SOCKET:

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

---
 ell/main.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/main.h |  2 ++
 2 files changed, 73 insertions(+)
ell/main.c +71 -0
index b1d12b6..029ac8b 100644
@@ -28,9 +28,12 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <limits.h>
 #include <signal.h>
 #include <sys/epoll.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include "signal.h"
 #include "queue.h"
@@ -59,6 +62,8 @@ static bool epoll_running;
 static bool epoll_terminate;
 static int idle_id;
 
+static int notify_fd;
+
 static struct l_queue *idle_list;
 
 struct watch_data {
@@ -324,6 +329,40 @@ static void idle_dispatch(void *data, void *user_data)
 	idle->flags &= ~IDLE_FLAG_DISPATCHING;
 }
 
+static void create_sd_notify_socket(void)
+{
+	const char *sock;
+	struct sockaddr_un addr;
+
+	/* check if NOTIFY_SOCKET has been set */
+	sock = getenv("NOTIFY_SOCKET");
+	if (!sock)
+		return;
+
+	/* check for abstract socket or absolute path */
+	if (sock[0] != '@' && sock[0] != '/')
+		return;
+
+	notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+	if (notify_fd < 0) {
+		notify_fd = 0;
+		return;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, sock, sizeof(addr.sun_path) - 1);
+
+	if (addr.sun_path[0] == '@')
+		addr.sun_path[0] = '\0';
+
+	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		close(notify_fd);
+		notify_fd = 0;
+	}
+}
+
+
 /**
  * l_main_init:
  *
@@ -342,6 +381,8 @@ LIB_EXPORT bool l_main_init(void)
 	if (!create_epoll())
 		return false;
 
+	create_sd_notify_socket();
+
 	epoll_terminate = false;
 
 	return true;
@@ -438,6 +479,11 @@ LIB_EXPORT int l_main_run(void)
 
 	epoll_running = false;
 
+	if (notify_fd) {
+		close(notify_fd);
+		notify_fd = 0;
+	}
+
 	return EXIT_SUCCESS;
 }
 
@@ -570,3 +616,28 @@ LIB_EXPORT int l_main_get_epoll_fd(void)
 {
 	return epoll_fd;
 }
+
+/**
+ * l_main_sd_notify:
+ *
+ * Notify service manager about start-up completion and other service status
+ * changes:
+ * https://www.freedesktop.org/software/systemd/man/sd_notify.html
+ *
+ * Returns: On failure, these calls return a negative errno-style error code.
+ * If $NOTIFY_SOCKET was not set and hence no status message could be sent,
+ * -ENOCONN is returned.
+ **/
+LIB_EXPORT int l_main_sd_notify(const char *state)
+{
+	int err;
+
+	if (notify_fd <= 0)
+		return -ENOTCONN;
+
+	err = send(notify_fd, state, strlen(state), MSG_NOSIGNAL);
+	if (err < 0)
+		return -errno;
+
+	return err;
+}
ell/main.h +2 -0
index 99b34ad..ffa40af 100644
@@ -44,6 +44,8 @@ int l_main_run_with_signal(l_main_signal_cb_t callback, void *user_data);
 
 int l_main_get_epoll_fd();
 
+int l_main_sd_notify(const char *state);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.17.2
#1 lore raw

PATCH v2 2/2 main: Add support for $WATCHDOG_USEC

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

This adds handling to watchdog set by service manager by sending "WATCHDOG=1" to it in intervals shorter than what is set in $WATCHDOG_USEC.

---
 ell/main.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
ell/main.c +32 -0
index 029ac8b..cae1453 100644
@@ -41,6 +41,7 @@
 #include "util.h"
 #include "main.h"
 #include "private.h"
+#include "timeout.h"
 
 /**
  * SECTION:main
@@ -57,6 +58,8 @@
 #define WATCH_FLAG_DISPATCHING	1
 #define WATCH_FLAG_DESTROYED	2
 
+#define WATCHDOG_TRIGGER_FREQ	2
+
 static int epoll_fd;
 static bool epoll_running;
 static bool epoll_terminate;
@@ -64,6 +67,8 @@ static int idle_id;
 
 static int notify_fd;
 
+static struct l_timeout *watchdog;
+
 static struct l_queue *idle_list;
 
 struct watch_data {
@@ -329,10 +334,21 @@ static void idle_dispatch(void *data, void *user_data)
 	idle->flags &= ~IDLE_FLAG_DISPATCHING;
 }
 
+static void watchdog_callback(struct l_timeout *timeout, void *user_data)
+{
+	int msec = L_PTR_TO_INT(user_data);
+
+	l_main_sd_notify("WATCHDOG=1");
+
+	l_timeout_modify_ms(timeout, msec);
+}
+
 static void create_sd_notify_socket(void)
 {
 	const char *sock;
 	struct sockaddr_un addr;
+	const char *watchdog_usec;
+	int msec;
 
 	/* check if NOTIFY_SOCKET has been set */
 	sock = getenv("NOTIFY_SOCKET");
@@ -359,7 +375,21 @@ static void create_sd_notify_socket(void)
 	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		close(notify_fd);
 		notify_fd = 0;
+		return;
 	}
+
+	watchdog_usec = getenv("WATCHDOG_USEC");
+	if (!watchdog_usec)
+		return;
+
+	msec = atoi(watchdog_usec) / 1000;
+	if (msec < 0)
+		return;
+
+	msec /= WATCHDOG_TRIGGER_FREQ;
+
+	watchdog = l_timeout_create_ms(msec, watchdog_callback,
+					L_INT_TO_PTR(msec), NULL);
 }
 
 
@@ -482,6 +512,8 @@ LIB_EXPORT int l_main_run(void)
 	if (notify_fd) {
 		close(notify_fd);
 		notify_fd = 0;
+		l_timeout_remove(watchdog);
+		watchdog = NULL;
 	}
 
 	return EXIT_SUCCESS;
Signature · 1 line
2.17.2
#3 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 11 lines · Luiz Augusto von Dentz

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

This adds support for sending states to the service manager using the socket set in $NOTIFY_SOCKET:

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

---
  ell/main.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/main.h |  2 ++
  2 files changed, 73 insertions(+)

<snip>

Quoted 28 lines
ell/main.c +24 -0
@@ -324,6 +329,40 @@ static void idle_dispatch(void *data, void *user_data)
  	idle->flags &= ~IDLE_FLAG_DISPATCHING;
  }
 
+static void create_sd_notify_socket(void)
+{
+	const char *sock;
+	struct sockaddr_un addr;
+
+	/* check if NOTIFY_SOCKET has been set */
+	sock = getenv("NOTIFY_SOCKET");
+	if (!sock)
+		return;
+
+	/* check for abstract socket or absolute path */
+	if (sock[0] != '@' && sock[0] != '/')
+		return;
+
+	notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+	if (notify_fd < 0) {
+		notify_fd = 0;
+		return;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, sock, sizeof(addr.sun_path) - 1);
+

ell/main.c static void idle_dispatch(void *data, void *user_data) · line 365

Quoted 10 lines
ell/main.c +10 -0
+	if (addr.sun_path[0] == '@')
+		addr.sun_path[0] = '\0';
+
+	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		close(notify_fd);
+		notify_fd = 0;
+	}
+}
+
+

I removed this double empty line and applied this patch, thanks.

Regards,
-Denis

#2 lore raw

Re: [PATCH v2 2/2] main: Add support for $WATCHDOG_USEC

In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 9 lines · Luiz Augusto von Dentz

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

This adds handling to watchdog set by service manager by sending "WATCHDOG=1" to it in intervals shorter than what is set in $WATCHDOG_USEC.

---
  ell/main.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)

<snip>

ell/main.c static void create_sd_notify_socket(void) · line 386

Quoted 13 lines
ell/main.c +8 -0
@@ -359,7 +375,21 @@ static void create_sd_notify_socket(void)
  	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  		close(notify_fd);
  		notify_fd = 0;
+		return;
  	}
+
+	watchdog_usec = getenv("WATCHDOG_USEC");
+	if (!watchdog_usec)
+		return;
+
+	msec = atoi(watchdog_usec) / 1000;
+	if (msec < 0)

So I changed this to if (msec < WATCHDOG_TRIGGER_FREQ), otherwise you can end up with 0 for the msec value. We might want to put an even higher clamp value for this.

ell/main.c static void create_sd_notify_socket(void) · line 394

Quoted 8 lines
ell/main.c +6 -0
+		return;
+
+	msec /= WATCHDOG_TRIGGER_FREQ;
+
+	watchdog = l_timeout_create_ms(msec, watchdog_callback,
+					L_INT_TO_PTR(msec), NULL);
  }
 

Applied, thanks.

Regards,
-Denis