]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/terminal-util.c
core: Limit terminal reset using ANSI sequences to /dev/console
[thirdparty/systemd.git] / src / basic / terminal-util.c
index 11c0da870c72b24af237e199c87b6f2060f7ec5f..dda592089cb2a46bef8c02232618d22761ddf9ff 100644 (file)
@@ -306,29 +306,9 @@ int reset_terminal_fd(int fd, bool switch_to_text) {
         termios.c_cc[VMIN]   = 1;
 
         r = RET_NERRNO(tcsetattr(fd, TCSANOW, &termios));
-        if (r < 0) {
+        if (r < 0)
                 log_debug_errno(r, "Failed to set terminal parameters: %m");
-                goto finish;
-        }
-
-        if (!terminal_is_dumb()) {
-                r = fd_nonblock(fd, true);
-                if (r < 0) {
-                        log_debug_errno(r, "Failed to set terminal to non-blocking mode: %m");
-                        goto finish;
-                }
-
-                 /* Enable line wrapping. */
-                (void) loop_write_full(fd, "\033[?7h", SIZE_MAX, 50 * USEC_PER_MSEC);
 
-                if (r > 0) {
-                        r = fd_nonblock(fd, false);
-                        if (r < 0) {
-                                log_debug_errno(r, "Failed to set terminal back to blocking mode: %m");
-                                goto finish;
-                        }
-                }
-        }
 finish:
         /* Just in case, flush all crap out */
         (void) tcflush(fd, TCIOFLUSH);
@@ -1565,6 +1545,37 @@ int set_terminal_cursor_position(int fd, unsigned int row, unsigned int column)
         return 0;
 }
 
+int terminal_reset_ansi_seq(int fd) {
+        int r, k;
+
+        assert(fd >= 0);
+
+        if (getenv_terminal_is_dumb())
+                return 0;
+
+        r = fd_nonblock(fd, true);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to set terminal to non-blocking mode: %m");
+
+        k = loop_write_full(fd,
+                            "\033c"        /* reset to initial state */
+                            "\033[!p"      /* soft terminal reset */
+                            "\033]104\007" /* reset colors */
+                            "\033[?7h",    /* enable line-wrapping */
+                            SIZE_MAX,
+                            50 * USEC_PER_MSEC);
+        if (k < 0)
+                log_debug_errno(k, "Failed to write to terminal: %m");
+
+        if (r > 0) {
+                r = fd_nonblock(fd, false);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to set terminal back to blocking mode: %m");
+        }
+
+        return k < 0 ? k : r;
+}
+
 void termios_disable_echo(struct termios *termios) {
         assert(termios);