MythTV  master
mythsystemlegacy.h
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 #ifndef MYTHSYSTEMLEGACY_H
23 #define MYTHSYSTEMLEGACY_H
24 
25 #include "mythbaseexp.h"
26 
27 // Note: The FIXME were added 2013-05-18, as part of a multi-pass
28 // cleanup of MythSystemLegacy.
29 //
30 // MythSystemLegacy exists because the Qt QProcess has caused us numerous
31 // headaches, most importantly:
32 // http://code.mythtv.org/trac/ticket/7135
33 // https://bugreports.qt-project.org/browse/QTBUG-5990
34 // QProcess also demands that the thread in which it is used
35 // and created run a Qt event loop, which MythSystemLegacy does not
36 // require. A number of MythTV threads do not run an event loop
37 // so this requirement is a bit onerous.
38 //
39 // MythSystemLegacy has grown a bit fat around the middle as functionality
40 // has been added and as a core functionality class is due for a code
41 // review and some cleanup.
42 
43 // C headers
44 #include <array>
45 #include <cstdint>
46 #include <ctime>
47 
48 #include "exitcodes.h" // included for GENERIC_EXIT_OK
49 #include "mythsystem.h" // included for MythSystemFlag and MythSignal
50 
51 #include <QStringList>
52 #include <QSemaphore>
53 #include <QBuffer>
54 #include <QObject>
55 #include <QString>
56 #include <QMap> // FIXME: This shouldn't be needed, Setting_t is not public
57 
58 using Setting = QMap<QString, bool>;
59 
61 
62 // FIXME: Does MythSystemLegacy really need to inherit from QObject?
63 // we can probably create a private class that inherits
64 // from QObject to avoid exposing lots of thread-unsafe
65 // methods & complicated life-time management.
67 class MBASE_PUBLIC MythSystemLegacy : public QObject
68 {
69  Q_OBJECT
70 
71  public:
72  explicit MythSystemLegacy(QObject *parent = nullptr);
73  MythSystemLegacy(const QString &command, uint flags, QObject *parent = nullptr);
74  MythSystemLegacy(const QString &command, const QStringList &args, uint flags,
75  QObject *parent = nullptr);
76  ~MythSystemLegacy(void) override;
77 
78  // FIXME: We should not allow a MythSystemLegacy to be reused for a new command.
79  void SetCommand(const QString &command, uint flags);
80  // FIXME: We should not allow a MythSystemLegacy to be reused for a new command.
81  void SetCommand(const QString &command, const QStringList &args, uint flags);
82  // FIXME: This should only be in the constructor
83  void SetDirectory(const QString &directory);
84  // FIXME: This should only be in the constructor
85  bool SetNice(int nice);
86  // FIXME: This should only be in the constructor
87  bool SetIOPrio(int prio);
88 
89  // FIXME: Forks, should be called Start() for consistency with MThread.
90  // FIXME: Do we need this call at all?
91  void Run(std::chrono::seconds timeout = 0s);
92  // FIXME: This should just return a bool telling us if we hit the timeout.
93  uint Wait(std::chrono::seconds timeout = 0s);
94 
95  int Write(const QByteArray &ba);
96  QByteArray Read(int size);
97  QByteArray ReadErr(int size);
98  // FIXME: We should not return a reference here
99  QByteArray& ReadAll();
100  // FIXME: We should not return a reference here
101  QByteArray& ReadAllErr();
102 
103  // FIXME: Can Term be wrapped into Signal?
104  void Term(bool force = false);
105  void Signal(MythSignal sig);
106 
107  // FIXME: Should be IsBackground() + documented
108  bool isBackground(void) { return GetSetting("RunInBackground"); }
109  // FIXME: Should be IsAutoCleanupProcess() + documented
110  bool doAutoCleanup(void) { return GetSetting("AutoCleanup"); }
111  // FIXME: No idea what this is querying but should be StudlyCase
112  // and should be documented
113  bool onlyLowExitVal(void) { return GetSetting("OnlyLowExitVal"); }
114  // FIXME: Why is this public?
115  void HandlePreRun(void);
116  // FIXME: Why is this public?
117  void HandlePostRun(void);
118 
119  // FIXME: Rename "GetExitStatus" and document that this does not
120  // block until an exit status exists.
121  // FIXME: Document what this actually returns.
122  uint GetStatus(void) const { return m_status; }
123  // FIXME Make private.
124  void SetStatus(uint status) { m_status = status; }
125 
126  // FIXME: We should not return a reference here, add a Set if necessary
127  // Do we even need this function? Should it then be better named?
128  QString& GetLogCmd(void) { return m_logcmd; }
129  // FIXME: We should not return a reference here
130  QString& GetDirectory(void) { return m_directory; }
131 
132  // FIXME: Eliminate or make private, we don't allow any settings
133  // that can not be enumerated.
134  bool GetSetting(const char *setting)
135  { return m_settings.value(QString(setting)); }
136 
137  // FIXME: We should not return a reference here
138  QString& GetCommand(void) { return m_command; }
139  // FIXME: Eliminate. We should not allow setting the command
140  // after construcion.
141  void SetCommand(const QString &cmd) { m_command = cmd; }
142 
143  // FIXME: We should not return a reference here
144  // FIXME: Rename "GetArguments"
145  QStringList &GetArgs(void) { return m_args; }
146  // FIXME: Eliminate. We should not allow setting the arguements
147  // after construcion.
148  void SetArgs(const QStringList &args) { m_args = args; }
149 
150  int GetNice(void) const { return m_nice; }
151  int GetIOPrio(void) const { return m_ioprio; }
152 
153  // FIXME: We should not return a pointer to a QBuffer
154  QBuffer *GetBuffer(int index) { return &m_stdbuff[index]; }
155 
156  // FIXME: We should not make locking public
157  void Unlock(void) { m_semReady.release(1); }
158 
160 
161  // FIXME: This should be an independent function living elsewhere
162  static QString ShellEscape(const QString &in);
163 
164  // FIXME: Do we really need to expose Qt signals?
165  // If so why are they lower case?
166  signals:
167  void started(void);
168  void finished(void);
169  void error(uint status);
170  void readDataReady(int fd);
171 
172  private:
173  Q_DISABLE_COPY(MythSystemLegacy)
174  void initializePrivate(void);
175  // NOLINTNEXTLINE(readability-identifier-naming)
176  MythSystemLegacyPrivate *d {nullptr}; // FIXME we generally call this m_priv in MythTV
177 
178  protected:
179  void ProcessFlags(uint flags);
180 
181  // FIXME if we already have a private helper, why all this?
182  uint m_status {GENERIC_EXIT_OK};
183  QSemaphore m_semReady;
184 
185  QString m_command;
186  QString m_logcmd;
187  QStringList m_args;
188  QString m_directory;
189 
190  int m_nice {0};
191  int m_ioprio {0};
192 
194  std::array<QBuffer,3> m_stdbuff;
195 };
196 
197 MBASE_PUBLIC uint myth_system(const QString &command,
198  uint flags = kMSNone,
199  std::chrono::seconds timeout = 0s);
200 MBASE_PUBLIC uint myth_system(const QString &Command, const QStringList& Args,
201  uint Flags = kMSNone, std::chrono::seconds Timeout = 0s);
202 
203 #endif // MYTHSYSTEMLEGACY_H
204 /*
205  * vim:ts=4:sw=4:ai:et:si:sts=4
206  */
build_compdb.args
args
Definition: build_compdb.py:11
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:101
MythSystemLegacy::m_settings
Setting m_settings
Definition: mythsystemlegacy.h:193
MythSystemLegacy
Definition: mythsystemlegacy.h:67
MythSystemLegacy::SetStatus
void SetStatus(uint status)
Definition: mythsystemlegacy.h:124
MythSystemLegacy::m_command
QString m_command
Definition: mythsystemlegacy.h:185
MythSystemLegacy::m_logcmd
QString m_logcmd
Definition: mythsystemlegacy.h:186
MythSystemLegacy::GetStatus
uint GetStatus(void) const
Definition: mythsystemlegacy.h:122
MythSystemLegacyPrivate
-*- Mode: c++ -*-
Definition: mythsystemprivate.h:21
mythbaseexp.h
MythSystemLegacyPrivate::finished
void finished(void)
force
bool force
Definition: mythcommflag.cpp:61
MythSystemLegacy::GetDirectory
QString & GetDirectory(void)
Definition: mythsystemlegacy.h:130
MythSystemLegacy::SetCommand
void SetCommand(const QString &cmd)
Definition: mythsystemlegacy.h:141
MythSystemLegacy::GetNice
int GetNice(void) const
Definition: mythsystemlegacy.h:150
MythSystemLegacy::onlyLowExitVal
bool onlyLowExitVal(void)
Definition: mythsystemlegacy.h:113
mythsystem.h
MythSystemLegacy::doAutoCleanup
bool doAutoCleanup(void)
Definition: mythsystemlegacy.h:110
MythSystemLegacy::isBackground
bool isBackground(void)
Definition: mythsystemlegacy.h:108
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MythSystemLegacy::GetBuffer
QBuffer * GetBuffer(int index)
Definition: mythsystemlegacy.h:154
Command
Definition: gamesettings.cpp:234
MythSystemLegacy::m_directory
QString m_directory
Definition: mythsystemlegacy.h:188
MythSystemLegacy::Unlock
void Unlock(void)
Definition: mythsystemlegacy.h:157
MythSystemLegacyPrivate::started
void started(void)
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
MythSignal
MythSignal
Definition: mythsystem.h:56
MythSystemLegacy::GetArgs
QStringList & GetArgs(void)
Definition: mythsystemlegacy.h:145
MythSystemLegacy::GetSetting
bool GetSetting(const char *setting)
Definition: mythsystemlegacy.h:134
MythSystemLegacy::SetArgs
void SetArgs(const QStringList &args)
Definition: mythsystemlegacy.h:148
MythSystemLegacy::GetLogCmd
QString & GetLogCmd(void)
Definition: mythsystemlegacy.h:128
MythSystemLegacy::m_stdbuff
std::array< QBuffer, 3 > m_stdbuff
Definition: mythsystemlegacy.h:194
MythSystemLegacyPrivate::readDataReady
void readDataReady(int fd)
hardwareprofile.smolt.error
def error(message)
Definition: smolt.py:409
Setting
QMap< QString, bool > Setting
Definition: mythsystemlegacy.h:58
MythSystemLegacy::GetIOPrio
int GetIOPrio(void) const
Definition: mythsystemlegacy.h:151
MythSystemLegacy::GetCommand
QString & GetCommand(void)
Definition: mythsystemlegacy.h:138
myth_system
MBASE_PUBLIC uint myth_system(const QString &command, uint flags=kMSNone, std::chrono::seconds timeout=0s)
Definition: mythsystemlegacy.cpp:506
MythSystemLegacy::m_semReady
QSemaphore m_semReady
Definition: mythsystemlegacy.h:183
kMSNone
@ kMSNone
Definition: mythsystem.h:35
anonymous_namespace{videoplayercommand.cpp}::ShellEscape
QString ShellEscape(const QString &src)
Definition: videoplayercommand.cpp:20
nice
#define nice(x)
Definition: compat.h:122
ShutdownMythSystemLegacy
void MBASE_PUBLIC ShutdownMythSystemLegacy(void)
Definition: mythsystemunix.cpp:74
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
exitcodes.h
MythSystemLegacy::m_args
QStringList m_args
Definition: mythsystemlegacy.h:187
uint
unsigned int uint
Definition: freesurround.h:24