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