get rid of old recv char kludge, correct a debug printf
[unix-history] / usr / src / sys / kern / sysv_shm.c
CommitLineData
6f843dc9
KM
1/*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department. Originally from University of Wisconsin.
9 *
10 * %sccs.include.redist.c%
11 *
12 * from: Utah $Hdr: uipc_shm.c 1.9 89/08/14$
13 *
6be55866 14 * @(#)sysv_shm.c 7.6 (Berkeley) %G%
6f843dc9
KM
15 */
16
17/*
18 * System V shared memory routines.
19 */
20
21#ifdef SYSVSHM
22
23#include "machine/pte.h"
24
25#include "param.h"
26#include "systm.h"
161c14a2 27#include "syscontext.h"
6f843dc9
KM
28#include "kernel.h"
29#include "proc.h"
30#include "vm.h"
31#include "shm.h"
32#include "mapmem.h"
33#include "malloc.h"
34
35#ifdef HPUXCOMPAT
36#include "../hpux/hpux.h"
37#endif
38
39int shmat(), shmctl(), shmdt(), shmget();
40int (*shmcalls[])() = { shmat, shmctl, shmdt, shmget };
41int shmtot = 0;
42
43int shmfork(), shmexit();
44struct mapmemops shmops = { shmfork, (int (*)())0, shmexit, shmexit };
45
46shminit()
47{
48 register int i;
49
50 if (shminfo.shmmni > SHMMMNI)
51 shminfo.shmmni = SHMMMNI;
52 for (i = 0; i < shminfo.shmmni; i++) {
53 shmsegs[i].shm_perm.mode = 0;
54 shmsegs[i].shm_perm.seq = 0;
55 }
56}
57
161c14a2
KM
58/*
59 * Entry point for all SHM calls
60 */
61shmsys(p, uap, retval)
62 struct proc *p;
63 struct args {
6f843dc9 64 int which;
161c14a2
KM
65 } *uap;
66 int *retval;
67{
6f843dc9 68
161c14a2
KM
69 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
70 RETURN (EINVAL);
71 RETURN ((*shmcalls[uap->which])(p, &uap[1], retval));
6f843dc9
KM
72}
73
161c14a2
KM
74/*
75 * Get a shared memory segment
76 */
77shmget(p, uap, retval)
78 struct proc *p;
79 register struct args {
6f843dc9
KM
80 key_t key;
81 int size;
82 int shmflg;
161c14a2
KM
83 } *uap;
84 int *retval;
85{
6f843dc9 86 register struct shmid_ds *shp;
161c14a2 87 register struct ucred *cred = u.u_cred;
6f843dc9 88 register int i;
161c14a2 89 int error, size, rval = 0;
6f843dc9
KM
90 caddr_t kva;
91
92 /* look up the specified shm_id */
93 if (uap->key != IPC_PRIVATE) {
94 for (i = 0; i < shminfo.shmmni; i++)
95 if ((shmsegs[i].shm_perm.mode & SHM_ALLOC) &&
96 shmsegs[i].shm_perm.key == uap->key) {
97 rval = i;
98 break;
99 }
100 } else
101 i = shminfo.shmmni;
102
103 /* create a new shared segment if necessary */
104 if (i == shminfo.shmmni) {
161c14a2
KM
105 if ((uap->shmflg & IPC_CREAT) == 0)
106 return (ENOENT);
107 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
108 return (EINVAL);
6f843dc9
KM
109 for (i = 0; i < shminfo.shmmni; i++)
110 if ((shmsegs[i].shm_perm.mode & SHM_ALLOC) == 0) {
111 rval = i;
112 break;
113 }
161c14a2
KM
114 if (i == shminfo.shmmni)
115 return (ENOSPC);
6f843dc9 116 size = clrnd(btoc(uap->size));
161c14a2
KM
117 if (shmtot + size > shminfo.shmall)
118 return (ENOMEM);
6f843dc9
KM
119 shp = &shmsegs[rval];
120 /*
121 * We need to do a couple of things to ensure consistency
122 * in case we sleep in malloc(). We mark segment as
123 * allocated so that other shmgets() will not allocate it.
124 * We mark it as "destroyed" to insure that shmvalid() is
125 * false making most operations fail (XXX). We set the key,
126 * so that other shmget()s will fail.
127 */
128 shp->shm_perm.mode = SHM_ALLOC | SHM_DEST;
129 shp->shm_perm.key = uap->key;
130 kva = (caddr_t) malloc((u_long)ctob(size), M_SHM, M_WAITOK);
131 if (kva == NULL) {
132 shp->shm_perm.mode = 0;
161c14a2 133 return (ENOMEM);
6f843dc9
KM
134 }
135 if (!claligned(kva))
136 panic("shmget: non-aligned memory");
137 bzero(kva, (u_int)ctob(size));
138 shmtot += size;
161c14a2
KM
139 shp->shm_perm.cuid = shp->shm_perm.uid = cred->cr_uid;
140 shp->shm_perm.cgid = shp->shm_perm.gid = cred->cr_gid;
6f843dc9
KM
141 shp->shm_perm.mode = SHM_ALLOC | (uap->shmflg&0777);
142 shp->shm_handle = (void *) kvtopte(kva);
143 shp->shm_segsz = uap->size;
c9714ae3 144 shp->shm_cpid = p->p_pid;
6f843dc9
KM
145 shp->shm_lpid = shp->shm_nattch = 0;
146 shp->shm_atime = shp->shm_dtime = 0;
147 shp->shm_ctime = time.tv_sec;
148 } else {
149 shp = &shmsegs[rval];
150 /* XXX: probably not the right thing to do */
161c14a2
KM
151 if (shp->shm_perm.mode & SHM_DEST)
152 return (EBUSY);
153 if (error = ipcaccess(cred, &shp->shm_perm, uap->shmflg&0777))
154 return (error);
155 if (uap->size && uap->size > shp->shm_segsz)
156 return (EINVAL);
157 if ((uap->shmflg&IPC_CREAT) && (uap->shmflg&IPC_EXCL))
158 return (EEXIST);
6f843dc9 159 }
161c14a2 160 *retval = shp->shm_perm.seq * SHMMMNI + rval;
6f843dc9
KM
161}
162
161c14a2
KM
163/*
164 * Shared memory control
165 */
166/* ARGSUSED */
167shmctl(p, uap, retval)
168 struct proc *p;
169 register struct args {
6f843dc9
KM
170 int shmid;
171 int cmd;
172 caddr_t buf;
161c14a2
KM
173 } *uap;
174 int *retval;
175{
6f843dc9 176 register struct shmid_ds *shp;
161c14a2 177 register struct ucred *cred = u.u_cred;
6f843dc9 178 struct shmid_ds sbuf;
161c14a2 179 int error;
6f843dc9 180
161c14a2
KM
181 if (error = shmvalid(uap->shmid))
182 return (error);
6f843dc9
KM
183 shp = &shmsegs[uap->shmid % SHMMMNI];
184 switch (uap->cmd) {
185 case IPC_STAT:
161c14a2
KM
186 if (error = ipcaccess(cred, &shp->shm_perm, IPC_R))
187 return (error);
188 return (copyout((caddr_t)shp, uap->buf, sizeof(*shp)));
6f843dc9
KM
189
190 case IPC_SET:
161c14a2
KM
191 if (cred->cr_uid && cred->cr_uid != shp->shm_perm.uid &&
192 cred->cr_uid != shp->shm_perm.cuid)
193 return (EPERM);
194 if (error = copyin(uap->buf, (caddr_t)&sbuf, sizeof sbuf))
195 return (error);
196 shp->shm_perm.uid = sbuf.shm_perm.uid;
197 shp->shm_perm.gid = sbuf.shm_perm.gid;
198 shp->shm_perm.mode = (shp->shm_perm.mode & ~0777)
199 | (sbuf.shm_perm.mode & 0777);
200 shp->shm_ctime = time.tv_sec;
6f843dc9
KM
201 break;
202
203 case IPC_RMID:
161c14a2
KM
204 if (cred->cr_uid && cred->cr_uid != shp->shm_perm.uid &&
205 cred->cr_uid != shp->shm_perm.cuid)
206 return (EPERM);
6f843dc9
KM
207 /* set ctime? */
208 shp->shm_perm.key = IPC_PRIVATE;
209 shp->shm_perm.mode |= SHM_DEST;
210 if (shp->shm_nattch <= 0)
211 shmfree(shp);
212 break;
213
214#ifdef HPUXCOMPAT
215 case SHM_LOCK:
216 case SHM_UNLOCK:
217 /* don't really do anything, but make them think we did */
c9714ae3 218 if ((p->p_flag & SHPUX) == 0)
161c14a2
KM
219 return (EINVAL);
220 if (cred->cr_uid && cred->cr_uid != shp->shm_perm.uid &&
221 cred->cr_uid != shp->shm_perm.cuid)
222 return (EPERM);
6f843dc9
KM
223 break;
224#endif
225
226 default:
161c14a2 227 return (EINVAL);
6f843dc9 228 }
161c14a2 229 return (0);
6f843dc9
KM
230}
231
161c14a2
KM
232/*
233 * Attach to shared memory segment.
234 */
235shmat(p, uap, retval)
236 struct proc *p;
237 register struct args {
6f843dc9
KM
238 int shmid;
239 caddr_t shmaddr;
240 int shmflg;
161c14a2
KM
241 } *uap;
242 int *retval;
243{
6f843dc9
KM
244 register struct shmid_ds *shp;
245 register int size;
246 struct mapmem *mp;
247 caddr_t uva;
161c14a2 248 int error, error1, prot, shmmapin();
6f843dc9 249
161c14a2
KM
250 if (error = shmvalid(uap->shmid))
251 return (error);
6f843dc9
KM
252 shp = &shmsegs[uap->shmid % SHMMMNI];
253 if (shp->shm_handle == NULL)
96ea38ce 254 panic("shmat NULL handle");
161c14a2 255 if (error = ipcaccess(u.u_cred, &shp->shm_perm,
6f843dc9 256 (uap->shmflg&SHM_RDONLY) ? IPC_R : IPC_R|IPC_W))
161c14a2 257 return (error);
6f843dc9
KM
258 uva = uap->shmaddr;
259 if (uva && ((int)uva & (SHMLBA-1))) {
260 if (uap->shmflg & SHM_RND)
261 uva = (caddr_t) ((int)uva & ~(SHMLBA-1));
161c14a2
KM
262 else
263 return (EINVAL);
6f843dc9
KM
264 }
265 /*
266 * Make sure user doesn't use more than their fair share
267 */
268 size = 0;
269 for (mp = u.u_mmap; mp; mp = mp->mm_next)
270 if (mp->mm_ops == &shmops)
271 size++;
161c14a2
KM
272 if (size >= shminfo.shmseg)
273 return (EMFILE);
6f843dc9
KM
274 /*
275 * Allocate a mapped memory region descriptor and
276 * attempt to expand the user page table to allow for region
277 */
278 prot = (uap->shmflg & SHM_RDONLY) ? MM_RO : MM_RW;
279#if defined(hp300)
280 prot |= MM_CI;
281#endif
282 size = ctob(clrnd(btoc(shp->shm_segsz)));
c9714ae3 283 error = mmalloc(p, uap->shmid, &uva, (segsz_t)size, prot, &shmops, &mp);
161c14a2
KM
284 if (error)
285 return (error);
286 if (error = mmmapin(p, mp, shmmapin)) {
287 if (error1 = mmfree(p, mp))
288 return (error1);
289 return (error);
6f843dc9
KM
290 }
291 /*
292 * Fill in the remaining fields
293 */
c9714ae3 294 shp->shm_lpid = p->p_pid;
6f843dc9
KM
295 shp->shm_atime = time.tv_sec;
296 shp->shm_nattch++;
161c14a2 297 *retval = (int) uva;
6f843dc9
KM
298}
299
161c14a2
KM
300/*
301 * Detach from shared memory segment.
302 */
303/* ARGSUSED */
304shmdt(p, uap, retval)
305 struct proc *p;
306 struct args {
6f843dc9 307 caddr_t shmaddr;
161c14a2
KM
308 } *uap;
309 int *retval;
310{
6f843dc9
KM
311 register struct mapmem *mp;
312
313 for (mp = u.u_mmap; mp; mp = mp->mm_next)
314 if (mp->mm_ops == &shmops && mp->mm_uva == uap->shmaddr)
315 break;
161c14a2
KM
316 if (mp == MMNIL)
317 return (EINVAL);
c9714ae3 318 shmsegs[mp->mm_id % SHMMMNI].shm_lpid = p->p_pid;
161c14a2 319 return (shmufree(p, mp));
6f843dc9
KM
320}
321
322shmmapin(mp, off)
323 struct mapmem *mp;
324{
325 register struct shmid_ds *shp;
326
327 shp = &shmsegs[mp->mm_id % SHMMMNI];
328 if (off >= ctob(clrnd(btoc(shp->shm_segsz))))
329 return(-1);
330 return(((struct pte *)shp->shm_handle)[btop(off)].pg_pfnum);
331}
332
333/*
334 * Increment attach count on fork
335 */
6be55866 336/* ARGSUSED */
6f843dc9
KM
337shmfork(mp, ischild)
338 register struct mapmem *mp;
339{
340 if (!ischild)
341 shmsegs[mp->mm_id % SHMMMNI].shm_nattch++;
342}
343
344/*
345 * Detach from shared memory segment on exit (or exec)
346 */
6be55866
KM
347shmexit(p, mp)
348 struct proc *p;
349 struct mapmem *mp;
6f843dc9 350{
c9714ae3 351
161c14a2 352 return (shmufree(p, mp));
6f843dc9
KM
353}
354
355shmvalid(id)
356 register int id;
357{
358 register struct shmid_ds *shp;
359
360 if (id < 0 || (id % SHMMMNI) >= shminfo.shmmni)
161c14a2 361 return(EINVAL);
6f843dc9
KM
362 shp = &shmsegs[id % SHMMMNI];
363 if (shp->shm_perm.seq == (id / SHMMMNI) &&
364 (shp->shm_perm.mode & (SHM_ALLOC|SHM_DEST)) == SHM_ALLOC)
161c14a2
KM
365 return(0);
366 return(EINVAL);
6f843dc9
KM
367}
368
369/*
370 * Free user resources associated with a shared memory segment
371 */
c9714ae3
KM
372shmufree(p, mp)
373 struct proc *p;
6f843dc9
KM
374 struct mapmem *mp;
375{
376 register struct shmid_ds *shp;
c9714ae3 377 int error;
6f843dc9
KM
378
379 shp = &shmsegs[mp->mm_id % SHMMMNI];
c9714ae3
KM
380 mmmapout(p, mp);
381 error = mmfree(p, mp);
6f843dc9
KM
382 shp->shm_dtime = time.tv_sec;
383 if (--shp->shm_nattch <= 0 && (shp->shm_perm.mode & SHM_DEST))
384 shmfree(shp);
c9714ae3 385 return (error);
6f843dc9
KM
386}
387
388/*
389 * Deallocate resources associated with a shared memory segment
390 */
391shmfree(shp)
392 register struct shmid_ds *shp;
393{
394 caddr_t kva;
395
396 if (shp->shm_handle == NULL)
397 panic("shmfree");
398 kva = (caddr_t) ptetokv(shp->shm_handle);
399 free(kva, M_SHM);
400 shp->shm_handle = NULL;
401 shmtot -= clrnd(btoc(shp->shm_segsz));
402 shp->shm_perm.mode = 0;
403 /*
404 * Increment the sequence number to ensure that outstanding
405 * shmids for this segment will be invalid in the event that
406 * the segment is reallocated. Note that shmids must be
407 * positive as decreed by SVID.
408 */
409 shp->shm_perm.seq++;
410 if ((int)(shp->shm_perm.seq * SHMMMNI) < 0)
411 shp->shm_perm.seq = 0;
412}
413
414/*
415 * XXX This routine would be common to all sysV style IPC
416 * (if the others were implemented).
417 */
161c14a2 418ipcaccess(ipc, mode, cred)
6f843dc9 419 register struct ipc_perm *ipc;
161c14a2
KM
420 int mode;
421 register struct ucred *cred;
6f843dc9
KM
422{
423 register int m;
424
161c14a2 425 if (cred->cr_uid == 0)
6f843dc9
KM
426 return(0);
427 /*
428 * Access check is based on only one of owner, group, public.
429 * If not owner, then check group.
430 * If not a member of the group, then check public access.
431 */
432 mode &= 0700;
433 m = ipc->mode;
161c14a2 434 if (cred->cr_uid != ipc->uid && cred->cr_uid != ipc->cuid) {
6f843dc9 435 m <<= 3;
161c14a2
KM
436 if (!groupmember(ipc->gid, cred) &&
437 !groupmember(ipc->cgid, cred))
6f843dc9
KM
438 m <<= 3;
439 }
440 if ((mode&m) == mode)
161c14a2
KM
441 return (0);
442 return (EACCES);
6f843dc9
KM
443}
444
445#endif /* SYSVSHM */