mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:17:44 +00:00
Kernel: Use the Syscall string and buffer types more
While I was updating syscalls to stop passing null-terminated strings, I added some helpful struct types: - StringArgument { const char*; size_t; } - ImmutableBuffer<Data, Size> { const Data*; Size; } - MutableBuffer<Data, Size> { Data*; Size; } The Process class has some convenience functions for validating and optionally extracting the contents from these structs: - get_syscall_path_argument(StringArgument) - validate_and_copy_string_from_user(StringArgument) - validate(ImmutableBuffer) - validate(MutableBuffer) There's still so much code around this and I'm wondering if we should generate most of it instead. Possible nice little project.
This commit is contained in:
parent
1434f30f92
commit
24c736b0e7
8 changed files with 148 additions and 121 deletions
|
@ -33,22 +33,30 @@ int creat_with_path_length(const char* path, size_t path_length, mode_t mode)
|
|||
|
||||
int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode)
|
||||
{
|
||||
if (!path) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
if (path_length > INT32_MAX) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
Syscall::SC_open_params params { path, (int)path_length, options, mode };
|
||||
Syscall::SC_open_params params { { path, path_length }, options, mode };
|
||||
int rc = syscall(SC_open, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
||||
int openat_with_path_length(int dirfd, const char* path, size_t path_length, int options, mode_t mode)
|
||||
{
|
||||
if (!path) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
if (path_length > INT32_MAX) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
Syscall::SC_openat_params params { dirfd, path, (int)path_length, options, mode };
|
||||
Syscall::SC_openat_params params { dirfd, { path, path_length }, options, mode };
|
||||
int rc = syscall(SC_openat, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
|
|||
|
||||
void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, const char* name)
|
||||
{
|
||||
Syscall::SC_mmap_params params { (u32)addr, size, prot, flags, fd, offset, name, name ? strlen(name) : 0 };
|
||||
Syscall::SC_mmap_params params { (u32)addr, size, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
|
||||
int rc = syscall(SC_mmap, ¶ms);
|
||||
if (rc < 0 && -rc < EMAXERRNO) {
|
||||
errno = -rc;
|
||||
|
@ -40,7 +40,7 @@ int set_mmap_name(void* addr, size_t size, const char* name)
|
|||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
Syscall::SC_set_mmap_name_params params { addr, size, name, strlen(name) };
|
||||
Syscall::SC_set_mmap_name_params params { addr, size, { name, strlen(name) } };
|
||||
int rc = syscall(SC_set_mmap_name, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
|
|
@ -711,7 +711,7 @@ char* realpath(const char* pathname, char* buffer)
|
|||
size_t size = PATH_MAX;
|
||||
if (buffer == nullptr)
|
||||
buffer = (char*)malloc(size);
|
||||
Syscall::SC_realpath_params params { pathname, strlen(pathname), buffer, size };
|
||||
Syscall::SC_realpath_params params { { pathname, strlen(pathname) }, { buffer, size } };
|
||||
int rc = syscall(SC_realpath, ¶ms);
|
||||
if (rc < 0) {
|
||||
errno = -rc;
|
||||
|
|
|
@ -38,7 +38,7 @@ int connect(int sockfd, const sockaddr* addr, socklen_t addrlen)
|
|||
|
||||
ssize_t sendto(int sockfd, const void* data, size_t data_length, int flags, const struct sockaddr* addr, socklen_t addr_length)
|
||||
{
|
||||
Syscall::SC_sendto_params params { sockfd, data, data_length, flags, addr, addr_length };
|
||||
Syscall::SC_sendto_params params { sockfd, { data, data_length }, flags, addr, addr_length };
|
||||
int rc = syscall(SC_sendto, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ ssize_t send(int sockfd, const void* data, size_t data_length, int flags)
|
|||
|
||||
ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* addr, socklen_t* addr_length)
|
||||
{
|
||||
Syscall::SC_recvfrom_params params { sockfd, buffer, buffer_length, flags, addr, addr_length };
|
||||
Syscall::SC_recvfrom_params params { sockfd, { buffer, buffer_length }, flags, addr, addr_length };
|
||||
int rc = syscall(SC_recvfrom, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
|
|
@ -539,11 +539,19 @@ int pthread_setspecific(pthread_key_t key, const void* value)
|
|||
}
|
||||
int pthread_setname_np(pthread_t thread, const char* buffer, int buffer_size)
|
||||
{
|
||||
if (buffer_size < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return syscall(SC_set_thread_name, thread, buffer, buffer_size);
|
||||
}
|
||||
|
||||
int pthread_getname_np(pthread_t thread, char* buffer, int buffer_size)
|
||||
{
|
||||
if (buffer_size < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return syscall(SC_get_thread_name, thread, buffer, buffer_size);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue