]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
execute: add new helper exec_context_apply_tty_size()
authorLennart Poettering <lennart@poettering.net>
Wed, 8 Nov 2023 12:39:49 +0000 (13:39 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 10 Nov 2023 20:38:26 +0000 (21:38 +0100)
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.

src/core/exec-invoke.c
src/core/execute.c
src/core/execute.h

index 38af3b488dcb09a951fe01e54fde8d9f623f83e9..627b96d435923dc00231158730bd5d2d8084ff1c 100644 (file)
@@ -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;
 
index e83a2ab239f78ca648a558497e4bef8d56e8d877..61d0d721d71d9b92c3860279e638ca92945d98e0 100644 (file)
@@ -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);
index 4896a42492318b25469299d2d195eb092c265249..e15e19aae65aa4be6a9274e495f7f18112a65b9c 100644 (file)
@@ -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);