consoleprocess_unix.cpp 12.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
#include "consoleprocess_p.h"
con's avatar
con committed
31

32
33
34
#include "environment.h"
#include "qtcprocess.h"

35
36
#include <utils/hostosinfo.h>

37
38
39
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
40
#include <QTimer>
41
42
43
44
45
46
47

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

48
49
50
51
namespace Utils {

ConsoleProcessPrivate::ConsoleProcessPrivate() :
    m_mode(ConsoleProcess::Run),
52
53
    m_appPid(0),
    m_stubSocket(0),
54
    m_tempFile(0),
55
56
57
58
    m_settings(0),
    m_stubConnected(false),
    m_stubPid(0),
    m_stubConnectTimer(0)
con's avatar
con committed
59
{
60
}
61

62
63
64
65
ConsoleProcess::ConsoleProcess(QObject *parent)  :
    QObject(parent), d(new ConsoleProcessPrivate)
{
    connect(&d->m_stubServer, SIGNAL(newConnection()), SLOT(stubConnectionAvailable()));
66

67
    d->m_process.setProcessChannelMode(QProcess::ForwardedChannels);
con's avatar
con committed
68
69
}

70
71
72
73
74
void ConsoleProcess::setSettings(QSettings *settings)
{
    d->m_settings = settings;
}

75
bool ConsoleProcess::start(const QString &program, const QString &args)
con's avatar
con committed
76
{
77
    if (isRunning())
con's avatar
con committed
78
79
        return false;

80
    QtcProcess::SplitError perr;
81
    QStringList pargs = QtcProcess::prepareArgs(args, &perr, &d->m_environment, &d->m_workingDir);
82
83
84
85
86
    QString pcmd;
    if (perr == QtcProcess::SplitOk) {
        pcmd = program;
    } else {
        if (perr != QtcProcess::FoundMeta) {
87
            emit processError(tr("Quoting error in command."));
88
89
90
91
            return false;
        }
        if (d->m_mode == Debug) {
            // FIXME: QTCREATORBUG-2809
92
93
            emit processError(tr("Debugging complex shell commands in a terminal"
                                 " is currently not supported."));
94
95
96
97
98
99
100
101
            return false;
        }
        pcmd = QLatin1String("/bin/sh");
        pargs << QLatin1String("-c") << (QtcProcess::quoteArg(program) + QLatin1Char(' ') + args);
    }

    QtcProcess::SplitError qerr;
    QStringList xtermArgs = QtcProcess::prepareArgs(terminalEmulator(d->m_settings), &qerr,
102
                                                    &d->m_environment, &d->m_workingDir);
103
    if (qerr != QtcProcess::SplitOk) {
104
105
106
        emit processError(qerr == QtcProcess::BadQuoting
                          ? tr("Quoting error in terminal command.")
                          : tr("Terminal command may not be a shell command."));
107
108
109
        return false;
    }

110
    const QString err = stubServerListen();
111
    if (!err.isEmpty()) {
112
        emit processError(msgCommChannelFailed(err));
113
114
        return false;
    }
con's avatar
con committed
115

116
    QStringList env = d->m_environment.toStringList();
117
    if (!env.isEmpty()) {
118
119
        d->m_tempFile = new QTemporaryFile();
        if (!d->m_tempFile->open()) {
120
            stubServerShutdown();
121
            emit processError(msgCannotCreateTempFile(d->m_tempFile->errorString()));
122
123
            delete d->m_tempFile;
            d->m_tempFile = 0;
124
125
            return false;
        }
126
        QByteArray contents;
127
        foreach (const QString &var, env) {
128
129
130
131
132
            QByteArray l8b = var.toLocal8Bit();
            contents.append(l8b.constData(), l8b.size() + 1);
        }
        if (d->m_tempFile->write(contents) != contents.size() || !d->m_tempFile->flush()) {
            stubServerShutdown();
133
            emit processError(msgCannotWriteTempFile());
134
135
136
            delete d->m_tempFile;
            d->m_tempFile = 0;
            return false;
137
138
139
        }
    }

140
141
142
143
144
145
146
    if (Utils::HostOsInfo::isMacHost()) {
        xtermArgs << (QCoreApplication::applicationDirPath()
                      + QLatin1String("/../Resources/qtcreator_process_stub"));
    } else {
        xtermArgs << (QCoreApplication::applicationDirPath()
                      + QLatin1String("/qtcreator_process_stub"));
    }
147
    xtermArgs
148
149
              << modeOption(d->m_mode)
              << d->m_stubServer.fullServerName()
150
              << msgPromptToClose()
151
              << workingDirectory()
152
              << (d->m_tempFile ? d->m_tempFile->fileName() : QString())
153
              << pcmd << pargs;
con's avatar
con committed
154

155
    QString xterm = xtermArgs.takeFirst();
156
157
    d->m_process.start(xterm, xtermArgs);
    if (!d->m_process.waitForStarted()) {
158
        stubServerShutdown();
159
        emit processError(tr("Cannot start the terminal emulator '%1'.").arg(xterm));
160
161
        delete d->m_tempFile;
        d->m_tempFile = 0;
con's avatar
con committed
162
        return false;
163
    }
164
165
166
167
    d->m_stubConnectTimer = new QTimer(this);
    connect(d->m_stubConnectTimer, SIGNAL(timeout()), SLOT(stop()));
    d->m_stubConnectTimer->setSingleShot(true);
    d->m_stubConnectTimer->start(10000);
168
    d->m_executable = program;
con's avatar
con committed
169
170
171
    return true;
}

172
void ConsoleProcess::killProcess()
con's avatar
con committed
173
{
174
175
176
177
    if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
        d->m_stubSocket->write("k", 1);
        d->m_stubSocket->flush();
    }
178
    d->m_appPid = 0;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
}

void ConsoleProcess::killStub()
{
    if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
        d->m_stubSocket->write("s", 1);
        d->m_stubSocket->flush();
    }
    stubServerShutdown();
    d->m_stubPid = 0;
}

void ConsoleProcess::detachStub()
{
    if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
        d->m_stubSocket->write("d", 1);
        d->m_stubSocket->flush();
    }
    stubServerShutdown();
    d->m_stubPid = 0;
}

void ConsoleProcess::stop()
{
    killProcess();
    killStub();
    if (isRunning()) {
        d->m_process.terminate();
        if (!d->m_process.waitForFinished(1000)) {
            d->m_process.kill();
            d->m_process.waitForFinished();
        }
    }
con's avatar
con committed
212
213
214
215
}

bool ConsoleProcess::isRunning() const
{
216
217
    return d->m_process.state() != QProcess::NotRunning
            || (d->m_stubSocket && d->m_stubSocket->isOpen());
con's avatar
con committed
218
219
}

220
QString ConsoleProcess::stubServerListen()
con's avatar
con committed
221
{
222
223
224
225
226
227
228
    // We need to put the socket in a private directory, as some systems simply do not
    // check the file permissions of sockets.
    QString stubFifoDir;
    forever {
        {
            QTemporaryFile tf;
            if (!tf.open())
229
                return msgCannotCreateTempFile(tf.errorString());
230
            stubFifoDir = tf.fileName();
231
232
        }
        // By now the temp file was deleted again
233
234
        d->m_stubServerDir = QFile::encodeName(stubFifoDir);
        if (!::mkdir(d->m_stubServerDir.constData(), 0700))
235
236
            break;
        if (errno != EEXIST)
237
            return msgCannotCreateTempDir(stubFifoDir, QString::fromLocal8Bit(strerror(errno)));
238
    }
239
    const QString stubServer  = stubFifoDir + QLatin1String("/stub-socket");
240
241
242
    if (!d->m_stubServer.listen(stubServer)) {
        ::rmdir(d->m_stubServerDir.constData());
        return tr("Cannot create socket '%1': %2").arg(stubServer, d->m_stubServer.errorString());
243
244
245
246
247
248
    }
    return QString();
}

void ConsoleProcess::stubServerShutdown()
{
249
250
251
    if (d->m_stubSocket) {
        readStubOutput(); // we could get the shutdown signal before emptying the buffer
        d->m_stubSocket->disconnect(); // avoid getting queued readyRead signals
252
        d->m_stubSocket->deleteLater(); // we might be called from the disconnected signal of m_stubSocket
253
    }
254
255
256
257
    d->m_stubSocket = 0;
    if (d->m_stubServer.isListening()) {
        d->m_stubServer.close();
        ::rmdir(d->m_stubServerDir.constData());
258
259
260
261
262
    }
}

void ConsoleProcess::stubConnectionAvailable()
{
263
264
265
266
267
268
    if (d->m_stubConnectTimer) {
        delete d->m_stubConnectTimer;
        d->m_stubConnectTimer = 0;
    }
    d->m_stubConnected = true;
    emit stubStarted();
269
270
    d->m_stubSocket = d->m_stubServer.nextPendingConnection();
    connect(d->m_stubSocket, SIGNAL(readyRead()), SLOT(readStubOutput()));
271
    connect(d->m_stubSocket, SIGNAL(disconnected()), SLOT(stubExited()));
con's avatar
con committed
272
273
}

274
static QString errorMsg(int code)
con's avatar
con committed
275
{
276
    return QString::fromLocal8Bit(strerror(code));
con's avatar
con committed
277
278
}

279
void ConsoleProcess::readStubOutput()
con's avatar
con committed
280
{
281
282
    while (d->m_stubSocket->canReadLine()) {
        QByteArray out = d->m_stubSocket->readLine();
283
284
        out.chop(1); // \n
        if (out.startsWith("err:chdir ")) {
285
            emit processError(msgCannotChangeToWorkDir(workingDirectory(), errorMsg(out.mid(10).toInt())));
286
        } else if (out.startsWith("err:exec ")) {
287
            emit processError(msgCannotExecute(d->m_executable, errorMsg(out.mid(9).toInt())));
288
        } else if (out.startsWith("spid ")) {
289
290
            delete d->m_tempFile;
            d->m_tempFile = 0;
291

292
293
            d->m_stubPid = out.mid(4).toInt();
        } else if (out.startsWith("pid ")) {
294
            d->m_appPid = out.mid(4).toInt();
295
296
            emit processStarted();
        } else if (out.startsWith("exit ")) {
297
298
299
            d->m_appStatus = QProcess::NormalExit;
            d->m_appCode = out.mid(5).toInt();
            d->m_appPid = 0;
300
301
            emit processStopped();
        } else if (out.startsWith("crash ")) {
302
303
304
            d->m_appStatus = QProcess::CrashExit;
            d->m_appCode = out.mid(6).toInt();
            d->m_appPid = 0;
305
306
            emit processStopped();
        } else {
307
            emit processError(msgUnexpectedOutput(out));
308
            d->m_stubPid = 0;
309
            d->m_process.terminate();
310
311
312
            break;
        }
    }
con's avatar
con committed
313
314
}

315
316
317
void ConsoleProcess::stubExited()
{
    // The stub exit might get noticed before we read the error status.
318
319
    if (d->m_stubSocket && d->m_stubSocket->state() == QLocalSocket::ConnectedState)
        d->m_stubSocket->waitForDisconnected();
320
    stubServerShutdown();
321
    d->m_stubPid = 0;
322
323
324
325
326
327
    delete d->m_tempFile;
    d->m_tempFile = 0;
    if (d->m_appPid) {
        d->m_appStatus = QProcess::CrashExit;
        d->m_appCode = -1;
        d->m_appPid = 0;
328
329
        emit processStopped(); // Maybe it actually did not, but keep state consistent
    }
330
    emit stubStopped();
331
}
332

333
334
335
336
337
338
339
340
341
342
343
344
345
struct Terminal {
    const char *binary;
    const char *options;
};

static const Terminal knownTerminals[] =
{
    {"xterm", "-e"},
    {"aterm", "-e"},
    {"Eterm", "-e"},
    {"rxvt", "-e"},
    {"urxvt", "-e"},
    {"xfce4-terminal", "-x"},
346
    {"konsole", "-e"},
347
348
349
    {"gnome-terminal", "-x"}
};

350
351
QString ConsoleProcess::defaultTerminalEmulator()
{
352
353
354
355
    if (Utils::HostOsInfo::isMacHost()) {
        QString termCmd = QCoreApplication::applicationDirPath() + QLatin1String("/../Resources/scripts/openTerminal.command");
        if (QFile(termCmd).exists())
            return termCmd.replace(QLatin1Char(' '), QLatin1String("\\ "));
356
        return QLatin1String("/usr/X11/bin/xterm");
357
    }
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    const Environment env = Environment::systemEnvironment();
    const int terminalCount = int(sizeof(knownTerminals) / sizeof(knownTerminals[0]));
    for (int i = 0; i < terminalCount; ++i) {
        QString result = env.searchInPath(QLatin1String(knownTerminals[i].binary));
        if (!result.isEmpty()) {
            result += QLatin1Char(' ');
            result += QLatin1String(knownTerminals[i].options);
            return result;
        }
    }
    return QLatin1String("xterm -e");
}

QStringList ConsoleProcess::availableTerminalEmulators()
{
    QStringList result;
    const Environment env = Environment::systemEnvironment();
    const int terminalCount = int(sizeof(knownTerminals) / sizeof(knownTerminals[0]));
    for (int i = 0; i < terminalCount; ++i) {
        QString terminal = env.searchInPath(QLatin1String(knownTerminals[i].binary));
        if (!terminal.isEmpty()) {
            terminal += QLatin1Char(' ');
            terminal += QLatin1String(knownTerminals[i].options);
            result.push_back(terminal);
        }
    }
384
385
    if (!result.contains(defaultTerminalEmulator()))
        result.append(defaultTerminalEmulator());
386
387
    result.sort();
    return result;
388
389
}

390
} // namespace Utils