1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

fdstream: Add internal callback on stream close

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
This commit is contained in:
Peter Krempa 2012-02-23 13:45:25 +01:00
parent 95fdc1bc2b
commit 0c4bfdda42
2 changed files with 46 additions and 1 deletions

View 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;
}

View 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
@ -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_ */