VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/display-drm.cpp@ 84593

Last change on this file since 84593 was 84593, checked in by vboxsync, 5 years ago

bugref:9637. dont lose effective user id

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: display-drm.cpp 84593 2020-05-28 14:33:35Z vboxsync $ */
2/** @file
3 * X11 guest client - VMSVGA emulation resize event pass-through to drm guest
4 * driver.
5 */
6
7/*
8 * Copyright (C) 2016-2020 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.215389.xyz. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/*
20 * Known things to test when changing this code. All assume a guest with VMSVGA
21 * active and controlled by X11 or Wayland, and Guest Additions installed and
22 * running, unless otherwise stated.
23 * - On Linux 4.6 and later guests, VBoxClient --vmsvga should be running as
24 * root and not as the logged-in user. Dynamic resizing should work for all
25 * screens in any environment which handles kernel resize notifications,
26 * including at log-in screens. Test GNOME Shell Wayland and GNOME Shell
27 * under X.Org or Unity or KDE at the log-in screen and after log-in.
28 * - Linux 4.10 changed the user-kernel-ABI introduced in 4.6: test both.
29 * - On other guests (than Linux 4.6 or later) running X.Org Server 1.3 or
30 * later, VBoxClient --vmsvga should never be running as root, and should run
31 * (and dynamic resizing and screen enable/disable should work for all
32 * screens) whenever a user is logged in to a supported desktop environment.
33 * - On guests running X.Org Server 1.2 or older, VBoxClient --vmsvga should
34 * never run as root and should run whenever a user is logged in to a
35 * supported desktop environment. Dynamic resizing should work for the first
36 * screen, and enabling others should not be possible.
37 * - When VMSVGA is not enabled, VBoxClient --vmsvga should never stay running.
38 */
39
40#include "VBoxClient.h"
41
42#include <VBox/VBoxGuestLib.h>
43
44#include <iprt/assert.h>
45#include <iprt/file.h>
46#include <iprt/err.h>
47#include <iprt/string.h>
48#include <iprt/initterm.h>
49#include <iprt/message.h>
50#include <unistd.h>
51#include <stdio.h>
52
53
54/** Maximum number of supported screens. DRM and X11 both limit this to 32. */
55/** @todo if this ever changes, dynamically allocate resizeable arrays in the
56 * context structure. */
57#define VMW_MAX_HEADS 32
58
59/* VMWare kernel driver control parts definitions. */
60
61#ifdef RT_OS_LINUX
62# include <sys/ioctl.h>
63#else /* Solaris and BSDs, in case they ever adopt the DRM driver. */
64# include <sys/ioccom.h>
65#endif
66
67#define DRM_DRIVER_NAME "vmwgfx"
68
69/** Counter of how often our daemon has been respawned. */
70unsigned g_cRespawn = 0;
71/** Logging verbosity level. */
72unsigned g_cVerbosity = 0;
73
74/** DRM version structure. */
75struct DRMVERSION
76{
77 int cMajor;
78 int cMinor;
79 int cPatchLevel;
80 size_t cbName;
81 char *pszName;
82 size_t cbDate;
83 char *pszDate;
84 size_t cbDescription;
85 char *pszDescription;
86};
87AssertCompileSize(struct DRMVERSION, 8 + 7 * sizeof(void *));
88
89/** Rectangle structure for geometry of a single screen. */
90struct DRMVMWRECT
91{
92 int32_t x;
93 int32_t y;
94 uint32_t w;
95 uint32_t h;
96};
97AssertCompileSize(struct DRMVMWRECT, 16);
98
99#define DRM_IOCTL_VERSION _IOWR('d', 0x00, struct DRMVERSION)
100
101struct DRMCONTEXT
102{
103 RTFILE hDevice;
104};
105
106static void drmConnect(struct DRMCONTEXT *pContext)
107{
108 uid_t guid = getuid();
109 if (setreuid(0, 0) == -1)
110 {
111 perror("setuid failed drm device open.");
112 }
113 unsigned i;
114 RTFILE hDevice;
115
116 if (pContext->hDevice != NIL_RTFILE)
117 VBClLogFatalError("%s called with bad argument\n", __func__);
118 /* Try to open the SVGA DRM device. */
119 for (i = 0; i < 128; ++i)
120 {
121 char szPath[64];
122 struct DRMVERSION version;
123 char szName[sizeof(DRM_DRIVER_NAME)];
124 int rc;
125
126 /* Control devices for drm graphics driver control devices go from
127 * controlD64 to controlD127. Render node devices go from renderD128
128 * to renderD192. The driver takes resize hints via the control device
129 * on pre-4.10 kernels and on the render device on newer ones. Try
130 * both types. */
131 if (i % 2 == 0)
132 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/renderD%u", i / 2 + 128);
133 else
134 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/controlD%u", i / 2 + 64);
135 if (RT_FAILURE(rc))
136 VBClLogFatalError("RTStrPrintf of device path failed, rc=%Rrc\n", rc);
137 rc = RTFileOpen(&hDevice, szPath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
138 if (RT_FAILURE(rc))
139 continue;
140 RT_ZERO(version);
141 version.cbName = sizeof(szName);
142 version.pszName = szName;
143 rc = RTFileIoCtl(hDevice, DRM_IOCTL_VERSION, &version, sizeof(version), NULL);
144 if ( RT_SUCCESS(rc)
145 && !strncmp(szName, DRM_DRIVER_NAME, sizeof(DRM_DRIVER_NAME) - 1)
146 && ( version.cMajor > 2
147 || (version.cMajor == 2 && version.cMinor > 9)))
148 break;
149 hDevice = NIL_RTFILE;
150 }
151 pContext->hDevice = hDevice;
152 setreuid(guid, 0);
153}
154
155/** Preferred screen layout information for DRM_VMW_UPDATE_LAYOUT IoCtl. The
156 * rects argument is a cast pointer to an array of drm_vmw_rect. */
157struct DRMVMWUPDATELAYOUT {
158 uint32_t cOutputs;
159 uint32_t u32Pad;
160 uint64_t ptrRects;
161};
162AssertCompileSize(struct DRMVMWUPDATELAYOUT, 16);
163
164#define DRM_IOCTL_VMW_UPDATE_LAYOUT \
165 _IOW('d', 0x40 + 20, struct DRMVMWUPDATELAYOUT)
166
167static void drmSendHints(struct DRMCONTEXT *pContext, struct DRMVMWRECT *paRects,
168 unsigned cHeads)
169{
170 uid_t guid = getuid();
171 if (setreuid(0, 0) == -1)
172 {
173 perror("setuid failed drm device open.");
174 }
175
176 int rc;
177 struct DRMVMWUPDATELAYOUT ioctlLayout;
178
179 if (pContext->hDevice == NIL_RTFILE)
180 VBClLogFatalError("%s bad device argument\n", __func__);
181 ioctlLayout.cOutputs = cHeads;
182 ioctlLayout.ptrRects = (uint64_t)paRects;
183 rc = RTFileIoCtl(pContext->hDevice, DRM_IOCTL_VMW_UPDATE_LAYOUT,
184 &ioctlLayout, sizeof(ioctlLayout), NULL);
185 if (RT_FAILURE(rc) && rc != VERR_INVALID_PARAMETER)
186 VBClLogFatalError("Failure updating layout, rc=%Rrc\n", rc);
187 setreuid(guid, 0);
188}
189
190int main(int argc, char *argv[])
191{
192 int rc = RTR3InitExe(argc, &argv, 0);
193 if (RT_FAILURE(rc))
194 return RTMsgInitFailure(rc);
195 rc = VbglR3InitUser();
196 if (RT_FAILURE(rc))
197 VBClLogFatalError("VbglR3InitUser failed: %Rrc", rc);
198
199 struct DRMCONTEXT drmContext = { NIL_RTFILE };
200 static struct VMMDevDisplayDef aMonitors[VMW_MAX_HEADS];
201
202 unsigned cEnabledMonitors;
203 /* Do not acknowledge the first event we query for to pick up old events,
204 * e.g. from before a guest reboot. */
205 bool fAck = false;
206 drmConnect(&drmContext);
207 if (drmContext.hDevice == NIL_RTFILE)
208 return VERR_OPEN_FAILED;
209 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
210 if (RT_FAILURE(rc))
211 {
212 VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
213 return VERR_INVALID_HANDLE;
214 }
215 rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
216 if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
217 {
218 return VERR_RESOURCE_BUSY;
219 }
220 if (RT_FAILURE(rc))
221 {
222 VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
223 return VERR_INVALID_HANDLE;
224 }
225
226 for (;;)
227 {
228 uint32_t events;
229 struct VMMDevDisplayDef aDisplays[VMW_MAX_HEADS];
230 uint32_t cDisplaysOut;
231 /* Query the first size without waiting. This lets us e.g. pick up
232 * the last event before a guest reboot when we start again after. */
233 rc = VbglR3GetDisplayChangeRequestMulti(VMW_MAX_HEADS, &cDisplaysOut, aDisplays, fAck);
234 fAck = true;
235 if (RT_FAILURE(rc))
236 VBClLogFatalError("Failed to get display change request, rc=%Rrc\n", rc);
237 if (cDisplaysOut > VMW_MAX_HEADS)
238 VBClLogFatalError("Display change request contained, rc=%Rrc\n", rc);
239 if (cDisplaysOut > 0)
240 {
241 for (unsigned i = 0; i < cDisplaysOut && i < VMW_MAX_HEADS; ++i)
242 {
243 uint32_t idDisplay = aDisplays[i].idDisplay;
244 if (idDisplay >= VMW_MAX_HEADS)
245 continue;
246 aMonitors[idDisplay].fDisplayFlags = aDisplays[i].fDisplayFlags;
247 if (!(aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
248 {
249 if ((idDisplay == 0) || (aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_ORIGIN))
250 {
251 aMonitors[idDisplay].xOrigin = aDisplays[i].xOrigin;
252 aMonitors[idDisplay].yOrigin = aDisplays[i].yOrigin;
253 } else {
254 aMonitors[idDisplay].xOrigin = aMonitors[idDisplay - 1].xOrigin + aMonitors[idDisplay - 1].cx;
255 aMonitors[idDisplay].yOrigin = aMonitors[idDisplay - 1].yOrigin;
256 }
257 aMonitors[idDisplay].cx = aDisplays[i].cx;
258 aMonitors[idDisplay].cy = aDisplays[i].cy;
259 }
260 }
261 /* Create an dense (consisting of enabled monitors only) array to pass to DRM. */
262 cEnabledMonitors = 0;
263 struct DRMVMWRECT aEnabledMonitors[VMW_MAX_HEADS];
264 for (int j = 0; j < VMW_MAX_HEADS; ++j)
265 {
266 if (!(aMonitors[j].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
267 {
268 aEnabledMonitors[cEnabledMonitors].x = aMonitors[j].xOrigin;
269 aEnabledMonitors[cEnabledMonitors].y = aMonitors[j].yOrigin;
270 aEnabledMonitors[cEnabledMonitors].w = aMonitors[j].cx;
271 aEnabledMonitors[cEnabledMonitors].h = aMonitors[j].cy;
272 if (cEnabledMonitors > 0)
273 aEnabledMonitors[cEnabledMonitors].x = aEnabledMonitors[cEnabledMonitors - 1].x + aEnabledMonitors[cEnabledMonitors - 1].w;
274 ++cEnabledMonitors;
275 }
276 }
277 for (unsigned i = 0; i < cEnabledMonitors; ++i)
278 printf("Monitor %u: %dx%d, (%d, %d)\n", i, (int)aEnabledMonitors[i].w, (int)aEnabledMonitors[i].h,
279 (int)aEnabledMonitors[i].x, (int)aEnabledMonitors[i].y);
280 drmSendHints(&drmContext, aEnabledMonitors, cEnabledMonitors);
281 }
282 do
283 {
284 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
285 } while (rc == VERR_INTERRUPTED);
286 if (RT_FAILURE(rc))
287 VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
288 }
289 return 0;
290}
Note: See TracBrowser for help on using the repository browser.

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