diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/Makefile.am | 2 | ||||
-rw-r--r-- | src/shared/log.c | 1 | ||||
-rw-r--r-- | src/shared/terminal-util.c | 87 | ||||
-rw-r--r-- | src/shared/terminal-util.h | 39 | ||||
-rw-r--r-- | src/shared/util.c | 52 | ||||
-rw-r--r-- | src/shared/util.h | 12 |
6 files changed, 130 insertions, 63 deletions
diff --git a/src/shared/Makefile.am b/src/shared/Makefile.am index b0b4f59a6c..4d056e3efb 100644 --- a/src/shared/Makefile.am +++ b/src/shared/Makefile.am @@ -28,6 +28,7 @@ libudev_shared_la_SOURCES=\ strv.c \ strxcpyx.c \ sysctl-util.c \ + terminal-util.c \ time-util.c \ util.c \ utf8.c \ @@ -65,6 +66,7 @@ noinst_HEADERS = \ strv.h \ strxcpyx.h \ sysctl-util.h \ + terminal-util.h \ time-util.h \ util.h \ utf8.h \ diff --git a/src/shared/log.c b/src/shared/log.c index bcbf4a34bd..4a409964ad 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -34,6 +34,7 @@ #include "socket-util.h" #include "time-util.h" #include "process-util.h" +#include "terminal-util.h" #define SNDBUF_SIZE (8*1024*1024) diff --git a/src/shared/terminal-util.c b/src/shared/terminal-util.c new file mode 100644 index 0000000000..04c871a3f7 --- /dev/null +++ b/src/shared/terminal-util.c @@ -0,0 +1,87 @@ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> +#include <signal.h> +#include <time.h> +#include <assert.h> +#include <poll.h> +#include <linux/vt.h> +#include <linux/tiocl.h> +#include <linux/kd.h> + +#include "terminal-util.h" +#include "time-util.h" +#include "process-util.h" +#include "util.h" +#include "fileio.h" +#include "path-util.h" + +static volatile unsigned cached_columns = 0; +static volatile unsigned cached_lines = 0; + +int open_terminal(const char *name, int mode) { + int fd, r; + unsigned c = 0; + + /* + * If a TTY is in the process of being closed opening it might + * cause EIO. This is horribly awful, but unlikely to be + * changed in the kernel. Hence we work around this problem by + * retrying a couple of times. + * + * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 + */ + + assert(!(mode & O_CREAT)); + + for (;;) { + fd = open(name, mode, 0); + if (fd >= 0) + break; + + if (errno != EIO) + return -errno; + + /* Max 1s in total */ + if (c >= 20) + return -errno; + + usleep(50 * USEC_PER_MSEC); + c++; + } + + r = isatty(fd); + if (r < 0) { + safe_close(fd); + return -errno; + } + + if (!r) { + safe_close(fd); + return -ENOTTY; + } + + return fd; +} diff --git a/src/shared/terminal-util.h b/src/shared/terminal-util.h new file mode 100644 index 0000000000..b070780bc9 --- /dev/null +++ b/src/shared/terminal-util.h @@ -0,0 +1,39 @@ +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stdbool.h> +#include <stdarg.h> +#include <stdio.h> + +#include "macro.h" +#include "time-util.h" + +#define ANSI_HIGHLIGHT_ON "\x1B[1;39m" +#define ANSI_RED_ON "\x1B[31m" +#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m" +#define ANSI_GREEN_ON "\x1B[32m" +#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m" +#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m" +#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m" +#define ANSI_HIGHLIGHT_OFF "\x1B[0m" +#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K" + +int open_terminal(const char *name, int mode); diff --git a/src/shared/util.c b/src/shared/util.c index 0b04411f82..17ae61f90b 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -33,9 +33,6 @@ #include <fcntl.h> #include <dirent.h> #include <sys/ioctl.h> -#include <linux/vt.h> -#include <linux/tiocl.h> -#include <termios.h> #include <stdarg.h> #include <poll.h> #include <ctype.h> @@ -71,6 +68,7 @@ #include "virt.h" #include "process-util.h" #include "random-util.h" +#include "terminal-util.h" /* Put this test here for a lack of better place */ assert_cc(EAGAIN == EWOULDBLOCK); @@ -78,9 +76,6 @@ assert_cc(EAGAIN == EWOULDBLOCK); int saved_argc = 0; char **saved_argv = NULL; -static volatile unsigned cached_columns = 0; -static volatile unsigned cached_lines = 0; - size_t page_size(void) { static thread_local size_t pgsz = 0; long r; @@ -600,51 +595,6 @@ bool hidden_file(const char *filename) { return hidden_file_allow_backup(filename); } -int open_terminal(const char *name, int mode) { - int fd, r; - unsigned c = 0; - - /* - * If a TTY is in the process of being closed opening it might - * cause EIO. This is horribly awful, but unlikely to be - * changed in the kernel. Hence we work around this problem by - * retrying a couple of times. - * - * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 - */ - - assert(!(mode & O_CREAT)); - - for (;;) { - fd = open(name, mode, 0); - if (fd >= 0) - break; - - if (errno != EIO) - return -errno; - - /* Max 1s in total */ - if (c >= 20) - return -errno; - - usleep(50 * USEC_PER_MSEC); - c++; - } - - r = isatty(fd); - if (r < 0) { - safe_close(fd); - return -errno; - } - - if (!r) { - safe_close(fd); - return -ENOTTY; - } - - return fd; -} - int flush_fd(int fd) { struct pollfd pollfd = { .fd = fd, diff --git a/src/shared/util.h b/src/shared/util.h index c5e535069c..0dc46f103f 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -50,16 +50,6 @@ #define FORMAT_BYTES_MAX 8 -#define ANSI_HIGHLIGHT_ON "\x1B[1;39m" -#define ANSI_RED_ON "\x1B[31m" -#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m" -#define ANSI_GREEN_ON "\x1B[32m" -#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m" -#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m" -#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m" -#define ANSI_HIGHLIGHT_OFF "\x1B[0m" -#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K" - size_t page_size(void) _pure_; #define PAGE_ALIGN(l) ALIGN_TO((l), page_size()) @@ -223,8 +213,6 @@ bool hidden_file(const char *filename) _pure_; } \ struct __useless_struct_to_allow_trailing_semicolon__ -int open_terminal(const char *name, int mode); - int flush_fd(int fd); int fopen_temporary(const char *path, FILE **_f, char **_temp_path); |