]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
mount_setattr.2: Minor clean-ups in example program
authorMichael Kerrisk <mtk.manpages@gmail.com>
Mon, 9 Aug 2021 20:56:47 +0000 (22:56 +0200)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Tue, 10 Aug 2021 01:29:39 +0000 (03:29 +0200)
- 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>
man2/mount_setattr.2

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