~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/vfs/test-fsmount.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /* fd-based mount test.
  3  *
  4  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
  5  * Written by David Howells (dhowells@redhat.com)
  6  */
  7 
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <unistd.h>
 11 #include <errno.h>
 12 #include <fcntl.h>
 13 #include <sys/prctl.h>
 14 #include <sys/wait.h>
 15 #include <linux/mount.h>
 16 #include <linux/unistd.h>
 17 
 18 #define E(x) do { if ((x) == -1) { perror(#x); exit(1); } } while(0)
 19 
 20 static void check_messages(int fd)
 21 {
 22         char buf[4096];
 23         int err, n;
 24 
 25         err = errno;
 26 
 27         for (;;) {
 28                 n = read(fd, buf, sizeof(buf));
 29                 if (n < 0)
 30                         break;
 31                 n -= 2;
 32 
 33                 switch (buf[0]) {
 34                 case 'e':
 35                         fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2);
 36                         break;
 37                 case 'w':
 38                         fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2);
 39                         break;
 40                 case 'i':
 41                         fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2);
 42                         break;
 43                 }
 44         }
 45 
 46         errno = err;
 47 }
 48 
 49 static __attribute__((noreturn))
 50 void mount_error(int fd, const char *s)
 51 {
 52         check_messages(fd);
 53         fprintf(stderr, "%s: %m\n", s);
 54         exit(1);
 55 }
 56 
 57 /* Hope -1 isn't a syscall */
 58 #ifndef __NR_fsopen
 59 #define __NR_fsopen -1
 60 #endif
 61 #ifndef __NR_fsmount
 62 #define __NR_fsmount -1
 63 #endif
 64 #ifndef __NR_fsconfig
 65 #define __NR_fsconfig -1
 66 #endif
 67 #ifndef __NR_move_mount
 68 #define __NR_move_mount -1
 69 #endif
 70 
 71 
 72 static inline int fsopen(const char *fs_name, unsigned int flags)
 73 {
 74         return syscall(__NR_fsopen, fs_name, flags);
 75 }
 76 
 77 static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
 78 {
 79         return syscall(__NR_fsmount, fsfd, flags, ms_flags);
 80 }
 81 
 82 static inline int fsconfig(int fsfd, unsigned int cmd,
 83                            const char *key, const void *val, int aux)
 84 {
 85         return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux);
 86 }
 87 
 88 static inline int move_mount(int from_dfd, const char *from_pathname,
 89                              int to_dfd, const char *to_pathname,
 90                              unsigned int flags)
 91 {
 92         return syscall(__NR_move_mount,
 93                        from_dfd, from_pathname,
 94                        to_dfd, to_pathname, flags);
 95 }
 96 
 97 #define E_fsconfig(fd, cmd, key, val, aux)                              \
 98         do {                                                            \
 99                 if (fsconfig(fd, cmd, key, val, aux) == -1)             \
100                         mount_error(fd, key ?: "create");               \
101         } while (0)
102 
103 int main(int argc, char *argv[])
104 {
105         int fsfd, mfd;
106 
107         /* Mount a publically available AFS filesystem */
108         fsfd = fsopen("afs", 0);
109         if (fsfd == -1) {
110                 perror("fsopen");
111                 exit(1);
112         }
113 
114         E_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "#grand.central.org:root.cell.", 0);
115         E_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
116 
117         mfd = fsmount(fsfd, 0, MOUNT_ATTR_RDONLY);
118         if (mfd < 0)
119                 mount_error(fsfd, "fsmount");
120         E(close(fsfd));
121 
122         if (move_mount(mfd, "", AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
123                 perror("move_mount");
124                 exit(1);
125         }
126 
127         E(close(mfd));
128         exit(0);
129 }
130 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php