MythTV  master
mythtv/programs/mythbackend/main.cpp
Go to the documentation of this file.
1 #include "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 #ifndef _WIN32
10 #include <QCoreApplication>
11 #else
12 #include <QApplication>
13 #endif
14 
15 #include <QFileInfo>
16 #include <QRegExp>
17 #include <QFile>
18 #include <QDir>
19 #include <QMap>
20 #ifdef Q_OS_MAC
21 #include <QProcessEnvironment>
22 #endif
23 
24 #include "signalhandling.h"
25 #include "commandlineparser.h"
26 #include "scheduledrecording.h"
27 #include "previewgenerator.h"
28 #include "mythcorecontext.h"
29 #include "mythsystemevent.h"
30 #include "mythtranslation.h"
31 #include "backendcontext.h"
32 #include "main_helpers.h"
33 #include "mythmiscutil.h"
34 #include "storagegroup.h"
35 #include "mediaserver.h"
36 #include "loggingserver.h"
37 #include "mythlogging.h"
38 #include "mythversion.h"
39 #include "programinfo.h"
40 #include "autoexpire.h"
41 #include "mainserver.h"
42 #include "remoteutil.h"
43 #include "exitcodes.h"
44 #include "scheduler.h"
45 #include "jobqueue.h"
46 #include "dbcheck.h"
47 #include "compat.h"
48 #include "mythdb.h"
49 #include "tv_rec.h"
50 #include "cleanupguard.h"
51 
52 
53 #define LOC QString("MythBackend: ")
54 #define LOC_WARN QString("MythBackend, Warning: ")
55 #define LOC_ERR QString("MythBackend, Error: ")
56 
57 #ifdef Q_OS_MACX
58  // 10.6 uses some file handles for its new Grand Central Dispatch thingy
59  #define UNUSED_FILENO 6
60 #else
61  #define UNUSED_FILENO 3
62 #endif
63 
64 int main(int argc, char **argv)
65 {
67  if (!cmdline.Parse(argc, argv))
68  {
71  }
72 
73  if (cmdline.toBool("showhelp"))
74  {
76  return GENERIC_EXIT_OK;
77  }
78 
79  if (cmdline.toBool("showversion"))
80  {
82  return GENERIC_EXIT_OK;
83  }
84 
85 #ifndef _WIN32
86  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
87  close(i);
88  QCoreApplication a(argc, argv);
89 #else
90  // MINGW application needs a window to receive messages
91  // such as socket notifications :[
92  QApplication a(argc, argv);
93 #endif
94  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHBACKEND);
95 
96 #ifdef Q_OS_MAC
97  QString path = QCoreApplication::applicationDirPath();
98  setenv("PYTHONPATH",
99  QString("%1/../Resources/lib/python2.6/site-packages:%2")
100  .arg(path)
101  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
102  .toUtf8().constData(), 1);
103 #endif
104 
105  pidfile = cmdline.toString("pidfile");
106  int retval = cmdline.Daemonize();
107  if (retval != GENERIC_EXIT_OK)
108  return retval;
109 
110  bool daemonize = cmdline.toBool("daemon");
111  QString mask("general");
112  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
113  return retval;
114 
115  if (daemonize)
116  // Don't listen to console input if daemonized
117  close(0);
118 
119  CleanupGuard callCleanup(cleanup);
120 
121 #ifndef _WIN32
122  QList<int> signallist;
123  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
124  << SIGILL;
125 #if ! CONFIG_DARWIN
126  signallist << SIGRTMIN;
127 #endif
128  SignalHandler::Init(signallist);
129  SignalHandler::SetHandler(SIGHUP, logSigHup);
130 #endif
131 
132 #if CONFIG_SYSTEMD_NOTIFY
133  (void)sd_notify(0, "STATUS=Connecting to databse.");
134 #endif
136  if (!gContext->Init(false))
137  {
138  LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext.");
140  }
141 
142  MythTranslation::load("mythfrontend");
143 
144  setHttpProxy();
145 
147 
148  if (cmdline.toBool("event") || cmdline.toBool("systemevent") ||
149  cmdline.toBool("setverbose") || cmdline.toBool("printsched") ||
150  cmdline.toBool("testsched") || cmdline.toBool("resched") ||
151  cmdline.toBool("scanvideos") || cmdline.toBool("clearcache") ||
152  cmdline.toBool("printexpire") || cmdline.toBool("setloglevel"))
153  {
154  gCoreContext->SetAsBackend(false);
155  return handle_command(cmdline);
156  }
157 
158  gCoreContext->SetAsBackend(true);
159  retval = run_backend(cmdline);
160  return retval;
161 }
162 
163 /* vim: set expandtab tabstop=4 shiftwidth=4: */
int Daemonize(void)
Fork application into background, and detatch from terminal.
Startup context for MythTV.
Definition: mythcontext.h:42
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
int main(int argc, char **argv)
QString pidfile
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
static void PrintVersion(void)
Print application version information.
void PrintHelp(void) const
Print command line option help.
static void Init(QList< int > &signallist, QObject *parent=nullptr)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:62
void ApplySettingsOverride(void)
Apply all overrides to the global context.
int run_backend(MythBackendCommandLineParser &cmdline)
void SetAsBackend(bool backend)
#define close
Definition: compat.h:16
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
static void SetHandler(int signum, SigHandlerFunc handler)
MythCommFlagCommandLineParser cmdline
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
#define MYTH_APPNAME_MYTHBACKEND
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
#define SIGHUP
Definition: compat.h:213
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
static QString cleanup(const QString &str)
#define setenv(x, y, z)
Definition: compat.h:156
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
int handle_command(const MythBackendCommandLineParser &cmdline)
int ConfigureLogging(const QString &mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.