1SHMCTL(2) Linux Programmer's Manual SHMCTL(2)
2
3
4
6 shmctl - shared memory control
7
9 #include <sys/ipc.h> #include <sys/shm.h>
10
11 int shmctl(int shmid, int cmd, struct shmid_ds *buf);
12
14 shmctl() performs the control operation specified by cmd on the shared
15 memory segment whose identifier is given in shmid.
16
17 The buf argument is a pointer to a shmid_ds structure, defined in
18 <sys/shm.h> as follows:
19
20 struct shmid_ds {
21 struct ipc_perm shm_perm; /* Ownership and permissions */
22 size_t shm_segsz; /* Size of segment (bytes) */
23 time_t shm_atime; /* Last attach time */
24 time_t shm_dtime; /* Last detach time */
25 time_t shm_ctime; /* Last change time */
26 pid_t shm_cpid; /* PID of creator */
27 pid_t shm_lpid; /* PID of last shmat()/shmdt() */
28 shmatt_t shm_nattch; /* No. of current attaches */
29 ...
30 };
31
32 The ipc_perm structure is defined in <sys/ipc.h> as follows (the high‐
33 lighted fields are settable using IPC_SET):
34
35 struct ipc_perm {
36 key_t key; /* Key supplied to shmget() */
37 uid_t uid; /* Effective UID of owner */
38 gid_t gid; /* Effective GID of owner */
39 uid_t cuid; /* Effective UID of creator */
40 gid_t cgid; /* Effective GID of creator */
41 unsigned short mode; /* Permissions + SHM_DEST and
42 SHM_LOCKED flags */
43 unsigned short seq; /* Sequence number */
44 };
45
46 Valid values for cmd are:
47
48 IPC_STAT Copy information from the kernel data structure associated
49 with shmid into the shmid_ds structure pointed to by buf.
50 The caller must have read permission on the shared memory
51 segment.
52
53 IPC_SET Write the values of some members of the shmid_ds structure
54 pointed to by buf to the kernel data structure associated
55 with this shared memory segment, updating also its
56 shm_ctime member. The following fields can be changed:
57 shm_perm.uid, shm_perm.gid, and (the least significant 9
58 bits of) shm_perm.mode. The effective UID of the calling
59 process must match the owner (shm_perm.uid) or creator
60 (shm_perm.cuid) of the shared memory segment, or the caller
61 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 struc‐
66 ture shmid_ds is zero). The caller must be the owner or
67 creator, or be privileged. If a segment has been marked
68 for destruction, then the (non-standard) SHM_DEST flag of
69 the 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 and
77 parameters in the structure pointed to by buf. This structure
78 is of type shminfo (thus, a cast is required), defined in
79 <sys/shm.h> if the _GNU_SOURCE feature test macro is defined:
80
81 struct shminfo {
82 unsigned long shmmax; /* Max. segment size */
83 unsigned long shmmin; /* Min. segment size; always 1 */
84 unsigned long shmmni; /* Max. # of segments */
85 unsigned long shmseg; /* Max. # of segments that a
86 process can attach; unused */
87 unsigned long shmall; /* Max. # of pages of shared
88 memory, system-wide */
89 };
90
91 The shmmni, shmmax, and shmall settings can be changed via /proc
92 files of the same name; see proc(5) for details.
93
94 SHM_INFO (Linux specific)
95 Returns a shm_info structure whose fields contain information
96 about system resources consumed by shared memory. This struc‐
97 ture is defined in <sys/shm.h> if the _GNU_SOURCE feature test
98 macro is defined:
99
100 struct shm_info {
101 int used_ids; /* # of currently existing
102 segments */
103 unsigned long shm_tot; /* Total number of shared
104 memory pages */
105 unsigned long shm_rss; /* # of resident shared
106 memory pages */
107 unsigned long shm_swp; /* # of swapped shared
108 memory pages */
109 unsigned long swap_attempts; /* Unused since Linux 2.4 */
110 unsigned long swap_successes; /* Unused since Linux 2.4 */
111 };
112
113
114 SHM_STAT (Linux specific)
115 Returns a shmid_ds structure as for IPC_STAT. However, the
116 shmid argument is not a segment identifier, but instead an index
117 into the kernel's internal array that maintains information
118 about all shared memory segments on the system.
119
120 The caller can prevent or allow swapping of a shared memory segment
121 with the following cmd values:
122
123 SHM_LOCK (Linux specific)
124 Prevent swapping of the shared memory segment. The caller
125 must fault in any pages that are required to be present
126 after locking is enabled. If a segment has been locked,
127 then the (non-standard) SHM_LOCKED flag of the
128 shm_perm.mode field in the associated data structure
129 retrieved by IPC_STAT will be set.
130
131 SHM_UNLOCK (Linux specific)
132 Unlock the segment, allowing it to be swapped out.
133
134 In kernels before 2.6.10, only a privileged process could employ
135 SHM_LOCK and SHM_UNLOCK. Since kernel 2.6.10, an unprivileged process
136 can employ these operations if its effective UID matches the owner or
137 creator UID of the segment, and (for SHM_LOCK) the amount of memory to
138 be locked falls within the RLIMIT_MEMLOCK resource limit (see setr‐
139 limit(2)).
140
142 A successful IPC_INFO or SHM_INFO operation returns the index of the
143 highest used entry in the kernel's internal array recording information
144 about all shared memory segments. (This information can be used with
145 repeated SHM_STAT operations to obtain information about all shared
146 memory segments on the system.) A successful SHM_STAT operation
147 returns the identifier of the shared memory segment whose index was
148 given in shmid. Other operations return 0 on success.
149
150 On error, -1 is returned, and errno is set appropriately.
151
153 EACCES IPC_STAT or SHM_STAT is requested and shm_perm.mode does not
154 allow read access for shmid, and the calling process does
155 not have the CAP_IPC_OWNER capability.
156
157 EFAULT The argument cmd has value IPC_SET or IPC_STAT but the
158 address pointed to by buf isn't accessible.
159
160 EIDRM shmid points to a removed identifier.
161
162 EINVAL shmid is not a valid identifier, or cmd is not a valid com‐
163 mand. Or: for a SHM_STAT operation, the index value speci‐
164 fied in shmid referred to an array slot that is currently
165 unused.
166
167 ENOMEM (In kernels since 2.6.9), SHM_LOCK was specified and the
168 size of the to-be-locked segment would mean that the total
169 bytes in locked shared memory segments would exceed the
170 limit for the real user ID of the calling process. This
171 limit is defined by the RLIMIT_MEMLOCK soft resource limit
172 (see setrlimit(2)).
173
174 EOVERFLOW IPC_STAT is attempted, and the GID or UID value is too large
175 to be stored in the structure pointed to by buf.
176
177 EPERM IPC_SET or IPC_RMID is attempted, and the effective user ID
178 of the calling process is not that of the creator (found in
179 shm_perm.cuid), or the owner (found in shm_perm.uid), and
180 the process was not privileged (Linux: did not have the
181 CAP_SYS_ADMIN capability).
182
183 Or (in kernels before 2.6.9), SHM_LOCK or SHM_UNLOCK was
184 specified, but the process was not privileged (Linux: did
185 not have the CAP_IPC_LOCK capability). (Since Linux 2.6.9,
186 this error can also occur if the RLIMIT_MEMLOCK is 0 and the
187 caller is not privileged.)
188
190 The IPC_INFO, SHM_STAT and SHM_INFO operations are used by the ipcs(8)
191 program to provide information on allocated resources. In the future
192 these may modified or moved to a /proc file system interface.
193
194 Linux permits a process to attach (shmat()) a shared memory segment
195 that has already been marked for deletion using shmctl(IPC_RMID). This
196 feature is not available on other Unix implementations; portable appli‐
197 cations should avoid relying on it.
198
199 Various fields in a struct shmid_ds were shorts under Linux 2.2 and
200 have become longs under Linux 2.4. To take advantage of this, a recom‐
201 pilation under glibc-2.1.91 or later should suffice. (The kernel dis‐
202 tinguishes old and new calls by an IPC_64 flag in cmd.)
203
205 SVr4, POSIX.1-2001.
206
208 mlock(2), setrlimit(2), shmget(2), shmop(2), capabilities(7)
209
210
211
212Linux 2.6.11 2005-05-30 SHMCTL(2)