Skip to content

Commit f9d94f7

Browse files
committed
samples/vfs: use shared header
Share some infrastructure between sample programs and fix a build failure that was reported. Reported-by: Sasha Levin <[email protected]> Link: https://lore.kernel.org/r/Z42UkSXx0MS9qZ9w@lappy Link: https://qa-reports.linaro.org/lkft/sashal-linus-next/build/v6.13-rc7-511-g109a8e0fa9d6/testrun/26809210/suite/build/test/gcc-8-allyesconfig/log Signed-off-by: Christian Brauner <[email protected]>
1 parent f79e6eb commit f9d94f7

File tree

3 files changed

+249
-93
lines changed

3 files changed

+249
-93
lines changed

samples/vfs/mountinfo.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
#define __SANE_USERSPACE_TYPES__
99
#include <stdio.h>
1010
#include <stdint.h>
11-
#include <sys/ioctl.h>
12-
#include <sys/syscall.h>
13-
#include <linux/pidfd.h>
14-
#include <linux/mount.h>
15-
#include <linux/nsfs.h>
1611
#include <unistd.h>
1712
#include <alloca.h>
1813
#include <getopt.h>
1914
#include <stdlib.h>
2015
#include <stdbool.h>
2116
#include <errno.h>
2217

18+
#include "samples-vfs.h"
19+
2320
/* max mounts per listmount call */
2421
#define MAXMOUNTS 1024
2522

@@ -28,6 +25,10 @@
2825

2926
static bool ext_format;
3027

28+
#ifndef __NR_pidfd_open
29+
#define __NR_pidfd_open -1
30+
#endif
31+
3132
/*
3233
* There are no bindings in glibc for listmount() and statmount() (yet),
3334
* make our own here.
@@ -232,7 +233,7 @@ int main(int argc, char * const *argv)
232233
}
233234

234235
/* Get a pidfd for pid */
235-
pidfd = syscall(SYS_pidfd_open, pid, 0);
236+
pidfd = syscall(__NR_pidfd_open, pid, 0);
236237
if (pidfd < 0) {
237238
perror("pidfd_open");
238239
return 1;

samples/vfs/samples-vfs.h

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef __SAMPLES_VFS_H
4+
#define __SAMPLES_VFS_H
5+
6+
#include <errno.h>
7+
#include <linux/types.h>
8+
#include <sys/ioctl.h>
9+
#include <sys/syscall.h>
10+
11+
#define die_errno(format, ...) \
12+
do { \
13+
fprintf(stderr, "%m | %s: %d: %s: " format "\n", __FILE__, \
14+
__LINE__, __func__, ##__VA_ARGS__); \
15+
exit(EXIT_FAILURE); \
16+
} while (0)
17+
18+
struct statmount {
19+
__u32 size; /* Total size, including strings */
20+
__u32 mnt_opts; /* [str] Options (comma separated, escaped) */
21+
__u64 mask; /* What results were written */
22+
__u32 sb_dev_major; /* Device ID */
23+
__u32 sb_dev_minor;
24+
__u64 sb_magic; /* ..._SUPER_MAGIC */
25+
__u32 sb_flags; /* SB_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */
26+
__u32 fs_type; /* [str] Filesystem type */
27+
__u64 mnt_id; /* Unique ID of mount */
28+
__u64 mnt_parent_id; /* Unique ID of parent (for root == mnt_id) */
29+
__u32 mnt_id_old; /* Reused IDs used in proc/.../mountinfo */
30+
__u32 mnt_parent_id_old;
31+
__u64 mnt_attr; /* MOUNT_ATTR_... */
32+
__u64 mnt_propagation; /* MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */
33+
__u64 mnt_peer_group; /* ID of shared peer group */
34+
__u64 mnt_master; /* Mount receives propagation from this ID */
35+
__u64 propagate_from; /* Propagation from in current namespace */
36+
__u32 mnt_root; /* [str] Root of mount relative to root of fs */
37+
__u32 mnt_point; /* [str] Mountpoint relative to current root */
38+
__u64 mnt_ns_id; /* ID of the mount namespace */
39+
__u32 fs_subtype; /* [str] Subtype of fs_type (if any) */
40+
__u32 sb_source; /* [str] Source string of the mount */
41+
__u32 opt_num; /* Number of fs options */
42+
__u32 opt_array; /* [str] Array of nul terminated fs options */
43+
__u32 opt_sec_num; /* Number of security options */
44+
__u32 opt_sec_array; /* [str] Array of nul terminated security options */
45+
__u64 __spare2[46];
46+
char str[]; /* Variable size part containing strings */
47+
};
48+
49+
struct mnt_id_req {
50+
__u32 size;
51+
__u32 spare;
52+
__u64 mnt_id;
53+
__u64 param;
54+
__u64 mnt_ns_id;
55+
};
56+
57+
#ifndef MNT_ID_REQ_SIZE_VER0
58+
#define MNT_ID_REQ_SIZE_VER0 24 /* sizeof first published struct */
59+
#endif
60+
61+
#ifndef MNT_ID_REQ_SIZE_VER1
62+
#define MNT_ID_REQ_SIZE_VER1 32 /* sizeof second published struct */
63+
#endif
64+
65+
/* Get the id for a mount namespace */
66+
#ifndef NS_GET_MNTNS_ID
67+
#define NS_GET_MNTNS_ID _IO(0xb7, 0x5)
68+
#endif
69+
70+
struct mnt_ns_info {
71+
__u32 size;
72+
__u32 nr_mounts;
73+
__u64 mnt_ns_id;
74+
};
75+
76+
#ifndef MNT_NS_INFO_SIZE_VER0
77+
#define MNT_NS_INFO_SIZE_VER0 16 /* size of first published struct */
78+
#endif
79+
80+
#ifndef NS_MNT_GET_INFO
81+
#define NS_MNT_GET_INFO _IOR(0xb7, 10, struct mnt_ns_info)
82+
#endif
83+
84+
#ifndef NS_MNT_GET_NEXT
85+
#define NS_MNT_GET_NEXT _IOR(0xb7, 11, struct mnt_ns_info)
86+
#endif
87+
88+
#ifndef NS_MNT_GET_PREV
89+
#define NS_MNT_GET_PREV _IOR(0xb7, 12, struct mnt_ns_info)
90+
#endif
91+
92+
#ifndef PIDFD_GET_MNT_NAMESPACE
93+
#define PIDFD_GET_MNT_NAMESPACE _IO(0xFF, 3)
94+
#endif
95+
96+
#ifndef __NR_listmount
97+
#define __NR_listmount 458
98+
#endif
99+
100+
#ifndef __NR_statmount
101+
#define __NR_statmount 457
102+
#endif
103+
104+
#ifndef LSMT_ROOT
105+
#define LSMT_ROOT 0xffffffffffffffff /* root mount */
106+
#endif
107+
108+
/* @mask bits for statmount(2) */
109+
#ifndef STATMOUNT_SB_BASIC
110+
#define STATMOUNT_SB_BASIC 0x00000001U /* Want/got sb_... */
111+
#endif
112+
113+
#ifndef STATMOUNT_MNT_BASIC
114+
#define STATMOUNT_MNT_BASIC 0x00000002U /* Want/got mnt_... */
115+
#endif
116+
117+
#ifndef STATMOUNT_PROPAGATE_FROM
118+
#define STATMOUNT_PROPAGATE_FROM 0x00000004U /* Want/got propagate_from */
119+
#endif
120+
121+
#ifndef STATMOUNT_MNT_ROOT
122+
#define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */
123+
#endif
124+
125+
#ifndef STATMOUNT_MNT_POINT
126+
#define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */
127+
#endif
128+
129+
#ifndef STATMOUNT_FS_TYPE
130+
#define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */
131+
#endif
132+
133+
#ifndef STATMOUNT_MNT_NS_ID
134+
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/got mnt_ns_id */
135+
#endif
136+
137+
#ifndef STATMOUNT_MNT_OPTS
138+
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/got mnt_opts */
139+
#endif
140+
141+
#ifndef STATMOUNT_FS_SUBTYPE
142+
#define STATMOUNT_FS_SUBTYPE 0x00000100U /* Want/got fs_subtype */
143+
#endif
144+
145+
#ifndef STATMOUNT_SB_SOURCE
146+
#define STATMOUNT_SB_SOURCE 0x00000200U /* Want/got sb_source */
147+
#endif
148+
149+
#ifndef STATMOUNT_OPT_ARRAY
150+
#define STATMOUNT_OPT_ARRAY 0x00000400U /* Want/got opt_... */
151+
#endif
152+
153+
#ifndef STATMOUNT_OPT_SEC_ARRAY
154+
#define STATMOUNT_OPT_SEC_ARRAY 0x00000800U /* Want/got opt_sec... */
155+
#endif
156+
157+
#ifndef STATX_MNT_ID_UNIQUE
158+
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
159+
#endif
160+
161+
#ifndef MOUNT_ATTR_RDONLY
162+
#define MOUNT_ATTR_RDONLY 0x00000001 /* Mount read-only */
163+
#endif
164+
165+
#ifndef MOUNT_ATTR_NOSUID
166+
#define MOUNT_ATTR_NOSUID 0x00000002 /* Ignore suid and sgid bits */
167+
#endif
168+
169+
#ifndef MOUNT_ATTR_NODEV
170+
#define MOUNT_ATTR_NODEV 0x00000004 /* Disallow access to device special files */
171+
#endif
172+
173+
#ifndef MOUNT_ATTR_NOEXEC
174+
#define MOUNT_ATTR_NOEXEC 0x00000008 /* Disallow program execution */
175+
#endif
176+
177+
#ifndef MOUNT_ATTR__ATIME
178+
#define MOUNT_ATTR__ATIME 0x00000070 /* Setting on how atime should be updated */
179+
#endif
180+
181+
#ifndef MOUNT_ATTR_RELATIME
182+
#define MOUNT_ATTR_RELATIME 0x00000000 /* - Update atime relative to mtime/ctime. */
183+
#endif
184+
185+
#ifndef MOUNT_ATTR_NOATIME
186+
#define MOUNT_ATTR_NOATIME 0x00000010 /* - Do not update access times. */
187+
#endif
188+
189+
#ifndef MOUNT_ATTR_STRICTATIME
190+
#define MOUNT_ATTR_STRICTATIME 0x00000020 /* - Always perform atime updates */
191+
#endif
192+
193+
#ifndef MOUNT_ATTR_NODIRATIME
194+
#define MOUNT_ATTR_NODIRATIME 0x00000080 /* Do not update directory access times */
195+
#endif
196+
197+
#ifndef MOUNT_ATTR_IDMAP
198+
#define MOUNT_ATTR_IDMAP 0x00100000 /* Idmap mount to @userns_fd in struct mount_attr. */
199+
#endif
200+
201+
#ifndef MOUNT_ATTR_NOSYMFOLLOW
202+
#define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */
203+
#endif
204+
205+
#ifndef MS_RDONLY
206+
#define MS_RDONLY 1 /* Mount read-only */
207+
#endif
208+
209+
#ifndef MS_SYNCHRONOUS
210+
#define MS_SYNCHRONOUS 16 /* Writes are synced at once */
211+
#endif
212+
213+
#ifndef MS_MANDLOCK
214+
#define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */
215+
#endif
216+
217+
#ifndef MS_DIRSYNC
218+
#define MS_DIRSYNC 128 /* Directory modifications are synchronous */
219+
#endif
220+
221+
#ifndef MS_UNBINDABLE
222+
#define MS_UNBINDABLE (1<<17) /* change to unbindable */
223+
#endif
224+
225+
#ifndef MS_PRIVATE
226+
#define MS_PRIVATE (1<<18) /* change to private */
227+
#endif
228+
229+
#ifndef MS_SLAVE
230+
#define MS_SLAVE (1<<19) /* change to slave */
231+
#endif
232+
233+
#ifndef MS_SHARED
234+
#define MS_SHARED (1<<20) /* change to shared */
235+
#endif
236+
237+
#ifndef MS_LAZYTIME
238+
#define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */
239+
#endif
240+
241+
#endif /* __SAMPLES_VFS_H */

samples/vfs/test-list-all-mounts.c

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,95 +6,9 @@
66
#include <limits.h>
77
#include <linux/types.h>
88
#include <stdio.h>
9-
#include <sys/ioctl.h>
10-
#include <sys/syscall.h>
119

1210
#include "../../tools/testing/selftests/pidfd/pidfd.h"
13-
14-
#define die_errno(format, ...) \
15-
do { \
16-
fprintf(stderr, "%m | %s: %d: %s: " format "\n", __FILE__, \
17-
__LINE__, __func__, ##__VA_ARGS__); \
18-
exit(EXIT_FAILURE); \
19-
} while (0)
20-
21-
/* Get the id for a mount namespace */
22-
#define NS_GET_MNTNS_ID _IO(0xb7, 0x5)
23-
/* Get next mount namespace. */
24-
25-
struct mnt_ns_info {
26-
__u32 size;
27-
__u32 nr_mounts;
28-
__u64 mnt_ns_id;
29-
};
30-
31-
#define MNT_NS_INFO_SIZE_VER0 16 /* size of first published struct */
32-
33-
/* Get information about namespace. */
34-
#define NS_MNT_GET_INFO _IOR(0xb7, 10, struct mnt_ns_info)
35-
/* Get next namespace. */
36-
#define NS_MNT_GET_NEXT _IOR(0xb7, 11, struct mnt_ns_info)
37-
/* Get previous namespace. */
38-
#define NS_MNT_GET_PREV _IOR(0xb7, 12, struct mnt_ns_info)
39-
40-
#define PIDFD_GET_MNT_NAMESPACE _IO(0xFF, 3)
41-
42-
#ifndef __NR_listmount
43-
#define __NR_listmount 458
44-
#endif
45-
46-
#ifndef __NR_statmount
47-
#define __NR_statmount 457
48-
#endif
49-
50-
/* @mask bits for statmount(2) */
51-
#define STATMOUNT_SB_BASIC 0x00000001U /* Want/got sb_... */
52-
#define STATMOUNT_MNT_BASIC 0x00000002U /* Want/got mnt_... */
53-
#define STATMOUNT_PROPAGATE_FROM 0x00000004U /* Want/got propagate_from */
54-
#define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */
55-
#define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */
56-
#define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */
57-
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/got mnt_ns_id */
58-
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/got mnt_opts */
59-
60-
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
61-
62-
struct statmount {
63-
__u32 size;
64-
__u32 mnt_opts;
65-
__u64 mask;
66-
__u32 sb_dev_major;
67-
__u32 sb_dev_minor;
68-
__u64 sb_magic;
69-
__u32 sb_flags;
70-
__u32 fs_type;
71-
__u64 mnt_id;
72-
__u64 mnt_parent_id;
73-
__u32 mnt_id_old;
74-
__u32 mnt_parent_id_old;
75-
__u64 mnt_attr;
76-
__u64 mnt_propagation;
77-
__u64 mnt_peer_group;
78-
__u64 mnt_master;
79-
__u64 propagate_from;
80-
__u32 mnt_root;
81-
__u32 mnt_point;
82-
__u64 mnt_ns_id;
83-
__u64 __spare2[49];
84-
char str[];
85-
};
86-
87-
struct mnt_id_req {
88-
__u32 size;
89-
__u32 spare;
90-
__u64 mnt_id;
91-
__u64 param;
92-
__u64 mnt_ns_id;
93-
};
94-
95-
#define MNT_ID_REQ_SIZE_VER1 32 /* sizeof second published struct */
96-
97-
#define LSMT_ROOT 0xffffffffffffffff /* root mount */
11+
#include "samples-vfs.h"
9812

9913
static int __statmount(__u64 mnt_id, __u64 mnt_ns_id, __u64 mask,
10014
struct statmount *stmnt, size_t bufsize,

0 commit comments

Comments
 (0)