From da1debddf5988512ac50ff0b62ac85c8f3b8d0f4 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Tue, 18 Oct 2011 00:18:43 +0200 Subject: [PATCH] rework the networking locking facility using the hashlist. --- citadel/file_ops.c | 62 ++++++++++-------------- citadel/file_ops.h | 2 +- citadel/modules/network/serv_netconfig.c | 7 +-- citadel/modules/network/serv_netspool.c | 14 ++++-- citadel/user_ops.c | 2 +- 5 files changed, 40 insertions(+), 47 deletions(-) diff --git a/citadel/file_ops.c b/citadel/file_ops.c index 4ec3f8e51..9040c4c74 100644 --- a/citadel/file_ops.c +++ b/citadel/file_ops.c @@ -48,56 +48,46 @@ /* * network_talking_to() -- concurrency checker */ -static char *nttlist = NULL; -int network_talking_to(const char *nodename, int operation) { +static HashList *nttlist = NULL; +int network_talking_to(const char *nodename, long len, int operation) { - char *ptr = NULL; - int i; - char buf[SIZ]; int retval = 0; + HashPos *Pos = NULL; + void *vdata; begin_critical_section(S_NTTLIST); switch(operation) { case NTT_ADD: - if (nttlist == NULL) nttlist = strdup(""); - if (nttlist == NULL) break; - nttlist = (char *)realloc(nttlist, - (strlen(nttlist) + strlen(nodename) + 3) ); - strcat(nttlist, "|"); - strcat(nttlist, nodename); + if (nttlist == NULL) + nttlist = NewHash(1, NULL); + Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf); + syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename); break; - case NTT_REMOVE: - if (nttlist == NULL) break; - if (IsEmptyStr(nttlist)) break; - ptr = malloc(strlen(nttlist)); - if (ptr == NULL) break; - strcpy(ptr, ""); - for (i = 0; i < num_tokens(nttlist, '|'); ++i) { - extract_token(buf, nttlist, i, '|', sizeof buf); - if ( (!IsEmptyStr(buf)) - && (strcasecmp(buf, nodename)) ) { - strcat(ptr, buf); - strcat(ptr, "|"); - } - } - free(nttlist); - nttlist = ptr; + if ((nttlist == NULL) || + (GetCount(nttlist) == 0)) + break; + Pos = GetNewHashPos(nttlist, 1); + GetHashPosFromKey (nttlist, nodename, len, Pos); + + DeleteEntryFromHash(nttlist, Pos); + DeleteHashPos(&Pos); + syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename); + break; case NTT_CHECK: - if (nttlist == NULL) break; - if (IsEmptyStr(nttlist)) break; - for (i = 0; i < num_tokens(nttlist, '|'); ++i) { - extract_token(buf, nttlist, i, '|', sizeof buf); - if (!strcasecmp(buf, nodename)) ++retval; - } + if ((nttlist == NULL) || + (GetCount(nttlist) == 0)) + break; + if (!GetHash(nttlist, nodename, len, &vdata)) + retval ++; + syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename); break; } - if (nttlist != NULL) syslog(LOG_DEBUG, "nttlist=<%s>\n", nttlist); end_critical_section(S_NTTLIST); return(retval); } @@ -105,9 +95,7 @@ int network_talking_to(const char *nodename, int operation) { void cleanup_nttlist(void) { begin_critical_section(S_NTTLIST); - if (nttlist != NULL) - free(nttlist); - nttlist = NULL; + DeleteHash(&nttlist); end_critical_section(S_NTTLIST); } diff --git a/citadel/file_ops.h b/citadel/file_ops.h index 03c3dc7ed..a2483a126 100644 --- a/citadel/file_ops.h +++ b/citadel/file_ops.h @@ -6,7 +6,7 @@ void OpenCmdResult (char *, const char *); void abort_upl (CitContext *who); -int network_talking_to(const char *nodename, int operation); +int network_talking_to(const char *nodename, long len, int operation); /* * Operations that can be performed by network_talking_to() diff --git a/citadel/modules/network/serv_netconfig.c b/citadel/modules/network/serv_netconfig.c index c6acdf55d..4f5eac421 100644 --- a/citadel/modules/network/serv_netconfig.c +++ b/citadel/modules/network/serv_netconfig.c @@ -365,6 +365,7 @@ void cmd_netp(char *cmdbuf) { char *working_ignetcfg; char node[256]; + long nodelen; char pass[256]; int v; @@ -373,7 +374,7 @@ void cmd_netp(char *cmdbuf) char err_buf[SIZ]; /* Authenticate */ - extract_token(node, cmdbuf, 0, '|', sizeof node); + nodelen = extract_token(node, cmdbuf, 0, '|', sizeof node); extract_token(pass, cmdbuf, 1, '|', sizeof pass); /* load the IGnet Configuration to check node validity */ @@ -404,7 +405,7 @@ void cmd_netp(char *cmdbuf) return; } - if (network_talking_to(node, NTT_CHECK)) { + if (network_talking_to(node, nodelen, NTT_CHECK)) { syslog(LOG_WARNING, "Duplicate session for network node <%s>", node); cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node); free(working_ignetcfg); @@ -412,7 +413,7 @@ void cmd_netp(char *cmdbuf) } safestrncpy(CC->net_node, node, sizeof CC->net_node); - network_talking_to(node, NTT_ADD); + network_talking_to(node, nodelen, NTT_ADD); syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n", CC->net_node, CC->cs_host, CC->cs_addr ); diff --git a/citadel/modules/network/serv_netspool.c b/citadel/modules/network/serv_netspool.c index f0128f016..5dd1eafb2 100644 --- a/citadel/modules/network/serv_netspool.c +++ b/citadel/modules/network/serv_netspool.c @@ -778,6 +778,7 @@ void network_consolidate_spoolout(char *working_ignetcfg, NetMap *the_netmap) char filename[PATH_MAX]; char cmd[PATH_MAX]; char nexthop[256]; + long nexthoplen; int i; char *ptr; @@ -790,9 +791,12 @@ void network_consolidate_spoolout(char *working_ignetcfg, NetMap *the_netmap) && (strcmp(d->d_name, "..")) && (strchr(d->d_name, '@') != NULL) ) { - safestrncpy(nexthop, d->d_name, sizeof nexthop); + nexthoplen = safestrncpy(nexthop, d->d_name, sizeof nexthop); ptr = strchr(nexthop, '@'); - if (ptr) *ptr = 0; + if (ptr) { + *ptr = 0; + nexthoplen = ptr - nexthop; + } snprintf(filename, sizeof filename, @@ -802,21 +806,21 @@ void network_consolidate_spoolout(char *working_ignetcfg, NetMap *the_netmap) ); syslog(LOG_DEBUG, "Consolidate %s to %s\n", filename, nexthop); - if (network_talking_to(nexthop, NTT_CHECK)) { + if (network_talking_to(nexthop, nexthoplen, NTT_CHECK)) { syslog(LOG_DEBUG, "Currently online with %s - skipping for now\n", nexthop ); } else { - network_talking_to(nexthop, NTT_ADD); + network_talking_to(nexthop, nexthoplen, NTT_ADD); snprintf(cmd, sizeof cmd, "/bin/cat %s >>%s/%s && /bin/rm -f %s", filename, ctdl_netout_dir, nexthop, filename ); system(cmd); - network_talking_to(nexthop, NTT_REMOVE); + network_talking_to(nexthop, nexthoplen, NTT_REMOVE); } } } diff --git a/citadel/user_ops.c b/citadel/user_ops.c index 4ebc7bdc2..7d4088c56 100644 --- a/citadel/user_ops.c +++ b/citadel/user_ops.c @@ -803,7 +803,7 @@ void CtdlUserLogout(void) * If we were talking to a network node, we're not anymore... */ if (!IsEmptyStr(CCC->net_node)) { - network_talking_to(CCC->net_node, NTT_REMOVE); + network_talking_to(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE); } /* Run any hooks registered by modules... */ -- 2.30.2