Skip to content

Commit

Permalink
libkfont: Check console mode
Browse files Browse the repository at this point in the history
No font getting or setting operations work unless the console is in
text mode[1]. This results in a hard-to-diagnose “Invalid argument”
error.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/tty/vt/vt.c#n4736

Link: #120
Signed-off-by: Alexey Gladkov <legion@kernel.org>
  • Loading branch information
legionus committed Oct 9, 2024
1 parent 072114b commit 9d60d24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libkfont/kdfontop.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,31 @@
#include "compat/linux-kd.h"
#endif

static int
is_kd_text(struct kfont_context *ctx, int fd)
{
unsigned int kd_mode;

if (ioctl(fd, KDGETMODE, &kd_mode)) {
KFONT_ERR(ctx, "ioctl(KDGETMODE): %m");
return 0;
}

if (kd_mode == KD_TEXT)
return 1;

KFONT_ERR(ctx, _("Console is not in text mode"));
return 0;
}

int
kfont_restore_font(struct kfont_context *ctx, int fd)
{
struct console_font_op cfo;

if (!is_kd_text(ctx, fd))
return -1;

cfo.op = KD_FONT_OP_SET_DEFAULT;

if (ioctl(fd, KDFONTOP, &cfo)) {
Expand Down Expand Up @@ -60,6 +80,9 @@ get_font_kdfontop(struct kfont_context *ctx, int consolefd,
{
struct console_font_op cfo;

if (!is_kd_text(ctx, consolefd))
return -1;

cfo.op = KD_FONT_OP_GET;
cfo.flags = 0;
cfo.width = 32;
Expand Down Expand Up @@ -162,6 +185,9 @@ put_font_kdfontop(struct kfont_context *ctx, int consolefd, unsigned char *buf,
{
struct console_font_op cfo;

if (!is_kd_text(ctx, consolefd))
return -1;

if (vpitch == 32 && width <= 32)
cfo.op = KD_FONT_OP_SET;
else {
Expand Down
1 change: 1 addition & 0 deletions tests/data/e2e/setfont-test01.calls

Large diffs are not rendered by default.

0 comments on commit 9d60d24

Please sign in to comment.