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/%2/site-packages:%3")
100  .arg(path)
101  .arg(QFileInfo(PYTHON_EXE).fileName())
102  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
103  .toUtf8().constData(), 1);
104 #endif
105 
106  pidfile = cmdline.toString("pidfile");
107  int retval = cmdline.Daemonize();
108  if (retval != GENERIC_EXIT_OK)
109  return retval;
110 
111  bool daemonize = cmdline.toBool("daemon");
112  QString mask("general");
113  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
114  return retval;
115 
116  if (daemonize)
117  // Don't listen to console input if daemonized
118  close(0);
119 
120  CleanupGuard callCleanup(cleanup);
121 
122 #ifndef _WIN32
123  QList<int> signallist;
124  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
125  << SIGILL;
126 #if ! CONFIG_DARWIN
127  signallist << SIGRTMIN;
128 #endif
129  SignalHandler::Init(signallist);
130  SignalHandler::SetHandler(SIGHUP, logSigHup);
131 #endif
132 
133 #if CONFIG_SYSTEMD_NOTIFY
134  (void)sd_notify(0, "STATUS=Connecting to databse.");
135 #endif
137  if (!gContext->Init(false))
138  {
139  LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext.");
141  }
142 
143  MythTranslation::load("mythfrontend");
144 
145  setHttpProxy();
146 
148 
149  if (cmdline.toBool("event") || cmdline.toBool("systemevent") ||
150  cmdline.toBool("setverbose") || cmdline.toBool("printsched") ||
151  cmdline.toBool("testsched") || cmdline.toBool("resched") ||
152  cmdline.toBool("scanvideos") || cmdline.toBool("clearcache") ||
153  cmdline.toBool("printexpire") || cmdline.toBool("setloglevel"))
154  {
155  gCoreContext->SetAsBackend(false);
156  return handle_command(cmdline);
157  }
158 
159  gCoreContext->SetAsBackend(true);
160  retval = run_backend(cmdline);
161  return retval;
162 }
163 
164 /* vim: set expandtab tabstop=4 shiftwidth=4: */
main_helpers.h
commandlineparser.h
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
setHttpProxy
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:852
backendcontext.h
mythdb.h
MythContext
Startup context for MythTV.
Definition: mythcontext.h:44
setenv
#define setenv(x, y, z)
Definition: compat.h:158
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythsystemevent.h
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
remoteutil.h
scheduler.h
close
#define close
Definition: compat.h:17
mythversion.h
run_backend
int run_backend(MythBackendCommandLineParser &cmdline)
Definition: main_helpers.cpp:572
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1435
autoexpire.h
programinfo.h
MYTH_APPNAME_MYTHBACKEND
#define MYTH_APPNAME_MYTHBACKEND
Definition: mythcorecontext.h:19
mythlogging.h
handle_command
int handle_command(const MythBackendCommandLineParser &cmdline)
Definition: main_helpers.cpp:314
dbcheck.h
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:7
compat.h
SIGHUP
#define SIGHUP
Definition: compat.h:215
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1263
mythtranslation.h
UNUSED_FILENO
#define UNUSED_FILENO
Definition: mythtv/programs/mythbackend/main.cpp:59
scheduledrecording.h
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1279
jobqueue.h
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:2760
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
pidfile
QString pidfile
Definition: backendcontext.cpp:13
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2654
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:672
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
mediaserver.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:76
mythmiscutil.h
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2098
mythcorecontext.h
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:1941
cleanupguard.h
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:138
tv_rec.h
mainserver.h
main
int main(int argc, char **argv)
Definition: mythtv/programs/mythbackend/main.cpp:64
MythBackendCommandLineParser
Definition: mythbackend/commandlineparser.h:8
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2585
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
loggingserver.h
MythCoreContext::SetAsBackend
void SetAsBackend(bool backend)
Definition: mythcorecontext.cpp:660
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:1564
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124