Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
/test-fsmount
/test-list-all-mounts
/test-statx
/mountinfo
+5
View File
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
userprogs-always-y += test-fsmount test-statx mountinfo test-list-all-mounts
userccflags += -I $(srctree)/tools/testing/selftests/
userccflags += -I usr/include
+274
View File
@@ -0,0 +1,274 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Use pidfds, nsfds, listmount() and statmount() mimic the
* contents of /proc/self/mountinfo.
*/
#define _GNU_SOURCE
#define __SANE_USERSPACE_TYPES__
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <alloca.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include "samples-vfs.h"
/* max mounts per listmount call */
#define MAXMOUNTS 1024
/* size of struct statmount (including trailing string buffer) */
#define STATMOUNT_BUFSIZE 4096
static bool ext_format;
#ifndef __NR_pidfd_open
#define __NR_pidfd_open -1
#endif
/*
* There are no bindings in glibc for listmount() and statmount() (yet),
* make our own here.
*/
static int statmount(__u64 mnt_id, __u64 mnt_ns_id, __u64 mask,
struct statmount *buf, size_t bufsize,
unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = mask,
};
if (mnt_ns_id) {
req.size = MNT_ID_REQ_SIZE_VER1;
req.mnt_ns_id = mnt_ns_id;
}
return syscall(__NR_statmount, &req, buf, bufsize, flags);
}
static ssize_t listmount(__u64 mnt_id, __u64 mnt_ns_id, __u64 last_mnt_id,
__u64 list[], size_t num, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = last_mnt_id,
};
if (mnt_ns_id) {
req.size = MNT_ID_REQ_SIZE_VER1;
req.mnt_ns_id = mnt_ns_id;
}
return syscall(__NR_listmount, &req, list, num, flags);
}
static void show_mnt_attrs(__u64 flags)
{
printf("%s", flags & MOUNT_ATTR_RDONLY ? "ro" : "rw");
if (flags & MOUNT_ATTR_NOSUID)
printf(",nosuid");
if (flags & MOUNT_ATTR_NODEV)
printf(",nodev");
if (flags & MOUNT_ATTR_NOEXEC)
printf(",noexec");
switch (flags & MOUNT_ATTR__ATIME) {
case MOUNT_ATTR_RELATIME:
printf(",relatime");
break;
case MOUNT_ATTR_NOATIME:
printf(",noatime");
break;
case MOUNT_ATTR_STRICTATIME:
/* print nothing */
break;
}
if (flags & MOUNT_ATTR_NODIRATIME)
printf(",nodiratime");
if (flags & MOUNT_ATTR_NOSYMFOLLOW)
printf(",nosymfollow");
if (flags & MOUNT_ATTR_IDMAP)
printf(",idmapped");
}
static void show_propagation(struct statmount *sm)
{
if (sm->mnt_propagation & MS_SHARED)
printf(" shared:%llu", sm->mnt_peer_group);
if (sm->mnt_propagation & MS_SLAVE) {
printf(" master:%llu", sm->mnt_master);
if (sm->propagate_from && sm->propagate_from != sm->mnt_master)
printf(" propagate_from:%llu", sm->propagate_from);
}
if (sm->mnt_propagation & MS_UNBINDABLE)
printf(" unbindable");
}
static void show_sb_flags(__u64 flags)
{
printf("%s", flags & MS_RDONLY ? "ro" : "rw");
if (flags & MS_SYNCHRONOUS)
printf(",sync");
if (flags & MS_DIRSYNC)
printf(",dirsync");
if (flags & MS_MANDLOCK)
printf(",mand");
if (flags & MS_LAZYTIME)
printf(",lazytime");
}
static int dump_mountinfo(__u64 mnt_id, __u64 mnt_ns_id)
{
int ret;
struct statmount *buf = alloca(STATMOUNT_BUFSIZE);
const __u64 mask = STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC |
STATMOUNT_PROPAGATE_FROM | STATMOUNT_FS_TYPE |
STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT |
STATMOUNT_MNT_OPTS | STATMOUNT_FS_SUBTYPE |
STATMOUNT_SB_SOURCE;
ret = statmount(mnt_id, mnt_ns_id, mask, buf, STATMOUNT_BUFSIZE, 0);
if (ret < 0) {
perror("statmount");
return 1;
}
if (ext_format)
printf("0x%llx 0x%llx 0x%llx ", mnt_ns_id, mnt_id, buf->mnt_parent_id);
printf("%u %u %u:%u %s %s ", buf->mnt_id_old, buf->mnt_parent_id_old,
buf->sb_dev_major, buf->sb_dev_minor,
&buf->str[buf->mnt_root],
&buf->str[buf->mnt_point]);
show_mnt_attrs(buf->mnt_attr);
show_propagation(buf);
printf(" - %s", &buf->str[buf->fs_type]);
if (buf->mask & STATMOUNT_FS_SUBTYPE)
printf(".%s", &buf->str[buf->fs_subtype]);
if (buf->mask & STATMOUNT_SB_SOURCE)
printf(" %s ", &buf->str[buf->sb_source]);
else
printf(" :none ");
show_sb_flags(buf->sb_flags);
if (buf->mask & STATMOUNT_MNT_OPTS)
printf(",%s", &buf->str[buf->mnt_opts]);
printf("\n");
return 0;
}
static int dump_mounts(__u64 mnt_ns_id)
{
__u64 mntid[MAXMOUNTS];
__u64 last_mnt_id = 0;
ssize_t count;
int i;
/*
* Get a list of all mntids in mnt_ns_id. If it returns MAXMOUNTS
* mounts, then go again until we get everything.
*/
do {
count = listmount(LSMT_ROOT, mnt_ns_id, last_mnt_id, mntid, MAXMOUNTS, 0);
if (count < 0 || count > MAXMOUNTS) {
errno = count < 0 ? errno : count;
perror("listmount");
return 1;
}
/* Walk the returned mntids and print info about each */
for (i = 0; i < count; ++i) {
int ret = dump_mountinfo(mntid[i], mnt_ns_id);
if (ret != 0)
return ret;
}
/* Set up last_mnt_id to pick up where we left off */
last_mnt_id = mntid[count - 1];
} while (count == MAXMOUNTS);
return 0;
}
static void usage(const char * const prog)
{
printf("Usage:\n");
printf("%s [-e] [-p pid] [-r] [-h]\n", prog);
printf(" -e: extended format\n");
printf(" -h: print usage message\n");
printf(" -p: get mount namespace from given pid\n");
printf(" -r: recursively print all mounts in all child namespaces\n");
}
int main(int argc, char * const *argv)
{
struct mnt_ns_info mni = { .size = MNT_NS_INFO_SIZE_VER0 };
int pidfd, mntns, ret, opt;
pid_t pid = getpid();
bool recursive = false;
while ((opt = getopt(argc, argv, "ehp:r")) != -1) {
switch (opt) {
case 'e':
ext_format = true;
break;
case 'h':
usage(argv[0]);
return 0;
case 'p':
pid = atoi(optarg);
break;
case 'r':
recursive = true;
break;
}
}
/* Get a pidfd for pid */
pidfd = syscall(__NR_pidfd_open, pid, 0);
if (pidfd < 0) {
perror("pidfd_open");
return 1;
}
/* Get the mnt namespace for pidfd */
mntns = ioctl(pidfd, PIDFD_GET_MNT_NAMESPACE, NULL);
if (mntns < 0) {
perror("PIDFD_GET_MNT_NAMESPACE");
return 1;
}
close(pidfd);
/* get info about mntns. In particular, the mnt_ns_id */
ret = ioctl(mntns, NS_MNT_GET_INFO, &mni);
if (ret < 0) {
perror("NS_MNT_GET_INFO");
return 1;
}
do {
int ret;
ret = dump_mounts(mni.mnt_ns_id);
if (ret)
return ret;
if (!recursive)
break;
/* get the next mntns (and overwrite the old mount ns info) */
ret = ioctl(mntns, NS_MNT_GET_NEXT, &mni);
close(mntns);
mntns = ret;
} while (mntns >= 0);
return 0;
}
+253
View File
@@ -0,0 +1,253 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __SAMPLES_VFS_H
#define __SAMPLES_VFS_H
#include <errno.h>
#include <linux/types.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#define die_errno(format, ...) \
do { \
fprintf(stderr, "%m | %s: %d: %s: " format "\n", __FILE__, \
__LINE__, __func__, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
struct statmount {
__u32 size; /* Total size, including strings */
__u32 mnt_opts; /* [str] Options (comma separated, escaped) */
__u64 mask; /* What results were written */
__u32 sb_dev_major; /* Device ID */
__u32 sb_dev_minor;
__u64 sb_magic; /* ..._SUPER_MAGIC */
__u32 sb_flags; /* SB_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */
__u32 fs_type; /* [str] Filesystem type */
__u64 mnt_id; /* Unique ID of mount */
__u64 mnt_parent_id; /* Unique ID of parent (for root == mnt_id) */
__u32 mnt_id_old; /* Reused IDs used in proc/.../mountinfo */
__u32 mnt_parent_id_old;
__u64 mnt_attr; /* MOUNT_ATTR_... */
__u64 mnt_propagation; /* MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */
__u64 mnt_peer_group; /* ID of shared peer group */
__u64 mnt_master; /* Mount receives propagation from this ID */
__u64 propagate_from; /* Propagation from in current namespace */
__u32 mnt_root; /* [str] Root of mount relative to root of fs */
__u32 mnt_point; /* [str] Mountpoint relative to current root */
__u64 mnt_ns_id; /* ID of the mount namespace */
__u32 fs_subtype; /* [str] Subtype of fs_type (if any) */
__u32 sb_source; /* [str] Source string of the mount */
__u32 opt_num; /* Number of fs options */
__u32 opt_array; /* [str] Array of nul terminated fs options */
__u32 opt_sec_num; /* Number of security options */
__u32 opt_sec_array; /* [str] Array of nul terminated security options */
__u32 mnt_uidmap_num; /* Number of uid mappings */
__u32 mnt_uidmap; /* [str] Array of uid mappings */
__u32 mnt_gidmap_num; /* Number of gid mappings */
__u32 mnt_gidmap; /* [str] Array of gid mappings */
__u64 __spare2[44];
char str[]; /* Variable size part containing strings */
};
struct mnt_id_req {
__u32 size;
__u32 spare;
__u64 mnt_id;
__u64 param;
__u64 mnt_ns_id;
};
#ifndef MNT_ID_REQ_SIZE_VER0
#define MNT_ID_REQ_SIZE_VER0 24 /* sizeof first published struct */
#endif
#ifndef MNT_ID_REQ_SIZE_VER1
#define MNT_ID_REQ_SIZE_VER1 32 /* sizeof second published struct */
#endif
/* Get the id for a mount namespace */
#ifndef NS_GET_MNTNS_ID
#define NS_GET_MNTNS_ID _IO(0xb7, 0x5)
#endif
struct mnt_ns_info {
__u32 size;
__u32 nr_mounts;
__u64 mnt_ns_id;
};
#ifndef MNT_NS_INFO_SIZE_VER0
#define MNT_NS_INFO_SIZE_VER0 16 /* size of first published struct */
#endif
#ifndef NS_MNT_GET_INFO
#define NS_MNT_GET_INFO _IOR(0xb7, 10, struct mnt_ns_info)
#endif
#ifndef NS_MNT_GET_NEXT
#define NS_MNT_GET_NEXT _IOR(0xb7, 11, struct mnt_ns_info)
#endif
#ifndef NS_MNT_GET_PREV
#define NS_MNT_GET_PREV _IOR(0xb7, 12, struct mnt_ns_info)
#endif
#ifndef PIDFD_GET_MNT_NAMESPACE
#define PIDFD_GET_MNT_NAMESPACE _IO(0xFF, 3)
#endif
#ifndef __NR_listmount
#define __NR_listmount 458
#endif
#ifndef __NR_statmount
#define __NR_statmount 457
#endif
#ifndef LSMT_ROOT
#define LSMT_ROOT 0xffffffffffffffff /* root mount */
#endif
/* @mask bits for statmount(2) */
#ifndef STATMOUNT_SB_BASIC
#define STATMOUNT_SB_BASIC 0x00000001U /* Want/got sb_... */
#endif
#ifndef STATMOUNT_MNT_BASIC
#define STATMOUNT_MNT_BASIC 0x00000002U /* Want/got mnt_... */
#endif
#ifndef STATMOUNT_PROPAGATE_FROM
#define STATMOUNT_PROPAGATE_FROM 0x00000004U /* Want/got propagate_from */
#endif
#ifndef STATMOUNT_MNT_ROOT
#define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */
#endif
#ifndef STATMOUNT_MNT_POINT
#define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */
#endif
#ifndef STATMOUNT_FS_TYPE
#define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */
#endif
#ifndef STATMOUNT_MNT_NS_ID
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/got mnt_ns_id */
#endif
#ifndef STATMOUNT_MNT_OPTS
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/got mnt_opts */
#endif
#ifndef STATMOUNT_FS_SUBTYPE
#define STATMOUNT_FS_SUBTYPE 0x00000100U /* Want/got fs_subtype */
#endif
#ifndef STATMOUNT_SB_SOURCE
#define STATMOUNT_SB_SOURCE 0x00000200U /* Want/got sb_source */
#endif
#ifndef STATMOUNT_OPT_ARRAY
#define STATMOUNT_OPT_ARRAY 0x00000400U /* Want/got opt_... */
#endif
#ifndef STATMOUNT_OPT_SEC_ARRAY
#define STATMOUNT_OPT_SEC_ARRAY 0x00000800U /* Want/got opt_sec... */
#endif
#ifndef STATX_MNT_ID_UNIQUE
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
#endif
#ifndef STATMOUNT_MNT_UIDMAP
#define STATMOUNT_MNT_UIDMAP 0x00002000U /* Want/got uidmap... */
#endif
#ifndef STATMOUNT_MNT_GIDMAP
#define STATMOUNT_MNT_GIDMAP 0x00004000U /* Want/got gidmap... */
#endif
#ifndef MOUNT_ATTR_RDONLY
#define MOUNT_ATTR_RDONLY 0x00000001 /* Mount read-only */
#endif
#ifndef MOUNT_ATTR_NOSUID
#define MOUNT_ATTR_NOSUID 0x00000002 /* Ignore suid and sgid bits */
#endif
#ifndef MOUNT_ATTR_NODEV
#define MOUNT_ATTR_NODEV 0x00000004 /* Disallow access to device special files */
#endif
#ifndef MOUNT_ATTR_NOEXEC
#define MOUNT_ATTR_NOEXEC 0x00000008 /* Disallow program execution */
#endif
#ifndef MOUNT_ATTR__ATIME
#define MOUNT_ATTR__ATIME 0x00000070 /* Setting on how atime should be updated */
#endif
#ifndef MOUNT_ATTR_RELATIME
#define MOUNT_ATTR_RELATIME 0x00000000 /* - Update atime relative to mtime/ctime. */
#endif
#ifndef MOUNT_ATTR_NOATIME
#define MOUNT_ATTR_NOATIME 0x00000010 /* - Do not update access times. */
#endif
#ifndef MOUNT_ATTR_STRICTATIME
#define MOUNT_ATTR_STRICTATIME 0x00000020 /* - Always perform atime updates */
#endif
#ifndef MOUNT_ATTR_NODIRATIME
#define MOUNT_ATTR_NODIRATIME 0x00000080 /* Do not update directory access times */
#endif
#ifndef MOUNT_ATTR_IDMAP
#define MOUNT_ATTR_IDMAP 0x00100000 /* Idmap mount to @userns_fd in struct mount_attr. */
#endif
#ifndef MOUNT_ATTR_NOSYMFOLLOW
#define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */
#endif
#ifndef MS_RDONLY
#define MS_RDONLY 1 /* Mount read-only */
#endif
#ifndef MS_SYNCHRONOUS
#define MS_SYNCHRONOUS 16 /* Writes are synced at once */
#endif
#ifndef MS_MANDLOCK
#define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */
#endif
#ifndef MS_DIRSYNC
#define MS_DIRSYNC 128 /* Directory modifications are synchronous */
#endif
#ifndef MS_UNBINDABLE
#define MS_UNBINDABLE (1<<17) /* change to unbindable */
#endif
#ifndef MS_PRIVATE
#define MS_PRIVATE (1<<18) /* change to private */
#endif
#ifndef MS_SLAVE
#define MS_SLAVE (1<<19) /* change to slave */
#endif
#ifndef MS_SHARED
#define MS_SHARED (1<<20) /* change to shared */
#endif
#ifndef MS_LAZYTIME
#define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */
#endif
#endif /* __SAMPLES_VFS_H */
+129
View File
@@ -0,0 +1,129 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* fd-based mount test.
*
* Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <linux/mount.h>
#include <linux/unistd.h>
#define E(x) do { if ((x) == -1) { perror(#x); exit(1); } } while(0)
static void check_messages(int fd)
{
char buf[4096];
int err, n;
err = errno;
for (;;) {
n = read(fd, buf, sizeof(buf));
if (n < 0)
break;
n -= 2;
switch (buf[0]) {
case 'e':
fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2);
break;
case 'w':
fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2);
break;
case 'i':
fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2);
break;
}
}
errno = err;
}
static __attribute__((noreturn))
void mount_error(int fd, const char *s)
{
check_messages(fd);
fprintf(stderr, "%s: %m\n", s);
exit(1);
}
/* Hope -1 isn't a syscall */
#ifndef __NR_fsopen
#define __NR_fsopen -1
#endif
#ifndef __NR_fsmount
#define __NR_fsmount -1
#endif
#ifndef __NR_fsconfig
#define __NR_fsconfig -1
#endif
#ifndef __NR_move_mount
#define __NR_move_mount -1
#endif
static inline int fsopen(const char *fs_name, unsigned int flags)
{
return syscall(__NR_fsopen, fs_name, flags);
}
static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
{
return syscall(__NR_fsmount, fsfd, flags, ms_flags);
}
static inline int fsconfig(int fsfd, unsigned int cmd,
const char *key, const void *val, int aux)
{
return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux);
}
static inline int move_mount(int from_dfd, const char *from_pathname,
int to_dfd, const char *to_pathname,
unsigned int flags)
{
return syscall(__NR_move_mount,
from_dfd, from_pathname,
to_dfd, to_pathname, flags);
}
#define E_fsconfig(fd, cmd, key, val, aux) \
do { \
if (fsconfig(fd, cmd, key, val, aux) == -1) \
mount_error(fd, key ?: "create"); \
} while (0)
int main(int argc, char *argv[])
{
int fsfd, mfd;
/* Mount a publically available AFS filesystem */
fsfd = fsopen("afs", 0);
if (fsfd == -1) {
perror("fsopen");
exit(1);
}
E_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "#grand.central.org:root.cell.", 0);
E_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
mfd = fsmount(fsfd, 0, MOUNT_ATTR_RDONLY);
if (mfd < 0)
mount_error(fsfd, "fsmount");
E(close(fsfd));
if (move_mount(mfd, "", AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
perror("move_mount");
exit(1);
}
E(close(mfd));
exit(0);
}
@@ -0,0 +1,173 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2024 Christian Brauner <brauner@kernel.org>
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <linux/types.h>
#include <inttypes.h>
#include <stdio.h>
#include "../../tools/testing/selftests/pidfd/pidfd.h"
#include "samples-vfs.h"
static int __statmount(__u64 mnt_id, __u64 mnt_ns_id, __u64 mask,
struct statmount *stmnt, size_t bufsize,
unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER1,
.mnt_id = mnt_id,
.param = mask,
.mnt_ns_id = mnt_ns_id,
};
return syscall(__NR_statmount, &req, stmnt, bufsize, flags);
}
static struct statmount *sys_statmount(__u64 mnt_id, __u64 mnt_ns_id,
__u64 mask, unsigned int flags)
{
size_t bufsize = 1 << 15;
struct statmount *stmnt = NULL, *tmp = NULL;
int ret;
for (;;) {
tmp = realloc(stmnt, bufsize);
if (!tmp)
goto out;
stmnt = tmp;
ret = __statmount(mnt_id, mnt_ns_id, mask, stmnt, bufsize, flags);
if (!ret)
return stmnt;
if (errno != EOVERFLOW)
goto out;
bufsize <<= 1;
if (bufsize >= UINT_MAX / 2)
goto out;
}
out:
free(stmnt);
return NULL;
}
static ssize_t sys_listmount(__u64 mnt_id, __u64 last_mnt_id, __u64 mnt_ns_id,
__u64 list[], size_t num, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER1,
.mnt_id = mnt_id,
.param = last_mnt_id,
.mnt_ns_id = mnt_ns_id,
};
return syscall(__NR_listmount, &req, list, num, flags);
}
int main(int argc, char *argv[])
{
#define LISTMNT_BUFFER 10
__u64 list[LISTMNT_BUFFER], last_mnt_id = 0;
int ret, pidfd, fd_mntns;
struct mnt_ns_info info = {};
pidfd = sys_pidfd_open(getpid(), 0);
if (pidfd < 0)
die_errno("pidfd_open failed");
fd_mntns = ioctl(pidfd, PIDFD_GET_MNT_NAMESPACE, 0);
if (fd_mntns < 0)
die_errno("ioctl(PIDFD_GET_MNT_NAMESPACE) failed");
ret = ioctl(fd_mntns, NS_MNT_GET_INFO, &info);
if (ret < 0)
die_errno("ioctl(NS_GET_MNTNS_ID) failed");
printf("Listing %u mounts for mount namespace %" PRIu64 "\n",
info.nr_mounts, (uint64_t)info.mnt_ns_id);
for (;;) {
ssize_t nr_mounts;
next:
nr_mounts = sys_listmount(LSMT_ROOT, last_mnt_id,
info.mnt_ns_id, list, LISTMNT_BUFFER,
0);
if (nr_mounts <= 0) {
int fd_mntns_next;
printf("Finished listing %u mounts for mount namespace %" PRIu64 "\n\n",
info.nr_mounts, (uint64_t)info.mnt_ns_id);
fd_mntns_next = ioctl(fd_mntns, NS_MNT_GET_NEXT, &info);
if (fd_mntns_next < 0) {
if (errno == ENOENT) {
printf("Finished listing all mount namespaces\n");
exit(0);
}
die_errno("ioctl(NS_MNT_GET_NEXT) failed");
}
close(fd_mntns);
fd_mntns = fd_mntns_next;
last_mnt_id = 0;
printf("Listing %u mounts for mount namespace %" PRIu64 "\n",
info.nr_mounts, (uint64_t)info.mnt_ns_id);
goto next;
}
for (size_t cur = 0; cur < nr_mounts; cur++) {
struct statmount *stmnt;
last_mnt_id = list[cur];
stmnt = sys_statmount(last_mnt_id, info.mnt_ns_id,
STATMOUNT_SB_BASIC |
STATMOUNT_MNT_BASIC |
STATMOUNT_MNT_ROOT |
STATMOUNT_MNT_POINT |
STATMOUNT_MNT_NS_ID |
STATMOUNT_MNT_OPTS |
STATMOUNT_FS_TYPE |
STATMOUNT_MNT_UIDMAP |
STATMOUNT_MNT_GIDMAP, 0);
if (!stmnt) {
printf("Failed to statmount(%" PRIu64 ") in mount namespace(%" PRIu64 ")\n",
(uint64_t)last_mnt_id, (uint64_t)info.mnt_ns_id);
continue;
}
printf("mnt_id:\t\t%" PRIu64 "\nmnt_parent_id:\t%" PRIu64 "\nfs_type:\t%s\nmnt_root:\t%s\nmnt_point:\t%s\nmnt_opts:\t%s\n",
(uint64_t)stmnt->mnt_id,
(uint64_t)stmnt->mnt_parent_id,
(stmnt->mask & STATMOUNT_FS_TYPE) ? stmnt->str + stmnt->fs_type : "",
(stmnt->mask & STATMOUNT_MNT_ROOT) ? stmnt->str + stmnt->mnt_root : "",
(stmnt->mask & STATMOUNT_MNT_POINT) ? stmnt->str + stmnt->mnt_point : "",
(stmnt->mask & STATMOUNT_MNT_OPTS) ? stmnt->str + stmnt->mnt_opts : "");
if (stmnt->mask & STATMOUNT_MNT_UIDMAP) {
const char *idmap = stmnt->str + stmnt->mnt_uidmap;
for (size_t idx = 0; idx < stmnt->mnt_uidmap_num; idx++) {
printf("mnt_uidmap[%zu]:\t%s\n", idx, idmap);
idmap += strlen(idmap) + 1;
}
}
if (stmnt->mask & STATMOUNT_MNT_GIDMAP) {
const char *idmap = stmnt->str + stmnt->mnt_gidmap;
for (size_t idx = 0; idx < stmnt->mnt_gidmap_num; idx++) {
printf("mnt_gidmap[%zu]:\t%s\n", idx, idmap);
idmap += strlen(idmap) + 1;
}
}
printf("\n");
free(stmnt);
}
}
exit(0);
}
+271
View File
@@ -0,0 +1,271 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* Test the statx() system call.
*
* Note that the output of this program is intended to look like the output of
* /bin/stat where possible.
*
* Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#define _GNU_SOURCE
#define _ATFILE_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/syscall.h>
#include <sys/types.h>
// Work around glibc header silliness
#undef AT_RENAME_NOREPLACE
#undef AT_RENAME_EXCHANGE
#undef AT_RENAME_WHITEOUT
#include <linux/stat.h>
#include <linux/fcntl.h>
#define statx foo
#define statx_timestamp foo_timestamp
struct statx;
struct statx_timestamp;
#include <sys/stat.h>
#undef statx
#undef statx_timestamp
#define AT_STATX_SYNC_TYPE 0x6000
#define AT_STATX_SYNC_AS_STAT 0x0000
#define AT_STATX_FORCE_SYNC 0x2000
#define AT_STATX_DONT_SYNC 0x4000
#ifndef __NR_statx
#define __NR_statx -1
#endif
static __attribute__((unused))
ssize_t statx(int dfd, const char *filename, unsigned flags,
unsigned int mask, struct statx *buffer)
{
return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
}
static void print_time(const char *field, struct statx_timestamp *ts)
{
struct tm tm;
time_t tim;
char buffer[100];
int len;
tim = ts->tv_sec;
if (!localtime_r(&tim, &tm)) {
perror("localtime_r");
exit(1);
}
len = strftime(buffer, 100, "%F %T", &tm);
if (len == 0) {
perror("strftime");
exit(1);
}
printf("%s", field);
fwrite(buffer, 1, len, stdout);
printf(".%09u", ts->tv_nsec);
len = strftime(buffer, 100, "%z", &tm);
if (len == 0) {
perror("strftime2");
exit(1);
}
fwrite(buffer, 1, len, stdout);
printf("\n");
}
static void dump_statx(struct statx *stx)
{
char buffer[256], ft = '?';
printf("results=%x\n", stx->stx_mask);
printf(" ");
if (stx->stx_mask & STATX_SIZE)
printf(" Size: %-15llu", (unsigned long long)stx->stx_size);
if (stx->stx_mask & STATX_BLOCKS)
printf(" Blocks: %-10llu", (unsigned long long)stx->stx_blocks);
printf(" IO Block: %-6llu", (unsigned long long)stx->stx_blksize);
if (stx->stx_mask & STATX_TYPE) {
switch (stx->stx_mode & S_IFMT) {
case S_IFIFO: printf(" FIFO\n"); ft = 'p'; break;
case S_IFCHR: printf(" character special file\n"); ft = 'c'; break;
case S_IFDIR: printf(" directory\n"); ft = 'd'; break;
case S_IFBLK: printf(" block special file\n"); ft = 'b'; break;
case S_IFREG: printf(" regular file\n"); ft = '-'; break;
case S_IFLNK: printf(" symbolic link\n"); ft = 'l'; break;
case S_IFSOCK: printf(" socket\n"); ft = 's'; break;
default:
printf(" unknown type (%o)\n", stx->stx_mode & S_IFMT);
break;
}
} else {
printf(" no type\n");
}
sprintf(buffer, "%02x:%02x", stx->stx_dev_major, stx->stx_dev_minor);
printf("Device: %-15s", buffer);
if (stx->stx_mask & STATX_INO)
printf(" Inode: %-11llu", (unsigned long long) stx->stx_ino);
if (stx->stx_mask & STATX_NLINK)
printf(" Links: %-5u", stx->stx_nlink);
if (stx->stx_mask & STATX_TYPE) {
switch (stx->stx_mode & S_IFMT) {
case S_IFBLK:
case S_IFCHR:
printf(" Device type: %u,%u",
stx->stx_rdev_major, stx->stx_rdev_minor);
break;
}
}
printf("\n");
if (stx->stx_mask & STATX_MODE)
printf("Access: (%04o/%c%c%c%c%c%c%c%c%c%c) ",
stx->stx_mode & 07777,
ft,
stx->stx_mode & S_IRUSR ? 'r' : '-',
stx->stx_mode & S_IWUSR ? 'w' : '-',
stx->stx_mode & S_IXUSR ? 'x' : '-',
stx->stx_mode & S_IRGRP ? 'r' : '-',
stx->stx_mode & S_IWGRP ? 'w' : '-',
stx->stx_mode & S_IXGRP ? 'x' : '-',
stx->stx_mode & S_IROTH ? 'r' : '-',
stx->stx_mode & S_IWOTH ? 'w' : '-',
stx->stx_mode & S_IXOTH ? 'x' : '-');
if (stx->stx_mask & STATX_UID)
printf("Uid: %5d ", stx->stx_uid);
if (stx->stx_mask & STATX_GID)
printf("Gid: %5d\n", stx->stx_gid);
if (stx->stx_mask & STATX_ATIME)
print_time("Access: ", &stx->stx_atime);
if (stx->stx_mask & STATX_MTIME)
print_time("Modify: ", &stx->stx_mtime);
if (stx->stx_mask & STATX_CTIME)
print_time("Change: ", &stx->stx_ctime);
if (stx->stx_mask & STATX_BTIME)
print_time(" Birth: ", &stx->stx_btime);
if (stx->stx_attributes_mask) {
unsigned char bits, mbits;
int loop, byte;
static char attr_representation[64 + 1] =
/* STATX_ATTR_ flags: */
"????????" /* 63-56 */
"????????" /* 55-48 */
"????????" /* 47-40 */
"????????" /* 39-32 */
"????????" /* 31-24 0x00000000-ff000000 */
"????????" /* 23-16 0x00000000-00ff0000 */
"???me???" /* 15- 8 0x00000000-0000ff00 */
"?dai?c??" /* 7- 0 0x00000000-000000ff */
;
printf("Attributes: %016llx (",
(unsigned long long)stx->stx_attributes);
for (byte = 64 - 8; byte >= 0; byte -= 8) {
bits = stx->stx_attributes >> byte;
mbits = stx->stx_attributes_mask >> byte;
for (loop = 7; loop >= 0; loop--) {
int bit = byte + loop;
if (!(mbits & 0x80))
putchar('.'); /* Not supported */
else if (bits & 0x80)
putchar(attr_representation[63 - bit]);
else
putchar('-'); /* Not set */
bits <<= 1;
mbits <<= 1;
}
if (byte)
putchar(' ');
}
printf(")\n");
}
}
static void dump_hex(unsigned long long *data, int from, int to)
{
unsigned offset, print_offset = 1, col = 0;
from /= 8;
to = (to + 7) / 8;
for (offset = from; offset < to; offset++) {
if (print_offset) {
printf("%04x: ", offset * 8);
print_offset = 0;
}
printf("%016llx", data[offset]);
col++;
if ((col & 3) == 0) {
printf("\n");
print_offset = 1;
} else {
printf(" ");
}
}
if (!print_offset)
printf("\n");
}
int main(int argc, char **argv)
{
struct statx stx;
int ret, raw = 0, atflag = AT_SYMLINK_NOFOLLOW;
unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
for (argv++; *argv; argv++) {
if (strcmp(*argv, "-F") == 0) {
atflag &= ~AT_STATX_SYNC_TYPE;
atflag |= AT_STATX_FORCE_SYNC;
continue;
}
if (strcmp(*argv, "-D") == 0) {
atflag &= ~AT_STATX_SYNC_TYPE;
atflag |= AT_STATX_DONT_SYNC;
continue;
}
if (strcmp(*argv, "-L") == 0) {
atflag &= ~AT_SYMLINK_NOFOLLOW;
continue;
}
if (strcmp(*argv, "-O") == 0) {
mask &= ~STATX_BASIC_STATS;
continue;
}
if (strcmp(*argv, "-A") == 0) {
atflag |= AT_NO_AUTOMOUNT;
continue;
}
if (strcmp(*argv, "-R") == 0) {
raw = 1;
continue;
}
memset(&stx, 0xbf, sizeof(stx));
ret = statx(AT_FDCWD, *argv, atflag, mask, &stx);
printf("statx(%s) = %d\n", *argv, ret);
if (ret < 0) {
perror(*argv);
exit(1);
}
if (raw)
dump_hex((unsigned long long *)&stx, 0, sizeof(stx));
dump_statx(&stx);
}
return 0;
}