summaryrefslogtreecommitdiffstats
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-07-17 09:49:01 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-07-17 11:35:04 +0200
commitba5d26ccb23f6a312a11ba710fefc2ea92160ac6 (patch)
tree60c9fafaf5b80b4124966484f4e60a2cf3ad5286 /src/basic
parentshared: fix assert call (diff)
downloadsystemd-ba5d26ccb23f6a312a11ba710fefc2ea92160ac6.tar.xz
systemd-ba5d26ccb23f6a312a11ba710fefc2ea92160ac6.zip
shared: voidify call to loop_write() and trim duplicate code
Coverity CID#1402375.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/terminal-util.c82
1 files changed, 34 insertions, 48 deletions
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 76d6d1a20c..1f39c17306 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -526,71 +526,57 @@ int terminal_vhangup(const char *name) {
}
int vt_disallocate(const char *name) {
- _cleanup_close_ int fd = -1;
- const char *e, *n;
- unsigned u;
+ const char *e;
int r;
/* Deallocate the VT if possible. If not possible
* (i.e. because it is the active one), at least clear it
- * entirely (including the scrollback buffer) */
+ * entirely (including the scrollback buffer). */
e = path_startswith(name, "/dev/");
if (!e)
return -EINVAL;
- if (!tty_is_vc(name)) {
- /* So this is not a VT. I guess we cannot deallocate
- * it then. But let's at least clear the screen */
-
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[2J", /* clear screen */
- 10, false);
- return 0;
- }
-
- n = startswith(e, "tty");
- if (!n)
- return -EINVAL;
+ if (tty_is_vc(name)) {
+ _cleanup_close_ int fd = -1;
+ unsigned u;
+ const char *n;
- r = safe_atou(n, &u);
- if (r < 0)
- return r;
+ n = startswith(e, "tty");
+ if (!n)
+ return -EINVAL;
- if (u <= 0)
- return -EINVAL;
+ r = safe_atou(n, &u);
+ if (r < 0)
+ return r;
- /* Try to deallocate */
- fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
- if (fd < 0)
- return fd;
+ if (u <= 0)
+ return -EINVAL;
- r = ioctl(fd, VT_DISALLOCATE, u);
- fd = safe_close(fd);
+ /* Try to deallocate */
+ fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return fd;
- if (r >= 0)
- return 0;
+ r = ioctl(fd, VT_DISALLOCATE, u);
+ if (r >= 0)
+ return 0;
+ if (errno != EBUSY)
+ return -errno;
+ }
- if (errno != EBUSY)
- return -errno;
+ /* So this is not a VT (in which case we cannot deallocate it),
+ * or we failed to deallocate. Let's at least clear the screen. */
- /* Couldn't deallocate, so let's clear it fully with
- * scrollback */
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ _cleanup_close_ int fd2 = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd2 < 0)
+ return fd2;
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
- 10, false);
+ (void) loop_write(fd2,
+ "\033[r" /* clear scrolling region */
+ "\033[H" /* move home */
+ "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
+ 10, false);
return 0;
}