sys_mqueue.c revision 1.19 1 /* $NetBSD: sys_mqueue.c,v 1.19 2009/06/23 19:36:38 elad 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 <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: sys_mqueue.c,v 1.19 2009/06/23 19:36:38 elad Exp $");
46
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/condvar.h>
50 #include <sys/errno.h>
51 #include <sys/fcntl.h>
52 #include <sys/file.h>
53 #include <sys/filedesc.h>
54 #include <sys/kauth.h>
55 #include <sys/kernel.h>
56 #include <sys/kmem.h>
57 #include <sys/lwp.h>
58 #include <sys/mqueue.h>
59 #include <sys/mutex.h>
60 #include <sys/pool.h>
61 #include <sys/poll.h>
62 #include <sys/proc.h>
63 #include <sys/queue.h>
64 #include <sys/select.h>
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/stat.h>
68 #include <sys/sysctl.h>
69 #include <sys/syscallargs.h>
70 #include <sys/systm.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73
74 #include <miscfs/genfs/genfs.h>
75
76 /* System-wide limits. */
77 static u_int mq_open_max = MQ_OPEN_MAX;
78 static u_int mq_prio_max = MQ_PRIO_MAX;
79
80 static u_int mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
81 static u_int mq_def_maxmsg = 32;
82
83 static kmutex_t mqlist_mtx;
84 static pool_cache_t mqmsg_cache;
85 static LIST_HEAD(, mqueue) mqueue_head =
86 LIST_HEAD_INITIALIZER(mqueue_head);
87
88 static int mq_poll_fop(file_t *, int);
89 static int mq_stat_fop(file_t *, struct stat *);
90 static int mq_close_fop(file_t *);
91
92 static const struct fileops mqops = {
93 .fo_read = fbadop_read,
94 .fo_write = fbadop_write,
95 .fo_ioctl = fbadop_ioctl,
96 .fo_fcntl = fnullop_fcntl,
97 .fo_poll = mq_poll_fop,
98 .fo_stat = mq_stat_fop,
99 .fo_close = mq_close_fop,
100 .fo_kqfilter = fnullop_kqfilter,
101 .fo_drain = fnullop_drain,
102 };
103
104 /*
105 * Initialize POSIX message queue subsystem.
106 */
107 void
108 mqueue_sysinit(void)
109 {
110
111 mqmsg_cache = pool_cache_init(MQ_DEF_MSGSIZE, coherency_unit,
112 0, 0, "mqmsgpl", NULL, IPL_NONE, NULL, NULL, NULL);
113 mutex_init(&mqlist_mtx, MUTEX_DEFAULT, IPL_NONE);
114 }
115
116 /*
117 * Free the message.
118 */
119 static void
120 mqueue_freemsg(struct mq_msg *msg, const size_t size)
121 {
122
123 if (size > MQ_DEF_MSGSIZE)
124 kmem_free(msg, size);
125 else
126 pool_cache_put(mqmsg_cache, msg);
127 }
128
129 /*
130 * Destroy the message queue.
131 */
132 static void
133 mqueue_destroy(struct mqueue *mq)
134 {
135 struct mq_msg *msg;
136
137 while ((msg = TAILQ_FIRST(&mq->mq_head)) != NULL) {
138 TAILQ_REMOVE(&mq->mq_head, msg, msg_queue);
139 mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
140 }
141 seldestroy(&mq->mq_rsel);
142 seldestroy(&mq->mq_wsel);
143 cv_destroy(&mq->mq_send_cv);
144 cv_destroy(&mq->mq_recv_cv);
145 mutex_destroy(&mq->mq_mtx);
146 kmem_free(mq, sizeof(struct mqueue));
147 }
148
149 /*
150 * Lookup for file name in general list of message queues.
151 * => locks the message queue
152 */
153 static void *
154 mqueue_lookup(char *name)
155 {
156 struct mqueue *mq;
157 KASSERT(mutex_owned(&mqlist_mtx));
158
159 LIST_FOREACH(mq, &mqueue_head, mq_list) {
160 if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
161 mutex_enter(&mq->mq_mtx);
162 return mq;
163 }
164 }
165
166 return NULL;
167 }
168
169 /*
170 * mqueue_get: get the mqueue from the descriptor.
171 * => locks the message queue, if found.
172 * => holds a reference on the file descriptor.
173 */
174 static int
175 mqueue_get(mqd_t mqd, file_t **fpr)
176 {
177 struct mqueue *mq;
178 file_t *fp;
179
180 fp = fd_getfile((int)mqd);
181 if (__predict_false(fp == NULL)) {
182 return EBADF;
183 }
184 if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
185 fd_putfile((int)mqd);
186 return EBADF;
187 }
188 mq = fp->f_data;
189 mutex_enter(&mq->mq_mtx);
190
191 *fpr = fp;
192 return 0;
193 }
194
195 /*
196 * Converter from struct timespec to the ticks.
197 * Used by mq_timedreceive(), mq_timedsend().
198 */
199 int
200 abstimeout2timo(struct timespec *ts, int *timo)
201 {
202 int error;
203
204 /*
205 * According to POSIX, validation check is needed only in case of
206 * blocking. Thus, set the invalid value right now, and fail latter.
207 */
208 error = itimespecfix(ts);
209 *timo = (error == 0) ? tstohz(ts) : -1;
210
211 return 0;
212 }
213
214 static int
215 mq_stat_fop(file_t *fp, struct stat *st)
216 {
217 struct mqueue *mq = fp->f_data;
218
219 (void)memset(st, 0, sizeof(*st));
220
221 mutex_enter(&mq->mq_mtx);
222 st->st_mode = mq->mq_mode;
223 st->st_uid = mq->mq_euid;
224 st->st_gid = mq->mq_egid;
225 st->st_atimespec = mq->mq_atime;
226 st->st_mtimespec = mq->mq_mtime;
227 st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;
228 st->st_uid = kauth_cred_geteuid(fp->f_cred);
229 st->st_gid = kauth_cred_getegid(fp->f_cred);
230 mutex_exit(&mq->mq_mtx);
231
232 return 0;
233 }
234
235 static int
236 mq_poll_fop(file_t *fp, int events)
237 {
238 struct mqueue *mq = fp->f_data;
239 int revents = 0;
240
241 mutex_enter(&mq->mq_mtx);
242 if (events & (POLLIN | POLLRDNORM)) {
243 /* Ready for receiving, if there are messages in the queue */
244 if (mq->mq_attrib.mq_curmsgs)
245 revents |= (POLLIN | POLLRDNORM);
246 else
247 selrecord(curlwp, &mq->mq_rsel);
248 }
249 if (events & (POLLOUT | POLLWRNORM)) {
250 /* Ready for sending, if the message queue is not full */
251 if (mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg)
252 revents |= (POLLOUT | POLLWRNORM);
253 else
254 selrecord(curlwp, &mq->mq_wsel);
255 }
256 mutex_exit(&mq->mq_mtx);
257
258 return revents;
259 }
260
261 static int
262 mq_close_fop(file_t *fp)
263 {
264 struct proc *p = curproc;
265 struct mqueue *mq = fp->f_data;
266 bool destroy;
267
268 mutex_enter(&mqlist_mtx);
269 mutex_enter(&mq->mq_mtx);
270
271 /* Decrease the counters */
272 p->p_mqueue_cnt--;
273 mq->mq_refcnt--;
274
275 /* Remove notification if registered for this process */
276 if (mq->mq_notify_proc == p)
277 mq->mq_notify_proc = NULL;
278
279 /*
280 * If this is the last reference and mqueue is marked for unlink,
281 * remove and later destroy the message queue.
282 */
283 if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
284 LIST_REMOVE(mq, mq_list);
285 destroy = true;
286 } else
287 destroy = false;
288
289 mutex_exit(&mq->mq_mtx);
290 mutex_exit(&mqlist_mtx);
291
292 if (destroy)
293 mqueue_destroy(mq);
294
295 return 0;
296 }
297
298 /*
299 * General mqueue system calls.
300 */
301
302 int
303 sys_mq_open(struct lwp *l, const struct sys_mq_open_args *uap,
304 register_t *retval)
305 {
306 /* {
307 syscallarg(const char *) name;
308 syscallarg(int) oflag;
309 syscallarg(mode_t) mode;
310 syscallarg(struct mq_attr) attr;
311 } */
312 struct proc *p = l->l_proc;
313 struct mqueue *mq, *mq_new = NULL;
314 file_t *fp;
315 char *name;
316 int mqd, error, oflag;
317
318 oflag = SCARG(uap, oflag);
319
320 /* Get the name from the user-space */
321 name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
322 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
323 if (error) {
324 kmem_free(name, MQ_NAMELEN);
325 return error;
326 }
327
328 if (oflag & O_CREAT) {
329 struct cwdinfo *cwdi = p->p_cwdi;
330 struct mq_attr attr;
331
332 /* Check the limit */
333 if (p->p_mqueue_cnt == mq_open_max) {
334 kmem_free(name, MQ_NAMELEN);
335 return EMFILE;
336 }
337
338 /* Empty name is invalid */
339 if (name[0] == '\0') {
340 kmem_free(name, MQ_NAMELEN);
341 return EINVAL;
342 }
343
344 /* Check for mqueue attributes */
345 if (SCARG(uap, attr)) {
346 error = copyin(SCARG(uap, attr), &attr,
347 sizeof(struct mq_attr));
348 if (error) {
349 kmem_free(name, MQ_NAMELEN);
350 return error;
351 }
352 if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0 ||
353 attr.mq_msgsize > mq_max_msgsize) {
354 kmem_free(name, MQ_NAMELEN);
355 return EINVAL;
356 }
357 attr.mq_curmsgs = 0;
358 } else {
359 memset(&attr, 0, sizeof(struct mq_attr));
360 attr.mq_maxmsg = mq_def_maxmsg;
361 attr.mq_msgsize =
362 MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
363 }
364
365 /*
366 * Allocate new mqueue, initialize data structures,
367 * copy the name, attributes and set the flag.
368 */
369 mq_new = kmem_zalloc(sizeof(struct mqueue), KM_SLEEP);
370
371 mutex_init(&mq_new->mq_mtx, MUTEX_DEFAULT, IPL_NONE);
372 cv_init(&mq_new->mq_send_cv, "mqsendcv");
373 cv_init(&mq_new->mq_recv_cv, "mqrecvcv");
374 TAILQ_INIT(&mq_new->mq_head);
375 selinit(&mq_new->mq_rsel);
376 selinit(&mq_new->mq_wsel);
377
378 strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
379 memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
380
381 CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);
382 mq_new->mq_attrib.mq_flags = (O_MASK & oflag);
383
384 /* Store mode and effective UID with GID */
385 mq_new->mq_mode = ((SCARG(uap, mode) &
386 ~cwdi->cwdi_cmask) & ALLPERMS) & ~S_ISTXT;
387 mq_new->mq_euid = kauth_cred_geteuid(l->l_cred);
388 mq_new->mq_egid = kauth_cred_getegid(l->l_cred);
389 }
390
391 /* Allocate file structure and descriptor */
392 error = fd_allocfile(&fp, &mqd);
393 if (error) {
394 if (mq_new)
395 mqueue_destroy(mq_new);
396 kmem_free(name, MQ_NAMELEN);
397 return error;
398 }
399 fp->f_type = DTYPE_MQUEUE;
400 fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
401 fp->f_ops = &mqops;
402
403 /* Look up for mqueue with such name */
404 mutex_enter(&mqlist_mtx);
405 mq = mqueue_lookup(name);
406 if (mq) {
407 mode_t acc_mode;
408
409 KASSERT(mutex_owned(&mq->mq_mtx));
410
411 /* Check if mqueue is not marked as unlinking */
412 if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
413 error = EACCES;
414 goto exit;
415 }
416 /* Fail if O_EXCL is set, and mqueue already exists */
417 if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
418 error = EEXIST;
419 goto exit;
420 }
421
422 /*
423 * Check the permissions. Note the difference between
424 * VREAD/VWRITE and FREAD/FWRITE.
425 */
426 acc_mode = 0;
427 if (fp->f_flag & FREAD) {
428 acc_mode |= VREAD;
429 }
430 if (fp->f_flag & FWRITE) {
431 acc_mode |= VWRITE;
432 }
433 if (genfs_can_access(VNON, mq->mq_mode, mq->mq_euid,
434 mq->mq_egid, acc_mode, l->l_cred)) {
435 error = EACCES;
436 goto exit;
437 }
438 } else {
439 /* Fail if mqueue neither exists, nor we create it */
440 if ((oflag & O_CREAT) == 0) {
441 mutex_exit(&mqlist_mtx);
442 KASSERT(mq_new == NULL);
443 fd_abort(p, fp, mqd);
444 kmem_free(name, MQ_NAMELEN);
445 return ENOENT;
446 }
447
448 /* Check the limit */
449 if (p->p_mqueue_cnt == mq_open_max) {
450 error = EMFILE;
451 goto exit;
452 }
453
454 /* Insert the queue to the list */
455 mq = mq_new;
456 mutex_enter(&mq->mq_mtx);
457 LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
458 mq_new = NULL;
459 getnanotime(&mq->mq_btime);
460 mq->mq_atime = mq->mq_mtime = mq->mq_btime;
461 }
462
463 /* Increase the counters, and make descriptor ready */
464 p->p_mqueue_cnt++;
465 mq->mq_refcnt++;
466 fp->f_data = mq;
467 exit:
468 mutex_exit(&mq->mq_mtx);
469 mutex_exit(&mqlist_mtx);
470
471 if (mq_new)
472 mqueue_destroy(mq_new);
473 if (error) {
474 fd_abort(p, fp, mqd);
475 } else {
476 fd_affix(p, fp, mqd);
477 *retval = mqd;
478 }
479 kmem_free(name, MQ_NAMELEN);
480
481 return error;
482 }
483
484 int
485 sys_mq_close(struct lwp *l, const struct sys_mq_close_args *uap,
486 register_t *retval)
487 {
488
489 return sys_close(l, (const void *)uap, retval);
490 }
491
492 /*
493 * Primary mq_receive1() function.
494 */
495 int
496 mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
497 unsigned *msg_prio, int t, ssize_t *mlen)
498 {
499 file_t *fp = NULL;
500 struct mqueue *mq;
501 struct mq_msg *msg = NULL;
502 int error;
503
504 /* Get the message queue */
505 error = mqueue_get(mqdes, &fp);
506 if (error)
507 return error;
508 mq = fp->f_data;
509
510 getnanotime(&mq->mq_atime);
511 /* Check the message size limits */
512 if (msg_len < mq->mq_attrib.mq_msgsize) {
513 error = EMSGSIZE;
514 goto error;
515 }
516
517 /* Check if queue is empty */
518 while (TAILQ_EMPTY(&mq->mq_head)) {
519 if (mq->mq_attrib.mq_flags & O_NONBLOCK) {
520 error = EAGAIN;
521 goto error;
522 }
523 if (t < 0) {
524 error = EINVAL;
525 goto error;
526 }
527 /*
528 * Block until someone sends the message.
529 * While doing this, notification should not be sent.
530 */
531 mq->mq_attrib.mq_flags |= MQ_RECEIVE;
532 error = cv_timedwait_sig(&mq->mq_send_cv, &mq->mq_mtx, t);
533 mq->mq_attrib.mq_flags &= ~MQ_RECEIVE;
534 if (error || (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
535 error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
536 goto error;
537 }
538 }
539
540 /* Remove the message from the queue */
541 msg = TAILQ_FIRST(&mq->mq_head);
542 KASSERT(msg != NULL);
543 TAILQ_REMOVE(&mq->mq_head, msg, msg_queue);
544
545 /* Decrement the counter and signal waiter, if any */
546 mq->mq_attrib.mq_curmsgs--;
547 cv_signal(&mq->mq_recv_cv);
548
549 /* Ready for sending now */
550 selnotify(&mq->mq_wsel, POLLOUT | POLLWRNORM, 0);
551 error:
552 mutex_exit(&mq->mq_mtx);
553 fd_putfile((int)mqdes);
554 if (error)
555 return error;
556
557 /*
558 * Copy the data to the user-space.
559 * Note: According to POSIX, no message should be removed from the
560 * queue in case of fail - this would be violated.
561 */
562 *mlen = msg->msg_len;
563 error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
564 if (error == 0 && msg_prio)
565 error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
566 mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
567
568 return error;
569 }
570
571 int
572 sys_mq_receive(struct lwp *l, const struct sys_mq_receive_args *uap,
573 register_t *retval)
574 {
575 /* {
576 syscallarg(mqd_t) mqdes;
577 syscallarg(char *) msg_ptr;
578 syscallarg(size_t) msg_len;
579 syscallarg(unsigned *) msg_prio;
580 } */
581 int error;
582 ssize_t mlen;
583
584 error = mq_receive1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
585 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
586 if (error == 0)
587 *retval = mlen;
588
589 return error;
590 }
591
592 int
593 sys___mq_timedreceive50(struct lwp *l,
594 const struct sys___mq_timedreceive50_args *uap, register_t *retval)
595 {
596 /* {
597 syscallarg(mqd_t) mqdes;
598 syscallarg(char *) msg_ptr;
599 syscallarg(size_t) msg_len;
600 syscallarg(unsigned *) msg_prio;
601 syscallarg(const struct timespec *) abs_timeout;
602 } */
603 int error, t;
604 ssize_t mlen;
605 struct timespec ts;
606
607 /* Get and convert time value */
608 if (SCARG(uap, abs_timeout)) {
609 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
610 if (error)
611 return error;
612
613 error = abstimeout2timo(&ts, &t);
614 if (error)
615 return error;
616 } else
617 t = 0;
618
619 error = mq_receive1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
620 SCARG(uap, msg_len), SCARG(uap, msg_prio), t, &mlen);
621 if (error == 0)
622 *retval = mlen;
623
624 return error;
625 }
626
627 /*
628 * Primary mq_send1() function.
629 */
630 int
631 mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
632 unsigned msg_prio, int t)
633 {
634 file_t *fp = NULL;
635 struct mqueue *mq;
636 struct mq_msg *msg, *pos_msg;
637 struct proc *notify = NULL;
638 ksiginfo_t ksi;
639 size_t size;
640 int error;
641
642 /* Check the priority range */
643 if (msg_prio >= mq_prio_max)
644 return EINVAL;
645
646 /* Allocate a new message */
647 size = sizeof(struct mq_msg) + msg_len;
648 if (size > mq_max_msgsize)
649 return EMSGSIZE;
650
651 if (size > MQ_DEF_MSGSIZE)
652 msg = kmem_alloc(size, KM_SLEEP);
653 else
654 msg = pool_cache_get(mqmsg_cache, PR_WAITOK);
655
656 /* Get the data from user-space */
657 error = copyin(msg_ptr, msg->msg_ptr, msg_len);
658 if (error) {
659 mqueue_freemsg(msg, size);
660 return error;
661 }
662 msg->msg_len = msg_len;
663 msg->msg_prio = msg_prio;
664
665 /* Get the mqueue */
666 error = mqueue_get(mqdes, &fp);
667 if (error) {
668 mqueue_freemsg(msg, size);
669 return error;
670 }
671 mq = fp->f_data;
672
673 getnanotime(&mq->mq_mtime);
674
675 /* Check the message size limit */
676 if (msg_len <= 0 || msg_len > mq->mq_attrib.mq_msgsize) {
677 error = EMSGSIZE;
678 goto error;
679 }
680
681 /* Check if queue is full */
682 while (mq->mq_attrib.mq_curmsgs >= mq->mq_attrib.mq_maxmsg) {
683 if (mq->mq_attrib.mq_flags & O_NONBLOCK) {
684 error = EAGAIN;
685 goto error;
686 }
687 if (t < 0) {
688 error = EINVAL;
689 goto error;
690 }
691 /* Block until queue becomes available */
692 error = cv_timedwait_sig(&mq->mq_recv_cv, &mq->mq_mtx, t);
693 if (error || (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
694 error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
695 goto error;
696 }
697 }
698 KASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
699
700 /* Insert message into the queue, according to the priority */
701 TAILQ_FOREACH(pos_msg, &mq->mq_head, msg_queue)
702 if (msg->msg_prio > pos_msg->msg_prio)
703 break;
704 if (pos_msg == NULL)
705 TAILQ_INSERT_TAIL(&mq->mq_head, msg, msg_queue);
706 else
707 TAILQ_INSERT_BEFORE(pos_msg, msg, msg_queue);
708
709 /* Check for the notify */
710 if (mq->mq_attrib.mq_curmsgs == 0 && mq->mq_notify_proc &&
711 (mq->mq_attrib.mq_flags & MQ_RECEIVE) == 0) {
712 /* Initialize the signal */
713 KSI_INIT(&ksi);
714 ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;
715 ksi.ksi_code = SI_MESGQ;
716 ksi.ksi_value = mq->mq_sig_notify.sigev_value;
717 /* Unregister the process */
718 notify = mq->mq_notify_proc;
719 mq->mq_notify_proc = NULL;
720 }
721
722 /* Increment the counter and signal waiter, if any */
723 mq->mq_attrib.mq_curmsgs++;
724 cv_signal(&mq->mq_send_cv);
725
726 /* Ready for receiving now */
727 selnotify(&mq->mq_rsel, POLLIN | POLLRDNORM, 0);
728 error:
729 mutex_exit(&mq->mq_mtx);
730 fd_putfile((int)mqdes);
731
732 if (error) {
733 mqueue_freemsg(msg, size);
734 } else if (notify) {
735 /* Send the notify, if needed */
736 mutex_enter(proc_lock);
737 kpsignal(notify, &ksi, NULL);
738 mutex_exit(proc_lock);
739 }
740
741 return error;
742 }
743
744 int
745 sys_mq_send(struct lwp *l, const struct sys_mq_send_args *uap,
746 register_t *retval)
747 {
748 /* {
749 syscallarg(mqd_t) mqdes;
750 syscallarg(const char *) msg_ptr;
751 syscallarg(size_t) msg_len;
752 syscallarg(unsigned) msg_prio;
753 } */
754
755 return mq_send1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
756 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
757 }
758
759 int
760 sys___mq_timedsend50(struct lwp *l, const struct sys___mq_timedsend50_args *uap,
761 register_t *retval)
762 {
763 /* {
764 syscallarg(mqd_t) mqdes;
765 syscallarg(const char *) msg_ptr;
766 syscallarg(size_t) msg_len;
767 syscallarg(unsigned) msg_prio;
768 syscallarg(const struct timespec *) abs_timeout;
769 } */
770 int t;
771 struct timespec ts;
772 int error;
773
774 /* Get and convert time value */
775 if (SCARG(uap, abs_timeout)) {
776 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
777 if (error)
778 return error;
779 error = abstimeout2timo(&ts, &t);
780 if (error)
781 return error;
782 } else
783 t = 0;
784
785 return mq_send1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
786 SCARG(uap, msg_len), SCARG(uap, msg_prio), t);
787 }
788
789 int
790 sys_mq_notify(struct lwp *l, const struct sys_mq_notify_args *uap,
791 register_t *retval)
792 {
793 /* {
794 syscallarg(mqd_t) mqdes;
795 syscallarg(const struct sigevent *) notification;
796 } */
797 file_t *fp = NULL;
798 struct mqueue *mq;
799 struct sigevent sig;
800 int error;
801
802 if (SCARG(uap, notification)) {
803 /* Get the signal from user-space */
804 error = copyin(SCARG(uap, notification), &sig,
805 sizeof(struct sigevent));
806 if (error)
807 return error;
808 }
809
810 error = mqueue_get(SCARG(uap, mqdes), &fp);
811 if (error)
812 return error;
813 mq = fp->f_data;
814
815 if (SCARG(uap, notification)) {
816 /* Register notification: set the signal and target process */
817 if (mq->mq_notify_proc == NULL) {
818 memcpy(&mq->mq_sig_notify, &sig,
819 sizeof(struct sigevent));
820 mq->mq_notify_proc = l->l_proc;
821 } else {
822 /* Fail if someone else already registered */
823 error = EBUSY;
824 }
825 } else {
826 /* Unregister the notification */
827 mq->mq_notify_proc = NULL;
828 }
829 mutex_exit(&mq->mq_mtx);
830 fd_putfile((int)SCARG(uap, mqdes));
831
832 return error;
833 }
834
835 int
836 sys_mq_getattr(struct lwp *l, const struct sys_mq_getattr_args *uap,
837 register_t *retval)
838 {
839 /* {
840 syscallarg(mqd_t) mqdes;
841 syscallarg(struct mq_attr *) mqstat;
842 } */
843 file_t *fp = NULL;
844 struct mqueue *mq;
845 struct mq_attr attr;
846 int error;
847
848 /* Get the message queue */
849 error = mqueue_get(SCARG(uap, mqdes), &fp);
850 if (error)
851 return error;
852 mq = fp->f_data;
853 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
854 mutex_exit(&mq->mq_mtx);
855 fd_putfile((int)SCARG(uap, mqdes));
856
857 return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
858 }
859
860 int
861 sys_mq_setattr(struct lwp *l, const struct sys_mq_setattr_args *uap,
862 register_t *retval)
863 {
864 /* {
865 syscallarg(mqd_t) mqdes;
866 syscallarg(const struct mq_attr *) mqstat;
867 syscallarg(struct mq_attr *) omqstat;
868 } */
869 file_t *fp = NULL;
870 struct mqueue *mq;
871 struct mq_attr attr;
872 int error, nonblock;
873
874 error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
875 if (error)
876 return error;
877 nonblock = (attr.mq_flags & O_NONBLOCK);
878
879 /* Get the message queue */
880 error = mqueue_get(SCARG(uap, mqdes), &fp);
881 if (error)
882 return error;
883 mq = fp->f_data;
884
885 /* Copy the old attributes, if needed */
886 if (SCARG(uap, omqstat))
887 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
888
889 /* Ignore everything, except O_NONBLOCK */
890 if (nonblock)
891 mq->mq_attrib.mq_flags |= O_NONBLOCK;
892 else
893 mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
894
895 mutex_exit(&mq->mq_mtx);
896 fd_putfile((int)SCARG(uap, mqdes));
897
898 /*
899 * Copy the data to the user-space.
900 * Note: According to POSIX, the new attributes should not be set in
901 * case of fail - this would be violated.
902 */
903 if (SCARG(uap, omqstat))
904 error = copyout(&attr, SCARG(uap, omqstat),
905 sizeof(struct mq_attr));
906
907 return error;
908 }
909
910 int
911 sys_mq_unlink(struct lwp *l, const struct sys_mq_unlink_args *uap,
912 register_t *retval)
913 {
914 /* {
915 syscallarg(const char *) name;
916 } */
917 struct mqueue *mq;
918 char *name;
919 int error, refcnt = 0;
920
921 /* Get the name from the user-space */
922 name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
923 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
924 if (error) {
925 kmem_free(name, MQ_NAMELEN);
926 return error;
927 }
928
929 /* Lookup for this file */
930 mutex_enter(&mqlist_mtx);
931 mq = mqueue_lookup(name);
932 if (mq == NULL) {
933 error = ENOENT;
934 goto error;
935 }
936
937 /* Check the permissions */
938 if (kauth_cred_geteuid(l->l_cred) != mq->mq_euid &&
939 kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
940 mutex_exit(&mq->mq_mtx);
941 error = EACCES;
942 goto error;
943 }
944
945 /* Mark message queue as unlinking, before leaving the window */
946 mq->mq_attrib.mq_flags |= MQ_UNLINK;
947
948 /* Wake up all waiters, if there are such */
949 cv_broadcast(&mq->mq_send_cv);
950 cv_broadcast(&mq->mq_recv_cv);
951
952 selnotify(&mq->mq_rsel, POLLHUP, 0);
953 selnotify(&mq->mq_wsel, POLLHUP, 0);
954
955 refcnt = mq->mq_refcnt;
956 if (refcnt == 0)
957 LIST_REMOVE(mq, mq_list);
958
959 mutex_exit(&mq->mq_mtx);
960 error:
961 mutex_exit(&mqlist_mtx);
962
963 /*
964 * If there are no references - destroy the message
965 * queue, otherwise, the last mq_close() will do that.
966 */
967 if (error == 0 && refcnt == 0)
968 mqueue_destroy(mq);
969
970 kmem_free(name, MQ_NAMELEN);
971 return error;
972 }
973
974 /*
975 * SysCtl.
976 */
977
978 SYSCTL_SETUP(sysctl_mqueue_setup, "sysctl mqueue setup")
979 {
980 const struct sysctlnode *node = NULL;
981
982 sysctl_createv(clog, 0, NULL, NULL,
983 CTLFLAG_PERMANENT,
984 CTLTYPE_NODE, "kern", NULL,
985 NULL, 0, NULL, 0,
986 CTL_KERN, CTL_EOL);
987 sysctl_createv(clog, 0, NULL, NULL,
988 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
989 CTLTYPE_INT, "posix_msg",
990 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
991 "Message Passing option to which the "
992 "system attempts to conform"),
993 NULL, _POSIX_MESSAGE_PASSING, NULL, 0,
994 CTL_KERN, CTL_CREATE, CTL_EOL);
995 sysctl_createv(clog, 0, NULL, &node,
996 CTLFLAG_PERMANENT,
997 CTLTYPE_NODE, "mqueue",
998 SYSCTL_DESCR("Message queue options"),
999 NULL, 0, NULL, 0,
1000 CTL_KERN, CTL_CREATE, CTL_EOL);
1001
1002 if (node == NULL)
1003 return;
1004
1005 sysctl_createv(clog, 0, &node, NULL,
1006 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1007 CTLTYPE_INT, "mq_open_max",
1008 SYSCTL_DESCR("Maximal number of message queue descriptors "
1009 "that process could open"),
1010 NULL, 0, &mq_open_max, 0,
1011 CTL_CREATE, CTL_EOL);
1012 sysctl_createv(clog, 0, &node, NULL,
1013 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1014 CTLTYPE_INT, "mq_prio_max",
1015 SYSCTL_DESCR("Maximal priority of the message"),
1016 NULL, 0, &mq_prio_max, 0,
1017 CTL_CREATE, CTL_EOL);
1018 sysctl_createv(clog, 0, &node, NULL,
1019 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1020 CTLTYPE_INT, "mq_max_msgsize",
1021 SYSCTL_DESCR("Maximal allowed size of the message"),
1022 NULL, 0, &mq_max_msgsize, 0,
1023 CTL_CREATE, CTL_EOL);
1024 sysctl_createv(clog, 0, &node, NULL,
1025 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1026 CTLTYPE_INT, "mq_def_maxmsg",
1027 SYSCTL_DESCR("Default maximal message count"),
1028 NULL, 0, &mq_def_maxmsg, 0,
1029 CTL_CREATE, CTL_EOL);
1030 }
1031
1032 /*
1033 * Debugging.
1034 */
1035 #if defined(DDB)
1036
1037 void
1038 mqueue_print_list(void (*pr)(const char *, ...))
1039 {
1040 struct mqueue *mq;
1041
1042 (*pr)("Global list of the message queues:\n");
1043 (*pr)("%20s %10s %8s %8s %3s %4s %4s %4s\n",
1044 "Name", "Ptr", "Mode", "Flags", "Ref",
1045 "MaxMsg", "MsgSze", "CurMsg");
1046 LIST_FOREACH(mq, &mqueue_head, mq_list) {
1047 (*pr)("%20s %10p %8x %8x %3u %6lu %6lu %6lu\n",
1048 mq->mq_name, mq, mq->mq_mode,
1049 mq->mq_attrib.mq_flags, mq->mq_refcnt,
1050 mq->mq_attrib.mq_maxmsg, mq->mq_attrib.mq_msgsize,
1051 mq->mq_attrib.mq_curmsgs);
1052 }
1053 }
1054
1055 #endif /* defined(DDB) */
1056