#463 Crash in dispatchServiceCtrl() for service with power event

closed-fixed
None
5
2010-08-30
2009-12-31
rolyapples
No

If you create a service that accepts power events (by overriding GetAcceptedControls), the service will crash in dispatchServiceCtrl() when any power event other than PBT_POWERSETTINGCHANGE occurs.

The problem is that the code assumes eventData for a SERVICE_CONTROL_POWEREVENT is always a pointer to a POWERBROADCAST_SETTING structure, however that is only the case when dwEventType is PBT_POWERSETTINGCHANGE. For other event types (e.g. PBT_APMSUSPEND), the eventData is null.

To reproduce:

1. From your python install directory run "python Lib\site-packages\win32\Demos\service\serviceEvents.py install"
2. Run "python Lib\site-packages\win32\Demos\service\serviceEvents.py start"
3. Put your machine to sleep
4. Wake your machine
5. Check the windows application event log - see that there's an error for the service & that the service is no longer running

Changing this in dispatchServiceCtrl() in PythonService.cpp:

case SERVICE_CONTROL_POWEREVENT: {
POWERBROADCAST_SETTING *pbs = (POWERBROADCAST_SETTING *)eventData;
sub = Py_BuildValue("NN",
PyWinObject_FromIID(pbs->PowerSetting),
PyString_FromStringAndSize((char *)pbs->Data, pbs->DataLength));
break;

To the following is working for me:

case SERVICE_CONTROL_POWEREVENT: {
if (dwEventType == PBT_POWERSETTINGCHANGE) {
POWERBROADCAST_SETTING *pbs = (POWERBROADCAST_SETTING *)eventData;
sub = Py_BuildValue("NN",
PyWinObject_FromIID(pbs->PowerSetting),
PyString_FromStringAndSize((char *)pbs->Data, pbs->DataLength));
}
else {
sub = Py_None;
Py_INCREF(sub);
}
break;

Discussion

  • Mark Hammond

    Mark Hammond - 2010-01-02
    • assigned_to: nobody --> mhammond
    • status: open --> open-accepted
     
  • Mark Hammond

    Mark Hammond - 2010-01-02

    looks good, thanks!

     
  • Mark Hammond

    Mark Hammond - 2010-08-30
    • status: open-accepted --> closed-fixed
     
  • Mark Hammond

    Mark Hammond - 2010-08-30

    thanks
    Checking in PythonService.cpp;
    new revision: 1.28; previous revision: 1.27

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks