X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=misc%2Flnstat_util.c;h=c2dc42ec1ff12a4848691d2e1ea2b642f0aa9631;hb=25c6339b223f17d1603702c0c87f06b252bb4949;hp=6dde7c49432714431157df06d32e8d56bc5d9433;hpb=92de1c2c820816801681d76f5de7137a2b5996df;p=mirror_iproute2.git diff --git a/misc/lnstat_util.c b/misc/lnstat_util.c index 6dde7c49..c2dc42ec 100644 --- a/misc/lnstat_util.c +++ b/misc/lnstat_util.c @@ -38,22 +38,27 @@ /* Read (and summarize for SMP) the different stats vars. */ static int scan_lines(struct lnstat_file *lf, int i) { + char buf[FGETS_BUF_SIZE]; int j, num_lines = 0; for (j = 0; j < lf->num_fields; j++) lf->fields[j].values[i] = 0; - while(!feof(lf->fp)) { - char buf[FGETS_BUF_SIZE]; + rewind(lf->fp); + /* skip first line */ + if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp)) + return -1; + + while (!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) { char *ptr = buf; num_lines++; - fgets(buf, sizeof(buf)-1, lf->fp); gettimeofday(&lf->last_read, NULL); for (j = 0; j < lf->num_fields; j++) { unsigned long f = strtoul(ptr, &ptr, 16); + if (j == 0) lf->fields[j].values[i] = f; else @@ -81,7 +86,6 @@ static int time_after(struct timeval *last, int lnstat_update(struct lnstat_file *lnstat_files) { struct lnstat_file *lf; - char buf[FGETS_BUF_SIZE]; struct timeval tv; gettimeofday(&tv, NULL); @@ -91,11 +95,6 @@ int lnstat_update(struct lnstat_file *lnstat_files) int i; struct lnstat_field *lfi; - rewind(lf->fp); - if (!lf->compat) { - /* skip first line */ - fgets(buf, sizeof(buf)-1, lf->fp); - } scan_lines(lf, 1); for (i = 0, lfi = &lf->fields[i]; @@ -104,11 +103,9 @@ int lnstat_update(struct lnstat_file *lnstat_files) lfi->result = lfi->values[1]; else lfi->result = (lfi->values[1]-lfi->values[0]) - / lf->interval.tv_sec; + / lf->interval.tv_sec; } - rewind(lf->fp); - fgets(buf, sizeof(buf)-1, lf->fp); scan_lines(lf, 0); } } @@ -142,7 +139,8 @@ static int lnstat_scan_fields(struct lnstat_file *lf) char buf[FGETS_BUF_SIZE]; rewind(lf->fp); - fgets(buf, sizeof(buf)-1, lf->fp); + if (!fgets(buf, sizeof(buf)-1, lf->fp)) + return -1; return __lnstat_scan_fields(lf, buf); } @@ -152,7 +150,8 @@ static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf) { char buf[FGETS_BUF_SIZE]; - strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1); + strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; return __lnstat_scan_fields(lf, buf); } @@ -161,6 +160,7 @@ static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf) static int name_in_array(const int num, const char **arr, const char *name) { int i; + for (i = 0; i < num; i++) { if (!strcmp(arr[i], name)) return 1; @@ -174,18 +174,15 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file) struct lnstat_file *lf; /* allocate */ - lf = malloc(sizeof(*lf)); - if (!lf) + lf = calloc(1, sizeof(*lf)); + if (!lf) { + fprintf(stderr, "out of memory\n"); return NULL; + } /* initialize */ - memset(lf, 0, sizeof(*lf)); - - /* de->d_name is guaranteed to be <= NAME_MAX */ - strcpy(lf->basename, file); - strcpy(lf->path, path); - strcat(lf->path, "/"); - strcat(lf->path, lf->basename); + snprintf(lf->basename, sizeof(lf->basename), "%s", file); + snprintf(lf->path, sizeof(lf->path), "%s/%s", path, file); /* initialize to default */ lf->interval.tv_sec = 1; @@ -193,6 +190,7 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file) /* open */ lf->fp = fopen(lf->path, "r"); if (!lf->fp) { + perror(lf->path); free(lf); return NULL; } @@ -259,12 +257,16 @@ struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files, continue; lf = alloc_and_open(path, de->d_name); - if (!lf) + if (!lf) { + closedir(dir); return NULL; + } /* fill in field structure */ - if (lnstat_scan_fields(lf) < 0) + if (lnstat_scan_fields(lf) < 0) { + closedir(dir); return NULL; + } /* prepend to global list */ lf->next = lnstat_files;