From patchwork Fri Jun 29 15:22:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10496909 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 02134601C7 for ; Fri, 29 Jun 2018 15:25:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D94D5299BB for ; Fri, 29 Jun 2018 15:25:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CCDEE29A7D; Fri, 29 Jun 2018 15:25:05 +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=-3.5 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED, URIBL_BLACK 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 7C839299BB for ; Fri, 29 Jun 2018 15:25:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1B4C66EE47; Fri, 29 Jun 2018 15:25:04 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x22d.google.com (mail-wm0-x22d.google.com [IPv6:2a00:1450:400c:c09::22d]) by gabe.freedesktop.org (Postfix) with ESMTPS id 29A556EE47 for ; Fri, 29 Jun 2018 15:25:03 +0000 (UTC) Received: by mail-wm0-x22d.google.com with SMTP id 69-v6so2475314wmf.3 for ; Fri, 29 Jun 2018 08:25:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rzRG+Rpf+kFyo+HGRrb7Rg3VD0U6oq9vqHWwjmTQsDU=; b=C8Kl8UI6Dp6gSdVvuquXW/YPGaXEdJiJkiq7rUGVlDqJXWIxjSydMYv5JXcobmv9zI voSHYPqQLhlha1L5rPaxVicsaMaamMRUy0IltVJ9rFs9O5m7WWdnFORmGls6LxDzrPBB irN3A7LYeQWfGpZ7w9Gei/CFZuW+OMx/30oTEDdfYxaP9J1m/AUl2tzUvqku9o4Nq7aR vTDb6C7AbQcFInHBCgxFaicXrtzvcqyri9Xr6kAtG/eiW4hM7juuPF975Y/cmXsYBwsY cQYa8RHmRogElm0UNKAAkak4/mt4N2xJLKXCXAbbEoA2IxXMxdquJxfYVtIQ4waQcVOC DiiQ== X-Gm-Message-State: APt69E3XpDzi5DGQvy4UMEZ/74bKPJyv+Hq01p7UvCxeQth3/YVggMyE 9sEcJ9cJMJfuUKpdMXx2R/VWxQ== X-Google-Smtp-Source: AAOMgpcyJ31Qzy8iuPl6rndZ3KvYWYo2QNZBFGLGvEJw8ptzbXtPwpAAbf59FpA3PSjlbftVKHQAHA== X-Received: by 2002:a1c:2e51:: with SMTP id u78-v6mr2076649wmu.101.1530285901581; Fri, 29 Jun 2018 08:25:01 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:5f00:102:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id v19-v6sm1200376wmc.9.2018.06.29.08.24.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Jun 2018 08:25:00 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm v2 05/10] xf86drm: introduce a get_real_pci_path() helper Date: Fri, 29 Jun 2018 16:22:54 +0100 Message-Id: <20180629152254.29924-1-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180625173626.8046-4-emil.l.velikov@gmail.com> References: <20180625173626.8046-4-emil.l.velikov@gmail.com> 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: Eric Engestrom , emil.l.velikov@gmail.com MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Introduce a helper which gets the real sysfs path for the given pci device. In other words, instead opening the /sys/dev/char/*/device symlink, we opt for the actual /sys/devices/pci*/*/ It folds three (nearly identical) snprintf's and paves the way of adding extra devices (see next patch) a piece of pie. v2: use a caller (on stack) provided real_path (Eric) Cc: Eric Engestrom Signed-off-by: Emil Velikov Tested-by: Robert Foss (v1) Reviewed-by: Robert Foss (v1) Reviewed-by: Eric Engestrom (v1) --- Eric, I couldn't quite remove all the error handling since on realpath() failure the output buffer contents are undefined :-\ xf86drm.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 02da3e1f..51e00d23 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -2992,16 +2992,29 @@ static int drmParseSubsystemType(int maj, int min) #endif } +static char * +get_real_pci_path(int maj, int min, char *real_path) +{ + char path[PATH_MAX + 1]; + + snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); + if (!realpath(path, real_path)) + return NULL; + + return real_path; +} + static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) { #ifdef __linux__ unsigned int domain, bus, dev, func; - char path[PATH_MAX + 1], *value; + char real_path[PATH_MAX + 1], *value; int num; - snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); + if (get_real_pci_path(maj, min, real_path) == NULL) + return -ENOENT; - value = sysfs_uevent_get(path, "PCI_SLOT_NAME"); + value = sysfs_uevent_get(real_path, "PCI_SLOT_NAME"); if (!value) return -ENOENT; @@ -3114,14 +3127,16 @@ static int parse_separate_sysfs_files(int maj, int min, "subsystem_vendor", "subsystem_device", }; - char path[PATH_MAX + 1]; + char path[PATH_MAX + 1], real_path[PATH_MAX + 1]; unsigned int data[ARRAY_SIZE(attrs)]; FILE *fp; int ret; + if (get_real_pci_path(maj, min, real_path) == NULL) + return -ENOENT; + for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) { - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/%s", maj, min, - attrs[i]); + snprintf(path, PATH_MAX, "%s/%s", real_path, attrs[i]); fp = fopen(path, "r"); if (!fp) return -errno; @@ -3145,11 +3160,14 @@ static int parse_separate_sysfs_files(int maj, int min, static int parse_config_sysfs_file(int maj, int min, drmPciDeviceInfoPtr device) { - char path[PATH_MAX + 1]; + char path[PATH_MAX + 1], real_path[PATH_MAX + 1]; unsigned char config[64]; int fd, ret; - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min); + if (get_real_pci_path(maj, min, real_path) == NULL) + return -ENOENT; + + snprintf(path, PATH_MAX, "%s/config", real_path); fd = open(path, O_RDONLY); if (fd < 0) return -errno;