Files @ c538d9965b82
Branch filter:

Location: libtransport.git/spectrum/src/main.cpp - annotation

HanzZ
Handle backendConfig
  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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
742ed6bc6987
e863d8551966
e863d8551966
e863d8551966
2131b86869ca
56dd64999814
e863d8551966
320738eda85a
a06a47ed110e
cfa85cd21caf
851ef182607a
851ef182607a
e863d8551966
e863d8551966
b99313a73a91
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
8acda88ffe61
e863d8551966
d8e683f17ea1
d8e683f17ea1
84d1dc97559a
e863d8551966
e863d8551966
e863d8551966
e863d8551966
bbafb94d6f58
e863d8551966
7c93aee6f49a
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
56dd64999814
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
cd2501c4360d
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
47f86754eae6
e863d8551966
e863d8551966
e863d8551966
e863d8551966
02daddc0ce32
81bcacf6953d
81bcacf6953d
8acda88ffe61
8acda88ffe61
8acda88ffe61
8acda88ffe61
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
29b5b7d27f6b
e863d8551966
e863d8551966
e863d8551966
6c6071f30bf0
02daddc0ce32
e863d8551966
29b5b7d27f6b
e863d8551966
98982a60fba3
84d1dc97559a
84d1dc97559a
84d1dc97559a
98982a60fba3
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e1d14b695134
e863d8551966
e863d8551966
29b5b7d27f6b
29b5b7d27f6b
29b5b7d27f6b
29b5b7d27f6b
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
98982a60fba3
84d1dc97559a
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
98982a60fba3
84d1dc97559a
98982a60fba3
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
02daddc0ce32
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e17059a6a677
e17059a6a677
14e450f7b5a8
e17059a6a677
e17059a6a677
14e450f7b5a8
e17059a6a677
e17059a6a677
14e450f7b5a8
e17059a6a677
e17059a6a677
14e450f7b5a8
14e450f7b5a8
14e450f7b5a8
e17059a6a677
e17059a6a677
14e450f7b5a8
e17059a6a677
e17059a6a677
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
14e450f7b5a8
e17059a6a677
bbafb94d6f58
bbafb94d6f58
bbafb94d6f58
bbafb94d6f58
29888fae6e1e
e17059a6a677
e17059a6a677
e17059a6a677
e17059a6a677
e17059a6a677
e863d8551966
e17059a6a677
e17059a6a677
e17059a6a677
e863d8551966
e863d8551966
e17059a6a677
e17059a6a677
e863d8551966
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
fd54120de795
e17059a6a677
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
c290016c8e99
b99313a73a91
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
f6d4966dd56d
e863d8551966
e863d8551966
e863d8551966
e863d8551966
f6d4966dd56d
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
f6d4966dd56d
e863d8551966
e863d8551966
e863d8551966
e863d8551966
f6d4966dd56d
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
6c6071f30bf0
6c6071f30bf0
6c6071f30bf0
6c6071f30bf0
6c6071f30bf0
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
48f2d9d6b146
48f2d9d6b146
48f2d9d6b146
a06a47ed110e
a06a47ed110e
a06a47ed110e
a06a47ed110e
93418187e2a7
8dfcbc87728f
48f2d9d6b146
48f2d9d6b146
93418187e2a7
93418187e2a7
c290016c8e99
8dfcbc87728f
e863d8551966
e863d8551966
56dd64999814
e863d8551966
56dd64999814
e863d8551966
e863d8551966
e863d8551966
56dd64999814
56dd64999814
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
981513b2a52c
8b7923f3e3f4
8b7923f3e3f4
2131b86869ca
2131b86869ca
e863d8551966
320738eda85a
320738eda85a
320738eda85a
cfa85cd21caf
cfa85cd21caf
cfa85cd21caf
8bb71e514842
851ef182607a
851ef182607a
851ef182607a
851ef182607a
851ef182607a
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
e863d8551966
56dd64999814
56dd64999814
56dd64999814
56dd64999814
56dd64999814
e863d8551966
e863d8551966
a06a47ed110e
e863d8551966
#include "transport/config.h"
#include "transport/transport.h"
#include "transport/filetransfermanager.h"
#include "transport/usermanager.h"
#include "transport/logger.h"
#include "transport/sqlite3backend.h"
#include "transport/mysqlbackend.h"
#include "transport/pqxxbackend.h"
#include "transport/userregistration.h"
#include "transport/networkpluginserver.h"
#include "transport/admininterface.h"
#include "transport/statsresponder.h"
#include "transport/usersreconnecter.h"
#include "transport/util.h"
#include "transport/gatewayresponder.h"
#include "transport/logging.h"
#include "transport/discoitemsresponder.h"
#include "transport/adhocmanager.h"
#include "transport/settingsadhoccommand.h"
#include "Swiften/EventLoop/SimpleEventLoop.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#ifndef WIN32
#include "sys/signal.h"
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>
#include "libgen.h"
#include <malloc.h>
#else
#include <process.h>
#define getpid _getpid
// #include "win32/SpectrumService.h"
#endif
#include <sys/stat.h>

using namespace Transport;
using namespace Transport::Util;

DEFINE_LOGGER(logger, "Spectrum");

Swift::SimpleEventLoop *eventLoop_ = NULL;
Component *component_ = NULL;
UserManager *userManager_ = NULL;

static void stop_spectrum() {
	userManager_->removeAllUsers(false);
	component_->stop();
	eventLoop_->stop();
}

static void spectrum_sigint_handler(int sig) {
	eventLoop_->postEvent(&stop_spectrum);
}

static void spectrum_sigterm_handler(int sig) {
	eventLoop_->postEvent(&stop_spectrum);
}

static void removeOldIcons(std::string iconDir) {
	std::vector<std::string> dirs;
	dirs.push_back(iconDir);

	boost::thread thread(boost::bind(Util::removeEverythingOlderThan, dirs, time(NULL) - 3600*24*14));
}

#ifndef WIN32
static void daemonize(const char *cwd, const char *lock_file) {
	pid_t pid, sid;
	FILE* lock_file_f;
	char process_pid[20];
	/* already a daemon */
	if ( getppid() == 1 ) return;

	/* Fork off the parent process */
	pid = fork();
	if (pid < 0) {
		exit(1);
	}
	/* If we got a good PID, then we can exit the parent process. */
	if (pid > 0) {
		if (lock_file) {
			/* write our pid into it & close the file. */
			lock_file_f = fopen(lock_file, "w+");
			if (lock_file_f == NULL) {
				std::cerr << "Cannot create lock file " << lock_file << ". Exiting\n";
				exit(1);
			}
			sprintf(process_pid,"%d\n",pid);
			if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) {
				std::cerr << "Cannot write to lock file " << lock_file << ". Exiting\n";
				exit(1);
			}
			fclose(lock_file_f);
		}
		exit(0);
	}

	/* Change the file mode mask */
	umask(0);

	/* Create a new SID for the child process */
	sid = setsid();
	if (sid < 0) {
		exit(1);
	}

	/* Change the current working directory.  This prevents the current
		directory from being locked; hence not being able to remove it. */
	if ((chdir(cwd)) < 0) {
		exit(1);
	}
	
	if (freopen( "/dev/null", "r", stdin) == NULL) {
		std::cout << "EE cannot open /dev/null. Exiting\n";
		exit(1);
	}
}

#endif

int main(int argc, char **argv)
{
	Config config(argc, argv);

	boost::program_options::variables_map vm;
	bool no_daemon = false;
	std::string config_file;
	std::string jid;

	setlocale(LC_ALL, "");
#ifndef WIN32
	mallopt(M_CHECK_ACTION, 2);
	mallopt(M_PERTURB, 0xb);
#endif

#ifndef WIN32
	if (signal(SIGINT, spectrum_sigint_handler) == SIG_ERR) {
		std::cout << "SIGINT handler can't be set\n";
		return -1;
	}

	if (signal(SIGTERM, spectrum_sigterm_handler) == SIG_ERR) {
		std::cout << "SIGTERM handler can't be set\n";
		return -1;
	}
#endif
	boost::program_options::options_description desc(std::string("Spectrum version: ") + SPECTRUM_VERSION + "\nUsage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
	desc.add_options()
		("help,h", "help")
		("no-daemonize,n", "Do not run spectrum as daemon")
		("no-debug,d", "Create coredumps on crash")
		("jid,j", boost::program_options::value<std::string>(&jid)->default_value(""), "Specify JID of transport manually")
		("config", boost::program_options::value<std::string>(&config_file)->default_value(""), "Config file")
		("version,v", "Shows Spectrum version")
		;
#ifdef WIN32
// 	desc.add_options()
// 		("install-service,i", "Install spectrum as Windows service")
// 		("uninstall-service,u", "Uninstall Windows service");
#endif
	try
	{
		boost::program_options::positional_options_description p;
		p.add("config", -1);
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
          options(desc).positional(p).allow_unregistered().run(), vm);
		boost::program_options::notify(vm);

		if (vm.count("version")) {
			std::cout << SPECTRUM_VERSION << "\n";
			return 0;
		}

		if(vm.count("help"))
		{
			std::cout << desc << "\n";
			return 1;
		}

		if(vm.count("config") == 0) {
			std::cout << desc << "\n";
			return 1;
		}

		if(vm.count("no-daemonize")) {
			no_daemon = true;
		}
#ifdef WIN32
#if 0
		if (vm.count("install-service")) {
			SpectrumService ntservice;
			if (!ntservice.IsInstalled()) {
					// determine the name of the currently executing file
				char szFilePath[MAX_PATH];
				GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
				std::string exe_file(szFilePath);
				std::string config_file = exe_file.replace(exe_file.end() - 4, exe_file.end(), ".cfg");
				std::string service_path = std::string(szFilePath) + std::string(" --config ") + config_file;

				if (ntservice.Install(service_path.c_str())) {
					std::cout << "Successfully installed" << std::endl;
					return 0;
				} else {
					std::cout << "Error installing service, are you an Administrator?" << std::endl;
					return 1;
				}                				
			} else {
				std::cout << "Already installed" << std::endl;
				return 1;
			}
		}
		if (vm.count("uninstall-service")) {
			SpectrumService ntservice;
			if (ntservice.IsInstalled()) {
				if (ntservice.Remove()) {
					std::cout << "Successfully removed" << std::endl;
					return 0;
				} else {
					std::cout << "Error removing service, are you an Administrator?" << std::endl;
					return 1;
				}                               				
			} else {
				std::cout << "Service not installed" << std::endl;
				return 1;
			}
		}
#endif
#endif
	}
	catch (std::runtime_error& e)
	{
		std::cout << desc << "\n";
		return 1;
	}
	catch (...)
	{
		std::cout << desc << "\n";
		return 1;
	}

	if (!config.load(vm["config"].as<std::string>(), jid)) {
		std::cerr << "Can't load configuration file.\n";
		return 1;
	}

	// create directories
	try {
		boost::filesystem::create_directories(CONFIG_STRING(&config, "service.working_dir"));
	}
	catch (...) {
		std::cerr << "Can't create service.working_dir directory " << CONFIG_STRING(&config, "service.working_dir") << ".\n";
		return 1;
	}
#ifndef WIN32
	// create directories
	try {
		boost::filesystem::create_directories(
			boost::filesystem::path(CONFIG_STRING(&config, "service.pidfile")).parent_path().string()
		);
	}
	catch (...) {
		std::cerr << "Can't create service.pidfile directory " << boost::filesystem::path(CONFIG_STRING(&config, "service.pidfile")).parent_path().string() << ".\n";
		return 1;
	}
	// create directories
	try {
		boost::filesystem::create_directories(
			boost::filesystem::path(CONFIG_STRING(&config, "service.portfile")).parent_path().string()
		);
	}
	catch (...) {
		std::cerr << "Can't create service.portfile directory " << CONFIG_STRING(&config, "service.portfile") << ".\n";
		return 1;
	}
#endif

#ifdef WIN32
	SetCurrentDirectory( utf8ToUtf16(CONFIG_STRING(&config, "service.working_dir")).c_str() );
#endif

#ifndef WIN32
	if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) {
		struct group *gr;
		if ((gr = getgrnam(CONFIG_STRING(&config, "service.group").c_str())) == NULL) {
			std::cerr << "Invalid service.group name " << CONFIG_STRING(&config, "service.group") << "\n";
			return 1;
		}
		struct passwd *pw;
		if ((pw = getpwnam(CONFIG_STRING(&config, "service.user").c_str())) == NULL) {
			std::cerr << "Invalid service.user name " << CONFIG_STRING(&config, "service.user") << "\n";
			return 1;
		}
		chown(CONFIG_STRING(&config, "service.working_dir").c_str(), pw->pw_uid, gr->gr_gid);
	}

	char backendport[20];
	FILE* port_file_f;
	port_file_f = fopen(CONFIG_STRING(&config, "service.portfile").c_str(), "w+");
	if (port_file_f == NULL) {
		std::cerr << "Cannot create port_file file " << CONFIG_STRING(&config, "service.portfile").c_str() << ". Exiting\n";
		exit(1);
	}
	sprintf(backendport,"%s\n",CONFIG_STRING(&config, "service.backend_port").c_str());
	if (fwrite(backendport,1,strlen(backendport),port_file_f) < strlen(backendport)) {
		std::cerr << "Cannot write to port file " << CONFIG_STRING(&config, "service.portfile") << ". Exiting\n";
		exit(1);
	}
	fclose(port_file_f);

	if (!no_daemon) {
		// daemonize
		daemonize(CONFIG_STRING(&config, "service.working_dir").c_str(), CONFIG_STRING(&config, "service.pidfile").c_str());
// 		removeOldIcons(CONFIG_STRING(&config, "service.working_dir") + "/icons");
    }
#endif

	Logging::initMainLogging(&config);

#ifndef WIN32
	if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) {
		struct rlimit limit;
		getrlimit(RLIMIT_CORE, &limit);

		if (!CONFIG_STRING(&config, "service.group").empty()) {
			struct group *gr;
			if ((gr = getgrnam(CONFIG_STRING(&config, "service.group").c_str())) == NULL) {
				std::cerr << "Invalid service.group name " << CONFIG_STRING(&config, "service.group") << "\n";
				return 1;
			}

			if (((setgid(gr->gr_gid)) != 0) || (initgroups(CONFIG_STRING(&config, "service.user").c_str(), gr->gr_gid) != 0)) {
				std::cerr << "Failed to set service.group name " << CONFIG_STRING(&config, "service.group") << " - " << gr->gr_gid << ":" << strerror(errno) << "\n";
				return 1;
			}
		}

		if (!CONFIG_STRING(&config, "service.user").empty()) {
			struct passwd *pw;
			if ((pw = getpwnam(CONFIG_STRING(&config, "service.user").c_str())) == NULL) {
				std::cerr << "Invalid service.user name " << CONFIG_STRING(&config, "service.user") << "\n";
				return 1;
			}

			if ((setuid(pw->pw_uid)) != 0) {
				std::cerr << "Failed to set service.user name " << CONFIG_STRING(&config, "service.user") << " - " << pw->pw_uid << ":" << strerror(errno) << "\n";
				return 1;
			}
		}
		setrlimit(RLIMIT_CORE, &limit);
	}

	struct rlimit limit;
	limit.rlim_max = RLIM_INFINITY;
	limit.rlim_cur = RLIM_INFINITY;
	setrlimit(RLIMIT_CORE, &limit);
#endif

	Swift::SimpleEventLoop eventLoop;

	Swift::BoostNetworkFactories *factories = new Swift::BoostNetworkFactories(&eventLoop);
	UserRegistry userRegistry(&config, factories);

	Component transport(&eventLoop, factories, &config, NULL, &userRegistry);
	component_ = &transport;
// 	Logger logger(&transport);

	std::string error;
	StorageBackend *storageBackend = StorageBackend::createBackend(&config, error);
	if (storageBackend == NULL) {
		if (!error.empty()) {
			std::cerr << error << "\n";
			return -2;
		}
	}
	else if (!storageBackend->connect()) {
		std::cerr << "Can't connect to database. Check the log to find out the reason.\n";
		return -1;
	}

	Logging::redirect_stderr();

	UserManager userManager(&transport, &userRegistry, storageBackend);
	userManager_ = &userManager;

	UserRegistration *userRegistration = NULL;
	UsersReconnecter *usersReconnecter = NULL;
	if (storageBackend) {
		userRegistration = new UserRegistration(&transport, &userManager, storageBackend);
		userRegistration->start();

		usersReconnecter = new UsersReconnecter(&transport, storageBackend);
	}

	FileTransferManager ftManager(&transport, &userManager);

	NetworkPluginServer plugin(&transport, &config, &userManager, &ftManager);

	AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend, userRegistration);
	plugin.setAdminInterface(&adminInterface);

	StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend);
	statsResponder.start();

	GatewayResponder gatewayResponder(transport.getIQRouter(), &userManager);
	gatewayResponder.start();

	DiscoItemsResponder discoItemsResponder(&transport);
	discoItemsResponder.start();

	AdHocManager adhocmanager(&transport, &discoItemsResponder, &userManager, storageBackend);
	adhocmanager.start();

	SettingsAdHocCommandFactory settings;
	adhocmanager.addAdHocCommand(&settings);

	eventLoop_ = &eventLoop;

	eventLoop.run();

	if (userRegistration) {
		userRegistration->stop();
		delete userRegistration;
	}

	if (usersReconnecter) {
		delete usersReconnecter;
	}

	delete storageBackend;
	delete factories;
	return 0;
}