xref: /freebsd-13.1/lib/libc/sys/shmat.2 (revision ea7e7006)
1.\"
2.\" Copyright (c) 1995 David Hovemeyer <[email protected]>
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.\" $FreeBSD$
27.\"
28.Dd January 14, 2019
29.Dt SHMAT 2
30.Os
31.Sh NAME
32.Nm shmat ,
33.Nm shmdt
34.Nd attach or detach shared memory
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In sys/types.h
39.In sys/ipc.h
40.In sys/shm.h
41.Ft void *
42.Fn shmat "int shmid" "const void *addr" "int flag"
43.Ft int
44.Fn shmdt "const void *addr"
45.Sh DESCRIPTION
46The
47.Fn shmat
48system call
49attaches the shared memory segment identified by
50.Fa shmid
51to the calling process's address space.
52The address where the segment
53is attached is determined as follows:
54.\"
55.\" These are cribbed almost exactly from Stevens, _Advanced Programming in
56.\" the UNIX Environment_.
57.\"
58.Bl -bullet
59.It
60If
61.Fa addr
62is 0, the segment is attached at an address selected by the
63kernel.
64.It
65If
66.Fa addr
67is nonzero and
68.Va SHM_RND
69is not specified in
70.Fa flag ,
71the segment is attached the specified address.
72.It
73If
74.Fa addr
75is specified and
76.Va SHM_RND
77is specified,
78.Fa addr
79is rounded down to the nearest multiple of SHMLBA.
80.El
81.Pp
82If the
83.Va SHM_REMAP
84flag is specified and the passed
85.Fa addr
86is not
87.Dv NULL ,
88any existing mappings in the virtual addresses range are
89cleared before the segment is attached.
90If the flag is not specified,
91.Fa addr
92is not
93.Dv NULL ,
94and the virtual address range contains
95some pre-existing mappings, the
96.Fn shmat
97call fails.
98.Pp
99The
100.Fn shmdt
101system call
102detaches the shared memory segment at the address specified by
103.Fa addr
104from the calling process's address space.
105.Sh RETURN VALUES
106Upon success,
107.Fn shmat
108returns the address where the segment is attached; otherwise, -1
109is returned and
110.Va errno
111is set to indicate the error.
112.Pp
113.Rv -std shmdt
114.Sh ERRORS
115The
116.Fn shmat
117system call
118will fail if:
119.Bl -tag -width Er
120.It Bq Er EINVAL
121No shared memory segment was found corresponding to
122.Fa shmid .
123.It Bq Er EINVAL
124The
125.Fa addr
126argument
127was not an acceptable address.
128.It Bq Er ENOMEM
129The specified
130.Fa addr
131cannot be used for mapping, for instance due to the amount of available
132space being smaller than the segment size,
133or because pre-existing mappings are in the range and no
134.Va SHM_REMAP
135flag was provided.
136.It Bq Er EMFILE
137Failed to attach the shared memory segment because the per-process
138.Va kern.ipc.shmseg
139.Xr sysctl 3
140limit was reached.
141.El
142.Pp
143The
144.Fn shmdt
145system call
146will fail if:
147.Bl -tag -width Er
148.It Bq Er EINVAL
149The
150.Fa addr
151argument
152does not point to a shared memory segment.
153.El
154.Sh "SEE ALSO"
155.Xr shmctl 2 ,
156.Xr shmget 2
157