From 25cf3a858505fc0d046a13fc931fc07edc9866cf Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Thu, 18 Nov 2021 14:05:19 +0100 Subject: [PATCH] Allocate only the needed looms --- emu.h | 2 +- ovni.h | 1 - trace.c | 101 +++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/emu.h b/emu.h index b6bab9a..575f6f3 100644 --- a/emu.h +++ b/emu.h @@ -395,7 +395,7 @@ struct ovni_loom { struct ovni_trace { size_t nlooms; - struct ovni_loom loom[OVNI_MAX_LOOM]; + struct ovni_loom *loom; size_t nstreams; struct ovni_stream *stream; diff --git a/ovni.h b/ovni.h index 923b7a0..98268ae 100644 --- a/ovni.h +++ b/ovni.h @@ -43,7 +43,6 @@ typedef struct json_value_t JSON_Value; #define OVNI_MAX_CPU 256 #define OVNI_MAX_PROC 256 #define OVNI_MAX_THR 256 -#define OVNI_MAX_LOOM 4 #define OVNI_TRACEDIR "ovni" #define OVNI_MAX_HOSTNAME 512 diff --git a/trace.c b/trace.c index 108da4d..a31fc90 100644 --- a/trace.c +++ b/trace.c @@ -206,16 +206,16 @@ load_loom(struct ovni_loom *loom, int loomid, char *loomdir) } static int -compare_alph(const void* a, const void* b) +compare_alph(const void *a, const void *b) { - return strcmp(*(const char**)a, *(const char**)b); + return strcmp((const char *)a, (const char *)b); } int ovni_load_trace(struct ovni_trace *trace, char *tracedir) { - char *looms[OVNI_MAX_LOOM]; - char *hosts[OVNI_MAX_LOOM]; + char (*looms)[PATH_MAX]; + char (*hosts)[PATH_MAX]; const char *loom_name; DIR *dir; struct dirent *dirent; @@ -225,17 +225,60 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir) if((dir = opendir(tracedir)) == NULL) { - fprintf(stderr, "opendir %s failed: %s\n", - tracedir, strerror(errno)); + err("opendir %s failed: %s\n", tracedir, strerror(errno)); return -1; } - for(l=0; lnlooms++; } + closedir(dir); + + if(trace->nlooms == 0) + { + err("cannot find any loom in %s\n", tracedir); + return -1; + } + + /* Then allocate the loom array */ + trace->loom = calloc(trace->nlooms, sizeof(struct ovni_loom)); + + if(trace->loom == NULL) + { + perror("calloc failed\n"); + return -1; + } + + if((looms = calloc(trace->nlooms, PATH_MAX)) == NULL) + { + perror("calloc failed\n"); + return -1; + } + + if((hosts = calloc(trace->nlooms, PATH_MAX)) == NULL) + { + perror("calloc failed\n"); + return -1; + } + + /* Read again the directory */ + if((dir = opendir(tracedir)) == NULL) + { + err("opendir %s failed: %s\n", tracedir, strerror(errno)); + return -1; + } + + l = 0; + while((dirent = readdir(dir)) != NULL) { if(find_dir_prefix_str(dirent, "loom", &loom_name) != 0) @@ -244,29 +287,40 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir) continue; } - if(trace->nlooms >= OVNI_MAX_LOOM) + if(l >= trace->nlooms) { - err("too many looms for trace %s\n", - tracedir); - abort(); + err("extra loom detected\n"); + return -1; } - sprintf(hosts[trace->nlooms], "%s", loom_name); + if(snprintf(hosts[l], PATH_MAX, "%s", + loom_name) >= PATH_MAX) + { + err("error: hostname %s too long\n", loom_name); + return -1; + } - sprintf(looms[trace->nlooms], "%s/%s", tracedir, dirent->d_name); + if(snprintf(looms[l], PATH_MAX, "%s/%s", + tracedir, dirent->d_name) >= PATH_MAX) + { + err("error: loom name %s too long\n", loom_name); + return -1; + } - trace->nlooms++; + l++; } - qsort((const char **) hosts, trace->nlooms, sizeof(const char*), compare_alph); - qsort((const char **) looms, trace->nlooms, sizeof(const char*), compare_alph); + closedir(dir); + + qsort(hosts, trace->nlooms, PATH_MAX, compare_alph); + qsort(looms, trace->nlooms, PATH_MAX, compare_alph); for(l=0; lnlooms; l++) { if(strlen(hosts[l]) >= PATH_MAX) { err("error hostname too long: %s\n", hosts[l]); - exit(EXIT_FAILURE); + return -1; } /* Safe */ @@ -276,13 +330,8 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir) return -1; } - closedir(dir); - - for(l=0; l