Skip to content

Commit

Permalink
fixed cleanup,; ClientList->next
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-vii committed Apr 23, 2021
1 parent 94fa04d commit e684a3d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 73 deletions.
3 changes: 2 additions & 1 deletion mdstcpip/io_routines/IoRoutinesThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ static void io_listen(void *pp)
{
pipes->connection = PopConnection(id);
do
status = DoMessageC(pipes->connection);
status = ConnectionDoMessage(pipes->connection);
while (STATUS_OK);
destroyConnection(pipes->connection);
}
free(pp);
}
Expand Down
3 changes: 2 additions & 1 deletion mdstcpip/io_routines/IoRoutinesTunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ static int io_listen(int argc __attribute__((unused)),
{
pipes.connection = PopConnection(id);
do
status = DoMessageC(pipes.connection);
status = ConnectionDoMessage(pipes.connection);
while (STATUS_OK);
destroyConnection(pipes.connection);
return C_OK;
}
return C_ERROR;
Expand Down
5 changes: 0 additions & 5 deletions mdstcpip/io_routines/ioroutinestcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,7 @@ static int io_listen(int argc, char **argv)
client->sock = sock;
client->username = username;
client->iphost = getHostInfo(sock, &client->host);
pthread_mutex_lock(&ClientListLock);
ClientList = client;
client->next = ClientList;
pthread_mutex_unlock(&ClientListLock);
dispatch_client(client);
// dispatch_client will closed connection and inlist client on failure
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions mdstcpip/io_routines/ioroutinesudt.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ static int io_listen(int argc, char **argv)
client->username = username;
client->addr = ((struct sockaddr_in *)&sin)->sin_addr.s_addr;
client->iphost = getHostInfo(sock, &client->host);
pthread_mutex_lock(&ClientListLock);
client->next = ClientList;
ClientList = client;
pthread_mutex_unlock(&ClientListLock);
dispatch_client(client);
}
else
Expand Down
90 changes: 49 additions & 41 deletions mdstcpip/io_routines/ioroutinesx.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,42 @@ static int io_check(Connection *c)
////////////////////////////////////////////////////////////////////////////////
static void destroyClient(Client *c)
{
DBG("destroyClient");
if (c->thread)
{
if (!pthread_equal(*c->thread, pthread_self()))
{
pthread_cancel(*c->thread);
pthread_detach(*c->thread);
}
else
{
pthread_detach(*c->thread);
}
free(c->thread);
}
else
destroyConnection(c->connection);
free(c->username);
free(c->iphost);
free(c->host);
free(c);
}

static inline void destroyClientList()
{
Client *cl;
pthread_mutex_lock(&ClientListLock);
cl = ClientList;
ClientList = NULL;
pthread_mutex_unlock(&ClientListLock);
while (cl)
{
Client *const c = cl;
cl = cl->next;
destroyClient(c);
}
}

inline static Client *pop_client(Connection *con)
{
Client *c, *p;
Expand All @@ -541,36 +564,26 @@ inline static Client *pop_client(Connection *con)

static int io_disconnect(Connection *con)
{
SOCKET sock = getSocket(con);
int err = C_OK;
char now[32];
Now32(now);
SOCKET sock = getSocket(con);
Client *c = pop_client(con);
if (c)
{
char now[32];
Now32(now);
fprintf(stdout, "%s (%d) (pid %d) Connection disconnected from %s@%s [%s]\r\n",
now, (int)sock, getpid(), c->username, c->host, c->iphost);
c->connection = NULL;
destroyClient(c);
}
if (sock != INVALID_SOCKET)
{
Client *c = pop_client(con);
if (c)
{
#ifdef _TCP
if (FD_ISSET(sock, &fdactive))
{
FD_CLR(sock, &fdactive);
#else
if (server_epoll != -1)
{
udt_epoll_remove_usock(server_epoll, sock);
#endif
printf("%s (%d) (pid %d) Connection disconnected from %s@%s [%s]\r\n",
now, (int)sock, getpid(), c->username, c->host, c->iphost);
}
destroyClient(c);
}
#ifdef _TCP
err = shutdown(sock, SHUT_RDWR);
#endif
err = close(sock);
}
fflush(stdout);
fflush(stderr);
return err;
}

Expand All @@ -595,8 +608,9 @@ static int run_server_mode(Options *options)
int status;
do
{
status = DoMessageC(connection);
status = ConnectionDoMessage(connection);
} while (STATUS_OK);
destroyConnection(connection);
return C_ERROR;
}

Expand All @@ -605,11 +619,13 @@ static void *client_thread(void *args)
Client *client = (Client *)args;
Connection *connection = client->connection;
MdsSetClientAddr(client->addr);
pthread_cleanup_push((void *)destroyConnection, (void *)connection);
int status;
do
{
status = DoMessageC(connection);
status = ConnectionDoMessage(connection);
} while (STATUS_OK);
pthread_cleanup_pop(1);
return NULL;
}

Expand All @@ -623,23 +639,15 @@ static inline int dispatch_client(Client *client)
perror("dispatch_client");
free(client->thread);
client->thread = NULL;
destroyConnection(client->connection);
return err;
client->connection->id = INVALID_CONNECTION_ID;
destroyClient(client);
}
return err;
}

static inline void destroyClientList()
{
Client *c, *n;
pthread_mutex_lock(&ClientListLock);
n = ClientList;
ClientList = NULL;
pthread_mutex_unlock(&ClientListLock);
while ((c = n))
else
{
n = c->next;
destroyConnection(c->connection);
destroyClient(c);
pthread_mutex_lock(&ClientListLock);
client->next = ClientList;
ClientList = client;
pthread_mutex_unlock(&ClientListLock);
}
}
return err;
}
2 changes: 1 addition & 1 deletion mdstcpip/mdsip_connections.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ EXPORT int DisconnectFromMds(int id);
/// \return the status of the Process message.
///
EXPORT int DoMessage(int id);
EXPORT int DoMessageC(Connection *connection);
EXPORT int ConnectionDoMessage(Connection *connection);
EXPORT int destroyConnection(Connection *connection);

////////////////////////////////////////////////////////////////////////////////
Expand Down
37 changes: 20 additions & 17 deletions mdstcpip/mdsipshr/Connections.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,31 @@ extern int TdiDeleteContext();
extern int MDSEventCan();
int destroyConnection(Connection *connection)
{
if (connection && connection->id != INVALID_CONNECTION_ID //
&& connection->state != CON_DETACHED)
{ // connection should not have been in list at this point
connection = PopConnection(connection->id);
}
if (!connection)
return MDSplusERROR;

MdsEventList *e, *nexte;
for (e = connection->event; e; e = nexte)
if (connection->id != INVALID_CONNECTION_ID)
{
nexte = e->next;
MDSEventCan(e->eventid);
if (e->info_len > 0)
free(e->info);
free(e);
if (connection->state != CON_DETACHED)
{ // connection should not have been in list at this point
if (connection != PopConnection(connection->id))
fprintf(stderr, "Connections: %02d not detached but not found\n",
connection->id);
}
MdsEventList *e, *nexte;
for (e = connection->event; e; e = nexte)
{
nexte = e->next;
MDSEventCan(e->eventid);
if (e->info_len > 0)
free(e->info);
free(e);
}
TdiDeleteContext(connection->tdicontext);
connection->io->disconnect(connection);
DBG("Connections: %02d disconnected\n", c->id);
FreeDescriptors(connection);
}
TdiDeleteContext(connection->tdicontext);
connection->io->disconnect(connection);
DBG("Connections: %02d disconnected\n", c->id);
free(connection->info);
FreeDescriptors(connection);
free(connection->protocol);
free(connection->info_name);
free(connection->rm_user);
Expand Down
7 changes: 4 additions & 3 deletions mdstcpip/mdsipshr/DoMessage.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern void ProcessMessage(Connection *, Message *);
////////////////////////////////////////////////////////////////////////////////
// DoMessage /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int DoMessageC(Connection *connection)
int ConnectionDoMessage(Connection *connection)
{
if (!connection)
return 0; // will cause tunnel to terminate
Expand All @@ -44,14 +44,15 @@ int DoMessageC(Connection *connection)
return 1;
}
free(message);
destroyConnection(connection);
return 0;
}

int DoMessage(int id)
{
Connection *connection = FindConnectionWithLock(id, CON_ACTIVITY);
int status = DoMessageC(connection);
int status = ConnectionDoMessage(connection);
UnlockConnection(connection);
if (!status)
destroyConnection(connection);
return status;
}

0 comments on commit e684a3d

Please sign in to comment.