event_notifier: handle initialization failure better

Add 'initialized' field and use it to avoid touching event notifiers which are
either not initialized or if their initialization failed.

This is somewhat a hack, but it seems the less intrusive way to make
virtio code deal with event notifiers that failed initialization.

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
Message-Id: <20201217150040.906961-4-mlevitsk@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Maxim Levitsky 2020-12-17 17:00:40 +02:00 committed by Paolo Bonzini
parent dec2bb14b8
commit e34e47eb28
2 changed files with 17 additions and 0 deletions

View File

@ -24,6 +24,7 @@ struct EventNotifier {
#else #else
int rfd; int rfd;
int wfd; int wfd;
bool initialized;
#endif #endif
}; };

View File

@ -29,6 +29,7 @@ void event_notifier_init_fd(EventNotifier *e, int fd)
{ {
e->rfd = fd; e->rfd = fd;
e->wfd = fd; e->wfd = fd;
e->initialized = true;
} }
#endif #endif
@ -68,6 +69,7 @@ int event_notifier_init(EventNotifier *e, int active)
if (active) { if (active) {
event_notifier_set(e); event_notifier_set(e);
} }
e->initialized = true;
return 0; return 0;
fail: fail:
@ -78,12 +80,18 @@ fail:
void event_notifier_cleanup(EventNotifier *e) void event_notifier_cleanup(EventNotifier *e)
{ {
if (!e->initialized) {
return;
}
if (e->rfd != e->wfd) { if (e->rfd != e->wfd) {
close(e->rfd); close(e->rfd);
} }
e->rfd = -1; e->rfd = -1;
close(e->wfd); close(e->wfd);
e->wfd = -1; e->wfd = -1;
e->initialized = false;
} }
int event_notifier_get_fd(const EventNotifier *e) int event_notifier_get_fd(const EventNotifier *e)
@ -96,6 +104,10 @@ int event_notifier_set(EventNotifier *e)
static const uint64_t value = 1; static const uint64_t value = 1;
ssize_t ret; ssize_t ret;
if (!e->initialized) {
return -1;
}
do { do {
ret = write(e->wfd, &value, sizeof(value)); ret = write(e->wfd, &value, sizeof(value));
} while (ret < 0 && errno == EINTR); } while (ret < 0 && errno == EINTR);
@ -113,6 +125,10 @@ int event_notifier_test_and_clear(EventNotifier *e)
ssize_t len; ssize_t len;
char buffer[512]; char buffer[512];
if (!e->initialized) {
return 0;
}
/* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
value = 0; value = 0;
do { do {