2001-10-08 19:24:46 +00:00
|
|
|
#include "CClient.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CPlatform.h"
|
|
|
|
#include "ProtocolTypes.h"
|
|
|
|
#include "Version.h"
|
|
|
|
#include "CNetwork.h"
|
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include "XSocket.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
#include "CCondVar.h"
|
|
|
|
#include "CLock.h"
|
2002-05-31 14:25:26 +00:00
|
|
|
#include "CMutex.h"
|
2001-10-14 18:29:43 +00:00
|
|
|
#include "CThread.h"
|
2002-06-02 18:49:35 +00:00
|
|
|
#include "XThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
|
|
|
#include "CString.h"
|
|
|
|
#include <cstring>
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// platform dependent name of a daemon
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
#define DAEMON_NAME "Synergy Client"
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
#define DAEMON_NAME "synergy"
|
|
|
|
#endif
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
//
|
|
|
|
// program arguments
|
|
|
|
//
|
|
|
|
|
|
|
|
static const char* pname = NULL;
|
|
|
|
static bool s_restartable = true;
|
|
|
|
static bool s_daemon = true;
|
2002-06-09 22:20:28 +00:00
|
|
|
static bool s_camp = true;
|
2002-06-08 21:48:00 +00:00
|
|
|
static bool s_install = false;
|
|
|
|
static bool s_uninstall = false;
|
2002-06-04 11:06:26 +00:00
|
|
|
static const char* s_logFilter = NULL;
|
2002-06-09 17:59:32 +00:00
|
|
|
static CString s_name;
|
2002-06-09 16:53:25 +00:00
|
|
|
static CNetworkAddress s_serverAddress;
|
2002-06-04 11:06:26 +00:00
|
|
|
|
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
// logging thread safety
|
|
|
|
//
|
|
|
|
|
|
|
|
static CMutex* s_logMutex = NULL;
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
logLock(bool lock)
|
2002-05-31 14:25:26 +00:00
|
|
|
{
|
|
|
|
assert(s_logMutex != NULL);
|
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
s_logMutex->lock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s_logMutex->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2002-06-08 21:48:00 +00:00
|
|
|
// platform independent main
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
static CClient* s_client = NULL;
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
realMain(CMutex* mutex)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2001-11-19 00:33:36 +00:00
|
|
|
try {
|
2002-06-08 21:48:00 +00:00
|
|
|
// initialize threading library
|
|
|
|
CThread::init();
|
|
|
|
|
|
|
|
// make logging thread safe
|
|
|
|
CMutex logMutex;
|
|
|
|
s_logMutex = &logMutex;
|
|
|
|
CLog::setLock(&logLock);
|
|
|
|
|
|
|
|
bool locked = true;
|
|
|
|
try {
|
|
|
|
// create client
|
2002-06-09 17:59:32 +00:00
|
|
|
s_client = new CClient(s_name);
|
2002-06-09 22:20:28 +00:00
|
|
|
s_client->camp(s_camp);
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// run client
|
|
|
|
if (mutex != NULL) {
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
|
|
|
locked = false;
|
2002-06-09 22:20:28 +00:00
|
|
|
bool success = s_client->run(s_serverAddress);
|
2002-06-08 21:48:00 +00:00
|
|
|
locked = true;
|
|
|
|
if (mutex != NULL) {
|
|
|
|
mutex->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete s_client;
|
|
|
|
s_client = NULL;
|
|
|
|
CNetwork::cleanup();
|
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
2002-06-09 22:20:28 +00:00
|
|
|
|
|
|
|
return success ? 16 : 0;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// clean up
|
|
|
|
if (!locked && mutex != NULL) {
|
|
|
|
mutex->lock();
|
|
|
|
}
|
|
|
|
delete s_client;
|
|
|
|
s_client = NULL;
|
|
|
|
CNetwork::cleanup();
|
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
|
|
|
throw;
|
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
catch (XBase& e) {
|
|
|
|
log((CLOG_CRIT "failed: %s", e.what()));
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
// terminated
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
|
|
|
restartMain()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
return realMain(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoke realMain and wait for it. if s_restartable then keep
|
|
|
|
// restarting realMain until it returns a terminate code.
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
|
|
|
restartableMain()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
if (s_restartable) {
|
|
|
|
CPlatform platform;
|
|
|
|
return platform.restart(restartMain, 16);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return realMain(NULL);
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
//
|
|
|
|
// command line parsing
|
|
|
|
//
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
#define BYE "\nTry `%s --help' for more information."
|
|
|
|
|
|
|
|
static void (*bye)(int) = &exit;
|
2002-06-04 11:06:26 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
version()
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
|
|
|
log((CLOG_PRINT
|
|
|
|
"%s %d.%d.%d, protocol version %d.%d\n"
|
|
|
|
"%s",
|
|
|
|
pname,
|
|
|
|
kMajorVersion,
|
|
|
|
kMinorVersion,
|
|
|
|
kReleaseVersion,
|
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion,
|
|
|
|
kCopyright));
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
help()
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-14 18:08:20 +00:00
|
|
|
|
|
|
|
# define PLATFORM_ARGS \
|
|
|
|
" [--install]" \
|
|
|
|
" <server-address>\n" \
|
|
|
|
"or\n" \
|
|
|
|
" --uninstall\n"
|
|
|
|
# define PLATFORM_DESC \
|
|
|
|
" --install install server as a service.\n" \
|
|
|
|
" --uninstall uninstall server service.\n"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define PLATFORM_ARGS \
|
|
|
|
" <server-address>\n"
|
|
|
|
# define PLATFORM_DESC
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
log((CLOG_PRINT
|
|
|
|
"Usage: %s"
|
2002-06-09 22:20:28 +00:00
|
|
|
" [--camp|--no-camp]"
|
2002-06-14 18:08:20 +00:00
|
|
|
" [--daemon|--no-daemon]"
|
2002-06-09 17:59:32 +00:00
|
|
|
" [--debug <level>]"
|
|
|
|
" [--name <screen-name>]"
|
2002-06-04 11:06:26 +00:00
|
|
|
" [--restart|--no-restart]"
|
2002-06-14 18:08:20 +00:00
|
|
|
PLATFORM_ARGS
|
|
|
|
"\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
"Start the synergy mouse/keyboard sharing server.\n"
|
|
|
|
"\n"
|
2002-06-09 22:20:28 +00:00
|
|
|
"* --camp keep attempting to connect to the server until\n"
|
|
|
|
" successful.\n"
|
|
|
|
" --no-camp do not camp.\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
" -d, --debug <level> filter out log messages with priorty below level.\n"
|
|
|
|
" level may be: FATAL, ERROR, WARNING, NOTE, INFO,\n"
|
|
|
|
" DEBUG, DEBUG1, DEBUG2.\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
" -f, --no-daemon run the client in the foreground.\n"
|
|
|
|
"* --daemon run the client as a daemon.\n"
|
2002-06-09 17:59:32 +00:00
|
|
|
" -n, --name <screen-name> use screen-name instead the hostname to identify\n"
|
|
|
|
" ourself to the server.\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
" -1, --no-restart do not try to restart the client if it fails for\n"
|
|
|
|
" some reason.\n"
|
2002-06-09 22:20:28 +00:00
|
|
|
"* --restart restart the client automatically if it fails for\n"
|
|
|
|
" some unexpected reason, including the server\n"
|
|
|
|
" disconnecting but not including failing to\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
" connect to the server.\n"
|
|
|
|
PLATFORM_DESC
|
2002-06-04 11:06:26 +00:00
|
|
|
" -h, --help display this help and exit.\n"
|
|
|
|
" --version display version information and exit.\n"
|
|
|
|
"\n"
|
2002-06-08 21:48:00 +00:00
|
|
|
"* marks defaults.\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
"\n"
|
2002-06-09 16:53:25 +00:00
|
|
|
"The server address is of the form: [<hostname>][:<port>]. The hostname\n"
|
|
|
|
"must be the address or hostname of the server. The port overrides the\n"
|
|
|
|
"default port, %d.\n"
|
|
|
|
"\n"
|
2002-06-04 11:06:26 +00:00
|
|
|
"Where log messages go depends on the platform and whether or not the\n"
|
2002-06-14 18:08:20 +00:00
|
|
|
"client is running as a daemon.",
|
2002-06-09 16:53:25 +00:00
|
|
|
pname, kDefaultPort));
|
2002-06-04 11:06:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
isArg(int argi, int argc, const char** argv,
|
|
|
|
const char* name1, const char* name2,
|
|
|
|
int minRequiredParameters = 0)
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
|
|
|
if ((name1 != NULL && strcmp(argv[argi], name1) == 0) ||
|
|
|
|
(name2 != NULL && strcmp(argv[argi], name2) == 0)) {
|
|
|
|
// match. check args left.
|
|
|
|
if (argi + minRequiredParameters >= argc) {
|
2002-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: missing arguments for `%s'" BYE,
|
|
|
|
pname, argv[argi], pname));
|
|
|
|
bye(2);
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
parse(int argc, const char** argv)
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
|
|
|
assert(pname != NULL);
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(argc >= 1);
|
|
|
|
|
2002-06-09 17:59:32 +00:00
|
|
|
// set defaults
|
|
|
|
char hostname[256];
|
|
|
|
if (CNetwork::gethostname(hostname, sizeof(hostname)) != CNetwork::Error) {
|
|
|
|
s_name = hostname;
|
|
|
|
}
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// parse options
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (isArg(i, argc, argv, "-d", "--debug", 1)) {
|
|
|
|
// change logging level
|
|
|
|
s_logFilter = argv[++i];
|
|
|
|
}
|
|
|
|
|
2002-06-09 17:59:32 +00:00
|
|
|
else if (isArg(i, argc, argv, "-n", "--name", 1)) {
|
|
|
|
// save screen name
|
|
|
|
s_name = argv[++i];
|
|
|
|
}
|
|
|
|
|
2002-06-09 22:20:28 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--camp")) {
|
|
|
|
// enable camping
|
|
|
|
s_camp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--no-camp")) {
|
|
|
|
// disable camping
|
|
|
|
s_camp = false;
|
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, "-f", "--no-daemon")) {
|
2002-06-04 11:06:26 +00:00
|
|
|
// not a daemon
|
|
|
|
s_daemon = false;
|
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--daemon")) {
|
2002-06-04 11:06:26 +00:00
|
|
|
// daemonize
|
|
|
|
s_daemon = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
|
|
|
// don't try to restart
|
|
|
|
s_restartable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--restart")) {
|
|
|
|
// try to restart
|
|
|
|
s_restartable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, "-h", "--help")) {
|
|
|
|
help();
|
2002-06-08 21:48:00 +00:00
|
|
|
bye(0);
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, NULL, "--version")) {
|
|
|
|
version();
|
2002-06-08 21:48:00 +00:00
|
|
|
bye(0);
|
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--install")) {
|
|
|
|
s_install = true;
|
|
|
|
if (s_uninstall) {
|
|
|
|
log((CLOG_PRINT "%s: `--install' and `--uninstall'"
|
|
|
|
" are mutually exclusive" BYE,
|
|
|
|
pname, argv[i], pname));
|
|
|
|
bye(2);
|
|
|
|
}
|
|
|
|
}
|
2002-06-14 18:08:20 +00:00
|
|
|
#endif
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
else if (isArg(i, argc, argv, NULL, "--uninstall")) {
|
|
|
|
s_uninstall = true;
|
|
|
|
if (s_install) {
|
|
|
|
log((CLOG_PRINT "%s: `--install' and `--uninstall'"
|
|
|
|
" are mutually exclusive" BYE,
|
|
|
|
pname, argv[i], pname));
|
|
|
|
bye(2);
|
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
2002-06-14 18:08:20 +00:00
|
|
|
#endif
|
2002-06-04 11:06:26 +00:00
|
|
|
|
|
|
|
else if (isArg(i, argc, argv, "--", NULL)) {
|
|
|
|
// remaining arguments are not options
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (argv[i][0] == '-') {
|
2002-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
|
|
|
pname, argv[i], pname));
|
|
|
|
bye(2);
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// this and remaining arguments are not options
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// exactly one non-option argument (server-address) unless using
|
|
|
|
// --uninstall.
|
|
|
|
if (s_uninstall) {
|
|
|
|
if (i != argc) {
|
|
|
|
log((CLOG_PRINT "%s: unrecognized option `%s' to `%s'" BYE,
|
|
|
|
pname, argv[i], pname,
|
|
|
|
s_install ? "--install" : "--uninstall"));
|
|
|
|
bye(2);
|
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else {
|
|
|
|
if (i == argc) {
|
|
|
|
log((CLOG_PRINT "%s: a server address or name is required" BYE,
|
|
|
|
pname, pname));
|
|
|
|
bye(1);
|
|
|
|
}
|
|
|
|
if (i + 1 != argc) {
|
|
|
|
log((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
|
|
|
|
pname, argv[i], pname));
|
|
|
|
bye(2);
|
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
|
|
|
// save server address
|
|
|
|
try {
|
|
|
|
s_serverAddress = CNetworkAddress(argv[i], kDefaultPort);
|
|
|
|
}
|
|
|
|
catch (XSocketAddress&) {
|
|
|
|
log((CLOG_PRINT "%s: invalid server address" BYE,
|
|
|
|
pname, pname));
|
|
|
|
bye(2);
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase default filter level for daemon. the user must
|
|
|
|
// explicitly request another level for a daemon.
|
|
|
|
if (s_daemon && s_logFilter == NULL) {
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-08 21:48:00 +00:00
|
|
|
if (CPlatform::isWindows95Family()) {
|
|
|
|
// windows 95 has no place for logging so avoid showing
|
|
|
|
// the log console window.
|
|
|
|
s_logFilter = "FATAL";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
s_logFilter = "NOTE";
|
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set log filter
|
|
|
|
if (!CLog::setFilter(s_logFilter)) {
|
2002-06-08 21:48:00 +00:00
|
|
|
log((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
|
|
|
|
pname, s_logFilter, pname));
|
|
|
|
bye(2);
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-31 14:25:26 +00:00
|
|
|
//
|
|
|
|
// platform dependent entry points
|
|
|
|
//
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
#include "CMSWindowsScreen.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
logMessageBox(int priority, const char* msg)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
if (priority <= CLog::kFATAL) {
|
|
|
|
MessageBox(NULL, msg, pname, MB_OK | MB_ICONWARNING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
byeThrow(int x)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
throw CWin32Platform::CDaemonFailed(x);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
daemonStop(void)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
s_client->quit();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
daemonStartup(IPlatform* iplatform, int argc, const char** argv)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
// get platform pointer
|
|
|
|
CWin32Platform* platform = static_cast<CWin32Platform*>(iplatform);
|
|
|
|
|
|
|
|
// catch errors that would normally exit
|
|
|
|
bye = &byeThrow;
|
|
|
|
|
|
|
|
// parse command line
|
|
|
|
s_install = false;
|
|
|
|
s_uninstall = false;
|
|
|
|
parse(argc, argv);
|
|
|
|
if (s_install || s_uninstall) {
|
|
|
|
// not allowed to install/uninstall from service
|
|
|
|
throw CWin32Platform::CDaemonFailed(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// run as a service
|
|
|
|
return platform->runDaemon(realMain, daemonStop);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
2002-06-17 15:44:45 +00:00
|
|
|
int
|
|
|
|
daemonStartup95(IPlatform*, int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-06-17 15:44:45 +00:00
|
|
|
return restartableMain();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int WINAPI
|
2002-06-17 13:31:21 +00:00
|
|
|
WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-04 12:26:23 +00:00
|
|
|
CPlatform platform;
|
|
|
|
|
|
|
|
// save instance
|
2001-11-19 00:33:36 +00:00
|
|
|
CMSWindowsScreen::init(instance);
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// get program name
|
2002-06-08 21:48:00 +00:00
|
|
|
pname = platform.getBasename(__argv[0]);
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// initialize network library
|
|
|
|
CNetwork::init();
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
// send PRINT and FATAL output to a message box
|
|
|
|
CLog::setOutputter(&logMessageBox);
|
|
|
|
|
2002-06-17 15:44:45 +00:00
|
|
|
// windows NT family starts services using no command line options.
|
|
|
|
// since i'm not sure how to tell the difference between that and
|
|
|
|
// a user providing no options we'll assume that if there are no
|
|
|
|
// arguments and we're on NT then we're being invoked as a service.
|
|
|
|
// users on NT can use `--daemon' or `--no-daemon' to force us out
|
|
|
|
// of the service code path.
|
|
|
|
if (__argc <= 1 && !CWin32Platform::isWindows95Family()) {
|
|
|
|
int result = platform.daemonize(DAEMON_NAME, &daemonStartup);
|
|
|
|
if (result == -1) {
|
|
|
|
log((CLOG_CRIT "failed to start as a service" BYE, pname));
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
return result;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
|
2002-06-17 15:44:45 +00:00
|
|
|
// parse command line
|
|
|
|
parse(__argc, const_cast<const char**>(__argv));
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// install/uninstall
|
|
|
|
if (s_install) {
|
|
|
|
// get the full path to this program
|
|
|
|
TCHAR path[MAX_PATH];
|
|
|
|
if (GetModuleFileName(NULL, path,
|
|
|
|
sizeof(path) / sizeof(path[0])) == 0) {
|
|
|
|
log((CLOG_CRIT "cannot determine absolute path to program"));
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct the command line to start the service with
|
2002-06-14 18:08:20 +00:00
|
|
|
CString commandLine = "--daemon";
|
2002-06-08 21:48:00 +00:00
|
|
|
if (s_restartable) {
|
|
|
|
commandLine += " --restart";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
commandLine += " --no-restart";
|
|
|
|
}
|
|
|
|
if (s_logFilter != NULL) {
|
|
|
|
commandLine += " --debug ";
|
|
|
|
commandLine += s_logFilter;
|
|
|
|
}
|
|
|
|
commandLine += " ";
|
2002-06-09 16:53:25 +00:00
|
|
|
commandLine += s_serverAddress.getHostname().c_str();
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// install
|
|
|
|
if (!platform.installDaemon(DAEMON_NAME,
|
|
|
|
"Shares this system's mouse and keyboard with others.",
|
|
|
|
path, commandLine.c_str())) {
|
|
|
|
log((CLOG_CRIT "failed to install service"));
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
log((CLOG_PRINT "installed successfully"));
|
2001-11-19 00:33:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else if (s_uninstall) {
|
2002-06-14 18:08:20 +00:00
|
|
|
switch (platform.uninstallDaemon(DAEMON_NAME)) {
|
|
|
|
case IPlatform::kSuccess:
|
|
|
|
log((CLOG_PRINT "uninstalled successfully"));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IPlatform::kFailed:
|
2002-06-08 21:48:00 +00:00
|
|
|
log((CLOG_CRIT "failed to uninstall service"));
|
|
|
|
return 16;
|
2002-06-14 18:08:20 +00:00
|
|
|
|
|
|
|
case IPlatform::kAlready:
|
|
|
|
log((CLOG_CRIT "service isn't installed"));
|
|
|
|
return 16;
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
// daemonize if requested
|
|
|
|
int result;
|
2002-06-17 15:44:45 +00:00
|
|
|
if (s_daemon) {
|
|
|
|
// redirect log messages
|
|
|
|
platform.installDaemonLogger(DAEMON_NAME);
|
|
|
|
|
|
|
|
// start as a daemon
|
2002-06-08 21:48:00 +00:00
|
|
|
if (CWin32Platform::isWindows95Family()) {
|
2002-06-17 15:44:45 +00:00
|
|
|
result = platform.daemonize(DAEMON_NAME, &daemonStartup95);
|
|
|
|
if (result == -1) {
|
|
|
|
log((CLOG_CRIT "failed to start as a service" BYE, pname));
|
|
|
|
return 16;
|
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-06-17 15:44:45 +00:00
|
|
|
// cannot start a service from the command line so just
|
|
|
|
// run normally (except with log messages redirected).
|
|
|
|
result = restartableMain();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-06-14 18:08:20 +00:00
|
|
|
// run
|
2002-06-08 21:48:00 +00:00
|
|
|
result = restartableMain();
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
return result;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
static
|
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
daemonStartup(IPlatform*, int, const char**)
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
|
|
|
return restartableMain();
|
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
main(int argc, char** argv)
|
2002-06-04 11:06:26 +00:00
|
|
|
{
|
2002-06-04 12:26:23 +00:00
|
|
|
CPlatform platform;
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// get program name
|
2002-06-04 12:26:23 +00:00
|
|
|
pname = platform.getBasename(argv[0]);
|
2002-06-04 11:06:26 +00:00
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// initialize network library
|
|
|
|
CNetwork::init();
|
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
// parse command line
|
2002-06-08 21:48:00 +00:00
|
|
|
parse(argc, const_cast<const char**>(argv));
|
2002-06-04 11:06:26 +00:00
|
|
|
|
|
|
|
// daemonize if requested
|
2002-06-08 21:48:00 +00:00
|
|
|
int result;
|
2002-06-04 11:06:26 +00:00
|
|
|
if (s_daemon) {
|
2002-06-08 21:48:00 +00:00
|
|
|
result = platform.daemonize(DAEMON_NAME, &daemonStartup);
|
|
|
|
if (result == -1) {
|
2002-06-04 12:26:23 +00:00
|
|
|
log((CLOG_CRIT "failed to daemonize"));
|
|
|
|
return 16;
|
|
|
|
}
|
2002-06-04 11:06:26 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
else {
|
|
|
|
result = restartableMain();
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
2002-06-08 21:48:00 +00:00
|
|
|
|
|
|
|
return result;
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-06-04 11:06:26 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error no main() for platform
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|