linux-modules.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luis Chamberlain <mcgrof@kernel.org>
To: lucas.demarchi@intel.com, linux-modules@vger.kernel.org
Cc: live-patching@vger.kernel.org, fstests@vger.kernel.org,
	linux-block@vger.kernel.org, hare@suse.de, dgilbert@interlog.com,
	jeyu@kernel.org, osandov@fb.com, linux-kernel@vger.kernel.org,
	Luis Chamberlain <mcgrof@kernel.org>
Subject: [PATCH v2 2/3] libkmod/libkmod-module: add refcnt fd helper
Date: Mon,  9 Aug 2021 22:16:01 -0700	[thread overview]
Message-ID: <20210810051602.3067384-3-mcgrof@kernel.org> (raw)
In-Reply-To: <20210810051602.3067384-1-mcgrof@kernel.org>

The first part of kmod_module_get_refcnt() is to open the
file descriptor for the refcnt file. Add a helper to do this
as it can then be used in other cases in the library.

Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 libkmod/libkmod-module.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 6e0ff1a..04bb4d9 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -1877,25 +1877,16 @@ done:
 	return size;
 }
 
-/**
- * kmod_module_get_refcnt:
- * @mod: kmod module
- *
- * Get the ref count of this @mod, as returned by Linux Kernel, by reading
- * /sys filesystem.
- *
- * Returns: the reference count on success or < 0 on failure.
- */
-KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
+static int kmod_module_get_refcnt_fd(const struct kmod_module *mod)
 {
 	char path[PATH_MAX];
-	long refcnt;
 	int fd, err;
 
 	if (mod == NULL)
 		return -ENOENT;
 
 	snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
+
 	fd = open(path, O_RDONLY|O_CLOEXEC);
 	if (fd < 0) {
 		err = -errno;
@@ -1903,12 +1894,32 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
 			path, strerror(errno));
 		return err;
 	}
+	return fd;
+}
+
+/**
+ * kmod_module_get_refcnt:
+ * @mod: kmod module
+ *
+ * Get the ref count of this @mod, as returned by Linux Kernel, by reading
+ * /sys filesystem.
+ *
+ * Returns: the reference count on success or < 0 on failure.
+ */
+KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
+{
+	long refcnt;
+	int fd, err;
+
+	fd = kmod_module_get_refcnt_fd(mod);
+	if (fd < 0)
+		return fd;
 
 	err = read_str_long(fd, &refcnt, 10);
 	close(fd);
 	if (err < 0) {
-		ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
-			path, strerror(-err));
+		ERR(mod->ctx, "could not read integer from '/sys/module/%s/refcnt': '%s'\n",
+		    mod->name, strerror(-err));
 		return err;
 	}
 
-- 
2.30.2


  parent reply	other threads:[~2021-08-10  5:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10  5:15 [PATCH v2 0/3] kmod: add patient module removal support Luis Chamberlain
2021-08-10  5:16 ` [PATCH v2 1/3] libkmod: add a library notice log level print Luis Chamberlain
2021-09-21  5:53   ` Lucas De Marchi
2021-09-21 18:40     ` Luis Chamberlain
2021-09-23  8:02       ` Lucas De Marchi
2021-08-10  5:16 ` Luis Chamberlain [this message]
2021-08-10  5:16 ` [PATCH v2 3/3] libkmod-module: add support for a patient module removal option Luis Chamberlain
2021-09-23  8:51   ` Lucas De Marchi
2021-09-28  1:38     ` Luis Chamberlain
2021-09-29 18:48       ` Lucas De Marchi
2021-11-15 20:21         ` Luis Chamberlain
2021-12-08 12:53           ` Luis Chamberlain
2021-09-15 17:40 ` [PATCH v2 0/3] kmod: add patient module removal support Luis Chamberlain
2021-09-21  5:51   ` Lucas De Marchi
2022-02-17  8:13     ` Luis Chamberlain
2022-02-18  8:20       ` Lucas De Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210810051602.3067384-3-mcgrof@kernel.org \
    --to=mcgrof@kernel.org \
    --cc=dgilbert@interlog.com \
    --cc=fstests@vger.kernel.org \
    --cc=hare@suse.de \
    --cc=jeyu@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=osandov@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).