/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <poll.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "io-util.h"
#include "log.h"
#include "macro.h"
#include "parse-util.h"
#include "process-util.h"
#include "socket-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "time-util.h"
#include "util.h"
static volatile unsigned cached_columns = 0;
static volatile unsigned cached_lines = 0;
if (fd < 0)
return -errno;
if (vt <= 0) {
0
};
return -errno;
}
return -errno;
return 0;
}
assert(f);
size_t k;
if (t != USEC_INFINITY) {
return -ETIMEDOUT;
}
}
if (k <= 0)
return -EIO;
if (need_nl)
*need_nl = c != '\n';
*ret = c;
return 0;
}
}
if (t != USEC_INFINITY) {
return -ETIMEDOUT;
}
errno = 0;
return -EBADMSG;
if (need_nl)
*need_nl = false;
return 0;
}
int r;
for (;;) {
char c;
bool need_nl = true;
if (on_tty())
if (on_tty())
if (r < 0) {
if (r == -EBADMSG) {
puts("Bad input, please try again.");
continue;
}
putchar('\n');
return r;
}
if (need_nl)
putchar('\n');
*ret = c;
return 0;
}
puts("Read unexpected character, please try again.");
}
}
for (;;) {
if (on_tty())
if (on_tty())
errno = 0;
putchar('\n');
else {
char *s;
continue;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
}
}
int r = 0;
/* Set terminal to some sane defaults */
/* We leave locked terminal attributes untouched, so that
* Plymouth may set whatever it wants to set, and we don't
* interfere with that. */
/* Disable exclusive mode, just in case */
/* Switch to text mode */
if (switch_to_text)
/* Enable console unicode mode */
r = -errno;
goto finish;
}
/* We only reset the stuff that matters to the software. How
* hardware is set up we don't touch assuming that somebody
* else will do that for us */
r = -errno;
/* Just in case, flush all crap out */
return r;
}
/* We open the terminal with O_NONBLOCK here, to ensure we
* don't block on carrier if this is a terminal with carrier
* configured. */
if (fd < 0)
return fd;
return reset_terminal_fd(fd, true);
}
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.
*
*/
return -EINVAL;
for (;;) {
if (fd >= 0)
break;
return -errno;
/* Max 1s in total */
if (c >= 20)
return -errno;
c++;
}
if (r < 0) {
safe_close(fd);
return -errno;
}
if (!r) {
safe_close(fd);
return -ENOTTY;
}
return fd;
}
int acquire_terminal(
const char *name,
bool fail,
bool force,
bool ignore_tiocstty_eperm,
/* We use inotify to be notified when the tty is closed. We
* create the watch before checking if we can actually acquire
* it, so that we don't lose any event.
*
* Note: strictly speaking this actually watches for the
* device being closed, it does *not* really watch whether a
* tty loses its controlling process. However, unless some
* its tty otherwise this will not become a problem. As long
* as the administrator makes sure not configure any service
* on the same tty as an untrusted user this should not be a
* problem. (Which he probably should not do anyway.) */
if (timeout != USEC_INFINITY)
if (notify < 0) {
r = -errno;
goto fail;
}
if (wd < 0) {
r = -errno;
goto fail;
}
}
for (;;) {
.sa_handler = SIG_IGN,
.sa_flags = SA_RESTART,
};
if (notify >= 0) {
if (r < 0)
goto fail;
}
/* We pass here O_NOCTTY only so that we can check the return
* value TIOCSCTTY and have a reliable way to figure out if we
* successfully became the controlling process of the tty */
if (fd < 0)
return fd;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* if we already own the tty. */
/* First, try to get the tty */
r = -errno;
/* Sometimes, it makes sense to ignore TIOCSCTTY
* returning EPERM, i.e. when very likely we already
* are have this controlling terminal. */
if (r < 0 && r == -EPERM && ignore_tiocstty_eperm)
r = 0;
goto fail;
if (r >= 0)
break;
for (;;) {
struct inotify_event *e;
ssize_t l;
if (timeout != USEC_INFINITY) {
usec_t n;
n = now(CLOCK_MONOTONIC);
r = -ETIMEDOUT;
goto fail;
}
if (r < 0)
goto fail;
if (r == 0) {
r = -ETIMEDOUT;
goto fail;
}
}
if (l < 0) {
continue;
r = -errno;
goto fail;
}
r = -EIO;
goto fail;
}
}
break;
}
/* We close the tty fd here since if the old session
* ended our handle will be dead. It's important that
* we do this after sleeping, so that we don't enter
* an endless loop. */
}
return fd;
fail:
safe_close(fd);
return r;
}
int release_terminal(void) {
.sa_handler = SIG_IGN,
.sa_flags = SA_RESTART,
};
int r = 0;
if (fd < 0)
return -errno;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* by our own TIOCNOTTY */
r = -errno;
return r;
}
return -errno;
return 0;
}
if (fd < 0)
return fd;
return terminal_vhangup_fd(fd);
}
unsigned u;
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) */
return -EINVAL;
/* So this is not a VT. I guess we cannot deallocate
* it then. But let's at least clear the screen */
if (fd < 0)
return fd;
"\033[r" /* clear scrolling region */
"\033[H" /* move home */
"\033[2J", /* clear screen */
10, false);
return 0;
}
return -EINVAL;
if (r < 0)
return r;
if (u <= 0)
return -EINVAL;
/* Try to deallocate */
if (fd < 0)
return fd;
if (r >= 0)
return 0;
return -errno;
/* Couldn't deallocate, so let's clear it fully with
* scrollback */
if (fd < 0)
return fd;
"\033[r" /* clear scrolling region */
"\033[H" /* move home */
"\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
10, false);
return 0;
}
int make_console_stdio(void) {
int fd, r;
if (fd < 0)
r = reset_terminal_fd(fd, true);
if (r < 0)
log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
r = make_stdio(fd);
if (r < 0)
return log_error_errno(r, "Failed to duplicate terminal fd: %m");
return 0;
}
return vtnr_from_tty(tty) >= 0;
}
tty += 5;
}
int i, r;
tty += 5;
return -EINVAL;
return -EINVAL;
if (r < 0)
return r;
if (i < 0 || i > 63)
return -EINVAL;
return i;
}
char *tty;
* (i.e. not read-only-mounted which is a sign for container setups) */
if (path_is_read_only_fs("/sys") > 0)
return NULL;
return NULL;
/* If multiple log outputs are configured the last one is what
if (tty)
tty++;
else
char *tmp;
/* Get the active VC (e.g. tty1) */
}
}
return tty;
}
tty += 5;
if (!tty)
return false;
}
}
}
return -errno;
return -EIO;
}
unsigned columns(void) {
const char *e;
int c;
if (_likely_(cached_columns > 0))
return cached_columns;
c = 0;
e = getenv("COLUMNS");
if (e)
(void) safe_atoi(e, &c);
if (c <= 0)
c = fd_columns(STDOUT_FILENO);
if (c <= 0)
c = 80;
cached_columns = c;
return cached_columns;
}
return -errno;
return -EIO;
}
unsigned lines(void) {
const char *e;
int l;
if (_likely_(cached_lines > 0))
return cached_lines;
l = 0;
e = getenv("LINES");
if (e)
(void) safe_atoi(e, &l);
if (l <= 0)
l = fd_lines(STDOUT_FILENO);
if (l <= 0)
l = 24;
cached_lines = l;
return cached_lines;
}
/* intended to be used as a SIGWINCH sighandler */
cached_columns = 0;
cached_lines = 0;
}
bool on_tty(void) {
if (_unlikely_(cached_on_tty < 0))
return cached_on_tty;
}
int r, s, t;
if (fd >= 3)
safe_close(fd);
if (r < 0 || s < 0 || t < 0)
return -errno;
/* Explicitly unset O_CLOEXEC, since if fd was < 3, then
* dup2() was a NOP and the bit hence possibly set. */
fd_cloexec(STDIN_FILENO, false);
fd_cloexec(STDOUT_FILENO, false);
fd_cloexec(STDERR_FILENO, false);
return 0;
}
int make_null_stdio(void) {
int null_fd;
if (null_fd < 0)
return -errno;
return make_stdio(null_fd);
}
size_t l = 100;
int r;
for (;;) {
char path[l];
if (r == 0) {
const char *p;
char *c;
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
if (r != ERANGE)
return -r;
l *= 2;
}
return 0;
}
int k;
char *s = NULL;
k = getttyname_malloc(fd, &s);
if (k < 0)
return k;
if (streq(s, "tty")) {
free(s);
}
*r = s;
return 0;
}
int r;
const char *p;
unsigned long ttynr;
r = read_one_line_file(p, &line);
if (r < 0)
return r;
if (!p)
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%*d " /* ppid */
"%*d " /* pgrp */
"%*d " /* session */
"%lu ", /* ttynr */
&ttynr) != 1)
return -EIO;
return -ENXIO;
if (d)
return 0;
}
_cleanup_free_ char *s = NULL;
const char *p;
int k;
assert(r);
if (k < 0)
return k;
k = readlink_malloc(fn, &s);
if (k < 0) {
if (k != -ENOENT)
return k;
/* This is an ugly hack */
return -ENOMEM;
} else {
/* Probably something like the ptys which have no
* vaguely useful. */
if (!b)
return -ENOMEM;
}
} else {
if (startswith(s, "/dev/"))
p = s + 5;
else if (startswith(s, "../"))
p = s + 3;
else
p = s;
b = strdup(p);
if (!b)
return -ENOMEM;
}
*r = b;
if (_devnr)
return 0;
}
size_t l = 100;
for (;;) {
char *c;
c = new(char, l);
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
free(c);
return -errno;
}
free(c);
l *= 2;
}
}
/* Like ptsname(), but doesn't assume that the path is
* accessible in the local namespace. */
if (r < 0)
return -errno;
if (no < 0)
return -EIO;
return -ENOMEM;
return 0;
}
int r;
if (r < 0)
return r;
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
int master;
if (r < 0)
if (master < 0)
}
if (r < 0)
return r;
return -EIO;
return receive_one_fd(pair[0], 0);
}
int r;
if (r < 0)
return r;
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
int master;
if (r < 0)
if (master < 0)
}
if (r < 0)
return r;
return -EIO;
return receive_one_fd(pair[0], 0);
}
bool colors_enabled(void) {
const char *colors;
if (!colors) {
return false;
return on_tty();
}
return parse_boolean(colors) != 0;
}