Skip to content

Commit 2b379aa

Browse files
committed
Revert "terminal-util: unify code that resets /dev/console in common helper"
This reverts commit 2736295.
1 parent 9c8559b commit 2b379aa

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

src/basic/terminal-util.c

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static int vt_reset_keyboard(int fd) {
577577
return RET_NERRNO(ioctl(fd, KDSKBMODE, kb));
578578
}
579579

580-
static int terminal_reset_ioctl(int fd, bool switch_to_text) {
580+
int terminal_reset_ioctl(int fd, bool switch_to_text) {
581581
struct termios termios;
582582
int r;
583583

@@ -645,7 +645,7 @@ static int terminal_reset_ioctl(int fd, bool switch_to_text) {
645645
return r;
646646
}
647647

648-
static int terminal_reset_ansi_seq(int fd) {
648+
int terminal_reset_ansi_seq(int fd) {
649649
int r, k;
650650

651651
assert(fd >= 0);
@@ -675,35 +675,6 @@ static int terminal_reset_ansi_seq(int fd) {
675675
return k < 0 ? k : r;
676676
}
677677

678-
void reset_dev_console_fd(int fd, bool switch_to_text) {
679-
int r;
680-
681-
assert(fd >= 0);
682-
683-
_cleanup_close_ int lock_fd = lock_dev_console();
684-
if (lock_fd < 0)
685-
log_debug_errno(lock_fd, "Failed to lock /dev/console, ignoring: %m");
686-
687-
r = terminal_reset_ioctl(fd, switch_to_text);
688-
if (r < 0)
689-
log_warning_errno(r, "Failed to reset /dev/console, ignoring: %m");
690-
691-
unsigned rows, cols;
692-
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
693-
if (r < 0)
694-
log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m");
695-
else if (r > 0) {
696-
r = terminal_set_size_fd(fd, NULL, rows, cols);
697-
if (r < 0)
698-
log_warning_errno(r, "Failed to set configured terminal size on /dev/console, ignoring: %m");
699-
} else
700-
(void) terminal_fix_size(fd, fd);
701-
702-
r = terminal_reset_ansi_seq(fd);
703-
if (r < 0)
704-
log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m");
705-
}
706-
707678
int lock_dev_console(void) {
708679
_cleanup_close_ int fd = -EBADF;
709680
int r;
@@ -737,7 +708,25 @@ int make_console_stdio(void) {
737708
return log_error_errno(r, "Failed to make /dev/null stdin/stdout/stderr: %m");
738709

739710
} else {
740-
reset_dev_console_fd(fd, /* switch_to_text= */ true);
711+
unsigned rows, cols;
712+
713+
r = terminal_reset_ioctl(fd, /* switch_to_text= */ true);
714+
if (r < 0)
715+
log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
716+
717+
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
718+
if (r < 0)
719+
log_warning_errno(r, "Failed to get terminal size, ignoring: %m");
720+
else if (r > 0) {
721+
r = terminal_set_size_fd(fd, NULL, rows, cols);
722+
if (r < 0)
723+
log_warning_errno(r, "Failed to set configured terminal size, ignoring: %m");
724+
} else
725+
(void) terminal_fix_size(fd, fd);
726+
727+
r = terminal_reset_ansi_seq(fd);
728+
if (r < 0)
729+
log_warning_errno(r, "Failed to reset terminal using ANSI sequences, ignoring: %m");
741730

742731
r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */
743732
if (r < 0)

src/basic/terminal-util.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
bool isatty_safe(int fd);
4141

42+
int terminal_reset_ioctl(int fd, bool switch_to_text);
43+
int terminal_reset_ansi_seq(int fd);
4244
int terminal_reset_defensive(int fd, bool switch_to_text);
4345
int terminal_reset_defensive_locked(int fd, bool switch_to_text);
4446

@@ -101,7 +103,6 @@ bool tty_is_console(const char *tty) _pure_;
101103
int vtnr_from_tty(const char *tty);
102104
const char* default_term_for_tty(const char *tty);
103105

104-
void reset_dev_console_fd(int fd, bool switch_to_text);
105106
int lock_dev_console(void);
106107
int make_console_stdio(void);
107108

src/core/main.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,32 @@ static int save_console_winsize_in_environment(int tty_fd) {
245245
}
246246

247247
static int console_setup(void) {
248-
249-
if (getpid_cached() != 1)
250-
return 0;
251-
252248
_cleanup_close_ int tty_fd = -EBADF;
249+
unsigned rows, cols;
250+
int r;
253251

254-
tty_fd = open_terminal("/dev/console", O_RDWR|O_NOCTTY|O_CLOEXEC);
252+
tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
255253
if (tty_fd < 0)
256254
return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
257255

258-
/* We don't want to force text mode. Plymouth may be showing pictures already from initrd. */
259-
reset_dev_console_fd(tty_fd, /* switch_to_text= */ false);
256+
/* We don't want to force text mode. plymouth may be showing
257+
* pictures already from initrd. */
258+
r = terminal_reset_ioctl(tty_fd, false);
259+
if (r < 0)
260+
return log_error_errno(r, "Failed to reset /dev/console: %m");
261+
262+
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
263+
if (r < 0)
264+
log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m");
265+
else {
266+
r = terminal_set_size_fd(tty_fd, NULL, rows, cols);
267+
if (r < 0)
268+
log_warning_errno(r, "Failed to set /dev/console size, ignoring: %m");
269+
}
270+
271+
r = terminal_reset_ansi_seq(tty_fd);
272+
if (r < 0)
273+
log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m");
260274

261275
save_console_winsize_in_environment(tty_fd);
262276

@@ -2977,7 +2991,7 @@ static void setup_console_terminal(bool skip_setup) {
29772991
(void) release_terminal();
29782992

29792993
/* Reset the console, but only if this is really init and we are freshly booted */
2980-
if (!skip_setup)
2994+
if (getpid_cached() == 1 && !skip_setup)
29812995
(void) console_setup();
29822996
}
29832997

0 commit comments

Comments
 (0)