diff --git a/treeshr/RemoteAccess.c b/treeshr/RemoteAccess.c index 6864c93096..b811398d2e 100644 --- a/treeshr/RemoteAccess.c +++ b/treeshr/RemoteAccess.c @@ -84,8 +84,8 @@ static int remote_connect(char *server) int conid = -1; MDSSHR_LOAD_LIBROUTINE_LOCAL(MdsIpShr, ReuseCheck, return conid, int, (char *, char *, int)); MDSSHR_LOAD_LIBROUTINE_LOCAL(MdsIpShr, ConnectToMds, return conid, int, (char *)); - char unique[128] = ""; - if (ReuseCheck(server, unique, 128) < 0) + char unique[HOST_UNIQUE_SIZE] = ""; + if (ReuseCheck(server, unique, HOST_UNIQUE_SIZE) < 0) { conid = ConnectToMds(server); if (conid == -1) @@ -103,7 +103,7 @@ static int remote_connect(char *server) TREETHREADSTATIC_INIT; for (host = TREE_HOSTLIST; host; host = host->next) { - if (!strcmp(host->unique, unique)) + if (!strncmp(host->unique, unique, HOST_UNIQUE_SIZE)) { conid = host->conid; host->links++; @@ -121,7 +121,7 @@ static int remote_connect(char *server) host = malloc(sizeof(Host)); host->conid = conid; host->links = 1; - host->unique = strdup(unique); + strncpy(host->unique, unique, HOST_UNIQUE_SIZE); host->next = TREE_HOSTLIST; TREE_HOSTLIST = host; MDSDBG(HOST_PRI ", server='%s': new", HOST_VAR(host), server); @@ -141,8 +141,8 @@ static int remote_disconnect(int conid, int force) if (conid == -1) return TreeSUCCESS; TREETHREADSTATIC_INIT; - Host **prev = &TREE_HOSTLIST, *host = TREE_HOSTLIST; - for (; host; prev = &host->next, host = host->next) + Host **phost = &TREE_HOSTLIST, *host = TREE_HOSTLIST; + for (; host; phost = &host->next, host = host->next) { if (host->conid == conid) { @@ -155,14 +155,14 @@ static int remote_disconnect(int conid, int force) { if (force) { + *phost = host->next; MDSWRN(HOST_PRI " disconnected", HOST_VAR(host)); + destroy_host(host); } else { - MDSDBG(HOST_PRI " disconnected", HOST_VAR(host)); + MDSDBG(HOST_PRI " idle", HOST_VAR(host)); } - *prev = host->next; - destroy_host(host); } break; } @@ -170,6 +170,25 @@ static int remote_disconnect(int conid, int force) return TreeSUCCESS; } +void TreeCleanupConnections() +{ + TREETHREADSTATIC_INIT; + Host **phost = &TREE_HOSTLIST, *host = TREE_HOSTLIST; + for (; host; host = *phost) + { + if (host->links <= 0) + { + *phost = host->next; + MDSDBG(HOST_PRI " cleaned up", HOST_VAR(host)); + destroy_host(host); + } + else + { + phost = &host->next; + } + } +} + /////////////////////////////////////////////////////////////////// ///////OLD THICK CLIENT//////////////////////////////////////////// /////////////////////////////////////////////////////////////////// diff --git a/treeshr/TreeOpen.c b/treeshr/TreeOpen.c index f38ff6de52..87eb4c03ac 100644 --- a/treeshr/TreeOpen.c +++ b/treeshr/TreeOpen.c @@ -83,7 +83,7 @@ static int GetVmForTree(TREE_INFO *info, int nomap); static int MapTree(TREE_INFO *info, TREE_INFO *root, int edit_flag); static void SubtreeNodeConnect(PINO_DATABASE *dblist, NODE *parent, NODE *subtreetop); - +extern void TreeCleanupConnections(); extern void **TreeCtx(); int TreeClose(char const *tree, int shot) @@ -325,6 +325,7 @@ int _TreeClose(void **dbid, char const *tree, int shot) status = TreeNOT_OPEN; } } + TreeCleanupConnections(); return status; } @@ -1351,6 +1352,7 @@ int _TreeOpenEdit(void **dbid, char const *tree_in, int shot_in) free_top_db(dblist); } } + TreeCleanupConnections(); return status; } @@ -1465,6 +1467,7 @@ int _TreeOpenNew(void **dbid, char const *tree_in, int shot_in) _TreeWriteTree(dbid, 0, 0); MDS_IO_CLOSE(fd_keepalive); } + TreeCleanupConnections(); return status; } diff --git a/treeshr/TreeThreadStatic.c b/treeshr/TreeThreadStatic.c index 6050ebd33d..3b0946d8da 100644 --- a/treeshr/TreeThreadStatic.c +++ b/treeshr/TreeThreadStatic.c @@ -49,7 +49,6 @@ void destroy_host(Host *host) MDSSHR_LOAD_LIBROUTINE_LOCAL(MdsIpShr, DisconnectFromMds, abort(), void, (int)); MDSDBG(HOST_PRI, HOST_VAR(host)); DisconnectFromMds(host->conid); - free(host->unique); free(host); } diff --git a/treeshr/treethreadstatic.h b/treeshr/treethreadstatic.h index c2e627dc8d..88badfa835 100644 --- a/treeshr/treethreadstatic.h +++ b/treeshr/treethreadstatic.h @@ -4,12 +4,13 @@ #include "../mdsshr/mdsthreadstatic.h" #include "treeshrp.h" +#define HOST_UNIQUE_SIZE 64 typedef struct host { struct host *next; int conid; int links; - char *unique; + char unique[HOST_UNIQUE_SIZE]; } Host; #define HOST_PRI "Host(conid=%d, links=%d, unique='%s')" #define HOST_VAR(h) (h)->conid, (h)->links, (h)->unique