summaryrefslogtreecommitdiff
path: root/patches/xserver-meson/0002-Use-spawnl-rather-than-pipe-fork-to-invoke-xkbcomp.patch
blob: d4afebf0cf124b80a88a0e84d9caa976ae840883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
From 591c0c87b40180afe5c01b78b74a7545fe6867ce Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Mon, 5 Mar 2012 22:16:33 +0000
Subject: [PATCH xserver] Use spawnl() rather than pipe() & fork() to invoke
 xkbcomp

Implement System() specially on Cygwin not to use fork() and exec(), but
spawnl()

Use System() and a temp file rather than pipe() & fork() to invoke xkbcomp on
cygwin, just as done on mingw

Somewhat the counsel of despair, but sometimes Windows is being such a little
bitch that the cygwin DLL just can't emulate fork() successfully, so just using
spawnl() here is more robust and means I don't have to deal with end-users
reporting this as a problem with the xserver

Popen is not used, but must be built as it is referenced by sdksyms

v2: Update to include fix to Win32TempDir() when TMP is set but TEMP isn't

XXX: Really this should check TMPDIR first, as that's the canonical UNIX way to
set the temporary directory?

v3: Fix implicit-function-declaration of Win32TempDir() in XkbDDXCompileKeymapByNames()

Add a prototype for Win32TempDir()

v4: Improve handling of error conditions with the xkbcomp temporary file

Fix a compilation conditional so that a failure to write to the xkbcomp
temporary file is correctly reported, and reports the problematic filename.

Improve Win32TempDir() so that the directory given by the TEMP and TMP
environment variables is checked for accessibility, so that specifying a
non-existent directory doesn't cause a failure.

Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
---
 include/os.h  |  4 ++++
 os/utils.c    | 35 +++++++++++++++++++++++++++++++++++
 xkb/ddxLoad.c | 17 +++++++++--------
 3 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/include/os.h b/include/os.h
index e141a6b02..face87297 100644
--- a/include/os.h
+++ b/include/os.h
@@ -356,6 +356,10 @@ extern _X_EXPORT void *
 Fopen(const char *, const char *);
 extern _X_EXPORT int
 Fclose(void *);
+#if defined(__CYGWIN__)
+extern const char *
+Win32TempDir(void);
+#endif
 #else
 
 extern const char *
diff --git a/os/utils.c b/os/utils.c
index 8a758f0b9..d1df04900 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1361,6 +1361,26 @@ OsAbort(void)
  * as well.  As it is now, xkbcomp messages don't end up in the log file.
  */
 
+#ifdef __CYGWIN__
+#include <process.h>
+int
+System(const char *command)
+{
+    int status;
+
+    if (!command)
+        return 1;
+
+    DebugF("System: `%s'\n", command);
+
+    /*
+       Use spawnl() rather than execl() to implement System() on cygwin to
+       avoid fork emulation overhead and brittleness
+     */
+    status = spawnl(_P_WAIT, "/bin/sh", "sh", "-c", command, (char *) NULL);
+    return status;
+}
+#else
 int
 System(const char *command)
 {
@@ -1403,6 +1423,7 @@ System(const char *command)
 
     return p == -1 ? -1 : status;
 }
+#endif
 
 static struct pid {
     struct pid *next;
@@ -1717,6 +1738,20 @@ System(const char *cmdline)
 
     return dwExitCode;
 }
+#elif defined(__CYGWIN__)
+const char*
+Win32TempDir(void)
+{
+    const char *temp = getenv("TEMP");
+    if ((temp != NULL) && (access(temp, W_OK | X_OK) == 0))
+        return temp;
+
+    temp = getenv("TMP");
+    if ((temp != NULL) && (access(temp, W_OK | X_OK) == 0))
+        return temp;
+
+    return "/tmp";
+}
 #endif
 
 /*
diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c
index a8b8368a6..4e39cd0c1 100644
--- a/xkb/ddxLoad.c
+++ b/xkb/ddxLoad.c
@@ -44,6 +44,7 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include <xkbsrv.h>
 #include <X11/extensions/XI.h>
 #include "xkb.h"
+#include "os.h"
 
 #define	PRE_ERROR_MSG "\"The XKEYBOARD keymap compiler (xkbcomp) reports:\""
 #define	ERROR_PREFIX	"\"> \""
@@ -101,7 +102,7 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
     const char *xkbbindir = emptystring;
     const char *xkbbindirsep = emptystring;
 
-#ifdef WIN32
+#if defined(WIN32) || defined(__CYGWIN__)
     /* WIN32 has no popen. The input must be stored in a file which is
        used as input for xkbcomp. xkbcomp does not read from stdin. */
     char tmpname[PATH_MAX];
@@ -114,9 +115,9 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
 
     OutputDirectory(xkm_output_dir, sizeof(xkm_output_dir));
 
-#ifdef WIN32
+#if defined(WIN32) || defined(__CYGWIN__)
     strcpy(tmpname, Win32TempDir());
-    strcat(tmpname, "\\xkb_XXXXXX");
+    strcat(tmpname, PATHSEPARATOR "xkb_XXXXXX");
     (void) mktemp(tmpname);
 #endif
 
@@ -155,7 +156,7 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
         return NULL;
     }
 
-#ifndef WIN32
+#if !defined(WIN32) && !defined(__CYGWIN__)
     out = Popen(buf, "w");
 #else
     out = fopen(tmpname, "w");
@@ -165,7 +166,7 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
         /* Now write to xkbcomp */
         (*callback)(out, userdata);
 
-#ifndef WIN32
+#if !defined(WIN32) && !defined(__CYGWIN__)
         if (Pclose(out) == 0)
 #else
         if (fclose(out) == 0 && System(buf) >= 0)
@@ -174,7 +175,7 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
             if (xkbDebugFlags)
                 DebugF("[xkb] xkb executes: %s\n", buf);
             free(buf);
-#ifdef WIN32
+#if defined(WIN32) || defined(__CYGWIN__)
             unlink(tmpname);
 #endif
             return xnfstrdup(keymap);
@@ -182,14 +183,14 @@ RunXkbComp(xkbcomp_buffer_callback callback, void *userdata)
         else {
             LogMessage(X_ERROR, "Error compiling keymap (%s) executing '%s'\n",
                        keymap, buf);
+#if defined(WIN32) || defined(__CYGWIN__)
         }
-#ifdef WIN32
         /* remove the temporary file */
         unlink(tmpname);
 #endif
     }
     else {
-#ifndef WIN32
+#if !defined(WIN32) && !defined(__CYGWIN__)
         LogMessage(X_ERROR, "XKB: Could not invoke xkbcomp\n");
 #else
         LogMessage(X_ERROR, "Could not open file %s\n", tmpname);
-- 
2.15.1