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       SEM_STAT_ANY (Linux-specific, since Linux 4.17)
123                 Return a seminfo structure containing the same information as
124                 for SEM_STAT.  However, sem_perm.mode is not checked for read
125                 access for semid meaning that any user can employ this opera‐
126                 tion (just as any user may read /proc/sysvipc/sem  to  obtain
127                 the same information).
128
129       GETALL    Return semval (i.e., the current value) for all semaphores of
130                 the set into arg.array.  The argument semnum is ignored.  The
131                 calling  process  must  have read permission on the semaphore
132                 set.
133
134       GETNCNT   Return the value of semncnt for the  semnum-th  semaphore  of
135                 the  set  (i.e.,  the  number  of  processes  waiting  for an
136                 increase of semval for the semnum-th semaphore of  the  set).
137                 The  calling  process  must have read permission on the sema‐
138                 phore set.
139
140       GETPID    Return the value of sempid for the semnum-th semaphore of the
141                 set.   This  is the PID of the process that last performed an
142                 operation on that semaphore (but  see  NOTES).   The  calling
143                 process must have read permission on the semaphore set.
144
145       GETVAL    Return the value of semval for the semnum-th semaphore of the
146                 set.  The calling process must have read  permission  on  the
147                 semaphore set.
148
149       GETZCNT   Return  the  value  of semzcnt for the semnum-th semaphore of
150                 the set (i.e., the number of processes waiting for semval  of
151                 the semnum-th semaphore of the set to become 0).  The calling
152                 process must have read permission on the semaphore set.
153
154       SETALL    Set semval for all semaphores of  the  set  using  arg.array,
155                 updating  also the sem_ctime member of the semid_ds structure
156                 associated with the set.  Undo  entries  (see  semop(2))  are
157                 cleared  for  altered  semaphores  in  all processes.  If the
158                 changes to semaphore values  would  permit  blocked  semop(2)
159                 calls in other processes to proceed, then those processes are
160                 woken up.  The  argument  semnum  is  ignored.   The  calling
161                 process  must  have alter (write) permission on the semaphore
162                 set.
163
164       SETVAL    Set the value of semval to arg.val for  the  semnum-th  sema‐
165                 phore  of  the set, updating also the sem_ctime member of the
166                 semid_ds structure associated with the set.  Undo entries are
167                 cleared  for  altered  semaphores  in  all processes.  If the
168                 changes to semaphore values  would  permit  blocked  semop(2)
169                 calls in other processes to proceed, then those processes are
170                 woken up.  The calling process must have alter permission  on
171                 the semaphore set.
172

RETURN VALUE

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

ERRORS

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

CONFORMING TO

232       POSIX.1-2001, POSIX.1-2008, SVr4.
233
234       POSIX.1 specifies the sem_nsems field of the semid_ds structure as hav‐
235       ing  the type unsigned short, and the field is so defined on most other
236       systems.  It was also so defined on Linux 2.2 and earlier,  but,  since
237       Linux 2.4, the field has the type unsigned long.
238

NOTES

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

SEE ALSO

288       ipc(2),    semget(2),   semop(2),   capabilities(7),   sem_overview(7),
289       sysvipc(7)
290

COLOPHON

292       This page is part of release 5.04 of the Linux  man-pages  project.   A
293       description  of  the project, information about reporting bugs, and the
294       latest    version    of    this    page,    can     be     found     at
295       https://www.kernel.org/doc/man-pages/.
296
297
298
299Linux                             2019-08-02                         SEMCTL(2)
Impressum