From 2813bd269513cd1a17475ba7525e42a42cc84e72 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sat, 11 Jul 2020 10:44:47 +0200 Subject: [PATCH] fixed segfault --- src/communication.c | 36 +++++++++++++++++++++++++++++++----- src/error.c | 2 ++ src/ipc.h | 4 +++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/communication.c b/src/communication.c index 9258795..172b228 100644 --- a/src/communication.c +++ b/src/communication.c @@ -101,7 +101,7 @@ struct ipc_error ipc_contact_ipcd (int *pfd, const char *sname) } // Create context, contact ipcd, connects to the service. -struct ipc_error ipc_connection_ (struct ipc_ctx *ctx, const char *sname, enum ipc_connection_type type) +struct ipc_error ipc_connection_ (struct ipc_ctx *ctx, const char *sname, enum ipc_connection_type type, int *serverfd) { T_R ((ctx == NULL), IPC_ERROR_CONNECTION__NO_CTX); T_R ((sname == NULL), IPC_ERROR_CONNECTION__NO_SERVICE_NAME); @@ -124,6 +124,10 @@ struct ipc_error ipc_connection_ (struct ipc_ctx *ctx, const char *sname, enum i // Add the server to the listened file descriptors. TEST_IPC_RR (ipc_add (ctx, &srv, &pollfd), "cannot add the server in the context"); + if (serverfd != NULL) { + *serverfd = pollfd.fd; + } + IPC_RETURN_NO_ERROR; } @@ -145,17 +149,37 @@ int ipc_ctx_fd_type (struct ipc_ctx *ctx, int fd, enum ipc_connection_type type) struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname) { // Data received on the socket = messages, not new clients, and not switched (no callbacks). - return ipc_connection_ (ctx, sname, IPC_CONNECTION_TYPE_IPC); + return ipc_connection_ (ctx, sname, IPC_CONNECTION_TYPE_IPC, NULL); } -struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname) +struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname, int clientfd, int *serverfd) { + int sfd = 0; // Data received are for switched fd (callbacks should be used). - return ipc_connection_ (ctx, sname, IPC_CONNECTION_TYPE_SWITCHED); + struct ipc_error ret = ipc_connection_ (ctx + , sname + , IPC_CONNECTION_TYPE_SWITCHED + , &sfd); + + if (ret.error_code != IPC_ERROR_NONE) { + return ret; + } + + if (serverfd != NULL) { + *serverfd = sfd; + } + + ipc_add_fd_switched (ctx, clientfd); + // ipc_ctx_fd_type (ctx, clientfd, IPC_CONNECTION_TYPE_SWITCHED); + ipc_ctx_switching_add (ctx, clientfd, sfd); + + return ret; } struct ipc_error ipc_close_all (struct ipc_ctx *ctx) { + T_R ((ctx == NULL), IPC_ERROR_CLOSE_ALL__NO_CTX_PARAM); + for (size_t i = 0 ; i < ctx->size ; i++) { TEST_IPC_P (ipc_close (ctx, i), "cannot close a connection in handle_message"); } @@ -165,6 +189,8 @@ struct ipc_error ipc_close_all (struct ipc_ctx *ctx) struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index) { + T_R ((ctx == NULL), IPC_ERROR_CLOSE__NO_CTX_PARAM); + struct ipc_error ret = usock_close (ctx->pollfd[index].fd); // TODO: verify that the close was OK?? @@ -255,7 +281,7 @@ struct ipc_error ipc_write (struct ipc_ctx *ctx, const struct ipc_message *m) /** * Allocate memory then add a new connection to the context. */ -struct ipc_error ipc_add ( struct ipc_ctx *ctx, struct ipc_connection_info *p, struct pollfd *pollfd) +struct ipc_error ipc_add (struct ipc_ctx *ctx, struct ipc_connection_info *p, struct pollfd *pollfd) { T_R ((ctx == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENTS); T_R ((p == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENT); diff --git a/src/error.c b/src/error.c index 7c831cc..6709f84 100644 --- a/src/error.c +++ b/src/error.c @@ -141,6 +141,8 @@ static struct ipc_errors_verbose ipc_errors_verbose[] = { , {IPC_ERROR_ADD__NO_PARAM_POLLFD, "IPC_ERROR_ADD__NO_PARAM_POLLFD"} , {IPC_ERROR_WRITE__FD_NOT_FOUND, "IPC_ERROR_WRITE__FD_NOT_FOUND"} , {IPC_ERROR_FD_SWITCHING__NO_FD_RECORD, "IPC_ERROR_FD_SWITCHING__NO_FD_RECORD"} + , {IPC_ERROR_CLOSE_ALL__NO_CTX_PARAM, "IPC_ERROR_CLOSE_ALL__NO_CTX_PARAM" } + , {IPC_ERROR_CLOSE__NO_CTX_PARAM, "IPC_ERROR_CLOSE__NO_CTX_PARAM"} }; const char *ipc_errors_get (enum ipc_error_code e) diff --git a/src/ipc.h b/src/ipc.h index 7fac91c..898d852 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -204,6 +204,8 @@ enum ipc_error_code { , IPC_ERROR_ADD__NOT_ENOUGH_MEMORY = 105 , IPC_ERROR_WAIT_EVENT__POLL = 106 , IPC_ERROR_FD_SWITCHING__NO_FD_RECORD = 107 + , IPC_ERROR_CLOSE_ALL__NO_CTX_PARAM = 108 + , IPC_ERROR_CLOSE__NO_CTX_PARAM = 109 }; struct ipc_error { @@ -328,7 +330,7 @@ struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *time struct ipc_error ipc_server_init (struct ipc_ctx *ctx, const char *sname); struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname); -struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname); +struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname, int clientfd, int *serverfd); struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index); struct ipc_error ipc_close_all (struct ipc_ctx *ctx);