From: Lennart Poettering Date: Wed, 8 Nov 2023 12:39:49 +0000 (+0100) Subject: execute: add new helper exec_context_apply_tty_size() X-Git-Tag: v255-rc2~45^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d2b9e755242a98f14d89537380bef4e8e5ab9791;p=thirdparty%2Fsystemd.git execute: add new helper exec_context_apply_tty_size() This combines exec_context_determine_tty_size() and terminal_set_size_fd() since we always use one after the other. Also make exec_context_determine_tty_size() return void, since it cannot fail. --- diff --git a/src/core/exec-invoke.c b/src/core/exec-invoke.c index 38af3b488dc..627b96d4359 100644 --- a/src/core/exec-invoke.c +++ b/src/core/exec-invoke.c @@ -392,12 +392,9 @@ static int setup_input( /* Try to make this the controlling tty, if it is a tty, and reset it */ if (isatty(STDIN_FILENO)) { - unsigned rows = context->tty_rows, cols = context->tty_cols; - - (void) exec_context_tty_size(context, &rows, &cols); (void) ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE); (void) reset_terminal_fd(STDIN_FILENO, true); - (void) terminal_set_size_fd(STDIN_FILENO, NULL, rows, cols); + (void) exec_context_apply_tty_size(context, STDIN_FILENO, /* tty_path= */ NULL); } return STDIN_FILENO; @@ -413,26 +410,29 @@ static int setup_input( case EXEC_INPUT_TTY: case EXEC_INPUT_TTY_FORCE: case EXEC_INPUT_TTY_FAIL: { - unsigned rows, cols; - int fd; + _cleanup_close_ int tty_fd = -EBADF; + const char *tty_path; - fd = acquire_terminal(exec_context_tty_path(context), - i == EXEC_INPUT_TTY_FAIL ? ACQUIRE_TERMINAL_TRY : - i == EXEC_INPUT_TTY_FORCE ? ACQUIRE_TERMINAL_FORCE : - ACQUIRE_TERMINAL_WAIT, - USEC_INFINITY); - if (fd < 0) - return fd; + tty_path = ASSERT_PTR(exec_context_tty_path(context)); + + tty_fd = acquire_terminal(tty_path, + i == EXEC_INPUT_TTY_FAIL ? ACQUIRE_TERMINAL_TRY : + i == EXEC_INPUT_TTY_FORCE ? ACQUIRE_TERMINAL_FORCE : + ACQUIRE_TERMINAL_WAIT, + USEC_INFINITY); + if (tty_fd < 0) + return tty_fd; - r = exec_context_tty_size(context, &rows, &cols); + r = exec_context_apply_tty_size(context, tty_fd, tty_path); if (r < 0) return r; - r = terminal_set_size_fd(fd, exec_context_tty_path(context), rows, cols); + r = move_fd(tty_fd, STDIN_FILENO, /* cloexec= */ false); if (r < 0) return r; - return move_fd(fd, STDIN_FILENO, false); + TAKE_FD(tty_fd); + return r; } case EXEC_INPUT_SOCKET: @@ -692,7 +692,6 @@ static int setup_confirm_stdio( int *ret_saved_stdout) { _cleanup_close_ int fd = -EBADF, saved_stdin = -EBADF, saved_stdout = -EBADF; - unsigned rows, cols; int r; assert(ret_saved_stdin); @@ -718,11 +717,7 @@ static int setup_confirm_stdio( if (r < 0) return r; - r = exec_context_tty_size(context, &rows, &cols); - if (r < 0) - return r; - - r = terminal_set_size_fd(fd, vc, rows, cols); + r = exec_context_apply_tty_size(context, fd, vc); if (r < 0) return r; diff --git a/src/core/execute.c b/src/core/execute.c index e83a2ab239f..61d0d721d71 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -99,27 +99,46 @@ const char *exec_context_tty_path(const ExecContext *context) { return "/dev/console"; } -int exec_context_tty_size(const ExecContext *context, unsigned *ret_rows, unsigned *ret_cols) { +static void exec_context_determine_tty_size( + const ExecContext *context, + const char *tty_path, + unsigned *ret_rows, + unsigned *ret_cols) { + unsigned rows, cols; - const char *tty; assert(context); assert(ret_rows); assert(ret_cols); + if (!tty_path) + tty_path = exec_context_tty_path(context); + rows = context->tty_rows; cols = context->tty_cols; - tty = exec_context_tty_path(context); - if (tty) - (void) proc_cmdline_tty_size(tty, rows == UINT_MAX ? &rows : NULL, cols == UINT_MAX ? &cols : NULL); + if (tty_path && (rows == UINT_MAX || cols == UINT_MAX)) + (void) proc_cmdline_tty_size( + tty_path, + rows == UINT_MAX ? &rows : NULL, + cols == UINT_MAX ? &cols : NULL); *ret_rows = rows; *ret_cols = cols; - - return 0; } +int exec_context_apply_tty_size( + const ExecContext *context, + int tty_fd, + const char *tty_path) { + + unsigned rows, cols; + + exec_context_determine_tty_size(context, tty_path, &rows, &cols); + + return terminal_set_size_fd(tty_fd, tty_path, rows, cols); + } + void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p) { _cleanup_close_ int _fd = -EBADF, lock_fd = -EBADF; int fd; @@ -152,12 +171,8 @@ void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p) if (context->tty_reset) (void) reset_terminal_fd(fd, true); - if (p && p->stdin_fd >= 0) { - unsigned rows = context->tty_rows, cols = context->tty_cols; - - (void) exec_context_tty_size(context, &rows, &cols); - (void) terminal_set_size_fd(p->stdin_fd, path, rows, cols); - } + if (p && p->stdin_fd >= 0) + (void) exec_context_apply_tty_size(context, p->stdin_fd, path); if (context->tty_vt_disallocate && path) (void) vt_disallocate(path); diff --git a/src/core/execute.h b/src/core/execute.h index 4896a424923..e15e19aae65 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -513,7 +513,7 @@ int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanM int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret); const char *exec_context_tty_path(const ExecContext *context); -int exec_context_tty_size(const ExecContext *context, unsigned *ret_rows, unsigned *ret_cols); +int exec_context_apply_tty_size(const ExecContext *context, int tty_fd, const char *tty_path); void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p); uint64_t exec_context_get_rlimit(const ExecContext *c, const char *name);