xref: /netbsd-src/lib/librt/sem_open.3 (revision 6237a794782646be1682f8910d468d1a3efdde41)
1*6237a794Swiz.\" $NetBSD: sem_open.3,v 1.8 2018/05/05 06:39:10 wiz Exp $
237946878Sthorpej.\"
337946878Sthorpej.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
437946878Sthorpej.\" All rights reserved.
537946878Sthorpej.\"
637946878Sthorpej.\" Redistribution and use in source and binary forms, with or without
737946878Sthorpej.\" modification, are permitted provided that the following conditions
837946878Sthorpej.\" are met:
937946878Sthorpej.\" 1. Redistributions of source code must retain the above copyright
1037946878Sthorpej.\"    notice(s), this list of conditions and the following disclaimer as
1137946878Sthorpej.\"    the first lines of this file unmodified other than the possible
1237946878Sthorpej.\"    addition of one or more copyright notices.
1337946878Sthorpej.\" 2. Redistributions in binary form must reproduce the above copyright
1437946878Sthorpej.\"    notice(s), this list of conditions and the following disclaimer in
1537946878Sthorpej.\"    the documentation and/or other materials provided with the
1637946878Sthorpej.\"    distribution.
1737946878Sthorpej.\"
1837946878Sthorpej.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1937946878Sthorpej.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2037946878Sthorpej.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2137946878Sthorpej.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
2237946878Sthorpej.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2337946878Sthorpej.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2437946878Sthorpej.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2537946878Sthorpej.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2637946878Sthorpej.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2737946878Sthorpej.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2837946878Sthorpej.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2937946878Sthorpej.\"
30a665897cSbriggs.\" From: FreeBSD: src/lib/libc/gen/sem_open.3,v 1.12 2004/07/02 16:45:56 ru
31a665897cSbriggs.\"
329308b349Schristos.Dd May 4, 2018
3337946878Sthorpej.Dt SEM_OPEN 3
3437946878Sthorpej.Os
3537946878Sthorpej.Sh NAME
3637946878Sthorpej.Nm sem_open ,
3737946878Sthorpej.Nm sem_close ,
3837946878Sthorpej.Nm sem_unlink
3937946878Sthorpej.Nd named semaphore operations
4037946878Sthorpej.Sh LIBRARY
41bae43254Speter.Lb librt
4237946878Sthorpej.Sh SYNOPSIS
4337946878Sthorpej.In semaphore.h
44a665897cSbriggs.Ft "sem_t *"
45a665897cSbriggs.Fn sem_open "const char *name" "int oflag" ...
4637946878Sthorpej.Ft int
4737946878Sthorpej.Fn sem_close "sem_t *sem"
4837946878Sthorpej.Ft int
4937946878Sthorpej.Fn sem_unlink "const char *name"
5037946878Sthorpej.Sh DESCRIPTION
5137946878SthorpejThe
5237946878Sthorpej.Fn sem_open
53a665897cSbriggsfunction creates or opens the named semaphore specified by
54a665897cSbriggs.Fa name .
55a665897cSbriggsThe returned semaphore may be used in subsequent calls to
56a665897cSbriggs.Xr sem_getvalue 3 ,
574acff4c0Sjoerg.Xr sem_timedwait 3 ,
58a665897cSbriggs.Xr sem_trywait 3 ,
594acff4c0Sjoerg.Xr sem_wait 3 ,
60a665897cSbriggs.Xr sem_post 3 ,
6137946878Sthorpejand
62a665897cSbriggs.Fn sem_close .
63a665897cSbriggs.Pp
64a665897cSbriggsThe following bits may be set in the
65a665897cSbriggs.Fa oflag
66a665897cSbriggsargument:
67a665897cSbriggs.Bl -tag -width ".Dv O_CREAT"
68a665897cSbriggs.It Dv O_CREAT
69a665897cSbriggsCreate the semaphore if it does not already exist.
70a665897cSbriggs.Pp
71a665897cSbriggsThe third argument to the call to
72a665897cSbriggs.Fn sem_open
73a665897cSbriggsmust be of type
74a665897cSbriggs.Vt mode_t
75a665897cSbriggsand specifies the mode for the semaphore.
76a665897cSbriggsOnly the
77a665897cSbriggs.Dv S_IWUSR , S_IWGRP ,
7837946878Sthorpejand
79a665897cSbriggs.Dv S_IWOTH
80a665897cSbriggsbits are examined;
81a665897cSbriggsit is not possible to grant only
82a665897cSbriggs.Dq read
83a665897cSbriggspermission on a semaphore.
84a665897cSbriggsThe mode is modified according to the process's file creation
85a665897cSbriggsmask; see
86a665897cSbriggs.Xr umask 2 .
87a665897cSbriggs.Pp
88a665897cSbriggsThe fourth argument must be an
89a665897cSbriggs.Vt "unsigned int"
90a665897cSbriggsand specifies the initial value for the semaphore,
91a665897cSbriggsand must be no greater than
92a665897cSbriggs.Dv SEM_VALUE_MAX .
93a665897cSbriggs.It Dv O_EXCL
94a665897cSbriggsCreate the semaphore if it does not exist.
95a665897cSbriggsIf the semaphore already exists,
96a665897cSbriggs.Fn sem_open
97a665897cSbriggswill fail.
98a665897cSbriggsThis flag is ignored unless
99a665897cSbriggs.Dv O_CREAT
100a665897cSbriggsis also specified.
10137946878Sthorpej.El
102a665897cSbriggs.Pp
103a665897cSbriggsThe
104a665897cSbriggs.Fn sem_close
105a665897cSbriggsfunction closes a named semaphore that was opened by a call to
106a665897cSbriggs.Fn sem_open .
107a665897cSbriggs.Pp
108a665897cSbriggsThe
109a665897cSbriggs.Fn sem_unlink
110a665897cSbriggsfunction removes the semaphore named
111a665897cSbriggs.Fa name .
112a665897cSbriggsResources allocated to the semaphore are only deallocated when all
113a665897cSbriggsprocesses that have the semaphore open close it.
114a665897cSbriggs.Sh RETURN VALUES
115a665897cSbriggsIf successful,
116a665897cSbriggsthe
117a665897cSbriggs.Fn sem_open
118a665897cSbriggsfunction returns the address of the opened semaphore.
119a665897cSbriggsIf the same
120a665897cSbriggs.Fa name
121a665897cSbriggsargument is given to multiple calls to
122a665897cSbriggs.Fn sem_open
123a665897cSbriggsby the same process without an intervening call to
124a665897cSbriggs.Fn sem_close ,
125a665897cSbriggsthe same address is returned each time.
126a665897cSbriggsIf the semaphore cannot be opened,
127a665897cSbriggs.Fn sem_open
128a665897cSbriggsreturns
129a665897cSbriggs.Dv SEM_FAILED
130a665897cSbriggsand the global variable
131a665897cSbriggs.Va errno
132a665897cSbriggsis set to indicate the error.
133a665897cSbriggs.Pp
134a665897cSbriggs.Rv -std sem_close sem_unlink
135a665897cSbriggs.Sh ERRORS
136a665897cSbriggsThe
137a665897cSbriggs.Fn sem_open
138a665897cSbriggsfunction will fail if:
139a665897cSbriggs.Bl -tag -width Er
140a665897cSbriggs.It Bq Er EACCES
141a665897cSbriggsThe semaphore exists and the permissions specified by
142a665897cSbriggs.Fa oflag
1435f354a83Swizat the time it was created deny access to this process;
1445f354a83Swizor the semaphore does not exist, but permission to create it is denied.
145a665897cSbriggs.It Bq Er EEXIST
146a665897cSbriggs.Dv O_CREAT
147a665897cSbriggsand
148a665897cSbriggs.Dv O_EXCL
149a665897cSbriggsare set but the semaphore already exists.
150a665897cSbriggs.It Bq Er EINTR
151a665897cSbriggsThe call was interrupted by a signal.
152a665897cSbriggs.It Bq Er EINVAL
153a665897cSbriggsThe
1549308b349Schristos.Fa name
1559308b349Schristosargument does not begin with a
1569308b349Schristos.Sq /
1579308b349Schristosor contains more slashes.
1589308b349SchristosThis is implementation-specific behavior and allowed by
1599308b349Schristos.St -p1003.1-96 .
160*6237a794SwizOr, the
161a665897cSbriggs.Fa value
162a665897cSbriggsargument is greater than
163a665897cSbriggs.Dv SEM_VALUE_MAX .
164a665897cSbriggs.\"FreeBSD never returns EMFILE
165a665897cSbriggs.\".It Bq Er EMFILE
166a665897cSbriggs.\"Too many semaphores are in use by this process.
167a665897cSbriggs.It Bq Er ENAMETOOLONG
1689308b349SchristosThe specified
169a665897cSbriggs.Fa name
1709308b349Schristosis longer than
1719308b349Schristos.Dv NAME_MAX ,
1729308b349Schristosor longer than the implementing file system will allow.
173a665897cSbriggs.It Bq Er ENFILE
174a665897cSbriggsThe system limit on semaphores has been reached.
175a665897cSbriggs.It Bq Er ENOENT
176a665897cSbriggs.Dv O_CREAT
177910da6b6Swizis not set and the named semaphore does not exist.
178a665897cSbriggs.It Bq Er ENOSPC
179a665897cSbriggsThere is not enough space to create the semaphore.
180a665897cSbriggs.El
181a665897cSbriggs.Pp
182a665897cSbriggsThe
183a665897cSbriggs.Fn sem_close
184a665897cSbriggsfunction will fail if:
185a665897cSbriggs.Bl -tag -width Er
186a665897cSbriggs.It Bq Er EINVAL
187a665897cSbriggsThe
188a665897cSbriggs.Fa sem
189a665897cSbriggsargument is not a valid semaphore.
190a665897cSbriggs.El
191a665897cSbriggs.Pp
192a665897cSbriggsThe
193a665897cSbriggs.Fn sem_unlink
194a665897cSbriggsfunction will fail if:
195a665897cSbriggs.Bl -tag -width Er
196a665897cSbriggs.It Bq Er EACCES
197a665897cSbriggsPermission is denied to unlink the semaphore.
198a665897cSbriggs.It Bq Er ENAMETOOLONG
199a665897cSbriggsThe specified
200a665897cSbriggs.Fa name
2019308b349Schristosis longer than
2029308b349Schristos.Dv NAME_MAX ,
2039308b349Schristosor longer than the implementing file system will allow.
204a665897cSbriggs.It Bq Er ENOENT
205a665897cSbriggsThe named semaphore does not exist.
206a665897cSbriggs.El
207a665897cSbriggs.Sh SEE ALSO
208a665897cSbriggs.Xr close 2 ,
209a665897cSbriggs.Xr open 2 ,
210a665897cSbriggs.Xr umask 2 ,
211a665897cSbriggs.Xr unlink 2 ,
212a665897cSbriggs.Xr sem_getvalue 3 ,
213a665897cSbriggs.Xr sem_post 3 ,
214a665897cSbriggs.Xr sem_trywait 3 ,
215a665897cSbriggs.Xr sem_wait 3 ,
216a665897cSbriggs.Xr sem 4
21737946878Sthorpej.Sh STANDARDS
218a665897cSbriggsThe
21937946878Sthorpej.Fn sem_open ,
22037946878Sthorpej.Fn sem_close ,
22137946878Sthorpejand
22237946878Sthorpej.Fn sem_unlink
223a665897cSbriggsfunctions conform to
22437946878Sthorpej.St -p1003.1-96 .
225a665897cSbriggs.Sh HISTORY
226a665897cSbriggsSupport for named semaphores first appeared in
227a665897cSbriggs.Nx 2.0 .
228