Move the connection type into the one_way struct. Seem
like a more obvious place for it next to the union.
Also it will help prevent issues like in qb_ipc_us_recv()
where there is a rather dodgy access of ctl->sent.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/ipc_int.h | 2 +-
lib/ipc_sysv_mq.c | 1 -
lib/ipc_us.c | 30 ++++++++++++++++++------------
lib/ipcc.c | 14 +++++++++-----
lib/ipcs.c | 6 ++++++
5 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/lib/ipc_int.h b/lib/ipc_int.h
index b4bdebc..16bc5c7 100644
--- a/lib/ipc_int.h
+++ b/lib/ipc_int.h
@@ -73,6 +73,7 @@ struct qb_ipcc_connection;
struct qb_ipc_one_way {
size_t max_msg_size;
+ enum qb_ipc_type type;
union {
struct {
int32_t sock;
@@ -105,7 +106,6 @@ struct qb_ipcc_funcs {
struct qb_ipcc_connection {
char name[NAME_MAX];
- enum qb_ipc_type type;
int32_t needs_sock_for_poll;
struct qb_ipc_one_way setup;
struct qb_ipc_one_way request;
diff --git a/lib/ipc_sysv_mq.c b/lib/ipc_sysv_mq.c
index f077617..9dfce1e 100644
--- a/lib/ipc_sysv_mq.c
+++ b/lib/ipc_sysv_mq.c
@@ -262,7 +262,6 @@ qb_ipcc_smq_connect(struct qb_ipcc_connection *c,
c->funcs.recv = qb_ipc_smq_recv;
c->funcs.fc_get = NULL;
c->funcs.disconnect = qb_ipcc_smq_disconnect;
- c->type = QB_IPC_SYSV_MQ;
c->needs_sock_for_poll = QB_TRUE;
if (strlen(c->name) > (NAME_MAX - 20)) {
diff --git a/lib/ipc_us.c b/lib/ipc_us.c
index d56a17d..2a749a1 100644
--- a/lib/ipc_us.c
+++ b/lib/ipc_us.c
@@ -89,8 +89,7 @@ qb_ipc_us_send(struct qb_ipc_one_way *one_way, const void *msg, size_t
len)
struct iovec iov_send;
char *rbuf = (char *)msg;
int32_t processed = 0;
- struct ipc_us_control *ctl =
- (struct ipc_us_control *)one_way->u.us.shared_data;
+ struct ipc_us_control *ctl = NULL;
msg_send.msg_iov = &iov_send;
msg_send.msg_iovlen = 1;
@@ -119,8 +118,11 @@ retry_send:
if (processed != len) {
goto retry_send;
}
- if (ctl) {
- qb_atomic_int_inc(&ctl->sent);
+ if (one_way->type == QB_IPC_SOCKET) {
+ ctl = (struct ipc_us_control *)one_way->u.us.shared_data;
+ if (ctl) {
+ qb_atomic_int_inc(&ctl->sent);
+ }
}
return processed;
}
@@ -134,8 +136,7 @@ qb_ipc_us_sendv(struct qb_ipc_one_way *one_way, const struct iovec
*iov,
int32_t processed = 0;
size_t len = 0;
int32_t i;
- struct ipc_us_control *ctl =
- (struct ipc_us_control *)one_way->u.us.shared_data;
+ struct ipc_us_control *ctl = NULL;
for (i = 0; i < iov_len; i++) {
len += iov[i].iov_len;
@@ -164,8 +165,11 @@ retry_send:
if (processed != len) {
goto retry_send;
}
- if (ctl) {
- qb_atomic_int_inc(&ctl->sent);
+ if (one_way->type == QB_IPC_SOCKET) {
+ ctl = (struct ipc_us_control *)one_way->u.us.shared_data;
+ if (ctl) {
+ qb_atomic_int_inc(&ctl->sent);
+ }
}
return processed;
}
@@ -231,8 +235,7 @@ qb_ipc_us_recv(struct qb_ipc_one_way * one_way,
void *msg, size_t len, int32_t timeout)
{
int32_t result;
- struct ipc_us_control *ctl =
- (struct ipc_us_control *)one_way->u.us.shared_data;
+ struct ipc_us_control *ctl = NULL;
retry_recv:
result = recv(one_way->u.us.sock, msg, len, MSG_NOSIGNAL | MSG_WAITALL);
@@ -245,8 +248,11 @@ retry_recv:
if (result == 0) {
return -ENOTCONN;
}
- if (ctl) {
- (void)qb_atomic_int_dec_and_test(&ctl->sent);
+ if (one_way->type == QB_IPC_SOCKET) {
+ ctl = (struct ipc_us_control *)one_way->u.us.shared_data;
+ if (ctl) {
+ (void)qb_atomic_int_dec_and_test(&ctl->sent);
+ }
}
return result;
diff --git a/lib/ipcc.c b/lib/ipcc.c
index cc664b4..5831e8c 100644
--- a/lib/ipcc.c
+++ b/lib/ipcc.c
@@ -46,7 +46,11 @@ qb_ipcc_connect(const char *name, size_t max_msg_size)
if (res < 0) {
goto disconnect_and_cleanup;
}
- c->type = response.connection_type;
+ c->response.type = response.connection_type;
+ c->request.type = response.connection_type;
+ c->event.type = response.connection_type;
+ c->setup.type = response.connection_type;
+
c->response.max_msg_size = response.max_msg_size;
c->request.max_msg_size = response.max_msg_size;
c->event.max_msg_size = response.max_msg_size;
@@ -57,7 +61,7 @@ qb_ipcc_connect(const char *name, size_t max_msg_size)
goto disconnect_and_cleanup;
}
- switch (c->type) {
+ switch (c->request.type) {
case QB_IPC_SHM:
res = qb_ipcc_shm_connect(c, &response);
break;
@@ -279,7 +283,7 @@ qb_ipcc_fd_get(struct qb_ipcc_connection * c, int32_t * fd)
if (c == NULL) {
return -EINVAL;
}
- if (c->type == QB_IPC_SOCKET) {
+ if (c->event.type == QB_IPC_SOCKET) {
*fd = c->event.u.us.sock;
} else {
*fd = c->setup.u.us.sock;
@@ -302,7 +306,7 @@ qb_ipcc_event_recv(struct qb_ipcc_connection * c, void *msg_pt,
if (c->needs_sock_for_poll) {
ow = &c->setup;
}
- if (c->type == QB_IPC_SOCKET) {
+ if (c->event.type == QB_IPC_SOCKET) {
ow = &c->event;
}
if (ow) {
@@ -344,7 +348,7 @@ qb_ipcc_disconnect(struct qb_ipcc_connection *c)
if (c->needs_sock_for_poll) {
ow = &c->setup;
}
- if (c->type == QB_IPC_SOCKET) {
+ if (c->event.type == QB_IPC_SOCKET) {
ow = &c->event;
}
if (ow) {
diff --git a/lib/ipcs.c b/lib/ipcs.c
index 0c630ba..419e0d9 100644
--- a/lib/ipcs.c
+++ b/lib/ipcs.c
@@ -442,6 +442,12 @@ qb_ipcs_connection_alloc(struct qb_ipcs_service *s)
c->fc_enabled = QB_FALSE;
c->state = QB_IPCS_CONNECTION_INACTIVE;
c->poll_events = POLLIN | POLLPRI | POLLNVAL;
+
+ c->setup.type = s->type;
+ c->request.type = s->type;
+ c->response.type = s->type;
+ c->event.type = s->type;
+
return c;
}
--
1.7.9.1