代码拉取完成,页面将自动刷新
From 702f6f0178fa74cf66a13813bd40e5c7839cfe6a Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 9 Apr 2025 10:51:59 +0200
Subject: [PATCH] swapoff: clean up tag resolution
- Rename swapoff_resolve_tag() to resolve_swapfile_tag() and retain
only code relevant to swapfiles.
- Always call mnt_resolve_*() before resolve_swapfile_tag() to resolve
tags/paths on standard block devices.
- Call free() for resolve_swapfile_tag() to avoid memory leaks.
Signed-off-by: Karel Zak <kzak@redhat.com>
---
sys-utils/swapoff.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/sys-utils/swapoff.c b/sys-utils/swapoff.c
index 5677d87a152..ab7e4ca11e1 100644
--- a/sys-utils/swapoff.c
+++ b/sys-utils/swapoff.c
@@ -54,20 +54,13 @@ static int all;
* mnt_resolve_tag() and mnt_resolve_spec() works with system visible block
* devices only.
*/
-static char *swapoff_resolve_tag(const char *name, const char *value,
- struct libmnt_cache *cache)
+static char *resolve_swapfile_tag(const char *name, const char *value)
{
char *path;
struct libmnt_table *tb;
struct libmnt_iter *itr;
struct libmnt_fs *fs;
- /* this is usual case for block devices (and it's really fast as it uses
- * udev /dev/disk/by-* symlinks by default */
- path = mnt_resolve_tag(name, value, cache);
- if (path)
- return path;
-
/* try regular files from /proc/swaps */
tb = get_swaps();
if (!tb)
@@ -103,6 +96,7 @@ static char *swapoff_resolve_tag(const char *name, const char *value,
static int do_swapoff(const char *orig_special, int quiet, int canonic)
{
const char *special = orig_special;
+ char *buf = NULL;
int rc = SWAPOFF_EX_OK;
if (verbose)
@@ -113,12 +107,14 @@ static int do_swapoff(const char *orig_special, int quiet, int canonic)
special = mnt_resolve_spec(orig_special, mntcache);
if (!special && blkid_parse_tag_string(orig_special, &n, &v) == 0) {
- special = swapoff_resolve_tag(n, v, mntcache);
+ special = buf = resolve_swapfile_tag(n, v);
free(n);
free(v);
}
- if (!special)
- return cannot_find(orig_special);
+ if (!special) {
+ rc = cannot_find(orig_special);
+ goto done;
+ }
}
if (swapoff(special) == 0)
@@ -140,13 +136,25 @@ static int do_swapoff(const char *orig_special, int quiet, int canonic)
}
}
+done:
+ free(buf);
return rc;
}
static int swapoff_by(const char *name, const char *value, int quiet)
{
- const char *special = swapoff_resolve_tag(name, value, mntcache);
- return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value);
+ const char *special;
+ char *buf = NULL;
+ int rc;
+
+ special = mnt_resolve_tag(name, value, mntcache);
+ if (!special)
+ special = buf = resolve_swapfile_tag(name, value);
+
+ rc = special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value);
+ free(buf);
+
+ return rc;
}
static void __attribute__((__noreturn__)) usage(void)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。