]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semctl.2
ffix
[thirdparty/man-pages.git] / man2 / semctl.2
CommitLineData
fea681da 1.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
1130df60 2.\" and Copyright 2004, 2005 Michael Kerrisk <mtk-manpages@gmx.net>
fea681da
MK
3.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\"
24.\" Modified Tue Oct 22 17:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
25.\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
26.\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
305a0578 27.\" Modified 20 Dec 2001, Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 28.\" Modified 21 Dec 2001, aeb
305a0578 29.\" Modified 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 30.\" Added notes on CAP_IPC_OWNER requirement
305a0578 31.\" Modified 17 Jun 2004, Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 32.\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
70d0e5ef
MK
33.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net>
34.\" Language and formatting clean-ups
35.\" Rewrote semun text
36.\" Added semid_ds and ipc_perm structure definitions
24ed820a 37.\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
fea681da 38.\"
d9343c5c 39.TH SEMCTL 2 2004-11-10 "Linux" "Linux Programmer's Manual"
fea681da
MK
40.SH NAME
41semctl \- semaphore control operations
42.SH SYNOPSIS
43.nf
44.B #include <sys/types.h>
45.B #include <sys/ipc.h>
46.B #include <sys/sem.h>
47.sp
48.BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
49.fi
50.SH DESCRIPTION
540036b2 51.BR semctl ()
fea681da
MK
52performs the control operation specified by
53.I cmd
54on the semaphore set identified by
55.IR semid ,
56or on the
57.IR semnum -th
58semaphore of that set.
70d0e5ef 59(The semaphores in a set are numbered starting at 0.)
fea681da 60.PP
70d0e5ef
MK
61This function has three or four arguments, depending on
62.IR cmd .
63When there are four, the fourth has the type
64.IR "union semun" .
65The \fIcalling program\fP must define this union as follows:
fea681da
MK
66
67.nf
70d0e5ef 68.in +4n
fea681da 69union semun {
70d0e5ef
MK
70 int val; /* Value for SETVAL */
71 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
72 unsigned short *array; /* Array for GETALL, SETALL */
c13182ef 73 struct seminfo *__buf; /* Buffer for IPC_INFO
70d0e5ef 74 (Linux specific) */
fea681da 75};
70d0e5ef 76.in -4n
fea681da
MK
77.fi
78.PP
70d0e5ef
MK
79The
80.I semid_ds
c84371c6 81data structure is defined in \fI<sys/sem.h>\fP as follows:
70d0e5ef
MK
82.nf
83.in +4n
84
85struct semid_ds {
86 struct ipc_perm sem_perm; /* Ownership and permissions
87 time_t sem_otime; /* Last semop time */
88 time_t sem_ctime; /* Last change time */
89 unsigned short sem_nsems; /* No. of semaphores in set */
90};
91.in -4n
92.fi
93.PP
94The
95.I ipc_perm
c84371c6 96structure is defined in \fI<sys/ipc.h>\fP as follows
70d0e5ef
MK
97(the highlighted fields are settable using
98.BR IPC_SET ):
99.PP
100.nf
101.in +4n
102struct ipc_perm {
0bfa087b 103 key_t key; /* Key supplied to semget(2) */
70d0e5ef
MK
104 uid_t \fBuid\fP; /* Effective UID of owner */
105 gid_t \fBgid\fP; /* Effective GID of owner */
106 uid_t cuid; /* Effective UID of creator */
107 gid_t cgid; /* Effective GID of creator */
108 unsigned short \fBmode\fP; /* Permissions */
109 unsigned short seq; /* Sequence number */
110};
111.in -4n
112.fi
113.PP
114Valid values for
fea681da
MK
115.I cmd
116are:
117.TP 12
118.B IPC_STAT
70d0e5ef
MK
119Copy information from the kernel data structure associated with
120.I semid
121into the
122.I semid_ds
123structure pointed to by
124.IR arg.buf .
fea681da
MK
125The argument
126.I semnum
127is ignored.
70d0e5ef 128The calling process must have read permission on the semaphore set.
fea681da
MK
129.TP
130.B IPC_SET
131Write the values of some members of the
70d0e5ef 132.I semid_ds
fea681da 133structure pointed to by
70d0e5ef
MK
134.I arg.buf
135to the kernel data structure associated with this semaphore set,
136updating also its
137.I sem_ctime
fea681da 138member.
c13182ef 139The following members of the structure are updated:
70d0e5ef
MK
140.IR sem_perm.uid ,
141.IR sem_perm.gid ,
142and (the least significant 9 bits of)
143.IR sem_perm.mode .
144The effective UID of the calling process must match the owner
145.RI ( sem_perm.uid )
c13182ef 146or creator
70d0e5ef
MK
147.RI ( sem_perm.cuid )
148of the semaphore set, or the caller must be privileged.
fea681da
MK
149The argument
150.I semnum
151is ignored.
152.TP
153.B IPC_RMID
70d0e5ef
MK
154Immediately remove the semaphore set,
155awakening all processes blocked in
0bfa087b 156.BR semop (2)
70d0e5ef
MK
157calls on the set (with an error return and
158.I errno
fea681da
MK
159set to
160.BR EIDRM ).
499d62f2 161The effective user ID of the calling process must
540036b2
MK
162match the creator or owner of the semaphore set,
163or the caller must be privileged.
fea681da
MK
164The argument
165.I semnum
166is ignored.
167.TP
24ed820a 168.BR IPC_INFO " (Linux specific)"
c13182ef 169Returns information about system-wide semaphore limits and
24ed820a
MK
170parameters in the structure pointed to by
171.IR arg.__buf .
172This structure is of type
173.IR seminfo ,
174defined in
175.I <sys/sem.h>
682edefb
MK
176if the
177.BR _GNU_SOURCE
178feature test macro is defined:
24ed820a
MK
179.nf
180.in +2n
181
182struct seminfo {
c13182ef 183 int semmap; /* # of entries in semaphore map;
24ed820a
MK
184 unused */
185 int semmni; /* Max. # of semaphore sets */
c13182ef 186 int semmns; /* Max. # of semaphores in all
24ed820a 187 semaphore sets */
c13182ef 188 int semmnu; /* System-wide max. # of undo
24ed820a
MK
189 structures; unused */
190 int semmsl; /* Max. # of semaphores in a set */
0bfa087b 191 int semopm; /* Max. # of operations for semop(2) */
c13182ef 192 int semume; /* Max. # of undo entries per
24ed820a
MK
193 process; unused */
194 int semusz; /* size of struct sem_undo */
195 int semvmx; /* Maximum semaphore value */
c13182ef 196 int semaem; /* Max. value that can be recorded for
24ed820a
MK
197 semaphore adjustment (SEM_UNDO) */
198};
199
200.in -2n
201.fi
c13182ef 202The
24ed820a
MK
203.IR semmsl ,
204.IR semmns ,
205.IR semopm ,
206and
207.I semmni
208settings can be changed via
209.IR /proc/sys/kernel/sem ;
210see
c13182ef 211.BR proc (5)
24ed820a
MK
212for details.
213.TP
214.BR SEM_INFO " (Linux specific)"
215Returns a
216.I seminfo
217structure containing the same information as for
218.BR IPC_INFO ,
c13182ef 219except that the following fields are returned with information
24ed820a
MK
220about system resources consumed by semaphores: the
221.I semusz
c13182ef 222field returns the number of semaphore sets that currently exist
24ed820a
MK
223on the system; and the
224.I semaem
c13182ef 225field returns the total number of semaphores in all semaphore sets
24ed820a
MK
226on the system.
227.TP
228.BR SEM_STAT " (Linux specific)"
c13182ef
MK
229Returns a
230.I semid_ds
24ed820a
MK
231structure as for
232.BR IPC_STAT .
233However, the
234.I semid
235argument is not a semaphore identifier, but instead an index into
236the kernel's internal array that maintains information about
237all semaphore sets on the system.
238.TP
fea681da
MK
239.B GETALL
240Return
241.B semval
70d0e5ef 242(i.e., the current value)
fea681da 243for all semaphores of the set into
70d0e5ef 244.IB arg.array .
fea681da
MK
245The argument
246.I semnum
247is ignored.
70d0e5ef 248The calling process must have read permission on the semaphore set.
fea681da
MK
249.TP
250.B GETNCNT
251The system call returns the value of
252.B semncnt
70d0e5ef
MK
253(i.e., the number of processes waiting for the value of
254this semaphore to increase) for the
fea681da
MK
255.IR semnum \-th
256semaphore of the set
75b94dc3 257(i.e., the number of processes waiting for an increase of
fea681da
MK
258.B semval
259for the
260.IR semnum \-th
261semaphore of the set).
70d0e5ef 262The calling process must have read permission on the semaphore set.
fea681da
MK
263.TP
264.B GETPID
265The system call returns the value of
266.B sempid
267for the
268.IR semnum \-th
269semaphore of the set
70d0e5ef 270(i.e. the PID of the process that executed the last
0bfa087b 271.BR semop (2)
fea681da
MK
272call for the
273.IR semnum \-th
274semaphore of the set).
70d0e5ef 275The calling process must have read permission on the semaphore set.
fea681da
MK
276.TP
277.B GETVAL
278The system call returns the value of
279.B semval
280for the
281.IR semnum \-th
282semaphore of the set.
70d0e5ef 283The calling process must have read permission on the semaphore set.
fea681da
MK
284.TP
285.B GETZCNT
286The system call returns the value of
287.B semzcnt
70d0e5ef
MK
288(i.e., the number of processes waiting for the value of
289this semaphore to become zero) for the
fea681da
MK
290.IR semnum \-th
291semaphore of the set
292(i.e. the number of processes waiting for
293.B semval
294of the
295.IR semnum \-th
296semaphore of the set to become 0).
70d0e5ef 297The calling process must have read permission on the semaphore set.
fea681da
MK
298.TP
299.B SETALL
300Set
301.B semval
302for all semaphores of the set using
70d0e5ef 303.IB arg.array ,
fea681da 304updating also the
70d0e5ef 305.I sem_ctime
fea681da 306member of the
70d0e5ef
MK
307.I semid_ds
308structure associated with the set.
309Undo entries (see
310.BR semop (2))
311are cleared for altered semaphores in all processes.
312If the changes to semaphore values would permit blocked
0bfa087b 313.BR semop (2)
70d0e5ef 314calls in other processes to proceed, then those processes are woken up.
fea681da
MK
315The argument
316.I semnum
317is ignored.
70d0e5ef
MK
318The calling process must have alter (write) permission on
319the semaphore set.
fea681da
MK
320.TP
321.B SETVAL
322Set the value of
323.B semval
324to
70d0e5ef 325.IB arg.val
fea681da
MK
326for the
327.IR semnum \-th
328semaphore of the set, updating also the
70d0e5ef 329.I sem_ctime
fea681da 330member of the
70d0e5ef
MK
331.I semid_ds
332structure associated with the set.
fea681da 333Undo entries are cleared for altered semaphores in all processes.
70d0e5ef 334If the changes to semaphore values would permit blocked
0bfa087b 335.BR semop (2)
70d0e5ef
MK
336calls in other processes to proceed, then those processes are woken up.
337The calling process must have alter permission on the semaphore set.
fea681da
MK
338.SH "RETURN VALUE"
339On failure
540036b2 340.BR semctl ()
70d0e5ef 341returns \-1
fea681da 342with
70d0e5ef 343.I errno
fea681da 344indicating the error.
24ed820a 345
18be658d 346Otherwise the system call returns a non-negative value depending on
fea681da
MK
347.I cmd
348as follows:
349.TP 11
350.B GETNCNT
351the value of
352.BR semncnt .
353.TP
354.B GETPID
355the value of
356.BR sempid .
357.TP
358.B GETVAL
359the value of
360.BR semval .
361.TP
362.B GETZCNT
363the value of
364.BR semzcnt .
24ed820a
MK
365.TP
366.B IPC_INFO
367the index of the highest used entry in the
368kernel's internal array recording information about all
369semaphore sets.
370(This information can be used with repeated
c13182ef 371.B SEM_STAT
24ed820a
MK
372operations to obtain information about all semaphore sets on the system.)
373.TP
374.B SEM_INFO
c13182ef 375As for
24ed820a
MK
376.BR IPC_INFO .
377.TP
378.B SEM_STAT
379the identifier of the semaphore set whose index was given in
380.IR semid .
fea681da
MK
381.LP
382All other
383.I cmd
384values return 0 on success.
385.SH ERRORS
386On failure,
70d0e5ef 387.I errno
fea681da
MK
388will be set to one of the following:
389.TP 11
390.B EACCES
391The argument
392.I cmd
393has one of the values
394.BR GETALL ,
395.BR GETPID ,
396.BR GETVAL ,
397.BR GETNCNT ,
398.BR GETZCNT ,
399.BR IPC_STAT ,
400.BR SEM_STAT ,
401.BR SETALL ,
402or
403.B SETVAL
404and the calling process does not have the required
405permissions on the semaphore set and does not have the
406.B CAP_IPC_OWNER
407capability.
408.TP
409.B EFAULT
410The address pointed to by
70d0e5ef 411.IB arg.buf
fea681da 412or
70d0e5ef 413.IB arg.array
fea681da
MK
414isn't accessible.
415.TP
416.B EIDRM
417The semaphore set was removed.
418.TP
419.B EINVAL
420Invalid value for
421.I cmd
422or
423.IR semid .
24ed820a
MK
424Or: for a
425.B SEM_STAT
c13182ef 426operation, the index value specified in
24ed820a
MK
427.I semid
428referred to an array slot that is currently unused.
fea681da
MK
429.TP
430.B EPERM
431The argument
432.I cmd
70d0e5ef 433has the value
fea681da
MK
434.B IPC_SET
435or
436.B IPC_RMID
70d0e5ef 437but the effective user ID of the calling process is not the creator
fea681da
MK
438(as found in
439.IR sem_perm.cuid )
440or the owner
441(as found in
442.IR sem_perm.uid )
443of the semaphore set,
444and the process does not have the
445.B CAP_SYS_ADMIN
446capability.
447.TP
448.B ERANGE
449The argument
450.I cmd
70d0e5ef 451has the value
fea681da
MK
452.B SETALL
453or
454.B SETVAL
455and the value to which
456.B semval
70d0e5ef
MK
457is to be set (for some semaphore of the set) is less than 0
458or greater than the implementation limit
fea681da 459.BR SEMVMX .
a1d5f77c
MK
460.SH "CONFORMING TO"
461SVr4, POSIX.1-2001.
462.\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
fea681da
MK
463.SH NOTES
464The
465.BR IPC_INFO ,
466.BR SEM_STAT
467and
468.B SEM_INFO
24ed820a 469operations are used by the
fea681da
MK
470.BR ipcs (8)
471program to provide information on allocated resources.
70d0e5ef 472In the future these may modified or moved to a /proc file system
fea681da
MK
473interface.
474.LP
475Various fields in a \fIstruct semid_ds\fP were shorts under Linux 2.2
677f4766
MK
476and have become longs under Linux 2.4.
477To take advantage of this,
fea681da 478a recompilation under glibc-2.1.91 or later should suffice.
682edefb
MK
479(The kernel distinguishes old and new calls by an
480.BR IPC_64
481flag in
fea681da
MK
482.IR cmd .)
483.PP
70d0e5ef
MK
484In some earlier versions of glibc, the
485.I semun
c84371c6 486union was defined in \fI<sys/sem.h>\fP, but POSIX.1-2001 requires
70d0e5ef
MK
487that the caller define this union.
488On versions of glibc where this union is \fInot\fP defined,
489the macro
490.B _SEM_SEMUN_UNDEFINED
c84371c6 491is defined in \fI<sys/sem.h>\fP.
70d0e5ef 492.PP
fea681da 493The following system limit on semaphore sets affects a
540036b2 494.BR semctl ()
fea681da
MK
495call:
496.TP 11
497.B SEMVMX
498Maximum value for
499.BR semval :
500implementation dependent (32767).
501.LP
502For greater portability it is best to always call
540036b2 503.BR semctl ()
fea681da
MK
504with four arguments.
505.LP
70d0e5ef 506Under Linux,
540036b2 507.BR semctl ()
fea681da
MK
508is not a system call, but is implemented via the system call
509.BR ipc (2).
fea681da
MK
510.SH "SEE ALSO"
511.BR ipc (2),
512.BR semget (2),
513.BR semop (2),
f675ea37 514.BR capabilities (7),
2c5e151c
MK
515.BR sem_overview (7),
516.BR svipc (7)