LibC: Mark a bunch of functions as cancellation points

This commit is contained in:
Tim Schumacher 2022-06-12 15:15:09 +02:00 committed by Brian Gianforcaro
parent 899fd74f8e
commit c85f307e62
15 changed files with 78 additions and 0 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@ -17,6 +18,8 @@ extern "C" {
int fcntl(int fd, int cmd, ...)
{
__pthread_maybe_cancel();
va_list ap;
va_start(ap, cmd);
uintptr_t extra_arg = va_arg(ap, uintptr_t);
@ -46,11 +49,15 @@ int inode_watcher_remove_watch(int fd, int wd)
int creat(char const* path, mode_t mode)
{
__pthread_maybe_cancel();
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
}
int open(char const* path, int options, ...)
{
__pthread_maybe_cancel();
if (!path) {
errno = EFAULT;
return -1;
@ -71,6 +78,8 @@ int open(char const* path, int options, ...)
int openat(int dirfd, char const* path, int options, ...)
{
__pthread_maybe_cancel();
if (!path) {
errno = EFAULT;
return -1;

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <poll.h>
#include <sys/time.h>
@ -14,6 +15,8 @@ extern "C" {
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
int poll(pollfd* fds, nfds_t nfds, int timeout_ms)
{
__pthread_maybe_cancel();
timespec timeout;
timespec* timeout_ts = &timeout;
if (timeout_ms < 0)

View file

@ -192,6 +192,8 @@ void pthread_cleanup_pop(int execute)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html
int pthread_join(pthread_t thread, void** exit_value_ptr)
{
__pthread_maybe_cancel();
int rc = syscall(SC_join_thread, thread, exit_value_ptr);
__RETURN_PTHREAD_ERROR(rc);
}

View file

@ -8,6 +8,7 @@
#include <AK/Assertions.h>
#include <AK/Atomic.h>
#include <AK/Types.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <pthread.h>
#include <serenity.h>
@ -87,6 +88,8 @@ int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html
int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
{
__pthread_maybe_cancel();
// Save the mutex this condition variable is associated with. We don't (yet)
// support changing this mutex once set.
pthread_mutex_t* old_mutex = AK::atomic_exchange(&cond->mutex, mutex, AK::memory_order_relaxed);

View file

@ -12,6 +12,7 @@
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
@ -310,6 +311,8 @@ int sem_wait(sem_t* sem)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_timedwait.html
int sem_timedwait(sem_t* sem, const struct timespec* abstime)
{
__pthread_maybe_cancel();
if (sem->magic != SEM_MAGIC) {
errno = EINVAL;
return -1;

View file

@ -6,6 +6,7 @@
#include <AK/Format.h>
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
@ -175,6 +176,8 @@ void siglongjmp(jmp_buf env, int val)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigsuspend.html
int sigsuspend(sigset_t const* set)
{
__pthread_maybe_cancel();
int rc = syscall(SC_sigsuspend, set);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -182,6 +185,8 @@ int sigsuspend(sigset_t const* set)
// https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
int sigwait(sigset_t const* set, int* sig)
{
__pthread_maybe_cancel();
int rc = syscall(Syscall::SC_sigtimedwait, set, nullptr, nullptr);
VERIFY(rc != 0);
if (rc < 0)
@ -199,6 +204,8 @@ int sigwaitinfo(sigset_t const* set, siginfo_t* info)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigtimedwait.html
int sigtimedwait(sigset_t const* set, siginfo_t* info, struct timespec const* timeout)
{
__pthread_maybe_cancel();
int rc = syscall(Syscall::SC_sigtimedwait, set, info, timeout);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

View file

@ -14,6 +14,7 @@
#include <LibELF/AuxiliaryVector.h>
#include <alloca.h>
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@ -810,6 +811,8 @@ void srandom(unsigned seed)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html
int system(char const* command)
{
__pthread_maybe_cancel();
if (!command)
return 1;

View file

@ -5,6 +5,7 @@
*/
#include <AK/Format.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
@ -110,6 +111,8 @@ int munlock(void const*, size_t)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/msync.html
int msync(void* address, size_t size, int flags)
{
__pthread_maybe_cancel();
int rc = syscall(SC_msync, address, size, flags);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <stdio.h>
#include <sys/poll.h>
@ -18,6 +19,8 @@ extern "C" {
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/select.html
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout_tv)
{
__pthread_maybe_cancel();
timespec* timeout_ts = nullptr;
timespec timeout;
if (timeout_tv) {
@ -30,6 +33,8 @@ int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timev
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pselect.html
int pselect(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timespec const* timeout, sigset_t const* sigmask)
{
__pthread_maybe_cancel();
Vector<pollfd, FD_SETSIZE> fds;
if (nfds < 0 || static_cast<size_t>(nfds) >= fds.capacity())

View file

@ -5,6 +5,7 @@
*/
#include <AK/Assertions.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
@ -38,6 +39,8 @@ int listen(int sockfd, int backlog)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
int accept(int sockfd, sockaddr* addr, socklen_t* addrlen)
{
__pthread_maybe_cancel();
return accept4(sockfd, addr, addrlen, 0);
}
@ -51,6 +54,8 @@ int accept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
int connect(int sockfd, sockaddr const* addr, socklen_t addrlen)
{
__pthread_maybe_cancel();
int rc = syscall(SC_connect, sockfd, addr, addrlen);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -65,6 +70,8 @@ int shutdown(int sockfd, int how)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html
ssize_t sendmsg(int sockfd, const struct msghdr* msg, int flags)
{
__pthread_maybe_cancel();
int rc = syscall(SC_sendmsg, sockfd, msg, flags);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -86,6 +93,8 @@ ssize_t send(int sockfd, void const* data, size_t data_length, int flags)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html
ssize_t recvmsg(int sockfd, struct msghdr* msg, int flags)
{
__pthread_maybe_cancel();
int rc = syscall(SC_recvmsg, sockfd, msg, flags);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -93,6 +102,8 @@ ssize_t recvmsg(int sockfd, struct msghdr* msg, int flags)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html
ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* addr, socklen_t* addr_length)
{
__pthread_maybe_cancel();
if (!addr_length && addr) {
errno = EINVAL;
return -1;

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <sys/uio.h>
#include <syscall.h>
@ -12,12 +13,16 @@ extern "C" {
ssize_t writev(int fd, const struct iovec* iov, int iov_count)
{
__pthread_maybe_cancel();
int rc = syscall(SC_writev, fd, iov, iov_count);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
ssize_t readv(int fd, const struct iovec* iov, int iov_count)
{
__pthread_maybe_cancel();
int rc = syscall(SC_readv, fd, iov, iov_count);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

View file

@ -5,6 +5,7 @@
*/
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <sys/wait.h>
#include <syscall.h>
@ -21,6 +22,8 @@ pid_t wait(int* wstatus)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html
pid_t waitpid(pid_t waitee, int* wstatus, int options)
{
__pthread_maybe_cancel();
siginfo_t siginfo;
idtype_t idtype;
id_t id;
@ -78,6 +81,8 @@ pid_t waitpid(pid_t waitee, int* wstatus, int options)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/waitid.html
int waitid(idtype_t idtype, id_t id, siginfo_t* infop, int options)
{
__pthread_maybe_cancel();
Syscall::SC_waitid_params params { idtype, id, infop, options };
int rc = syscall(SC_waitid, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);

View file

@ -5,6 +5,7 @@
*/
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
@ -51,6 +52,8 @@ int tcflush(int fd, int queue_selector)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/tcdrain.html
int tcdrain([[maybe_unused]] int fd)
{
__pthread_maybe_cancel();
// FIXME: Implement this for real.
return 0;
}

View file

@ -11,6 +11,7 @@
#include <Kernel/API/TimePage.h>
#include <LibTimeZone/TimeZone.h>
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
@ -458,6 +459,8 @@ int clock_settime(clockid_t clock_id, struct timespec* ts)
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
{
__pthread_maybe_cancel();
Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
int rc = syscall(SC_clock_nanosleep, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);

View file

@ -10,6 +10,7 @@
#include <AK/Vector.h>
#include <alloca.h>
#include <assert.h>
#include <bits/pthread_cancel.h>
#include <bits/pthread_integration.h>
#include <dirent.h>
#include <errno.h>
@ -374,6 +375,8 @@ pid_t getpgrp()
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
ssize_t read(int fd, void* buf, size_t count)
{
__pthread_maybe_cancel();
int rc = syscall(SC_read, fd, buf, count);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -381,6 +384,8 @@ ssize_t read(int fd, void* buf, size_t count)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
ssize_t pread(int fd, void* buf, size_t count, off_t offset)
{
__pthread_maybe_cancel();
int rc = syscall(SC_pread, fd, buf, count, &offset);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -388,6 +393,8 @@ ssize_t pread(int fd, void* buf, size_t count, off_t offset)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
ssize_t write(int fd, void const* buf, size_t count)
{
__pthread_maybe_cancel();
int rc = syscall(SC_write, fd, buf, count);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -395,6 +402,8 @@ ssize_t write(int fd, void const* buf, size_t count)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
ssize_t pwrite(int fd, void const* buf, size_t count, off_t offset)
{
__pthread_maybe_cancel();
// FIXME: This is not thread safe and should be implemented in the kernel instead.
off_t old_offset = lseek(fd, 0, SEEK_CUR);
lseek(fd, offset, SEEK_SET);
@ -484,6 +493,8 @@ char* ttyname(int fd)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
int close(int fd)
{
__pthread_maybe_cancel();
int rc = syscall(SC_close, fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
@ -891,6 +902,8 @@ int sysbeep()
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html
int fsync(int fd)
{
__pthread_maybe_cancel();
int rc = syscall(SC_fsync, fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}