MythTV  master
mythbackend.cpp
Go to the documentation of this file.
1 #include "libmythbase/mythconfig.h"
2 #if CONFIG_SYSTEMD_NOTIFY
3  #include <systemd/sd-daemon.h>
4 #endif
5 
6 #include <csignal> // for signal
7 #include <cstdlib>
8 
9 #include <QtGlobal>
10 #ifndef _WIN32
11 #include <QCoreApplication>
12 #else
13 #include <QApplication>
14 #endif
15 
16 #include <QDir>
17 #include <QFile>
18 #include <QFileInfo>
19 #include <QMap>
20 #ifdef Q_OS_DARWIN
21 #include <QProcessEnvironment>
22 #endif
23 
24 // MythTV
26 #include "libmythbase/compat.h"
28 #include "libmythbase/exitcodes.h"
30 #include "libmythbase/mythdb.h"
34 #include "libmythbase/mythversion.h"
36 #include "libmythbase/remoteutil.h"
39 #include "libmythtv/dbcheck.h"
40 #include "libmythtv/jobqueue.h"
44 #include "libmythtv/tv_rec.h"
45 
46 // MythBackend
47 #include "autoexpire.h"
48 #include "backendcontext.h"
49 #include "mainserver.h"
50 #include "mediaserver.h"
53 #include "scheduler.h"
54 
55 
56 #define LOC QString("MythBackend: ")
57 #define LOC_WARN QString("MythBackend, Warning: ")
58 #define LOC_ERR QString("MythBackend, Error: ")
59 
60 #ifdef Q_OS_MACOS
61 // 10.6 uses some file handles for its new Grand Central Dispatch thingy
62 static constexpr long UNUSED_FILENO { 6 };
63 #else
64 static constexpr long UNUSED_FILENO { 3 };
65 #endif
66 
67 int main(int argc, char **argv)
68 {
70  if (!cmdline.Parse(argc, argv))
71  {
74  }
75 
76  if (cmdline.toBool("showhelp"))
77  {
79  return GENERIC_EXIT_OK;
80  }
81 
82  if (cmdline.toBool("showversion"))
83  {
85  return GENERIC_EXIT_OK;
86  }
87 
88 #ifndef _WIN32
89  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
90  close(i);
91  QCoreApplication a(argc, argv);
92 #else
93  // MINGW application needs a window to receive messages
94  // such as socket notifications :[
95  QApplication a(argc, argv);
96 #endif
97  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHBACKEND);
98 
99 #ifdef Q_OS_DARWIN
100  QString path = QCoreApplication::applicationDirPath();
101  setenv("PYTHONPATH",
102  QString("%1/../Resources/lib/%2/site-packages:%3")
103  .arg(path)
104  .arg(QFileInfo(PYTHON_EXE).fileName())
105  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
106  .toUtf8().constData(), 1);
107 #endif
108 
109  int retval = cmdline.Daemonize();
110  if (retval != GENERIC_EXIT_OK)
111  return retval;
112 
113  bool daemonize = cmdline.toBool("daemon");
114  QString mask("general");
115  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
116  return retval;
117 
118  if (daemonize)
119  // Don't listen to console input if daemonized
120  close(0);
121 
122  CleanupGuard callCleanup(cleanup);
123 
124 #ifndef _WIN32
126 #endif
127 
128 #if CONFIG_SYSTEMD_NOTIFY
129  (void)sd_notify(0, "STATUS=Connecting to database.");
130 #endif
131  gContext = new MythContext(MYTH_BINARY_VERSION);
132 
133  // If setup has not been done (ie. the config.xml does not exist),
134  // set the ignoreDB flag, which will cause only the web-app to
135  // start, so that setup can be done.
136 
137  bool ignoreDB = false;
138  {
139  auto config = XmlConfiguration();
140  ignoreDB = !config.FileExists();
141  }
142 
143  // Init Parameters:
144  // bool Init(bool gui = true,
145  // bool promptForBackend = false,
146  // bool disableAutoDiscovery = false,
147  // bool ignoreDB = false);
148 
149  if (!gContext->Init(false,false,false,ignoreDB))
150  {
151  LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext.");
152  gCoreContext->GetDB()->IgnoreDatabase(true);
153  // return GENERIC_EXIT_NO_MYTHCONTEXT;
154  }
155 
156  MythTranslation::load("mythfrontend");
157 
158  setHttpProxy();
159 
161 
162  if (cmdline.toBool("setverbose") || cmdline.toBool("printsched") ||
163  cmdline.toBool("testsched") ||
164  cmdline.toBool("printexpire") || cmdline.toBool("setloglevel"))
165  {
166  gCoreContext->SetAsBackend(false);
167  return handle_command(cmdline);
168  }
169 
170  gCoreContext->SetAsBackend(true);
171  retval = run_backend(cmdline);
172  // Retcode 258 is a special value to signal to mythbackend to restart
173  // This is used by the V2Myth/Shutdown?Restart=true API call
174  if (retval == 258) {
175  LOG(VB_GENERAL, LOG_INFO,
176  QString("Restarting mythbackend"));
177  usleep(50000);
178  int rc = execvp(argv[0], argv);
179  LOG(VB_GENERAL, LOG_ERR,
180  QString("execvp failed prog %1 rc=%2 errno=%3").arg(argv[0]).arg(rc).arg(errno));
181  }
182  return retval;
183 }
184 
185 /* vim: set expandtab tabstop=4 shiftwidth=4: */
setHttpProxy
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:800
backendcontext.h
mythdb.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
setenv
#define setenv(x, y, z)
Definition: compat.h:89
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythsystemevent.h
main
int main(int argc, char **argv)
Definition: mythbackend.cpp:67
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
remoteutil.h
scheduler.h
XmlConfiguration
Definition: configuration.h:38
close
#define close
Definition: compat.h:43
mythbackend_commandlineparser.h
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1554
autoexpire.h
programinfo.h
mythlogging.h
MYTH_APPNAME_MYTHBACKEND
static constexpr const char * MYTH_APPNAME_MYTHBACKEND
Definition: mythcorecontext.h:20
dbcheck.h
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:6
compat.h
MythCoreContext::GetDB
MythDB * GetDB(void)
Definition: mythcorecontext.cpp:1756
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1382
mythtranslation.h
scheduledrecording.h
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1398
jobqueue.h
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:3038
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2932
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:673
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:127
mediaserver.h
mythmiscutil.h
mythcorecontext.h
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2202
cleanupguard.h
configuration.h
tv_rec.h
handle_command
int handle_command(const MythBackendCommandLineParser &cmdline)
Definition: mythbackend_main_helpers.cpp:331
UNUSED_FILENO
static constexpr long UNUSED_FILENO
Definition: mythbackend.cpp:62
mainserver.h
mythbackend_main_helpers.h
MythBackendCommandLineParser
Definition: mythbackend_commandlineparser.h:7
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2864
previewgenerator.h
MythTranslation::load
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
Definition: mythtranslation.cpp:37
exitcodes.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
run_backend
int run_backend(MythBackendCommandLineParser &cmdline)
Definition: mythbackend_main_helpers.cpp:524
MythCoreContext::SetAsBackend
void SetAsBackend(bool backend)
Definition: mythcorecontext.cpp:638
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1603