kernel - use new td_ucred in numerous places
[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.21 2008/01/06 16:55:51 swildner 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 sys_oshmctl (struct proc *p, struct oshmctl_args *uap);
64
65 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
66 static int shmget_existing (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 *)sys_shmat, (sy_call_t *)sys_oshmctl,
71         (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
72         (sy_call_t *)sys_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 (struct shmid_ds *);
94 static int shm_find_segment_by_key (key_t);
95 static struct shmid_ds *shm_find_segment_by_shmid (int);
96 static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *);
97 static void shmrealloc (void);
98 static void shminit (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_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
139 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
140 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
141 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, "");
144
145 static int
146 shm_find_segment_by_key(key_t key)
147 {
148         int i;
149
150         for (i = 0; i < shmalloced; i++)
151                 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
152                     shmsegs[i].shm_perm.key == key)
153                         return i;
154         return -1;
155 }
156
157 static struct shmid_ds *
158 shm_find_segment_by_shmid(int shmid)
159 {
160         int segnum;
161         struct shmid_ds *shmseg;
162
163         segnum = IPCID_TO_IX(shmid);
164         if (segnum < 0 || segnum >= shmalloced)
165                 return NULL;
166         shmseg = &shmsegs[segnum];
167         if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
168             != SHMSEG_ALLOCATED ||
169             shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
170                 return NULL;
171         return shmseg;
172 }
173
174 static void
175 shm_deallocate_segment(struct shmid_ds *shmseg)
176 {
177         struct shm_handle *shm_handle;
178         size_t size;
179
180         shm_handle = shmseg->shm_internal;
181         vm_object_deallocate(shm_handle->shm_object);
182         kfree((caddr_t)shm_handle, M_SHM);
183         shmseg->shm_internal = NULL;
184         size = round_page(shmseg->shm_segsz);
185         shm_committed -= btoc(size);
186         shm_nused--;
187         shmseg->shm_perm.mode = SHMSEG_FREE;
188 }
189
190 static int
191 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
192 {
193         struct shmid_ds *shmseg;
194         int segnum, result;
195         size_t size;
196
197         segnum = IPCID_TO_IX(shmmap_s->shmid);
198         shmseg = &shmsegs[segnum];
199         size = round_page(shmseg->shm_segsz);
200         result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
201         if (result != KERN_SUCCESS)
202                 return EINVAL;
203         shmmap_s->shmid = -1;
204         shmseg->shm_dtime = time_second;
205         if ((--shmseg->shm_nattch <= 0) &&
206             (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
207                 shm_deallocate_segment(shmseg);
208                 shm_last_free = segnum;
209         }
210         return 0;
211 }
212
213 /*
214  * MPALMOSTSAFE
215  */
216 int
217 sys_shmdt(struct shmdt_args *uap)
218 {
219         struct thread *td = curthread;
220         struct proc *p = td->td_proc;
221         struct shmmap_state *shmmap_s;
222         int i;
223         int error;
224
225         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
226                 return (ENOSYS);
227
228         get_mplock();
229         shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
230         if (shmmap_s == NULL) {
231                 error = EINVAL;
232                 goto done;
233         }
234         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
235                 if (shmmap_s->shmid != -1 &&
236                     shmmap_s->va == (vm_offset_t)uap->shmaddr)
237                         break;
238         }
239         if (i == shminfo.shmseg)
240                 error = EINVAL;
241         else
242                 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
243 done:
244         rel_mplock();
245         return (error);
246 }
247
248 /*
249  * MPALMOSTSAFE
250  */
251 int
252 sys_shmat(struct shmat_args *uap)
253 {
254         struct thread *td = curthread;
255         struct proc *p = td->td_proc;
256         int error, i, flags;
257         struct shmid_ds *shmseg;
258         struct shmmap_state *shmmap_s = NULL;
259         struct shm_handle *shm_handle;
260         vm_offset_t attach_va;
261         vm_prot_t prot;
262         vm_size_t size;
263         int rv;
264
265         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
266                 return (ENOSYS);
267
268         get_mplock();
269 again:
270         shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
271         if (shmmap_s == NULL) {
272                 size = shminfo.shmseg * sizeof(struct shmmap_state);
273                 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
274                 for (i = 0; i < shminfo.shmseg; i++)
275                         shmmap_s[i].shmid = -1;
276                 if (p->p_vmspace->vm_shm != NULL) {
277                         kfree(shmmap_s, M_SHM);
278                         goto again;
279                 }
280                 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
281         }
282         shmseg = shm_find_segment_by_shmid(uap->shmid);
283         if (shmseg == NULL) {
284                 error = EINVAL;
285                 goto done;
286         }
287         error = ipcperm(p, &shmseg->shm_perm,
288                         (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
289         if (error)
290                 goto done;
291         for (i = 0; i < shminfo.shmseg; i++) {
292                 if (shmmap_s->shmid == -1)
293                         break;
294                 shmmap_s++;
295         }
296         if (i >= shminfo.shmseg) {
297                 error = EMFILE;
298                 goto done;
299         }
300         size = round_page(shmseg->shm_segsz);
301 #ifdef VM_PROT_READ_IS_EXEC
302         prot = VM_PROT_READ | VM_PROT_EXECUTE;
303 #else
304         prot = VM_PROT_READ;
305 #endif
306         if ((uap->shmflg & SHM_RDONLY) == 0)
307                 prot |= VM_PROT_WRITE;
308         flags = MAP_ANON | MAP_SHARED;
309         if (uap->shmaddr) {
310                 flags |= MAP_FIXED;
311                 if (uap->shmflg & SHM_RND) {
312                         attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
313                 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
314                         attach_va = (vm_offset_t)uap->shmaddr;
315                 } else {
316                         error = EINVAL;
317                         goto done;
318                 }
319         } else {
320                 /*
321                  * This is just a hint to vm_map_find() about where to put it.
322                  */
323                 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
324         }
325
326         shm_handle = shmseg->shm_internal;
327         vm_object_reference(shm_handle->shm_object);
328         rv = vm_map_find(&p->p_vmspace->vm_map, 
329                          shm_handle->shm_object, 0,
330                          &attach_va, size,
331                          ((flags & MAP_FIXED) ? 0 : 1), 
332                          VM_MAPTYPE_NORMAL,
333                          prot, prot,
334                          0);
335         if (rv != KERN_SUCCESS) {
336                 vm_object_deallocate(shm_handle->shm_object);
337                 error = ENOMEM;
338                 goto done;
339         }
340         vm_map_inherit(&p->p_vmspace->vm_map,
341                        attach_va, attach_va + size, VM_INHERIT_SHARE);
342
343         KKASSERT(shmmap_s->shmid == -1);
344         shmmap_s->va = attach_va;
345         shmmap_s->shmid = uap->shmid;
346         shmseg->shm_lpid = p->p_pid;
347         shmseg->shm_atime = time_second;
348         shmseg->shm_nattch++;
349         uap->sysmsg_result = attach_va;
350         error = 0;
351 done:
352         rel_mplock();
353         return error;
354 }
355
356 struct oshmid_ds {
357         struct  ipc_perm shm_perm;      /* operation perms */
358         int     shm_segsz;              /* size of segment (bytes) */
359         ushort  shm_cpid;               /* pid, creator */
360         ushort  shm_lpid;               /* pid, last operation */
361         short   shm_nattch;             /* no. of current attaches */
362         time_t  shm_atime;              /* last attach time */
363         time_t  shm_dtime;              /* last detach time */
364         time_t  shm_ctime;              /* last change time */
365         void    *shm_handle;            /* internal handle for shm segment */
366 };
367
368 struct oshmctl_args {
369         struct sysmsg sysmsg;
370         int shmid;
371         int cmd;
372         struct oshmid_ds *ubuf;
373 };
374
375 /*
376  * MPALMOSTSAFE
377  */
378 static int
379 sys_oshmctl(struct proc *p, struct oshmctl_args *uap)
380 {
381 #ifdef COMPAT_43
382         struct thread *td = curthread;
383         struct shmid_ds *shmseg;
384         struct oshmid_ds outbuf;
385         int error;
386
387         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
388                 return (ENOSYS);
389
390         get_mplock();
391         shmseg = shm_find_segment_by_shmid(uap->shmid);
392         if (shmseg == NULL) {
393                 error = EINVAL;
394                 goto done;
395         }
396
397         switch (uap->cmd) {
398         case IPC_STAT:
399                 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
400                 if (error)
401                         break;
402                 outbuf.shm_perm = shmseg->shm_perm;
403                 outbuf.shm_segsz = shmseg->shm_segsz;
404                 outbuf.shm_cpid = shmseg->shm_cpid;
405                 outbuf.shm_lpid = shmseg->shm_lpid;
406                 outbuf.shm_nattch = shmseg->shm_nattch;
407                 outbuf.shm_atime = shmseg->shm_atime;
408                 outbuf.shm_dtime = shmseg->shm_dtime;
409                 outbuf.shm_ctime = shmseg->shm_ctime;
410                 outbuf.shm_handle = shmseg->shm_internal;
411                 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
412                 break;
413         default:
414                 /* XXX casting to (sy_call_t *) is bogus, as usual. */
415                 error = sys_shmctl((struct shmctl_args *)uap);
416         }
417 done:
418         rel_mplock();
419         return error;
420 #else
421         return EINVAL;
422 #endif
423 }
424
425 /*
426  * MPALMOSTSAFE
427  */
428 int
429 sys_shmctl(struct shmctl_args *uap)
430 {
431         struct thread *td = curthread;
432         struct proc *p = td->td_proc;
433         int error;
434         struct shmid_ds inbuf;
435         struct shmid_ds *shmseg;
436
437         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
438                 return (ENOSYS);
439
440         get_mplock();
441         shmseg = shm_find_segment_by_shmid(uap->shmid);
442         if (shmseg == NULL) {
443                 error = EINVAL;
444                 goto done;
445         }
446
447         switch (uap->cmd) {
448         case IPC_STAT:
449                 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
450                 if (error == 0)
451                         error = copyout(shmseg, uap->buf, sizeof(inbuf));
452                 break;
453         case IPC_SET:
454                 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
455                 if (error == 0)
456                         error = copyin(uap->buf, &inbuf, sizeof(inbuf));
457                 if (error == 0) {
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                 }
465                 break;
466         case IPC_RMID:
467                 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
468                 if (error == 0) {
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                 }
476                 break;
477 #if 0
478         case SHM_LOCK:
479         case SHM_UNLOCK:
480 #endif
481         default:
482                 error = EINVAL;
483                 break;
484         }
485 done:
486         rel_mplock();
487         return error;
488 }
489
490 static int
491 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
492 {
493         struct shmid_ds *shmseg;
494         int error;
495
496         shmseg = &shmsegs[segnum];
497         if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
498                 /*
499                  * This segment is in the process of being allocated.  Wait
500                  * until it's done, and look the key up again (in case the
501                  * allocation failed or it was freed).
502                  */
503                 shmseg->shm_perm.mode |= SHMSEG_WANTED;
504                 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
505                 if (error)
506                         return error;
507                 return EAGAIN;
508         }
509         if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
510                 return EEXIST;
511         error = ipcperm(p, &shmseg->shm_perm, mode);
512         if (error)
513                 return error;
514         if (uap->size && uap->size > shmseg->shm_segsz)
515                 return EINVAL;
516         uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
517         return 0;
518 }
519
520 static int
521 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
522 {
523         int i, segnum, shmid, size;
524         struct ucred *cred = p->p_ucred;
525         struct shmid_ds *shmseg;
526         struct shm_handle *shm_handle;
527
528         if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
529                 return EINVAL;
530         if (shm_nused >= shminfo.shmmni) /* any shmids left? */
531                 return ENOSPC;
532         size = round_page(uap->size);
533         if (shm_committed + btoc(size) > shminfo.shmall)
534                 return ENOMEM;
535         if (shm_last_free < 0) {
536                 shmrealloc();   /* maybe expand the shmsegs[] array */
537                 for (i = 0; i < shmalloced; i++)
538                         if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
539                                 break;
540                 if (i == shmalloced)
541                         return ENOSPC;
542                 segnum = i;
543         } else  {
544                 segnum = shm_last_free;
545                 shm_last_free = -1;
546         }
547         shmseg = &shmsegs[segnum];
548         /*
549          * In case we sleep in malloc(), mark the segment present but deleted
550          * so that noone else tries to create the same key.
551          */
552         shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
553         shmseg->shm_perm.key = uap->key;
554         shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
555         shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
556         shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
557         
558         /*
559          * We make sure that we have allocated a pager before we need
560          * to.
561          */
562         if (shm_use_phys) {
563                 shm_handle->shm_object =
564                     vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
565         } else {
566                 shm_handle->shm_object =
567                     vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
568         }
569         vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
570         vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
571
572         shmseg->shm_internal = shm_handle;
573         shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
574         shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
575         shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
576             (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
577         shmseg->shm_segsz = uap->size;
578         shmseg->shm_cpid = p->p_pid;
579         shmseg->shm_lpid = shmseg->shm_nattch = 0;
580         shmseg->shm_atime = shmseg->shm_dtime = 0;
581         shmseg->shm_ctime = time_second;
582         shm_committed += btoc(size);
583         shm_nused++;
584         if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
585                 /*
586                  * Somebody else wanted this key while we were asleep.  Wake
587                  * them up now.
588                  */
589                 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
590                 wakeup((caddr_t)shmseg);
591         }
592         uap->sysmsg_result = shmid;
593         return 0;
594 }
595
596 /*
597  * MPALMOSTSAFE
598  */
599 int
600 sys_shmget(struct shmget_args *uap)
601 {
602         struct thread *td = curthread;
603         struct proc *p = td->td_proc;
604         int segnum, mode, error;
605
606         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
607                 return (ENOSYS);
608
609         mode = uap->shmflg & ACCESSPERMS;
610         get_mplock();
611
612         if (uap->key != IPC_PRIVATE) {
613         again:
614                 segnum = shm_find_segment_by_key(uap->key);
615                 if (segnum >= 0) {
616                         error = shmget_existing(p, uap, mode, segnum);
617                         if (error == EAGAIN)
618                                 goto again;
619                         goto done;
620                 }
621                 if ((uap->shmflg & IPC_CREAT) == 0) {
622                         error = ENOENT;
623                         goto done;
624                 }
625         }
626         error = shmget_allocate_segment(p, uap, mode);
627 done:
628         rel_mplock();
629         return (error);
630 }
631
632 /*
633  * shmsys_args(int which, int a2, ...) (VARARGS)
634  *
635  * MPALMOSTSAFE
636  */
637 int
638 sys_shmsys(struct shmsys_args *uap)
639 {
640         struct thread *td = curthread;
641         unsigned int which = (unsigned int)uap->which;
642         int error;
643
644         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
645                 return (ENOSYS);
646
647         if (which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
648                 return EINVAL;
649         get_mplock();
650         bcopy(&uap->a2, &uap->which,
651                 sizeof(struct shmsys_args) - offsetof(struct shmsys_args, a2));
652         error = ((*shmcalls[which])(uap));
653         rel_mplock();
654
655         return(error);
656 }
657
658 void
659 shmfork(struct proc *p1, struct proc *p2)
660 {
661         struct shmmap_state *shmmap_s;
662         size_t size;
663         int i;
664
665         size = shminfo.shmseg * sizeof(struct shmmap_state);
666         shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
667         bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
668         p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
669         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
670                 if (shmmap_s->shmid != -1)
671                         shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
672 }
673
674 void
675 shmexit(struct vmspace *vm)
676 {
677         struct shmmap_state *base, *shm;
678         int i;
679
680         if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
681                 vm->vm_shm = NULL;
682                 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
683                         if (shm->shmid != -1)
684                                 shm_delete_mapping(vm, shm);
685                 }
686                 kfree(base, M_SHM);
687         }
688 }
689
690 static void
691 shmrealloc(void)
692 {
693         int i;
694         struct shmid_ds *newsegs;
695
696         if (shmalloced >= shminfo.shmmni)
697                 return;
698
699         newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
700         for (i = 0; i < shmalloced; i++)
701                 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
702         for (; i < shminfo.shmmni; i++) {
703                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
704                 shmsegs[i].shm_perm.seq = 0;
705         }
706         kfree(shmsegs, M_SHM);
707         shmsegs = newsegs;
708         shmalloced = shminfo.shmmni;
709 }
710
711 static void
712 shminit(void *dummy)
713 {
714         int i;
715
716         shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
717         shmalloced = shminfo.shmmni;
718         shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
719         for (i = 0; i < shmalloced; i++) {
720                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
721                 shmsegs[i].shm_perm.seq = 0;
722         }
723         shm_last_free = 0;
724         shm_nused = 0;
725         shm_committed = 0;
726 }
727 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);