Exemple #1
0
std::string Server::send_command(const std::string &jid, const std::string &cmd, int timeout) {
	Swift::SimpleEventLoop eventLoop;
	Swift::BoostNetworkFactories networkFactories(&eventLoop);

	try {
		ask_local_server(m_config, networkFactories, jid, cmd);
		struct timeval td_start,td_end;
		float elapsed = 0; 
		gettimeofday(&td_start, NULL);
		gettimeofday(&td_end, NULL);

		time_t started = time(NULL);
		while(get_response().empty() && td_end.tv_sec - td_start.tv_sec < timeout) {
			gettimeofday(&td_end, NULL);
			eventLoop.runOnce();
		}
	}
	catch (const boost::bad_any_cast &e) {
		LOG4CXX_ERROR(logger, "Server::send_command: bad_any_cast: " << e.what());
		return "";
	}

	std::string response = get_response();
	if (response == "Empty response") {
		return "";
	}

	return response;
}
Exemple #2
0
int main(int argc, char **argv)
{
	Config config;

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

#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")
		;
	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).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;
		}
	}
	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(
			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(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
	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);
	}

	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;
	}

	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);
	StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend);
	statsResponder.start();

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

	eventLoop_ = &eventLoop;

	eventLoop.run();

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

	if (usersReconnecter) {
		delete usersReconnecter;
	}

	delete storageBackend;
	delete factories;
	return 0;
}
Exemple #3
0
int mainloop() {

#ifndef WIN32
	mode_t old_cmask = umask(0007);
#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();

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

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

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

		usersReconnecter = new UsersReconnecter(&transport, storageBackend);
	}
	else if (!CONFIG_BOOL(config_, "service.server_mode")) {
		LOG4CXX_WARN(logger, "Registrations won't work, you have specified [database] type=none in config file.");
	}

	FileTransferManager ftManager(&transport, &userManager);

	NetworkPluginServer plugin(&transport, config_, &userManager, &ftManager, &discoItemsResponder);
	plugin.start();

	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();

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

	SettingsAdHocCommandFactory settings;
	adhocmanager.addAdHocCommand(&settings);

	eventLoop_ = &eventLoop;

	eventLoop.run();

#ifndef WIN32
	umask(old_cmask);
#endif

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

	if (usersReconnecter) {
		delete usersReconnecter;
	}

	delete storageBackend;
	delete factories;
	return 0;
}
Exemple #4
0
int mainloop() {

#ifndef WIN32
	mode_t old_cmask = umask(0007);
#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);

    
	Frontend *frontend = NULL;
	
	std::string frontend_name = CONFIG_STRING_DEFAULTED(config_, "service.frontend", "xmpp");
	std::string plugin_fc = "create_" + frontend_name + "_frontend_plugin";

	dll::shared_library self(dll::program_location());
	boost::function<boost::shared_ptr<FrontendPlugin>()> creator;

	try {
		creator = self.get_alias<boost::shared_ptr<FrontendPlugin>()>(plugin_fc);
	}
	catch (...) {
	}
	
	if (!creator) {
		LOG4CXX_ERROR(logger, "Unknown Frontend name " << frontend_name);
		return -3;
	}

	frontend = creator()->createFrontend();

	Component transport(frontend, &eventLoop, factories, config_, NULL, &userRegistry);
	component_ = &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 = frontend->createUserManager(&transport, &userRegistry, storageBackend);;
	UserRegistration *userRegistration = userManager->getUserRegistration();

	UsersReconnecter *usersReconnecter = NULL;
	if (storageBackend) {
		usersReconnecter = new UsersReconnecter(&transport, storageBackend);
	}
	else if (!CONFIG_BOOL(config_, "service.server_mode")) {
		LOG4CXX_WARN(logger, "Registrations won't work, you have specified [database] type=none in config file.");
	}

	FileTransferManager ftManager(&transport, userManager);

	NetworkPluginServer plugin(&transport, config_, userManager, &ftManager);
	plugin.start();

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

	eventLoop_ = &eventLoop;

	eventLoop.run();

#ifndef WIN32
	umask(old_cmask);
#endif

	delete userManager;

	if (usersReconnecter) {
		delete usersReconnecter;
	}

	delete storageBackend;
	delete factories;
	return 0;
}
Exemple #5
0
int main(int argc, char **argv)
{
	ManagerConfig config;
	std::string config_file;
	std::vector<std::string> command;
	boost::program_options::variables_map vm;

	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\n"
													 "       spectrum [OPTIONS] <instance_JID> <other>\nCommands:\n"
													 " start - start all local Spectrum2 instances\n"
													 " stop  - stop all local Spectrum2 instances\n"
													 " restart  - restart all local Spectrum2 instances\n"
													 " status - status of local Spectrum2 instances\n"
													 " <other> - send command to local Spectrum2 instance and print output\n"
													 "Allowed options");
	desc.add_options()
		("help,h", "Show help output")
		("config,c", boost::program_options::value<std::string>(&config_file)->default_value("/etc/spectrum2/spectrum_manager.cfg"), "Spectrum manager config file")
		("command", boost::program_options::value<std::vector<std::string> >(&command), "Command")
		;
	try
	{
		boost::program_options::positional_options_description p;
		p.add("command", -1);
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
          options(desc).positional(p).run(), vm);
		boost::program_options::notify(vm);

		if(vm.count("help"))
		{
			std::cout << desc << "\n";
			return 1;
		}
	}
	catch (std::runtime_error& e)
	{
		std::cout << desc << "\n";
		return 2;
	}
	catch (...)
	{
		std::cout << desc << "\n";
		return 3;
	}

	if (!config.load(config_file)) {
		std::cerr << "Can't load configuration file.\n";
		return 4;
	}

	if (command.empty()) {
		std::cout << desc << "\n";
		return 1;
	}

	if (command[0] == "start") {
		return start_instances(&config);
	}
	else if (command[0] == "stop") {
		stop_instances(&config);
	}
	else if (command[0] == "status") {
		return show_status(&config);
	}
	else if (command[0] == "list") {
		std::vector<std::string> list = show_list(&config);
	}
	else if (command[0] == "restart") {
		return restart_instances(&config);
	}
	else if (command[0] == "server") {
		Server server(&config);
		if (server.start() == false) {
			std::cerr << "Can't set up server handler.\n";
			return 1;
		}
		while (1) { sleep(10); }
	}
	else {
		if (command.size() < 2) {
			std::cout << desc << "\n";
			return 11;
		}
		Swift::SimpleEventLoop eventLoop;
		Swift::BoostNetworkFactories networkFactories(&eventLoop);

		std::string jid = command[0];
		command.erase(command.begin());
		std::string cmd = boost::algorithm::join(command, " ");

		if (cmd == "start") {
			return start_instances(&config, jid);
		}
		else if (cmd == "stop") {
			stop_instances(&config, jid);
			return 0;
		}
		else if (cmd == "restart") {
			return restart_instances(&config, jid);
		}

		ask_local_server(&config, networkFactories, jid, cmd);
// 		std::string message = command;
// 		m = &message;

// 		ask_local_server(&config, networkFactories, message);

		eventLoop.runUntilEvents();


		struct timeval td_start,td_end;
		float elapsed = 0; 
		gettimeofday(&td_start, NULL);
	
		time_t started = time(NULL);
		while(get_response().empty()) {
			eventLoop.runUntilEvents();
		}
		if (!get_response().empty()) {
			gettimeofday(&td_end, NULL);
			elapsed = 1000000.0 * (td_end.tv_sec -td_start.tv_sec); \
			elapsed += (td_end.tv_usec - td_start.tv_usec); \
			elapsed = elapsed / 1000 / 1000; \
// 			std::cout << "Response received after " << (elapsed) << " seconds\n";
		}
	}
}