From 89d8881a40df48c6f12978587f8cfc4f22e91b7e Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sun, 28 Jun 2020 18:05:28 +0200 Subject: [PATCH] Compile again, but lacks poll(2) call. --- src/communication.c | 114 +++++++++++++++++++------------------------- src/ipc.h | 14 ++++-- src/message.c | 2 +- src/print.c | 2 +- 4 files changed, 60 insertions(+), 72 deletions(-) diff --git a/src/communication.c b/src/communication.c index d1c9806..450d9dd 100644 --- a/src/communication.c +++ b/src/communication.c @@ -132,7 +132,7 @@ struct ipc_error ipc_contact_networkd (int *pfd, const char *sname) if (strncmp (networkvar, sname, strlen (sname)) != 0 && strstr (networkvar, columnthensname) == NULL) { // printf ("sname %s not found\n", sname); - *fd = 0; + *pfd = 0; IPC_RETURN_NO_ERROR; } @@ -206,11 +206,13 @@ struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index) { struct ipc_error ret = usock_close (ctx->pollfd[index].fd); - if (ctx->cinfos[i]->type == IPC_CONNECTION_TYPE_SERVER) { - struct ipc_error ret = usock_remove (ctx->cinfos[i]->spath); - if (srv->spath != NULL) { - free (srv->spath); - srv->spath = NULL; + // TODO: verify that the close was OK?? + + if (ctx->cinfos[index].type == IPC_CONNECTION_TYPE_SERVER) { + ret = usock_remove (ctx->cinfos[index].spath); + if (ctx->cinfos[index].spath != NULL) { + free (ctx->cinfos[index].spath); + ctx->cinfos[index].spath = NULL; } } @@ -233,10 +235,12 @@ struct ipc_error ipc_accept_add (struct ipc_event *event, struct ipc_ctx *ctx, u ctx->pollfd[ctx->size -1].events = POLLIN; // Tell to poll(2) to watch for incoming data from this fd. ctx->cinfos[ctx->size -1].type = IPC_CONNECTION_TYPE_IPC; - struct ipc_connection_info *new_client = &ctx->cinfos[ctx->size]; ctx->size++; - IPC_EVENT_SET (event, IPC_EVENT_TYPE_CONNECTION, NULL, new_client); + // Set the event structure. + uint32_t client_index = ctx->size - 1; + IPC_EVENT_SET (event, IPC_EVENT_TYPE_CONNECTION, NULL, client_index, *client_fd); + IPC_RETURN_NO_ERROR; } @@ -294,53 +298,57 @@ struct ipc_error ipc_add ( struct ipc_ctx *ctx, struct ipc_connection_info *p, s // Memory reallocation. ipc_ctx_new_alloc (ctx); - memcpy (ctx->cinfos[ctx->size - 1], p , sizeof (struct ipc_connection_info)); - memcpy (ctx->pollfd[ctx->size - 1], pollfd, sizeof (struct pollfd)); + ctx->cinfos[ctx->size - 1] = *p; + ctx->pollfd[ctx->size - 1] = *pollfd; IPC_RETURN_NO_ERROR; } -struct ipc_error ipc_del ( - struct ipc_ctx *ctx - , struct ipc_connection_info *p) +struct ipc_error ipc_del (struct ipc_ctx *ctx, uint32_t index) { - T_R ((ctx == NULL), IPC_ERROR_DEL__NO_CLIENTS_PARAM); - T_R ((p == NULL), IPC_ERROR_DEL__NO_CLIENT_PARAM); - T_R ((ctx->cinfos == NULL), IPC_ERROR_DEL__EMPTY_LIST); + T_R ((ctx == NULL), IPC_ERROR_DEL__NO_CLIENTS_PARAM); + T_R ((ctx->cinfos == NULL || ctx->pollfd == NULL), IPC_ERROR_DEL__EMPTY_LIST); - size_t i; - for (i = 0; i < ctx->size; i++) { - // WARNING: The test is performed on the pointers of both structures, - // this is efficient but it doesn't work if the p structure is a copy. - if (ctx->cinfos[i] == p) { - // TODO: possible memory leak if the ipc_connection_info is not deeply free'ed. - ctx->cinfos[i] = ctx->cinfos[ctx->size - 1]; - ctx->size--; - if (ctx->size == 0) { - ipc_connections_free (ctx); - } else { - ctx->cinfos = realloc (ctx->cinfos, sizeof (struct ipc_connection_info) * ctx->size); - - if (ctx->cinfos == NULL) { - IPC_RETURN_ERROR (IPC_ERROR_DEL__EMPTIED_LIST); - } - } - - IPC_RETURN_NO_ERROR; - } + if (index >= ctx->size) { + IPC_RETURN_ERROR (IPC_ERROR_DEL__CANNOT_FIND_CLIENT); } - IPC_RETURN_ERROR (IPC_ERROR_DEL__CANNOT_FIND_CLIENT); + if (ctx->cinfos[index].spath != NULL) + free (ctx->cinfos[index].spath); + + ctx->size--; + + if (ctx->size == 0) { + // free ctx->cinfos and ctx->pollfd + ipc_connections_free (ctx); + IPC_RETURN_NO_ERROR; + } + + // The last element in the array replaces the removed one. + ctx->cinfos[index] = ctx->cinfos[ctx->size]; + ctx->pollfd[index] = ctx->pollfd[ctx->size]; + + // Reallocation of the arrays. TODO: should be optimised someday. + ctx->cinfos = realloc (ctx->cinfos, sizeof (struct ipc_connection_info) * ctx->size); + ctx->pollfd = realloc (ctx->pollfd, sizeof (struct pollfd ) * ctx->size); + + if (ctx->cinfos == NULL || ctx->pollfd == NULL) { + IPC_RETURN_ERROR (IPC_ERROR_DEL__EMPTIED_LIST); + } + + IPC_RETURN_NO_ERROR; } void ipc_connections_free (struct ipc_ctx *ctx) { + // The close and close_all functions perform deep removal of structures. + ipc_close_all (ctx); + if (ctx->cinfos != NULL) { - for (size_t i = 0; i < ctx->size; i++) { - free (ctx->cinfos[i]); - } free (ctx->cinfos); + free (ctx->pollfd); ctx->cinfos = NULL; + ctx->pollfd = NULL; } ctx->size = 0; } @@ -367,32 +375,8 @@ struct ipc_error ipc_del_fd (struct ipc_ctx *ctx, int fd) T_R ((ctx->cinfos == NULL || ctx->pollfd), IPC_ERROR_DEL_FD__EMPTY_LIST); for (size_t i = 0; i < ctx->size; i++) { - if (ctx->pollfd[i].fd == fd) { - - if (ctx->cinfos[i].spath != NULL) - free (ctx->cinfos[i].spath); - - ctx->size--; - - if (ctx->size == 0) { - // free ctx->cinfos and ctx->pollfd - ipc_connections_free (ctx); - } else { - // The last element in the array replaces the removed one. - ctx->cinfos[i] = ctx->cinfos[ctx->size]; - ctx->pollfd[i] = ctx->pollfd[ctx->size]; - - // Reallocation of the arrays. TODO: should be optimised someday. - ctx->cinfos = realloc (ctx->cinfos, sizeof (struct ipc_connection_info) * ctx->size); - ctx->pollfd = realloc (ctx->pollfd, sizeof (struct pollfd ) * ctx->size); - - if (ctx->cinfos == NULL || ctx->pollfd == NULL) { - IPC_RETURN_ERROR (IPC_ERROR_DEL_FD__EMPTIED_LIST); - } - } - - IPC_RETURN_NO_ERROR; + return ipc_del (ctx, i); } } diff --git a/src/ipc.h b/src/ipc.h index ab4851b..732d512 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -182,6 +182,8 @@ enum ipc_error_code { , IPC_ERROR_CONNECTION__NO_CTX = 95 , IPC_ERROR_CTX_INIT__MALLOC_CTX = 96 , IPC_ERROR_CTX_INIT__MALLOC_POLLFD = 97 + , IPC_ERROR_CONTACT_NETWORKD__NO_FD_PARAM = 98 + , IPC_ERROR_HANDLE_NEW_CONNECTION__INCONSISTENT_INDEX = 99 }; struct ipc_error { @@ -233,7 +235,8 @@ struct ipc_message { struct ipc_event { enum ipc_event_type type; - struct ipc_connection_info *origin; + uint32_t index; + int origin; void *m; // message pointer }; @@ -241,10 +244,11 @@ struct ipc_event { * ipc event macros **/ -#define IPC_EVENT_SET(pevent,type_,message_,origin_) {\ +#define IPC_EVENT_SET(pevent,type_,message_,index_, origin_fd_) {\ pevent->type = type_; \ pevent->m = message_; \ - pevent->origin = origin_; \ + pevent->index = index_; \ + pevent->origin = origin_fd_; \ }; enum ipc_connection_types { @@ -278,8 +282,8 @@ struct ipc_error ipc_write (const struct ipc_ctx *, uint32_t index, const struct struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *timer); // store and remove only pointers on allocated structures -struct ipc_error ipc_add (struct ipc_ctx *, struct ipc_connection_info *); -struct ipc_error ipc_del (struct ipc_ctx *, struct ipc_connection_info *); +struct ipc_error ipc_add (struct ipc_ctx *, struct ipc_connection_info *, struct pollfd *); +struct ipc_error ipc_del (struct ipc_ctx *, uint32_t index); // add an arbitrary file descriptor to read struct ipc_error ipc_add_fd (struct ipc_ctx *cinfos, int fd); diff --git a/src/message.c b/src/message.c index 625ac7b..cd679ec 100644 --- a/src/message.c +++ b/src/message.c @@ -163,7 +163,7 @@ struct ipc_error ipc_messages_add (struct ipc_messages *messages, struct ipc_me T_R ((messages->messages == NULL), IPC_ERROR_ADD_MESSAGE_TO_SEND__EMPTY_LIST); - messages->messages[messages->size - 1] = p; + messages->messages[messages->size - 1] = message; IPC_RETURN_NO_ERROR; } diff --git a/src/print.c b/src/print.c index f1dc58f..92b0580 100644 --- a/src/print.c +++ b/src/print.c @@ -14,7 +14,7 @@ void ipc_connection_print (struct ipc_connection_info *cinfo) void ipc_connections_print (struct ipc_ctx *cinfos) { for (size_t i = 0; i < cinfos->size; i++) { - ipc_connection_print (cinfos->cinfos[i]); + ipc_connection_print (&cinfos->cinfos[i]); } }