Deleted Added
full compact
sysv_shm.c (79224) sysv_shm.c (82607)
1/* $FreeBSD: head/sys/kern/sysv_shm.c 79224 2001-07-04 16:20:28Z dillon $ */
1/* $FreeBSD: head/sys/kern/sysv_shm.c 82607 2001-08-31 00:02:18Z dillon $ */
2/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3
4/*
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 218 unchanged lines hidden (view full) ---

228}
229
230#ifndef _SYS_SYSPROTO_H_
231struct shmdt_args {
232 void *shmaddr;
233};
234#endif
235
2/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3
4/*
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 218 unchanged lines hidden (view full) ---

228}
229
230#ifndef _SYS_SYSPROTO_H_
231struct shmdt_args {
232 void *shmaddr;
233};
234#endif
235
236/*
237 * MPSAFE
238 */
236int
237shmdt(p, uap)
238 struct proc *p;
239 struct shmdt_args *uap;
240{
241 struct shmmap_state *shmmap_s;
242 int i;
239int
240shmdt(p, uap)
241 struct proc *p;
242 struct shmdt_args *uap;
243{
244 struct shmmap_state *shmmap_s;
245 int i;
243 int error;
246 int error = 0;
244
247
245 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
246 return (ENOSYS);
248 mtx_lock(&Giant);
247
249
250 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
251 error = ENOSYS;
252 goto done2;
253 }
254
248 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
255 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
249 if (shmmap_s == NULL)
250 return EINVAL;
251 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
256 if (shmmap_s == NULL) {
257 error = EINVAL;
258 goto done2;
259 }
260 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
252 if (shmmap_s->shmid != -1 &&
261 if (shmmap_s->shmid != -1 &&
253 shmmap_s->va == (vm_offset_t)uap->shmaddr)
262 shmmap_s->va == (vm_offset_t)uap->shmaddr) {
254 break;
263 break;
255 if (i == shminfo.shmseg)
256 return EINVAL;
264 }
265 }
266 if (i == shminfo.shmseg) {
267 error = EINVAL;
268 goto done2;
269 }
257 error = shm_delete_mapping(p, shmmap_s);
270 error = shm_delete_mapping(p, shmmap_s);
258 return error;
271done2:
272 mtx_unlock(&Giant);
273 return (error);
259}
260
261#ifndef _SYS_SYSPROTO_H_
262struct shmat_args {
263 int shmid;
264 void *shmaddr;
265 int shmflg;
266};
267#endif
268
274}
275
276#ifndef _SYS_SYSPROTO_H_
277struct shmat_args {
278 int shmid;
279 void *shmaddr;
280 int shmflg;
281};
282#endif
283
284/*
285 * MPSAFE
286 */
269int
270shmat(p, uap)
271 struct proc *p;
272 struct shmat_args *uap;
273{
287int
288shmat(p, uap)
289 struct proc *p;
290 struct shmat_args *uap;
291{
274 int error, i, flags;
292 int i, flags;
275 struct shmid_ds *shmseg;
276 struct shmmap_state *shmmap_s = NULL;
277 struct shm_handle *shm_handle;
278 vm_offset_t attach_va;
279 vm_prot_t prot;
280 vm_size_t size;
281 int rv;
293 struct shmid_ds *shmseg;
294 struct shmmap_state *shmmap_s = NULL;
295 struct shm_handle *shm_handle;
296 vm_offset_t attach_va;
297 vm_prot_t prot;
298 vm_size_t size;
299 int rv;
300 int error = 0;
282
301
283 GIANT_REQUIRED;
302 mtx_lock(&Giant);
284
303
285 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
286 return (ENOSYS);
304 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
305 error = ENOSYS;
306 goto done2;
307 }
287
288 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
289 if (shmmap_s == NULL) {
290 size = shminfo.shmseg * sizeof(struct shmmap_state);
291 shmmap_s = malloc(size, M_SHM, M_WAITOK);
292 for (i = 0; i < shminfo.shmseg; i++)
293 shmmap_s[i].shmid = -1;
294 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
295 }
296 shmseg = shm_find_segment_by_shmid(uap->shmid);
308
309 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
310 if (shmmap_s == NULL) {
311 size = shminfo.shmseg * sizeof(struct shmmap_state);
312 shmmap_s = malloc(size, M_SHM, M_WAITOK);
313 for (i = 0; i < shminfo.shmseg; i++)
314 shmmap_s[i].shmid = -1;
315 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
316 }
317 shmseg = shm_find_segment_by_shmid(uap->shmid);
297 if (shmseg == NULL)
298 return EINVAL;
318 if (shmseg == NULL) {
319 error = EINVAL;
320 goto done2;
321 }
299 error = ipcperm(p, &shmseg->shm_perm,
300 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
301 if (error)
322 error = ipcperm(p, &shmseg->shm_perm,
323 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
324 if (error)
302 return error;
325 goto done2;
303 for (i = 0; i < shminfo.shmseg; i++) {
304 if (shmmap_s->shmid == -1)
305 break;
306 shmmap_s++;
307 }
326 for (i = 0; i < shminfo.shmseg; i++) {
327 if (shmmap_s->shmid == -1)
328 break;
329 shmmap_s++;
330 }
308 if (i >= shminfo.shmseg)
309 return EMFILE;
331 if (i >= shminfo.shmseg) {
332 error = EMFILE;
333 goto done2;
334 }
310 size = round_page(shmseg->shm_segsz);
311#ifdef VM_PROT_READ_IS_EXEC
312 prot = VM_PROT_READ | VM_PROT_EXECUTE;
313#else
314 prot = VM_PROT_READ;
315#endif
316 if ((uap->shmflg & SHM_RDONLY) == 0)
317 prot |= VM_PROT_WRITE;
318 flags = MAP_ANON | MAP_SHARED;
319 if (uap->shmaddr) {
320 flags |= MAP_FIXED;
335 size = round_page(shmseg->shm_segsz);
336#ifdef VM_PROT_READ_IS_EXEC
337 prot = VM_PROT_READ | VM_PROT_EXECUTE;
338#else
339 prot = VM_PROT_READ;
340#endif
341 if ((uap->shmflg & SHM_RDONLY) == 0)
342 prot |= VM_PROT_WRITE;
343 flags = MAP_ANON | MAP_SHARED;
344 if (uap->shmaddr) {
345 flags |= MAP_FIXED;
321 if (uap->shmflg & SHM_RND)
346 if (uap->shmflg & SHM_RND) {
322 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
347 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
323 else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
348 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
324 attach_va = (vm_offset_t)uap->shmaddr;
349 attach_va = (vm_offset_t)uap->shmaddr;
325 else
326 return EINVAL;
350 } else {
351 error = EINVAL;
352 goto done2;
353 }
327 } else {
328 /*
329 * This is just a hint to vm_map_find() about where to
330 * put it.
331 */
332 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
333 + MAXTSIZ + MAXDSIZ);
334 }
335
336 shm_handle = shmseg->shm_internal;
337 vm_object_reference(shm_handle->shm_object);
338 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
339 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
340 if (rv != KERN_SUCCESS) {
354 } else {
355 /*
356 * This is just a hint to vm_map_find() about where to
357 * put it.
358 */
359 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
360 + MAXTSIZ + MAXDSIZ);
361 }
362
363 shm_handle = shmseg->shm_internal;
364 vm_object_reference(shm_handle->shm_object);
365 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
366 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
367 if (rv != KERN_SUCCESS) {
341 return ENOMEM;
368 error = ENOMEM;
369 goto done2;
342 }
343 vm_map_inherit(&p->p_vmspace->vm_map,
344 attach_va, attach_va + size, VM_INHERIT_SHARE);
345
346 shmmap_s->va = attach_va;
347 shmmap_s->shmid = uap->shmid;
348 shmseg->shm_lpid = p->p_pid;
349 shmseg->shm_atime = time_second;
350 shmseg->shm_nattch++;
351 p->p_retval[0] = attach_va;
370 }
371 vm_map_inherit(&p->p_vmspace->vm_map,
372 attach_va, attach_va + size, VM_INHERIT_SHARE);
373
374 shmmap_s->va = attach_va;
375 shmmap_s->shmid = uap->shmid;
376 shmseg->shm_lpid = p->p_pid;
377 shmseg->shm_atime = time_second;
378 shmseg->shm_nattch++;
379 p->p_retval[0] = attach_va;
352 return 0;
380done2:
381 mtx_unlock(&Giant);
382 return (error);
353}
354
355struct oshmid_ds {
356 struct ipc_perm shm_perm; /* operation perms */
357 int shm_segsz; /* size of segment (bytes) */
358 ushort shm_cpid; /* pid, creator */
359 ushort shm_lpid; /* pid, last operation */
360 short shm_nattch; /* no. of current attaches */

--- 4 unchanged lines hidden (view full) ---

365};
366
367struct oshmctl_args {
368 int shmid;
369 int cmd;
370 struct oshmid_ds *ubuf;
371};
372
383}
384
385struct oshmid_ds {
386 struct ipc_perm shm_perm; /* operation perms */
387 int shm_segsz; /* size of segment (bytes) */
388 ushort shm_cpid; /* pid, creator */
389 ushort shm_lpid; /* pid, last operation */
390 short shm_nattch; /* no. of current attaches */

--- 4 unchanged lines hidden (view full) ---

395};
396
397struct oshmctl_args {
398 int shmid;
399 int cmd;
400 struct oshmid_ds *ubuf;
401};
402
403/*
404 * MPSAFE
405 */
373static int
374oshmctl(p, uap)
375 struct proc *p;
376 struct oshmctl_args *uap;
377{
378#ifdef COMPAT_43
406static int
407oshmctl(p, uap)
408 struct proc *p;
409 struct oshmctl_args *uap;
410{
411#ifdef COMPAT_43
379 int error;
412 int error = 0;
380 struct shmid_ds *shmseg;
381 struct oshmid_ds outbuf;
382
413 struct shmid_ds *shmseg;
414 struct oshmid_ds outbuf;
415
383 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
384 return (ENOSYS);
416 mtx_lock(&Giant);
385
417
418 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
419 error = ENOSYS;
420 goto done2;
421 }
422
386 shmseg = shm_find_segment_by_shmid(uap->shmid);
423 shmseg = shm_find_segment_by_shmid(uap->shmid);
387 if (shmseg == NULL)
388 return EINVAL;
424 if (shmseg == NULL) {
425 error = EINVAL;
426 goto done2;
427 }
389 switch (uap->cmd) {
390 case IPC_STAT:
391 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
392 if (error)
428 switch (uap->cmd) {
429 case IPC_STAT:
430 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
431 if (error)
393 return error;
432 goto done2;
394 outbuf.shm_perm = shmseg->shm_perm;
395 outbuf.shm_segsz = shmseg->shm_segsz;
396 outbuf.shm_cpid = shmseg->shm_cpid;
397 outbuf.shm_lpid = shmseg->shm_lpid;
398 outbuf.shm_nattch = shmseg->shm_nattch;
399 outbuf.shm_atime = shmseg->shm_atime;
400 outbuf.shm_dtime = shmseg->shm_dtime;
401 outbuf.shm_ctime = shmseg->shm_ctime;
402 outbuf.shm_handle = shmseg->shm_internal;
403 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
404 if (error)
433 outbuf.shm_perm = shmseg->shm_perm;
434 outbuf.shm_segsz = shmseg->shm_segsz;
435 outbuf.shm_cpid = shmseg->shm_cpid;
436 outbuf.shm_lpid = shmseg->shm_lpid;
437 outbuf.shm_nattch = shmseg->shm_nattch;
438 outbuf.shm_atime = shmseg->shm_atime;
439 outbuf.shm_dtime = shmseg->shm_dtime;
440 outbuf.shm_ctime = shmseg->shm_ctime;
441 outbuf.shm_handle = shmseg->shm_internal;
442 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
443 if (error)
405 return error;
444 goto done2;
406 break;
407 default:
408 /* XXX casting to (sy_call_t *) is bogus, as usual. */
445 break;
446 default:
447 /* XXX casting to (sy_call_t *) is bogus, as usual. */
409 return ((sy_call_t *)shmctl)(p, uap);
448 error = ((sy_call_t *)shmctl)(p, uap);
449 break;
410 }
450 }
411 return 0;
451done2:
452 mtx_unlock(&Giant);
453 return (error);
412#else
413 return EINVAL;
414#endif
415}
416
417#ifndef _SYS_SYSPROTO_H_
418struct shmctl_args {
419 int shmid;
420 int cmd;
421 struct shmid_ds *buf;
422};
423#endif
424
454#else
455 return EINVAL;
456#endif
457}
458
459#ifndef _SYS_SYSPROTO_H_
460struct shmctl_args {
461 int shmid;
462 int cmd;
463 struct shmid_ds *buf;
464};
465#endif
466
467/*
468 * MPSAFE
469 */
425int
426shmctl(p, uap)
427 struct proc *p;
428 struct shmctl_args *uap;
429{
470int
471shmctl(p, uap)
472 struct proc *p;
473 struct shmctl_args *uap;
474{
430 int error;
475 int error = 0;
431 struct shmid_ds inbuf;
432 struct shmid_ds *shmseg;
433
476 struct shmid_ds inbuf;
477 struct shmid_ds *shmseg;
478
434 GIANT_REQUIRED;
479 mtx_lock(&Giant);
435
480
436 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
437 return (ENOSYS);
481 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
482 error = ENOSYS;
483 goto done2;
484 }
438
439 shmseg = shm_find_segment_by_shmid(uap->shmid);
485
486 shmseg = shm_find_segment_by_shmid(uap->shmid);
440 if (shmseg == NULL)
441 return EINVAL;
487 if (shmseg == NULL) {
488 error = EINVAL;
489 goto done2;
490 }
442 switch (uap->cmd) {
443 case IPC_STAT:
444 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
445 if (error)
491 switch (uap->cmd) {
492 case IPC_STAT:
493 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
494 if (error)
446 return error;
495 goto done2;
447 error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
448 if (error)
496 error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
497 if (error)
449 return error;
498 goto done2;
450 break;
451 case IPC_SET:
452 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
453 if (error)
499 break;
500 case IPC_SET:
501 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
502 if (error)
454 return error;
503 goto done2;
455 error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
456 if (error)
504 error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
505 if (error)
457 return error;
506 goto done2;
458 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
459 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
460 shmseg->shm_perm.mode =
461 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
462 (inbuf.shm_perm.mode & ACCESSPERMS);
463 shmseg->shm_ctime = time_second;
464 break;
465 case IPC_RMID:
466 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
467 if (error)
507 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
508 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
509 shmseg->shm_perm.mode =
510 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
511 (inbuf.shm_perm.mode & ACCESSPERMS);
512 shmseg->shm_ctime = time_second;
513 break;
514 case IPC_RMID:
515 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
516 if (error)
468 return error;
517 goto done2;
469 shmseg->shm_perm.key = IPC_PRIVATE;
470 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
471 if (shmseg->shm_nattch <= 0) {
472 shm_deallocate_segment(shmseg);
473 shm_last_free = IPCID_TO_IX(uap->shmid);
474 }
475 break;
476#if 0
477 case SHM_LOCK:
478 case SHM_UNLOCK:
479#endif
480 default:
518 shmseg->shm_perm.key = IPC_PRIVATE;
519 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
520 if (shmseg->shm_nattch <= 0) {
521 shm_deallocate_segment(shmseg);
522 shm_last_free = IPCID_TO_IX(uap->shmid);
523 }
524 break;
525#if 0
526 case SHM_LOCK:
527 case SHM_UNLOCK:
528#endif
529 default:
481 return EINVAL;
530 error = EINVAL;
531 break;
482 }
532 }
483 return 0;
533done2:
534 mtx_unlock(&Giant);
535 return (error);
484}
485
486#ifndef _SYS_SYSPROTO_H_
487struct shmget_args {
488 key_t key;
489 size_t size;
490 int shmflg;
491};

--- 110 unchanged lines hidden (view full) ---

602 */
603 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
604 wakeup((caddr_t)shmseg);
605 }
606 p->p_retval[0] = shmid;
607 return 0;
608}
609
536}
537
538#ifndef _SYS_SYSPROTO_H_
539struct shmget_args {
540 key_t key;
541 size_t size;
542 int shmflg;
543};

--- 110 unchanged lines hidden (view full) ---

654 */
655 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
656 wakeup((caddr_t)shmseg);
657 }
658 p->p_retval[0] = shmid;
659 return 0;
660}
661
662/*
663 * MPSAFE
664 */
610int
611shmget(p, uap)
612 struct proc *p;
613 struct shmget_args *uap;
614{
665int
666shmget(p, uap)
667 struct proc *p;
668 struct shmget_args *uap;
669{
615 int segnum, mode, error;
670 int segnum, mode;
671 int error;
616
672
617 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
618 return (ENOSYS);
673 mtx_lock(&Giant);
619
674
675 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
676 error = ENOSYS;
677 goto done2;
678 }
679
620 mode = uap->shmflg & ACCESSPERMS;
621 if (uap->key != IPC_PRIVATE) {
622 again:
623 segnum = shm_find_segment_by_key(uap->key);
624 if (segnum >= 0) {
625 error = shmget_existing(p, uap, mode, segnum);
626 if (error == EAGAIN)
627 goto again;
680 mode = uap->shmflg & ACCESSPERMS;
681 if (uap->key != IPC_PRIVATE) {
682 again:
683 segnum = shm_find_segment_by_key(uap->key);
684 if (segnum >= 0) {
685 error = shmget_existing(p, uap, mode, segnum);
686 if (error == EAGAIN)
687 goto again;
628 return error;
688 goto done2;
629 }
689 }
630 if ((uap->shmflg & IPC_CREAT) == 0)
631 return ENOENT;
690 if ((uap->shmflg & IPC_CREAT) == 0) {
691 error = ENOENT;
692 goto done2;
693 }
632 }
694 }
633 return shmget_allocate_segment(p, uap, mode);
695 error = shmget_allocate_segment(p, uap, mode);
696done2:
697 mtx_unlock(&Giant);
698 return (error);
634}
635
699}
700
701/*
702 * MPSAFE
703 */
636int
637shmsys(p, uap)
638 struct proc *p;
639 /* XXX actually varargs. */
640 struct shmsys_args /* {
641 u_int which;
642 int a2;
643 int a3;
644 int a4;
645 } */ *uap;
646{
704int
705shmsys(p, uap)
706 struct proc *p;
707 /* XXX actually varargs. */
708 struct shmsys_args /* {
709 u_int which;
710 int a2;
711 int a3;
712 int a4;
713 } */ *uap;
714{
715 int error;
647
716
648 if (!jail_sysvipc_allowed && jailed(p->p_ucred))
649 return (ENOSYS);
717 mtx_lock(&Giant);
650
718
651 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
652 return EINVAL;
653 return ((*shmcalls[uap->which])(p, &uap->a2));
719 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
720 error = ENOSYS;
721 goto done2;
722 }
723
724 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0])) {
725 error = EINVAL;
726 goto done2;
727 }
728 error = (*shmcalls[uap->which])(p, &uap->a2);
729done2:
730 mtx_unlock(&Giant);
731 return (error);
654}
655
656static void
657shmfork_myhook(p1, p2)
658 struct proc *p1, *p2;
659{
660 struct shmmap_state *shmmap_s;
661 size_t size;

--- 127 unchanged lines hidden ---
732}
733
734static void
735shmfork_myhook(p1, p2)
736 struct proc *p1, *p2;
737{
738 struct shmmap_state *shmmap_s;
739 size_t size;

--- 127 unchanged lines hidden ---