Skip to content

Commit

Permalink
libmount: add mnt_context_sprintf_errmsg()
Browse files Browse the repository at this point in the history
Let's make it easy to create error message and return the message to
applications also when not generated by a syscall (kernel).

Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed May 22, 2024
1 parent c402357 commit 41476dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions libmount/src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "namespace.h"
#include "match.h"

#include <stdarg.h>
#include <sys/wait.h>

/**
Expand Down Expand Up @@ -2666,6 +2667,25 @@ int mnt_context_append_errmsg(struct libmnt_context *cxt, const char *msg)
return strappend(&cxt->errmsg, msg);
}

int mnt_context_sprintf_errmsg(struct libmnt_context *cxt, const char *msg, ...)
{
int rc;
va_list ap;
char *p = NULL;

va_start(ap, msg);
rc = vasprintf(&p, msg, ap);
va_end(ap);

if (rc < 0 || !p)
return rc;

free(cxt->errmsg);
cxt->errmsg = p;

return 0;
}

/**
* mnt_context_strerror
* @cxt: context
Expand Down
5 changes: 5 additions & 0 deletions libmount/src/context_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,11 @@ int mnt_context_get_mount_excode(
mnt_context_get_user_mflags(cxt, &uflags); /* userspace flags */

if (!mnt_context_syscall_called(cxt)) {
if (buf && cxt->errmsg) {
xstrncpy(buf, cxt->errmsg, bufsz);
return MNT_EX_USAGE;
}

/*
* libmount errors (extra library checks)
*/
Expand Down
1 change: 1 addition & 0 deletions libmount/src/mountP.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ extern int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg

extern int mnt_context_set_errmsg(struct libmnt_context *cxt, const char *msg);
extern int mnt_context_append_errmsg(struct libmnt_context *cxt, const char *msg);
extern int mnt_context_sprintf_errmsg(struct libmnt_context *cxt, const char *msg, ...);

extern int mnt_context_propagation_only(struct libmnt_context *cxt)
__attribute__((nonnull));
Expand Down

0 comments on commit 41476dd

Please sign in to comment.