2152935700c4799e678e5445d49bf5c87884c441
[dragonfly.git] / sys / kern / sys_mqueue.c
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 < nstick)
250                 ts->tv_nsec = nstick;
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         error = itimespecfix(ts);
281         if (error) {
282                 return error;
283         }
284         getnanotime(&tsd);
285         timespecsub(ts, &tsd);
286         if (ts->tv_sec < 0 || (ts->tv_sec == 0 && ts->tv_nsec <= 0)) {
287                 return ETIMEDOUT;
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 thread *td = curthread;
393         struct proc *p = td->td_proc;
394         struct filedesc *fdp = p->p_fd;
395         struct mqueue *mq, *mq_new = NULL;
396         file_t *fp;
397         char *name;
398         int mqd, error, oflag;
399
400         /* Check access mode flags */
401         oflag = SCARG(uap, oflag);
402         if ((oflag & O_ACCMODE) == (O_WRONLY | O_RDWR)) {
403                 return EINVAL;
404         }
405
406         /* Get the name from the user-space */
407         name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
408         error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
409         if (error) {
410                 kfree(name, M_MQBUF);
411                 return error;
412         }
413
414         if (oflag & O_CREAT) {
415                 struct mq_attr attr;
416                 u_int i;
417
418                 /* Check the limit */
419                 if (p->p_mqueue_cnt == mq_open_max) {
420                         kfree(name, M_MQBUF);
421                         return EMFILE;
422                 }
423
424                 /* Empty name is invalid */
425                 if (name[0] == '\0') {
426                         kfree(name, M_MQBUF);
427                         return EINVAL;
428                 }
429
430                 /* Check for mqueue attributes */
431                 if (SCARG(uap, attr)) {
432                         error = copyin(SCARG(uap, attr), &attr,
433                                 sizeof(struct mq_attr));
434                         if (error) {
435                                 kfree(name, M_MQBUF);
436                                 return error;
437                         }
438                         if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0 ||
439                             attr.mq_msgsize > mq_max_msgsize) {
440                                 kfree(name, M_MQBUF);
441                                 return EINVAL;
442                         }
443                         attr.mq_curmsgs = 0;
444                 } else {
445                         memset(&attr, 0, sizeof(struct mq_attr));
446                         attr.mq_maxmsg = mq_def_maxmsg;
447                         attr.mq_msgsize =
448                             MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
449                 }
450
451                 /*
452                  * Allocate new mqueue, initialize data structures,
453                  * copy the name, attributes and set the flag.
454                  */
455                 mq_new = kmalloc(sizeof(struct mqueue), M_MQBUF, M_WAITOK | M_ZERO);
456
457                 lockinit(&mq_new->mq_mtx, "mq_new->mq_mtx", 0, LK_CANRECURSE);
458                 for (i = 0; i < (MQ_PQSIZE + 1); i++) {
459                         TAILQ_INIT(&mq_new->mq_head[i]);
460                 }
461
462                 strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
463                 memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
464
465                 /*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
466                 /* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
467                 mq_new->mq_attrib.mq_flags = oflag;
468
469                 /* Store mode and effective UID with GID */
470                 mq_new->mq_mode = ((SCARG(uap, mode) &
471                     ~p->p_fd->fd_cmask) & ALLPERMS) & ~S_ISTXT;
472                 mq_new->mq_euid = td->td_ucred->cr_uid;
473                 mq_new->mq_egid = td->td_ucred->cr_svgid;
474         }
475
476         /* Allocate file structure and descriptor */
477         error = falloc(td->td_lwp, &fp, &mqd);
478         if (error) {
479                 if (mq_new)
480                         mqueue_destroy(mq_new);
481                 kfree(name, M_MQBUF);
482                 return error;
483         }
484         fp->f_type = DTYPE_MQUEUE;
485         fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
486         fp->f_ops = &mqops;
487
488         /* Look up for mqueue with such name */
489         lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
490         mq = mqueue_lookup(name);
491         if (mq) {
492                 int acc_mode;
493
494                 KKASSERT(lockstatus(&mq->mq_mtx, curthread));
495
496                 /* Check if mqueue is not marked as unlinking */
497                 if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
498                         error = EACCES;
499                         goto exit;
500                 }
501                 /* Fail if O_EXCL is set, and mqueue already exists */
502                 if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
503                         error = EEXIST;
504                         goto exit;
505                 }
506
507                 /*
508                  * Check the permissions. Note the difference between
509                  * VREAD/VWRITE and FREAD/FWRITE.
510                  */
511                 acc_mode = 0;
512                 if (fp->f_flag & FREAD) {
513                         acc_mode |= VREAD;
514                 }
515                 if (fp->f_flag & FWRITE) {
516                         acc_mode |= VWRITE;
517                 }
518                 if (vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid,
519                         acc_mode, td->td_ucred)) {
520
521                         error = EACCES;
522                         goto exit;
523                 }
524         } else {
525                 /* Fail if mqueue neither exists, nor we create it */
526                 if ((oflag & O_CREAT) == 0) {
527                         lockmgr(&mqlist_mtx, LK_RELEASE);
528                         KKASSERT(mq_new == NULL);
529                         fsetfd(fdp, NULL, mqd);
530                         fp->f_ops = &badfileops;
531                         fdrop(fp);
532                         kfree(name, M_MQBUF);
533                         return ENOENT;
534                 }
535
536                 /* Check the limit */
537                 if (p->p_mqueue_cnt == mq_open_max) {
538                         error = EMFILE;
539                         goto exit;
540                 }
541
542                 /* Insert the queue to the list */
543                 mq = mq_new;
544                 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
545                 LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
546                 mq_new = NULL;
547                 getnanotime(&mq->mq_btime);
548                 mq->mq_atime = mq->mq_mtime = mq->mq_btime;
549         }
550
551         /* Increase the counters, and make descriptor ready */
552         p->p_mqueue_cnt++;
553         mq->mq_refcnt++;
554         fp->f_data = mq;
555 exit:
556         lockmgr(&mq->mq_mtx, LK_RELEASE);
557         lockmgr(&mqlist_mtx, LK_RELEASE);
558
559         if (mq_new)
560                 mqueue_destroy(mq_new);
561         if (error) {
562                 fsetfd(fdp, NULL, mqd);
563                 fp->f_ops = &badfileops;
564         } else {
565                 fsetfd(fdp, fp, mqd);
566                 uap->sysmsg_result = mqd;
567         }
568         fdrop(fp);
569         kfree(name, M_MQBUF);
570
571         return error;
572 }
573
574 int
575 sys_mq_close(struct mq_close_args *uap)
576 {
577         return sys_close((void *)uap);
578 }
579
580 /*
581  * Primary mq_receive1() function.
582  */
583 int
584 mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
585     unsigned *msg_prio, struct timespec *ts, ssize_t *mlen)
586 {
587         file_t *fp = NULL;
588         struct mqueue *mq;
589         struct mq_msg *msg = NULL;
590         struct mq_attr *mqattr;
591         u_int idx;
592         int error;
593
594         /* Get the message queue */
595         error = mqueue_get(l, mqdes, &fp);
596         if (error) {
597                 return error;
598         }
599         mq = fp->f_data;
600         if ((fp->f_flag & FREAD) == 0) {
601                 error = EBADF;
602                 goto error;
603         }
604         getnanotime(&mq->mq_atime);
605         mqattr = &mq->mq_attrib;
606
607         /* Check the message size limits */
608         if (msg_len < mqattr->mq_msgsize) {
609                 error = EMSGSIZE;
610                 goto error;
611         }
612
613         /* Check if queue is empty */
614         while (mqattr->mq_curmsgs == 0) {
615                 int t;
616
617                 if (mqattr->mq_flags & O_NONBLOCK) {
618                         error = EAGAIN;
619                         goto error;
620                 }
621                 if (ts) {
622                         error = abstimeout2timo(ts, &t);
623                         if (error)
624                                 goto error;
625                 } else
626                         t = 0;
627                 /*
628                  * Block until someone sends the message.
629                  * While doing this, notification should not be sent.
630                  */
631                 mqattr->mq_flags |= MQ_RECEIVE;
632                 error = tsleep(&mq->mq_send_cv, PCATCH, "mqsend", t);
633                 mqattr->mq_flags &= ~MQ_RECEIVE;
634                 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
635                         error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
636                         goto error;
637                 }
638         }
639
640
641         /*
642          * Find the highest priority message, and remove it from the queue.
643          * At first, reserved queue is checked, bitmap is next.
644          */
645         msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
646         if (__predict_true(msg == NULL)) {
647                 idx = ffs(mq->mq_bitmap);
648                 msg = TAILQ_FIRST(&mq->mq_head[idx]);
649                 KKASSERT(msg != NULL);
650         } else {
651                 idx = MQ_PQRESQ;
652         }
653         TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
654
655         /* Unmark the bit, if last message. */
656         if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
657                 KKASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
658                 mq->mq_bitmap &= ~(1 << --idx);
659         }
660
661         /* Decrement the counter and signal waiter, if any */
662         mqattr->mq_curmsgs--;
663         wakeup_one(&mq->mq_recv_cv);
664
665         /* Ready for sending now */
666         selwakeup(&mq->mq_wsel);
667 error:
668         lockmgr(&mq->mq_mtx, LK_RELEASE);
669         fdrop(fp);
670         if (error)
671                 return error;
672
673         /*
674          * Copy the data to the user-space.
675          * Note: According to POSIX, no message should be removed from the
676          * queue in case of fail - this would be violated.
677          */
678         *mlen = msg->msg_len;
679         error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
680         if (error == 0 && msg_prio)
681                 error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
682         mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
683
684         return error;
685 }
686
687 int
688 sys_mq_receive(struct mq_receive_args *uap)
689 {
690         /* {
691                 syscallarg(mqd_t) mqdes;
692                 syscallarg(char *) msg_ptr;
693                 syscallarg(size_t) msg_len;
694                 syscallarg(unsigned *) msg_prio;
695         } */
696         ssize_t mlen;
697         int error;
698
699         error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
700             SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
701         if (error == 0)
702                 uap->sysmsg_result = mlen;
703
704         return error;
705 }
706
707 int
708 sys_mq_timedreceive(struct mq_timedreceive_args *uap)
709 {
710         /* {
711                 syscallarg(mqd_t) mqdes;
712                 syscallarg(char *) msg_ptr;
713                 syscallarg(size_t) msg_len;
714                 syscallarg(unsigned *) msg_prio;
715                 syscallarg(const struct timespec *) abs_timeout;
716         } */
717         int error;
718         ssize_t mlen;
719         struct timespec ts, *tsp;
720
721         /* Get and convert time value */
722         if (SCARG(uap, abs_timeout)) {
723                 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
724                 if (error)
725                         return error;
726                 tsp = &ts;
727         } else {
728                 tsp = NULL;
729         }
730
731         error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
732             SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
733         if (error == 0)
734                 uap->sysmsg_result = mlen;
735
736         return error;
737 }
738
739 /*
740  * Primary mq_send1() function.
741  */
742 int
743 mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
744     unsigned msg_prio, struct timespec *ts)
745 {
746         file_t *fp = NULL;
747         struct mqueue *mq;
748         struct mq_msg *msg;
749         struct mq_attr *mqattr;
750         struct proc *notify = NULL;
751         /*ksiginfo_t ksi;*/
752         size_t size;
753         int error;
754
755         /* Check the priority range */
756         if (msg_prio >= mq_prio_max)
757                 return EINVAL;
758
759         /* Allocate a new message */
760         size = sizeof(struct mq_msg) + msg_len;
761         if (size > mq_max_msgsize)
762                 return EMSGSIZE;
763
764         if (size > MQ_DEF_MSGSIZE) {
765                 msg = kmalloc(size, M_MQBUF, M_WAITOK);
766         } else {
767                 msg = objcache_get(mqmsg_cache, M_WAITOK);
768         }
769
770         /* Get the data from user-space */
771         error = copyin(msg_ptr, msg->msg_ptr, msg_len);
772         if (error) {
773                 mqueue_freemsg(msg, size);
774                 return error;
775         }
776         msg->msg_len = msg_len;
777         msg->msg_prio = msg_prio;
778
779         /* Get the mqueue */
780         error = mqueue_get(l, mqdes, &fp);
781         if (error) {
782                 mqueue_freemsg(msg, size);
783                 return error;
784         }
785         mq = fp->f_data;
786         if ((fp->f_flag & FWRITE) == 0) {
787                 error = EBADF;
788                 goto error;
789         }
790         getnanotime(&mq->mq_mtime);
791         mqattr = &mq->mq_attrib;
792
793         /* Check the message size limit */
794         if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
795                 error = EMSGSIZE;
796                 goto error;
797         }
798
799         /* Check if queue is full */
800         while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
801                 int t;
802
803                 if (mqattr->mq_flags & O_NONBLOCK) {
804                         error = EAGAIN;
805                         goto error;
806                 }
807                 if (ts) {
808                         error = abstimeout2timo(ts, &t);
809                         if (error)
810                                 goto error;
811                 } else
812                         t = 0;
813                 /* Block until queue becomes available */
814                 error = tsleep(&mq->mq_recv_cv, PCATCH, "mqrecv", t);
815                 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
816                         error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
817                         goto error;
818                 }
819         }
820         KKASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
821
822         /*
823          * Insert message into the queue, according to the priority.
824          * Note the difference between index and priority.
825          */
826         if (__predict_true(msg_prio < MQ_PQSIZE)) {
827                 u_int idx = MQ_PQSIZE - msg_prio;
828
829                 KKASSERT(idx != MQ_PQRESQ);
830                 TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
831                 mq->mq_bitmap |= (1 << --idx);
832         } else {
833                 mqueue_linear_insert(mq, msg);
834         }
835
836         /* Check for the notify */
837         if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
838             (mqattr->mq_flags & MQ_RECEIVE) == 0 &&
839             mq->mq_sig_notify.sigev_notify == SIGEV_SIGNAL) {
840                 /* Initialize the signal */
841                 /*KSI_INIT(&ksi);*/
842                 /*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
843                 /*ksi.ksi_code = SI_MESGQ;*/
844                 /*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
845                 /* Unregister the process */
846                 notify = mq->mq_notify_proc;
847                 mq->mq_notify_proc = NULL;
848         }
849
850         /* Increment the counter and signal waiter, if any */
851         mqattr->mq_curmsgs++;
852         wakeup_one(&mq->mq_send_cv);
853
854         /* Ready for receiving now */
855         selwakeup(&mq->mq_rsel);
856 error:
857         lockmgr(&mq->mq_mtx, LK_RELEASE);
858         fdrop(fp);
859
860         if (error) {
861                 mqueue_freemsg(msg, size);
862         } else if (notify) {
863                 /* Send the notify, if needed */
864                 spin_lock_wr(&allproc_spin);
865                 /*kpsignal(notify, &ksi, NULL);*/
866                 ksignal(notify, mq->mq_sig_notify.sigev_signo);
867                 spin_unlock_wr(&allproc_spin);
868         }
869
870         return error;
871 }
872
873 int
874 sys_mq_send(struct mq_send_args *uap)
875 {
876         /* {
877                 syscallarg(mqd_t) mqdes;
878                 syscallarg(const char *) msg_ptr;
879                 syscallarg(size_t) msg_len;
880                 syscallarg(unsigned) msg_prio;
881         } */
882
883         return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
884             SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
885 }
886
887 int
888 sys_mq_timedsend(struct mq_timedsend_args *uap)
889 {
890         /* {
891                 syscallarg(mqd_t) mqdes;
892                 syscallarg(const char *) msg_ptr;
893                 syscallarg(size_t) msg_len;
894                 syscallarg(unsigned) msg_prio;
895                 syscallarg(const struct timespec *) abs_timeout;
896         } */
897         struct timespec ts, *tsp;
898         int error;
899
900         /* Get and convert time value */
901         if (SCARG(uap, abs_timeout)) {
902                 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
903                 if (error)
904                         return error;
905                 tsp = &ts;
906         } else {
907                 tsp = NULL;
908         }
909
910         return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
911             SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
912 }
913
914 int
915 sys_mq_notify(struct mq_notify_args *uap)
916 {
917         /* {
918                 syscallarg(mqd_t) mqdes;
919                 syscallarg(const struct sigevent *) notification;
920         } */
921         file_t *fp = NULL;
922         struct mqueue *mq;
923         struct sigevent sig;
924         int error;
925
926         if (SCARG(uap, notification)) {
927                 /* Get the signal from user-space */
928                 error = copyin(SCARG(uap, notification), &sig,
929                     sizeof(struct sigevent));
930                 if (error)
931                         return error;
932                 if (sig.sigev_notify == SIGEV_SIGNAL &&
933                     (sig.sigev_signo <= 0 || sig.sigev_signo >= NSIG))
934                         return EINVAL;
935         }
936
937         error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
938         if (error)
939                 return error;
940         mq = fp->f_data;
941
942         if (SCARG(uap, notification)) {
943                 /* Register notification: set the signal and target process */
944                 if (mq->mq_notify_proc == NULL) {
945                         memcpy(&mq->mq_sig_notify, &sig,
946                             sizeof(struct sigevent));
947                         mq->mq_notify_proc = curproc;
948                 } else {
949                         /* Fail if someone else already registered */
950                         error = EBUSY;
951                 }
952         } else {
953                 /* Unregister the notification */
954                 mq->mq_notify_proc = NULL;
955         }
956         lockmgr(&mq->mq_mtx, LK_RELEASE);
957         fdrop(fp);
958
959         return error;
960 }
961
962 int
963 sys_mq_getattr(struct mq_getattr_args *uap)
964 {
965         /* {
966                 syscallarg(mqd_t) mqdes;
967                 syscallarg(struct mq_attr *) mqstat;
968         } */
969         file_t *fp = NULL;
970         struct mqueue *mq;
971         struct mq_attr attr;
972         int error;
973
974         /* Get the message queue */
975         error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
976         if (error)
977                 return error;
978         mq = fp->f_data;
979         memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
980         lockmgr(&mq->mq_mtx, LK_RELEASE);
981         fdrop(fp);
982
983         return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
984 }
985
986 int
987 sys_mq_setattr(struct mq_setattr_args *uap)
988 {
989         /* {
990                 syscallarg(mqd_t) mqdes;
991                 syscallarg(const struct mq_attr *) mqstat;
992                 syscallarg(struct mq_attr *) omqstat;
993         } */
994         file_t *fp = NULL;
995         struct mqueue *mq;
996         struct mq_attr attr;
997         int error, nonblock;
998
999         error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
1000         if (error)
1001                 return error;
1002         nonblock = (attr.mq_flags & O_NONBLOCK);
1003
1004         /* Get the message queue */
1005         error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
1006         if (error)
1007                 return error;
1008         mq = fp->f_data;
1009
1010         /* Copy the old attributes, if needed */
1011         if (SCARG(uap, omqstat))
1012                 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
1013
1014         /* Ignore everything, except O_NONBLOCK */
1015         if (nonblock)
1016                 mq->mq_attrib.mq_flags |= O_NONBLOCK;
1017         else
1018                 mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
1019
1020         lockmgr(&mq->mq_mtx, LK_RELEASE);
1021         fdrop(fp);
1022
1023         /*
1024          * Copy the data to the user-space.
1025          * Note: According to POSIX, the new attributes should not be set in
1026          * case of fail - this would be violated.
1027          */
1028         if (SCARG(uap, omqstat))
1029                 error = copyout(&attr, SCARG(uap, omqstat),
1030                     sizeof(struct mq_attr));
1031
1032         return error;
1033 }
1034
1035 int
1036 sys_mq_unlink(struct mq_unlink_args *uap)
1037 {
1038         /* {
1039                 syscallarg(const char *) name;
1040         } */
1041         struct thread *td = curthread;
1042         struct mqueue *mq;
1043         char *name;
1044         int error, refcnt = 0;
1045
1046         /* Get the name from the user-space */
1047         name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
1048         error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
1049         if (error) {
1050                 kfree(name, M_MQBUF);
1051                 return error;
1052         }
1053
1054         /* Lookup for this file */
1055         lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
1056         mq = mqueue_lookup(name);
1057         if (mq == NULL) {
1058                 error = ENOENT;
1059                 goto error;
1060         }
1061
1062         /* Check the permissions */
1063         if (td->td_ucred->cr_uid != mq->mq_euid &&
1064             priv_check(td, PRIV_ROOT) != 0) {
1065                 lockmgr(&mq->mq_mtx, LK_RELEASE);
1066                 error = EACCES;
1067                 goto error;
1068         }
1069
1070         /* Mark message queue as unlinking, before leaving the window */
1071         mq->mq_attrib.mq_flags |= MQ_UNLINK;
1072
1073         /* Wake up all waiters, if there are such */
1074         wakeup(&mq->mq_send_cv);
1075         wakeup(&mq->mq_recv_cv);
1076
1077         selwakeup(&mq->mq_rsel);
1078         selwakeup(&mq->mq_wsel);
1079
1080         refcnt = mq->mq_refcnt;
1081         if (refcnt == 0)
1082                 LIST_REMOVE(mq, mq_list);
1083
1084         lockmgr(&mq->mq_mtx, LK_RELEASE);
1085 error:
1086         lockmgr(&mqlist_mtx, LK_RELEASE);
1087
1088         /*
1089          * If there are no references - destroy the message
1090          * queue, otherwise, the last mq_close() will do that.
1091          */
1092         if (error == 0 && refcnt == 0)
1093                 mqueue_destroy(mq);
1094
1095         kfree(name, M_MQBUF);
1096         return error;
1097 }
1098
1099 /*
1100  * SysCtl.
1101  */
1102 SYSCTL_NODE(_kern, OID_AUTO, mqueue,
1103     CTLFLAG_RW, 0, "Message queue options");
1104
1105 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_open_max,
1106     CTLFLAG_RW, &mq_open_max, 0,
1107     "Maximal number of message queue descriptors per process");
1108
1109 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_prio_max,
1110     CTLFLAG_RW, &mq_prio_max, 0,
1111     "Maximal priority of the message");
1112
1113 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_max_msgsize,
1114     CTLFLAG_RW, &mq_max_msgsize, 0,
1115     "Maximal allowed size of the message");
1116
1117 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_def_maxmsg,
1118     CTLFLAG_RW, &mq_def_maxmsg, 0,
1119     "Default maximal message count");
1120
1121 SYSINIT(sys_mqueue_init, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, mqueue_sysinit, NULL);