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