VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/win/VBoxGuestInst.cpp@ 78392

Last change on this file since 78392 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: VBoxGuestInst.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * Small tool to (un)install the VBoxGuest device driver.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include <VBox/VBoxGuest.h> /* for VBOXGUEST_SERVICE_NAME */
38#include <iprt/err.h>
39
40
41//#define TESTMODE
42
43
44
45static int installDriver(bool fStartIt)
46{
47 /*
48 * Assume it didn't exist, so we'll create the service.
49 */
50 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
51 if (!hSMgrCreate)
52 {
53 printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
54 return -1;
55 }
56
57 const char *pszSlashName = "\\VBoxGuest.sys";
58 char szDriver[MAX_PATH * 2];
59 GetCurrentDirectory(MAX_PATH, szDriver);
60 strcat(szDriver, pszSlashName);
61 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
62 {
63 GetSystemDirectory(szDriver, sizeof(szDriver));
64 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
65
66 /* Try FAT name abbreviation. */
67 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
68 {
69 pszSlashName = "\\VBoxGst.sys";
70 GetCurrentDirectory(MAX_PATH, szDriver);
71 strcat(szDriver, pszSlashName);
72 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
73 {
74 GetSystemDirectory(szDriver, sizeof(szDriver));
75 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
76
77 }
78 }
79 }
80
81 SC_HANDLE hService = CreateService(hSMgrCreate,
82 VBOXGUEST_SERVICE_NAME,
83 "VBoxGuest Support Driver",
84 SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
85 SERVICE_KERNEL_DRIVER,
86 SERVICE_BOOT_START,
87 SERVICE_ERROR_NORMAL,
88 szDriver,
89 "System",
90 NULL, NULL, NULL, NULL);
91 if (hService)
92 {
93 printf("Successfully created service '%s' for driver '%s'.\n", VBOXGUEST_SERVICE_NAME, szDriver);
94 if (fStartIt)
95 {
96 if (StartService(hService, 0, NULL))
97 printf("successfully started driver '%s'\n", szDriver);
98 else
99 printf("StartService failed: %d\n", GetLastError());
100 }
101 CloseServiceHandle(hService);
102 }
103 else
104 printf("CreateService failed! lasterr=%d (szDriver=%s)\n", GetLastError(), szDriver);
105 CloseServiceHandle(hSMgrCreate);
106 return hService ? 0 : -1;
107}
108
109static int uninstallDriver(void)
110{
111 int rc = -1;
112 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
113 if (!hSMgr)
114 {
115 printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
116 return -1;
117 }
118 SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
119 if (hService)
120 {
121 /*
122 * Try stop it if it's running.
123 */
124 SERVICE_STATUS Status = { 0, 0, 0, 0, 0, 0, 0 };
125 QueryServiceStatus(hService, &Status);
126 if (Status.dwCurrentState == SERVICE_STOPPED)
127 rc = VINF_SUCCESS;
128 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
129 {
130 int iWait = 100;
131 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
132 {
133 Sleep(100);
134 QueryServiceStatus(hService, &Status);
135 }
136 if (Status.dwCurrentState == SERVICE_STOPPED)
137 rc = VINF_SUCCESS;
138 else
139 {
140 printf("Failed to stop service. status=%d (%#x)\n", Status.dwCurrentState, Status.dwCurrentState);
141 rc = VERR_GENERAL_FAILURE;
142 }
143 }
144 else
145 {
146 DWORD dwErr = GetLastError();
147 if ( Status.dwCurrentState == SERVICE_STOP_PENDING
148 && dwErr == ERROR_SERVICE_CANNOT_ACCEPT_CTRL)
149 rc = VERR_RESOURCE_BUSY; /* better than VERR_GENERAL_FAILURE */
150 else
151 {
152 printf("ControlService failed with dwErr=%u. status=%d (%#x)\n",
153 dwErr, Status.dwCurrentState, Status.dwCurrentState);
154 rc = -1;
155 }
156 }
157
158 /*
159 * Delete the service.
160 */
161 if (RT_SUCCESS(rc))
162 {
163 if (DeleteService(hService))
164 rc = 0;
165 else
166 {
167 printf("DeleteService failed lasterr=%d\n", GetLastError());
168 rc = -1;
169 }
170 }
171 CloseServiceHandle(hService);
172 }
173 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
174 rc = 0;
175 else
176 printf("OpenService failed lasterr=%d\n", GetLastError());
177 CloseServiceHandle(hSMgr);
178 return rc;
179}
180
181#ifdef TESTMODE
182
183static HANDLE openDriver(void)
184{
185 HANDLE hDevice;
186
187 hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
188 GENERIC_READ | GENERIC_WRITE,
189 FILE_SHARE_READ | FILE_SHARE_WRITE,
190 NULL,
191 OPEN_EXISTING,
192 FILE_ATTRIBUTE_NORMAL,
193 NULL);
194 if (hDevice == INVALID_HANDLE_VALUE)
195 {
196 printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
197 }
198 return hDevice;
199}
200
201static int closeDriver(HANDLE hDevice)
202{
203 CloseHandle(hDevice);
204 return 0;
205}
206
207static int performTest(void)
208{
209 int rc = 0;
210
211 HANDLE hDevice = openDriver();
212
213 if (hDevice != INVALID_HANDLE_VALUE)
214 closeDriver(hDevice);
215 else
216 printf("openDriver failed!\n");
217
218 return rc;
219}
220
221#endif /* TESTMODE */
222
223static int usage(char *programName)
224{
225 printf("error, syntax: %s [install|uninstall]\n", programName);
226 return 1;
227}
228
229int main(int argc, char **argv)
230{
231 bool installMode;
232#ifdef TESTMODE
233 bool testMode = false;
234#endif
235
236 if (argc != 2)
237 return usage(argv[0]);
238
239 if (strcmp(argv[1], "install") == 0)
240 installMode = true;
241 else if (strcmp(argv[1], "uninstall") == 0)
242 installMode = false;
243#ifdef TESTMODE
244 else if (strcmp(argv[1], "test") == 0)
245 testMode = true;
246#endif
247 else
248 return usage(argv[0]);
249
250
251 int rc;
252#ifdef TESTMODE
253 if (testMode)
254 rc = performTest();
255 else
256#endif
257 if (installMode)
258 rc = installDriver(true);
259 else
260 rc = uninstallDriver();
261
262 if (rc == 0)
263 printf("operation completed successfully!\n");
264 else
265 printf("error: operation failed with status code %d\n", rc);
266
267 return rc;
268}
269
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette