man-pages/man7/sock_diag.7.html
2021-03-31 01:06:50 +01:00

1102 lines
33 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SOCK_DIAG</TITLE>
</HEAD><BODY>
<H1>SOCK_DIAG</H1>
Section: Linux Programmer's Manual (7)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sock_diag - obtaining information about sockets
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/linux/sock_diag.h">linux/sock_diag.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/linux/unix_diag.h">linux/unix_diag.h</A>&gt;</B> /* for UNIX domain sockets */
<B>#include &lt;<A HREF="file:///usr/include/linux/inet_diag.h">linux/inet_diag.h</A>&gt;</B> /* for IPv4 and IPv6 sockets */
<B>diag_socket = socket(AF_NETLINK, </B><I>socket_type</I><B>, NETLINK_SOCK_DIAG);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The sock_diag netlink subsystem provides a mechanism for obtaining
information about sockets of various address families from the kernel.
This subsystem can be used to obtain information about individual
sockets or request a list of sockets.
<P>
In the request, the caller can specify additional information it would
like to obtain about the socket, for example, memory information or
information specific to the address family.
<P>
When requesting a list of sockets, the caller can specify filters that
would be applied by the kernel to select a subset of sockets to report.
For now, there is only the ability to filter sockets by state (connected,
listening, and so on.)
<P>
Note that sock_diag reports only those sockets that have a name;
that is, either sockets bound explicitly with
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
or sockets that were automatically bound to an address (e.g., by
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)).
This is the same set of sockets that is available via
<I>/proc/net/unix</I>,
<I>/proc/net/tcp</I>,
<I>/proc/net/udp</I>,
and so on.
<A NAME="lbAE">&nbsp;</A>
<H3>Request</H3>
The request starts with a
<I>struct nlmsghdr</I>
header described in
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7)
with
<I>nlmsg_type</I>
field set to
<B>SOCK_DIAG_BY_FAMILY</B>.
It is followed by a header specific to the address family that starts with
a common part shared by all address families:
<P>
struct sock_diag_req {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;sdiag_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;sdiag_protocol;
};
<P>
The fields of this structure are as follows:
<DL COMPACT>
<DT id="1"><I>sdiag_family</I>
<DD>
An address family.
It should be set to the appropriate
<B>AF_*</B>
constant.
<DT id="2"><I>sdiag_protocol</I>
<DD>
Depends on
<I>sdiag_family</I>.
It should be set to the appropriate
<B>IPPROTO_*</B>
constant for
<B>AF_INET</B>
and
<B>AF_INET6</B>,
and to 0 otherwise.
</DL>
<P>
If the
<I>nlmsg_flags</I>
field of the
<I>struct nlmsghdr</I>
header has the
<B>NLM_F_DUMP</B>
flag set, it means that a list of sockets is being requested;
otherwise it is a query about an individual socket.
<A NAME="lbAF">&nbsp;</A>
<H3>Response</H3>
The response starts with a
<I>struct nlmsghdr</I>
header and is followed by an array of objects specific to the address family.
The array is to be accessed with the standard
<B>NLMSG_*</B>
macros from the
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3)
API.
<P>
Each object is the NLA (netlink attributes) list that is to be accessed
with the
<B>RTA_*</B>
macros from
<B><A HREF="/cgi-bin/man/man2html?3+rtnetlink">rtnetlink</A></B>(3)
API.
<A NAME="lbAG">&nbsp;</A>
<H3>UNIX domain sockets</H3>
For UNIX domain sockets the request is represented in the following structure:
<P>
struct unix_diag_req {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;sdiag_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;sdiag_protocol;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u16&nbsp;&nbsp;&nbsp;pad;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_states;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_ino;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_show;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_cookie[2];
};
<P>
The fields of this structure are as follows:
<DL COMPACT>
<DT id="3"><I>sdiag_family</I>
<DD>
The address family; it should be set to
<B>AF_UNIX</B>.
</DL>
<P>
<I>sdiag_protocol</I>
<DL COMPACT>
<DT id="4">
<DD>
<I>pad</I>
These fields should be set to 0.
<DT id="5"><I>udiag_states</I>
<DD>
This is a bit mask that defines a filter of sockets states.
Only those sockets whose states are in this mask will be reported.
Ignored when querying for an individual socket.
Supported values are:
</DL>
<P>
<DL COMPACT><DT id="6"><DD>
1 &lt;&lt;
<B>TCP_ESTABLISHED</B>
<P>
1 &lt;&lt;
<B>TCP_LISTEN</B>
</DL>
<DL COMPACT>
<DT id="7"><I>udiag_ino</I>
<DD>
This is an inode number when querying for an individual socket.
Ignored when querying for a list of sockets.
<DT id="8"><I>udiag_show</I>
<DD>
This is a set of flags defining what kind of information to report.
Each requested kind of information is reported back as a netlink
attribute as described below:
<DL COMPACT><DT id="9"><DD>
<DL COMPACT>
<DT id="10"><B>UDIAG_SHOW_NAME</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_NAME</B>.
The payload associated with this attribute is the pathname to which
the socket was bound (a sequence of bytes up to
<B>UNIX_PATH_MAX</B>
length).
<DT id="11"><B>UDIAG_SHOW_VFS</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_VFS</B>.
The payload associated with this attribute is represented in the following
structure:
<DT id="12"><DD>
struct unix_diag_vfs {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;udiag_vfs_dev;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;udiag_vfs_ino;
};
<DT id="13"><DD>
The fields of this structure are as follows:
<DL COMPACT><DT id="14"><DD>
<DL COMPACT>
<DT id="15"><I>udiag_vfs_dev</I>
<DD>
The device number of the corresponding on-disk socket inode.
<DT id="16"><I>udiag_vfs_ino</I>
<DD>
The inode number of the corresponding on-disk socket inode.
</DL>
</DL>
<DT id="17"><B>UDIAG_SHOW_PEER</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_PEER</B>.
The payload associated with this attribute is a __u32 value
which is the peer's inode number.
This attribute is reported for connected sockets only.
<DT id="18"><B>UDIAG_SHOW_ICONS</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_ICONS</B>.
The payload associated with this attribute is an array of __u32 values
which are inode numbers of sockets that has passed the
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
call, but hasn't been processed with
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
yet.
This attribute is reported for listening sockets only.
<DT id="19"><B>UDIAG_SHOW_RQLEN</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_RQLEN</B>.
The payload associated with this attribute is represented in the following
structure:
<DT id="20"><DD>
struct unix_diag_rqlen {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;udiag_rqueue;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;udiag_wqueue;
};
<DT id="21"><DD>
The fields of this structure are as follows:
<DL COMPACT><DT id="22"><DD>
<DL COMPACT>
<DT id="23"><I>udiag_rqueue</I>
<DD>
For listening sockets:
the number of pending connections.
The length of the array associated with the
<B>UNIX_DIAG_ICONS</B>
response attribute is equal to this value.
<DT id="24"><DD>
For established sockets:
the amount of data in incoming queue.
<DT id="25"><I>udiag_wqueue</I>
<DD>
For listening sockets:
the backlog length which equals to the value passed as the second argument to
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2).
<DT id="26"><DD>
For established sockets:
the amount of memory available for sending.
</DL>
</DL>
<DT id="27"><B>UDIAG_SHOW_MEMINFO</B>
<DD>
The attribute reported in answer to this request is
<B>UNIX_DIAG_MEMINFO</B>.
The payload associated with this attribute is an array of __u32 values
described below in the subsection &quot;Socket memory information&quot;.
</DL>
<P>
The following attributes are reported back without any specific request:
<DL COMPACT>
<DT id="28"><B>UNIX_DIAG_SHUTDOWN</B>
<DD>
The payload associated with this attribute is __u8 value which represents
bits of
<B><A HREF="/cgi-bin/man/man2html?2+shutdown">shutdown</A></B>(2)
state.
</DL>
</DL>
<DT id="29"><I>udiag_cookie</I>
<DD>
This is an array of opaque identifiers that could be used along with
<I>udiag_ino</I>
to specify an individual socket.
It is ignored when querying for a list
of sockets, as well as when all its elements are set to -1.
</DL>
<P>
The response to a query for UNIX domain sockets is represented as an array of
<P>
struct unix_diag_msg {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;udiag_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;udiag_type;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;udiag_state;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;pad;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_ino;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;udiag_cookie[2];
};
<P>
followed by netlink attributes.
<P>
The fields of this structure are as follows:
<DL COMPACT>
<DT id="30"><I>udiag_family</I>
<DD>
This field has the same meaning as in
<I>struct unix_diag_req</I>.
<DT id="31"><I>udiag_type</I>
<DD>
This is set to one of
<B>SOCK_PACKET</B>,
<B>SOCK_STREAM</B>,
or
<B>SOCK_SEQPACKET</B>.
<DT id="32"><I>udiag_state</I>
<DD>
This is set to one of
<B>TCP_LISTEN</B>
or
<B>TCP_ESTABLISHED</B>.
<DT id="33"><I>pad</I>
<DD>
This field is set to 0.
<DT id="34"><I>udiag_ino</I>
<DD>
This is the socket inode number.
<DT id="35"><I>udiag_cookie</I>
<DD>
This is an array of opaque identifiers that could be used in subsequent
queries.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H3>IPv4 and IPv6 sockets</H3>
For IPv4 and IPv6 sockets,
the request is represented in the following structure:
<P>
struct inet_diag_req_v2 {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;sdiag_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;sdiag_protocol;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;idiag_ext;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;pad;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_states;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;inet_diag_sockid&nbsp;id;
};
<P>
where
<I>struct inet_diag_sockid</I>
is defined as follows:
<P>
struct inet_diag_sockid {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__be16&nbsp;&nbsp;idiag_sport;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__be16&nbsp;&nbsp;idiag_dport;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__be32&nbsp;&nbsp;idiag_src[4];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__be32&nbsp;&nbsp;idiag_dst[4];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_if;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_cookie[2];
};
<P>
The fields of
<I>struct inet_diag_req_v2</I>
are as follows:
<DL COMPACT>
<DT id="36"><I>sdiag_family</I>
<DD>
This should be set to either
<B>AF_INET</B>
or
<B>AF_INET6</B>
for IPv4 or IPv6 sockets respectively.
<DT id="37"><I>sdiag_protocol</I>
<DD>
This should be set to one of
<B>IPPROTO_TCP</B>,
<B>IPPROTO_UDP</B>,
or
<B>IPPROTO_UDPLITE</B>.
<DT id="38"><I>idiag_ext</I>
<DD>
This is a set of flags defining what kind of extended information to report.
Each requested kind of information is reported back as a netlink attribute
as described below:
<DL COMPACT><DT id="39"><DD>
<DL COMPACT>
<DT id="40"><B>INET_DIAG_TOS</B>
<DD>
The payload associated with this attribute is a __u8 value
which is the TOS of the socket.
<DT id="41"><B>INET_DIAG_TCLASS</B>
<DD>
The payload associated with this attribute is a __u8 value
which is the TClass of the socket.
IPv6 sockets only.
For LISTEN and CLOSE sockets, this is followed by
<B>INET_DIAG_SKV6ONLY</B>
attribute with associated __u8 payload value meaning whether the socket
is IPv6-only or not.
<DT id="42"><B>INET_DIAG_MEMINFO</B>
<DD>
The payload associated with this attribute is represented in the following
structure:
<DT id="43"><DD>
struct inet_diag_meminfo {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;idiag_rmem;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;idiag_wmem;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;idiag_fmem;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;idiag_tmem;
};
<DT id="44"><DD>
The fields of this structure are as follows:
<DL COMPACT><DT id="45"><DD>
<DL COMPACT>
<DT id="46"><I>idiag_rmem</I>
<DD>
The amount of data in the receive queue.
<DT id="47"><I>idiag_wmem</I>
<DD>
The amount of data that is queued by TCP but not yet sent.
<DT id="48"><I>idiag_fmem</I>
<DD>
The amount of memory scheduled for future use (TCP only).
<DT id="49"><I>idiag_tmem</I>
<DD>
The amount of data in send queue.
</DL>
</DL>
<DT id="50"><B>INET_DIAG_SKMEMINFO</B>
<DD>
The payload associated with this attribute is an array of __u32 values
described below in the subsection &quot;Socket memory information&quot;.
<DT id="51"><B>INET_DIAG_INFO</B>
<DD>
The payload associated with this attribute is specific to the address family.
For TCP sockets, it is an object of type
<I>struct tcp_info</I>.
<DT id="52"><B>INET_DIAG_CONG</B>
<DD>
The payload associated with this attribute is a string that describes the
congestion control algorithm used.
For TCP sockets only.
</DL>
</DL>
<DT id="53"><I>pad</I>
<DD>
This should be set to 0.
<DT id="54"><I>idiag_states</I>
<DD>
This is a bit mask that defines a filter of socket states.
Only those sockets whose states are in this mask will be reported.
Ignored when querying for an individual socket.
<DT id="55"><I>id</I>
<DD>
This is a socket ID object that is used in dump requests, in queries
about individual sockets, and is reported back in each response.
Unlike UNIX domain sockets, IPv4 and IPv6 sockets are identified
using addresses and ports.
All values are in network byte order.
</DL>
<P>
The fields of
<I>struct inet_diag_sockid</I>
are as follows:
<DL COMPACT>
<DT id="56"><I>idiag_sport</I>
<DD>
The source port.
<DT id="57"><I>idiag_dport</I>
<DD>
The destination port.
<DT id="58"><I>idiag_src</I>
<DD>
The source address.
<DT id="59"><I>idiag_dst</I>
<DD>
The destination address.
<DT id="60"><I>idiag_if</I>
<DD>
The interface number the socket is bound to.
<DT id="61"><I>idiag_cookie</I>
<DD>
This is an array of opaque identifiers that could be used along with
other fields of this structure to specify an individual socket.
It is ignored when querying for a list of sockets, as well as
when all its elements are set to -1.
</DL>
<P>
The response to a query for IPv4 or IPv6 sockets is represented as an array of
<P>
struct inet_diag_msg {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;idiag_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;idiag_state;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;idiag_timer;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u8&nbsp;&nbsp;&nbsp;&nbsp;idiag_retrans;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;inet_diag_sockid&nbsp;id;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_expires;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_rqueue;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_wqueue;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_uid;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;&nbsp;&nbsp;idiag_inode;
};
<P>
followed by netlink attributes.
<P>
The fields of this structure are as follows:
<DL COMPACT>
<DT id="62"><I>idiag_family</I>
<DD>
This is the same field as in
<I>struct inet_diag_req_v2</I>.
<DT id="63"><I>idiag_state</I>
<DD>
This denotes socket state as in
<I>struct inet_diag_req_v2</I>.
<DT id="64"><I>idiag_timer</I>
<DD>
For TCP sockets, this field describes the type of timer that is currently
active for the socket.
It is set to one of the following constants:
<DT id="65"><DD>
<DL COMPACT><DT id="66"><DD>
<DL COMPACT>
<DT id="67"><B>0</B>
<DD>
no timer is active
<DT id="68"><B>1</B>
<DD>
a retransmit timer
<DT id="69"><B>2</B>
<DD>
a keep-alive timer
<DT id="70"><B>3</B>
<DD>
a TIME_WAIT timer
<DT id="71"><B>4</B>
<DD>
a zero window probe timer
</DL>
</DL>
<DT id="72"><DD>
For non-TCP sockets, this field is set to 0.
<DT id="73"><I>idiag_retrans</I>
<DD>
For
<I>idiag_timer</I>
values 1, 2, and 4, this field contains the number of retransmits.
For other
<I>idiag_timer</I>
values, this field is set to 0.
<DT id="74"><I>idiag_expires</I>
<DD>
For TCP sockets that have an active timer, this field describes its expiration
time in milliseconds.
For other sockets, this field is set to 0.
<DT id="75"><I>idiag_rqueue</I>
<DD>
For listening sockets:
the number of pending connections.
<DT id="76"><DD>
For other sockets:
the amount of data in the incoming queue.
<DT id="77"><I>idiag_wqueue</I>
<DD>
For listening sockets:
the backlog length.
<DT id="78"><DD>
For other sockets:
the amount of memory available for sending.
<DT id="79"><I>idiag_uid</I>
<DD>
This is the socket owner UID.
<DT id="80"><I>idiag_inode</I>
<DD>
This is the socket inode number.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H3>Socket memory information</H3>
The payload associated with
<B>UNIX_DIAG_MEMINFO</B>
and
<B>INET_DIAG_SKMEMINFO</B>
netlink attributes is an array of the following __u32 values:
<DL COMPACT>
<DT id="81"><B>SK_MEMINFO_RMEM_ALLOC</B>
<DD>
The amount of data in receive queue.
<DT id="82"><B>SK_MEMINFO_RCVBUF</B>
<DD>
The receive socket buffer as set by
<B>SO_RCVBUF</B>.
<DT id="83"><B>SK_MEMINFO_WMEM_ALLOC</B>
<DD>
The amount of data in send queue.
<DT id="84"><B>SK_MEMINFO_SNDBUF</B>
<DD>
The send socket buffer as set by
<B>SO_SNDBUF</B>.
<DT id="85"><B>SK_MEMINFO_FWD_ALLOC</B>
<DD>
The amount of memory scheduled for future use (TCP only).
<DT id="86"><B>SK_MEMINFO_WMEM_QUEUED</B>
<DD>
The amount of data queued by TCP, but not yet sent.
<DT id="87"><B>SK_MEMINFO_OPTMEM</B>
<DD>
The amount of memory allocated for the socket's service needs (e.g., socket
filter).
<DT id="88"><B>SK_MEMINFO_BACKLOG</B>
<DD>
The amount of packets in the backlog (not yet processed).
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H2>VERSIONS</H2>
<B>NETLINK_INET_DIAG</B>
was introduced in Linux 2.6.14 and supported
<B>AF_INET</B>
and
<B>AF_INET6</B>
sockets only.
In Linux 3.3, it was renamed to
<B>NETLINK_SOCK_DIAG</B>
and extended to support
<B>AF_UNIX</B>
sockets.
<P>
<B>UNIX_DIAG_MEMINFO</B>
and
<B>INET_DIAG_SKMEMINFO</B>
were introduced in Linux 3.6.
<A NAME="lbAK">&nbsp;</A>
<H2>CONFORMING TO</H2>
The NETLINK_SOCK_DIAG API is Linux-specific.
<A NAME="lbAL">&nbsp;</A>
<H2>EXAMPLE</H2>
The following example program prints inode number, peer's inode number,
and name of all UNIX domain sockets in the current namespace.
<P>
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/un.h">sys/un.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/netlink.h">linux/netlink.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/rtnetlink.h">linux/rtnetlink.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/sock_diag.h">linux/sock_diag.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/unix_diag.h">linux/unix_diag.h</A>&gt;
<P>
static int
send_query(int fd)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sockaddr_nl&nbsp;nladdr&nbsp;=&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.nl_family&nbsp;=&nbsp;AF_NETLINK
<BR>&nbsp;&nbsp;&nbsp;&nbsp;};
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct
<BR>&nbsp;&nbsp;&nbsp;&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;nlmsghdr&nbsp;nlh;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbs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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;sendmsg&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<P>
static int
print_diag(const struct unix_diag_msg *diag, unsigned int len)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(len&nbsp;&lt;&nbsp;NLMSG_LENGTH(sizeof(*diag)))&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fputs(&quot;short&nbsp;response\n&quot;,&nbsp;stderr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(diag-&gt;udiag_family&nbsp;!=&nbsp;AF_UNIX)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;unexpected&nbsp;family&nbsp;%u\n&quot;,&nbsp;diag-&gt;udiag_family);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rtattr&nbsp;*attr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;rta_len&nbsp;=&nbsp;len&nbsp;-&nbsp;NLMSG_LENGTH(sizeof(*diag));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;peer&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;path_len&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;path[sizeof(((struct&nbsp;sockaddr_un&nbsp;*)&nbsp;0)-&gt;sun_path)&nbsp;+&nbsp;1];
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(attr&nbsp;=&nbsp;(struct&nbsp;rtattr&nbsp;*)&nbsp;(diag&nbsp;+&nbsp;1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RTA_OK(attr,&nbsp;rta_len);&nbsp;attr&nbsp;=&nbsp;RTA_NEXT(attr,&nbsp;rta_len))&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(attr-&gt;rta_type)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;UNIX_DIAG_NAME:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!path_len)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path_len&nbsp;=&nbsp;RTA_PAYLOAD(attr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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p;*)&nbsp;RTA_DATA(attr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;inode=%u&quot;,&nbsp;diag-&gt;udiag_ino);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(peer)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;,&nbsp;peer=%u&quot;,&nbsp;peer);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(path_len)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;,&nbsp;name=%s%s&quot;,&nbsp;*path&nbsp;?&nbsp;&quot;&quot;&nbsp;:&nbsp;&quot;@&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*path&nbsp;?&nbsp;path&nbsp;:&nbsp;path&nbsp;+&nbsp;1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;putchar('\n');
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
}
<P>
static int
receive_responses(int fd)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;buf[8192&nbsp;/&nbsp;sizeof(long)];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sockaddr_nl&nbsp;nladdr&nbsp;=&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.nl_family&nbsp;=&nbsp;AF_NETLINK
<BR>&nbs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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;socket&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;ret&nbsp;=&nbsp;send_query(fd)&nbsp;||&nbsp;receive_responses(fd);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;close(fd);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ret;
}
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+rtnetlink">rtnetlink</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7)
<A NAME="lbAN">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="89"><A HREF="#lbAB">NAME</A><DD>
<DT id="90"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="91"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="92"><A HREF="#lbAE">Request</A><DD>
<DT id="93"><A HREF="#lbAF">Response</A><DD>
<DT id="94"><A HREF="#lbAG">UNIX domain sockets</A><DD>
<DT id="95"><A HREF="#lbAH">IPv4 and IPv6 sockets</A><DD>
<DT id="96"><A HREF="#lbAI">Socket memory information</A><DD>
</DL>
<DT id="97"><A HREF="#lbAJ">VERSIONS</A><DD>
<DT id="98"><A HREF="#lbAK">CONFORMING TO</A><DD>
<DT id="99"><A HREF="#lbAL">EXAMPLE</A><DD>
<DT id="100"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="101"><A HREF="#lbAN">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:06:09 GMT, March 31, 2021
</BODY>
</HTML>