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

NAME

6       shmctl - System V shared memory control
7

SYNOPSIS

9       #include <sys/ipc.h>
10       #include <sys/shm.h>
11
12       int shmctl(int shmid, int cmd, struct shmid_ds *buf);
13

DESCRIPTION

15       shmctl()  performs  the  control operation specified by cmd on the Sys‐
16       tem V shared memory segment whose identifier is given in shmid.
17
18       The buf argument is a pointer  to  a  shmid_ds  structure,  defined  in
19       <sys/shm.h> as follows:
20
21           struct shmid_ds {
22               struct ipc_perm shm_perm;    /* Ownership and permissions */
23               size_t          shm_segsz;   /* Size of segment (bytes) */
24               time_t          shm_atime;   /* Last attach time */
25               time_t          shm_dtime;   /* Last detach time */
26               time_t          shm_ctime;   /* Last change time */
27               pid_t           shm_cpid;    /* PID of creator */
28               pid_t           shm_lpid;    /* PID of last shmat(2)/shmdt(2) */
29               shmatt_t        shm_nattch;  /* No. of current attaches */
30               ...
31           };
32
33       The  ipc_perm  structure  is defined as follows (the highlighted fields
34       are settable using IPC_SET):
35
36           struct ipc_perm {
37               key_t          __key;    /* Key supplied to shmget(2) */
38               uid_t          uid;      /* Effective UID of owner */
39               gid_t          gid;      /* Effective GID of owner */
40               uid_t          cuid;     /* Effective UID of creator */
41               gid_t          cgid;     /* Effective GID of creator */
42               unsigned short mode;     /* Permissions + SHM_DEST and
43                                           SHM_LOCKED flags */
44               unsigned short __seq;    /* Sequence number */
45           };
46
47       Valid values for cmd are:
48
49       IPC_STAT  Copy information from the kernel  data  structure  associated
50                 with  shmid  into  the  shmid_ds structure pointed to by buf.
51                 The caller must have read permission  on  the  shared  memory
52                 segment.
53
54       IPC_SET   Write  the  values  of some members of the shmid_ds structure
55                 pointed to by buf to the  kernel  data  structure  associated
56                 with  this shared memory segment, updating also its shm_ctime
57                 member.  The following fields can be  changed:  shm_perm.uid,
58                 shm_perm.gid,   and   (the   least  significant  9  bits  of)
59                 shm_perm.mode.  The effective UID of the calling process must
60                 match  the owner (shm_perm.uid) or creator (shm_perm.cuid) of
61                 the shared memory segment, or the caller must be privileged.
62
63       IPC_RMID  Mark the segment to be destroyed.  The segment will  actually
64                 be  destroyed  only after the last process detaches it (i.e.,
65                 when  the  shm_nattch  member  of  the  associated  structure
66                 shmid_ds  is  zero).  The caller must be the owner or creator
67                 of the segment,  or  be  privileged.   The  buf  argument  is
68                 ignored.
69
70                 If  a segment has been marked for destruction, then the (non‐
71                 standard) SHM_DEST flag of the  shm_perm.mode  field  in  the
72                 associated data structure retrieved by IPC_STAT will be set.
73
74                 The   caller   must  ensure  that  a  segment  is  eventually
75                 destroyed; otherwise its pages  that  were  faulted  in  will
76                 remain in memory or swap.
77
78                 See  also the description of /proc/sys/kernel/shm_rmid_forced
79                 in proc(5).
80
81       IPC_INFO (Linux-specific)
82                 Return information about system-wide shared memory limits and
83                 parameters  in  the structure pointed to by buf.  This struc‐
84                 ture is of type shminfo (thus, a cast is  required),  defined
85                 in  <sys/shm.h>  if  the  _GNU_SOURCE  feature  test macro is
86                 defined:
87
88                     struct shminfo {
89                         unsigned long shmmax; /* Maximum segment size */
90                         unsigned long shmmin; /* Minimum segment size;
91                                                  always 1 */
92                         unsigned long shmmni; /* Maximum number of segments */
93                         unsigned long shmseg; /* Maximum number of segments
94                                                  that a process can attach;
95                                                  unused within kernel */
96                         unsigned long shmall; /* Maximum number of pages of
97                                                  shared memory, system-wide */
98                     };
99
100                 The shmmni, shmmax, and shmall settings can  be  changed  via
101                 /proc files of the same name; see proc(5) for details.
102
103       SHM_INFO (Linux-specific)
104                 Return  a shm_info structure whose fields contain information
105                 about system  resources  consumed  by  shared  memory.   This
106                 structure  is  defined in <sys/shm.h> if the _GNU_SOURCE fea‐
107                 ture test macro is defined:
108
109                     struct shm_info {
110                         int           used_ids; /* # of currently existing
111                                                    segments */
112                         unsigned long shm_tot;  /* Total number of shared
113                                                    memory pages */
114                         unsigned long shm_rss;  /* # of resident shared
115                                                    memory pages */
116                         unsigned long shm_swp;  /* # of swapped shared
117                                                    memory pages */
118                         unsigned long swap_attempts;
119                                                 /* Unused since Linux 2.4 */
120                         unsigned long swap_successes;
121                                                 /* Unused since Linux 2.4 */
122                     };
123
124       SHM_STAT (Linux-specific)
125                 Return a shmid_ds structure as for  IPC_STAT.   However,  the
126                 shmid  argument  is  not a segment identifier, but instead an
127                 index into the kernel's internal array that maintains  infor‐
128                 mation about all shared memory segments on the system.
129
130       The  caller  can  prevent  or allow swapping of a shared memory segment
131       with the following cmd values:
132
133       SHM_LOCK (Linux-specific)
134                 Prevent swapping of the shared memory  segment.   The  caller
135                 must fault in any pages that are required to be present after
136                 locking is enabled.  If a segment has been locked,  then  the
137                 (nonstandard)  SHM_LOCKED  flag of the shm_perm.mode field in
138                 the associated data structure retrieved by IPC_STAT  will  be
139                 set.
140
141       SHM_UNLOCK (Linux-specific)
142                 Unlock the segment, allowing it to be swapped out.
143
144       In  kernels  before  2.6.10,  only  a  privileged  process could employ
145       SHM_LOCK and SHM_UNLOCK.  Since kernel 2.6.10, an unprivileged  process
146       can  employ  these operations if its effective UID matches the owner or
147       creator UID of the segment, and (for SHM_LOCK) the amount of memory  to
148       be  locked  falls  within  the RLIMIT_MEMLOCK resource limit (see setr‐
149       limit(2)).
150

RETURN VALUE

152       A successful IPC_INFO or SHM_INFO operation returns the  index  of  the
153       highest used entry in the kernel's internal array recording information
154       about all shared memory segments.  (This information can be  used  with
155       repeated  SHM_STAT  operations  to  obtain information about all shared
156       memory segments  on  the  system.)   A  successful  SHM_STAT  operation
157       returns  the  identifier  of  the shared memory segment whose index was
158       given in shmid.  Other operations return 0 on success.
159
160       On error, -1 is returned, and errno is set appropriately.
161

ERRORS

163       EACCES IPC_STAT or SHM_STAT is requested  and  shm_perm.mode  does  not
164              allow  read  access  for shmid, and the calling process does not
165              have the CAP_IPC_OWNER capability in  the  user  namespace  that
166              governs its IPC namespace.
167
168       EFAULT The  argument  cmd has value IPC_SET or IPC_STAT but the address
169              pointed to by buf isn't accessible.
170
171       EIDRM  shmid points to a removed identifier.
172
173       EINVAL shmid is not a valid identifier, or cmd is not a valid  command.
174              Or: for a SHM_STAT operation, the index value specified in shmid
175              referred to an array slot that is currently unused.
176
177       ENOMEM (In kernels since 2.6.9), SHM_LOCK was specified and the size of
178              the  to-be-locked  segment  would  mean  that the total bytes in
179              locked shared memory segments would exceed  the  limit  for  the
180              real  user  ID of the calling process.  This limit is defined by
181              the RLIMIT_MEMLOCK soft resource limit (see setrlimit(2)).
182
183       EOVERFLOW
184              IPC_STAT is attempted, and the GID or UID value is too large  to
185              be stored in the structure pointed to by buf.
186
187       EPERM  IPC_SET  or  IPC_RMID is attempted, and the effective user ID of
188              the calling process  is  not  that  of  the  creator  (found  in
189              shm_perm.cuid),  or  the  owner (found in shm_perm.uid), and the
190              process  was  not  privileged   (Linux:   did   not   have   the
191              CAP_SYS_ADMIN capability).
192
193              Or  (in kernels before 2.6.9), SHM_LOCK or SHM_UNLOCK was speci‐
194              fied, but the process was not privileged (Linux:  did  not  have
195              the  CAP_IPC_LOCK  capability).   (Since Linux 2.6.9, this error
196              can also occur if the RLIMIT_MEMLOCK is 0 and the caller is  not
197              privileged.)
198

CONFORMING TO

200       POSIX.1-2001, POSIX.1-2008, SVr4.
201

NOTES

203       The  inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
204       or by any version of POSIX.  However, some old implementations required
205       the inclusion of these header files, and the SVID also documented their
206       inclusion.  Applications intended to be portable to  such  old  systems
207       may need to include these header files.
208
209       The  IPC_INFO, SHM_STAT and SHM_INFO operations are used by the ipcs(1)
210       program to provide information on allocated resources.  In the  future,
211       these may modified or moved to a /proc filesystem interface.
212
213       Linux  permits  a  process to attach (shmat(2)) a shared memory segment
214       that has already been marked for deletion using shmctl(IPC_RMID).  This
215       feature is not available on other UNIX implementations; portable appli‐
216       cations should avoid relying on it.
217
218       Various fields in a struct shmid_ds were typed as short under Linux 2.2
219       and  have  become  long  under Linux 2.4.  To take advantage of this, a
220       recompilation under glibc-2.1.91 or later should suffice.  (The  kernel
221       distinguishes old and new calls by an IPC_64 flag in cmd.)
222

SEE ALSO

224       mlock(2), setrlimit(2), shmget(2), shmop(2), capabilities(7), svipc(7)
225

COLOPHON

227       This  page  is  part of release 4.15 of the Linux man-pages project.  A
228       description of the project, information about reporting bugs,  and  the
229       latest     version     of     this    page,    can    be    found    at
230       https://www.kernel.org/doc/man-pages/.
231
232
233
234Linux                             2017-09-15                         SHMCTL(2)
Impressum