如何确保 Windows 2008 R2 将 SERVICE_CONTROL_PRESHUTDOWN 控制通知传递给服务
我正在使用纯 C++ 和 Windows API 编写一个服务,并希望它接收 Windows 2008 R2 提供的 PRESHUTDOWN 通知。
http://msdn.microsoft。 com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx
我希望此服务能够处理预关闭事件并防止机器在任意时间内完成关闭。
我的 ServiceMain 循环,每 5 秒注销一条消息。
我将 dwControlsAccepted 设置为:
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN
我在 ServiceControl 回调中收到了 SERVICE_CONTROL_STOP 通知,效果很好。
如果我运行我的服务,并调用
sc query "MyService"
它,它会说:
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
一旦收到任何服务控制通知,我就会在日志文件中写入一个条目,甚至在我查看它是什么之前。
当我关机时,Windows 会在屏幕上用白色的大字显示“Stopping MyService”,然后等待一段时间,然后完成关机。
但是:
主循环不断写入日志文件。 日志文件不包含任何服务控制尝试的引用。
然后 Windows 放弃关闭我的服务并杀死它。
现在,如果我做完全相同的事情,但注册 SHUTDOWN 而不是 PRESHUTDOWN,我会在日志文件中看到我期望看到的所有通知(尽管不能延长关闭时间,因为我们不能这样做)来自 SHUTDOWN 通知)。
对代码的唯一更改是删除了设置状态调用中的三个字母“PRE”。
我什至还没有达到可以开始增加 dwCheckPoint 的程度 - windows 子系统没有通知我。
Windows 知道我正在寻找预关闭消息,如“sc 查询”所示,有什么可以阻止它接收预关闭消息吗?
相关方法:
报告状态时(SCM 报告服务已启动,非常高兴)
VOID ServiceReportStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
// Fill in the SERVICE_STATUS structure.
g_serviceStatus.dwCurrentState = dwCurrentState;
g_serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
g_serviceStatus.dwWaitHint = dwWaitHint;
switch(dwCurrentState)
{
case SERVICE_START_PENDING:
case SERVICE_STOP_PENDING:
case SERVICE_CONTINUE_PENDING:
case SERVICE_PAUSE_PENDING:
g_serviceStatus.dwControlsAccepted = 0;
break;
default:
g_serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN;
}
// Report the status of the service to the SCM.
SetServiceStatus (g_serviceStatusHandle, &g_serviceStatus);
}
当我收到消息时:
VOID WINAPI ServiceCtrlHandler (DWORD dwCtrl)
{
LOG(eDeveloper10, L"ServiceCtrlHandler", L"Decoding SvcControl %d\n", dwCtrl);
switch(dwCtrl)
{
case SERVICE_CONTROL_PRESHUTDOWN:
{
LOG(eDeveloper10, L"ServiceCtrlHandler", L"Received SERVICE_CONTROL_PRESHUTDOWN\n");
ServiceReportStatus(SERVICE_STOP_PENDING, NO_ERROR, 10000);
// Signal the service to stop.
SetEvent(g_serviceStopEvent);
break;
}
}
}
它执行了更多操作,但对于此示例没有任何用处,因为我什至没有看到日志(除非我正在注册)改为关闭)。
ServiceCtrlHandler 总是快速返回,我不会在这里阻塞,也不会进入这里进行预关闭处理程序。 注册 PRESHUTDOWN 和 SHUTDOWN 也没有帮助。 我在两台 Windows 2008R2 机器(一台虚拟机,一台物理机)上尝试过此操作,结果相同。 服务被编译为 x64 二进制文件。
有什么想法吗?
一些示例日志,此日志是当我请求关闭通知时的日志:
L0 T2672 2781140 [CDbgEntry::LoadSystemDetails()] System Details Follow:
Computer Name : \\****
Domain/Workgroup : **** [Domain]
User Name : SYSTEM
Operating System : Windows NT Version 6.1 Build 7600
Service Pack : None.
Terminal Services : Terminal Services Present
System Root : C:\Windows\system32
User's Windir : C:\Windows
This file is recording debug messages with a maximum level of 10
L10 T2672 2781140 [ServiceInitialize] Service initialised, waiting for stop event
L10 T2672 2786140 [ServiceInitialize] Still waiting for stop event.
L10 T2668 2789328 [ServiceCtrlHandler] Decoding SvcControl 5
L10 T2668 2789328 [ServiceCtrlHandler] Received SERVICE_CONTROL_SHUTDOWN
L10 T2672 2789328 [ServiceInitialize] Stop event detected
L10 T2672 2789328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2790328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2791328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2792328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2793328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2794328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2795437 [ServiceInitialize] Blocking service shutdown
L10 T2672 2796437 [ServiceInitialize] Blocking service shutdown
请注意,在这种特殊情况下,阻塞服务关闭是正确的行为。
如果我注册 PRESHUTDOWN,我根本不会从 ServiceCtrlHandler 得到任何信息:
L0 T2244 516625 [CDbgEntry::LoadSystemDetails()] System Details Follow:
Computer Name : \\****
Domain/Workgroup : **** [Domain]
User Name : SYSTEM
Operating System : Windows NT Version 6.1 Build 7600
Service Pack : None.
Terminal Services : Terminal Services Present
System Root : C:\Windows\system32
User's Windir : C:\Windows
This file is recording debug messages with a maximum level of 10
L10 T2244 516453 [ServiceInitialize] Service initialised, waiting for stop event
L10 T2244 521625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 526625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 531625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 536625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 541625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 546625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 551625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 556625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 561625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 566625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 571625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 576625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 581625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 586625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 591625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 596625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 601625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 606625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 611625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 616625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 621625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 626625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 631625 [ServiceInitialize] Still waiting for stop event.
没有任何事件,直到 Windows 放弃并终止它。
I'm writing a service using plain C++ and the windows API and want it to receive the PRESHUTDOWN notifications which are available from Windows 2008 R2.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx
I want this service to handle pre-shutdown events and prevent the machine from finishing its shutdown for an arbitrary time.
My ServiceMain loops, logging out a message every 5 seconds.
I'm setting dwControlsAccepted to:
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN
I'm receiving the SERVICE_CONTROL_STOP notification in my ServiceControl callback just fine.
If I run my service, and call
sc query "MyService"
It says:
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
I write an entry to the log file as soon as I receive any service control notification, before I even look at what it is.
When I shutdown, windows makes a point of saying "Stopping MyService" in big white letters on the screen, and waits for a bit, then completes shutdown.
However:
The main loop keeps writing to the log file.
The log file contains no reference to any service control attempt.
Then windows gives up on closing my service and just kills it.
Now, if I do exactly the same thing, but register for SHUTDOWN instead of PRESHUTDOWN, I see all the notifications in the log file that I would expect to see (although can't extend the shutdown period, as we can't do that from the SHUTDOWN notification).
The only change to the code is the removal of the three letters "PRE" in the calls to set status.
I'm not even getting to the point where I can start incrementing dwCheckPoint - the windows subsystem isn't notifying me.
Windows knows I'm looking for pre-shutdown messages, as evidenced by "sc query", is there anything that can be preventing it from receiving the pre-shutdown message?
Relevant methods:
When reporting the status (SCM reports service as started, quite happily)
VOID ServiceReportStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
// Fill in the SERVICE_STATUS structure.
g_serviceStatus.dwCurrentState = dwCurrentState;
g_serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
g_serviceStatus.dwWaitHint = dwWaitHint;
switch(dwCurrentState)
{
case SERVICE_START_PENDING:
case SERVICE_STOP_PENDING:
case SERVICE_CONTINUE_PENDING:
case SERVICE_PAUSE_PENDING:
g_serviceStatus.dwControlsAccepted = 0;
break;
default:
g_serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN;
}
// Report the status of the service to the SCM.
SetServiceStatus (g_serviceStatusHandle, &g_serviceStatus);
}
When I receive a message:
VOID WINAPI ServiceCtrlHandler (DWORD dwCtrl)
{
LOG(eDeveloper10, L"ServiceCtrlHandler", L"Decoding SvcControl %d\n", dwCtrl);
switch(dwCtrl)
{
case SERVICE_CONTROL_PRESHUTDOWN:
{
LOG(eDeveloper10, L"ServiceCtrlHandler", L"Received SERVICE_CONTROL_PRESHUTDOWN\n");
ServiceReportStatus(SERVICE_STOP_PENDING, NO_ERROR, 10000);
// Signal the service to stop.
SetEvent(g_serviceStopEvent);
break;
}
}
}
It does more, but nothing useful for this example, as I don't even see the log (unless I'm registering for SHUTDOWN instead).
The ServiceCtrlHandler always returns quickly, I'm not blocking in here, I'm not getting in to here for the pre-shutdown handler.
Registering for both PRESHUTDOWN and SHUTDOWN doesn't help either.
I've tried this on two Windows 2008R2 machines, one virtual machine, one physical, with the same results.
Service is compiled as a x64 binary.
Any ideas?
Some sample logs, this log is when I request to be notified of SHUTDOWN:
L0 T2672 2781140 [CDbgEntry::LoadSystemDetails()] System Details Follow:
Computer Name : \\****
Domain/Workgroup : **** [Domain]
User Name : SYSTEM
Operating System : Windows NT Version 6.1 Build 7600
Service Pack : None.
Terminal Services : Terminal Services Present
System Root : C:\Windows\system32
User's Windir : C:\Windows
This file is recording debug messages with a maximum level of 10
L10 T2672 2781140 [ServiceInitialize] Service initialised, waiting for stop event
L10 T2672 2786140 [ServiceInitialize] Still waiting for stop event.
L10 T2668 2789328 [ServiceCtrlHandler] Decoding SvcControl 5
L10 T2668 2789328 [ServiceCtrlHandler] Received SERVICE_CONTROL_SHUTDOWN
L10 T2672 2789328 [ServiceInitialize] Stop event detected
L10 T2672 2789328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2790328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2791328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2792328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2793328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2794328 [ServiceInitialize] Blocking service shutdown
L10 T2672 2795437 [ServiceInitialize] Blocking service shutdown
L10 T2672 2796437 [ServiceInitialize] Blocking service shutdown
Note that the Blocking Service shutdown is the correct behaviour in this particular case.
If I register for PRESHUTDOWN, I get nothing from the ServiceCtrlHandler at all:
L0 T2244 516625 [CDbgEntry::LoadSystemDetails()] System Details Follow:
Computer Name : \\****
Domain/Workgroup : **** [Domain]
User Name : SYSTEM
Operating System : Windows NT Version 6.1 Build 7600
Service Pack : None.
Terminal Services : Terminal Services Present
System Root : C:\Windows\system32
User's Windir : C:\Windows
This file is recording debug messages with a maximum level of 10
L10 T2244 516453 [ServiceInitialize] Service initialised, waiting for stop event
L10 T2244 521625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 526625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 531625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 536625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 541625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 546625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 551625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 556625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 561625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 566625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 571625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 576625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 581625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 586625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 591625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 596625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 601625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 606625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 611625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 616625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 621625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 626625 [ServiceInitialize] Still waiting for stop event.
L10 T2244 631625 [ServiceInitialize] Still waiting for stop event.
No events whatsoever, until windows gives up and kills it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档, SERVICE_CONTROL_PRESHUTDOWN 只发送到扩展回调。尝试使用 RegisterServiceCtrlHandlerEx 。
According to the documentation, SERVICE_CONTROL_PRESHUTDOWN is only sent to the extended callback. Try using RegisterServiceCtrlHandlerEx.