1SHMCTL(2) Linux Programmer's Manual SHMCTL(2)
2
3
4
6 shmctl - shared memory control
7
9 #include <sys/ipc.h>
10 #include <sys/shm.h>
11
12 int shmctl(int shmid, int cmd, struct shmid_ds *buf);
13
15 shmctl() performs the control operation specified by cmd on the shared
16 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 in <sys/ipc.h> as follows (the high‐
34 lighted fields 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 destroyed; other‐
73 wise its pages that were faulted in will remain in memory or swap.
74
75 IPC_INFO (Linux-specific)
76 Returns information about system-wide shared memory limits
77 and parameters in the structure pointed to by buf. This
78 structure is of type shminfo (thus, a cast is required),
79 defined in <sys/shm.h> if the _GNU_SOURCE feature test macro
80 is defined:
81
82 struct shminfo {
83 unsigned long shmmax; /* Maximum segment size */
84 unsigned long shmmin; /* Minimum segment size;
85 always 1 */
86 unsigned long shmmni; /* Maximum number of segments */
87 unsigned long shmseg; /* Maximum number of segments
88 that a process can attach;
89 unused within kernel */
90 unsigned long shmall; /* Maximum number of pages of
91 shared memory, system-wide */
92 };
93
94 The shmmni, shmmax, and shmall settings can be changed via
95 /proc files of the same name; see proc(5) for details.
96
97 SHM_INFO (Linux-specific)
98 Returns a shm_info structure whose fields contain information
99 about system resources consumed by shared memory. This
100 structure is defined in <sys/shm.h> if the _GNU_SOURCE fea‐
101 ture test macro is defined:
102
103 struct shm_info {
104 int used_ids; /* # of currently existing
105 segments */
106 unsigned long shm_tot; /* Total number of shared
107 memory pages */
108 unsigned long shm_rss; /* # of resident shared
109 memory pages */
110 unsigned long shm_swp; /* # of swapped shared
111 memory pages */
112 unsigned long swap_attempts;
113 /* Unused since Linux 2.4 */
114 unsigned long swap_successes;
115 /* Unused since Linux 2.4 */
116 };
117
118 SHM_STAT (Linux-specific)
119 Returns a shmid_ds structure as for IPC_STAT. However, the
120 shmid argument is not a segment identifier, but instead an
121 index into the kernel's internal array that maintains infor‐
122 mation about all shared memory segments on the system.
123
124 The caller can prevent or allow swapping of a shared memory segment
125 with the following cmd values:
126
127 SHM_LOCK (Linux-specific)
128 Prevent swapping of the shared memory segment. The caller
129 must fault in any pages that are required to be present after
130 locking is enabled. If a segment has been locked, then the
131 (nonstandard) SHM_LOCKED flag of the shm_perm.mode field in
132 the associated data structure retrieved by IPC_STAT will be
133 set.
134
135 SHM_UNLOCK (Linux-specific)
136 Unlock the segment, allowing it to be swapped out.
137
138 In kernels before 2.6.10, only a privileged process could employ
139 SHM_LOCK and SHM_UNLOCK. Since kernel 2.6.10, an unprivileged process
140 can employ these operations if its effective UID matches the owner or
141 creator UID of the segment, and (for SHM_LOCK) the amount of memory to
142 be locked falls within the RLIMIT_MEMLOCK resource limit (see setr‐
143 limit(2)).
144
146 A successful IPC_INFO or SHM_INFO operation returns the index of the
147 highest used entry in the kernel's internal array recording information
148 about all shared memory segments. (This information can be used with
149 repeated SHM_STAT operations to obtain information about all shared
150 memory segments on the system.) A successful SHM_STAT operation
151 returns the identifier of the shared memory segment whose index was
152 given in shmid. Other operations return 0 on success.
153
154 On error, -1 is returned, and errno is set appropriately.
155
157 EACCES IPC_STAT or SHM_STAT is requested and shm_perm.mode does not
158 allow read access for shmid, and the calling process does not
159 have the CAP_IPC_OWNER capability.
160
161 EFAULT The argument cmd has value IPC_SET or IPC_STAT but the address
162 pointed to by buf isn't accessible.
163
164 EIDRM shmid points to a removed identifier.
165
166 EINVAL shmid is not a valid identifier, or cmd is not a valid command.
167 Or: for a SHM_STAT operation, the index value specified in shmid
168 referred to an array slot that is currently unused.
169
170 ENOMEM (In kernels since 2.6.9), SHM_LOCK was specified and the size of
171 the to-be-locked segment would mean that the total bytes in
172 locked shared memory segments would exceed the limit for the
173 real user ID of the calling process. This limit is defined by
174 the RLIMIT_MEMLOCK soft resource limit (see setrlimit(2)).
175
176 EOVERFLOW
177 IPC_STAT is attempted, and the GID or UID value is too large to
178 be stored in the structure pointed to by buf.
179
180 EPERM IPC_SET or IPC_RMID is attempted, and the effective user ID of
181 the calling process is not that of the creator (found in
182 shm_perm.cuid), or the owner (found in shm_perm.uid), and the
183 process was not privileged (Linux: did not have the
184 CAP_SYS_ADMIN capability).
185
186 Or (in kernels before 2.6.9), SHM_LOCK or SHM_UNLOCK was speci‐
187 fied, but the process was not privileged (Linux: did not have
188 the CAP_IPC_LOCK capability). (Since Linux 2.6.9, this error
189 can also occur if the RLIMIT_MEMLOCK is 0 and the caller is not
190 privileged.)
191
193 SVr4, POSIX.1-2001.
194
196 The IPC_INFO, SHM_STAT and SHM_INFO operations are used by the ipcs(1)
197 program to provide information on allocated resources. In the future
198 these may modified or moved to a /proc file system interface.
199
200 Linux permits a process to attach (shmat(2)) a shared memory segment
201 that has already been marked for deletion using shmctl(IPC_RMID). This
202 feature is not available on other Unix implementations; portable appli‐
203 cations should avoid relying on it.
204
205 Various fields in a struct shmid_ds were typed as short under Linux 2.2
206 and have become long under Linux 2.4. To take advantage of this, a
207 recompilation under glibc-2.1.91 or later should suffice. (The kernel
208 distinguishes old and new calls by an IPC_64 flag in cmd.)
209
211 mlock(2), setrlimit(2), shmget(2), shmop(2), capabilities(7), svipc(7)
212
214 This page is part of release 3.25 of the Linux man-pages project. A
215 description of the project, and information about reporting bugs, can
216 be found at http://www.kernel.org/doc/man-pages/.
217
218
219
220Linux 2008-08-07 SHMCTL(2)