#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_mqueue.c,v 1.2 2025/05/17 19:00:56 andvar Exp $");
#include <sys/param.h>
#include <sys/filedesc.h>
#include <sys/fcntl.h>
#include <sys/mqueue.h>
#include <sys/syscallargs.h>
#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_sched.h>
#include <compat/linux/common/linux_fcntl.h>
#include <compat/linux/common/linux_ipc.h>
#include <compat/linux/common/linux_sem.h>
#include <compat/linux/common/linux_signal.h>
#include <compat/linux/common/linux_sigevent.h>
#include <compat/linux/common/linux_util.h>
#include <compat/linux/common/linux_mqueue.h>
#include <compat/linux/linux_syscallargs.h>
#include <compat/linux/linux_syscall.h>
static void
linux_to_bsd_mq_attr(const struct linux_mq_attr *lmp, struct mq_attr *bmp)
{
memset(bmp, 0, sizeof(*bmp));
bmp->mq_flags = cvtto_bsd_mask(lmp->mq_flags, LINUX_O_NONBLOCK,
O_NONBLOCK);
bmp->mq_maxmsg = lmp->mq_maxmsg;
bmp->mq_msgsize = lmp->mq_msgsize;
bmp->mq_curmsgs = lmp->mq_curmsgs;
}
static void
bsd_to_linux_mq_attr(const struct mq_attr *bmp, struct linux_mq_attr *lmp)
{
memset(lmp, 0, sizeof(*lmp));
lmp->mq_flags = cvtto_linux_mask(bmp->mq_flags, O_NONBLOCK,
LINUX_O_NONBLOCK);
lmp->mq_maxmsg = bmp->mq_maxmsg;
lmp->mq_msgsize = bmp->mq_msgsize;
lmp->mq_curmsgs = bmp->mq_curmsgs;
}
int
linux_sys_mq_open(struct lwp *l, const struct linux_sys_mq_open_args *uap,
register_t *retval)
{
struct linux_mq_attr lattr;
struct mq_attr *attr = NULL, a;
int error, oflag;
oflag = linux_to_bsd_ioflags(SCARG(uap, oflag));
if ((oflag & O_CREAT) != 0 && SCARG(uap, attr) != NULL) {
error = copyin(SCARG(uap, attr), &lattr, sizeof(lattr));
if (error)
return error;
linux_to_bsd_mq_attr(&lattr, &a);
attr = &a;
}
return mq_handle_open(l, SCARG(uap, name), oflag,
(mode_t)SCARG(uap, mode), attr, retval);
}
int
linux_sys_mq_unlink(struct lwp *l, const struct linux_sys_mq_unlink_args *uap,
register_t *retval)
{
struct sys_mq_unlink_args args;
SCARG(&args, name) = SCARG(uap, name);
return sys_mq_unlink(l, &args, retval);
}
int
linux_sys_mq_timedsend(struct lwp *l, const struct linux_sys_mq_timedsend_args *uap,
register_t *retval)
{
struct linux_timespec lts;
struct timespec ts, *tsp;
int error;
if (SCARG(uap, abs_timeout)) {
error = copyin(SCARG(uap, abs_timeout), <s, sizeof(lts));
if (error)
return error;
linux_to_native_timespec(&ts, <s);
tsp = &ts;
} else {
tsp = NULL;
}
return mq_send1((mqd_t)SCARG(uap, mqdes), SCARG(uap, msg_ptr),
SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
}
int
linux_sys_mq_timedreceive(struct lwp *l,
const struct linux_sys_mq_timedreceive_args *uap, register_t *retval)
{
struct linux_timespec lts;
struct timespec ts, *tsp;
ssize_t mlen;
int error;
if (SCARG(uap, abs_timeout)) {
error = copyin(SCARG(uap, abs_timeout), <s, sizeof(lts));
if (error)
return error;
linux_to_native_timespec(&ts, <s);
tsp = &ts;
} else {
tsp = NULL;
}
error = mq_recv1((mqd_t)SCARG(uap, mqdes), SCARG(uap, msg_ptr),
SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
if (error == 0)
*retval = mlen;
return error;
}
int
linux_sys_mq_notify(struct lwp *l, const struct linux_sys_mq_notify_args *uap,
register_t *retval)
{
struct mqueue *mq;
struct sigevent sig;
int error;
if (SCARG(uap, sevp)) {
error = linux_sigevent_copyin(SCARG(uap, sevp), &sig,
sizeof(sig));
if (error)
return error;
if (sig.sigev_notify == SIGEV_SIGNAL &&
(sig.sigev_signo <= 0 || sig.sigev_signo >= NSIG))
return EINVAL;
}
error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
if (error)
return error;
if (SCARG(uap, sevp)) {
if (mq->mq_notify_proc == NULL) {
memcpy(&mq->mq_sig_notify, &sig,
sizeof(struct sigevent));
mq->mq_notify_proc = l->l_proc;
} else {
error = EBUSY;
}
} else {
mq->mq_notify_proc = NULL;
}
mutex_exit(&mq->mq_mtx);
fd_putfile((int)SCARG(uap, mqdes));
return error;
}
static int
linux_sys_mq_getattr(struct lwp *l,
const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
{
struct linux_mq_attr lattr;
struct mq_attr attr;
struct mqueue *mq;
int error;
error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
if (error)
return error;
memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
bsd_to_linux_mq_attr(&attr, &lattr);
mutex_exit(&mq->mq_mtx);
fd_putfile((int)SCARG(uap, mqdes));
return copyout(&lattr, SCARG(uap, oldattr), sizeof(lattr));
}
static int
linux_sys_mq_setattr(struct lwp *l,
const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
{
struct linux_mq_attr lattr;
struct mq_attr attr;
struct mqueue *mq;
int error, nonblock;
error = copyin(SCARG(uap, newattr), &lattr, sizeof(lattr));
if (error)
return error;
linux_to_bsd_mq_attr(&lattr, &attr);
nonblock = (attr.mq_flags & O_NONBLOCK);
error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
if (error)
return error;
if (SCARG(uap, oldattr)) {
memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
bsd_to_linux_mq_attr(&attr, &lattr);
}
if (nonblock)
mq->mq_attrib.mq_flags |= O_NONBLOCK;
else
mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
mutex_exit(&mq->mq_mtx);
fd_putfile((int)SCARG(uap, mqdes));
if (SCARG(uap, oldattr))
return copyout(&lattr, SCARG(uap, oldattr), sizeof(lattr));
return 0;
}
int
linux_sys_mq_getsetattr(struct lwp *l,
const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
{
if (SCARG(uap, newattr) == NULL)
return linux_sys_mq_getattr(l, uap, retval);
return linux_sys_mq_setattr(l, uap, retval);
}