1 | /* $Id: VBoxGuestInst.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Small tool to (un)install the VBoxGuest device driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/win/windows.h>
|
---|
42 |
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <string.h>
|
---|
46 |
|
---|
47 | #include <VBox/VBoxGuest.h> /* for VBOXGUEST_SERVICE_NAME */
|
---|
48 | #include <iprt/err.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | //#define TESTMODE
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | static int installDriver(bool fStartIt)
|
---|
56 | {
|
---|
57 | /*
|
---|
58 | * Assume it didn't exist, so we'll create the service.
|
---|
59 | */
|
---|
60 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
61 | if (!hSMgrCreate)
|
---|
62 | {
|
---|
63 | printf("OpenSCManager(,,create) failed rc=%lu\n", GetLastError());
|
---|
64 | return -1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | const char *pszSlashName = "\\VBoxGuest.sys";
|
---|
68 | char szDriver[MAX_PATH * 2];
|
---|
69 | GetCurrentDirectory(MAX_PATH, szDriver);
|
---|
70 | strcat(szDriver, pszSlashName);
|
---|
71 | if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
|
---|
72 | {
|
---|
73 | GetSystemDirectory(szDriver, sizeof(szDriver));
|
---|
74 | strcat(strcat(szDriver, "\\drivers"), pszSlashName);
|
---|
75 |
|
---|
76 | /* Try FAT name abbreviation. */
|
---|
77 | if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
|
---|
78 | {
|
---|
79 | pszSlashName = "\\VBoxGst.sys";
|
---|
80 | GetCurrentDirectory(MAX_PATH, szDriver);
|
---|
81 | strcat(szDriver, pszSlashName);
|
---|
82 | if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
|
---|
83 | {
|
---|
84 | GetSystemDirectory(szDriver, sizeof(szDriver));
|
---|
85 | strcat(strcat(szDriver, "\\drivers"), pszSlashName);
|
---|
86 |
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
92 | VBOXGUEST_SERVICE_NAME,
|
---|
93 | "VBoxGuest Support Driver",
|
---|
94 | SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
|
---|
95 | SERVICE_KERNEL_DRIVER,
|
---|
96 | SERVICE_BOOT_START,
|
---|
97 | SERVICE_ERROR_NORMAL,
|
---|
98 | szDriver,
|
---|
99 | "System",
|
---|
100 | NULL, NULL, NULL, NULL);
|
---|
101 | if (hService)
|
---|
102 | {
|
---|
103 | printf("Successfully created service '%s' for driver '%s'.\n", VBOXGUEST_SERVICE_NAME, szDriver);
|
---|
104 | if (fStartIt)
|
---|
105 | {
|
---|
106 | if (StartService(hService, 0, NULL))
|
---|
107 | printf("successfully started driver '%s'\n", szDriver);
|
---|
108 | else
|
---|
109 | printf("StartService failed: %lu\n", GetLastError());
|
---|
110 | }
|
---|
111 | CloseServiceHandle(hService);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | printf("CreateService failed! lasterr=%lu (szDriver=%s)\n", GetLastError(), szDriver);
|
---|
115 | CloseServiceHandle(hSMgrCreate);
|
---|
116 | return hService ? 0 : -1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int uninstallDriver(void)
|
---|
120 | {
|
---|
121 | int rc = -1;
|
---|
122 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
123 | if (!hSMgr)
|
---|
124 | {
|
---|
125 | printf("OpenSCManager(,,delete) failed rc=%lu\n", GetLastError());
|
---|
126 | return -1;
|
---|
127 | }
|
---|
128 | SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
|
---|
129 | if (hService)
|
---|
130 | {
|
---|
131 | /*
|
---|
132 | * Try stop it if it's running.
|
---|
133 | */
|
---|
134 | SERVICE_STATUS Status = { 0, 0, 0, 0, 0, 0, 0 };
|
---|
135 | QueryServiceStatus(hService, &Status);
|
---|
136 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
137 | rc = VINF_SUCCESS;
|
---|
138 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
139 | {
|
---|
140 | int iWait = 100;
|
---|
141 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
142 | {
|
---|
143 | Sleep(100);
|
---|
144 | QueryServiceStatus(hService, &Status);
|
---|
145 | }
|
---|
146 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
147 | rc = VINF_SUCCESS;
|
---|
148 | else
|
---|
149 | {
|
---|
150 | printf("Failed to stop service. status=%ld (%#lx)\n", Status.dwCurrentState, Status.dwCurrentState);
|
---|
151 | rc = VERR_GENERAL_FAILURE;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | DWORD dwErr = GetLastError();
|
---|
157 | if ( Status.dwCurrentState == SERVICE_STOP_PENDING
|
---|
158 | && dwErr == ERROR_SERVICE_CANNOT_ACCEPT_CTRL)
|
---|
159 | rc = VERR_RESOURCE_BUSY; /* better than VERR_GENERAL_FAILURE */
|
---|
160 | else
|
---|
161 | {
|
---|
162 | printf("ControlService failed with dwErr=%ld. status=%lu (%#lx)\n",
|
---|
163 | dwErr, Status.dwCurrentState, Status.dwCurrentState);
|
---|
164 | rc = -1;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Delete the service.
|
---|
170 | */
|
---|
171 | if (RT_SUCCESS(rc))
|
---|
172 | {
|
---|
173 | if (DeleteService(hService))
|
---|
174 | rc = 0;
|
---|
175 | else
|
---|
176 | {
|
---|
177 | printf("DeleteService failed lasterr=%lu\n", GetLastError());
|
---|
178 | rc = -1;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | CloseServiceHandle(hService);
|
---|
182 | }
|
---|
183 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
184 | rc = 0;
|
---|
185 | else
|
---|
186 | printf("OpenService failed lasterr=%lu\n", GetLastError());
|
---|
187 | CloseServiceHandle(hSMgr);
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 | #ifdef TESTMODE
|
---|
192 |
|
---|
193 | static HANDLE openDriver(void)
|
---|
194 | {
|
---|
195 | HANDLE hDevice;
|
---|
196 |
|
---|
197 | hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
|
---|
198 | GENERIC_READ | GENERIC_WRITE,
|
---|
199 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
200 | NULL,
|
---|
201 | OPEN_EXISTING,
|
---|
202 | FILE_ATTRIBUTE_NORMAL,
|
---|
203 | NULL);
|
---|
204 | if (hDevice == INVALID_HANDLE_VALUE)
|
---|
205 | {
|
---|
206 | printf("CreateFile did not work. GetLastError() %lu\n", GetLastError());
|
---|
207 | }
|
---|
208 | return hDevice;
|
---|
209 | }
|
---|
210 |
|
---|
211 | static int closeDriver(HANDLE hDevice)
|
---|
212 | {
|
---|
213 | CloseHandle(hDevice);
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static int performTest(void)
|
---|
218 | {
|
---|
219 | int rc = 0;
|
---|
220 |
|
---|
221 | HANDLE hDevice = openDriver();
|
---|
222 |
|
---|
223 | if (hDevice != INVALID_HANDLE_VALUE)
|
---|
224 | closeDriver(hDevice);
|
---|
225 | else
|
---|
226 | printf("openDriver failed!\n");
|
---|
227 |
|
---|
228 | return rc;
|
---|
229 | }
|
---|
230 |
|
---|
231 | #endif /* TESTMODE */
|
---|
232 |
|
---|
233 | static int usage(char *programName)
|
---|
234 | {
|
---|
235 | printf("error, syntax: %s [install|uninstall]\n", programName);
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | int main(int argc, char **argv)
|
---|
240 | {
|
---|
241 | bool installMode;
|
---|
242 | #ifdef TESTMODE
|
---|
243 | bool testMode = false;
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | if (argc != 2)
|
---|
247 | return usage(argv[0]);
|
---|
248 |
|
---|
249 | if (strcmp(argv[1], "install") == 0)
|
---|
250 | installMode = true;
|
---|
251 | else if (strcmp(argv[1], "uninstall") == 0)
|
---|
252 | installMode = false;
|
---|
253 | #ifdef TESTMODE
|
---|
254 | else if (strcmp(argv[1], "test") == 0)
|
---|
255 | testMode = true;
|
---|
256 | #endif
|
---|
257 | else
|
---|
258 | return usage(argv[0]);
|
---|
259 |
|
---|
260 |
|
---|
261 | int rc;
|
---|
262 | #ifdef TESTMODE
|
---|
263 | if (testMode)
|
---|
264 | rc = performTest();
|
---|
265 | else
|
---|
266 | #endif
|
---|
267 | if (installMode)
|
---|
268 | rc = installDriver(true);
|
---|
269 | else
|
---|
270 | rc = uninstallDriver();
|
---|
271 |
|
---|
272 | if (rc == 0)
|
---|
273 | printf("operation completed successfully!\n");
|
---|
274 | else
|
---|
275 | printf("error: operation failed with status code %d\n", rc);
|
---|
276 |
|
---|
277 | return rc;
|
---|
278 | }
|
---|
279 |
|
---|