blob: 50f47b72e85fbc8dd52dedad96ee96e6379da5b8 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsf1b56182018-11-01 23:36:32 +00002/* fd-based mount test.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsf1b56182018-11-01 23:36:32 +00006 */
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
20static 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
49static __attribute__((noreturn))
50void 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
72static inline int fsopen(const char *fs_name, unsigned int flags)
73{
74 return syscall(__NR_fsopen, fs_name, flags);
75}
76
77static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
78{
79 return syscall(__NR_fsmount, fsfd, flags, ms_flags);
80}
81
82static 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
88static 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
103int 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}