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