]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add functions to use error buffer
authorKarel Zak <kzak@redhat.com>
Wed, 22 May 2024 08:52:04 +0000 (10:52 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 22 May 2024 08:52:04 +0000 (10:52 +0200)
* Add mnt_context_set_errmsg() and mnt_context_append_errmsg() functions.
* Replace custom code with mnt_context_syscall_save_status() function.
* Optionally use syscall name when generating error message.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context.c
libmount/src/context_mount.c
libmount/src/hook_mount.c
libmount/src/hook_mount_legacy.c
libmount/src/mountP.h

index ef5da32a0de12f4c2350b373c865d6ff2df3535f..7cf4ef79edc6bc9b48b254fe9114b404c343e292 100644 (file)
@@ -2635,8 +2635,35 @@ void mnt_context_syscall_reset_status(struct libmnt_context *cxt)
        cxt->syscall_status = 0;
        cxt->syscall_name = NULL;
 
-       free(cxt->syscall_errmsg);
-       cxt->syscall_errmsg = NULL;
+       free(cxt->errmsg);
+       cxt->errmsg = NULL;
+}
+
+int mnt_context_set_errmsg(struct libmnt_context *cxt, const char *msg)
+{
+       char *p = NULL;
+
+       if (msg) {
+               p = strdup(msg);
+               if (!p)
+                       return -ENOMEM;
+       }
+
+       free(cxt->errmsg);
+       cxt->errmsg = p;
+
+       return 0;
+}
+
+int mnt_context_append_errmsg(struct libmnt_context *cxt, const char *msg)
+{
+       if (cxt->errmsg) {
+               int rc = strappend(&cxt->errmsg, "; ");
+               if (rc)
+                       return rc;
+       }
+
+       return strappend(&cxt->errmsg, msg);
 }
 
 /**
index f9d610f77840526c4c231c123144c4add6031212..783244a61c37051ed66bf6c8f11697d3301a0192 100644 (file)
@@ -1572,10 +1572,13 @@ int mnt_context_get_mount_excode(
         */
        syserr = mnt_context_get_syscall_errno(cxt);
 
-       if (buf && cxt->syscall_errmsg) {
-               snprintf(buf, bufsz, _("%s system call failed: %s"),
-                                       cxt->syscall_name ? : "mount",
-                                       cxt->syscall_errmsg);
+       if (buf && cxt->errmsg) {
+               if (cxt->syscall_name)
+                       snprintf(buf, bufsz, _("%s system call failed: %s"),
+                                       cxt->syscall_name, cxt->errmsg);
+               else
+                       xstrncpy(buf, cxt->errmsg, bufsz);
+
                return MNT_EX_FAIL;
        }
 
index c85c92b983dd10551a63b59bff902cb6b45f8c95..6b7caff853003189bbe57d6e940f38405113c184 100644 (file)
@@ -70,8 +70,7 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd)
        uint8_t buf[BUFSIZ];
        int rc;
 
-       free(cxt->syscall_errmsg);
-       cxt->syscall_errmsg = NULL;
+       mnt_context_set_errmsg(cxt, NULL);
 
        while ((rc = read(fd, buf, sizeof(buf))) != -1) {
                if (rc > 0 && buf[rc - 1] == '\n')
@@ -80,10 +79,7 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd)
 
                if (rc < 3 || strncmp((char *) buf, "e ", 2) != 0)
                        continue;
-               if (cxt->syscall_errmsg)
-                       strappend(&cxt->syscall_errmsg, "; ");
-
-               strappend(&cxt->syscall_errmsg, ((char *) buf) + 2);
+               mnt_context_append_errmsg(cxt, ((char *) buf) + 2);
        }
 }
 
index 7e62864673a46584607475536e69b160e71bcf79..df3a691e0dde249bb333d344c33e769cf842e3c6 100644 (file)
@@ -76,10 +76,8 @@ static int hook_propagation(struct libmnt_context *cxt,
 
        if (rc) {
                /* Update global syscall status if only this function called */
-               if (mnt_context_propagation_only(cxt)) {
-                       cxt->syscall_status = -errno;
-                       cxt->syscall_name = "mount";
-               }
+               if (mnt_context_propagation_only(cxt))
+                       mnt_context_syscall_save_status(cxt, "mount", rc == 0);
 
                DBG(HOOK, ul_debugobj(hs, "  mount(2) failed [errno=%d %m]", errno));
                rc = -MNT_ERR_APPLYFLAGS;
@@ -240,10 +238,7 @@ static int hook_mount(struct libmnt_context *cxt,
                          options : "<none>"));
 
        if (mount(src, target, type, flags, options)) {
-               cxt->syscall_status = -errno;
-               cxt->syscall_name = "mount";
-               DBG(HOOK, ul_debugobj(hs, "  mount(2) failed [errno=%d %m]",
-                                       -cxt->syscall_status));
+               mnt_context_syscall_save_status(cxt, "mount", 0);
                rc = -cxt->syscall_status;
                return rc;
        }
index 63a9675bbd7fee8524ccec2edd70ef66e4919a2a..db58e9f749998c1ba9f6007580d22ddd0896013f 100644 (file)
@@ -432,7 +432,7 @@ struct libmnt_context
 
        int     syscall_status; /* 1: not called yet, 0: success, <0: -errno */
        const char *syscall_name;       /* failed syscall name */
-       char    *syscall_errmsg;        /* message from kernel */
+       char    *errmsg;        /* library or kernel message */
 
        struct libmnt_ns        ns_orig;        /* original namespace */
        struct libmnt_ns        ns_tgt;         /* target namespace */
@@ -626,6 +626,9 @@ extern int mnt_context_update_tabs(struct libmnt_context *cxt);
 extern int mnt_context_umount_setopt(struct libmnt_context *cxt, int c, char *arg);
 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_propagation_only(struct libmnt_context *cxt)
                        __attribute__((nonnull));