xref: /freebsd/lib/libsys/shmat.2 (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1.\"
2.\" Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
3.\"
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.Dd January 14, 2019
27.Dt SHMAT 2
28.Os
29.Sh NAME
30.Nm shmat ,
31.Nm shmdt
32.Nd attach or detach shared memory
33.Sh LIBRARY
34.Lb libc
35.Sh SYNOPSIS
36.In sys/types.h
37.In sys/ipc.h
38.In sys/shm.h
39.Ft void *
40.Fn shmat "int shmid" "const void *addr" "int flag"
41.Ft int
42.Fn shmdt "const void *addr"
43.Sh DESCRIPTION
44The
45.Fn shmat
46system call
47attaches the shared memory segment identified by
48.Fa shmid
49to the calling process's address space.
50The address where the segment
51is attached is determined as follows:
52.\"
53.\" These are cribbed almost exactly from Stevens, _Advanced Programming in
54.\" the UNIX Environment_.
55.\"
56.Bl -bullet
57.It
58If
59.Fa addr
60is 0, the segment is attached at an address selected by the
61kernel.
62.It
63If
64.Fa addr
65is nonzero and
66.Va SHM_RND
67is not specified in
68.Fa flag ,
69the segment is attached the specified address.
70.It
71If
72.Fa addr
73is specified and
74.Va SHM_RND
75is specified,
76.Fa addr
77is rounded down to the nearest multiple of SHMLBA.
78.El
79.Pp
80If the
81.Va SHM_REMAP
82flag is specified and the passed
83.Fa addr
84is not
85.Dv NULL ,
86any existing mappings in the virtual addresses range are
87cleared before the segment is attached.
88If the flag is not specified,
89.Fa addr
90is not
91.Dv NULL ,
92and the virtual address range contains
93some pre-existing mappings, the
94.Fn shmat
95call fails.
96.Pp
97The
98.Fn shmdt
99system call
100detaches the shared memory segment at the address specified by
101.Fa addr
102from the calling process's address space.
103.Sh RETURN VALUES
104Upon success,
105.Fn shmat
106returns the address where the segment is attached; otherwise, -1
107is returned and
108.Va errno
109is set to indicate the error.
110.Pp
111.Rv -std shmdt
112.Sh ERRORS
113The
114.Fn shmat
115system call
116will fail if:
117.Bl -tag -width Er
118.It Bq Er EINVAL
119No shared memory segment was found corresponding to
120.Fa shmid .
121.It Bq Er EINVAL
122The
123.Fa addr
124argument
125was not an acceptable address.
126.It Bq Er ENOMEM
127The specified
128.Fa addr
129cannot be used for mapping, for instance due to the amount of available
130space being smaller than the segment size,
131or because pre-existing mappings are in the range and no
132.Va SHM_REMAP
133flag was provided.
134.It Bq Er EMFILE
135Failed to attach the shared memory segment because the per-process
136.Va kern.ipc.shmseg
137.Xr sysctl 3
138limit was reached.
139.El
140.Pp
141The
142.Fn shmdt
143system call
144will fail if:
145.Bl -tag -width Er
146.It Bq Er EINVAL
147The
148.Fa addr
149argument
150does not point to a shared memory segment.
151.El
152.Sh "SEE ALSO"
153.Xr shmctl 2 ,
154.Xr shmget 2
155