Changeset - 573b15bc2f3b
[Not reviewed]
0 2 0
Jan Kaluza - 14 years ago 2011-09-13 14:57:28
hanzz.k@gmail.com
Preparation for removeOldIcons thread
2 files changed with 23 insertions and 9 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
#include "transport/config.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/mysqlbackend.h"
 
#include "transport/userregistration.h"
 
#include "transport/networkpluginserver.h"
 
#include "transport/admininterface.h"
 
#include "transport/util.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
#include <boost/filesystem.hpp>
 
#ifndef WIN32
 
#include "sys/signal.h"
 
#include <pwd.h>
 
#include <grp.h>
 
#else
 
#include <Windows.h>
 
#include <tchar.h>
 
#endif
 
#include "log4cxx/logger.h"
 
#include "log4cxx/patternlayout.h"
 
#include "log4cxx/propertyconfigurator.h"
 
#include "log4cxx/consoleappender.h"
 
#include "libgen.h"
 
 
using namespace log4cxx;
 
 
using namespace Transport;
 
 
static LoggerPtr logger = log4cxx::Logger::getLogger("Spectrum");
 
 
Swift::SimpleEventLoop *eventLoop_ = NULL;
 
Component *component_ = NULL;
 
UserManager *userManager_ = NULL;
 
 
static void stop_spectrum() {
 
	userManager_->removeAllUsers();
 
	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) {
 
		exit(0);
 
	}
 
 
	/* At this point we are executing as the child process */
 
 
	/* Change the file mode mask */
 
	umask(0);
 
 
@@ -174,48 +182,49 @@ int main(int argc, char **argv)
 
	}
 
 
#ifndef WIN32
 
	if (!no_daemon) {
 
		// 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;
 
		}
 
		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;
 
		}
 
 
		// 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
 
 
	if (CONFIG_STRING(&config, "logging.config").empty()) {
 
		LoggerPtr root = log4cxx::Logger::getRootLogger();
 
#ifdef WIN32
 
		root->addAppender(new ConsoleAppender(new PatternLayout(L"%d %-5p %c: %m%n")));
 
#else
 
		root->addAppender(new ConsoleAppender(new PatternLayout("%d %-5p %c: %m%n")));
 
#endif
 
	}
 
	else {
 
		log4cxx::PropertyConfigurator::configure(CONFIG_STRING(&config, "logging.config"));
 
	}
 
 
#ifndef WIN32
 
	if (!CONFIG_STRING(&config, "service.group").empty()) {
 
		struct group *gr;
 
		if ((gr = getgrnam(CONFIG_STRING(&config, "service.group").c_str())) == NULL) {
 
			LOG4CXX_ERROR(logger, "Invalid service.group name " << CONFIG_STRING(&config, "service.group"));
 
			return 1;
 
		}
 
 
		if (((setgid(gr->gr_gid)) != 0) || (initgroups(CONFIG_STRING(&config, "service.user").c_str(), gr->gr_gid) != 0)) {
src/util.cpp
Show inline comments
 
@@ -27,49 +27,54 @@
 

	
 
using namespace boost::filesystem;
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
namespace Util {
 

	
 
void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t) {
 
	BOOST_FOREACH(const std::string &dir, dirs) {
 
		path p(dir);
 

	
 
		try {
 
			if (!exists(p)) {
 
				continue;
 
			}
 
			if (!is_directory(p)) {
 
				continue;
 
			}
 

	
 
			directory_iterator end_itr;
 
			for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
				if (last_write_time(itr->path()) < t) {
 
					if (is_regular(itr->path())) {
 
						remove(itr->path());
 
					}
 
					else if (is_directory(itr->path())) {
 
						std::vector<std::string> nextDirs;
 
						nextDirs.push_back(itr->path().string());
 
						removeEverythingOlderThan(nextDirs, t);
 
						if (is_empty(itr->path())) {
 
							remove_all(itr->path());
 
					try {
 
						if (is_regular(itr->path())) {
 
							remove(itr->path());
 
						}
 
						else if (is_directory(itr->path())) {
 
							std::vector<std::string> nextDirs;
 
							nextDirs.push_back(itr->path().string());
 
							removeEverythingOlderThan(nextDirs, t);
 
							if (is_empty(itr->path())) {
 
								remove_all(itr->path());
 
							}
 
						}
 
					}
 
					catch (const filesystem_error& ex) {
 
						
 
					}
 
				}
 
			}
 

	
 

	
 
		}
 
		catch (const filesystem_error& ex) {
 
			
 
		}
 
	}
 
}
 

	
 
}
 

	
 
}
0 comments (0 inline, 0 general)