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