FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_shm.c
1 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
2 /*-
3 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Adam Glass and Charles
16 * Hannum.
17 * 4. The names of the authors may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*-
32 * Copyright (c) 2003-2005 McAfee, Inc.
33 * All rights reserved.
34 *
35 * This software was developed for the FreeBSD Project in part by McAfee
36 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
37 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
38 * program.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD: releng/6.3/sys/kern/sysv_shm.c 173886 2007-11-24 19:45:58Z cvs2svn $");
64
65 #include "opt_compat.h"
66 #include "opt_sysvipc.h"
67 #include "opt_mac.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/lock.h>
73 #include <sys/sysctl.h>
74 #include <sys/shm.h>
75 #include <sys/proc.h>
76 #include <sys/malloc.h>
77 #include <sys/mman.h>
78 #include <sys/module.h>
79 #include <sys/mutex.h>
80 #include <sys/resourcevar.h>
81 #include <sys/stat.h>
82 #include <sys/syscall.h>
83 #include <sys/syscallsubr.h>
84 #include <sys/sysent.h>
85 #include <sys/sysproto.h>
86 #include <sys/jail.h>
87 #include <sys/mac.h>
88
89 #include <vm/vm.h>
90 #include <vm/vm_param.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pager.h>
96
97 #ifdef MAC_DEBUG
98 #define MPRINTF(a) printf a
99 #else
100 #define MPRINTF(a)
101 #endif
102
103 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
104
105 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
106 struct oshmctl_args;
107 static int oshmctl(struct thread *td, struct oshmctl_args *uap);
108 #endif
109
110 static int shmget_allocate_segment(struct thread *td,
111 struct shmget_args *uap, int mode);
112 static int shmget_existing(struct thread *td, struct shmget_args *uap,
113 int mode, int segnum);
114
115 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
116 /* XXX casting to (sy_call_t *) is bogus, as usual. */
117 static sy_call_t *shmcalls[] = {
118 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
119 (sy_call_t *)shmdt, (sy_call_t *)shmget,
120 (sy_call_t *)shmctl
121 };
122 #endif
123
124 #define SHMSEG_FREE 0x0200
125 #define SHMSEG_REMOVED 0x0400
126 #define SHMSEG_ALLOCATED 0x0800
127 #define SHMSEG_WANTED 0x1000
128
129 static int shm_last_free, shm_nused, shm_committed, shmalloced;
130 static struct shmid_kernel *shmsegs;
131
132 struct shmmap_state {
133 vm_offset_t va;
134 int shmid;
135 };
136
137 static void shm_deallocate_segment(struct shmid_kernel *);
138 static int shm_find_segment_by_key(key_t);
139 static struct shmid_kernel *shm_find_segment_by_shmid(int);
140 static struct shmid_kernel *shm_find_segment_by_shmidx(int);
141 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
142 static void shmrealloc(void);
143 static void shminit(void);
144 static int sysvshm_modload(struct module *, int, void *);
145 static int shmunload(void);
146 static void shmexit_myhook(struct vmspace *vm);
147 static void shmfork_myhook(struct proc *p1, struct proc *p2);
148 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
149
150 /*
151 * Tuneable values.
152 */
153 #ifndef SHMMAXPGS
154 #define SHMMAXPGS 8192 /* Note: sysv shared memory is swap backed. */
155 #endif
156 #ifndef SHMMAX
157 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
158 #endif
159 #ifndef SHMMIN
160 #define SHMMIN 1
161 #endif
162 #ifndef SHMMNI
163 #define SHMMNI 192
164 #endif
165 #ifndef SHMSEG
166 #define SHMSEG 128
167 #endif
168 #ifndef SHMALL
169 #define SHMALL (SHMMAXPGS)
170 #endif
171
172 struct shminfo shminfo = {
173 SHMMAX,
174 SHMMIN,
175 SHMMNI,
176 SHMSEG,
177 SHMALL
178 };
179
180 static int shm_use_phys;
181 static int shm_allow_removed;
182
183 SYSCTL_DECL(_kern_ipc);
184 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
185 "Maximum shared memory segment size");
186 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
187 "Minimum shared memory segment size");
188 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
189 "Number of shared memory identifiers");
190 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
191 "Number of segments per process");
192 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
193 "Maximum number of pages available for shared memory");
194 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
195 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
196 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
197 &shm_allow_removed, 0,
198 "Enable/Disable attachment to attached segments marked for removal");
199 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
200 NULL, 0, sysctl_shmsegs, "",
201 "Current number of shared memory segments allocated");
202
203 static int
204 shm_find_segment_by_key(key)
205 key_t key;
206 {
207 int i;
208
209 for (i = 0; i < shmalloced; i++)
210 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
211 shmsegs[i].u.shm_perm.key == key)
212 return (i);
213 return (-1);
214 }
215
216 static struct shmid_kernel *
217 shm_find_segment_by_shmid(int shmid)
218 {
219 int segnum;
220 struct shmid_kernel *shmseg;
221
222 segnum = IPCID_TO_IX(shmid);
223 if (segnum < 0 || segnum >= shmalloced)
224 return (NULL);
225 shmseg = &shmsegs[segnum];
226 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
227 (!shm_allow_removed &&
228 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
229 shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid))
230 return (NULL);
231 return (shmseg);
232 }
233
234 static struct shmid_kernel *
235 shm_find_segment_by_shmidx(int segnum)
236 {
237 struct shmid_kernel *shmseg;
238
239 if (segnum < 0 || segnum >= shmalloced)
240 return (NULL);
241 shmseg = &shmsegs[segnum];
242 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
243 (!shm_allow_removed &&
244 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0))
245 return (NULL);
246 return (shmseg);
247 }
248
249 static void
250 shm_deallocate_segment(shmseg)
251 struct shmid_kernel *shmseg;
252 {
253 size_t size;
254
255 GIANT_REQUIRED;
256
257 vm_object_deallocate(shmseg->u.shm_internal);
258 shmseg->u.shm_internal = NULL;
259 size = round_page(shmseg->u.shm_segsz);
260 shm_committed -= btoc(size);
261 shm_nused--;
262 shmseg->u.shm_perm.mode = SHMSEG_FREE;
263 #ifdef MAC
264 mac_cleanup_sysv_shm(shmseg);
265 #endif
266 }
267
268 static int
269 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
270 {
271 struct shmid_kernel *shmseg;
272 int segnum, result;
273 size_t size;
274
275 GIANT_REQUIRED;
276
277 segnum = IPCID_TO_IX(shmmap_s->shmid);
278 shmseg = &shmsegs[segnum];
279 size = round_page(shmseg->u.shm_segsz);
280 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
281 if (result != KERN_SUCCESS)
282 return (EINVAL);
283 shmmap_s->shmid = -1;
284 shmseg->u.shm_dtime = time_second;
285 if ((--shmseg->u.shm_nattch <= 0) &&
286 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
287 shm_deallocate_segment(shmseg);
288 shm_last_free = segnum;
289 }
290 return (0);
291 }
292
293 #ifndef _SYS_SYSPROTO_H_
294 struct shmdt_args {
295 const void *shmaddr;
296 };
297 #endif
298
299 /*
300 * MPSAFE
301 */
302 int
303 shmdt(td, uap)
304 struct thread *td;
305 struct shmdt_args *uap;
306 {
307 struct proc *p = td->td_proc;
308 struct shmmap_state *shmmap_s;
309 #ifdef MAC
310 struct shmid_kernel *shmsegptr;
311 #endif
312 int i;
313 int error = 0;
314
315 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
316 return (ENOSYS);
317 mtx_lock(&Giant);
318 shmmap_s = p->p_vmspace->vm_shm;
319 if (shmmap_s == NULL) {
320 error = EINVAL;
321 goto done2;
322 }
323 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
324 if (shmmap_s->shmid != -1 &&
325 shmmap_s->va == (vm_offset_t)uap->shmaddr) {
326 break;
327 }
328 }
329 if (i == shminfo.shmseg) {
330 error = EINVAL;
331 goto done2;
332 }
333 #ifdef MAC
334 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
335 error = mac_check_sysv_shmdt(td->td_ucred, shmsegptr);
336 if (error != 0) {
337 MPRINTF(("mac_check_sysv_shmdt returned %d\n", error));
338 goto done2;
339 }
340 #endif
341 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
342 done2:
343 mtx_unlock(&Giant);
344 return (error);
345 }
346
347 #ifndef _SYS_SYSPROTO_H_
348 struct shmat_args {
349 int shmid;
350 const void *shmaddr;
351 int shmflg;
352 };
353 #endif
354
355 /*
356 * MPSAFE
357 */
358 int
359 kern_shmat(td, shmid, shmaddr, shmflg)
360 struct thread *td;
361 int shmid;
362 const void *shmaddr;
363 int shmflg;
364 {
365 struct proc *p = td->td_proc;
366 int i, flags;
367 struct shmid_kernel *shmseg;
368 struct shmmap_state *shmmap_s = NULL;
369 vm_offset_t attach_va;
370 vm_prot_t prot;
371 vm_size_t size;
372 int rv;
373 int error = 0;
374
375 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
376 return (ENOSYS);
377 mtx_lock(&Giant);
378 shmmap_s = p->p_vmspace->vm_shm;
379 if (shmmap_s == NULL) {
380 size = shminfo.shmseg * sizeof(struct shmmap_state);
381 shmmap_s = malloc(size, M_SHM, M_WAITOK);
382 for (i = 0; i < shminfo.shmseg; i++)
383 shmmap_s[i].shmid = -1;
384 p->p_vmspace->vm_shm = shmmap_s;
385 }
386 shmseg = shm_find_segment_by_shmid(shmid);
387 if (shmseg == NULL) {
388 error = EINVAL;
389 goto done2;
390 }
391 error = ipcperm(td, &shmseg->u.shm_perm,
392 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
393 if (error)
394 goto done2;
395 #ifdef MAC
396 error = mac_check_sysv_shmat(td->td_ucred, shmseg, shmflg);
397 if (error != 0) {
398 MPRINTF(("mac_check_sysv_shmat returned %d\n", error));
399 goto done2;
400 }
401 #endif
402 for (i = 0; i < shminfo.shmseg; i++) {
403 if (shmmap_s->shmid == -1)
404 break;
405 shmmap_s++;
406 }
407 if (i >= shminfo.shmseg) {
408 error = EMFILE;
409 goto done2;
410 }
411 size = round_page(shmseg->u.shm_segsz);
412 #ifdef VM_PROT_READ_IS_EXEC
413 prot = VM_PROT_READ | VM_PROT_EXECUTE;
414 #else
415 prot = VM_PROT_READ;
416 #endif
417 if ((shmflg & SHM_RDONLY) == 0)
418 prot |= VM_PROT_WRITE;
419 flags = MAP_ANON | MAP_SHARED;
420 if (shmaddr) {
421 flags |= MAP_FIXED;
422 if (shmflg & SHM_RND) {
423 attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
424 } else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
425 attach_va = (vm_offset_t)shmaddr;
426 } else {
427 error = EINVAL;
428 goto done2;
429 }
430 } else {
431 /*
432 * This is just a hint to vm_map_find() about where to
433 * put it.
434 */
435 PROC_LOCK(p);
436 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
437 lim_max(p, RLIMIT_DATA));
438 PROC_UNLOCK(p);
439 }
440
441 vm_object_reference(shmseg->u.shm_internal);
442 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->u.shm_internal,
443 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
444 if (rv != KERN_SUCCESS) {
445 vm_object_deallocate(shmseg->u.shm_internal);
446 error = ENOMEM;
447 goto done2;
448 }
449 vm_map_inherit(&p->p_vmspace->vm_map,
450 attach_va, attach_va + size, VM_INHERIT_SHARE);
451
452 shmmap_s->va = attach_va;
453 shmmap_s->shmid = shmid;
454 shmseg->u.shm_lpid = p->p_pid;
455 shmseg->u.shm_atime = time_second;
456 shmseg->u.shm_nattch++;
457 td->td_retval[0] = attach_va;
458 done2:
459 mtx_unlock(&Giant);
460 return (error);
461 }
462
463 int
464 shmat(td, uap)
465 struct thread *td;
466 struct shmat_args *uap;
467 {
468 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
469 }
470
471 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
472 struct oshmid_ds {
473 struct ipc_perm shm_perm; /* operation perms */
474 int shm_segsz; /* size of segment (bytes) */
475 u_short shm_cpid; /* pid, creator */
476 u_short shm_lpid; /* pid, last operation */
477 short shm_nattch; /* no. of current attaches */
478 time_t shm_atime; /* last attach time */
479 time_t shm_dtime; /* last detach time */
480 time_t shm_ctime; /* last change time */
481 void *shm_handle; /* internal handle for shm segment */
482 };
483
484 struct oshmctl_args {
485 int shmid;
486 int cmd;
487 struct oshmid_ds *ubuf;
488 };
489
490 /*
491 * MPSAFE
492 */
493 static int
494 oshmctl(td, uap)
495 struct thread *td;
496 struct oshmctl_args *uap;
497 {
498 #ifdef COMPAT_43
499 int error = 0;
500 struct shmid_kernel *shmseg;
501 struct oshmid_ds outbuf;
502
503 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
504 return (ENOSYS);
505 mtx_lock(&Giant);
506 shmseg = shm_find_segment_by_shmid(uap->shmid);
507 if (shmseg == NULL) {
508 error = EINVAL;
509 goto done2;
510 }
511 switch (uap->cmd) {
512 case IPC_STAT:
513 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
514 if (error)
515 goto done2;
516 #ifdef MAC
517 error = mac_check_sysv_shmctl(td->td_ucred, shmseg, uap->cmd);
518 if (error != 0) {
519 MPRINTF(("mac_check_sysv_shmctl returned %d\n",
520 error));
521 goto done2;
522 }
523 #endif
524 outbuf.shm_perm = shmseg->u.shm_perm;
525 outbuf.shm_segsz = shmseg->u.shm_segsz;
526 outbuf.shm_cpid = shmseg->u.shm_cpid;
527 outbuf.shm_lpid = shmseg->u.shm_lpid;
528 outbuf.shm_nattch = shmseg->u.shm_nattch;
529 outbuf.shm_atime = shmseg->u.shm_atime;
530 outbuf.shm_dtime = shmseg->u.shm_dtime;
531 outbuf.shm_ctime = shmseg->u.shm_ctime;
532 outbuf.shm_handle = shmseg->u.shm_internal;
533 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
534 if (error)
535 goto done2;
536 break;
537 default:
538 error = shmctl(td, (struct shmctl_args *)uap);
539 break;
540 }
541 done2:
542 mtx_unlock(&Giant);
543 return (error);
544 #else
545 return (EINVAL);
546 #endif
547 }
548 #endif
549
550 #ifndef _SYS_SYSPROTO_H_
551 struct shmctl_args {
552 int shmid;
553 int cmd;
554 struct shmid_ds *buf;
555 };
556 #endif
557
558 /*
559 * MPSAFE
560 */
561 int
562 kern_shmctl(td, shmid, cmd, buf, bufsz)
563 struct thread *td;
564 int shmid;
565 int cmd;
566 void *buf;
567 size_t *bufsz;
568 {
569 int error = 0;
570 struct shmid_kernel *shmseg;
571
572 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
573 return (ENOSYS);
574
575 mtx_lock(&Giant);
576 switch (cmd) {
577 case IPC_INFO:
578 memcpy(buf, &shminfo, sizeof(shminfo));
579 if (bufsz)
580 *bufsz = sizeof(shminfo);
581 td->td_retval[0] = shmalloced;
582 goto done2;
583 case SHM_INFO: {
584 struct shm_info shm_info;
585 shm_info.used_ids = shm_nused;
586 shm_info.shm_rss = 0; /*XXX where to get from ? */
587 shm_info.shm_tot = 0; /*XXX where to get from ? */
588 shm_info.shm_swp = 0; /*XXX where to get from ? */
589 shm_info.swap_attempts = 0; /*XXX where to get from ? */
590 shm_info.swap_successes = 0; /*XXX where to get from ? */
591 memcpy(buf, &shm_info, sizeof(shm_info));
592 if (bufsz)
593 *bufsz = sizeof(shm_info);
594 td->td_retval[0] = shmalloced;
595 goto done2;
596 }
597 }
598 if (cmd == SHM_STAT)
599 shmseg = shm_find_segment_by_shmidx(shmid);
600 else
601 shmseg = shm_find_segment_by_shmid(shmid);
602 if (shmseg == NULL) {
603 error = EINVAL;
604 goto done2;
605 }
606 #ifdef MAC
607 error = mac_check_sysv_shmctl(td->td_ucred, shmseg, cmd);
608 if (error != 0) {
609 MPRINTF(("mac_check_sysv_shmctl returned %d\n", error));
610 goto done2;
611 }
612 #endif
613 switch (cmd) {
614 case SHM_STAT:
615 case IPC_STAT:
616 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
617 if (error)
618 goto done2;
619 memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
620 if (bufsz)
621 *bufsz = sizeof(struct shmid_ds);
622 if (cmd == SHM_STAT)
623 td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm);
624 break;
625 case IPC_SET: {
626 struct shmid_ds *shmid;
627
628 shmid = (struct shmid_ds *)buf;
629 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
630 if (error)
631 goto done2;
632 shmseg->u.shm_perm.uid = shmid->shm_perm.uid;
633 shmseg->u.shm_perm.gid = shmid->shm_perm.gid;
634 shmseg->u.shm_perm.mode =
635 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
636 (shmid->shm_perm.mode & ACCESSPERMS);
637 shmseg->u.shm_ctime = time_second;
638 break;
639 }
640 case IPC_RMID:
641 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
642 if (error)
643 goto done2;
644 shmseg->u.shm_perm.key = IPC_PRIVATE;
645 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
646 if (shmseg->u.shm_nattch <= 0) {
647 shm_deallocate_segment(shmseg);
648 shm_last_free = IPCID_TO_IX(shmid);
649 }
650 break;
651 #if 0
652 case SHM_LOCK:
653 case SHM_UNLOCK:
654 #endif
655 default:
656 error = EINVAL;
657 break;
658 }
659 done2:
660 mtx_unlock(&Giant);
661 return (error);
662 }
663
664 int
665 shmctl(td, uap)
666 struct thread *td;
667 struct shmctl_args *uap;
668 {
669 int error = 0;
670 struct shmid_ds buf;
671 size_t bufsz;
672
673 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
674 if (uap->cmd == IPC_SET) {
675 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
676 goto done;
677 }
678
679 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
680 if (error)
681 goto done;
682
683 /* Cases in which we need to copyout */
684 switch (uap->cmd) {
685 case IPC_INFO:
686 case SHM_INFO:
687 case SHM_STAT:
688 case IPC_STAT:
689 error = copyout(&buf, uap->buf, bufsz);
690 break;
691 }
692
693 done:
694 if (error) {
695 /* Invalidate the return value */
696 td->td_retval[0] = -1;
697 }
698 return (error);
699 }
700
701
702 #ifndef _SYS_SYSPROTO_H_
703 struct shmget_args {
704 key_t key;
705 size_t size;
706 int shmflg;
707 };
708 #endif
709
710 static int
711 shmget_existing(td, uap, mode, segnum)
712 struct thread *td;
713 struct shmget_args *uap;
714 int mode;
715 int segnum;
716 {
717 struct shmid_kernel *shmseg;
718 int error;
719
720 shmseg = &shmsegs[segnum];
721 if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) {
722 /*
723 * This segment is in the process of being allocated. Wait
724 * until it's done, and look the key up again (in case the
725 * allocation failed or it was freed).
726 */
727 shmseg->u.shm_perm.mode |= SHMSEG_WANTED;
728 error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
729 if (error)
730 return (error);
731 return (EAGAIN);
732 }
733 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
734 return (EEXIST);
735 #ifdef MAC
736 error = mac_check_sysv_shmget(td->td_ucred, shmseg, uap->shmflg);
737 if (error != 0) {
738 MPRINTF(("mac_check_sysv_shmget returned %d\n", error));
739 return (error);
740 }
741 #endif
742 error = ipcperm(td, &shmseg->u.shm_perm, mode);
743 if (error)
744 return (error);
745 if (uap->size && uap->size > shmseg->u.shm_segsz)
746 return (EINVAL);
747 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
748 return (0);
749 }
750
751 static int
752 shmget_allocate_segment(td, uap, mode)
753 struct thread *td;
754 struct shmget_args *uap;
755 int mode;
756 {
757 int i, segnum, shmid, size;
758 struct ucred *cred = td->td_ucred;
759 struct shmid_kernel *shmseg;
760 vm_object_t shm_object;
761
762 GIANT_REQUIRED;
763
764 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
765 return (EINVAL);
766 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
767 return (ENOSPC);
768 size = round_page(uap->size);
769 if (shm_committed + btoc(size) > shminfo.shmall)
770 return (ENOMEM);
771 if (shm_last_free < 0) {
772 shmrealloc(); /* Maybe expand the shmsegs[] array. */
773 for (i = 0; i < shmalloced; i++)
774 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
775 break;
776 if (i == shmalloced)
777 return (ENOSPC);
778 segnum = i;
779 } else {
780 segnum = shm_last_free;
781 shm_last_free = -1;
782 }
783 shmseg = &shmsegs[segnum];
784 /*
785 * In case we sleep in malloc(), mark the segment present but deleted
786 * so that noone else tries to create the same key.
787 */
788 shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
789 shmseg->u.shm_perm.key = uap->key;
790 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
791 shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
792
793 /*
794 * We make sure that we have allocated a pager before we need
795 * to.
796 */
797 if (shm_use_phys) {
798 shm_object =
799 vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
800 } else {
801 shm_object =
802 vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
803 }
804 VM_OBJECT_LOCK(shm_object);
805 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
806 vm_object_set_flag(shm_object, OBJ_NOSPLIT);
807 VM_OBJECT_UNLOCK(shm_object);
808
809 shmseg->u.shm_internal = shm_object;
810 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
811 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
812 shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) |
813 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
814 shmseg->u.shm_segsz = uap->size;
815 shmseg->u.shm_cpid = td->td_proc->p_pid;
816 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
817 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
818 #ifdef MAC
819 mac_create_sysv_shm(cred, shmseg);
820 #endif
821 shmseg->u.shm_ctime = time_second;
822 shm_committed += btoc(size);
823 shm_nused++;
824 if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) {
825 /*
826 * Somebody else wanted this key while we were asleep. Wake
827 * them up now.
828 */
829 shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED;
830 wakeup(shmseg);
831 }
832 td->td_retval[0] = shmid;
833 return (0);
834 }
835
836 /*
837 * MPSAFE
838 */
839 int
840 shmget(td, uap)
841 struct thread *td;
842 struct shmget_args *uap;
843 {
844 int segnum, mode;
845 int error;
846
847 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
848 return (ENOSYS);
849 mtx_lock(&Giant);
850 mode = uap->shmflg & ACCESSPERMS;
851 if (uap->key != IPC_PRIVATE) {
852 again:
853 segnum = shm_find_segment_by_key(uap->key);
854 if (segnum >= 0) {
855 error = shmget_existing(td, uap, mode, segnum);
856 if (error == EAGAIN)
857 goto again;
858 goto done2;
859 }
860 if ((uap->shmflg & IPC_CREAT) == 0) {
861 error = ENOENT;
862 goto done2;
863 }
864 }
865 error = shmget_allocate_segment(td, uap, mode);
866 done2:
867 mtx_unlock(&Giant);
868 return (error);
869 }
870
871 /*
872 * MPSAFE
873 */
874 int
875 shmsys(td, uap)
876 struct thread *td;
877 /* XXX actually varargs. */
878 struct shmsys_args /* {
879 int which;
880 int a2;
881 int a3;
882 int a4;
883 } */ *uap;
884 {
885 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
886 int error;
887
888 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
889 return (ENOSYS);
890 if (uap->which < 0 ||
891 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
892 return (EINVAL);
893 mtx_lock(&Giant);
894 error = (*shmcalls[uap->which])(td, &uap->a2);
895 mtx_unlock(&Giant);
896 return (error);
897 #else
898 return (nosys(td, NULL));
899 #endif
900 }
901
902 static void
903 shmfork_myhook(p1, p2)
904 struct proc *p1, *p2;
905 {
906 struct shmmap_state *shmmap_s;
907 size_t size;
908 int i;
909
910 mtx_lock(&Giant);
911 size = shminfo.shmseg * sizeof(struct shmmap_state);
912 shmmap_s = malloc(size, M_SHM, M_WAITOK);
913 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
914 p2->p_vmspace->vm_shm = shmmap_s;
915 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
916 if (shmmap_s->shmid != -1)
917 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
918 mtx_unlock(&Giant);
919 }
920
921 static void
922 shmexit_myhook(struct vmspace *vm)
923 {
924 struct shmmap_state *base, *shm;
925 int i;
926
927 if ((base = vm->vm_shm) != NULL) {
928 vm->vm_shm = NULL;
929 mtx_lock(&Giant);
930 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
931 if (shm->shmid != -1)
932 shm_delete_mapping(vm, shm);
933 }
934 mtx_unlock(&Giant);
935 free(base, M_SHM);
936 }
937 }
938
939 static void
940 shmrealloc(void)
941 {
942 int i;
943 struct shmid_kernel *newsegs;
944
945 if (shmalloced >= shminfo.shmmni)
946 return;
947
948 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
949 if (newsegs == NULL)
950 return;
951 for (i = 0; i < shmalloced; i++)
952 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
953 for (; i < shminfo.shmmni; i++) {
954 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
955 shmsegs[i].u.shm_perm.seq = 0;
956 #ifdef MAC
957 mac_init_sysv_shm(&shmsegs[i]);
958 #endif
959 }
960 free(shmsegs, M_SHM);
961 shmsegs = newsegs;
962 shmalloced = shminfo.shmmni;
963 }
964
965 static void
966 shminit()
967 {
968 int i;
969
970 TUNABLE_INT_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
971 for (i = PAGE_SIZE; i > 0; i--) {
972 shminfo.shmmax = shminfo.shmall * i;
973 if (shminfo.shmmax >= shminfo.shmall)
974 break;
975 }
976 TUNABLE_INT_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
977 TUNABLE_INT_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
978 TUNABLE_INT_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
979 TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
980
981 shmalloced = shminfo.shmmni;
982 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
983 if (shmsegs == NULL)
984 panic("cannot allocate initial memory for sysvshm");
985 for (i = 0; i < shmalloced; i++) {
986 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
987 shmsegs[i].u.shm_perm.seq = 0;
988 #ifdef MAC
989 mac_init_sysv_shm(&shmsegs[i]);
990 #endif
991 }
992 shm_last_free = 0;
993 shm_nused = 0;
994 shm_committed = 0;
995 shmexit_hook = &shmexit_myhook;
996 shmfork_hook = &shmfork_myhook;
997 }
998
999 static int
1000 shmunload()
1001 {
1002 #ifdef MAC
1003 int i;
1004 #endif
1005
1006 if (shm_nused > 0)
1007 return (EBUSY);
1008
1009 #ifdef MAC
1010 for (i = 0; i < shmalloced; i++)
1011 mac_destroy_sysv_shm(&shmsegs[i]);
1012 #endif
1013 free(shmsegs, M_SHM);
1014 shmexit_hook = NULL;
1015 shmfork_hook = NULL;
1016 return (0);
1017 }
1018
1019 static int
1020 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1021 {
1022
1023 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
1024 }
1025
1026 static int
1027 sysvshm_modload(struct module *module, int cmd, void *arg)
1028 {
1029 int error = 0;
1030
1031 switch (cmd) {
1032 case MOD_LOAD:
1033 shminit();
1034 break;
1035 case MOD_UNLOAD:
1036 error = shmunload();
1037 break;
1038 case MOD_SHUTDOWN:
1039 break;
1040 default:
1041 error = EINVAL;
1042 break;
1043 }
1044 return (error);
1045 }
1046
1047 static moduledata_t sysvshm_mod = {
1048 "sysvshm",
1049 &sysvshm_modload,
1050 NULL
1051 };
1052
1053 SYSCALL_MODULE_HELPER(shmsys);
1054 SYSCALL_MODULE_HELPER(shmat);
1055 SYSCALL_MODULE_HELPER(shmctl);
1056 SYSCALL_MODULE_HELPER(shmdt);
1057 SYSCALL_MODULE_HELPER(shmget);
1058
1059 DECLARE_MODULE(sysvshm, sysvshm_mod,
1060 SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1061 MODULE_VERSION(sysvshm, 1);
Cache object: 8dc2b82d9c2b6bb26e4ec086fc6b439c
|