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