]> code.ossystems Code Review - openembedded-core.git/blob
6b1b40f62ebdf92bf6be07f8cef9d40911efdc83
[openembedded-core.git] /
1 From 0f70ba9d3412b17ac4e08e33e1be3c226c06ea54 Mon Sep 17 00:00:00 2001
2 From: Yan Li <yan.i.li@intel.com>
3 Date: Tue, 12 May 2009 17:49:07 +0800
4 Subject: [PATCH] XKB: cache xkbcomp output for fast start-up v5 for 1.6.1
5 Organization: Intel
6
7 xkbcomp outputs will be cached in files with hashed keymap as
8 names. This saves boot time for around 1s on commodity netbooks.
9
10 Upstream-Status: Pending
11
12 Signed-off-by: Yan Li <yan.i.li@intel.com>
13 ---
14  configure.ac        |    6 +-
15  xkb/README.compiled |    8 +-
16  xkb/ddxLoad.c       |  192 +++++++++++++++++++++++++++++++++++++++++---------
17  3 files changed, 164 insertions(+), 42 deletions(-)
18
19 diff --git a/configure.ac b/configure.ac
20 index 4c4c797..7a5020a 100644
21 --- a/configure.ac
22 +++ b/configure.ac
23 @@ -476,9 +476,9 @@ AC_ARG_WITH(default-font-path, AS_HELP_STRING([--with-default-font-path=PATH], [
24  AC_ARG_WITH(xkb-path,         AS_HELP_STRING([--with-xkb-path=PATH], [Path to XKB base dir (default: ${datadir}/X11/xkb)]),
25                                 [ XKBPATH="$withval" ],
26                                 [ XKBPATH="${datadir}/X11/xkb" ])
27 -AC_ARG_WITH(xkb-output,       AS_HELP_STRING([--with-xkb-output=PATH], [Path to XKB output dir (default: ${datadir}/X11/xkb/compiled)]),
28 +AC_ARG_WITH(xkb-output,       AS_HELP_STRING([--with-xkb-output=PATH], [Path to XKB output dir (default: ${localstatedir}/cache/xkb)]),
29                                 [ XKBOUTPUT="$withval" ],
30 -                               [ XKBOUTPUT="compiled" ])
31 +                               [ XKBOUTPUT="${localstatedir}/cache/xkb" ])
32  AC_ARG_WITH(serverconfig-path, AS_HELP_STRING([--with-serverconfig-path=PATH],
33                                    [Directory where ancillary server config files are installed (default: ${libdir}/xorg)]),
34                                 [ SERVERCONFIG="$withval" ],
35 @@ -1757,7 +1757,7 @@ AC_DEFINE_DIR(XKB_BIN_DIRECTORY, bindir, [Path to XKB bin dir])
36  XKBOUTPUT_FIRSTCHAR=`echo $XKBOUTPUT | cut -b 1`
37  
38  if [[ x$XKBOUTPUT_FIRSTCHAR != x/ ]] ; then
39 -   XKBOUTPUT="$XKB_BASE_DIRECTORY/$XKBOUTPUT"
40 +   AC_MSG_ERROR([xkb-output must be an absolute path.])
41  fi
42  
43  # XKM_OUTPUT_DIR (used in code) must end in / or file names get hosed
44 diff --git a/xkb/README.compiled b/xkb/README.compiled
45 index 71caa2f..a4a2ae0 100644
46 --- a/xkb/README.compiled
47 +++ b/xkb/README.compiled
48 @@ -4,10 +4,10 @@ current keymap and/or any scratch keymaps used by clients.  The X server
49  or some other tool might destroy or replace the files in this directory,
50  so it is not a safe place to store compiled keymaps for long periods of
51  time.  The default keymap for any server is usually stored in:
52 -     X<num>-default.xkm
53 -where <num> is the display number of the server in question, which makes
54 -it possible for several servers *on the same host* to share the same 
55 -directory.
56 +     server-<SHA1>.xkm
57 +
58 +where <SHA1> is the SHA1 hash of keymap source, so that compiled
59 +keymap of different keymap sources are stored in different files.
60  
61  Unless the X server is modified, sharing this directory between servers on
62  different hosts could cause problems.
63 diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c
64 index 4d5dfb6..60a68af 100644
65 --- a/xkb/ddxLoad.c
66 +++ b/xkb/ddxLoad.c
67 @@ -32,6 +32,12 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
68  #include <xkb-config.h>
69  #endif
70  
71 +#ifdef HAVE_SHA1_IN_LIBMD /* Use libmd for SHA1 */
72 +# include <sha1.h>
73 +#else /* Use OpenSSL's libcrypto */
74 +# include <stddef.h>  /* buggy openssl/sha.h wants size_t */
75 +# include <openssl/sha.h>
76 +#endif
77  #include <stdio.h>
78  #include <ctype.h>
79  #define        NEED_EVENTS 1
80 @@ -46,24 +52,13 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
81  #define        XKBSRV_NEED_FILE_FUNCS
82  #include <xkbsrv.h>
83  #include <X11/extensions/XI.h>
84 +#include <errno.h>
85  #include "xkb.h"
86  
87  #if defined(CSRG_BASED) || defined(linux) || defined(__GNU__)
88  #include <paths.h>
89  #endif
90  
91 -       /*
92 -        * If XKM_OUTPUT_DIR specifies a path without a leading slash, it is
93 -        * relative to the top-level XKB configuration directory.
94 -        * Making the server write to a subdirectory of that directory
95 -        * requires some work in the general case (install procedure
96 -        * has to create links to /var or somesuch on many machines),
97 -        * so we just compile into /usr/tmp for now.
98 -        */
99 -#ifndef XKM_OUTPUT_DIR
100 -#define        XKM_OUTPUT_DIR  "compiled/"
101 -#endif
102 -
103  #define        PRE_ERROR_MSG "\"The XKEYBOARD keymap compiler (xkbcomp) reports:\""
104  #define        ERROR_PREFIX    "\"> \""
105  #define        POST_ERROR_MSG1 "\"Errors from xkbcomp are not fatal to the X server\""
106 @@ -179,6 +174,45 @@ OutputDirectory(
107  }
108  
109  static Bool            
110 +Sha1Asc(char sha1Asc[SHA_DIGEST_LENGTH*2+1], const char * input)
111 +{
112 +    int i;
113 +    unsigned char sha1[SHA_DIGEST_LENGTH];
114 +
115 +#ifdef HAVE_SHA1_IN_LIBMD /* Use libmd for SHA1 */
116 +    SHA1_CTX ctx;
117 +
118 +    SHA1Init (&ctx);
119 +    SHA1Update (&ctx, input, strlen(input));
120 +    SHA1Final (sha1, &ctx);
121 +#else /* Use OpenSSL's libcrypto */
122 +    SHA_CTX ctx;
123 +    int success;
124 +
125 +    success = SHA1_Init (&ctx);
126 +    if (! success)
127 +       return BadAlloc;
128 +
129 +    success = SHA1_Update (&ctx, input, strlen(input));
130 +    if (! success)
131 +       return BadAlloc;
132 +
133 +    success = SHA1_Final (sha1, &ctx);
134 +    if (! success)
135 +       return BadAlloc;
136 +#endif
137 +
138 +    /* convert sha1 to sha1_asc */
139 +    for(i=0; i<SHA_DIGEST_LENGTH; ++i) {
140 +        sprintf(sha1Asc+i*2, "%02X", sha1[i]);
141 +    }
142 +
143 +    return Success;
144 +}
145 +
146 +/* call xkbcomp and compile XKB keymap, return xkm file name in
147 +   nameRtrn */
148 +static Bool
149  XkbDDXCompileKeymapByNames(    XkbDescPtr              xkb,
150                                 XkbComponentNamesPtr    names,
151                                 unsigned                want,
152 @@ -187,7 +221,11 @@ XkbDDXCompileKeymapByNames(        XkbDescPtr              xkb,
153                                 int                     nameRtrnLen)
154  {
155      FILE *     out;
156 -    char       *buf = NULL, keymap[PATH_MAX], xkm_output_dir[PATH_MAX];
157 +    char *     buf = NULL, xkmfile[PATH_MAX], xkm_output_dir[PATH_MAX];
158 +    char *     tmpXkmFile = NULL;
159 +    char *     canonicalXkmFileName = NULL;
160 +    char       sha1Asc[SHA_DIGEST_LENGTH*2+1], xkbKeyMapBuf[100*1024];
161 +    int        ret, result;
162  
163      const char *emptystring = "";
164      const char *xkbbasedirflag = emptystring;
165 @@ -198,16 +236,70 @@ XkbDDXCompileKeymapByNames(       XkbDescPtr              xkb,
166      /* WIN32 has no popen. The input must be stored in a file which is
167         used as input for xkbcomp. xkbcomp does not read from stdin. */
168      char tmpname[PATH_MAX];
169 -    const char *xkmfile = tmpname;
170 +    const char *xkbfile = tmpname;
171  #else
172 -    const char *xkmfile = "-";
173 +    const char *xkbfile = "-";
174  #endif
175  
176 -    snprintf(keymap, sizeof(keymap), "server-%s", display);
177 +    /* Write keymap source (xkbfile) to memory buffer `xkbKeyMapBuf',
178 +       of which SHA1 is generated and used as result xkm file name  */
179 +    memset(xkbKeyMapBuf, 0, sizeof(xkbKeyMapBuf));
180 +    out = fmemopen(xkbKeyMapBuf, sizeof(xkbKeyMapBuf), "w");
181 +    if (NULL == out) {
182 +        ErrorF("[xkb] Open xkbKeyMapBuf for writing failed\n");
183 +        return False;
184 +    }
185 +    ret = XkbWriteXKBKeymapForNames(out, names, xkb, want, need);
186 +    if (fclose(out) !=0)
187 +    {
188 +        ErrorF("[xkb] XkbWriteXKBKeymapForNames error, perhaps xkbKeyMapBuf is too small\n");
189 +        return False;
190 +    }
191 +#ifdef DEBUG
192 +    if (xkbDebugFlags) {
193 +       ErrorF("[xkb] XkbDDXCompileKeymapByNames compiling keymap:\n");
194 +       fputs(xkbKeyMapBuf, stderr);
195 +    }
196 +#endif
197 +    if (!ret) {
198 +        ErrorF("[xkb] Generating XKB Keymap failed, giving up compiling keymap\n");
199 +        return False;
200 +    }
201 +
202 +    DebugF("[xkb] computing SHA1 of keymap\n");
203 +    if (Success == Sha1Asc(sha1Asc, xkbKeyMapBuf)) {
204 +        snprintf(xkmfile, sizeof(xkmfile), "server-%s", sha1Asc);
205 +    }
206 +    else {
207 +        ErrorF("[xkb] Computing SHA1 of keymap failed, "
208 +               "using display name instead as xkm file name\n");
209 +        snprintf(xkmfile, sizeof(xkmfile), "server-%s", display);
210 +    }
211  
212 -    XkbEnsureSafeMapName(keymap);
213 +    XkbEnsureSafeMapName(xkmfile);
214      OutputDirectory(xkm_output_dir, sizeof(xkm_output_dir));
215  
216 +    /* set nameRtrn, fail if it's too small */
217 +    if ((strlen(xkmfile)+1 > nameRtrnLen) && nameRtrn) {
218 +        ErrorF("[xkb] nameRtrn too small to hold xkmfile name\n");
219 +        return False;
220 +    }
221 +    strncpy(nameRtrn, xkmfile, nameRtrnLen);
222 +
223 +    /* if the xkm file already exists, reuse it */
224 +    canonicalXkmFileName = Xprintf("%s%s.xkm", xkm_output_dir, xkmfile);
225 +    if (access(canonicalXkmFileName, R_OK) == 0) {
226 +        /* yes, we can reuse the old xkm file */
227 +        LogMessage(X_INFO, "XKB: reuse xkmfile %s\n", canonicalXkmFileName);
228 +        result = True;
229 +        goto _ret;
230 +    }
231 +    LogMessage(X_INFO, "XKB: generating xkmfile %s\n", canonicalXkmFileName);
232 +
233 +    /* continue to call xkbcomp to compile the keymap. to avoid race
234 +       condition, we compile it to a tmpfile then rename it to
235 +       xkmfile */
236 +
237  #ifdef WIN32
238      strcpy(tmpname, Win32TempDir());
239      strcat(tmpname, "\\xkb_XXXXXX");
240 @@ -230,19 +322,30 @@ XkbDDXCompileKeymapByNames(       XkbDescPtr              xkb,
241         }
242      }
243  
244 +    if ( (tmpXkmFile = tempnam(xkm_output_dir, NULL)) == NULL ) {
245 +        ErrorF("[xkb] Can't generate temp xkm file name");
246 +        result = False;
247 +        goto _ret;
248 +    }
249 +
250      buf = Xprintf("\"%s%sxkbcomp\" -w %d %s -xkm \"%s\" "
251 -                 "-em1 %s -emp %s -eml %s \"%s%s.xkm\"",
252 +                 "-em1 %s -emp %s -eml %s \"%s\"",
253                   xkbbindir, xkbbindirsep,
254                   ( (xkbDebugFlags < 2) ? 1 :
255                     ((xkbDebugFlags > 10) ? 10 : (int)xkbDebugFlags) ),
256 -                 xkbbasedirflag, xkmfile,
257 +                 xkbbasedirflag, xkbfile,
258                   PRE_ERROR_MSG, ERROR_PREFIX, POST_ERROR_MSG1,
259 -                 xkm_output_dir, keymap);
260 +                 tmpXkmFile);
261  
262      if (xkbbasedirflag != emptystring) {
263         xfree(xkbbasedirflag);
264      }
265      
266 +    /* there's a potential race condition between calling tempnam()
267 +       and invoking xkbcomp to write the result file (potential temp
268 +       file name conflicts), but since xkbcomp is a standalone
269 +       program, we have to live with this */
270 +
271  #ifndef WIN32
272      out= Popen(buf,"w");
273  #else
274 @@ -250,31 +353,43 @@ XkbDDXCompileKeymapByNames(       XkbDescPtr              xkb,
275  #endif
276      
277      if (out!=NULL) {
278 -#ifdef DEBUG
279 -    if (xkbDebugFlags) {
280 -       ErrorF("[xkb] XkbDDXCompileKeymapByNames compiling keymap:\n");
281 -       XkbWriteXKBKeymapForNames(stderr,names,xkb,want,need);
282 +        /* write XKBKeyMapBuf to xkbcomp */
283 +        if (EOF==fputs(xkbKeyMapBuf, out))
284 +        {
285 +            ErrorF("[xkb] Sending keymap to xkbcomp failed\n");
286 +            result = False;
287 +            goto _ret;
288      }
289 -#endif
290 -       XkbWriteXKBKeymapForNames(out,names,xkb,want,need);
291  #ifndef WIN32
292         if (Pclose(out)==0)
293  #else
294         if (fclose(out)==0 && System(buf) >= 0)
295  #endif
296         {
297 +        /* xkbcomp success */
298              if (xkbDebugFlags)
299                  DebugF("[xkb] xkb executes: %s\n",buf);
300 -           if (nameRtrn) {
301 -               strncpy(nameRtrn,keymap,nameRtrnLen);
302 -               nameRtrn[nameRtrnLen-1]= '\0';
303 +
304 +            /* if canonicalXkmFileName already exists now, we simply
305 +               overwrite it, this is OK */
306 +            ret = rename(tmpXkmFile, canonicalXkmFileName);
307 +            if (0 != ret) {
308 +                ErrorF("[xkb] Can't rename %s to %s, error: %s\n",
309 +                       tmpXkmFile, canonicalXkmFileName,
310 +                       strerror(errno));
311 +
312 +                /* in case of error, don't unlink tmpXkmFile, leave it
313 +                   for debugging */
314 +
315 +                result = False;
316 +                goto _ret;
317             }
318 -            if (buf != NULL)
319 -                xfree (buf);
320 -           return True;
321 +
322 +            result = True;
323 +            goto _ret;
324         }
325         else
326 -           LogMessage(X_ERROR, "Error compiling keymap (%s)\n", keymap);
327 +           LogMessage(X_ERROR, "Error compiling keymap (%s)\n", xkbfile);
328  #ifdef WIN32
329          /* remove the temporary file */
330          unlink(tmpname);
331 @@ -289,9 +404,17 @@ XkbDDXCompileKeymapByNames(        XkbDescPtr              xkb,
332      }
333      if (nameRtrn)
334         nameRtrn[0]= '\0';
335 +    result = False;
336 +
337 +_ret:
338 +    if (tmpXkmFile)
339 +        free(tmpXkmFile);
340 +    if (canonicalXkmFileName)
341 +        xfree(canonicalXkmFileName);
342      if (buf != NULL)
343          xfree (buf);
344 -    return False;
345 +
346 +    return result;
347  }
348  
349  static FILE *
350 @@ -375,7 +498,6 @@ unsigned    missing;
351         DebugF("Loaded XKB keymap %s, defined=0x%x\n",fileName,(*xkbRtrn)->defined);
352      }
353      fclose(file);
354 -    (void) unlink (fileName);
355      return (need|want)&(~missing);
356  }
357  
358 -- 
359 1.5.6.5
360