27aea6
From b81490dd5c9eebdd29c0848ca80d6ea78f4818bd Mon Sep 17 00:00:00 2001
27aea6
From: Stephan Bergmann <sbergman@redhat.com>
27aea6
Date: Thu, 19 Apr 2018 13:59:16 +0200
27aea6
Subject: [PATCH] tdf#95843: Wait for fire_glxtest_process also in --headless
27aea6
 mode
27aea6
MIME-Version: 1.0
27aea6
Content-Type: text/plain; charset=UTF-8
27aea6
Content-Transfer-Encoding: 8bit
27aea6
27aea6
Discussed with mmeeks on IRC that fire_glxtest_process is probably called as
27aea6
early as possible so that its reuslt is ready by the time it is needed in the
27aea6
non-headless case.  So best fix for headless is probably to just wait for the
27aea6
sub-process at an opportune point, instead of redesigning the whole mess so that
27aea6
fire_glxtest_process would only be called once its result is actually needed.
27aea6
27aea6
Change-Id: I4ea9c9d54b83c9695a3b72317e68fed0c410da0e
27aea6
Reviewed-on: https://gerrit.libreoffice.org/53154
27aea6
Tested-by: Jenkins <ci@libreoffice.org>
27aea6
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
27aea6
(cherry picked from commit 4bacf58f4af44ac8c4632b43289ccfcc07e5820c)
27aea6
Reviewed-on: https://gerrit.libreoffice.org/53170
27aea6
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
27aea6
Tested-by: Caolán McNamara <caolanm@redhat.com>
27aea6
---
27aea6
 desktop/inc/app.hxx                |  1 +
27aea6
 desktop/source/app/app.cxx         |  9 +++++++++
27aea6
 desktop/source/app/sofficemain.cxx |  4 ++++
27aea6
 vcl/inc/opengl/x11/glxtest.hxx     |  2 ++
27aea6
 vcl/unx/glxtest.cxx                | 16 ++++++++++++++++
27aea6
 5 files changed, 32 insertions(+)
27aea6
27aea6
diff --git a/desktop/inc/app.hxx b/desktop/inc/app.hxx
27aea6
index 91dde49..68fc4a3 100644
27aea6
--- a/desktop/inc/app.hxx
27aea6
+++ b/desktop/inc/app.hxx
27aea6
@@ -187,6 +187,7 @@ OUString ReplaceStringHookProc(const OUString& rStr);
27aea6
 
27aea6
 #if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined LIBO_HEADLESS
27aea6
 bool fire_glxtest_process();
27aea6
+void reap_glxtest_process();
27aea6
 #endif
27aea6
 
27aea6
 #endif // INCLUDED_DESKTOP_INC_APP_HXX
27aea6
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
27aea6
index dc7ff1e..c8b8db5 100644
27aea6
--- a/desktop/source/app/app.cxx
27aea6
+++ b/desktop/source/app/app.cxx
27aea6
@@ -1649,6 +1649,15 @@ int Desktop::Main()
27aea6
         CheckOpenCLCompute(xDesktop);
27aea6
 #endif
27aea6
 
27aea6
+        // In headless mode, reap the process started by fire_glxtest_process() early in soffice_main
27aea6
+        // (desktop/source/app/sofficemain.cxx), in a code block that needs to be covered by the same
27aea6
+        // #if condition as this code block:
27aea6
+#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined(LIBO_HEADLESS) && HAVE_FEATURE_OPENGL
27aea6
+        if (rCmdLineArgs.IsHeadless()) {
27aea6
+            reap_glxtest_process();
27aea6
+        }
27aea6
+#endif
27aea6
+
27aea6
         // Release solar mutex just before we wait for our client to connect
27aea6
         {
27aea6
             SolarMutexReleaser aReleaser;
27aea6
diff --git a/desktop/source/app/sofficemain.cxx b/desktop/source/app/sofficemain.cxx
27aea6
index 27b9efa..9cbcbc0 100644
27aea6
--- a/desktop/source/app/sofficemain.cxx
27aea6
+++ b/desktop/source/app/sofficemain.cxx
27aea6
@@ -125,6 +125,10 @@ extern "C" int DESKTOP_DLLPUBLIC soffice_main()
27aea6
 #if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined(LIBO_HEADLESS) && HAVE_FEATURE_OPENGL
27aea6
     /* Run test for OpenGL support in own process to avoid crash with broken
27aea6
      * OpenGL drivers. Start process as early as possible.
27aea6
+     * In non-headless mode, the process will be reaped in X11OpenGLDeviceInfo::GetData
27aea6
+     * (vcl/opengl/x11/X11DeviceInfo.cxx).  In headless mode, the process will be reaped late in
27aea6
+     * Desktop::Main (desktop/source/app/app.cxx), in a code block that needs to be covered by the
27aea6
+     * same #if condition as this code block.
27aea6
      */
27aea6
     bool bSuccess = fire_glxtest_process();
27aea6
     SAL_WARN_IF(!bSuccess, "desktop.opengl", "problems with glxtest");
27aea6
diff --git a/vcl/inc/opengl/x11/glxtest.hxx b/vcl/inc/opengl/x11/glxtest.hxx
27aea6
index 979f795..d74436a 100644
27aea6
--- a/vcl/inc/opengl/x11/glxtest.hxx
27aea6
+++ b/vcl/inc/opengl/x11/glxtest.hxx
27aea6
@@ -18,6 +18,8 @@ VCL_DLLPUBLIC pid_t* getGlxPid();
27aea6
 
27aea6
 bool fire_glxtest_process();
27aea6
 
27aea6
+void reap_glxtest_process();
27aea6
+
27aea6
 #endif
27aea6
 
27aea6
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
27aea6
diff --git a/vcl/unx/glxtest.cxx b/vcl/unx/glxtest.cxx
27aea6
index efe5591..87c6044 100644
27aea6
--- a/vcl/unx/glxtest.cxx
27aea6
+++ b/vcl/unx/glxtest.cxx
27aea6
@@ -26,6 +26,8 @@
27aea6
 #include "stdint.h"
27aea6
 #include <string.h>
27aea6
 
27aea6
+#include <sys/wait.h>
27aea6
+
27aea6
 #include "opengl/x11/glxtest.hxx"
27aea6
 
27aea6
 #ifdef __SUNPRO_CC
27aea6
@@ -35,6 +37,8 @@
27aea6
 #include "X11/Xlib.h"
27aea6
 #include "X11/Xutil.h"
27aea6
 
27aea6
+#include <sal/log.hxx>
27aea6
+
27aea6
 // stuff from glx.h
27aea6
 typedef struct __GLXcontextRec *GLXContext;
27aea6
 typedef XID GLXPixmap;
27aea6
@@ -273,3 +277,15 @@ bool fire_glxtest_process()
27aea6
   *glxtest_pid = pid;
27aea6
   return true;
27aea6
 }
27aea6
+
27aea6
+void reap_glxtest_process() {
27aea6
+    pid_t * pid = getGlxPid();
27aea6
+    if (*pid != 0) {
27aea6
+        // Use WNOHANG, as it is probably better to have a (rather harmless) zombie child process
27aea6
+        // hanging around for the duration of the calling process, than to potentially block the
27aea6
+        // calling process here:
27aea6
+        pid_t e = waitpid(*pid, nullptr, WNOHANG);
27aea6
+        SAL_INFO_IF(
27aea6
+            e <= 0, "vcl.opengl", "waiting for glxtest process " << *pid << " failed with " << e);
27aea6
+    }
27aea6
+}
27aea6
-- 
27aea6
2.14.3
27aea6