Skip to content

Commit 2736295

Browse files
committed
terminal-util: unify code that resets /dev/console in common helper
We have pretty much the same code at two places, let's make it one.
1 parent 963e25c commit 2736295

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

src/basic/terminal-util.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,31 @@ int vt_disallocate(const char *name) {
582582
return 0;
583583
}
584584

585+
void reset_dev_console_fd(int fd, bool switch_to_text) {
586+
int r;
587+
588+
assert(fd >= 0);
589+
590+
r = reset_terminal_fd(fd, switch_to_text);
591+
if (r < 0)
592+
log_warning_errno(r, "Failed to reset /dev/console, ignoring: %m");
593+
594+
unsigned rows, cols;
595+
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
596+
if (r < 0)
597+
log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m");
598+
else if (r > 0) {
599+
r = terminal_set_size_fd(fd, NULL, rows, cols);
600+
if (r < 0)
601+
log_warning_errno(r, "Failed to set configured terminal size on /dev/console, ignoring: %m");
602+
} else
603+
(void) terminal_fix_size(fd, fd);
604+
605+
r = terminal_reset_ansi_seq(fd);
606+
if (r < 0)
607+
log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m");
608+
}
609+
585610
int make_console_stdio(void) {
586611
int fd, r;
587612

@@ -598,25 +623,7 @@ int make_console_stdio(void) {
598623
return log_error_errno(r, "Failed to make /dev/null stdin/stdout/stderr: %m");
599624

600625
} else {
601-
unsigned rows, cols;
602-
603-
r = reset_terminal_fd(fd, /* switch_to_text= */ true);
604-
if (r < 0)
605-
log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
606-
607-
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
608-
if (r < 0)
609-
log_warning_errno(r, "Failed to get terminal size, ignoring: %m");
610-
else if (r > 0) {
611-
r = terminal_set_size_fd(fd, NULL, rows, cols);
612-
if (r < 0)
613-
log_warning_errno(r, "Failed to set configured terminal size, ignoring: %m");
614-
} else
615-
(void) terminal_fix_size(fd, fd);
616-
617-
r = terminal_reset_ansi_seq(fd);
618-
if (r < 0)
619-
log_warning_errno(r, "Failed to reset terminal using ANSI sequences, ignoring: %m");
626+
reset_dev_console_fd(fd, /* switch_to_text= */ true);
620627

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

src/basic/terminal-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ bool tty_is_console(const char *tty) _pure_;
156156
int vtnr_from_tty(const char *tty);
157157
const char* default_term_for_tty(const char *tty);
158158

159+
void reset_dev_console_fd(int fd, bool switch_to_text);
159160
int make_console_stdio(void);
160161

161162
int fd_columns(int fd);

src/core/main.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,18 @@ static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) {
205205
}
206206

207207
static int console_setup(void) {
208+
209+
if (getpid_cached() != 1)
210+
return 0;
211+
208212
_cleanup_close_ int tty_fd = -EBADF;
209-
unsigned rows, cols;
210-
int r;
211213

212-
tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
214+
tty_fd = open_terminal("/dev/console", O_RDWR|O_NOCTTY|O_CLOEXEC);
213215
if (tty_fd < 0)
214216
return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
215217

216-
/* We don't want to force text mode. plymouth may be showing
217-
* pictures already from initrd. */
218-
r = reset_terminal_fd(tty_fd, false);
219-
if (r < 0)
220-
return log_error_errno(r, "Failed to reset /dev/console: %m");
221-
222-
r = proc_cmdline_tty_size("/dev/console", &rows, &cols);
223-
if (r < 0)
224-
log_warning_errno(r, "Failed to get /dev/console size, ignoring: %m");
225-
else {
226-
r = terminal_set_size_fd(tty_fd, NULL, rows, cols);
227-
if (r < 0)
228-
log_warning_errno(r, "Failed to set /dev/console size, ignoring: %m");
229-
}
230-
231-
r = terminal_reset_ansi_seq(tty_fd);
232-
if (r < 0)
233-
log_warning_errno(r, "Failed to reset /dev/console using ANSI sequences, ignoring: %m");
234-
218+
/* We don't want to force text mode. Plymouth may be showing pictures already from initrd. */
219+
reset_dev_console_fd(tty_fd, /* switch_to_text= */ false);
235220
return 0;
236221
}
237222

@@ -2910,7 +2895,7 @@ static void setup_console_terminal(bool skip_setup) {
29102895
(void) release_terminal();
29112896

29122897
/* Reset the console, but only if this is really init and we are freshly booted */
2913-
if (getpid_cached() == 1 && !skip_setup)
2898+
if (!skip_setup)
29142899
(void) console_setup();
29152900
}
29162901

0 commit comments

Comments
 (0)