/// Program entry point. If invoked with no parameters, runs as the service. If the "configure" parameter is /// given runs the interactive configuration dialogs. /// /// @param[in] argc number of parameters /// @param[in] argv parameters int main (int argc, char **argv) { _mainStart (); if (argc == 2) { if (!_tcscmp (argv[1], TEXT ("run"))) { ServiceRun (SERVICE_RUN_INLINE); } else if (!_tcscmp (argv[1], TEXT ("configure"))) { LOGDEBUG (TEXT ("Configuring service")); ServiceConfigure (); } else { LOGERROR (TEXT ("Unrecognised parameter - ") << argv[1]); } } else { ServiceRun (SERVICE_RUN_INLINE); } _mainEnd (); return 0; }
/// Program entry point. If invoked from the command line with parameter "run" will run the service. If /// invoked from the service control manager will register the service handlers and then start the service /// dispatch logic. /// /// @param[in] argc number of parameters /// @param[in] argv parameters int _tmain (int argc, _TCHAR* argv[]) { #ifdef _WIN32 #ifndef _DEBUG fclose (stderr); FreeConsole (); #endif /*ifndef _DEBUG */ #endif /* ifdef _WIN32 */ _mainStart (); if (argc == 2) { if (!_tcscmp (argv[1], TEXT ("run"))) { LOGDEBUG (TEXT ("Running inline")); ServiceRun (SERVICE_RUN_INLINE); } else if (!_tcscmp (argv[1], TEXT ("configure"))) { SHELLEXECUTEINFO sei; ZeroMemory (&sei, sizeof (sei)); sei.cbSize = sizeof (sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.lpVerb = TEXT ("runas"); sei.lpFile = argv[0]; sei.lpParameters = TEXT ("configure-impl"); if (ShellExecuteEx (&sei)) { LOGINFO (TEXT ("Started priviledged process ") << GetProcessId (sei.hProcess)); WaitForSingleObject (sei.hProcess, INFINITE); LOGINFO (TEXT ("Priviledged process terminated")); CloseHandle (sei.hProcess); } else { LOGERROR (TEXT ("Couldn't launch priviledged form")); } } else if (!_tcscmp (argv[1], TEXT ("configure-impl"))) { LOGDEBUG (TEXT ("Configuring service")); ServiceConfigure (); } else { LOGERROR (TEXT ("Unrecognised parameter - ") << argv[1]); } } else { LOGDEBUG (TEXT ("Running as service")); SERVICE_TABLE_ENTRY *pServiceEntry = _CreateDispatchTable (); if (!StartServiceCtrlDispatcher (pServiceEntry)) { LOGFATAL (TEXT ("Couldn't start service control dispatcher, error ") << GetLastError ()); } delete pServiceEntry; } _mainEnd (); return 0; }
/// Program entry point. If invoked with no parameters, runs as the service. If the "configure" parameter is /// given runs the interactive configuration dialogs. /// /// @param[in] argc number of parameters /// @param[in] argv parameters int main (int argc, char **argv) { _mainStart (); if (argc == 2) { if (!_tcscmp (argv[1], TEXT ("run"))) { ServiceRun (SERVICE_RUN_INLINE); } else if (!_tcscmp (argv[1], TEXT ("configure"))) { LOGDEBUG (TEXT ("Configuring service")); ServiceConfigure (); } else { LOGERROR (TEXT ("Unrecognised parameter - ") << argv[1]); } } else { LOGDEBUG (TEXT ("Running as a daemon")); pid_t pid = fork (); if (pid < 0) { LOGERROR (TEXT ("Couldn't fork daemon process")); return 1; } if (pid > 0) { LOGDEBUG (TEXT ("Exiting parent process")); return 0; } pid_t sid = setsid (); if (sid < 0) { LOGERROR (TEXT ("Couldn't create SID for daemon process")); return 1; } chdir ("/"); close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO); ServiceRun (SERVICE_RUN_DAEMON); } _mainEnd (); return 0; }