| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "env-util.h" |
| 5 |
#include "errno-util.h" |
| 6 |
#include "kbd-util.h" |
| 7 |
#include "log.h" |
| 8 |
#include "recurse-dir.h" |
| 9 |
#include "set.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
|
| 13 |
#define KBD_KEYMAP_DIRS \ |
| 14 |
"/usr/share/keymaps/", \ |
| 15 |
"/usr/share/kbd/keymaps/", \ |
| 16 |
"/usr/lib/kbd/keymaps/" |
| 17 |
|
| 18 |
int keymap_directories(char ***ret) { |
| 19 |
assert(ret); |
| 20 |
|
| 21 |
if (getenv_path_list("SYSTEMD_KEYMAP_DIRECTORIES", ret) >= 0) |
| 22 |
return 0; |
| 23 |
|
| 24 |
char **paths = strv_new(KBD_KEYMAP_DIRS); |
| 25 |
if (!paths) |
| 26 |
return log_oom_debug(); |
| 27 |
|
| 28 |
*ret = TAKE_PTR(paths); |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
struct recurse_dir_userdata { |
| 33 |
const char *keymap_name; |
| 34 |
Set *keymaps; |
| 35 |
}; |
| 36 |
|
| 37 |
static int keymap_recurse_dir_callback( |
| 38 |
RecurseDirEvent event, |
| 39 |
const char *path, |
| 40 |
int dir_fd, |
| 41 |
int inode_fd, |
| 42 |
const struct dirent *de, |
| 43 |
const struct statx *sx, |
| 44 |
void *userdata) { |
| 45 |
|
| 46 |
struct recurse_dir_userdata *data = userdata; |
| 47 |
_cleanup_free_ char *p = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
assert(de); |
| 51 |
|
| 52 |
/* If 'keymap_name' is non-NULL, return true if keymap 'keymap_name' is found. Otherwise, add all |
| 53 |
* keymaps to 'keymaps'. */ |
| 54 |
|
| 55 |
if (event != RECURSE_DIR_ENTRY) |
| 56 |
return RECURSE_DIR_CONTINUE; |
| 57 |
|
| 58 |
if (!IN_SET(de->d_type, DT_REG, DT_LNK)) |
| 59 |
return RECURSE_DIR_CONTINUE; |
| 60 |
|
| 61 |
const char *e = endswith(de->d_name, ".map") ?: endswith(de->d_name, ".map.gz"); |
| 62 |
if (!e) |
| 63 |
return RECURSE_DIR_CONTINUE; |
| 64 |
|
| 65 |
p = strndup(de->d_name, e - de->d_name); |
| 66 |
if (!p) |
| 67 |
return -ENOMEM; |
| 68 |
|
| 69 |
if (data->keymap_name) |
| 70 |
return streq(p, data->keymap_name) ? 1 : RECURSE_DIR_CONTINUE; |
| 71 |
|
| 72 |
assert(data->keymaps); |
| 73 |
|
| 74 |
if (!keymap_is_valid(p)) |
| 75 |
return 0; |
| 76 |
|
| 77 |
r = set_consume(data->keymaps, TAKE_PTR(p)); |
| 78 |
if (r < 0) |
| 79 |
return r; |
| 80 |
|
| 81 |
return RECURSE_DIR_CONTINUE; |
| 82 |
} |
| 83 |
|
| 84 |
int get_keymaps(char ***ret) { |
| 85 |
_cleanup_set_free_ Set *keymaps = NULL; |
| 86 |
_cleanup_strv_free_ char **keymap_dirs = NULL; |
| 87 |
int r; |
| 88 |
|
| 89 |
r = keymap_directories(&keymap_dirs); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
keymaps = set_new(&string_hash_ops_free); |
| 94 |
if (!keymaps) |
| 95 |
return -ENOMEM; |
| 96 |
|
| 97 |
STRV_FOREACH(dir, keymap_dirs) { |
| 98 |
r = recurse_dir_at( |
| 99 |
AT_FDCWD, |
| 100 |
*dir, |
| 101 |
/* statx_mask= */ 0, |
| 102 |
/* n_depth_max= */ UINT_MAX, |
| 103 |
RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, |
| 104 |
keymap_recurse_dir_callback, |
| 105 |
&(struct recurse_dir_userdata) { |
| 106 |
.keymaps = keymaps, |
| 107 |
}); |
| 108 |
if (r == -ENOENT) |
| 109 |
continue; |
| 110 |
if (ERRNO_IS_NEG_RESOURCE(r)) |
| 111 |
return log_warning_errno(r, "Failed to read keymap list from %s: %m", *dir); |
| 112 |
if (r < 0) |
| 113 |
log_debug_errno(r, "Failed to read keymap list from %s, ignoring: %m", *dir); |
| 114 |
} |
| 115 |
|
| 116 |
_cleanup_strv_free_ char **l = set_to_strv(&keymaps); |
| 117 |
if (!l) |
| 118 |
return -ENOMEM; |
| 119 |
|
| 120 |
if (strv_isempty(l)) |
| 121 |
return -ENOENT; |
| 122 |
|
| 123 |
strv_sort(l); |
| 124 |
|
| 125 |
*ret = TAKE_PTR(l); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
bool keymap_is_valid(const char *name) { |
| 130 |
if (!string_is_safe(name, STRING_FILENAME)) |
| 131 |
return false; |
| 132 |
|
| 133 |
if (strlen(name) >= 128) |
| 134 |
return false; |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
int keymap_exists(const char *name) { |
| 140 |
_cleanup_strv_free_ char **keymap_dirs = NULL; |
| 141 |
int r; |
| 142 |
|
| 143 |
if (!keymap_is_valid(name)) |
| 144 |
return -EINVAL; |
| 145 |
|
| 146 |
r = keymap_directories(&keymap_dirs); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
STRV_FOREACH(dir, keymap_dirs) { |
| 151 |
r = recurse_dir_at( |
| 152 |
AT_FDCWD, |
| 153 |
*dir, |
| 154 |
/* statx_mask= */ 0, |
| 155 |
/* n_depth_max= */ UINT_MAX, |
| 156 |
RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, |
| 157 |
keymap_recurse_dir_callback, |
| 158 |
&(struct recurse_dir_userdata) { |
| 159 |
.keymap_name = name, |
| 160 |
}); |
| 161 |
if (r > 0) |
| 162 |
return true; |
| 163 |
if (ERRNO_IS_NEG_RESOURCE(r)) |
| 164 |
return r; |
| 165 |
if (r < 0 && r != -ENOENT) |
| 166 |
log_debug_errno(r, "Failed to read keymap list from %s, ignoring: %m", *dir); |
| 167 |
} |
| 168 |
|
| 169 |
return false; |
| 170 |
} |