xref: /dflybsd-src/sys/kern/sys_mqueue.c (revision 32af04f740541c3e943edbdb70f13dff978147be)
1 /*	$NetBSD: sys_mqueue.c,v 1.16 2009/04/11 23:05:26 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2007, 2008 Mindaugas Rasiukevicius <rmind at NetBSD org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Implementation of POSIX message queues.
31  * Defined in the Base Definitions volume of IEEE Std 1003.1-2001.
32  *
33  * Locking
34  *
35  * Global list of message queues (mqueue_head) and proc_t::p_mqueue_cnt
36  * counter are protected by mqlist_mtx lock.  The very message queue and
37  * its members are protected by mqueue::mq_mtx.
38  *
39  * Lock order:
40  *	mqlist_mtx
41  *	  -> mqueue::mq_mtx
42  */
43 
44 #include <stdbool.h>
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/errno.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/filedesc.h>
51 #include <sys/ucred.h>
52 #include <sys/priv.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mqueue.h>
56 #include <sys/objcache.h>
57 #include <sys/poll.h>
58 #include <sys/proc.h>
59 #include <sys/queue.h>
60 #include <sys/select.h>
61 #include <sys/serialize.h>
62 #include <sys/signal.h>
63 #include <sys/signalvar.h>
64 #include <sys/spinlock.h>
65 #include <sys/spinlock2.h>
66 #include <sys/stat.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysproto.h>
69 #include <sys/systm.h>
70 #include <sys/lock.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 
74 /* System-wide limits. */
75 static u_int			mq_open_max = MQ_OPEN_MAX;
76 static u_int			mq_prio_max = MQ_PRIO_MAX;
77 static u_int			mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
78 static u_int			mq_def_maxmsg = 32;
79 
80 struct lock			mqlist_mtx;
81 static struct objcache *	mqmsg_cache;
82 static LIST_HEAD(, mqueue)	mqueue_head =
83 	LIST_HEAD_INITIALIZER(mqueue_head);
84 
85 typedef struct	file file_t;	/* XXX: Should we put this in sys/types.h ? */
86 
87 /* Function prototypes */
88 static int	mq_poll_fop(file_t *, int, struct ucred *cred);
89 static int	mq_stat_fop(file_t *, struct stat *, struct ucred *cred);
90 static int	mq_close_fop(file_t *);
91 
92 /* Some time-related utility functions */
93 static int	itimespecfix(struct timespec *ts);
94 static int	tstohz(const struct timespec *ts);
95 
96 /* File operations vector */
97 static struct fileops mqops = {
98 	.fo_read = badfo_readwrite,
99 	.fo_write = badfo_readwrite,
100 	.fo_ioctl = badfo_ioctl,
101 	.fo_poll = mq_poll_fop,
102 	.fo_stat = mq_stat_fop,
103 	.fo_close = mq_close_fop,
104 	.fo_kqfilter = badfo_kqfilter,
105 	.fo_shutdown = badfo_shutdown
106 };
107 
108 /* Define a new malloc type for message queues */
109 MALLOC_DECLARE(M_MQBUF);
110 MALLOC_DEFINE(M_MQBUF, "mqueues", "Buffers to message queues");
111 
112 /* Malloc arguments for object cache */
113 struct objcache_malloc_args mqueue_malloc_args = {
114 	sizeof(struct mqueue), M_MQBUF };
115 
116 /* Spinlock around the process list */
117 extern struct spinlock allproc_spin;
118 
119 /*
120  * Initialize POSIX message queue subsystem.
121  */
122 void
123 mqueue_sysinit(void)
124 {
125 	mqmsg_cache = objcache_create("mqmsg_cache",
126 	    0,		/* infinite depot's capacity */
127 	    0,		/* default magazine's capacity */
128 	    NULL,	/* constructor */
129 	    NULL,	/* deconstructor */
130 	    NULL,
131 	    objcache_malloc_alloc,
132 	    objcache_malloc_free,
133 	    &mqueue_malloc_args);
134 
135 	lockinit(&mqlist_mtx, "mqlist_mtx", 0, LK_CANRECURSE);
136 }
137 
138 /*
139  * Free the message.
140  */
141 static void
142 mqueue_freemsg(struct mq_msg *msg, const size_t size)
143 {
144 
145 	if (size > MQ_DEF_MSGSIZE)
146 		kfree(msg, M_MQBUF);
147 	else
148 		objcache_put(mqmsg_cache, msg);
149 }
150 
151 /*
152  * Destroy the message queue.
153  */
154 static void
155 mqueue_destroy(struct mqueue *mq)
156 {
157 	struct mq_msg *msg;
158 	size_t msz;
159 	u_int i;
160 
161 	/* Note MQ_PQSIZE + 1. */
162 	for (i = 0; i < MQ_PQSIZE + 1; i++) {
163 		while ((msg = TAILQ_FIRST(&mq->mq_head[i])) != NULL) {
164 			TAILQ_REMOVE(&mq->mq_head[i], msg, msg_queue);
165 			msz = sizeof(struct mq_msg) + msg->msg_len;
166 			mqueue_freemsg(msg, msz);
167 		}
168 	}
169 	lockuninit(&mq->mq_mtx);
170 	kfree(mq, M_MQBUF);
171 }
172 
173 /*
174  * Lookup for file name in general list of message queues.
175  *  => locks the message queue
176  */
177 static void *
178 mqueue_lookup(char *name)
179 {
180 	struct mqueue *mq;
181 
182 	KKASSERT(lockstatus(&mqlist_mtx, curthread));
183 
184 	LIST_FOREACH(mq, &mqueue_head, mq_list) {
185 		if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
186 			lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
187 			return mq;
188 		}
189 	}
190 
191 	return NULL;
192 }
193 
194 /*
195  * mqueue_get: get the mqueue from the descriptor.
196  *  => locks the message queue, if found.
197  *  => hold a reference on the file descriptor.
198  */
199 static int
200 mqueue_get(struct lwp *l, mqd_t mqd, file_t **fpr)
201 {
202 	struct mqueue *mq;
203 	file_t *fp;
204 
205 	fp = holdfp(curproc->p_fd, (int)mqd, -1);	/* XXX: Why -1 ? */
206 	if (__predict_false(fp == NULL))
207 		return EBADF;
208 
209 	if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
210 		fdrop(fp);
211 		return EBADF;
212 	}
213 	mq = fp->f_data;
214 	lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
215 
216 	*fpr = fp;
217 	return 0;
218 }
219 
220 /*
221  * mqueue_linear_insert: perform linear insert according to the message
222  * priority into the reserved queue (MQ_PQRESQ).  Reserved queue is a
223  * sorted list used only when mq_prio_max is increased via sysctl.
224  */
225 static inline void
226 mqueue_linear_insert(struct mqueue *mq, struct mq_msg *msg)
227 {
228 	struct mq_msg *mit;
229 
230 	TAILQ_FOREACH(mit, &mq->mq_head[MQ_PQRESQ], msg_queue) {
231 		if (msg->msg_prio > mit->msg_prio)
232 			break;
233 	}
234 	if (mit == NULL) {
235 		TAILQ_INSERT_TAIL(&mq->mq_head[MQ_PQRESQ], msg, msg_queue);
236 	} else {
237 		TAILQ_INSERT_BEFORE(mit, msg, msg_queue);
238 	}
239 }
240 
241 /*
242  * Validate input.
243  */
244 int
245 itimespecfix(struct timespec *ts)
246 {
247 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
248 		return (EINVAL);
249 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
250 		ts->tv_nsec = tick * 1000;
251 	return (0);
252 }
253 
254 /*
255  * Compute number of ticks in the specified amount of time.
256  */
257 int
258 tstohz(const struct timespec *ts)
259 {
260 	struct timeval tv;
261 
262 	/*
263 	 * usec has great enough resolution for hz, so convert to a
264 	 * timeval and use tvtohz() above.
265 	 */
266 	TIMESPEC_TO_TIMEVAL(&tv, ts);
267 	return tvtohz_high(&tv);	/* XXX Why _high() and not _low() ? */
268 }
269 
270 /*
271  * Converter from struct timespec to the ticks.
272  * Used by mq_timedreceive(), mq_timedsend().
273  */
274 int
275 abstimeout2timo(struct timespec *ts, int *timo)
276 {
277 	struct timespec tsd;
278 	int error;
279 
280 	getnanotime(&tsd);
281 	timespecsub(ts, &tsd);
282 	if (ts->tv_sec < 0 || (ts->tv_sec == 0 && ts->tv_nsec <= 0)) {
283 		return ETIMEDOUT;
284 	}
285 	error = itimespecfix(ts);
286 	if (error) {
287 		return error;
288 	}
289 	*timo = tstohz(ts);
290 	KKASSERT(*timo != 0);
291 
292 	return 0;
293 }
294 
295 static int
296 mq_stat_fop(file_t *fp, struct stat *st, struct ucred *cred)
297 {
298 	struct mqueue *mq = fp->f_data;
299 
300 	(void)memset(st, 0, sizeof(*st));
301 
302 	lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
303 	st->st_mode = mq->mq_mode;
304 	st->st_uid = mq->mq_euid;
305 	st->st_gid = mq->mq_egid;
306 	st->st_atimespec = mq->mq_atime;
307 	st->st_mtimespec = mq->mq_mtime;
308 	/*st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;*/
309 	st->st_uid = fp->f_cred->cr_uid;
310 	st->st_gid = fp->f_cred->cr_svgid;
311 	lockmgr(&mq->mq_mtx, LK_RELEASE);
312 
313 	return 0;
314 }
315 
316 static int
317 mq_poll_fop(file_t *fp, int events, struct ucred *cred)
318 {
319 	struct mqueue *mq = fp->f_data;
320 	int revents = 0;
321 
322 	lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
323 	if (events & (POLLIN | POLLRDNORM)) {
324 		/* Ready for receiving, if there are messages in the queue */
325 		if (mq->mq_attrib.mq_curmsgs)
326 			revents |= (POLLIN | POLLRDNORM);
327 		else
328 			selrecord(curthread, &mq->mq_rsel);
329 	}
330 	if (events & (POLLOUT | POLLWRNORM)) {
331 		/* Ready for sending, if the message queue is not full */
332 		if (mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg)
333 			revents |= (POLLOUT | POLLWRNORM);
334 		else
335 			selrecord(curthread, &mq->mq_wsel);
336 	}
337 	lockmgr(&mq->mq_mtx, LK_RELEASE);
338 
339 	return revents;
340 }
341 
342 static int
343 mq_close_fop(file_t *fp)
344 {
345 	struct proc *p = curproc;
346 	struct mqueue *mq = fp->f_data;
347 	bool destroy;
348 
349 	lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
350 	lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
351 
352 	/* Decrease the counters */
353 	p->p_mqueue_cnt--;
354 	mq->mq_refcnt--;
355 
356 	/* Remove notification if registered for this process */
357 	if (mq->mq_notify_proc == p)
358 		mq->mq_notify_proc = NULL;
359 
360 	/*
361 	 * If this is the last reference and mqueue is marked for unlink,
362 	 * remove and later destroy the message queue.
363 	 */
364 	if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
365 		LIST_REMOVE(mq, mq_list);
366 		destroy = true;
367 	} else
368 		destroy = false;
369 
370 	lockmgr(&mq->mq_mtx, LK_RELEASE);
371 	lockmgr(&mqlist_mtx, LK_RELEASE);
372 
373 	if (destroy)
374 		mqueue_destroy(mq);
375 
376 	return 0;
377 }
378 
379 /*
380  * General mqueue system calls.
381  */
382 
383 int
384 sys_mq_open(struct mq_open_args *uap)
385 {
386 	/* {
387 		syscallarg(const char *) name;
388 		syscallarg(int) oflag;
389 		syscallarg(mode_t) mode;
390 		syscallarg(struct mq_attr) attr;
391 	} */
392 	struct proc *p = curproc;
393 	struct mqueue *mq, *mq_new = NULL;
394 	file_t *fp;
395 	char *name;
396 	int mqd, error, oflag;
397 
398 	/* Check access mode flags */
399 	oflag = SCARG(uap, oflag);
400 	if ((oflag & O_ACCMODE) == (O_WRONLY | O_RDWR)) {
401 		return EINVAL;
402 	}
403 
404 	/* Get the name from the user-space */
405 	name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
406 	error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
407 	if (error) {
408 		kfree(name, M_MQBUF);
409 		return error;
410 	}
411 
412 	if (oflag & O_CREAT) {
413 		struct mq_attr attr;
414 		u_int i;
415 
416 		/* Check the limit */
417 		if (p->p_mqueue_cnt == mq_open_max) {
418 			kfree(name, M_MQBUF);
419 			return EMFILE;
420 		}
421 
422 		/* Empty name is invalid */
423 		if (name[0] == '\0') {
424 			kfree(name, M_MQBUF);
425 			return EINVAL;
426 		}
427 
428 		/* Check for mqueue attributes */
429 		if (SCARG(uap, attr)) {
430 			error = copyin(SCARG(uap, attr), &attr,
431 				sizeof(struct mq_attr));
432 			if (error) {
433 				kfree(name, M_MQBUF);
434 				return error;
435 			}
436 			if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0 ||
437 			    attr.mq_msgsize > mq_max_msgsize) {
438 				kfree(name, M_MQBUF);
439 				return EINVAL;
440 			}
441 			attr.mq_curmsgs = 0;
442 		} else {
443 			memset(&attr, 0, sizeof(struct mq_attr));
444 			attr.mq_maxmsg = mq_def_maxmsg;
445 			attr.mq_msgsize =
446 			    MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
447 		}
448 
449 		/*
450 		 * Allocate new mqueue, initialize data structures,
451 		 * copy the name, attributes and set the flag.
452 		 */
453 		mq_new = kmalloc(sizeof(struct mqueue), M_MQBUF, M_WAITOK | M_ZERO);
454 
455 		lockinit(&mq_new->mq_mtx, "mq_new->mq_mtx", 0, LK_CANRECURSE);
456 		for (i = 0; i < (MQ_PQSIZE + 1); i++) {
457 			TAILQ_INIT(&mq_new->mq_head[i]);
458 		}
459 
460 		strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
461 		memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
462 
463 		/*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
464 		/* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
465 		mq_new->mq_attrib.mq_flags = oflag;
466 
467 		/* Store mode and effective UID with GID */
468 		mq_new->mq_mode = ((SCARG(uap, mode) &
469 		    ~p->p_fd->fd_cmask) & ALLPERMS) & ~S_ISTXT;
470 		mq_new->mq_euid = curproc->p_ucred->cr_uid;
471 		mq_new->mq_egid = curproc->p_ucred->cr_svgid;
472 	}
473 
474 	/* Allocate file structure and descriptor */
475 	error = falloc(curproc, &fp, &mqd);
476 	if (error) {
477 		if (mq_new)
478 			mqueue_destroy(mq_new);
479 		kfree(name, M_MQBUF);
480 		return error;
481 	}
482 	fp->f_type = DTYPE_MQUEUE;
483 	fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
484 	fp->f_ops = &mqops;
485 
486 	/* Look up for mqueue with such name */
487 	lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
488 	mq = mqueue_lookup(name);
489 	if (mq) {
490 		int acc_mode;
491 
492 		KKASSERT(lockstatus(&mq->mq_mtx, curthread));
493 
494 		/* Check if mqueue is not marked as unlinking */
495 		if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
496 			error = EACCES;
497 			goto exit;
498 		}
499 		/* Fail if O_EXCL is set, and mqueue already exists */
500 		if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
501 			error = EEXIST;
502 			goto exit;
503 		}
504 
505 		/*
506 		 * Check the permissions. Note the difference between
507 		 * VREAD/VWRITE and FREAD/FWRITE.
508 		 */
509 		acc_mode = 0;
510 		if (fp->f_flag & FREAD) {
511 			acc_mode |= VREAD;
512 		}
513 		if (fp->f_flag & FWRITE) {
514 			acc_mode |= VWRITE;
515 		}
516 		if (vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid,
517 			acc_mode, curproc->p_ucred)) {
518 
519 			error = EACCES;
520 			goto exit;
521 		}
522 	} else {
523 		/* Fail if mqueue neither exists, nor we create it */
524 		if ((oflag & O_CREAT) == 0) {
525 			lockmgr(&mqlist_mtx, LK_RELEASE);
526 			KKASSERT(mq_new == NULL);
527 			fsetfd(curproc, NULL, mqd);
528 			fp->f_ops = &badfileops;
529 			fdrop(fp);
530 			kfree(name, M_MQBUF);
531 			return ENOENT;
532 		}
533 
534 		/* Check the limit */
535 		if (p->p_mqueue_cnt == mq_open_max) {
536 			error = EMFILE;
537 			goto exit;
538 		}
539 
540 		/* Insert the queue to the list */
541 		mq = mq_new;
542 		lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
543 		LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
544 		mq_new = NULL;
545 		getnanotime(&mq->mq_btime);
546 		mq->mq_atime = mq->mq_mtime = mq->mq_btime;
547 	}
548 
549 	/* Increase the counters, and make descriptor ready */
550 	p->p_mqueue_cnt++;
551 	mq->mq_refcnt++;
552 	fp->f_data = mq;
553 exit:
554 	lockmgr(&mq->mq_mtx, LK_RELEASE);
555 	lockmgr(&mqlist_mtx, LK_RELEASE);
556 
557 	if (mq_new)
558 		mqueue_destroy(mq_new);
559 	if (error) {
560 		fsetfd(curproc, NULL, mqd);
561 		fp->f_ops = &badfileops;
562 	} else {
563 		fsetfd(p, fp, mqd);
564 		uap->sysmsg_result = mqd;
565 	}
566 	fdrop(fp);
567 	kfree(name, M_MQBUF);
568 
569 	return error;
570 }
571 
572 int
573 sys_mq_close(struct mq_close_args *uap)
574 {
575 	return sys_close((void *)uap);
576 }
577 
578 /*
579  * Primary mq_receive1() function.
580  */
581 int
582 mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
583     unsigned *msg_prio, struct timespec *ts, ssize_t *mlen)
584 {
585 	file_t *fp = NULL;
586 	struct mqueue *mq;
587 	struct mq_msg *msg = NULL;
588 	struct mq_attr *mqattr;
589 	u_int idx;
590 	int error;
591 
592 	/* Get the message queue */
593 	error = mqueue_get(l, mqdes, &fp);
594 	if (error) {
595 		return error;
596 	}
597 	mq = fp->f_data;
598 	if ((fp->f_flag & FREAD) == 0) {
599 		error = EBADF;
600 		goto error;
601 	}
602 	getnanotime(&mq->mq_atime);
603 	mqattr = &mq->mq_attrib;
604 
605 	/* Check the message size limits */
606 	if (msg_len < mqattr->mq_msgsize) {
607 		error = EMSGSIZE;
608 		goto error;
609 	}
610 
611 	/* Check if queue is empty */
612 	while (mqattr->mq_curmsgs == 0) {
613 		int t;
614 
615 		if (mqattr->mq_flags & O_NONBLOCK) {
616 			error = EAGAIN;
617 			goto error;
618 		}
619 		error = abstimeout2timo(ts, &t);
620 		if (error) {
621 			goto error;
622 		}
623 		/*
624 		 * Block until someone sends the message.
625 		 * While doing this, notification should not be sent.
626 		 */
627 		mqattr->mq_flags |= MQ_RECEIVE;
628 		error = tsleep(&mq->mq_send_cv, PCATCH, "mqsend", t);
629 		mqattr->mq_flags &= ~MQ_RECEIVE;
630 		if (error || (mqattr->mq_flags & MQ_UNLINK)) {
631 			error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
632 			goto error;
633 		}
634 	}
635 
636 
637 	/*
638 	 * Find the highest priority message, and remove it from the queue.
639 	 * At first, reserved queue is checked, bitmap is next.
640 	 */
641 	msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
642 	if (__predict_true(msg == NULL)) {
643 		idx = ffs(mq->mq_bitmap);
644 		msg = TAILQ_FIRST(&mq->mq_head[idx]);
645 		KKASSERT(msg != NULL);
646 	} else {
647 		idx = MQ_PQRESQ;
648 	}
649 	TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
650 
651 	/* Unmark the bit, if last message. */
652 	if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
653 		KKASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
654 		mq->mq_bitmap &= ~(1 << --idx);
655 	}
656 
657 	/* Decrement the counter and signal waiter, if any */
658 	mqattr->mq_curmsgs--;
659 	wakeup_one(&mq->mq_recv_cv);
660 
661 	/* Ready for sending now */
662 	selwakeup(&mq->mq_wsel);
663 error:
664 	lockmgr(&mq->mq_mtx, LK_RELEASE);
665 	fdrop(fp);
666 	if (error)
667 		return error;
668 
669 	/*
670 	 * Copy the data to the user-space.
671 	 * Note: According to POSIX, no message should be removed from the
672 	 * queue in case of fail - this would be violated.
673 	 */
674 	*mlen = msg->msg_len;
675 	error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
676 	if (error == 0 && msg_prio)
677 		error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
678 	mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
679 
680 	return error;
681 }
682 
683 int
684 sys_mq_receive(struct mq_receive_args *uap)
685 {
686 	/* {
687 		syscallarg(mqd_t) mqdes;
688 		syscallarg(char *) msg_ptr;
689 		syscallarg(size_t) msg_len;
690 		syscallarg(unsigned *) msg_prio;
691 	} */
692 	ssize_t mlen;
693 	int error;
694 
695 	error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
696 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
697 	if (error == 0)
698 		uap->sysmsg_result = mlen;
699 
700 	return error;
701 }
702 
703 int
704 sys_mq_timedreceive(struct mq_timedreceive_args *uap)
705 {
706 	/* {
707 		syscallarg(mqd_t) mqdes;
708 		syscallarg(char *) msg_ptr;
709 		syscallarg(size_t) msg_len;
710 		syscallarg(unsigned *) msg_prio;
711 		syscallarg(const struct timespec *) abs_timeout;
712 	} */
713 	int error;
714 	ssize_t mlen;
715 	struct timespec ts, *tsp;
716 
717 	/* Get and convert time value */
718 	if (SCARG(uap, abs_timeout)) {
719 		error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
720 		if (error)
721 			return error;
722 		tsp = &ts;
723 	} else {
724 		tsp = NULL;
725 	}
726 
727 	error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
728 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
729 	if (error == 0)
730 		uap->sysmsg_result = mlen;
731 
732 	return error;
733 }
734 
735 /*
736  * Primary mq_send1() function.
737  */
738 int
739 mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
740     unsigned msg_prio, struct timespec *ts)
741 {
742 	file_t *fp = NULL;
743 	struct mqueue *mq;
744 	struct mq_msg *msg;
745 	struct mq_attr *mqattr;
746 	struct proc *notify = NULL;
747 	/*ksiginfo_t ksi;*/
748 	size_t size;
749 	int error;
750 
751 	/* Check the priority range */
752 	if (msg_prio >= mq_prio_max)
753 		return EINVAL;
754 
755 	/* Allocate a new message */
756 	size = sizeof(struct mq_msg) + msg_len;
757 	if (size > mq_max_msgsize)
758 		return EMSGSIZE;
759 
760 	if (size > MQ_DEF_MSGSIZE) {
761 		msg = kmalloc(size, M_MQBUF, M_WAITOK);
762 	} else {
763 		msg = objcache_get(mqmsg_cache, M_WAITOK);
764 	}
765 
766 	/* Get the data from user-space */
767 	error = copyin(msg_ptr, msg->msg_ptr, msg_len);
768 	if (error) {
769 		mqueue_freemsg(msg, size);
770 		return error;
771 	}
772 	msg->msg_len = msg_len;
773 	msg->msg_prio = msg_prio;
774 
775 	/* Get the mqueue */
776 	error = mqueue_get(l, mqdes, &fp);
777 	if (error) {
778 		mqueue_freemsg(msg, size);
779 		return error;
780 	}
781 	mq = fp->f_data;
782 	if ((fp->f_flag & FWRITE) == 0) {
783 		error = EBADF;
784 		goto error;
785 	}
786 	getnanotime(&mq->mq_mtime);
787 	mqattr = &mq->mq_attrib;
788 
789 	/* Check the message size limit */
790 	if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
791 		error = EMSGSIZE;
792 		goto error;
793 	}
794 
795 	/* Check if queue is full */
796 	while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
797 		int t;
798 
799 		if (mqattr->mq_flags & O_NONBLOCK) {
800 			error = EAGAIN;
801 			goto error;
802 		}
803 		error = abstimeout2timo(ts, &t);
804 		if (error) {
805 			goto error;
806 		}
807 		/* Block until queue becomes available */
808 		error = tsleep(&mq->mq_recv_cv, PCATCH, "mqrecv", t);
809 		if (error || (mqattr->mq_flags & MQ_UNLINK)) {
810 			error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
811 			goto error;
812 		}
813 	}
814 	KKASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
815 
816 	/*
817 	 * Insert message into the queue, according to the priority.
818 	 * Note the difference between index and priority.
819 	 */
820 	if (__predict_true(msg_prio < MQ_PQSIZE)) {
821 		u_int idx = MQ_PQSIZE - msg_prio;
822 
823 		KKASSERT(idx != MQ_PQRESQ);
824 		TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
825 		mq->mq_bitmap |= (1 << --idx);
826 	} else {
827 		mqueue_linear_insert(mq, msg);
828 	}
829 
830 	/* Check for the notify */
831 	if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
832 	    (mqattr->mq_flags & MQ_RECEIVE) == 0) {
833 		/* Initialize the signal */
834 		/*KSI_INIT(&ksi);*/
835 		/*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
836 		/*ksi.ksi_code = SI_MESGQ;*/
837 		/*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
838 		/* Unregister the process */
839 		notify = mq->mq_notify_proc;
840 		mq->mq_notify_proc = NULL;
841 	}
842 
843 	/* Increment the counter and signal waiter, if any */
844 	mqattr->mq_curmsgs++;
845 	wakeup_one(&mq->mq_send_cv);
846 
847 	/* Ready for receiving now */
848 	selwakeup(&mq->mq_rsel);
849 error:
850 	lockmgr(&mq->mq_mtx, LK_RELEASE);
851 	fdrop(fp);
852 
853 	if (error) {
854 		mqueue_freemsg(msg, size);
855 	} else if (notify) {
856 		/* Send the notify, if needed */
857 		spin_lock_wr(&allproc_spin);
858 		/*kpsignal(notify, &ksi, NULL);*/
859 		ksignal(notify, mq->mq_sig_notify.sigev_signo);
860 		spin_unlock_wr(&allproc_spin);
861 	}
862 
863 	return error;
864 }
865 
866 int
867 sys_mq_send(struct mq_send_args *uap)
868 {
869 	/* {
870 		syscallarg(mqd_t) mqdes;
871 		syscallarg(const char *) msg_ptr;
872 		syscallarg(size_t) msg_len;
873 		syscallarg(unsigned) msg_prio;
874 	} */
875 
876 	return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
877 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
878 }
879 
880 int
881 sys_mq_timedsend(struct mq_timedsend_args *uap)
882 {
883 	/* {
884 		syscallarg(mqd_t) mqdes;
885 		syscallarg(const char *) msg_ptr;
886 		syscallarg(size_t) msg_len;
887 		syscallarg(unsigned) msg_prio;
888 		syscallarg(const struct timespec *) abs_timeout;
889 	} */
890 	struct timespec ts, *tsp;
891 	int error;
892 
893 	/* Get and convert time value */
894 	if (SCARG(uap, abs_timeout)) {
895 		error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
896 		if (error)
897 			return error;
898 		tsp = &ts;
899 	} else {
900 		tsp = NULL;
901 	}
902 
903 	return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
904 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
905 }
906 
907 int
908 sys_mq_notify(struct mq_notify_args *uap)
909 {
910 	/* {
911 		syscallarg(mqd_t) mqdes;
912 		syscallarg(const struct sigevent *) notification;
913 	} */
914 	file_t *fp = NULL;
915 	struct mqueue *mq;
916 	struct sigevent sig;
917 	int error;
918 
919 	if (SCARG(uap, notification)) {
920 		/* Get the signal from user-space */
921 		error = copyin(SCARG(uap, notification), &sig,
922 		    sizeof(struct sigevent));
923 		if (error)
924 			return error;
925 	}
926 
927 	error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
928 	if (error)
929 		return error;
930 	mq = fp->f_data;
931 
932 	if (SCARG(uap, notification)) {
933 		/* Register notification: set the signal and target process */
934 		if (mq->mq_notify_proc == NULL) {
935 			memcpy(&mq->mq_sig_notify, &sig,
936 			    sizeof(struct sigevent));
937 			mq->mq_notify_proc = curproc;
938 		} else {
939 			/* Fail if someone else already registered */
940 			error = EBUSY;
941 		}
942 	} else {
943 		/* Unregister the notification */
944 		mq->mq_notify_proc = NULL;
945 	}
946 	lockmgr(&mq->mq_mtx, LK_RELEASE);
947 	fdrop(fp);
948 
949 	return error;
950 }
951 
952 int
953 sys_mq_getattr(struct mq_getattr_args *uap)
954 {
955 	/* {
956 		syscallarg(mqd_t) mqdes;
957 		syscallarg(struct mq_attr *) mqstat;
958 	} */
959 	file_t *fp = NULL;
960 	struct mqueue *mq;
961 	struct mq_attr attr;
962 	int error;
963 
964 	/* Get the message queue */
965 	error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
966 	if (error)
967 		return error;
968 	mq = fp->f_data;
969 	memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
970 	lockmgr(&mq->mq_mtx, LK_RELEASE);
971 	fdrop(fp);
972 
973 	return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
974 }
975 
976 int
977 sys_mq_setattr(struct mq_setattr_args *uap)
978 {
979 	/* {
980 		syscallarg(mqd_t) mqdes;
981 		syscallarg(const struct mq_attr *) mqstat;
982 		syscallarg(struct mq_attr *) omqstat;
983 	} */
984 	file_t *fp = NULL;
985 	struct mqueue *mq;
986 	struct mq_attr attr;
987 	int error, nonblock;
988 
989 	error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
990 	if (error)
991 		return error;
992 	nonblock = (attr.mq_flags & O_NONBLOCK);
993 
994 	/* Get the message queue */
995 	error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
996 	if (error)
997 		return error;
998 	mq = fp->f_data;
999 
1000 	/* Copy the old attributes, if needed */
1001 	if (SCARG(uap, omqstat))
1002 		memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
1003 
1004 	/* Ignore everything, except O_NONBLOCK */
1005 	if (nonblock)
1006 		mq->mq_attrib.mq_flags |= O_NONBLOCK;
1007 	else
1008 		mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
1009 
1010 	lockmgr(&mq->mq_mtx, LK_RELEASE);
1011 	fdrop(fp);
1012 
1013 	/*
1014 	 * Copy the data to the user-space.
1015 	 * Note: According to POSIX, the new attributes should not be set in
1016 	 * case of fail - this would be violated.
1017 	 */
1018 	if (SCARG(uap, omqstat))
1019 		error = copyout(&attr, SCARG(uap, omqstat),
1020 		    sizeof(struct mq_attr));
1021 
1022 	return error;
1023 }
1024 
1025 int
1026 sys_mq_unlink(struct mq_unlink_args *uap)
1027 {
1028 	/* {
1029 		syscallarg(const char *) name;
1030 	} */
1031 	struct mqueue *mq;
1032 	char *name;
1033 	int error, refcnt = 0;
1034 
1035 	/* Get the name from the user-space */
1036 	name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
1037 	error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
1038 	if (error) {
1039 		kfree(name, M_MQBUF);
1040 		return error;
1041 	}
1042 
1043 	/* Lookup for this file */
1044 	lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
1045 	mq = mqueue_lookup(name);
1046 	if (mq == NULL) {
1047 		error = ENOENT;
1048 		goto error;
1049 	}
1050 
1051 	/* Check the permissions */
1052 	if (curproc->p_ucred->cr_uid != mq->mq_euid &&
1053 	    priv_check(curthread, PRIV_ROOT) != 0) {
1054 		lockmgr(&mq->mq_mtx, LK_RELEASE);
1055 		error = EACCES;
1056 		goto error;
1057 	}
1058 
1059 	/* Mark message queue as unlinking, before leaving the window */
1060 	mq->mq_attrib.mq_flags |= MQ_UNLINK;
1061 
1062 	/* Wake up all waiters, if there are such */
1063 	wakeup(&mq->mq_send_cv);
1064 	wakeup(&mq->mq_recv_cv);
1065 
1066 	selwakeup(&mq->mq_rsel);
1067 	selwakeup(&mq->mq_wsel);
1068 
1069 	refcnt = mq->mq_refcnt;
1070 	if (refcnt == 0)
1071 		LIST_REMOVE(mq, mq_list);
1072 
1073 	lockmgr(&mq->mq_mtx, LK_RELEASE);
1074 error:
1075 	lockmgr(&mqlist_mtx, LK_RELEASE);
1076 
1077 	/*
1078 	 * If there are no references - destroy the message
1079 	 * queue, otherwise, the last mq_close() will do that.
1080 	 */
1081 	if (error == 0 && refcnt == 0)
1082 		mqueue_destroy(mq);
1083 
1084 	kfree(name, M_MQBUF);
1085 	return error;
1086 }
1087 
1088 /*
1089  * SysCtl.
1090  */
1091 SYSCTL_NODE(_kern, OID_AUTO, mqueue,
1092     CTLFLAG_RW, 0, "Message queue options");
1093 
1094 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_open_max,
1095     CTLFLAG_RW, &mq_open_max, 0,
1096     "Maximal number of message queue descriptors per process");
1097 
1098 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_prio_max,
1099     CTLFLAG_RW, &mq_prio_max, 0,
1100     "Maximal priority of the message");
1101 
1102 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_max_msgsize,
1103     CTLFLAG_RW, &mq_max_msgsize, 0,
1104     "Maximal allowed size of the message");
1105 
1106 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_def_maxmsg,
1107     CTLFLAG_RW, &mq_def_maxmsg, 0,
1108     "Default maximal message count");
1109 
1110 SYSINIT(sys_mqueue_init, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, mqueue_sysinit, NULL);
1111