sysv_shm.c revision 1.82 1 /* $NetBSD: sysv_shm.c,v 1.82 2004/10/17 11:24:42 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1994 Adam Glass and Charles M. Hannum. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Adam Glass and Charles M.
54 * Hannum.
55 * 4. The names of the authors may not be used to endorse or promote products
56 * derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.82 2004/10/17 11:24:42 jdolecek Exp $");
72
73 #define SYSVSHM
74
75 #include <sys/param.h>
76 #include <sys/kernel.h>
77 #include <sys/shm.h>
78 #include <sys/malloc.h>
79 #include <sys/mman.h>
80 #include <sys/stat.h>
81 #include <sys/sysctl.h>
82 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
83 #include <sys/sa.h>
84 #include <sys/syscallargs.h>
85 #include <sys/queue.h>
86 #include <sys/pool.h>
87
88 #include <uvm/uvm_extern.h>
89 #include <uvm/uvm_object.h>
90
91 struct shmid_ds *shm_find_segment_by_shmid(int);
92
93 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
94
95 /*
96 * Provides the following externally accessible functions:
97 *
98 * shminit(void); initialization
99 * shmexit(struct vmspace *) cleanup
100 * shmfork(struct vmspace *, struct vmspace *) fork handling
101 *
102 * Structures:
103 * shmsegs (an array of 'struct shmid_ds')
104 * per proc array of 'struct shmmap_state'
105 */
106
107 #define SHMSEG_FREE 0x0200
108 #define SHMSEG_REMOVED 0x0400
109 #define SHMSEG_ALLOCATED 0x0800
110 #define SHMSEG_WANTED 0x1000
111 #define SHMSEG_RMLINGER 0x2000
112
113 static int shm_last_free, shm_nused, shm_committed;
114 struct shmid_ds *shmsegs;
115
116 struct shmmap_entry {
117 SLIST_ENTRY(shmmap_entry) next;
118 vaddr_t va;
119 int shmid;
120 };
121
122 static POOL_INIT(shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
123 "shmmp", &pool_allocator_nointr);
124
125 struct shmmap_state {
126 unsigned int nitems;
127 unsigned int nrefs;
128 SLIST_HEAD(, shmmap_entry) entries;
129 };
130
131 static int shm_find_segment_by_key(key_t);
132 static void shm_deallocate_segment(struct shmid_ds *);
133 static void shm_delete_mapping(struct vmspace *, struct shmmap_state *,
134 struct shmmap_entry *);
135 static int shmget_existing(struct proc *, struct sys_shmget_args *,
136 int, int, register_t *);
137 static int shmget_allocate_segment(struct proc *, struct sys_shmget_args *,
138 int, register_t *);
139 static struct shmmap_state *shmmap_getprivate(struct proc *);
140 static struct shmmap_entry *shm_find_mapping(struct shmmap_state *, vaddr_t);
141
142 static int
143 shm_find_segment_by_key(key)
144 key_t key;
145 {
146 int i;
147
148 for (i = 0; i < shminfo.shmmni; i++)
149 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
150 shmsegs[i].shm_perm._key == key)
151 return i;
152 return -1;
153 }
154
155 struct shmid_ds *
156 shm_find_segment_by_shmid(shmid)
157 int shmid;
158 {
159 int segnum;
160 struct shmid_ds *shmseg;
161
162 segnum = IPCID_TO_IX(shmid);
163 if (segnum < 0 || segnum >= shminfo.shmmni)
164 return NULL;
165 shmseg = &shmsegs[segnum];
166 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
167 return NULL;
168 if ((shmseg->shm_perm.mode & (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED)
169 return NULL;
170 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
171 return NULL;
172 return shmseg;
173 }
174
175 static void
176 shm_deallocate_segment(shmseg)
177 struct shmid_ds *shmseg;
178 {
179 struct uvm_object *uobj = shmseg->_shm_internal;
180 size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
181
182 (*uobj->pgops->pgo_detach)(uobj);
183 shmseg->_shm_internal = NULL;
184 shm_committed -= btoc(size);
185 shmseg->shm_perm.mode = SHMSEG_FREE;
186 shm_nused--;
187 }
188
189 static void
190 shm_delete_mapping(vm, shmmap_s, shmmap_se)
191 struct vmspace *vm;
192 struct shmmap_state *shmmap_s;
193 struct shmmap_entry *shmmap_se;
194 {
195 struct shmid_ds *shmseg;
196 int segnum;
197 size_t size;
198
199 segnum = IPCID_TO_IX(shmmap_se->shmid);
200 #ifdef DEBUG
201 if (segnum < 0 || segnum >= shminfo.shmmni)
202 panic("shm_delete_mapping: vmspace %p state %p entry %p - "
203 "entry segment ID bad (%d)",
204 vm, shmmap_s, shmmap_se, segnum);
205 #endif
206 shmseg = &shmsegs[segnum];
207 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
208 uvm_deallocate(&vm->vm_map, shmmap_se->va, size);
209 SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
210 shmmap_s->nitems--;
211 pool_put(&shmmap_entry_pool, shmmap_se);
212 shmseg->shm_dtime = time.tv_sec;
213 if ((--shmseg->shm_nattch <= 0) &&
214 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
215 shm_deallocate_segment(shmseg);
216 shm_last_free = segnum;
217 }
218 }
219
220 /*
221 * Get a non-shared shm map for that vmspace.
222 * 3 cases:
223 * - no shm map present: create a fresh one
224 * - a shm map with refcount=1, just used by ourselves: fine
225 * - a shared shm map: copy to a fresh one and adjust refcounts
226 */
227 static struct shmmap_state *
228 shmmap_getprivate(struct proc *p)
229 {
230 struct shmmap_state *oshmmap_s, *shmmap_s;
231 struct shmmap_entry *oshmmap_se, *shmmap_se;
232
233 oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
234 if (oshmmap_s && oshmmap_s->nrefs == 1)
235 return (oshmmap_s);
236
237 shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK);
238 memset(shmmap_s, 0, sizeof(struct shmmap_state));
239 shmmap_s->nrefs = 1;
240 SLIST_INIT(&shmmap_s->entries);
241 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
242
243 if (!oshmmap_s)
244 return (shmmap_s);
245
246 #ifdef SHMDEBUG
247 printf("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
248 p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs);
249 #endif
250 SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
251 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
252 shmmap_se->va = oshmmap_se->va;
253 shmmap_se->shmid = oshmmap_se->shmid;
254 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
255 }
256 shmmap_s->nitems = oshmmap_s->nitems;
257 oshmmap_s->nrefs--;
258 return (shmmap_s);
259 }
260
261 static struct shmmap_entry *
262 shm_find_mapping(map, va)
263 struct shmmap_state *map;
264 vaddr_t va;
265 {
266 struct shmmap_entry *shmmap_se;
267
268 SLIST_FOREACH(shmmap_se, &map->entries, next) {
269 if (shmmap_se->va == va)
270 return shmmap_se;
271 }
272 return 0;
273 }
274
275 int
276 sys_shmdt(l, v, retval)
277 struct lwp *l;
278 void *v;
279 register_t *retval;
280 {
281 struct sys_shmdt_args /* {
282 syscallarg(const void *) shmaddr;
283 } */ *uap = v;
284 struct proc *p = l->l_proc;
285 struct shmmap_state *shmmap_s, *shmmap_s1;
286 struct shmmap_entry *shmmap_se;
287
288 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
289 if (shmmap_s == NULL)
290 return EINVAL;
291
292 shmmap_se = shm_find_mapping(shmmap_s, (vaddr_t)SCARG(uap, shmaddr));
293 if (!shmmap_se)
294 return EINVAL;
295
296 shmmap_s1 = shmmap_getprivate(p);
297 if (shmmap_s1 != shmmap_s) {
298 /* map has been copied, lookup entry in new map */
299 shmmap_se = shm_find_mapping(shmmap_s1,
300 (vaddr_t)SCARG(uap, shmaddr));
301 KASSERT(shmmap_se != NULL);
302 }
303 #ifdef SHMDEBUG
304 printf("shmdt: vm %p: remove %d @%lx\n",
305 p->p_vmspace, shmmap_se->shmid, shmmap_se->va);
306 #endif
307 shm_delete_mapping(p->p_vmspace, shmmap_s1, shmmap_se);
308 return 0;
309 }
310
311 int
312 sys_shmat(l, v, retval)
313 struct lwp *l;
314 void *v;
315 register_t *retval;
316 {
317 struct sys_shmat_args /* {
318 syscallarg(int) shmid;
319 syscallarg(const void *) shmaddr;
320 syscallarg(int) shmflg;
321 } */ *uap = v;
322 int error, flags;
323 struct proc *p = l->l_proc;
324 struct ucred *cred = p->p_ucred;
325 struct shmid_ds *shmseg;
326 struct shmmap_state *shmmap_s;
327 struct uvm_object *uobj;
328 vaddr_t attach_va;
329 vm_prot_t prot;
330 vsize_t size;
331 struct shmmap_entry *shmmap_se;
332
333 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
334 if (shmseg == NULL)
335 return EINVAL;
336 error = ipcperm(cred, &shmseg->shm_perm,
337 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
338 if (error)
339 return error;
340
341 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
342 if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg)
343 return EMFILE;
344
345 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
346 prot = VM_PROT_READ;
347 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
348 prot |= VM_PROT_WRITE;
349 flags = MAP_ANON | MAP_SHARED;
350 if (SCARG(uap, shmaddr)) {
351 flags |= MAP_FIXED;
352 if (SCARG(uap, shmflg) & SHM_RND)
353 attach_va =
354 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
355 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
356 attach_va = (vaddr_t)SCARG(uap, shmaddr);
357 else
358 return EINVAL;
359 } else {
360 /* This is just a hint to uvm_mmap() about where to put it. */
361 attach_va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size);
362 }
363 uobj = shmseg->_shm_internal;
364 (*uobj->pgops->pgo_reference)(uobj);
365 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
366 uobj, 0, 0,
367 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
368 if (error) {
369 (*uobj->pgops->pgo_detach)(uobj);
370 return error;
371 }
372 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
373 shmmap_se->va = attach_va;
374 shmmap_se->shmid = SCARG(uap, shmid);
375 shmmap_s = shmmap_getprivate(p);
376 #ifdef SHMDEBUG
377 printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmid, attach_va);
378 #endif
379 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
380 shmmap_s->nitems++;
381 shmseg->shm_lpid = p->p_pid;
382 shmseg->shm_atime = time.tv_sec;
383 shmseg->shm_nattch++;
384
385 retval[0] = attach_va;
386 return 0;
387 }
388
389 int
390 sys___shmctl13(l, v, retval)
391 struct lwp *l;
392 void *v;
393 register_t *retval;
394 {
395 struct sys___shmctl13_args /* {
396 syscallarg(int) shmid;
397 syscallarg(int) cmd;
398 syscallarg(struct shmid_ds *) buf;
399 } */ *uap = v;
400 struct proc *p = l->l_proc;
401 struct shmid_ds shmbuf;
402 int cmd, error;
403
404 cmd = SCARG(uap, cmd);
405
406 if (cmd == IPC_SET) {
407 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
408 if (error)
409 return (error);
410 }
411
412 error = shmctl1(p, SCARG(uap, shmid), cmd,
413 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
414
415 if (error == 0 && cmd == IPC_STAT)
416 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
417
418 return (error);
419 }
420
421 int
422 shmctl1(p, shmid, cmd, shmbuf)
423 struct proc *p;
424 int shmid;
425 int cmd;
426 struct shmid_ds *shmbuf;
427 {
428 struct ucred *cred = p->p_ucred;
429 struct shmid_ds *shmseg;
430 int error = 0;
431
432 shmseg = shm_find_segment_by_shmid(shmid);
433 if (shmseg == NULL)
434 return EINVAL;
435 switch (cmd) {
436 case IPC_STAT:
437 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
438 return error;
439 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
440 break;
441 case IPC_SET:
442 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
443 return error;
444 shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
445 shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
446 shmseg->shm_perm.mode =
447 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
448 (shmbuf->shm_perm.mode & ACCESSPERMS);
449 shmseg->shm_ctime = time.tv_sec;
450 break;
451 case IPC_RMID:
452 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
453 return error;
454 shmseg->shm_perm._key = IPC_PRIVATE;
455 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
456 if (shmseg->shm_nattch <= 0) {
457 shm_deallocate_segment(shmseg);
458 shm_last_free = IPCID_TO_IX(shmid);
459 }
460 break;
461 case SHM_LOCK:
462 case SHM_UNLOCK:
463 default:
464 return EINVAL;
465 }
466 return 0;
467 }
468
469 static int
470 shmget_existing(p, uap, mode, segnum, retval)
471 struct proc *p;
472 struct sys_shmget_args /* {
473 syscallarg(key_t) key;
474 syscallarg(size_t) size;
475 syscallarg(int) shmflg;
476 } */ *uap;
477 int mode;
478 int segnum;
479 register_t *retval;
480 {
481 struct shmid_ds *shmseg;
482 struct ucred *cred = p->p_ucred;
483 int error;
484
485 shmseg = &shmsegs[segnum];
486 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
487 /*
488 * This segment is in the process of being allocated. Wait
489 * until it's done, and look the key up again (in case the
490 * allocation failed or it was freed).
491 */
492 shmseg->shm_perm.mode |= SHMSEG_WANTED;
493 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
494 if (error)
495 return error;
496 return EAGAIN;
497 }
498 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
499 return error;
500 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
501 return EINVAL;
502 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
503 (IPC_CREAT | IPC_EXCL))
504 return EEXIST;
505 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
506 return 0;
507 }
508
509 static int
510 shmget_allocate_segment(p, uap, mode, retval)
511 struct proc *p;
512 struct sys_shmget_args /* {
513 syscallarg(key_t) key;
514 syscallarg(size_t) size;
515 syscallarg(int) shmflg;
516 } */ *uap;
517 int mode;
518 register_t *retval;
519 {
520 int i, segnum, shmid, size;
521 struct ucred *cred = p->p_ucred;
522 struct shmid_ds *shmseg;
523 int error = 0;
524
525 if (SCARG(uap, size) < shminfo.shmmin ||
526 SCARG(uap, size) > shminfo.shmmax)
527 return EINVAL;
528 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
529 return ENOSPC;
530 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
531 if (shm_committed + btoc(size) > shminfo.shmall)
532 return ENOMEM;
533 if (shm_last_free < 0) {
534 for (i = 0; i < shminfo.shmmni; i++)
535 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
536 break;
537 if (i == shminfo.shmmni)
538 panic("shmseg free count inconsistent");
539 segnum = i;
540 } else {
541 segnum = shm_last_free;
542 shm_last_free = -1;
543 }
544 shmseg = &shmsegs[segnum];
545 /*
546 * In case we sleep in malloc(), mark the segment present but deleted
547 * so that noone else tries to create the same key.
548 */
549 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
550 shmseg->shm_perm._key = SCARG(uap, key);
551 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
552 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
553
554 shmseg->_shm_internal = uao_create(size, 0);
555
556 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
557 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
558 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
559 (mode & (ACCESSPERMS|SHMSEG_RMLINGER)) | SHMSEG_ALLOCATED;
560 shmseg->shm_segsz = SCARG(uap, size);
561 shmseg->shm_cpid = p->p_pid;
562 shmseg->shm_lpid = shmseg->shm_nattch = 0;
563 shmseg->shm_atime = shmseg->shm_dtime = 0;
564 shmseg->shm_ctime = time.tv_sec;
565 shm_committed += btoc(size);
566 shm_nused++;
567
568 *retval = shmid;
569 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
570 /*
571 * Somebody else wanted this key while we were asleep. Wake
572 * them up now.
573 */
574 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
575 wakeup((caddr_t)shmseg);
576 }
577 return error;
578 }
579
580 int
581 sys_shmget(l, v, retval)
582 struct lwp *l;
583 void *v;
584 register_t *retval;
585 {
586 struct sys_shmget_args /* {
587 syscallarg(key_t) key;
588 syscallarg(int) size;
589 syscallarg(int) shmflg;
590 } */ *uap = v;
591 struct proc *p = l->l_proc;
592 int segnum, mode, error;
593
594 mode = SCARG(uap, shmflg) & ACCESSPERMS;
595 if (SCARG(uap, shmflg) & _SHM_RMLINGER)
596 mode |= SHMSEG_RMLINGER;
597
598 if (SCARG(uap, key) != IPC_PRIVATE) {
599 again:
600 segnum = shm_find_segment_by_key(SCARG(uap, key));
601 if (segnum >= 0) {
602 error = shmget_existing(p, uap, mode, segnum, retval);
603 if (error == EAGAIN)
604 goto again;
605 return error;
606 }
607 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
608 return ENOENT;
609 }
610 return shmget_allocate_segment(p, uap, mode, retval);
611 }
612
613 void
614 shmfork(vm1, vm2)
615 struct vmspace *vm1, *vm2;
616 {
617 struct shmmap_state *shmmap_s;
618 struct shmmap_entry *shmmap_se;
619
620 vm2->vm_shm = vm1->vm_shm;
621
622 if (vm1->vm_shm == NULL)
623 return;
624
625 #ifdef SHMDEBUG
626 printf("shmfork %p->%p\n", vm1, vm2);
627 #endif
628
629 shmmap_s = (struct shmmap_state *)vm1->vm_shm;
630
631 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
632 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
633 shmmap_s->nrefs++;
634 }
635
636 void
637 shmexit(vm)
638 struct vmspace *vm;
639 {
640 struct shmmap_state *shmmap_s;
641 struct shmmap_entry *shmmap_se;
642
643 shmmap_s = (struct shmmap_state *)vm->vm_shm;
644 if (shmmap_s == NULL)
645 return;
646
647 vm->vm_shm = NULL;
648
649 if (--shmmap_s->nrefs > 0) {
650 #ifdef SHMDEBUG
651 printf("shmexit: vm %p drop ref (%d entries), now used by %d\n",
652 vm, shmmap_s->nitems, shmmap_s->nrefs);
653 #endif
654 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
655 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
656 return;
657 }
658
659 #ifdef SHMDEBUG
660 printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
661 #endif
662 while (!SLIST_EMPTY(&shmmap_s->entries)) {
663 shmmap_se = SLIST_FIRST(&shmmap_s->entries);
664 shm_delete_mapping(vm, shmmap_s, shmmap_se);
665 }
666 KASSERT(shmmap_s->nitems == 0);
667 free(shmmap_s, M_SHM);
668 }
669
670 void
671 shminit()
672 {
673 int i, sz;
674 vaddr_t v;
675
676 /* Allocate pageable memory for our structures */
677 sz = shminfo.shmmni * sizeof(struct shmid_ds);
678 if ((v = uvm_km_alloc(kernel_map, round_page(sz))) == 0)
679 panic("sysv_shm: cannot allocate memory");
680 shmsegs = (void *)v;
681
682 shminfo.shmmax *= PAGE_SIZE;
683
684 for (i = 0; i < shminfo.shmmni; i++) {
685 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
686 shmsegs[i].shm_perm._seq = 0;
687 }
688 shm_last_free = 0;
689 shm_nused = 0;
690 shm_committed = 0;
691 }
692