2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2015-03-31 07:30:19 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
|
2015-03-31 07:30:19 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2015-03-31 07:30:19 +00:00
|
|
|
#include "virtual_terminal.h"
|
|
|
|
// kwin
|
|
|
|
#include "logind.h"
|
2015-04-15 15:47:56 +00:00
|
|
|
#include "main.h"
|
2015-03-31 07:30:19 +00:00
|
|
|
#include "utils.h"
|
|
|
|
// Qt
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
// linux
|
2020-05-31 21:35:34 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
2015-03-31 07:30:19 +00:00
|
|
|
#include <linux/major.h>
|
2015-04-15 15:52:14 +00:00
|
|
|
#include <linux/kd.h>
|
2015-03-31 07:30:19 +00:00
|
|
|
#include <linux/vt.h>
|
2020-05-31 21:35:34 +00:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_FREEBSD
|
|
|
|
#include <sys/consio.h>
|
|
|
|
#endif
|
2015-03-31 07:30:19 +00:00
|
|
|
// system
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/signalfd.h>
|
|
|
|
#include <sys/stat.h>
|
2019-07-09 19:19:26 +00:00
|
|
|
// c++
|
|
|
|
#include <csignal>
|
2015-03-31 07:30:19 +00:00
|
|
|
|
|
|
|
#define RELEASE_SIGNAL SIGUSR1
|
|
|
|
#define ACQUISITION_SIGNAL SIGUSR2
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
KWIN_SINGLETON_FACTORY(VirtualTerminal)
|
|
|
|
|
|
|
|
VirtualTerminal::VirtualTerminal(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualTerminal::init()
|
|
|
|
{
|
|
|
|
auto logind = LogindIntegration::self();
|
|
|
|
if (logind->vt() != -1) {
|
|
|
|
setup(logind->vt());
|
|
|
|
}
|
|
|
|
connect(logind, &LogindIntegration::virtualTerminalChanged, this, &VirtualTerminal::setup);
|
|
|
|
if (logind->isConnected()) {
|
|
|
|
logind->takeControl();
|
|
|
|
} else {
|
|
|
|
connect(logind, &LogindIntegration::connectedChanged, logind, &LogindIntegration::takeControl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualTerminal::~VirtualTerminal()
|
|
|
|
{
|
|
|
|
s_self = nullptr;
|
|
|
|
closeFd();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isTty(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-31 21:35:34 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
// Not a TTY device or weird vt number, skip it
|
2015-03-31 07:30:19 +00:00
|
|
|
if (major(st.st_rdev) != TTY_MAJOR || minor (st.st_rdev) <= 0 || minor(st.st_rdev) >= 64) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-31 21:35:34 +00:00
|
|
|
#endif
|
|
|
|
// FreeBSD doesn't have a notion of major device number, so nothing
|
|
|
|
// to check. isatty() might not do the trick.
|
2015-03-31 07:30:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualTerminal::setup(int vtNr)
|
|
|
|
{
|
|
|
|
if (m_vt != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (vtNr == -1) {
|
|
|
|
// error condition
|
|
|
|
return;
|
|
|
|
}
|
2020-05-31 21:52:28 +00:00
|
|
|
QString ttyName = QStringLiteral(KWIN_TTY_PREFIX "%1").arg(vtNr);
|
2015-03-31 07:30:19 +00:00
|
|
|
|
|
|
|
m_vt = open(ttyName.toUtf8().constData(), O_RDWR|O_CLOEXEC|O_NONBLOCK);
|
|
|
|
if (m_vt < 0) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to open tty" << vtNr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isTty(m_vt)) {
|
|
|
|
qCWarning(KWIN_CORE) << vtNr << " is no tty";
|
|
|
|
closeFd();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 15:52:14 +00:00
|
|
|
if (ioctl(m_vt, KDSETMODE, KD_GRAPHICS) < 0) {
|
|
|
|
qCWarning(KWIN_CORE()) << "Failed to set tty " << vtNr << " in graphics mode";
|
|
|
|
closeFd();
|
|
|
|
return;
|
|
|
|
}
|
2015-03-31 07:30:19 +00:00
|
|
|
if (!createSignalHandler()) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to create signalfd";
|
|
|
|
closeFd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vt_mode mode = {
|
|
|
|
VT_PROCESS,
|
|
|
|
0,
|
|
|
|
RELEASE_SIGNAL,
|
|
|
|
ACQUISITION_SIGNAL,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
if (ioctl(m_vt, VT_SETMODE, &mode) < 0) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to take over virtual terminal";
|
|
|
|
closeFd();
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 09:49:41 +00:00
|
|
|
m_vtNumber = vtNr;
|
2015-03-31 07:30:19 +00:00
|
|
|
setActive(true);
|
2015-04-15 15:47:56 +00:00
|
|
|
emit kwinApp()->virtualTerminalCreated();
|
2015-03-31 07:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualTerminal::closeFd()
|
|
|
|
{
|
|
|
|
if (m_vt < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_notifier) {
|
|
|
|
const int sfd = m_notifier->socket();
|
|
|
|
delete m_notifier;
|
|
|
|
m_notifier = nullptr;
|
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
close(m_vt);
|
|
|
|
m_vt = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualTerminal::createSignalHandler()
|
|
|
|
{
|
|
|
|
if (m_notifier) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, RELEASE_SIGNAL);
|
|
|
|
sigaddset(&mask, ACQUISITION_SIGNAL);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, nullptr);
|
|
|
|
|
|
|
|
const int fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
|
|
|
|
connect(m_notifier, &QSocketNotifier::activated, this,
|
|
|
|
[this] {
|
|
|
|
if (m_vt < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
signalfd_siginfo sigInfo;
|
|
|
|
if (read(m_notifier->socket(), &sigInfo, sizeof(sigInfo)) != sizeof(sigInfo)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (sigInfo.ssi_signo) {
|
|
|
|
case RELEASE_SIGNAL:
|
|
|
|
setActive(false);
|
2015-04-15 15:53:04 +00:00
|
|
|
ioctl(m_vt, VT_RELDISP, 1);
|
2015-03-31 07:30:19 +00:00
|
|
|
break;
|
|
|
|
case ACQUISITION_SIGNAL:
|
|
|
|
ioctl(m_vt, VT_RELDISP, VT_ACKACQ);
|
|
|
|
setActive(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualTerminal::activate(int vt)
|
|
|
|
{
|
|
|
|
if (m_vt < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 09:49:41 +00:00
|
|
|
if (vt == m_vtNumber) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-31 07:30:19 +00:00
|
|
|
ioctl(m_vt, VT_ACTIVATE, vt);
|
|
|
|
setActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualTerminal::setActive(bool active)
|
|
|
|
{
|
|
|
|
if (m_active == active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_active = active;
|
|
|
|
emit activeChanged(m_active);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|