/
main.cpp
357 lines (314 loc) · 11 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* main.cpp
* ============================================================
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published bythe Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include "zmserver.h"
// default port to listen on
#define PORT 6548
// default location of zoneminders config file
#define ZM_CONFIG "/etc/zm.conf"
// Care should be taken to keep these in sync with the exit codes in
// libmythbase/exitcodes.h (which is not included here to keep this code
// separate from mythtv libraries).
#define EXIT_OK 0
#define EXIT_INVALID_CMDLINE 132
#define EXIT_OPENING_LOGFILE_ERROR 136 // mapped to _PERMISSIONS_ERROR
#define EXIT_DAEMONIZING_ERROR 145
#define EXIT_SOCKET_ERROR 135
using namespace std;
int main(int argc, char **argv)
{
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
struct sockaddr_in myaddr; // server address
struct sockaddr_in remoteaddr; // client address
struct timeval timeout; // maximum time to wait for data
int res; // result from select()
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
int newfd; // newly accept()ed socket descriptor
char buf[4096]; // buffer for client data
int nbytes;
int yes=1; // for setsockopt() SO_REUSEADDR, below
socklen_t addrlen;
int i;
bool quit = false; // quit flag
bool debug = false; // debug mode enabled
bool daemon_mode = false; // is daemon mode enabled
int port = PORT; // port we're listening on
string logfile = ""; // log file
string zmconfig = ZM_CONFIG; // location of zoneminders config file
// Check command line arguments
for (int argpos = 1; argpos < argc; ++argpos)
{
if (!strcmp(argv[argpos],"-d") ||
!strcmp(argv[argpos],"--daemon"))
{
daemon_mode = true;
}
else if (!strcmp(argv[argpos],"-n") ||
!strcmp(argv[argpos],"--nodaemon"))
{
daemon_mode = false;
}
else if (!strcmp(argv[argpos],"-p") ||
!strcmp(argv[argpos],"--port"))
{
if (argc > argpos)
{
port = atoi(argv[argpos+1]);
if (port < 1 || port > 65534)
{
cout << "Bad port number: " << port << endl;
return EXIT_INVALID_CMDLINE;
}
++argpos;
}
else
{
cout << "Missing argument to -p/--port option\n";
return EXIT_INVALID_CMDLINE;
}
}
else if (!strcmp(argv[argpos],"-l") ||
!strcmp(argv[argpos],"--logfile"))
{
if (argc > argpos)
{
logfile = argv[argpos+1];
if (logfile[0] == '-')
{
cerr << "Invalid or missing argument to -l/--logfile option\n";
return EXIT_INVALID_CMDLINE;
}
++argpos;
}
else
{
cerr << "Missing argument to -l/--logfile option\n";
return EXIT_INVALID_CMDLINE;
}
}
else if (!strcmp(argv[argpos],"-c") ||
!strcmp(argv[argpos],"--zmconfig"))
{
if (argc > argpos)
{
zmconfig = argv[argpos+1];
if (zmconfig[0] == '-')
{
cerr << "Invalid or missing argument to -c/--zmconfig option\n";
return EXIT_INVALID_CMDLINE;
}
++argpos;
}
else
{
cerr << "Missing argument to -c/--zmconfig option\n";
return EXIT_INVALID_CMDLINE;
}
}
else if (!strcmp(argv[argpos],"-v") ||
!strcmp(argv[argpos],"--verbose"))
{
debug = true;
}
else
{
cerr << "Invalid argument: " << argv[argpos] << endl <<
"Valid options are: " << endl <<
"-p or --port number A port number to listen on (default is 6548) " << endl <<
"-d or --daemon Runs mythzmserver as a daemon " << endl <<
"-n or --nodaemon Does not run mythzmserver as a daemon (default)" << endl <<
"-c or --zmconfig Location of zoneminders config file (default is " << ZM_CONFIG << ")" << endl <<
"-l or --logfile filename Writes STDERR and STDOUT messages to filename" << endl <<
"-v or --verbose Prints more debug output" << endl;
return EXIT_INVALID_CMDLINE;
}
}
// set up log file
int logfd = -1;
if (logfile != "")
{
logfd = open(logfile.c_str(), O_WRONLY|O_CREAT|O_APPEND, 0664);
if (logfd < 0)
{
perror("open(logfile)");
return EXIT_OPENING_LOGFILE_ERROR;
}
}
if (logfd != -1)
{
// Send stdout and stderr to the logfile
dup2(logfd, 1);
dup2(logfd, 2);
// Close the unduplicated logfd
if (logfd != 1 && logfd != 2)
close(logfd);
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
cout << "Unable to ignore SIGPIPE\n";
// Switch to daemon mode?
if (daemon_mode)
{
if (daemon(0, 0) < 0)
{
cout << "Failed to run as a daemon. Bailing out.\n";
return EXIT_DAEMONIZING_ERROR;
}
cout << endl;
}
map<int, ZMServer*> serverList; // list of ZMServers
// load the config
loadZMConfig(zmconfig);
cout << "ZM is version '" << g_zmversion << "'" << endl;
// connect to the DB
connectToDatabase();
// clear the master and temp sets
FD_ZERO(&master);
FD_ZERO(&read_fds);
// get the listener
if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return EXIT_SOCKET_ERROR;
}
// lose the pesky "address already in use" error message
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1)
{
perror("setsockopt");
return EXIT_SOCKET_ERROR;
}
// bind
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(port);
memset(&(myaddr.sin_zero), '\0', 8);
if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1)
{
perror("bind");
return EXIT_SOCKET_ERROR;
}
// listen
if (listen(listener, 10) == -1)
{
perror("listen");
return EXIT_SOCKET_ERROR;
}
cout << "Listening on port: " << port << endl;
// add the listener to the master set
FD_SET(listener, &master);
// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one
// main loop
while (!quit)
{
// the maximum time select() should wait
timeout.tv_sec = DB_CHECK_TIME;
timeout.tv_usec = 0;
read_fds = master; // copy it
res = select(fdmax+1, &read_fds, NULL, NULL, &timeout);
if (res == -1)
{
perror("select");
return EXIT_SOCKET_ERROR;
}
else if (res == 0)
{
// select timed out
// just kick the DB connection to keep it alive
kickDatabase(debug);
continue;
}
// run through the existing connections looking for data to read
for (i = 0; i <= fdmax; i++)
{
if (FD_ISSET(i, &read_fds))
{
// we got one!!
if (i == listener)
{
// handle new connections
addrlen = sizeof(remoteaddr);
if ((newfd = accept(listener,
(struct sockaddr *) &remoteaddr,
&addrlen)) == -1)
{
perror("accept");
}
else
{
// add to master set
FD_SET(newfd, &master);
if (newfd > fdmax)
{ // keep track of the maximum
fdmax = newfd;
}
// create new ZMServer and add to map
ZMServer *server = new ZMServer(newfd, debug);
serverList[newfd] = server;
printf("new connection from %s on socket %d\n",
inet_ntoa(remoteaddr.sin_addr), newfd);
}
}
else
{
// handle data from a client
if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0)
{
// got error or connection closed by client
if (nbytes == 0)
{
// connection closed
printf("socket %d hung up\n", i);
}
else
{
perror("recv");
}
close(i);
// remove from master set
FD_CLR(i, &master);
// remove from server list
ZMServer *server = serverList[i];
if (server)
delete server;
serverList.erase(i);
}
else
{
ZMServer *server = serverList[i];
quit = server->processRequest(buf, nbytes);
}
}
}
}
}
mysql_close(&g_dbConn);
return EXIT_OK;
}