]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semctl.2
Many pages: LIBRARY: srcfix
[thirdparty/man-pages.git] / man2 / semctl.2
CommitLineData
fea681da 1.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
c11b1abf 2.\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
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>
c11b1abf 9.\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 10.\" Modified 21 Dec 2001, aeb
c11b1abf 11.\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 12.\" Added notes on CAP_IPC_OWNER requirement
c11b1abf 13.\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 14.\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
c11b1abf 15.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
70d0e5ef
MK
16.\" Language and formatting clean-ups
17.\" Rewrote semun text
18.\" Added semid_ds and ipc_perm structure definitions
24ed820a 19.\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
d2ff4f86 20.\" 2018-03-20, dbueso: Added SEM_STAT_ANY description.
fea681da 21.\"
1d767b55 22.TH SEMCTL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
fea681da 23.SH NAME
47f065d6 24semctl \- System V semaphore control operations
34f51a62
AC
25.SH LIBRARY
26Standard C library
8fc3b2cf 27.RI ( libc ", " \-lc )
fea681da
MK
28.SH SYNOPSIS
29.nf
fea681da 30.B #include <sys/sem.h>
68e4db0a 31.PP
fea681da
MK
32.BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
33.fi
34.SH DESCRIPTION
540036b2 35.BR semctl ()
fea681da
MK
36performs the control operation specified by
37.I cmd
efbfd7ec 38on the System\ V semaphore set identified by
fea681da
MK
39.IR semid ,
40or on the
41.IR semnum -th
42semaphore of that set.
70d0e5ef 43(The semaphores in a set are numbered starting at 0.)
fea681da 44.PP
70d0e5ef
MK
45This function has three or four arguments, depending on
46.IR cmd .
47When there are four, the fourth has the type
48.IR "union semun" .
49The \fIcalling program\fP must define this union as follows:
efeece04 50.PP
70d0e5ef 51.in +4n
b8302363 52.EX
fea681da 53union semun {
70d0e5ef
MK
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 */
c13182ef 57 struct seminfo *__buf; /* Buffer for IPC_INFO
d064d41a 58 (Linux\-specific) */
fea681da 59};
b8302363 60.EE
e646a1ba 61.in
fea681da 62.PP
70d0e5ef
MK
63The
64.I semid_ds
c84371c6 65data structure is defined in \fI<sys/sem.h>\fP as follows:
e646a1ba 66.PP
70d0e5ef 67.in +4n
e646a1ba 68.EX
70d0e5ef 69struct semid_ds {
45cbde66 70 struct ipc_perm sem_perm; /* Ownership and permissions */
70d0e5ef 71 time_t sem_otime; /* Last semop time */
5fd4345e
MK
72 time_t sem_ctime; /* Creation time/time of last
73 modification via semctl() */
32398a62 74 unsigned long sem_nsems; /* No. of semaphores in set */
70d0e5ef 75};
b8302363 76.EE
e646a1ba 77.in
a79c3fc0 78.PP
1c9b3f5f
MK
79The fields of the
80.I semid_ds
81structure are as follows:
82.TP 11
83.I sem_perm
c7d32a9e 84This is an
1c9b3f5f 85.I ipc_perm
c7d32a9e 86structure (see below) that specifies the access permissions on the semaphore
1c9b3f5f
MK
87set.
88.TP
89.I sem_otime
90Time of last
91.BR semop (2)
92system call.
93.TP
94.I sem_ctime
5fd4345e 95Time of creation of semaphore set or time of last
6fea5f4f 96.BR semctl ()
5fd4345e
MK
97.BR IPCSET ,
98.BR SETVAL ,
99or
f3061d36 100.BR SETALL
5fd4345e 101operation.
1c9b3f5f
MK
102.TP
103.I sem_nsems
104Number of semaphores in the set.
105Each semaphore of the set is referenced by a nonnegative integer
106ranging from
107.B 0
108to
109.IR sem_nsems\-1 .
70d0e5ef
MK
110.PP
111The
112.I ipc_perm
548be2a6 113structure is defined as follows
70d0e5ef
MK
114(the highlighted fields are settable using
115.BR IPC_SET ):
116.PP
70d0e5ef 117.in +4n
b8302363 118.EX
70d0e5ef 119struct ipc_perm {
b546318a 120 key_t __key; /* Key supplied to semget(2) */
58413227
MK
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 */
70d0e5ef 125 unsigned short \fBmode\fP; /* Permissions */
b546318a 126 unsigned short __seq; /* Sequence number */
70d0e5ef 127};
b8302363 128.EE
e646a1ba 129.in
70d0e5ef 130.PP
702f1a76
MK
131The least significant 9 bits of the
132.I mode
133field of the
134.I ipc_perm
135structure define the access permissions for the shared memory segment.
136The permission bits are as follows:
137.TS
138l l.
1390400 Read by user
1400200 Write by user
1410040 Read by group
1420020 Write by group
1430004 Read by others
1440002 Write by others
145.TE
146.PP
147In effect, "write" means "alter" for a semaphore set.
148Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
149.PP
70d0e5ef 150Valid values for
fea681da
MK
151.I cmd
152are:
0019177e 153.TP
fea681da 154.B IPC_STAT
70d0e5ef
MK
155Copy information from the kernel data structure associated with
156.I semid
157into the
158.I semid_ds
159structure pointed to by
160.IR arg.buf .
fea681da
MK
161The argument
162.I semnum
163is ignored.
70d0e5ef 164The calling process must have read permission on the semaphore set.
fea681da
MK
165.TP
166.B IPC_SET
167Write the values of some members of the
70d0e5ef 168.I semid_ds
fea681da 169structure pointed to by
70d0e5ef
MK
170.I arg.buf
171to the kernel data structure associated with this semaphore set,
172updating also its
173.I sem_ctime
fea681da 174member.
0a33c83e 175.IP
c13182ef 176The following members of the structure are updated:
70d0e5ef
MK
177.IR sem_perm.uid ,
178.IR sem_perm.gid ,
179and (the least significant 9 bits of)
180.IR sem_perm.mode .
0a33c83e 181.IP
70d0e5ef
MK
182The effective UID of the calling process must match the owner
183.RI ( sem_perm.uid )
c13182ef 184or creator
70d0e5ef
MK
185.RI ( sem_perm.cuid )
186of the semaphore set, or the caller must be privileged.
fea681da
MK
187The argument
188.I semnum
189is ignored.
190.TP
191.B IPC_RMID
70d0e5ef
MK
192Immediately remove the semaphore set,
193awakening all processes blocked in
0bfa087b 194.BR semop (2)
70d0e5ef
MK
195calls on the set (with an error return and
196.I errno
fea681da
MK
197set to
198.BR EIDRM ).
499d62f2 199The effective user ID of the calling process must
540036b2
MK
200match the creator or owner of the semaphore set,
201or the caller must be privileged.
fea681da
MK
202The argument
203.I semnum
204is ignored.
205.TP
d064d41a 206.BR IPC_INFO " (Linux\-specific)"
5fe2d5b7 207Return information about system-wide semaphore limits and
24ed820a
MK
208parameters in the structure pointed to by
209.IR arg.__buf .
210This structure is of type
211.IR seminfo ,
212defined in
213.I <sys/sem.h>
682edefb 214if the
0daa9e92 215.B _GNU_SOURCE
682edefb 216feature test macro is defined:
e646a1ba 217.IP
088a639b 218.in +4n
e646a1ba 219.EX
24ed820a 220struct seminfo {
ffc19357
MK
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
24ed820a 225 semaphore sets */
d064d41a 226 int semmnu; /* System\-wide maximum number of undo
ffc19357
MK
227 structures; unused within kernel */
228 int semmsl; /* Maximum number of semaphores in a
229 set */
230 int semopm; /* Maximum number of operations for
231 semop(2) */
232 int semume; /* Maximum number of undo entries per
233 process; unused within kernel */
234 int semusz; /* Size of struct sem_undo */
24ed820a 235 int semvmx; /* Maximum semaphore value */
c13182ef 236 int semaem; /* Max. value that can be recorded for
24ed820a
MK
237 semaphore adjustment (SEM_UNDO) */
238};
e646a1ba 239.EE
088a639b 240.in
e646a1ba 241.IP
c13182ef 242The
24ed820a
MK
243.IR semmsl ,
244.IR semmns ,
245.IR semopm ,
246and
247.I semmni
248settings can be changed via
249.IR /proc/sys/kernel/sem ;
250see
c13182ef 251.BR proc (5)
24ed820a
MK
252for details.
253.TP
8382f16d 254.BR SEM_INFO " (Linux-specific)"
5fe2d5b7 255Return a
24ed820a
MK
256.I seminfo
257structure containing the same information as for
258.BR IPC_INFO ,
c13182ef 259except that the following fields are returned with information
24ed820a
MK
260about system resources consumed by semaphores: the
261.I semusz
c13182ef 262field returns the number of semaphore sets that currently exist
24ed820a
MK
263on the system; and the
264.I semaem
c13182ef 265field returns the total number of semaphores in all semaphore sets
24ed820a
MK
266on the system.
267.TP
8382f16d 268.BR SEM_STAT " (Linux-specific)"
5fe2d5b7 269Return a
c13182ef 270.I semid_ds
24ed820a
MK
271structure as for
272.BR IPC_STAT .
273However, the
274.I semid
275argument is not a semaphore identifier, but instead an index into
276the kernel's internal array that maintains information about
277all semaphore sets on the system.
278.TP
f7246c6d 279.BR SEM_STAT_ANY " (Linux-specific, since Linux 4.17)"
d2ff4f86 280Return a
43ebbd19
YX
281.I semid_ds
282structure as for
d2ff4f86 283.BR SEM_STAT .
c020b5a2 284However,
d2ff4f86
DB
285.I sem_perm.mode
286is not checked for read access for
c020b5a2
MK
287.IR semid
288meaning that any user can employ this operation (just as any user may read
289.IR /proc/sysvipc/sem
290to obtain the same information).
14cbb25e 291.TP
fea681da
MK
292.B GETALL
293Return
294.B semval
70d0e5ef 295(i.e., the current value)
fea681da 296for all semaphores of the set into
d6c57ce6 297.IR arg.array .
fea681da
MK
298The argument
299.I semnum
300is ignored.
70d0e5ef 301The calling process must have read permission on the semaphore set.
fea681da
MK
302.TP
303.B GETNCNT
dd7f869f 304Return the
fea681da 305.B semncnt
dd7f869f 306value for the
fea681da
MK
307.IR semnum \-th
308semaphore of the set
dd7f869f 309(i.e., the number of processes waiting for the semaphore's value to increase).
70d0e5ef 310The calling process must have read permission on the semaphore set.
fea681da
MK
311.TP
312.B GETPID
dd7f869f 313Return the
fea681da 314.B sempid
dd7f869f 315value for the
fea681da 316.IR semnum \-th
84466c16
MK
317semaphore of the set.
318This is the PID of the process that last performed an operation on
319that semaphore (but see NOTES).
70d0e5ef 320The calling process must have read permission on the semaphore set.
fea681da
MK
321.TP
322.B GETVAL
dd7f869f 323Return
fea681da 324.B semval
dd7f869f 325(i.e., the semaphore value) for the
fea681da
MK
326.IR semnum \-th
327semaphore of the set.
70d0e5ef 328The calling process must have read permission on the semaphore set.
fea681da
MK
329.TP
330.B GETZCNT
dd7f869f 331Return the
fea681da 332.B semzcnt
dd7f869f 333value for the
fea681da
MK
334.IR semnum \-th
335semaphore of the set
dd7f869f 336(i.e., the number of processes waiting for the semaphore value to become 0).
70d0e5ef 337The calling process must have read permission on the semaphore set.
fea681da
MK
338.TP
339.B SETALL
dd7f869f 340Set the
fea681da 341.B semval
dd7f869f 342values for all semaphores of the set using
d6c57ce6 343.IR arg.array ,
fea681da 344updating also the
70d0e5ef 345.I sem_ctime
fea681da 346member of the
70d0e5ef
MK
347.I semid_ds
348structure associated with the set.
349Undo entries (see
350.BR semop (2))
351are cleared for altered semaphores in all processes.
352If the changes to semaphore values would permit blocked
0bfa087b 353.BR semop (2)
70d0e5ef 354calls in other processes to proceed, then those processes are woken up.
fea681da
MK
355The argument
356.I semnum
357is ignored.
70d0e5ef
MK
358The calling process must have alter (write) permission on
359the semaphore set.
fea681da
MK
360.TP
361.B SETVAL
765a67c3 362Set the semaphore value
a1286972 363.RB ( semval )
fea681da 364to
0daa9e92 365.I arg.val
fea681da
MK
366for the
367.IR semnum \-th
368semaphore of the set, updating also the
70d0e5ef 369.I sem_ctime
fea681da 370member of the
70d0e5ef
MK
371.I semid_ds
372structure associated with the set.
fea681da 373Undo entries are cleared for altered semaphores in all processes.
70d0e5ef 374If the changes to semaphore values would permit blocked
0bfa087b 375.BR semop (2)
70d0e5ef
MK
376calls in other processes to proceed, then those processes are woken up.
377The calling process must have alter permission on the semaphore set.
47297adb 378.SH RETURN VALUE
9862ec0c 379On success,
540036b2 380.BR semctl ()
9862ec0c 381returns a nonnegative value depending on
fea681da
MK
382.I cmd
383as follows:
0019177e 384.TP
fea681da
MK
385.B GETNCNT
386the value of
387.BR semncnt .
388.TP
389.B GETPID
390the value of
391.BR sempid .
392.TP
393.B GETVAL
394the value of
395.BR semval .
396.TP
397.B GETZCNT
398the value of
399.BR semzcnt .
24ed820a
MK
400.TP
401.B IPC_INFO
402the index of the highest used entry in the
403kernel's internal array recording information about all
404semaphore sets.
405(This information can be used with repeated
14cbb25e
MK
406.B SEM_STAT
407or
408.B SEM_STAT_ANY
24ed820a
MK
409operations to obtain information about all semaphore sets on the system.)
410.TP
411.B SEM_INFO
c5c3137d 412as for
24ed820a
MK
413.BR IPC_INFO .
414.TP
415.B SEM_STAT
416the identifier of the semaphore set whose index was given in
417.IR semid .
d2ff4f86
DB
418.TP
419.B SEM_STAT_ANY
420as for
421.BR SEM_STAT .
dd3568a1 422.PP
fea681da
MK
423All other
424.I cmd
425values return 0 on success.
9862ec0c
MK
426.PP
427On failure,
428.BR semctl ()
429returns \-1 and sets
430.I errno
431to indicate the error.
fea681da 432.SH ERRORS
89b3c6b8 433.TP
fea681da
MK
434.B EACCES
435The argument
436.I cmd
437has one of the values
438.BR GETALL ,
439.BR GETPID ,
440.BR GETVAL ,
441.BR GETNCNT ,
442.BR GETZCNT ,
443.BR IPC_STAT ,
444.BR SEM_STAT ,
d2ff4f86 445.BR SEM_STAT_ANY ,
fea681da
MK
446.BR SETALL ,
447or
448.B SETVAL
449and the calling process does not have the required
450permissions on the semaphore set and does not have the
451.B CAP_IPC_OWNER
3294109d 452capability in the user namespace that governs its IPC namespace.
fea681da
MK
453.TP
454.B EFAULT
455The address pointed to by
0daa9e92 456.I arg.buf
fea681da 457or
0daa9e92 458.I arg.array
fea681da
MK
459isn't accessible.
460.TP
461.B EIDRM
462The semaphore set was removed.
463.TP
464.B EINVAL
465Invalid value for
466.I cmd
467or
468.IR semid .
24ed820a
MK
469Or: for a
470.B SEM_STAT
c13182ef 471operation, the index value specified in
24ed820a
MK
472.I semid
473referred to an array slot that is currently unused.
fea681da
MK
474.TP
475.B EPERM
476The argument
477.I cmd
70d0e5ef 478has the value
fea681da
MK
479.B IPC_SET
480or
481.B IPC_RMID
70d0e5ef 482but the effective user ID of the calling process is not the creator
fea681da
MK
483(as found in
484.IR sem_perm.cuid )
485or the owner
486(as found in
487.IR sem_perm.uid )
488of the semaphore set,
489and the process does not have the
490.B CAP_SYS_ADMIN
491capability.
492.TP
493.B ERANGE
494The argument
495.I cmd
70d0e5ef 496has the value
fea681da
MK
497.B SETALL
498or
499.B SETVAL
500and the value to which
501.B semval
70d0e5ef
MK
502is to be set (for some semaphore of the set) is less than 0
503or greater than the implementation limit
fea681da 504.BR SEMVMX .
47297adb 505.SH CONFORMING TO
175b0eec 506POSIX.1-2001, POSIX.1-2008, SVr4.
a1d5f77c 507.\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
efeece04 508.PP
26356091
MK
509POSIX.1 specifies the
510.\" POSIX.1-2001, POSIX.1-2008
32398a62
MK
511.I sem_nsems
512field of the
513.I semid_ds
514structure as having the type
515.IR "unsigned\ short" ,
516and the field is so defined on most other systems.
517It was also so defined on Linux 2.2 and earlier,
518but, since Linux 2.4, the field has the type
519.IR "unsigned\ long" .
fea681da
MK
520.SH NOTES
521The
522.BR IPC_INFO ,
d556548b 523.BR SEM_STAT ,
fea681da
MK
524and
525.B SEM_INFO
24ed820a 526operations are used by the
81a8f8e7 527.BR ipcs (1)
fea681da 528program to provide information on allocated resources.
282c1a35
MK
529In the future these may modified or moved to a
530.I /proc
531filesystem interface.
dd3568a1 532.PP
e3e25559
MK
533Various fields in a \fIstruct semid_ds\fP were typed as
534.I short
535under Linux 2.2
536and have become
537.I long
538under Linux 2.4.
677f4766 539To take advantage of this,
fea681da 540a recompilation under glibc-2.1.91 or later should suffice.
682edefb 541(The kernel distinguishes old and new calls by an
0daa9e92 542.B IPC_64
682edefb 543flag in
fea681da
MK
544.IR cmd .)
545.PP
70d0e5ef
MK
546In some earlier versions of glibc, the
547.I semun
26356091
MK
548union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
549.\" POSIX.1-2001, POSIX.1-2008
70d0e5ef
MK
550that the caller define this union.
551On versions of glibc where this union is \fInot\fP defined,
552the macro
553.B _SEM_SEMUN_UNDEFINED
c84371c6 554is defined in \fI<sys/sem.h>\fP.
70d0e5ef 555.PP
fea681da 556The following system limit on semaphore sets affects a
540036b2 557.BR semctl ()
fea681da 558call:
89b3c6b8 559.TP
fea681da
MK
560.B SEMVMX
561Maximum value for
562.BR semval :
563implementation dependent (32767).
dd3568a1 564.PP
27ab91ca 565For greater portability, it is best to always call
540036b2 566.BR semctl ()
fea681da 567with four arguments.
84466c16
MK
568.\"
569.SS The sempid value
570POSIX.1 defines
571.I sempid
572as the "process ID of [the] last operation" on a semaphore,
573and explicitly notes that this value is set by a successful
574.BR semop (2)
575call, with the implication that no other interface affects the
576.I sempid
577value.
efeece04 578.PP
84466c16
MK
579While some implementations conform to the behavior specified in POSIX.1,
580others do not.
581(The fault here probably lies with POSIX.1 inasmuch as it likely failed
582to capture the full range of existing implementation behaviors.)
583Various other implementations
584.\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
585also update
586.I sempid
587for the other operations that update the value of a semaphore: the
588.B SETVAL
589and
590.B SETALL
591operations, as well as the semaphore adjustments performed
592on process termination as a consequence of the use of the
593.B SEM_UNDO
594flag (see
595.BR semop (2)).
efeece04 596.PP
84466c16
MK
597Linux also updates
598.I sempid
599for
600.BR SETVAL
601operations and semaphore adjustments.
ce160b21
MK
602However, somewhat inconsistently, up to and including Linux 4.5,
603the kernel did not update
84466c16
MK
604.I sempid
605for
606.BR SETALL
607operations.
efcc21c9
MK
608This was rectified
609.\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f
610in Linux 4.6.
a14af333 611.SH EXAMPLES
5d7a1843
MK
612See
613.BR shmop (2).
47297adb 614.SH SEE ALSO
fea681da
MK
615.BR ipc (2),
616.BR semget (2),
617.BR semop (2),
f675ea37 618.BR capabilities (7),
2c5e151c 619.BR sem_overview (7),
343cdc5a 620.BR sysvipc (7)