From dc461306241ec2263f16220947ac6d49b6d77ee7 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Tue, 11 May 2010 15:23:50 -0400 Subject: fix windows service registration (cherry picked from commit d0eccc9c80123e1d7e1fbfad32431d044b925008) (Backport of SERVER-1070 to v1.4) --- util/ntservice.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/util/ntservice.cpp b/util/ntservice.cpp index 602d98a8e6d..07dce206a02 100644 --- a/util/ntservice.cpp +++ b/util/ntservice.cpp @@ -17,6 +17,7 @@ #include "stdafx.h" #include "ntservice.h" +#include #if defined(_WIN32) @@ -32,17 +33,26 @@ namespace mongo { } bool ServiceController::installService( const std::wstring& serviceName, const std::wstring& displayName, const std::wstring& serviceDesc, int argc, char* argv[] ) { + assert(argc >= 1); + + stringstream commandLine; + + if ( strchr(argv[0], ':') ) { // a crude test for fully qualified path + commandLine << '"' << argv[0] << "\" "; + } else { + char buffer[256]; + assert( _getcwd(buffer, 256) ); + commandLine << '"' << buffer << '\\' << argv[0] << "\" "; + } - std::string commandLine; - - for ( int i = 0; i < argc; i++ ) { + for ( int i = 1; i < argc; i++ ) { std::string arg( argv[ i ] ); // replace install command to indicate process is being started as a service if ( arg == "--install" ) arg = "--service"; - commandLine += arg + " "; + commandLine << arg << " "; } SC_HANDLE schSCManager = ::OpenSCManager( null, null, SC_MANAGER_ALL_ACCESS ); @@ -50,7 +60,7 @@ namespace mongo { return false; std::basic_ostringstream< TCHAR > commandLineWide; - commandLineWide << commandLine.c_str(); + commandLineWide << commandLine.str().c_str(); // create new service SC_HANDLE schService = ::CreateService( schSCManager, serviceName.c_str(), displayName.c_str(), -- cgit v1.2.1