1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

vconsole-setup: use normal variables instead of an array

We had this elaborate scheme with an array of strings instead of a bunch of a
normal string fields. If there were hundreds of those strings, this would make
sense. But we had just five and one was actually a bit different because it had
a fallback, so overall, the code is easier to read when normal fields are used.

The fallback was implemented in the accessor function, now it's actually
implemented in the place where it's used.

While at it, rename the variables so that they match the config keys for
legibility.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2024-11-13 13:10:57 +01:00 committed by Luca Boccassi
parent 390bab5392
commit a814fd7897

View File

@ -39,50 +39,32 @@
#include "terminal-util.h" #include "terminal-util.h"
#include "virt.h" #include "virt.h"
typedef enum VCMeta {
VC_KEYMAP,
VC_KEYMAP_TOGGLE,
VC_FONT,
VC_FONT_MAP,
VC_FONT_UNIMAP,
_VC_META_MAX,
_VC_META_INVALID = -EINVAL,
} VCMeta;
typedef struct Context { typedef struct Context {
char *config[_VC_META_MAX]; char *keymap;
char *keymap_toggle;
char *font;
char *font_map;
char *font_unimap;
} Context; } Context;
static const char * const vc_meta_names[_VC_META_MAX] = {
[VC_KEYMAP] = "vconsole.keymap",
[VC_KEYMAP_TOGGLE] = "vconsole.keymap_toggle",
[VC_FONT] = "vconsole.font",
[VC_FONT_MAP] = "vconsole.font_map",
[VC_FONT_UNIMAP] = "vconsole.font_unimap",
};
/* compatibility with obsolete multiple-dot scheme */
static const char * const vc_meta_compat_names[_VC_META_MAX] = {
[VC_KEYMAP_TOGGLE] = "vconsole.keymap.toggle",
[VC_FONT_MAP] = "vconsole.font.map",
[VC_FONT_UNIMAP] = "vconsole.font.unimap",
};
static const char * const vc_env_names[_VC_META_MAX] = {
[VC_KEYMAP] = "KEYMAP",
[VC_KEYMAP_TOGGLE] = "KEYMAP_TOGGLE",
[VC_FONT] = "FONT",
[VC_FONT_MAP] = "FONT_MAP",
[VC_FONT_UNIMAP] = "FONT_UNIMAP",
};
static void context_done(Context *c) { static void context_done(Context *c) {
assert(c); assert(c);
FOREACH_ARRAY(cc, c->config, _VC_META_MAX) free(c->keymap);
free(*cc); free(c->keymap_toggle);
free(c->font);
free(c->font_map);
free(c->font_unimap);
} }
#define context_merge(dst, src, src_compat, name) \
({ \
if (src->name) \
free_and_replace(dst->name, src->name); \
else if (src_compat && src_compat->name) \
free_and_replace(dst->name, src_compat->name); \
})
static void context_merge_config( static void context_merge_config(
Context *dst, Context *dst,
Context *src, Context *src,
@ -91,21 +73,11 @@ static void context_merge_config(
assert(dst); assert(dst);
assert(src); assert(src);
for (VCMeta i = 0; i < _VC_META_MAX; i++) context_merge(dst, src, src_compat, keymap);
if (src->config[i]) context_merge(dst, src, src_compat, keymap_toggle);
free_and_replace(dst->config[i], src->config[i]); context_merge(dst, src, src_compat, font);
else if (src_compat && src_compat->config[i]) context_merge(dst, src, src_compat, font_map);
free_and_replace(dst->config[i], src_compat->config[i]); context_merge(dst, src, src_compat, font_unimap);
}
static const char* context_get_config(Context *c, VCMeta meta) {
assert(c);
assert(meta >= 0 && meta < _VC_META_MAX);
if (meta == VC_KEYMAP)
return isempty(c->config[VC_KEYMAP]) ? SYSTEMD_DEFAULT_KEYMAP : c->config[VC_KEYMAP];
return empty_to_null(c->config[meta]);
} }
static int context_read_creds(Context *c) { static int context_read_creds(Context *c) {
@ -115,11 +87,11 @@ static int context_read_creds(Context *c) {
assert(c); assert(c);
r = read_credential_strings_many( r = read_credential_strings_many(
vc_meta_names[VC_KEYMAP], &v.config[VC_KEYMAP], "vconsole.keymap", &v.keymap,
vc_meta_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], "vconsole.keymap_toggle", &v.keymap_toggle,
vc_meta_names[VC_FONT], &v.config[VC_FONT], "vconsole.font", &v.font,
vc_meta_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], "vconsole.font_map", &v.font_map,
vc_meta_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP]); "vconsole.font_unimap", &v.font_unimap);
if (r < 0) if (r < 0)
log_warning_errno(r, "Failed to import credentials, ignoring: %m"); log_warning_errno(r, "Failed to import credentials, ignoring: %m");
@ -135,11 +107,11 @@ static int context_read_env(Context *c) {
r = parse_env_file( r = parse_env_file(
NULL, "/etc/vconsole.conf", NULL, "/etc/vconsole.conf",
vc_env_names[VC_KEYMAP], &v.config[VC_KEYMAP], "KEYMAP", &v.keymap,
vc_env_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], "KEYMAP_TOGGLE", &v.keymap_toggle,
vc_env_names[VC_FONT], &v.config[VC_FONT], "FONT", &v.font,
vc_env_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], "FONT_MAP", &v.font_map,
vc_env_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP]); "FONT_UNIMAP", &v.font_unimap);
if (r < 0) { if (r < 0) {
if (r != -ENOENT) if (r != -ENOENT)
log_warning_errno(r, "Failed to read /etc/vconsole.conf, ignoring: %m"); log_warning_errno(r, "Failed to read /etc/vconsole.conf, ignoring: %m");
@ -158,14 +130,15 @@ static int context_read_proc_cmdline(Context *c) {
r = proc_cmdline_get_key_many( r = proc_cmdline_get_key_many(
PROC_CMDLINE_STRIP_RD_PREFIX, PROC_CMDLINE_STRIP_RD_PREFIX,
vc_meta_names[VC_KEYMAP], &v.config[VC_KEYMAP], "vconsole.keymap", &v.keymap,
vc_meta_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], "vconsole.keymap_toggle", &v.keymap_toggle,
vc_meta_names[VC_FONT], &v.config[VC_FONT], "vconsole.font", &v.font,
vc_meta_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], "vconsole.font_map", &v.font_map,
vc_meta_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP], "vconsole.font_unimap", &v.font_unimap,
vc_meta_compat_names[VC_KEYMAP_TOGGLE], &w.config[VC_KEYMAP_TOGGLE], /* compatibility with obsolete multiple-dot scheme */
vc_meta_compat_names[VC_FONT_MAP], &w.config[VC_FONT_MAP], "vconsole.keymap.toggle", &w.keymap_toggle,
vc_meta_compat_names[VC_FONT_UNIMAP], &w.config[VC_FONT_UNIMAP]); "vconsole.font.map", &w.font_map,
"vconsole.font.unimap", &w.font_unimap);
if (r < 0) { if (r < 0) {
if (r != -ENOENT) if (r != -ENOENT)
log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m"); log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
@ -286,7 +259,7 @@ static int toggle_utf8_sysfs(bool utf8) {
} }
static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) { static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) {
const char *map, *map_toggle, *args[8]; const char* args[8];
unsigned i = 0; unsigned i = 0;
pid_t pid; pid_t pid;
int r; int r;
@ -294,11 +267,12 @@ static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) {
assert(vc); assert(vc);
assert(c); assert(c);
map = context_get_config(c, VC_KEYMAP); const char
map_toggle = context_get_config(c, VC_KEYMAP_TOGGLE); *keymap = empty_to_null(c->keymap) ?: SYSTEMD_DEFAULT_KEYMAP,
*keymap_toggle = empty_to_null(c->keymap_toggle);
/* An empty map means kernel map */ /* An empty map means kernel map */
if (isempty(map) || streq(map, "@kernel")) if (!keymap || streq(keymap, "@kernel"))
return 0; return 0;
args[i++] = KBD_LOADKEYS; args[i++] = KBD_LOADKEYS;
@ -307,9 +281,9 @@ static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) {
args[i++] = vc; args[i++] = vc;
if (utf8) if (utf8)
args[i++] = "-u"; args[i++] = "-u";
args[i++] = map; args[i++] = keymap;
if (map_toggle) if (keymap_toggle)
args[i++] = map_toggle; args[i++] = keymap_toggle;
args[i++] = NULL; args[i++] = NULL;
if (DEBUG_LOGGING) { if (DEBUG_LOGGING) {
@ -331,7 +305,7 @@ static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) {
} }
static int font_load_and_wait(const char *vc, Context *c) { static int font_load_and_wait(const char *vc, Context *c) {
const char *font, *map, *unimap, *args[9]; const char* args[9];
unsigned i = 0; unsigned i = 0;
pid_t pid; pid_t pid;
int r; int r;
@ -339,24 +313,25 @@ static int font_load_and_wait(const char *vc, Context *c) {
assert(vc); assert(vc);
assert(c); assert(c);
font = context_get_config(c, VC_FONT); const char
map = context_get_config(c, VC_FONT_MAP); *font = empty_to_null(c->font),
unimap = context_get_config(c, VC_FONT_UNIMAP); *font_map = empty_to_null(c->font_map),
*font_unimap = empty_to_null(c->font_unimap);
/* Any part can be set independently */ /* Any part can be set independently */
if (!font && !map && !unimap) if (!font && !font_map && !font_unimap)
return 0; return 0;
args[i++] = KBD_SETFONT; args[i++] = KBD_SETFONT;
args[i++] = "-C"; args[i++] = "-C";
args[i++] = vc; args[i++] = vc;
if (map) { if (font_map) {
args[i++] = "-m"; args[i++] = "-m";
args[i++] = map; args[i++] = font_map;
} }
if (unimap) { if (font_unimap) {
args[i++] = "-u"; args[i++] = "-u";
args[i++] = unimap; args[i++] = font_unimap;
} }
if (font) if (font)
args[i++] = font; args[i++] = font;
@ -377,7 +352,7 @@ static int font_load_and_wait(const char *vc, Context *c) {
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
/* setfont returns EX_OSERR when ioctl(KDFONTOP/PIO_FONTX/PIO_FONTX) fails. This might mean various /* setfont returns EX_OSERR when ioctl(KDFONTOP/PIO_FONTX/PIO_FONTX) fails. This might mean various
* things, but in particular lack of a graphical console. Let's be generous and not treat this as an * things, but in particular lack of a graphical console. Let's be generous and not treat this as an
* error. */ * error. */
r = wait_for_terminate_and_check(KBD_SETFONT, pid, WAIT_LOG_ABNORMAL); r = wait_for_terminate_and_check(KBD_SETFONT, pid, WAIT_LOG_ABNORMAL);