[libvirt] [PATCHv3 3/4] util: function to get local nl_pid used by netlink event service socket

Laine Stump laine at laine.org
Fri May 4 18:51:15 UTC 2012


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, 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 9b20dd4..4ab1e65 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1331,6 +1331,7 @@ virNetlinkCommand;
 virNetlinkEventAddClient;
 virNetlinkEventRemoveClient;
 virNetlinkEventServiceIsRunning;
+virNetlinkEventServiceLocalPid;
 virNetlinkEventServiceStop;
 virNetlinkEventServiceStart;
 virNetlinkShutdown;
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index df0f57a..aeca2a7 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -379,6 +379,24 @@ virNetlinkEventServiceIsRunning(void)
 }
 
 /**
+ * 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:
  *
  * start a monitor to receive netlink messages for libvirtd.
diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index c57be82..bafe8ca 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -62,6 +62,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
  */
 int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
-- 
1.7.10




More information about the libvir-list mailing list