1SEMCTL(2)                  Linux Programmer's Manual                 SEMCTL(2)
2
3
4

NAME

6       semctl - System V semaphore control operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/ipc.h>
11       #include <sys/sem.h>
12
13       int semctl(int semid, int semnum, int cmd, ...);
14

DESCRIPTION

16       semctl()  performs  the  control operation specified by cmd on the Sys‐
17       tem V semaphore set identified by semid, or on the semnum-th  semaphore
18       of that set.  (The semaphores in a set are numbered starting at 0.)
19
20       This  function  has  three  or  four arguments, depending on cmd.  When
21       there are four, the fourth has the type union semun.  The calling  pro‐
22       gram must define this union as follows:
23
24           union semun {
25               int              val;    /* Value for SETVAL */
26               struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
27               unsigned short  *array;  /* Array for GETALL, SETALL */
28               struct seminfo  *__buf;  /* Buffer for IPC_INFO
29                                           (Linux-specific) */
30           };
31
32       The semid_ds data structure is defined in <sys/sem.h> as follows:
33
34           struct semid_ds {
35               struct ipc_perm sem_perm;  /* Ownership and permissions */
36               time_t          sem_otime; /* Last semop time */
37               time_t          sem_ctime; /* Creation time/time of last
38                                             modification via semctl() */
39               unsigned long   sem_nsems; /* No. of semaphores in set */
40           };
41
42       The fields of the semid_ds structure are as follows:
43
44       sem_perm   This is an ipc_perm structure (see below) that specifies the
45                  access permissions on the semaphore set.
46
47       sem_otime  Time of last semop(2) system call.
48
49       sem_ctime  Time of creation of semaphore set or time of  last  semctl()
50                  IPCSET, SETVAL, or SETALL operation.
51
52       sem_nsems  Number  of semaphores in the set.  Each semaphore of the set
53                  is referenced by a nonnegative integer  ranging  from  0  to
54                  sem_nsems-1.
55
56       The  ipc_perm  structure  is defined as follows (the highlighted fields
57       are settable using IPC_SET):
58
59           struct ipc_perm {
60               key_t          __key; /* Key supplied to semget(2) */
61               uid_t          uid;   /* Effective UID of owner */
62               gid_t          gid;   /* Effective GID of owner */
63               uid_t          cuid;  /* Effective UID of creator */
64               gid_t          cgid;  /* Effective GID of creator */
65               unsigned short mode;  /* Permissions */
66               unsigned short __seq; /* Sequence number */
67           };
68
69       The least significant 9 bits of the mode field of the  ipc_perm  struc‐
70       ture  define the access permissions for the shared memory segment.  The
71       permission bits are as follows:
72
73       0400   Read by user
74       0200   Write by user
75       0040   Read by group
76       0020   Write by group
77       0004   Read by others
78       0002   Write by others
79
80       In effect, "write" means "alter" for a semaphore set.  Bits 0100, 0010,
81       and 0001 (the execute bits) are unused by the system.
82
83       Valid values for cmd are:
84
85       IPC_STAT
86              Copy  information from the kernel data structure associated with
87              semid into the semid_ds structure pointed to  by  arg.buf.   The
88              argument  semnum is ignored.  The calling process must have read
89              permission on the semaphore set.
90
91       IPC_SET
92              Write the values of  some  members  of  the  semid_ds  structure
93              pointed  to  by  arg.buf to the kernel data structure associated
94              with this semaphore set, updating also its sem_ctime member.
95
96              The  following   members   of   the   structure   are   updated:
97              sem_perm.uid,  sem_perm.gid,  and  (the least significant 9 bits
98              of) sem_perm.mode.
99
100              The effective UID of the calling process must  match  the  owner
101              (sem_perm.uid)  or creator (sem_perm.cuid) of the semaphore set,
102              or the caller must be privileged.  The argument  semnum  is  ig‐
103              nored.
104
105       IPC_RMID
106              Immediately  remove  the  semaphore set, awakening all processes
107              blocked in semop(2) calls on the set (with an error  return  and
108              errno  set  to  EIDRM).   The  effective  user ID of the calling
109              process must match the creator or owner of the semaphore set, or
110              the caller must be privileged.  The argument semnum is ignored.
111
112       IPC_INFO (Linux-specific)
113              Return information about system-wide semaphore limits and param‐
114              eters in the structure pointed to by arg.__buf.  This  structure
115              is  of  type  seminfo, defined in <sys/sem.h> if the _GNU_SOURCE
116              feature test macro is defined:
117
118                  struct  seminfo {
119                      int semmap;  /* Number of entries in semaphore
120                                      map; unused within kernel */
121                      int semmni;  /* Maximum number of semaphore sets */
122                      int semmns;  /* Maximum number of semaphores in all
123                                      semaphore sets */
124                      int semmnu;  /* System-wide maximum number of undo
125                                      structures; unused within kernel */
126                      int semmsl;  /* Maximum number of semaphores in a
127                                      set */
128                      int semopm;  /* Maximum number of operations for
129                                      semop(2) */
130                      int semume;  /* Maximum number of undo entries per
131                                      process; unused within kernel */
132                      int semusz;  /* Size of struct sem_undo */
133                      int semvmx;  /* Maximum semaphore value */
134                      int semaem;  /* Max. value that can be recorded for
135                                      semaphore adjustment (SEM_UNDO) */
136                  };
137
138              The semmsl, semmns, semopm, and semmni settings can  be  changed
139              via /proc/sys/kernel/sem; see proc(5) for details.
140
141       SEM_INFO (Linux-specific)
142              Return  a  seminfo  structure containing the same information as
143              for IPC_INFO, except that the following fields are returned with
144              information  about  system resources consumed by semaphores: the
145              semusz field returns the number of semaphore sets that currently
146              exist on the system; and the semaem field returns the total num‐
147              ber of semaphores in all semaphore sets on the system.
148
149       SEM_STAT (Linux-specific)
150              Return a semid_ds structure as for IPC_STAT.  However, the semid
151              argument  is  not  a  semaphore identifier, but instead an index
152              into the kernel's  internal  array  that  maintains  information
153              about all semaphore sets on the system.
154
155       SEM_STAT_ANY (Linux-specific, since Linux 4.17)
156              Return  a  seminfo  structure containing the same information as
157              for SEM_STAT.  However, sem_perm.mode is not  checked  for  read
158              access for semid meaning that any user can employ this operation
159              (just as any user may read /proc/sysvipc/sem to obtain the  same
160              information).
161
162       GETALL Return  semval  (i.e.,  the current value) for all semaphores of
163              the set into arg.array.  The argument semnum  is  ignored.   The
164              calling process must have read permission on the semaphore set.
165
166       GETNCNT
167              Return  the semncnt value for the semnum-th semaphore of the set
168              (i.e., the number of processes waiting for the semaphore's value
169              to  increase).  The calling process must have read permission on
170              the semaphore set.
171
172       GETPID Return the sempid value for the semnum-th semaphore of the  set.
173              This  is the PID of the process that last performed an operation
174              on that semaphore (but see NOTES).   The  calling  process  must
175              have read permission on the semaphore set.
176
177       GETVAL Return semval (i.e., the semaphore value) for the semnum-th sem‐
178              aphore of the set.  The calling process must have  read  permis‐
179              sion on the semaphore set.
180
181       GETZCNT
182              Return  the semzcnt value for the semnum-th semaphore of the set
183              (i.e., the number of processes waiting for the  semaphore  value
184              to  become 0).  The calling process must have read permission on
185              the semaphore set.
186
187       SETALL Set the semval values  for  all  semaphores  of  the  set  using
188              arg.array,  updating  also  the sem_ctime member of the semid_ds
189              structure associated with the set.  Undo entries (see  semop(2))
190              are  cleared  for  altered  semaphores in all processes.  If the
191              changes to semaphore values would permit blocked semop(2)  calls
192              in  other  processes  to proceed, then those processes are woken
193              up.  The argument semnum is ignored.  The calling  process  must
194              have alter (write) permission on the semaphore set.
195
196       SETVAL Set  the  semaphore  value (semval) to arg.val for the semnum-th
197              semaphore of the set, updating also the sem_ctime member of  the
198              semid_ds  structure  associated  with the set.  Undo entries are
199              cleared for altered semaphores in all processes.  If the changes
200              to semaphore values would permit blocked semop(2) calls in other
201              processes to proceed, then those processes are  woken  up.   The
202              calling process must have alter permission on the semaphore set.
203

RETURN VALUE

205       On failure, semctl() returns -1 with errno indicating the error.
206
207       Otherwise, the system call returns a nonnegative value depending on cmd
208       as follows:
209
210       GETNCNT
211              the value of semncnt.
212
213       GETPID the value of sempid.
214
215       GETVAL the value of semval.
216
217       GETZCNT
218              the value of semzcnt.
219
220       IPC_INFO
221              the index of the highest used entry in the kernel's internal ar‐
222              ray  recording  information about all semaphore sets.  (This in‐
223              formation can be used with repeated SEM_STAT or SEM_STAT_ANY op‐
224              erations  to  obtain information about all semaphore sets on the
225              system.)
226
227       SEM_INFO
228              as for IPC_INFO.
229
230       SEM_STAT
231              the identifier of the semaphore set whose  index  was  given  in
232              semid.
233
234       SEM_STAT_ANY
235              as for SEM_STAT.
236
237       All other cmd values return 0 on success.
238

ERRORS

240       On failure, errno will be set to one of the following:
241
242       EACCES The  argument  cmd has one of the values GETALL, GETPID, GETVAL,
243              GETNCNT, GETZCNT, IPC_STAT, SEM_STAT, SEM_STAT_ANY,  SETALL,  or
244              SETVAL  and  the calling process does not have the required per‐
245              missions  on  the  semaphore  set  and   does   not   have   the
246              CAP_IPC_OWNER  capability in the user namespace that governs its
247              IPC namespace.
248
249       EFAULT The address pointed to by arg.buf or arg.array isn't accessible.
250
251       EIDRM  The semaphore set was removed.
252
253       EINVAL Invalid value for cmd or semid.  Or: for a  SEM_STAT  operation,
254              the  index  value  specified  in semid referred to an array slot
255              that is currently unused.
256
257       EPERM  The argument cmd has the value IPC_SET or IPC_RMID but  the  ef‐
258              fective  user  ID  of the calling process is not the creator (as
259              found in sem_perm.cuid) or the owner (as found in  sem_perm.uid)
260              of  the  semaphore  set,  and  the  process  does  not  have the
261              CAP_SYS_ADMIN capability.
262
263       ERANGE The argument cmd has the value SETALL or SETVAL and the value to
264              which  semval  is  to  be set (for some semaphore of the set) is
265              less than 0 or greater than the implementation limit SEMVMX.
266

CONFORMING TO

268       POSIX.1-2001, POSIX.1-2008, SVr4.
269
270       POSIX.1 specifies the sem_nsems field of the semid_ds structure as hav‐
271       ing  the type unsigned short, and the field is so defined on most other
272       systems.  It was also so defined on Linux 2.2 and earlier,  but,  since
273       Linux 2.4, the field has the type unsigned long.
274

NOTES

276       The  inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
277       or by any version of POSIX.  However, some old implementations required
278       the inclusion of these header files, and the SVID also documented their
279       inclusion.  Applications intended to be portable to  such  old  systems
280       may need to include these header files.
281
282       The IPC_INFO, SEM_STAT, and SEM_INFO operations are used by the ipcs(1)
283       program to provide information on allocated resources.  In  the  future
284       these may modified or moved to a /proc filesystem interface.
285
286       Various fields in a struct semid_ds were typed as short under Linux 2.2
287       and have become long under Linux 2.4.  To take advantage of this, a re‐
288       compilation  under  glibc-2.1.91  or later should suffice.  (The kernel
289       distinguishes old and new calls by an IPC_64 flag in cmd.)
290
291       In some earlier versions of glibc,  the  semun  union  was  defined  in
292       <sys/sem.h>,  but  POSIX.1  requires that the caller define this union.
293       On versions of glibc  where  this  union  is  not  defined,  the  macro
294       _SEM_SEMUN_UNDEFINED is defined in <sys/sem.h>.
295
296       The following system limit on semaphore sets affects a semctl() call:
297
298       SEMVMX Maximum value for semval: implementation dependent (32767).
299
300       For  greater  portability, it is best to always call semctl() with four
301       arguments.
302
303   The sempid value
304       POSIX.1 defines sempid as the "process ID of [the] last operation" on a
305       semaphore,  and explicitly notes that this value is set by a successful
306       semop(2) call, with the implication that no other interface affects the
307       sempid value.
308
309       While  some  implementations  conform  to  the  behavior  specified  in
310       POSIX.1, others do not.  (The fault here  probably  lies  with  POSIX.1
311       inasmuch  as it likely failed to capture the full range of existing im‐
312       plementation behaviors.)  Various  other  implementations  also  update
313       sempid  for  the other operations that update the value of a semaphore:
314       the SETVAL and SETALL operations, as well as the semaphore  adjustments
315       performed  on  process  termination  as a consequence of the use of the
316       SEM_UNDO flag (see semop(2)).
317
318       Linux also updates sempid for SETVAL operations and  semaphore  adjust‐
319       ments.   However,  somewhat  inconsistently,  up to and including Linux
320       4.5, the kernel did not update sempid for SETALL operations.  This  was
321       rectified in Linux 4.6.
322

EXAMPLES

324       See shmop(2).
325

SEE ALSO

327       ipc(2),    semget(2),   semop(2),   capabilities(7),   sem_overview(7),
328       sysvipc(7)
329

COLOPHON

331       This page is part of release 5.10 of the Linux  man-pages  project.   A
332       description  of  the project, information about reporting bugs, and the
333       latest    version    of    this    page,    can     be     found     at
334       https://www.kernel.org/doc/man-pages/.
335
336
337
338Linux                             2020-12-21                         SEMCTL(2)
Impressum