]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */ | |
23 | ||
24 | /* | |
25 | * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved. | |
26 | * | |
27 | * Redistribution and use in source and binary forms, with or without | |
28 | * modification, are permitted provided that the following conditions | |
29 | * are met: | |
30 | * 1. Redistributions of source code must retain the above copyright | |
31 | * notice, this list of conditions and the following disclaimer. | |
32 | * 2. Redistributions in binary form must reproduce the above copyright | |
33 | * notice, this list of conditions and the following disclaimer in the | |
34 | * documentation and/or other materials provided with the distribution. | |
35 | * 3. All advertising materials mentioning features or use of this software | |
36 | * must display the following acknowledgement: | |
37 | * This product includes software developed by Adam Glass and Charles | |
38 | * Hannum. | |
39 | * 4. The names of the authors may not be used to endorse or promote products | |
40 | * derived from this software without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR | |
43 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
44 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
45 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
46 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
47 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
48 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
49 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
50 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
51 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | */ | |
53 | ||
54 | ||
55 | #include <sys/appleapiopts.h> | |
56 | #include <sys/param.h> | |
57 | #include <sys/systm.h> | |
58 | #include <sys/kernel.h> | |
59 | #include <sys/shm.h> | |
60 | #include <sys/proc.h> | |
61 | #include <sys/malloc.h> | |
62 | #include <sys/mman.h> | |
63 | #include <sys/stat.h> | |
64 | #include <sys/sysctl.h> | |
65 | ||
66 | #include <bsm/audit_kernel.h> | |
67 | ||
68 | #include <mach/mach_types.h> | |
69 | #include <mach/vm_inherit.h> | |
70 | #include <vm/vm_map.h> | |
71 | ||
72 | struct shmat_args; | |
73 | extern int shmat __P((struct proc *p, struct shmat_args *uap, int *retval)); | |
74 | struct shmctl_args; | |
75 | extern int shmctl __P((struct proc *p, struct shmctl_args *uap, int *retval)); | |
76 | struct shmdt_args; | |
77 | extern int shmdt __P((struct proc *p, struct shmdt_args *uap, int *retval)); | |
78 | struct shmget_args; | |
79 | extern int shmget __P((struct proc *p, struct shmget_args *uap, int *retval)); | |
80 | ||
81 | #if 0 | |
82 | static void shminit __P((void *)); | |
83 | SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL) | |
84 | #endif 0 | |
85 | ||
86 | struct oshmctl_args; | |
87 | static int oshmctl __P((struct proc *p, struct oshmctl_args *uap, int * retval)); | |
88 | static int shmget_allocate_segment __P((struct proc *p, struct shmget_args *uap, int mode, int * retval)); | |
89 | static int shmget_existing __P((struct proc *p, struct shmget_args *uap, int mode, int segnum, int * retval)); | |
90 | ||
91 | typedef int sy_call_t __P((struct proc *, void *, int *)); | |
92 | ||
93 | /* XXX casting to (sy_call_t *) is bogus, as usual. */ | |
94 | static sy_call_t *shmcalls[] = { | |
95 | (sy_call_t *)shmat, (sy_call_t *)oshmctl, | |
96 | (sy_call_t *)shmdt, (sy_call_t *)shmget, | |
97 | (sy_call_t *)shmctl | |
98 | }; | |
99 | ||
100 | #define SHMSEG_FREE 0x0200 | |
101 | #define SHMSEG_REMOVED 0x0400 | |
102 | #define SHMSEG_ALLOCATED 0x0800 | |
103 | #define SHMSEG_WANTED 0x1000 | |
104 | ||
105 | static int shm_last_free, shm_nused, shm_committed; | |
106 | struct shmid_ds *shmsegs; | |
107 | static int shm_inited = 0; | |
108 | ||
109 | struct shm_handle { | |
110 | /* vm_offset_t kva; */ | |
111 | void * shm_object; | |
112 | }; | |
113 | ||
114 | struct shmmap_state { | |
115 | vm_offset_t va; | |
116 | int shmid; | |
117 | }; | |
118 | ||
119 | static void shm_deallocate_segment __P((struct shmid_ds *)); | |
120 | static int shm_find_segment_by_key __P((key_t)); | |
121 | static struct shmid_ds *shm_find_segment_by_shmid __P((int)); | |
122 | static int shm_delete_mapping __P((struct proc *, struct shmmap_state *, int)); | |
123 | ||
124 | #ifdef __APPLE_API_PRIVATE | |
125 | struct shminfo shminfo = { | |
126 | -1, /* SHMMAX 4096 *1024 */ | |
127 | -1, /* SHMMIN = 1 */ | |
128 | -1, /* SHMMNI = 1 */ | |
129 | -1, /* SHMSEG = 8 */ | |
130 | -1 /* SHMALL = 1024 */ | |
131 | }; | |
132 | #endif /* __APPLE_API_PRIVATE */ | |
133 | ||
134 | static int | |
135 | shm_find_segment_by_key(key) | |
136 | key_t key; | |
137 | { | |
138 | int i; | |
139 | ||
140 | for (i = 0; i < shminfo.shmmni; i++) | |
141 | if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) && | |
142 | shmsegs[i].shm_perm.key == key) | |
143 | return i; | |
144 | return -1; | |
145 | } | |
146 | ||
147 | static struct shmid_ds * | |
148 | shm_find_segment_by_shmid(shmid) | |
149 | int shmid; | |
150 | { | |
151 | int segnum; | |
152 | struct shmid_ds *shmseg; | |
153 | ||
154 | segnum = IPCID_TO_IX(shmid); | |
155 | if (segnum < 0 || segnum >= shminfo.shmmni) | |
156 | return NULL; | |
157 | shmseg = &shmsegs[segnum]; | |
158 | if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED)) | |
159 | != SHMSEG_ALLOCATED || | |
160 | shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid)) | |
161 | return NULL; | |
162 | return shmseg; | |
163 | } | |
164 | ||
165 | static void | |
166 | shm_deallocate_segment(shmseg) | |
167 | struct shmid_ds *shmseg; | |
168 | { | |
169 | struct shm_handle *shm_handle; | |
170 | struct shmmap_state *shmmap_s=NULL; | |
171 | size_t size; | |
172 | char * ptr; | |
173 | ||
174 | shm_handle = shmseg->shm_internal; | |
175 | size = round_page_32(shmseg->shm_segsz); | |
176 | mach_destroy_memory_entry(shm_handle->shm_object); | |
177 | FREE((caddr_t)shm_handle, M_SHM); | |
178 | shmseg->shm_internal = NULL; | |
179 | shm_committed -= btoc(size); | |
180 | shm_nused--; | |
181 | shmseg->shm_perm.mode = SHMSEG_FREE; | |
182 | } | |
183 | ||
184 | static int | |
185 | shm_delete_mapping(p, shmmap_s, deallocate) | |
186 | struct proc *p; | |
187 | struct shmmap_state *shmmap_s; | |
188 | int deallocate; | |
189 | { | |
190 | struct shmid_ds *shmseg; | |
191 | int segnum, result; | |
192 | size_t size; | |
193 | ||
194 | segnum = IPCID_TO_IX(shmmap_s->shmid); | |
195 | shmseg = &shmsegs[segnum]; | |
196 | size = round_page_32(shmseg->shm_segsz); | |
197 | if (deallocate) { | |
198 | result = vm_deallocate(current_map(), shmmap_s->va, size); | |
199 | if (result != KERN_SUCCESS) | |
200 | return EINVAL; | |
201 | } | |
202 | shmmap_s->shmid = -1; | |
203 | shmseg->shm_dtime = time_second; | |
204 | if ((--shmseg->shm_nattch <= 0) && | |
205 | (shmseg->shm_perm.mode & SHMSEG_REMOVED)) { | |
206 | shm_deallocate_segment(shmseg); | |
207 | shm_last_free = segnum; | |
208 | } | |
209 | return 0; | |
210 | } | |
211 | ||
212 | struct shmdt_args { | |
213 | void *shmaddr; | |
214 | }; | |
215 | ||
216 | int | |
217 | shmdt(p, uap, retval) | |
218 | struct proc *p; | |
219 | struct shmdt_args *uap; | |
220 | register_t *retval; | |
221 | { | |
222 | struct shmmap_state *shmmap_s; | |
223 | int i; | |
224 | ||
225 | AUDIT_ARG(svipc_addr, uap->shmaddr); | |
226 | if (!shm_inited) | |
227 | return(EINVAL); | |
228 | shmmap_s = (struct shmmap_state *)p->vm_shm; | |
229 | if (shmmap_s == NULL) | |
230 | return EINVAL; | |
231 | for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) | |
232 | if (shmmap_s->shmid != -1 && | |
233 | shmmap_s->va == (vm_offset_t)uap->shmaddr) | |
234 | break; | |
235 | if (i == shminfo.shmseg) | |
236 | return EINVAL; | |
237 | return shm_delete_mapping(p, shmmap_s, 1); | |
238 | } | |
239 | ||
240 | #ifndef _SYS_SYSPROTO_H_ | |
241 | struct shmat_args { | |
242 | int shmid; | |
243 | void *shmaddr; | |
244 | int shmflg; | |
245 | }; | |
246 | #endif | |
247 | ||
248 | int | |
249 | shmat(p, uap, retval) | |
250 | struct proc *p; | |
251 | struct shmat_args *uap; | |
252 | register_t *retval; | |
253 | { | |
254 | int error, i, flags; | |
255 | struct ucred *cred = p->p_ucred; | |
256 | struct shmid_ds *shmseg; | |
257 | struct shmmap_state *shmmap_s = NULL; | |
258 | struct shm_handle *shm_handle; | |
259 | vm_offset_t attach_va; | |
260 | vm_prot_t prot; | |
261 | vm_size_t size; | |
262 | kern_return_t rv; | |
263 | ||
264 | AUDIT_ARG(svipc_id, uap->shmid); | |
265 | AUDIT_ARG(svipc_addr, uap->shmaddr); | |
266 | if (!shm_inited) | |
267 | return(EINVAL); | |
268 | shmmap_s = (struct shmmap_state *)p->vm_shm; | |
269 | if (shmmap_s == NULL) { | |
270 | size = shminfo.shmseg * sizeof(struct shmmap_state); | |
271 | shmmap_s = (struct shmmap_state *)_MALLOC(size, M_SHM, M_WAITOK); | |
272 | for (i = 0; i < shminfo.shmseg; i++) | |
273 | shmmap_s[i].shmid = -1; | |
274 | p->vm_shm = (caddr_t)shmmap_s; | |
275 | } | |
276 | shmseg = shm_find_segment_by_shmid(uap->shmid); | |
277 | if (shmseg == NULL) | |
278 | return EINVAL; | |
279 | ||
280 | AUDIT_ARG(svipc_perm, &shmseg->shm_perm); | |
281 | error = ipcperm(cred, &shmseg->shm_perm, | |
282 | (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); | |
283 | if (error) | |
284 | return error; | |
285 | for (i = 0; i < shminfo.shmseg; i++) { | |
286 | if (shmmap_s->shmid == -1) | |
287 | break; | |
288 | shmmap_s++; | |
289 | } | |
290 | if (i >= shminfo.shmseg) | |
291 | return EMFILE; | |
292 | size = round_page_32(shmseg->shm_segsz); | |
293 | prot = VM_PROT_READ; | |
294 | if ((uap->shmflg & SHM_RDONLY) == 0) | |
295 | prot |= VM_PROT_WRITE; | |
296 | flags = MAP_ANON | MAP_SHARED; | |
297 | if (uap->shmaddr) { | |
298 | flags |= MAP_FIXED; | |
299 | if (uap->shmflg & SHM_RND) | |
300 | attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1); | |
301 | else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) | |
302 | attach_va = (vm_offset_t)uap->shmaddr; | |
303 | else | |
304 | return EINVAL; | |
305 | } else { | |
306 | attach_va = round_page_32((unsigned int)uap->shmaddr); | |
307 | } | |
308 | ||
309 | shm_handle = shmseg->shm_internal; | |
310 | rv = vm_map(current_map(), &attach_va, size, 0, (flags & MAP_FIXED)? FALSE: TRUE, | |
311 | shm_handle->shm_object, 0, FALSE, prot, prot, VM_INHERIT_DEFAULT); | |
312 | if (rv != KERN_SUCCESS) | |
313 | goto out; | |
314 | rv = vm_inherit(current_map(), attach_va, size, | |
315 | VM_INHERIT_SHARE); | |
316 | if (rv != KERN_SUCCESS) { | |
317 | (void) vm_deallocate(current_map(), attach_va, size); | |
318 | goto out; | |
319 | } | |
320 | ||
321 | shmmap_s->va = attach_va; | |
322 | shmmap_s->shmid = uap->shmid; | |
323 | shmseg->shm_lpid = p->p_pid; | |
324 | shmseg->shm_atime = time_second; | |
325 | shmseg->shm_nattch++; | |
326 | *retval = attach_va; | |
327 | return( 0); | |
328 | out: | |
329 | switch (rv) { | |
330 | case KERN_INVALID_ADDRESS: | |
331 | case KERN_NO_SPACE: | |
332 | return (ENOMEM); | |
333 | case KERN_PROTECTION_FAILURE: | |
334 | return (EACCES); | |
335 | default: | |
336 | return (EINVAL); | |
337 | } | |
338 | ||
339 | } | |
340 | ||
341 | struct oshmid_ds { | |
342 | struct ipc_perm shm_perm; /* operation perms */ | |
343 | int shm_segsz; /* size of segment (bytes) */ | |
344 | ushort shm_cpid; /* pid, creator */ | |
345 | ushort shm_lpid; /* pid, last operation */ | |
346 | short shm_nattch; /* no. of current attaches */ | |
347 | time_t shm_atime; /* last attach time */ | |
348 | time_t shm_dtime; /* last detach time */ | |
349 | time_t shm_ctime; /* last change time */ | |
350 | void *shm_handle; /* internal handle for shm segment */ | |
351 | }; | |
352 | ||
353 | struct oshmctl_args { | |
354 | int shmid; | |
355 | int cmd; | |
356 | struct oshmid_ds *ubuf; | |
357 | }; | |
358 | ||
359 | static int | |
360 | oshmctl(p, uap, retval) | |
361 | struct proc *p; | |
362 | struct oshmctl_args *uap; | |
363 | register_t *retval; | |
364 | { | |
365 | #ifdef COMPAT_43 | |
366 | int error; | |
367 | struct ucred *cred = p->p_ucred; | |
368 | struct shmid_ds *shmseg; | |
369 | struct oshmid_ds outbuf; | |
370 | ||
371 | if (!shm_inited) | |
372 | return(EINVAL); | |
373 | shmseg = shm_find_segment_by_shmid(uap->shmid); | |
374 | if (shmseg == NULL) | |
375 | return EINVAL; | |
376 | switch (uap->cmd) { | |
377 | case IPC_STAT: | |
378 | error = ipcperm(cred, &shmseg->shm_perm, IPC_R); | |
379 | if (error) | |
380 | return error; | |
381 | outbuf.shm_perm = shmseg->shm_perm; | |
382 | outbuf.shm_segsz = shmseg->shm_segsz; | |
383 | outbuf.shm_cpid = shmseg->shm_cpid; | |
384 | outbuf.shm_lpid = shmseg->shm_lpid; | |
385 | outbuf.shm_nattch = shmseg->shm_nattch; | |
386 | outbuf.shm_atime = shmseg->shm_atime; | |
387 | outbuf.shm_dtime = shmseg->shm_dtime; | |
388 | outbuf.shm_ctime = shmseg->shm_ctime; | |
389 | outbuf.shm_handle = shmseg->shm_internal; | |
390 | error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf)); | |
391 | if (error) | |
392 | return error; | |
393 | break; | |
394 | default: | |
395 | /* XXX casting to (sy_call_t *) is bogus, as usual. */ | |
396 | return ((sy_call_t *)shmctl)(p, uap, retval); | |
397 | } | |
398 | return 0; | |
399 | #else | |
400 | return EINVAL; | |
401 | #endif | |
402 | } | |
403 | ||
404 | #ifndef _SYS_SYSPROTO_H_ | |
405 | struct shmctl_args { | |
406 | int shmid; | |
407 | int cmd; | |
408 | struct shmid_ds *buf; | |
409 | }; | |
410 | #endif | |
411 | ||
412 | int | |
413 | shmctl(p, uap, retval) | |
414 | struct proc *p; | |
415 | struct shmctl_args *uap; | |
416 | register_t *retval; | |
417 | { | |
418 | int error; | |
419 | struct ucred *cred = p->p_ucred; | |
420 | struct shmid_ds inbuf; | |
421 | struct shmid_ds *shmseg; | |
422 | ||
423 | AUDIT_ARG(svipc_cmd, uap->cmd); | |
424 | AUDIT_ARG(svipc_id, uap->shmid); | |
425 | if (!shm_inited) | |
426 | return(EINVAL); | |
427 | shmseg = shm_find_segment_by_shmid(uap->shmid); | |
428 | if (shmseg == NULL) | |
429 | return EINVAL; | |
430 | /* XXAUDIT: This is the perms BEFORE any change by this call. This | |
431 | * may not be what is desired. | |
432 | */ | |
433 | AUDIT_ARG(svipc_perm, &shmseg->shm_perm); | |
434 | ||
435 | switch (uap->cmd) { | |
436 | case IPC_STAT: | |
437 | error = ipcperm(cred, &shmseg->shm_perm, IPC_R); | |
438 | if (error) | |
439 | return error; | |
440 | error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf)); | |
441 | if (error) | |
442 | return error; | |
443 | break; | |
444 | case IPC_SET: | |
445 | error = ipcperm(cred, &shmseg->shm_perm, IPC_M); | |
446 | if (error) | |
447 | return error; | |
448 | error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf)); | |
449 | if (error) | |
450 | return error; | |
451 | shmseg->shm_perm.uid = inbuf.shm_perm.uid; | |
452 | shmseg->shm_perm.gid = inbuf.shm_perm.gid; | |
453 | shmseg->shm_perm.mode = | |
454 | (shmseg->shm_perm.mode & ~ACCESSPERMS) | | |
455 | (inbuf.shm_perm.mode & ACCESSPERMS); | |
456 | shmseg->shm_ctime = time_second; | |
457 | break; | |
458 | case IPC_RMID: | |
459 | error = ipcperm(cred, &shmseg->shm_perm, IPC_M); | |
460 | if (error) | |
461 | return error; | |
462 | shmseg->shm_perm.key = IPC_PRIVATE; | |
463 | shmseg->shm_perm.mode |= SHMSEG_REMOVED; | |
464 | if (shmseg->shm_nattch <= 0) { | |
465 | shm_deallocate_segment(shmseg); | |
466 | shm_last_free = IPCID_TO_IX(uap->shmid); | |
467 | } | |
468 | break; | |
469 | #if 0 | |
470 | case SHM_LOCK: | |
471 | case SHM_UNLOCK: | |
472 | #endif | |
473 | default: | |
474 | return EINVAL; | |
475 | } | |
476 | return 0; | |
477 | } | |
478 | ||
479 | #ifndef _SYS_SYSPROTO_H_ | |
480 | struct shmget_args { | |
481 | key_t key; | |
482 | size_t size; | |
483 | int shmflg; | |
484 | }; | |
485 | #endif | |
486 | ||
487 | static int | |
488 | shmget_existing(p, uap, mode, segnum, retval) | |
489 | struct proc *p; | |
490 | struct shmget_args *uap; | |
491 | int mode; | |
492 | int segnum; | |
493 | int *retval; | |
494 | { | |
495 | struct shmid_ds *shmseg; | |
496 | struct ucred *cred = p->p_ucred; | |
497 | int error; | |
498 | ||
499 | shmseg = &shmsegs[segnum]; | |
500 | if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { | |
501 | /* | |
502 | * This segment is in the process of being allocated. Wait | |
503 | * until it's done, and look the key up again (in case the | |
504 | * allocation failed or it was freed). | |
505 | */ | |
506 | shmseg->shm_perm.mode |= SHMSEG_WANTED; | |
507 | error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0); | |
508 | if (error) | |
509 | return error; | |
510 | return EAGAIN; | |
511 | } | |
512 | error = ipcperm(cred, &shmseg->shm_perm, mode); | |
513 | if (error) | |
514 | return error; | |
515 | if (uap->size && uap->size > shmseg->shm_segsz) | |
516 | return EINVAL; | |
517 | if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) | |
518 | return EEXIST; | |
519 | *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static int | |
524 | shmget_allocate_segment(p, uap, mode, retval) | |
525 | struct proc *p; | |
526 | struct shmget_args *uap; | |
527 | int mode; | |
528 | int * retval; | |
529 | { | |
530 | int i, segnum, shmid, size; | |
531 | struct ucred *cred = p->p_ucred; | |
532 | struct shmid_ds *shmseg; | |
533 | struct shm_handle *shm_handle; | |
534 | kern_return_t kret; | |
535 | vm_offset_t user_addr; | |
536 | void * mem_object; | |
537 | ||
538 | if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax) | |
539 | return EINVAL; | |
540 | if (shm_nused >= shminfo.shmmni) /* any shmids left? */ | |
541 | return ENOSPC; | |
542 | size = round_page_32(uap->size); | |
543 | if (shm_committed + btoc(size) > shminfo.shmall) | |
544 | return ENOMEM; | |
545 | if (shm_last_free < 0) { | |
546 | for (i = 0; i < shminfo.shmmni; i++) | |
547 | if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) | |
548 | break; | |
549 | if (i == shminfo.shmmni) | |
550 | panic("shmseg free count inconsistent"); | |
551 | segnum = i; | |
552 | } else { | |
553 | segnum = shm_last_free; | |
554 | shm_last_free = -1; | |
555 | } | |
556 | shmseg = &shmsegs[segnum]; | |
557 | /* | |
558 | * In case we sleep in malloc(), mark the segment present but deleted | |
559 | * so that noone else tries to create the same key. | |
560 | */ | |
561 | kret = vm_allocate(current_map(), &user_addr, size, TRUE); | |
562 | if (kret != KERN_SUCCESS) | |
563 | goto out; | |
564 | ||
565 | kret = mach_make_memory_entry (current_map(), &size, | |
566 | user_addr, VM_PROT_DEFAULT, &mem_object, 0); | |
567 | ||
568 | if (kret != KERN_SUCCESS) | |
569 | goto out; | |
570 | shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; | |
571 | shmseg->shm_perm.key = uap->key; | |
572 | shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff; | |
573 | shm_handle = (struct shm_handle *) | |
574 | _MALLOC(sizeof(struct shm_handle), M_SHM, M_WAITOK); | |
575 | shm_handle->shm_object = mem_object; | |
576 | shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); | |
577 | ||
578 | shmseg->shm_internal = shm_handle; | |
579 | shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; | |
580 | shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; | |
581 | shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | | |
582 | (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; | |
583 | shmseg->shm_segsz = uap->size; | |
584 | shmseg->shm_cpid = p->p_pid; | |
585 | shmseg->shm_lpid = shmseg->shm_nattch = 0; | |
586 | shmseg->shm_atime = shmseg->shm_dtime = 0; | |
587 | shmseg->shm_ctime = time_second; | |
588 | shm_committed += btoc(size); | |
589 | shm_nused++; | |
590 | AUDIT_ARG(svipc_perm, &shmseg->shm_perm); | |
591 | if (shmseg->shm_perm.mode & SHMSEG_WANTED) { | |
592 | /* | |
593 | * Somebody else wanted this key while we were asleep. Wake | |
594 | * them up now. | |
595 | */ | |
596 | shmseg->shm_perm.mode &= ~SHMSEG_WANTED; | |
597 | wakeup((caddr_t)shmseg); | |
598 | } | |
599 | *retval = shmid; | |
600 | AUDIT_ARG(svipc_id, shmid); | |
601 | return 0; | |
602 | out: | |
603 | switch (kret) { | |
604 | case KERN_INVALID_ADDRESS: | |
605 | case KERN_NO_SPACE: | |
606 | return (ENOMEM); | |
607 | case KERN_PROTECTION_FAILURE: | |
608 | return (EACCES); | |
609 | default: | |
610 | return (EINVAL); | |
611 | } | |
612 | ||
613 | } | |
614 | ||
615 | int | |
616 | shmget(p, uap, retval) | |
617 | struct proc *p; | |
618 | struct shmget_args *uap; | |
619 | register_t *retval; | |
620 | { | |
621 | int segnum, mode, error; | |
622 | ||
623 | /* Auditing is actually done in shmget_allocate_segment() */ | |
624 | if (!shm_inited) | |
625 | return(EINVAL); | |
626 | ||
627 | mode = uap->shmflg & ACCESSPERMS; | |
628 | if (uap->key != IPC_PRIVATE) { | |
629 | again: | |
630 | segnum = shm_find_segment_by_key(uap->key); | |
631 | if (segnum >= 0) { | |
632 | error = shmget_existing(p, uap, mode, segnum, retval); | |
633 | if (error == EAGAIN) | |
634 | goto again; | |
635 | return(error); | |
636 | } | |
637 | if ((uap->shmflg & IPC_CREAT) == 0) | |
638 | return ENOENT; | |
639 | } | |
640 | return( shmget_allocate_segment(p, uap, mode, retval));; | |
641 | /*NOTREACHED*/ | |
642 | ||
643 | } | |
644 | ||
645 | struct shmsys_args { | |
646 | u_int which; | |
647 | int a2; | |
648 | int a3; | |
649 | int a4; | |
650 | }; | |
651 | int | |
652 | shmsys(p, uap, retval) | |
653 | struct proc *p; | |
654 | /* XXX actually varargs. */ | |
655 | struct shmsys_args *uap; | |
656 | register_t *retval; | |
657 | { | |
658 | ||
659 | if (!shm_inited) | |
660 | return(EINVAL); | |
661 | ||
662 | if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0])) | |
663 | return EINVAL; | |
664 | return ((*shmcalls[uap->which])(p, &uap->a2, retval)); | |
665 | } | |
666 | ||
667 | void | |
668 | shmfork(p1, p2) | |
669 | struct proc *p1, *p2; | |
670 | { | |
671 | struct shmmap_state *shmmap_s; | |
672 | size_t size; | |
673 | int i; | |
674 | ||
675 | if (!shm_inited) | |
676 | return; | |
677 | size = shminfo.shmseg * sizeof(struct shmmap_state); | |
678 | shmmap_s = (struct shmmap_state *)_MALLOC(size, M_SHM, M_WAITOK); | |
679 | bcopy((caddr_t)p1->vm_shm, (caddr_t)shmmap_s, size); | |
680 | p2->vm_shm = (caddr_t)shmmap_s; | |
681 | for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) | |
682 | if (shmmap_s->shmid != -1) | |
683 | shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++; | |
684 | } | |
685 | ||
686 | void | |
687 | shmexit(p) | |
688 | struct proc *p; | |
689 | { | |
690 | struct shmmap_state *shmmap_s; | |
691 | int i; | |
692 | ||
693 | shmmap_s = (struct shmmap_state *)p->vm_shm; | |
694 | for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) | |
695 | if (shmmap_s->shmid != -1) | |
696 | shm_delete_mapping(p, shmmap_s, 1); | |
697 | FREE((caddr_t)p->vm_shm, M_SHM); | |
698 | p->vm_shm = NULL; | |
699 | } | |
700 | ||
701 | /* | |
702 | * shmexec() is like shmexit(), only it doesn't delete the mappings, | |
703 | * since the old address space has already been destroyed and the new | |
704 | * one instantiated. Instead, it just does the housekeeping work we | |
705 | * need to do to keep the System V shared memory subsystem sane. | |
706 | */ | |
707 | __private_extern__ void | |
708 | shmexec(p) | |
709 | struct proc *p; | |
710 | { | |
711 | struct shmmap_state *shmmap_s; | |
712 | int i; | |
713 | ||
714 | shmmap_s = (struct shmmap_state *)p->vm_shm; | |
715 | for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) | |
716 | if (shmmap_s->shmid != -1) | |
717 | shm_delete_mapping(p, shmmap_s, 0); | |
718 | FREE((caddr_t)p->vm_shm, M_SHM); | |
719 | p->vm_shm = NULL; | |
720 | } | |
721 | ||
722 | void | |
723 | shminit(dummy) | |
724 | void *dummy; | |
725 | { | |
726 | int i; | |
727 | int s; | |
728 | ||
729 | if (!shm_inited) { | |
730 | s = sizeof(struct shmid_ds) * shminfo.shmmni; | |
731 | ||
732 | MALLOC(shmsegs, struct shmid_ds *, s, | |
733 | M_SHM, M_WAITOK); | |
734 | for (i = 0; i < shminfo.shmmni; i++) { | |
735 | shmsegs[i].shm_perm.mode = SHMSEG_FREE; | |
736 | shmsegs[i].shm_perm.seq = 0; | |
737 | } | |
738 | shm_last_free = 0; | |
739 | shm_nused = 0; | |
740 | shm_committed = 0; | |
741 | shm_inited = 1; | |
742 | } | |
743 | } | |
744 | ||
745 | /* (struct sysctl_oid *oidp, void *arg1, int arg2, \ | |
746 | struct sysctl_req *req) */ | |
747 | static int | |
748 | sysctl_shminfo SYSCTL_HANDLER_ARGS | |
749 | { | |
750 | int error = 0; | |
751 | ||
752 | error = SYSCTL_OUT(req, arg1, sizeof(int)); | |
753 | if (error || !req->newptr) | |
754 | return(error); | |
755 | ||
756 | /* Set the values only if shared memory is not initialised */ | |
757 | if (!shm_inited) { | |
758 | if (error = SYSCTL_IN(req, arg1, sizeof(int))) | |
759 | return(error); | |
760 | if (arg1 == &shminfo.shmmax) { | |
761 | if (shminfo.shmmax & PAGE_MASK) { | |
762 | shminfo.shmmax = -1; | |
763 | return(EINVAL); | |
764 | } | |
765 | } | |
766 | ||
767 | /* Initialize only when all values are set */ | |
768 | if ((shminfo.shmmax != -1) && | |
769 | (shminfo.shmmin != -1) && | |
770 | (shminfo.shmmni != -1) && | |
771 | (shminfo.shmseg != -1) && | |
772 | (shminfo.shmall != -1)) { | |
773 | shminit(NULL); | |
774 | } | |
775 | } | |
776 | return(0); | |
777 | } | |
778 | ||
779 | SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); | |
780 | ||
781 | SYSCTL_PROC(_kern_sysv, KSYSV_SHMMAX, shmmax, CTLTYPE_INT | CTLFLAG_RW, | |
782 | &shminfo.shmmax, 0, &sysctl_shminfo ,"I","shmmax"); | |
783 | ||
784 | SYSCTL_PROC(_kern_sysv, KSYSV_SHMMIN, shmmin, CTLTYPE_INT | CTLFLAG_RW, | |
785 | &shminfo.shmmin, 0, &sysctl_shminfo ,"I","shmmin"); | |
786 | ||
787 | SYSCTL_PROC(_kern_sysv, KSYSV_SHMMNI, shmmni, CTLTYPE_INT | CTLFLAG_RW, | |
788 | &shminfo.shmmni, 0, &sysctl_shminfo ,"I","shmmni"); | |
789 | ||
790 | SYSCTL_PROC(_kern_sysv, KSYSV_SHMSEG, shmseg, CTLTYPE_INT | CTLFLAG_RW, | |
791 | &shminfo.shmseg, 0, &sysctl_shminfo ,"I","shmseg"); | |
792 | ||
793 | SYSCTL_PROC(_kern_sysv, KSYSV_SHMALL, shmall, CTLTYPE_INT | CTLFLAG_RW, | |
794 | &shminfo.shmall, 0, &sysctl_shminfo ,"I","shmall"); | |
795 | ||
796 |