mqueues: Port POSIX message queues from NetBSD.
[dragonfly.git] / sys / sys / mqueue.h
1 /*      $NetBSD: mqueue.h,v 1.7 2009/04/11 15:47:34 christos Exp $      */
2
3 /*
4  * Copyright (c) 2007, 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 #ifndef _SYS_MQUEUE_H_
30 #define _SYS_MQUEUE_H_
31
32 /* Maximal number of mqueue descriptors, that process could open */
33 #define MQ_OPEN_MAX             512
34
35 /* Maximal priority of the message */
36 #define MQ_PRIO_MAX             32
37
38 struct mq_attr {
39         long    mq_flags;       /* Flags of message queue */
40         long    mq_maxmsg;      /* Maximum number of messages */
41         long    mq_msgsize;     /* Maximum size of the message */
42         long    mq_curmsgs;     /* Count of the queued messages */
43 };
44
45 /* Internal kernel data */
46 /*#ifdef _KERNEL*/
47 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
48
49 /*#include <sys/condvar.h>*/
50 /*#include <sys/mutex.h>*/
51 #include <sys/types.h>
52 #include <sys/lock.h>
53 #include <sys/types.h>
54 #include <sys/queue.h>
55 #include <sys/selinfo.h>
56 #include <sys/types.h>
57
58 /*
59  * Flags below are used in mq_flags for internal
60  * purposes, this is appropriate according to POSIX.
61  */
62
63 /* Message queue is unlinking */
64 #define MQ_UNLINK               0x10000000
65 /* There are receive-waiters */
66 #define MQ_RECEIVE              0x20000000
67
68 /* Maximal length of mqueue name */
69 #define MQ_NAMELEN              (NAME_MAX + 1)
70
71 /* Default size of the message */
72 #define MQ_DEF_MSGSIZE          1024
73
74 /* Size/bits and index of reserved queue */
75 #define MQ_PQSIZE               32
76 #define MQ_PQRESQ               0
77
78 /* Structure of the message queue */
79 struct mqueue {
80         char                    mq_name[MQ_NAMELEN];
81         struct lock             mq_mtx;
82         int                     mq_send_cv;     /* Condition variables for tsleep() */
83         int                     mq_recv_cv;
84         struct mq_attr          mq_attrib;
85         /* Notification */
86         struct selinfo          mq_rsel;
87         struct selinfo          mq_wsel;
88         struct sigevent         mq_sig_notify;
89         struct proc *           mq_notify_proc;
90         /* Permissions */
91         mode_t                  mq_mode;
92         uid_t                   mq_euid;
93         gid_t                   mq_egid;
94         /*
95          * Reference counter, heads of the message queue
96          *
97          * Every message queue has `MQ_PQSIZE' priority queues that guarantee
98          * constant-time insertion of messages. If `mq_prio_max' is increased
99          * via sysctl, then the reserved queue (MQ_PQRESQ) is used which
100          * performs linear insertion.
101          */
102         u_int                   mq_refcnt;
103         TAILQ_HEAD(, mq_msg)    mq_head[1 + MQ_PQSIZE];
104         uint32_t                mq_bitmap;
105         /* Entry of the global list */
106         LIST_ENTRY(mqueue)      mq_list;
107         struct timespec         mq_atime;
108         struct timespec         mq_mtime;
109         struct timespec         mq_btime;
110 };
111
112 /* Structure of the message */
113 struct mq_msg {
114         TAILQ_ENTRY(mq_msg)     msg_queue;
115         size_t                  msg_len;
116         u_int                   msg_prio;
117         int8_t                  msg_ptr[1];
118 };
119
120 /* Prototypes */
121 void    mqueue_sysinit(void);
122 void    mqueue_print_list(void (*pr)(const char *, ...));
123 int     abstimeout2timo(struct timespec *, int *);
124 int     mq_send1(struct lwp *, mqd_t, const char *, size_t, unsigned, struct timespec *);
125 int     mq_receive1(struct lwp *, mqd_t, void *, size_t, unsigned *, struct timespec *,
126     ssize_t *);
127
128 #endif  /* _KERNEL || _KERNEL_STRUCTURES */
129
130 #endif  /* _SYS_MQUEUE_H_ */
131