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