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