1 | /* $Id: USBUninstall.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver uninstallation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/win/windows.h>
|
---|
32 | #include <iprt/win/setupapi.h>
|
---|
33 | #include <newdev.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/errcore.h>
|
---|
41 | #include <VBox/VBoxDrvCfg-win.h>
|
---|
42 | #include <stdio.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | int usblibOsStopService(void);
|
---|
46 | int usblibOsDeleteService(void);
|
---|
47 |
|
---|
48 | static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char *pszMsg, void *pvContext)
|
---|
49 | {
|
---|
50 | RT_NOREF1(pvContext);
|
---|
51 | switch (enmSeverity)
|
---|
52 | {
|
---|
53 | case VBOXDRVCFG_LOG_SEVERITY_FLOW:
|
---|
54 | case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
|
---|
55 | break;
|
---|
56 | case VBOXDRVCFG_LOG_SEVERITY_REL:
|
---|
57 | printf("%s", pszMsg);
|
---|
58 | break;
|
---|
59 | default:
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | static DECLCALLBACK(void) vboxUsbPanic(void *pvPanic)
|
---|
65 | {
|
---|
66 | RT_NOREF1(pvPanic);
|
---|
67 | #ifndef DEBUG_bird
|
---|
68 | AssertFailed();
|
---|
69 | #endif
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | int __cdecl main(int argc, char **argv)
|
---|
74 | {
|
---|
75 | RT_NOREF2(argc, argv);
|
---|
76 | printf("USB uninstallation\n");
|
---|
77 |
|
---|
78 | VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
|
---|
79 | VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
|
---|
80 |
|
---|
81 | usblibOsStopService();
|
---|
82 | usblibOsDeleteService();
|
---|
83 |
|
---|
84 | HRESULT hr = VBoxDrvCfgInfUninstallAllF(L"USB", L"USB\\VID_80EE&PID_CAFE", SUOI_FORCEDELETE);
|
---|
85 | if (hr != S_OK)
|
---|
86 | {
|
---|
87 | printf("SetupUninstallOEMInf failed with hr=0x%x\n", hr);
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | printf("USB uninstallation succeeded!\n");
|
---|
92 |
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** The support service name. */
|
---|
97 | #define SERVICE_NAME "VBoxUSBMon"
|
---|
98 | /** Win32 Device name. */
|
---|
99 | #define DEVICE_NAME "\\\\.\\VBoxUSBMon"
|
---|
100 | /** NT Device name. */
|
---|
101 | #define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
|
---|
102 | /** Win32 Symlink name. */
|
---|
103 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Stops a possibly running service.
|
---|
107 | *
|
---|
108 | * @returns 0 on success.
|
---|
109 | * @returns -1 on failure.
|
---|
110 | */
|
---|
111 | int usblibOsStopService(void)
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Assume it didn't exist, so we'll create the service.
|
---|
115 | */
|
---|
116 | int rc = -1;
|
---|
117 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
118 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
119 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
|
---|
120 | if (hSMgr)
|
---|
121 | {
|
---|
122 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
123 | if (hService)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | * Stop the service.
|
---|
127 | */
|
---|
128 | SERVICE_STATUS Status;
|
---|
129 | QueryServiceStatus(hService, &Status);
|
---|
130 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
131 | rc = 0;
|
---|
132 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
133 | {
|
---|
134 | /*
|
---|
135 | * Wait for finish about 1 minute.
|
---|
136 | * It should be enough for work with driver verifier
|
---|
137 | */
|
---|
138 | int iWait = 600;
|
---|
139 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
140 | {
|
---|
141 | Sleep(100);
|
---|
142 | QueryServiceStatus(hService, &Status);
|
---|
143 | }
|
---|
144 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
145 | rc = 0;
|
---|
146 | else
|
---|
147 | AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
|
---|
148 | }
|
---|
149 | else
|
---|
150 | {
|
---|
151 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
152 | AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
|
---|
153 | }
|
---|
154 | CloseServiceHandle(hService);
|
---|
155 | }
|
---|
156 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
157 | rc = 0;
|
---|
158 | else
|
---|
159 | {
|
---|
160 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
161 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
162 | }
|
---|
163 | CloseServiceHandle(hSMgr);
|
---|
164 | }
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Deletes the service.
|
---|
171 | *
|
---|
172 | * @returns 0 on success.
|
---|
173 | * @returns -1 on failure.
|
---|
174 | */
|
---|
175 | int usblibOsDeleteService(void)
|
---|
176 | {
|
---|
177 | /*
|
---|
178 | * Assume it didn't exist, so we'll create the service.
|
---|
179 | */
|
---|
180 | int rc = -1;
|
---|
181 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
182 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
183 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
|
---|
184 | if (hSMgr)
|
---|
185 | {
|
---|
186 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
|
---|
187 | if (hService)
|
---|
188 | {
|
---|
189 | /*
|
---|
190 | * Delete the service.
|
---|
191 | */
|
---|
192 | if (DeleteService(hService))
|
---|
193 | rc = 0;
|
---|
194 | else
|
---|
195 | {
|
---|
196 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
197 | AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
|
---|
198 | }
|
---|
199 | CloseServiceHandle(hService);
|
---|
200 | }
|
---|
201 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
202 | rc = 0;
|
---|
203 | else
|
---|
204 | {
|
---|
205 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
206 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
207 | }
|
---|
208 | CloseServiceHandle(hSMgr);
|
---|
209 | }
|
---|
210 | return rc;
|
---|
211 | }
|
---|