Skip to content

Instantly share code, notes, and snippets.

@legionus
Last active October 9, 2024 11:31
Show Gist options
  • Save legionus/1cc007048481bd41b47196592244a697 to your computer and use it in GitHub Desktop.
Save legionus/1cc007048481bd41b47196592244a697 to your computer and use it in GitHub Desktop.
diff --git a/src/libkfont/kdfontop.c b/src/libkfont/kdfontop.c
index 1eadbebb..cada3f95 100644
--- a/src/libkfont/kdfontop.c
+++ b/src/libkfont/kdfontop.c
@@ -21,11 +21,29 @@
#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;
+ }
+
+ return (kd_mode == KD_TEXT);
+}
+
int
kfont_restore_font(struct kfont_context *ctx, int fd)
{
struct console_font_op cfo;
+ if (!is_kd_text(ctx, fd)) {
+ KFONT_ERR(ctx, _("Console is not in text mode"));
+ return -1;
+ }
+
cfo.op = KD_FONT_OP_SET_DEFAULT;
if (ioctl(fd, KDFONTOP, &cfo)) {
@@ -60,6 +78,11 @@ get_font_kdfontop(struct kfont_context *ctx, int consolefd,
{
struct console_font_op cfo;
+ if (!is_kd_text(ctx, consolefd)) {
+ KFONT_ERR(ctx, _("Console is not in text mode"));
+ return -1;
+ }
+
cfo.op = KD_FONT_OP_GET;
cfo.flags = 0;
cfo.width = 32;
@@ -162,6 +185,11 @@ put_font_kdfontop(struct kfont_context *ctx, int consolefd, unsigned char *buf,
{
struct console_font_op cfo;
+ if (!is_kd_text(ctx, consolefd)) {
+ KFONT_ERR(ctx, _("Console is not in text mode"));
+ return -1;
+ }
+
if (vpitch == 32 && width <= 32)
cfo.op = KD_FONT_OP_SET;
else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment