1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-03 05:18:29 +03:00

dmeventd: nicer error path for reading pipe

When _daemon_read()/_client_read() fails during the read,
ensure memory allocated withing function is also release here
(so caller does not need to care). Also improve code readbility a bit
a for same functionality use more similar code.
This commit is contained in:
Zdenek Kabelac 2020-01-29 16:41:28 +01:00
parent 91d16fc049
commit 4791d0f035
2 changed files with 39 additions and 35 deletions

View File

@ -1485,37 +1485,34 @@ static int _client_read(struct dm_event_fifos *fifos,
t.tv_usec = 0; t.tv_usec = 0;
ret = select(fifos->client + 1, &fds, NULL, NULL, &t); ret = select(fifos->client + 1, &fds, NULL, NULL, &t);
if (!ret && !bytes) /* nothing to read */ if (!ret && bytes)
return 0; continue; /* trying to finish read */
if (!ret) /* trying to finish read */ if (ret <= 0) /* nothing to read */
continue; goto bad;
if (ret < 0) /* error */
return 0;
ret = read(fifos->client, buf + bytes, size - bytes); ret = read(fifos->client, buf + bytes, size - bytes);
bytes += ret > 0 ? ret : 0; bytes += ret > 0 ? ret : 0;
if (header && (bytes == 2 * sizeof(uint32_t))) { if (!msg->data && (bytes == 2 * sizeof(uint32_t))) {
msg->cmd = ntohl(header[0]); msg->cmd = ntohl(header[0]);
size = msg->size = ntohl(header[1]);
bytes = 0; bytes = 0;
if (!size)
break; /* No data -> error */ if (!(size = msg->size = ntohl(header[1])))
buf = msg->data = malloc(msg->size); break;
if (!buf)
break; /* No mem -> error */ if (!(buf = msg->data = malloc(msg->size)))
header = 0; goto bad;
} }
} }
if (bytes != size) { if (bytes == size)
free(msg->data); return 1;
msg->data = NULL;
return 0;
}
return 1; bad:
free(msg->data);
msg->data = NULL;
return 0;
} }
/* /*

View File

@ -237,16 +237,16 @@ static int _daemon_read(struct dm_event_fifos *fifos,
ret = select(fifos->server + 1, &fds, NULL, NULL, &tval); ret = select(fifos->server + 1, &fds, NULL, NULL, &tval);
if (ret < 0 && errno != EINTR) { if (ret < 0 && errno != EINTR) {
log_error("Unable to read from event server."); log_error("Unable to read from event server.");
return 0; goto bad;
} }
if ((ret == 0) && (i > 4) && !bytes) { if ((ret == 0) && (i > 4) && !bytes) {
log_error("No input from event server."); log_error("No input from event server.");
return 0; goto bad;
} }
} }
if (ret < 1) { if (ret < 1) {
log_error("Unable to read from event server."); log_error("Unable to read from event server.");
return 0; goto bad;
} }
ret = read(fifos->server, buf + bytes, size); ret = read(fifos->server, buf + bytes, size);
@ -255,25 +255,32 @@ static int _daemon_read(struct dm_event_fifos *fifos,
continue; continue;
log_error("Unable to read from event server."); log_error("Unable to read from event server.");
return 0; goto bad;
} }
bytes += ret; bytes += ret;
if (header && (bytes == 2 * sizeof(uint32_t))) { if (!msg->data && (bytes == 2 * sizeof(uint32_t))) {
msg->cmd = ntohl(header[0]); msg->cmd = ntohl(header[0]);
msg->size = ntohl(header[1]);
buf = msg->data = malloc(msg->size);
size = msg->size;
bytes = 0; bytes = 0;
header = 0;
if (!(size = msg->size = ntohl(header[1])))
break;
if (!(buf = msg->data = malloc(msg->size))) {
log_error("Unable to allocate message data.");
return 0;
}
} }
} }
if (bytes != size) { if (bytes == size)
free(msg->data); return 1;
msg->data = NULL;
} bad:
return bytes == size; free(msg->data);
msg->data = NULL;
return 0;
} }
/* Write message to daemon. */ /* Write message to daemon. */