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