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"
27 #include "libmythbase/exitcodes.h"
29 #include "libmythbase/mythdb.h"
33 #include "libmythbase/mythversion.h"
35 #include "libmythbase/remoteutil.h"
38 #include "libmythtv/dbcheck.h"
39 #include "libmythtv/jobqueue.h"
43 #include "libmythtv/tv_rec.h"
44 
45 // MythBackend
46 #include "autoexpire.h"
47 #include "backendcontext.h"
48 #include "mainserver.h"
49 #include "mediaserver.h"
52 #include "scheduler.h"
53 
54 
55 #define LOC QString("MythBackend: ")
56 #define LOC_WARN QString("MythBackend, Warning: ")
57 #define LOC_ERR QString("MythBackend, Error: ")
58 
59 #ifdef Q_OS_MACOS
60 // 10.6 uses some file handles for its new Grand Central Dispatch thingy
61 static constexpr long UNUSED_FILENO { 6 };
62 #else
63 static constexpr long UNUSED_FILENO { 3 };
64 #endif
65 
66 int main(int argc, char **argv)
67 {
69  if (!cmdline.Parse(argc, argv))
70  {
73  }
74 
75  if (cmdline.toBool("showhelp"))
76  {
78  return GENERIC_EXIT_OK;
79  }
80 
81  if (cmdline.toBool("showversion"))
82  {
84  return GENERIC_EXIT_OK;
85  }
86 
87 #ifndef _WIN32
88  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
89  close(i);
90  QCoreApplication a(argc, argv);
91 #else
92  // MINGW application needs a window to receive messages
93  // such as socket notifications :[
94  QApplication a(argc, argv);
95 #endif
96  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHBACKEND);
97 
98 #ifdef Q_OS_DARWIN
99  QString path = QCoreApplication::applicationDirPath();
100  setenv("PYTHONPATH",
101  QString("%1/../Resources/lib/%2/site-packages:%3")
102  .arg(path)
103  .arg(QFileInfo(PYTHON_EXE).fileName())
104  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
105  .toUtf8().constData(), 1);
106 #endif
107 
108  gPidFile = cmdline.toString("pidfile");
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  if (!gContext->Init(false))
133  {
134  LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext.");
136  }
137 
138  MythTranslation::load("mythfrontend");
139 
140  setHttpProxy();
141 
143 
144  if (cmdline.toBool("event") || cmdline.toBool("systemevent") ||
145  cmdline.toBool("setverbose") || cmdline.toBool("printsched") ||
146  cmdline.toBool("testsched") || cmdline.toBool("resched") ||
147  cmdline.toBool("scanvideos") || cmdline.toBool("clearcache") ||
148  cmdline.toBool("printexpire") || cmdline.toBool("setloglevel"))
149  {
150  gCoreContext->SetAsBackend(false);
151  return handle_command(cmdline);
152  }
153 
154  gCoreContext->SetAsBackend(true);
155  retval = run_backend(cmdline);
156  return retval;
157 }
158 
159 #ifdef _WIN32 // TODO Needs fixing for Windows
160  QString GetPlaybackURL(ProgramInfo *pginfo, bool storePath)
161  {
162  return "";
163  }
164 #endif
165 
166 /* 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:796
backendcontext.h
mythdb.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
GetPlaybackURL
QString GetPlaybackURL(ProgramInfo *pginfo, bool storePath)
Definition: mythbackend.cpp:160
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
setenv
#define setenv(x, y, z)
Definition: compat.h:87
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythsystemevent.h
main
int main(int argc, char **argv)
Definition: mythbackend.cpp:66
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
remoteutil.h
scheduler.h
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:1553
autoexpire.h
programinfo.h
mythlogging.h
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:14
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
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1381
mythtranslation.h
scheduledrecording.h
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1397
jobqueue.h
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:3037
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:54
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2931
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
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
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:2358
gPidFile
QString gPidFile
Definition: backendcontext.cpp:13
mythcorecontext.h
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2201
cleanupguard.h
tv_rec.h
handle_command
int handle_command(const MythBackendCommandLineParser &cmdline)
Definition: mythbackend_main_helpers.cpp:326
UNUSED_FILENO
static constexpr long UNUSED_FILENO
Definition: mythbackend.cpp:61
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:2862
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:584
MythCoreContext::SetAsBackend
void SetAsBackend(bool backend)
Definition: mythcorecontext.cpp:637
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:57
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1584