mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-03 22:14:26 +08:00
performance fixes on win32 plus clean up of some warnings. also
improved error messages when uninstalling service.
This commit is contained in:
@@ -202,9 +202,7 @@ CMSWindowsScreen::getEvent(
|
||||
MSG* msg) const
|
||||
{
|
||||
// wait for an event in a cancellable way
|
||||
while (HIWORD(GetQueueStatus(QS_ALLINPUT)) == 0) {
|
||||
CThread::sleep(0.01);
|
||||
}
|
||||
CThread::waitForEvent();
|
||||
GetMessage(msg, NULL, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,12 +35,12 @@ CUnixPlatform::installDaemon(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
CUnixPlatform::EResult
|
||||
CUnixPlatform::uninstallDaemon(
|
||||
const char*)
|
||||
{
|
||||
// daemons don't require special installation
|
||||
return true;
|
||||
return kSuccess;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -85,12 +85,20 @@ CUnixPlatform::daemonize(
|
||||
dup(1);
|
||||
|
||||
// hook up logger
|
||||
setDaemonLogger(name);
|
||||
installDaemonLogger(name);
|
||||
|
||||
// invoke function
|
||||
return func(this, 1, &name);
|
||||
}
|
||||
|
||||
void
|
||||
CUnixPlatform::installDaemonLogger(
|
||||
const char* name)
|
||||
{
|
||||
openlog(name, 0, LOG_DAEMON);
|
||||
CLog::setOutputter(&CUnixPlatform::deamonLogger);
|
||||
}
|
||||
|
||||
int
|
||||
CUnixPlatform::restart(
|
||||
RestartFunc func,
|
||||
@@ -195,14 +203,6 @@ CUnixPlatform::addPathComponent(
|
||||
return path;
|
||||
}
|
||||
|
||||
void
|
||||
CUnixPlatform::setDaemonLogger(
|
||||
const char* name)
|
||||
{
|
||||
openlog(name, 0, LOG_DAEMON);
|
||||
CLog::setOutputter(&CUnixPlatform::deamonLogger);
|
||||
}
|
||||
|
||||
bool
|
||||
CUnixPlatform::deamonLogger(
|
||||
int priority,
|
||||
|
||||
@@ -13,8 +13,9 @@ public:
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine);
|
||||
virtual bool uninstallDaemon(const char* name);
|
||||
virtual EResult uninstallDaemon(const char* name);
|
||||
virtual int daemonize(const char* name, DaemonFunc);
|
||||
virtual void installDaemonLogger(const char* name);
|
||||
virtual int restart(RestartFunc, int minErrorCode);
|
||||
virtual const char* getBasename(const char* pathname) const;
|
||||
virtual CString getUserDirectory() const;
|
||||
@@ -23,9 +24,6 @@ public:
|
||||
const CString& prefix,
|
||||
const CString& suffix) const;
|
||||
|
||||
protected:
|
||||
virtual void setDaemonLogger(const char* name);
|
||||
|
||||
private:
|
||||
static bool deamonLogger(int, const char*);
|
||||
};
|
||||
|
||||
@@ -180,7 +180,7 @@ CWin32Platform::installDaemon(
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
IPlatform::EResult
|
||||
CWin32Platform::uninstallDaemon(
|
||||
const char* name)
|
||||
{
|
||||
@@ -190,7 +190,7 @@ CWin32Platform::uninstallDaemon(
|
||||
HKEY key = open95ServicesKey();
|
||||
if (key == NULL) {
|
||||
log((CLOG_ERR "cannot open RunServices registry key", GetLastError()));
|
||||
return false;
|
||||
return kAlready;
|
||||
}
|
||||
|
||||
// remove entry
|
||||
@@ -199,7 +199,7 @@ CWin32Platform::uninstallDaemon(
|
||||
// clean up
|
||||
closeKey(key);
|
||||
|
||||
return true;
|
||||
return kSuccess;
|
||||
}
|
||||
|
||||
// windows NT family services
|
||||
@@ -216,26 +216,41 @@ CWin32Platform::uninstallDaemon(
|
||||
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
||||
if (mgr == NULL) {
|
||||
log((CLOG_ERR "OpenSCManager failed with %d", GetLastError()));
|
||||
return false;
|
||||
return kFailed;
|
||||
}
|
||||
|
||||
// open the service. oddly, you must open a service to delete it.
|
||||
bool success;
|
||||
EResult result;
|
||||
SC_HANDLE service = OpenService(mgr, name, DELETE);
|
||||
if (service == NULL) {
|
||||
log((CLOG_ERR "OpenService failed with %d", GetLastError()));
|
||||
success = false;
|
||||
const DWORD e = GetLastError();
|
||||
log((CLOG_ERR "OpenService failed with %d", e));
|
||||
result = (e == ERROR_SERVICE_DOES_NOT_EXIST) ? kAlready : kFailed;
|
||||
}
|
||||
|
||||
else {
|
||||
success = (DeleteService(service) != 0);
|
||||
if (DeleteService(service) != 0) {
|
||||
result = kSuccess;
|
||||
}
|
||||
else {
|
||||
const DWORD e = GetLastError();
|
||||
switch (e) {
|
||||
case ERROR_SERVICE_MARKED_FOR_DELETE:
|
||||
result = kAlready;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = kFailed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CloseServiceHandle(service);
|
||||
}
|
||||
|
||||
// close the manager
|
||||
CloseServiceHandle(mgr);
|
||||
|
||||
return success;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,6 +318,21 @@ CWin32Platform::daemonize(
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::installDaemonLogger(
|
||||
const char* name)
|
||||
{
|
||||
if (!CWin32Platform::isWindows95Family()) {
|
||||
// open event log and direct log messages to it
|
||||
if (s_eventLog == NULL) {
|
||||
s_eventLog = RegisterEventSource(NULL, name);
|
||||
if (s_eventLog != NULL) {
|
||||
CLog::setOutputter(&CWin32Platform::serviceLogger);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
CWin32Platform::restart(
|
||||
RestartFunc func,
|
||||
@@ -643,12 +673,7 @@ CWin32Platform::serviceMain(
|
||||
const char** argv = const_cast<const char**>(argvIn);
|
||||
|
||||
// open event log and direct log messages to it
|
||||
if (s_eventLog == NULL) {
|
||||
s_eventLog = RegisterEventSource(NULL, argv[0]);
|
||||
if (s_eventLog != NULL) {
|
||||
CLog::setOutputter(&CWin32Platform::serviceLogger);
|
||||
}
|
||||
}
|
||||
installDaemonLogger(argv[0]);
|
||||
|
||||
// create synchronization objects
|
||||
CThread::init();
|
||||
|
||||
@@ -46,8 +46,9 @@ public:
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine);
|
||||
virtual bool uninstallDaemon(const char* name);
|
||||
virtual EResult uninstallDaemon(const char* name);
|
||||
virtual int daemonize(const char* name, DaemonFunc);
|
||||
virtual void installDaemonLogger(const char* name);
|
||||
virtual int restart(RestartFunc, int minErrorCode);
|
||||
virtual const char* getBasename(const char* pathname) const;
|
||||
virtual CString getUserDirectory() const;
|
||||
|
||||
@@ -9,6 +9,12 @@ public:
|
||||
typedef int (*DaemonFunc)(IPlatform*, int argc, const char** argv);
|
||||
typedef int (*RestartFunc)();
|
||||
|
||||
enum EResult {
|
||||
kSuccess,
|
||||
kFailed,
|
||||
kAlready
|
||||
};
|
||||
|
||||
// manipulators
|
||||
|
||||
// install/uninstall a daemon. commandLine should *not*
|
||||
@@ -18,12 +24,10 @@ public:
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine) = 0;
|
||||
virtual bool uninstallDaemon(const char* name) = 0;
|
||||
virtual EResult uninstallDaemon(const char* name) = 0;
|
||||
|
||||
// daemonize. this should have the side effect of sending log
|
||||
// messages to a system message logger since messages can no
|
||||
// longer go to the console. returns true iff successful.
|
||||
// the name is the name of the daemon.
|
||||
// daemonize. this should call installDaemonLogger(). returns
|
||||
// true iff successful. the name is the name of the daemon.
|
||||
|
||||
// daemonize. this should have the side effect of sending log
|
||||
// messages to a system message logger since messages can no
|
||||
@@ -44,6 +48,11 @@ public:
|
||||
// SetServiceStatus().
|
||||
virtual int daemonize(const char* name, DaemonFunc func) = 0;
|
||||
|
||||
// directs CLog to send messages to the daemon log. used when
|
||||
// messages should no longer go to the console. name is used
|
||||
// in the log to identify this process.
|
||||
virtual void installDaemonLogger(const char* name) = 0;
|
||||
|
||||
// continually restart the given function in a separate process
|
||||
// or thread until it exits normally with a code less than the
|
||||
// given code then return the code.
|
||||
|
||||
Reference in New Issue
Block a user