Skip to contentSkip to conversation

xf86drm: Add drmIsMaster()

PATCH Superseded Christopher James Halse Rogers 5 participants 12 messages Nov 20, 2018 – Feb 19, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xf86drm: Add drmIsMaster()

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

---
 xf86drm.c | 20 ++++++++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 22 insertions(+)
xf86drm.c +20 -0
index 10df682b..bdb0439d 100644
@@ -2741,6 +2741,26 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+    struct drm_mode_mode_cmd cmd;
+
+    memclear(cmd);
+    /* Set an invalid connector_id to ensure that ATTACHMODE errors with
+     * EINVAL in the unlikely event someone feels like calling this on a
+     * kernel prior to 3.9. */
+    cmd.connector_id = -1;
+
+    if (drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &cmd) != -1)
+    {
+        /* On 3.9 ATTACHMODE was changed to drm_noop, and so will succeed
+	 * iff we've got a master fd */
+        return true;
+    }
+
+    return errno == EINVAL;
+}
+
 drm_public char *drmGetDeviceNameFromFd(int fd)
 {
     char name[128];
xf86drm.h +2 -0
index 7773d71a..9e920db9 100644
@@ -37,6 +37,7 @@
 #include <stdarg.h>
 #include <sys/types.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <drm.h>
 
 #if defined(__cplusplus)
@@ -733,6 +734,7 @@ extern void drmMsg(const char *format, ...) DRM_PRINTFLIKE(1, 2);
 
 extern int drmSetMaster(int fd);
 extern int drmDropMaster(int fd);
+extern bool drmIsMaster(int fd);
 
 #define DRM_EVENT_CONTEXT_VERSION 4
 
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Hi Christopher,

Quoted 4 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Can you please mention the exact use case here? You mentioned it over IRC although it'll be nice to have it here for posterity.

Quoted 4 lines

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

I'm wondering if we cannot extent DRM_IOCTL_GET_CLIENT or another IOCTL. What do you think? May I interest you in writing an RFC for the kernel-side?

IMHO checking with the kernel devs for a cleaner solution is a worthy goal. If they're unhappy we can use this workaround.

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 16 lines · Emil Velikov

Hi Christopher,

Quoted 4 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Can you please mention the exact use case here? You mentioned it over IRC although it'll be nice to have it here for posterity.

Quoted 4 lines

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

Quoted 5 lines

I'm wondering if we cannot extent DRM_IOCTL_GET_CLIENT or another IOCTL. What do you think? May I interest you in writing an RFC for the kernel-side?

IMHO checking with the kernel devs for a cleaner solution is a worthy goal. If they're unhappy we can use this workaround.

I think an invalid request through authmagic would be cleaner. That should give you EINVAL (you're master) or EACCESS (you're not master). It's also directly related to the master status, so less of a mindbinder. Note that we allocate magic numbers through the idr starting at 1, 0 is considered an invalid magic number and rejected. -Daniel

Quoted 7 lines

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw
In reply to Emil Velikov
Quoted 10 lines · Emil Velikov

Hi Christopher,

Quoted 4 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Can you please mention the exact use case here? You mentioned it over IRC although it'll be nice to have it here for posterity.

Certainly!

The particular use-case I was hitting was testing my display server in a container, where container-root is not real-root but the implicit-master FD you get by opening the DRM node when there is no current master would be sufficient.

Just assuming the FD we get is master and failing later breaks the platform probing; for example, when run under X11 if we don't check master we'll load the KMS platform and then fail, rather than noticing that KMS won't work and using our X11 backend.

Quoted 9 lines
Quoted 5 lines

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

I'm wondering if we cannot extent DRM_IOCTL_GET_CLIENT or another IOCTL.
What do you think? May I interest you in writing an RFC for the kernel-side?

I think if I was going to do kernel-side changes if probably just make it so that IOCTL_SET_MASTER just unconditionally succeeded on an fd that was already master?
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 35 lines · Christopher James Halse Rogers
Quoted 10 lines · Emil Velikov

Hi Christopher,

Quoted 4 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Can you please mention the exact use case here? You mentioned it over IRC although it'll be nice to have it here for posterity.

Certainly!

The particular use-case I was hitting was testing my display server in a container, where container-root is not real-root but the implicit-master FD you get by opening the DRM node when there is no current master would be sufficient.

Just assuming the FD we get is master and failing later breaks the platform probing; for example, when run under X11 if we don't check master we'll load the KMS platform and then fail, rather than noticing that KMS won't work and using our X11 backend.

Quoted 9 lines
Quoted 5 lines

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

I'm wondering if we cannot extent DRM_IOCTL_GET_CLIENT or another IOCTL.
What do you think? May I interest you in writing an RFC for the kernel-side?

Quoted 7 lines

I think if I was going to do kernel-side changes if probably just make it so that IOCTL_SET_MASTER just unconditionally succeeded on an fd that was already master?
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Ping!

Is there anything more you'd like to know about my use-case here? Are there any objections to adding drmIsMaster()?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 29 lines · Christopher James Halse Rogers
Quoted 27 lines · Christopher James Halse Rogers
Quoted 11 lines · Emil Velikov

Hi Christopher,

Quoted 4 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Can you please mention the exact use case here? You mentioned it over IRC although it'll be nice to have it here for posterity.

Certainly!

The particular use-case I was hitting was testing my display server in a container, where container-root is not real-root but the implicit-master FD you get by opening the DRM node when there is no current master would be sufficient.

Just assuming the FD we get is master and failing later breaks the platform probing; for example, when run under X11 if we don't check master we'll load the KMS platform and then fail, rather than noticing that KMS won't work and using our X11 backend.

Quoted 6 lines
Quoted 6 lines

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I'm wondering if we cannot extent DRM_IOCTL_GET_CLIENT or another IOCTL.
What do you think? May I interest you in writing an RFC for the kernel-side?

I think if I was going to do kernel-side changes if probably just make it so that IOCTL_SET_MASTER just unconditionally succeeded on an fd that was already master?

Use-case seems all reasonable, I was waiting for a respin with my suggestion ... -Daniel

Quoted 13 lines
Quoted 4 lines

________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Ping!

Is there anything more you'd like to know about my use-case here? Are there any objections to adding drmIsMaster()?
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

PATCH v2 1/1 xf86drm: Add drmIsMaster()

In reply to Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm device while no other master exists, but can't call drmSetMaster itself because it's not running as root or is in a container, where container-root isn't real-root.

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's more
clearly related to master status.

---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +15 -0
index 10df682b..adee5bd9 100644
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires master.
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this on
+         * a master fd will fail therefore fail with EINVAL because 0 is
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel checks for
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;
+}
+
 drm_public char *drmGetDeviceNameFromFd(int fd)
 {
     char name[128];
xf86drm.h +2 -0
index 7773d71a..9e920db9 100644
@@ -37,6 +37,7 @@
 #include <stdarg.h>
 #include <sys/types.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <drm.h>
 
 #if defined(__cplusplus)
@@ -733,6 +734,7 @@ extern void drmMsg(const char *format, ...) DRM_PRINTFLIKE(1, 2);
 
 extern int drmSetMaster(int fd);
 extern int drmDropMaster(int fd);
+extern bool drmIsMaster(int fd);
 
 #define DRM_EVENT_CONTEXT_VERSION 4
 
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v2 1/1
#7 lore raw
Quoted 12 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm device while no other master exists, but can't call drmSetMaster itself because it's not running as root or is in a container, where container-root isn't real-root.

Quoted 4 lines

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's more
clearly related to master status.

Can I also motivate you for an igt, to make sure this uapi never breaks again? I think adding a new subtest to core_auth.c would fit well, which does:

1. open drm fd, check that his IsMaster function returns true (to avoid dependencies with unreleased libdrm just make a localdrmIsMaster copy, we'll collect it eventually).

2. keep the first fd open, open a 2nd fd, check that we're _not_ master anymore.

3. close both fd.

Also, do you have libdrm commit rights to push this and make a release?

Cheers, Daniel

Quoted 58 lines
---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +15 -0
index 10df682b..adee5bd9 100644
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires master.
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this on
+         * a master fd will fail therefore fail with EINVAL because 0 is
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel checks for
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;
+}
+
 drm_public char *drmGetDeviceNameFromFd(int fd)
 {
     char name[128];
xf86drm.h +2 -0
index 7773d71a..9e920db9 100644
@@ -37,6 +37,7 @@
 #include <stdarg.h>
 #include <sys/types.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <drm.h>
 
 #if defined(__cplusplus)
@@ -733,6 +734,7 @@ extern void drmMsg(const char *format, ...) DRM_PRINTFLIKE(1, 2);
 
 extern int drmSetMaster(int fd);
 extern int drmDropMaster(int fd);
+extern bool drmIsMaster(int fd);
 
 #define DRM_EVENT_CONTEXT_VERSION 4
 
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#8 lore raw
In reply to Christopher James Halse Rogers
Quoted 33 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm device while no other master exists, but can't call drmSetMaster itself because it's not running as root or is in a container, where container-root isn't real-root.

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's more
clearly related to master status.

---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +3 -0
index 10df682b..adee5bd9 100644
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires master.

xf86drm.c drm_public int drmDropMaster(int fd) · line 2756

Quoted 10 lines
xf86drm.c +10 -0
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this on
+         * a master fd will fail therefore fail with EINVAL because 0 is
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel checks for
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;

What magic value is valid, is a DRM implementation detail, which we don't need to depend upon.

Instead we can check for EACCES, since we care if we have permissions
- aka are we master.
The function returns a negative errno, so I'd make this a:

        return drmAuthMagic(fd, 0) != -EACCES;

If you and Daniel agree, I'll squash this locally and push.

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw
Quoted 57 lines · Emil Velikov
Quoted 9 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm

device

Quoted 1 line

while no other master exists, but can't call drmSetMaster itself

because

Quoted 1 line

it's not running as root or is in a container, where container-root

isn't

Quoted 3 lines

real-root.

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's

more

Quoted 3 lines

clearly related to master status.

Signed-off-by Christopher James Halse Rogers

<[email protected]>

Quoted 10 lines
---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +0 -0
index 10df682b..adee5bd9 100644

xf86drm.c drm_public int drmDropMaster(int fd) · line 2746

Quoted 6 lines
xf86drm.c +3 -0
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires

master.

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this

on

xf86drm.c drm_public int drmDropMaster(int fd) · line 2750

Quoted 1 line
xf86drm.c +1 -0
+         * a master fd will fail therefore fail with EINVAL because

0 is

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel

checks for

xf86drm.c drm_public int drmDropMaster(int fd) · line 2756

Quoted 3 lines
xf86drm.c +3 -0
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;

What magic value is valid, is a DRM implementation detail, which we don't need to depend upon.

Quoted 7 lines

Instead we can check for EACCES, since we care if we have permissions
- aka are we master.
The function returns a negative errno, so I'd make this a:

       return drmAuthMagic(fd, 0) != -EACCES;

If you and Daniel agree, I'll squash this locally and push.

That's a much better idea, thanks!

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw
Quoted 62 lines · Christopher James Halse Rogers
Quoted 61 lines · Emil Velikov
Quoted 9 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm

device

Quoted 1 line

while no other master exists, but can't call drmSetMaster itself

because

Quoted 1 line

it's not running as root or is in a container, where container-root

isn't

Quoted 3 lines

real-root.

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's

more

Quoted 3 lines

clearly related to master status.

Signed-off-by Christopher James Halse Rogers

<[email protected]>

Quoted 10 lines
---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +0 -0
index 10df682b..adee5bd9 100644

xf86drm.c drm_public int drmDropMaster(int fd) · line 2746

Quoted 6 lines
xf86drm.c +3 -0
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires

master.

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this

on

xf86drm.c drm_public int drmDropMaster(int fd) · line 2750

Quoted 1 line
xf86drm.c +1 -0
+         * a master fd will fail therefore fail with EINVAL because

0 is

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel

checks for

xf86drm.c drm_public int drmDropMaster(int fd) · line 2756

Quoted 3 lines
xf86drm.c +3 -0
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;

What magic value is valid, is a DRM implementation detail, which we don't need to depend upon.

Instead we can check for EACCES, since we care if we have permissions
- aka are we master.
The function returns a negative errno, so I'd make this a:

Quoted 5 lines
Quoted 3 lines
       return drmAuthMagic(fd, 0) != -EACCES;

If you and Daniel agree, I'll squash this locally and push.

That's a much better idea, thanks!

I don't like checks for "something else happened", I much prefer checking for something specific. Hence == -EINVAL over != EACCESS. But I guess in this case here it doesn't matter.

We just need to make sure that the igt tests both ways, to make sure we'll never ever break this. I'd still prefer the current version, imo easier to write an igt to make sure it keeps working.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#11 lore raw
Quoted 69 lines · Daniel Vetter
Quoted 62 lines · Christopher James Halse Rogers
Quoted 61 lines · Emil Velikov
Quoted 9 lines · Christopher James Halse Rogers

We can't use drmSetMaster to query whether or not a drm fd is master because it requires CAP_SYS_ADMIN, even if the fd *is* a master fd.

Pick DRM_IOCTL_MODE_ATTACHMODE as a long-deprecated ioctl that is DRM_MASTER but not DRM_ROOT_ONLY as the probe by which we can detect whether or not the fd is master.

This is useful for code that might get master by open()ing the drm

device

Quoted 1 line

while no other master exists, but can't call drmSetMaster itself

because

Quoted 1 line

it's not running as root or is in a container, where container-root

isn't

Quoted 3 lines

real-root.

v2: Use the AUTH_MAGIC request rather than MODE_ATTACHMODE, as it's

more

Quoted 3 lines

clearly related to master status.

Signed-off-by Christopher James Halse Rogers

<[email protected]>

Quoted 10 lines
---
 xf86drm.c | 15 +++++++++++++++
 xf86drm.h |  2 ++
 2 files changed, 17 insertions(+)
xf86drm.c +0 -0
index 10df682b..adee5bd9 100644

xf86drm.c drm_public int drmDropMaster(int fd) · line 2746

Quoted 6 lines
xf86drm.c +3 -0
@@ -2741,6 +2741,21 @@ drm_public int drmDropMaster(int fd)
         return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
 }
 
+drm_public bool drmIsMaster(int fd)
+{
+        /* Detect master by attempting something that requires

master.

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         *
+         * Authenticating magic tokens requires master and 0 is
+         * guaranteed to be an invalid magic number. Attempting this

on

xf86drm.c drm_public int drmDropMaster(int fd) · line 2750

Quoted 1 line
xf86drm.c +1 -0
+         * a master fd will fail therefore fail with EINVAL because

0 is

xf86drm.c drm_public int drmDropMaster(int fd)

Quoted 3 lines
xf86drm.c +3 -0
+         * invalid.
+         *
+         * A non-master fd will fail with EACCESS, as the kernel

checks for

xf86drm.c drm_public int drmDropMaster(int fd) · line 2756

Quoted 3 lines
xf86drm.c +3 -0
+         * master before attempting to do anything else.
+         */
+        return drmAuthMagic(fd, 0) == EINVAL;

What magic value is valid, is a DRM implementation detail, which we don't need to depend upon.

Instead we can check for EACCES, since we care if we have permissions
- aka are we master.
The function returns a negative errno, so I'd make this a:

Quoted 5 lines
Quoted 3 lines
       return drmAuthMagic(fd, 0) != -EACCES;

If you and Daniel agree, I'll squash this locally and push.

That's a much better idea, thanks!

Quoted 7 lines

I don't like checks for "something else happened", I much prefer checking for something specific. Hence == -EINVAL over != EACCESS. But I guess in this case here it doesn't matter.

We just need to make sure that the igt tests both ways, to make sure we'll never ever break this. I'd still prefer the current version, imo easier to write an igt to make sure it keeps working.

Since this landed now ... is the igt anywhere to make sure this keeps working?
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel