Skip to contentSkip to conversation

SAFE_MOUNT: print debug info about mounting operation

PATCH Superseded Murphy Zhou 3 participants 5 messages Jun 4, 2023 – Jun 6, 2023 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SAFE_MOUNT: print debug info about mounting operation

---
 lib/safe_macros.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
lib/safe_macros.c +17 -0
index af6dd0716..a66285a0e 100644
@@ -898,7 +898,24 @@ int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
 	       const void *data)
 {
 	int rval = -1;
+	char cdir[PATH_MAX], mpath[PATH_MAX];
 
+	if (!getcwd(cdir, PATH_MAX)) {
+		tst_resm(TWARN | TERRNO, "Failed to find current directory");
+		return 0;
+	}
+
+	rval = snprintf(mpath, PATH_MAX, "%s/%s", cdir, target);
+	if (rval < 0 || rval >= PATH_MAX) {
+		tst_resm(TWARN | TERRNO,
+			 "snprintf() should have returned %d instead of %d",
+			 PATH_MAX, rval);
+		return 0;
+	}
+
+	tst_resm_(file, lineno, TINFO,
+		"Mounting %s to %s fstyp=%s flags=%x",
+		source, mpath, filesystemtype, mountflags);
 	/*
 	 * Don't try using the kernel's NTFS driver when mounting NTFS, since
 	 * the kernel's NTFS driver doesn't have proper write support.
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Murphy,

This is helpful for knowing the mount info.

But as SAFE_MOUNT is not only used for relative path but mount absolute path, so we'd better determine what the '*target' path type.

How about changing to below:

       if (target[0] == '/') {
               strncpy(mpath, target, PATH_MAX-1);
               mpath[PATH_MAX-1] = '\0';
       } else {
               .... your relative path code ...
       }

       tst_resm_(file, lineno, TINFO,
               "Mounting %s to %s fstyp=%s flags=%x",
               source, mpath, filesystemtype, mountflags);
Quoted 21 lines · Murphy Zhou
---
 lib/safe_macros.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
lib/safe_macros.c +5 -0
index af6dd0716..a66285a0e 100644
@@ -898,7 +898,24 @@ int safe_mount(const char *file, const int lineno,
 void (*cleanup_fn)(void),
               const void *data)
 {
        int rval = -1;
+       char cdir[PATH_MAX], mpath[PATH_MAX];
 
+       if (!getcwd(cdir, PATH_MAX)) {
+               tst_resm(TWARN | TERRNO, "Failed to find current
 directory");
+               return 0;
+       }

lib/safe_macros.c int safe_mount(const char *file, const int lineno, · line 918

Quoted 10 lines
lib/safe_macros.c +9 -0
+
+       rval = snprintf(mpath, PATH_MAX, "%s/%s", cdir, target);
+       if (rval < 0 || rval >= PATH_MAX) {
+               tst_resm(TWARN | TERRNO,
+                        "snprintf() should have returned %d instead of
 %d",
+                        PATH_MAX, rval);
+               return 0;
+       }
+

+ tst_resm_(file, lineno, TINFO,

Quoted 14 lines
lib/safe_macros.c +2 -0
+               "Mounting %s to %s fstyp=%s flags=%x",
+               source, mpath, filesystemtype, mountflags);
        /*
         * Don't try using the kernel's NTFS driver when mounting NTFS,

since
* the kernel's NTFS driver doesn't have proper write support.
--
2.31.1

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Murphy Zhou

Hi,

Quoted 10 lines · Murphy Zhou
---
  lib/safe_macros.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
lib/safe_macros.c +0 -0
index af6dd0716..a66285a0e 100644

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 905

Quoted 8 lines
lib/safe_macros.c +4 -0
@@ -898,7 +898,24 @@ int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
  	       const void *data)
  {
  	int rval = -1;
+	char cdir[PATH_MAX], mpath[PATH_MAX];
 
+	if (!getcwd(cdir, PATH_MAX)) {
+		tst_resm(TWARN | TERRNO, "Failed to find current directory");
+		return 0;

You return success when nothing was mounted. That is clearly wrong. Either call tst_brkm_(... TBROK ...) if the failure is so serious that the test cannot continue, or don't return at all.

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 908

Quoted 3 lines
lib/safe_macros.c +3 -0
+	}
+
+	rval = snprintf(mpath, PATH_MAX, "%s/%s", cdir, target);

The C library has a function for resolving paths: realpath(). Please use that.

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 913

Quoted 5 lines
lib/safe_macros.c +5 -0
+	if (rval < 0 || rval >= PATH_MAX) {
+		tst_resm(TWARN | TERRNO,
+			 "snprintf() should have returned %d instead of %d",
+			 PATH_MAX, rval);
+		return 0;

Returning here is also wrong.

Quoted 8 lines
lib/safe_macros.c +5 -0
+	}
+
+	tst_resm_(file, lineno, TINFO,
+		"Mounting %s to %s fstyp=%s flags=%x",
+		source, mpath, filesystemtype, mountflags);
  	/*
  	 * Don't try using the kernel's NTFS driver when mounting NTFS, since
  	 * the kernel's NTFS driver doesn't have proper write support.
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 28 lines · Martin Doucha

Hi,

lib/safe_macros.c int safe_mount(const char *file, const int lineno,

Quoted 10 lines · Murphy Zhou
---
  lib/safe_macros.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
lib/safe_macros.c +0 -0
index af6dd0716..a66285a0e 100644
@@ -898,7 +898,24 @@ int safe_mount(const char *file, const int lineno,

void (*cleanup_fn)(void),

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),

Quoted 7 lines
lib/safe_macros.c +3 -0
             const void *data)
  {
      int rval = -1;
+     char cdir[PATH_MAX], mpath[PATH_MAX];
 
+     if (!getcwd(cdir, PATH_MAX)) {
+             tst_resm(TWARN | TERRNO, "Failed to find current

directory");

lib/safe_macros.c int safe_mount(const char *file, const int lineno, · line 905

Quoted 1 line
lib/safe_macros.c +1 -0
+             return 0;

You return success when nothing was mounted. That is clearly wrong. Either call tst_brkm_(... TBROK ...) if the failure is so serious that the test cannot continue, or don't return at all.

Quoted 7 lines

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 908

Quoted 3 lines
lib/safe_macros.c +3 -0
+     }
+
+     rval = snprintf(mpath, PATH_MAX, "%s/%s", cdir, target);

The C library has a function for resolving paths: realpath(). Please use that.

+1, good point. realpath() is more convenient, we don't need to additionally copy string to the absolute path.

@Murphy, ignore my previous comments plz.

Quoted 34 lines

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),

Quoted 4 lines
lib/safe_macros.c +3 -0
+     if (rval < 0 || rval >= PATH_MAX) {
+             tst_resm(TWARN | TERRNO,
+                      "snprintf() should have returned %d instead of

%d",

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 913

Quoted 2 lines
lib/safe_macros.c +2 -0
+                      PATH_MAX, rval);
+             return 0;

Returning here is also wrong.

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),

Quoted 7 lines
lib/safe_macros.c +5 -0
+     }
+
+     tst_resm_(file, lineno, TINFO,
+             "Mounting %s to %s fstyp=%s flags=%x",
+             source, mpath, filesystemtype, mountflags);
      /*
       * Don't try using the kernel's NTFS driver when mounting NTFS,

since

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 921

Quoted 1 line
lib/safe_macros.c +0 -0
       * the kernel's NTFS driver doesn't have proper write support.

--
Martin Doucha [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 40 lines · Li Wang
Quoted 27 lines · Martin Doucha

Hi,

Quoted 10 lines · Murphy Zhou
---
  lib/safe_macros.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
lib/safe_macros.c +0 -0
index af6dd0716..a66285a0e 100644

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 905

Quoted 8 lines
lib/safe_macros.c +4 -0
@@ -898,7 +898,24 @@ int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
             const void *data)
  {
      int rval = -1;
+     char cdir[PATH_MAX], mpath[PATH_MAX];
 
+     if (!getcwd(cdir, PATH_MAX)) {
+             tst_resm(TWARN | TERRNO, "Failed to find current directory");
+             return 0;

You return success when nothing was mounted. That is clearly wrong. Either call tst_brkm_(... TBROK ...) if the failure is so serious that the test cannot continue, or don't return at all.

Quoted 6 lines

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 908

Quoted 3 lines
lib/safe_macros.c +3 -0
+     }
+
+     rval = snprintf(mpath, PATH_MAX, "%s/%s", cdir, target);

The C library has a function for resolving paths: realpath(). Please use that.

Quoted 4 lines

+1, good point. realpath() is more convenient, we don't need to additionally copy string to the absolute path.

@Murphy, ignore my previous comments plz.

All very good catches! Thanks for reviewing!

Cooking V2.

Murphy

Quoted 39 lines
Quoted 31 lines

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 913

Quoted 9 lines
lib/safe_macros.c +5 -0
+     if (rval < 0 || rval >= PATH_MAX) {
+             tst_resm(TWARN | TERRNO,
+                      "snprintf() should have returned %d instead of %d",
+                      PATH_MAX, rval);
+             return 0;

Returning here is also wrong.

lib/safe_macros.c int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), · line 921

Quoted 8 lines
lib/safe_macros.c +5 -0
+     }
+
+     tst_resm_(file, lineno, TINFO,
+             "Mounting %s to %s fstyp=%s flags=%x",
+             source, mpath, filesystemtype, mountflags);
      /*
       * Don't try using the kernel's NTFS driver when mounting NTFS, since
       * the kernel's NTFS driver doesn't have proper write support.

--
Martin Doucha [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

Quoted 3 lines

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Regards,
Li Wang

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp