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