[PATCH v2 4/6] fdstream: Add internal callback on stream close

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds another callback to a FDstream object. The original
callback is used by the daemon stream driver to handle events.

This callback is called if and only if the stream is about to be closed.
This might be used to handle cleanup steps after a fdstream exits. This
will be used later on in ensuring mutualy exclusive access to consoles.

* src/fdstream.c:
        - emit the callback, when stream is being closed
        - add data structures needed to handle the callback
        - add function to register callback
* src/fdstream.h:
        - define function prototypes for the callback
---
 src/fdstream.c |   39 ++++++++++++++++++++++++++++++++++-----
 src/fdstream.h |   11 +++++++++++
 2 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/src/fdstream.c b/src/fdstream.c
index 35f5135..18d7b29 100644
--- a/src/fdstream.c
+++ b/src/fdstream.c
@@ -67,6 +67,12 @@ struct virFDStreamData {
     bool abortCallbackCalled;
     bool abortCallbackDispatching;

+    /* internal callback, as the regular one (from generic streams) gets
+     * eaten up by the server stream driver */
+    virFDStreamInternalCloseCb icbCb;
+    virFDStreamInternalCloseCbFreeOpaque icbFreeOpaque;
+    void *icbOpaque;
+
     virMutex lock;
 };

@@ -183,7 +189,6 @@ static void virFDStreamCallbackFree(void *opaque)
     virStreamFree(st);
 }

-
 static int
 virFDStreamAddCallback(virStreamPtr st,
                        int events,
@@ -306,6 +311,14 @@ virFDStreamCloseInt(virStreamPtr st, bool streamAbort)

     st->privateData = NULL;

+    /* call the internal stream closing callback */
+    if (fdst->icbCb) {
+        /* the mutex is not accessible anymore, as private data are null */
+        (fdst->icbCb)(st, fdst->icbOpaque);
+        if (fdst->icbFreeOpaque && fdst->icbOpaque)
+            (fdst->icbFreeOpaque)(fdst->icbOpaque);
+    }
+
     if (fdst->dispatching) {
         fdst->closed = true;
         virMutexUnlock(&fdst->lock);
@@ -381,7 +394,6 @@ retry:
     return ret;
 }

-
 static int virFDStreamRead(virStreamPtr st, char *bytes, size_t nbytes)
 {
     struct virFDStreamData *fdst = st->privateData;
@@ -431,7 +443,6 @@ retry:
     return ret;
 }

-
 static virStreamDriver virFDStreamDrv = {
     .streamSend = virFDStreamWrite,
     .streamRecv = virFDStreamRead,
@@ -479,14 +490,12 @@ static int virFDStreamOpenInternal(virStreamPtr st,
     return 0;
 }

-
 int virFDStreamOpen(virStreamPtr st,
                     int fd)
 {
     return virFDStreamOpenInternal(st, fd, NULL, -1, 0);
 }

-
 #if HAVE_SYS_UN_H
 int virFDStreamConnectUNIX(virStreamPtr st,
                            const char *path,
@@ -680,3 +689,23 @@ int virFDStreamCreateFile(virStreamPtr st,
                                        offset, length,
                                        oflags | O_CREAT, mode);
 }
+
+int virFDStreamSetInternalCloseCb(virStreamPtr st,
+                                  virFDStreamInternalCloseCb cb,
+                                  void *opaque,
+                                  virFDStreamInternalCloseCbFreeOpaque fcb)
+{
+    struct virFDStreamData *fdst = st->privateData;
+
+    virMutexLock(&fdst->lock);
+
+    if (fdst->icbOpaque && fdst->icbFreeOpaque)
+        (fdst->icbFreeOpaque)(fdst->icbOpaque);
+
+    fdst->icbCb = cb;
+    fdst->icbOpaque = opaque;
+    fdst->icbFreeOpaque = fcb;
+
+    virMutexUnlock(&fdst->lock);
+    return 0;
+}
diff --git a/src/fdstream.h b/src/fdstream.h
index f9edb23..11acb90 100644
--- a/src/fdstream.h
+++ b/src/fdstream.h
@@ -26,6 +26,13 @@
 # include "internal.h"
 # include "command.h"

+/* internal callback, the generic one is used up by daemon stream driver */
+/* the close callback is called with fdstream private data locked */
+typedef void (*virFDStreamInternalCloseCb)(virStreamPtr st, void *opaque);
+
+typedef void (*virFDStreamInternalCloseCbFreeOpaque)(void *opaque);
+
+
 int virFDStreamOpen(virStreamPtr st,
                     int fd);

@@ -45,4 +52,8 @@ int virFDStreamCreateFile(virStreamPtr st,
                           int oflags,
                           mode_t mode);

+int virFDStreamSetInternalCloseCb(virStreamPtr st,
+                                  virFDStreamInternalCloseCb cb,
+                                  void *opaque,
+                                  virFDStreamInternalCloseCbFreeOpaque fcb);
 #endif /* __VIR_FDSTREAM_H_ */
-- 
1.7.3.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]