What guarantees does the service control manager (SCM) make about the method calls on a service?
Does it guarantee against cases like the following?
The best references I've found so far seem to indicate that these guarantees are indeed made, but the only reference in the documentation I found was pretty vague, so I'm trying to find a better reference.
Here's a thread on the MSDN forums that says that the SCM ensures the state is consistent or "At least I've never found a case where [the SCM sending a stop request if the service isn't running or paused] could happen".
Here's the best reference I've found so far in the docs:
These methods [OnStart/OnStop/etc.] represent states that the service moves through in its lifetime; the service transitions from one state to the next. For example, you will never get the service to respond to an OnContinue command before OnStart has been called.
Michael Taylor provided a good answer to this question on the MSDN forums referencing the Win32 API which documents what the SCM will do in response to invalid control codes (reject them in various ways).
State change methods are called only when the SCM believes the service is in the appropriate state and assuming that the service says it can handle the state (pause/continue).
- OnStart will be called if the service is stopped.
- OnPause is called if the service is running.
- OnContinue if the service is paused.
- A paused service can only be resumed or stopped.
- A running service can only be paused or stopped.
- SCM serializes commands to a service so only a single command will be running at a time.
Refer to the Win32 documentation on how services work for more information.