1 | /* |
---|
2 | * This file is a part of QTerminal - http://gitorious.org/qterminal |
---|
3 | * |
---|
4 | * This file was un-linked from KDE and modified |
---|
5 | * by Maxim Bourmistrov <maxim@unixconn.com> |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | This file is part of the KDE libraries |
---|
11 | |
---|
12 | Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org> |
---|
13 | |
---|
14 | This library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU Library General Public |
---|
16 | License as published by the Free Software Foundation; either |
---|
17 | version 2 of the License, or (at your option) any later version. |
---|
18 | |
---|
19 | This library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | Library General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU Library General Public License |
---|
25 | along with this library; see the file COPYING.LIB. If not, write to |
---|
26 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
27 | Boston, MA 02110-1301, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | |
---|
31 | #include "kptyprocess.h" |
---|
32 | #include "kprocess.h" |
---|
33 | #include "kptydevice.h" |
---|
34 | |
---|
35 | #include <cstdlib> |
---|
36 | #include <unistd.h> |
---|
37 | #include <csignal> |
---|
38 | #include <QDebug> |
---|
39 | |
---|
40 | KPtyProcess::KPtyProcess(QObject *parent) : |
---|
41 | KPtyProcess(-1, parent) |
---|
42 | { |
---|
43 | Q_D(KPtyProcess); |
---|
44 | |
---|
45 | d->pty = std::make_unique<KPtyDevice>(this); |
---|
46 | d->pty->open(); |
---|
47 | connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), |
---|
48 | SLOT(_k_onStateChanged(QProcess::ProcessState))); |
---|
49 | |
---|
50 | setChildProcessModifier([this] { |
---|
51 | Q_D(KPtyProcess); |
---|
52 | d->pty->setCTty(); |
---|
53 | |
---|
54 | #if 0 |
---|
55 | if (d->addUtmp) |
---|
56 | d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY")); |
---|
57 | #endif |
---|
58 | if (d->ptyChannels & StdinChannel) |
---|
59 | dup2(d->pty->slaveFd(), 0); |
---|
60 | |
---|
61 | if (d->ptyChannels & StdoutChannel) |
---|
62 | dup2(d->pty->slaveFd(), 1); |
---|
63 | |
---|
64 | if (d->ptyChannels & StderrChannel) |
---|
65 | dup2(d->pty->slaveFd(), 2); |
---|
66 | }); |
---|
67 | } |
---|
68 | |
---|
69 | KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) : |
---|
70 | KProcess(parent), |
---|
71 | d_ptr(new KPtyProcessPrivate) |
---|
72 | { |
---|
73 | Q_D(KPtyProcess); |
---|
74 | |
---|
75 | d->pty = std::make_unique<KPtyDevice>(this); |
---|
76 | |
---|
77 | if (ptyMasterFd == -1) { |
---|
78 | d->pty->open(); |
---|
79 | } else { |
---|
80 | d->pty->open(ptyMasterFd); |
---|
81 | } |
---|
82 | |
---|
83 | connect(this, &QProcess::stateChanged, this, [this](QProcess::ProcessState state) { |
---|
84 | if (state == QProcess::NotRunning && d_ptr->addUtmp) { |
---|
85 | d_ptr->pty->logout(); |
---|
86 | } |
---|
87 | }); |
---|
88 | } |
---|
89 | |
---|
90 | KPtyProcess::~KPtyProcess() |
---|
91 | { |
---|
92 | Q_D(KPtyProcess); |
---|
93 | |
---|
94 | if (state() != QProcess::NotRunning) |
---|
95 | { |
---|
96 | if (d->addUtmp) |
---|
97 | { |
---|
98 | d->pty->logout(); |
---|
99 | disconnect(this, &QProcess::stateChanged, this, nullptr); |
---|
100 | } |
---|
101 | } |
---|
102 | waitForFinished(300); // give it some time to finish |
---|
103 | if (state() != QProcess::NotRunning) |
---|
104 | { |
---|
105 | qWarning() << Q_FUNC_INFO << "the terminal process is still running, trying to stop it by SIGHUP"; |
---|
106 | ::kill(static_cast<pid_t>(processId()), SIGHUP); |
---|
107 | waitForFinished(300); |
---|
108 | if (state() != QProcess::NotRunning) |
---|
109 | qCritical() << Q_FUNC_INFO << "process didn't stop upon SIGHUP and will be SIGKILL-ed"; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void KPtyProcess::setPtyChannels(PtyChannels channels) |
---|
114 | { |
---|
115 | Q_D(KPtyProcess); |
---|
116 | |
---|
117 | d->ptyChannels = channels; |
---|
118 | } |
---|
119 | |
---|
120 | KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const |
---|
121 | { |
---|
122 | Q_D(const KPtyProcess); |
---|
123 | |
---|
124 | return d->ptyChannels; |
---|
125 | } |
---|
126 | |
---|
127 | void KPtyProcess::setUseUtmp(bool value) |
---|
128 | { |
---|
129 | Q_D(KPtyProcess); |
---|
130 | |
---|
131 | d->addUtmp = value; |
---|
132 | } |
---|
133 | |
---|
134 | bool KPtyProcess::isUseUtmp() const |
---|
135 | { |
---|
136 | Q_D(const KPtyProcess); |
---|
137 | |
---|
138 | return d->addUtmp; |
---|
139 | } |
---|
140 | |
---|
141 | KPtyDevice *KPtyProcess::pty() const |
---|
142 | { |
---|
143 | Q_D(const KPtyProcess); |
---|
144 | |
---|
145 | return d->pty.get(); |
---|
146 | } |
---|
147 | |
---|
148 | #if QT_VERSION < 0x060000 |
---|
149 | void KPtyProcess::setupChildProcess() |
---|
150 | { |
---|
151 | Q_D(KPtyProcess); |
---|
152 | |
---|
153 | d->pty->setCTty(); |
---|
154 | |
---|
155 | #if 0 |
---|
156 | if (d->addUtmp) |
---|
157 | d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY")); |
---|
158 | #endif |
---|
159 | if (d->ptyChannels & StdinChannel) |
---|
160 | dup2(d->pty->slaveFd(), 0); |
---|
161 | |
---|
162 | if (d->ptyChannels & StdoutChannel) |
---|
163 | dup2(d->pty->slaveFd(), 1); |
---|
164 | |
---|
165 | if (d->ptyChannels & StderrChannel) |
---|
166 | dup2(d->pty->slaveFd(), 2); |
---|
167 | |
---|
168 | KProcess::setupChildProcess(); |
---|
169 | } |
---|
170 | #endif |
---|
171 | |
---|
172 | //#include "kptyprocess.moc" |
---|