summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/x11/VBoxClient/display-svga-session.cpp
blob: 168adb9b2b7af4feae6ec647071b445542d17e4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* $Id: display-svga-session.cpp $ */
/** @file
 * Guest Additions - VMSVGA Desktop Environment user session assistant.
 *
 * This service connects to VBoxDRMClient IPC server, listens for
 * its commands and reports current display offsets to it. If IPC
 * server is not available, it forks legacy 'VBoxClient --vmsvga
 * service and terminates.
 */

/*
 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/*
 * This service is an IPC client for VBoxDRMClient daemon. It is also
 * a proxy bridge to a Desktop Environment specific code (so called
 * Desktop Environment helpers).
 *
 * Once started, it will try to enumerate and probe all the registered
 * helpers and if appropriate helper found, it will forward incoming IPC
 * commands to it as well as send helper's commands back to VBoxDRMClient.
 * Generic helper is a special one. It will be used by default if all the
 * other helpers are failed on probe. Moreover, generic helper provides
 * helper functions that can be used by other helpers as well. For example,
 * once Gnome3 Desktop Environment is running on X11, it will be also use
 * display offsets change notification monitor of a generic helper.
 *
 * Multiple instances of this daemon are allowed to run in parallel
 * with the following limitations.
 * A single user cannot run multiple daemon instances per single TTY device,
 * however, multiple instances are allowed for the user on different
 * TTY devices (i.e. in case if user runs multiple X servers on different
 * terminals). On multiple TTY devices multiple users can run multiple
 * daemon instances (i.e. in case of "switch user" DE configuration when
 * multiple X/Wayland servers are running on separate TTY devices).
 */

#include "VBoxClient.h"
#include "display-ipc.h"
#include "display-helper.h"

#include <VBox/VBoxGuestLib.h>

#include <iprt/localipc.h>
#include <iprt/asm.h>
#include <iprt/errcore.h>
#include <iprt/path.h>
#include <iprt/linux/sysfs.h>


/** Handle to IPC client connection. */
VBOX_DRMIPC_CLIENT g_hClient = VBOX_DRMIPC_CLIENT_INITIALIZER;

/** IPC client handle critical section. */
static RTCRITSECT g_hClientCritSect;

/** List of available Desktop Environment specific display helpers. */
static const VBCLDISPLAYHELPER *g_apDisplayHelpers[] =
{
    &g_DisplayHelperGnome3,  /* GNOME3 helper. */
    &g_DisplayHelperGeneric, /* Generic helper. */
    NULL,                    /* Terminate list. */
};

/** Selected Desktop Environment specific display helper. */
static const VBCLDISPLAYHELPER *g_pDisplayHelper = NULL;

/** IPC connection session handle. */
static RTLOCALIPCSESSION g_hSession = 0;

/**
 * Callback for display offsets change events provided by Desktop Environment specific display helper.
 *
 * @returns IPRT status code.
 * @param   cDisplays   Number of displays which have changed offset.
 * @param   aDisplays   Display data.
 */
static DECLCALLBACK(int) vbclSVGASessionDisplayOffsetChanged(uint32_t cDisplays, struct VBOX_DRMIPC_VMWRECT *aDisplays)
{
    int rc = RTCritSectEnter(&g_hClientCritSect);

    if (RT_SUCCESS(rc))
    {
        rc = vbDrmIpcReportDisplayOffsets(&g_hClient, cDisplays, aDisplays);
        int rc2 = RTCritSectLeave(&g_hClientCritSect);
        if (RT_FAILURE(rc2))
            VBClLogError("vbclSVGASessionDisplayOffsetChanged: unable to leave critical session, rc=%Rrc\n", rc2);
    }
    else
        VBClLogError("vbclSVGASessionDisplayOffsetChanged: unable to enter critical session, rc=%Rrc\n", rc);

    return rc;
}

/**
 * @interface_method_impl{VBCLSERVICE,pfnInit}
 */
static DECLCALLBACK(int) vbclSVGASessionInit(void)
{
    int rc;
    RTLOCALIPCSESSION hSession;
    int idxDisplayHelper = 0;

    /** Custom log prefix to be used for logger instance of this process. */
    static const char *pszLogPrefix = "VBoxClient VMSVGA:";

    VBClLogSetLogPrefix(pszLogPrefix);

    rc = RTCritSectInit(&g_hClientCritSect);
    if (RT_FAILURE(rc))
    {
        VBClLogError("unable to init locking, rc=%Rrc\n", rc);
        return rc;
    }

    /* Go through list of available Desktop Environment specific helpers and try to pick up one. */
    while (g_apDisplayHelpers[idxDisplayHelper])
    {
        if (g_apDisplayHelpers[idxDisplayHelper]->pfnProbe)
        {
            VBClLogInfo("probing Desktop Environment helper '%s'\n",
                         g_apDisplayHelpers[idxDisplayHelper]->pszName);

            rc = g_apDisplayHelpers[idxDisplayHelper]->pfnProbe();

            /* Found compatible helper. */
            if (RT_SUCCESS(rc))
            {
                /* Initialize it. */
                if (g_apDisplayHelpers[idxDisplayHelper]->pfnInit)
                {
                    rc = g_apDisplayHelpers[idxDisplayHelper]->pfnInit();
                }

                /* Some helpers might have no .pfnInit(), that's ok. */
                if (RT_SUCCESS(rc))
                {
                    /* Subscribe to display offsets change event. */
                    if (g_apDisplayHelpers[idxDisplayHelper]->pfnSubscribeDisplayOffsetChangeNotification)
                    {
                        g_apDisplayHelpers[idxDisplayHelper]->
                            pfnSubscribeDisplayOffsetChangeNotification(
                                vbclSVGASessionDisplayOffsetChanged);
                    }

                    g_pDisplayHelper = g_apDisplayHelpers[idxDisplayHelper];
                    break;
                }
                else
                    VBClLogError("compatible Desktop Environment "
                                 "helper has been found, but it cannot be initialized, rc=%Rrc\n", rc);
            }
        }

        idxDisplayHelper++;
    }

    /* Make sure we found compatible Desktop Environment specific helper. */
    if (g_pDisplayHelper)
    {
        VBClLogInfo("using Desktop Environment specific display helper '%s'\n",
                    g_pDisplayHelper->pszName);
    }
    else
    {
        VBClLogError("unable to find Desktop Environment specific display helper\n");
        return VERR_NOT_IMPLEMENTED;
    }

    /* Attempt to connect to VBoxDRMClient IPC server. */
    rc = RTLocalIpcSessionConnect(&hSession, VBOX_DRMIPC_SERVER_NAME, 0);
    if (RT_SUCCESS(rc))
    {
        g_hSession = hSession;
    }
    else
        VBClLogError("unable to connect to IPC server, rc=%Rrc\n", rc);

    /* We cannot initialize ourselves, start legacy service and terminate. */
    if (RT_FAILURE(rc))
    {
        /* Free helper resources. */
        if (g_pDisplayHelper->pfnUnsubscribeDisplayOffsetChangeNotification)
            g_pDisplayHelper->pfnUnsubscribeDisplayOffsetChangeNotification();

        if (g_pDisplayHelper->pfnTerm)
        {
            rc = g_pDisplayHelper->pfnTerm();
            VBClLogInfo("helper service terminated, rc=%Rrc\n", rc);
        }

        rc = VbglR3DrmLegacyClientStart();
        VBClLogInfo("starting legacy service, rc=%Rrc\n", rc);
        /* Force return status, so parent thread wont be trying to start worker thread. */
        rc = VERR_NOT_AVAILABLE;
    }

    return rc;
}

/**
 * A callback function which is triggered on IPC data receive.
 *
 * @returns IPRT status code.
 * @param   idCmd   DRM IPC command ID.
 * @param   pvData  DRM IPC command payload.
 * @param   cbData  Size of DRM IPC command payload.
 */
static DECLCALLBACK(int) vbclSVGASessionRxCallBack(uint8_t idCmd, void *pvData, uint32_t cbData)
{
    VBOXDRMIPCCLTCMD enmCmd =
        (idCmd > VBOXDRMIPCCLTCMD_INVALID && idCmd < VBOXDRMIPCCLTCMD_MAX) ?
            (VBOXDRMIPCCLTCMD)idCmd : VBOXDRMIPCCLTCMD_INVALID;

    int rc = VERR_INVALID_PARAMETER;

    AssertReturn(pvData,            VERR_INVALID_PARAMETER);
    AssertReturn(cbData,            VERR_INVALID_PARAMETER);
    AssertReturn(g_pDisplayHelper,  VERR_INVALID_PARAMETER);

    switch (enmCmd)
    {
        case VBOXDRMIPCCLTCMD_SET_PRIMARY_DISPLAY:
        {
            if (g_pDisplayHelper->pfnSetPrimaryDisplay)
            {
                PVBOX_DRMIPC_COMMAND_SET_PRIMARY_DISPLAY pCmd = (PVBOX_DRMIPC_COMMAND_SET_PRIMARY_DISPLAY)pvData;
                static uint32_t idPrimaryDisplayCached = VBOX_DRMIPC_MONITORS_MAX;

                if (   pCmd->idDisplay < VBOX_DRMIPC_MONITORS_MAX
                    && idPrimaryDisplayCached != pCmd->idDisplay)
                {
                    rc = g_pDisplayHelper->pfnSetPrimaryDisplay(pCmd->idDisplay);
                    /* Update cache. */
                    idPrimaryDisplayCached = pCmd->idDisplay;
                }
                else
                    VBClLogVerbose(1, "do not set %u as a primary display\n", pCmd->idDisplay);
            }

            break;
        }
        default:
        {
            VBClLogError("received unknown IPC command 0x%x\n", idCmd);
            break;
        }
    }

    return rc;
}

/**
 * Reconnect to DRM IPC server.
 */
static int vbclSVGASessionReconnect(void)
{
    int rc = VERR_GENERAL_FAILURE;

    rc = RTCritSectEnter(&g_hClientCritSect);
    if (RT_FAILURE(rc))
    {
        VBClLogError("unable to enter critical section on reconnect, rc=%Rrc\n", rc);
        return rc;
    }

    /* Check if session was not closed before. */
    if (RT_VALID_PTR(g_hSession))
    {
        rc = RTLocalIpcSessionClose(g_hSession);
        if (RT_FAILURE(rc))
            VBClLogError("unable to release IPC connection on reconnect, rc=%Rrc\n", rc);

        rc = vbDrmIpcClientReleaseResources(&g_hClient);
        if (RT_FAILURE(rc))
            VBClLogError("unable to release IPC session resources, rc=%Rrc\n", rc);
    }

    rc = RTLocalIpcSessionConnect(&g_hSession, VBOX_DRMIPC_SERVER_NAME, 0);
    if (RT_SUCCESS(rc))
    {
        rc = vbDrmIpcClientInit(&g_hClient, RTThreadSelf(), g_hSession, VBOX_DRMIPC_TX_QUEUE_SIZE, vbclSVGASessionRxCallBack);
        if (RT_FAILURE(rc))
            VBClLogError("unable to re-initialize IPC session, rc=%Rrc\n", rc);
    }
    else
        VBClLogError("unable to reconnect to IPC server, rc=%Rrc\n", rc);

    int rc2 = RTCritSectLeave(&g_hClientCritSect);
    if (RT_FAILURE(rc2))
        VBClLogError("unable to leave critical section on reconnect, rc=%Rrc\n", rc);

    return rc;
}

/**
 * @interface_method_impl{VBCLSERVICE,pfnWorker}
 */
static DECLCALLBACK(int) vbclSVGASessionWorker(bool volatile *pfShutdown)
{
    int             rc = VINF_SUCCESS;

    /* Notify parent thread that we started successfully. */
    rc = RTThreadUserSignal(RTThreadSelf());
    if (RT_FAILURE(rc))
        VBClLogError("unable to notify parent thread about successful start\n");

    rc = RTCritSectEnter(&g_hClientCritSect);

    if (RT_FAILURE(rc))
    {
        VBClLogError("unable to enter critical section on worker start, rc=%Rrc\n", rc);
        return rc;
    }

    rc = vbDrmIpcClientInit(&g_hClient, RTThreadSelf(), g_hSession, VBOX_DRMIPC_TX_QUEUE_SIZE, vbclSVGASessionRxCallBack);
    int rc2 = RTCritSectLeave(&g_hClientCritSect);
    if (RT_FAILURE(rc2))
        VBClLogError("unable to leave critical section on worker start, rc=%Rrc\n", rc);

    if (RT_FAILURE(rc))
    {
        VBClLogError("cannot initialize IPC session, rc=%Rrc\n", rc);
        return rc;
    }

    for (;;)
    {
        rc = vbDrmIpcConnectionHandler(&g_hClient);

        /* Try to shutdown thread as soon as possible. */
        if (ASMAtomicReadBool(pfShutdown))
        {
            /* Shutdown requested. */
            break;
        }

        /* Normal case, there was no incoming messages for a while. */
        if (rc == VERR_TIMEOUT)
        {
            continue;
        }
        else if (RT_FAILURE(rc))
        {
            VBClLogError("unable to handle IPC connection, rc=%Rrc\n", rc);

            /* Relax a bit before spinning the loop. */
            RTThreadSleep(VBOX_DRMIPC_RX_RELAX_MS);
            /* Try to reconnect to server. */
            rc = vbclSVGASessionReconnect();
        }
    }

    /* Check if session was not closed before. */
    if (RT_VALID_PTR(g_hSession))
    {
        rc2 = RTCritSectEnter(&g_hClientCritSect);
        if (RT_SUCCESS(rc2))
        {
            rc2 = vbDrmIpcClientReleaseResources(&g_hClient);
            if (RT_FAILURE(rc2))
                VBClLogError("cannot release IPC session resources, rc=%Rrc\n", rc2);

            rc2 = RTCritSectLeave(&g_hClientCritSect);
            if (RT_FAILURE(rc2))
                VBClLogError("unable to leave critical section on worker end, rc=%Rrc\n", rc);
        }
        else
            VBClLogError("unable to enter critical section on worker end, rc=%Rrc\n", rc);
    }

    return rc;
}

/**
 * @interface_method_impl{VBCLSERVICE,pfnStop}
 */
static DECLCALLBACK(void) vbclSVGASessionStop(void)
{
    int rc;

    /* Check if session was not closed before. */
    if (!RT_VALID_PTR(g_hSession))
        return;

    /* Attempt to release any waiting syscall related to RTLocalIpcSessionXXX(). */
    rc = RTLocalIpcSessionFlush(g_hSession);
    if (RT_FAILURE(rc))
        VBClLogError("unable to flush data to IPC connection, rc=%Rrc\n", rc);

    rc = RTLocalIpcSessionCancel(g_hSession);
    if (RT_FAILURE(rc))
        VBClLogError("unable to cancel IPC session, rc=%Rrc\n", rc);
}

/**
 * @interface_method_impl{VBCLSERVICE,pfnTerm}
 */
static DECLCALLBACK(int) vbclSVGASessionTerm(void)
{
    int rc = VINF_SUCCESS;

    if (g_hSession)
    {
        rc = RTLocalIpcSessionClose(g_hSession);
        g_hSession = 0;

        if (RT_FAILURE(rc))
            VBClLogError("unable to close IPC connection, rc=%Rrc\n", rc);
    }

    if (g_pDisplayHelper)
    {
        if (g_pDisplayHelper->pfnUnsubscribeDisplayOffsetChangeNotification)
            g_pDisplayHelper->pfnUnsubscribeDisplayOffsetChangeNotification();

        if (g_pDisplayHelper->pfnTerm)
        {
            rc = g_pDisplayHelper->pfnTerm();
            if (RT_FAILURE(rc))
                VBClLogError("unable to terminate Desktop Environment helper '%s', rc=%Rrc\n",
                             rc, g_pDisplayHelper->pszName);
        }
    }

    return VINF_SUCCESS;
}

VBCLSERVICE g_SvcDisplaySVGASession =
{
    "vmsvga-session",                   /* szName */
    "VMSVGA display assistant",         /* pszDescription */
    ".vboxclient-vmsvga-session",       /* pszPidFilePathTemplate */
    NULL,                               /* pszUsage */
    NULL,                               /* pszOptions */
    NULL,                               /* pfnOption */
    vbclSVGASessionInit,                /* pfnInit */
    vbclSVGASessionWorker,              /* pfnWorker */
    vbclSVGASessionStop,                /* pfnStop */
    vbclSVGASessionTerm,                /* pfnTerm */
};