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