Skip to contentSkip to conversation

Add open_tree() wrapper

PATCH Superseded [email protected] 2 participants 14 messages Sep 7 – Oct 6

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add open_tree() wrapper

From: Dmitry Sakhonchik <[email protected]>

v2:
- Handle relative paths correctly in the open_tree wrapper
- Add fallback to SYS_open_tree when libc function is absent
- Add tests coverage with relative paths (dirfd != AT_FDCWD)
- Add support and tests for syscall(SYS_open_tree, ...)

v3:
- Add open_tree_attr wrapper
- Add open_tree_attr syscall test

Dmitry Sakhonchik (6):
ports/linux/guts: Add open_tree wrapper
test: Add open_tree test
ports/linux/pseudo_wrappers.c: Add syscall support for open_tree()
wrapper
test: Add open_tree syscall test
test: Add open_tree_attr syscall test
ports/linux/guts: Add open_tree_attr wrapper

 ports/linux/guts/open_tree.c        |  46 ++++++++
 ports/linux/guts/open_tree_attr.c   |  48 ++++++++
 ports/linux/portdefs.h              |  18 +++
 ports/linux/pseudo_wrappers.c       |  30 +++++
 ports/linux/wrapfuncs.in            |   2 +
 pseudo_wrappers.c                   |   1 +
 test/test-open_tree-syscall.c       | 152 +++++++++++++++++++++++++
 test/test-open_tree-syscall.sh      |   5 +
 test/test-open_tree.c               | 139 +++++++++++++++++++++++
 test/test-open_tree.sh              |   5 +
 test/test-open_tree_attr-syscall.c  | 165 ++++++++++++++++++++++++++++
 test/test-open_tree_attr-syscall.sh |   5 +
 12 files changed, 616 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
 create mode 100644 ports/linux/guts/open_tree_attr.c
 create mode 100644 test/test-open_tree-syscall.c
 create mode 100755 test/test-open_tree-syscall.sh
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
 create mode 100644 test/test-open_tree_attr-syscall.c
 create mode 100755 test/test-open_tree_attr-syscall.sh
#1 lore raw

PATCH v3 1/6 ports/linux/guts: Add open_tree wrapper

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is outside the common pseudo use case.

---
 ports/linux/guts/open_tree.c | 46 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       |  9 +++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 56 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +46 -0
new file mode 100644
index 0000000..6c2aad9
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *	int rc = -1;
+ */
+
+	struct stat64 buf;
+	int save_errno;
+	char *pseudo_path;
+
+	if (real_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling open_tree.\n");
+		rc = real_open_tree(dirfd, path, flags);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+	}
+
+	pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path, &buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+
+		pseudo_debug(PDBGF_FILE, "open_tree(path %s), flags %o, stat rc %d, stat mode %o\n",
+			pseudo_path, flags, stat_rc, buf.st_mode);
+
+		if (stat_rc != -1) {
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "open_tree(fd %d, path %d/%s, flags %d) succeeded, but stat failed (%s).\n",
+				rc, dirfd, pseudo_path, flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, 0);
+		}
+		errno = save_errno;
+	}
+
+/*	return rc;
+ * }
+ */
ports/linux/portdefs.h +9 -0
index 1f1a41a..565fed9 100644
@@ -82,3 +82,12 @@ extern int unshare(int flags);
 
 #define SYS_openat2 __NR_openat2
 #endif
+
+#ifndef SYS_open_tree
+
+#ifndef __NR_open_tree
+#define __NR_open_tree 428
+#endif
+
+#define SYS_open_tree __NR_open_tree
+#endif
ports/linux/wrapfuncs.in +1 -0
index 3cea20a..a51e9a0 100644
@@ -10,6 +10,7 @@ int lchown(const char *path, uid_t owner, gid_t group); /* flags=AT_SYMLINK_NOFO
 int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags);
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
+int open_tree(int dirfd, const char *path, unsigned int flags); /* noignore_path=1 */
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
#2 lore raw

Re: [pseudo] [PATCH v3 1/6] ports/linux/guts: Add open_tree wrapper

Quoted 5 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is outside the common pseudo use case.

We should at least log an error if OPEN_TREE_CLONE is seen instead of silently ignoring it.

Quoted 26 lines
---
 ports/linux/guts/open_tree.c | 46 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       |  9 +++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 56 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +11 -0
new file mode 100644
index 0000000..6c2aad9
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *	int rc = -1;
+ */
+
+	struct stat64 buf;

ports/linux/guts/open_tree.c line 21

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+	int save_errno;
+	char *pseudo_path;
+
+	if (real_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling open_tree.\n");
+		rc = real_open_tree(dirfd, path, flags);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+	}

The commit message or a comment should briefly explain why we need to handle both ways of invoking the real open_tree syscall.

ports/linux/guts/open_tree.c line 23

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+	pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

This should be after the (rc != -1) conditional, as this call could clobber errno.

ports/linux/guts/open_tree.c line 25

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+	if (rc != -1) {

Nitpick: To save indentation, we can check for (rc == -1) and return early.

Quoted 43 lines
ports/linux/guts/open_tree.c new file +21 -0
+		save_errno = errno;
+		int stat_rc;
+
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path, &buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+
+		pseudo_debug(PDBGF_FILE, "open_tree(path %s), flags %o, stat rc %d, stat mode %o\n",
+			pseudo_path, flags, stat_rc, buf.st_mode);
+
+		if (stat_rc != -1) {
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "open_tree(fd %d, path %d/%s, flags %d) succeeded, but stat failed (%s).\n",
+				rc, dirfd, pseudo_path, flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, 0);
+		}
+		errno = save_errno;
+	}
+
+/*	return rc;
+ * }
+ */
ports/linux/portdefs.h +9 -0
index 1f1a41a..565fed9 100644
@@ -82,3 +82,12 @@ extern int unshare(int flags);
 
 #define SYS_openat2 __NR_openat2
 #endif
+
+#ifndef SYS_open_tree
+
+#ifndef __NR_open_tree
+#define __NR_open_tree 428
+#endif
+
+#define SYS_open_tree __NR_open_tree
+#endif
ports/linux/wrapfuncs.in +0 -0
index 3cea20a..a51e9a0 100644

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t group); /* flags=AT_SYMLINK_NOFO · line 13

Quoted 4 lines
ports/linux/wrapfuncs.in +1 -0
@@ -10,6 +10,7 @@ int lchown(const char *path, uid_t owner, gid_t group); /* flags=AT_SYMLINK_NOFO
 int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags);
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
+int open_tree(int dirfd, const char *path, unsigned int flags); /* noignore_path=1 */

Does this need preserve_paths=1 (similar to openat2())?

That would require us to account for a possible chroot in the open_tree() guts, but it would avoid us running pseudo_root_path() on an already canonicalized path which may introduce problems under a chroot.

Quoted 4 lines
ports/linux/wrapfuncs.in +0 -0
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
Signature · 1 line
Paul Barker
#3 lore raw

Re: [pseudo] [PATCH v3 1/6] ports/linux/guts: Add open_tree wrapper

пн, 21 сент. 2026 г. в 12:28, Paul Barker <[email protected]>:

Quoted 53 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:
Quoted 5 lines

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is

outside the common pseudo use case.

We should at least log an error if OPEN_TREE_CLONE is seen instead of silently ignoring it.

Quoted 21 lines
---
 ports/linux/guts/open_tree.c | 46 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       |  9 +++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 56 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +6 -0
new file mode 100644
index 0000000..6c2aad9
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *

ports/linux/guts/open_tree.c line 16

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *   int rc = -1;
+ */
+
+     struct stat64 buf;
+     int save_errno;
+     char *pseudo_path;
+
+     if (real_open_tree) {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

open_tree.\n");

ports/linux/guts/open_tree.c

Quoted 3 lines
ports/linux/guts/open_tree.c new file +3 -0
+             rc = real_open_tree(dirfd, path, flags);
+     } else {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

syscall.\n");

ports/linux/guts/open_tree.c line 21

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+             rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+     }

The commit message or a comment should briefly explain why we need to handle both ways of invoking the real open_tree syscall.

Quoted 7 lines

ports/linux/guts/open_tree.c line 23

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+     pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

This should be after the (rc != -1) conditional, as this call could clobber errno.

If we use preserve_path=1, then the absolute path must go through

pseudo_root_path before the real open_tree call so that a possible chroot is handled correctly.
So I think these two cases need to be somehow handled separately rather than moving pseudo_root_path() entirely below the rc != -1 check. By the way, for some reason the openat2 wrapper is ok with the pseudo_root_path outside of conditional.

Quoted 85 lines

ports/linux/guts/open_tree.c line 25

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+     if (rc != -1) {

Nitpick: To save indentation, we can check for (rc == -1) and return early.

ports/linux/guts/open_tree.c

Quoted 4 lines
ports/linux/guts/open_tree.c new file +4 -0
+             save_errno = errno;
+             int stat_rc;
+
+             stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path,

&buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);

ports/linux/guts/open_tree.c line 31

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+             pseudo_debug(PDBGF_FILE, "open_tree(path %s), flags %o,

stat rc %d, stat mode %o\n",

ports/linux/guts/open_tree.c

Quoted 4 lines
ports/linux/guts/open_tree.c new file +4 -0
+                     pseudo_path, flags, stat_rc, buf.st_mode);
+
+             if (stat_rc != -1) {
+                     pseudo_client_op(OP_OPEN, 0, rc, dirfd,

pseudo_path, &buf);

ports/linux/guts/open_tree.c line 37

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+             } else {
+                     pseudo_debug(PDBGF_FILE, "open_tree(fd %d, path

%d/%s, flags %d) succeeded, but stat failed (%s).\n",

ports/linux/guts/open_tree.c line 38

Quoted 1 line
ports/linux/guts/open_tree.c new file +1 -0
+                             rc, dirfd, pseudo_path, flags,

strerror(errno));

ports/linux/guts/open_tree.c line 39

Quoted 1 line
ports/linux/guts/open_tree.c new file +1 -0
+                     pseudo_client_op(OP_OPEN, 0, rc, dirfd,

pseudo_path, 0);

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t

Quoted 29 lines
ports/linux/guts/open_tree.c new file +7 -0
+             }
+             errno = save_errno;
+     }
+
+/*   return rc;
+ * }
+ */
ports/linux/portdefs.h +9 -0
index 1f1a41a..565fed9 100644
@@ -82,3 +82,12 @@ extern int unshare(int flags);
 
 #define SYS_openat2 __NR_openat2
 #endif
+
+#ifndef SYS_open_tree
+
+#ifndef __NR_open_tree
+#define __NR_open_tree 428
+#endif
+
+#define SYS_open_tree __NR_open_tree
+#endif
ports/linux/wrapfuncs.in +0 -0
index 3cea20a..a51e9a0 100644
@@ -10,6 +10,7 @@ int lchown(const char *path, uid_t owner, gid_t

group); /* flags=AT_SYMLINK_NOFO

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 10

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf,

int flags);

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 11

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode});

/* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 12

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int __openat_2(int dirfd, const char *path, int flags); /*

flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 13

Quoted 1 line
ports/linux/wrapfuncs.in +1 -0
+int open_tree(int dirfd, const char *path, unsigned int flags); /*

noignore_path=1 */

Does this need preserve_paths=1 (similar to openat2())?

That would require us to account for a possible chroot in the open_tree() guts, but it would avoid us running pseudo_root_path() on an already canonicalized path which may introduce problems under a chroot.

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 14

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int mknod(const char *path, mode_t mode, dev_t dev); /*

real_func=pseudo_mknod */

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 15

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /*

real_func=pseudo_mknodat */

ports/linux/wrapfuncs.in int lchown(const char *path, uid_t owner, gid_t · line 16

Quoted 1 line
ports/linux/wrapfuncs.in +0 -0
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /*

flags=AT_SYMLINK_NOFOLLOW */

Quoted 1 line

--
Paul Barker

#4 lore raw

Re: [pseudo] [PATCH v3 1/6] ports/linux/guts: Add open_tree wrapper

Hi Dmitry,

Apologies for the slow replies here, we're all busy and pseudo is under-maintained.

Quoted 61 lines · Dmitry Sakhonchik

пн, 21 сент. 2026 г. в 12:28, Paul Barker <[email protected]>:

Quoted 59 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:
Quoted 5 lines

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is

outside the common pseudo use case.

We should at least log an error if OPEN_TREE_CLONE is seen instead of silently ignoring it.

Quoted 21 lines
---
 ports/linux/guts/open_tree.c | 46 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       |  9 +++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 56 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +6 -0
new file mode 100644
index 0000000..6c2aad9
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *

ports/linux/guts/open_tree.c line 16

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *   int rc = -1;
+ */
+
+     struct stat64 buf;
+     int save_errno;
+     char *pseudo_path;
+
+     if (real_open_tree) {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

open_tree.\n");

ports/linux/guts/open_tree.c

Quoted 3 lines
ports/linux/guts/open_tree.c new file +3 -0
+             rc = real_open_tree(dirfd, path, flags);
+     } else {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

syscall.\n");

ports/linux/guts/open_tree.c line 21

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+             rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+     }

The commit message or a comment should briefly explain why we need to handle both ways of invoking the real open_tree syscall.

ports/linux/guts/open_tree.c line 23

Quoted 2 lines
ports/linux/guts/open_tree.c new file +2 -0
+
+     pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

This should be after the (rc != -1) conditional, as this call could clobber errno.

Quoted 7 lines
Quoted 1 line

If we use preserve_path=1, then the absolute path must go through

pseudo_root_path before the real open_tree call so that a possible chroot is handled correctly.
So I think these two cases need to be somehow handled separately rather than moving pseudo_root_path() entirely below the rc != -1 check. By the way, for some reason the openat2 wrapper is ok with the pseudo_root_path outside of conditional.

The problem is that the real open_tree() call could set errno if it fails, and calling pseudo_root_path() after this before returning the error could overwrite errno. It is safe to call pseudo_root_path() before calling the real open_tree(), or to only call it if the real open_tree() call succeeds.

Best regards,

Signature · 1 line
Paul Barker
#5 lore raw

PATCH v3 2/6 test: Add open_tree test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic open_tree wrapper functionality and the AT_EMPTY_PATH case.

---
 test/test-open_tree.c  | 139 +++++++++++++++++++++++++++++++++++++++++
 test/test-open_tree.sh |   5 ++
 2 files changed, 144 insertions(+)
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
test/test-open_tree.c new file +139 -0
new file mode 100644
index 0000000..0fc02f4
@@ -0,0 +1,139 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = open_tree(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (basefd != -1) {
+        close(basefd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() || test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree.sh new file +5 -0
new file mode 100755
index 0000000..1654a0b
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+./test/test-open_tree
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree
\ No newline at end of file
#6 lore raw

Re: [pseudo] [PATCH v3 2/6] test: Add open_tree test

Quoted 144 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic open_tree wrapper functionality and the AT_EMPTY_PATH case.

---
 test/test-open_tree.c  | 139 +++++++++++++++++++++++++++++++++++++++++
 test/test-open_tree.sh |   5 ++
 2 files changed, 144 insertions(+)
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
test/test-open_tree.c new file +124 -0
new file mode 100644
index 0000000..0fc02f4
@@ -0,0 +1,139 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = open_tree(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (basefd != -1) {
+        close(basefd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}

test/test-open_tree.c line 134

Quoted 10 lines
test/test-open_tree.c new file +10 -0
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() || test_relative_dirfd();

This will short-circuit on the first failing test. We should run all test cases and accumulate the pass/fail results.

test/test-open_tree.c line 136

Quoted 2 lines
test/test-open_tree.c new file +2 -0
+
+    cleanup();

Do we need to run cleanup() between test cases? Or at least cleanup TEST_FILE between test cases?

Quoted 9 lines
test/test-open_tree.c new file +3 -0
+
+    return rc;
+}
test/test-open_tree.sh new file +0 -0
new file mode 100755
index 0000000..1654a0b

test/test-open_tree.sh line 5

Quoted 6 lines
test/test-open_tree.sh new file +5 -0
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+./test/test-open_tree
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree
\ No newline at end of file

This runs test-open_tree twice but the script exit code only depends on the second invocation. We should or these two results together.

Nitpick: Missing newline at end of file.

Best regards,

Signature · 1 line
Paul Barker
#7 lore raw

Re: [pseudo] [PATCH v3 2/6] test: Add open_tree test

пн, 21 сент. 2026 г. в 12:09, Paul Barker <[email protected]>:

Quoted 157 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:
Quoted 144 lines

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic open_tree wrapper functionality and the AT_EMPTY_PATH case.

---
 test/test-open_tree.c  | 139 +++++++++++++++++++++++++++++++++++++++++
 test/test-open_tree.sh |   5 ++
 2 files changed, 144 insertions(+)
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
test/test-open_tree.c new file +124 -0
new file mode 100644
index 0000000..0fc02f4
@@ -0,0 +1,139 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = open_tree(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (basefd != -1) {
+        close(basefd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}

test/test-open_tree.c line 134

Quoted 10 lines
test/test-open_tree.c new file +10 -0
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() ||

test_relative_dirfd();

Quoted 9 lines

This will short-circuit on the first failing test. We should run all test cases and accumulate the pass/fail results.

test/test-open_tree.c line 136

Quoted 2 lines
test/test-open_tree.c new file +2 -0
+
+    cleanup();

Do we need to run cleanup() between test cases? Or at least cleanup TEST_FILE between test cases?

I don't think so. In every test case we always open TEST_DIR with open_tree and then openat(TEST_FILE) with O_CREAT.
So I think removing this file is not necessary.

Quoted 28 lines
Quoted 10 lines
test/test-open_tree.c new file +3 -0
+
+    return rc;
+}
test/test-open_tree.sh new file +0 -0
new file mode 100755
index 0000000..1654a0b

test/test-open_tree.sh line 5

Quoted 6 lines
test/test-open_tree.sh new file +5 -0
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+./test/test-open_tree
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree
\ No newline at end of file

This runs test-open_tree twice but the script exit code only depends on the second invocation. We should or these two results together.

Nitpick: Missing newline at end of file.

Best regards,

--
Paul Barker

#8 lore raw

Re: [pseudo] [PATCH v3 2/6] test: Add open_tree test

Quoted 165 lines · Dmitry Sakhonchik

пн, 21 сент. 2026 г. в 12:09, Paul Barker <[email protected]>:

Quoted 163 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:
Quoted 144 lines

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic open_tree wrapper functionality and the AT_EMPTY_PATH case.

---
 test/test-open_tree.c  | 139 +++++++++++++++++++++++++++++++++++++++++
 test/test-open_tree.sh |   5 ++
 2 files changed, 144 insertions(+)
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
test/test-open_tree.c new file +124 -0
new file mode 100644
index 0000000..0fc02f4
@@ -0,0 +1,139 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = open_tree(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (basefd != -1) {
+        close(basefd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}

test/test-open_tree.c line 134

Quoted 10 lines
test/test-open_tree.c new file +10 -0
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() ||

test_relative_dirfd();

This will short-circuit on the first failing test. We should run all test cases and accumulate the pass/fail results.

Quoted 2 lines
test/test-open_tree.c new file +2 -0
+
+    cleanup();
Quoted 7 lines
Quoted 3 lines

Do we need to run cleanup() between test cases? Or at least cleanup TEST_FILE between test cases?

I don't think so. In every test case we always open TEST_DIR with open_tree and then openat(TEST_FILE) with O_CREAT.
So I think removing this file is not necessary.

Ok. I'm happy this isn't needed.

Best regards,

Signature · 1 line
Paul Barker
#9 lore raw

PATCH v3 3/6 ports/linux/pseudo_wrappers.c: Add syscall support for open_tree() wrapper

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

---
 ports/linux/pseudo_wrappers.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
ports/linux/pseudo_wrappers.c +14 -0
index b920cb2..33cba2d 100644
@@ -102,6 +102,20 @@ syscall(long number, ...) {
 #error SYS_openat2 not defined
 #endif
 
+#ifdef SYS_open_tree
+	if (number == SYS_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking open_tree.\n");
+		va_start(ap, number);
+		int dirfd = va_arg(ap, int);
+		const char *path = va_arg(ap, const char*);
+		unsigned int flags = va_arg(ap, unsigned int);
+
+		return wrap_open_tree(dirfd, path, flags);
+	}
+#else
+#error SYS_open_tree not defined
+#endif
+
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {
#10 lore raw

Re: [pseudo] [PATCH v3 3/6] ports/linux/pseudo_wrappers.c: Add syscall support for open_tree() wrapper

Quoted 5 lines · On Mon, 2026-09-07 at 14:18 +0300, [email protected] wrote:

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

The commit message needs to explain why a syscall intercept is needed for SYS_open_tree. We don't do this for all system calls.

Quoted 20 lines
---
 ports/linux/pseudo_wrappers.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
ports/linux/pseudo_wrappers.c +7 -0
index b920cb2..33cba2d 100644
@@ -102,6 +102,20 @@ syscall(long number, ...) {
 #error SYS_openat2 not defined
 #endif
 
+#ifdef SYS_open_tree
+	if (number == SYS_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking open_tree.\n");
+		va_start(ap, number);
+		int dirfd = va_arg(ap, int);
+		const char *path = va_arg(ap, const char*);
+		unsigned int flags = va_arg(ap, unsigned int);

ports/linux/pseudo_wrappers.c syscall(long number, ...) { · line 121

Quoted 10 lines
ports/linux/pseudo_wrappers.c +7 -0
+
+		return wrap_open_tree(dirfd, path, flags);
+	}
+#else
+#error SYS_open_tree not defined
+#endif
+
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {

Best regards,

Signature · 1 line
Paul Barker
#11 lore raw

PATCH v3 4/6 test: Add open_tree syscall test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic wrapper functionality when using syscall(SYS_open_tree, ...), also tests AT_EMPTY_PATH flag and related paths using dirfd != AT_FDCWD.

---
 test/test-open_tree-syscall.c  | 152 +++++++++++++++++++++++++++++++++
 test/test-open_tree-syscall.sh |   5 ++
 2 files changed, 157 insertions(+)
 create mode 100644 test/test-open_tree-syscall.c
 create mode 100755 test/test-open_tree-syscall.sh
test/test-open_tree-syscall.c new file +152 -0
new file mode 100644
index 0000000..03f5618
@@ -0,0 +1,152 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <sys/stat.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int do_open_tree_syscall(int dirfd, const char *path,
+                unsigned int flags)
+{
+#ifdef SYS_open_tree
+    return syscall(SYS_open_tree, dirfd, path, flags);
+#elif defined(__NR_open_tree)
+    return syscall(__NR_open_tree, dirfd, path, flags);
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = do_open_tree_syscall(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_syscall(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+    out:
+        if (basefd != -1) {
+            close(basefd);
+        }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_syscall(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() || test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree-syscall.sh new file +5 -0
new file mode 100755
index 0000000..3cb4d96
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+./test/test-open_tree-syscall
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree-syscall
\ No newline at end of file
#12 lore raw

PATCH v3 5/6 test: Add open_tree_attr syscall test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic wrapper functionality when using syscall(SYS_open_tree_attr, ...), also tests AT_EMPTY_PATH flag and related paths using dirfd != AT_FDCWD. The test uses attr == NULL, because using mount_attr in this call starts mount-specific privilege checks which end up with EPERM.

---
 test/test-open_tree_attr-syscall.c  | 165 ++++++++++++++++++++++++++++
 test/test-open_tree_attr-syscall.sh |   5 +
 2 files changed, 170 insertions(+)
 create mode 100644 test/test-open_tree_attr-syscall.c
 create mode 100755 test/test-open_tree_attr-syscall.sh
test/test-open_tree_attr-syscall.c new file +165 -0
new file mode 100644
index 0000000..4b656cb
@@ -0,0 +1,165 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <linux/mount.h>
+#include <sys/stat.h>
+
+#ifndef SYS_open_tree_attr
+
+#ifndef __NR_open_tree_attr
+#define __NR_open_tree_attr 467
+#endif
+
+#define SYS_open_tree_attr __NR_open_tree_attr
+#endif
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int do_open_tree_attr_syscall(int dirfd, const char *path,
+                unsigned int flags)
+{
+    /* Using mount_attr in this call starts mount-specific privilege checks
+     * which end up with EPERM.
+     * The functionality when using attr == NULL is equivalent to open_tree() */
+#ifdef SYS_open_tree_attr
+    return syscall(SYS_open_tree_attr, dirfd, path, flags, NULL, 0);
+#elif defined(__NR_open_tree_attr)
+    return syscall(__NR_open_tree_attr, dirfd, path, flags, NULL, 0);
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = do_open_tree_attr_syscall(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_attr_syscall(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+    out:
+        if (basefd != -1) {
+            close(basefd);
+        }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_attr_syscall(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags() || test_at_empty_path() || test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree_attr-syscall.sh new file +5 -0
new file mode 100755
index 0000000..3e50ebe
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+./test/test-open_tree_attr-syscall
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree_attr-syscall
#13 lore raw

PATCH v3 6/6 ports/linux/guts: Add open_tree_attr wrapper

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

It mostly mirrors open_tree, with the additional mount attribute arguments passed to the underlying syscall.

---
 ports/linux/guts/open_tree_attr.c | 48 +++++++++++++++++++++++++++++++
 ports/linux/portdefs.h            |  9 ++++++
 ports/linux/pseudo_wrappers.c     | 16 +++++++++++
 ports/linux/wrapfuncs.in          |  1 +
 pseudo_wrappers.c                 |  1 +
 5 files changed, 75 insertions(+)
 create mode 100644 ports/linux/guts/open_tree_attr.c
ports/linux/guts/open_tree_attr.c new file +48 -0
new file mode 100644
index 0000000..da0315c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree_attr(int dirfd, const char *path, unsigned int flags, struct mount_attr *attr, size_t size)
+ *	int rc = -1;
+ */
+
+	/* Mostly based on open_tree wrapper */
+
+	struct stat64 buf;
+	int save_errno;
+	char *pseudo_path;
+
+	if (real_open_tree_attr) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree_attr, calling open_tree_attr.\n");
+		rc = real_open_tree_attr(dirfd, path, flags, attr, size);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree_attr, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree_attr, dirfd, path, flags, attr, size);
+	}
+
+	pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path, &buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+
+		pseudo_debug(PDBGF_FILE, "open_tree_attr(path %s), flags %o, stat rc %d, stat mode %o\n",
+			pseudo_path, flags, stat_rc, buf.st_mode);
+
+		if (stat_rc != -1) {
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "open_tree_attr(fd %d, path %d/%s, flags %d) succeeded, but stat failed (%s).\n",
+				rc, dirfd, pseudo_path, flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, 0);
+		}
+		errno = save_errno;
+	}
+
+/*	return rc;
+ * }
+ */
ports/linux/portdefs.h +9 -0
index 565fed9..af8b8e8 100644
@@ -91,3 +91,12 @@ extern int unshare(int flags);
 
 #define SYS_open_tree __NR_open_tree
 #endif
+
+#ifndef SYS_open_tree_attr
+
+#ifndef __NR_open_tree_attr
+#define __NR_open_tree_attr 467
+#endif
+
+#define SYS_open_tree_attr __NR_open_tree_attr
+#endif
ports/linux/pseudo_wrappers.c +16 -0
index 33cba2d..4141556 100644
@@ -116,6 +116,22 @@ syscall(long number, ...) {
 #error SYS_open_tree not defined
 #endif
 
+#ifdef SYS_open_tree_attr
+	if (number == SYS_open_tree_attr) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking open_tree_attr.\n");
+		va_start(ap, number);
+		int dirfd = va_arg(ap, int);
+		const char *path = va_arg(ap, const char*);
+		unsigned int flags = va_arg(ap, unsigned int);
+		struct mount_attr *attr = va_arg(ap, struct mount_attr*);
+		size_t size = va_arg(ap, size_t);
+
+		return wrap_open_tree_attr(dirfd, path, flags, attr, size);
+	}
+#else
+#error SYS_open_tree_attr not defined
+#endif
+
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {
ports/linux/wrapfuncs.in +1 -0
index a51e9a0..639d9a5 100644
@@ -11,6 +11,7 @@ int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int open_tree(int dirfd, const char *path, unsigned int flags); /* noignore_path=1 */
+int open_tree_attr(int dirfd, const char *path, unsigned int flags, struct mount_attr *attr, size_t size); /* noignore_path=1 */
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
pseudo_wrappers.c +1 -0
index 112239b..78cdecf 100644
@@ -37,6 +37,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <utime.h>
+#include <sys/mount.h>
 #ifdef PSEUDO_PORT_LINUX_STATVFS
 #include <sys/statvfs.h>
 #endif