1.\" $OpenBSD: shmat.2,v 1.14 2007/05/31 19:19:33 jmc Exp $ 2.\" $NetBSD: shmat.2,v 1.2 1997/03/27 08:20:38 mikel Exp $ 3.\" 4.\" Copyright (c) 1995 Frank van der Linden 5.\" 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: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed for the NetBSD Project 18.\" by Frank van der Linden 19.\" 4. The name of the author may not be used to endorse or promote products 20.\" derived from this software without specific prior written permission 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.\"/ 33.Dd $Mdocdate: May 31 2007 $ 34.Dt SHMAT 2 35.Os 36.Sh NAME 37.Nm shmat , 38.Nm shmdt 39.Nd map/unmap shared memory 40.Sh SYNOPSIS 41.Fd #include <sys/types.h> 42.Fd #include <sys/ipc.h> 43.Fd #include <sys/shm.h> 44.Ft void * 45.Fn shmat "int shmid" "const void *shmaddr" "int shmflg" 46.Ft int 47.Fn shmdt "const void *shmaddr" 48.Sh DESCRIPTION 49.Fn shmat 50maps the shared memory segment associated with the shared memory identifier 51.Fa shmid 52into the address space of the calling process. 53The address at which the segment is mapped is determined by the 54.Fa shmaddr 55parameter. 56If it is equal to 0, the system will pick an address itself. 57Otherwise, an attempt is made to map the shared memory segment at the 58address 59.Fa shmaddr 60specifies. 61If 62.Dv SHM_RND 63is set in 64.Fa shmflg , 65the system will round the address down to a multiple of 66.Dv SHMLBA 67bytes 68.Pf ( Dv SHMLBA 69is defined in 70.Aq Pa sys/shm.h ) . 71.Pp 72A shared memory segment can be mapped read-only by specifying the 73.Dv SHM_RDONLY 74flag in 75.Fa shmflg . 76.Pp 77.Fn shmdt 78unmaps the shared memory segment that is currently mapped at 79.Fa shmaddr 80from the calling process' address space. 81.Fa shmaddr 82must be a value returned by a prior 83.Fn shmat 84call. 85A shared memory segment will remain existent until it is removed by 86a call to 87.Xr shmctl 2 88with the 89.Dv IPC_RMID 90command. 91.Sh RETURN VALUES 92.Fn shmat 93returns the address at which the shared memory segment has been mapped into 94the calling process' address space when successful, 95.Fn shmdt 96returns 0 on successful completion. 97Otherwise, a value of \-1 is returned, and the global variable 98.Va errno 99is set to indicate the error. 100.Sh ERRORS 101.Fn shmat 102will fail if: 103.Bl -tag -width Er 104.It Bq Er EACCES 105The calling process has no permission to access this shared memory segment. 106.It Bq Er ENOMEM 107There is not enough available data space for the calling process to 108map the shared memory segment. 109.It Bq Er EINVAL 110.Fa shmid 111is not a valid shared memory identifier. 112.Pp 113.Fa shmaddr 114specifies an illegal address. 115.It Bq Er EMFILE 116The number of shared memory segments has reached the system-wide limit. 117.El 118.Pp 119.Fn shmdt 120will fail if: 121.Bl -tag -width Er 122.It Bq Er EINVAL 123.Fa shmaddr 124is not the start address of a mapped shared memory segment. 125.El 126.Sh SEE ALSO 127.Xr ipcrm 1 , 128.Xr ipcs 1 , 129.Xr mmap 2 , 130.Xr shmctl 2 , 131.Xr shmget 2 132