xref: /freebsd/lib/libsys/semget.2 (revision a91a246563dffa876a52f53a98de4af9fa364c52)
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 March 4, 2018
27.Dt SEMGET 2
28.Os
29.Sh NAME
30.Nm semget
31.Nd obtain a semaphore id
32.Sh LIBRARY
33.Lb libc
34.Sh SYNOPSIS
35.In sys/sem.h
36.Ft int
37.Fn semget "key_t key" "int nsems" "int flag"
38.Sh DESCRIPTION
39Based on the values of
40.Fa key
41and
42.Fa flag ,
43.Fn semget
44returns the identifier of a newly created or previously existing
45set of semaphores.
46.\"
47.\" This is copied verbatim from the shmget manpage.  Perhaps
48.\" it should go in a common manpage, such as .Xr ipc 2
49.\"
50The key
51is analogous to a filename: it provides a handle that names an
52IPC object.
53There are three ways to specify a key:
54.Bl -bullet
55.It
56IPC_PRIVATE may be specified, in which case a new IPC object
57will be created.
58.It
59An integer constant may be specified.
60If no IPC object corresponding
61to
62.Fa key
63is specified and the IPC_CREAT bit is set in
64.Fa flag ,
65a new one will be created.
66.It
67The
68.Xr ftok 3
69function
70may be used to generate a key from a pathname.
71.El
72.\"
73.\" Likewise for this section, except SHM_* becomes SEM_*.
74.\"
75.Pp
76The mode of a newly created IPC object is determined by ORing these constants
77into the
78.Fa flag
79argument:
80.Bl -tag -width 0000
81.It Dv 0400
82Read access for user.
83.It Dv 0200
84Alter access for user.
85.It Dv 0040
86Read access for group.
87.It Dv 0020
88Alter access for group.
89.It Dv 0004
90Read access for other.
91.It Dv 0002
92Alter access for other.
93.El
94.Pp
95If a new set of semaphores is being created,
96.Fa nsems
97is used to indicate the number of semaphores the set should contain.
98Otherwise,
99.Fa nsems
100may be specified as 0.
101.Sh RETURN VALUES
102The
103.Fn semget
104system call
105returns the id of a semaphore set if successful; otherwise, -1
106is returned and
107.Va errno
108is set to indicate the error.
109.Sh ERRORS
110The
111.Fn semget
112system call
113will fail if:
114.Bl -tag -width Er
115.\" ipcperm could fail (we are opening to read and write, as it were)
116.It Bq Er EACCES
117Access permission failure.
118.\"
119.\" sysv_sem.c is quite explicit about these, so I'm pretty sure
120.\" this is accurate
121.\"
122.It Bq Er EEXIST
123IPC_CREAT and IPC_EXCL were specified, and a semaphore set
124corresponding to
125.Fa key
126already exists.
127.It Bq Er EINVAL
128The number of semaphores requested exceeds the system imposed maximum
129per set.
130.It Bq Er EINVAL
131A semaphore set corresponding to
132.Fa key
133already exists and contains fewer semaphores than
134.Fa nsems .
135.It Bq Er EINVAL
136A semaphore set corresponding to
137.Fa key
138does not exist and
139.Fa nsems
140is 0 or negative.
141.It Bq Er ENOSPC
142Insufficiently many semaphores are available.
143.It Bq Er ENOSPC
144The kernel could not allocate a
145.Fa "struct semid_ds" .
146.It Bq Er ENOENT
147No semaphore set was found corresponding to
148.Fa key ,
149and IPC_CREAT was not specified.
150.El
151.Sh SEE ALSO
152.Xr semctl 2 ,
153.Xr semop 2 ,
154.Xr ftok 3
155