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
---
Diff to v2:
- removed unnecessary whitespace deletion
src/fdstream.c | 39 +++++++++++++++++++++++++++++++++++++--
src/fdstream.h | 11 +++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/src/fdstream.c b/src/fdstream.c
index 35f5135..192a7c4 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;
};
@@ -232,6 +238,7 @@ cleanup:
return ret;
}
+
static int
virFDStreamCloseInt(virStreamPtr st, bool streamAbort)
{
@@ -251,7 +258,7 @@ virFDStreamCloseInt(virStreamPtr st, bool streamAbort)
fdst->cb &&
(fdst->events & (VIR_STREAM_EVENT_READABLE |
VIR_STREAM_EVENT_WRITABLE))) {
- /* don't enter this function accidentaly from the callback again */
+ /* don't enter this function accidentally from the callback again */
if (fdst->abortCallbackCalled) {
virMutexUnlock(&fdst->lock);
return 0;
@@ -261,7 +268,7 @@ virFDStreamCloseInt(virStreamPtr st, bool streamAbort)
fdst->abortCallbackDispatching = true;
virMutexUnlock(&fdst->lock);
- /* call failure callback, poll does report nothing on closed fd */
+ /* call failure callback, poll reports nothing on closed fd */
(fdst->cb)(st, VIR_STREAM_EVENT_ERROR, fdst->opaque);
virMutexLock(&fdst->lock);
@@ -306,6 +313,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);
@@ -680,3 +695,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