Blame otp-0012-Add-systemd-support-to-epmd.patch

f7eb6d
From: "Matwey V. Kornilov" <matwey.kornilov@gmail.com>
f7eb6d
Date: Tue, 17 Dec 2013 18:57:54 +0400
f7eb6d
Subject: [PATCH] Add systemd support to epmd
f7eb6d
f7eb6d
f7eb6d
diff --git a/erts/configure.in b/erts/configure.in
f7eb6d
index 125e579..d9bc1ec 100644
f7eb6d
--- a/erts/configure.in
f7eb6d
+++ b/erts/configure.in
f7eb6d
@@ -969,6 +969,8 @@ AC_CHECK_LIB(dl, dlopen)
f7eb6d
 AC_CHECK_LIB(inet, main)
f7eb6d
 AC_CHECK_LIB(util, openpty)
f7eb6d
 
f7eb6d
+AC_CHECK_LIB(systemd-daemon, sd_listen_fds)
f7eb6d
+
f7eb6d
 dnl Try to find a thread library.
f7eb6d
 dnl
f7eb6d
 dnl ETHR_LIB_NAME, ETHR_LIBS, ETHR_X_LIBS, ETHR_THR_LIB_BASE and ETHR_DEFS
f7eb6d
diff --git a/erts/epmd/src/epmd_int.h b/erts/epmd/src/epmd_int.h
f7eb6d
index bf1ddd8..363923e 100644
f7eb6d
--- a/erts/epmd/src/epmd_int.h
f7eb6d
+++ b/erts/epmd/src/epmd_int.h
f7eb6d
@@ -110,6 +110,10 @@
f7eb6d
 
f7eb6d
 #include <stdarg.h>
f7eb6d
 
f7eb6d
+#ifdef HAVE_SYSTEMD_SD_DAEMON_H
f7eb6d
+#  include <systemd/sd-daemon.h>
f7eb6d
+#endif
f7eb6d
+
f7eb6d
 /* ************************************************************************ */
f7eb6d
 /* Replace some functions by others by making the function name a macro */
f7eb6d
 
f7eb6d
diff --git a/erts/epmd/src/epmd_srv.c b/erts/epmd/src/epmd_srv.c
f7eb6d
index 90df7cc..cb8ca96 100644
f7eb6d
--- a/erts/epmd/src/epmd_srv.c
f7eb6d
+++ b/erts/epmd/src/epmd_srv.c
f7eb6d
@@ -208,6 +208,39 @@ void run(EpmdVars *g)
f7eb6d
   node_init(g);
f7eb6d
   g->conn = conn_init(g);
f7eb6d
 
f7eb6d
+#ifdef HAVE_SYSTEMD_SD_DAEMON_H
f7eb6d
+  if (g->is_systemd)
f7eb6d
+    {
f7eb6d
+      int n;
f7eb6d
+      
f7eb6d
+      dbg_printf(g,2,"try to obtain sockets from systemd");
f7eb6d
+
f7eb6d
+      n = sd_listen_fds(0);
f7eb6d
+      if (n < 0)
f7eb6d
+        {
f7eb6d
+          dbg_perror(g,"cannot obtain sockets from systemd");
f7eb6d
+          epmd_cleanup_exit(g,1);
f7eb6d
+        }
f7eb6d
+      else if (n == 0)
f7eb6d
+        {
f7eb6d
+          dbg_tty_printf(g,0,"systemd provides no sockets");
f7eb6d
+          epmd_cleanup_exit(g,1);
f7eb6d
+      }
f7eb6d
+      else if (n > MAX_LISTEN_SOCKETS)
f7eb6d
+      {
f7eb6d
+          dbg_tty_printf(g,0,"cannot listen on more than %d IP addresses", MAX_LISTEN_SOCKETS);
f7eb6d
+          epmd_cleanup_exit(g,1);
f7eb6d
+      } 
f7eb6d
+      num_sockets = n;
f7eb6d
+      for (i = 0; i < num_sockets; i++)
f7eb6d
+        {
f7eb6d
+          g->listenfd[i] = listensock[i] = SD_LISTEN_FDS_START + i;
f7eb6d
+        }
f7eb6d
+    }
f7eb6d
+  else
f7eb6d
+    {
f7eb6d
+#endif
f7eb6d
+
f7eb6d
   dbg_printf(g,2,"try to initiate listening port %d", g->port);
f7eb6d
 
f7eb6d
   if (g->addresses != NULL && /* String contains non-separator characters if: */
f7eb6d
@@ -272,6 +305,9 @@ void run(EpmdVars *g)
f7eb6d
       SET_ADDR(iserv_addr[0],EPMD_ADDR_ANY,sport);
f7eb6d
       num_sockets = 1;
f7eb6d
     }
f7eb6d
+#ifdef HAVE_SYSTEMD_SD_DAEMON_H
f7eb6d
+    }
f7eb6d
+#endif
f7eb6d
 
f7eb6d
 #if !defined(__WIN32__)
f7eb6d
   /* We ignore the SIGPIPE signal that is raised when we call write
f7eb6d
@@ -289,6 +325,13 @@ void run(EpmdVars *g)
f7eb6d
   FD_ZERO(&g->orig_read_mask);
f7eb6d
   g->select_fd_top = 0;
f7eb6d
 
f7eb6d
+#ifdef HAVE_SYSTEMD_SD_DAEMON_H
f7eb6d
+  if (g->is_systemd)
f7eb6d
+      for (i = 0; i < num_sockets; i++)
f7eb6d
+          select_fd_set(g, listensock[i]);
f7eb6d
+  else
f7eb6d
+    {
f7eb6d
+#endif
f7eb6d
   for (i = 0; i < num_sockets; i++)
f7eb6d
     {
f7eb6d
       if ((listensock[i] = socket(FAMILY,SOCK_STREAM,0)) < 0)
f7eb6d
@@ -351,6 +394,9 @@ void run(EpmdVars *g)
f7eb6d
       }
f7eb6d
       select_fd_set(g, listensock[i]);
f7eb6d
     }
f7eb6d
+#ifdef HAVE_SYSTEMD_SD_DAEMON_H 
f7eb6d
+    }
f7eb6d
+#endif
f7eb6d
 
f7eb6d
   dbg_tty_printf(g,2,"entering the main select() loop");
f7eb6d