MythTV  master
mythmediaserver.cpp
Go to the documentation of this file.
1 // C/C++
2 #include <csignal>
3 #include <cstdio>
4 #include <cstdlib>
5 #include <fcntl.h>
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9 #include <unistd.h>
10 
11 // Qt
12 #include <QCoreApplication>
13 #include <QDir>
14 #include <QString>
15 
16 // MythTV
17 #include "libmyth/mythcontext.h"
19 #include "libmythbase/exitcodes.h"
20 #include "libmythbase/mythconfig.h"
21 #include "libmythbase/mythdbcon.h"
23 #include "libmythbase/mythversion.h"
29 #include "libmythtv/dbcheck.h"
31 
32 // MythMediaServer
33 #include "controlrequesthandler.h"
35 
36 #if CONFIG_SYSTEMD_NOTIFY
37 #include <systemd/sd-daemon.h>
38 static inline void ms_sd_notify(const char *str) { sd_notify(0, str); };
39 #else
40 static inline void ms_sd_notify(const char */*str*/) {};
41 #endif
42 
43 #define LOC QString("MythMediaServer: ")
44 #define LOC_WARN QString("MythMediaServer, Warning: ")
45 #define LOC_ERR QString("MythMediaServer, Error: ")
46 
47 QString pidfile;
48 QString logfile = "";
49 
50 static void cleanup(void)
51 {
52  delete gContext;
53  gContext = nullptr;
54 
55  if (!pidfile.isEmpty())
56  {
57  unlink(pidfile.toLatin1().constData());
58  pidfile.clear();
59  }
60 
62 }
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  QCoreApplication a(argc, argv);
86  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHMEDIASERVER);
87 
88  int retval = cmdline.Daemonize();
89  if (retval != GENERIC_EXIT_OK)
90  return retval;
91 
92  bool daemonize = cmdline.toBool("daemon");
93  QString mask("general");
94  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
95  return retval;
96 
97  CleanupGuard callCleanup(cleanup);
98 
99 #ifndef _WIN32
101 #endif
102 
103  ms_sd_notify("STATUS=Connecting to database.");
104  gContext = new MythContext(MYTH_BINARY_VERSION);
105  if (!gContext->Init(false))
106  {
107  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init MythContext, exiting.");
109  }
110 
111  if (!UpgradeTVDatabaseSchema(false, false, true))
112  {
113  LOG(VB_GENERAL, LOG_ERR, "Exiting due to schema mismatch.");
115  }
116 
118 
119  gCoreContext->SetAsBackend(true); // blocks the event connection
120  ms_sd_notify("STATUS=Connecting to master server.");
122  {
123  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect to master server");
125  }
126 
127  int port = gCoreContext->GetBackendServerPort();
128  if (gCoreContext->GetBackendServerIP().isEmpty())
129  {
130  std::cerr << "No setting found for this machine's BackendServerIP.\n"
131  << "Please run setup on this machine and modify the first page\n"
132  << "of the general settings.\n";
134  }
135 
136  ms_sd_notify("STATUS=Creating socket manager");
137  auto *sockmanager = new MythSocketManager();
138  if (!sockmanager->Listen(port))
139  {
140  LOG(VB_GENERAL, LOG_ERR,
141  "Mediaserver exiting, failed to bind to listen port.");
142  delete sockmanager;
144  }
145 
146  sockmanager->RegisterHandler(new BaseRequestHandler());
147  sockmanager->RegisterHandler(new FileServerHandler());
148  sockmanager->RegisterHandler(new MessageHandler());
149 
150  auto *controlRequestHandler = new ControlRequestHandler();
151  sockmanager->RegisterHandler(controlRequestHandler);
152  controlRequestHandler->ConnectToMaster();
153 
154  auto *sysEventHandler = new MythSystemEventHandler();
155 
156  // Provide systemd ready notification (for type=notify units)
157  ms_sd_notify("STATUS=");
158  ms_sd_notify("READY=1");
159 
160  int exitCode = QCoreApplication::exec();
161 
162  ms_sd_notify("STOPPING=1\nSTATUS=Exiting");
163  delete sysEventHandler;
164 
165  return exitCode ? exitCode : GENERIC_EXIT_OK;
166 }
167 
168 /* vim: set expandtab tabstop=4 shiftwidth=4: */
logfile
QString logfile
Definition: mythmediaserver.cpp:48
GENERIC_EXIT_SETUP_ERROR
@ GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:22
ms_sd_notify
static void ms_sd_notify(const char *)
Definition: mythmediaserver.cpp:40
UpgradeTVDatabaseSchema
bool UpgradeTVDatabaseSchema(const bool upgradeAllowed, const bool upgradeIfNoUI, const bool informSystemd)
Called from outside dbcheck.cpp to update the schema.
Definition: dbcheck.cpp:361
LOC
#define LOC
Definition: mythmediaserver.cpp:43
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
MythCoreContext::ConnectToMasterServer
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
Definition: mythcorecontext.cpp:355
mythmediaserver_commandlineparser.h
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
messagehandler.h
mythsocketmanager.h
mythdbcon.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MessageHandler
Definition: messagehandler.h:10
mythsystemevent.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
cleanup
static void cleanup(void)
Definition: mythmediaserver.cpp:50
MythSocketManager
Definition: mythsocketmanager.h:21
BaseRequestHandler
Definition: basehandler.h:13
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1553
mythlogging.h
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:14
MYTH_APPNAME_MYTHMEDIASERVER
static constexpr const char * MYTH_APPNAME_MYTHMEDIASERVER
Definition: mythcorecontext.h:33
dbcheck.h
MythCoreContext::GetBackendServerPort
int GetBackendServerPort(void)
Returns the locally defined backend control port.
Definition: mythcorecontext.cpp:1062
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:6
controlrequesthandler.h
MythCoreContext::GetBackendServerIP
QString GetBackendServerIP(void)
Returns the IP address of the locally defined backend IP.
Definition: mythcorecontext.cpp:1002
GENERIC_EXIT_SOCKET_ERROR
@ GENERIC_EXIT_SOCKET_ERROR
Socket error.
Definition: exitcodes.h:19
main
int main(int argc, char *argv[])
Definition: mythmediaserver.cpp:64
fileserverhandler.h
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1381
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1397
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:3037
pidfile
QString pidfile
Definition: mythmediaserver.cpp:47
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
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:127
GENERIC_EXIT_CONNECT_ERROR
@ GENERIC_EXIT_CONNECT_ERROR
Can't connect to master backend.
Definition: exitcodes.h:21
basehandler.h
FileServerHandler
Definition: fileserverhandler.h:12
ControlRequestHandler
Definition: controlrequesthandler.h:13
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2201
cleanupguard.h
GENERIC_EXIT_DB_OUTOFDATE
@ GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:17
mythcontext.h
MythMediaServerCommandLineParser
Definition: mythmediaserver_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
exitcodes.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
MythSystemEventHandler
Handles incoming MythSystemEvent messages.
Definition: mythsystemevent.h:24
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
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134