Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_mqueue.c revision 1.1
      1  1.1  martin /*	$NetBSD: netbsd32_mqueue.c,v 1.1 2015/06/20 19:58:40 martin Exp $	*/
      2  1.1  martin 
      3  1.1  martin /*-
      4  1.1  martin  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1  martin  * All rights reserved.
      6  1.1  martin  *
      7  1.1  martin  * This code is derived from software developed for The NetBSD Foundation.
      8  1.1  martin  *
      9  1.1  martin  * Redistribution and use in source and binary forms, with or without
     10  1.1  martin  * modification, are permitted provided that the following conditions
     11  1.1  martin  * are met:
     12  1.1  martin  * 1. Redistributions of source code must retain the above copyright
     13  1.1  martin  *    notice, this list of conditions and the following disclaimer.
     14  1.1  martin  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  martin  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  martin  *    documentation and/or other materials provided with the distribution.
     17  1.1  martin  *
     18  1.1  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.1  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.1  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1  martin  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1  martin  */
     30  1.1  martin 
     31  1.1  martin #include <sys/cdefs.h>
     32  1.1  martin __KERNEL_RCSID(0, "$NetBSD: netbsd32_mqueue.c,v 1.1 2015/06/20 19:58:40 martin Exp $");
     33  1.1  martin 
     34  1.1  martin #if defined(_KERNEL_OPT)
     35  1.1  martin #include "opt_compat_netbsd.h"
     36  1.1  martin #endif
     37  1.1  martin 
     38  1.1  martin #include <sys/param.h>
     39  1.1  martin #include <sys/dirent.h>
     40  1.1  martin #include <sys/filedesc.h>
     41  1.1  martin #include <sys/fcntl.h>
     42  1.1  martin #include <sys/module.h>
     43  1.1  martin 
     44  1.1  martin #include <compat/netbsd32/netbsd32.h>
     45  1.1  martin #include <compat/netbsd32/netbsd32_syscall.h>
     46  1.1  martin #include <compat/netbsd32/netbsd32_syscallargs.h>
     47  1.1  martin #include <compat/netbsd32/netbsd32_conv.h>
     48  1.1  martin 
     49  1.1  martin 
     50  1.1  martin int
     51  1.1  martin netbsd32_mq_open(struct lwp *l, const struct netbsd32_mq_open_args *uap,
     52  1.1  martin     register_t *retval)
     53  1.1  martin {
     54  1.1  martin 	/* {
     55  1.1  martin 		syscallarg(const netbsd32_charp) name;
     56  1.1  martin 		syscallarg(int) oflag;
     57  1.1  martin 		syscallarg(mode_t) mode;
     58  1.1  martin 		syscallarg(struct netbsd32_mq_attrp_t) attr;
     59  1.1  martin 	} */
     60  1.1  martin 	struct netbsd32_mq_attr attr32;
     61  1.1  martin 	struct mq_attr *attr = NULL, a;
     62  1.1  martin 	int error;
     63  1.1  martin 
     64  1.1  martin 	if ((SCARG(uap, oflag) & O_CREAT) && (SCARG_P32(uap,attr) != NULL)) {
     65  1.1  martin 		error = copyin(&attr32, SCARG_P32(uap,attr), sizeof(attr32));
     66  1.1  martin 		if (error)
     67  1.1  martin 			return error;
     68  1.1  martin 		netbsd32_to_mq_attr(&attr32, &a);
     69  1.1  martin 		attr = &a;
     70  1.1  martin 	}
     71  1.1  martin 
     72  1.1  martin 	return mq_handle_open(l, SCARG_P32(uap, name), SCARG(uap, oflag),
     73  1.1  martin 	    SCARG(uap, mode), attr, retval);
     74  1.1  martin }
     75  1.1  martin 
     76  1.1  martin int
     77  1.1  martin netbsd32_mq_close(struct lwp *l, const struct netbsd32_mq_close_args *uap,
     78  1.1  martin     register_t *retval)
     79  1.1  martin {
     80  1.1  martin 	/* {
     81  1.1  martin 		syscallarg(mqd_t) mqdes;
     82  1.1  martin 	} */
     83  1.1  martin 
     84  1.1  martin 	return netbsd32_close(l, (const void*)uap, retval);
     85  1.1  martin }
     86  1.1  martin 
     87  1.1  martin int
     88  1.1  martin netbsd32_mq_unlink(struct lwp *l, const struct netbsd32_mq_unlink_args *uap,
     89  1.1  martin     register_t *retval)
     90  1.1  martin {
     91  1.1  martin 	/* {
     92  1.1  martin 		syscallarg(const netbsd32_charp) name;
     93  1.1  martin 	} */
     94  1.1  martin 	struct sys_mq_unlink_args ua;
     95  1.1  martin 
     96  1.1  martin 	NETBSD32TOP_UAP(name, const char);
     97  1.1  martin 	return sys_mq_unlink(l, &ua, retval);
     98  1.1  martin }
     99  1.1  martin 
    100  1.1  martin int
    101  1.1  martin netbsd32_mq_getattr(struct lwp *l, const struct netbsd32_mq_getattr_args *uap,
    102  1.1  martin     register_t *retval)
    103  1.1  martin {
    104  1.1  martin 	/* {
    105  1.1  martin 		syscallarg(mqd_t) mqdes;
    106  1.1  martin 		syscallarg(netbsd32_mq_attrp_t) mqstat;
    107  1.1  martin 	} */
    108  1.1  martin 	struct mqueue *mq;
    109  1.1  martin 	struct mq_attr attr;
    110  1.1  martin 	struct netbsd32_mq_attr a32;
    111  1.1  martin 	int error;
    112  1.1  martin 
    113  1.1  martin 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
    114  1.1  martin 	if (error)
    115  1.1  martin 		return error;
    116  1.1  martin 
    117  1.1  martin 	memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
    118  1.1  martin 	mutex_exit(&mq->mq_mtx);
    119  1.1  martin 	fd_putfile((int)SCARG(uap, mqdes));
    120  1.1  martin 	netbsd32_from_mq_attr(&attr, &a32);
    121  1.1  martin 	return copyout(&a32, SCARG_P32(uap,mqstat), sizeof(a32));
    122  1.1  martin }
    123  1.1  martin 
    124  1.1  martin int
    125  1.1  martin netbsd32_mq_setattr(struct lwp *l, const struct netbsd32_mq_setattr_args *uap,
    126  1.1  martin     register_t *retval)
    127  1.1  martin {
    128  1.1  martin 	/* {
    129  1.1  martin 		syscallarg(mqd_t) mqdes;
    130  1.1  martin 		syscallarg(const netbsd32_mq_attrp_t) mqstat;
    131  1.1  martin 		syscallarg(netbsd32_mq_attrp_t) omqstat;
    132  1.1  martin 	} */
    133  1.1  martin 	struct mqueue *mq;
    134  1.1  martin 	struct netbsd32_mq_attr attr32;
    135  1.1  martin 	struct mq_attr attr;
    136  1.1  martin 	int error, nonblock;
    137  1.1  martin 
    138  1.1  martin 	error = copyin(SCARG_P32(uap, mqstat), &attr32, sizeof(attr32));
    139  1.1  martin 	if (error)
    140  1.1  martin 		return error;
    141  1.1  martin 	netbsd32_to_mq_attr(&attr32, &attr);
    142  1.1  martin 	nonblock = (attr.mq_flags & O_NONBLOCK);
    143  1.1  martin 
    144  1.1  martin 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
    145  1.1  martin 	if (error)
    146  1.1  martin 		return error;
    147  1.1  martin 
    148  1.1  martin 	/* Copy the old attributes, if needed */
    149  1.1  martin 	if (SCARG_P32(uap, omqstat))
    150  1.1  martin 		memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
    151  1.1  martin 
    152  1.1  martin 	/* Ignore everything, except O_NONBLOCK */
    153  1.1  martin 	if (nonblock)
    154  1.1  martin 		mq->mq_attrib.mq_flags |= O_NONBLOCK;
    155  1.1  martin 	else
    156  1.1  martin 		mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
    157  1.1  martin 
    158  1.1  martin 	mutex_exit(&mq->mq_mtx);
    159  1.1  martin 	fd_putfile((int)SCARG(uap, mqdes));
    160  1.1  martin 
    161  1.1  martin 	/*
    162  1.1  martin 	 * Copy the data to the user-space.
    163  1.1  martin 	 * Note: According to POSIX, the new attributes should not be set in
    164  1.1  martin 	 * case of fail - this would be violated.
    165  1.1  martin 	 */
    166  1.1  martin 	if (SCARG_P32(uap, omqstat)) {
    167  1.1  martin 		netbsd32_from_mq_attr(&attr, &attr32);
    168  1.1  martin 		error = copyout(&attr32, SCARG_P32(uap, omqstat),
    169  1.1  martin 		    sizeof(attr32));
    170  1.1  martin 	}
    171  1.1  martin 
    172  1.1  martin 	return error;
    173  1.1  martin }
    174  1.1  martin 
    175  1.1  martin int
    176  1.1  martin netbsd32_mq_notify(struct lwp *l, const struct netbsd32_mq_notify_args *uap,
    177  1.1  martin     register_t *result)
    178  1.1  martin {
    179  1.1  martin 	/* {
    180  1.1  martin 		syscallarg(mqd_t) mqdes;
    181  1.1  martin 		syscallarg(const netbsd32_sigeventp_t) notification;
    182  1.1  martin 	} */
    183  1.1  martin 	struct mqueue *mq;
    184  1.1  martin 	struct netbsd32_sigevent sig32;
    185  1.1  martin 	int error;
    186  1.1  martin 
    187  1.1  martin 	if (SCARG_P32(uap, notification)) {
    188  1.1  martin 		/* Get the signal from user-space */
    189  1.1  martin 		error = copyin(SCARG_P32(uap, notification), &sig32,
    190  1.1  martin 		    sizeof(sig32));
    191  1.1  martin 		if (error)
    192  1.1  martin 			return error;
    193  1.1  martin 		if (sig32.sigev_notify == SIGEV_SIGNAL &&
    194  1.1  martin 		    (sig32.sigev_signo <=0 || sig32.sigev_signo >= NSIG))
    195  1.1  martin 			return EINVAL;
    196  1.1  martin 	}
    197  1.1  martin 
    198  1.1  martin 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
    199  1.1  martin 	if (error) {
    200  1.1  martin 		return error;
    201  1.1  martin 	}
    202  1.1  martin 	if (SCARG_P32(uap, notification)) {
    203  1.1  martin 		/* Register notification: set the signal and target process */
    204  1.1  martin 		if (mq->mq_notify_proc == NULL) {
    205  1.1  martin 			netbsd32_to_sigevent(&sig32, &mq->mq_sig_notify);
    206  1.1  martin 			mq->mq_notify_proc = l->l_proc;
    207  1.1  martin 		} else {
    208  1.1  martin 			/* Fail if someone else already registered */
    209  1.1  martin 			error = EBUSY;
    210  1.1  martin 		}
    211  1.1  martin 	} else {
    212  1.1  martin 		/* Unregister the notification */
    213  1.1  martin 		mq->mq_notify_proc = NULL;
    214  1.1  martin 	}
    215  1.1  martin 	mutex_exit(&mq->mq_mtx);
    216  1.1  martin 	fd_putfile((int)SCARG(uap, mqdes));
    217  1.1  martin 
    218  1.1  martin 	return error;
    219  1.1  martin }
    220  1.1  martin 
    221  1.1  martin int
    222  1.1  martin netbsd32_mq_send(struct lwp *l, const struct netbsd32_mq_send_args *uap,
    223  1.1  martin     register_t *result)
    224  1.1  martin {
    225  1.1  martin 	/* {
    226  1.1  martin 		syscallarg(mqd_t) mqdes;
    227  1.1  martin 		syscallarg(const netbsd32_charp) msg_ptr;
    228  1.1  martin 		syscallarg(netbsd32_size_t) msg_len;
    229  1.1  martin 		syscallarg(unsigned) msg_prio;
    230  1.1  martin 	} */
    231  1.1  martin 
    232  1.1  martin 
    233  1.1  martin 	return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
    234  1.1  martin 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL);
    235  1.1  martin }
    236  1.1  martin 
    237  1.1  martin int
    238  1.1  martin netbsd32_mq_receive(struct lwp *l, const struct netbsd32_mq_receive_args *uap,
    239  1.1  martin     register_t *retval)
    240  1.1  martin {
    241  1.1  martin 	/* {
    242  1.1  martin 		syscallarg(mqd_t) mqdes;
    243  1.1  martin 		syscallarg(netbsd32_charp) msg_ptr;
    244  1.1  martin 		syscallarg(netbsd32_size_t) msg_len;
    245  1.1  martin 		syscallarg(netbsd32_uintp) msg_prio;
    246  1.1  martin 	} */
    247  1.1  martin 	ssize_t mlen;
    248  1.1  martin 	int error;
    249  1.1  martin 
    250  1.1  martin 	error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
    251  1.1  martin 	    SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), NULL, &mlen);
    252  1.1  martin 	if (error == 0)
    253  1.1  martin 		*retval = mlen;
    254  1.1  martin 
    255  1.1  martin 	return error;
    256  1.1  martin }
    257  1.1  martin 
    258  1.1  martin #ifdef COMPAT_50
    259  1.1  martin 
    260  1.1  martin int
    261  1.1  martin compat_50_netbsd32_mq_timedsend(struct lwp *l,
    262  1.1  martin     const struct compat_50_netbsd32_mq_timedsend_args *uap,
    263  1.1  martin     register_t *retval)
    264  1.1  martin {
    265  1.1  martin 	/* {
    266  1.1  martin 		syscallarg(mqd_t) mqdes;
    267  1.1  martin 		syscallarg(const netbsd32_charp) msg_ptr;
    268  1.1  martin 		syscallarg(netbsd32_size_t) msg_len;
    269  1.1  martin 		syscallarg(unsigned) msg_prio;
    270  1.1  martin 		syscallarg(const netbsd32_timespec50p_t) abs_timeout;
    271  1.1  martin 	} */
    272  1.1  martin 	struct timespec ts, *tsp;
    273  1.1  martin 	struct netbsd32_timespec ts32;
    274  1.1  martin 	int error;
    275  1.1  martin 
    276  1.1  martin 	/* Get and convert time value */
    277  1.1  martin 	if (SCARG_P32(uap, abs_timeout)) {
    278  1.1  martin 		error = copyin(SCARG_P32(uap, abs_timeout), &ts32,
    279  1.1  martin 		     sizeof(ts32));
    280  1.1  martin 		if (error)
    281  1.1  martin 			return error;
    282  1.1  martin 		netbsd32_to_timespec(&ts32, &ts);
    283  1.1  martin 		tsp = &ts;
    284  1.1  martin 	} else {
    285  1.1  martin 		tsp = NULL;
    286  1.1  martin 	}
    287  1.1  martin 
    288  1.1  martin 	return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
    289  1.1  martin 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
    290  1.1  martin }
    291  1.1  martin 
    292  1.1  martin int
    293  1.1  martin compat_50_netbsd32_mq_timedreceive(struct lwp *l,
    294  1.1  martin     const struct compat_50_netbsd32_mq_timedreceive_args *uap,
    295  1.1  martin     register_t *retval)
    296  1.1  martin {
    297  1.1  martin 	/* {
    298  1.1  martin 		syscallarg(mqd_t) mqdes;
    299  1.1  martin 		syscallarg(netbsd32_charp) msg_ptr;
    300  1.1  martin 		syscallarg(netbsd32_size_t) msg_len;
    301  1.1  martin 		syscallarg(netbsd32_uintp) msg_prio;
    302  1.1  martin 		syscallarg(const netbsd32_timespec50p_t) abs_timeout;
    303  1.1  martin 	} */
    304  1.1  martin 	struct timespec ts, *tsp;
    305  1.1  martin 	struct netbsd32_timespec ts32;
    306  1.1  martin 	ssize_t mlen;
    307  1.1  martin 	int error;
    308  1.1  martin 
    309  1.1  martin 	/* Get and convert time value */
    310  1.1  martin 	if (SCARG_P32(uap, abs_timeout)) {
    311  1.1  martin 		error = copyin(SCARG_P32(uap, abs_timeout), &ts32,
    312  1.1  martin 		    sizeof(ts32));
    313  1.1  martin 		if (error)
    314  1.1  martin 			return error;
    315  1.1  martin 		netbsd32_to_timespec(&ts32, &ts);
    316  1.1  martin 		tsp = &ts;
    317  1.1  martin 	} else {
    318  1.1  martin 		tsp = NULL;
    319  1.1  martin 	}
    320  1.1  martin 
    321  1.1  martin 	error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
    322  1.1  martin 	    SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), tsp, &mlen);
    323  1.1  martin 	if (error == 0)
    324  1.1  martin 		*retval = mlen;
    325  1.1  martin 
    326  1.1  martin 	return error;
    327  1.1  martin }
    328  1.1  martin #endif
    329