sysv_shm.c revision 1.21 1 1.11 hpeyerl /*
2 1.17 mycroft * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
3 1.11 hpeyerl *
4 1.11 hpeyerl * Redistribution and use in source and binary forms, with or without
5 1.11 hpeyerl * modification, are permitted provided that the following conditions
6 1.11 hpeyerl * are met:
7 1.11 hpeyerl * 1. Redistributions of source code must retain the above copyright
8 1.11 hpeyerl * notice, this list of conditions and the following disclaimer.
9 1.17 mycroft * 2. Redistributions in binary form must reproduce the above copyright
10 1.17 mycroft * notice, this list of conditions and the following disclaimer in the
11 1.17 mycroft * documentation and/or other materials provided with the distribution.
12 1.17 mycroft * 3. All advertising materials mentioning features or use of this software
13 1.17 mycroft * must display the following acknowledgement:
14 1.17 mycroft * This product includes software developed by Adam Glass and Charles
15 1.17 mycroft * Hannum.
16 1.17 mycroft * 4. The names of the authors may not be used to endorse or promote products
17 1.11 hpeyerl * derived from this software without specific prior written permission.
18 1.11 hpeyerl *
19 1.17 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 1.17 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.17 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.17 mycroft * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.17 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.17 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.17 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.17 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.17 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.17 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.11 hpeyerl */
30 1.11 hpeyerl
31 1.11 hpeyerl #include <sys/types.h>
32 1.11 hpeyerl #include <sys/param.h>
33 1.11 hpeyerl #include <sys/kernel.h>
34 1.11 hpeyerl #include <sys/shm.h>
35 1.11 hpeyerl #include <sys/proc.h>
36 1.11 hpeyerl #include <sys/uio.h>
37 1.11 hpeyerl #include <sys/time.h>
38 1.11 hpeyerl #include <sys/malloc.h>
39 1.11 hpeyerl #include <sys/mman.h>
40 1.11 hpeyerl #include <sys/systm.h>
41 1.12 mycroft #include <sys/stat.h>
42 1.11 hpeyerl
43 1.11 hpeyerl #include <vm/vm.h>
44 1.11 hpeyerl #include <vm/vm_map.h>
45 1.11 hpeyerl #include <vm/vm_map.h>
46 1.11 hpeyerl #include <vm/vm_kern.h>
47 1.11 hpeyerl
48 1.11 hpeyerl /*
49 1.11 hpeyerl * Provides the following externally accessible functions:
50 1.11 hpeyerl *
51 1.11 hpeyerl * shminit(void); initialization
52 1.11 hpeyerl * shmexit(struct proc *) cleanup
53 1.11 hpeyerl * shmfork(struct proc *, struct proc *, int) fork handling
54 1.11 hpeyerl * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
55 1.11 hpeyerl *
56 1.11 hpeyerl * Structures:
57 1.11 hpeyerl * shmsegs (an array of 'struct shmid_ds')
58 1.11 hpeyerl * per proc array of 'struct shmmap_state'
59 1.11 hpeyerl */
60 1.12 mycroft
61 1.12 mycroft int shmat(), shmctl(), shmdt(), shmget();
62 1.12 mycroft int (*shmcalls[])() = { shmat, shmctl, shmdt, shmget };
63 1.11 hpeyerl
64 1.16 mycroft #define SHMSEG_FREE 0x0200
65 1.16 mycroft #define SHMSEG_REMOVED 0x0400
66 1.16 mycroft #define SHMSEG_ALLOCATED 0x0800
67 1.16 mycroft #define SHMSEG_WANTED 0x1000
68 1.11 hpeyerl
69 1.11 hpeyerl vm_map_t sysvshm_map;
70 1.11 hpeyerl int shm_last_free, shm_nused, shm_committed;
71 1.11 hpeyerl
72 1.11 hpeyerl struct shm_handle {
73 1.11 hpeyerl vm_offset_t kva;
74 1.11 hpeyerl };
75 1.11 hpeyerl
76 1.11 hpeyerl struct shmmap_state {
77 1.11 hpeyerl vm_offset_t va;
78 1.11 hpeyerl int shmid;
79 1.11 hpeyerl };
80 1.11 hpeyerl
81 1.12 mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
82 1.12 mycroft static int shm_find_segment_by_key __P((key_t));
83 1.15 mycroft static struct shmid_ds *shm_find_segment_by_shmid __P((int));
84 1.12 mycroft static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
85 1.11 hpeyerl
86 1.12 mycroft static int
87 1.12 mycroft shm_find_segment_by_key(key)
88 1.11 hpeyerl key_t key;
89 1.11 hpeyerl {
90 1.11 hpeyerl int i;
91 1.11 hpeyerl
92 1.12 mycroft for (i = 0; i < shminfo.shmmni; i++)
93 1.12 mycroft if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
94 1.12 mycroft shmsegs[i].shm_perm.key == key)
95 1.12 mycroft return i;
96 1.11 hpeyerl return -1;
97 1.11 hpeyerl }
98 1.11 hpeyerl
99 1.12 mycroft static struct shmid_ds *
100 1.15 mycroft shm_find_segment_by_shmid(shmid)
101 1.11 hpeyerl int shmid;
102 1.11 hpeyerl {
103 1.11 hpeyerl int segnum;
104 1.11 hpeyerl struct shmid_ds *shmseg;
105 1.11 hpeyerl
106 1.11 hpeyerl segnum = IPCID_TO_IX(shmid);
107 1.12 mycroft if (segnum < 0 || segnum >= shminfo.shmmni)
108 1.11 hpeyerl return NULL;
109 1.11 hpeyerl shmseg = &shmsegs[segnum];
110 1.12 mycroft if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
111 1.12 mycroft != SHMSEG_ALLOCATED ||
112 1.12 mycroft shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
113 1.11 hpeyerl return NULL;
114 1.11 hpeyerl return shmseg;
115 1.11 hpeyerl }
116 1.11 hpeyerl
117 1.12 mycroft static void
118 1.12 mycroft shm_deallocate_segment(shmseg)
119 1.12 mycroft struct shmid_ds *shmseg;
120 1.12 mycroft {
121 1.12 mycroft struct shm_handle *shm_handle;
122 1.14 mycroft size_t size;
123 1.12 mycroft
124 1.12 mycroft shm_handle = shmseg->shm_internal;
125 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
126 1.14 mycroft vm_deallocate(sysvshm_map, shm_handle->kva, size);
127 1.12 mycroft free((caddr_t)shm_handle, M_SHM);
128 1.12 mycroft shmseg->shm_internal = NULL;
129 1.14 mycroft shm_committed -= btoc(size);
130 1.12 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
131 1.12 mycroft }
132 1.12 mycroft
133 1.12 mycroft static int
134 1.12 mycroft shm_delete_mapping(p, shmmap_s)
135 1.11 hpeyerl struct proc *p;
136 1.11 hpeyerl struct shmmap_state *shmmap_s;
137 1.11 hpeyerl {
138 1.12 mycroft struct shmid_ds *shmseg;
139 1.12 mycroft int segnum, result;
140 1.14 mycroft size_t size;
141 1.11 hpeyerl
142 1.12 mycroft segnum = IPCID_TO_IX(shmmap_s->shmid);
143 1.12 mycroft shmseg = &shmsegs[segnum];
144 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
145 1.14 mycroft result = vm_deallocate(&p->p_vmspace->vm_map, shmmap_s->va, size);
146 1.11 hpeyerl if (result != KERN_SUCCESS)
147 1.11 hpeyerl return EINVAL;
148 1.12 mycroft shmmap_s->shmid = -1;
149 1.11 hpeyerl shmseg->shm_dtime = time.tv_sec;
150 1.12 mycroft if ((--shmseg->shm_nattch <= 0) &&
151 1.11 hpeyerl (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
152 1.12 mycroft shm_deallocate_segment(shmseg);
153 1.12 mycroft shm_last_free = segnum;
154 1.11 hpeyerl }
155 1.11 hpeyerl return 0;
156 1.11 hpeyerl }
157 1.11 hpeyerl
158 1.12 mycroft struct shmdt_args {
159 1.12 mycroft void *shmaddr;
160 1.12 mycroft };
161 1.12 mycroft int
162 1.12 mycroft shmdt(p, uap, retval)
163 1.11 hpeyerl struct proc *p;
164 1.11 hpeyerl struct shmdt_args *uap;
165 1.11 hpeyerl int *retval;
166 1.11 hpeyerl {
167 1.12 mycroft struct shmmap_state *shmmap_s;
168 1.11 hpeyerl int i;
169 1.11 hpeyerl
170 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
171 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
172 1.12 mycroft if (shmmap_s->shmid != -1 &&
173 1.12 mycroft shmmap_s->va == (vm_offset_t)uap->shmaddr)
174 1.12 mycroft break;
175 1.11 hpeyerl if (i == shminfo.shmseg)
176 1.11 hpeyerl return EINVAL;
177 1.12 mycroft return shm_delete_mapping(p, shmmap_s);
178 1.11 hpeyerl }
179 1.11 hpeyerl
180 1.12 mycroft struct shmat_args {
181 1.12 mycroft int shmid;
182 1.12 mycroft void *shmaddr;
183 1.12 mycroft int shmflg;
184 1.12 mycroft };
185 1.12 mycroft int
186 1.12 mycroft shmat(p, uap, retval)
187 1.11 hpeyerl struct proc *p;
188 1.11 hpeyerl struct shmat_args *uap;
189 1.11 hpeyerl int *retval;
190 1.11 hpeyerl {
191 1.12 mycroft int error, i, flags;
192 1.12 mycroft struct ucred *cred = p->p_ucred;
193 1.11 hpeyerl struct shmid_ds *shmseg;
194 1.11 hpeyerl struct shmmap_state *shmmap_s = NULL;
195 1.11 hpeyerl vm_offset_t attach_va;
196 1.11 hpeyerl vm_prot_t prot;
197 1.11 hpeyerl vm_size_t size;
198 1.11 hpeyerl
199 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
200 1.12 mycroft if (shmmap_s == NULL) {
201 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
202 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
203 1.18 cgd for (i = 0; i < shminfo.shmseg; i++)
204 1.18 cgd shmmap_s[i].shmid = -1;
205 1.12 mycroft p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
206 1.12 mycroft }
207 1.15 mycroft shmseg = shm_find_segment_by_shmid(uap->shmid);
208 1.11 hpeyerl if (shmseg == NULL)
209 1.11 hpeyerl return EINVAL;
210 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm,
211 1.12 mycroft (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W))
212 1.12 mycroft return error;
213 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++) {
214 1.12 mycroft if (shmmap_s->shmid == -1)
215 1.12 mycroft break;
216 1.12 mycroft shmmap_s++;
217 1.11 hpeyerl }
218 1.12 mycroft if (i >= shminfo.shmseg)
219 1.12 mycroft return EMFILE;
220 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
221 1.12 mycroft prot = VM_PROT_READ;
222 1.12 mycroft if ((uap->shmflg & SHM_RDONLY) == 0)
223 1.12 mycroft prot |= VM_PROT_WRITE;
224 1.12 mycroft flags = MAP_ANON | MAP_SHARED;
225 1.11 hpeyerl if (uap->shmaddr) {
226 1.12 mycroft flags |= MAP_FIXED;
227 1.11 hpeyerl if (uap->shmflg & SHM_RND)
228 1.12 mycroft attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
229 1.14 mycroft else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
230 1.14 mycroft attach_va = (vm_offset_t)uap->shmaddr;
231 1.11 hpeyerl else
232 1.14 mycroft return EINVAL;
233 1.12 mycroft } else {
234 1.20 mycroft /* This is just a hint to vm_mmap() about where to put it. */
235 1.20 mycroft attach_va = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
236 1.11 hpeyerl }
237 1.12 mycroft error = vm_mmap(&p->p_vmspace->vm_map, &attach_va, size, prot,
238 1.12 mycroft VM_PROT_DEFAULT, flags, uap->shmid, 0);
239 1.11 hpeyerl if (error)
240 1.11 hpeyerl return error;
241 1.12 mycroft shmmap_s->va = attach_va;
242 1.11 hpeyerl shmmap_s->shmid = uap->shmid;
243 1.11 hpeyerl shmseg->shm_lpid = p->p_pid;
244 1.11 hpeyerl shmseg->shm_atime = time.tv_sec;
245 1.11 hpeyerl shmseg->shm_nattch++;
246 1.12 mycroft *retval = attach_va;
247 1.11 hpeyerl return 0;
248 1.11 hpeyerl }
249 1.11 hpeyerl
250 1.12 mycroft struct shmctl_args {
251 1.12 mycroft int shmid;
252 1.12 mycroft int cmd;
253 1.12 mycroft struct shmat_ds *ubuf;
254 1.12 mycroft };
255 1.12 mycroft int
256 1.12 mycroft shmctl(p, uap, retval)
257 1.11 hpeyerl struct proc *p;
258 1.11 hpeyerl struct shmctl_args *uap;
259 1.11 hpeyerl int *retval;
260 1.11 hpeyerl {
261 1.11 hpeyerl int error, segnum;
262 1.12 mycroft struct ucred *cred = p->p_ucred;
263 1.11 hpeyerl struct shmid_ds inbuf;
264 1.11 hpeyerl struct shmid_ds *shmseg;
265 1.11 hpeyerl
266 1.15 mycroft shmseg = shm_find_segment_by_shmid(uap->shmid);
267 1.11 hpeyerl if (shmseg == NULL)
268 1.11 hpeyerl return EINVAL;
269 1.11 hpeyerl switch (uap->cmd) {
270 1.11 hpeyerl case IPC_STAT:
271 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_R))
272 1.11 hpeyerl return error;
273 1.12 mycroft if (error = copyout((caddr_t)shmseg, uap->ubuf, sizeof(inbuf)))
274 1.11 hpeyerl return error;
275 1.11 hpeyerl break;
276 1.11 hpeyerl case IPC_SET:
277 1.13 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
278 1.13 mycroft return error;
279 1.12 mycroft if (error = copyin(uap->ubuf, (caddr_t)&inbuf, sizeof(inbuf)))
280 1.11 hpeyerl return error;
281 1.11 hpeyerl shmseg->shm_perm.uid = inbuf.shm_perm.uid;
282 1.11 hpeyerl shmseg->shm_perm.gid = inbuf.shm_perm.gid;
283 1.12 mycroft shmseg->shm_perm.mode =
284 1.12 mycroft (shmseg->shm_perm.mode & ~ACCESSPERMS) |
285 1.12 mycroft (inbuf.shm_perm.mode & ACCESSPERMS);
286 1.12 mycroft shmseg->shm_ctime = time.tv_sec;
287 1.11 hpeyerl break;
288 1.11 hpeyerl case IPC_RMID:
289 1.13 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
290 1.13 mycroft return error;
291 1.12 mycroft shmseg->shm_perm.key = IPC_PRIVATE;
292 1.12 mycroft shmseg->shm_perm.mode |= SHMSEG_REMOVED;
293 1.12 mycroft if (shmseg->shm_nattch <= 0) {
294 1.12 mycroft shm_deallocate_segment(shmseg);
295 1.15 mycroft shm_last_free = IPCID_TO_IX(uap->shmid);
296 1.11 hpeyerl }
297 1.11 hpeyerl break;
298 1.11 hpeyerl #if 0
299 1.11 hpeyerl case SHM_LOCK:
300 1.11 hpeyerl case SHM_UNLOCK:
301 1.11 hpeyerl #endif
302 1.11 hpeyerl default:
303 1.11 hpeyerl return EINVAL;
304 1.11 hpeyerl }
305 1.11 hpeyerl return 0;
306 1.11 hpeyerl }
307 1.11 hpeyerl
308 1.12 mycroft struct shmget_args {
309 1.12 mycroft key_t key;
310 1.12 mycroft size_t size;
311 1.12 mycroft int shmflg;
312 1.12 mycroft };
313 1.12 mycroft static int
314 1.12 mycroft shmget_existing(p, uap, mode, segnum, retval)
315 1.11 hpeyerl struct proc *p;
316 1.11 hpeyerl struct shmget_args *uap;
317 1.11 hpeyerl int mode;
318 1.11 hpeyerl int segnum;
319 1.11 hpeyerl int *retval;
320 1.11 hpeyerl {
321 1.12 mycroft struct shmid_ds *shmseg;
322 1.12 mycroft struct ucred *cred = p->p_ucred;
323 1.11 hpeyerl int error;
324 1.11 hpeyerl
325 1.11 hpeyerl shmseg = &shmsegs[segnum];
326 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
327 1.16 mycroft /*
328 1.16 mycroft * This segment is in the process of being allocated. Wait
329 1.16 mycroft * until it's done, and look the key up again (in case the
330 1.16 mycroft * allocation failed or it was freed).
331 1.16 mycroft */
332 1.16 mycroft shmseg->shm_perm.mode |= SHMSEG_WANTED;
333 1.16 mycroft if (error =
334 1.16 mycroft tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0))
335 1.16 mycroft return error;
336 1.16 mycroft return EAGAIN;
337 1.16 mycroft }
338 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, mode))
339 1.11 hpeyerl return error;
340 1.12 mycroft if (uap->size && uap->size > shmseg->shm_segsz)
341 1.11 hpeyerl return EINVAL;
342 1.14 mycroft if (uap->shmflg & (IPC_CREAT | IPC_EXCL) == (IPC_CREAT | IPC_EXCL))
343 1.12 mycroft return EEXIST;
344 1.11 hpeyerl *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
345 1.11 hpeyerl return 0;
346 1.11 hpeyerl }
347 1.11 hpeyerl
348 1.14 mycroft static int
349 1.14 mycroft shmget_allocate_segment(p, uap, mode, retval)
350 1.14 mycroft struct proc *p;
351 1.14 mycroft struct shmget_args *uap;
352 1.14 mycroft int mode;
353 1.14 mycroft int *retval;
354 1.14 mycroft {
355 1.14 mycroft int i, segnum, result, shmid, size;
356 1.14 mycroft struct ucred *cred = p->p_ucred;
357 1.14 mycroft struct shmid_ds *shmseg;
358 1.14 mycroft struct shm_handle *shm_handle;
359 1.14 mycroft
360 1.14 mycroft if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
361 1.14 mycroft return EINVAL;
362 1.14 mycroft if (shm_nused >= shminfo.shmmni) /* any shmids left? */
363 1.14 mycroft return ENOSPC;
364 1.14 mycroft size = (uap->size + CLOFSET) & ~CLOFSET;
365 1.14 mycroft if (shm_committed + btoc(size) > shminfo.shmall)
366 1.14 mycroft return ENOMEM;
367 1.14 mycroft if (shm_last_free < 0) {
368 1.14 mycroft for (i = 0; i < shminfo.shmmni; i++)
369 1.14 mycroft if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
370 1.14 mycroft break;
371 1.14 mycroft if (i == shminfo.shmmni)
372 1.14 mycroft panic("shmseg free count inconsistent");
373 1.14 mycroft segnum = i;
374 1.14 mycroft } else {
375 1.14 mycroft segnum = shm_last_free;
376 1.14 mycroft shm_last_free = -1;
377 1.14 mycroft }
378 1.14 mycroft shmseg = &shmsegs[segnum];
379 1.14 mycroft /*
380 1.14 mycroft * In case we sleep in malloc(), mark the segment present but deleted
381 1.14 mycroft * so that noone else tries to create the same key.
382 1.14 mycroft */
383 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
384 1.14 mycroft shmseg->shm_perm.key = uap->key;
385 1.14 mycroft shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
386 1.14 mycroft shm_handle = (struct shm_handle *)
387 1.14 mycroft malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
388 1.14 mycroft shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
389 1.14 mycroft result = vm_mmap(sysvshm_map, &shm_handle->kva, size, VM_PROT_ALL,
390 1.14 mycroft VM_PROT_DEFAULT, MAP_ANON, shmid, 0);
391 1.14 mycroft if (result != KERN_SUCCESS) {
392 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
393 1.15 mycroft shm_last_free = segnum;
394 1.14 mycroft free((caddr_t)shm_handle, M_SHM);
395 1.16 mycroft /* Just in case. */
396 1.16 mycroft wakeup((caddr_t)shmseg);
397 1.14 mycroft return ENOMEM;
398 1.14 mycroft }
399 1.14 mycroft shmseg->shm_internal = shm_handle;
400 1.14 mycroft shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
401 1.14 mycroft shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
402 1.16 mycroft shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
403 1.16 mycroft (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
404 1.14 mycroft shmseg->shm_segsz = uap->size;
405 1.14 mycroft shmseg->shm_cpid = p->p_pid;
406 1.14 mycroft shmseg->shm_lpid = shmseg->shm_nattch = 0;
407 1.14 mycroft shmseg->shm_atime = shmseg->shm_dtime = 0;
408 1.14 mycroft shmseg->shm_ctime = time.tv_sec;
409 1.14 mycroft shm_committed += btoc(size);
410 1.14 mycroft shm_nused++;
411 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
412 1.16 mycroft /*
413 1.16 mycroft * Somebody else wanted this key while we were asleep. Wake
414 1.16 mycroft * them up now.
415 1.16 mycroft */
416 1.16 mycroft shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
417 1.16 mycroft wakeup((caddr_t)shmseg);
418 1.16 mycroft }
419 1.14 mycroft *retval = shmid;
420 1.14 mycroft return 0;
421 1.14 mycroft }
422 1.14 mycroft
423 1.12 mycroft int
424 1.12 mycroft shmget(p, uap, retval)
425 1.11 hpeyerl struct proc *p;
426 1.11 hpeyerl struct shmget_args *uap;
427 1.11 hpeyerl int *retval;
428 1.11 hpeyerl {
429 1.11 hpeyerl int segnum, mode, error;
430 1.11 hpeyerl struct shmid_ds *shmseg;
431 1.11 hpeyerl
432 1.12 mycroft mode = uap->shmflg & ACCESSPERMS;
433 1.11 hpeyerl if (uap->key != IPC_PRIVATE) {
434 1.16 mycroft again:
435 1.12 mycroft segnum = shm_find_segment_by_key(uap->key);
436 1.16 mycroft if (segnum >= 0) {
437 1.16 mycroft error = shmget_existing(p, uap, mode, segnum, retval);
438 1.16 mycroft if (error == EAGAIN)
439 1.16 mycroft goto again;
440 1.16 mycroft return error;
441 1.16 mycroft }
442 1.14 mycroft if ((uap->shmflg & IPC_CREAT) == 0)
443 1.14 mycroft return ENOENT;
444 1.11 hpeyerl }
445 1.14 mycroft return shmget_allocate_segment(p, uap, mode, retval);
446 1.11 hpeyerl }
447 1.11 hpeyerl
448 1.12 mycroft struct shmsys_args {
449 1.12 mycroft u_int which;
450 1.12 mycroft };
451 1.12 mycroft int
452 1.12 mycroft shmsys(p, uap, retval)
453 1.11 hpeyerl struct proc *p;
454 1.11 hpeyerl struct shmsys_args *uap;
455 1.11 hpeyerl int *retval;
456 1.11 hpeyerl {
457 1.11 hpeyerl
458 1.12 mycroft if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
459 1.12 mycroft return EINVAL;
460 1.12 mycroft return ((*shmcalls[uap->which])(p, &uap[1], retval));
461 1.11 hpeyerl }
462 1.11 hpeyerl
463 1.12 mycroft void
464 1.12 mycroft shmfork(p1, p2, isvfork)
465 1.12 mycroft struct proc *p1, *p2;
466 1.12 mycroft int isvfork;
467 1.11 hpeyerl {
468 1.11 hpeyerl struct shmmap_state *shmmap_s;
469 1.12 mycroft size_t size;
470 1.11 hpeyerl int i;
471 1.11 hpeyerl
472 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
473 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
474 1.12 mycroft bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
475 1.12 mycroft p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
476 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
477 1.12 mycroft if (shmmap_s->shmid != -1)
478 1.11 hpeyerl shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
479 1.11 hpeyerl }
480 1.11 hpeyerl
481 1.12 mycroft void
482 1.12 mycroft shmexit(p)
483 1.11 hpeyerl struct proc *p;
484 1.11 hpeyerl {
485 1.12 mycroft struct shmmap_state *shmmap_s;
486 1.12 mycroft struct shmid_ds *shmseg;
487 1.11 hpeyerl int i;
488 1.11 hpeyerl
489 1.11 hpeyerl shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
490 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
491 1.12 mycroft if (shmmap_s->shmid != -1)
492 1.12 mycroft shm_delete_mapping(p, shmmap_s);
493 1.12 mycroft free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
494 1.11 hpeyerl p->p_vmspace->vm_shm = NULL;
495 1.11 hpeyerl }
496 1.11 hpeyerl
497 1.12 mycroft void
498 1.12 mycroft shminit()
499 1.11 hpeyerl {
500 1.11 hpeyerl int i;
501 1.11 hpeyerl vm_offset_t garbage1, garbage2;
502 1.11 hpeyerl
503 1.11 hpeyerl /* actually this *should* be pageable. SHM_{LOCK,UNLOCK} */
504 1.11 hpeyerl sysvshm_map = kmem_suballoc(kernel_map, &garbage1, &garbage2,
505 1.21 mycroft shminfo.shmall * NBPG, TRUE);
506 1.11 hpeyerl for (i = 0; i < shminfo.shmmni; i++) {
507 1.11 hpeyerl shmsegs[i].shm_perm.mode = SHMSEG_FREE;
508 1.11 hpeyerl shmsegs[i].shm_perm.seq = 0;
509 1.11 hpeyerl }
510 1.11 hpeyerl shm_last_free = 0;
511 1.11 hpeyerl shm_nused = 0;
512 1.11 hpeyerl shm_committed = 0;
513 1.11 hpeyerl }
514