[SERVER-7252] Faulty command line is produced when installing as a service on Windows and using equal sign in options Created: 04/Oct/12  Updated: 11/Jul/16  Resolved: 06/Dec/12

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.0.8, 2.2.2, 2.3.1
Fix Version/s: 2.3.2

Type: Bug Priority: Major - P3
Reporter: Stefano Ricciardi Assignee: Andy Schwerin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 2008


Issue Links:
Depends
Operating System: Windows
Participants:

 Description   

When you try to install mongod as a service on Windows specifying any of the --serviceName, --serviceDisplayName or --serviceDescription options using an equal sign to separate the option name from the option value, the command line produced for the service may be missing options and/or have leftover text and the service may not be able to be successfully started.

Steps to reproduce

1. Try to install mongod as a service with as below:
C:\Mongo\bin>mongod --install --serviceName="My Service" --serviceDescription "My Service" --serviceDisplayName "My Service" --dbpath C:\mongo\data\config --port 20001 --logpath C:\mongo\logs\mongo_config.log.txt --configsvr

2. Issue the following command
net start "My Service"

3. Verify that the following error is reported:
The service is not responding to the control function

4. WORKAROUND
a. Go to the registry for this service (HKLM\SYSTEM\CurrentControlSet\services)
b. Verify that in the ImagePath after --service there's a spurious "My Service" string (without quotes)
c. remove that string and save
d. verify that the service is now starting properly.



 Comments   
Comment by Andy Schwerin [ 06/Dec/12 ]

The altered functionality is unit tested, but there is no facility to test installation/removal of Windows services in our automated test framework. This was hand tested by following the "steps to reproduce" in the original ticket description.

Comment by auto [ 06/Dec/12 ]

Author:

{u'date': u'2012-12-05T20:40:32Z', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: SERVER-7252 Properly handle empty-string arguments in argv when generating a windows command line.
Branch: master
https://github.com/mongodb/mongo/commit/ce53447ced1d3c7ad26274dad8099f2bb199c2b0

Comment by auto [ 05/Dec/12 ]

Author:

{u'date': u'2012-12-04T22:40:02Z', u'name': u'Andy Schwerin', u'email': u'schwerin@10gen.com'}

Message: SERVER-7252 Produce correct commandline for Windows service for --install, --reinstall or --service.

The service command line is derived from the mongod/mongos command line when Mongo is started with
--install, --reinstall and --service. It does this by stripping out options that describe how to
install the service, and correctly quoting the arguments. This patch makes this procedure more explicit,
and handles the equals sign and arguments with spaces and backslashes in the process.
Branch: master
https://github.com/mongodb/mongo/commit/c24aa0214b18d875236ff8f31e7bfe54fb537f61

Comment by auto [ 04/Dec/12 ]

Author:

{u'date': u'2012-12-04T20:15:40Z', u'name': u'Andy Schwerin', u'email': u'schwerin@10gen.com'}

Message: SERVER-7252 Introduce and test function for building a Windows command line from an argument vector.
Branch: master
https://github.com/mongodb/mongo/commit/0753b2b2a5ed8e70763d50e6945aea72f0e9f372

Comment by Tad Marshall [ 30/Nov/12 ]

There is another issue caused by poor handling of equals signs used with options: this causes the filename parameters to be unrecognized by name, leading to a failure to re-quote them. This case does not involve the --service* parameters.

For example:

C:\mongo> mongod --install --dbpath="U:\Special Location" --logpath="L:\Special Log Location\mongod.log"

produces

"C:\mongo\mongod.exe" --service --dbpath=U:\Special Location --logpath=L:\Special Log Location\mongod.log

This will also fail to start due to the unquoted file paths.

Comment by Tad Marshall [ 30/Nov/12 ]

I verified that this is a bug. It's been in every version since the Windows Service feature was introduced, and affects mongos as well as mongod.

The bug is triggered in this example by using an equal sign between "--serviceName" and the text "My Service". Boost::program_options correctly parses this as an option with key "serviceName" and value "My Service", but our logic for constructing the command line for the service uses the original argc and argv as provided by Windows.

Windows considers '--serviceName="My Service"' to be a single argument. Our logic removes all arguments that begin with "--service" or "-service" and also removes the argument that follows, which is assumed to be the value associated with the key. In this case, it removes the key "--serviceDescription". This leaves "My Service" as an "unknown" key, which is then copied verbatim into the constructed command line.

Using a different example command line, this bug could produce a start-able service, but one in which important parameters are missing. Consider this case:

C:\mongo> mongod --install --serviceName="Name" --dbpath="U:\SpecialLocation" --serviceDescription="Description" --port=20001 --serviceDisplayName="DisplayName" --rest --logpath="L:\SpecialLogLocation\mongod.log"

This command will result in this command line for the service:

"C:\mongo\mongod.exe" --service --logpath=L:\SpecialLogLocation\mongod.log

The incorrect parsing of 'serviceName' caused 'dbpath' to be skipped, the incorrect parsing of 'serviceDescription' caused 'port' to be skipped and the incorrect parsing of 'serviceDisplayName' caused 'rest' to be skipped.

The workaround, besides editing an incorrect registry entry, is to separate parameter keys and values with a space instead of an equal sign.

Generated at Thu Feb 08 03:14:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.