kernel - use new td_ucred in numerous places
[dragonfly.git] / sys / kern / sysv_shm.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/kern/sysv_shm.c,v 1.45.2.6 2002/10/22 20:45:03 fjoe Exp $ */
978400d3 2/* $DragonFly: src/sys/kern/sysv_shm.c,v 1.21 2008/01/06 16:55:51 swildner Exp $ */
984263bc
MD
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
60static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
61
62struct oshmctl_args;
753fd850 63static int sys_oshmctl (struct proc *p, struct oshmctl_args *uap);
984263bc 64
402ed7e1
RG
65static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
66static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum);
984263bc
MD
67
68/* XXX casting to (sy_call_t *) is bogus, as usual. */
69static sy_call_t *shmcalls[] = {
753fd850
MD
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
984263bc
MD
73};
74
75#define SHMSEG_FREE 0x0200
76#define SHMSEG_REMOVED 0x0400
77#define SHMSEG_ALLOCATED 0x0800
78#define SHMSEG_WANTED 0x1000
79
80static int shm_last_free, shm_nused, shm_committed, shmalloced;
81static struct shmid_ds *shmsegs;
82
83struct shm_handle {
84 /* vm_offset_t kva; */
85 vm_object_t shm_object;
86};
87
88struct shmmap_state {
89 vm_offset_t va;
90 int shmid;
91};
92
402ed7e1
RG
93static void shm_deallocate_segment (struct shmid_ds *);
94static int shm_find_segment_by_key (key_t);
95static struct shmid_ds *shm_find_segment_by_shmid (int);
96static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *);
97static void shmrealloc (void);
98static void shminit (void *);
984263bc
MD
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
122struct shminfo shminfo = {
123 SHMMAX,
124 SHMMIN,
125 SHMMNI,
126 SHMSEG,
127 SHMALL
128};
129
130static int shm_use_phys;
131
132TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
133TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
134TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
135TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
136TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
137
984263bc
MD
138SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
139SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
140SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
141SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, "");
142SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
143SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, "");
144
145static int
c972a82f 146shm_find_segment_by_key(key_t key)
984263bc
MD
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
157static struct shmid_ds *
c972a82f 158shm_find_segment_by_shmid(int shmid)
984263bc
MD
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
174static void
c972a82f 175shm_deallocate_segment(struct shmid_ds *shmseg)
984263bc
MD
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);
efda3bd0 182 kfree((caddr_t)shm_handle, M_SHM);
984263bc
MD
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
190static int
fef0fdf2 191shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
984263bc
MD
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);
fef0fdf2 200 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
984263bc
MD
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
3919ced0
MD
213/*
214 * MPALMOSTSAFE
215 */
984263bc 216int
753fd850 217sys_shmdt(struct shmdt_args *uap)
984263bc 218{
9910d07b
MD
219 struct thread *td = curthread;
220 struct proc *p = td->td_proc;
984263bc
MD
221 struct shmmap_state *shmmap_s;
222 int i;
3919ced0 223 int error;
984263bc 224
9910d07b 225 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
226 return (ENOSYS);
227
3919ced0 228 get_mplock();
984263bc 229 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
3919ced0
MD
230 if (shmmap_s == NULL) {
231 error = EINVAL;
232 goto done;
233 }
234 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
984263bc
MD
235 if (shmmap_s->shmid != -1 &&
236 shmmap_s->va == (vm_offset_t)uap->shmaddr)
237 break;
3919ced0 238 }
984263bc 239 if (i == shminfo.shmseg)
3919ced0
MD
240 error = EINVAL;
241 else
242 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
243done:
244 rel_mplock();
245 return (error);
984263bc
MD
246}
247
3919ced0
MD
248/*
249 * MPALMOSTSAFE
250 */
984263bc 251int
753fd850 252sys_shmat(struct shmat_args *uap)
984263bc 253{
9910d07b
MD
254 struct thread *td = curthread;
255 struct proc *p = td->td_proc;
984263bc
MD
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
9910d07b 265 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
266 return (ENOSYS);
267
3919ced0
MD
268 get_mplock();
269again:
984263bc
MD
270 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
271 if (shmmap_s == NULL) {
272 size = shminfo.shmseg * sizeof(struct shmmap_state);
efda3bd0 273 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
984263bc
MD
274 for (i = 0; i < shminfo.shmseg; i++)
275 shmmap_s[i].shmid = -1;
3919ced0
MD
276 if (p->p_vmspace->vm_shm != NULL) {
277 kfree(shmmap_s, M_SHM);
278 goto again;
279 }
984263bc
MD
280 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
281 }
282 shmseg = shm_find_segment_by_shmid(uap->shmid);
3919ced0
MD
283 if (shmseg == NULL) {
284 error = EINVAL;
285 goto done;
286 }
984263bc 287 error = ipcperm(p, &shmseg->shm_perm,
3919ced0 288 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
984263bc 289 if (error)
3919ced0 290 goto done;
984263bc
MD
291 for (i = 0; i < shminfo.shmseg; i++) {
292 if (shmmap_s->shmid == -1)
293 break;
294 shmmap_s++;
295 }
3919ced0
MD
296 if (i >= shminfo.shmseg) {
297 error = EMFILE;
298 goto done;
299 }
984263bc
MD
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;
3919ced0 311 if (uap->shmflg & SHM_RND) {
984263bc 312 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
3919ced0 313 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
984263bc 314 attach_va = (vm_offset_t)uap->shmaddr;
3919ced0
MD
315 } else {
316 error = EINVAL;
317 goto done;
318 }
984263bc 319 } else {
3919ced0
MD
320 /*
321 * This is just a hint to vm_map_find() about where to put it.
322 */
984263bc
MD
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);
1b874851
MD
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);
984263bc 335 if (rv != KERN_SUCCESS) {
f23f0be6 336 vm_object_deallocate(shm_handle->shm_object);
3919ced0
MD
337 error = ENOMEM;
338 goto done;
984263bc
MD
339 }
340 vm_map_inherit(&p->p_vmspace->vm_map,
3919ced0 341 attach_va, attach_va + size, VM_INHERIT_SHARE);
984263bc 342
3919ced0 343 KKASSERT(shmmap_s->shmid == -1);
984263bc
MD
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++;
c7114eea 349 uap->sysmsg_result = attach_va;
3919ced0
MD
350 error = 0;
351done:
352 rel_mplock();
353 return error;
984263bc
MD
354}
355
356struct 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
368struct oshmctl_args {
df2244e3 369 struct sysmsg sysmsg;
984263bc
MD
370 int shmid;
371 int cmd;
372 struct oshmid_ds *ubuf;
373};
374
3919ced0
MD
375/*
376 * MPALMOSTSAFE
377 */
984263bc 378static int
c972a82f 379sys_oshmctl(struct proc *p, struct oshmctl_args *uap)
984263bc
MD
380{
381#ifdef COMPAT_43
9910d07b 382 struct thread *td = curthread;
984263bc
MD
383 struct shmid_ds *shmseg;
384 struct oshmid_ds outbuf;
9910d07b 385 int error;
984263bc 386
9910d07b 387 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
388 return (ENOSYS);
389
3919ced0 390 get_mplock();
984263bc 391 shmseg = shm_find_segment_by_shmid(uap->shmid);
3919ced0
MD
392 if (shmseg == NULL) {
393 error = EINVAL;
394 goto done;
395 }
396
984263bc
MD
397 switch (uap->cmd) {
398 case IPC_STAT:
399 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
400 if (error)
3919ced0 401 break;
984263bc
MD
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));
984263bc
MD
412 break;
413 default:
414 /* XXX casting to (sy_call_t *) is bogus, as usual. */
3919ced0 415 error = sys_shmctl((struct shmctl_args *)uap);
984263bc 416 }
3919ced0
MD
417done:
418 rel_mplock();
419 return error;
984263bc
MD
420#else
421 return EINVAL;
422#endif
423}
424
3919ced0
MD
425/*
426 * MPALMOSTSAFE
427 */
984263bc 428int
753fd850 429sys_shmctl(struct shmctl_args *uap)
984263bc 430{
9910d07b
MD
431 struct thread *td = curthread;
432 struct proc *p = td->td_proc;
984263bc
MD
433 int error;
434 struct shmid_ds inbuf;
435 struct shmid_ds *shmseg;
436
9910d07b 437 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
438 return (ENOSYS);
439
3919ced0 440 get_mplock();
984263bc 441 shmseg = shm_find_segment_by_shmid(uap->shmid);
3919ced0
MD
442 if (shmseg == NULL) {
443 error = EINVAL;
444 goto done;
445 }
446
984263bc
MD
447 switch (uap->cmd) {
448 case IPC_STAT:
449 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
3919ced0
MD
450 if (error == 0)
451 error = copyout(shmseg, uap->buf, sizeof(inbuf));
984263bc
MD
452 break;
453 case IPC_SET:
454 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
3919ced0
MD
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 }
984263bc
MD
465 break;
466 case IPC_RMID:
467 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
3919ced0
MD
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 }
984263bc
MD
475 }
476 break;
477#if 0
478 case SHM_LOCK:
479 case SHM_UNLOCK:
480#endif
481 default:
3919ced0
MD
482 error = EINVAL;
483 break;
984263bc 484 }
3919ced0
MD
485done:
486 rel_mplock();
487 return error;
984263bc
MD
488}
489
984263bc 490static int
c972a82f 491shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
984263bc
MD
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;
377d4740 504 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
984263bc
MD
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;
c7114eea 516 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
984263bc
MD
517 return 0;
518}
519
520static int
c972a82f 521shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
984263bc
MD
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;
efda3bd0 555 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
984263bc
MD
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 }
c7114eea 592 uap->sysmsg_result = shmid;
984263bc
MD
593 return 0;
594}
595
3919ced0
MD
596/*
597 * MPALMOSTSAFE
598 */
984263bc 599int
753fd850 600sys_shmget(struct shmget_args *uap)
984263bc 601{
9910d07b
MD
602 struct thread *td = curthread;
603 struct proc *p = td->td_proc;
984263bc
MD
604 int segnum, mode, error;
605
9910d07b 606 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
607 return (ENOSYS);
608
609 mode = uap->shmflg & ACCESSPERMS;
3919ced0
MD
610 get_mplock();
611
984263bc
MD
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;
3919ced0
MD
619 goto done;
620 }
621 if ((uap->shmflg & IPC_CREAT) == 0) {
622 error = ENOENT;
623 goto done;
984263bc 624 }
984263bc 625 }
3919ced0
MD
626 error = shmget_allocate_segment(p, uap, mode);
627done:
628 rel_mplock();
629 return (error);
984263bc
MD
630}
631
41c20dac 632/*
3919ced0
MD
633 * shmsys_args(int which, int a2, ...) (VARARGS)
634 *
635 * MPALMOSTSAFE
41c20dac 636 */
984263bc 637int
753fd850 638sys_shmsys(struct shmsys_args *uap)
984263bc 639{
9910d07b 640 struct thread *td = curthread;
180508ff 641 unsigned int which = (unsigned int)uap->which;
df44673d 642 int error;
984263bc 643
9910d07b 644 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
984263bc
MD
645 return (ENOSYS);
646
df44673d 647 if (which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
984263bc 648 return EINVAL;
3919ced0 649 get_mplock();
df44673d
MD
650 bcopy(&uap->a2, &uap->which,
651 sizeof(struct shmsys_args) - offsetof(struct shmsys_args, a2));
652 error = ((*shmcalls[which])(uap));
3919ced0
MD
653 rel_mplock();
654
df44673d 655 return(error);
984263bc
MD
656}
657
658void
c972a82f 659shmfork(struct proc *p1, struct proc *p2)
984263bc
MD
660{
661 struct shmmap_state *shmmap_s;
662 size_t size;
663 int i;
664
665 size = shminfo.shmseg * sizeof(struct shmmap_state);
efda3bd0 666 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
984263bc
MD
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
674void
fef0fdf2 675shmexit(struct vmspace *vm)
984263bc 676{
fef0fdf2 677 struct shmmap_state *base, *shm;
984263bc
MD
678 int i;
679
fef0fdf2
MD
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 }
efda3bd0 686 kfree(base, M_SHM);
fef0fdf2 687 }
984263bc
MD
688}
689
690static void
691shmrealloc(void)
692{
693 int i;
694 struct shmid_ds *newsegs;
695
696 if (shmalloced >= shminfo.shmmni)
697 return;
698
efda3bd0 699 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
984263bc
MD
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 }
efda3bd0 706 kfree(shmsegs, M_SHM);
984263bc
MD
707 shmsegs = newsegs;
708 shmalloced = shminfo.shmmni;
709}
710
711static void
c972a82f 712shminit(void *dummy)
984263bc
MD
713{
714 int i;
715
716 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
717 shmalloced = shminfo.shmmni;
efda3bd0 718 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
984263bc
MD
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}
727SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);