KPty

kptyprocess.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kptyprocess.h"
9
10#include <kptydevice.h>
11#include <kuser.h>
12
13#include <stdlib.h>
14#include <unistd.h>
15
16//////////////////
17// private data //
18//////////////////
19
20class KPtyProcessPrivate
21{
22public:
23 KPtyProcessPrivate()
24 {
25 }
26
27 std::unique_ptr<KPtyDevice> pty;
29 bool addUtmp = false;
30};
31
33 : KPtyProcess(-1, parent)
34{
35}
36
37KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent)
38 : KProcess(parent)
39 , d_ptr(new KPtyProcessPrivate)
40{
42
43 auto parentChildProcModifier = KProcess::childProcessModifier();
44 setChildProcessModifier([d, parentChildProcModifier]() {
45 d->pty->setCTty();
46 if (d->addUtmp) {
47 d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().constData(), qgetenv("DISPLAY").constData());
48 }
49 if (d->ptyChannels & StdinChannel) {
50 dup2(d->pty->slaveFd(), 0);
51 }
52 if (d->ptyChannels & StdoutChannel) {
53 dup2(d->pty->slaveFd(), 1);
54 }
55 if (d->ptyChannels & StderrChannel) {
56 dup2(d->pty->slaveFd(), 2);
57 }
58
59 if (parentChildProcModifier) {
60 parentChildProcModifier();
61 }
62 });
63
64 d->pty = std::make_unique<KPtyDevice>(this);
65
66 if (ptyMasterFd == -1) {
67 d->pty->open();
68 } else {
69 d->pty->open(ptyMasterFd);
70 }
71
73 if (state == QProcess::NotRunning && d_ptr->addUtmp) {
74 d_ptr->pty->logout();
75 }
76 });
77}
78
80{
82
83 if (state() != QProcess::NotRunning && d->addUtmp) {
84 d->pty->logout();
85 disconnect(this, &QProcess::stateChanged, this, nullptr);
86 }
87}
88
90{
92
93 d->ptyChannels = channels;
94}
95
97{
98 Q_D(const KPtyProcess);
99
100 return d->ptyChannels;
101}
102
104{
106
107 d->addUtmp = value;
108}
109
111{
112 Q_D(const KPtyProcess);
113
114 return d->addUtmp;
115}
116
118{
119 Q_D(const KPtyProcess);
120
121 return d->pty.get();
122}
123
124#include "moc_kptyprocess.cpp"
Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc.
Definition kptydevice.h:21
This class extends KProcess by support for PTYs (pseudo TTYs).
Definition kptyprocess.h:60
bool isUseUtmp() const
Get whether to register the process as a TTY login in utmp.
PtyChannels ptyChannels() const
Query to which channels the PTY is assigned.
void setUseUtmp(bool value)
Set whether to register the process as a TTY login in utmp.
void setPtyChannels(PtyChannels channels)
Set to which channels the PTY should be assigned.
KPtyDevice * pty() const
Get the PTY device of this process.
KPtyProcess(QObject *parent=nullptr)
Constructor.
~KPtyProcess() override
Destructor.
@ StdinChannel
Connect PTY to stdin.
Definition kptyprocess.h:70
@ StdoutChannel
Connect PTY to stdout.
Definition kptyprocess.h:71
@ NoChannels
The PTY is not connected to any channel.
Definition kptyprocess.h:69
@ StderrChannel
Connect PTY to stderr.
Definition kptyprocess.h:72
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
std::function< void()> childProcessModifier()> childProcessModifier() const const
void setChildProcessModifier(const std::function< void()> &modifier)
QProcess::ProcessState state() const const
void stateChanged(QProcess::ProcessState newState)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:52:53 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.