[PATCH v3 4/5] dirmngr: Implement --supervised command (for systemd, etc).

Daniel Kahn Gillmor dkg at fifthhorseman.net
Fri Aug 12 07:38:00 CEST 2016


* dirmngr/dirmngr.c (main): Add new --supervised command, which is
  a mode designed for running under a process supervision system
  like systemd or runit.
* doc/dirmngr.texi: document --supervised option.

--

"dirmngr --supervised" is a way to invoke dirmngr such that a system
supervisor like systemd can provide socket-activated startup, log
management, and scheduled shutdown.

When running in this mode, dirmngr:

 * Does not open its own listening socket; rather, it expects to be
   given a listening socket on file descriptor 3.

 * Does not detach from the invoking process, staying in the
   foreground instead.

Signed-off-by: Daniel Kahn Gillmor <dkg at fifthhorseman.net>
---
 dirmngr/dirmngr.c | 37 +++++++++++++++++++++++++++++++++++++
 doc/dirmngr.texi  |  7 +++++++
 2 files changed, 44 insertions(+)

diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c
index cb17420..fbbad82 100644
--- a/dirmngr/dirmngr.c
+++ b/dirmngr/dirmngr.c
@@ -94,6 +94,7 @@ enum cmd_and_opt_values {
 
   aServer,
   aDaemon,
+  aSupervised,
   aService,
   aListCRLs,
   aLoadCRL,
@@ -155,6 +156,7 @@ static ARGPARSE_OPTS opts[] = {
 
   ARGPARSE_c (aServer,   "server",  N_("run in server mode (foreground)") ),
   ARGPARSE_c (aDaemon,   "daemon",  N_("run in daemon mode (background)") ),
+  ARGPARSE_c (aSupervised, "supervised", N_("run under supervision (e.g. systemd)")),
 #ifdef USE_W32_SERVICE
   ARGPARSE_c (aService,  "service", N_("run as windows service (background)")),
 #endif
@@ -911,6 +913,7 @@ main (int argc, char **argv)
         {
         case aServer:
         case aDaemon:
+        case aSupervised:
         case aService:
         case aShutdown:
         case aFlush:
@@ -1093,6 +1096,40 @@ main (int argc, char **argv)
       start_command_handler (ASSUAN_INVALID_FD);
       shutdown_reaper ();
     }
+  else if (cmd == aSupervised)
+    {
+    /* In supervised mode, we expect file descriptor 3 to be an
+       already opened, listening socket.
+
+       We will also not detach from the controlling process or close
+       stderr; the supervisor should handle all of that.
+
+       FIXME: should we try to extract the actual socket_name from the
+       file descriptor with getsockname(2) so that we can return it
+       correctly in response to "GETINFO socket_name"?  This isn't
+       currently done when --socket-name is supplied.
+     */
+      /* Now start with logging to a file if this is desired. */
+      if (logfile)
+        {
+          log_set_file (logfile);
+          log_set_prefix (NULL, (GPGRT_LOG_WITH_PREFIX
+                                 |GPGRT_LOG_WITH_TIME
+                                 |GPGRT_LOG_WITH_PID));
+          current_logfile = xstrdup (logfile);
+        }
+      else
+        log_set_prefix (NULL, 0);
+
+#if USE_LDAP
+      ldap_wrapper_launch_thread ();
+#endif /*USE_LDAP*/
+      cert_cache_init ();
+      crl_cache_init ();
+      handle_connections (3);
+      assuan_sock_close (3);
+      shutdown_reaper ();
+    }
   else if (cmd == aDaemon)
     {
       assuan_fd_t fd;
diff --git a/doc/dirmngr.texi b/doc/dirmngr.texi
index 033b5d3..65f7d59 100644
--- a/doc/dirmngr.texi
+++ b/doc/dirmngr.texi
@@ -90,6 +90,13 @@ Run in background daemon mode and listen for commands on a socket.
 Note that this also changes the default home directory and enables the
 internal certificate validation code.  This mode is deprecated.
 
+ at item --supervised
+ at opindex supervised
+Run in the foreground, sending logs to stderr, and listening on file
+descriptor 3, which must already be bound to a listening socket.  This
+is useful when running under systemd or other similar process
+supervision schemes.
+
 @item --list-crls
 @opindex list-crls
 List the contents of the CRL cache on @code{stdout}. This is probably
-- 
2.8.1




More information about the Gnupg-devel mailing list