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