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  retval = cmdline.ConfigureLogging(mask, daemonize);
95  if (retval != GENERIC_EXIT_OK)
96  return retval;
97 
98  CleanupGuard callCleanup(cleanup);
99 
100 #ifndef _WIN32
102 #endif
103 
104  ms_sd_notify("STATUS=Connecting to database.");
105  gContext = new MythContext(MYTH_BINARY_VERSION);
106  if (!gContext->Init(false))
107  {
108  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init MythContext, exiting.");
110  }
111 
112  if (!UpgradeTVDatabaseSchema(false, false, true))
113  {
114  LOG(VB_GENERAL, LOG_ERR, "Exiting due to schema mismatch.");
116  }
117 
119 
120  gCoreContext->SetAsBackend(true); // blocks the event connection
121  ms_sd_notify("STATUS=Connecting to master server.");
123  {
124  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect to master server");
126  }
127 
128  int port = gCoreContext->GetBackendServerPort();
129  if (gCoreContext->GetBackendServerIP().isEmpty())
130  {
131  std::cerr << "No setting found for this machine's BackendServerIP.\n"
132  << "Please run setup on this machine and modify the first page\n"
133  << "of the general settings.\n";
135  }
136 
137  ms_sd_notify("STATUS=Creating socket manager");
138  auto *sockmanager = new MythSocketManager();
139  if (!sockmanager->Listen(port))
140  {
141  LOG(VB_GENERAL, LOG_ERR,
142  "Mediaserver exiting, failed to bind to listen port.");
143  delete sockmanager;
145  }
146 
147  sockmanager->RegisterHandler(new BaseRequestHandler());
148  sockmanager->RegisterHandler(new FileServerHandler());
149  sockmanager->RegisterHandler(new MessageHandler());
150 
151  auto *controlRequestHandler = new ControlRequestHandler();
152  sockmanager->RegisterHandler(controlRequestHandler);
153  controlRequestHandler->ConnectToMaster();
154 
155  auto *sysEventHandler = new MythSystemEventHandler();
156 
157  // Provide systemd ready notification (for type=notify units)
158  ms_sd_notify("STATUS=");
159  ms_sd_notify("READY=1");
160 
161  int exitCode = QCoreApplication::exec();
162 
163  ms_sd_notify("STOPPING=1\nSTATUS=Exiting");
164  delete sysEventHandler;
165  delete sockmanager;
166 
167  return exitCode ? exitCode : GENERIC_EXIT_OK;
168 }
169 
170 /* vim: set expandtab tabstop=4 shiftwidth=4: */
logfile
QString logfile
Definition: mythmediaserver.cpp:48
ms_sd_notify
static void ms_sd_notify(const char *)
Definition: mythmediaserver.cpp:40
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:357
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
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:1552
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
mythlogging.h
UpgradeTVDatabaseSchema
bool UpgradeTVDatabaseSchema(const bool upgradeAllowed, const bool upgradeIfNoUI, [[maybe_unused]] const bool informSystemd)
Called from outside dbcheck.cpp to update the schema.
Definition: dbcheck.cpp:362
MYTH_APPNAME_MYTHMEDIASERVER
static constexpr const char * MYTH_APPNAME_MYTHMEDIASERVER
Definition: mythcorecontext.h:32
dbcheck.h
MythCoreContext::GetBackendServerPort
int GetBackendServerPort(void)
Returns the locally defined backend control port.
Definition: mythcorecontext.cpp:1068
signalhandling.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
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:1008
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:1380
GENERIC_EXIT_CONNECT_ERROR
@ GENERIC_EXIT_CONNECT_ERROR
Can't connect to master backend.
Definition: exitcodes.h:23
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1396
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:3047
pidfile
QString pidfile
Definition: mythmediaserver.cpp:47
GENERIC_EXIT_DB_OUTOFDATE
@ GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:19
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
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:128
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:2197
GENERIC_EXIT_SETUP_ERROR
@ GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:24
cleanupguard.h
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:2870
exitcodes.h
MythSystemEventHandler
Handles incoming MythSystemEvent messages.
Definition: mythsystemevent.h:24
GENERIC_EXIT_SOCKET_ERROR
@ GENERIC_EXIT_SOCKET_ERROR
Socket error.
Definition: exitcodes.h:21
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
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:135
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:16