Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement various syscalls on Linux for systemd #1197

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions options/linux/generic/linux-unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
37 changes: 28 additions & 9 deletions options/linux/generic/sched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,17 +38,32 @@ 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 *) {
__ensure(!"Not implemented");
__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;
}
14 changes: 10 additions & 4 deletions options/linux/generic/sys-sendfile.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@

#include <errno.h>
#include <sys/sendfile.h>
#include <bits/ensure.h>
#include <mlibc/linux-sysdeps.hpp>

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;
}

6 changes: 6 additions & 0 deletions options/linux/include/mlibc/linux-sysdeps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ 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);
[[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

#endif // MLIBX_LINUX_SYSDEPS
21 changes: 15 additions & 6 deletions options/posix/generic/fcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,28 @@ 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) {
__ensure(!"Not implemented");
__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) {
Expand Down
3 changes: 3 additions & 0 deletions options/posix/include/mlibc/posix-sysdeps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ 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

#endif // MLIBC_POSIX_SYSDEPS
66 changes: 66 additions & 0 deletions sysdeps/linux/generic/sysdeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,72 @@ 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<size_t>(ret);
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;
}

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<int>(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<size_t>(ret);
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;
}

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<pid_t>(ret);
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;
}

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);
Expand Down
Loading