Merge tag 'for-linus-5.9-rc1b-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / samples / vfs / test-fsmount.c
blob50f47b72e85fbc8dd52dedad96ee96e6379da5b8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* fd-based mount test.
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
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>
18 #define E(x) do { if ((x) == -1) { perror(#x); exit(1); } } while(0)
20 static void check_messages(int fd)
22 char buf[4096];
23 int err, n;
25 err = errno;
27 for (;;) {
28 n = read(fd, buf, sizeof(buf));
29 if (n < 0)
30 break;
31 n -= 2;
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;
46 errno = err;
49 static __attribute__((noreturn))
50 void mount_error(int fd, const char *s)
52 check_messages(fd);
53 fprintf(stderr, "%s: %m\n", s);
54 exit(1);
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
72 static inline int fsopen(const char *fs_name, unsigned int flags)
74 return syscall(__NR_fsopen, fs_name, flags);
77 static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
79 return syscall(__NR_fsmount, fsfd, flags, ms_flags);
82 static inline int fsconfig(int fsfd, unsigned int cmd,
83 const char *key, const void *val, int aux)
85 return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux);
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)
92 return syscall(__NR_move_mount,
93 from_dfd, from_pathname,
94 to_dfd, to_pathname, flags);
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)
103 int main(int argc, char *argv[])
105 int fsfd, mfd;
107 /* Mount a publically available AFS filesystem */
108 fsfd = fsopen("afs", 0);
109 if (fsfd == -1) {
110 perror("fsopen");
111 exit(1);
114 E_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "#grand.central.org:root.cell.", 0);
115 E_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
117 mfd = fsmount(fsfd, 0, MOUNT_ATTR_RDONLY);
118 if (mfd < 0)
119 mount_error(fsfd, "fsmount");
120 E(close(fsfd));
122 if (move_mount(mfd, "", AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
123 perror("move_mount");
124 exit(1);
127 E(close(mfd));
128 exit(0);