From f9b00252d37d74493f375e2e8209ab08e59fa289 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:30:05 +0100 Subject: [PATCH 1/8] sysdeps/linux: Implement sys_sendfile Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/linux/generic/sys-sendfile.cpp | 14 ++++++++++---- options/linux/include/mlibc/linux-sysdeps.hpp | 2 ++ sysdeps/linux/generic/sysdeps.cpp | 9 +++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/options/linux/generic/sys-sendfile.cpp b/options/linux/generic/sys-sendfile.cpp index 25cd0a0eae..4cbf3d7bfa 100644 --- a/options/linux/generic/sys-sendfile.cpp +++ b/options/linux/generic/sys-sendfile.cpp @@ -1,9 +1,15 @@ - +#include #include #include +#include -ssize_t sendfile(int, int, off_t *, size_t) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +ssize_t sendfile(int outfd, int infd, off_t *offset, size_t size) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_sendfile, -1); + size_t out; + if(int e = mlibc::sys_sendfile(outfd, infd, offset, size, &out); e) { + errno = e; + return -1; + } + return out; } diff --git a/options/linux/include/mlibc/linux-sysdeps.hpp b/options/linux/include/mlibc/linux-sysdeps.hpp index 0ac22f185a..3a2d498c9a 100644 --- a/options/linux/include/mlibc/linux-sysdeps.hpp +++ b/options/linux/include/mlibc/linux-sysdeps.hpp @@ -83,6 +83,8 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result); [[gnu::weak]] int sys_getifaddrs(struct ifaddrs **); +[[gnu::weak]] int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out); + } // namespace mlibc #endif // MLIBX_LINUX_SYSDEPS diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index 1db9a7ef54..445197ad95 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -1997,6 +1997,15 @@ int sys_sigtimedwait(const sigset_t *__restrict set, siginfo_t *__restrict info, return 0; } +int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out) { + auto ret = do_syscall(SYS_sendfile, outfd, infd, offset, count); + if(int e = sc_error(ret); e) { + return e; + } + *out = sc_int_result(ret); + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 8ab4edd8a54c3577fa118b1aa86354a81d63bef4 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:34:23 +0100 Subject: [PATCH 2/8] sysdeps/linux: Implement sys_syncfs Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/linux/generic/linux-unistd.cpp | 10 +++++++--- options/linux/include/mlibc/linux-sysdeps.hpp | 1 + sysdeps/linux/generic/sysdeps.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/options/linux/generic/linux-unistd.cpp b/options/linux/generic/linux-unistd.cpp index ec13f72b01..f5e330b8fc 100644 --- a/options/linux/generic/linux-unistd.cpp +++ b/options/linux/generic/linux-unistd.cpp @@ -27,7 +27,11 @@ int getdtablesize(void){ return sysconf(_SC_OPEN_MAX); } -int syncfs(int) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int syncfs(int fd) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_syncfs, -1); + if(int e = mlibc::sys_syncfs(fd); e) { + errno = e; + return -1; + } + return 0; } diff --git a/options/linux/include/mlibc/linux-sysdeps.hpp b/options/linux/include/mlibc/linux-sysdeps.hpp index 3a2d498c9a..44fdfceb40 100644 --- a/options/linux/include/mlibc/linux-sysdeps.hpp +++ b/options/linux/include/mlibc/linux-sysdeps.hpp @@ -84,6 +84,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result); [[gnu::weak]] int sys_getifaddrs(struct ifaddrs **); [[gnu::weak]] int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out); +[[gnu::weak]] int sys_syncfs(int fd); } // namespace mlibc diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index 445197ad95..633d91e96e 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2006,6 +2006,14 @@ int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out) return 0; } +int sys_syncfs(int fd) { + auto ret = do_syscall(SYS_syncfs, fd); + if(int e = sc_error(ret); e) { + return e; + } + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 14fbaf02597d69c05c5adb0b50a86bcfb39fab88 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:36:42 +0100 Subject: [PATCH 3/8] sysdeps/linux: Implement sys_name_to_handle_at Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/posix/generic/fcntl.cpp | 10 +++++++--- options/posix/include/mlibc/posix-sysdeps.hpp | 2 ++ sysdeps/linux/generic/sysdeps.cpp | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/options/posix/generic/fcntl.cpp b/options/posix/generic/fcntl.cpp index c05d51b59d..969f2cc26b 100644 --- a/options/posix/generic/fcntl.cpp +++ b/options/posix/generic/fcntl.cpp @@ -68,9 +68,13 @@ int posix_fallocate(int fd, off_t offset, off_t size) { } // This is a linux extension -int name_to_handle_at(int, const char *, struct file_handle *, int *, int) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int name_to_handle_at(int dirfd, const char *pathname, struct file_handle *handle, int *mount_id, int flags) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_name_to_handle_at, -1); + if(int e = mlibc::sys_name_to_handle_at(dirfd, pathname, handle, mount_id, flags); e) { + errno = e; + return -1; + } + return 0; } int open_by_handle_at(int, struct file_handle *, int) { diff --git a/options/posix/include/mlibc/posix-sysdeps.hpp b/options/posix/include/mlibc/posix-sysdeps.hpp index a224474be8..74669d6c56 100644 --- a/options/posix/include/mlibc/posix-sysdeps.hpp +++ b/options/posix/include/mlibc/posix-sysdeps.hpp @@ -236,6 +236,8 @@ int sys_vm_unmap(void *pointer, size_t size); [[gnu::weak]] int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options); +[[gnu::weak]] int sys_name_to_handle_at(int dirfd, const char *pathname, struct file_handle *handle, int *mount_id, int flags); + } //namespace mlibc #endif // MLIBC_POSIX_SYSDEPS diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index 633d91e96e..728307c360 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2014,6 +2014,13 @@ int sys_syncfs(int fd) { return 0; } +int sys_name_to_handle_at(int dirfd, const char *pathname, struct file_handle *handle, int *mount_id, int flags) { + auto ret = do_syscall(SYS_name_to_handle_at, dirfd, pathname, handle, mount_id, flags); + if (int e = sc_error(ret); e) + return e; + return sc_int_result(ret); +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 9db709a64ba892da1f76c35fb0e3ca586c3365ca Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:38:35 +0100 Subject: [PATCH 4/8] sysdeps/linux: Implement sys_splice Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/posix/generic/fcntl.cpp | 11 ++++++++--- options/posix/include/mlibc/posix-sysdeps.hpp | 1 + sysdeps/linux/generic/sysdeps.cpp | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/options/posix/generic/fcntl.cpp b/options/posix/generic/fcntl.cpp index 969f2cc26b..38936ee4ca 100644 --- a/options/posix/generic/fcntl.cpp +++ b/options/posix/generic/fcntl.cpp @@ -82,9 +82,14 @@ int open_by_handle_at(int, struct file_handle *, int) { __builtin_unreachable(); } -ssize_t splice(int, off_t *, int, off_t *, size_t, unsigned int) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +ssize_t splice(int in_fd, off_t *in_off, int out_fd, off_t *out_off, size_t size, unsigned int flags) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_splice, -1); + size_t ret; + if(int e = mlibc::sys_splice(in_fd, in_off, out_fd, out_off, size, flags, &ret); e) { + errno = e; + return -1; + } + return ret; } ssize_t vmsplice(int, const struct iovec *, size_t, unsigned int) { diff --git a/options/posix/include/mlibc/posix-sysdeps.hpp b/options/posix/include/mlibc/posix-sysdeps.hpp index 74669d6c56..e525acfe6b 100644 --- a/options/posix/include/mlibc/posix-sysdeps.hpp +++ b/options/posix/include/mlibc/posix-sysdeps.hpp @@ -237,6 +237,7 @@ int sys_vm_unmap(void *pointer, size_t size); [[gnu::weak]] int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options); [[gnu::weak]] int sys_name_to_handle_at(int dirfd, const char *pathname, struct file_handle *handle, int *mount_id, int flags); +[[gnu::weak]] int sys_splice(int in_fd, off_t *in_off, int out_fd, off_t *out_off, size_t size, unsigned int flags, size_t *out); } //namespace mlibc diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index 728307c360..30c50e6eaf 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2021,6 +2021,15 @@ int sys_name_to_handle_at(int dirfd, const char *pathname, struct file_handle *h return sc_int_result(ret); } +int sys_splice(int in_fd, off_t *in_off, int out_fd, off_t *out_off, size_t size, unsigned int flags, size_t *out) { + auto ret = do_syscall(SYS_copy_file_range, in_fd, in_off, out_fd, out_off, size, flags); + if(int e = sc_error(ret); e) { + return e; + } + *out = sc_int_result(ret); + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 8c962c8356fe0f98122ff3772324e45d85bfa5a2 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:40:40 +0100 Subject: [PATCH 5/8] sysdeps/linux: Implement sys_unshare Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/linux/generic/sched.cpp | 10 +++++++--- options/linux/include/mlibc/linux-sysdeps.hpp | 1 + sysdeps/linux/generic/sysdeps.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/options/linux/generic/sched.cpp b/options/linux/generic/sched.cpp index 2dc5210418..9de2a56d45 100644 --- a/options/linux/generic/sched.cpp +++ b/options/linux/generic/sched.cpp @@ -34,9 +34,13 @@ int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) { return 0; } -int unshare(int) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int unshare(int flags) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_unshare, -1); + if(int e = mlibc::sys_unshare(flags); e) { + errno = e; + return -1; + } + return 0; } int sched_setaffinity(pid_t, size_t, const cpu_set_t *) { diff --git a/options/linux/include/mlibc/linux-sysdeps.hpp b/options/linux/include/mlibc/linux-sysdeps.hpp index 44fdfceb40..2275a0c931 100644 --- a/options/linux/include/mlibc/linux-sysdeps.hpp +++ b/options/linux/include/mlibc/linux-sysdeps.hpp @@ -85,6 +85,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result); [[gnu::weak]] int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out); [[gnu::weak]] int sys_syncfs(int fd); +[[gnu::weak]] int sys_unshare(int flags); } // namespace mlibc diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index 30c50e6eaf..d9aecda99a 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2030,6 +2030,14 @@ int sys_splice(int in_fd, off_t *in_off, int out_fd, off_t *out_off, size_t size return 0; } +int sys_unshare(int flags) { + auto ret = do_syscall(SYS_unshare, flags); + if(int e = sc_error(ret); e) { + return e; + } + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 92966de5598cffa0e77e53b85ebec02f17f5209a Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:41:46 +0100 Subject: [PATCH 6/8] sysdeps/linux: Implement clone Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/linux/generic/sched.cpp | 17 ++++++++++++++--- options/linux/include/mlibc/linux-sysdeps.hpp | 1 + sysdeps/linux/generic/sysdeps.cpp | 9 +++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/options/linux/generic/sched.cpp b/options/linux/generic/sched.cpp index 9de2a56d45..046ea0f06d 100644 --- a/options/linux/generic/sched.cpp +++ b/options/linux/generic/sched.cpp @@ -48,7 +48,18 @@ int sched_setaffinity(pid_t, size_t, const cpu_set_t *) { __builtin_unreachable(); } -int clone(int (*)(void *), void *, int, void *, ...) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int clone(int (*fn)(void *), void *stack, int flags, void *arg, ...) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_linux_clone, -1); + if(stack) { + mlibc::infoLogger() << "clone: stack argument is ignored" << frg::endlog; + } + pid_t ret; + if(int e = mlibc::sys_linux_clone(flags, NULL, &ret); e) { + errno = e; + return -1; + } + if(!ret) { + mlibc::sys_exit(fn(arg)); + } + return ret; } diff --git a/options/linux/include/mlibc/linux-sysdeps.hpp b/options/linux/include/mlibc/linux-sysdeps.hpp index 2275a0c931..f5b7169669 100644 --- a/options/linux/include/mlibc/linux-sysdeps.hpp +++ b/options/linux/include/mlibc/linux-sysdeps.hpp @@ -86,6 +86,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result); [[gnu::weak]] int sys_sendfile(int outfd, int infd, off_t *offset, size_t count, size_t *out); [[gnu::weak]] int sys_syncfs(int fd); [[gnu::weak]] int sys_unshare(int flags); +[[gnu::weak]] int sys_linux_clone(int flags, void *stack, pid_t *out); } // namespace mlibc diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index d9aecda99a..fe70f4c733 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2038,6 +2038,15 @@ int sys_unshare(int flags) { return 0; } +int sys_linux_clone(int flags, void *stack, pid_t *out) { + auto ret = do_syscall(SYS_clone, flags, stack); + if(int e = sc_error(ret); e) { + return e; + } + *out = sc_int_result(ret); + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 3b8fd7a66ffd34d9035de78e6fd8009d538a38bd Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:43:17 +0100 Subject: [PATCH 7/8] sysdeps/linux: Implement sys_setns Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- options/linux/generic/sched.cpp | 10 +++++++--- options/linux/include/mlibc/linux-sysdeps.hpp | 1 + sysdeps/linux/generic/sysdeps.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/options/linux/generic/sched.cpp b/options/linux/generic/sched.cpp index 046ea0f06d..c7116031d4 100644 --- a/options/linux/generic/sched.cpp +++ b/options/linux/generic/sched.cpp @@ -15,9 +15,13 @@ int sched_getcpu(void) { return cpu; } -int setns(int, int) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int setns(int fd, int nstype) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setns, -1); + if(int e = mlibc::sys_setns(fd, nstype); e) { + errno = e; + return -1; + } + return 0; } int sched_getscheduler(pid_t) { diff --git a/options/linux/include/mlibc/linux-sysdeps.hpp b/options/linux/include/mlibc/linux-sysdeps.hpp index f5b7169669..6a51afde76 100644 --- a/options/linux/include/mlibc/linux-sysdeps.hpp +++ b/options/linux/include/mlibc/linux-sysdeps.hpp @@ -87,6 +87,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result); [[gnu::weak]] int sys_syncfs(int fd); [[gnu::weak]] int sys_unshare(int flags); [[gnu::weak]] int sys_linux_clone(int flags, void *stack, pid_t *out); +[[gnu::weak]] int sys_setns(int fd, int nstype); } // namespace mlibc diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index fe70f4c733..e32c58425a 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2047,6 +2047,14 @@ int sys_linux_clone(int flags, void *stack, pid_t *out) { return 0; } +int sys_setns(int fd, int nstype) { + auto ret = do_syscall(SYS_setns, fd, nstype); + if(int e = sc_error(ret); e) { + return e; + } + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0); From 45fbbdccc3da5ce3cd901ddf0882c14886d48906 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 13 Dec 2024 18:44:04 +0100 Subject: [PATCH 8/8] sysdeps/linux: Implement sys_setgroups Co-authored-by: 48cf <32851089+48cf@users.noreply.github.com> --- sysdeps/linux/generic/sysdeps.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sysdeps/linux/generic/sysdeps.cpp b/sysdeps/linux/generic/sysdeps.cpp index e32c58425a..026550a577 100644 --- a/sysdeps/linux/generic/sysdeps.cpp +++ b/sysdeps/linux/generic/sysdeps.cpp @@ -2055,6 +2055,14 @@ int sys_setns(int fd, int nstype) { return 0; } +int sys_setgroups(size_t size, const gid_t *list) { + auto ret = do_syscall(SYS_setgroups, size, list); + if(int e = sc_error(ret); e) { + return e; + } + return 0; +} + #if __MLIBC_BSD_OPTION int sys_brk(void **out) { auto ret = do_syscall(SYS_brk, 0);