From d21097cf2641e7c9df82e79ac38c6cffb2e9a2f6 Mon Sep 17 00:00:00 2001 From: Stuart Inglis Date: Wed, 31 Jan 2024 15:00:29 +1300 Subject: [PATCH] updates --- spit/CMakeLists.txt | 2 +- spit/blockdevices.c | 4 ++-- spit/blockdevicesMain.c | 4 ++-- spit/cluster.c | 23 +++++++++++++++++++---- spit/cluster.h | 2 +- spit/clusterMain.c | 6 +++++- spit/interfaces.c | 1 + spit/keyvalue.c | 9 +++++++-- spit/respond-mc.c | 17 +++++++++++------ spit/sat.c | 3 +-- 10 files changed, 50 insertions(+), 21 deletions(-) diff --git a/spit/CMakeLists.txt b/spit/CMakeLists.txt index 3f9cd4e2..8c393318 100644 --- a/spit/CMakeLists.txt +++ b/spit/CMakeLists.txt @@ -95,7 +95,7 @@ target_link_libraries(pair spitlib m ) add_executable(raidsimulation raidsimulationmain.c failureType.c deviceProbs.c numList.c ) target_link_libraries(raidsimulation m ) -add_executable(stush stush.c snack.h snack.c failureType.c deviceProbs.c pciUtils.c dns.c qr/qrcodegen.c tpmtotp/base32.c TOTP-MCU/sha1.c TOTP-MCU/TOTP.c auth.c qr.c status.c iprange.c simpmail.c simpmail.h simpsock.c simpsock.h blockdevices.c keyvalue.c) +add_executable(stush stush.c snack.h snack.c failureType.c deviceProbs.c pciUtils.c dns.c qr/qrcodegen.c tpmtotp/base32.c TOTP-MCU/sha1.c TOTP-MCU/TOTP.c auth.c qr.c status.c iprange.c simpmail.c simpmail.h simpsock.c simpsock.h blockdevices.c keyvalue.c advertise-mc.c respond-mc.c cluster.c) target_link_libraries(stush spitlib m pthread aio numa readline pci uuid) diff --git a/spit/blockdevices.c b/spit/blockdevices.c index b22cdfcc..0216058c 100644 --- a/spit/blockdevices.c +++ b/spit/blockdevices.c @@ -52,7 +52,7 @@ void blockDevicesScan(blockDevicesType *bd) { if (stat(path, &st) == 0) { if (st.st_mode | S_IFBLK) { int fd = open(path, O_RDONLY); - if (fd) { + if (fd >= 0) { keyvalueType *k = keyvalueInit(); char *suf = getSuffix(path); @@ -123,7 +123,7 @@ void blockDevicesScan(blockDevicesType *bd) { free(model); free(scsi); } - } + } // fd close(fd); } } diff --git a/spit/blockdevicesMain.c b/spit/blockdevicesMain.c index 5f700842..7cb039f2 100644 --- a/spit/blockdevicesMain.c +++ b/spit/blockdevicesMain.c @@ -7,8 +7,8 @@ int main() { blockDevicesType *bd = blockDevicesInit(); blockDevicesScan(bd); - blockDevicesScan(bd); - blockDevicesScan(bd); + // blockDevicesScan(bd); + // blockDevicesScan(bd); for (size_t i = 0; i < bd->num; i++) { char *s = keyvalueDumpAsString(bd->devices[i].kv); diff --git a/spit/cluster.c b/spit/cluster.c index 9075eae4..a6514736 100644 --- a/spit/cluster.c +++ b/spit/cluster.c @@ -14,7 +14,7 @@ // a cluster all has the same port clusterType * clusterInit(const size_t port) { fprintf(stderr,"clusterInit on port %zd\n", port); - clusterType *p = calloc(1, sizeof(clusterType)); assert(p); + clusterType *p = calloc(sizeof(clusterType), 1); assert(p); p->port = port; return p; @@ -49,7 +49,7 @@ int clusterAddNode(clusterType *c, const char *nodename, const double createdtim c->id++; c->node = realloc(c->node, c->id * sizeof(clusterNodeType *)); - c->node[index] = calloc(1, sizeof(clusterNodeType)); // create cluster node + c->node[index] = calloc(sizeof(clusterNodeType), 1); // create cluster node c->node[index]->name = strdup(nodename); c->node[index]->ipaddress = strdup(nodename); c->node[index]->created = createdtime; @@ -86,8 +86,8 @@ char *clusterDumpJSONString(clusterType *c) { // sort if (c) - for (size_t i = 0; i < c->id-1; i++) { - for (size_t j = i+1; j < c->id; j++) { + for (int i = 0; i < c->id-1; i++) { + for (int j = i+1; j < c->id; j++) { if (strcmp(c->node[i]->ipaddress, c->node[j]->ipaddress) > 0) { // swap clusterNodeType t = *c->node[i]; @@ -106,6 +106,7 @@ char *clusterDumpJSONString(clusterType *c) { for (size_t i = 0; i < c->id; i++) { buf += sprintf(buf, " {\n"); buf += sprintf(buf, " \"node\": \"%s\",\n", c->node[i]->name); + buf += sprintf(buf, " \"hostname\": \"%s\",\n", c->node[i]->hostname); buf += sprintf(buf, " \"lastseen\": %.0lf,\n", now - c->node[i]->seen); buf += sprintf(buf, " \"created\": %lf,\n", c->node[i]->created); buf += sprintf(buf, " \"age\": %lf,\n", now - c->node[i]->created); @@ -158,3 +159,17 @@ char *clusterGetNodeIP(clusterType *c, size_t nodeid) { void clusterUpdateSeen(clusterType *c, const size_t nodeid) { c->node[nodeid]->seen = timeAsDouble(); } + +void clusterFree(clusterType *c) { + const size_t index = c->id; + for (int i = 0; i node[i]->name); + free(c->node[i]->hostname); + free(c->node[i]->ipaddress); + free(c->node[i]->osrelease); + free(c->node[i]); + } + free(c->node); + free(c); +} + diff --git a/spit/cluster.h b/spit/cluster.h index a8746d21..427ec2ae 100644 --- a/spit/cluster.h +++ b/spit/cluster.h @@ -24,7 +24,7 @@ typedef struct { typedef struct { - size_t id; // count + int id; // count size_t port; double latestchange; diff --git a/spit/clusterMain.c b/spit/clusterMain.c index 711ba869..eef9ccbc 100644 --- a/spit/clusterMain.c +++ b/spit/clusterMain.c @@ -11,13 +11,15 @@ int main() { printf("%s", s); free(s); + clusterType *c = clusterInit(1600); - + printf("\n"); s=clusterDumpJSONString(c); printf("%s", s); free(s); clusterAddNode(c, "stu", timeAsDouble()); + c->node[0]->hostname = strdup("cool"); clusterAddNode(c, "nic", timeAsDouble()); clusterAddNode(c, "nic", timeAsDouble()); @@ -27,5 +29,7 @@ int main() { printf("%s", s); free(s); + clusterFree(c); + return 0; } diff --git a/spit/interfaces.c b/spit/interfaces.c index 82dba84f..090c4129 100644 --- a/spit/interfaces.c +++ b/spit/interfaces.c @@ -285,6 +285,7 @@ void interfacesFree(interfacesIntType *n) { } free(p->addr); p->addr = NULL; free(p->devicename); p->devicename = NULL; + free(p->label); p->label = NULL; free(p->hw); p->hw = NULL; free(p); p = NULL; } diff --git a/spit/keyvalue.c b/spit/keyvalue.c index 82b40818..0f861c83 100644 --- a/spit/keyvalue.c +++ b/spit/keyvalue.c @@ -36,14 +36,14 @@ void keyvalueParsePair(keyvalueType *kv, char *pp) { char *key = pp; char *value = ch+1; - keyvalueSetString(kv, strdup(key), strdup(value)); + keyvalueSetString(kv, key, value); } else { ch = strchr(pp, ';'); // longs if (ch) { *ch = 0; char *key = pp; char *value = ch+1; - keyvalueSetLong(kv, strdup(key), atol(value)); + keyvalueSetLong(kv, key, atol(value)); } else { fprintf(stderr,"pair with '%s' has no value\n", pp); } @@ -60,6 +60,7 @@ keyvalueType *keyvalueInitFromString(char *par) { keyvalueParsePair(p, tok); while ((tok = strtok(NULL, " "))) { + // fprintf(stderr,"storing: %s\n", tok); keyvalueParsePair(p, tok); } @@ -228,6 +229,10 @@ long keyvalueGetLong(keyvalueType *kv, const char *key) { char *keyvalueGetString(keyvalueType *kv, const char *key) { int index = keyvalueFindKey(kv, key); + if (index < 0) { + fprintf(stderr,"can't find: %s\n", key); + return NULL; + } assert(kv->pairs[index].type != 1); if (index >= 0) { return kv->pairs[index].value; diff --git a/spit/respond-mc.c b/spit/respond-mc.c index 961ec192..f8d13e1d 100644 --- a/spit/respond-mc.c +++ b/spit/respond-mc.c @@ -36,7 +36,7 @@ void *respondMC(void *arg) { int sock, cnt; socklen_t addrlen; struct ip_mreq mreq; - char *message = calloc(200, 1); assert(message); + char *message = calloc(300, 1); assert(message); /* set up socket */ sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -78,7 +78,7 @@ void *respondMC(void *arg) { while (1) { - cnt = recvfrom(sock, message, 200, 0, + cnt = recvfrom(sock, message, 300, 0, (struct sockaddr *) &addr, &addrlen); if (cnt < 0) { perror("recvfrom"); @@ -86,7 +86,7 @@ void *respondMC(void *arg) { } else if (cnt == 0) { break; } - if (cnt < 200) message[cnt] = 0; // make it terminated nicely + if (cnt < 300) message[cnt] = 0; // make it terminated nicely // fprintf(stderr, "**NEW** should try and connect to '%s' message = \"%s\"\n", inet_ntoa(addr.sin_addr), message); @@ -112,8 +112,10 @@ void *respondMC(void *arg) { if ((nodeid = clusterFindNode(cluster, nodename)) < 0) { // add and say hi - fprintf(stderr, "adding node %s\n", nodename); + char *hostname = keyvalueGetString(kv, "hostname"); + fprintf(stderr, "adding node %s (%s)\n", nodename, hostname); nodeid = clusterAddNode(cluster, nodename, startedtime); + cluster->node[nodeid]->hostname= hostname; cluster->node[nodeid]->HDDcount = keyvalueGetLong(kv, "HDDcount"); cluster->node[nodeid]->HDDsizeGB= keyvalueGetLong(kv, "HDDsizeGB"); cluster->node[nodeid]->SSDcount = keyvalueGetLong(kv, "SSDcount"); @@ -123,12 +125,15 @@ void *respondMC(void *arg) { cluster->node[nodeid]->Cores = keyvalueGetLong(kv, "Cores"); } - keyvalueFree(kv); if (strcmp(clusterGetNodeIP(cluster, nodeid), node) != 0) { - fprintf(stderr, "updating node %s IP %s\n", cluster->node[nodeid]->name, node); + // fprintf(stderr, "updating nodeid %d, %s IP %s: '^%s'\n", nodeid, cluster->node[nodeid]->name, node, message); clusterSetNodeIP(cluster, nodeid, node); + cluster->node[nodeid]->hostname= keyvalueGetString(kv, "hostname"); } + + keyvalueFree(kv); + clusterUpdateSeen(cluster, nodeid); diff --git a/spit/sat.c b/spit/sat.c index 992c33f0..924ec8af 100644 --- a/spit/sat.c +++ b/spit/sat.c @@ -224,12 +224,11 @@ void startThreads(interfacesIntType *n, const int serverport) { } } - sleep(100); - for (size_t i = 0; i < num; i++) { pthread_join(pt[i], NULL); // printf("thread %zd finished %d\n", i, keepRunning); } + clusterFree(cluster); free(tc); free(pt); }