Mark Wielaard da3f26
commit ba675ed25a26fd425ffd19b02cf18babf4291b4f
Mark Wielaard da3f26
Author: Mark Wielaard <mark@klomp.org>
Mark Wielaard da3f26
Date:   Thu May 5 23:59:57 2022 +0200
Mark Wielaard da3f26
Mark Wielaard da3f26
    debuginfod: Try without MHD_USE_DUAL_STACK if MHD_start_daemon fails
Mark Wielaard da3f26
    
Mark Wielaard da3f26
    On a systems that have ipv6 disabled debuginfod doesn't start up
Mark Wielaard da3f26
    anymore because libhttpd MHD_USE_DUAL_STACK only works if it can
Mark Wielaard da3f26
    open an ipv6 socket. If MHD_start_daemon with MHD_USE_DUAL_STACK
Mark Wielaard da3f26
    fails try again without that flag set.
Mark Wielaard da3f26
    
Mark Wielaard da3f26
    https://sourceware.org/bugzilla/show_bug.cgi?id=29122
Mark Wielaard da3f26
    
Mark Wielaard da3f26
    Signed-off-by: Mark Wielaard <mark@klomp.org>
Mark Wielaard da3f26
Mark Wielaard da3f26
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
Mark Wielaard da3f26
index 4aaf41c0..c02540f1 100644
Mark Wielaard da3f26
--- a/debuginfod/debuginfod.cxx
Mark Wielaard da3f26
+++ b/debuginfod/debuginfod.cxx
Mark Wielaard da3f26
@@ -3899,40 +3899,67 @@ main (int argc, char *argv[])
Mark Wielaard da3f26
         }
Mark Wielaard da3f26
     }
Mark Wielaard da3f26
 
Mark Wielaard da3f26
-  // Start httpd server threads.  Use a single dual-homed pool.
Mark Wielaard da3f26
-  MHD_Daemon *d46 = MHD_start_daemon ((connection_pool ? 0 : MHD_USE_THREAD_PER_CONNECTION)
Mark Wielaard da3f26
+  unsigned int mhd_flags = ((connection_pool
Mark Wielaard da3f26
+			     ? 0 : MHD_USE_THREAD_PER_CONNECTION)
Mark Wielaard da3f26
 #if MHD_VERSION >= 0x00095300
Mark Wielaard da3f26
-                                     | MHD_USE_INTERNAL_POLLING_THREAD
Mark Wielaard da3f26
+			    | MHD_USE_INTERNAL_POLLING_THREAD
Mark Wielaard da3f26
 #else
Mark Wielaard da3f26
-                                     | MHD_USE_SELECT_INTERNALLY
Mark Wielaard da3f26
+			    | MHD_USE_SELECT_INTERNALLY
Mark Wielaard da3f26
 #endif
Mark Wielaard da3f26
+			    | MHD_USE_DUAL_STACK
Mark Wielaard da3f26
 #ifdef MHD_USE_EPOLL
Mark Wielaard da3f26
-                                     | MHD_USE_EPOLL
Mark Wielaard da3f26
+			    | MHD_USE_EPOLL
Mark Wielaard da3f26
 #endif
Mark Wielaard da3f26
-                                     | MHD_USE_DUAL_STACK
Mark Wielaard da3f26
 #if MHD_VERSION >= 0x00095200
Mark Wielaard da3f26
-                                     | MHD_USE_ITC
Mark Wielaard da3f26
+			    | MHD_USE_ITC
Mark Wielaard da3f26
 #endif
Mark Wielaard da3f26
-                                     | MHD_USE_DEBUG, /* report errors to stderr */
Mark Wielaard da3f26
-                                     http_port,
Mark Wielaard da3f26
-                                     NULL, NULL, /* default accept policy */
Mark Wielaard da3f26
-                                     handler_cb, NULL, /* handler callback */
Mark Wielaard da3f26
-                                     MHD_OPTION_EXTERNAL_LOGGER, error_cb, NULL,
Mark Wielaard da3f26
-                                     (connection_pool ? MHD_OPTION_THREAD_POOL_SIZE : MHD_OPTION_END),
Mark Wielaard da3f26
-                                     (connection_pool ? (int)connection_pool : MHD_OPTION_END),
Mark Wielaard da3f26
-                                     MHD_OPTION_END);
Mark Wielaard da3f26
+			    | MHD_USE_DEBUG); /* report errors to stderr */
Mark Wielaard da3f26
 
Mark Wielaard da3f26
+  // Start httpd server threads.  Use a single dual-homed pool.
Mark Wielaard da3f26
+  MHD_Daemon *d46 = MHD_start_daemon (mhd_flags, http_port,
Mark Wielaard da3f26
+				      NULL, NULL, /* default accept policy */
Mark Wielaard da3f26
+				      handler_cb, NULL, /* handler callback */
Mark Wielaard da3f26
+				      MHD_OPTION_EXTERNAL_LOGGER,
Mark Wielaard da3f26
+				      error_cb, NULL,
Mark Wielaard da3f26
+				      (connection_pool
Mark Wielaard da3f26
+				       ? MHD_OPTION_THREAD_POOL_SIZE
Mark Wielaard da3f26
+				       : MHD_OPTION_END),
Mark Wielaard da3f26
+				      (connection_pool
Mark Wielaard da3f26
+				       ? (int)connection_pool
Mark Wielaard da3f26
+				       : MHD_OPTION_END),
Mark Wielaard da3f26
+				      MHD_OPTION_END);
Mark Wielaard da3f26
+
Mark Wielaard da3f26
+  MHD_Daemon *d4 = NULL;
Mark Wielaard da3f26
   if (d46 == NULL)
Mark Wielaard da3f26
     {
Mark Wielaard da3f26
-      sqlite3 *database = db;
Mark Wielaard da3f26
-      sqlite3 *databaseq = dbq;
Mark Wielaard da3f26
-      db = dbq = 0; // for signal_handler not to freak
Mark Wielaard da3f26
-      sqlite3_close (databaseq);
Mark Wielaard da3f26
-      sqlite3_close (database);
Mark Wielaard da3f26
-      error (EXIT_FAILURE, 0, "cannot start http server at port %d", http_port);
Mark Wielaard da3f26
-    }
Mark Wielaard da3f26
+      // Cannot use dual_stack, use ipv4 only
Mark Wielaard da3f26
+      mhd_flags &= ~(MHD_USE_DUAL_STACK);
Mark Wielaard da3f26
+      d4 = MHD_start_daemon (mhd_flags, http_port,
Mark Wielaard da3f26
+			     NULL, NULL, /* default accept policy */
Mark Wielaard da3f26
+			     handler_cb, NULL, /* handler callback */
Mark Wielaard da3f26
+			     MHD_OPTION_EXTERNAL_LOGGER,
Mark Wielaard da3f26
+			     error_cb, NULL,
Mark Wielaard da3f26
+			     (connection_pool
Mark Wielaard da3f26
+			      ? MHD_OPTION_THREAD_POOL_SIZE
Mark Wielaard da3f26
+			      : MHD_OPTION_END),
Mark Wielaard da3f26
+			     (connection_pool
Mark Wielaard da3f26
+			      ? (int)connection_pool
Mark Wielaard da3f26
+			      : MHD_OPTION_END),
Mark Wielaard da3f26
+			     MHD_OPTION_END);
Mark Wielaard da3f26
+      if (d4 == NULL)
Mark Wielaard da3f26
+	{
Mark Wielaard da3f26
+	  sqlite3 *database = db;
Mark Wielaard da3f26
+	  sqlite3 *databaseq = dbq;
Mark Wielaard da3f26
+	  db = dbq = 0; // for signal_handler not to freak
Mark Wielaard da3f26
+	  sqlite3_close (databaseq);
Mark Wielaard da3f26
+	  sqlite3_close (database);
Mark Wielaard da3f26
+	  error (EXIT_FAILURE, 0, "cannot start http server at port %d",
Mark Wielaard da3f26
+		 http_port);
Mark Wielaard da3f26
+	}
Mark Wielaard da3f26
 
Mark Wielaard da3f26
-  obatched(clog) << "started http server on IPv4 IPv6 "
Mark Wielaard da3f26
+    }
Mark Wielaard da3f26
+  obatched(clog) << "started http server on"
Mark Wielaard da3f26
+                 << (d4 != NULL ? " IPv4 " : " IPv4 IPv6 ")
Mark Wielaard da3f26
                  << "port=" << http_port << endl;
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   // add maxigroom sql if -G given
Mark Wielaard da3f26
@@ -4053,6 +4080,7 @@ main (int argc, char *argv[])
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   /* Stop all the web service threads. */
Mark Wielaard da3f26
   if (d46) MHD_stop_daemon (d46);
Mark Wielaard da3f26
+  if (d4) MHD_stop_daemon (d4);
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   if (! passive_p)
Mark Wielaard da3f26
     {