rework the networking locking facility using the hashlist.
authorWilfried Goesgens <dothebart@citadel.org>
Mon, 17 Oct 2011 22:18:43 +0000 (00:18 +0200)
committerWilfried Goesgens <dothebart@citadel.org>
Mon, 17 Oct 2011 22:18:43 +0000 (00:18 +0200)
citadel/file_ops.c
citadel/file_ops.h
citadel/modules/network/serv_netconfig.c
citadel/modules/network/serv_netspool.c
citadel/user_ops.c

index 4ec3f8e5149029531728344a191f7f38b80c6105..9040c4c746b4d009ba5cad19ebc91644be48c1b3 100644 (file)
 /*
  * 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);
 }
 
index 03c3dc7ed8647139398d44dd23ad80b3ca6fbee2..a2483a126e115446026c5a1c67ffcd85849437a5 100644 (file)
@@ -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()
index c6acdf55d62aa103f524328cfacc85a4b6468d2a..4f5eac42194a6c97f9f36791640262cd807c5719 100644 (file)
@@ -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
        );
index f0128f0168fffdd5135000912a40f47ba69d8786..5dd1eafb2be0350e47f81570fdf7832c74ec85b7 100644 (file)
@@ -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);
                        }
                }
        }
index 4ebc7bdc23ae07e45801c2555d29c82a2e07b08f..7d4088c56fb20c023eeb724f2d8fd5fcfc8fbfee 100644 (file)
@@ -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... */