From patchwork Mon Jun 25 17:36:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10487089 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 B4CA160230 for ; Mon, 25 Jun 2018 17:38:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F15A20182 for ; Mon, 25 Jun 2018 17:38:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9333F22362; Mon, 25 Jun 2018 17:38:44 +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, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM, 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 39E3620182 for ; Mon, 25 Jun 2018 17:38:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D70496E36E; Mon, 25 Jun 2018 17:38:41 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x230.google.com (mail-wr0-x230.google.com [IPv6:2a00:1450:400c:c0c::230]) by gabe.freedesktop.org (Postfix) with ESMTPS id 34B916E369 for ; Mon, 25 Jun 2018 17:38:33 +0000 (UTC) Received: by mail-wr0-x230.google.com with SMTP id k16-v6so14540469wro.0 for ; Mon, 25 Jun 2018 10:38:33 -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=1p4lB/eU0IDgJJd0whqW1ASA8xlNGa7Up7ZOfhf6Qlk=; b=IXZZqR/lw35Vk6karcDYLTS2voDvUE71qMCRpyasoUrhkP6svX3GuzNRwEreVfuQ5g upKs1fk/WJs88VnJeAZ00POZHh51uUh7TrcXTgSlqV44fjtgIwX+hyjmYeKb9QCZNIEq xavU0VfraAd2+qY4OvTvExmjV558PKo3weOIRPaJt2pHnx2wcM/FGGfR3bp7LouuS/cu 8cIIlVywSsztmnLR2ZotaPwL5YgQZHzs5EZ3RcUrGgIpHe771BIR/gAoOxoGSXdjZUM3 Ej4HB3QbBrwCHVGo+PQFK9H+ps+qRs0HTPK6k5S+mx2AuQYf9BN7tFt1X1oo5agiEb4J e8mg== X-Gm-Message-State: APt69E1XzdyRixmaWusnT8aCwVTH3sUgcnsD8MOVFU+3076QeG82MQzm zz6clB8tNd0oF5p+Efucq1JUkQ== X-Google-Smtp-Source: AAOMgpc0WWh7/BxzHgymthp9+/erJZaD9aaYLvEFVHZdmu/ngS67jdoNBUHfY9Zdm9BI/ZRKh+BS4A== X-Received: by 2002:adf:9345:: with SMTP id 63-v6mr4208815wro.60.1529948311684; Mon, 25 Jun 2018 10:38:31 -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 p8-v6sm6581019wrq.70.2018.06.25.10.38.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jun 2018 10:38:30 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 05/10] xf86drm: introduce a get_real_pci_path() helper Date: Mon, 25 Jun 2018 18:36:21 +0100 Message-Id: <20180625173626.8046-5-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180625173626.8046-1-emil.l.velikov@gmail.com> References: <20180625173626.8046-1-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: 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. Signed-off-by: Emil Velikov Reviewed-by: Eric Engestrom --- xf86drm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index d4810740..8ccd528f 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -2992,16 +2992,37 @@ static int drmParseSubsystemType(int maj, int min) #endif } +static char * +get_real_pci_path(int maj, int min) +{ + char path[PATH_MAX + 1]; + char *real_path = malloc(PATH_MAX); + + if (!real_path) + return NULL; + + snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); + if (!realpath(path, real_path)) { + free(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, *value; int num; - snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); + real_path = get_real_pci_path(maj, min); + if (!real_path) + return -ENOENT; - value = sysfs_uevent_get(path, "PCI_SLOT_NAME"); + value = sysfs_uevent_get(real_path, "PCI_SLOT_NAME"); + free(real_path); if (!value) return -ENOENT; @@ -3114,24 +3135,32 @@ 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; unsigned int data[ARRAY_SIZE(attrs)]; FILE *fp; int ret; + real_path = get_real_pci_path(maj, min); + if (!real_path) + 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) + if (!fp) { + free(real_path); return -errno; + } ret = fscanf(fp, "%x", &data[i]); fclose(fp); - if (ret != 1) + if (ret != 1) { + free(real_path); return -errno; + } } + free(real_path); device->revision_id = ignore_revision ? 0xff : data[0] & 0xff; device->vendor_id = data[1] & 0xffff; @@ -3145,19 +3174,28 @@ 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; unsigned char config[64]; int fd, ret; - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min); + real_path = get_real_pci_path(maj, min); + if (!real_path) + return -ENOENT; + + snprintf(path, PATH_MAX, "%s/config", real_path); fd = open(path, O_RDONLY); - if (fd < 0) + if (fd < 0) { + free(real_path); return -errno; + } ret = read(fd, config, sizeof(config)); close(fd); - if (ret < 0) + if (ret < 0) { + free(real_path); return -errno; + } + free(real_path); device->vendor_id = config[0] | (config[1] << 8); device->device_id = config[2] | (config[3] << 8);