diff mbox series

[3/4] samples/vfs: check whether flag was raised

Message ID 20250130-work-mnt_idmap-statmount-v1-3-d4ced5874e14@kernel.org (mailing list archive)
State New
Headers show
Series statmount: allow to retrieve idmappings | expand

Commit Message

Christian Brauner Jan. 29, 2025, 11:19 p.m. UTC
For string options the kernel will raise the corresponding flag only if
the string isn't empty. So check for the flag.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 samples/vfs/test-list-all-mounts.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Miklos Szeredi Jan. 30, 2025, 8:45 a.m. UTC | #1
On Thu, 30 Jan 2025 at 00:20, Christian Brauner <brauner@kernel.org> wrote:
>
> For string options the kernel will raise the corresponding flag only if
> the string isn't empty. So check for the flag.

Hmm, seems like this is going to be a common mistake.

How about just putting a nul char at sm->str, which will solve this
once and for all (any unset str_ offset will point to this empty
string)?

Then we can say that instead of checking the mask for a string it's
okay to rely on it being empty by default.

Thanks,
Miklos
Christian Brauner Jan. 30, 2025, 3:17 p.m. UTC | #2
On Thu, Jan 30, 2025 at 09:45:50AM +0100, Miklos Szeredi wrote:
> On Thu, 30 Jan 2025 at 00:20, Christian Brauner <brauner@kernel.org> wrote:
> >
> > For string options the kernel will raise the corresponding flag only if
> > the string isn't empty. So check for the flag.
> 
> Hmm, seems like this is going to be a common mistake.

Probably.

> 
> How about just putting a nul char at sm->str, which will solve this
> once and for all (any unset str_ offset will point to this empty
> string)?

Agreed.

> 
> Then we can say that instead of checking the mask for a string it's
> okay to rely on it being empty by default.

Right, but I do prefer checking whether the flag is raised or not. But
we can just support both.
diff mbox series

Patch

diff --git a/samples/vfs/test-list-all-mounts.c b/samples/vfs/test-list-all-mounts.c
index 1a02ea4593e3..ce272ded8a79 100644
--- a/samples/vfs/test-list-all-mounts.c
+++ b/samples/vfs/test-list-all-mounts.c
@@ -138,10 +138,11 @@  int main(int argc, char *argv[])
 			printf("mnt_id:\t\t%" PRIu64 "\nmnt_parent_id:\t%" PRIu64 "\nfs_type:\t%s\nmnt_root:\t%s\nmnt_point:\t%s\nmnt_opts:\t%s\n\n",
 			       (uint64_t)stmnt->mnt_id,
 			       (uint64_t)stmnt->mnt_parent_id,
-			       stmnt->str + stmnt->fs_type,
-			       stmnt->str + stmnt->mnt_root,
-			       stmnt->str + stmnt->mnt_point,
-			       stmnt->str + stmnt->mnt_opts);
+			       (stmnt->mask & STATMOUNT_FS_TYPE)   ? stmnt->str + stmnt->fs_type   : "",
+			       (stmnt->mask & STATMOUNT_MNT_ROOT)  ? stmnt->str + stmnt->mnt_root  : "",
+			       (stmnt->mask & STATMOUNT_MNT_POINT) ? stmnt->str + stmnt->mnt_point : "",
+			       (stmnt->mask & STATMOUNT_MNT_OPTS)  ? stmnt->str + stmnt->mnt_opts  : "");
+
 			free(stmnt);
 		}
 	}