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 #include <unistd.h>
25 
26 // MythTV
28 #include "libmythbase/compat.h"
30 #include "libmythbase/exitcodes.h"
32 #include "libmythbase/mythdb.h"
36 #include "libmythbase/mythversion.h"
38 #include "libmythbase/remoteutil.h"
41 #include "libmythtv/dbcheck.h"
42 #include "libmythtv/jobqueue.h"
46 #include "libmythtv/tv_rec.h"
47 
48 // MythBackend
49 #include "autoexpire.h"
50 #include "backendcontext.h"
51 #include "mainserver.h"
52 #include "mediaserver.h"
55 #include "scheduler.h"
56 
57 
58 #define LOC QString("MythBackend: ")
59 #define LOC_WARN QString("MythBackend, Warning: ")
60 #define LOC_ERR QString("MythBackend, Error: ")
61 
62 #ifdef Q_OS_MACOS
63 // 10.6 uses some file handles for its new Grand Central Dispatch thingy
64 static constexpr long UNUSED_FILENO { 6 };
65 #else
66 static constexpr long UNUSED_FILENO { 3 };
67 #endif
68 
69 int main(int argc, char **argv)
70 {
72  if (!cmdline.Parse(argc, argv))
73  {
76  }
77 
78  if (cmdline.toBool("showhelp"))
79  {
81  return GENERIC_EXIT_OK;
82  }
83 
84  if (cmdline.toBool("showversion"))
85  {
87  return GENERIC_EXIT_OK;
88  }
89 
90 #ifndef _WIN32
91 #if HAVE_CLOSE_RANGE
92  close_range(UNUSED_FILENO, sysconf(_SC_OPEN_MAX) - 1, 0);
93 #else
94  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
95  close(i);
96 #endif
97  QCoreApplication a(argc, argv);
98 #else
99  // MINGW application needs a window to receive messages
100  // such as socket notifications :[
101  QApplication a(argc, argv);
102 #endif
103  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHBACKEND);
104 
105 #ifdef Q_OS_DARWIN
106  QString path = QCoreApplication::applicationDirPath();
107  setenv("PYTHONPATH",
108  QString("%1/../Resources/lib/%2:/../Resources/lib/%2/site-packages:/../Resources/lib/%2/lib-dynload:%3")
109  .arg(path)
110  .arg(QFileInfo(PYTHON_EXE).fileName())
111  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
112  .toUtf8().constData(), 1);
113 #endif
114 
115  int retval = cmdline.Daemonize();
116  if (retval != GENERIC_EXIT_OK)
117  return retval;
118 
119  bool daemonize = cmdline.toBool("daemon");
120  QString mask("general");
121  retval = cmdline.ConfigureLogging(mask, daemonize);
122  if (retval != GENERIC_EXIT_OK)
123  return retval;
124 
125  if (daemonize)
126  // Don't listen to console input if daemonized
127  close(0);
128 
129  CleanupGuard callCleanup(cleanup);
130 
131 #ifndef _WIN32
133 #endif
134 
135 #if CONFIG_SYSTEMD_NOTIFY
136  (void)sd_notify(0, "STATUS=Connecting to database.");
137 #endif
138  gContext = new MythContext(MYTH_BINARY_VERSION);
139 
140  // If setup has not been done (ie. the config.xml does not exist),
141  // set the ignoreDB flag, which will cause only the web-app to
142  // start, so that setup can be done.
143 
144  bool ignoreDB = false;
145  {
146  auto config = XmlConfiguration();
147  ignoreDB = !config.FileExists();
148  if (ignoreDB)
150  }
151 
152  // Init Parameters:
153  // bool Init(bool gui = true,
154  // bool promptForBackend = false,
155  // bool disableAutoDiscovery = false,
156  // bool ignoreDB = false);
157 
158  if (!gContext->Init(false,false,false,ignoreDB))
159  {
160  LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext.");
162  }
163 
164  MythTranslation::load("mythfrontend");
165 
166  setHttpProxy();
167 
169 
170  if (cmdline.toBool("setverbose") || cmdline.toBool("printsched") ||
171  cmdline.toBool("testsched") ||
172  cmdline.toBool("printexpire") || cmdline.toBool("setloglevel"))
173  {
174  gCoreContext->SetAsBackend(false);
175  return handle_command(cmdline);
176  }
177 
178  gCoreContext->SetAsBackend(true);
179  retval = run_backend(cmdline);
180  // Retcode 258 is a special value to signal to mythbackend to restart
181  // This is used by the V2Myth/Shutdown?Restart=true API call
182  // Retcode 259 is a special value to signal to mythbackend to restart
183  // in webonly mode
184  if (retval == 258 || retval == 259)
185  {
186  char ** newargv = new char * [argc + 2];
187  std::string webonly = "--webonly";
188  newargv[0] = argv[0];
189  int newargc = 1;
190  for (int ix = 1 ; ix < argc ; ++ix)
191  {
192  if (webonly != argv[ix])
193  newargv[newargc++] = argv[ix];
194  }
195  if (retval == 259)
196  newargv[newargc++] = webonly.data();
197  newargv[newargc] = nullptr;
198  LOG(VB_GENERAL, LOG_INFO,
199  QString("Restarting mythbackend"));
200  usleep(50000);
201  int rc = execvp(newargv[0], newargv);
202  LOG(VB_GENERAL, LOG_ERR,
203  QString("execvp failed prog %1 rc=%2 errno=%3").arg(argv[0]).arg(rc).arg(errno));
204  delete[] newargv;
205  }
206  return retval;
207 }
208 
209 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythContext::kWebOnlyDBSetup
@ kWebOnlyDBSetup
Definition: mythcontext.h:61
setHttpProxy
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:803
backendcontext.h
mythdb.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
MythContext::setWebOnly
void setWebOnly(WebOnlyStartup w)
Definition: mythcontext.h:68
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:69
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:1552
autoexpire.h
programinfo.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
mythlogging.h
MYTH_APPNAME_MYTHBACKEND
static constexpr const char * MYTH_APPNAME_MYTHBACKEND
Definition: mythcorecontext.h:19
dbcheck.h
signalhandling.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
CleanupGuard
Definition: cleanupguard.h:6
compat.h
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1380
mythtranslation.h
scheduledrecording.h
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1396
jobqueue.h
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:3047
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:2941
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:673
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:128
mediaserver.h
mythmiscutil.h
mythcorecontext.h
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2197
cleanupguard.h
configuration.h
tv_rec.h
handle_command
int handle_command(const MythBackendCommandLineParser &cmdline)
Definition: mythbackend_main_helpers.cpp:333
UNUSED_FILENO
static constexpr long UNUSED_FILENO
Definition: mythbackend.cpp:64
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:2870
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
run_backend
int run_backend(MythBackendCommandLineParser &cmdline)
Definition: mythbackend_main_helpers.cpp:526
MythCoreContext::SetAsBackend
void SetAsBackend(bool backend)
Definition: mythcorecontext.cpp:643
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:1608
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:16