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