#define DEBUG_RAW_IO 0
struct _qemuMonitor {
- virMutex lock;
+ virMutex lock; /* also used to protect fd */
virCond notify;
int refs;
* the next monitor msg */
int lastErrno;
- /* If the monitor EOF callback is currently active (stops more commands being run) */
- unsigned eofcb: 1;
- /* If the monitor is in process of shutting down */
- unsigned closed: 1;
-
unsigned json: 1;
};
}
+/* Call this function while holding the monitor lock. */
static int
qemuMonitorIOWriteWithFD(qemuMonitorPtr mon,
const char *data,
return ret;
}
-/* Called when the monitor is able to write data */
+/*
+ * Called when the monitor is able to write data
+ * Call this function while holding the monitor lock.
+ */
static int
qemuMonitorIOWrite(qemuMonitorPtr mon)
{
/*
* Called when the monitor has incoming data to read
+ * Call this function while holding the monitor lock.
*
* Returns -1 on error, or number of bytes read
*/
qemuMonitorPtr mon = opaque;
int quit = 0, failed = 0;
+ /* lock access to the monitor and protect fd */
qemuMonitorLock(mon);
qemuMonitorRef(mon);
#if DEBUG_IO
VIR_DEBUG("mon=%p", mon);
qemuMonitorLock(mon);
- if (!mon->closed) {
+
+ if (mon->fd >= 0) {
if (mon->watch)
virEventRemoveHandle(mon->watch);
- if (mon->fd != -1)
- close(mon->fd);
- /* NB: ordinarily one might immediately set mon->watch to -1
- * and mon->fd to -1, but there may be a callback active
- * that is still relying on these fields being valid. So
- * we merely close them, but not clear their values and
- * use this explicit 'closed' flag to track this state */
- mon->closed = 1;
+ VIR_FORCE_CLOSE(mon->fd);
}
if (qemuMonitorUnref(mon) > 0)
{
int ret = -1;
- if (mon->eofcb) {
- msg->lastErrno = EIO;
- return -1;
- }
-
mon->msg = msg;
qemuMonitorUpdateWatch(mon);
if (safewrite (fd, doc, strlen (doc)) == -1) {
vshError(ctl, _("write: %s: failed to write to temporary file: %s"),
ret, strerror(errno));
- close (fd);
+ VIR_FORCE_CLOSE(fd);
unlink (ret);
VIR_FREE(ret);
return NULL;
}
- if (close (fd) == -1) {
+ if (VIR_CLOSE(fd) < 0) {
vshError(ctl, _("close: %s: failed to write or close temporary file: %s"),
ret, strerror(errno));
unlink (ret);