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