From patchwork Sat Nov 26 00:40:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Gray X-Patchwork-Id: 9448199 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A3C0760778 for ; Sat, 26 Nov 2016 00:47:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 93C6B26E69 for ; Sat, 26 Nov 2016 00:47:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 87C192787C; Sat, 26 Nov 2016 00:47:42 +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=-4.2 required=2.0 tests=BAYES_00, 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 C2E5D26E82 for ; Sat, 26 Nov 2016 00:47:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B04CD6E2E8; Sat, 26 Nov 2016 00:47:25 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from lechuck.jsg.id.au (jsg.id.au [210.15.216.215]) by gabe.freedesktop.org (Postfix) with ESMTPS id B6CE46E19D for ; Sat, 26 Nov 2016 00:47:19 +0000 (UTC) Received: from largo.jsg.id.au (largo.jsg.id.au [192.168.1.43]) by lechuck.jsg.id.au (OpenSMTPD) with ESMTP id dfd724d8; Sat, 26 Nov 2016 11:40:35 +1100 (AEDT) Received: from largo.jsg.id.au (localhost [127.0.0.1]) by largo.jsg.id.au (OpenSMTPD) with ESMTP id 110d949a; Sat, 26 Nov 2016 11:40:34 +1100 (AEDT) From: Jonathan Gray To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 5/5] xf86drm: implement an OpenBSD specific drmGetDevice Date: Sat, 26 Nov 2016 11:40:34 +1100 Message-Id: <20161126004034.53376-5-jsg@jsg.id.au> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20161126004034.53376-1-jsg@jsg.id.au> References: <20161126004034.53376-1-jsg@jsg.id.au> Cc: emil.l.velikov@gmail.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP This avoids walking all of /dev and directly maps the fd to a path to a primary node. Signed-off-by: Jonathan Gray --- xf86drm.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index 2a60b2e..a4b2506 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3175,6 +3175,46 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) */ int drmGetDevice(int fd, drmDevicePtr *device) { +#ifdef __OpenBSD__ + drmDevicePtr d; + struct stat sbuf; + char node[PATH_MAX + 1]; + char d_name[PATH_MAX + 1]; + int maj, min, n; + int ret; + int max_count = 1; + + if (fd == -1 || device == NULL) + return -EINVAL; + + if (fstat(fd, &sbuf)) + return -errno; + + maj = major(sbuf.st_rdev); + min = minor(sbuf.st_rdev); + + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) + return -EINVAL; + + n = snprintf(d_name, PATH_MAX, "drm%d", min); + if (n == -1 || n >= PATH_MAX) + return -errno; + + n = snprintf(node, PATH_MAX, DRM_DEV_NAME, DRM_DIR_NAME, min); + if (n == -1 || n >= PATH_MAX) + return -errno; + if (stat(node, &sbuf)) + return -EINVAL; + + ret = drmProcessPciDevice(&d, d_name, node, DRM_NODE_PRIMARY, + maj, min, true); + if (ret) + return ret; + + *device = d; + + return 0; +#else drmDevicePtr *local_devices; drmDevicePtr d; DIR *sysdir; @@ -3282,6 +3322,7 @@ free_devices: free_locals: free(local_devices); return ret; +#endif } /**