sys_mqueue.c revision 1.29 1 1.29 dsl /* $NetBSD: sys_mqueue.c,v 1.29 2009/12/20 09:36:05 dsl Exp $ */
2 1.1 rmind
3 1.1 rmind /*
4 1.22 rmind * Copyright (c) 2007-2009 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.22 rmind * mqlist_mtx ->
41 1.22 rmind * mqueue::mq_mtx
42 1.1 rmind */
43 1.1 rmind
44 1.1 rmind #include <sys/cdefs.h>
45 1.29 dsl __KERNEL_RCSID(0, "$NetBSD: sys_mqueue.c,v 1.29 2009/12/20 09:36:05 dsl 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.24 rmind #include <sys/module.h>
60 1.1 rmind #include <sys/mutex.h>
61 1.1 rmind #include <sys/pool.h>
62 1.8 rmind #include <sys/poll.h>
63 1.1 rmind #include <sys/proc.h>
64 1.1 rmind #include <sys/queue.h>
65 1.8 rmind #include <sys/select.h>
66 1.1 rmind #include <sys/signal.h>
67 1.1 rmind #include <sys/signalvar.h>
68 1.12 rmind #include <sys/stat.h>
69 1.1 rmind #include <sys/sysctl.h>
70 1.24 rmind #include <sys/syscall.h>
71 1.24 rmind #include <sys/syscallvar.h>
72 1.1 rmind #include <sys/syscallargs.h>
73 1.1 rmind #include <sys/systm.h>
74 1.1 rmind #include <sys/unistd.h>
75 1.1 rmind
76 1.19 elad #include <miscfs/genfs/genfs.h>
77 1.19 elad
78 1.24 rmind MODULE(MODULE_CLASS_MISC, mqueue, NULL);
79 1.24 rmind
80 1.1 rmind /* System-wide limits. */
81 1.1 rmind static u_int mq_open_max = MQ_OPEN_MAX;
82 1.1 rmind static u_int mq_prio_max = MQ_PRIO_MAX;
83 1.1 rmind static u_int mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
84 1.1 rmind static u_int mq_def_maxmsg = 32;
85 1.28 drochner static u_int mq_max_maxmsg = 16 * 32;
86 1.1 rmind
87 1.1 rmind static kmutex_t mqlist_mtx;
88 1.8 rmind static pool_cache_t mqmsg_cache;
89 1.22 rmind static LIST_HEAD(, mqueue) mqueue_head;
90 1.1 rmind
91 1.24 rmind static int mqueue_sysinit(void);
92 1.24 rmind static int mqueue_sysfini(bool);
93 1.8 rmind static int mq_poll_fop(file_t *, int);
94 1.15 christos static int mq_stat_fop(file_t *, struct stat *);
95 1.7 ad static int mq_close_fop(file_t *);
96 1.1 rmind
97 1.1 rmind static const struct fileops mqops = {
98 1.14 ad .fo_read = fbadop_read,
99 1.14 ad .fo_write = fbadop_write,
100 1.14 ad .fo_ioctl = fbadop_ioctl,
101 1.14 ad .fo_fcntl = fnullop_fcntl,
102 1.14 ad .fo_poll = mq_poll_fop,
103 1.15 christos .fo_stat = mq_stat_fop,
104 1.14 ad .fo_close = mq_close_fop,
105 1.14 ad .fo_kqfilter = fnullop_kqfilter,
106 1.29 dsl .fo_restart = fnullop_restart,
107 1.1 rmind };
108 1.1 rmind
109 1.24 rmind static const struct syscall_package mqueue_syscalls[] = {
110 1.24 rmind { SYS_mq_open, 0, (sy_call_t *)sys_mq_open },
111 1.24 rmind { SYS_mq_close, 0, (sy_call_t *)sys_mq_close },
112 1.24 rmind { SYS_mq_unlink, 0, (sy_call_t *)sys_mq_unlink },
113 1.24 rmind { SYS_mq_getattr, 0, (sy_call_t *)sys_mq_getattr },
114 1.24 rmind { SYS_mq_setattr, 0, (sy_call_t *)sys_mq_setattr },
115 1.24 rmind { SYS_mq_notify, 0, (sy_call_t *)sys_mq_notify },
116 1.24 rmind { SYS_mq_send, 0, (sy_call_t *)sys_mq_send },
117 1.24 rmind { SYS_mq_receive, 0, (sy_call_t *)sys_mq_receive },
118 1.24 rmind { SYS___mq_timedsend50, 0, (sy_call_t *)sys___mq_timedsend50 },
119 1.24 rmind { SYS___mq_timedreceive50, 0, (sy_call_t *)sys___mq_timedreceive50 },
120 1.24 rmind { 0, 0, NULL }
121 1.24 rmind };
122 1.24 rmind
123 1.1 rmind /*
124 1.24 rmind * Initialisation and unloading of POSIX message queue subsystem.
125 1.1 rmind */
126 1.24 rmind
127 1.24 rmind static int
128 1.1 rmind mqueue_sysinit(void)
129 1.1 rmind {
130 1.24 rmind int error;
131 1.1 rmind
132 1.9 ad mqmsg_cache = pool_cache_init(MQ_DEF_MSGSIZE, coherency_unit,
133 1.12 rmind 0, 0, "mqmsgpl", NULL, IPL_NONE, NULL, NULL, NULL);
134 1.1 rmind mutex_init(&mqlist_mtx, MUTEX_DEFAULT, IPL_NONE);
135 1.22 rmind LIST_INIT(&mqueue_head);
136 1.24 rmind
137 1.24 rmind error = syscall_establish(NULL, mqueue_syscalls);
138 1.24 rmind if (error) {
139 1.24 rmind (void)mqueue_sysfini(false);
140 1.24 rmind }
141 1.24 rmind return error;
142 1.24 rmind }
143 1.24 rmind
144 1.24 rmind static int
145 1.24 rmind mqueue_sysfini(bool interface)
146 1.24 rmind {
147 1.24 rmind
148 1.24 rmind if (interface) {
149 1.24 rmind int error;
150 1.24 rmind bool inuse;
151 1.24 rmind
152 1.24 rmind /* Stop syscall activity. */
153 1.24 rmind error = syscall_disestablish(NULL, mqueue_syscalls);
154 1.24 rmind if (error)
155 1.24 rmind return error;
156 1.24 rmind /*
157 1.24 rmind * Check if there are any message queues in use.
158 1.24 rmind * TODO: We shall support forced unload.
159 1.24 rmind */
160 1.24 rmind mutex_enter(&mqlist_mtx);
161 1.24 rmind inuse = !LIST_EMPTY(&mqueue_head);
162 1.24 rmind mutex_exit(&mqlist_mtx);
163 1.24 rmind if (inuse) {
164 1.24 rmind error = syscall_establish(NULL, mqueue_syscalls);
165 1.24 rmind KASSERT(error == 0);
166 1.24 rmind return EBUSY;
167 1.24 rmind }
168 1.24 rmind }
169 1.24 rmind mutex_destroy(&mqlist_mtx);
170 1.24 rmind pool_cache_destroy(mqmsg_cache);
171 1.24 rmind return 0;
172 1.24 rmind }
173 1.24 rmind
174 1.24 rmind /*
175 1.24 rmind * Module interface.
176 1.24 rmind */
177 1.24 rmind static int
178 1.24 rmind mqueue_modcmd(modcmd_t cmd, void *arg)
179 1.24 rmind {
180 1.24 rmind
181 1.24 rmind switch (cmd) {
182 1.24 rmind case MODULE_CMD_INIT:
183 1.24 rmind return mqueue_sysinit();
184 1.24 rmind case MODULE_CMD_FINI:
185 1.24 rmind return mqueue_sysfini(true);
186 1.24 rmind default:
187 1.24 rmind return ENOTTY;
188 1.24 rmind }
189 1.1 rmind }
190 1.1 rmind
191 1.1 rmind /*
192 1.1 rmind * Free the message.
193 1.1 rmind */
194 1.1 rmind static void
195 1.1 rmind mqueue_freemsg(struct mq_msg *msg, const size_t size)
196 1.1 rmind {
197 1.1 rmind
198 1.22 rmind if (size > MQ_DEF_MSGSIZE) {
199 1.1 rmind kmem_free(msg, size);
200 1.22 rmind } else {
201 1.8 rmind pool_cache_put(mqmsg_cache, msg);
202 1.22 rmind }
203 1.1 rmind }
204 1.1 rmind
205 1.1 rmind /*
206 1.1 rmind * Destroy the message queue.
207 1.1 rmind */
208 1.1 rmind static void
209 1.1 rmind mqueue_destroy(struct mqueue *mq)
210 1.1 rmind {
211 1.1 rmind struct mq_msg *msg;
212 1.22 rmind size_t msz;
213 1.22 rmind u_int i;
214 1.1 rmind
215 1.23 rmind /* Note MQ_PQSIZE + 1. */
216 1.23 rmind for (i = 0; i <= MQ_PQSIZE; i++) {
217 1.22 rmind while ((msg = TAILQ_FIRST(&mq->mq_head[i])) != NULL) {
218 1.22 rmind TAILQ_REMOVE(&mq->mq_head[i], msg, msg_queue);
219 1.22 rmind msz = sizeof(struct mq_msg) + msg->msg_len;
220 1.22 rmind mqueue_freemsg(msg, msz);
221 1.22 rmind }
222 1.1 rmind }
223 1.8 rmind seldestroy(&mq->mq_rsel);
224 1.8 rmind seldestroy(&mq->mq_wsel);
225 1.1 rmind cv_destroy(&mq->mq_send_cv);
226 1.1 rmind cv_destroy(&mq->mq_recv_cv);
227 1.1 rmind mutex_destroy(&mq->mq_mtx);
228 1.1 rmind kmem_free(mq, sizeof(struct mqueue));
229 1.1 rmind }
230 1.1 rmind
231 1.1 rmind /*
232 1.1 rmind * Lookup for file name in general list of message queues.
233 1.1 rmind * => locks the message queue
234 1.1 rmind */
235 1.1 rmind static void *
236 1.1 rmind mqueue_lookup(char *name)
237 1.1 rmind {
238 1.1 rmind struct mqueue *mq;
239 1.1 rmind KASSERT(mutex_owned(&mqlist_mtx));
240 1.1 rmind
241 1.1 rmind LIST_FOREACH(mq, &mqueue_head, mq_list) {
242 1.1 rmind if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
243 1.1 rmind mutex_enter(&mq->mq_mtx);
244 1.1 rmind return mq;
245 1.1 rmind }
246 1.1 rmind }
247 1.1 rmind
248 1.1 rmind return NULL;
249 1.1 rmind }
250 1.1 rmind
251 1.1 rmind /*
252 1.18 rmind * mqueue_get: get the mqueue from the descriptor.
253 1.18 rmind * => locks the message queue, if found.
254 1.18 rmind * => holds a reference on the file descriptor.
255 1.1 rmind */
256 1.1 rmind static int
257 1.18 rmind mqueue_get(mqd_t mqd, file_t **fpr)
258 1.1 rmind {
259 1.18 rmind struct mqueue *mq;
260 1.7 ad file_t *fp;
261 1.1 rmind
262 1.7 ad fp = fd_getfile((int)mqd);
263 1.18 rmind if (__predict_false(fp == NULL)) {
264 1.18 rmind return EBADF;
265 1.18 rmind }
266 1.18 rmind if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
267 1.18 rmind fd_putfile((int)mqd);
268 1.1 rmind return EBADF;
269 1.18 rmind }
270 1.1 rmind mq = fp->f_data;
271 1.1 rmind mutex_enter(&mq->mq_mtx);
272 1.1 rmind
273 1.18 rmind *fpr = fp;
274 1.1 rmind return 0;
275 1.1 rmind }
276 1.1 rmind
277 1.1 rmind /*
278 1.22 rmind * mqueue_linear_insert: perform linear insert according to the message
279 1.23 rmind * priority into the reserved queue (MQ_PQRESQ). Reserved queue is a
280 1.23 rmind * sorted list used only when mq_prio_max is increased via sysctl.
281 1.22 rmind */
282 1.22 rmind static inline void
283 1.22 rmind mqueue_linear_insert(struct mqueue *mq, struct mq_msg *msg)
284 1.22 rmind {
285 1.22 rmind struct mq_msg *mit;
286 1.22 rmind
287 1.23 rmind TAILQ_FOREACH(mit, &mq->mq_head[MQ_PQRESQ], msg_queue) {
288 1.22 rmind if (msg->msg_prio > mit->msg_prio)
289 1.22 rmind break;
290 1.22 rmind }
291 1.22 rmind if (mit == NULL) {
292 1.23 rmind TAILQ_INSERT_TAIL(&mq->mq_head[MQ_PQRESQ], msg, msg_queue);
293 1.22 rmind } else {
294 1.22 rmind TAILQ_INSERT_BEFORE(mit, msg, msg_queue);
295 1.22 rmind }
296 1.22 rmind }
297 1.22 rmind
298 1.1 rmind static int
299 1.15 christos mq_stat_fop(file_t *fp, struct stat *st)
300 1.15 christos {
301 1.15 christos struct mqueue *mq = fp->f_data;
302 1.15 christos
303 1.22 rmind memset(st, 0, sizeof(*st));
304 1.16 christos
305 1.16 christos mutex_enter(&mq->mq_mtx);
306 1.15 christos st->st_mode = mq->mq_mode;
307 1.15 christos st->st_uid = mq->mq_euid;
308 1.15 christos st->st_gid = mq->mq_egid;
309 1.15 christos st->st_atimespec = mq->mq_atime;
310 1.15 christos st->st_mtimespec = mq->mq_mtime;
311 1.15 christos st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;
312 1.16 christos st->st_uid = kauth_cred_geteuid(fp->f_cred);
313 1.16 christos st->st_gid = kauth_cred_getegid(fp->f_cred);
314 1.16 christos mutex_exit(&mq->mq_mtx);
315 1.16 christos
316 1.15 christos return 0;
317 1.15 christos }
318 1.15 christos
319 1.15 christos static int
320 1.8 rmind mq_poll_fop(file_t *fp, int events)
321 1.8 rmind {
322 1.8 rmind struct mqueue *mq = fp->f_data;
323 1.22 rmind struct mq_attr *mqattr;
324 1.8 rmind int revents = 0;
325 1.8 rmind
326 1.8 rmind mutex_enter(&mq->mq_mtx);
327 1.22 rmind mqattr = &mq->mq_attrib;
328 1.8 rmind if (events & (POLLIN | POLLRDNORM)) {
329 1.8 rmind /* Ready for receiving, if there are messages in the queue */
330 1.22 rmind if (mqattr->mq_curmsgs)
331 1.8 rmind revents |= (POLLIN | POLLRDNORM);
332 1.8 rmind else
333 1.8 rmind selrecord(curlwp, &mq->mq_rsel);
334 1.8 rmind }
335 1.8 rmind if (events & (POLLOUT | POLLWRNORM)) {
336 1.8 rmind /* Ready for sending, if the message queue is not full */
337 1.22 rmind if (mqattr->mq_curmsgs < mqattr->mq_maxmsg)
338 1.8 rmind revents |= (POLLOUT | POLLWRNORM);
339 1.8 rmind else
340 1.8 rmind selrecord(curlwp, &mq->mq_wsel);
341 1.8 rmind }
342 1.8 rmind mutex_exit(&mq->mq_mtx);
343 1.8 rmind
344 1.8 rmind return revents;
345 1.8 rmind }
346 1.8 rmind
347 1.8 rmind static int
348 1.7 ad mq_close_fop(file_t *fp)
349 1.1 rmind {
350 1.7 ad struct proc *p = curproc;
351 1.1 rmind struct mqueue *mq = fp->f_data;
352 1.1 rmind bool destroy;
353 1.1 rmind
354 1.1 rmind mutex_enter(&mqlist_mtx);
355 1.1 rmind mutex_enter(&mq->mq_mtx);
356 1.1 rmind
357 1.1 rmind /* Decrease the counters */
358 1.1 rmind p->p_mqueue_cnt--;
359 1.1 rmind mq->mq_refcnt--;
360 1.1 rmind
361 1.1 rmind /* Remove notification if registered for this process */
362 1.1 rmind if (mq->mq_notify_proc == p)
363 1.1 rmind mq->mq_notify_proc = NULL;
364 1.1 rmind
365 1.1 rmind /*
366 1.1 rmind * If this is the last reference and mqueue is marked for unlink,
367 1.1 rmind * remove and later destroy the message queue.
368 1.1 rmind */
369 1.1 rmind if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
370 1.1 rmind LIST_REMOVE(mq, mq_list);
371 1.1 rmind destroy = true;
372 1.1 rmind } else
373 1.1 rmind destroy = false;
374 1.1 rmind
375 1.1 rmind mutex_exit(&mq->mq_mtx);
376 1.1 rmind mutex_exit(&mqlist_mtx);
377 1.1 rmind
378 1.1 rmind if (destroy)
379 1.1 rmind mqueue_destroy(mq);
380 1.1 rmind
381 1.1 rmind return 0;
382 1.1 rmind }
383 1.1 rmind
384 1.20 elad static int
385 1.20 elad mqueue_access(struct mqueue *mq, mode_t mode, kauth_cred_t cred)
386 1.20 elad {
387 1.22 rmind
388 1.20 elad if (genfs_can_access(VNON, mq->mq_mode, mq->mq_euid,
389 1.20 elad mq->mq_egid, mode, cred)) {
390 1.20 elad return EACCES;
391 1.20 elad }
392 1.20 elad
393 1.20 elad return 0;
394 1.20 elad }
395 1.20 elad
396 1.1 rmind /*
397 1.1 rmind * General mqueue system calls.
398 1.1 rmind */
399 1.1 rmind
400 1.1 rmind int
401 1.8 rmind sys_mq_open(struct lwp *l, const struct sys_mq_open_args *uap,
402 1.8 rmind register_t *retval)
403 1.1 rmind {
404 1.6 dsl /* {
405 1.1 rmind syscallarg(const char *) name;
406 1.1 rmind syscallarg(int) oflag;
407 1.1 rmind syscallarg(mode_t) mode;
408 1.1 rmind syscallarg(struct mq_attr) attr;
409 1.6 dsl } */
410 1.1 rmind struct proc *p = l->l_proc;
411 1.1 rmind struct mqueue *mq, *mq_new = NULL;
412 1.7 ad file_t *fp;
413 1.1 rmind char *name;
414 1.1 rmind int mqd, error, oflag;
415 1.1 rmind
416 1.1 rmind oflag = SCARG(uap, oflag);
417 1.1 rmind
418 1.1 rmind /* Get the name from the user-space */
419 1.1 rmind name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
420 1.1 rmind error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
421 1.1 rmind if (error) {
422 1.1 rmind kmem_free(name, MQ_NAMELEN);
423 1.1 rmind return error;
424 1.1 rmind }
425 1.1 rmind
426 1.1 rmind if (oflag & O_CREAT) {
427 1.12 rmind struct cwdinfo *cwdi = p->p_cwdi;
428 1.1 rmind struct mq_attr attr;
429 1.22 rmind u_int i;
430 1.1 rmind
431 1.1 rmind /* Check the limit */
432 1.1 rmind if (p->p_mqueue_cnt == mq_open_max) {
433 1.1 rmind kmem_free(name, MQ_NAMELEN);
434 1.1 rmind return EMFILE;
435 1.1 rmind }
436 1.1 rmind
437 1.18 rmind /* Empty name is invalid */
438 1.18 rmind if (name[0] == '\0') {
439 1.18 rmind kmem_free(name, MQ_NAMELEN);
440 1.18 rmind return EINVAL;
441 1.18 rmind }
442 1.21 rmind
443 1.1 rmind /* Check for mqueue attributes */
444 1.1 rmind if (SCARG(uap, attr)) {
445 1.1 rmind error = copyin(SCARG(uap, attr), &attr,
446 1.22 rmind sizeof(struct mq_attr));
447 1.1 rmind if (error) {
448 1.1 rmind kmem_free(name, MQ_NAMELEN);
449 1.1 rmind return error;
450 1.1 rmind }
451 1.28 drochner if (attr.mq_maxmsg <= 0 ||
452 1.28 drochner attr.mq_maxmsg > mq_max_maxmsg ||
453 1.28 drochner attr.mq_msgsize <= 0 ||
454 1.1 rmind attr.mq_msgsize > mq_max_msgsize) {
455 1.1 rmind kmem_free(name, MQ_NAMELEN);
456 1.1 rmind return EINVAL;
457 1.1 rmind }
458 1.1 rmind attr.mq_curmsgs = 0;
459 1.1 rmind } else {
460 1.1 rmind memset(&attr, 0, sizeof(struct mq_attr));
461 1.1 rmind attr.mq_maxmsg = mq_def_maxmsg;
462 1.1 rmind attr.mq_msgsize =
463 1.1 rmind MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
464 1.1 rmind }
465 1.1 rmind
466 1.1 rmind /*
467 1.1 rmind * Allocate new mqueue, initialize data structures,
468 1.1 rmind * copy the name, attributes and set the flag.
469 1.1 rmind */
470 1.1 rmind mq_new = kmem_zalloc(sizeof(struct mqueue), KM_SLEEP);
471 1.1 rmind
472 1.1 rmind mutex_init(&mq_new->mq_mtx, MUTEX_DEFAULT, IPL_NONE);
473 1.1 rmind cv_init(&mq_new->mq_send_cv, "mqsendcv");
474 1.1 rmind cv_init(&mq_new->mq_recv_cv, "mqrecvcv");
475 1.22 rmind for (i = 0; i < (MQ_PQSIZE + 1); i++) {
476 1.22 rmind TAILQ_INIT(&mq_new->mq_head[i]);
477 1.22 rmind }
478 1.8 rmind selinit(&mq_new->mq_rsel);
479 1.8 rmind selinit(&mq_new->mq_wsel);
480 1.1 rmind
481 1.1 rmind strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
482 1.1 rmind memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
483 1.18 rmind
484 1.18 rmind CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);
485 1.18 rmind mq_new->mq_attrib.mq_flags = (O_MASK & oflag);
486 1.1 rmind
487 1.1 rmind /* Store mode and effective UID with GID */
488 1.12 rmind mq_new->mq_mode = ((SCARG(uap, mode) &
489 1.12 rmind ~cwdi->cwdi_cmask) & ALLPERMS) & ~S_ISTXT;
490 1.1 rmind mq_new->mq_euid = kauth_cred_geteuid(l->l_cred);
491 1.1 rmind mq_new->mq_egid = kauth_cred_getegid(l->l_cred);
492 1.1 rmind }
493 1.1 rmind
494 1.1 rmind /* Allocate file structure and descriptor */
495 1.7 ad error = fd_allocfile(&fp, &mqd);
496 1.1 rmind if (error) {
497 1.1 rmind if (mq_new)
498 1.1 rmind mqueue_destroy(mq_new);
499 1.1 rmind kmem_free(name, MQ_NAMELEN);
500 1.1 rmind return error;
501 1.1 rmind }
502 1.1 rmind fp->f_type = DTYPE_MQUEUE;
503 1.11 rmind fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
504 1.1 rmind fp->f_ops = &mqops;
505 1.1 rmind
506 1.1 rmind /* Look up for mqueue with such name */
507 1.1 rmind mutex_enter(&mqlist_mtx);
508 1.1 rmind mq = mqueue_lookup(name);
509 1.1 rmind if (mq) {
510 1.18 rmind mode_t acc_mode;
511 1.18 rmind
512 1.12 rmind KASSERT(mutex_owned(&mq->mq_mtx));
513 1.11 rmind
514 1.1 rmind /* Check if mqueue is not marked as unlinking */
515 1.1 rmind if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
516 1.1 rmind error = EACCES;
517 1.1 rmind goto exit;
518 1.1 rmind }
519 1.22 rmind
520 1.1 rmind /* Fail if O_EXCL is set, and mqueue already exists */
521 1.1 rmind if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
522 1.1 rmind error = EEXIST;
523 1.1 rmind goto exit;
524 1.1 rmind }
525 1.18 rmind
526 1.18 rmind /*
527 1.18 rmind * Check the permissions. Note the difference between
528 1.18 rmind * VREAD/VWRITE and FREAD/FWRITE.
529 1.18 rmind */
530 1.18 rmind acc_mode = 0;
531 1.18 rmind if (fp->f_flag & FREAD) {
532 1.18 rmind acc_mode |= VREAD;
533 1.18 rmind }
534 1.18 rmind if (fp->f_flag & FWRITE) {
535 1.18 rmind acc_mode |= VWRITE;
536 1.18 rmind }
537 1.20 elad if (mqueue_access(mq, acc_mode, l->l_cred) != 0) {
538 1.1 rmind error = EACCES;
539 1.1 rmind goto exit;
540 1.1 rmind }
541 1.1 rmind } else {
542 1.1 rmind /* Fail if mqueue neither exists, nor we create it */
543 1.1 rmind if ((oflag & O_CREAT) == 0) {
544 1.1 rmind mutex_exit(&mqlist_mtx);
545 1.1 rmind KASSERT(mq_new == NULL);
546 1.7 ad fd_abort(p, fp, mqd);
547 1.1 rmind kmem_free(name, MQ_NAMELEN);
548 1.1 rmind return ENOENT;
549 1.1 rmind }
550 1.1 rmind
551 1.1 rmind /* Check the limit */
552 1.1 rmind if (p->p_mqueue_cnt == mq_open_max) {
553 1.1 rmind error = EMFILE;
554 1.1 rmind goto exit;
555 1.1 rmind }
556 1.1 rmind
557 1.1 rmind /* Insert the queue to the list */
558 1.1 rmind mq = mq_new;
559 1.1 rmind mutex_enter(&mq->mq_mtx);
560 1.1 rmind LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
561 1.1 rmind mq_new = NULL;
562 1.15 christos getnanotime(&mq->mq_btime);
563 1.15 christos mq->mq_atime = mq->mq_mtime = mq->mq_btime;
564 1.1 rmind }
565 1.1 rmind
566 1.1 rmind /* Increase the counters, and make descriptor ready */
567 1.1 rmind p->p_mqueue_cnt++;
568 1.1 rmind mq->mq_refcnt++;
569 1.1 rmind fp->f_data = mq;
570 1.1 rmind exit:
571 1.1 rmind mutex_exit(&mq->mq_mtx);
572 1.1 rmind mutex_exit(&mqlist_mtx);
573 1.1 rmind
574 1.1 rmind if (mq_new)
575 1.1 rmind mqueue_destroy(mq_new);
576 1.1 rmind if (error) {
577 1.7 ad fd_abort(p, fp, mqd);
578 1.7 ad } else {
579 1.7 ad fd_affix(p, fp, mqd);
580 1.1 rmind *retval = mqd;
581 1.7 ad }
582 1.1 rmind kmem_free(name, MQ_NAMELEN);
583 1.1 rmind
584 1.1 rmind return error;
585 1.1 rmind }
586 1.1 rmind
587 1.1 rmind int
588 1.8 rmind sys_mq_close(struct lwp *l, const struct sys_mq_close_args *uap,
589 1.8 rmind register_t *retval)
590 1.1 rmind {
591 1.1 rmind
592 1.6 dsl return sys_close(l, (const void *)uap, retval);
593 1.1 rmind }
594 1.1 rmind
595 1.1 rmind /*
596 1.25 rmind * Primary mq_recv1() function.
597 1.1 rmind */
598 1.13 christos int
599 1.25 rmind mq_recv1(mqd_t mqdes, void *msg_ptr, size_t msg_len, u_int *msg_prio,
600 1.25 rmind struct timespec *ts, ssize_t *mlen)
601 1.1 rmind {
602 1.7 ad file_t *fp = NULL;
603 1.1 rmind struct mqueue *mq;
604 1.1 rmind struct mq_msg *msg = NULL;
605 1.22 rmind struct mq_attr *mqattr;
606 1.23 rmind u_int idx;
607 1.1 rmind int error;
608 1.1 rmind
609 1.1 rmind /* Get the message queue */
610 1.18 rmind error = mqueue_get(mqdes, &fp);
611 1.21 rmind if (error) {
612 1.1 rmind return error;
613 1.21 rmind }
614 1.1 rmind mq = fp->f_data;
615 1.21 rmind if ((fp->f_flag & FREAD) == 0) {
616 1.21 rmind error = EBADF;
617 1.21 rmind goto error;
618 1.21 rmind }
619 1.21 rmind getnanotime(&mq->mq_atime);
620 1.22 rmind mqattr = &mq->mq_attrib;
621 1.1 rmind
622 1.1 rmind /* Check the message size limits */
623 1.22 rmind if (msg_len < mqattr->mq_msgsize) {
624 1.1 rmind error = EMSGSIZE;
625 1.1 rmind goto error;
626 1.1 rmind }
627 1.1 rmind
628 1.1 rmind /* Check if queue is empty */
629 1.22 rmind while (mqattr->mq_curmsgs == 0) {
630 1.25 rmind int t;
631 1.25 rmind
632 1.22 rmind if (mqattr->mq_flags & O_NONBLOCK) {
633 1.1 rmind error = EAGAIN;
634 1.1 rmind goto error;
635 1.1 rmind }
636 1.28 drochner if (ts) {
637 1.28 drochner error = abstimeout2timo(ts, &t);
638 1.28 drochner if (error)
639 1.28 drochner goto error;
640 1.28 drochner } else
641 1.28 drochner t = 0;
642 1.1 rmind /*
643 1.1 rmind * Block until someone sends the message.
644 1.1 rmind * While doing this, notification should not be sent.
645 1.1 rmind */
646 1.22 rmind mqattr->mq_flags |= MQ_RECEIVE;
647 1.1 rmind error = cv_timedwait_sig(&mq->mq_send_cv, &mq->mq_mtx, t);
648 1.22 rmind mqattr->mq_flags &= ~MQ_RECEIVE;
649 1.22 rmind if (error || (mqattr->mq_flags & MQ_UNLINK)) {
650 1.3 rmind error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
651 1.1 rmind goto error;
652 1.1 rmind }
653 1.1 rmind }
654 1.1 rmind
655 1.23 rmind /*
656 1.23 rmind * Find the highest priority message, and remove it from the queue.
657 1.23 rmind * At first, reserved queue is checked, bitmap is next.
658 1.23 rmind */
659 1.23 rmind msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
660 1.23 rmind if (__predict_true(msg == NULL)) {
661 1.23 rmind idx = ffs(mq->mq_bitmap);
662 1.23 rmind msg = TAILQ_FIRST(&mq->mq_head[idx]);
663 1.23 rmind KASSERT(msg != NULL);
664 1.23 rmind } else {
665 1.23 rmind idx = MQ_PQRESQ;
666 1.23 rmind }
667 1.23 rmind TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
668 1.23 rmind
669 1.23 rmind /* Unmark the bit, if last message. */
670 1.23 rmind if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
671 1.23 rmind KASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
672 1.23 rmind mq->mq_bitmap &= ~(1 << --idx);
673 1.22 rmind }
674 1.1 rmind
675 1.1 rmind /* Decrement the counter and signal waiter, if any */
676 1.22 rmind mqattr->mq_curmsgs--;
677 1.1 rmind cv_signal(&mq->mq_recv_cv);
678 1.8 rmind
679 1.8 rmind /* Ready for sending now */
680 1.8 rmind selnotify(&mq->mq_wsel, POLLOUT | POLLWRNORM, 0);
681 1.1 rmind error:
682 1.1 rmind mutex_exit(&mq->mq_mtx);
683 1.7 ad fd_putfile((int)mqdes);
684 1.1 rmind if (error)
685 1.1 rmind return error;
686 1.1 rmind
687 1.1 rmind /*
688 1.1 rmind * Copy the data to the user-space.
689 1.1 rmind * Note: According to POSIX, no message should be removed from the
690 1.1 rmind * queue in case of fail - this would be violated.
691 1.1 rmind */
692 1.1 rmind *mlen = msg->msg_len;
693 1.1 rmind error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
694 1.1 rmind if (error == 0 && msg_prio)
695 1.1 rmind error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
696 1.1 rmind mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
697 1.1 rmind
698 1.1 rmind return error;
699 1.1 rmind }
700 1.1 rmind
701 1.1 rmind int
702 1.8 rmind sys_mq_receive(struct lwp *l, const struct sys_mq_receive_args *uap,
703 1.8 rmind register_t *retval)
704 1.1 rmind {
705 1.6 dsl /* {
706 1.1 rmind syscallarg(mqd_t) mqdes;
707 1.1 rmind syscallarg(char *) msg_ptr;
708 1.1 rmind syscallarg(size_t) msg_len;
709 1.1 rmind syscallarg(unsigned *) msg_prio;
710 1.6 dsl } */
711 1.25 rmind ssize_t mlen;
712 1.1 rmind int error;
713 1.1 rmind
714 1.25 rmind error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
715 1.25 rmind SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL, &mlen);
716 1.1 rmind if (error == 0)
717 1.1 rmind *retval = mlen;
718 1.1 rmind
719 1.1 rmind return error;
720 1.1 rmind }
721 1.1 rmind
722 1.1 rmind int
723 1.13 christos sys___mq_timedreceive50(struct lwp *l,
724 1.13 christos const struct sys___mq_timedreceive50_args *uap, register_t *retval)
725 1.1 rmind {
726 1.6 dsl /* {
727 1.1 rmind syscallarg(mqd_t) mqdes;
728 1.1 rmind syscallarg(char *) msg_ptr;
729 1.1 rmind syscallarg(size_t) msg_len;
730 1.1 rmind syscallarg(unsigned *) msg_prio;
731 1.1 rmind syscallarg(const struct timespec *) abs_timeout;
732 1.6 dsl } */
733 1.25 rmind struct timespec ts, *tsp;
734 1.1 rmind ssize_t mlen;
735 1.25 rmind int error;
736 1.1 rmind
737 1.1 rmind /* Get and convert time value */
738 1.1 rmind if (SCARG(uap, abs_timeout)) {
739 1.13 christos error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
740 1.13 christos if (error)
741 1.13 christos return error;
742 1.25 rmind tsp = &ts;
743 1.25 rmind } else {
744 1.25 rmind tsp = NULL;
745 1.25 rmind }
746 1.13 christos
747 1.25 rmind error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
748 1.25 rmind SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
749 1.1 rmind if (error == 0)
750 1.1 rmind *retval = mlen;
751 1.1 rmind
752 1.1 rmind return error;
753 1.1 rmind }
754 1.1 rmind
755 1.1 rmind /*
756 1.1 rmind * Primary mq_send1() function.
757 1.1 rmind */
758 1.13 christos int
759 1.25 rmind mq_send1(mqd_t mqdes, const char *msg_ptr, size_t msg_len, u_int msg_prio,
760 1.25 rmind struct timespec *ts)
761 1.1 rmind {
762 1.7 ad file_t *fp = NULL;
763 1.1 rmind struct mqueue *mq;
764 1.22 rmind struct mq_msg *msg;
765 1.22 rmind struct mq_attr *mqattr;
766 1.1 rmind struct proc *notify = NULL;
767 1.1 rmind ksiginfo_t ksi;
768 1.1 rmind size_t size;
769 1.1 rmind int error;
770 1.1 rmind
771 1.1 rmind /* Check the priority range */
772 1.1 rmind if (msg_prio >= mq_prio_max)
773 1.1 rmind return EINVAL;
774 1.1 rmind
775 1.1 rmind /* Allocate a new message */
776 1.1 rmind size = sizeof(struct mq_msg) + msg_len;
777 1.1 rmind if (size > mq_max_msgsize)
778 1.1 rmind return EMSGSIZE;
779 1.1 rmind
780 1.22 rmind if (size > MQ_DEF_MSGSIZE) {
781 1.1 rmind msg = kmem_alloc(size, KM_SLEEP);
782 1.22 rmind } else {
783 1.8 rmind msg = pool_cache_get(mqmsg_cache, PR_WAITOK);
784 1.22 rmind }
785 1.1 rmind
786 1.1 rmind /* Get the data from user-space */
787 1.1 rmind error = copyin(msg_ptr, msg->msg_ptr, msg_len);
788 1.1 rmind if (error) {
789 1.1 rmind mqueue_freemsg(msg, size);
790 1.1 rmind return error;
791 1.1 rmind }
792 1.1 rmind msg->msg_len = msg_len;
793 1.1 rmind msg->msg_prio = msg_prio;
794 1.1 rmind
795 1.1 rmind /* Get the mqueue */
796 1.18 rmind error = mqueue_get(mqdes, &fp);
797 1.1 rmind if (error) {
798 1.1 rmind mqueue_freemsg(msg, size);
799 1.1 rmind return error;
800 1.1 rmind }
801 1.1 rmind mq = fp->f_data;
802 1.21 rmind if ((fp->f_flag & FWRITE) == 0) {
803 1.21 rmind error = EBADF;
804 1.21 rmind goto error;
805 1.21 rmind }
806 1.15 christos getnanotime(&mq->mq_mtime);
807 1.22 rmind mqattr = &mq->mq_attrib;
808 1.15 christos
809 1.1 rmind /* Check the message size limit */
810 1.22 rmind if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
811 1.1 rmind error = EMSGSIZE;
812 1.1 rmind goto error;
813 1.1 rmind }
814 1.1 rmind
815 1.1 rmind /* Check if queue is full */
816 1.22 rmind while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
817 1.25 rmind int t;
818 1.25 rmind
819 1.22 rmind if (mqattr->mq_flags & O_NONBLOCK) {
820 1.1 rmind error = EAGAIN;
821 1.1 rmind goto error;
822 1.1 rmind }
823 1.28 drochner if (ts) {
824 1.28 drochner error = abstimeout2timo(ts, &t);
825 1.28 drochner if (error)
826 1.28 drochner goto error;
827 1.28 drochner } else
828 1.28 drochner t = 0;
829 1.1 rmind /* Block until queue becomes available */
830 1.1 rmind error = cv_timedwait_sig(&mq->mq_recv_cv, &mq->mq_mtx, t);
831 1.22 rmind if (error || (mqattr->mq_flags & MQ_UNLINK)) {
832 1.1 rmind error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
833 1.1 rmind goto error;
834 1.1 rmind }
835 1.1 rmind }
836 1.22 rmind KASSERT(mqattr->mq_curmsgs < mqattr->mq_maxmsg);
837 1.1 rmind
838 1.23 rmind /*
839 1.23 rmind * Insert message into the queue, according to the priority.
840 1.23 rmind * Note the difference between index and priority.
841 1.23 rmind */
842 1.22 rmind if (__predict_true(msg_prio < MQ_PQSIZE)) {
843 1.23 rmind u_int idx = MQ_PQSIZE - msg_prio;
844 1.23 rmind
845 1.23 rmind KASSERT(idx != MQ_PQRESQ);
846 1.23 rmind TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
847 1.23 rmind mq->mq_bitmap |= (1 << --idx);
848 1.22 rmind } else {
849 1.22 rmind mqueue_linear_insert(mq, msg);
850 1.22 rmind }
851 1.1 rmind
852 1.1 rmind /* Check for the notify */
853 1.22 rmind if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
854 1.28 drochner (mqattr->mq_flags & MQ_RECEIVE) == 0 &&
855 1.28 drochner mq->mq_sig_notify.sigev_notify == SIGEV_SIGNAL) {
856 1.1 rmind /* Initialize the signal */
857 1.1 rmind KSI_INIT(&ksi);
858 1.1 rmind ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;
859 1.1 rmind ksi.ksi_code = SI_MESGQ;
860 1.1 rmind ksi.ksi_value = mq->mq_sig_notify.sigev_value;
861 1.1 rmind /* Unregister the process */
862 1.1 rmind notify = mq->mq_notify_proc;
863 1.1 rmind mq->mq_notify_proc = NULL;
864 1.1 rmind }
865 1.1 rmind
866 1.1 rmind /* Increment the counter and signal waiter, if any */
867 1.22 rmind mqattr->mq_curmsgs++;
868 1.1 rmind cv_signal(&mq->mq_send_cv);
869 1.8 rmind
870 1.8 rmind /* Ready for receiving now */
871 1.8 rmind selnotify(&mq->mq_rsel, POLLIN | POLLRDNORM, 0);
872 1.1 rmind error:
873 1.1 rmind mutex_exit(&mq->mq_mtx);
874 1.7 ad fd_putfile((int)mqdes);
875 1.1 rmind
876 1.1 rmind if (error) {
877 1.1 rmind mqueue_freemsg(msg, size);
878 1.1 rmind } else if (notify) {
879 1.1 rmind /* Send the notify, if needed */
880 1.10 ad mutex_enter(proc_lock);
881 1.1 rmind kpsignal(notify, &ksi, NULL);
882 1.10 ad mutex_exit(proc_lock);
883 1.1 rmind }
884 1.1 rmind return error;
885 1.1 rmind }
886 1.1 rmind
887 1.1 rmind int
888 1.8 rmind sys_mq_send(struct lwp *l, const struct sys_mq_send_args *uap,
889 1.8 rmind register_t *retval)
890 1.1 rmind {
891 1.6 dsl /* {
892 1.1 rmind syscallarg(mqd_t) mqdes;
893 1.1 rmind syscallarg(const char *) msg_ptr;
894 1.1 rmind syscallarg(size_t) msg_len;
895 1.1 rmind syscallarg(unsigned) msg_prio;
896 1.6 dsl } */
897 1.1 rmind
898 1.25 rmind return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
899 1.25 rmind SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL);
900 1.1 rmind }
901 1.1 rmind
902 1.1 rmind int
903 1.13 christos sys___mq_timedsend50(struct lwp *l, const struct sys___mq_timedsend50_args *uap,
904 1.8 rmind register_t *retval)
905 1.1 rmind {
906 1.6 dsl /* {
907 1.1 rmind syscallarg(mqd_t) mqdes;
908 1.1 rmind syscallarg(const char *) msg_ptr;
909 1.1 rmind syscallarg(size_t) msg_len;
910 1.1 rmind syscallarg(unsigned) msg_prio;
911 1.1 rmind syscallarg(const struct timespec *) abs_timeout;
912 1.6 dsl } */
913 1.25 rmind struct timespec ts, *tsp;
914 1.13 christos int error;
915 1.1 rmind
916 1.1 rmind /* Get and convert time value */
917 1.1 rmind if (SCARG(uap, abs_timeout)) {
918 1.13 christos error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
919 1.13 christos if (error)
920 1.13 christos return error;
921 1.25 rmind tsp = &ts;
922 1.25 rmind } else {
923 1.25 rmind tsp = NULL;
924 1.25 rmind }
925 1.1 rmind
926 1.25 rmind return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
927 1.25 rmind SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
928 1.1 rmind }
929 1.1 rmind
930 1.1 rmind int
931 1.8 rmind sys_mq_notify(struct lwp *l, const struct sys_mq_notify_args *uap,
932 1.8 rmind register_t *retval)
933 1.1 rmind {
934 1.6 dsl /* {
935 1.1 rmind syscallarg(mqd_t) mqdes;
936 1.1 rmind syscallarg(const struct sigevent *) notification;
937 1.6 dsl } */
938 1.7 ad file_t *fp = NULL;
939 1.1 rmind struct mqueue *mq;
940 1.1 rmind struct sigevent sig;
941 1.1 rmind int error;
942 1.1 rmind
943 1.1 rmind if (SCARG(uap, notification)) {
944 1.1 rmind /* Get the signal from user-space */
945 1.1 rmind error = copyin(SCARG(uap, notification), &sig,
946 1.1 rmind sizeof(struct sigevent));
947 1.1 rmind if (error)
948 1.1 rmind return error;
949 1.28 drochner if (sig.sigev_notify == SIGEV_SIGNAL &&
950 1.28 drochner (sig.sigev_signo <=0 || sig.sigev_signo >= NSIG))
951 1.28 drochner return EINVAL;
952 1.1 rmind }
953 1.1 rmind
954 1.18 rmind error = mqueue_get(SCARG(uap, mqdes), &fp);
955 1.1 rmind if (error)
956 1.1 rmind return error;
957 1.1 rmind mq = fp->f_data;
958 1.1 rmind
959 1.1 rmind if (SCARG(uap, notification)) {
960 1.1 rmind /* Register notification: set the signal and target process */
961 1.1 rmind if (mq->mq_notify_proc == NULL) {
962 1.1 rmind memcpy(&mq->mq_sig_notify, &sig,
963 1.1 rmind sizeof(struct sigevent));
964 1.1 rmind mq->mq_notify_proc = l->l_proc;
965 1.1 rmind } else {
966 1.1 rmind /* Fail if someone else already registered */
967 1.1 rmind error = EBUSY;
968 1.1 rmind }
969 1.1 rmind } else {
970 1.1 rmind /* Unregister the notification */
971 1.1 rmind mq->mq_notify_proc = NULL;
972 1.1 rmind }
973 1.1 rmind mutex_exit(&mq->mq_mtx);
974 1.7 ad fd_putfile((int)SCARG(uap, mqdes));
975 1.1 rmind
976 1.1 rmind return error;
977 1.1 rmind }
978 1.1 rmind
979 1.1 rmind int
980 1.8 rmind sys_mq_getattr(struct lwp *l, const struct sys_mq_getattr_args *uap,
981 1.8 rmind register_t *retval)
982 1.1 rmind {
983 1.6 dsl /* {
984 1.1 rmind syscallarg(mqd_t) mqdes;
985 1.1 rmind syscallarg(struct mq_attr *) mqstat;
986 1.6 dsl } */
987 1.7 ad file_t *fp = NULL;
988 1.1 rmind struct mqueue *mq;
989 1.1 rmind struct mq_attr attr;
990 1.1 rmind int error;
991 1.1 rmind
992 1.1 rmind /* Get the message queue */
993 1.18 rmind error = mqueue_get(SCARG(uap, mqdes), &fp);
994 1.1 rmind if (error)
995 1.1 rmind return error;
996 1.1 rmind mq = fp->f_data;
997 1.1 rmind memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
998 1.1 rmind mutex_exit(&mq->mq_mtx);
999 1.7 ad fd_putfile((int)SCARG(uap, mqdes));
1000 1.1 rmind
1001 1.1 rmind return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
1002 1.1 rmind }
1003 1.1 rmind
1004 1.1 rmind int
1005 1.8 rmind sys_mq_setattr(struct lwp *l, const struct sys_mq_setattr_args *uap,
1006 1.8 rmind register_t *retval)
1007 1.1 rmind {
1008 1.6 dsl /* {
1009 1.1 rmind syscallarg(mqd_t) mqdes;
1010 1.1 rmind syscallarg(const struct mq_attr *) mqstat;
1011 1.1 rmind syscallarg(struct mq_attr *) omqstat;
1012 1.6 dsl } */
1013 1.7 ad file_t *fp = NULL;
1014 1.1 rmind struct mqueue *mq;
1015 1.1 rmind struct mq_attr attr;
1016 1.1 rmind int error, nonblock;
1017 1.1 rmind
1018 1.1 rmind error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
1019 1.1 rmind if (error)
1020 1.1 rmind return error;
1021 1.1 rmind nonblock = (attr.mq_flags & O_NONBLOCK);
1022 1.1 rmind
1023 1.1 rmind /* Get the message queue */
1024 1.18 rmind error = mqueue_get(SCARG(uap, mqdes), &fp);
1025 1.1 rmind if (error)
1026 1.1 rmind return error;
1027 1.1 rmind mq = fp->f_data;
1028 1.1 rmind
1029 1.1 rmind /* Copy the old attributes, if needed */
1030 1.22 rmind if (SCARG(uap, omqstat)) {
1031 1.1 rmind memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
1032 1.22 rmind }
1033 1.1 rmind
1034 1.1 rmind /* Ignore everything, except O_NONBLOCK */
1035 1.1 rmind if (nonblock)
1036 1.1 rmind mq->mq_attrib.mq_flags |= O_NONBLOCK;
1037 1.1 rmind else
1038 1.1 rmind mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
1039 1.1 rmind
1040 1.1 rmind mutex_exit(&mq->mq_mtx);
1041 1.7 ad fd_putfile((int)SCARG(uap, mqdes));
1042 1.1 rmind
1043 1.1 rmind /*
1044 1.1 rmind * Copy the data to the user-space.
1045 1.1 rmind * Note: According to POSIX, the new attributes should not be set in
1046 1.1 rmind * case of fail - this would be violated.
1047 1.1 rmind */
1048 1.1 rmind if (SCARG(uap, omqstat))
1049 1.1 rmind error = copyout(&attr, SCARG(uap, omqstat),
1050 1.1 rmind sizeof(struct mq_attr));
1051 1.1 rmind
1052 1.1 rmind return error;
1053 1.1 rmind }
1054 1.1 rmind
1055 1.1 rmind int
1056 1.8 rmind sys_mq_unlink(struct lwp *l, const struct sys_mq_unlink_args *uap,
1057 1.8 rmind register_t *retval)
1058 1.1 rmind {
1059 1.6 dsl /* {
1060 1.1 rmind syscallarg(const char *) name;
1061 1.6 dsl } */
1062 1.1 rmind struct mqueue *mq;
1063 1.1 rmind char *name;
1064 1.1 rmind int error, refcnt = 0;
1065 1.1 rmind
1066 1.1 rmind /* Get the name from the user-space */
1067 1.1 rmind name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
1068 1.1 rmind error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
1069 1.1 rmind if (error) {
1070 1.1 rmind kmem_free(name, MQ_NAMELEN);
1071 1.1 rmind return error;
1072 1.1 rmind }
1073 1.1 rmind
1074 1.1 rmind /* Lookup for this file */
1075 1.1 rmind mutex_enter(&mqlist_mtx);
1076 1.1 rmind mq = mqueue_lookup(name);
1077 1.1 rmind if (mq == NULL) {
1078 1.1 rmind error = ENOENT;
1079 1.1 rmind goto error;
1080 1.1 rmind }
1081 1.1 rmind
1082 1.1 rmind /* Check the permissions */
1083 1.18 rmind if (kauth_cred_geteuid(l->l_cred) != mq->mq_euid &&
1084 1.18 rmind kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
1085 1.1 rmind mutex_exit(&mq->mq_mtx);
1086 1.1 rmind error = EACCES;
1087 1.1 rmind goto error;
1088 1.1 rmind }
1089 1.1 rmind
1090 1.1 rmind /* Mark message queue as unlinking, before leaving the window */
1091 1.1 rmind mq->mq_attrib.mq_flags |= MQ_UNLINK;
1092 1.1 rmind
1093 1.1 rmind /* Wake up all waiters, if there are such */
1094 1.1 rmind cv_broadcast(&mq->mq_send_cv);
1095 1.1 rmind cv_broadcast(&mq->mq_recv_cv);
1096 1.1 rmind
1097 1.8 rmind selnotify(&mq->mq_rsel, POLLHUP, 0);
1098 1.8 rmind selnotify(&mq->mq_wsel, POLLHUP, 0);
1099 1.8 rmind
1100 1.1 rmind refcnt = mq->mq_refcnt;
1101 1.1 rmind if (refcnt == 0)
1102 1.1 rmind LIST_REMOVE(mq, mq_list);
1103 1.1 rmind
1104 1.1 rmind mutex_exit(&mq->mq_mtx);
1105 1.1 rmind error:
1106 1.1 rmind mutex_exit(&mqlist_mtx);
1107 1.1 rmind
1108 1.1 rmind /*
1109 1.1 rmind * If there are no references - destroy the message
1110 1.1 rmind * queue, otherwise, the last mq_close() will do that.
1111 1.1 rmind */
1112 1.1 rmind if (error == 0 && refcnt == 0)
1113 1.1 rmind mqueue_destroy(mq);
1114 1.1 rmind
1115 1.1 rmind kmem_free(name, MQ_NAMELEN);
1116 1.1 rmind return error;
1117 1.1 rmind }
1118 1.1 rmind
1119 1.1 rmind /*
1120 1.22 rmind * System control nodes.
1121 1.1 rmind */
1122 1.1 rmind
1123 1.1 rmind SYSCTL_SETUP(sysctl_mqueue_setup, "sysctl mqueue setup")
1124 1.1 rmind {
1125 1.1 rmind const struct sysctlnode *node = NULL;
1126 1.1 rmind
1127 1.1 rmind sysctl_createv(clog, 0, NULL, NULL,
1128 1.1 rmind CTLFLAG_PERMANENT,
1129 1.1 rmind CTLTYPE_NODE, "kern", NULL,
1130 1.1 rmind NULL, 0, NULL, 0,
1131 1.1 rmind CTL_KERN, CTL_EOL);
1132 1.1 rmind sysctl_createv(clog, 0, NULL, NULL,
1133 1.1 rmind CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1134 1.1 rmind CTLTYPE_INT, "posix_msg",
1135 1.1 rmind SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
1136 1.1 rmind "Message Passing option to which the "
1137 1.1 rmind "system attempts to conform"),
1138 1.1 rmind NULL, _POSIX_MESSAGE_PASSING, NULL, 0,
1139 1.1 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
1140 1.1 rmind sysctl_createv(clog, 0, NULL, &node,
1141 1.1 rmind CTLFLAG_PERMANENT,
1142 1.1 rmind CTLTYPE_NODE, "mqueue",
1143 1.1 rmind SYSCTL_DESCR("Message queue options"),
1144 1.1 rmind NULL, 0, NULL, 0,
1145 1.1 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
1146 1.1 rmind
1147 1.1 rmind if (node == NULL)
1148 1.1 rmind return;
1149 1.1 rmind
1150 1.1 rmind sysctl_createv(clog, 0, &node, NULL,
1151 1.1 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1152 1.1 rmind CTLTYPE_INT, "mq_open_max",
1153 1.1 rmind SYSCTL_DESCR("Maximal number of message queue descriptors "
1154 1.1 rmind "that process could open"),
1155 1.1 rmind NULL, 0, &mq_open_max, 0,
1156 1.1 rmind CTL_CREATE, CTL_EOL);
1157 1.1 rmind sysctl_createv(clog, 0, &node, NULL,
1158 1.1 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1159 1.1 rmind CTLTYPE_INT, "mq_prio_max",
1160 1.1 rmind SYSCTL_DESCR("Maximal priority of the message"),
1161 1.1 rmind NULL, 0, &mq_prio_max, 0,
1162 1.1 rmind CTL_CREATE, CTL_EOL);
1163 1.1 rmind sysctl_createv(clog, 0, &node, NULL,
1164 1.1 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1165 1.1 rmind CTLTYPE_INT, "mq_max_msgsize",
1166 1.1 rmind SYSCTL_DESCR("Maximal allowed size of the message"),
1167 1.1 rmind NULL, 0, &mq_max_msgsize, 0,
1168 1.1 rmind CTL_CREATE, CTL_EOL);
1169 1.1 rmind sysctl_createv(clog, 0, &node, NULL,
1170 1.1 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1171 1.1 rmind CTLTYPE_INT, "mq_def_maxmsg",
1172 1.1 rmind SYSCTL_DESCR("Default maximal message count"),
1173 1.1 rmind NULL, 0, &mq_def_maxmsg, 0,
1174 1.1 rmind CTL_CREATE, CTL_EOL);
1175 1.28 drochner sysctl_createv(clog, 0, &node, NULL,
1176 1.28 drochner CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1177 1.28 drochner CTLTYPE_INT, "mq_max_maxmsg",
1178 1.28 drochner SYSCTL_DESCR("Maximal allowed message count"),
1179 1.28 drochner NULL, 0, &mq_max_maxmsg, 0,
1180 1.28 drochner CTL_CREATE, CTL_EOL);
1181 1.1 rmind }
1182 1.1 rmind
1183 1.1 rmind /*
1184 1.1 rmind * Debugging.
1185 1.1 rmind */
1186 1.1 rmind #if defined(DDB)
1187 1.1 rmind
1188 1.1 rmind void
1189 1.1 rmind mqueue_print_list(void (*pr)(const char *, ...))
1190 1.1 rmind {
1191 1.1 rmind struct mqueue *mq;
1192 1.1 rmind
1193 1.1 rmind (*pr)("Global list of the message queues:\n");
1194 1.1 rmind (*pr)("%20s %10s %8s %8s %3s %4s %4s %4s\n",
1195 1.1 rmind "Name", "Ptr", "Mode", "Flags", "Ref",
1196 1.1 rmind "MaxMsg", "MsgSze", "CurMsg");
1197 1.1 rmind LIST_FOREACH(mq, &mqueue_head, mq_list) {
1198 1.1 rmind (*pr)("%20s %10p %8x %8x %3u %6lu %6lu %6lu\n",
1199 1.1 rmind mq->mq_name, mq, mq->mq_mode,
1200 1.1 rmind mq->mq_attrib.mq_flags, mq->mq_refcnt,
1201 1.1 rmind mq->mq_attrib.mq_maxmsg, mq->mq_attrib.mq_msgsize,
1202 1.1 rmind mq->mq_attrib.mq_curmsgs);
1203 1.1 rmind }
1204 1.1 rmind }
1205 1.1 rmind
1206 1.1 rmind #endif /* defined(DDB) */
1207