1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Tue Oct 22 17:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
7 .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
8 .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
9 .\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Modified 21 Dec 2001, aeb
11 .\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" Added notes on CAP_IPC_OWNER requirement
13 .\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
14 .\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
15 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
16 .\" Language and formatting clean-ups
17 .\" Rewrote semun text
18 .\" Added semid_ds and ipc_perm structure definitions
19 .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
20 .\" 2018-03-20, dbueso: Added SEM_STAT_ANY description.
22 .TH SEMCTL 2 2022-09-09 "Linux man-pages (unreleased)"
24 semctl \- System V semaphore control operations
27 .RI ( libc ", " \-lc )
30 .B #include <sys/sem.h>
32 .BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
36 performs the control operation specified by
38 on the System\ V semaphore set identified by
42 semaphore of that set.
43 (The semaphores in a set are numbered starting at 0.)
45 This function has three or four arguments, depending on
47 When there are four, the fourth has the type
49 The \fIcalling program\fP must define this union as follows:
54 int val; /* Value for SETVAL */
55 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
56 unsigned short *array; /* Array for GETALL, SETALL */
57 struct seminfo *__buf; /* Buffer for IPC_INFO
65 data structure is defined in \fI<sys/sem.h>\fP as follows:
70 struct ipc_perm sem_perm; /* Ownership and permissions */
71 time_t sem_otime; /* Last semop time */
72 time_t sem_ctime; /* Creation time/time of last
73 modification via semctl() */
74 unsigned long sem_nsems; /* No. of semaphores in set */
81 structure are as follows:
86 structure (see below) that specifies the access permissions on the semaphore
95 Time of creation of semaphore set or time of last
104 Number of semaphores in the set.
105 Each semaphore of the set is referenced by a nonnegative integer
113 structure is defined as follows
114 (the highlighted fields are settable using
120 key_t __key; /* Key supplied to semget(2) */
121 uid_t \fBuid\fP; /* Effective UID of owner */
122 gid_t \fBgid\fP; /* Effective GID of owner */
123 uid_t cuid; /* Effective UID of creator */
124 gid_t cgid; /* Effective GID of creator */
125 unsigned short \fBmode\fP; /* Permissions */
126 unsigned short __seq; /* Sequence number */
131 The least significant 9 bits of the
135 structure define the access permissions for the shared memory segment.
136 The permission bits are as follows:
147 In effect, "write" means "alter" for a semaphore set.
148 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
155 Copy information from the kernel data structure associated with
159 structure pointed to by
164 The calling process must have read permission on the semaphore set.
167 Write the values of some members of the
169 structure pointed to by
171 to the kernel data structure associated with this semaphore set,
176 The following members of the structure are updated:
179 and (the least significant 9 bits of)
182 The effective UID of the calling process must match the owner
185 .RI ( sem_perm.cuid )
186 of the semaphore set, or the caller must be privileged.
192 Immediately remove the semaphore set,
193 awakening all processes blocked in
195 calls on the set (with an error return and
199 The effective user ID of the calling process must
200 match the creator or owner of the semaphore set,
201 or the caller must be privileged.
206 .BR IPC_INFO " (Linux\-specific)"
207 Return information about system-wide semaphore limits and
208 parameters in the structure pointed to by
210 This structure is of type
216 feature test macro is defined:
221 int semmap; /* Number of entries in semaphore
222 map; unused within kernel */
223 int semmni; /* Maximum number of semaphore sets */
224 int semmns; /* Maximum number of semaphores in all
226 int semmnu; /* System\-wide maximum number of undo
227 structures; unused within kernel */
228 int semmsl; /* Maximum number of semaphores in a
230 int semopm; /* Maximum number of operations for
232 int semume; /* Maximum number of undo entries per
233 process; unused within kernel */
234 int semusz; /* Size of struct sem_undo */
235 int semvmx; /* Maximum semaphore value */
236 int semaem; /* Max. value that can be recorded for
237 semaphore adjustment (SEM_UNDO) */
248 settings can be changed via
249 .IR /proc/sys/kernel/sem ;
254 .BR SEM_INFO " (Linux-specific)"
257 structure containing the same information as for
259 except that the following fields are returned with information
260 about system resources consumed by semaphores: the
262 field returns the number of semaphore sets that currently exist
263 on the system; and the
265 field returns the total number of semaphores in all semaphore sets
268 .BR SEM_STAT " (Linux-specific)"
275 argument is not a semaphore identifier, but instead an index into
276 the kernel's internal array that maintains information about
277 all semaphore sets on the system.
279 .BR SEM_STAT_ANY " (Linux-specific, since Linux 4.17)"
286 is not checked for read access for
288 meaning that any user can employ this operation (just as any user may read
290 to obtain the same information).
295 (i.e., the current value)
296 for all semaphores of the set into
301 The calling process must have read permission on the semaphore set.
309 (i.e., the number of processes waiting for the semaphore's value to increase).
310 The calling process must have read permission on the semaphore set.
317 semaphore of the set.
318 This is the PID of the process that last performed an operation on
319 that semaphore (but see NOTES).
320 The calling process must have read permission on the semaphore set.
325 (i.e., the semaphore value) for the
327 semaphore of the set.
328 The calling process must have read permission on the semaphore set.
336 (i.e., the number of processes waiting for the semaphore value to become 0).
337 The calling process must have read permission on the semaphore set.
342 values for all semaphores of the set using
348 structure associated with the set.
351 are cleared for altered semaphores in all processes.
352 If the changes to semaphore values would permit blocked
354 calls in other processes to proceed, then those processes are woken up.
358 The calling process must have alter (write) permission on
362 Set the semaphore value
368 semaphore of the set, updating also the
372 structure associated with the set.
373 Undo entries are cleared for altered semaphores in all processes.
374 If the changes to semaphore values would permit blocked
376 calls in other processes to proceed, then those processes are woken up.
377 The calling process must have alter permission on the semaphore set.
381 returns a nonnegative value depending on
402 the index of the highest used entry in the
403 kernel's internal array recording information about all
405 (This information can be used with repeated
409 operations to obtain information about all semaphore sets on the system.)
416 the identifier of the semaphore set whose index was given in
425 values return 0 on success.
431 to indicate the error.
437 has one of the values
449 and the calling process does not have the required
450 permissions on the semaphore set and does not have the
452 capability in the user namespace that governs its IPC namespace.
455 The address pointed to by
462 The semaphore set was removed.
471 operation, the index value specified in
473 referred to an array slot that is currently unused.
482 but the effective user ID of the calling process is not the creator
488 of the semaphore set,
489 and the process does not have the
500 and the value to which
502 is to be set (for some semaphore of the set) is less than 0
503 or greater than the implementation limit
506 POSIX.1-2001, POSIX.1-2008, SVr4.
507 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
509 POSIX.1 specifies the
510 .\" POSIX.1-2001, POSIX.1-2008
514 structure as having the type
515 .IR "unsigned\ short" ,
516 and the field is so defined on most other systems.
517 It was also so defined on Linux 2.2 and earlier,
518 but, since Linux 2.4, the field has the type
519 .IR "unsigned\ long" .
526 operations are used by the
528 program to provide information on allocated resources.
529 In the future these may modified or moved to a
531 filesystem interface.
533 Various fields in a \fIstruct semid_ds\fP were typed as
539 To take advantage of this,
540 a recompilation under glibc-2.1.91 or later should suffice.
541 (The kernel distinguishes old and new calls by an
546 In some earlier versions of glibc, the
548 union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
549 .\" POSIX.1-2001, POSIX.1-2008
550 that the caller define this union.
551 On versions of glibc where this union is \fInot\fP defined,
553 .B _SEM_SEMUN_UNDEFINED
554 is defined in \fI<sys/sem.h>\fP.
556 The following system limit on semaphore sets affects a
563 implementation dependent (32767).
565 For greater portability, it is best to always call
572 as the "process ID of [the] last operation" on a semaphore,
573 and explicitly notes that this value is set by a successful
575 call, with the implication that no other interface affects the
579 While some implementations conform to the behavior specified in POSIX.1,
581 (The fault here probably lies with POSIX.1 inasmuch as it likely failed
582 to capture the full range of existing implementation behaviors.)
583 Various other implementations
584 .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
587 for the other operations that update the value of a semaphore: the
591 operations, as well as the semaphore adjustments performed
592 on process termination as a consequence of the use of the
601 operations and semaphore adjustments.
602 However, somewhat inconsistently, up to and including Linux 4.5,
603 the kernel did not update
609 .\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f
618 .BR capabilities (7),
619 .BR sem_overview (7),