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 System
16       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  only
64                 actually  be  destroyed  after  the  last process detaches it
65                 (i.e., when the shm_nattch member of the associated structure
66                 shmid_ds  is zero).  The caller must be the owner or creator,
67                 or be privileged.  If a segment has been marked for  destruc‐
68                 tion,   then   the   (nonstandard)   SHM_DEST   flag  of  the
69                 shm_perm.mode  field  in  the   associated   data   structure
70                 retrieved by IPC_STAT will be set.
71
72                 The   caller   must  ensure  that  a  segment  is  eventually
73                 destroyed; otherwise its pages  that  were  faulted  in  will
74                 remain in memory or swap.
75
76                 See  also the description of /proc/sys/kernel/shm_rmid_forced
77                 in proc(5).
78
79       IPC_INFO (Linux-specific)
80                 Returns information about system-wide  shared  memory  limits
81                 and  parameters  in  the  structure  pointed to by buf.  This
82                 structure is of type shminfo  (thus,  a  cast  is  required),
83                 defined  in <sys/shm.h> if the _GNU_SOURCE feature test macro
84                 is defined:
85
86                     struct  shminfo {
87                         unsigned long shmmax; /* Maximum segment size */
88                         unsigned long shmmin; /* Minimum segment size;
89                                                  always 1 */
90                         unsigned long shmmni; /* Maximum number of segments */
91                         unsigned long shmseg; /* Maximum number of segments
92                                                  that a process can attach;
93                                                  unused within kernel */
94                         unsigned long shmall; /* Maximum number of pages of
95                                                  shared memory, system-wide */
96                     };
97
98                 The shmmni, shmmax, and shmall settings can  be  changed  via
99                 /proc files of the same name; see proc(5) for details.
100
101       SHM_INFO (Linux-specific)
102                 Returns a shm_info structure whose fields contain information
103                 about system  resources  consumed  by  shared  memory.   This
104                 structure  is  defined in <sys/shm.h> if the _GNU_SOURCE fea‐
105                 ture test macro is defined:
106
107                     struct shm_info {
108                         int           used_ids; /* # of currently existing
109                                                    segments */
110                         unsigned long shm_tot;  /* Total number of shared
111                                                    memory pages */
112                         unsigned long shm_rss;  /* # of resident shared
113                                                    memory pages */
114                         unsigned long shm_swp;  /* # of swapped shared
115                                                    memory pages */
116                         unsigned long swap_attempts;
117                                                 /* Unused since Linux 2.4 */
118                         unsigned long swap_successes;
119                                                 /* Unused since Linux 2.4 */
120                     };
121
122       SHM_STAT (Linux-specific)
123                 Returns a shmid_ds structure as for IPC_STAT.   However,  the
124                 shmid  argument  is  not a segment identifier, but instead an
125                 index into the kernel's internal array that maintains  infor‐
126                 mation about all shared memory segments on the system.
127
128       The  caller  can  prevent  or allow swapping of a shared memory segment
129       with the following cmd values:
130
131       SHM_LOCK (Linux-specific)
132                 Prevent swapping of the shared memory  segment.   The  caller
133                 must fault in any pages that are required to be present after
134                 locking is enabled.  If a segment has been locked,  then  the
135                 (nonstandard)  SHM_LOCKED  flag of the shm_perm.mode field in
136                 the associated data structure retrieved by IPC_STAT  will  be
137                 set.
138
139       SHM_UNLOCK (Linux-specific)
140                 Unlock the segment, allowing it to be swapped out.
141
142       In  kernels  before  2.6.10,  only  a  privileged  process could employ
143       SHM_LOCK and SHM_UNLOCK.  Since kernel 2.6.10, an unprivileged  process
144       can  employ  these operations if its effective UID matches the owner or
145       creator UID of the segment, and (for SHM_LOCK) the amount of memory  to
146       be  locked  falls  within  the RLIMIT_MEMLOCK resource limit (see setr‐
147       limit(2)).
148

RETURN VALUE

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

ERRORS

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

CONFORMING TO

197       SVr4, POSIX.1-2001.
198

NOTES

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

SEE ALSO

221       mlock(2), setrlimit(2), shmget(2), shmop(2), capabilities(7), svipc(7)
222

COLOPHON

224       This page is part of release 3.53 of the Linux  man-pages  project.   A
225       description  of  the project, and information about reporting bugs, can
226       be found at http://www.kernel.org/doc/man-pages/.
227
228
229
230Linux                             2013-03-18                         SHMCTL(2)
Impressum