1 | /* $Id: VBoxGuestInst.cpp 70164 2017-12-15 17:32:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Small tool to (un)install the VBoxGuest device driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 |
|
---|
39 |
|
---|
40 | //#define TESTMODE
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | static int installDriver(void)
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | * Assume it didn't exist, so we'll create the service.
|
---|
48 | */
|
---|
49 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
50 | if (!hSMgrCreate)
|
---|
51 | {
|
---|
52 | printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
|
---|
53 | return -1;
|
---|
54 | }
|
---|
55 |
|
---|
56 | char szDriver[MAX_PATH];
|
---|
57 | GetSystemDirectory(szDriver, sizeof(szDriver));
|
---|
58 | strcat(szDriver, "\\drivers\\VBoxGuest.sys");
|
---|
59 |
|
---|
60 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
61 | VBOXGUEST_SERVICE_NAME,
|
---|
62 | "VBoxGuest Support Driver",
|
---|
63 | SERVICE_QUERY_STATUS,
|
---|
64 | SERVICE_KERNEL_DRIVER,
|
---|
65 | SERVICE_BOOT_START,
|
---|
66 | SERVICE_ERROR_NORMAL,
|
---|
67 | szDriver,
|
---|
68 | "System",
|
---|
69 | NULL, NULL, NULL, NULL);
|
---|
70 | if (!hService)
|
---|
71 | printf("CreateService failed! lasterr=%d\n", GetLastError());
|
---|
72 | else
|
---|
73 | CloseServiceHandle(hService);
|
---|
74 | CloseServiceHandle(hSMgrCreate);
|
---|
75 | return hService ? 0 : -1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static int uninstallDriver(void)
|
---|
79 | {
|
---|
80 | int rc = -1;
|
---|
81 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
82 | if (!hSMgr)
|
---|
83 | {
|
---|
84 | printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 | SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, DELETE);
|
---|
88 | if (hService)
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * Delete the service.
|
---|
92 | */
|
---|
93 | if (DeleteService(hService))
|
---|
94 | rc = 0;
|
---|
95 | else
|
---|
96 | printf("DeleteService failed lasterr=%d\n", GetLastError());
|
---|
97 | CloseServiceHandle(hService);
|
---|
98 | }
|
---|
99 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
100 | rc = 0;
|
---|
101 | else
|
---|
102 | printf("OpenService failed lasterr=%d\n", GetLastError());
|
---|
103 | CloseServiceHandle(hSMgr);
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | #ifdef TESTMODE
|
---|
108 |
|
---|
109 | static HANDLE openDriver(void)
|
---|
110 | {
|
---|
111 | HANDLE hDevice;
|
---|
112 |
|
---|
113 | hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
|
---|
114 | GENERIC_READ | GENERIC_WRITE,
|
---|
115 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
116 | NULL,
|
---|
117 | OPEN_EXISTING,
|
---|
118 | FILE_ATTRIBUTE_NORMAL,
|
---|
119 | NULL);
|
---|
120 | if (hDevice == INVALID_HANDLE_VALUE)
|
---|
121 | {
|
---|
122 | printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
|
---|
123 | }
|
---|
124 | return hDevice;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static int closeDriver(HANDLE hDevice)
|
---|
128 | {
|
---|
129 | CloseHandle(hDevice);
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int performTest(void)
|
---|
134 | {
|
---|
135 | int rc = 0;
|
---|
136 |
|
---|
137 | HANDLE hDevice = openDriver();
|
---|
138 |
|
---|
139 | if (hDevice != INVALID_HANDLE_VALUE)
|
---|
140 | closeDriver(hDevice);
|
---|
141 | else
|
---|
142 | printf("openDriver failed!\n");
|
---|
143 |
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | #endif /* TESTMODE */
|
---|
148 |
|
---|
149 | static int usage(char *programName)
|
---|
150 | {
|
---|
151 | printf("error, syntax: %s [install|uninstall]\n", programName);
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 |
|
---|
155 | int main(int argc, char **argv)
|
---|
156 | {
|
---|
157 | bool installMode;
|
---|
158 | #ifdef TESTMODE
|
---|
159 | bool testMode = false;
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | if (argc != 2)
|
---|
163 | return usage(argv[0]);
|
---|
164 |
|
---|
165 | if (strcmp(argv[1], "install") == 0)
|
---|
166 | installMode = true;
|
---|
167 | else if (strcmp(argv[1], "uninstall") == 0)
|
---|
168 | installMode = false;
|
---|
169 | #ifdef TESTMODE
|
---|
170 | else if (strcmp(argv[1], "test") == 0)
|
---|
171 | testMode = true;
|
---|
172 | #endif
|
---|
173 | else
|
---|
174 | return usage(argv[0]);
|
---|
175 |
|
---|
176 |
|
---|
177 | int rc;
|
---|
178 | #ifdef TESTMODE
|
---|
179 | if (testMode)
|
---|
180 | rc = performTest();
|
---|
181 | else
|
---|
182 | #endif
|
---|
183 | if (installMode)
|
---|
184 | rc = installDriver();
|
---|
185 | else
|
---|
186 | rc = uninstallDriver();
|
---|
187 |
|
---|
188 | if (rc == 0)
|
---|
189 | printf("operation completed successfully!\n");
|
---|
190 | else
|
---|
191 | printf("error: operation failed with status code %d\n", rc);
|
---|
192 |
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|