From c99e93758d44e7c6d62dbd3d064b6df61d1fefb2 Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Fri, 4 May 2012 13:19:51 -0400 Subject: [PATCH] util: function to get local nl_pid used by netlink event socket This value will be needed to set the src_pid when sending netlink messages to lldpad. It is part of the solution to: https://bugzilla.redhat.com/show_bug.cgi?id=816465 Note that libnl's port generation algorithm guarantees that the nl_socket_get_local_port() will always be > 0 (since it is "getpid() + (n << 22>" where n is always < 1024), so it is okay to cast the uint32_t to int (thus allowing us to use -1 as an error sentinel). --- src/libvirt_private.syms | 1 + src/util/virnetlink.c | 18 ++++++++++++++++++ src/util/virnetlink.h | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 739d8c4127..4f34d25610 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1333,6 +1333,7 @@ virNetlinkCommand; virNetlinkEventAddClient; virNetlinkEventRemoveClient; virNetlinkEventServiceIsRunning; +virNetlinkEventServiceLocalPid; virNetlinkEventServiceStop; virNetlinkEventServiceStart; virNetlinkShutdown; diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index d3cf199258..ac61ff086e 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -378,6 +378,24 @@ virNetlinkEventServiceIsRunning(void) return server != NULL; } +/** + * virNetlinkEventServiceLocalPid: + * + * Returns the nl_pid value that was used to bind() the netlink socket + * used by the netlink event service, or -1 on error (netlink + * guarantees that this value will always be > 0). + */ +int virNetlinkEventServiceLocalPid(void) +{ + if (!(server && server->netlinknh)) { + netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", + _("netlink event service not running")); + return -1; + } + return (int)nl_socket_get_local_port(server->netlinknh); +} + + /** * virNetlinkEventServiceStart: * diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index c57be821ff..bafe8caf83 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -61,6 +61,11 @@ int virNetlinkEventServiceStart(void); */ bool virNetlinkEventServiceIsRunning(void); +/** + * virNetlinkEventServiceLocalPid: returns nl_pid used to bind() netlink socket + */ +int virNetlinkEventServiceLocalPid(void); + /** * virNetlinkEventAddClient: register a callback for handling of netlink messages */