/
mythsystemlegacy.cpp
523 lines (447 loc) · 13.9 KB
/
mythsystemlegacy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* -*- Mode: c++ -*-
* Class MythSystemLegacy
*
* Copyright (C) Gavin Hurlbut 2012
* Copyright (C) Issac Richards 2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// compat header
#include "compat.h"
// Own header
#include "mythsystemlegacy.h"
// C++/C headers
#include <cerrno>
#include <csignal> // for kill() and SIGXXX
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fcntl.h>
#include <unistd.h>
// QT headers
#include <QCoreApplication>
// libmythbase headers
#include "referencecounter.h"
#include "mythcorecontext.h"
#include "mythevent.h"
#include "mythlogging.h"
#if CONFIG_CYGWIN || defined(_WIN32)
#include "mythsystemwindows.h"
#else
#include "mythsystemunix.h"
#endif
/*******************************
* MythSystemLegacy method defines
******************************/
void MythSystemLegacy::initializePrivate(void)
{
#if CONFIG_CYGWIN || defined(_WIN32)
d = new MythSystemLegacyWindows(this);
#else
d = new MythSystemLegacyUnix(this);
#endif
}
MythSystemLegacy::MythSystemLegacy(QObject *parent) :
QObject(parent)
{
setObjectName("MythSystemLegacy()");
m_semReady.release(1); // initialize
initializePrivate();
}
MythSystemLegacy::MythSystemLegacy(const QString &command, uint flags,
QObject *parent) :
QObject(parent)
{
setObjectName(QString("MythSystemLegacy(%1)").arg(command));
m_semReady.release(1); // initialize
initializePrivate();
SetCommand(command, flags);
}
/**
* \brief Resets an existing MythSystemLegacy object to a new command
*/
void MythSystemLegacy::SetCommand(const QString &command, uint flags)
{
if (flags & kMSRunShell)
{
SetCommand(command, QStringList(), flags);
}
else
{
QString abscommand;
QStringList args;
if (!d->ParseShell(command, abscommand, args))
{
LOG(VB_GENERAL, LOG_ERR,
QString("MythSystemLegacy(%1) command not understood")
.arg(command));
m_status = GENERIC_EXIT_INVALID_CMDLINE;
return;
}
SetCommand(abscommand, args, flags);
}
if (m_settings["UseStdin"])
m_stdbuff[0].open(QIODevice::WriteOnly);
if (m_settings["UseStdout"])
m_stdbuff[1].open(QIODevice::ReadOnly);
if (m_settings["UseStderr"])
m_stdbuff[2].open(QIODevice::ReadOnly);
}
MythSystemLegacy::MythSystemLegacy(const QString &command,
const QStringList &args,
uint flags,
QObject *parent) :
QObject(parent)
{
m_semReady.release(1); // initialize
initializePrivate();
SetCommand(command, args, flags);
}
/**
* \brief Resets an existing MythSystemLegacy object to a new command
*/
void MythSystemLegacy::SetCommand(const QString &command,
const QStringList &args, uint flags)
{
m_status = GENERIC_EXIT_START;
m_command = QString(command).trimmed();
m_args = QStringList(args);
ProcessFlags(flags);
// add logging arguments
if (GetSetting("PropagateLogs"))
{
if (GetSetting("UseShell") && m_args.isEmpty())
{
m_command += logPropagateArgs;
if (!logPropagateQuiet())
m_command += " --quiet";
}
else
{
m_args << logPropagateArgList;
if (!logPropagateQuiet())
m_args << "--quiet";
}
}
// check for execute rights
if (!GetSetting("UseShell") && (access(command.toUtf8().constData(), X_OK)) != 0)
{
LOG(VB_GENERAL, LOG_ERR,
QString("MythSystemLegacy(%1) command not executable, ")
.arg(command) + ENO);
m_status = GENERIC_EXIT_CMD_NOT_FOUND;
}
m_logcmd = (m_command + " " + m_args.join(" ")).trimmed();
if (GetSetting("AnonLog"))
{
m_logcmd.truncate(m_logcmd.indexOf(" "));
m_logcmd.append(" (anonymized)");
}
}
// QBuffers may also need freeing
MythSystemLegacy::~MythSystemLegacy(void)
{
if (GetStatus() == GENERIC_EXIT_RUNNING)
{
Term(true);
Wait();
}
d->DecrRef();
}
void MythSystemLegacy::SetDirectory(const QString &directory)
{
m_settings["SetDirectory"] = true;
m_directory = QString(directory);
}
bool MythSystemLegacy::SetNice(int nice)
{
if (!d || (GetStatus() != GENERIC_EXIT_START))
return false;
m_nice = nice;
return true;
}
bool MythSystemLegacy::SetIOPrio(int prio)
{
if (!d || (GetStatus() != GENERIC_EXIT_START))
return false;
m_ioprio = prio;
return true;
}
/// \brief Runs a command inside the /bin/sh shell. Returns immediately
void MythSystemLegacy::Run(time_t timeout)
{
if (!d)
m_status = GENERIC_EXIT_NO_HANDLER;
if (GetStatus() != GENERIC_EXIT_START)
{
emit error(GetStatus());
return;
}
// Handle any locking of drawing, etc
HandlePreRun();
d->Fork(timeout);
if (GetStatus() == GENERIC_EXIT_RUNNING)
{
m_semReady.acquire(1);
emit started();
d->Manage();
}
else
{
emit error(GetStatus());
}
}
// should there be a separate 'getstatus' call? or is using
// Wait() for that purpose sufficient?
uint MythSystemLegacy::Wait(time_t timeout)
{
if (!d)
m_status = GENERIC_EXIT_NO_HANDLER;
if ((GetStatus() != GENERIC_EXIT_RUNNING) || GetSetting("RunInBackground"))
return GetStatus();
if (GetSetting("ProcessEvents"))
{
if (timeout > 0)
timeout += time(nullptr);
while (!timeout || time(nullptr) < timeout)
{
// loop until timeout hits or process ends
if (m_semReady.tryAcquire(1,100))
{
m_semReady.release(1);
break;
}
qApp->processEvents();
}
}
else
{
if (timeout > 0)
{
if (m_semReady.tryAcquire(1, timeout*1000))
m_semReady.release(1);
}
else
{
m_semReady.acquire(1);
m_semReady.release(1);
}
}
return GetStatus();
}
void MythSystemLegacy::Term(bool force)
{
if (!d)
m_status = GENERIC_EXIT_NO_HANDLER;
if (m_status != GENERIC_EXIT_RUNNING)
return;
d->Term(force);
}
void MythSystemLegacy::Signal(MythSignal sig)
{
if (!d)
m_status = GENERIC_EXIT_NO_HANDLER;
if (m_status != GENERIC_EXIT_RUNNING)
return;
int posix_signal = SIGTRAP;
switch (sig)
{
case kSignalNone:
case kSignalUnknown:
break;
case kSignalHangup: posix_signal = SIGHUP; break;
case kSignalInterrupt: posix_signal = SIGINT; break;
case kSignalContinue: posix_signal = SIGCONT; break;
case kSignalQuit: posix_signal = SIGQUIT; break;
case kSignalSegfault: posix_signal = SIGSEGV; break;
case kSignalKill: posix_signal = SIGKILL; break;
case kSignalUser1: posix_signal = SIGUSR1; break;
case kSignalUser2: posix_signal = SIGUSR2; break;
case kSignalTerm: posix_signal = SIGTERM; break;
case kSignalStop: posix_signal = SIGSTOP; break;
}
// The default less switch above will cause a compiler warning
// if someone adds a signal without updating the switch, but in
// case that is missed print out a message.
if (SIGTRAP == posix_signal)
{
LOG(VB_SYSTEM, LOG_ERR,
QString("Programmer error: Unknown signal %1").arg(sig));
return;
}
d->Signal(posix_signal);
}
void MythSystemLegacy::ProcessFlags(uint flags)
{
if (m_status != GENERIC_EXIT_START)
{
LOG(VB_SYSTEM, LOG_DEBUG, QString("status: %1").arg(m_status));
return;
}
m_settings["IsInUI"] = gCoreContext->HasGUI() && gCoreContext->IsUIThread();
if (flags & kMSRunBackground)
m_settings["RunInBackground"] = true;
if (m_command.endsWith("&"))
{
if (!GetSetting("RunInBackground"))
LOG(VB_SYSTEM, LOG_DEBUG, "Adding background flag");
// Remove the &
m_command.chop(1);
m_command = m_command.trimmed();
m_settings["RunInBackground"] = true;
m_settings["UseShell"] = true;
m_settings["IsInUI"] = false;
}
if (GetSetting("IsInUI"))
{
// Check for UI-only locks
m_settings["BlockInputDevs"] = ((flags & kMSDontBlockInputDevs) == 0U);
m_settings["DisableDrawing"] = ((flags & kMSDontDisableDrawing) == 0U);
m_settings["ProcessEvents"] = ((flags & kMSProcessEvents) != 0U);
m_settings["DisableUDP"] = ((flags & kMSDisableUDPListener) != 0U);
}
if (flags & kMSStdIn)
m_settings["UseStdin"] = true;
if (flags & kMSStdOut)
m_settings["UseStdout"] = true;
if (flags & kMSStdErr)
m_settings["UseStderr"] = true;
if (flags & kMSRunShell)
m_settings["UseShell"] = true;
if (flags & kMSAutoCleanup && GetSetting("RunInBackground"))
m_settings["AutoCleanup"] = true;
if (flags & kMSAnonLog)
m_settings["AnonLog"] = true;
if (flags & kMSLowExitVal)
m_settings["OnlyLowExitVal"] = true;
if (flags & kMSPropagateLogs)
m_settings["PropagateLogs"] = true;
}
QByteArray MythSystemLegacy::Read(int size)
{
return m_stdbuff[1].read(size);
}
QByteArray MythSystemLegacy::ReadErr(int size)
{
return m_stdbuff[2].read(size);
}
QByteArray& MythSystemLegacy::ReadAll(void)
{
return m_stdbuff[1].buffer();
}
QByteArray& MythSystemLegacy::ReadAllErr(void)
{
return m_stdbuff[2].buffer();
}
/** This writes to the standard input of the program being run.
* All calls to this must be done before Run() is called.
* All calls after Run() is called are silently ignored.
*/
int MythSystemLegacy::Write(const QByteArray &ba)
{
if (!GetSetting("UseStdin"))
return 0;
return m_stdbuff[0].write(ba.constData());
}
void MythSystemLegacy::HandlePreRun(void)
{
// This needs to be a send event so that the MythUI locks the input devices
// immediately instead of after existing events are processed
// since this function could be called inside one of those events.
if (GetSetting("BlockInputDevs"))
{
QEvent event(MythEvent::kLockInputDevicesEventType);
QCoreApplication::sendEvent(gCoreContext->GetGUIObject(), &event);
}
// This needs to be a send event so that the listener is disabled
// immediately instead of after existing events are processed, since the
// listen server must be terminated before the spawned application tries
// to start its own
if (GetSetting("DisableUDP"))
{
QEvent event(MythEvent::kDisableUDPListenerEventType);
QCoreApplication::sendEvent(gCoreContext->GetGUIObject(), &event);
}
// This needs to be a send event so that the MythUI m_drawState change is
// flagged immediately instead of after existing events are processed
// since this function could be called inside one of those events.
if (GetSetting("DisableDrawing"))
{
QEvent event(MythEvent::kPushDisableDrawingEventType);
QCoreApplication::sendEvent(gCoreContext->GetGUIObject(), &event);
}
}
void MythSystemLegacy::HandlePostRun(void)
{
// Since this is *not* running in the UI thread (but rather the signal
// handler thread), we need to use postEvents
if (GetSetting("DisableDrawing"))
{
QEvent *event = new QEvent(MythEvent::kPopDisableDrawingEventType);
QCoreApplication::postEvent(gCoreContext->GetGUIObject(), event);
}
// This needs to be a post event so we do not try to start listening on
// the UDP ports before the child application has stopped and terminated
if (GetSetting("DisableUDP"))
{
QEvent *event = new QEvent(MythEvent::kEnableUDPListenerEventType);
QCoreApplication::postEvent(gCoreContext->GetGUIObject(), event);
}
// This needs to be a post event so that the MythUI unlocks input devices
// after all existing (blocked) events are processed and ignored.
if (GetSetting("BlockInputDevs"))
{
QEvent *event = new QEvent(MythEvent::kUnlockInputDevicesEventType);
QCoreApplication::postEvent(gCoreContext->GetGUIObject(), event);
}
}
QString MythSystemLegacy::ShellEscape(const QString &in)
{
QString out = in;
if (out.contains("\""))
out = out.replace("\"", "\\\"");
if (out.contains("\'"))
out = out.replace("\'", "\\\'");
if (out.contains(" "))
{
out.prepend("\"");
out.append("\"");
}
return out;
}
MythSystemLegacyPrivate::MythSystemLegacyPrivate(const QString &debugName) :
ReferenceCounter(debugName)
{
}
uint myth_system(const QString &command, uint flags, uint timeout)
{
flags |= kMSRunShell | kMSAutoCleanup;
MythSystemLegacy *ms = new MythSystemLegacy(command, flags);
ms->Run(timeout);
uint result = ms->Wait(0);
if (!ms->GetSetting("RunInBackground"))
delete ms;
return result;
}
extern "C" {
unsigned int myth_system_c(char *command, uint flags, uint timeout)
{
QString cmd(command);
return myth_system(cmd, flags, timeout);
}
}
/*
* vim:ts=4:sw=4:ai:et:si:sts=4
*/