1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "sshremoteprocessrunner.h"
27 
28 #include "sshconnectionmanager.h"
29 
30 #include <utils/qtcassert.h>
31 
32 /*!
33     \class QSsh::SshRemoteProcessRunner
34 
35     \brief The SshRemoteProcessRunner class is a convenience class for
36     running a remote process over an SSH connection.
37 */
38 
39 namespace QSsh {
40 namespace Internal {
41 namespace {
42 enum State { Inactive, Connecting, Connected, ProcessRunning };
43 } // anonymous namespace
44 
45 class SshRemoteProcessRunnerPrivate
46 {
47 public:
SshRemoteProcessRunnerPrivate()48     SshRemoteProcessRunnerPrivate() : m_state(Inactive) {}
49 
50     SshRemoteProcessPtr m_process;
51     SshConnection *m_connection;
52     bool m_runInTerminal;
53     QString m_command;
54     QString m_lastConnectionErrorString;
55     QProcess::ExitStatus m_exitStatus;
56     QByteArray m_stdout;
57     QByteArray m_stderr;
58     int m_exitCode;
59     QString m_processErrorString;
60     State m_state;
61 };
62 
63 } // namespace Internal
64 
65 using namespace Internal;
66 
SshRemoteProcessRunner(QObject * parent)67 SshRemoteProcessRunner::SshRemoteProcessRunner(QObject *parent)
68     : QObject(parent), d(new SshRemoteProcessRunnerPrivate)
69 {
70 }
71 
~SshRemoteProcessRunner()72 SshRemoteProcessRunner::~SshRemoteProcessRunner()
73 {
74     disconnect();
75     setState(Inactive);
76     delete d;
77 }
78 
run(const QString & command,const SshConnectionParameters & sshParams)79 void SshRemoteProcessRunner::run(const QString &command, const SshConnectionParameters &sshParams)
80 {
81     QTC_ASSERT(d->m_state == Inactive, return);
82 
83     d->m_runInTerminal = false;
84     runInternal(command, sshParams);
85 }
86 
runInTerminal(const QString & command,const SshConnectionParameters & sshParams)87 void SshRemoteProcessRunner::runInTerminal(const QString &command,
88                                            const SshConnectionParameters &sshParams)
89 {
90     d->m_runInTerminal = true;
91     runInternal(command, sshParams);
92 }
93 
runInternal(const QString & command,const SshConnectionParameters & sshParams)94 void SshRemoteProcessRunner::runInternal(const QString &command,
95     const SshConnectionParameters &sshParams)
96 {
97     setState(Connecting);
98 
99     d->m_lastConnectionErrorString.clear();
100     d->m_processErrorString.clear();
101     d->m_exitCode = -1;
102     d->m_command = command;
103     d->m_connection = QSsh::acquireConnection(sshParams);
104     connect(d->m_connection, &SshConnection::errorOccurred,
105             this, &SshRemoteProcessRunner::handleConnectionError);
106     connect(d->m_connection, &SshConnection::disconnected,
107             this, &SshRemoteProcessRunner::handleDisconnected);
108     if (d->m_connection->state() == SshConnection::Connected) {
109         handleConnected();
110     } else {
111         connect(d->m_connection, &SshConnection::connected, this, &SshRemoteProcessRunner::handleConnected);
112         if (d->m_connection->state() == SshConnection::Unconnected)
113             d->m_connection->connectToHost();
114     }
115 }
116 
handleConnected()117 void SshRemoteProcessRunner::handleConnected()
118 {
119     QTC_ASSERT(d->m_state == Connecting, return);
120     setState(Connected);
121 
122     d->m_process = d->m_connection->createRemoteProcess(d->m_command);
123     connect(d->m_process.get(), &SshRemoteProcess::started,
124             this, &SshRemoteProcessRunner::handleProcessStarted);
125     connect(d->m_process.get(), &SshRemoteProcess::done,
126             this, &SshRemoteProcessRunner::handleProcessFinished);
127     connect(d->m_process.get(), &SshRemoteProcess::readyReadStandardOutput,
128             this, &SshRemoteProcessRunner::handleStdout);
129     connect(d->m_process.get(), &SshRemoteProcess::readyReadStandardError,
130             this, &SshRemoteProcessRunner::handleStderr);
131     if (d->m_runInTerminal)
132         d->m_process->requestTerminal();
133     d->m_process->start();
134 }
135 
handleConnectionError()136 void SshRemoteProcessRunner::handleConnectionError()
137 {
138     d->m_lastConnectionErrorString = d->m_connection->errorString();
139     handleDisconnected();
140     emit connectionError();
141 }
142 
handleDisconnected()143 void SshRemoteProcessRunner::handleDisconnected()
144 {
145     QTC_ASSERT(d->m_state == Connecting || d->m_state == Connected || d->m_state == ProcessRunning,
146                return);
147     setState(Inactive);
148 }
149 
handleProcessStarted()150 void SshRemoteProcessRunner::handleProcessStarted()
151 {
152     QTC_ASSERT(d->m_state == Connected, return);
153 
154     setState(ProcessRunning);
155     emit processStarted();
156 }
157 
handleProcessFinished(const QString & error)158 void SshRemoteProcessRunner::handleProcessFinished(const QString &error)
159 {
160     d->m_exitStatus = d->m_process->exitStatus();
161     d->m_exitCode = d->m_process->exitCode();
162     d->m_processErrorString = error;
163     setState(Inactive);
164     emit processClosed(d->m_processErrorString);
165 }
166 
handleStdout()167 void SshRemoteProcessRunner::handleStdout()
168 {
169     d->m_stdout += d->m_process->readAllStandardOutput();
170     emit readyReadStandardOutput();
171 }
172 
handleStderr()173 void SshRemoteProcessRunner::handleStderr()
174 {
175     d->m_stderr += d->m_process->readAllStandardError();
176     emit readyReadStandardError();
177 }
178 
setState(int newState)179 void SshRemoteProcessRunner::setState(int newState)
180 {
181     if (d->m_state == newState)
182         return;
183 
184     d->m_state = static_cast<State>(newState);
185     if (d->m_state == Inactive) {
186         if (d->m_process) {
187             disconnect(d->m_process.get(), nullptr, this, nullptr);
188             d->m_process->terminate();
189             d->m_process.release()->deleteLater();
190         }
191         if (d->m_connection) {
192             disconnect(d->m_connection, nullptr, this, nullptr);
193             QSsh::releaseConnection(d->m_connection);
194             d->m_connection = nullptr;
195         }
196     }
197 }
198 
command() const199 QString SshRemoteProcessRunner::command() const { return d->m_command; }
lastConnectionErrorString() const200 QString SshRemoteProcessRunner::lastConnectionErrorString() const {
201     return d->m_lastConnectionErrorString;
202 }
203 
isProcessRunning() const204 bool SshRemoteProcessRunner::isProcessRunning() const
205 {
206     return d->m_process && d->m_process->isRunning();
207 }
208 
processExitStatus() const209 SshRemoteProcess::ExitStatus SshRemoteProcessRunner::processExitStatus() const
210 {
211     QTC_CHECK(!isProcessRunning());
212     return d->m_exitStatus;
213 }
214 
processExitCode() const215 int SshRemoteProcessRunner::processExitCode() const
216 {
217     QTC_CHECK(processExitStatus() == SshRemoteProcess::NormalExit);
218     return d->m_exitCode;
219 }
220 
processErrorString() const221 QString SshRemoteProcessRunner::processErrorString() const
222 {
223     return d->m_processErrorString;
224 }
225 
readAllStandardOutput()226 QByteArray SshRemoteProcessRunner::readAllStandardOutput()
227 {
228     const QByteArray data = d->m_stdout;
229     d->m_stdout.clear();
230     return data;
231 }
232 
readAllStandardError()233 QByteArray SshRemoteProcessRunner::readAllStandardError()
234 {
235     const QByteArray data = d->m_stderr;
236     d->m_stderr.clear();
237     return data;
238 }
239 
writeDataToProcess(const QByteArray & data)240 void SshRemoteProcessRunner::writeDataToProcess(const QByteArray &data)
241 {
242     QTC_CHECK(isProcessRunning());
243     d->m_process->write(data);
244 }
245 
cancel()246 void SshRemoteProcessRunner::cancel()
247 {
248     setState(Inactive);
249 }
250 
251 } // namespace QSsh
252