1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Tue Oct 22 17:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Modified 21 Dec 2001, aeb
31 .\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Added notes on CAP_IPC_OWNER requirement
33 .\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Language and formatting clean-ups
37 .\" Rewrote semun text
38 .\" Added semid_ds and ipc_perm structure definitions
39 .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
40 .\" 2018-03-20, dbueso: Added SEM_STAT_ANY description.
42 .TH SEMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
44 semctl \- System V semaphore control operations
47 .B #include <sys/types.h>
48 .B #include <sys/ipc.h>
49 .B #include <sys/sem.h>
51 .BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
55 performs the control operation specified by
57 on the System\ V semaphore set identified by
61 semaphore of that set.
62 (The semaphores in a set are numbered starting at 0.)
64 This function has three or four arguments, depending on
66 When there are four, the fourth has the type
68 The \fIcalling program\fP must define this union as follows:
73 int val; /* Value for SETVAL */
74 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
75 unsigned short *array; /* Array for GETALL, SETALL */
76 struct seminfo *__buf; /* Buffer for IPC_INFO
84 data structure is defined in \fI<sys/sem.h>\fP as follows:
89 struct ipc_perm sem_perm; /* Ownership and permissions */
90 time_t sem_otime; /* Last semop time */
91 time_t sem_ctime; /* Last change time */
92 unsigned long sem_nsems; /* No. of semaphores in set */
99 structure is defined as follows
100 (the highlighted fields are settable using
106 key_t __key; /* Key supplied to semget(2) */
107 uid_t \fBuid\fP; /* Effective UID of owner */
108 gid_t \fBgid\fP; /* Effective GID of owner */
109 uid_t cuid; /* Effective UID of creator */
110 gid_t cgid; /* Effective GID of creator */
111 unsigned short \fBmode\fP; /* Permissions */
112 unsigned short __seq; /* Sequence number */
122 Copy information from the kernel data structure associated with
126 structure pointed to by
131 The calling process must have read permission on the semaphore set.
134 Write the values of some members of the
136 structure pointed to by
138 to the kernel data structure associated with this semaphore set,
142 The following members of the structure are updated:
145 and (the least significant 9 bits of)
147 The effective UID of the calling process must match the owner
150 .RI ( sem_perm.cuid )
151 of the semaphore set, or the caller must be privileged.
157 Immediately remove the semaphore set,
158 awakening all processes blocked in
160 calls on the set (with an error return and
164 The effective user ID of the calling process must
165 match the creator or owner of the semaphore set,
166 or the caller must be privileged.
171 .BR IPC_INFO " (Linux-specific)"
172 Return information about system-wide semaphore limits and
173 parameters in the structure pointed to by
175 This structure is of type
181 feature test macro is defined:
186 int semmap; /* Number of entries in semaphore
187 map; unused within kernel */
188 int semmni; /* Maximum number of semaphore sets */
189 int semmns; /* Maximum number of semaphores in all
191 int semmnu; /* System-wide maximum number of undo
192 structures; unused within kernel */
193 int semmsl; /* Maximum number of semaphores in a
195 int semopm; /* Maximum number of operations for
197 int semume; /* Maximum number of undo entries per
198 process; unused within kernel */
199 int semusz; /* Size of struct sem_undo */
200 int semvmx; /* Maximum semaphore value */
201 int semaem; /* Max. value that can be recorded for
202 semaphore adjustment (SEM_UNDO) */
213 settings can be changed via
214 .IR /proc/sys/kernel/sem ;
219 .BR SEM_INFO " (Linux-specific)"
222 structure containing the same information as for
224 except that the following fields are returned with information
225 about system resources consumed by semaphores: the
227 field returns the number of semaphore sets that currently exist
228 on the system; and the
230 field returns the total number of semaphores in all semaphore sets
233 .BR SEM_STAT " (Linux-specific)"
240 argument is not a semaphore identifier, but instead an index into
241 the kernel's internal array that maintains information about
242 all semaphore sets on the system.
244 .BR SEM_STAT_ANY " (Linux-specific, since Linux 4.17)"
247 structure containing the same information as for
251 is not checked for read access for
253 meaning that any user can employ this operation (just as any user may read
254 .IR /proc/sysvipc/sem
255 to obtain the same information).
260 (i.e., the current value)
261 for all semaphores of the set into
266 The calling process must have read permission on the semaphore set.
274 (i.e., the number of processes waiting for an increase of
278 semaphore of the set).
279 The calling process must have read permission on the semaphore set.
286 semaphore of the set.
287 This is the PID of the process that last performed an operation on
288 that semaphore (but see NOTES).
289 The calling process must have read permission on the semaphore set.
296 semaphore of the set.
297 The calling process must have read permission on the semaphore set.
305 (i.e., the number of processes waiting for
309 semaphore of the set to become 0).
310 The calling process must have read permission on the semaphore set.
315 for all semaphores of the set using
321 structure associated with the set.
324 are cleared for altered semaphores in all processes.
325 If the changes to semaphore values would permit blocked
327 calls in other processes to proceed, then those processes are woken up.
331 The calling process must have alter (write) permission on
341 semaphore of the set, updating also the
345 structure associated with the set.
346 Undo entries are cleared for altered semaphores in all processes.
347 If the changes to semaphore values would permit blocked
349 calls in other processes to proceed, then those processes are woken up.
350 The calling process must have alter permission on the semaphore set.
357 indicating the error.
359 Otherwise, the system call returns a nonnegative value depending on
380 the index of the highest used entry in the
381 kernel's internal array recording information about all
383 (This information can be used with repeated
387 operations to obtain information about all semaphore sets on the system.)
394 the identifier of the semaphore set whose index was given in
403 values return 0 on success.
407 will be set to one of the following:
412 has one of the values
424 and the calling process does not have the required
425 permissions on the semaphore set and does not have the
427 capability in the user namespace that governs its IPC namespace.
430 The address pointed to by
437 The semaphore set was removed.
446 operation, the index value specified in
448 referred to an array slot that is currently unused.
457 but the effective user ID of the calling process is not the creator
463 of the semaphore set,
464 and the process does not have the
475 and the value to which
477 is to be set (for some semaphore of the set) is less than 0
478 or greater than the implementation limit
481 POSIX.1-2001, POSIX.1-2008, SVr4.
482 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
484 POSIX.1 specifies the
485 .\" POSIX.1-2001, POSIX.1-2008
489 structure as having the type
490 .IR "unsigned\ short" ,
491 and the field is so defined on most other systems.
492 It was also so defined on Linux 2.2 and earlier,
493 but, since Linux 2.4, the field has the type
494 .IR "unsigned\ long" .
500 isn't required on Linux or by any version of POSIX.
502 some old implementations required the inclusion of these header files,
503 and the SVID also documented their inclusion.
504 Applications intended to be portable to such old systems may need
505 to include these header files.
506 .\" Like Linux, the FreeBSD man pages still document
507 .\" the inclusion of these header files.
514 operations are used by the
516 program to provide information on allocated resources.
517 In the future these may modified or moved to a
519 filesystem interface.
521 Various fields in a \fIstruct semid_ds\fP were typed as
527 To take advantage of this,
528 a recompilation under glibc-2.1.91 or later should suffice.
529 (The kernel distinguishes old and new calls by an
534 In some earlier versions of glibc, the
536 union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
537 .\" POSIX.1-2001, POSIX.1-2008
538 that the caller define this union.
539 On versions of glibc where this union is \fInot\fP defined,
541 .B _SEM_SEMUN_UNDEFINED
542 is defined in \fI<sys/sem.h>\fP.
544 The following system limit on semaphore sets affects a
551 implementation dependent (32767).
553 For greater portability, it is best to always call
560 as the "process ID of [the] last operation" on a semaphore,
561 and explicitly notes that this value is set by a successful
563 call, with the implication that no other interface affects the
567 While some implementations conform to the behavior specified in POSIX.1,
569 (The fault here probably lies with POSIX.1 inasmuch as it likely failed
570 to capture the full range of existing implementation behaviors.)
571 Various other implementations
572 .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
575 for the other operations that update the value of a semaphore: the
579 operations, as well as the semaphore adjustments performed
580 on process termination as a consequence of the use of the
589 operations and semaphore adjustments.
590 However, somewhat inconsistently, up to and including 4.5,
597 .\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f
603 .BR capabilities (7),
604 .BR sem_overview (7),