Xcode....
[reactos.git] / reactos / ntoskrnl / io / pnpmgr / plugplay.c
index fda41f4..9c01544 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <ntoskrnl.h>
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 
 #if defined (ALLOC_PRAGMA)
 #pragma alloc_text(INIT, IopInitPlugPlayEvents)
@@ -18,8 +18,8 @@
 
 typedef struct _PNP_EVENT_ENTRY
 {
-  LIST_ENTRY ListEntry;
-  PLUGPLAY_EVENT_BLOCK Event;
+    LIST_ENTRY ListEntry;
+    PLUGPLAY_EVENT_BLOCK Event;
 } PNP_EVENT_ENTRY, *PPNP_EVENT_ENTRY;
 
 
@@ -95,21 +95,21 @@ IopQueueTargetDeviceEvent(const GUID *Guid,
 static NTSTATUS
 IopRemovePlugPlayEvent(VOID)
 {
-  /* Remove a pnp event entry from the tail of the queue */
-  if (!IsListEmpty(&IopPnpEventQueueHead))
-  {
-    ExFreePool(RemoveTailList(&IopPnpEventQueueHead));
-  }
-
-  /* Signal the next pnp event in the queue */
-  if (!IsListEmpty(&IopPnpEventQueueHead))
-  {
-    KeSetEvent(&IopPnpNotifyEvent,
-               0,
-               FALSE);
-  }
+    /* Remove a pnp event entry from the tail of the queue */
+    if (!IsListEmpty(&IopPnpEventQueueHead))
+    {
+        ExFreePool(CONTAINING_RECORD(RemoveTailList(&IopPnpEventQueueHead), PNP_EVENT_ENTRY, ListEntry));
+    }
+
+    /* Signal the next pnp event in the queue */
+    if (!IsListEmpty(&IopPnpEventQueueHead))
+    {
+        KeSetEvent(&IopPnpNotifyEvent,
+                   0,
+                   FALSE);
+    }
 
-  return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
 static PDEVICE_OBJECT
@@ -148,8 +148,7 @@ IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance)
         return NULL;
 
     if (DeviceInstance == NULL ||
-        DeviceInstance->Length == 0
-        )
+        DeviceInstance->Length == 0)
     {
         if (IopRootDeviceNode->PhysicalDeviceObject)
         {
@@ -171,40 +170,41 @@ IopCaptureUnicodeString(PUNICODE_STRING DstName, PUNICODE_STRING SrcName)
     UNICODE_STRING Name;
 
     Name.Buffer = NULL;
-    _SEH_TRY
-    {
-       Name.Length = SrcName->Length;
-       Name.MaximumLength = SrcName->MaximumLength;
-       if (Name.Length > Name.MaximumLength)
-       {
-           Status = STATUS_INVALID_PARAMETER;
-           _SEH_LEAVE;
-       }
-       if (Name.MaximumLength)
-       {
-           ProbeForRead(SrcName->Buffer,
-                        Name.MaximumLength,
-                        sizeof(WCHAR));
-           Name.Buffer = ExAllocatePool(NonPagedPool, Name.MaximumLength);
-           if (Name.Buffer == NULL)
-           {
-               Status = STATUS_INSUFFICIENT_RESOURCES;
-               _SEH_LEAVE;
-           }
-           memcpy(Name.Buffer, SrcName->Buffer, Name.MaximumLength);
-       }
-       *DstName = Name;
-    }
-    _SEH_HANDLE
-    {
-        Status = _SEH_GetExceptionCode();
-    }
-    _SEH_END;
-    
-    if (!NT_SUCCESS(Status) && Name.Buffer)
-    {   
-       ExFreePool(Name.Buffer);
+    _SEH2_TRY
+    {
+        Name.Length = SrcName->Length;
+        Name.MaximumLength = SrcName->MaximumLength;
+        if (Name.Length > Name.MaximumLength)
+        {
+            Status = STATUS_INVALID_PARAMETER;
+            _SEH2_LEAVE;
+        }
+
+        if (Name.MaximumLength)
+        {
+            ProbeForRead(SrcName->Buffer,
+                         Name.MaximumLength,
+                         sizeof(WCHAR));
+            Name.Buffer = ExAllocatePool(NonPagedPool, Name.MaximumLength);
+            if (Name.Buffer == NULL)
+            {
+                Status = STATUS_INSUFFICIENT_RESOURCES;
+                _SEH2_LEAVE;
+            }
+
+            memcpy(Name.Buffer, SrcName->Buffer, Name.MaximumLength);
+        }
+
+        *DstName = Name;
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        if (Name.Buffer)
+            ExFreePool(Name.Buffer);
+        Status = _SEH2_GetExceptionCode();
     }
+    _SEH2_END;
+
     return Status;
 }
 
@@ -212,7 +212,7 @@ static NTSTATUS
 IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData)
 {
     PDEVICE_OBJECT DeviceObject = NULL;
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
     UNICODE_STRING DeviceInstance;
     ULONG BufferSize;
     ULONG Property = 0;
@@ -224,28 +224,23 @@ IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData)
     Status = IopCaptureUnicodeString(&DeviceInstance, &PropertyData->DeviceInstance);
     if (!NT_SUCCESS(Status))
     {
-       return Status;
+        return Status;
     }
 
-    _SEH_TRY
+    _SEH2_TRY
     {
-       Property = PropertyData->Property;
+        Property = PropertyData->Property;
         BufferSize = PropertyData->BufferSize;
         ProbeForWrite(PropertyData->Buffer,
                       BufferSize,
                       sizeof(UCHAR));
     }
-    _SEH_HANDLE
-    {
-        Status = _SEH_GetExceptionCode();
-    }
-    _SEH_END;
-    
-    if (!NT_SUCCESS(Status))
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-       ExFreePool(DeviceInstance.Buffer);
-       return Status;
+        ExFreePool(DeviceInstance.Buffer);
+        _SEH2_YIELD(return _SEH2_GetExceptionCode());
     }
+    _SEH2_END;
 
     /* Get the device object */
     DeviceObject = IopGetDeviceObjectFromDeviceInstance(&DeviceInstance);
@@ -258,10 +253,9 @@ IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData)
     Buffer = ExAllocatePool(NonPagedPool, BufferSize);
     if (Buffer == NULL)
     {
-       return STATUS_INSUFFICIENT_RESOURCES;
+        return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-
     Status = IoGetDeviceProperty(DeviceObject,
                                  Property,
                                  BufferSize,
@@ -272,17 +266,18 @@ IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData)
 
     if (NT_SUCCESS(Status))
     {
-       _SEH_TRY
-       {
-           memcpy(Buffer, PropertyData->Buffer, BufferSize);
-           PropertyData->BufferSize = BufferSize;
-       }
-       _SEH_HANDLE
-       {
-           Status = _SEH_GetExceptionCode();
-       }
-       _SEH_END;
+        _SEH2_TRY
+        {
+            memcpy(PropertyData->Buffer, Buffer, BufferSize);
+            PropertyData->BufferSize = BufferSize;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            Status = _SEH2_GetExceptionCode();
+        }
+        _SEH2_END;
     }
+
     ExFreePool(Buffer);
     return Status;
 }
@@ -306,28 +301,23 @@ IopGetRelatedDevice(PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA RelatedDeviceData)
     Status = IopCaptureUnicodeString(&TargetDeviceInstance, &RelatedDeviceData->TargetDeviceInstance);
     if (!NT_SUCCESS(Status))
     {
-       return Status;
+        return Status;
     }
 
-    _SEH_TRY
+    _SEH2_TRY
     {
-       Relation = RelatedDeviceData->Relation;
-       MaximumLength = RelatedDeviceData->RelatedDeviceInstanceLength;
-       ProbeForWrite(RelatedDeviceData->RelatedDeviceInstance,
-                     MaximumLength,
-                     sizeof(WCHAR));
+        Relation = RelatedDeviceData->Relation;
+        MaximumLength = RelatedDeviceData->RelatedDeviceInstanceLength;
+        ProbeForWrite(RelatedDeviceData->RelatedDeviceInstance,
+                      MaximumLength,
+                      sizeof(WCHAR));
     }
-    _SEH_HANDLE
-    {
-        Status = _SEH_GetExceptionCode();
-    }
-    _SEH_END;
-
-    if (!NT_SUCCESS(Status))
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
         ExFreePool(TargetDeviceInstance.Buffer);
-       return Status;
+        _SEH2_YIELD(return _SEH2_GetExceptionCode());
     }
+    _SEH2_END;
 
     RtlInitUnicodeString(&RootDeviceName,
                          L"HTREE\\ROOT\\0");
@@ -336,13 +326,13 @@ IopGetRelatedDevice(PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA RelatedDeviceData)
                               TRUE))
     {
         DeviceNode = IopRootDeviceNode;
-       ExFreePool(TargetDeviceInstance.Buffer);
+        ExFreePool(TargetDeviceInstance.Buffer);
     }
     else
     {
         /* Get the device object */
         DeviceObject = IopGetDeviceObjectFromDeviceInstance(&TargetDeviceInstance);
-       ExFreePool(TargetDeviceInstance.Buffer);
+        ExFreePool(TargetDeviceInstance.Buffer);
         if (DeviceObject == NULL)
             return STATUS_NO_SUCH_DEVICE;
 
@@ -393,18 +383,18 @@ IopGetRelatedDevice(PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA RelatedDeviceData)
     }
 
     /* Copy related device instance name */
-    _SEH_TRY
+    _SEH2_TRY
     {
         RtlCopyMemory(RelatedDeviceData->RelatedDeviceInstance,
                       RelatedDeviceNode->InstancePath.Buffer,
                       RelatedDeviceNode->InstancePath.Length);
         RelatedDeviceData->RelatedDeviceInstanceLength = RelatedDeviceNode->InstancePath.Length;
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = _SEH_GetExceptionCode();
+        Status = _SEH2_GetExceptionCode();
     }
-    _SEH_END;
+    _SEH2_END;
 
     if (DeviceObject != NULL)
     {
@@ -426,7 +416,7 @@ IopDeviceStatus(PPLUGPLAY_CONTROL_STATUS_DATA StatusData)
     ULONG DeviceStatus = 0;
     ULONG DeviceProblem = 0;
     UNICODE_STRING DeviceInstance;
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
 
     DPRINT("IopDeviceStatus() called\n");
 
@@ -435,27 +425,21 @@ IopDeviceStatus(PPLUGPLAY_CONTROL_STATUS_DATA StatusData)
         return Status;
     DPRINT("Device name: '%wZ'\n", &DeviceInstance);
 
-    _SEH_TRY
+    _SEH2_TRY
     {
-       Operation = StatusData->Operation;
-       if (Operation == PNP_SET_DEVICE_STATUS)
-       {
-           DeviceStatus = StatusData->DeviceStatus;
-           DeviceProblem = StatusData->DeviceProblem;
-       }
+        Operation = StatusData->Operation;
+        if (Operation == PNP_SET_DEVICE_STATUS)
+        {
+            DeviceStatus = StatusData->DeviceStatus;
+            DeviceProblem = StatusData->DeviceProblem;
+        }
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = _SEH_GetExceptionCode();
-    }
-    _SEH_END;
-
-    if (!NT_SUCCESS(Status))
-    {
-        if (DeviceInstance.Buffer)
-            ExFreePool(DeviceInstance.Buffer);
-        return Status;
+        if (DeviceInstance.Buffer) ExFreePool(DeviceInstance.Buffer);
+        _SEH2_YIELD(return _SEH2_GetExceptionCode());
     }
+    _SEH2_END;
 
     /* Get the device object */
     DeviceObject = IopGetDeviceObjectFromDeviceInstance(&DeviceInstance);
@@ -488,16 +472,16 @@ IopDeviceStatus(PPLUGPLAY_CONTROL_STATUS_DATA StatusData)
 
     if (Operation == PNP_GET_DEVICE_STATUS)
     {
-       _SEH_TRY
-       {
-           StatusData->DeviceStatus = DeviceStatus;
-           StatusData->DeviceProblem = DeviceProblem;
-       }
-       _SEH_HANDLE
-       {
-           Status = _SEH_GetExceptionCode();
-       }
-       _SEH_END;
+        _SEH2_TRY
+        {
+            StatusData->DeviceStatus = DeviceStatus;
+            StatusData->DeviceProblem = DeviceProblem;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            Status = _SEH2_GetExceptionCode();
+        }
+        _SEH2_END;
     }
 
     return Status;
@@ -518,7 +502,7 @@ IopGetDeviceDepth(PPLUGPLAY_CONTROL_DEPTH_DATA DepthData)
     Status = IopCaptureUnicodeString(&DeviceInstance, &DepthData->DeviceInstance);
     if (!NT_SUCCESS(Status))
     {
-       return Status;
+        return Status;
     }
 
     /* Get the device object */
@@ -529,19 +513,17 @@ IopGetDeviceDepth(PPLUGPLAY_CONTROL_DEPTH_DATA DepthData)
 
     DeviceNode = IopGetDeviceNode(DeviceObject);
 
-    DepthData->Depth = DeviceNode->Level;
-
-    ObDereferenceObject(DeviceObject);
-
-    _SEH_TRY
+    _SEH2_TRY
     {
-       DepthData->Depth = DeviceNode->Level;
+        DepthData->Depth = DeviceNode->Level;
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = _SEH_GetExceptionCode();
+        Status = _SEH2_GetExceptionCode();
     }
-    _SEH_END;
+    _SEH2_END;
+
+    ObDereferenceObject(DeviceObject);
 
     return Status;
 }
@@ -651,59 +633,59 @@ NtGetPlugPlayEvent(IN ULONG Reserved1,
                    OUT PPLUGPLAY_EVENT_BLOCK Buffer,
                    IN ULONG BufferSize)
 {
-  PPNP_EVENT_ENTRY Entry;
-  NTSTATUS Status;
-
-  DPRINT("NtGetPlugPlayEvent() called\n");
-
-  /* Function can only be called from user-mode */
-  if (KeGetPreviousMode() == KernelMode)
-  {
-    DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
-    return STATUS_ACCESS_DENIED;
-  }
-
-  /* Check for Tcb privilege */
-  if (!SeSinglePrivilegeCheck(SeTcbPrivilege,
-                              UserMode))
-  {
-    DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
-    return STATUS_PRIVILEGE_NOT_HELD;
-  }
-
-  /* Wait for a PnP event */
-  DPRINT("Waiting for pnp notification event\n");
-  Status = KeWaitForSingleObject(&IopPnpNotifyEvent,
-                                 UserRequest,
-                                 KernelMode,
-                                 FALSE,
-                                 NULL);
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("KeWaitForSingleObject() failed (Status %lx)\n", Status);
-    return Status;
-  }
+    PPNP_EVENT_ENTRY Entry;
+    NTSTATUS Status;
 
-  /* Get entry from the tail of the queue */
-  Entry = CONTAINING_RECORD(IopPnpEventQueueHead.Blink,
-                            PNP_EVENT_ENTRY,
-                            ListEntry);
+    DPRINT("NtGetPlugPlayEvent() called\n");
 
-  /* Check the buffer size */
-  if (BufferSize < Entry->Event.TotalSize)
-  {
-    DPRINT1("Buffer is too small for the pnp-event\n");
-    return STATUS_BUFFER_TOO_SMALL;
-  }
+    /* Function can only be called from user-mode */
+    if (KeGetPreviousMode() == KernelMode)
+    {
+        DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
+        return STATUS_ACCESS_DENIED;
+    }
 
-  /* Copy event data to the user buffer */
-  memcpy(Buffer,
-         &Entry->Event,
-         Entry->Event.TotalSize);
+    /* Check for Tcb privilege */
+    if (!SeSinglePrivilegeCheck(SeTcbPrivilege,
+                                UserMode))
+    {
+        DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
+        return STATUS_PRIVILEGE_NOT_HELD;
+    }
+
+    /* Wait for a PnP event */
+    DPRINT("Waiting for pnp notification event\n");
+    Status = KeWaitForSingleObject(&IopPnpNotifyEvent,
+                                   UserRequest,
+                                   KernelMode,
+                                   FALSE,
+                                   NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("KeWaitForSingleObject() failed (Status %lx)\n", Status);
+        return Status;
+    }
 
-  DPRINT("NtGetPlugPlayEvent() done\n");
+    /* Get entry from the tail of the queue */
+    Entry = CONTAINING_RECORD(IopPnpEventQueueHead.Blink,
+                              PNP_EVENT_ENTRY,
+                              ListEntry);
 
-  return STATUS_SUCCESS;
+    /* Check the buffer size */
+    if (BufferSize < Entry->Event.TotalSize)
+    {
+        DPRINT1("Buffer is too small for the pnp-event\n");
+        return STATUS_BUFFER_TOO_SMALL;
+    }
+
+    /* Copy event data to the user buffer */
+    memcpy(Buffer,
+           &Entry->Event,
+           Entry->Event.TotalSize);
+
+    DPRINT("NtGetPlugPlayEvent() done\n");
+
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -768,8 +750,6 @@ NtPlugPlayControl(IN PLUGPLAY_CONTROL_CLASS PlugPlayControlClass,
                   IN OUT PVOID Buffer,
                   IN ULONG BufferLength)
 {
-    NTSTATUS Status = STATUS_SUCCESS;
-
     DPRINT("NtPlugPlayControl(%lu %p %lu) called\n",
            PlugPlayControlClass, Buffer, BufferLength);
 
@@ -789,22 +769,17 @@ NtPlugPlayControl(IN PLUGPLAY_CONTROL_CLASS PlugPlayControlClass,
     }
 
     /* Probe the buffer */
-    _SEH_TRY
+    _SEH2_TRY
     {
         ProbeForWrite(Buffer,
                       BufferLength,
                       sizeof(ULONG));
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = _SEH_GetExceptionCode();
-    }
-    _SEH_END;
-
-    if (!NT_SUCCESS(Status))
-    {
-        return Status;
+        _SEH2_YIELD(return _SEH2_GetExceptionCode());
     }
+    _SEH2_END;
 
     switch (PlugPlayControlClass)
     {