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