From: "Fabio M. Di Nitto" <fdinitto(a)redhat.com>
I (Angus) have changed some of Fabio's changes.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
examples/ipcclient.c | 49 ++++++++++++++++++++++++++-----------------------
examples/ipcserver.c | 2 +-
examples/tcpclient.c | 2 +-
lib/ipc_us.c | 12 ++++++------
lib/log_format.c | 29 +++++++++++++++--------------
tests/bmc.c | 15 ++++++++++-----
tests/bmcpt.c | 15 ++++++++++-----
tests/check_ipc.c | 26 +++++++++++++++++---------
tests/check_rb.c | 24 ++++++++++++------------
9 files changed, 98 insertions(+), 76 deletions(-)
diff --git a/examples/ipcclient.c b/examples/ipcclient.c
index c43998f..a8180c2 100644
--- a/examples/ipcclient.c
+++ b/examples/ipcclient.c
@@ -27,12 +27,23 @@
#define MAX_MSG_SIZE (8192)
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[256];
+};
+
+struct my_res {
+ struct qb_ipc_response_header hdr;
+ char message[256];
+};
+
int
main(int argc, char *argv[])
{
qb_ipcc_connection_t *conn;
- int32_t res;
- char *buffer[MAX_MSG_SIZE];
+ int32_t rc;
+ struct my_req req;
+ struct my_res res;
conn = qb_ipcc_connect("ipcserver", MAX_MSG_SIZE);
if (conn == NULL) {
@@ -41,39 +52,31 @@ main(int argc, char *argv[])
}
while(1) {
- struct qb_ipc_request_header *req_header = (struct qb_ipc_request_header *)buffer;
- struct qb_ipc_response_header *res_header = (struct qb_ipc_response_header *)buffer;
- char *data = (char*)buffer + sizeof(struct qb_ipc_request_header);
-
printf("SEND (q or Q to quit) : ");
- if (gets(data) == NULL) {
+ if (gets(req.message) == NULL) {
continue;
}
- if (strcmp(data , "q") != 0 &&
- strcmp(data , "Q") != 0) {
- req_header->id = QB_IPC_MSG_USER_START + 3;
- req_header->size = sizeof(struct qb_ipc_request_header) + strlen(data) + 1;
- res = qb_ipcc_send(conn, req_header, req_header->size);
- if (res < 0) {
+ if (strcmp(req.message, "q") != 0 &&
+ strcmp(req.message, "Q") != 0) {
+ req.hdr.id = QB_IPC_MSG_USER_START + 3;
+ req.hdr.size = sizeof(struct my_req);
+ rc = qb_ipcc_send(conn, &req, req.hdr.size);
+ if (rc < 0) {
perror("qb_ipcc_send");
}
} else {
break;
}
- if (res > 0) {
- res = qb_ipcc_recv(conn,
- buffer,
- MAX_MSG_SIZE, -1);
- if (res < 0) {
+ if (rc > 0) {
+ rc = qb_ipcc_recv(conn,
+ &res,
+ sizeof(res), -1);
+ if (rc < 0) {
perror("qb_ipcc_recv");
}
- res_header = (struct qb_ipc_response_header*)buffer;
- data = (char*)buffer + sizeof(struct qb_ipc_response_header);
- data[res - sizeof(struct qb_ipc_response_header)] = '\0';
-
- printf("Response[%d]: %s \n", res_header->id, data);
+ printf("Response[%d]: %s \n", res.hdr.id, res.message);
}
}
diff --git a/examples/ipcserver.c b/examples/ipcserver.c
index f4c47d4..4a51121 100644
--- a/examples/ipcserver.c
+++ b/examples/ipcserver.c
@@ -111,7 +111,7 @@ s1_msg_process_fn(qb_ipcs_connection_t *c,
snprintf(resp, 100, "ACK %zd bytes", size);
iov[0].iov_len = sizeof(response);
iov[0].iov_base = &response;
- iov[1].iov_len = strlen(resp);
+ iov[1].iov_len = strlen(resp) + 1;
iov[1].iov_base = resp;
res = qb_ipcs_response_sendv(c, iov, 2);
diff --git a/examples/tcpclient.c b/examples/tcpclient.c
index 8283321..15c60f0 100644
--- a/examples/tcpclient.c
+++ b/examples/tcpclient.c
@@ -39,7 +39,7 @@ main(int argc, char *argv[])
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
- server_addr.sin_addr = *((struct in_addr *)host->h_addr);
+ memcpy(&server_addr.sin_addr, host->h_addr, sizeof(host->h_addr));
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,
diff --git a/lib/ipc_us.c b/lib/ipc_us.c
index 3f11dfe..1387b0b 100644
--- a/lib/ipc_us.c
+++ b/lib/ipc_us.c
@@ -720,15 +720,15 @@ qb_ipcs_uc_recv_and_auth(int32_t sock, void *msg, size_t len,
* Usually Linux systems
*/
{
- struct ucred *cred;
+ struct ucred cred;
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg_recv);
assert(cmsg != NULL);
- cred = (struct ucred *)CMSG_DATA(cmsg);
- if (cred) {
+ if (CMSG_DATA(cmsg)) {
+ memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct ucred));
res = 0;
- ugp->pid = cred->pid;
- ugp->uid = cred->uid;
- ugp->gid = cred->gid;
+ ugp->pid = cred.pid;
+ ugp->uid = cred.uid;
+ ugp->gid = cred.gid;
} else {
res = -EBADMSG;
}
diff --git a/lib/log_format.c b/lib/log_format.c
index 55d8a6f..a626cae 100644
--- a/lib/log_format.c
+++ b/lib/log_format.c
@@ -482,11 +482,12 @@ reprocess:
goto reprocess;
case '*': {
- int *arg_int = (int *)&buf[data_pos];
+ int arg_int;
+ memcpy(&arg_int, &buf[data_pos], sizeof(int));
data_pos += sizeof(int);
fmt_pos += snprintf(&fmt[fmt_pos],
MINI_FORMAT_STR_LEN - fmt_pos,
- "%d", *arg_int);
+ "%d", arg_int);
format++;
goto reprocess;
}
@@ -506,38 +507,38 @@ reprocess:
case 'x':
case 'X':
if (type_long) {
- long int *arg_int;
+ long int arg_int;
fmt[fmt_pos++] = *format;
fmt[fmt_pos++] = '\0';
- arg_int = (long int *)&buf[data_pos];
+ memcpy(&arg_int, &buf[data_pos], sizeof(long int));
location += snprintf(&string[location],
str_len - location,
- fmt, *arg_int);
+ fmt, arg_int);
data_pos += sizeof(long int);
format++;
break;
} else if (type_longlong) {
- long long int *arg_int;
+ long long int arg_int;
fmt[fmt_pos++] = *format;
fmt[fmt_pos++] = '\0';
- arg_int = (long long int *)&buf[data_pos];
+ memcpy(&arg_int, &buf[data_pos], sizeof(long long int));
location += snprintf(&string[location],
str_len - location,
- fmt, *arg_int);
+ fmt, arg_int);
data_pos += sizeof(long long int);
format++;
break;
} else {
- int *arg_int;
+ int arg_int;
fmt[fmt_pos++] = *format;
fmt[fmt_pos++] = '\0';
- arg_int = (int *)&buf[data_pos];
+ memcpy(&arg_int, &buf[data_pos], sizeof(int));
location += snprintf(&string[location],
str_len - location,
- fmt, *arg_int);
+ fmt, arg_int);
data_pos += sizeof(int);
format++;
break;
@@ -551,14 +552,14 @@ reprocess:
case 'a':
case 'A':
{
- double *arg_double;
+ double arg_double;
fmt[fmt_pos++] = *format;
fmt[fmt_pos++] = '\0';
- arg_double = (double *)&buf[data_pos];
+ memcpy(&arg_double, &buf[data_pos], sizeof(double));
location += snprintf(&string[location],
str_len - location,
- fmt, *arg_double);
+ fmt, arg_double);
data_pos += sizeof(double);
format++;
break;
diff --git a/tests/bmc.c b/tests/bmc.c
index 3e805ed..6a4eeb4 100644
--- a/tests/bmc.c
+++ b/tests/bmc.c
@@ -49,18 +49,23 @@ static void bm_finish(const char *operation, int32_t size)
printf("MB/sec, %9.3f\n", mbs_per_sec);
}
-static char buffer[1024 * 1024];
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[1024 * 1024];
+};
+
+static struct my_req request;
+
static int32_t bmc_send_nozc(uint32_t size)
{
- struct qb_ipc_request_header *req_header = (struct qb_ipc_request_header *)buffer;
struct qb_ipc_response_header res_header;
int32_t res;
- req_header->id = QB_IPC_MSG_USER_START + 3;
- req_header->size = sizeof(struct qb_ipc_request_header) + size;
+ request.hdr.id = QB_IPC_MSG_USER_START + 3;
+ request.hdr.size = sizeof(struct qb_ipc_request_header) + size;
repeat_send:
- res = qb_ipcc_send(conn, req_header, req_header->size);
+ res = qb_ipcc_send(conn, &request, request.hdr.size);
if (res < 0) {
if (res == -EAGAIN) {
goto repeat_send;
diff --git a/tests/bmcpt.c b/tests/bmcpt.c
index e9faca1..5f5d1f6 100644
--- a/tests/bmcpt.c
+++ b/tests/bmcpt.c
@@ -88,18 +88,23 @@ static void bmc_disconnect(struct bm_ctx *ctx)
qb_ipcc_disconnect(ctx->conn);
}
-static char buffer[1024 * 1024];
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[1024 * 1024];
+};
+
+static struct my_req request;
+
static int32_t bmc_send_nozc(struct bm_ctx *ctx, uint32_t size)
{
- struct qb_ipc_request_header *req_header = (struct qb_ipc_request_header *)buffer;
struct qb_ipc_response_header res_header;
int32_t res;
- req_header->id = QB_IPC_MSG_USER_START + 3;
- req_header->size = sizeof(struct qb_ipc_request_header) + size;
+ request.hdr.id = QB_IPC_MSG_USER_START + 3;
+ request.hdr.size = sizeof(struct qb_ipc_request_header) + size;
repeat_send:
- res = qb_ipcc_send(ctx->conn, req_header, req_header->size);
+ res = qb_ipcc_send(ctx->conn, &request, request.hdr.size);
if (res < 0) {
if (res == -EAGAIN) {
goto repeat_send;
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
index 7c93383..b31b4a3 100644
--- a/tests/check_ipc.c
+++ b/tests/check_ipc.c
@@ -210,21 +210,24 @@ static int32_t stop_process(pid_t pid)
return 0;
}
-#define IPC_BUF_SIZE (1024 * 1024)
-static char buffer[IPC_BUF_SIZE];
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[1024 * 1024];
+};
+
+static struct my_req request;
static int32_t send_and_check(uint32_t size, int32_t ms_timeout)
{
- struct qb_ipc_request_header *req_header = (struct qb_ipc_request_header *)buffer;
struct qb_ipc_response_header res_header;
int32_t res;
int32_t try_times = 0;
- req_header->id = IPC_MSG_REQ_TX_RX;
- req_header->size = sizeof(struct qb_ipc_request_header) + size;
+ request.hdr.id = IPC_MSG_REQ_TX_RX;
+ request.hdr.size = sizeof(struct qb_ipc_request_header) + size;
repeat_send:
- res = qb_ipcc_send(conn, req_header, req_header->size);
+ res = qb_ipcc_send(conn, &request, request.hdr.size);
try_times++;
if (res < 0) {
if (res == -EAGAIN && try_times < 10) {
@@ -378,6 +381,12 @@ START_TEST(test_ipc_txrx_smq)
}
END_TEST
+struct my_res {
+ struct qb_ipc_response_header hdr;
+ char message[1024 * 1024];
+};
+
+static struct my_res response;
static void test_ipc_dispatch(void)
{
int32_t res;
@@ -385,7 +394,6 @@ static void test_ipc_dispatch(void)
int32_t c = 0;
pid_t pid;
struct qb_ipc_request_header req_header;
- struct qb_ipc_response_header *res_header = (struct qb_ipc_response_header*)buffer;
pid = run_function_in_new_process(run_ipc_server);
fail_if(pid == -1);
@@ -420,7 +428,7 @@ static void test_ipc_dispatch(void)
}
}
repeat_event_recv:
- res = qb_ipcc_event_recv(conn, res_header, IPC_BUF_SIZE, 0);
+ res = qb_ipcc_event_recv(conn, &response, sizeof(struct my_res), 0);
if (res < 0) {
if (res == -EAGAIN || res == -ETIMEDOUT) {
goto repeat_event_recv;
@@ -431,7 +439,7 @@ static void test_ipc_dispatch(void)
}
}
ck_assert_int_eq(res, sizeof(struct qb_ipc_response_header));
- ck_assert_int_eq(res_header->id, IPC_MSG_RES_DISPATCH);
+ ck_assert_int_eq(response.hdr.id, IPC_MSG_RES_DISPATCH);
qb_ipcc_disconnect(conn);
stop_process(pid);
diff --git a/tests/check_rb.c b/tests/check_rb.c
index d67263e..002521e 100644
--- a/tests/check_rb.c
+++ b/tests/check_rb.c
@@ -35,7 +35,7 @@
START_TEST(test_ring_buffer1)
{
char my_buf[512];
- struct qb_ipc_request_header *hdr;
+ struct qb_ipc_request_header hdr;
char *str;
qb_ringbuffer_t *rb;
int32_t i;
@@ -47,39 +47,39 @@ START_TEST(test_ring_buffer1)
fail_if(rb == NULL);
for (b = 0; b < 3; b++) {
- hdr = (struct qb_ipc_request_header *) my_buf;
+ memcpy(&hdr, my_buf, sizeof(struct qb_ipc_request_header));
str = my_buf + sizeof(struct qb_ipc_request_header);
for (i = 0; i < 900; i++) {
- hdr->id = __LINE__ + i;
- hdr->size =
+ hdr.id = __LINE__ + i;
+ hdr.size =
sprintf(str, "ID: %d (%s + i(%d)) -- %s-%s!",
- hdr->id, "actually the line number", i,
+ hdr.id, "actually the line number", i,
__func__, __FILE__) + 1;
- hdr->size += sizeof(struct qb_ipc_request_header);
+ hdr.size += sizeof(struct qb_ipc_request_header);
avail = qb_rb_space_free(rb);
- actual = qb_rb_chunk_write(rb, hdr, hdr->size);
- if (avail < (hdr->size + (2 * sizeof(uint32_t)))) {
+ actual = qb_rb_chunk_write(rb, &hdr, hdr.size);
+ if (avail < (hdr.size + (2 * sizeof(uint32_t)))) {
ck_assert_int_eq(actual, -EAGAIN);
} else {
- ck_assert_int_eq(actual, hdr->size);
+ ck_assert_int_eq(actual, hdr.size);
}
}
memset(my_buf, 0, sizeof(my_buf));
- hdr = (struct qb_ipc_request_header *) my_buf;
+ memcpy(&hdr, my_buf, sizeof(struct qb_ipc_request_header));
str = my_buf + sizeof(struct qb_ipc_request_header);
for (i = 0; i < 15; i++) {
- actual = qb_rb_chunk_read(rb, hdr, 512, 0);
+ actual = qb_rb_chunk_read(rb, &hdr, 512, 0);
if (actual < 0) {
ck_assert_int_eq(0, qb_rb_chunks_used(rb));
break;
}
str[actual - sizeof(struct qb_ipc_request_header)] = '\0';
- ck_assert_int_eq(actual, hdr->size);
+ ck_assert_int_eq(actual, hdr.size);
}
}
qb_rb_close(rb);
--
1.7.7.4