597 lines
17 KiB
HTML
597 lines
17 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of SHMCTL</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>SHMCTL</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
shmctl - System V shared memory control
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/shm.h">sys/shm.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int shmctl(int </B><I>shmid</I><B>, int </B><I>cmd</I><B>, struct shmid_ds *</B><I>buf</I><B>);</B>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>shmctl</B>()
|
|
|
|
performs the control operation specified by
|
|
<I>cmd</I>
|
|
|
|
on the System V shared memory segment whose identifier is given in
|
|
<I>shmid</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>buf</I>
|
|
|
|
argument is a pointer to a <I>shmid_ds</I> structure,
|
|
defined in <I><<A HREF="file:///usr/include/sys/shm.h">sys/shm.h</A>></I> as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct shmid_ds {
|
|
<BR> struct ipc_perm shm_perm; /* Ownership and permissions */
|
|
<BR> size_t shm_segsz; /* Size of segment (bytes) */
|
|
<BR> time_t shm_atime; /* Last attach time */
|
|
<BR> time_t shm_dtime; /* Last detach time */
|
|
<BR> time_t shm_ctime; /* Last change time */
|
|
<BR> pid_t shm_cpid; /* PID of creator */
|
|
<BR> pid_t shm_lpid; /* PID of last <A HREF="/cgi-bin/man/man2html?2+shmat">shmat</A>(2)/<A HREF="/cgi-bin/man/man2html?2+shmdt">shmdt</A>(2) */
|
|
<BR> shmatt_t shm_nattch; /* No. of current attaches */
|
|
<BR> ...
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>ipc_perm</I>
|
|
|
|
structure is defined as follows
|
|
(the highlighted fields are settable using
|
|
<B>IPC_SET</B>):
|
|
|
|
<P>
|
|
|
|
|
|
|
|
struct ipc_perm {
|
|
<BR> key_t __key; /* Key supplied to <A HREF="/cgi-bin/man/man2html?2+shmget">shmget</A>(2) */
|
|
<BR> uid_t <B>uid</B>; /* Effective UID of owner */
|
|
<BR> gid_t <B>gid</B>; /* Effective GID of owner */
|
|
<BR> uid_t cuid; /* Effective UID of creator */
|
|
<BR> gid_t cgid; /* Effective GID of creator */
|
|
<BR> unsigned short <B>mode</B>; /* <B>Permissions</B> + SHM_DEST and
|
|
<BR> SHM_LOCKED flags */
|
|
<BR> unsigned short __seq; /* Sequence number */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
Valid values for
|
|
<I>cmd</I>
|
|
|
|
are:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>IPC_STAT</B>
|
|
|
|
<DD>
|
|
Copy information from the kernel data structure associated with
|
|
<I>shmid</I>
|
|
|
|
into the
|
|
<I>shmid_ds</I>
|
|
|
|
structure pointed to by <I>buf</I>.
|
|
The caller must have read permission on the
|
|
shared memory segment.
|
|
<DT id="2"><B>IPC_SET</B>
|
|
|
|
<DD>
|
|
Write the values of some members of the
|
|
<I>shmid_ds</I>
|
|
|
|
structure pointed to by
|
|
<I>buf</I>
|
|
|
|
to the kernel data structure associated with this shared memory segment,
|
|
updating also its
|
|
<I>shm_ctime</I>
|
|
|
|
member.
|
|
The following fields can be changed:
|
|
<I>shm_perm.uid</I>, <I>shm_perm.gid</I>,
|
|
and (the least significant 9 bits of) <I>shm_perm.mode</I>.
|
|
The effective UID of the calling process must match the owner
|
|
(<I>shm_perm.uid</I>)
|
|
|
|
or creator
|
|
(<I>shm_perm.cuid</I>)
|
|
|
|
of the shared memory segment, or the caller must be privileged.
|
|
<DT id="3"><B>IPC_RMID</B>
|
|
|
|
<DD>
|
|
Mark the segment to be destroyed.
|
|
The segment will actually be destroyed
|
|
only after the last process detaches it (i.e., when the
|
|
<I>shm_nattch</I>
|
|
|
|
member of the associated structure
|
|
<I>shmid_ds</I>
|
|
|
|
is zero).
|
|
The caller must be the owner or creator of the segment, or be privileged.
|
|
The
|
|
<I>buf</I>
|
|
|
|
argument is ignored.
|
|
<DT id="4"><DD>
|
|
If a segment has been marked for destruction, then the (nonstandard)
|
|
<B>SHM_DEST</B>
|
|
|
|
flag of the
|
|
<I>shm_perm.mode</I>
|
|
|
|
field in the associated data structure retrieved by
|
|
<B>IPC_STAT</B>
|
|
|
|
will be set.
|
|
<DT id="5"><DD>
|
|
The caller <I>must</I> ensure that a segment is eventually destroyed;
|
|
otherwise its pages that were faulted in will remain in memory or swap.
|
|
<DT id="6"><DD>
|
|
See also the description of
|
|
<I>/proc/sys/kernel/shm_rmid_forced</I>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5).
|
|
|
|
<DT id="7"><B>IPC_INFO</B> (Linux-specific)
|
|
|
|
<DD>
|
|
Return information about system-wide shared memory limits and
|
|
parameters in the structure pointed to by
|
|
<I>buf</I>.
|
|
|
|
This structure is of type
|
|
<I>shminfo</I>
|
|
|
|
(thus, a cast is required),
|
|
defined in
|
|
<I><<A HREF="file:///usr/include/sys/shm.h">sys/shm.h</A>></I>
|
|
|
|
if the
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
feature test macro is defined:
|
|
<DT id="8"><DD>
|
|
|
|
|
|
struct shminfo {
|
|
<BR> unsigned long shmmax; /* Maximum segment size */
|
|
<BR> unsigned long shmmin; /* Minimum segment size;
|
|
<BR> always 1 */
|
|
<BR> unsigned long shmmni; /* Maximum number of segments */
|
|
<BR> unsigned long shmseg; /* Maximum number of segments
|
|
<BR> that a process can attach;
|
|
<BR> unused within kernel */
|
|
<BR> unsigned long shmall; /* Maximum number of pages of
|
|
<BR> shared memory, system-wide */
|
|
};
|
|
|
|
|
|
<DT id="9"><DD>
|
|
The
|
|
<I>shmmni</I>,
|
|
|
|
<I>shmmax</I>,
|
|
|
|
and
|
|
<I>shmall</I>
|
|
|
|
settings can be changed via
|
|
<I>/proc</I>
|
|
|
|
files of the same name; see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
|
|
|
|
for details.
|
|
<DT id="10"><B>SHM_INFO</B> (Linux-specific)
|
|
|
|
<DD>
|
|
Return a
|
|
<I>shm_info</I>
|
|
|
|
structure whose fields contain information
|
|
about system resources consumed by shared memory.
|
|
This structure is defined in
|
|
<I><<A HREF="file:///usr/include/sys/shm.h">sys/shm.h</A>></I>
|
|
|
|
if the
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
feature test macro is defined:
|
|
<DT id="11"><DD>
|
|
|
|
|
|
struct shm_info {
|
|
<BR> int used_ids; /* # of currently existing
|
|
<BR> segments */
|
|
<BR> unsigned long shm_tot; /* Total number of shared
|
|
<BR> memory pages */
|
|
<BR> unsigned long shm_rss; /* # of resident shared
|
|
<BR> memory pages */
|
|
<BR> unsigned long shm_swp; /* # of swapped shared
|
|
<BR> memory pages */
|
|
<BR> unsigned long swap_attempts;
|
|
<BR> /* Unused since Linux 2.4 */
|
|
<BR> unsigned long swap_successes;
|
|
<BR> /* Unused since Linux 2.4 */
|
|
};
|
|
|
|
|
|
<DT id="12"><B>SHM_STAT</B> (Linux-specific)
|
|
|
|
<DD>
|
|
Return a
|
|
<I>shmid_ds</I>
|
|
|
|
structure as for
|
|
<B>IPC_STAT</B>.
|
|
|
|
However, the
|
|
<I>shmid</I>
|
|
|
|
argument is not a segment identifier, but instead an index into
|
|
the kernel's internal array that maintains information about
|
|
all shared memory segments on the system.
|
|
<DT id="13"><B>SHM_STAT_ANY</B> (Linux-specific, since Linux 4.17)
|
|
|
|
<DD>
|
|
Return a
|
|
<I>shmid_ds</I>
|
|
|
|
structure as for
|
|
<B>SHM_STAT</B>.
|
|
|
|
However,
|
|
<I>shm_perm.mode</I>
|
|
|
|
is not checked for read access for
|
|
<I>shmid</I>,
|
|
|
|
meaning that any user can employ this operation (just as any user may read
|
|
<I>/proc/sysvipc/shm</I>
|
|
|
|
to obtain the same information).
|
|
</DL>
|
|
<P>
|
|
|
|
The caller can prevent or allow swapping of a shared
|
|
memory segment with the following <I>cmd</I> values:
|
|
<DL COMPACT>
|
|
<DT id="14"><B>SHM_LOCK</B> (Linux-specific)
|
|
|
|
<DD>
|
|
Prevent swapping of the shared memory segment.
|
|
The caller must fault in
|
|
any pages that are required to be present after locking is enabled.
|
|
If a segment has been locked, then the (nonstandard)
|
|
<B>SHM_LOCKED</B>
|
|
|
|
flag of the
|
|
<I>shm_perm.mode</I>
|
|
|
|
field in the associated data structure retrieved by
|
|
<B>IPC_STAT</B>
|
|
|
|
will be set.
|
|
<DT id="15"><B>SHM_UNLOCK</B> (Linux-specific)
|
|
|
|
<DD>
|
|
Unlock the segment, allowing it to be swapped out.
|
|
</DL>
|
|
<P>
|
|
|
|
In kernels before 2.6.10, only a privileged process
|
|
could employ
|
|
<B>SHM_LOCK</B>
|
|
|
|
and
|
|
<B>SHM_UNLOCK</B>.
|
|
|
|
Since kernel 2.6.10, an unprivileged process can employ these operations
|
|
if its effective UID matches the owner or creator UID of the segment, and
|
|
(for
|
|
<B>SHM_LOCK</B>)
|
|
|
|
the amount of memory to be locked falls within the
|
|
<B>RLIMIT_MEMLOCK</B>
|
|
|
|
resource limit (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setrlimit">setrlimit</A></B>(2)).
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
A successful
|
|
<B>IPC_INFO</B>
|
|
|
|
or
|
|
<B>SHM_INFO</B>
|
|
|
|
operation returns the index of the highest used entry in the
|
|
kernel's internal array recording information about all
|
|
shared memory segments.
|
|
(This information can be used with repeated
|
|
<B>SHM_STAT</B>
|
|
|
|
or
|
|
<B>SHM_STAT_ANY</B>
|
|
|
|
operations to obtain information about all shared memory segments
|
|
on the system.)
|
|
A successful
|
|
<B>SHM_STAT</B>
|
|
|
|
operation returns the identifier of the shared memory segment
|
|
whose index was given in
|
|
<I>shmid</I>.
|
|
|
|
Other operations return 0 on success.
|
|
<P>
|
|
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="16"><B>EACCES</B>
|
|
|
|
<DD>
|
|
<B>IPC_STAT</B> or <B>SHM_STAT</B> is requested and
|
|
<I>shm_perm.mode</I> does not allow read access for
|
|
<I>shmid</I>,
|
|
|
|
and the calling process does not have the
|
|
<B>CAP_IPC_OWNER</B>
|
|
|
|
capability in the user namespace that governs its IPC namespace.
|
|
<DT id="17"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The argument
|
|
<I>cmd</I>
|
|
|
|
has value
|
|
<B>IPC_SET</B>
|
|
|
|
or
|
|
<B>IPC_STAT</B>
|
|
|
|
but the address pointed to by
|
|
<I>buf</I>
|
|
|
|
isn't accessible.
|
|
<DT id="18"><B>EIDRM</B>
|
|
|
|
<DD>
|
|
<I>shmid</I> points to a removed identifier.
|
|
<DT id="19"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>shmid</I> is not a valid identifier, or <I>cmd</I>
|
|
is not a valid command.
|
|
Or: for a
|
|
<B>SHM_STAT</B>
|
|
|
|
or
|
|
<B>SHM_STAT_ANY</B>
|
|
|
|
operation, the index value specified in
|
|
<I>shmid</I>
|
|
|
|
referred to an array slot that is currently unused.
|
|
<DT id="20"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
(In kernels since 2.6.9),
|
|
<B>SHM_LOCK</B>
|
|
|
|
was specified and the size of the to-be-locked segment would mean
|
|
that the total bytes in locked shared memory segments would exceed
|
|
the limit for the real user ID of the calling process.
|
|
This limit is defined by the
|
|
<B>RLIMIT_MEMLOCK</B>
|
|
|
|
soft resource limit (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setrlimit">setrlimit</A></B>(2)).
|
|
|
|
<DT id="21"><B>EOVERFLOW</B>
|
|
|
|
<DD>
|
|
<B>IPC_STAT</B> is attempted, and the GID or UID value
|
|
is too large to be stored in the structure pointed to by
|
|
<I>buf</I>.
|
|
|
|
<DT id="22"><B>EPERM</B>
|
|
|
|
<DD>
|
|
<B>IPC_SET</B> or <B>IPC_RMID</B> is attempted, and the
|
|
effective user ID of the calling process is not that of the creator
|
|
(found in
|
|
<I>shm_perm.cuid</I>),
|
|
|
|
or the owner
|
|
(found in
|
|
<I>shm_perm.uid</I>),
|
|
|
|
and the process was not privileged (Linux: did not have the
|
|
<B>CAP_SYS_ADMIN</B>
|
|
|
|
capability).
|
|
<DT id="23"><DD>
|
|
Or (in kernels before 2.6.9),
|
|
<B>SHM_LOCK</B>
|
|
|
|
or
|
|
<B>SHM_UNLOCK</B>
|
|
|
|
was specified, but the process was not privileged
|
|
(Linux: did not have the
|
|
<B>CAP_IPC_LOCK</B>
|
|
|
|
capability).
|
|
(Since Linux 2.6.9, this error can also occur if the
|
|
<B>RLIMIT_MEMLOCK</B>
|
|
|
|
is 0 and the caller is not privileged.)
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4.
|
|
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The inclusion of
|
|
<I><<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></I>
|
|
|
|
and
|
|
<I><<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>></I>
|
|
|
|
isn't required on Linux or by any version of POSIX.
|
|
However,
|
|
some old implementations required the inclusion of these header files,
|
|
and the SVID also documented their inclusion.
|
|
Applications intended to be portable to such old systems may need
|
|
to include these header files.
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>IPC_INFO</B>,
|
|
|
|
<B>SHM_STAT</B>
|
|
|
|
and
|
|
<B>SHM_INFO</B>
|
|
|
|
operations are used by the
|
|
<B><A HREF="/cgi-bin/man/man2html?1+ipcs">ipcs</A></B>(1)
|
|
|
|
program to provide information on allocated resources.
|
|
In the future, these may modified or moved to a
|
|
<I>/proc</I>
|
|
|
|
filesystem interface.
|
|
<P>
|
|
|
|
Linux permits a process to attach
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+shmat">shmat</A></B>(2))
|
|
|
|
a shared memory segment that has already been marked for deletion
|
|
using
|
|
<I>shmctl(IPC_RMID)</I>.
|
|
|
|
This feature is not available on other UNIX implementations;
|
|
portable applications should avoid relying on it.
|
|
<P>
|
|
|
|
Various fields in a <I>struct shmid_ds</I> were typed as
|
|
<I>short</I>
|
|
|
|
under Linux 2.2
|
|
and have become
|
|
<I>long</I>
|
|
|
|
under Linux 2.4.
|
|
To take advantage of this,
|
|
a recompilation under glibc-2.1.91 or later should suffice.
|
|
(The kernel distinguishes old and new calls by an
|
|
<B>IPC_64</B>
|
|
|
|
flag in
|
|
<I>cmd</I>.)
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mlock">mlock</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setrlimit">setrlimit</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+shmget">shmget</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+shmop">shmop</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sysvipc">sysvipc</A></B>(7)
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="24"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="25"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="26"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="27"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="28"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="29"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="30"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="31"><A HREF="#lbAI">SEE ALSO</A><DD>
|
|
<DT id="32"><A HREF="#lbAJ">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:34 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|