@@ -589,10 +589,21 @@ int make_console_stdio(void) {
589589 return log_error_errno (r , "Failed to make /dev/null stdin/stdout/stderr: %m" );
590590
591591 } else {
592- r = reset_terminal_fd (fd , true);
592+ unsigned rows , cols ;
593+
594+ r = reset_terminal_fd (fd , /* switch_to_text= */ true);
593595 if (r < 0 )
594596 log_warning_errno (r , "Failed to reset terminal, ignoring: %m" );
595597
598+ r = proc_cmdline_tty_size ("/dev/console" , & rows , & cols );
599+ if (r < 0 )
600+ log_warning_errno (r , "Failed to get terminal size, ignoring: %m" );
601+ else {
602+ r = terminal_set_size_fd (fd , NULL , rows , cols );
603+ if (r < 0 )
604+ log_warning_errno (r , "Failed to set terminal size, ignoring: %m" );
605+ }
606+
596607 r = rearrange_stdio (fd , fd , fd ); /* This invalidates 'fd' both on success and on failure. */
597608 if (r < 0 )
598609 return log_error_errno (r , "Failed to make terminal stdin/stdout/stderr: %m" );
@@ -882,6 +893,54 @@ int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols
882893 return 0 ;
883894}
884895
896+ int proc_cmdline_tty_size (const char * tty , unsigned * ret_rows , unsigned * ret_cols ) {
897+ _cleanup_free_ char * rowskey = NULL , * rowsvalue = NULL , * colskey = NULL , * colsvalue = NULL ;
898+ unsigned rows = UINT_MAX , cols = UINT_MAX ;
899+ int r ;
900+
901+ assert (tty );
902+
903+ if (!ret_rows && !ret_cols )
904+ return 0 ;
905+
906+ tty = skip_dev_prefix (tty );
907+ if (!in_charset (tty , ALPHANUMERICAL ))
908+ return log_debug_errno (SYNTHETIC_ERRNO (EINVAL ), "%s contains non-alphanumeric characters" , tty );
909+
910+ rowskey = strjoin ("systemd.tty.rows." , tty );
911+ if (!rowskey )
912+ return - ENOMEM ;
913+
914+ colskey = strjoin ("systemd.tty.columns." , tty );
915+ if (!colskey )
916+ return - ENOMEM ;
917+
918+ r = proc_cmdline_get_key_many (/* flags = */ 0 ,
919+ rowskey , & rowsvalue ,
920+ colskey , & colsvalue );
921+ if (r < 0 )
922+ return log_debug_errno (r , "Failed to read TTY size of %s from kernel cmdline: %m" , tty );
923+
924+ if (rowsvalue ) {
925+ r = safe_atou (rowsvalue , & rows );
926+ if (r < 0 )
927+ return log_debug_errno (r , "Failed to parse %s=%s: %m" , rowskey , rowsvalue );
928+ }
929+
930+ if (colsvalue ) {
931+ r = safe_atou (colsvalue , & cols );
932+ if (r < 0 )
933+ return log_debug_errno (r , "Failed to parse %s=%s: %m" , colskey , colsvalue );
934+ }
935+
936+ if (ret_rows )
937+ * ret_rows = rows ;
938+ if (ret_cols )
939+ * ret_cols = cols ;
940+
941+ return 0 ;
942+ }
943+
885944/* intended to be used as a SIGWINCH sighandler */
886945void columns_lines_cache_reset (int signum ) {
887946 cached_columns = 0 ;
0 commit comments