locked
System wide locking in XP? RRS feed

  • Question

  • Hello Experts,

    In Windows XP, is there any way to implement a system-wide locking mechanism like FwpmTransactionBegin0() so that multiple drivers can operate on the shared-resource safely and load/unload without dependency?

    Is it possible to use zwCreateFile as a locking mechanism with following parameter setup?

    • DesiredAccess = FILE_READ_DATA
    • ShareAccess = NONE
    If zwCreateFile is possible, do I really have to create a file/folder?


    Best regards,
    Peter.

    Tuesday, January 11, 2011 8:09 AM

Answers

  • Peter,

       Yes you can use the old trick from user space of creating a file with exclusive access and using that. 

       The reason I've used small legacy driver model is because once it is working it rarely if ever changes.  Basically you have a driver you open, send an IOCTL to get the pointer to the data area, and when your driver is finished using the data area closes it.  The only thing the driver knows about the other drivers is how to allocate and initialize the data area (or at least the lock and a flag to indicate the rest of the area is not freed).  Once the legacy driver is loaded no one touches it, but since it does almost nothing, this is not a burden for the hotfix model.

        A kernel mode DLL can be crafted so that it can detect if it has initialized a lock previously or not.  The DLL would export functions to acquire and release the lock that it defines in it static data area.  If an acquire comes before the lock is initialized initialize it then acquire it.

     


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    • Proposed as answer by Don Burn [Windrvr] Tuesday, January 11, 2011 6:48 PM
    • Marked as answer by peter..._ Friday, January 14, 2011 4:53 AM
    Tuesday, January 11, 2011 6:48 PM

All replies

  • Any number of driver may share data structures and the locks for them.  The only "special effort" is providing a way for the drivers to find the locations of the structures and locks.  A common approach to this a small legacy driver that the other drivers open, then either call IOCTL's to get information, or call an IOCTL to get a pointer to the data structure with locks.  Variations on the theme include kernel DLL's to do this, or a PnP driver creating a device interface that other PnP drivers register for.

    The one caveat is do not try this with locking mechanisms from KMDF, the framework works on objects only owned by the driver include locks.


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    Tuesday, January 11, 2011 12:15 PM
  • Thank you very much, Don.

    Sorry I didn't state the requirement that all drivers must be loaded and unloaded independently without any dependency.

    So how do I synchronize situation like Driver A allocates the data structure and set a ready-event. In the middle driver B's IOCTL finding its way to driver A to get the data structure, driver A has already been unloaded?

    Really appreciate for any suggestions.

    Best regards,

    Peter.

    Tuesday, January 11, 2011 2:25 PM
  • Why do you have such a requirement, it seems to be very artificial?  I typically do this with a small legacy driver that on the first open creates the data structures, there is little in the way of resources taken by the driver.

    You can do this with a kernel mode DLL and some tracking uf usage, but getting this right can be painful.  It means your requirement can be met, but the overhead of doing it that way is a lot bigger than a small driver dedicated to the problem.


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    Tuesday, January 11, 2011 2:32 PM
  • Thank you Don!

    The requirement is so because we wish each driver's owner team can hotfix (unload and load new driver) their driver without stopping other drivers (features).

    Currently by using CallbackObject, lots of event, and serializing cross-driver communication tasks into one thread, i am able to accomplish most of the load/unload synchronization problem but indeed as you said, it's quite painful task. In the end I see the problem is there is no permanent system-wide lock for drivers to use without worrying about is the lock still there..

    Do you think I can use zwCreateFile as a global lock? Is there any other kernel createXXX API that has both 'DesiredAccess' and 'SharedAccess' parameter? Can I use ZwCreateFile to create a name-pipe (as far as I know, name pipe is a file-system driver, right?) instead of a real file/directory?

    I really appreciate your suggestions.

     

    Best regards,

    Peter.

    Tuesday, January 11, 2011 6:16 PM
  • Peter,

       Yes you can use the old trick from user space of creating a file with exclusive access and using that. 

       The reason I've used small legacy driver model is because once it is working it rarely if ever changes.  Basically you have a driver you open, send an IOCTL to get the pointer to the data area, and when your driver is finished using the data area closes it.  The only thing the driver knows about the other drivers is how to allocate and initialize the data area (or at least the lock and a flag to indicate the rest of the area is not freed).  Once the legacy driver is loaded no one touches it, but since it does almost nothing, this is not a burden for the hotfix model.

        A kernel mode DLL can be crafted so that it can detect if it has initialized a lock previously or not.  The DLL would export functions to acquire and release the lock that it defines in it static data area.  If an acquire comes before the lock is initialized initialize it then acquire it.

     


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    • Proposed as answer by Don Burn [Windrvr] Tuesday, January 11, 2011 6:48 PM
    • Marked as answer by peter..._ Friday, January 14, 2011 4:53 AM
    Tuesday, January 11, 2011 6:48 PM