/
main.cpp
335 lines (291 loc) · 9.25 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <QCoreApplication>
#include <QString>
#include <QRegExp>
#include <QDir>
#include "exitcodes.h"
#include "mythcontext.h"
#include "jobqueue.h"
#include "mythdbcon.h"
#include "mythverbose.h"
#include "mythversion.h"
#include "mythcommandlineparser.h"
#include "compat.h"
#include "mythsystemevent.h"
#define LOC QString("MythJobQueue: ")
#define LOC_WARN QString("MythJobQueue, Warning: ")
#define LOC_ERR QString("MythJobQueue, Error: ")
using namespace std;
JobQueue *jobqueue = NULL;
QString pidfile;
QString logfile = QString::null;
static void cleanup(void)
{
delete gContext;
gContext = NULL;
if (pidfile.size())
{
unlink(pidfile.toAscii().constData());
pidfile.clear();
}
signal(SIGHUP, SIG_DFL);
}
static int log_rotate(int report_error)
{
int new_logfd = open(logfile.toLocal8Bit().constData(),
O_WRONLY|O_CREAT|O_APPEND|O_SYNC, 0664);
if (new_logfd < 0)
{
// If we can't open the new logfile, send data to /dev/null
if (report_error)
{
VERBOSE(VB_IMPORTANT, LOC_ERR +
QString("Cannot open logfile '%1'").arg(logfile));
return -1;
}
new_logfd = open("/dev/null", O_WRONLY);
if (new_logfd < 0)
{
// There's not much we can do, so punt.
return -1;
}
}
while (dup2(new_logfd, 1) < 0 && errno == EINTR) ;
while (dup2(new_logfd, 2) < 0 && errno == EINTR) ;
while (close(new_logfd) < 0 && errno == EINTR) ;
return 0;
}
static void log_rotate_handler(int)
{
log_rotate(0);
}
namespace
{
class CleanupGuard
{
public:
typedef void (*CleanupFunc)();
public:
CleanupGuard(CleanupFunc cleanFunction) :
m_cleanFunction(cleanFunction) {}
~CleanupGuard()
{
m_cleanFunction();
}
private:
CleanupFunc m_cleanFunction;
};
}
int main(int argc, char *argv[])
{
bool cmdline_err;
MythCommandLineParser cmdline(
kCLPOverrideSettingsFile |
kCLPOverrideSettings |
kCLPQueryVersion);
for (int argpos = 0; argpos < argc; ++argpos)
{
if (cmdline.PreParse(argc, argv, argpos, cmdline_err))
{
if (cmdline_err)
return GENERIC_EXIT_INVALID_CMDLINE;
if (cmdline.WantsToExit())
return GENERIC_EXIT_OK;
}
}
QCoreApplication a(argc, argv);
QMap<QString, QString> settingsOverride;
int argpos = 1;
bool daemonize = false;
QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHJOBQUEUE);
QString filename;
while (argpos < a.argc())
{
if (!strcmp(a.argv()[argpos],"-v") ||
!strcmp(a.argv()[argpos],"--verbose"))
{
if (a.argc()-1 > argpos)
{
if (parse_verbose_arg(a.argv()[argpos+1]) ==
GENERIC_EXIT_INVALID_CMDLINE)
return GENERIC_EXIT_INVALID_CMDLINE;
++argpos;
} else
{
cerr << "Missing argument to -v/--verbose option\n";
return GENERIC_EXIT_INVALID_CMDLINE;
}
}
else if (!strcmp(a.argv()[argpos],"-l") ||
!strcmp(a.argv()[argpos],"--logfile"))
{
if (a.argc() > argpos)
{
logfile = a.argv()[argpos+1];
if (logfile.startsWith("-"))
{
cerr << "Invalid or missing argument to -l/--logfile option\n";
return GENERIC_EXIT_INVALID_CMDLINE;
}
else
{
++argpos;
}
}
}
else if (!strcmp(a.argv()[argpos],"-p") ||
!strcmp(a.argv()[argpos],"--pidfile"))
{
if (a.argc() > argpos)
{
pidfile = a.argv()[argpos+1];
if (pidfile.startsWith("-"))
{
cerr << "Invalid or missing argument to -p/--pidfile option\n";
return GENERIC_EXIT_INVALID_CMDLINE;
}
else
{
++argpos;
}
}
}
else if (!strcmp(a.argv()[argpos],"-d") ||
!strcmp(a.argv()[argpos],"--daemon"))
{
daemonize = true;
}
else if (!strcmp(a.argv()[argpos],"-O") ||
!strcmp(a.argv()[argpos],"--override-setting"))
{
if ((a.argc() - 1) > argpos)
{
QString tmpArg = a.argv()[argpos+1];
if (tmpArg.startsWith("-"))
{
cerr << "Invalid or missing argument to "
"-O/--override-setting option\n";
return GENERIC_EXIT_INVALID_CMDLINE;
}
QStringList pairs = tmpArg.split(",");
for (int index = 0; index < pairs.size(); ++index)
{
QStringList tokens = pairs[index].split("=");
tokens[0].replace(QRegExp("^[\"']"), "");
tokens[0].replace(QRegExp("[\"']$"), "");
tokens[1].replace(QRegExp("^[\"']"), "");
tokens[1].replace(QRegExp("[\"']$"), "");
settingsOverride[tokens[0]] = tokens[1];
}
}
else
{
cerr << "Invalid or missing argument to -O/--override-setting "
"option\n";
return GENERIC_EXIT_INVALID_CMDLINE;
}
++argpos;
}
else if (!strcmp(a.argv()[argpos],"-h") ||
!strcmp(a.argv()[argpos],"--help"))
{
cerr << "Valid Options are:" << endl <<
"-v or --verbose debug-level Use '-v help' for level info" << endl <<
"-l or --logfile filename Writes STDERR and STDOUT messages to filename" << endl <<
"-p or --pidfile filename Write PID of mythjobqueue to filename " <<
"-d or --daemon Runs mythjobqueue as a daemon" << endl <<
endl;
return GENERIC_EXIT_INVALID_CMDLINE;
}
else if (cmdline.Parse(a.argc(), a.argv(), argpos, cmdline_err))
{
if (cmdline_err)
return GENERIC_EXIT_INVALID_CMDLINE;
if (cmdline.WantsToExit())
return GENERIC_EXIT_OK;
}
else
{
printf("illegal option: '%s' (use --help)\n", a.argv()[argpos]);
return GENERIC_EXIT_INVALID_CMDLINE;
}
++argpos;
}
if (!logfile.isEmpty())
{
if (log_rotate(1) < 0)
{
VERBOSE(VB_IMPORTANT, LOC_WARN +
"Cannot open logfile; using stdout/stderr instead");
}
else
signal(SIGHUP, &log_rotate_handler);
}
CleanupGuard callCleanup(cleanup);
ofstream pidfs;
if (pidfile.size())
{
pidfs.open(pidfile.toAscii().constData());
if (!pidfs)
{
VERBOSE(VB_IMPORTANT, LOC_ERR +
"Could not open pid file" + ENO);
return GENERIC_EXIT_PERMISSIONS_ERROR;
}
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
VERBOSE(VB_IMPORTANT, LOC_WARN + "Unable to ignore SIGPIPE");
if (daemonize && (daemon(0, 1) < 0))
{
VERBOSE(VB_IMPORTANT, LOC_ERR + "Failed to daemonize" + ENO);
return GENERIC_EXIT_DAEMONIZING_ERROR;
}
if (pidfs)
{
pidfs << getpid() << endl;
pidfs.close();
}
VERBOSE(VB_IMPORTANT, QString("%1 version: %2 [%3] www.mythtv.org")
.arg(MYTH_APPNAME_MYTHJOBQUEUE)
.arg(MYTH_SOURCE_PATH)
.arg(MYTH_SOURCE_VERSION));
gContext = new MythContext(MYTH_BINARY_VERSION);
if (!gContext->Init(false))
{
VERBOSE(VB_IMPORTANT, LOC_ERR + "Failed to init MythContext, exiting.");
return GENERIC_EXIT_NO_MYTHCONTEXT;
}
if (settingsOverride.size())
{
QMap<QString, QString>::iterator it;
for (it = settingsOverride.begin(); it != settingsOverride.end(); ++it)
{
VERBOSE(VB_IMPORTANT, QString("Setting '%1' being forced to '%2'")
.arg(it.key()).arg(*it));
gCoreContext->OverrideSettingForSession(it.key(), *it);
}
}
if (!gCoreContext->ConnectToMasterServer())
{
VERBOSE(VB_IMPORTANT, LOC_ERR + "Failed to connect to master server");
return GENERIC_EXIT_CONNECT_ERROR;
}
jobqueue = new JobQueue(false);
MythSystemEventHandler *sysEventHandler = new MythSystemEventHandler();
int exitCode = a.exec();
if (sysEventHandler)
delete sysEventHandler;
return exitCode ? exitCode : GENERIC_EXIT_OK;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */