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; /* Last change time */
38               unsigned long   sem_nsems; /* No. of semaphores in set */
39           };
40
41       The  ipc_perm  structure  is defined as follows (the highlighted fields
42       are settable using IPC_SET):
43
44           struct ipc_perm {
45               key_t          __key; /* Key supplied to semget(2) */
46               uid_t          uid;   /* Effective UID of owner */
47               gid_t          gid;   /* Effective GID of owner */
48               uid_t          cuid;  /* Effective UID of creator */
49               gid_t          cgid;  /* Effective GID of creator */
50               unsigned short mode;  /* Permissions */
51               unsigned short __seq; /* Sequence number */
52           };
53
54       Valid values for cmd are:
55
56       IPC_STAT  Copy information from the kernel  data  structure  associated
57                 with semid into the semid_ds structure pointed to by arg.buf.
58                 The argument semnum is ignored.   The  calling  process  must
59                 have read permission on the semaphore set.
60
61       IPC_SET   Write  the  values  of some members of the semid_ds structure
62                 pointed to by arg.buf to the kernel data structure associated
63                 with  this semaphore set, updating also its sem_ctime member.
64                 The  following  members  of  the   structure   are   updated:
65                 sem_perm.uid, sem_perm.gid, and (the least significant 9 bits
66                 of) sem_perm.mode.  The effective UID of the calling  process
67                 must    match    the    owner   (sem_perm.uid)   or   creator
68                 (sem_perm.cuid) of the semaphore set, or the caller  must  be
69                 privileged.  The argument semnum is ignored.
70
71       IPC_RMID  Immediately remove the semaphore set, awakening all processes
72                 blocked in semop(2) calls on the set (with  an  error  return
73                 and  errno set to EIDRM).  The effective user ID of the call‐
74                 ing process must match the creator or owner of the  semaphore
75                 set,  or  the caller must be privileged.  The argument semnum
76                 is ignored.
77
78       IPC_INFO (Linux-specific)
79                 Return information about  system-wide  semaphore  limits  and
80                 parameters  in  the  structure pointed to by arg.__buf.  This
81                 structure is of type seminfo, defined in <sys/sem.h>  if  the
82                 _GNU_SOURCE feature test macro is defined:
83
84                     struct  seminfo {
85                         int semmap;  /* Number of entries in semaphore
86                                         map; unused within kernel */
87                         int semmni;  /* Maximum number of semaphore sets */
88                         int semmns;  /* Maximum number of semaphores in all
89                                         semaphore sets */
90                         int semmnu;  /* System-wide maximum number of undo
91                                         structures; unused within kernel */
92                         int semmsl;  /* Maximum number of semaphores in a
93                                         set */
94                         int semopm;  /* Maximum number of operations for
95                                         semop(2) */
96                         int semume;  /* Maximum number of undo entries per
97                                         process; unused within kernel */
98                         int semusz;  /* Size of struct sem_undo */
99                         int semvmx;  /* Maximum semaphore value */
100                         int semaem;  /* Max. value that can be recorded for
101                                         semaphore adjustment (SEM_UNDO) */
102                     };
103
104                 The  semmsl,  semmns,  semopm,  and  semmni  settings  can be
105                 changed via /proc/sys/kernel/sem; see proc(5) for details.
106
107       SEM_INFO (Linux-specific)
108                 Return a seminfo structure containing the same information as
109                 for  IPC_INFO,  except that the following fields are returned
110                 with information about system  resources  consumed  by  sema‐
111                 phores: the semusz field returns the number of semaphore sets
112                 that currently exist on the  system;  and  the  semaem  field
113                 returns  the total number of semaphores in all semaphore sets
114                 on the system.
115
116       SEM_STAT (Linux-specific)
117                 Return a semid_ds structure as for  IPC_STAT.   However,  the
118                 semid  argument is not a semaphore identifier, but instead an
119                 index into the kernel's internal array that maintains  infor‐
120                 mation about all semaphore sets on the system.
121
122       GETALL    Return semval (i.e., the current value) for all semaphores of
123                 the set into arg.array.  The argument semnum is ignored.  The
124                 calling  process  must  have read permission on the semaphore
125                 set.
126
127       GETNCNT   Return the value of semncnt for the  semnum-th  semaphore  of
128                 the  set  (i.e.,  the  number  of  processes  waiting  for an
129                 increase of semval for the semnum-th semaphore of  the  set).
130                 The  calling  process  must have read permission on the sema‐
131                 phore set.
132
133       GETPID    Return the value of sempid for the semnum-th semaphore of the
134                 set.   This  is the PID of the process that last performed an
135                 operation on that semaphore (but  see  NOTES).   The  calling
136                 process must have read permission on the semaphore set.
137
138       GETVAL    Return the value of semval for the semnum-th semaphore of the
139                 set.  The calling process must have read  permission  on  the
140                 semaphore set.
141
142       GETZCNT   Return  the  value  of semzcnt for the semnum-th semaphore of
143                 the set (i.e., the number of processes waiting for semval  of
144                 the semnum-th semaphore of the set to become 0).  The calling
145                 process must have read permission on the semaphore set.
146
147       SETALL    Set semval for all semaphores of  the  set  using  arg.array,
148                 updating  also the sem_ctime member of the semid_ds structure
149                 associated with the set.  Undo  entries  (see  semop(2))  are
150                 cleared  for  altered  semaphores  in  all processes.  If the
151                 changes to semaphore values  would  permit  blocked  semop(2)
152                 calls in other processes to proceed, then those processes are
153                 woken up.  The  argument  semnum  is  ignored.   The  calling
154                 process  must  have alter (write) permission on the semaphore
155                 set.
156
157       SETVAL    Set the value of semval to arg.val for  the  semnum-th  sema‐
158                 phore  of  the set, updating also the sem_ctime member of the
159                 semid_ds structure associated with the set.  Undo entries are
160                 cleared  for  altered  semaphores  in  all processes.  If the
161                 changes to semaphore values  would  permit  blocked  semop(2)
162                 calls in other processes to proceed, then those processes are
163                 woken up.  The calling process must have alter permission  on
164                 the semaphore set.
165

RETURN VALUE

167       On failure, semctl() returns -1 with errno indicating the error.
168
169       Otherwise, the system call returns a nonnegative value depending on cmd
170       as follows:
171
172       GETNCNT   the value of semncnt.
173
174       GETPID    the value of sempid.
175
176       GETVAL    the value of semval.
177
178       GETZCNT   the value of semzcnt.
179
180       IPC_INFO  the index of the highest used entry in the kernel's  internal
181                 array  recording information about all semaphore sets.  (This
182                 information can be used with repeated SEM_STAT operations  to
183                 obtain information about all semaphore sets on the system.)
184
185       SEM_INFO  as for IPC_INFO.
186
187       SEM_STAT  the  identifier of the semaphore set whose index was given in
188                 semid.
189
190       All other cmd values return 0 on success.
191

ERRORS

193       On failure, errno will be set to one of the following:
194
195       EACCES The argument cmd has one of the values GETALL,  GETPID,  GETVAL,
196              GETNCNT,  GETZCNT, IPC_STAT, SEM_STAT, SETALL, or SETVAL and the
197              calling process does not have the required  permissions  on  the
198              semaphore  set and does not have the CAP_IPC_OWNER capability in
199              the user namespace that governs its IPC namespace.
200
201       EFAULT The address pointed to by arg.buf or arg.array isn't accessible.
202
203       EIDRM  The semaphore set was removed.
204
205       EINVAL Invalid value for cmd or semid.  Or: for a  SEM_STAT  operation,
206              the  index  value  specified  in semid referred to an array slot
207              that is currently unused.
208
209       EPERM  The argument cmd has the  value  IPC_SET  or  IPC_RMID  but  the
210              effective  user ID of the calling process is not the creator (as
211              found in sem_perm.cuid) or the owner (as found in  sem_perm.uid)
212              of  the  semaphore  set,  and  the  process  does  not  have the
213              CAP_SYS_ADMIN capability.
214
215       ERANGE The argument cmd has the value SETALL or SETVAL and the value to
216              which  semval  is  to  be set (for some semaphore of the set) is
217              less than 0 or greater than the implementation limit SEMVMX.
218

CONFORMING TO

220       POSIX.1-2001, POSIX.1-2008, SVr4.
221
222       POSIX.1 specifies the sem_nsems field of the semid_ds structure as hav‐
223       ing  the type unsigned short, and the field is so defined on most other
224       systems.  It was also so defined on Linux 2.2 and earlier,  but,  since
225       Linux 2.4, the field has the type unsigned long.
226

NOTES

228       The  inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
229       or by any version of POSIX.  However, some old implementations required
230       the inclusion of these header files, and the SVID also documented their
231       inclusion.  Applications intended to be portable to  such  old  systems
232       may need to include these header files.
233
234       The  IPC_INFO, SEM_STAT and SEM_INFO operations are used by the ipcs(1)
235       program to provide information on allocated resources.  In  the  future
236       these may modified or moved to a /proc filesystem interface.
237
238       Various fields in a struct semid_ds were typed as short under Linux 2.2
239       and have become long under Linux 2.4.  To take  advantage  of  this,  a
240       recompilation  under glibc-2.1.91 or later should suffice.  (The kernel
241       distinguishes old and new calls by an IPC_64 flag in cmd.)
242
243       In some earlier versions of glibc,  the  semun  union  was  defined  in
244       <sys/sem.h>,  but  POSIX.1  requires that the caller define this union.
245       On versions of glibc  where  this  union  is  not  defined,  the  macro
246       _SEM_SEMUN_UNDEFINED is defined in <sys/sem.h>.
247
248       The following system limit on semaphore sets affects a semctl() call:
249
250       SEMVMX Maximum value for semval: implementation dependent (32767).
251
252       For  greater  portability, it is best to always call semctl() with four
253       arguments.
254
255   The sempid value
256       POSIX.1 defines sempid as the "process ID of [the] last operation" on a
257       semaphore,  and explicitly notes that this value is set by a successful
258       semop(2) call, with the implication that no other interface affects the
259       sempid value.
260
261       While  some  implementations  conform  to  the  behavior  specified  in
262       POSIX.1, others do not.  (The fault here  probably  lies  with  POSIX.1
263       inasmuch  as  it  likely  failed  to capture the full range of existing
264       implementation behaviors.)  Various other implementations  also  update
265       sempid  for  the other operations that update the value of a semaphore:
266       the SETVAL and SETALL operations, as well as the semaphore  adjustments
267       performed  on  process  termination  as a consequence of the use of the
268       SEM_UNDO flag (see semop(2)).
269
270       Linux also updates sempid for SETVAL operations and  semaphore  adjust‐
271       ments.   However,  somewhat  inconsistently,  up  to and including 4.5,
272       Linux did not update sempid for SETALL operations.  This was  rectified
273       in Linux 4.6.
274

SEE ALSO

276       ipc(2), semget(2), semop(2), capabilities(7), sem_overview(7), svipc(7)
277

COLOPHON

279       This  page  is  part of release 4.15 of the Linux man-pages project.  A
280       description of the project, information about reporting bugs,  and  the
281       latest     version     of     this    page,    can    be    found    at
282       https://www.kernel.org/doc/man-pages/.
283
284
285
286Linux                             2017-09-15                         SEMCTL(2)
Impressum