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 
46 #if CONFIG_CYGWIN || defined(_WIN32)
47 #include "mythsystemwindows.h"
48 #else
49 #include "mythsystemunix.h"
50 #endif
51 
52 
53 /*******************************
54  * MythSystemLegacy method defines
55  ******************************/
56 
58 {
59 #if CONFIG_CYGWIN || defined(_WIN32)
60  d = new MythSystemLegacyWindows(this);
61 #else
62  d = new MythSystemLegacyUnix(this);
63 #endif
64 }
65 
67  QObject(parent)
68 {
69  setObjectName("MythSystemLegacy()");
70  m_semReady.release(1); // initialize
72 }
73 
74 MythSystemLegacy::MythSystemLegacy(const QString &command, uint flags,
75  QObject *parent) :
76  QObject(parent)
77 {
78  setObjectName(QString("MythSystemLegacy(%1)").arg(command));
79  m_semReady.release(1); // initialize
81  SetCommand(command, flags);
82 }
83 
87 void MythSystemLegacy::SetCommand(const QString &command, uint flags)
88 {
89  if (flags & kMSRunShell)
90  {
91  SetCommand(command, QStringList(), flags);
92  }
93  else
94  {
95  QString abscommand;
96  QStringList args;
97  if (!d->ParseShell(command, abscommand, args))
98  {
99  LOG(VB_GENERAL, LOG_ERR,
100  QString("MythSystemLegacy(%1) command not understood")
101  .arg(command));
103  return;
104  }
105 
106  SetCommand(abscommand, args, flags);
107  }
108 
109  if (m_settings["UseStdin"])
110  m_stdbuff[0].open(QIODevice::WriteOnly);
111  if (m_settings["UseStdout"])
112  m_stdbuff[1].open(QIODevice::ReadOnly);
113  if (m_settings["UseStderr"])
114  m_stdbuff[2].open(QIODevice::ReadOnly);
115 }
116 
117 
118 MythSystemLegacy::MythSystemLegacy(const QString &command,
119  const QStringList &args,
120  uint flags,
121  QObject *parent) :
122  QObject(parent)
123 {
124  m_semReady.release(1); // initialize
126  SetCommand(command, args, flags);
127 }
128 
132 void MythSystemLegacy::SetCommand(const QString &command,
133  const QStringList &args, uint flags)
134 {
136  m_command = QString(command).trimmed();
137  m_args = QStringList(args);
138 
139  ProcessFlags(flags);
140 
141  // add logging arguments
142  if (GetSetting("PropagateLogs"))
143  {
144  if (GetSetting("UseShell") && m_args.isEmpty())
145  {
147  if (!logPropagateQuiet())
148  m_command += " --quiet";
149  }
150  else
151  {
153  if (!logPropagateQuiet())
154  m_args << "--quiet";
155  }
156  }
157 
158  // check for execute rights
159  if (!GetSetting("UseShell") && (access(command.toUtf8().constData(), X_OK)) != 0)
160  {
161  LOG(VB_GENERAL, LOG_ERR,
162  QString("MythSystemLegacy(%1) command not executable, ")
163  .arg(command) + ENO);
165  }
166 
167  m_logcmd = (m_command + " " + m_args.join(" ")).trimmed();
168 
169  if (GetSetting("AnonLog"))
170  {
171  m_logcmd.truncate(m_logcmd.indexOf(" "));
172  m_logcmd.append(" (anonymized)");
173  }
174 }
175 
176 // QBuffers may also need freeing
178 {
180  {
181  Term(true);
182  Wait();
183  }
184  d->DecrRef();
185 }
186 
187 void MythSystemLegacy::SetDirectory(const QString &directory)
188 {
189  m_settings["SetDirectory"] = true;
190  m_directory = QString(directory);
191 }
192 
194 {
195  if (!d || (GetStatus() != GENERIC_EXIT_START))
196  return false;
197 
198  m_nice = nice;
199  return true;
200 }
201 
203 {
204  if (!d || (GetStatus() != GENERIC_EXIT_START))
205  return false;
206 
207  m_ioprio = prio;
208  return true;
209 }
210 
213 {
214  if (!d)
216 
217  if (GetStatus() != GENERIC_EXIT_START)
218  {
219  emit error(GetStatus());
220  return;
221  }
222 
223  // Handle any locking of drawing, etc
224  HandlePreRun();
225 
226  d->Fork(timeout);
227 
229  {
230  m_semReady.acquire(1);
231  emit started();
232  d->Manage();
233  }
234  else
235  {
236  emit error(GetStatus());
237  }
238 }
239 
240 // should there be a separate 'getstatus' call? or is using
241 // Wait() for that purpose sufficient?
243 {
244  if (!d)
246 
247  if ((GetStatus() != GENERIC_EXIT_RUNNING) || GetSetting("RunInBackground"))
248  return GetStatus();
249 
250  if (GetSetting("ProcessEvents"))
251  {
252  if (timeout > 0)
253  timeout += time(nullptr);
254 
255  while (!timeout || time(nullptr) < timeout)
256  {
257  // loop until timeout hits or process ends
258  if (m_semReady.tryAcquire(1,100))
259  {
260  m_semReady.release(1);
261  break;
262  }
263 
264  qApp->processEvents();
265  }
266  }
267  else
268  {
269  if (timeout > 0)
270  {
271  if (m_semReady.tryAcquire(1, timeout*1000))
272  m_semReady.release(1);
273  }
274  else
275  {
276  m_semReady.acquire(1);
277  m_semReady.release(1);
278  }
279  }
280  return GetStatus();
281 }
282 
284 {
285  if (!d)
287 
289  return;
290 
291  d->Term(force);
292 }
293 
295 {
296  if (!d)
298 
300  return;
301 
302  int posix_signal = SIGTRAP;
303  switch (sig)
304  {
305  case kSignalNone:
306  case kSignalUnknown:
307  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  auto *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  auto *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  auto *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  auto *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.
virtual void Manage(void)=0
#define SIGSTOP
Definition: compat.h:221
bool HasGUI(void) const
anonymize the logs
Definition: mythsystem.h:42
General purpose reference counter.
MythSystemLegacyPrivate * d
#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)
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)
static Type kPopDisableDrawingEventType
Definition: mythevent.h:72
void ProcessFlags(uint flags)
add arguments for MythTV log propagation
Definition: mythsystem.h:50
void Term(bool force=false)
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
#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:700
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
unsigned int uint
Definition: compat.h:140
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
#define SIGUSR1
Definition: compat.h:216
virtual void Fork(time_t timeout)=0
disable MythMessage UDP listener for the duration of application.
Definition: mythsystem.h:48
uint Wait(time_t timeout=0)
#define GENERIC_EXIT_START
MythSystemLegacy process starting.
Definition: exitcodes.h:35
static Type kEnableUDPListenerEventType
Definition: mythevent.h:77
run process through shell
Definition: mythsystem.h:41
static Type kDisableUDPListenerEventType
Definition: mythevent.h:76
automatically delete if backgrounded
Definition: mythsystem.h:43
process events while waiting
Definition: mythsystem.h:37
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QByteArray ReadErr(int size)
avoid disabling UI drawing
Definition: mythsystem.h:35
#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
run child in the background
Definition: mythsystem.h:36
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
avoid blocking LIRC & Joystick Menu
Definition: mythsystem.h:34
allow exit values 0-127 only
Definition: mythsystem.h:45
allow access to stdin
Definition: mythsystem.h:38
allow access to stderr
Definition: mythsystem.h:40
QStringList logPropagateArgList
Definition: logging.cpp:90
void started(void)
allow access to stdout
Definition: mythsystem.h:39