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