From patchwork Wed Jan 23 04:38:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher James Halse Rogers X-Patchwork-Id: 10776525 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 632CF1390 for ; Wed, 23 Jan 2019 04:39:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52550285DA for ; Wed, 23 Jan 2019 04:39:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 463A62B3FA; Wed, 23 Jan 2019 04:39:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BF6A7285DA for ; Wed, 23 Jan 2019 04:39:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0356F6E491; Wed, 23 Jan 2019 04:39:06 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail.cooperteam.net (mail.cooperteam.net [45.248.50.188]) by gabe.freedesktop.org (Postfix) with ESMTPS id DC7C76E491 for ; Wed, 23 Jan 2019 04:39:04 +0000 (UTC) Received: from Behemoth.canterbury.ac.nz (unknown [202.36.179.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: chris) by mail.cooperteam.net (Postfix) with ESMTPSA id 4B8A11975D; Wed, 23 Jan 2019 15:38:59 +1100 (AEDT) From: Christopher James Halse Rogers To: dri-devel@lists.freedesktop.org Subject: [PATCH v2] xf86drm: Add drmIsMaster() Date: Wed, 23 Jan 2019 15:38:45 +1100 Message-Id: <20190123043845.26916-1-christopher.halse.rogers@canonical.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181120033652.29067-1-christopher.halse.rogers@canonical.com> References: <20181120033652.29067-1-christopher.halse.rogers@canonical.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christopher James Halse Rogers Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Christopher James Halse Rogers Reviewed-by: Daniel Vetter --- xf86drm.c | 15 +++++++++++++++ xf86drm.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index 10df682b..adee5bd9 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -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]; diff --git a/xf86drm.h b/xf86drm.h index 7773d71a..9e920db9 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #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