main: connect instead of binding to systemd notify socket
PATCH 1/4 main: connect instead of binding to systemd notify socket
Binding is for the server (i.e. systemd), while connecting is for the client (i.e. ell).
--- ell/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ell/main.c +1 -1
@@ -374,7 +374,7 @@ static void create_sd_notify_socket(void)if (addr.sun_path[0 ] =='@' ) addr.sun_path[0 ] ='\0' ; -if (bind (notify_fd, (struct sockaddr *) &addr,sizeof (addr)) <0 ) { +if (connect (notify_fd, (struct sockaddr *) &addr,sizeof (addr)) <0 ) {close (notify_fd); notify_fd =0 ;return ;
Signature · 1 line
2.51.0
PATCH 2/4 main: strip trailing null bytes from systemd notify socket path
When connecting to abstract unix sockets, the trailing null bytes are considered as part of the (abstract) socket path, hence failing the connection if the abstract name doesn't fill up the socket pacth member.
Fix support for abstract notify socket names by only passing the absolutely needed size of the socket address structure when connecting (i.e. the two family bytes, the null path prefix, and the path name).
--- ell/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
ell/main.c +3 -1
@@ -350,6 +350,7 @@ static void create_sd_notify_socket(void)const char *sock;struct sockaddr_un addr;const char *watchdog_usec; +socklen_t len;int msec;/* check if NOTIFY_SOCKET has been set */
@@ -370,11 +371,12 @@ static void create_sd_notify_socket(void)memset (&addr,0 ,sizeof (addr)); addr.sun_family =AF_UNIX ;strncpy (addr.sun_path, sock,sizeof (addr.sun_path) -1 ); + len = offsetof(struct sockaddr_un , sun_path) +strlen (sock);if (addr.sun_path[0 ] =='@' ) addr.sun_path[0 ] ='\0' ; -if (connect (notify_fd, (struct sockaddr *) &addr,sizeof (addr)) <0 ) { +if (connect (notify_fd, (struct sockaddr *) &addr, len) <0 ) {close (notify_fd); notify_fd =0 ;return ;
Signature · 1 line
2.51.0
PATCH 3/4 main: signal readiness to system before entering main loop
In reply to Martin HundebøllThe systemd watchdog protocol requires the service to signal readiness before maintaining the watchdog. Do this by sending READY=1 on the notification socket before entering the main loop, from where the watchdog is maintained.
--- ell/main.c | 1 + 1 file changed, 1 insertion(+)
ell/main.c +1 -0
@@ -500,6 +500,7 @@ LIB_EXPORT int l_main_run(void)if (unlikely (epoll_running))return EXIT_FAILURE ; +sd_notify ("READY=1" ); epoll_running = true;for (;;) {
Signature · 1 line
2.51.0
PATCH 4/4 unit: test sd-notify functionality
In reply to Martin Hundebøll--- unit/test-main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
unit/test-main.c +70 -0
@@ -14,6 +14,9 @@#include <assert.h> #include <limits.h> #include <signal.h> +#include <stdlib.h> +#include <sys/socket.h> +#include <sys/un.h> #include <ell/ell.h>
@@ -123,11 +126,78 @@ static void test_main(const void *data)assert (l_main_exit ()); } +static int sock_bind (const char *sock) +{ +struct sockaddr_un addr; +socklen_t len; +int fd; + + fd =socket (AF_UNIX ,SOCK_DGRAM |SOCK_CLOEXEC |SOCK_NONBLOCK ,0 ); +assert (fd >=0 ); + +memset (&addr,0 ,sizeof (addr)); + addr.sun_family =AF_UNIX ; +strncpy (addr.sun_path, sock,sizeof (addr.sun_path) -1 ); + len = offsetof(struct sockaddr_un , sun_path) +strlen (sock); + +if (addr.sun_path[0 ] =='@' ) + addr.sun_path[0 ] ='\0' ; + +assert (!bind (fd, (struct sockaddr *) &addr, len)); + +return fd; +} + +static void test_sd_notify (const void *data) +{ +struct l_timeout *timeout_quit; +char sock[] ="/tmp/ell-notify.XXXXXX" ; +bool abstract =L_PTR_TO_INT (data); +int exit_status; +char buf[4096 ]; +int fd; + +/* Create a unique temporary path. */ +assert (!close (mkstemp (sock))); +assert (!unlink (sock)); + +if (abstract) + sock[0 ] ='@' ; + + fd =sock_bind (sock); + +setenv ("NOTIFY_SOCKET" , sock,1 ); +setenv ("WATCHDOG_USEC" ,"1000000" ,1 ); + +assert (l_main_init ()); + + timeout_quit =l_timeout_create (1 , timeout_quit_handler,NULL ,NULL ); +assert (timeout_quit); + + exit_status =l_main_run_with_signal (signal_handler,NULL ); +assert (exit_status ==EXIT_SUCCESS ); + +assert (l_main_exit ()); + +assert (recv (fd, buf,sizeof (buf),MSG_TRUNC ) ==7 ); +assert (strncmp (buf,"READY=1" ,7 ) ==0 ); + +assert (recv (fd, buf,sizeof (buf),MSG_TRUNC ) ==10 ); +assert (strncmp (buf,"WATCHDOG=1" ,10 ) ==0 ); + +if (!abstract) +unlink (sock); + +close (fd); +} +int main (int argc,char *argv[]) {l_test_init (&argc, &argv);l_test_add ("main" , test_main,NULL ); +l_test_add ("sd_notify path" , test_sd_notify,L_INT_TO_PTR (false)); +l_test_add ("sd_notify abstract" , test_sd_notify,L_INT_TO_PTR (true));return l_test_run (); }
Signature · 1 line
2.51.0
Quoted 6 lines · Martin Hundebøll
Binding is for the server (i.e. systemd), while connecting is for the client (i.e. ell).
--- ell/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
All applied, thanks Martin.
Regards,
-Denis