sysv_msg.c revision 1.22 1 1.22 kleink /* $NetBSD: sysv_msg.c,v 1.22 1998/05/07 18:00:49 kleink Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.1 cgd * Implementation of SVID messages
5 1.1 cgd *
6 1.1 cgd * Author: Daniel Boulet
7 1.1 cgd *
8 1.1 cgd * Copyright 1993 Daniel Boulet and RTMX Inc.
9 1.1 cgd *
10 1.1 cgd * This system call was implemented by Daniel Boulet under contract from RTMX.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source forms, with and without modification,
13 1.1 cgd * are permitted provided that this entire comment appears intact.
14 1.1 cgd *
15 1.1 cgd * Redistribution in binary form may occur without any restrictions.
16 1.1 cgd * Obviously, it would be nice if you gave credit where credit is due
17 1.1 cgd * but requiring it would be too onerous.
18 1.1 cgd *
19 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
20 1.1 cgd */
21 1.1 cgd
22 1.2 mycroft #include <sys/param.h>
23 1.2 mycroft #include <sys/systm.h>
24 1.2 mycroft #include <sys/kernel.h>
25 1.2 mycroft #include <sys/proc.h>
26 1.2 mycroft #include <sys/msg.h>
27 1.2 mycroft #include <sys/malloc.h>
28 1.1 cgd
29 1.10 cgd #include <sys/mount.h>
30 1.10 cgd #include <sys/syscallargs.h>
31 1.18 christos
32 1.1 cgd #define MSG_DEBUG
33 1.1 cgd #undef MSG_DEBUG_OK
34 1.1 cgd
35 1.20 christos #ifdef MSG_DEBUG_OK
36 1.21 christos #define MSG_PRINTF(a) printf a
37 1.20 christos #else
38 1.20 christos #define MSG_PRINTF(a)
39 1.20 christos #endif
40 1.20 christos
41 1.1 cgd int nfree_msgmaps; /* # of free map entries */
42 1.1 cgd short free_msgmaps; /* head of linked list of free map entries */
43 1.1 cgd struct msg *free_msghdrs; /* list of free msg headers */
44 1.1 cgd
45 1.18 christos static void msg_freehdr __P((struct msg *));
46 1.18 christos
47 1.18 christos void
48 1.1 cgd msginit()
49 1.1 cgd {
50 1.3 mycroft register int i;
51 1.1 cgd
52 1.3 mycroft /*
53 1.3 mycroft * msginfo.msgssz should be a power of two for efficiency reasons.
54 1.3 mycroft * It is also pretty silly if msginfo.msgssz is less than 8
55 1.3 mycroft * or greater than about 256 so ...
56 1.3 mycroft */
57 1.1 cgd
58 1.3 mycroft i = 8;
59 1.3 mycroft while (i < 1024 && i != msginfo.msgssz)
60 1.3 mycroft i <<= 1;
61 1.3 mycroft if (i != msginfo.msgssz) {
62 1.20 christos MSG_PRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
63 1.20 christos msginfo.msgssz));
64 1.3 mycroft panic("msginfo.msgssz not a small power of 2");
65 1.3 mycroft }
66 1.3 mycroft
67 1.3 mycroft if (msginfo.msgseg > 32767) {
68 1.20 christos MSG_PRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
69 1.3 mycroft panic("msginfo.msgseg > 32767");
70 1.3 mycroft }
71 1.3 mycroft
72 1.3 mycroft if (msgmaps == NULL)
73 1.3 mycroft panic("msgmaps is NULL");
74 1.3 mycroft
75 1.3 mycroft for (i = 0; i < msginfo.msgseg; i++) {
76 1.3 mycroft if (i > 0)
77 1.3 mycroft msgmaps[i-1].next = i;
78 1.3 mycroft msgmaps[i].next = -1; /* implies entry is available */
79 1.3 mycroft }
80 1.3 mycroft free_msgmaps = 0;
81 1.3 mycroft nfree_msgmaps = msginfo.msgseg;
82 1.3 mycroft
83 1.3 mycroft if (msghdrs == NULL)
84 1.3 mycroft panic("msghdrs is NULL");
85 1.3 mycroft
86 1.3 mycroft for (i = 0; i < msginfo.msgtql; i++) {
87 1.3 mycroft msghdrs[i].msg_type = 0;
88 1.3 mycroft if (i > 0)
89 1.3 mycroft msghdrs[i-1].msg_next = &msghdrs[i];
90 1.3 mycroft msghdrs[i].msg_next = NULL;
91 1.3 mycroft }
92 1.3 mycroft free_msghdrs = &msghdrs[0];
93 1.3 mycroft
94 1.3 mycroft if (msqids == NULL)
95 1.3 mycroft panic("msqids is NULL");
96 1.3 mycroft
97 1.4 mycroft for (i = 0; i < msginfo.msgmni; i++) {
98 1.3 mycroft msqids[i].msg_qbytes = 0; /* implies entry is available */
99 1.3 mycroft msqids[i].msg_perm.seq = 0; /* reset to a known value */
100 1.3 mycroft }
101 1.1 cgd }
102 1.1 cgd
103 1.3 mycroft static void
104 1.1 cgd msg_freehdr(msghdr)
105 1.3 mycroft struct msg *msghdr;
106 1.1 cgd {
107 1.3 mycroft while (msghdr->msg_ts > 0) {
108 1.3 mycroft short next;
109 1.3 mycroft if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
110 1.3 mycroft panic("msghdr->msg_spot out of range");
111 1.3 mycroft next = msgmaps[msghdr->msg_spot].next;
112 1.3 mycroft msgmaps[msghdr->msg_spot].next = free_msgmaps;
113 1.3 mycroft free_msgmaps = msghdr->msg_spot;
114 1.5 mycroft nfree_msgmaps++;
115 1.3 mycroft msghdr->msg_spot = next;
116 1.3 mycroft if (msghdr->msg_ts >= msginfo.msgssz)
117 1.3 mycroft msghdr->msg_ts -= msginfo.msgssz;
118 1.3 mycroft else
119 1.3 mycroft msghdr->msg_ts = 0;
120 1.3 mycroft }
121 1.3 mycroft if (msghdr->msg_spot != -1)
122 1.3 mycroft panic("msghdr->msg_spot != -1");
123 1.3 mycroft msghdr->msg_next = free_msghdrs;
124 1.3 mycroft free_msghdrs = msghdr;
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd int
128 1.17 mycroft sys_msgctl(p, v, retval)
129 1.1 cgd struct proc *p;
130 1.16 thorpej void *v;
131 1.16 thorpej register_t *retval;
132 1.16 thorpej {
133 1.17 mycroft register struct sys_msgctl_args /* {
134 1.10 cgd syscallarg(int) msqid;
135 1.10 cgd syscallarg(int) cmd;
136 1.10 cgd syscallarg(struct msqid_ds *) buf;
137 1.16 thorpej } */ *uap = v;
138 1.10 cgd int msqid = SCARG(uap, msqid);
139 1.10 cgd int cmd = SCARG(uap, cmd);
140 1.10 cgd struct msqid_ds *user_msqptr = SCARG(uap, buf);
141 1.3 mycroft struct ucred *cred = p->p_ucred;
142 1.18 christos int rval, eval;
143 1.3 mycroft struct msqid_ds msqbuf;
144 1.3 mycroft register struct msqid_ds *msqptr;
145 1.1 cgd
146 1.20 christos MSG_PRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, user_msqptr));
147 1.1 cgd
148 1.3 mycroft msqid = IPCID_TO_IX(msqid);
149 1.1 cgd
150 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
151 1.20 christos MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
152 1.20 christos msginfo.msgmni));
153 1.3 mycroft return(EINVAL);
154 1.3 mycroft }
155 1.1 cgd
156 1.3 mycroft msqptr = &msqids[msqid];
157 1.1 cgd
158 1.3 mycroft if (msqptr->msg_qbytes == 0) {
159 1.20 christos MSG_PRINTF(("no such msqid\n"));
160 1.3 mycroft return(EINVAL);
161 1.3 mycroft }
162 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
163 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
164 1.3 mycroft return(EINVAL);
165 1.3 mycroft }
166 1.1 cgd
167 1.3 mycroft eval = 0;
168 1.3 mycroft rval = 0;
169 1.1 cgd
170 1.3 mycroft switch (cmd) {
171 1.1 cgd
172 1.3 mycroft case IPC_RMID:
173 1.1 cgd {
174 1.3 mycroft struct msg *msghdr;
175 1.18 christos if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
176 1.7 mycroft return(eval);
177 1.3 mycroft /* Free the message headers */
178 1.3 mycroft msghdr = msqptr->msg_first;
179 1.3 mycroft while (msghdr != NULL) {
180 1.3 mycroft struct msg *msghdr_tmp;
181 1.3 mycroft
182 1.3 mycroft /* Free the segments of each message */
183 1.3 mycroft msqptr->msg_cbytes -= msghdr->msg_ts;
184 1.5 mycroft msqptr->msg_qnum--;
185 1.3 mycroft msghdr_tmp = msghdr;
186 1.3 mycroft msghdr = msghdr->msg_next;
187 1.3 mycroft msg_freehdr(msghdr_tmp);
188 1.3 mycroft }
189 1.1 cgd
190 1.3 mycroft if (msqptr->msg_cbytes != 0)
191 1.3 mycroft panic("msg_cbytes is screwed up");
192 1.3 mycroft if (msqptr->msg_qnum != 0)
193 1.3 mycroft panic("msg_qnum is screwed up");
194 1.1 cgd
195 1.3 mycroft msqptr->msg_qbytes = 0; /* Mark it as free */
196 1.1 cgd
197 1.3 mycroft wakeup((caddr_t)msqptr);
198 1.1 cgd }
199 1.1 cgd
200 1.3 mycroft break;
201 1.1 cgd
202 1.3 mycroft case IPC_SET:
203 1.8 mycroft if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
204 1.7 mycroft return(eval);
205 1.3 mycroft if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
206 1.3 mycroft return(eval);
207 1.3 mycroft if (msqbuf.msg_qbytes > msqptr->msg_qbytes && cred->cr_uid != 0)
208 1.3 mycroft return(EPERM);
209 1.3 mycroft if (msqbuf.msg_qbytes > msginfo.msgmnb) {
210 1.20 christos MSG_PRINTF(("can't increase msg_qbytes beyond %d (truncating)\n",
211 1.20 christos msginfo.msgmnb));
212 1.3 mycroft msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
213 1.3 mycroft }
214 1.3 mycroft if (msqbuf.msg_qbytes == 0) {
215 1.20 christos MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
216 1.3 mycroft return(EINVAL); /* non-standard errno! */
217 1.3 mycroft }
218 1.3 mycroft msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
219 1.3 mycroft msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
220 1.3 mycroft msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
221 1.3 mycroft (msqbuf.msg_perm.mode & 0777);
222 1.3 mycroft msqptr->msg_qbytes = msqbuf.msg_qbytes;
223 1.3 mycroft msqptr->msg_ctime = time.tv_sec;
224 1.3 mycroft break;
225 1.1 cgd
226 1.3 mycroft case IPC_STAT:
227 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
228 1.20 christos MSG_PRINTF(("requester doesn't have read access\n"));
229 1.3 mycroft return(eval);
230 1.3 mycroft }
231 1.3 mycroft eval = copyout((caddr_t)msqptr, user_msqptr,
232 1.3 mycroft sizeof(struct msqid_ds));
233 1.3 mycroft break;
234 1.1 cgd
235 1.3 mycroft default:
236 1.20 christos MSG_PRINTF(("invalid command %d\n", cmd));
237 1.3 mycroft return(EINVAL);
238 1.3 mycroft }
239 1.3 mycroft
240 1.3 mycroft if (eval == 0)
241 1.3 mycroft *retval = rval;
242 1.3 mycroft return(eval);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd int
246 1.17 mycroft sys_msgget(p, v, retval)
247 1.1 cgd struct proc *p;
248 1.16 thorpej void *v;
249 1.16 thorpej register_t *retval;
250 1.16 thorpej {
251 1.17 mycroft register struct sys_msgget_args /* {
252 1.10 cgd syscallarg(key_t) key;
253 1.10 cgd syscallarg(int) msgflg;
254 1.16 thorpej } */ *uap = v;
255 1.3 mycroft int msqid, eval;
256 1.10 cgd int key = SCARG(uap, key);
257 1.10 cgd int msgflg = SCARG(uap, msgflg);
258 1.3 mycroft struct ucred *cred = p->p_ucred;
259 1.18 christos register struct msqid_ds *msqptr = NULL;
260 1.1 cgd
261 1.20 christos MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
262 1.1 cgd
263 1.5 mycroft if (key != IPC_PRIVATE) {
264 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
265 1.3 mycroft msqptr = &msqids[msqid];
266 1.3 mycroft if (msqptr->msg_qbytes != 0 &&
267 1.3 mycroft msqptr->msg_perm.key == key)
268 1.3 mycroft break;
269 1.3 mycroft }
270 1.3 mycroft if (msqid < msginfo.msgmni) {
271 1.20 christos MSG_PRINTF(("found public key\n"));
272 1.3 mycroft if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
273 1.20 christos MSG_PRINTF(("not exclusive\n"));
274 1.3 mycroft return(EEXIST);
275 1.3 mycroft }
276 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, msgflg & 0700 ))) {
277 1.20 christos MSG_PRINTF(("requester doesn't have 0%o access\n",
278 1.20 christos msgflg & 0700));
279 1.3 mycroft return(eval);
280 1.3 mycroft }
281 1.5 mycroft goto found;
282 1.3 mycroft }
283 1.1 cgd }
284 1.1 cgd
285 1.20 christos MSG_PRINTF(("need to allocate the msqid_ds\n"));
286 1.5 mycroft if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
287 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
288 1.5 mycroft /*
289 1.5 mycroft * Look for an unallocated and unlocked msqid_ds.
290 1.5 mycroft * msqid_ds's can be locked by msgsnd or msgrcv while
291 1.5 mycroft * they are copying the message in/out. We can't
292 1.5 mycroft * re-use the entry until they release it.
293 1.5 mycroft */
294 1.5 mycroft msqptr = &msqids[msqid];
295 1.5 mycroft if (msqptr->msg_qbytes == 0 &&
296 1.5 mycroft (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
297 1.5 mycroft break;
298 1.5 mycroft }
299 1.5 mycroft if (msqid == msginfo.msgmni) {
300 1.20 christos MSG_PRINTF(("no more msqid_ds's available\n"));
301 1.5 mycroft return(ENOSPC);
302 1.5 mycroft }
303 1.20 christos MSG_PRINTF(("msqid %d is available\n", msqid));
304 1.5 mycroft msqptr->msg_perm.key = key;
305 1.5 mycroft msqptr->msg_perm.cuid = cred->cr_uid;
306 1.5 mycroft msqptr->msg_perm.uid = cred->cr_uid;
307 1.5 mycroft msqptr->msg_perm.cgid = cred->cr_gid;
308 1.5 mycroft msqptr->msg_perm.gid = cred->cr_gid;
309 1.5 mycroft msqptr->msg_perm.mode = (msgflg & 0777);
310 1.5 mycroft /* Make sure that the returned msqid is unique */
311 1.5 mycroft msqptr->msg_perm.seq++;
312 1.5 mycroft msqptr->msg_first = NULL;
313 1.5 mycroft msqptr->msg_last = NULL;
314 1.5 mycroft msqptr->msg_cbytes = 0;
315 1.5 mycroft msqptr->msg_qnum = 0;
316 1.5 mycroft msqptr->msg_qbytes = msginfo.msgmnb;
317 1.5 mycroft msqptr->msg_lspid = 0;
318 1.5 mycroft msqptr->msg_lrpid = 0;
319 1.5 mycroft msqptr->msg_stime = 0;
320 1.5 mycroft msqptr->msg_rtime = 0;
321 1.5 mycroft msqptr->msg_ctime = time.tv_sec;
322 1.5 mycroft } else {
323 1.20 christos MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
324 1.5 mycroft return(ENOENT);
325 1.1 cgd }
326 1.1 cgd
327 1.5 mycroft found:
328 1.3 mycroft /* Construct the unique msqid */
329 1.3 mycroft *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
330 1.3 mycroft return(0);
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd int
334 1.17 mycroft sys_msgsnd(p, v, retval)
335 1.1 cgd struct proc *p;
336 1.16 thorpej void *v;
337 1.16 thorpej register_t *retval;
338 1.16 thorpej {
339 1.17 mycroft register struct sys_msgsnd_args /* {
340 1.10 cgd syscallarg(int) msqid;
341 1.22 kleink syscallarg(const void *) msgp;
342 1.10 cgd syscallarg(size_t) msgsz;
343 1.10 cgd syscallarg(int) msgflg;
344 1.16 thorpej } */ *uap = v;
345 1.10 cgd int msqid = SCARG(uap, msqid);
346 1.22 kleink const char *user_msgp = SCARG(uap, msgp);
347 1.10 cgd size_t msgsz = SCARG(uap, msgsz);
348 1.10 cgd int msgflg = SCARG(uap, msgflg);
349 1.3 mycroft int segs_needed, eval;
350 1.3 mycroft struct ucred *cred = p->p_ucred;
351 1.3 mycroft register struct msqid_ds *msqptr;
352 1.3 mycroft register struct msg *msghdr;
353 1.3 mycroft short next;
354 1.1 cgd
355 1.20 christos MSG_PRINTF(("call to msgsnd(%d, %p, %d, %d)\n", msqid, user_msgp, msgsz,
356 1.20 christos msgflg));
357 1.1 cgd
358 1.3 mycroft msqid = IPCID_TO_IX(msqid);
359 1.1 cgd
360 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
361 1.20 christos MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
362 1.20 christos msginfo.msgmni));
363 1.3 mycroft return(EINVAL);
364 1.3 mycroft }
365 1.1 cgd
366 1.3 mycroft msqptr = &msqids[msqid];
367 1.3 mycroft if (msqptr->msg_qbytes == 0) {
368 1.20 christos MSG_PRINTF(("no such message queue id\n"));
369 1.3 mycroft return(EINVAL);
370 1.3 mycroft }
371 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
372 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
373 1.3 mycroft return(EINVAL);
374 1.3 mycroft }
375 1.1 cgd
376 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
377 1.20 christos MSG_PRINTF(("requester doesn't have write access\n"));
378 1.3 mycroft return(eval);
379 1.3 mycroft }
380 1.1 cgd
381 1.3 mycroft segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
382 1.20 christos MSG_PRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
383 1.20 christos segs_needed));
384 1.3 mycroft for (;;) {
385 1.3 mycroft int need_more_resources = 0;
386 1.1 cgd
387 1.3 mycroft /*
388 1.18 christos * check msgsz [cannot be negative since it is unsigned]
389 1.3 mycroft * (inside this loop in case msg_qbytes changes while we sleep)
390 1.3 mycroft */
391 1.1 cgd
392 1.18 christos if (msgsz > msqptr->msg_qbytes) {
393 1.20 christos MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
394 1.3 mycroft return(EINVAL);
395 1.3 mycroft }
396 1.1 cgd
397 1.3 mycroft if (msqptr->msg_perm.mode & MSG_LOCKED) {
398 1.20 christos MSG_PRINTF(("msqid is locked\n"));
399 1.3 mycroft need_more_resources = 1;
400 1.3 mycroft }
401 1.3 mycroft if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
402 1.20 christos MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
403 1.3 mycroft need_more_resources = 1;
404 1.3 mycroft }
405 1.3 mycroft if (segs_needed > nfree_msgmaps) {
406 1.20 christos MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
407 1.3 mycroft need_more_resources = 1;
408 1.3 mycroft }
409 1.3 mycroft if (free_msghdrs == NULL) {
410 1.20 christos MSG_PRINTF(("no more msghdrs\n"));
411 1.3 mycroft need_more_resources = 1;
412 1.3 mycroft }
413 1.1 cgd
414 1.3 mycroft if (need_more_resources) {
415 1.3 mycroft int we_own_it;
416 1.1 cgd
417 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
418 1.20 christos MSG_PRINTF(("need more resources but caller doesn't want to wait\n"));
419 1.3 mycroft return(EAGAIN);
420 1.3 mycroft }
421 1.1 cgd
422 1.3 mycroft if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
423 1.20 christos MSG_PRINTF(("we don't own the msqid_ds\n"));
424 1.3 mycroft we_own_it = 0;
425 1.3 mycroft } else {
426 1.3 mycroft /* Force later arrivals to wait for our
427 1.3 mycroft request */
428 1.20 christos MSG_PRINTF(("we own the msqid_ds\n"));
429 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
430 1.3 mycroft we_own_it = 1;
431 1.3 mycroft }
432 1.20 christos MSG_PRINTF(("goodnight\n"));
433 1.3 mycroft eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
434 1.3 mycroft "msgwait", 0);
435 1.20 christos MSG_PRINTF(("good morning, eval=%d\n", eval));
436 1.3 mycroft if (we_own_it)
437 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
438 1.3 mycroft if (eval != 0) {
439 1.20 christos MSG_PRINTF(("msgsnd: interrupted system call\n"));
440 1.3 mycroft return(EINTR);
441 1.3 mycroft }
442 1.1 cgd
443 1.3 mycroft /*
444 1.3 mycroft * Make sure that the msq queue still exists
445 1.3 mycroft */
446 1.1 cgd
447 1.3 mycroft if (msqptr->msg_qbytes == 0) {
448 1.20 christos MSG_PRINTF(("msqid deleted\n"));
449 1.3 mycroft /* The SVID says to return EIDRM. */
450 1.1 cgd #ifdef EIDRM
451 1.3 mycroft return(EIDRM);
452 1.1 cgd #else
453 1.3 mycroft /* Unfortunately, BSD doesn't define that code
454 1.3 mycroft yet! */
455 1.3 mycroft return(EINVAL);
456 1.1 cgd #endif
457 1.3 mycroft }
458 1.1 cgd
459 1.3 mycroft } else {
460 1.20 christos MSG_PRINTF(("got all the resources that we need\n"));
461 1.3 mycroft break;
462 1.3 mycroft }
463 1.1 cgd }
464 1.1 cgd
465 1.3 mycroft /*
466 1.3 mycroft * We have the resources that we need.
467 1.3 mycroft * Make sure!
468 1.3 mycroft */
469 1.1 cgd
470 1.3 mycroft if (msqptr->msg_perm.mode & MSG_LOCKED)
471 1.3 mycroft panic("msg_perm.mode & MSG_LOCKED");
472 1.3 mycroft if (segs_needed > nfree_msgmaps)
473 1.3 mycroft panic("segs_needed > nfree_msgmaps");
474 1.3 mycroft if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
475 1.3 mycroft panic("msgsz + msg_cbytes > msg_qbytes");
476 1.3 mycroft if (free_msghdrs == NULL)
477 1.3 mycroft panic("no more msghdrs");
478 1.1 cgd
479 1.3 mycroft /*
480 1.3 mycroft * Re-lock the msqid_ds in case we page-fault when copying in the
481 1.3 mycroft * message
482 1.3 mycroft */
483 1.1 cgd
484 1.3 mycroft if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
485 1.3 mycroft panic("msqid_ds is already locked");
486 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
487 1.1 cgd
488 1.3 mycroft /*
489 1.3 mycroft * Allocate a message header
490 1.3 mycroft */
491 1.1 cgd
492 1.3 mycroft msghdr = free_msghdrs;
493 1.3 mycroft free_msghdrs = msghdr->msg_next;
494 1.3 mycroft msghdr->msg_spot = -1;
495 1.3 mycroft msghdr->msg_ts = msgsz;
496 1.1 cgd
497 1.3 mycroft /*
498 1.3 mycroft * Allocate space for the message
499 1.3 mycroft */
500 1.1 cgd
501 1.3 mycroft while (segs_needed > 0) {
502 1.3 mycroft if (nfree_msgmaps <= 0)
503 1.3 mycroft panic("not enough msgmaps");
504 1.3 mycroft if (free_msgmaps == -1)
505 1.3 mycroft panic("nil free_msgmaps");
506 1.3 mycroft next = free_msgmaps;
507 1.3 mycroft if (next <= -1)
508 1.3 mycroft panic("next too low #1");
509 1.3 mycroft if (next >= msginfo.msgseg)
510 1.3 mycroft panic("next out of range #1");
511 1.20 christos MSG_PRINTF(("allocating segment %d to message\n", next));
512 1.3 mycroft free_msgmaps = msgmaps[next].next;
513 1.5 mycroft nfree_msgmaps--;
514 1.3 mycroft msgmaps[next].next = msghdr->msg_spot;
515 1.3 mycroft msghdr->msg_spot = next;
516 1.5 mycroft segs_needed--;
517 1.1 cgd }
518 1.1 cgd
519 1.3 mycroft /*
520 1.3 mycroft * Copy in the message type
521 1.3 mycroft */
522 1.1 cgd
523 1.3 mycroft if ((eval = copyin(user_msgp, &msghdr->msg_type,
524 1.3 mycroft sizeof(msghdr->msg_type))) != 0) {
525 1.20 christos MSG_PRINTF(("error %d copying the message type\n", eval));
526 1.3 mycroft msg_freehdr(msghdr);
527 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
528 1.3 mycroft wakeup((caddr_t)msqptr);
529 1.3 mycroft return(eval);
530 1.3 mycroft }
531 1.3 mycroft user_msgp += sizeof(msghdr->msg_type);
532 1.1 cgd
533 1.3 mycroft /*
534 1.3 mycroft * Validate the message type
535 1.3 mycroft */
536 1.1 cgd
537 1.3 mycroft if (msghdr->msg_type < 1) {
538 1.3 mycroft msg_freehdr(msghdr);
539 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
540 1.3 mycroft wakeup((caddr_t)msqptr);
541 1.20 christos MSG_PRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
542 1.3 mycroft return(EINVAL);
543 1.3 mycroft }
544 1.1 cgd
545 1.3 mycroft /*
546 1.3 mycroft * Copy in the message body
547 1.3 mycroft */
548 1.3 mycroft
549 1.3 mycroft next = msghdr->msg_spot;
550 1.3 mycroft while (msgsz > 0) {
551 1.3 mycroft size_t tlen;
552 1.3 mycroft if (msgsz > msginfo.msgssz)
553 1.3 mycroft tlen = msginfo.msgssz;
554 1.3 mycroft else
555 1.3 mycroft tlen = msgsz;
556 1.3 mycroft if (next <= -1)
557 1.3 mycroft panic("next too low #2");
558 1.3 mycroft if (next >= msginfo.msgseg)
559 1.3 mycroft panic("next out of range #2");
560 1.3 mycroft if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
561 1.3 mycroft tlen)) != 0) {
562 1.20 christos MSG_PRINTF(("error %d copying in message segment\n", eval));
563 1.3 mycroft msg_freehdr(msghdr);
564 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
565 1.3 mycroft wakeup((caddr_t)msqptr);
566 1.3 mycroft return(eval);
567 1.3 mycroft }
568 1.3 mycroft msgsz -= tlen;
569 1.3 mycroft user_msgp += tlen;
570 1.3 mycroft next = msgmaps[next].next;
571 1.1 cgd }
572 1.3 mycroft if (next != -1)
573 1.3 mycroft panic("didn't use all the msg segments");
574 1.1 cgd
575 1.3 mycroft /*
576 1.3 mycroft * We've got the message. Unlock the msqid_ds.
577 1.3 mycroft */
578 1.1 cgd
579 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
580 1.1 cgd
581 1.3 mycroft /*
582 1.3 mycroft * Make sure that the msqid_ds is still allocated.
583 1.3 mycroft */
584 1.1 cgd
585 1.3 mycroft if (msqptr->msg_qbytes == 0) {
586 1.3 mycroft msg_freehdr(msghdr);
587 1.3 mycroft wakeup((caddr_t)msqptr);
588 1.3 mycroft /* The SVID says to return EIDRM. */
589 1.1 cgd #ifdef EIDRM
590 1.3 mycroft return(EIDRM);
591 1.1 cgd #else
592 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
593 1.3 mycroft return(EINVAL);
594 1.1 cgd #endif
595 1.3 mycroft }
596 1.3 mycroft
597 1.3 mycroft /*
598 1.3 mycroft * Put the message into the queue
599 1.3 mycroft */
600 1.1 cgd
601 1.3 mycroft if (msqptr->msg_first == NULL) {
602 1.3 mycroft msqptr->msg_first = msghdr;
603 1.3 mycroft msqptr->msg_last = msghdr;
604 1.3 mycroft } else {
605 1.3 mycroft msqptr->msg_last->msg_next = msghdr;
606 1.3 mycroft msqptr->msg_last = msghdr;
607 1.3 mycroft }
608 1.3 mycroft msqptr->msg_last->msg_next = NULL;
609 1.3 mycroft
610 1.3 mycroft msqptr->msg_cbytes += msghdr->msg_ts;
611 1.5 mycroft msqptr->msg_qnum++;
612 1.3 mycroft msqptr->msg_lspid = p->p_pid;
613 1.3 mycroft msqptr->msg_stime = time.tv_sec;
614 1.3 mycroft
615 1.3 mycroft wakeup((caddr_t)msqptr);
616 1.3 mycroft *retval = 0;
617 1.3 mycroft return(0);
618 1.1 cgd }
619 1.1 cgd
620 1.1 cgd int
621 1.17 mycroft sys_msgrcv(p, v, retval)
622 1.1 cgd struct proc *p;
623 1.16 thorpej void *v;
624 1.16 thorpej register_t *retval;
625 1.16 thorpej {
626 1.17 mycroft register struct sys_msgrcv_args /* {
627 1.10 cgd syscallarg(int) msqid;
628 1.10 cgd syscallarg(void *) msgp;
629 1.10 cgd syscallarg(size_t) msgsz;
630 1.10 cgd syscallarg(long) msgtyp;
631 1.10 cgd syscallarg(int) msgflg;
632 1.16 thorpej } */ *uap = v;
633 1.10 cgd int msqid = SCARG(uap, msqid);
634 1.10 cgd char *user_msgp = SCARG(uap, msgp);
635 1.10 cgd size_t msgsz = SCARG(uap, msgsz);
636 1.10 cgd long msgtyp = SCARG(uap, msgtyp);
637 1.10 cgd int msgflg = SCARG(uap, msgflg);
638 1.3 mycroft size_t len;
639 1.3 mycroft struct ucred *cred = p->p_ucred;
640 1.3 mycroft register struct msqid_ds *msqptr;
641 1.3 mycroft register struct msg *msghdr;
642 1.3 mycroft int eval;
643 1.3 mycroft short next;
644 1.1 cgd
645 1.20 christos MSG_PRINTF(("call to msgrcv(%d, %p, %d, %ld, %d)\n", msqid, user_msgp,
646 1.20 christos msgsz, msgtyp, msgflg));
647 1.1 cgd
648 1.3 mycroft msqid = IPCID_TO_IX(msqid);
649 1.1 cgd
650 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
651 1.20 christos MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
652 1.20 christos msginfo.msgmni));
653 1.3 mycroft return(EINVAL);
654 1.3 mycroft }
655 1.1 cgd
656 1.3 mycroft msqptr = &msqids[msqid];
657 1.3 mycroft if (msqptr->msg_qbytes == 0) {
658 1.20 christos MSG_PRINTF(("no such message queue id\n"));
659 1.3 mycroft return(EINVAL);
660 1.3 mycroft }
661 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
662 1.20 christos MSG_PRINTF(("wrong sequence number\n"));
663 1.3 mycroft return(EINVAL);
664 1.3 mycroft }
665 1.1 cgd
666 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
667 1.20 christos MSG_PRINTF(("requester doesn't have read access\n"));
668 1.3 mycroft return(eval);
669 1.3 mycroft }
670 1.1 cgd
671 1.18 christos #if 0
672 1.18 christos /* cannot happen, msgsz is unsigned */
673 1.3 mycroft if (msgsz < 0) {
674 1.20 christos MSG_PRINTF(("msgsz < 0\n"));
675 1.3 mycroft return(EINVAL);
676 1.3 mycroft }
677 1.18 christos #endif
678 1.1 cgd
679 1.3 mycroft msghdr = NULL;
680 1.3 mycroft while (msghdr == NULL) {
681 1.3 mycroft if (msgtyp == 0) {
682 1.3 mycroft msghdr = msqptr->msg_first;
683 1.3 mycroft if (msghdr != NULL) {
684 1.3 mycroft if (msgsz < msghdr->msg_ts &&
685 1.3 mycroft (msgflg & MSG_NOERROR) == 0) {
686 1.20 christos MSG_PRINTF(("first message on the queue is too big (want %d, got %d)\n",
687 1.20 christos msgsz, msghdr->msg_ts));
688 1.3 mycroft return(E2BIG);
689 1.3 mycroft }
690 1.3 mycroft if (msqptr->msg_first == msqptr->msg_last) {
691 1.3 mycroft msqptr->msg_first = NULL;
692 1.3 mycroft msqptr->msg_last = NULL;
693 1.3 mycroft } else {
694 1.3 mycroft msqptr->msg_first = msghdr->msg_next;
695 1.3 mycroft if (msqptr->msg_first == NULL)
696 1.3 mycroft panic("msg_first/last screwed up #1");
697 1.3 mycroft }
698 1.3 mycroft }
699 1.3 mycroft } else {
700 1.3 mycroft struct msg *previous;
701 1.3 mycroft struct msg **prev;
702 1.1 cgd
703 1.12 mycroft for (previous = NULL, prev = &msqptr->msg_first;
704 1.12 mycroft (msghdr = *prev) != NULL;
705 1.12 mycroft previous = msghdr, prev = &msghdr->msg_next) {
706 1.3 mycroft /*
707 1.3 mycroft * Is this message's type an exact match or is
708 1.3 mycroft * this message's type less than or equal to
709 1.3 mycroft * the absolute value of a negative msgtyp?
710 1.3 mycroft * Note that the second half of this test can
711 1.3 mycroft * NEVER be true if msgtyp is positive since
712 1.3 mycroft * msg_type is always positive!
713 1.3 mycroft */
714 1.3 mycroft
715 1.3 mycroft if (msgtyp == msghdr->msg_type ||
716 1.3 mycroft msghdr->msg_type <= -msgtyp) {
717 1.20 christos MSG_PRINTF(("found message type %d, requested %d\n",
718 1.20 christos msghdr->msg_type, msgtyp));
719 1.3 mycroft if (msgsz < msghdr->msg_ts &&
720 1.3 mycroft (msgflg & MSG_NOERROR) == 0) {
721 1.20 christos MSG_PRINTF(("requested message on the queue is too big (want %d, got %d)\n",
722 1.20 christos msgsz, msghdr->msg_ts));
723 1.3 mycroft return(E2BIG);
724 1.3 mycroft }
725 1.3 mycroft *prev = msghdr->msg_next;
726 1.3 mycroft if (msghdr == msqptr->msg_last) {
727 1.3 mycroft if (previous == NULL) {
728 1.3 mycroft if (prev !=
729 1.3 mycroft &msqptr->msg_first)
730 1.3 mycroft panic("msg_first/last screwed up #2");
731 1.3 mycroft msqptr->msg_first =
732 1.3 mycroft NULL;
733 1.3 mycroft msqptr->msg_last =
734 1.3 mycroft NULL;
735 1.3 mycroft } else {
736 1.3 mycroft if (prev ==
737 1.3 mycroft &msqptr->msg_first)
738 1.3 mycroft panic("msg_first/last screwed up #3");
739 1.3 mycroft msqptr->msg_last =
740 1.3 mycroft previous;
741 1.3 mycroft }
742 1.3 mycroft }
743 1.3 mycroft break;
744 1.3 mycroft }
745 1.3 mycroft }
746 1.1 cgd }
747 1.1 cgd
748 1.3 mycroft /*
749 1.3 mycroft * We've either extracted the msghdr for the appropriate
750 1.3 mycroft * message or there isn't one.
751 1.3 mycroft * If there is one then bail out of this loop.
752 1.3 mycroft */
753 1.1 cgd
754 1.3 mycroft if (msghdr != NULL)
755 1.3 mycroft break;
756 1.1 cgd
757 1.1 cgd /*
758 1.3 mycroft * Hmph! No message found. Does the user want to wait?
759 1.1 cgd */
760 1.1 cgd
761 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
762 1.20 christos MSG_PRINTF(("no appropriate message found (msgtyp=%d)\n",
763 1.20 christos msgtyp));
764 1.3 mycroft /* The SVID says to return ENOMSG. */
765 1.1 cgd #ifdef ENOMSG
766 1.3 mycroft return(ENOMSG);
767 1.1 cgd #else
768 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
769 1.3 mycroft return(EAGAIN);
770 1.1 cgd #endif
771 1.3 mycroft }
772 1.1 cgd
773 1.3 mycroft /*
774 1.3 mycroft * Wait for something to happen
775 1.3 mycroft */
776 1.1 cgd
777 1.20 christos MSG_PRINTF(("msgrcv: goodnight\n"));
778 1.3 mycroft eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
779 1.3 mycroft 0);
780 1.20 christos MSG_PRINTF(("msgrcv: good morning (eval=%d)\n", eval));
781 1.1 cgd
782 1.3 mycroft if (eval != 0) {
783 1.20 christos MSG_PRINTF(("msgsnd: interrupted system call\n"));
784 1.3 mycroft return(EINTR);
785 1.3 mycroft }
786 1.1 cgd
787 1.3 mycroft /*
788 1.3 mycroft * Make sure that the msq queue still exists
789 1.3 mycroft */
790 1.1 cgd
791 1.3 mycroft if (msqptr->msg_qbytes == 0 ||
792 1.10 cgd msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
793 1.20 christos MSG_PRINTF(("msqid deleted\n"));
794 1.3 mycroft /* The SVID says to return EIDRM. */
795 1.1 cgd #ifdef EIDRM
796 1.3 mycroft return(EIDRM);
797 1.1 cgd #else
798 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
799 1.3 mycroft return(EINVAL);
800 1.1 cgd #endif
801 1.3 mycroft }
802 1.1 cgd }
803 1.1 cgd
804 1.3 mycroft /*
805 1.3 mycroft * Return the message to the user.
806 1.3 mycroft *
807 1.3 mycroft * First, do the bookkeeping (before we risk being interrupted).
808 1.3 mycroft */
809 1.1 cgd
810 1.3 mycroft msqptr->msg_cbytes -= msghdr->msg_ts;
811 1.5 mycroft msqptr->msg_qnum--;
812 1.3 mycroft msqptr->msg_lrpid = p->p_pid;
813 1.3 mycroft msqptr->msg_rtime = time.tv_sec;
814 1.1 cgd
815 1.3 mycroft /*
816 1.3 mycroft * Make msgsz the actual amount that we'll be returning.
817 1.3 mycroft * Note that this effectively truncates the message if it is too long
818 1.3 mycroft * (since msgsz is never increased).
819 1.3 mycroft */
820 1.1 cgd
821 1.20 christos MSG_PRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
822 1.20 christos msghdr->msg_ts));
823 1.3 mycroft if (msgsz > msghdr->msg_ts)
824 1.3 mycroft msgsz = msghdr->msg_ts;
825 1.1 cgd
826 1.3 mycroft /*
827 1.3 mycroft * Return the type to the user.
828 1.3 mycroft */
829 1.1 cgd
830 1.12 mycroft eval = copyout((caddr_t)&msghdr->msg_type, user_msgp,
831 1.3 mycroft sizeof(msghdr->msg_type));
832 1.3 mycroft if (eval != 0) {
833 1.20 christos MSG_PRINTF(("error (%d) copying out message type\n", eval));
834 1.3 mycroft msg_freehdr(msghdr);
835 1.3 mycroft wakeup((caddr_t)msqptr);
836 1.3 mycroft return(eval);
837 1.3 mycroft }
838 1.3 mycroft user_msgp += sizeof(msghdr->msg_type);
839 1.3 mycroft
840 1.3 mycroft /*
841 1.3 mycroft * Return the segments to the user
842 1.3 mycroft */
843 1.1 cgd
844 1.3 mycroft next = msghdr->msg_spot;
845 1.3 mycroft for (len = 0; len < msgsz; len += msginfo.msgssz) {
846 1.3 mycroft size_t tlen;
847 1.3 mycroft
848 1.3 mycroft if (msgsz > msginfo.msgssz)
849 1.3 mycroft tlen = msginfo.msgssz;
850 1.3 mycroft else
851 1.3 mycroft tlen = msgsz;
852 1.3 mycroft if (next <= -1)
853 1.3 mycroft panic("next too low #3");
854 1.3 mycroft if (next >= msginfo.msgseg)
855 1.3 mycroft panic("next out of range #3");
856 1.3 mycroft eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
857 1.3 mycroft user_msgp, tlen);
858 1.3 mycroft if (eval != 0) {
859 1.20 christos MSG_PRINTF(("error (%d) copying out message segment\n",
860 1.20 christos eval));
861 1.3 mycroft msg_freehdr(msghdr);
862 1.3 mycroft wakeup((caddr_t)msqptr);
863 1.3 mycroft return(eval);
864 1.3 mycroft }
865 1.3 mycroft user_msgp += tlen;
866 1.3 mycroft next = msgmaps[next].next;
867 1.1 cgd }
868 1.1 cgd
869 1.3 mycroft /*
870 1.3 mycroft * Done, return the actual number of bytes copied out.
871 1.3 mycroft */
872 1.1 cgd
873 1.3 mycroft msg_freehdr(msghdr);
874 1.3 mycroft wakeup((caddr_t)msqptr);
875 1.3 mycroft *retval = msgsz;
876 1.3 mycroft return(0);
877 1.1 cgd }
878