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       SHM_STAT_ANY (Linux-specific, since Linux 4.17)
131                 Return  a  shmid_ds  structure  as  for  SHM_STAT.   However,
132                 shm_perm.mode is not checked for read access for shmid, mean‐
133                 ing that any user can employ this operation (just as any user
134                 may read /proc/sysvipc/shm to obtain the same information).
135
136       The caller can prevent or allow swapping of  a  shared  memory  segment
137       with the following cmd values:
138
139       SHM_LOCK (Linux-specific)
140                 Prevent  swapping  of  the shared memory segment.  The caller
141                 must fault in any pages that are required to be present after
142                 locking  is  enabled.  If a segment has been locked, then the
143                 (nonstandard) SHM_LOCKED flag of the shm_perm.mode  field  in
144                 the  associated  data structure retrieved by IPC_STAT will be
145                 set.
146
147       SHM_UNLOCK (Linux-specific)
148                 Unlock the segment, allowing it to be swapped out.
149
150       In kernels before  2.6.10,  only  a  privileged  process  could  employ
151       SHM_LOCK  and SHM_UNLOCK.  Since kernel 2.6.10, an unprivileged process
152       can employ these operations if its effective UID matches the  owner  or
153       creator  UID of the segment, and (for SHM_LOCK) the amount of memory to
154       be locked falls within the RLIMIT_MEMLOCK  resource  limit  (see  setr‐
155       limit(2)).
156

RETURN VALUE

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

ERRORS

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

CONFORMING TO

207       POSIX.1-2001, POSIX.1-2008, SVr4.
208

NOTES

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

SEE ALSO

231       mlock(2),    setrlimit(2),    shmget(2),   shmop(2),   capabilities(7),
232       sysvipc(7)
233

COLOPHON

235       This page is part of release 5.04 of the Linux  man-pages  project.   A
236       description  of  the project, information about reporting bugs, and the
237       latest    version    of    this    page,    can     be     found     at
238       https://www.kernel.org/doc/man-pages/.
239
240
241
242Linux                             2019-08-02                         SHMCTL(2)
Impressum