Skip to content

Commit cfa3585

Browse files
committed
functions: extract fail_unseal and detect_heads_tty as shared utilities
fail_unseal() was defined identically in both unseal-hotp and unseal-totp, differing only in the debug message string. Move it to /etc/functions using basename \$0 so the message still identifies the calling script. detect_heads_tty() was copy-pasted between gui-init and gui-init-basic. Move it to /etc/functions and replace both inline blocks with a single call. Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 7775758 commit cfa3585

5 files changed

Lines changed: 33 additions & 38 deletions

File tree

initrd/bin/gui-init

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,7 @@ export BG_COLOR_MAIN_MENU="normal"
1717
# fd0/1/2 with the correct console device before launching this script.
1818
# Fall back to /sys/class/tty/console/active (last entry = preferred console,
1919
# same source used by systemd and busybox cttyhack) when tty is unavailable.
20-
if ! HEADS_TTY=$(tty 2>/dev/null); then
21-
_active=$(cat /sys/class/tty/console/active 2>/dev/null)
22-
_dev="${_active##* }"
23-
[ "$_dev" = "tty0" ] && _dev=$(cat /sys/class/tty/tty0/active 2>/dev/null || echo tty0)
24-
HEADS_TTY="/dev/${_dev:-console}"
25-
fi
26-
unset _active _dev
27-
export HEADS_TTY
28-
export GPG_TTY="$HEADS_TTY"
20+
detect_heads_tty
2921

3022
# skip_to_menu is set if the user selects "continue to the main menu" from any
3123
# error, so we will indeed go to the main menu even if other errors occur. It's

initrd/bin/gui-init-basic

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@ export BG_COLOR_MAIN_MENU="normal"
99
. /etc/gui_functions
1010
. /tmp/config
1111

12-
# Detect the terminal this gui-init-basic session is running on.
13-
# Same logic as gui-init — see comments there.
14-
if ! HEADS_TTY=$(tty 2>/dev/null); then
15-
_active=$(cat /sys/class/tty/console/active 2>/dev/null)
16-
_dev="${_active##* }"
17-
[ "$_dev" = "tty0" ] && _dev=$(cat /sys/class/tty/tty0/active 2>/dev/null || echo tty0)
18-
HEADS_TTY="/dev/${_dev:-console}"
19-
fi
20-
unset _active _dev
21-
export HEADS_TTY
22-
export GPG_TTY="$HEADS_TTY"
12+
# Detect the terminal — see detect_heads_tty in /etc/functions.
13+
detect_heads_tty
2314

2415
# skip_to_menu is set if the user selects "continue to the main menu" from any
2516
# error, so we will indeed go to the main menu even if other errors occur. It's

initrd/bin/unseal-hotp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
HOTP_SECRET="/tmp/secret/hotp.key"
77
HOTP_COUNTER="/boot/kexec_hotp_counter"
88

9-
fail_unseal() {
10-
TRACE_FUNC
11-
if [ "$HEADS_NONFATAL_UNSEAL" = "y" ]; then
12-
DEBUG "nonfatal unseal-hotp failure: $*"
13-
return 1
14-
fi
15-
DIE "$*"
16-
}
17-
189
mount_boot_or_die() {
1910
TRACE_FUNC
2011
# Mount local disk if it is not already mounted

initrd/bin/unseal-totp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55

66
TOTP_SECRET="/tmp/secret/totp.key"
77

8-
fail_unseal() {
9-
TRACE_FUNC
10-
if [ "$HEADS_NONFATAL_UNSEAL" = "y" ]; then
11-
DEBUG "nonfatal unseal-totp failure: $*"
12-
return 1
13-
fi
14-
DIE "$*"
15-
}
16-
178
fail_unseal_reset_required() {
189
TRACE_FUNC
1910
# A TPM-side unseal failure generally indicates that reset/re-ownership is

initrd/etc/functions

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ LOG() {
366366
# All modes: console prompt in bold white to /dev/console, plain text in debug.log
367367
#
368368
# See doc/logging.md.
369+
370+
# detect_heads_tty - resolve the active interactive terminal and export it.
371+
# Sets and exports HEADS_TTY and GPG_TTY.
372+
# Must be called at script top-level (not inside a subshell) to take effect.
373+
detect_heads_tty() {
374+
if ! HEADS_TTY=$(tty 2>/dev/null); then
375+
local _active _dev
376+
_active=$(cat /sys/class/tty/console/active 2>/dev/null)
377+
_dev="${_active##* }"
378+
[ "$_dev" = "tty0" ] && _dev=$(cat /sys/class/tty/tty0/active 2>/dev/null || echo tty0)
379+
HEADS_TTY="/dev/${_dev:-console}"
380+
fi
381+
export HEADS_TTY
382+
export GPG_TTY="$HEADS_TTY"
383+
}
384+
369385
INPUT() {
370386
TRACE_FUNC
371387
local prompt="$1"
@@ -2611,6 +2627,20 @@ load_keymap() {
26112627
fi
26122628
}
26132629

2630+
# fail_unseal - called by unseal-hotp and unseal-totp on failure.
2631+
# If HEADS_NONFATAL_UNSEAL=y (set by callers that handle failure themselves,
2632+
# e.g. gui-init's integrity report), log at DEBUG and return 1 so the caller
2633+
# can decide what to do. Otherwise DIE, which is appropriate when the unseal
2634+
# script is run standalone and failure is unrecoverable.
2635+
fail_unseal() {
2636+
TRACE_FUNC
2637+
if [ "$HEADS_NONFATAL_UNSEAL" = "y" ]; then
2638+
DEBUG "nonfatal $(basename "$0") failure: $*"
2639+
return 1
2640+
fi
2641+
DIE "$*"
2642+
}
2643+
26142644
# Show an updating UTC timestamp and optional TOTP on a single refreshed line
26152645
# until the user presses the Escape key. Returns 0 after ESC pressed.
26162646
# Function name: show_totp_until_esc - clearly indicates this displays the

0 commit comments

Comments
 (0)