mount_setattr.2: EXAMPLES: use -1 rather than -EBADF

From email with Christian Braner:

> [1]: In this code "source" is expected to be absolute. If it's not
>      absolute we should fail. This can be achieved by passing -1/-EBADF,
>      afaict.
D'oh! Okay. I hadn't considered that use case for an invalid dirfd.
(And now I've done some adjustments to openat(2),which contains a
rationale for the *at() functions.)

So, now I understand your purpose, but still the code is obscure,
since

* You use a magic value (-EBADF) rather than (say) -1.
* There's no explanation (comment about) of the fact that you want
  to prevent relative pathnames.

So, I've changed the code to use -1, not -EBADF, and I've added some
comments to explain that the intent is to prevent relative pathnames.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2021-08-12 05:16:42 +02:00
parent faf2534942
commit 45ea537cf2
1 changed files with 8 additions and 2 deletions

View File

@ -961,7 +961,10 @@ main(int argc, char *argv[])
const char *source = argv[optind];
const char *target = argv[optind + 1];
int fd_tree = open_tree(\-EBADF, source,
/* In the following, \-1 as the \(aqdirfd\(aq argument ensures that
open_tree() fails if \(aqsource\(aq is not an absolute pathname. */
int fd_tree = open_tree(\-1, source,
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC |
AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0));
if (fd_tree == \-1)
@ -980,7 +983,10 @@ main(int argc, char *argv[])
close(fd_userns);
ret = move_mount(fd_tree, "", \-EBADF, target,
/* In the following, \-1 as the \(aqto_dirfd\(aq argument ensures that
open_tree() fails if \(aqtarget\(aq is not an absolute pathname. */
ret = move_mount(fd_tree, "", \-1, target,
MOVE_MOUNT_F_EMPTY_PATH);
if (ret == \-1)
exit_log("%m \- Failed to attach mount to %s\en", target);