kdelibs/kpty/kptyprocess.cpp

144 lines
3.4 KiB
C++
Raw Normal View History

2014-11-13 01:04:59 +02:00
/*
This file is part of the KDE libraries
Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kptyprocess.h"
#include "kprocess_p.h"
#include <kuser.h>
#include <kptydevice.h>
#include <stdlib.h>
#include <unistd.h>
//////////////////
// private data //
//////////////////
class KPtyProcessPrivate : public KProcessPrivate
{
public:
KPtyProcessPrivate()
: pty(nullptr),
ptyChannels(KPtyProcess::NoChannels),
2014-11-13 01:04:59 +02:00
addUtmp(false)
{
}
void _k_onStateChanged(QProcess::ProcessState newState)
{
if (newState == QProcess::NotRunning && addUtmp) {
2014-11-13 01:04:59 +02:00
pty->logout();
}
2014-11-13 01:04:59 +02:00
}
KPtyDevice *pty;
KPtyProcess::PtyChannels ptyChannels;
bool addUtmp;
2014-11-13 01:04:59 +02:00
};
KPtyProcess::KPtyProcess(QObject *parent) :
KProcess(new KPtyProcessPrivate, parent)
{
Q_D(KPtyProcess);
d->pty = new KPtyDevice(this);
d->pty->open();
connect(
this, SIGNAL(stateChanged(QProcess::ProcessState)),
SLOT(_k_onStateChanged(QProcess::ProcessState))
);
2014-11-13 01:04:59 +02:00
}
KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) :
KProcess(new KPtyProcessPrivate, parent)
{
Q_D(KPtyProcess);
d->pty = new KPtyDevice(this);
d->pty->open(ptyMasterFd);
connect(
this, SIGNAL(stateChanged(QProcess::ProcessState)),
SLOT(_k_onStateChanged(QProcess::ProcessState))
);
2014-11-13 01:04:59 +02:00
}
KPtyProcess::~KPtyProcess()
{
Q_D(KPtyProcess);
if (state() != QProcess::NotRunning && d->addUtmp) {
disconnect(
SIGNAL(stateChanged(QProcess::ProcessState)),
this, SLOT(_k_onStateChanged(QProcess::ProcessState))
);
d->pty->logout();
2014-11-13 01:04:59 +02:00
}
delete d->pty;
}
void KPtyProcess::setPtyChannels(PtyChannels channels)
{
Q_D(KPtyProcess);
d->ptyChannels = channels;
}
KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const
{
Q_D(const KPtyProcess);
return d->ptyChannels;
}
void KPtyProcess::setUseUtmp(bool value)
{
Q_D(KPtyProcess);
d->addUtmp = value;
}
bool KPtyProcess::isUseUtmp() const
{
Q_D(const KPtyProcess);
return d->addUtmp;
}
KPtyDevice *KPtyProcess::pty() const
{
Q_D(const KPtyProcess);
return d->pty;
}
void KPtyProcess::setupChildProcess()
{
Q_D(KPtyProcess);
d->pty->setCTty();
if (d->addUtmp)
d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY"));
if (d->ptyChannels & StdinChannel) {
::dup2(d->pty->slaveFd(), 0);
}
if (d->ptyChannels & StdoutChannel) {
::dup2(d->pty->slaveFd(), 1);
}
if (d->ptyChannels & StderrChannel) {
::dup2(d->pty->slaveFd(), 2);
}
2014-11-13 01:04:59 +02:00
KProcess::setupChildProcess();
}
#include "moc_kptyprocess.cpp"