|
| 1 | +commit de5ab3029c796e51d246bab9a83c66bbb5602e86 |
| 2 | +Author: Chrissie Caulfield <ccaulfie@redhat.com> |
| 3 | +Date: Wed Jan 5 10:53:09 2022 +0000 |
| 4 | + |
| 5 | + ipcc: Add an async connect API (#450) |
| 6 | + |
| 7 | +diff --git a/include/qb/qbipcc.h b/include/qb/qbipcc.h |
| 8 | +index de96c72..867ba04 100644 |
| 9 | +--- a/include/qb/qbipcc.h |
| 10 | ++++ b/include/qb/qbipcc.h |
| 11 | +@@ -80,6 +80,36 @@ typedef struct qb_ipcc_connection qb_ipcc_connection_t; |
| 12 | + qb_ipcc_connection_t* |
| 13 | + qb_ipcc_connect(const char *name, size_t max_msg_size); |
| 14 | + |
| 15 | ++/** |
| 16 | ++ * Asynchronously connect to an IPC service |
| 17 | ++ * @param name name of the service. |
| 18 | ++ * @param max_msg_size biggest msg size. |
| 19 | ++ * @param connect_fd return FD to continue connection with |
| 20 | ++ * @return NULL (error: see errno) or a connection object. |
| 21 | ++ * |
| 22 | ++ * qb_ipcc_connect_async() returns a connection FD which |
| 23 | ++ * should be used added to the application's mainloop - when it is |
| 24 | ++ * active, qb_ipcc_connect_continue() should be called for the |
| 25 | ++ * connection to be finalised. |
| 26 | ++ * NOTE: This is NOT the same FD that is used for normal applicaion |
| 27 | ++ * polling. qb_ipcc_fd_get() must still be called once the connection |
| 28 | ++ * is established. |
| 29 | ++ */ |
| 30 | ++qb_ipcc_connection_t * |
| 31 | ++qb_ipcc_connect_async(const char *name, size_t max_msg_size, int *connect_fd); |
| 32 | ++ |
| 33 | ++/** |
| 34 | ++ * Finish up an asynchonous IPC connection |
| 35 | ++ * @param c connection handle as returned from qb_ipcc_connect_async() |
| 36 | ++ * @return 0 or -errno. |
| 37 | ++ * |
| 38 | ++ * Finishes up a connection that was initiated by qb_ipcc_connect_async(), |
| 39 | ++ * this should only be called when the fd returned by qb_ipcc_connect_async() |
| 40 | ++ * becomes active, usually as a callback in the application's main loop. |
| 41 | ++ */ |
| 42 | ++int |
| 43 | ++qb_ipcc_connect_continue(struct qb_ipcc_connection * c); |
| 44 | ++ |
| 45 | + /** |
| 46 | + * Test kernel dgram socket buffers to verify the largest size up |
| 47 | + * to the max_msg_size value a single msg can be. Rounds down to the |
| 48 | +diff --git a/lib/ipc_int.h b/lib/ipc_int.h |
| 49 | +index 03c5dab..87f1de1 100644 |
| 50 | +--- a/lib/ipc_int.h |
| 51 | ++++ b/lib/ipc_int.h |
| 52 | +@@ -106,7 +106,8 @@ struct qb_ipcc_connection { |
| 53 | + }; |
| 54 | + |
| 55 | + int32_t qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c, |
| 56 | +- struct qb_ipc_connection_response *r); |
| 57 | ++ struct qb_ipc_connection_response *r); |
| 58 | ++int qb_ipcc_setup_connect_continue(struct qb_ipcc_connection *c, struct qb_ipc_connection_response *response); |
| 59 | + ssize_t qb_ipc_us_send(struct qb_ipc_one_way *one_way, const void *msg, size_t len); |
| 60 | + ssize_t qb_ipc_us_recv(struct qb_ipc_one_way *one_way, void *msg, size_t len, int32_t timeout); |
| 61 | + int32_t qb_ipc_us_ready(struct qb_ipc_one_way *ow_data, struct qb_ipc_one_way *ow_conn, |
| 62 | +diff --git a/lib/ipc_setup.c b/lib/ipc_setup.c |
| 63 | +index c144a5e..0ef9bb6 100644 |
| 64 | +--- a/lib/ipc_setup.c |
| 65 | ++++ b/lib/ipc_setup.c |
| 66 | +@@ -446,9 +446,7 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c, |
| 67 | + { |
| 68 | + int32_t res; |
| 69 | + struct qb_ipc_connection_request request; |
| 70 | +- struct ipc_auth_data *data; |
| 71 | + #ifdef QB_LINUX |
| 72 | +- int off = 0; |
| 73 | + int on = 1; |
| 74 | + #endif |
| 75 | + |
| 76 | +@@ -471,13 +469,24 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c, |
| 77 | + return res; |
| 78 | + } |
| 79 | + |
| 80 | ++ /* ... To be continued ... (when the FD is active) */ |
| 81 | ++ return 0; |
| 82 | ++} |
| 83 | ++ |
| 84 | ++/* Called from ipcc_connect_continue() when async connect socket is active */ |
| 85 | ++int qb_ipcc_setup_connect_continue(struct qb_ipcc_connection *c, struct qb_ipc_connection_response *r) |
| 86 | ++{ |
| 87 | ++ struct ipc_auth_data *data; |
| 88 | ++ int32_t res; |
| 89 | ++#ifdef QB_LINUX |
| 90 | ++ int off = 0; |
| 91 | ++#endif |
| 92 | + data = init_ipc_auth_data(c->setup.u.us.sock, sizeof(struct qb_ipc_connection_response)); |
| 93 | + if (data == NULL) { |
| 94 | + qb_ipcc_us_sock_close(c->setup.u.us.sock); |
| 95 | + return -ENOMEM; |
| 96 | + } |
| 97 | + |
| 98 | +- qb_ipc_us_ready(&c->setup, NULL, -1, POLLIN); |
| 99 | + res = qb_ipc_us_recv_msghdr(data); |
| 100 | + |
| 101 | + #ifdef QB_LINUX |
| 102 | +@@ -498,6 +507,7 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c, |
| 103 | + c->server_pid = data->ugp.pid; |
| 104 | + |
| 105 | + destroy_ipc_auth_data(data); |
| 106 | ++ |
| 107 | + return r->hdr.error; |
| 108 | + } |
| 109 | + |
| 110 | +diff --git a/lib/ipcc.c b/lib/ipcc.c |
| 111 | +index a6cf409..c744ea1 100644 |
| 112 | +--- a/lib/ipcc.c |
| 113 | ++++ b/lib/ipcc.c |
| 114 | +@@ -45,6 +45,70 @@ qb_ipcc_connect(const char *name, size_t max_msg_size) |
| 115 | + if (res < 0) { |
| 116 | + goto disconnect_and_cleanup; |
| 117 | + } |
| 118 | ++ qb_ipc_us_ready(&c->setup, NULL, -1, POLLIN); |
| 119 | ++ res = qb_ipcc_connect_continue(c); |
| 120 | ++ if (res != 0) { |
| 121 | ++ /* qb_ipcc_connect_continue() has cleaned up for us */ |
| 122 | ++ errno = -res; |
| 123 | ++ return NULL; |
| 124 | ++ } |
| 125 | ++ |
| 126 | ++ return c; |
| 127 | ++ |
| 128 | ++disconnect_and_cleanup: |
| 129 | ++ if (c->setup.u.us.sock >= 0) { |
| 130 | ++ qb_ipcc_us_sock_close(c->setup.u.us.sock); |
| 131 | ++ } |
| 132 | ++ free(c->receive_buf); |
| 133 | ++ free(c); |
| 134 | ++ errno = -res; |
| 135 | ++ return NULL; |
| 136 | ++} |
| 137 | ++ |
| 138 | ++qb_ipcc_connection_t * |
| 139 | ++qb_ipcc_connect_async(const char *name, size_t max_msg_size, int *connect_fd) |
| 140 | ++{ |
| 141 | ++ int32_t res; |
| 142 | ++ qb_ipcc_connection_t *c = NULL; |
| 143 | ++ struct qb_ipc_connection_response response; |
| 144 | ++ |
| 145 | ++ c = calloc(1, sizeof(struct qb_ipcc_connection)); |
| 146 | ++ if (c == NULL) { |
| 147 | ++ return NULL; |
| 148 | ++ } |
| 149 | ++ |
| 150 | ++ c->setup.max_msg_size = QB_MAX(max_msg_size, |
| 151 | ++ sizeof(struct qb_ipc_connection_response)); |
| 152 | ++ (void)strlcpy(c->name, name, NAME_MAX); |
| 153 | ++ res = qb_ipcc_us_setup_connect(c, &response); |
| 154 | ++ if (res < 0) { |
| 155 | ++ goto disconnect_and_cleanup; |
| 156 | ++ } |
| 157 | ++ |
| 158 | ++ *connect_fd = c->setup.u.us.sock; |
| 159 | ++ return c; |
| 160 | ++ |
| 161 | ++disconnect_and_cleanup: |
| 162 | ++ if (c->setup.u.us.sock >= 0) { |
| 163 | ++ qb_ipcc_us_sock_close(c->setup.u.us.sock); |
| 164 | ++ } |
| 165 | ++ free(c->receive_buf); |
| 166 | ++ free(c); |
| 167 | ++ errno = -res; |
| 168 | ++ return NULL; |
| 169 | ++} |
| 170 | ++ |
| 171 | ++int qb_ipcc_connect_continue(struct qb_ipcc_connection * c) |
| 172 | ++{ |
| 173 | ++ struct qb_ipc_connection_response response; |
| 174 | ++ int32_t res; |
| 175 | ++ |
| 176 | ++ /* Finish up the authentication part */ |
| 177 | ++ res = qb_ipcc_setup_connect_continue(c, &response); |
| 178 | ++ if (res != 0) { |
| 179 | ++ goto disconnect_and_cleanup; |
| 180 | ++ } |
| 181 | ++ |
| 182 | + c->response.type = response.connection_type; |
| 183 | + c->request.type = response.connection_type; |
| 184 | + c->event.type = response.connection_type; |
| 185 | +@@ -79,7 +143,7 @@ qb_ipcc_connect(const char *name, size_t max_msg_size) |
| 186 | + goto disconnect_and_cleanup; |
| 187 | + } |
| 188 | + c->is_connected = QB_TRUE; |
| 189 | +- return c; |
| 190 | ++ return 0; |
| 191 | + |
| 192 | + disconnect_and_cleanup: |
| 193 | + if (c->setup.u.us.sock >= 0) { |
| 194 | +@@ -88,7 +152,8 @@ disconnect_and_cleanup: |
| 195 | + free(c->receive_buf); |
| 196 | + free(c); |
| 197 | + errno = -res; |
| 198 | +- return NULL; |
| 199 | ++ return -res; |
| 200 | ++ |
| 201 | + } |
| 202 | + |
| 203 | + static int32_t |
| 204 | +diff --git a/tests/check_ipc.c b/tests/check_ipc.c |
| 205 | +index e8f81f3..6090354 100644 |
| 206 | +--- a/tests/check_ipc.c |
| 207 | ++++ b/tests/check_ipc.c |
| 208 | +@@ -1007,6 +1007,62 @@ repeat_send: |
| 209 | + return res; |
| 210 | + } |
| 211 | + |
| 212 | ++ |
| 213 | ++static int32_t |
| 214 | ++process_async_connect(int32_t fd, int32_t revents, void *data) |
| 215 | ++{ |
| 216 | ++ qb_loop_t *cl = (qb_loop_t *)data; |
| 217 | ++ int res; |
| 218 | ++ |
| 219 | ++ res = qb_ipcc_connect_continue(conn); |
| 220 | ++ ck_assert_int_eq(res, 0); |
| 221 | ++ qb_loop_stop(cl); |
| 222 | ++ return 0; |
| 223 | ++} |
| 224 | ++static void test_ipc_connect_async(void) |
| 225 | ++{ |
| 226 | ++ struct qb_ipc_request_header req_header; |
| 227 | ++ struct qb_ipc_response_header res_header; |
| 228 | ++ int32_t res; |
| 229 | ++ pid_t pid; |
| 230 | ++ uint32_t max_size = MAX_MSG_SIZE; |
| 231 | ++ int connect_fd; |
| 232 | ++ struct iovec iov[1]; |
| 233 | ++ static qb_loop_t *cl; |
| 234 | ++ |
| 235 | ++ pid = run_function_in_new_process("server", run_ipc_server, NULL); |
| 236 | ++ ck_assert(pid != -1); |
| 237 | ++ |
| 238 | ++ conn = qb_ipcc_connect_async(ipc_name, max_size, &connect_fd); |
| 239 | ++ ck_assert(conn != NULL); |
| 240 | ++ |
| 241 | ++ cl = qb_loop_create(); |
| 242 | ++ res = qb_loop_poll_add(cl, QB_LOOP_MED, |
| 243 | ++ connect_fd, POLLIN, |
| 244 | ++ cl, process_async_connect); |
| 245 | ++ ck_assert_int_eq(res, 0); |
| 246 | ++ qb_loop_run(cl); |
| 247 | ++ |
| 248 | ++ /* Send some data */ |
| 249 | ++ req_header.id = IPC_MSG_REQ_TX_RX; |
| 250 | ++ req_header.size = sizeof(struct qb_ipc_request_header); |
| 251 | ++ |
| 252 | ++ iov[0].iov_len = req_header.size; |
| 253 | ++ iov[0].iov_base = &req_header; |
| 254 | ++ |
| 255 | ++ res = qb_ipcc_sendv_recv(conn, iov, 1, |
| 256 | ++ &res_header, |
| 257 | ++ sizeof(struct qb_ipc_response_header), 5000); |
| 258 | ++ |
| 259 | ++ ck_assert_int_ge(res, 0); |
| 260 | ++ |
| 261 | ++ request_server_exit(); |
| 262 | ++ verify_graceful_stop(pid); |
| 263 | ++ |
| 264 | ++ |
| 265 | ++ qb_ipcc_disconnect(conn); |
| 266 | ++} |
| 267 | ++ |
| 268 | + static void |
| 269 | + test_ipc_txrx_timeout(void) |
| 270 | + { |
| 271 | +@@ -1226,6 +1282,7 @@ START_TEST(test_ipc_txrx_shm_timeout) |
| 272 | + } |
| 273 | + END_TEST |
| 274 | + |
| 275 | ++ |
| 276 | + START_TEST(test_ipc_txrx_us_timeout) |
| 277 | + { |
| 278 | + qb_enter(); |
| 279 | +@@ -1236,6 +1293,25 @@ START_TEST(test_ipc_txrx_us_timeout) |
| 280 | + } |
| 281 | + END_TEST |
| 282 | + |
| 283 | ++START_TEST(test_ipc_shm_connect_async) |
| 284 | ++{ |
| 285 | ++ qb_enter(); |
| 286 | ++ ipc_type = QB_IPC_SHM; |
| 287 | ++ set_ipc_name(__func__); |
| 288 | ++ test_ipc_connect_async(); |
| 289 | ++ qb_leave(); |
| 290 | ++} |
| 291 | ++END_TEST |
| 292 | ++ |
| 293 | ++START_TEST(test_ipc_us_connect_async) |
| 294 | ++{ |
| 295 | ++ qb_enter(); |
| 296 | ++ ipc_type = QB_IPC_SHM; |
| 297 | ++ set_ipc_name(__func__); |
| 298 | ++ test_ipc_connect_async(); |
| 299 | ++ qb_leave(); |
| 300 | ++} |
| 301 | ++END_TEST |
| 302 | + |
| 303 | + START_TEST(test_ipc_txrx_shm_getauth) |
| 304 | + { |
| 305 | +@@ -2277,6 +2353,8 @@ make_shm_suite(void) |
| 306 | + TCase *tc; |
| 307 | + Suite *s = suite_create("shm"); |
| 308 | + |
| 309 | ++ add_tcase(s, tc, test_ipc_shm_connect_async, 7); |
| 310 | ++ |
| 311 | + add_tcase(s, tc, test_ipc_txrx_shm_getauth, 7); |
| 312 | + add_tcase(s, tc, test_ipc_txrx_shm_timeout, 28); |
| 313 | + add_tcase(s, tc, test_ipc_server_fail_shm, 7); |
| 314 | +@@ -2308,6 +2386,8 @@ make_soc_suite(void) |
| 315 | + Suite *s = suite_create("socket"); |
| 316 | + TCase *tc; |
| 317 | + |
| 318 | ++ add_tcase(s, tc, test_ipc_us_connect_async, 7); |
| 319 | ++ |
| 320 | + add_tcase(s, tc, test_ipc_txrx_us_getauth, 7); |
| 321 | + add_tcase(s, tc, test_ipc_txrx_us_timeout, 28); |
| 322 | + /* Commented out for the moment as space in /dev/shm on the CI machines |
0 commit comments