mount_setattr.2: Minor clean-ups in example program

- Change some instances of "-" to "\"
- Use C99 style (declare variables nearer use in code)
- Add a bit of white space
- Remove one 'const...const' added by Alex that caused
  compiler warnings
- Use "reverse Christmas tree" form for declarations in main()
- Other minor changes

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2021-08-09 22:56:47 +02:00
parent 8c67481023
commit f606879ab1
1 changed files with 33 additions and 35 deletions

View File

@ -629,7 +629,7 @@ More than one of
.BR MS_PRIVATE , .BR MS_PRIVATE ,
or or
.B MS_UNBINDABLE .B MS_UNBINDABLE
was set in was set in the
.I propagation .I propagation
field of field of
.IR mount_attr . .IR mount_attr .
@ -880,8 +880,7 @@ mount_setattr(int dfd, const char *path, unsigned int flags,
} }
static inline int static inline int
open_tree(int dfd, const char *filename, open_tree(int dfd, const char *filename, unsigned int flags)
unsigned int flags)
{ {
return syscall(SYS_open_tree, dfd, filename, flags); return syscall(SYS_open_tree, dfd, filename, flags);
} }
@ -895,13 +894,13 @@ move_mount(int from_dfd, const char *from_pathname,
} }
static const struct option longopts[] = { static const struct option longopts[] = {
{"map-mount", required_argument, NULL, 'a'}, {"map\-mount", required_argument, NULL, 'a'},
{"recursive", no_argument, NULL, 'b'}, {"recursive", no_argument, NULL, 'b'},
{"read-only", no_argument, NULL, 'c'}, {"read\-only", no_argument, NULL, 'c'},
{"block-setid", no_argument, NULL, 'd'}, {"block\-setid", no_argument, NULL, 'd'},
{"block-devices", no_argument, NULL, 'e'}, {"block\-devices", no_argument, NULL, 'e'},
{"block-exec", no_argument, NULL, 'f'}, {"block\-exec", no_argument, NULL, 'f'},
{"no-access-time", no_argument, NULL, 'g'}, {"no\-access\-time", no_argument, NULL, 'g'},
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
@ -914,13 +913,11 @@ static const struct option longopts[] = {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd_userns = \-EBADF;
int index = 0;
bool recursive = false;
struct mount_attr *attr = &(struct mount_attr){}; struct mount_attr *attr = &(struct mount_attr){};
const char *source, *target; int fd_userns = \-EBADF;
int fd_tree, new_argc, ret; bool recursive = false;
const char *const *new_argv; int index = 0;
int ret;
while ((ret = getopt_long_only(argc, argv, "", while ((ret = getopt_long_only(argc, argv, "",
longopts, &index)) != \-1) { longopts, &index)) != \-1) {
@ -934,57 +931,58 @@ main(int argc, char *argv[])
recursive = true; recursive = true;
break; break;
case 'c': case 'c':
attr->attr_set |= MOUNT_ATTR_RDONLY; attr\->attr_set |= MOUNT_ATTR_RDONLY;
break; break;
case 'd': case 'd':
attr->attr_set |= MOUNT_ATTR_NOSUID; attr\->attr_set |= MOUNT_ATTR_NOSUID;
break; break;
case 'e': case 'e':
attr->attr_set |= MOUNT_ATTR_NODEV; attr\->attr_set |= MOUNT_ATTR_NODEV;
break; break;
case 'f': case 'f':
attr->attr_set |= MOUNT_ATTR_NOEXEC; attr\->attr_set |= MOUNT_ATTR_NOEXEC;
break; break;
case 'g': case 'g':
attr->attr_set |= MOUNT_ATTR_NOATIME; attr\->attr_set |= MOUNT_ATTR_NOATIME;
attr->attr_clr |= MOUNT_ATTR__ATIME; attr\->attr_clr |= MOUNT_ATTR__ATIME;
break; break;
default: default:
exit_log("Invalid argument specified"); exit_log("Invalid argument specified");
} }
} }
new_argv = &argv[optind]; char **new_argv = &argv[optind];
new_argc = argc \- optind; int new_argc = argc \- optind;
if (new_argc < 2) if (new_argc < 2)
exit_log("Missing source or target mount point\en"); exit_log("Missing source or target mount point\en");
source = new_argv[0];
target = new_argv[1];
fd_tree = open_tree(\-EBADF, source, const char *source = new_argv[0];
OPEN_TREE_CLONE | const char *target = new_argv[1];
OPEN_TREE_CLOEXEC |
AT_EMPTY_PATH | int fd_tree = open_tree(\-EBADF, source,
(recursive ? AT_RECURSIVE : 0)); OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC |
AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0));
if (fd_tree == \-1) if (fd_tree == \-1)
exit_log("%m \- Failed to open %s\en", source); exit_log("%m \- Failed to open %s\en", source);
if (fd_userns >= 0) { if (fd_userns >= 0) {
attr->attr_set |= MOUNT_ATTR_IDMAP; attr\->attr_set |= MOUNT_ATTR_IDMAP;
attr->userns_fd = fd_userns; attr\->userns_fd = fd_userns;
} }
ret = mount_setattr(fd_tree, "", ret = mount_setattr(fd_tree, "",
AT_EMPTY_PATH | AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0),
(recursive ? AT_RECURSIVE : 0),
attr, sizeof(struct mount_attr)); attr, sizeof(struct mount_attr));
if (ret == \-1) if (ret == \-1)
exit_log("%m - Failed to change mount attributes\en"); exit_log("%m \- Failed to change mount attributes\en");
close(fd_userns); close(fd_userns);
ret = move_mount(fd_tree, "", \-EBADF, target, ret = move_mount(fd_tree, "", \-EBADF, target,
MOVE_MOUNT_F_EMPTY_PATH); MOVE_MOUNT_F_EMPTY_PATH);
if (ret == \-1) if (ret == \-1)
exit_log("%m \- Failed to attach mount to %s\en", target); exit_log("%m \- Failed to attach mount to %s\en", target);
close(fd_tree); close(fd_tree);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);