xref: /openbsd-src/lib/libc/sys/semget.2 (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1.\"	$OpenBSD: semget.2,v 1.18 2014/02/13 07:30:39 guenther Exp $
2.\"	$NetBSD: semget.2,v 1.2 1997/03/27 08:20:41 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: February 13 2014 $
34.Dt SEMGET 2
35.Os
36.Sh NAME
37.Nm semget
38.Nd get semaphore set
39.Sh SYNOPSIS
40.Fd #include <sys/types.h>
41.Fd #include <sys/ipc.h>
42.Fd #include <sys/sem.h>
43.Ft int
44.Fn semget "key_t key" "int nsems" "int semflg"
45.Sh DESCRIPTION
46The
47.Fn semget
48system call returns the semaphore identifier associated with
49.Fa key .
50.Pp
51A new set containing
52.Fa nsems
53semaphores is created if either
54.Fa key
55is equal to
56.Dv IPC_PRIVATE ,
57or
58.Fa key
59does not have a semaphore set associated with it and the
60.Dv IPC_CREAT
61bit is set in
62.Fa semflg .
63.Pp
64The access modes of the created semaphores is specified in
65.Fa semflg
66as a bitwise OR of zero or more of the following values:
67.Bd -literal -offset indent
68SEM_A        alter permission for owner
69SEM_R        read permission for owner
70
71SEM_A >> 3   alter permission for group
72SEM_R >> 3   read permission for group
73
74SEM_A >> 6   alter permission for other
75SEM_R >> 6   read permission for other
76.Ed
77.Pp
78If a new set of semaphores is created, the data structure associated with it
79(the
80.Va semid_ds
81structure, see
82.Xr semctl 2 )
83is initialized as follows:
84.Bl -bullet
85.It
86.Va sem_perm.cuid
87and
88.Va sem_perm.uid
89are set to the effective UID of the calling process.
90.It
91.Va sem_perm.gid
92and
93.Va sem_perm.cgid
94are set to the effective GID of the calling process.
95.It
96.Va sem_perm.mode
97is set to the lower 9 bits of
98.Fa semflg .
99.It
100.Va sem_nsems
101is set to the value of
102.Fa nsems .
103.It
104.Va sem_ctime
105is set to the current time.
106.It
107.Va sem_otime
108is set to 0.
109.El
110.Sh RETURN VALUES
111.Fn semget
112returns a non-negative semaphore identifier if successful.
113Otherwise, \-1 is returned and
114.Va errno
115is set to reflect the error.
116.Sh ERRORS
117.Bl -tag -width Er
118.It Bq Er EACCES
119The caller has no permission to access a semaphore set already associated with
120.Fa key .
121.It Bq Er EEXIST
122Both
123.Dv IPC_CREAT
124and
125.Dv IPC_EXCL
126are set in
127.Fa semflg ,
128and a semaphore set is already associated with
129.Fa key .
130.It Bq Er EINVAL
131.Va nsems
132is less than or equal to 0 or greater than the system limit for the
133number in a semaphore set.
134.Pp
135A semaphore set associated with
136.Fa key
137exists, but has fewer semaphores than the number specified in
138.Fa nsems .
139.It Bq Er ENOSPC
140A new set of semaphores could not be created because the system limit
141for the number of semaphores or the number of semaphore sets has been
142reached.
143.It Bq Er ENOENT
144.Dv IPC_CREAT
145was not set in
146.Fa semflg
147and no semaphore set associated with
148.Fa key
149was found.
150.El
151.Sh SEE ALSO
152.Xr semctl 2 ,
153.Xr semop 2 ,
154.Xr ftok 3
155