From patchwork Tue Nov 20 03:36:52 2018 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: 10689849 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 3D74E15A7 for ; Tue, 20 Nov 2018 03:37:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3080D2A567 for ; Tue, 20 Nov 2018 03:37:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 24CFC2A570; Tue, 20 Nov 2018 03:37:02 +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 9F4382A567 for ; Tue, 20 Nov 2018 03:37:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A2AA06E2BB; Tue, 20 Nov 2018 03:37:00 +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 0BDDA6E2BB for ; Tue, 20 Nov 2018 03:36:59 +0000 (UTC) Received: from Extravaganza.lan (unknown [192.168.1.1]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: chris) by mail.cooperteam.net (Postfix) with ESMTPSA id 8B59A2CBA3; Tue, 20 Nov 2018 14:36:56 +1100 (AEDT) From: Christopher James Halse Rogers To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm] xf86drm: Add drmIsMaster() Date: Tue, 20 Nov 2018 14:36:52 +1100 Message-Id: <20181120033652.29067-1-christopher.halse.rogers@canonical.com> X-Mailer: git-send-email 2.19.1 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. Signed-off-by: Christopher James Halse Rogers --- xf86drm.c | 20 ++++++++++++++++++++ xf86drm.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index 10df682b..bdb0439d 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -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]; 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