[PATCH 1/4] fuse2fs: hook library error message printing

Darrick J. Wong
2025-11-06T22:40:24Z

Hook the com_err library so that error messages coming from libext2fs
such as:
Illegal block number passed to ext2fs_test_block_bitmap #9462 for block bitmap for /dev/sda

are actually printed with the standard "FUSE2FS (sda):" prefix.
Libraries shouldn't be printing that kind of stuff, but it is what it
is, and what it is is against the normal conventions.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
misc/fuse2fs.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index d6be5e9968567c..c2896de2316bce 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -201,6 +201,8 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)
errcode_t ext2fs_check_ext3_journal(ext2_filsys fs);
errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);

+const char *err_shortdev;
+
#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
int journal_enable_debug = -1;
#endif
@@ -5522,6 +5524,18 @@ static void try_adjust_oom_score(struct fuse2fs *ff)
fclose(fp);
}

+static void fuse2fs_com_err_proc(const char *whoami, errcode_t code,
+ const char *fmt, va_list args)
+{
+ fprintf(stderr, "FUSE2FS (%s): ", err_shortdev ? err_shortdev : "?");
+ if (whoami)
+ fprintf(stderr, "%s: ", whoami);
+ fprintf(stderr, "%s ", error_message(code));
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, "\n");
+ fflush(stderr);
+}
+
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
@@ -5551,6 +5565,10 @@ int main(int argc, char *argv[])
else
fctx.shortdev = fctx.device;

+ /* capture library error messages */
+ err_shortdev = fctx.shortdev;
+ set_com_err_hook(fuse2fs_com_err_proc);
+
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
@@ -5650,6 +5668,8 @@ int main(int argc, char *argv[])
}
fuse2fs_mmp_destroy(&fctx);
fuse2fs_unmount(&fctx);
+ reset_com_err_hook();
+ err_shortdev = NULL;
if (fctx.device)
free(fctx.device);
pthread_mutex_destroy(&fctx.bfl);