Deleted Added
sdiff udiff text old ( 79224 ) new ( 82607 )
full compact
1/* $FreeBSD: head/sys/kern/sysv_shm.c 79224 2001-07-04 16:20:28Z dillon $ */
2/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3
4/*
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass and Charles
18 * Hannum.
19 * 4. The names of the authors may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "opt_compat.h"
35#include "opt_rlimit.h"
36#include "opt_sysvipc.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/sysctl.h>
43#include <sys/shm.h>
44#include <sys/proc.h>
45#include <sys/malloc.h>
46#include <sys/mman.h>
47#include <sys/mutex.h>
48#include <sys/stat.h>
49#include <sys/syscall.h>
50#include <sys/sysent.h>
51#include <sys/sysproto.h>
52#include <sys/jail.h>
53
54#include <vm/vm.h>
55#include <vm/vm_param.h>
56#include <vm/pmap.h>
57#include <vm/vm_object.h>
58#include <vm/vm_map.h>
59#include <vm/vm_page.h>
60#include <vm/vm_pager.h>
61
62static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
63
64struct oshmctl_args;
65static int oshmctl __P((struct proc *p, struct oshmctl_args *uap));
66
67static int shmget_allocate_segment __P((struct proc *p,
68 struct shmget_args *uap, int mode));
69static int shmget_existing __P((struct proc *p, struct shmget_args *uap,
70 int mode, int segnum));
71
72/* XXX casting to (sy_call_t *) is bogus, as usual. */
73static sy_call_t *shmcalls[] = {
74 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
75 (sy_call_t *)shmdt, (sy_call_t *)shmget,
76 (sy_call_t *)shmctl
77};
78
79#define SHMSEG_FREE 0x0200
80#define SHMSEG_REMOVED 0x0400
81#define SHMSEG_ALLOCATED 0x0800
82#define SHMSEG_WANTED 0x1000
83
84static int shm_last_free, shm_nused, shm_committed, shmalloced;
85static struct shmid_ds *shmsegs;
86
87struct shm_handle {
88 /* vm_offset_t kva; */
89 vm_object_t shm_object;
90};
91
92struct shmmap_state {
93 vm_offset_t va;
94 int shmid;
95};
96
97static void shm_deallocate_segment __P((struct shmid_ds *));
98static int shm_find_segment_by_key __P((key_t));
99static struct shmid_ds *shm_find_segment_by_shmid __P((int));
100static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
101static void shmrealloc __P((void));
102static void shminit __P((void));
103static int sysvshm_modload __P((struct module *, int, void *));
104static int shmunload __P((void));
105static void shmexit_myhook __P((struct proc *p));
106static void shmfork_myhook __P((struct proc *p1, struct proc *p2));
107static int sysctl_shmsegs __P((SYSCTL_HANDLER_ARGS));
108
109/*
110 * Tuneable values.
111 */
112#ifndef SHMMAXPGS
113#define SHMMAXPGS 8192 /* Note: sysv shared memory is swap backed. */
114#endif
115#ifndef SHMMAX
116#define SHMMAX (SHMMAXPGS*PAGE_SIZE)
117#endif
118#ifndef SHMMIN
119#define SHMMIN 1
120#endif
121#ifndef SHMMNI
122#define SHMMNI 192
123#endif
124#ifndef SHMSEG
125#define SHMSEG 128
126#endif
127#ifndef SHMALL
128#define SHMALL (SHMMAXPGS)
129#endif
130
131struct shminfo shminfo = {
132 SHMMAX,
133 SHMMIN,
134 SHMMNI,
135 SHMSEG,
136 SHMALL
137};
138
139static int shm_use_phys;
140
141SYSCTL_DECL(_kern_ipc);
142SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
143SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
144SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
145SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RD, &shminfo.shmseg, 0, "");
146SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
147SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
148 &shm_use_phys, 0, "");
149SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
150 NULL, 0, sysctl_shmsegs, "", "");
151
152static int
153shm_find_segment_by_key(key)
154 key_t key;
155{
156 int i;
157
158 for (i = 0; i < shmalloced; i++)
159 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
160 shmsegs[i].shm_perm.key == key)
161 return i;
162 return -1;
163}
164
165static struct shmid_ds *
166shm_find_segment_by_shmid(shmid)
167 int shmid;
168{
169 int segnum;
170 struct shmid_ds *shmseg;
171
172 segnum = IPCID_TO_IX(shmid);
173 if (segnum < 0 || segnum >= shmalloced)
174 return NULL;
175 shmseg = &shmsegs[segnum];
176 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
177 != SHMSEG_ALLOCATED ||
178 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
179 return NULL;
180 return shmseg;
181}
182
183static void
184shm_deallocate_segment(shmseg)
185 struct shmid_ds *shmseg;
186{
187 struct shm_handle *shm_handle;
188 size_t size;
189
190 GIANT_REQUIRED;
191
192 shm_handle = shmseg->shm_internal;
193 vm_object_deallocate(shm_handle->shm_object);
194 free((caddr_t)shm_handle, M_SHM);
195 shmseg->shm_internal = NULL;
196 size = round_page(shmseg->shm_segsz);
197 shm_committed -= btoc(size);
198 shm_nused--;
199 shmseg->shm_perm.mode = SHMSEG_FREE;
200}
201
202static int
203shm_delete_mapping(p, shmmap_s)
204 struct proc *p;
205 struct shmmap_state *shmmap_s;
206{
207 struct shmid_ds *shmseg;
208 int segnum, result;
209 size_t size;
210
211 GIANT_REQUIRED;
212
213 segnum = IPCID_TO_IX(shmmap_s->shmid);
214 shmseg = &shmsegs[segnum];
215 size = round_page(shmseg->shm_segsz);
216 result = vm_map_remove(&p->p_vmspace->vm_map, shmmap_s->va,
217 shmmap_s->va + size);
218 if (result != KERN_SUCCESS)
219 return EINVAL;
220 shmmap_s->shmid = -1;
221 shmseg->shm_dtime = time_second;
222 if ((--shmseg->shm_nattch <= 0) &&
223 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
224 shm_deallocate_segment(shmseg);
225 shm_last_free = segnum;
226 }
227 return 0;
228}
229
230#ifndef _SYS_SYSPROTO_H_
231struct shmdt_args {
232 void *shmaddr;
233};
234#endif
235
236int
237shmdt(p, uap)
238 struct proc *p;
239 struct shmdt_args *uap;
240{
241 struct shmmap_state *shmmap_s;
242 int i;
243 int error;
244
245 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
246 return (ENOSYS);
247
248 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
249 if (shmmap_s == NULL)
250 return EINVAL;
251 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
252 if (shmmap_s->shmid != -1 &&
253 shmmap_s->va == (vm_offset_t)uap->shmaddr)
254 break;
255 if (i == shminfo.shmseg)
256 return EINVAL;
257 error = shm_delete_mapping(p, shmmap_s);
258 return error;
259}
260
261#ifndef _SYS_SYSPROTO_H_
262struct shmat_args {
263 int shmid;
264 void *shmaddr;
265 int shmflg;
266};
267#endif
268
269int
270shmat(p, uap)
271 struct proc *p;
272 struct shmat_args *uap;
273{
274 int error, i, flags;
275 struct shmid_ds *shmseg;
276 struct shmmap_state *shmmap_s = NULL;
277 struct shm_handle *shm_handle;
278 vm_offset_t attach_va;
279 vm_prot_t prot;
280 vm_size_t size;
281 int rv;
282
283 GIANT_REQUIRED;
284
285 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
286 return (ENOSYS);
287
288 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
289 if (shmmap_s == NULL) {
290 size = shminfo.shmseg * sizeof(struct shmmap_state);
291 shmmap_s = malloc(size, M_SHM, M_WAITOK);
292 for (i = 0; i < shminfo.shmseg; i++)
293 shmmap_s[i].shmid = -1;
294 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
295 }
296 shmseg = shm_find_segment_by_shmid(uap->shmid);
297 if (shmseg == NULL)
298 return EINVAL;
299 error = ipcperm(p, &shmseg->shm_perm,
300 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
301 if (error)
302 return error;
303 for (i = 0; i < shminfo.shmseg; i++) {
304 if (shmmap_s->shmid == -1)
305 break;
306 shmmap_s++;
307 }
308 if (i >= shminfo.shmseg)
309 return EMFILE;
310 size = round_page(shmseg->shm_segsz);
311#ifdef VM_PROT_READ_IS_EXEC
312 prot = VM_PROT_READ | VM_PROT_EXECUTE;
313#else
314 prot = VM_PROT_READ;
315#endif
316 if ((uap->shmflg & SHM_RDONLY) == 0)
317 prot |= VM_PROT_WRITE;
318 flags = MAP_ANON | MAP_SHARED;
319 if (uap->shmaddr) {
320 flags |= MAP_FIXED;
321 if (uap->shmflg & SHM_RND)
322 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
323 else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
324 attach_va = (vm_offset_t)uap->shmaddr;
325 else
326 return EINVAL;
327 } else {
328 /*
329 * This is just a hint to vm_map_find() about where to
330 * put it.
331 */
332 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
333 + MAXTSIZ + MAXDSIZ);
334 }
335
336 shm_handle = shmseg->shm_internal;
337 vm_object_reference(shm_handle->shm_object);
338 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
339 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
340 if (rv != KERN_SUCCESS) {
341 return ENOMEM;
342 }
343 vm_map_inherit(&p->p_vmspace->vm_map,
344 attach_va, attach_va + size, VM_INHERIT_SHARE);
345
346 shmmap_s->va = attach_va;
347 shmmap_s->shmid = uap->shmid;
348 shmseg->shm_lpid = p->p_pid;
349 shmseg->shm_atime = time_second;
350 shmseg->shm_nattch++;
351 p->p_retval[0] = attach_va;
352 return 0;
353}
354
355struct oshmid_ds {
356 struct ipc_perm shm_perm; /* operation perms */
357 int shm_segsz; /* size of segment (bytes) */
358 ushort shm_cpid; /* pid, creator */
359 ushort shm_lpid; /* pid, last operation */
360 short shm_nattch; /* no. of current attaches */
361 time_t shm_atime; /* last attach time */
362 time_t shm_dtime; /* last detach time */
363 time_t shm_ctime; /* last change time */
364 void *shm_handle; /* internal handle for shm segment */
365};
366
367struct oshmctl_args {
368 int shmid;
369 int cmd;
370 struct oshmid_ds *ubuf;
371};
372
373static int
374oshmctl(p, uap)
375 struct proc *p;
376 struct oshmctl_args *uap;
377{
378#ifdef COMPAT_43
379 int error;
380 struct shmid_ds *shmseg;
381 struct oshmid_ds outbuf;
382
383 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
384 return (ENOSYS);
385
386 shmseg = shm_find_segment_by_shmid(uap->shmid);
387 if (shmseg == NULL)
388 return EINVAL;
389 switch (uap->cmd) {
390 case IPC_STAT:
391 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
392 if (error)
393 return error;
394 outbuf.shm_perm = shmseg->shm_perm;
395 outbuf.shm_segsz = shmseg->shm_segsz;
396 outbuf.shm_cpid = shmseg->shm_cpid;
397 outbuf.shm_lpid = shmseg->shm_lpid;
398 outbuf.shm_nattch = shmseg->shm_nattch;
399 outbuf.shm_atime = shmseg->shm_atime;
400 outbuf.shm_dtime = shmseg->shm_dtime;
401 outbuf.shm_ctime = shmseg->shm_ctime;
402 outbuf.shm_handle = shmseg->shm_internal;
403 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
404 if (error)
405 return error;
406 break;
407 default:
408 /* XXX casting to (sy_call_t *) is bogus, as usual. */
409 return ((sy_call_t *)shmctl)(p, uap);
410 }
411 return 0;
412#else
413 return EINVAL;
414#endif
415}
416
417#ifndef _SYS_SYSPROTO_H_
418struct shmctl_args {
419 int shmid;
420 int cmd;
421 struct shmid_ds *buf;
422};
423#endif
424
425int
426shmctl(p, uap)
427 struct proc *p;
428 struct shmctl_args *uap;
429{
430 int error;
431 struct shmid_ds inbuf;
432 struct shmid_ds *shmseg;
433
434 GIANT_REQUIRED;
435
436 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
437 return (ENOSYS);
438
439 shmseg = shm_find_segment_by_shmid(uap->shmid);
440 if (shmseg == NULL)
441 return EINVAL;
442 switch (uap->cmd) {
443 case IPC_STAT:
444 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
445 if (error)
446 return error;
447 error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
448 if (error)
449 return error;
450 break;
451 case IPC_SET:
452 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
453 if (error)
454 return error;
455 error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
456 if (error)
457 return error;
458 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
459 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
460 shmseg->shm_perm.mode =
461 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
462 (inbuf.shm_perm.mode & ACCESSPERMS);
463 shmseg->shm_ctime = time_second;
464 break;
465 case IPC_RMID:
466 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
467 if (error)
468 return error;
469 shmseg->shm_perm.key = IPC_PRIVATE;
470 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
471 if (shmseg->shm_nattch <= 0) {
472 shm_deallocate_segment(shmseg);
473 shm_last_free = IPCID_TO_IX(uap->shmid);
474 }
475 break;
476#if 0
477 case SHM_LOCK:
478 case SHM_UNLOCK:
479#endif
480 default:
481 return EINVAL;
482 }
483 return 0;
484}
485
486#ifndef _SYS_SYSPROTO_H_
487struct shmget_args {
488 key_t key;
489 size_t size;
490 int shmflg;
491};
492#endif
493
494static int
495shmget_existing(p, uap, mode, segnum)
496 struct proc *p;
497 struct shmget_args *uap;
498 int mode;
499 int segnum;
500{
501 struct shmid_ds *shmseg;
502 int error;
503
504 shmseg = &shmsegs[segnum];
505 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
506 /*
507 * This segment is in the process of being allocated. Wait
508 * until it's done, and look the key up again (in case the
509 * allocation failed or it was freed).
510 */
511 shmseg->shm_perm.mode |= SHMSEG_WANTED;
512 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
513 if (error)
514 return error;
515 return EAGAIN;
516 }
517 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
518 return EEXIST;
519 error = ipcperm(p, &shmseg->shm_perm, mode);
520 if (error)
521 return error;
522 if (uap->size && uap->size > shmseg->shm_segsz)
523 return EINVAL;
524 p->p_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
525 return 0;
526}
527
528static int
529shmget_allocate_segment(p, uap, mode)
530 struct proc *p;
531 struct shmget_args *uap;
532 int mode;
533{
534 int i, segnum, shmid, size;
535 struct ucred *cred = p->p_ucred;
536 struct shmid_ds *shmseg;
537 struct shm_handle *shm_handle;
538
539 GIANT_REQUIRED;
540
541 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
542 return EINVAL;
543 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
544 return ENOSPC;
545 size = round_page(uap->size);
546 if (shm_committed + btoc(size) > shminfo.shmall)
547 return ENOMEM;
548 if (shm_last_free < 0) {
549 shmrealloc(); /* Maybe expand the shmsegs[] array. */
550 for (i = 0; i < shmalloced; i++)
551 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
552 break;
553 if (i == shmalloced)
554 return ENOSPC;
555 segnum = i;
556 } else {
557 segnum = shm_last_free;
558 shm_last_free = -1;
559 }
560 shmseg = &shmsegs[segnum];
561 /*
562 * In case we sleep in malloc(), mark the segment present but deleted
563 * so that noone else tries to create the same key.
564 */
565 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
566 shmseg->shm_perm.key = uap->key;
567 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
568 shm_handle = (struct shm_handle *)
569 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
570 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
571
572 /*
573 * We make sure that we have allocated a pager before we need
574 * to.
575 */
576 if (shm_use_phys) {
577 shm_handle->shm_object =
578 vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
579 } else {
580 shm_handle->shm_object =
581 vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
582 }
583 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
584 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
585
586 shmseg->shm_internal = shm_handle;
587 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
588 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
589 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
590 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
591 shmseg->shm_segsz = uap->size;
592 shmseg->shm_cpid = p->p_pid;
593 shmseg->shm_lpid = shmseg->shm_nattch = 0;
594 shmseg->shm_atime = shmseg->shm_dtime = 0;
595 shmseg->shm_ctime = time_second;
596 shm_committed += btoc(size);
597 shm_nused++;
598 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
599 /*
600 * Somebody else wanted this key while we were asleep. Wake
601 * them up now.
602 */
603 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
604 wakeup((caddr_t)shmseg);
605 }
606 p->p_retval[0] = shmid;
607 return 0;
608}
609
610int
611shmget(p, uap)
612 struct proc *p;
613 struct shmget_args *uap;
614{
615 int segnum, mode, error;
616
617 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
618 return (ENOSYS);
619
620 mode = uap->shmflg & ACCESSPERMS;
621 if (uap->key != IPC_PRIVATE) {
622 again:
623 segnum = shm_find_segment_by_key(uap->key);
624 if (segnum >= 0) {
625 error = shmget_existing(p, uap, mode, segnum);
626 if (error == EAGAIN)
627 goto again;
628 return error;
629 }
630 if ((uap->shmflg & IPC_CREAT) == 0)
631 return ENOENT;
632 }
633 return shmget_allocate_segment(p, uap, mode);
634}
635
636int
637shmsys(p, uap)
638 struct proc *p;
639 /* XXX actually varargs. */
640 struct shmsys_args /* {
641 u_int which;
642 int a2;
643 int a3;
644 int a4;
645 } */ *uap;
646{
647
648 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
649 return (ENOSYS);
650
651 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
652 return EINVAL;
653 return ((*shmcalls[uap->which])(p, &uap->a2));
654}
655
656static void
657shmfork_myhook(p1, p2)
658 struct proc *p1, *p2;
659{
660 struct shmmap_state *shmmap_s;
661 size_t size;
662 int i;
663
664 size = shminfo.shmseg * sizeof(struct shmmap_state);
665 shmmap_s = malloc(size, M_SHM, M_WAITOK);
666 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
667 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
668 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
669 if (shmmap_s->shmid != -1)
670 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
671}
672
673static void
674shmexit_myhook(p)
675 struct proc *p;
676{
677 struct shmmap_state *shmmap_s;
678 int i;
679
680 GIANT_REQUIRED;
681
682 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
683 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
684 if (shmmap_s->shmid != -1)
685 shm_delete_mapping(p, shmmap_s);
686 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
687 p->p_vmspace->vm_shm = NULL;
688}
689
690static void
691shmrealloc(void)
692{
693 int i;
694 struct shmid_ds *newsegs;
695
696 if (shmalloced >= shminfo.shmmni)
697 return;
698
699 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
700 if (newsegs == NULL)
701 return;
702 for (i = 0; i < shmalloced; i++)
703 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
704 for (; i < shminfo.shmmni; i++) {
705 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
706 shmsegs[i].shm_perm.seq = 0;
707 }
708 free(shmsegs, M_SHM);
709 shmsegs = newsegs;
710 shmalloced = shminfo.shmmni;
711}
712
713static void
714shminit()
715{
716 int i;
717
718 shmalloced = shminfo.shmmni;
719 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
720 if (shmsegs == NULL)
721 panic("cannot allocate initial memory for sysvshm");
722 for (i = 0; i < shmalloced; i++) {
723 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
724 shmsegs[i].shm_perm.seq = 0;
725 }
726 shm_last_free = 0;
727 shm_nused = 0;
728 shm_committed = 0;
729 shmexit_hook = &shmexit_myhook;
730 shmfork_hook = &shmfork_myhook;
731}
732
733static int
734shmunload()
735{
736
737 if (shm_nused > 0)
738 return (EBUSY);
739
740 free(shmsegs, M_SHM);
741 shmexit_hook = NULL;
742 shmfork_hook = NULL;
743 return (0);
744}
745
746static int
747sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
748{
749
750 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
751}
752
753static int
754sysvshm_modload(struct module *module, int cmd, void *arg)
755{
756 int error = 0;
757
758 switch (cmd) {
759 case MOD_LOAD:
760 shminit();
761 break;
762 case MOD_UNLOAD:
763 error = shmunload();
764 break;
765 case MOD_SHUTDOWN:
766 break;
767 default:
768 error = EINVAL;
769 break;
770 }
771 return (error);
772}
773
774static moduledata_t sysvshm_mod = {
775 "sysvshm",
776 &sysvshm_modload,
777 NULL
778};
779
780SYSCALL_MODULE_HELPER(shmsys, 4);
781SYSCALL_MODULE_HELPER(shmat, 3);
782SYSCALL_MODULE_HELPER(shmctl, 3);
783SYSCALL_MODULE_HELPER(shmdt, 1);
784SYSCALL_MODULE_HELPER(shmget, 3);
785
786DECLARE_MODULE(sysvshm, sysvshm_mod,
787 SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
788MODULE_VERSION(sysvshm, 1);