Skip to content

Commit

Permalink
LibC: Mark a bunch of functions as cancellation points
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi authored and bgianfo committed Jul 22, 2022
1 parent 899fd74 commit c85f307
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Userland/Libraries/LibC/fcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <bits/pthread_cancel.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibC/pthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/pthread_cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions Userland/Libraries/LibC/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -175,13 +176,17 @@ 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);
}

// 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)
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/stdlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/sys/mman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <AK/Format.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibC/sys/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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) {
Expand All @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions Userland/Libraries/LibC/sys/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <AK/Assertions.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -86,13 +93,17 @@ 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);
}

// 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;
Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibC/sys/uio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibC/sys/wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <sys/wait.h>
#include <syscall.h>
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/termios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <assert.h>
#include <bits/pthread_cancel.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions Userland/Libraries/LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -374,27 +375,35 @@ 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);
}

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

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

// 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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c85f307

Please sign in to comment.