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)
13 #include <sys/prctl.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)
28 n = read(fd, buf, sizeof(buf));
35 fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2);
38 fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2);
41 fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2);
49 static __attribute__((noreturn))
50 void mount_error(int fd, const char *s)
53 fprintf(stderr, "%s: %m\n", s);
57 /* Hope -1 isn't a syscall */
59 #define __NR_fsopen -1
62 #define __NR_fsmount -1
65 #define __NR_fsconfig -1
67 #ifndef __NR_move_mount
68 #define __NR_move_mount -1
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,
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) \
99 if (fsconfig(fd, cmd, key, val, aux) == -1) \
100 mount_error(fd, key ?: "create"); \
103 int main(int argc, char *argv[])
107 /* Mount a publically available AFS filesystem */
108 fsfd = fsopen("afs", 0);
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);
119 mount_error(fsfd, "fsmount");
122 if (move_mount(mfd, "", AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
123 perror("move_mount");