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