Skip to content

Commit

Permalink
modesetting: Allow a DRM fd to be passed on command line with -master…
Browse files Browse the repository at this point in the history
…fd [v2]

This lets an application open a suitable DRM device and pass the file
descriptor to the mode setting driver through an X server command line
option, '-masterfd'.

There's a companion application, xlease, which creates a DRM master by
leasing an output from another X server. That is available at

	git clone git://people.freedesktop.org/~keithp/xlease

v2:
	Always print usage, but note that it can't be used if
	setuid/gid

	Suggested-by: Lyude Paul <lyude@redhat.com>

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Lyude Paul <lyude@redhat.com>
  • Loading branch information
keith-packard committed Jun 29, 2018
1 parent 2f39b2a commit 38ff29e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions hw/xfree86/common/xf86Globals.c
Expand Up @@ -53,6 +53,8 @@ DevPrivateKeyRec xf86ScreenKeyRec;
ScrnInfoPtr *xf86Screens = NULL; /* List of ScrnInfos */
ScrnInfoPtr *xf86GPUScreens = NULL; /* List of ScrnInfos */

int xf86DRMMasterFd = -1; /* Command line argument for DRM master file descriptor */

const unsigned char byte_reversed[256] = {
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
Expand Down
1 change: 1 addition & 0 deletions hw/xfree86/common/xf86Priv.h
Expand Up @@ -93,6 +93,7 @@ extern _X_EXPORT int xf86LogVerbose; /* log file verbosity level */

extern ScrnInfoPtr *xf86GPUScreens; /* List of pointers to ScrnInfoRecs */
extern int xf86NumGPUScreens;
extern _X_EXPORT int xf86DRMMasterFd; /* Command line argument for DRM master file descriptor */
#ifndef DEFAULT_VERBOSE
#define DEFAULT_VERBOSE 0
#endif
Expand Down
26 changes: 25 additions & 1 deletion hw/xfree86/drivers/modesetting/driver.c
Expand Up @@ -36,6 +36,7 @@
#include <unistd.h>
#include <fcntl.h>
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSproc.h"
#include "compiler.h"
#include "xf86Pci.h"
Expand Down Expand Up @@ -194,11 +195,24 @@ modesettingEntPtr ms_ent_priv(ScrnInfoPtr scrn)
return pPriv->ptr;
}

static int
get_passed_fd(void)
{
if (xf86DRMMasterFd >= 0) {
xf86DrvMsg(-1, X_INFO, "Using passed DRM master file descriptor %d\n", xf86DRMMasterFd);
return dup(xf86DRMMasterFd);
}
return -1;
}

static int
open_hw(const char *dev)
{
int fd;

if ((fd = get_passed_fd()) != -1)
return fd;

if (dev)
fd = open(dev, O_RDWR | O_CLOEXEC, 0);
else {
Expand Down Expand Up @@ -818,6 +832,12 @@ ms_get_drm_master_fd(ScrnInfoPtr pScrn)
return TRUE;
}

ms->fd_passed = FALSE;
if ((ms->fd = get_passed_fd()) >= 0) {
ms->fd_passed = TRUE;
return TRUE;
}

#ifdef XSERVER_PLATFORM_BUS
if (pEnt->location.type == BUS_PLATFORM) {
#ifdef XF86_PDEV_SERVER_FD
Expand Down Expand Up @@ -1502,6 +1522,9 @@ SetMaster(ScrnInfoPtr pScrn)
return TRUE;
#endif

if (ms->fd_passed)
return TRUE;

ret = drmSetMaster(ms->fd);
if (ret)
xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "drmSetMaster failed: %s\n",
Expand Down Expand Up @@ -1754,7 +1777,8 @@ LeaveVT(ScrnInfoPtr pScrn)
return;
#endif

drmDropMaster(ms->fd);
if (!ms->fd_passed)
drmDropMaster(ms->fd);
}

/*
Expand Down
1 change: 1 addition & 0 deletions hw/xfree86/drivers/modesetting/driver.h
Expand Up @@ -84,6 +84,7 @@ struct ms_drm_queue {

typedef struct _modesettingRec {
int fd;
Bool fd_passed;

int Chipset;
EntityInfoPtr pEnt;
Expand Down
21 changes: 21 additions & 0 deletions hw/xfree86/os-support/linux/lnx_init.c
Expand Up @@ -346,6 +346,13 @@ xf86CloseConsole(void)
close(xf86Info.consoleFd); /* make the vt-manager happy */
}

#define CHECK_FOR_REQUIRED_ARGUMENT() \
if (((i + 1) >= argc) || (!argv[i + 1])) { \
ErrorF("Required argument to %s not specified\n", argv[i]); \
UseMsg(); \
FatalError("Required argument to %s not specified\n", argv[i]); \
}

int
xf86ProcessArgument(int argc, char *argv[], int i)
{
Expand All @@ -366,6 +373,19 @@ xf86ProcessArgument(int argc, char *argv[], int i)
}
return 1;
}

if (!strcmp(argv[i], "-masterfd")) {
CHECK_FOR_REQUIRED_ARGUMENT();
if (xf86PrivsElevated())
FatalError("\nCannot specify -masterfd when server is setuid/setgid\n");
if (sscanf(argv[++i], "%d", &xf86DRMMasterFd) != 1) {
UseMsg();
xf86DRMMasterFd = -1;
return 0;
}
return 2;
}

return 0;
}

Expand All @@ -375,4 +395,5 @@ xf86UseMsg(void)
ErrorF("vtXX use the specified VT number\n");
ErrorF("-keeptty ");
ErrorF("don't detach controlling tty (for debugging only)\n");
ErrorF("-masterfd <fd> use the specified fd as the DRM master fd (not if setuid/gid)\n");
}

0 comments on commit 38ff29e

Please sign in to comment.