🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19328

[PATCH iproute2-next v2 2/7] lib/fs: parse /proc/mounts with getmntent and check fs magic

#19328
From: Stephen Hemminger
Date: Thu, 19 Nov 2026 19:02
84 lines
12953 bytes
---
 lib/fs.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/lib/fs.c b/lib/fs.c
index a7d23498..fead9d08 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -9,6 +9,8 @@
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/mount.h>
+#include <sys/vfs.h>
+#include <mntent.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -17,6 +19,7 @@
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
+#include <linux/magic.h>
 
 #include "utils.h"
 
@@ -50,30 +53,34 @@ static int open_by_handle_at(int mount_fd, struct file_handle *handle, int flags
 }
 #endif
 
-/* return mount path of first occurrence of given fstype */
-static char *find_fs_mount(const char *fs_to_find)
+/* return mount path of first mount of given fstype that passes statfs check */
+static char *find_fs_mount(const char *fs_to_find, unsigned long magic)
 {
-	char path[4096];
-	char fstype[128];    /* max length of any filesystem name */
+	struct mntent *ent;
+	struct statfs st;
 	char *mnt = NULL;
 	FILE *fp;
 
-	fp = fopen("/proc/mounts", "r");
+	fp = setmntent("/proc/mounts", "r");
 	if (!fp) {
 		fprintf(stderr,
 			"Failed to open mounts file: %s\n", strerror(errno));
 		return NULL;
 	}
 
-	while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
-		      path, fstype) == 2) {
-		if (strcmp(fstype, fs_to_find) == 0) {
-			mnt = strdup(path);
-			break;
-		}
+	while ((ent = getmntent(fp)) != NULL) {
+		if (strcmp(ent->mnt_type, fs_to_find) != 0)
+			continue;
+
+		if (statfs(ent->mnt_dir, &st) < 0 ||
+		    (unsigned long)st.f_type != magic)
+			continue;
+
+		mnt = strdup(ent->mnt_dir);
+		break;
 	}
 
-	fclose(fp);
+	endmntent(fp);
 
 	return mnt;
 }
@@ -81,7 +88,7 @@ static char *find_fs_mount(const char *fs_to_find)
 /* caller needs to free string returned */
 char *find_cgroup2_mount(bool do_mount)
 {
-	char *mnt = find_fs_mount(CGROUP2_FS_NAME);
+	char *mnt = find_fs_mount(CGROUP2_FS_NAME, CGROUP2_SUPER_MAGIC);
 
 	if (mnt)
 		return mnt;
-- 
2.53.0
Technical Details
Message-ID: <20261119190312.578885-3-stephen@networkplumber.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367262-linux-kernel-netdev=dewy.ru
References: <20261119190312.578885-1-stephen@networkplumber.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367262-linux-kernel-netdev=dewy.ru
From: stephen@networkplumber.org (Stephen Hemminger)
Newsgroups: linux.kernel.netdev
Subject: [PATCH iproute2-next v2 2/7] lib/fs: parse /proc/mounts with getmntent and check fs magic
Date: Thu, 19 Nov 2026 11:02:37 -0800
Organization: Usenet Network
Message-ID: <20261119190312.578885-3-stephen@networkplumber.org>
References: <20261119190312.578885-1-stephen@networkplumber.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2957804"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:ptZ2pZwrF0fThdbgQxiKwnoRcyQ= sha256:z0Wl+IX/moOsKBXIK1wEo5P/vxMjnQ2U9CLcTeMUxLQ=
	sha1:uTCFkTo6k7JVgrkdLpZZl3VeSPM= sha256:CuVHGjrLCTBYVjl/CYqPrWRtNUPuYxAa+HOZxpVn3yc=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id DBEF1115746; Thu, 19 Nov 2026 22:03:38 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31
	tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=networkplumber-org.20260707.gappssmtp.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id UmF063iUQn2A;
	Thu, 19 Nov 2026 22:03:35 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-367262-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=quarantine dis=none) header.from=networkplumber.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id 0596811573C; Thu, 19 Nov 2026 22:03:26 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id 9ED2D62369F; Thu, 19 Nov 2026 19:03:25 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795114998;
	b=SN5GqiV4fmNLrDJkCAWFj2FJWqA/Hlpr0OjrLTNrAatuqVgWrp0tuCBr/QCegoS3tc1f
	 EeZ1/CyhoBMyhuvSIgW7c/I4YUh4ysziWC3jnQpSib8aGLU4PgvmI5ni8Ig5nqPuERgUq
	 WERIjI9MRi/eqoTyHeDnw9lAOPK+tP5cj0deiVkg/caL8ElUTrVD+EnciHoeH7Raqp705
	 rOoCEAMHTYoTpqyMZCY/rXQz1w+/pmOGNeCDY3W639i8nAdX9VGD0PGs6+Trfmo1PKMJz
	 JxMTvaCBGtvLTGm6yPWBR/jclPyGsjbYE6BlJvQSL/XoisBRiv1prqBoW0k5y64STUQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795114998;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=SOpz8xJcda/FwAFV+mAdjAIt7fp1KDUfQnq3ELZx+Yo=;
	b=O2cOf9Ib8zojLW71YaKKBNE3Q2T/wZ4Vrzq3fxfapX6hmShEGAsBrhp4w4xictoogRFj
	 6PtCvf+Z8EcbhO3ELzNtQMIaNKoKV58DwVdKOBGoSdjYh8crjq+hb6LnZQ96Om3/hoGCD
	 INrY05C50iUevSPlHdg/76g1NdiDfvYy4nSBFUaNY5RWeZqFuSSgQ0iRyqse1EJ3zTU7n
	 e4c8J2Sm1bovKFDqKYmn3qCNRsznB+Dt1oO1wtlzjjIPW0Nd48VW5wksU53Z3zyknvUHV
	 eJsmfzPO2L3s0C9DTtVHK2DROjfS+MeUvbJoRrFbvuTJfVTajmvL97oLFgpE4hZ59/g==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=F5r54Kgy;
	dmarc=pass header.from=networkplumber.org;
	spf=pass smtp.mailfrom=networkplumber.org;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 72DFA60A2605;
	Thu, 19 Nov 2026 19:03:18 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=F5r54Kgy
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795114997;
	b=cdOVx9fNwWCjCBgmssceeJI2i7QMutnPLNBg99TfYEX/l52cCvwnR0vsxOoPTGjiDSyP
	 11jkPIqLEBA+6PQKVTUUbl0dQqeygZoNlRKBT2NXaP6ghdD41EdeaVNW/VUxX/0mna867
	 IC4i4g+i5N+1bx/yzRhDOxGNBPYyVSIJbeBc1KWd6jZG8t906TeWfd9bzfQWZCIUl/mU4
	 Ze59O5gEa6125nXzz5PgDPfF2l2Um0Rp2QghK0Qx3gzCHGKTqjhTIG4vJUkR6fC74zOvb
	 /9blasRotdmBgB99ha27tXAhELXJxaDpucVcO72amGZYEexCEBofegGy1shEW8OwZuw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795114997;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=SOpz8xJcda/FwAFV+mAdjAIt7fp1KDUfQnq3ELZx+Yo=;
	b=a+X1EYawTtyZR0HWQpcGAxNC4PX+J9yUyTAsHSWsUHDqhTJv5j8jQ1c9FqiZetjoBREY
	 aOT2L8xKHEFSozyFjAUtywWEfAYDHYoi1RE0fgjoG2PKhkLpXIl4NuHGRKc6hzke3i0bk
	 mUFRfQCk5lauNHL9Naw3eAjSt/QaNqQl7TQYmWz3JoFamQkRBbL+CYdLj+UqoxtrfChEI
	 5SVnXYZqVN5LkS5P0uKmOKQwxJfn07g8AB0yrVSEKohTnad2CD1b7LYFwU5xnkMEBK55w
	 aEzxdomBFaL8JTgRdbaYTGbDfMBrLyPMKS9mbFlG1tmX8bJJb/H3wvQIbDVKDiwHiNA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=networkplumber.org;
	spf=pass smtp.mailfrom=networkplumber.org;
	dkim=pass header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=F5r54Kgy;
	arc=none smtp.remote-ip=209.85.214.169
X-Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4722B606A338
	for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 19:03:17 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4722B606A338
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=networkplumber.org
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=networkplumber.org
X-Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2eff29ca765so234995ad.3
        for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 11:03:17 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1795114997; x=1795719797; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=SOpz8xJcda/FwAFV+mAdjAIt7fp1KDUfQnq3ELZx+Yo=;
        b=F5r54KgyOzOnWQrcNuw/byQgOufsr1Pur2lWh9UdN0shmrye5CYupokRw4MCFfxU3R
         AY99ieNlqP9IvpFRBFUde+qcQ+ms9PkORvf4y/45d83A6DRy7oR870vbsImEIhx0ZYF9
         QMfe8TSPHCqDzWjf9uMtgA9pPZWEEjbwaZivTbEzSGBNyAei5x+x15OD1XwNRfvWlyK4
         R6FeURZUMU6UqCHu8Jp2ouKFA6udI2CA3Jjy7soU0Grv/ZRsm6b3J3wHrF/qji3OFuRB
         wRXMs0RCfGvJX6Hy9lTMbfnILaGYI/YDw7q/lFQK7ad6VCi6aBOK1dHu3iiEIPHD0oEJ
         qiMg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1795114997; x=1795719797;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=SOpz8xJcda/FwAFV+mAdjAIt7fp1KDUfQnq3ELZx+Yo=;
        b=hcUV2WVIoD8GY/C0bTuSSuL7uXDEZVgEgYn4fPlnGZIx1Pb9WY9rLQBsonm1dqThM8
         hAWqlNkdU+9T14bHOb4TEP9tQdia9rUjn0P3u5DNeCiCZUZ6+1BFrbhxNtUdQJObpc4X
         fs9ptuw4YWOucK1orotIkpvmaRK+4zE0EN3atLTLA4cJzjGrdlrB9IgfVXqkaWuE3j+n
         X+cBl3iyc9Mc5RZA8F8xzS2vDqBe89Dma8WaEoD9GIVFr3ErJdkGIYpF309OiHswe1f7
         /zvdlEFi3wRSZCM1FLhXTfqOhVzj0G0hLseTLJAcbPMy9Fdzk7SqtjBoBhr0DGh/i4BX
         cDFA==
X-Gm-Message-State: APuU4Z9r6E1E3RKQOiH9p3eDa7xoV86yYjrVYCrbgPL7xqxCSeE3flAm
	8loCTa/3CqIcnB+Y8xLst+gFWXvhL+yiSTGpIUAaHyyNJzYi7mAoJcwY4cQDLfhKZw7ol7UNf9h
	h4CW/Lho=
X-Gm-Gg: AZFdTZc42iyb+orc5WfP0AKuMKBzjwNAdqIh8+GAV2xtEJ13tzu83+qarW84SqJwLA8
	tTmb8SgwC5o3DNYwmk7vc1x33peES6WXYFGUK7gf5t5Xo6a0N9RuaQd15klTZanrqXHoge/P5xF
	Loh59UQErpXECRPZLZex0G9b35EuxZN+Li89WQMwVf/UCT0hAlVuUUdxt5Xa4L2SLwZLl+JCzEJ
	24Dzjm3xPm87wkmUBCY/G92QVASB6WU5rdXd/R+wgLEGQw1ZedqEBesa8tq0GiWX6rZ2cV6bBoI
	3udCW6SujAGNHBDMJzIeHr2KIOMtsOttb8LSJhU4HyLaB8Bf+XptRm4NjCJBg+0EtZPSJGaljVc
	JP8dMku3/S0bf8d0SlrxYCBIlD7b3unPjflA8ep0if2ScTatKRPP1gF09KbUWS9F6xavPPQus4z
	+TFxJ3I+CONU/EWdsLhXB9P2G+J0SW95pVl0lnRR+/5ig44cbw3xNJe/fboWnWddGq0aBu3pEZj
	g9CRoLXgOzBTFEvchn2he/adeE8ckmOUgEs5I7CuXk0YuXWttCa5PVEY5/kVv5umdaF2Z5PD1Dj
	zibWw+9J+VNmFiBQ9lYQulJGRrHpRKthasjOWl54PbwA2OeSYoB/q7+6tUQVpdKeK3YS
X-Received: by 2002:a17:903:22c8:b0:2e2:dd98:59e8 with SMTP id d9443c01a7336-2f03ef59ce4mr8085085ad.3.1795114996686;
        Thu, 19 Nov 2026 11:03:16 -0800 (PST)
X-Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43])
        by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f0de00asm3750225ad.35.2026.11.19.11.03.16
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 19 Nov 2026 11:03:16 -0800 (PST)
X-Cc: dsahern@kernel.org,
	Stephen Hemminger <stephen@networkplumber.org>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <20261119190312.578885-1-stephen@networkplumber.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list