libc: INTERPOS_SYS macro for interposed syscalls

This macro makes uses the __sys_<foo>_t typedefs from libsys.h to
greatly simplify calling functions in the interposing table.

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D44389
This commit is contained in:
Brooks Davis
2024-04-16 17:48:08 +01:00
parent 792081a730
commit 7dd9070e44
49 changed files with 56 additions and 123 deletions
+1 -3
View File
@@ -55,9 +55,7 @@ __sleep(unsigned int seconds)
time_to_sleep.tv_sec = seconds;
time_to_sleep.tv_nsec = 0;
if (((int (*)(const struct timespec *, struct timespec *))
(*__libc_interposing_slot(INTERPOS_nanosleep)))(
&time_to_sleep, &time_remaining) != -1)
if (INTERPOS_SYS(nanosleep, &time_to_sleep, &time_remaining) != -1)
return (0);
if (errno != EINTR)
return (seconds); /* best guess */
+1 -3
View File
@@ -45,9 +45,7 @@ __usleep(useconds_t useconds)
time_to_sleep.tv_nsec = (useconds % 1000000) * 1000;
time_to_sleep.tv_sec = useconds / 1000000;
return (((int (*)(const struct timespec *, struct timespec *))
(*__libc_interposing_slot(INTERPOS_nanosleep)))(&time_to_sleep,
NULL));
return (INTERPOS_SYS(nanosleep, &time_to_sleep, NULL));
}
__weak_reference(__usleep, usleep);
+6
View File
@@ -252,6 +252,12 @@ enum {
INTERPOS_MAX
};
#define _INTERPOS_SYS(type, idx, ...) \
((type *)*(__libc_interposing_slot(idx)))(__VA_ARGS__)
#define INTERPOS_SYS(syscall, ...) \
_INTERPOS_SYS(__sys_## syscall ##_t, INTERPOS_## syscall \
__VA_OPT__(,) __VA_ARGS__)
/*
* yplib internal interfaces
*/
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_accept, __accept);
int
accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
return (((int (*)(int, struct sockaddr *, socklen_t *))
*(__libc_interposing_slot(INTERPOS_accept)))(s, addr, addrlen));
return (INTERPOS_SYS(accept, s, addr, addrlen));
}
+1 -3
View File
@@ -40,7 +40,5 @@ __weak_reference(__sys_accept4, __accept4);
int
accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
return (((int (*)(int, struct sockaddr *, socklen_t *, int))
*(__libc_interposing_slot(INTERPOS_accept4)))
(s, addr, addrlen, flags));
return (INTERPOS_SYS(accept4, s, addr, addrlen, flags));
}
+1 -4
View File
@@ -40,8 +40,5 @@ int
aio_suspend(const struct aiocb * const iocbs[], int niocb,
const struct timespec *timeout)
{
return (((int (*)(const struct aiocb * const[], int,
const struct timespec *))
*(__libc_interposing_slot(INTERPOS_aio_suspend)))
(iocbs, niocb, timeout));
return (INTERPOS_SYS(aio_suspend, iocbs, niocb, timeout));
}
+1 -4
View File
@@ -41,8 +41,5 @@ int
clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
struct timespec *rmtp)
{
return (((int (*)(clockid_t, int, const struct timespec *,
struct timespec *))
*(__libc_interposing_slot(INTERPOS_clock_nanosleep)))
(clock_id, flags, rqtp, rmtp));
return (INTERPOS_SYS(clock_nanosleep, clock_id, flags, rqtp, rmtp));
}
+1 -1
View File
@@ -40,5 +40,5 @@ __weak_reference(__sys_close, __close);
int
close(int fd)
{
return (((int (*)(int))*(__libc_interposing_slot(INTERPOS_close)))(fd));
return (INTERPOS_SYS(close, fd));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_connect, __connect);
int
connect(int s, const struct sockaddr *addr, socklen_t addrlen)
{
return (((int (*)(int, const struct sockaddr *, socklen_t))
*(__libc_interposing_slot(INTERPOS_connect)))(s, addr, addrlen));
return (INTERPOS_SYS(connect, s, addr, addrlen));
}
+2 -3
View File
@@ -43,8 +43,7 @@ int __creat(const char *path, mode_t mode);
int
__creat(const char *path, mode_t mode)
{
return (((int (*)(int, const char *, int, ...))
*(__libc_interposing_slot(INTERPOS_openat)))
(AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC, mode));
return (INTERPOS_SYS(openat, AT_FDCWD, path,
O_WRONLY | O_CREAT | O_TRUNC, mode));
}
+1 -2
View File
@@ -49,6 +49,5 @@ fcntl(int fd, int cmd, ...)
arg = va_arg(args, long);
va_end(args);
return (((int (*)(int, int, ...))
*(__libc_interposing_slot(INTERPOS_fcntl)))(fd, cmd, arg));
return (INTERPOS_SYS(fcntl, fd, cmd, arg));
}
+1 -2
View File
@@ -37,6 +37,5 @@
int
fdatasync(int fd)
{
return (((int (*)(int))*(__libc_interposing_slot(INTERPOS_fdatasync)))
(fd));
return (INTERPOS_SYS(fdatasync, fd));
}
+1 -1
View File
@@ -39,5 +39,5 @@ __weak_reference(__sys_fork, __fork);
pid_t
fork(void)
{
return (((pid_t (*)(void))*(__libc_interposing_slot(INTERPOS_fork)))());
return (INTERPOS_SYS(fork));
}
+1 -1
View File
@@ -39,5 +39,5 @@ __weak_reference(__sys_fsync, __fsync);
int
fsync(int fd)
{
return (((int (*)(int))*(__libc_interposing_slot(INTERPOS_fsync)))(fd));
return (INTERPOS_SYS(fsync, fd));
}
+2 -4
View File
@@ -41,8 +41,6 @@ int
kevent(int kq, const struct kevent *changelist, int nchanges,
struct kevent *eventlist, int nevents, const struct timespec *timeout)
{
return (((int (*)(int, const struct kevent *, int,
struct kevent *, int, const struct timespec *))
*(__libc_interposing_slot(INTERPOS_kevent)))
(kq, changelist, nchanges, eventlist, nevents, timeout));
return (INTERPOS_SYS(kevent, kq, changelist, nchanges, eventlist,
nevents, timeout));
}
+1 -2
View File
@@ -78,6 +78,5 @@ lockf(int filedes, int function, off_t size)
/* NOTREACHED */
}
return (((int (*)(int, int, ...))
*(__libc_interposing_slot(INTERPOS_fcntl)))(filedes, cmd, &fl));
return (INTERPOS_SYS(fcntl, filedes, cmd, (intptr_t)&fl));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_msync, __msync);
int
msync(void *addr, size_t len, int flags)
{
return (((int (*)(void *, size_t, int))
*(__libc_interposing_slot(INTERPOS_msync)))(addr, len, flags));
return (INTERPOS_SYS(msync, addr, len, flags));
}
+1 -2
View File
@@ -39,6 +39,5 @@ __weak_reference(__sys_nanosleep, __nanosleep);
int
nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
return (((int (*)(const struct timespec *, struct timespec *))
*(__libc_interposing_slot(INTERPOS_nanosleep)))(rqtp, rmtp));
return (INTERPOS_SYS(nanosleep, rqtp, rmtp));
}
+1 -3
View File
@@ -50,7 +50,5 @@ open(const char *path, int flags, ...)
} else {
mode = 0;
}
return (((int (*)(int, const char *, int, ...))
*(__libc_interposing_slot(INTERPOS_openat)))
(AT_FDCWD, path, flags, mode));
return (INTERPOS_SYS(openat, AT_FDCWD, path, flags, mode));
}
+1 -3
View File
@@ -53,7 +53,5 @@ openat(int fd, const char *path, int flags, ...)
} else {
mode = 0;
}
return (((int (*)(int, const char *, int, ...))
*(__libc_interposing_slot(INTERPOS_openat)))
(fd, path, flags, mode));
return (INTERPOS_SYS(openat, fd, path, flags, mode));
}
+1 -3
View File
@@ -37,7 +37,5 @@
pid_t
pdfork(int *fdp, int flags)
{
return (((pid_t (*)(int *, int))
*(__libc_interposing_slot(INTERPOS_pdfork)))
(fdp, flags));
return (INTERPOS_SYS(pdfork, fdp, flags));
}
+1 -2
View File
@@ -39,6 +39,5 @@ __weak_reference(__sys_poll, __poll);
int
poll(struct pollfd pfd[], nfds_t nfds, int timeout)
{
return (((int (*)(struct pollfd *, nfds_t, int))
*(__libc_interposing_slot(INTERPOS_poll)))(pfd, nfds, timeout));
return (INTERPOS_SYS(poll, pfd, nfds, timeout));
}
+1 -3
View File
@@ -40,7 +40,5 @@ int
ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *__restrict
timeout, const sigset_t *__restrict newsigmask)
{
return (((int (*)(struct pollfd *, nfds_t, const struct timespec *,
const sigset_t *))*(__libc_interposing_slot(INTERPOS_ppoll)))
(pfd, nfds, timeout, newsigmask));
return (INTERPOS_SYS(ppoll, pfd, nfds, timeout, newsigmask));
}
+1 -3
View File
@@ -40,7 +40,5 @@ int
pselect(int n, fd_set *rs, fd_set *ws, fd_set *es, const struct timespec *t,
const sigset_t *s)
{
return (((int (*)(int, fd_set *, fd_set *, fd_set *,
const struct timespec *, const sigset_t *))
*(__libc_interposing_slot(INTERPOS_pselect)))(n, rs, ws, es, t, s));
return (INTERPOS_SYS(pselect, n, rs, ws, es, t, s));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_read, __read);
ssize_t
read(int fd, void *buf, size_t nbytes)
{
return (((ssize_t (*)(int, void *, size_t))
*(__libc_interposing_slot(INTERPOS_read)))(fd, buf, nbytes));
return (INTERPOS_SYS(read, fd, buf, nbytes));
}
+1 -2
View File
@@ -41,6 +41,5 @@ __weak_reference(__sys_readv, __readv);
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{
return (((ssize_t (*)(int, const struct iovec *, int))
*(__libc_interposing_slot(INTERPOS_readv)))(fd, iov, iovcnt));
return (INTERPOS_SYS(readv, fd, iov, iovcnt));
}
+1 -4
View File
@@ -42,8 +42,5 @@ recv(int s, void *buf, size_t len, int flags)
* POSIX says recv() shall be a cancellation point, so call the
* cancellation-enabled recvfrom() and not _recvfrom().
*/
return (((ssize_t (*)(int, void *, size_t, int,
struct sockaddr *, socklen_t *))
*(__libc_interposing_slot(INTERPOS_recvfrom)))
(s, buf, len, flags, NULL, NULL));
return (INTERPOS_SYS(recvfrom, s, buf, len, flags, NULL, NULL));
}
+1 -4
View File
@@ -41,8 +41,5 @@ ssize_t
recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr * __restrict from, socklen_t * __restrict fromlen)
{
return (((ssize_t (*)(int, void *, size_t, int,
struct sockaddr *, socklen_t *))
*(__libc_interposing_slot(INTERPOS_recvfrom)))
(s, buf, len, flags, from, fromlen));
return (INTERPOS_SYS(recvfrom, s, buf, len, flags, from, fromlen));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_recvmsg, __recvmsg);
ssize_t
recvmsg(int s, struct msghdr *msg, int flags)
{
return (((int (*)(int, struct msghdr *, int))
*(__libc_interposing_slot(INTERPOS_recvmsg)))(s, msg, flags));
return (INTERPOS_SYS(recvmsg, s, msg, flags));
}
+1 -2
View File
@@ -39,6 +39,5 @@ __weak_reference(__sys_select, __select);
int
select(int n, fd_set *rs, fd_set *ws, fd_set *es, struct timeval *t)
{
return (((int (*)(int, fd_set *, fd_set *, fd_set *, struct timeval *))
*(__libc_interposing_slot(INTERPOS_select)))(n, rs, ws, es, t));
return (INTERPOS_SYS(select, n, rs, ws, es, t));
}
+1 -4
View File
@@ -42,8 +42,5 @@ send(int s, const void *msg, size_t len, int flags)
* POSIX says send() shall be a cancellation point, so call the
* cancellation-enabled sendto() and not _sendto().
*/
return (((ssize_t (*)(int, const void *, size_t, int,
const struct sockaddr *, socklen_t))
*__libc_interposing_slot(INTERPOS_sendto))(s, msg, len, flags,
NULL, 0));
return (INTERPOS_SYS(sendto, s, msg, len, flags, NULL, 0));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_sendmsg, __sendmsg);
ssize_t
sendmsg(int s, const struct msghdr *msg, int flags)
{
return (((int (*)(int, const struct msghdr *, int))
*(__libc_interposing_slot(INTERPOS_sendmsg)))(s, msg, flags));
return (INTERPOS_SYS(sendmsg, s, msg, flags));
}
+1 -4
View File
@@ -41,8 +41,5 @@ ssize_t
sendto(int s, const void *msg, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
return (((ssize_t (*)(int, const void *, size_t, int,
const struct sockaddr *, socklen_t))
*(__libc_interposing_slot(INTERPOS_sendto)))
(s, msg, len, flags, to, tolen));
return (INTERPOS_SYS(sendto, s, msg, len, flags, to, tolen));
}
+1 -2
View File
@@ -42,6 +42,5 @@ __sym_default(setcontext, setcontext, FBSD_1.2);
int
setcontext(const ucontext_t *uc)
{
return (((int (*)(const ucontext_t *))
*(__libc_interposing_slot(INTERPOS_setcontext)))(uc));
return (INTERPOS_SYS(setcontext, uc));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(sigaction, __libc_sigaction);
int
sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
{
return (((int (*)(int, const struct sigaction *, struct sigaction *))
*(__libc_interposing_slot(INTERPOS_sigaction)))(sig, act, oact));
return (INTERPOS_SYS(sigaction, sig, act, oact));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(sigprocmask, __libc_sigprocmask);
int
sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
return (((int (*)(int, const sigset_t *, sigset_t *))
*(__libc_interposing_slot(INTERPOS_sigprocmask)))(how, set, oset));
return (INTERPOS_SYS(sigprocmask, how, set, oset));
}
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(sigsuspend, __libc_sigsuspend);
int
sigsuspend(const sigset_t *set)
{
return (((int (*)(const sigset_t *))
*(__libc_interposing_slot(INTERPOS_sigsuspend)))(set));
return (INTERPOS_SYS(sigsuspend, set));
}
+1 -3
View File
@@ -40,7 +40,5 @@ int
sigtimedwait(const sigset_t * __restrict set, siginfo_t * __restrict info,
const struct timespec * __restrict t)
{
return (((int (*)(const sigset_t *, siginfo_t *,
const struct timespec *))
*(__libc_interposing_slot(INTERPOS_sigtimedwait)))(set, info, t));
return (INTERPOS_SYS(sigtimedwait, set, info, t));
}
+1 -2
View File
@@ -32,6 +32,5 @@
int
sigwait(const sigset_t *set, int *sig)
{
return (((int (*)(const sigset_t *, int *))
*(__libc_interposing_slot(INTERPOS_sigwait)))(set, sig));
return (INTERPOS_SYS(sigwait, set, sig));
}
+1 -2
View File
@@ -39,6 +39,5 @@ __weak_reference(__sys_sigwaitinfo, __sigwaitinfo);
int
sigwaitinfo(const sigset_t * __restrict set, siginfo_t * __restrict info)
{
return (((int (*)(const sigset_t *, siginfo_t *))
*(__libc_interposing_slot(INTERPOS_sigwaitinfo)))(set, info));
return (INTERPOS_SYS(sigwaitinfo, set, info));
}
+1 -2
View File
@@ -44,6 +44,5 @@ __sym_default(swapcontext, swapcontext, FBSD_1.2);
int
swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
{
return (((int (*)(ucontext_t *, const ucontext_t *))
*(__libc_interposing_slot(INTERPOS_swapcontext)))(oucp, ucp));
return (INTERPOS_SYS(swapcontext, oucp, ucp));
}
+1 -3
View File
@@ -43,9 +43,7 @@ pid_t __wait(int *);
pid_t
__wait(int *istat)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
*(__libc_interposing_slot(INTERPOS_wait4)))
(WAIT_ANY, istat, 0, NULL));
return (INTERPOS_SYS(wait4, WAIT_ANY, istat, 0, NULL));
}
__weak_reference(__wait, wait);
+1 -3
View File
@@ -43,9 +43,7 @@ pid_t __wait3(int *, int, struct rusage *);
pid_t
__wait3(int *istat, int options, struct rusage *rup)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
*(__libc_interposing_slot(INTERPOS_wait4)))
(WAIT_ANY, istat, options, rup));
return (INTERPOS_SYS(wait4, WAIT_ANY, istat, options, rup));
}
__weak_reference(__wait3, wait3);
+1 -3
View File
@@ -39,7 +39,5 @@ __weak_reference(__sys_wait4, __wait4);
pid_t
wait4(pid_t pid, int *status, int options, struct rusage *ru)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
*(__libc_interposing_slot(INTERPOS_wait4)))
(pid, status, options, ru));
return (INTERPOS_SYS(wait4, pid, status, options, ru));
}
+1 -3
View File
@@ -41,7 +41,5 @@ pid_t
wait6(idtype_t idtype, id_t id, int *status, int options, struct __wrusage *ru,
siginfo_t *infop)
{
return (((pid_t (*)(idtype_t, id_t, int *, int, struct __wrusage *,
siginfo_t *))*(__libc_interposing_slot(INTERPOS_wait6)))
(idtype, id, status, options, ru, infop));
return (INTERPOS_SYS(wait6, idtype, id, status, options, ru, infop));
}
+1 -3
View File
@@ -46,9 +46,7 @@ __waitid(idtype_t idtype, id_t id, siginfo_t *info, int flags)
int status;
pid_t ret;
ret = ((pid_t (*)(idtype_t, id_t, int *, int, struct __wrusage *,
siginfo_t *))*(__libc_interposing_slot(INTERPOS_wait6)))
(idtype, id, &status, flags, NULL, info);
ret = INTERPOS_SYS(wait6, idtype, id, &status, flags, NULL, info);
/*
* According to SUSv4, waitid() shall not return a PID when a
+1 -3
View File
@@ -43,9 +43,7 @@ pid_t __waitpid(pid_t, int *, int);
pid_t
__waitpid(pid_t pid, int *istat, int options)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
*(__libc_interposing_slot(INTERPOS_wait4)))
(pid, istat, options, NULL));
return (INTERPOS_SYS(wait4, pid, istat, options, NULL));
}
__weak_reference(__waitpid, waitpid);
+1 -2
View File
@@ -40,6 +40,5 @@ __weak_reference(__sys_write, __write);
ssize_t
write(int fd, const void *buf, size_t nbytes)
{
return (((ssize_t (*)(int, const void *, size_t))
*(__libc_interposing_slot(INTERPOS_write)))(fd, buf, nbytes));
return (INTERPOS_SYS(write, fd, buf, nbytes));
}
+1 -2
View File
@@ -41,6 +41,5 @@ __weak_reference(__sys_writev, __writev);
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{
return (((ssize_t (*)(int, const struct iovec *, int))
*(__libc_interposing_slot(INTERPOS_writev)))(fd, iov, iovcnt));
return (INTERPOS_SYS(writev, fd, iov, iovcnt));
}