Blame SOURCES/bz2031865-add-async-connect.patch

0809dd
commit de5ab3029c796e51d246bab9a83c66bbb5602e86
0809dd
Author: Chrissie Caulfield <ccaulfie@redhat.com>
0809dd
Date:   Wed Jan 5 10:53:09 2022 +0000
0809dd
0809dd
    ipcc: Add an async connect API (#450)
0809dd
0809dd
diff --git a/include/qb/qbipcc.h b/include/qb/qbipcc.h
0809dd
index de96c72..867ba04 100644
0809dd
--- a/include/qb/qbipcc.h
0809dd
+++ b/include/qb/qbipcc.h
0809dd
@@ -80,6 +80,36 @@ typedef struct qb_ipcc_connection qb_ipcc_connection_t;
0809dd
 qb_ipcc_connection_t*
0809dd
 qb_ipcc_connect(const char *name, size_t max_msg_size);
0809dd
 
0809dd
+/**
0809dd
+ * Asynchronously connect to an IPC service
0809dd
+ * @param name name of the service.
0809dd
+ * @param max_msg_size biggest msg size.
0809dd
+ * @param connect_fd return FD to continue connection with
0809dd
+ * @return NULL (error: see errno) or a connection object.
0809dd
+ *
0809dd
+ * qb_ipcc_connect_async() returns a connection FD which
0809dd
+ * should be used added to the application's mainloop - when it is
0809dd
+ * active, qb_ipcc_connect_continue() should be called for the
0809dd
+ * connection to be finalised.
0809dd
+ * NOTE: This is NOT the same FD that is used for normal applicaion
0809dd
+ * polling. qb_ipcc_fd_get() must still be called once the connection
0809dd
+ * is established.
0809dd
+ */
0809dd
+qb_ipcc_connection_t *
0809dd
+qb_ipcc_connect_async(const char *name, size_t max_msg_size, int *connect_fd);
0809dd
+
0809dd
+/**
0809dd
+ * Finish up an asynchonous IPC connection
0809dd
+ * @param c connection handle as returned from qb_ipcc_connect_async()
0809dd
+ * @return 0 or -errno.
0809dd
+ *
0809dd
+ * Finishes up a connection that was initiated by qb_ipcc_connect_async(),
0809dd
+ * this should only be called when the fd returned by qb_ipcc_connect_async()
0809dd
+ * becomes active, usually as a callback in the application's main loop.
0809dd
+ */
0809dd
+int
0809dd
+qb_ipcc_connect_continue(struct qb_ipcc_connection * c);
0809dd
+
0809dd
 /**
0809dd
  * Test kernel dgram socket buffers to verify the largest size up
0809dd
  * to the max_msg_size value a single msg can be. Rounds down to the
0809dd
diff --git a/lib/ipc_int.h b/lib/ipc_int.h
0809dd
index 03c5dab..87f1de1 100644
0809dd
--- a/lib/ipc_int.h
0809dd
+++ b/lib/ipc_int.h
0809dd
@@ -106,7 +106,8 @@ struct qb_ipcc_connection {
0809dd
 };
0809dd
 
0809dd
 int32_t qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c,
0809dd
-				   struct qb_ipc_connection_response *r);
0809dd
+				 struct qb_ipc_connection_response *r);
0809dd
+int qb_ipcc_setup_connect_continue(struct qb_ipcc_connection *c, struct qb_ipc_connection_response *response);
0809dd
 ssize_t qb_ipc_us_send(struct qb_ipc_one_way *one_way, const void *msg, size_t len);
0809dd
 ssize_t qb_ipc_us_recv(struct qb_ipc_one_way *one_way, void *msg, size_t len, int32_t timeout);
0809dd
 int32_t qb_ipc_us_ready(struct qb_ipc_one_way *ow_data, struct qb_ipc_one_way *ow_conn,
0809dd
diff --git a/lib/ipc_setup.c b/lib/ipc_setup.c
0809dd
index c144a5e..0ef9bb6 100644
0809dd
--- a/lib/ipc_setup.c
0809dd
+++ b/lib/ipc_setup.c
0809dd
@@ -446,9 +446,7 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c,
0809dd
 {
0809dd
 	int32_t res;
0809dd
 	struct qb_ipc_connection_request request;
0809dd
-	struct ipc_auth_data *data;
0809dd
 #ifdef QB_LINUX
0809dd
-	int off = 0;
0809dd
 	int on = 1;
0809dd
 #endif
0809dd
 
0809dd
@@ -471,13 +469,24 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c,
0809dd
 		return res;
0809dd
 	}
0809dd
 
0809dd
+	/* ... To be continued ... (when the FD is active) */
0809dd
+	return 0;
0809dd
+}
0809dd
+
0809dd
+/* Called from ipcc_connect_continue() when async connect socket is active */
0809dd
+int qb_ipcc_setup_connect_continue(struct qb_ipcc_connection *c, struct qb_ipc_connection_response *r)
0809dd
+{
0809dd
+	struct ipc_auth_data *data;
0809dd
+	int32_t res;
0809dd
+#ifdef QB_LINUX
0809dd
+	int off = 0;
0809dd
+#endif
0809dd
 	data = init_ipc_auth_data(c->setup.u.us.sock, sizeof(struct qb_ipc_connection_response));
0809dd
 	if (data == NULL) {
0809dd
 		qb_ipcc_us_sock_close(c->setup.u.us.sock);
0809dd
 		return -ENOMEM;
0809dd
 	}
0809dd
 
0809dd
-	qb_ipc_us_ready(&c->setup, NULL, -1, POLLIN);
0809dd
 	res = qb_ipc_us_recv_msghdr(data);
0809dd
 
0809dd
 #ifdef QB_LINUX
0809dd
@@ -498,6 +507,7 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c,
0809dd
 	c->server_pid = data->ugp.pid;
0809dd
 
0809dd
 	destroy_ipc_auth_data(data);
0809dd
+
0809dd
 	return r->hdr.error;
0809dd
 }
0809dd
 
0809dd
diff --git a/lib/ipcc.c b/lib/ipcc.c
0809dd
index a6cf409..c744ea1 100644
0809dd
--- a/lib/ipcc.c
0809dd
+++ b/lib/ipcc.c
0809dd
@@ -45,6 +45,70 @@ qb_ipcc_connect(const char *name, size_t max_msg_size)
0809dd
 	if (res < 0) {
0809dd
 		goto disconnect_and_cleanup;
0809dd
 	}
0809dd
+	qb_ipc_us_ready(&c->setup, NULL, -1, POLLIN);
0809dd
+	res = qb_ipcc_connect_continue(c);
0809dd
+	if (res != 0) {
0809dd
+		/* qb_ipcc_connect_continue() has cleaned up for us */
0809dd
+		errno = -res;
0809dd
+		return NULL;
0809dd
+	}
0809dd
+
0809dd
+	return c;
0809dd
+
0809dd
+disconnect_and_cleanup:
0809dd
+	if (c->setup.u.us.sock >= 0) {
0809dd
+		qb_ipcc_us_sock_close(c->setup.u.us.sock);
0809dd
+	}
0809dd
+	free(c->receive_buf);
0809dd
+	free(c);
0809dd
+	errno = -res;
0809dd
+	return NULL;
0809dd
+}
0809dd
+
0809dd
+qb_ipcc_connection_t *
0809dd
+qb_ipcc_connect_async(const char *name, size_t max_msg_size, int *connect_fd)
0809dd
+{
0809dd
+	int32_t res;
0809dd
+	qb_ipcc_connection_t *c = NULL;
0809dd
+	struct qb_ipc_connection_response response;
0809dd
+
0809dd
+	c = calloc(1, sizeof(struct qb_ipcc_connection));
0809dd
+	if (c == NULL) {
0809dd
+		return NULL;
0809dd
+	}
0809dd
+
0809dd
+	c->setup.max_msg_size = QB_MAX(max_msg_size,
0809dd
+				       sizeof(struct qb_ipc_connection_response));
0809dd
+	(void)strlcpy(c->name, name, NAME_MAX);
0809dd
+	res = qb_ipcc_us_setup_connect(c, &response);
0809dd
+	if (res < 0) {
0809dd
+		goto disconnect_and_cleanup;
0809dd
+	}
0809dd
+
0809dd
+	*connect_fd = c->setup.u.us.sock;
0809dd
+	return c;
0809dd
+
0809dd
+disconnect_and_cleanup:
0809dd
+	if (c->setup.u.us.sock >= 0) {
0809dd
+		qb_ipcc_us_sock_close(c->setup.u.us.sock);
0809dd
+	}
0809dd
+	free(c->receive_buf);
0809dd
+	free(c);
0809dd
+	errno = -res;
0809dd
+	return NULL;
0809dd
+}
0809dd
+
0809dd
+int qb_ipcc_connect_continue(struct qb_ipcc_connection * c)
0809dd
+{
0809dd
+	struct qb_ipc_connection_response response;
0809dd
+	int32_t res;
0809dd
+
0809dd
+	/* Finish up the authentication part */
0809dd
+	res = qb_ipcc_setup_connect_continue(c, &response);
0809dd
+	if (res != 0) {
0809dd
+		goto disconnect_and_cleanup;
0809dd
+	}
0809dd
+
0809dd
 	c->response.type = response.connection_type;
0809dd
 	c->request.type = response.connection_type;
0809dd
 	c->event.type = response.connection_type;
0809dd
@@ -79,7 +143,7 @@ qb_ipcc_connect(const char *name, size_t max_msg_size)
0809dd
 		goto disconnect_and_cleanup;
0809dd
 	}
0809dd
 	c->is_connected = QB_TRUE;
0809dd
-	return c;
0809dd
+	return 0;
0809dd
 
0809dd
 disconnect_and_cleanup:
0809dd
 	if (c->setup.u.us.sock >= 0) {
0809dd
@@ -88,7 +152,8 @@ disconnect_and_cleanup:
0809dd
 	free(c->receive_buf);
0809dd
 	free(c);
0809dd
 	errno = -res;
0809dd
-	return NULL;
0809dd
+	return -res;
0809dd
+
0809dd
 }
0809dd
 
0809dd
 static int32_t
0809dd
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
0809dd
index e8f81f3..6090354 100644
0809dd
--- a/tests/check_ipc.c
0809dd
+++ b/tests/check_ipc.c
0809dd
@@ -1007,6 +1007,62 @@ repeat_send:
0809dd
 	return res;
0809dd
 }
0809dd
 
0809dd
+
0809dd
+static int32_t
0809dd
+process_async_connect(int32_t fd, int32_t revents, void *data)
0809dd
+{
0809dd
+	qb_loop_t *cl = (qb_loop_t *)data;
0809dd
+	int res;
0809dd
+
0809dd
+	res = qb_ipcc_connect_continue(conn);
0809dd
+	ck_assert_int_eq(res, 0);
0809dd
+	qb_loop_stop(cl);
0809dd
+	return 0;
0809dd
+}
0809dd
+static void test_ipc_connect_async(void)
0809dd
+{
0809dd
+	struct qb_ipc_request_header req_header;
0809dd
+	struct qb_ipc_response_header res_header;
0809dd
+	int32_t res;
0809dd
+	pid_t pid;
0809dd
+	uint32_t max_size = MAX_MSG_SIZE;
0809dd
+	int connect_fd;
0809dd
+	struct iovec iov[1];
0809dd
+	static qb_loop_t *cl;
0809dd
+
0809dd
+	pid = run_function_in_new_process("server", run_ipc_server, NULL);
0809dd
+	ck_assert(pid != -1);
0809dd
+
0809dd
+	conn = qb_ipcc_connect_async(ipc_name, max_size, &connect_fd);
0809dd
+	ck_assert(conn != NULL);
0809dd
+
0809dd
+	cl = qb_loop_create();
0809dd
+	res = qb_loop_poll_add(cl, QB_LOOP_MED,
0809dd
+			 connect_fd, POLLIN,
0809dd
+			 cl, process_async_connect);
0809dd
+	ck_assert_int_eq(res, 0);
0809dd
+	qb_loop_run(cl);
0809dd
+
0809dd
+	/* Send some data */
0809dd
+	req_header.id = IPC_MSG_REQ_TX_RX;
0809dd
+	req_header.size = sizeof(struct qb_ipc_request_header);
0809dd
+
0809dd
+	iov[0].iov_len = req_header.size;
0809dd
+	iov[0].iov_base = &req_header;
0809dd
+
0809dd
+	res = qb_ipcc_sendv_recv(conn, iov, 1,
0809dd
+				 &res_header,
0809dd
+				 sizeof(struct qb_ipc_response_header), 5000);
0809dd
+
0809dd
+	ck_assert_int_ge(res, 0);
0809dd
+
0809dd
+	request_server_exit();
0809dd
+	verify_graceful_stop(pid);
0809dd
+
0809dd
+
0809dd
+	qb_ipcc_disconnect(conn);
0809dd
+}
0809dd
+
0809dd
 static void
0809dd
 test_ipc_txrx_timeout(void)
0809dd
 {
0809dd
@@ -1226,6 +1282,7 @@ START_TEST(test_ipc_txrx_shm_timeout)
0809dd
 }
0809dd
 END_TEST
0809dd
 
0809dd
+
0809dd
 START_TEST(test_ipc_txrx_us_timeout)
0809dd
 {
0809dd
 	qb_enter();
0809dd
@@ -1236,6 +1293,25 @@ START_TEST(test_ipc_txrx_us_timeout)
0809dd
 }
0809dd
 END_TEST
0809dd
 
0809dd
+START_TEST(test_ipc_shm_connect_async)
0809dd
+{
0809dd
+	qb_enter();
0809dd
+	ipc_type = QB_IPC_SHM;
0809dd
+	set_ipc_name(__func__);
0809dd
+	test_ipc_connect_async();
0809dd
+	qb_leave();
0809dd
+}
0809dd
+END_TEST
0809dd
+
0809dd
+START_TEST(test_ipc_us_connect_async)
0809dd
+{
0809dd
+	qb_enter();
0809dd
+	ipc_type = QB_IPC_SHM;
0809dd
+	set_ipc_name(__func__);
0809dd
+	test_ipc_connect_async();
0809dd
+	qb_leave();
0809dd
+}
0809dd
+END_TEST
0809dd
 
0809dd
 START_TEST(test_ipc_txrx_shm_getauth)
0809dd
 {
0809dd
@@ -2277,6 +2353,8 @@ make_shm_suite(void)
0809dd
 	TCase *tc;
0809dd
 	Suite *s = suite_create("shm");
0809dd
 
0809dd
+	add_tcase(s, tc, test_ipc_shm_connect_async, 7);
0809dd
+
0809dd
 	add_tcase(s, tc, test_ipc_txrx_shm_getauth, 7);
0809dd
 	add_tcase(s, tc, test_ipc_txrx_shm_timeout, 28);
0809dd
 	add_tcase(s, tc, test_ipc_server_fail_shm, 7);
0809dd
@@ -2308,6 +2386,8 @@ make_soc_suite(void)
0809dd
 	Suite *s = suite_create("socket");
0809dd
 	TCase *tc;
0809dd
 
0809dd
+	add_tcase(s, tc, test_ipc_us_connect_async, 7);
0809dd
+
0809dd
 	add_tcase(s, tc, test_ipc_txrx_us_getauth, 7);
0809dd
 	add_tcase(s, tc, test_ipc_txrx_us_timeout, 28);
0809dd
 /* Commented out for the moment as space in /dev/shm on the CI machines