Changeset - 79fd6782fbf1
[Not reviewed]
0 3 0
Vitaly Takmazov - 13 years ago 2012-12-19 08:42:00
vitalyster@gmail.com
handle Ctrl-C from win32 console
3 files changed with 18 insertions and 9 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
@@ -26,68 +26,72 @@
 
#include <pwd.h>
 
#include <grp.h>
 
#include <sys/resource.h>
 
#include "libgen.h"
 
#ifndef __FreeBSD__
 
#include <malloc.h>
 
#endif
 
#else
 
#include <process.h>
 
#define getpid _getpid
 
#include "win32/ServiceWrapper.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;
 
Config *config_ = NULL;
 

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

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

	
 
#ifdef WIN32
 
BOOL spectrum_control_handler( DWORD fdwCtrlType ) { 
 
	if (fdwCtrlType == CTRL_C_EVENT || fdwCtrlType == CTRL_CLOSE_EVENT) {
 
		eventLoop_->postEvent(&stop_spectrum);
 
		return TRUE;
 
	}
 
	return FALSE;
 
} 
 
#endif
 

	
 
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. */
 
@@ -105,49 +109,48 @@ static void daemonize(const char *cwd, const char *lock_file) {
 
		}
 
		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 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";
 
@@ -270,48 +273,54 @@ int main(int argc, char **argv)
 
	std::string install_service_name, uninstall_service_name, run_service_name;
 
	// determine the name of the currently executing file
 
	char szFilePath[MAX_PATH];
 
	GetModuleFileNameA(NULL, szFilePath, sizeof(szFilePath));
 
	std::string exe_file(szFilePath);					
 
#endif	
 
	setlocale(LC_ALL, "");
 
#ifndef WIN32
 
#ifndef __FreeBSD__
 
	mallopt(M_CHECK_ACTION, 2);
 
	mallopt(M_PERTURB, 0xb);
 
#endif
 
#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;
 
	}
 
#else
 
	if( !SetConsoleCtrlHandler( (PHANDLER_ROUTINE) spectrum_control_handler, TRUE ) ) 
 
	{ 
 
		std::cout << "control 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", boost::program_options::value<std::string>(&install_service_name)->default_value(""), "Install spectrum as Windows service")
 
 		("uninstall-service,u", boost::program_options::value<std::string>(&uninstall_service_name)->default_value(""), "Uninstall Windows service")
 
		("run-as-service,r", boost::program_options::value<std::string>(&run_service_name)->default_value(""), "stub for Windows Service Manager");
 
#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);
 

	
spectrum/src/win32/ServiceWrapper.cpp
Show inline comments
 
@@ -66,49 +66,49 @@ bool doAction(int action, int desiredAccess, LPSTR path) {
 
			return TRUE;
 
		break;
 
	case DO_CHECK:
 
		service = OpenServiceA(scm, ServiceName, SERVICE_QUERY_STATUS);
 
		return (service != NULL);
 
	default:
 
		return FALSE;
 
	}
 
	CloseServiceHandle(service);		
 
	CloseServiceHandle(scm);
 
	return FALSE;
 
}
 

	
 
void WINAPI ServiceControlHandler(DWORD controlCode) {
 
	switch (controlCode) {
 
	case SERVICE_CONTROL_INTERROGATE:
 
		break;
 
	case SERVICE_CONTROL_STOP:
 
		ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;		
 
		break;
 
	default:
 
		break;
 
	}
 
	SetServiceStatus(ServiceStatusHandle, &ServiceStatus);	
 
	stop();
 
	spectrum_control_handler(CTRL_CLOSE_EVENT);
 
}
 

	
 
void WINAPI ServiceMain(DWORD argc, LPSTR *argv) {
 
	ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 
	ServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
 

	
 
	ServiceStatusHandle = RegisterServiceCtrlHandlerA(ServiceName, ServiceControlHandler);
 
	if (ServiceStatusHandle) {
 
		ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
 
		SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
 
		ServiceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
 
		ServiceStatus.dwCurrentState = SERVICE_RUNNING;
 
		SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
 
		mainloop();
 
		ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
 
		SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
 
		ServiceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
 
		ServiceStatus.dwCurrentState = SERVICE_STOPPED;
 
		SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
 
	}	
 
}
 

	
 
void ServiceWrapper::RunService() {
 
	SERVICE_TABLE_ENTRYA serviceTable[] = {
spectrum/src/win32/ServiceWrapper.h
Show inline comments
 
#pragma once
 
#include "transport/config.h"
 
#include <windows.h>
 
#include <tchar.h>
 

	
 
class ServiceWrapper
 
{
 
public:
 
	ServiceWrapper(LPSTR serviceName);
 
	~ServiceWrapper(void);
 
	bool Install(LPSTR commandLine);
 
	bool UnInstall();
 
	bool IsInstalled();
 
	void RunService();
 
};
 

	
 
int mainloop();
 
void stop();
 
BOOL spectrum_control_handler( DWORD fdwCtrlType );
 

	
0 comments (0 inline, 0 general)