From patchwork Fri May 5 22:05:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 9714437 X-Patchwork-Delegate: christophe.varoqui@free.fr 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 2688E602B9 for ; Fri, 5 May 2017 22:07:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18952285F0 for ; Fri, 5 May 2017 22:07:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0DB652866A; Fri, 5 May 2017 22:07:06 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (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 AF7F3285F0 for ; Fri, 5 May 2017 22:07:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A5C8767EA3; Fri, 5 May 2017 22:07:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A5C8767EA3 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dm-devel-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A5C8767EA3 Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8159181B56; Fri, 5 May 2017 22:07:04 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4A59B18523D4; Fri, 5 May 2017 22:07:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v45M6ghb015087 for ; Fri, 5 May 2017 18:06:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 98FF97AF7F; Fri, 5 May 2017 22:06:42 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 911B37B552; Fri, 5 May 2017 22:06:42 +0000 (UTC) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A1AB580F99; Fri, 5 May 2017 22:06:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A1AB580F99 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mwilck@suse.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A1AB580F99 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 7F8ACAC85; Fri, 5 May 2017 22:06:38 +0000 (UTC) From: Martin Wilck To: Christophe Varoqui , Hannes Reinecke , Benjamin Marzinski Date: Sat, 6 May 2017 00:05:59 +0200 Message-Id: <20170505220559.7248-11-mwilck@suse.com> In-Reply-To: <20170505220559.7248-1-mwilck@suse.com> References: <20170505220559.7248-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 203 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 05 May 2017 22:06:41 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 05 May 2017 22:06:41 +0000 (UTC) for IP:'195.135.220.15' DOMAIN:'mx2.suse.de' HELO:'mx1.suse.de' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -1.501 (BAYES_50, RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.220.15 mx2.suse.de 195.135.220.15 mx2.suse.de X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH 10/10] kpartx: find_loop_by_file: use sysfs X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 05 May 2017 22:07:05 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Rather then searching through all of /dev, look up loop devices in /sys/devices/virtual/block. This is cleaner and more robust (/dev/loop$Xp$Y symlinks may confuse kpartx). Signed-off-by: Martin Wilck --- kpartx/lopart.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/kpartx/lopart.c b/kpartx/lopart.c index 44f0c277..0521d7dc 100644 --- a/kpartx/lopart.c +++ b/kpartx/lopart.c @@ -68,25 +68,46 @@ char *find_loop_by_file(const char *filename) { DIR *dir; struct dirent *dent; - char dev[64], *found = NULL; + char dev[64], *found = NULL, *p; int fd; struct stat statbuf; struct loop_info loopinfo; + const char VIRT_BLOCK[] = "/sys/devices/virtual/block"; + char path[PATH_MAX]; - dir = opendir("/dev"); + dir = opendir(VIRT_BLOCK); if (!dir) return NULL; while ((dent = readdir(dir)) != NULL) { if (strncmp(dent->d_name,"loop",4)) continue; - if (!strcmp(dent->d_name, "loop-control")) - continue; - sprintf(dev, "/dev/%s", dent->d_name); - fd = open (dev, O_RDONLY); + if (snprintf(path, PATH_MAX, "%s/%s/dev", VIRT_BLOCK, + dent->d_name) >= PATH_MAX) + continue; + + fd = open(path, O_RDONLY); if (fd < 0) - break; + continue; + + if (read(fd, dev, sizeof(dev)) <= 0) { + close(fd); + continue; + } + + close(fd); + + dev[sizeof(dev)-1] = '\0'; + p = strchr(dev, '\n'); + if (p != NULL) + *p = '\0'; + if (snprintf(path, PATH_MAX, "/dev/block/%s", dev) >= PATH_MAX) + continue; + + fd = open (path, O_RDONLY); + if (fd < 0) + continue; if (fstat (fd, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) { @@ -99,13 +120,12 @@ char *find_loop_by_file(const char *filename) continue; } + close (fd); + if (0 == strcmp(filename, loopinfo.lo_name)) { - close (fd); - found = xstrdup(dev); + found = realpath(path, NULL); break; } - - close (fd); } closedir(dir); return found;