]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
fdstream: Add internal callback on stream close
authorPeter Krempa <pkrempa@redhat.com>
Thu, 23 Feb 2012 12:45:25 +0000 (13:45 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 27 Feb 2012 14:05:17 +0000 (15:05 +0100)
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 mutually 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
src/fdstream.h

index 39a875364cb2fb369f6d706184804d8451e159f6..32d386dce6d9d24a22b390e19bd89f44cb3a095e 100644 (file)
@@ -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;
 };
 
@@ -313,6 +319,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 is null */
+        (fdst->icbCb)(st, fdst->icbOpaque);
+        if (fdst->icbFreeOpaque)
+            (fdst->icbFreeOpaque)(fdst->icbOpaque);
+    }
+
     if (fdst->dispatching) {
         fdst->closed = true;
         virMutexUnlock(&fdst->lock);
@@ -687,3 +701,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->icbFreeOpaque)
+        (fdst->icbFreeOpaque)(fdst->icbOpaque);
+
+    fdst->icbCb = cb;
+    fdst->icbOpaque = opaque;
+    fdst->icbFreeOpaque = fcb;
+
+    virMutexUnlock(&fdst->lock);
+    return 0;
+}
index f9edb23e9ea56a9ea126493af1810704e3156fb6..f08cab9d63eb7fa4cb070b809d4130c0c864aa25 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * fdstream.h: generic streams impl for file descriptors
  *
- * Copyright (C) 2009-2011 Red Hat, Inc.
+ * Copyright (C) 2009-2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 # 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_ */