Provides APIs for the management of events
typedef enum virEventHandleType
int virEventAddHandle (int fd,
int events,
virEventHandleCallback cb,
void * opaque,
virFreeCallback ff) typedef virEventAddHandleFunc int virEventAddHandleFunc (int fd,
int event,
virEventHandleCallback cb,
void * opaque,
virFreeCallback ff) int virEventAddTimeout (int timeout,
virEventTimeoutCallback cb,
void * opaque,
virFreeCallback ff) typedef virEventAddTimeoutFunc int virEventAddTimeoutFunc (int timeout,
virEventTimeoutCallback cb,
void * opaque,
virFreeCallback ff) typedef virEventHandleCallback void virEventHandleCallback (int watch,
int fd,
int events,
void * opaque) int virEventRegisterDefaultImpl (void) void virEventRegisterImpl (virEventAddHandleFunc addHandle,
virEventUpdateHandleFunc updateHandle,
virEventRemoveHandleFunc removeHandle,
virEventAddTimeoutFunc addTimeout,
virEventUpdateTimeoutFunc updateTimeout,
virEventRemoveTimeoutFunc removeTimeout) int virEventRemoveHandle (int watch) typedef virEventRemoveHandleFunc int virEventRemoveHandleFunc (int watch) int virEventRemoveTimeout (int timer) typedef virEventRemoveTimeoutFunc int virEventRemoveTimeoutFunc (int timer) int virEventRunDefaultImpl (void) typedef virEventTimeoutCallback void virEventTimeoutCallback (int timer,
void * opaque) void virEventUpdateHandle (int watch,
int events) typedef virEventUpdateHandleFunc void virEventUpdateHandleFunc (int watch,
int event) void virEventUpdateTimeout (int timer,
int timeout) typedef virEventUpdateTimeoutFunc void virEventUpdateTimeoutFunc (int timer,
int timeout)
virEventHandleType
¶a virEventHandleType is used similar to POLLxxx FD events, but is specific to libvirt. A client app must translate to, and from POLL events when using this construct.
enum virEventHandleType {
VIR_EVENT_HANDLE_READABLE | = | 1 (0x1; 1 << 0) | |
VIR_EVENT_HANDLE_WRITABLE | = | 2 (0x2; 1 << 1) | |
VIR_EVENT_HANDLE_ERROR | = | 4 (0x4; 1 << 2) | |
VIR_EVENT_HANDLE_HANGUP | = | 8 (0x8; 1 << 3) |
}
virEventAddHandle
¶int virEventAddHandle (int fd, int events, virEventHandleCallback cb, void * opaque, virFreeCallback ff)
Register a callback for monitoring file handle events. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
fd
must always always be a C runtime file descriptor. On Windows if the caller only has a HANDLE, the _open_osfhandle() method can be used to open an associated C runtime file descriptor for use with this API. After opening a runtime file descriptor, CloseHandle() must not be used, instead close() will close the runtime file descriptor and its original associated HANDLE.
virEventAddHandleFunc
¶typedef int (*virEventAddHandleFunc ) (int fd, int event, virEventHandleCallback cb, void * opaque, virFreeCallback ff)
Part of the EventImpl, this callback adds a file handle callback to listen for specific events. The same file handle can be registered multiple times provided the requested event sets are non-overlapping
fd
will always be a C runtime file descriptor. On Windows the _get_osfhandle() method can be used if a HANDLE is required instead.
If the opaque user data requires free'ing when the handle is unregistered, then a 2nd callback can be supplied for this purpose. This callback needs to be invoked from a clean stack. If 'ff' callbacks are invoked directly from the virEventRemoveHandleFunc they will likely deadlock in libvirt.
virEventAddTimeout
¶int virEventAddTimeout (int timeout, virEventTimeoutCallback cb, void * opaque, virFreeCallback ff)
Register a callback for a timer event. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
Setting timeout
to -1 will disable the timer. Setting timeout
to zero will cause it to fire on every event loop iteration.
virEventAddTimeoutFunc
¶typedef int (*virEventAddTimeoutFunc ) (int timeout, virEventTimeoutCallback cb, void * opaque, virFreeCallback ff)
Part of the EventImpl, this user-defined callback handles adding an event timeout.
If the opaque user data requires free'ing when the handle is unregistered, then a 2nd callback can be supplied for this purpose.
virEventHandleCallback
¶typedef void (*virEventHandleCallback ) (int watch, int fd, int events, void * opaque)
Callback for receiving file handle events. The callback will be invoked once for each event which is pending.
virEventRegisterDefaultImpl
¶int virEventRegisterDefaultImpl (void)
Registers a default event implementation based on the poll() system call. This is a generic implementation that can be used by any client application which does not have a need to integrate with an external event loop impl.
For proper event handling, it is important that the event implementation is registered before a connection to the Hypervisor is opened.
Once registered, the application has to invoke virEventRunDefaultImpl() in a loop to process events. Failure to do so may result in connections being closed unexpectedly as a result of keepalive timeout. The default event loop fully supports handle and timeout events, but only wakes up on events registered by libvirt API calls such as virEventAddHandle() or virConnectDomainEventRegisterAny().
virEventRegisterImpl
¶void virEventRegisterImpl (virEventAddHandleFunc addHandle, virEventUpdateHandleFunc updateHandle, virEventRemoveHandleFunc removeHandle, virEventAddTimeoutFunc addTimeout, virEventUpdateTimeoutFunc updateTimeout, virEventRemoveTimeoutFunc removeTimeout)
Registers an event implementation, to allow integration with an external event loop. Applications would use this to integrate with the libglib2 event loop, or libevent or the QT event loop.
For proper event handling, it is important that the event implementation is registered before a connection to the Hypervisor is opened.
Use of the virEventAddHandle() and similar APIs require that the corresponding handler is registered. Use of the virConnectDomainEventRegisterAny() and similar APIs requires that the three timeout handlers are registered. Likewise, the three timeout handlers must be registered if the remote server has been configured to send keepalive messages, or if the client intends to call virConnectSetKeepAlive(), to avoid either side from unexpectedly closing the connection due to inactivity.
If an application does not need to integrate with an existing event loop implementation, then the virEventRegisterDefaultImpl() method can be used to setup the generic libvirt implementation.
Once registered, the event loop implementation cannot be changed, and must be run continuously. Note that callbacks may remain registered for a short time even after calling virConnectClose on all open connections, so it is not safe to stop running the event loop immediately after closing the connection.
virEventRemoveHandle
¶int virEventRemoveHandle (int watch)
Unregister a callback from a file handle. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
virEventRemoveHandleFunc
¶typedef int (*virEventRemoveHandleFunc) (int watch)
Part of the EventImpl, this user-provided callback is notified when an fd is no longer being listened on.
If a virEventHandleFreeFunc was supplied when the handle was registered, it will be invoked some time during, or after this function call, when it is safe to release the user data.
virEventRemoveTimeout
¶int virEventRemoveTimeout (int timer)
Unregister a callback for a timer. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
virEventRemoveTimeoutFunc
¶typedef int (*virEventRemoveTimeoutFunc) (int timer)
Part of the EventImpl, this user-defined callback removes a timer
If a virEventTimeoutFreeFunc was supplied when the handle was registered, it will be invoked some time during, or after this function call, when it is safe to release the user data.
virEventRunDefaultImpl
¶int virEventRunDefaultImpl (void)
Run one iteration of the event loop. Applications will generally want to have a thread which invokes this method in an infinite loop. Furthermore, it is wise to set up a pipe-to-self handler (via virEventAddHandle()) or a timeout (via virEventAddTimeout()) before calling this function, as it will block forever if there are no registered events.
static bool quit; while (!quit) { if (virEventRunDefaultImpl() < 0) ...print error... }
virEventTimeoutCallback
¶typedef void (*virEventTimeoutCallback ) (int timer, void * opaque)
callback for receiving timer events
virEventUpdateHandle
¶void virEventUpdateHandle (int watch, int events)
Change event set for a monitored file handle. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
Will not fail if fd exists.
virEventUpdateHandleFunc
¶typedef void (*virEventUpdateHandleFunc) (int watch, int event)
Part of the EventImpl, this user-provided callback is notified when events to listen on change
virEventUpdateTimeout
¶void virEventUpdateTimeout (int timer, int timeout)
Change frequency for a timer. This function requires that an event loop has previously been registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().
Setting frequency to -1 will disable the timer. Setting the frequency to zero will cause it to fire on every event loop iteration.
Will not fail if timer exists.
virEventUpdateTimeoutFunc
¶typedef void (*virEventUpdateTimeoutFunc) (int timer, int timeout)
Part of the EventImpl, this user-defined callback updates an event timeout.