closing sockets (but not the client in a switch)

pollfd
Karchnu 2020-07-12 20:35:55 +02:00
parent 2813bd2695
commit 045b1433f5
2 changed files with 43 additions and 19 deletions

View File

@ -16,6 +16,11 @@
#include <fcntl.h>
int fd_is_valid(int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
struct ipc_error ipc_server_init (struct ipc_ctx *ctx, const char *sname)
{
@ -191,9 +196,36 @@ 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);
SECURE_DECLARATION (struct ipc_error, ret);
int fd = ctx->pollfd[index].fd;
#if 0
if (fd_is_valid (fd)) {
printf ("ipc_close: fd is valid ==> %d\n", fd);
if (ctx->cinfos[index].type == IPC_CONNECTION_TYPE_EXTERNAL) {
printf ("=== === === external fd: not closing %d\n", fd);
}
else {
ret = usock_close (fd);
}
}
else {
printf ("!!!!!!!!!!! !! !! !! IPC_CLOSE: fd is not valid!! ==> %d\n", fd);
}
#else
// Closing the file descriptor only if it is not an external connection,
// this should be handled by the libipc user application.
if (ctx->cinfos[index].type != IPC_CONNECTION_TYPE_EXTERNAL) {
ret = usock_close (fd);
}
#endif
// Verify that the close was OK.
if (ret.error_code != IPC_ERROR_NONE) {
return ret;
}
// 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) {
@ -454,11 +486,6 @@ handle_switched_message(struct ipc_event *event, struct ipc_ctx *ctx, uint32_t i
return fd_switching_read (event, ctx, index);
}
int fd_is_valid(int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
/* timer is in ms */
struct ipc_error ipc_wait_event (struct ipc_ctx *ctx, struct ipc_event *event, int *timer)
{
@ -520,13 +547,12 @@ struct ipc_error ipc_wait_event (struct ipc_ctx *ctx, struct ipc_event *event, i
// fd is switched: using callbacks for IO operations.
if (ctx->cinfos[i].type == IPC_CONNECTION_TYPE_SWITCHED) {
#if 0
int fd_validity = fd_is_valid (ctx->pollfd[i].fd);
if (fd_validity) {
printf ("switch happening in C: %d FD VALID\n", ctx->pollfd[i].fd);
}
else {
if (! fd_validity) {
printf ("switch happening in C: %d FD IS INVALID:::::::::: IM BROKEN INSIIIIIIIDE\n", ctx->pollfd[i].fd);
}
#endif
return handle_switched_message (event, ctx, i);
}

View File

@ -323,7 +323,6 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx
// Message reception OK: reading the message and put it in the list of messages to send.
if (r == IPC_CB_NO_ERROR) {
printf ("NO ERROR?? SURE?? REQUEST\n");
// In case of message reception:
// 1. put the message in the list to be sent
m.fd = dest_fd;
@ -348,19 +347,18 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx
IPC_RETURN_NO_ERROR;
}
printf ("CLOSING (ERROR/CLOSING...) REQUEST\n");
/**
* NOTE: In any other case, the fd is, or should be closed.
*/
// 1. close and remove both fd from switchdb
int delfd = ipc_switching_del (&ctx->switchdb, talkingfd);
if (delfd >= 0) {
close (delfd);
ipc_del_fd (ctx, delfd);
}
close (talkingfd);
close (sw->dest);
ipc_del_fd (ctx, sw->dest);
// Should not close the client: it's the job of the libipc user application.
// XXX: this may be normal, but should be documented.
// close (talkingfd);
ipc_del_fd (ctx, talkingfd);
ipc_switching_del (&ctx->switchdb, talkingfd);
// 2. set event (either error or disconnection)
if (r == IPC_CB_FD_CLOSING) {