SVN maintenance
[reactos.git] / reactos / subsys / system / services / driver.c
index 9764200..f0d93f1 100644 (file)
-/*\r
- * driver.c\r
- */\r
-\r
-/* INCLUDES *****************************************************************/\r
-\r
-#include "services.h"\r
-\r
-#define NDEBUG\r
-#include <debug.h>\r
-\r
-/* FUNCTIONS ****************************************************************/\r
-\r
-NTSTATUS\r
-ScmLoadDriver(PSERVICE lpService)\r
-{\r
-    WCHAR szDriverPath[MAX_PATH];\r
-    UNICODE_STRING DriverPath;\r
-    NTSTATUS Status;\r
-\r
-    /* Build the driver path */\r
-    wcscpy(szDriverPath,\r
-           L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");\r
-    wcscat(szDriverPath,\r
-           lpService->lpServiceName);\r
-\r
-    RtlInitUnicodeString(&DriverPath,\r
-                         szDriverPath);\r
-\r
-    /* FIXME: Acquire privilege */\r
-\r
-    DPRINT("  Path: %wZ\n", &DriverPath);\r
-    Status = NtLoadDriver(&DriverPath);\r
-\r
-    /* FIXME: Release privilege */\r
-\r
-    return Status;\r
-}\r
-\r
-\r
-DWORD\r
-ScmUnloadDriver(PSERVICE lpService)\r
-{\r
-    WCHAR szDriverPath[MAX_PATH];\r
-    UNICODE_STRING DriverPath;\r
-    NTSTATUS Status;\r
-    DWORD dwError = ERROR_SUCCESS;\r
-\r
-    /* Build the driver path */\r
-    wcscpy(szDriverPath,\r
-           L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");\r
-    wcscat(szDriverPath,\r
-           lpService->lpServiceName);\r
-\r
-    RtlInitUnicodeString(&DriverPath,\r
-                         szDriverPath);\r
-\r
-    /* FIXME: Acquire privilege */\r
-\r
-    Status = NtUnloadDriver(&DriverPath);\r
-\r
-    /* FIXME: Release privilege */\r
-\r
-    if (!NT_SUCCESS(Status))\r
-    {\r
-        dwError = RtlNtStatusToDosError(Status);\r
-    }\r
-\r
-    return dwError;\r
-}\r
-\r
-\r
-DWORD\r
-ScmGetDriverStatus(PSERVICE lpService,\r
-                   LPSERVICE_STATUS lpServiceStatus)\r
-{\r
-    OBJECT_ATTRIBUTES ObjectAttributes;\r
-    UNICODE_STRING DirName;\r
-    HANDLE DirHandle;\r
-    NTSTATUS Status = STATUS_SUCCESS;\r
-    POBJECT_DIRECTORY_INFORMATION DirInfo;\r
-    ULONG BufferLength;\r
-    ULONG DataLength;\r
-    ULONG Index;\r
-    DWORD dwError = ERROR_SUCCESS;\r
-    BOOLEAN bFound = FALSE;\r
-\r
-    DPRINT1("ScmGetDriverStatus() called\n");\r
-\r
-    memset(lpServiceStatus, 0, sizeof(SERVICE_STATUS));\r
-\r
-    if (lpService->Status.dwServiceType == SERVICE_KERNEL_DRIVER)\r
-    {\r
-        RtlInitUnicodeString(&DirName,\r
-                             L"\\Driver");\r
-    }\r
-    else\r
-    {\r
-        RtlInitUnicodeString(&DirName,\r
-                             L"\\FileSystem");\r
-    }\r
-\r
-    InitializeObjectAttributes(&ObjectAttributes,\r
-                               &DirName,\r
-                               0,\r
-                               NULL,\r
-                               NULL);\r
-\r
-    Status = NtOpenDirectoryObject(&DirHandle,\r
-                                   DIRECTORY_QUERY | DIRECTORY_TRAVERSE,\r
-                                   &ObjectAttributes);\r
-    if (!NT_SUCCESS(Status))\r
-    {\r
-        DPRINT1("NtOpenDirectoryObject() failed!\n");\r
-        return RtlNtStatusToDosError(Status);\r
-    }\r
-\r
-    BufferLength = sizeof(OBJECT_DIRECTORY_INFORMATION) +\r
-                   2 * MAX_PATH * sizeof(WCHAR);\r
-    DirInfo = HeapAlloc(GetProcessHeap(),\r
-                        HEAP_ZERO_MEMORY,\r
-                        BufferLength);\r
-\r
-    Index = 0;\r
-    while (TRUE)\r
-    {\r
-        Status = NtQueryDirectoryObject(DirHandle,\r
-                                        DirInfo,\r
-                                        BufferLength,\r
-                                        TRUE,\r
-                                        FALSE,\r
-                                        &Index,\r
-                                        &DataLength);\r
-        if (Status == STATUS_NO_MORE_ENTRIES)\r
-        {\r
-            DPRINT("No more services\n");\r
-            break;\r
-        }\r
-\r
-        if (!NT_SUCCESS(Status))\r
-            break;\r
-\r
-        DPRINT("Comparing: '%S'  '%wZ'\n", lpService->lpServiceName, &DirInfo->ObjectName);\r
-\r
-        if (_wcsicmp(lpService->lpServiceName, DirInfo->ObjectName.Buffer) == 0)\r
-        {\r
-            DPRINT1("Found: '%S'  '%wZ'\n",\r
-                    lpService->lpServiceName, &DirInfo->ObjectName);\r
-            bFound = TRUE;\r
-\r
-            break;\r
-        }\r
-    }\r
-\r
-    HeapFree(GetProcessHeap(),\r
-             0,\r
-             DirInfo);\r
-    NtClose(DirHandle);\r
-\r
-    if (!NT_SUCCESS(Status))\r
-    {\r
-        DPRINT1("Status: %lx\n", Status);\r
-        return RtlNtStatusToDosError(Status);\r
-    }\r
-\r
-    if ((bFound == TRUE) &&\r
-        (lpService->Status.dwCurrentState != SERVICE_STOP_PENDING))\r
-    {\r
-        if (lpService->Status.dwCurrentState == SERVICE_STOPPED)\r
-        {\r
-            lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;\r
-            lpService->Status.dwServiceSpecificExitCode = ERROR_SUCCESS;\r
-            lpService->Status.dwCheckPoint = 0;\r
-            lpService->Status.dwWaitHint = 0;\r
-            lpService->Status.dwControlsAccepted = 0;\r
-        }\r
-        else\r
-        {\r
-            lpService->Status.dwCurrentState = SERVICE_RUNNING;\r
-            lpService->Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;\r
-\r
-            if (lpService->Status.dwWin32ExitCode == ERROR_SERVICE_NEVER_STARTED)\r
-                lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;\r
-        }\r
-    }\r
-    else\r
-    {\r
-        lpService->Status.dwCurrentState = SERVICE_STOPPED;\r
-        lpService->Status.dwControlsAccepted = 0;\r
-        lpService->Status.dwCheckPoint = 0;\r
-        lpService->Status.dwWaitHint = 0;\r
-\r
-        if (lpService->Status.dwCurrentState == SERVICE_STOP_PENDING)\r
-            lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;\r
-        else\r
-            lpService->Status.dwWin32ExitCode = ERROR_GEN_FAILURE;\r
-    }\r
-\r
-    if (lpServiceStatus != NULL)\r
-    {\r
-        memcpy(lpServiceStatus,\r
-               &lpService->Status,\r
-               sizeof(SERVICE_STATUS));\r
-    }\r
-\r
-    DPRINT1("ScmGetDriverStatus() done (Error: %lu)\n", dwError);\r
-\r
-    return ERROR_SUCCESS;\r
-}\r
-\r
-\r
-DWORD\r
-ScmControlDriver(PSERVICE lpService,\r
-                 DWORD dwControl,\r
-                 LPSERVICE_STATUS lpServiceStatus)\r
-{\r
-    DWORD dwError;\r
-\r
-    DPRINT("ScmControlDriver() called\n");\r
-\r
-    switch (dwControl)\r
-    {\r
-        case SERVICE_CONTROL_STOP:\r
-            if (lpService->Status.dwCurrentState != SERVICE_RUNNING)\r
-            {\r
-                dwError = ERROR_INVALID_SERVICE_CONTROL;\r
-                goto done;\r
-            }\r
-\r
-            dwError = ScmUnloadDriver(lpService);\r
-            if (dwError == ERROR_SUCCESS)\r
-            {\r
-                lpService->Status.dwControlsAccepted = 0;\r
-                lpService->Status.dwCurrentState = SERVICE_STOPPED;\r
-            }\r
-            break;\r
-\r
-        case SERVICE_CONTROL_INTERROGATE:\r
-            dwError = ScmGetDriverStatus(lpService,\r
-                                         lpServiceStatus);\r
-            break;\r
-\r
-        default:\r
-            dwError = ERROR_INVALID_SERVICE_CONTROL;\r
-    }\r
-\r
-done:;\r
-    DPRINT("ScmControlDriver() done (Erorr: %lu)\n", dwError);\r
-\r
-    return dwError;\r
-}\r
-\r
-/* EOF */\r
+/*
+ * driver.c
+ */
+
+/* INCLUDES *****************************************************************/
+
+#include "services.h"
+
+#define NDEBUG
+#include <debug.h>
+
+/* FUNCTIONS ****************************************************************/
+
+NTSTATUS
+ScmLoadDriver(PSERVICE lpService)
+{
+    WCHAR szDriverPath[MAX_PATH];
+    UNICODE_STRING DriverPath;
+    NTSTATUS Status;
+
+    /* Build the driver path */
+    wcscpy(szDriverPath,
+           L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
+    wcscat(szDriverPath,
+           lpService->lpServiceName);
+
+    RtlInitUnicodeString(&DriverPath,
+                         szDriverPath);
+
+    /* FIXME: Acquire privilege */
+
+    DPRINT("  Path: %wZ\n", &DriverPath);
+    Status = NtLoadDriver(&DriverPath);
+
+    /* FIXME: Release privilege */
+
+    return Status;
+}
+
+
+DWORD
+ScmUnloadDriver(PSERVICE lpService)
+{
+    WCHAR szDriverPath[MAX_PATH];
+    UNICODE_STRING DriverPath;
+    NTSTATUS Status;
+    DWORD dwError = ERROR_SUCCESS;
+
+    /* Build the driver path */
+    wcscpy(szDriverPath,
+           L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
+    wcscat(szDriverPath,
+           lpService->lpServiceName);
+
+    RtlInitUnicodeString(&DriverPath,
+                         szDriverPath);
+
+    /* FIXME: Acquire privilege */
+
+    Status = NtUnloadDriver(&DriverPath);
+
+    /* FIXME: Release privilege */
+
+    if (!NT_SUCCESS(Status))
+    {
+        dwError = RtlNtStatusToDosError(Status);
+    }
+
+    return dwError;
+}
+
+
+DWORD
+ScmGetDriverStatus(PSERVICE lpService,
+                   LPSERVICE_STATUS lpServiceStatus)
+{
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING DirName;
+    HANDLE DirHandle;
+    NTSTATUS Status = STATUS_SUCCESS;
+    POBJECT_DIRECTORY_INFORMATION DirInfo;
+    ULONG BufferLength;
+    ULONG DataLength;
+    ULONG Index;
+    DWORD dwError = ERROR_SUCCESS;
+    BOOLEAN bFound = FALSE;
+
+    DPRINT1("ScmGetDriverStatus() called\n");
+
+    memset(lpServiceStatus, 0, sizeof(SERVICE_STATUS));
+
+    if (lpService->Status.dwServiceType == SERVICE_KERNEL_DRIVER)
+    {
+        RtlInitUnicodeString(&DirName,
+                             L"\\Driver");
+    }
+    else
+    {
+        RtlInitUnicodeString(&DirName,
+                             L"\\FileSystem");
+    }
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &DirName,
+                               0,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenDirectoryObject(&DirHandle,
+                                   DIRECTORY_QUERY | DIRECTORY_TRAVERSE,
+                                   &ObjectAttributes);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("NtOpenDirectoryObject() failed!\n");
+        return RtlNtStatusToDosError(Status);
+    }
+
+    BufferLength = sizeof(OBJECT_DIRECTORY_INFORMATION) +
+                   2 * MAX_PATH * sizeof(WCHAR);
+    DirInfo = HeapAlloc(GetProcessHeap(),
+                        HEAP_ZERO_MEMORY,
+                        BufferLength);
+
+    Index = 0;
+    while (TRUE)
+    {
+        Status = NtQueryDirectoryObject(DirHandle,
+                                        DirInfo,
+                                        BufferLength,
+                                        TRUE,
+                                        FALSE,
+                                        &Index,
+                                        &DataLength);
+        if (Status == STATUS_NO_MORE_ENTRIES)
+        {
+            DPRINT("No more services\n");
+            break;
+        }
+
+        if (!NT_SUCCESS(Status))
+            break;
+
+        DPRINT("Comparing: '%S'  '%wZ'\n", lpService->lpServiceName, &DirInfo->ObjectName);
+
+        if (_wcsicmp(lpService->lpServiceName, DirInfo->ObjectName.Buffer) == 0)
+        {
+            DPRINT1("Found: '%S'  '%wZ'\n",
+                    lpService->lpServiceName, &DirInfo->ObjectName);
+            bFound = TRUE;
+
+            break;
+        }
+    }
+
+    HeapFree(GetProcessHeap(),
+             0,
+             DirInfo);
+    NtClose(DirHandle);
+
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("Status: %lx\n", Status);
+        return RtlNtStatusToDosError(Status);
+    }
+
+    if ((bFound == TRUE) &&
+        (lpService->Status.dwCurrentState != SERVICE_STOP_PENDING))
+    {
+        if (lpService->Status.dwCurrentState == SERVICE_STOPPED)
+        {
+            lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;
+            lpService->Status.dwServiceSpecificExitCode = ERROR_SUCCESS;
+            lpService->Status.dwCheckPoint = 0;
+            lpService->Status.dwWaitHint = 0;
+            lpService->Status.dwControlsAccepted = 0;
+        }
+        else
+        {
+            lpService->Status.dwCurrentState = SERVICE_RUNNING;
+            lpService->Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+
+            if (lpService->Status.dwWin32ExitCode == ERROR_SERVICE_NEVER_STARTED)
+                lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;
+        }
+    }
+    else
+    {
+        lpService->Status.dwCurrentState = SERVICE_STOPPED;
+        lpService->Status.dwControlsAccepted = 0;
+        lpService->Status.dwCheckPoint = 0;
+        lpService->Status.dwWaitHint = 0;
+
+        if (lpService->Status.dwCurrentState == SERVICE_STOP_PENDING)
+            lpService->Status.dwWin32ExitCode = ERROR_SUCCESS;
+        else
+            lpService->Status.dwWin32ExitCode = ERROR_GEN_FAILURE;
+    }
+
+    if (lpServiceStatus != NULL)
+    {
+        memcpy(lpServiceStatus,
+               &lpService->Status,
+               sizeof(SERVICE_STATUS));
+    }
+
+    DPRINT1("ScmGetDriverStatus() done (Error: %lu)\n", dwError);
+
+    return ERROR_SUCCESS;
+}
+
+
+DWORD
+ScmControlDriver(PSERVICE lpService,
+                 DWORD dwControl,
+                 LPSERVICE_STATUS lpServiceStatus)
+{
+    DWORD dwError;
+
+    DPRINT("ScmControlDriver() called\n");
+
+    switch (dwControl)
+    {
+        case SERVICE_CONTROL_STOP:
+            if (lpService->Status.dwCurrentState != SERVICE_RUNNING)
+            {
+                dwError = ERROR_INVALID_SERVICE_CONTROL;
+                goto done;
+            }
+
+            dwError = ScmUnloadDriver(lpService);
+            if (dwError == ERROR_SUCCESS)
+            {
+                lpService->Status.dwControlsAccepted = 0;
+                lpService->Status.dwCurrentState = SERVICE_STOPPED;
+            }
+            break;
+
+        case SERVICE_CONTROL_INTERROGATE:
+            dwError = ScmGetDriverStatus(lpService,
+                                         lpServiceStatus);
+            break;
+
+        default:
+            dwError = ERROR_INVALID_SERVICE_CONTROL;
+    }
+
+done:;
+    DPRINT("ScmControlDriver() done (Erorr: %lu)\n", dwError);
+
+    return dwError;
+}
+
+/* EOF */