Debian Codegraph @

src/context.c

raw
1 /*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Ran Benita
4 * SPDX-License-Identifier: MIT
5 *
6 * Author: Daniel Stone <[email protected]>
7 */
8
9 #include "config.h"
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #if HAVE_XKB_EXTENSIONS_DIRECTORIES
18 #include <limits.h>
19 #if HAVE_UNISTD_H && HAVE_DIRENT_H
20 #include <dirent.h>
21 #include <unistd.h>
22 #endif
23 #endif
24
25 #include "xkbcommon/xkbcommon.h"
26 #include "context.h"
27 #include "darray.h"
28 #include "messages-codes.h"
29 #include "utils.h"
30
31
32 /**
33 * Append one directory to the context’s include path.
34 */
35 static int
36 context_include_path_append(struct xkb_context *ctx, const char *path)
37 {
38 int err = ENOMEM;
39
40 char *tmp = strdup(path);
41 if (!tmp)
42 goto err;
43
44 struct stat stat_buf;
45 err = stat(path, &stat_buf);
46 if (err != 0) {
47 err = errno;
48 goto err;
49 }
50 if (!S_ISDIR(stat_buf.st_mode)) {
51 err = ENOTDIR;
52 goto err;
53 }
54
55 if (!check_eaccess(path, R_OK | X_OK)) {
56 err = EACCES;
57 goto err;
58 }
59
60 darray_append(ctx->includes, tmp);
61 /* Use “info” log level to facilitate bug reporting. */
62 log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "Include path added: %s\n", tmp);
63
64 return 1;
65
66 err:
67 if (tmp)
68 darray_append(ctx->failed_includes, tmp);
69 /*
70 * This error is not fatal because some valid paths may still be defined.
71 * Use “info” log level to facilitate bug reporting.
72 */
73 log_info(ctx, XKB_LOG_MESSAGE_NO_ID,
74 "Include path failed: \"%s\" (%s)\n", path, strerror(err));
75 return 0;
76 }
77
78 /**
79 * Append one directory to the context’s include path.
80 */
81 int
82 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
83 {
84 return (xkb_context_init_includes(ctx))
85 ? context_include_path_append(ctx, path)
86 : 0;
87 }
88
89 const char *
90 xkb_context_include_path_get_extra_path(struct xkb_context *ctx)
91 {
92 const char * const extra = xkb_context_getenv(ctx, "XKB_CONFIG_EXTRA_PATH");
93 /*
94 * Only use default if path is undefined, but accept empty string, which may
95 * be unintentional and should be reported.
96 */
97 return extra ? extra : DFLT_XKB_CONFIG_EXTRA_PATH;
98 }
99
100 #ifdef HAVE_XKB_EXTENSIONS_DIRECTORIES
101 const char *
102 xkb_context_include_path_get_unversioned_extensions_path(struct xkb_context *ctx)
103 {
104 const char *ext =
105 xkb_context_getenv(ctx, "XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH");
106 return ext
107 ? ext
108 #ifdef DFLT_XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH
109 : DFLT_XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH;
110 #else
111 : NULL;
112 #endif
113 }
114
115 const char *
116 xkb_context_include_path_get_versioned_extensions_path(struct xkb_context *ctx)
117 {
118 const char *ext =
119 xkb_context_getenv(ctx, "XKB_CONFIG_VERSIONED_EXTENSIONS_PATH");
120 return ext
121 ? ext
122 #ifdef DFLT_XKB_CONFIG_VERSIONED_EXTENSIONS_PATH
123 : DFLT_XKB_CONFIG_VERSIONED_EXTENSIONS_PATH;
124 #else
125 : NULL;
126 #endif
127 }
128
129 static int
130 compare_str(const void *a, const void *b)
131 {
132 return strcmp(*(char **)a, *(char **) b);
133 }
134
135 static int
136 add_direct_subdirectories(struct xkb_context *ctx, const char *path,
137 darray_string *extensions,
138 darray_size_t versioned_count,
139 size_t versioned_path_length)
140 {
141 int ret = 0;
142 int err = ENOMEM;
143 DIR *dir = NULL;
144
145 /* Check extensions parent directory */
146 struct stat stat_buf;
147 err = stat(path, &stat_buf);
148 if (err != 0) {
149 err = errno;
150 goto err;
151 }
152 if (!S_ISDIR(stat_buf.st_mode)) {
153 err = ENOTDIR;
154 goto err;
155 }
156 if (!check_eaccess(path, R_OK | X_OK)) {
157 err = EACCES;
158 goto err;
159 }
160
161 dir = opendir(path);
162 if (dir == NULL) {
163 err = EACCES;
164 goto err;
165 }
166
167 struct dirent *entry;
168 char path_buf[PATH_MAX] = "";
169 versioned_path_length++; /* Additional final ‘/’ */
170 while ((entry = readdir(dir)) != NULL) {
171 const char *name = entry->d_name;
172
173 /* Skip special entries */
174 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
175 continue;
176
177 if (!snprintf_safe(path_buf, sizeof(path_buf), "%s/%s", path, name)) {
178 err = ENOMEM;
179 goto err;
180 }
181 if (stat(path_buf, &stat_buf) != 0 || !S_ISDIR(stat_buf.st_mode))
182 continue;
183
184 /* Skip if the corresponding versioned directory is already included */
185 for (darray_size_t i = 0; i < versioned_count; i++) {
186 const char * const prev_name = darray_item(*extensions, i)
187 + versioned_path_length;
188 if (strcmp(name, prev_name) == 0)
189 goto next;
190 }
191
192 char *ext_path = strdup_safe(path_buf);
193 if (!ext_path) {
194 err = ENOMEM;
195 goto err;
196 }
197
198 darray_append(*extensions, ext_path);
199 next:
200 {} /* Label at end of compound statement is a C23 extension */
201 }
202
203 closedir(dir);
204
205 if (darray_size(*extensions) > versioned_count) {
206 /* Sort the entries, so that they are appended in lexicographic order */
207 qsort(darray_items(*extensions) + versioned_count,
208 darray_size(*extensions) - versioned_count,
209 sizeof(*darray_items(*extensions)), &compare_str);
210 char **ext_path;
211 darray_foreach_from(ext_path, *extensions, versioned_count) {
212 ret |= context_include_path_append(ctx, *ext_path);
213 }
214 }
215
216 return ret;
217
218 err:
219 log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID,
220 "Include extensions path failed: %s (%s)\n", path, strerror(err));
221 if (dir)
222 closedir(dir);
223
224 return ret;
225 }
226 #endif
227
228 const char *
229 xkb_context_include_path_get_system_path(struct xkb_context *ctx)
230 {
231 const char * const root = xkb_context_getenv(ctx, "XKB_CONFIG_ROOT");
232 /*
233 * Only use default if path is undefined, but accept empty string, which may
234 * be unintentional and should be reported.
235 */
236 return root ? root : DFLT_XKB_CONFIG_ROOT;
237 }
238
239 /**
240 * Append the default include directories to the context.
241 */
242 int
243 xkb_context_include_path_append_default(struct xkb_context *ctx)
244 {
245 /*
246 * We do not call `xkb_context_init_includes()` here, because either
247 * we already initialized the includes paths or we are doing it now.
248 */
249
250 char *user_path;
251 int ret = 0;
252
253 const char * const home = xkb_context_getenv(ctx, "HOME");
254 const char * const xdg = xkb_context_getenv(ctx, "XDG_CONFIG_HOME");
255 /* Accept empty string, which may be unintentional and should be reported */
256 if (xdg != NULL) {
257 user_path = asprintf_safe("%s/xkb", xdg);
258 if (user_path) {
259 ret |= context_include_path_append(ctx, user_path);
260 free(user_path);
261 }
262 } else if (home != NULL) {
263 /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
264 user_path = asprintf_safe("%s/.config/xkb", home);
265 if (user_path) {
266 ret |= context_include_path_append(ctx, user_path);
267 free(user_path);
268 }
269 }
270
271 if (home != NULL) {
272 user_path = asprintf_safe("%s/.xkb", home);
273 if (user_path) {
274 ret |= context_include_path_append(ctx, user_path);
275 free(user_path);
276 }
277 }
278
279 const char * const extra = xkb_context_include_path_get_extra_path(ctx);
280 ret |= context_include_path_append(ctx, extra);
281
282 #ifdef HAVE_XKB_EXTENSIONS_DIRECTORIES
283 darray_string extensions = darray_new();
284
285 /* Versioned extensions directory */
286 const char *extensions_path =
287 xkb_context_include_path_get_versioned_extensions_path(ctx);
288 size_t versioned_path_length = 0;
289 if (extensions_path) {
290 /* Add direct subdirectories */
291 ret |= add_direct_subdirectories(ctx, extensions_path,
292 &extensions, 0, 0);
293 versioned_path_length = strlen(extensions_path);
294 }
295
296 /* Unversioned extensions directory */
297 extensions_path =
298 xkb_context_include_path_get_unversioned_extensions_path(ctx);
299 if (extensions_path) {
300 /*
301 * Add direct subdirectories, except those already added from the
302 * *versioned* extensions path hereinabove.
303 */
304 ret |= add_direct_subdirectories(ctx, extensions_path, &extensions,
305 darray_size(extensions),
306 versioned_path_length);
307 }
308
309 char **ext_path;
310 darray_foreach(ext_path, extensions) {
311 free(*ext_path);
312 }
313 darray_free(extensions);
314 #endif
315
316 /* Canonical XKB root */
317 const char * const root = xkb_context_include_path_get_system_path(ctx);
318 const bool has_root = context_include_path_append(ctx, root);
319 ret |= has_root;
320
321 /*
322 * Fallback for misconfigured setups.
323 * Some setups use the assumption that the canonical XKB root is always the
324 * legacy X11 one, but this is no longer true since xkeyboard-config 2.45,
325 * where the X11 path is now a mere symlink to a dedicated xkeyboard-config
326 * data directory.
327 * This fallback can still be skipped if deliberately using an empty string
328 * for the canonical XKB root hereinabove.
329 */
330 if (!has_root && root[0] != '\0') {
331 log_warn(ctx, XKB_LOG_MESSAGE_NO_ID,
332 "Root include path failed; fallback to \"%s\". "
333 "The setup is probably misconfigured. "
334 "Please ensure that \"%s\" is available in the environment.\n",
335 DFLT_XKB_LEGACY_ROOT, root);
336 ret |= context_include_path_append(ctx, DFLT_XKB_LEGACY_ROOT);
337 }
338
339 return ret;
340 }
341
342 /**
343 * Remove all entries in the context's include path.
344 */
345 void
346 xkb_context_include_path_clear(struct xkb_context *ctx)
347 {
348 char **path;
349
350 darray_foreach(path, ctx->includes)
351 free(*path);
352 darray_free(ctx->includes);
353
354 darray_foreach(path, ctx->failed_includes)
355 free(*path);
356 darray_free(ctx->failed_includes);
357
358 /* It does not make sense to keep the pending defaults */
359 ctx->pending_default_includes = false;
360 }
361
362 /**
363 * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
364 */
365 int
366 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
367 {
368 xkb_context_include_path_clear(ctx);
369 return xkb_context_include_path_append_default(ctx);
370 }
371
372 /**
373 * Returns the number of entries in the context's include path.
374 */
375 unsigned int
376 xkb_context_num_include_paths(struct xkb_context *ctx)
377 {
378 return (xkb_context_init_includes(ctx))
379 ? darray_size(ctx->includes)
380 : 0;
381 }
382
383 /**
384 * Returns the given entry in the context's include path, or NULL if an
385 * invalid index is passed.
386 */
387 const char *
388 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
389 {
390 if (idx >= xkb_context_num_include_paths(ctx))
391 return NULL;
392
393 return darray_item(ctx->includes, idx);
394 }
395
396 /**
397 * Take a new reference on the context.
398 */
399 struct xkb_context *
400 xkb_context_ref(struct xkb_context *ctx)
401 {
402 assert(ctx->refcnt > 0);
403 ctx->refcnt++;
404 return ctx;
405 }
406
407 /**
408 * Drop an existing reference on the context, and free it if the refcnt is
409 * now 0.
410 */
411 void
412 xkb_context_unref(struct xkb_context *ctx)
413 {
414 assert(!ctx || ctx->refcnt > 0);
415 if (!ctx || --ctx->refcnt > 0)
416 return;
417
418 free(ctx->x11_atom_cache);
419 xkb_context_include_path_clear(ctx);
420 atom_table_free(ctx->atom_table);
421 free(ctx);
422 }
423
424 static const char *
425 log_level_to_prefix(enum xkb_log_level level)
426 {
427 switch (level) {
428 case XKB_LOG_LEVEL_DEBUG:
429 return "xkbcommon: DEBUG: ";
430 case XKB_LOG_LEVEL_INFO:
431 return "xkbcommon: INFO: ";
432 case XKB_LOG_LEVEL_WARNING:
433 return "xkbcommon: WARNING: ";
434 case XKB_LOG_LEVEL_ERROR:
435 return "xkbcommon: ERROR: ";
436 case XKB_LOG_LEVEL_CRITICAL:
437 return "xkbcommon: CRITICAL: ";
438 default:
439 return NULL;
440 }
441 }
442
443 ATTR_PRINTF(3, 0) static void
444 default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
445 const char *fmt, va_list args)
446 {
447 const char *prefix = log_level_to_prefix(level);
448
449 if (prefix)
450 fprintf(stderr, "%s", prefix);
451 vfprintf(stderr, fmt, args);
452 }
453
454 static enum xkb_log_level
455 log_level(const char *level) {
456 char *endptr;
457 enum xkb_log_level lvl;
458
459 errno = 0;
460 lvl = strtol(level, &endptr, 10);
461 if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
462 return lvl;
463 if (istreq_prefix("crit", level))
464 return XKB_LOG_LEVEL_CRITICAL;
465 if (istreq_prefix("err", level))
466 return XKB_LOG_LEVEL_ERROR;
467 if (istreq_prefix("warn", level))
468 return XKB_LOG_LEVEL_WARNING;
469 if (istreq_prefix("info", level))
470 return XKB_LOG_LEVEL_INFO;
471 if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
472 return XKB_LOG_LEVEL_DEBUG;
473
474 return XKB_LOG_LEVEL_ERROR;
475 }
476
477 static int
478 log_verbosity(const char *verbosity) {
479 errno = 0;
480 const long int v = strtol(verbosity, NULL, 10);
481 if (errno == 0) {
482 /* Truncate */
483 return (int) v;
484 }
485
486 return XKB_LOG_VERBOSITY_DEFAULT;
487 }
488
489 /**
490 * Create a new context.
491 */
492 struct xkb_context *
493 xkb_context_new(enum xkb_context_flags flags)
494 {
495 const char *env;
496 struct xkb_context *ctx = calloc(1, sizeof(*ctx));
497
498 if (!ctx)
499 return NULL;
500
501 ctx->refcnt = 1;
502 ctx->log_fn = default_log_fn;
503 ctx->log_level = XKB_LOG_LEVEL_ERROR;
504 ctx->log_verbosity = XKB_LOG_VERBOSITY_DEFAULT;
505 ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
506 ctx->use_secure_getenv = !(flags & XKB_CONTEXT_NO_SECURE_GETENV);
507
508 /*
509 * Default includes paths are delayed and added only if necessary.
510 *
511 * It is more efficient for most clients, which only get the keymap from the
512 * server: it avoids unnecessary allocations and file system queries.
513 *
514 * It also avoid the corner case where a containerized app lacks access to
515 * the XKB directories.
516 *
517 * There might be an issue in case the environment variables relevant to the
518 * include paths change between the context initialization and the call to
519 * `xkb_context_include_path_append_default()`. However this seems very
520 * unlikely and would have already triggered issues when the `%`-expansion
521 * is used.
522 */
523 ctx->pending_default_includes = !(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES);
524 darray_init(ctx->includes);
525 darray_init(ctx->failed_includes);
526
527 /* Environment overwrites defaults. */
528 env = xkb_context_getenv(ctx, "XKB_LOG_LEVEL");
529 if (env)
530 xkb_context_set_log_level(ctx, log_level(env));
531
532 env = xkb_context_getenv(ctx, "XKB_LOG_VERBOSITY");
533 if (env)
534 xkb_context_set_log_verbosity(ctx, log_verbosity(env));
535
536 ctx->atom_table = atom_table_new();
537 if (!ctx->atom_table) {
538 xkb_context_unref(ctx);
539 return NULL;
540 }
541
542 ctx->x11_atom_cache = NULL;
543
544 return ctx;
545 }
546
547 void
548 xkb_context_set_log_fn(struct xkb_context *ctx,
549 void (*log_fn)(struct xkb_context *ctx,
550 enum xkb_log_level level,
551 const char *fmt, va_list args))
552 {
553 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
554 }
555
556 enum xkb_log_level
557 xkb_context_get_log_level(struct xkb_context *ctx)
558 {
559 return ctx->log_level;
560 }
561
562 void
563 xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
564 {
565 ctx->log_level = level;
566 }
567
568 int
569 xkb_context_get_log_verbosity(struct xkb_context *ctx)
570 {
571 return ctx->log_verbosity;
572 }
573
574 void
575 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
576 {
577 ctx->log_verbosity = verbosity;
578 }
579
580 void *
581 xkb_context_get_user_data(struct xkb_context *ctx)
582 {
583 if (ctx)
584 return ctx->user_data;
585 return NULL;
586 }
587
588 void
589 xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
590 {
591 ctx->user_data = user_data;
592 }