Hi everyone,
Here is the first patch of the series for hurd/boot.
This patch focuses on robustness around terminal state handling and
introduces infrastructure for console input diagnostics:
1.
*Terminal State & Signal Management:*
-
Track initialisation state (termstate_initialized) to prevent
restoring uninitialized termios attributes.
-
Handle SIGTSTP and SIGCONT so the terminal correctly exits and
re-enters raw mode when stopped and continued in shell sessions.
-
Register restore_termstate with atexit().
2.
*Diagnostics & Cleanup:*
-
Add write_diag() helper for thread-safe/signal-safe diagnostic output
to stderr.
-
Remove unused global variables and refine console_mscount type to
mach_port_mscount_t.
-
Declare console event infrastructure (wake/select pipes, stdin_eof
atomic flag) for queued input processing
---
>From 6a9ac9915e800ddc9fe597d17f84bae441d13f30 Mon Sep 17 00:00:00 2001
From: Alperen ERKAN <[email protected]>
Date: Sat, 16 Sep 2026 12:50:33 +0300
Subject: [PATCH 1/5] boot: fix terminal state handling and add diagnostic
helpers
Add copyright header, new includes, and rework the terminal state
management: track initialization, restore the tty on exit, and handle
SIGCONT/SIGTSTP so raw mode survives stop/continue. Add the
write_diag helper, drop unused globals, and declare the console
input event infrastructure (wake/select pipes, stdin_eof flag).
---
hurd/boot/boot.c | 93
++++++++++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 76 insertions(+), 17 deletions(-)
diff --git a/hurd/boot/boot.c b/hurd/boot/boot.c
--- a/hurd/boot/boot.c
+++ b/hurd/boot/boot.c
@@ -2,6 +3,7 @@
as if we were the kernel.
Copyright (C) 1993,94,95,96,97,98,99,2000,01,02,2006,14,16
Free Software Foundation, Inc.
+ Copyright (C) 2026 Alperen ERKAN
This file is part of the GNU Hurd.
@@ -60,8 +64,12 @@
#include <hurd/auth.h>
#include <unistd.h>
-#include <fcntl.h>
#include <signal.h>
+#include <poll.h>
+#include <stdatomic.h>
+#include <time.h>
+#include <limits.h>
+#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>
@@ -84,9 +125,50 @@ static int privileged;
static int want_privileged;
static struct termios orig_tty_state;
+static int termstate_initialized;
static int isig;
static char *kernel_command_line;
+static void
+restore_termstate (void)
+{
+ if (! termstate_initialized)
+ return;
+ tcsetattr (0, 0, &orig_tty_state);
+ termstate_initialized = 0;
+}
+
+static void
+sig_handler (int sig)
+{
+ switch (sig)
+ {
+ case SIGCONT:
+ /* Re-enter raw mode after being stopped. */
+ if (termstate_initialized)
+ {
+ struct termios tty_state = orig_tty_state;
+ cfmakeraw (&tty_state);
+ if (isig)
+ tty_state.c_lflag |= ISIG;
+ tcsetattr (0, 0, &tty_state);
+ }
+ break;
+
+ case SIGTSTP:
+ restore_termstate ();
+ signal (SIGTSTP, SIG_DFL);
+ raise (SIGTSTP);
+ break;
+
+ default:
+ restore_termstate ();
+ signal (sig, SIG_DFL);
+ raise (sig);
+ break;
+ }
+}
+
static void
init_termstate (void)
{
@@ -143,12 +145,14 @@ init_termstate (void)
if (tcsetattr (0, 0, &tty_state) < 0)
error (11, errno, "tcsetattr");
-}
-static void
-restore_termstate (void)
-{
- tcsetattr (0, 0, &orig_tty_state);
+ termstate_initialized = 1;
+
+ atexit (restore_termstate);
+ signal (SIGINT, sig_handler);
+ signal (SIGTERM, sig_handler);
+ signal (SIGTSTP, sig_handler);
+ signal (SIGCONT, sig_handler);
}
#define host_fstat fstat
@@ -163,6 +173,16 @@ host_exit (int status)
exit (status);
}
+/* Best-effort write of a diagnostic message to stderr. */
+static void
+write_diag (const char *msg, size_t len)
+{
+ ssize_t err;
+ do
+ err = write (2, msg, len);
+ while (err < 0 && errno == EINTR);
+}
+
int verbose;
mach_port_t privileged_host_port, master_device_port;
@@ -195,17 +185,7 @@ struct store *root_store;
pthread_spinlock_t queuelock = PTHREAD_SPINLOCK_INITIALIZER;
pthread_spinlock_t readlock = PTHREAD_SPINLOCK_INITIALIZER;
-mach_port_t php_child_name, psmdp_child_name, taskname;
-
-task_t child_task;
-mach_port_t bootport;
-
-int console_mscount;
-
-vm_address_t fs_stack_base;
-vm_size_t fs_stack_size;
-
-char *fsname;
+mach_port_mscount_t console_mscount;
char bootstrap_args[100] = "-";
char *bootdevice = 0;
@@ -315,6 +326,17 @@ boot_demuxer (mach_msg_header_t *inp,
static void read_reply (void);
static void * msg_thread (void *);
+static void * select_thread (void *);
+
+/* Console input event handling. The main thread polls the host stdin
+ only while console read requests are queued; the message threads
+ wake it via WAKE_PIPE. */
+static int wake_pipe[2];
+static int select_pipe[2];
+static _Atomic int stdin_eof;
+
+/* Maximum size of a single console read request (out-of-line). */
+#define CONSOLE_READ_MAX (16 * 1024 * 1024)
const char *argp_program_version = STANDARD_HURD_VERSION (boot);
--
2.43.0