open.2: Make it clearer that an FD is an index into the process's FD table
[man-pages.git] / man2 / semctl.2
blob504cdf9da89a3de9e976b8ee7b1b15cd6e54a46e
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Tue Oct 22 17:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Modified 21 Dec 2001, aeb
31 .\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\"     Added notes on CAP_IPC_OWNER requirement
33 .\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\"     Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\"     Language and formatting clean-ups
37 .\"     Rewrote semun text
38 .\"     Added semid_ds and ipc_perm structure definitions
39 .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
40 .\" 2018-03-20, dbueso: Added SEM_STAT_ANY description.
41 .\"
42 .TH SEMCTL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
43 .SH NAME
44 semctl \- System V semaphore control operations
45 .SH SYNOPSIS
46 .nf
47 .B #include <sys/ipc.h>
48 .B #include <sys/sem.h>
49 .PP
50 .BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
51 .fi
52 .SH DESCRIPTION
53 .BR semctl ()
54 performs the control operation specified by
55 .I cmd
56 on the System\ V semaphore set identified by
57 .IR semid ,
58 or on the
59 .IR semnum -th
60 semaphore of that set.
61 (The semaphores in a set are numbered starting at 0.)
62 .PP
63 This function has three or four arguments, depending on
64 .IR cmd .
65 When there are four, the fourth has the type
66 .IR "union semun" .
67 The \fIcalling program\fP must define this union as follows:
68 .PP
69 .in +4n
70 .EX
71 union semun {
72     int              val;    /* Value for SETVAL */
73     struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
74     unsigned short  *array;  /* Array for GETALL, SETALL */
75     struct seminfo  *__buf;  /* Buffer for IPC_INFO
76                                 (Linux\-specific) */
78 .EE
79 .in
80 .PP
81 The
82 .I semid_ds
83 data structure is defined in \fI<sys/sem.h>\fP as follows:
84 .PP
85 .in +4n
86 .EX
87 struct semid_ds {
88     struct ipc_perm sem_perm;  /* Ownership and permissions */
89     time_t          sem_otime; /* Last semop time */
90     time_t          sem_ctime; /* Creation time/time of last
91                                   modification via semctl() */
92     unsigned long   sem_nsems; /* No. of semaphores in set */
94 .EE
95 .in
96 .PP
97 The fields of the
98 .I semid_ds
99 structure are as follows:
100 .TP 11
101 .I sem_perm
102 This is an
103 .I ipc_perm
104 structure (see below) that specifies the access permissions on the semaphore
105 set.
107 .I sem_otime
108 Time of last
109 .BR semop (2)
110 system call.
112 .I sem_ctime
113 Time of creation of semaphore set or time of last
114 .BR semctl ()
115 .BR IPCSET ,
116 .BR SETVAL ,
118 .BR SETALL
119 operation.
121 .I sem_nsems
122 Number of semaphores in the set.
123 Each semaphore of the set is referenced by a nonnegative integer
124 ranging from
125 .B 0
127 .IR sem_nsems\-1 .
130 .I ipc_perm
131 structure is defined as follows
132 (the highlighted fields are settable using
133 .BR IPC_SET ):
135 .in +4n
137 struct ipc_perm {
138     key_t          __key; /* Key supplied to semget(2) */
139     uid_t          \fBuid\fP;   /* Effective UID of owner */
140     gid_t          \fBgid\fP;   /* Effective GID of owner */
141     uid_t          cuid;  /* Effective UID of creator */
142     gid_t          cgid;  /* Effective GID of creator */
143     unsigned short \fBmode\fP;  /* Permissions */
144     unsigned short __seq; /* Sequence number */
149 The least significant 9 bits of the
150 .I mode
151 field of the
152 .I ipc_perm
153 structure define the access permissions for the shared memory segment.
154 The permission bits are as follows:
156 l l.
157 0400    Read by user
158 0200    Write by user
159 0040    Read by group
160 0020    Write by group
161 0004    Read by others
162 0002    Write by others
165 In effect, "write" means "alter" for a semaphore set.
166 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
168 Valid values for
169 .I cmd
170 are:
172 .B IPC_STAT
173 Copy information from the kernel data structure associated with
174 .I semid
175 into the
176 .I semid_ds
177 structure pointed to by
178 .IR arg.buf .
179 The argument
180 .I semnum
181 is ignored.
182 The calling process must have read permission on the semaphore set.
184 .B IPC_SET
185 Write the values of some members of the
186 .I semid_ds
187 structure pointed to by
188 .I arg.buf
189 to the kernel data structure associated with this semaphore set,
190 updating also its
191 .I sem_ctime
192 member.
194 The following members of the structure are updated:
195 .IR sem_perm.uid ,
196 .IR sem_perm.gid ,
197 and (the least significant 9 bits of)
198 .IR sem_perm.mode .
200 The effective UID of the calling process must match the owner
201 .RI ( sem_perm.uid )
202 or creator
203 .RI ( sem_perm.cuid )
204 of the semaphore set, or the caller must be privileged.
205 The argument
206 .I semnum
207 is ignored.
209 .B IPC_RMID
210 Immediately remove the semaphore set,
211 awakening all processes blocked in
212 .BR semop (2)
213 calls on the set (with an error return and
214 .I errno
215 set to
216 .BR EIDRM ).
217 The effective user ID of the calling process must
218 match the creator or owner of the semaphore set,
219 or the caller must be privileged.
220 The argument
221 .I semnum
222 is ignored.
224 .BR IPC_INFO " (Linux\-specific)"
225 Return information about system-wide semaphore limits and
226 parameters in the structure pointed to by
227 .IR arg.__buf .
228 This structure is of type
229 .IR seminfo ,
230 defined in
231 .I <sys/sem.h>
232 if the
233 .B _GNU_SOURCE
234 feature test macro is defined:
236 .in +4n
238 struct  seminfo {
239     int semmap;  /* Number of entries in semaphore
240                     map; unused within kernel */
241     int semmni;  /* Maximum number of semaphore sets */
242     int semmns;  /* Maximum number of semaphores in all
243                     semaphore sets */
244     int semmnu;  /* System\-wide maximum number of undo
245                     structures; unused within kernel */
246     int semmsl;  /* Maximum number of semaphores in a
247                     set */
248     int semopm;  /* Maximum number of operations for
249                     semop(2) */
250     int semume;  /* Maximum number of undo entries per
251                     process; unused within kernel */
252     int semusz;  /* Size of struct sem_undo */
253     int semvmx;  /* Maximum semaphore value */
254     int semaem;  /* Max. value that can be recorded for
255                     semaphore adjustment (SEM_UNDO) */
261 .IR semmsl ,
262 .IR semmns ,
263 .IR semopm ,
265 .I semmni
266 settings can be changed via
267 .IR /proc/sys/kernel/sem ;
269 .BR proc (5)
270 for details.
272 .BR SEM_INFO " (Linux-specific)"
273 Return a
274 .I seminfo
275 structure containing the same information as for
276 .BR IPC_INFO ,
277 except that the following fields are returned with information
278 about system resources consumed by semaphores: the
279 .I semusz
280 field returns the number of semaphore sets that currently exist
281 on the system; and the
282 .I semaem
283 field returns the total number of semaphores in all semaphore sets
284 on the system.
286 .BR SEM_STAT " (Linux-specific)"
287 Return a
288 .I semid_ds
289 structure as for
290 .BR IPC_STAT .
291 However, the
292 .I semid
293 argument is not a semaphore identifier, but instead an index into
294 the kernel's internal array that maintains information about
295 all semaphore sets on the system.
297 .BR SEM_STAT_ANY " (Linux-specific, since Linux 4.17)"
298 Return a
299 .I semid_ds
300 structure as for
301 .BR SEM_STAT .
302 However,
303 .I sem_perm.mode
304 is not checked for read access for
305 .IR semid
306 meaning that any user can employ this operation (just as any user may read
307 .IR /proc/sysvipc/sem
308 to obtain the same information).
310 .B GETALL
311 Return
312 .B semval
313 (i.e., the current value)
314 for all semaphores of the set into
315 .IR arg.array .
316 The argument
317 .I semnum
318 is ignored.
319 The calling process must have read permission on the semaphore set.
321 .B GETNCNT
322 Return the
323 .B semncnt
324 value for the
325 .IR semnum \-th
326 semaphore of the set
327 (i.e., the number of processes waiting for the semaphore's value to increase).
328 The calling process must have read permission on the semaphore set.
330 .B GETPID
331 Return the
332 .B sempid
333 value for the
334 .IR semnum \-th
335 semaphore of the set.
336 This is the PID of the process that last performed an operation on
337 that semaphore (but see NOTES).
338 The calling process must have read permission on the semaphore set.
340 .B GETVAL
341 Return
342 .B semval
343 (i.e., the semaphore value) for the
344 .IR semnum \-th
345 semaphore of the set.
346 The calling process must have read permission on the semaphore set.
348 .B GETZCNT
349 Return the
350 .B semzcnt
351 value for the
352 .IR semnum \-th
353 semaphore of the set
354 (i.e., the number of processes waiting for the semaphore value to become 0).
355 The calling process must have read permission on the semaphore set.
357 .B SETALL
358 Set the
359 .B semval
360 values for all semaphores of the set using
361 .IR arg.array ,
362 updating also the
363 .I sem_ctime
364 member of the
365 .I semid_ds
366 structure associated with the set.
367 Undo entries (see
368 .BR semop (2))
369 are cleared for altered semaphores in all processes.
370 If the changes to semaphore values would permit blocked
371 .BR semop (2)
372 calls in other processes to proceed, then those processes are woken up.
373 The argument
374 .I semnum
375 is ignored.
376 The calling process must have alter (write) permission on
377 the semaphore set.
379 .B SETVAL
380 Set the semaphore value
381 .BR ( semval )
383 .I arg.val
384 for the
385 .IR semnum \-th
386 semaphore of the set, updating also the
387 .I sem_ctime
388 member of the
389 .I semid_ds
390 structure associated with the set.
391 Undo entries are cleared for altered semaphores in all processes.
392 If the changes to semaphore values would permit blocked
393 .BR semop (2)
394 calls in other processes to proceed, then those processes are woken up.
395 The calling process must have alter permission on the semaphore set.
396 .SH RETURN VALUE
397 On success,
398 .BR semctl ()
399 returns a nonnegative value depending on
400 .I cmd
401 as follows:
403 .B GETNCNT
404 the value of
405 .BR semncnt .
407 .B GETPID
408 the value of
409 .BR sempid .
411 .B GETVAL
412 the value of
413 .BR semval .
415 .B GETZCNT
416 the value of
417 .BR semzcnt .
419 .B IPC_INFO
420 the index of the highest used entry in the
421 kernel's internal array recording information about all
422 semaphore sets.
423 (This information can be used with repeated
424 .B SEM_STAT
426 .B SEM_STAT_ANY
427 operations to obtain information about all semaphore sets on the system.)
429 .B SEM_INFO
430 as for
431 .BR IPC_INFO .
433 .B SEM_STAT
434 the identifier of the semaphore set whose index was given in
435 .IR semid .
437 .B SEM_STAT_ANY
438 as for
439 .BR SEM_STAT .
441 All other
442 .I cmd
443 values return 0 on success.
445 On failure,
446 .BR semctl ()
447 returns \-1 and sets
448 .I errno
449 to indicate the error.
450 .SH ERRORS
452 .B EACCES
453 The argument
454 .I cmd
455 has one of the values
456 .BR GETALL ,
457 .BR GETPID ,
458 .BR GETVAL ,
459 .BR GETNCNT ,
460 .BR GETZCNT ,
461 .BR IPC_STAT ,
462 .BR SEM_STAT ,
463 .BR SEM_STAT_ANY ,
464 .BR SETALL ,
466 .B SETVAL
467 and the calling process does not have the required
468 permissions on the semaphore set and does not have the
469 .B CAP_IPC_OWNER
470 capability in the user namespace that governs its IPC namespace.
472 .B EFAULT
473 The address pointed to by
474 .I arg.buf
476 .I arg.array
477 isn't accessible.
479 .B EIDRM
480 The semaphore set was removed.
482 .B EINVAL
483 Invalid value for
484 .I cmd
486 .IR semid .
487 Or: for a
488 .B SEM_STAT
489 operation, the index value specified in
490 .I semid
491 referred to an array slot that is currently unused.
493 .B EPERM
494 The argument
495 .I cmd
496 has the value
497 .B IPC_SET
499 .B IPC_RMID
500 but the effective user ID of the calling process is not the creator
501 (as found in
502 .IR sem_perm.cuid )
503 or the owner
504 (as found in
505 .IR sem_perm.uid )
506 of the semaphore set,
507 and the process does not have the
508 .B CAP_SYS_ADMIN
509 capability.
511 .B ERANGE
512 The argument
513 .I cmd
514 has the value
515 .B SETALL
517 .B SETVAL
518 and the value to which
519 .B semval
520 is to be set (for some semaphore of the set) is less than 0
521 or greater than the implementation limit
522 .BR SEMVMX .
523 .SH CONFORMING TO
524 POSIX.1-2001, POSIX.1-2008, SVr4.
525 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
527 POSIX.1 specifies the
528 .\" POSIX.1-2001, POSIX.1-2008
529 .I sem_nsems
530 field of the
531 .I semid_ds
532 structure as having the type
533 .IR "unsigned\ short" ,
534 and the field is so defined on most other systems.
535 It was also so defined on Linux 2.2 and earlier,
536 but, since Linux 2.4, the field has the type
537 .IR "unsigned\ long" .
538 .SH NOTES
539 The inclusion of
540 .I <sys/ipc.h>
541 isn't required on Linux or by any version of POSIX.
542 However,
543 some old implementations required the inclusion of this header file,
544 and the SVID also documented its inclusion.
545 Applications intended to be portable to such old systems may need
546 to include this header file.
547 .\" Like Linux, the FreeBSD man pages still document
548 .\" the inclusion of these header files.
551 .BR IPC_INFO ,
552 .BR SEM_STAT ,
554 .B SEM_INFO
555 operations are used by the
556 .BR ipcs (1)
557 program to provide information on allocated resources.
558 In the future these may modified or moved to a
559 .I /proc
560 filesystem interface.
562 Various fields in a \fIstruct semid_ds\fP were typed as
563 .I short
564 under Linux 2.2
565 and have become
566 .I long
567 under Linux 2.4.
568 To take advantage of this,
569 a recompilation under glibc-2.1.91 or later should suffice.
570 (The kernel distinguishes old and new calls by an
571 .B IPC_64
572 flag in
573 .IR cmd .)
575 In some earlier versions of glibc, the
576 .I semun
577 union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
578 .\" POSIX.1-2001, POSIX.1-2008
579 that the caller define this union.
580 On versions of glibc where this union is \fInot\fP defined,
581 the macro
582 .B _SEM_SEMUN_UNDEFINED
583 is defined in \fI<sys/sem.h>\fP.
585 The following system limit on semaphore sets affects a
586 .BR semctl ()
587 call:
589 .B SEMVMX
590 Maximum value for
591 .BR semval :
592 implementation dependent (32767).
594 For greater portability, it is best to always call
595 .BR semctl ()
596 with four arguments.
598 .SS The sempid value
599 POSIX.1 defines
600 .I sempid
601 as the "process ID of [the] last operation" on a semaphore,
602 and explicitly notes that this value is set by a successful
603 .BR semop (2)
604 call, with the implication that no other interface affects the
605 .I sempid
606 value.
608 While some implementations conform to the behavior specified in POSIX.1,
609 others do not.
610 (The fault here probably lies with POSIX.1 inasmuch as it likely failed
611 to capture the full range of existing implementation behaviors.)
612 Various other implementations
613 .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
614 also update
615 .I sempid
616 for the other operations that update the value of a semaphore: the
617 .B SETVAL
619 .B SETALL
620 operations, as well as the semaphore adjustments performed
621 on process termination as a consequence of the use of the
622 .B SEM_UNDO
623 flag (see
624 .BR semop (2)).
626 Linux also updates
627 .I sempid
629 .BR SETVAL
630 operations and semaphore adjustments.
631 However, somewhat inconsistently, up to and including Linux 4.5,
632 the kernel did not update
633 .I sempid
635 .BR SETALL
636 operations.
637 This was rectified
638 .\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f
639 in Linux 4.6.
640 .SH EXAMPLES
642 .BR shmop (2).
643 .SH SEE ALSO
644 .BR ipc (2),
645 .BR semget (2),
646 .BR semop (2),
647 .BR capabilities (7),
648 .BR sem_overview (7),
649 .BR sysvipc (7)