#include <getopt.h>
#include <json_writer.h>
-#include <SNAPSHOT.h>
+#include "version.h"
#include "utils.h"
int dump_zeros;
int ignore_history;
int no_output;
int json_output;
-int pretty;
int no_update;
int scan_interval;
int time_constant;
return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
}
+static int net_sctp_snmp_open(void)
+{
+ return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp");
+}
+
struct nstat_ent {
struct nstat_ent *next;
char *id;
return 1;
for (i = 0; i < npatterns; i++) {
- if (!fnmatch(patterns[i], id, 0))
+ if (!fnmatch(patterns[i], id, FNM_CASEFOLD))
return 1;
}
return 0;
buf[strlen(buf)-1] = 0;
if (info_source[0] && strcmp(info_source, buf+1))
source_mismatch = 1;
- info_source[0] = 0;
- strncat(info_source, buf+1, sizeof(info_source)-1);
+ strlcpy(info_source, buf + 1, sizeof(info_source));
continue;
}
/* idbuf is as big as buf, so this is safe */
nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate);
- if (nr < 2)
- abort();
+ if (nr < 2) {
+ fprintf(stderr, "%s:%d: error parsing history file\n",
+ __FILE__, __LINE__);
+ exit(-2);
+ }
if (nr < 3)
rate = 0;
if (useless_number(idbuf))
continue;
- if ((n = malloc(sizeof(*n))) == NULL)
- abort();
+ if ((n = malloc(sizeof(*n))) == NULL) {
+ perror("nstat: malloc");
+ exit(-1);
+ }
n->id = strdup(idbuf);
n->val = val;
n->rate = rate;
static void load_ugly_table(FILE *fp)
{
- char buf[4096];
+ char *buf = NULL;
+ size_t buflen = 0;
+ ssize_t nread;
struct nstat_ent *db = NULL;
struct nstat_ent *n;
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- char idbuf[sizeof(buf)];
+ while ((nread = getline(&buf, &buflen, fp)) != -1) {
+ char idbuf[4096];
int off;
char *p;
int count1, count2, skip = 0;
p = strchr(buf, ':');
- if (!p)
- abort();
+ if (!p) {
+ fprintf(stderr, "%s:%d: error parsing history file\n",
+ __FILE__, __LINE__);
+ exit(-2);
+ }
count1 = count_spaces(buf);
*p = 0;
idbuf[0] = 0;
strncat(idbuf, p, sizeof(idbuf) - off - 1);
}
n = malloc(sizeof(*n));
- if (!n)
- abort();
+ if (!n) {
+ perror("nstat: malloc");
+ exit(-1);
+ }
n->id = strdup(idbuf);
n->rate = 0;
n->next = db;
p = next;
}
n = db;
- if (fgets(buf, sizeof(buf), fp) == NULL)
- abort();
+ nread = getline(&buf, &buflen, fp);
+ if (nread == -1) {
+ fprintf(stderr, "%s:%d: error parsing history file\n",
+ __FILE__, __LINE__);
+ exit(-2);
+ }
count2 = count_spaces(buf);
if (count2 > count1)
skip = count2 - count1;
do {
p = strrchr(buf, ' ');
- if (!p)
- abort();
+ if (!p) {
+ fprintf(stderr, "%s:%d: error parsing history file\n",
+ __FILE__, __LINE__);
+ exit(-2);
+ }
*p = 0;
- if (sscanf(p+1, "%llu", &n->val) != 1)
- abort();
+ if (sscanf(p+1, "%llu", &n->val) != 1) {
+ fprintf(stderr, "%s:%d: error parsing history file\n",
+ __FILE__, __LINE__);
+ exit(-2);
+ }
/* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
if (skip)
skip--;
n = n->next;
} while (p > buf + off + 2);
}
+ free(buf);
while (db) {
n = db;
}
}
+static void load_sctp_snmp(void)
+{
+ FILE *fp = fdopen(net_sctp_snmp_open(), "r");
+
+ if (fp) {
+ load_good_table(fp);
+ fclose(fp);
+ }
+}
+
static void load_snmp(void)
{
FILE *fp = fdopen(net_snmp_open(), "r");
load_netstat();
load_snmp6();
load_snmp();
+ load_sctp_snmp();
h = kern_db;
kern_db = n;
load_netstat();
load_snmp6();
load_snmp();
+ load_sctp_snmp();
for (;;) {
int status;
static void usage(void)
{
fprintf(stderr,
-"Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
-" -h, --help this message\n"
-" -a, --ignore ignore history\n"
-" -d, --scan=SECS sample every statistics every SECS\n"
-" -j, --json format output in JSON\n"
-" -n, --nooutput do history only\n"
-" -p, --pretty pretty print\n"
-" -r, --reset reset history\n"
-" -s, --noupdate don\'t update history\n"
-" -t, --interval=SECS report average over the last SECS\n"
-" -V, --version output version information\n"
-" -z, --zeros show entries with zero activity\n");
+ "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
+ " -h, --help this message\n"
+ " -a, --ignore ignore history\n"
+ " -d, --scan=SECS sample every statistics every SECS\n"
+ " -j, --json format output in JSON\n"
+ " -n, --nooutput do history only\n"
+ " -p, --pretty pretty print\n"
+ " -r, --reset reset history\n"
+ " -s, --noupdate don't update history\n"
+ " -t, --interval=SECS report average over the last SECS\n"
+ " -V, --version output version information\n"
+ " -z, --zeros show entries with zero activity\n");
exit(-1);
}
break;
case 'v':
case 'V':
- printf("nstat utility, iproute2-ss%s\n", SNAPSHOT);
+ printf("nstat utility, iproute2-%s\n", version);
exit(0);
case 'h':
case '?':
&& verify_forging(fd) == 0) {
FILE *sfp = fdopen(fd, "r");
- load_good_table(sfp);
- if (hist_db && source_mismatch) {
- fprintf(stderr, "nstat: history is stale, ignoring it.\n");
- hist_db = NULL;
+ if (!sfp) {
+ fprintf(stderr, "nstat: fdopen failed: %s\n",
+ strerror(errno));
+ close(fd);
+ } else {
+ load_good_table(sfp);
+ if (hist_db && source_mismatch) {
+ fprintf(stderr, "nstat: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ }
+ fclose(sfp);
}
- fclose(sfp);
} else {
if (fd >= 0)
close(fd);
load_netstat();
load_snmp6();
load_snmp();
+ load_sctp_snmp();
if (info_source[0] == 0)
strcpy(info_source, "kernel");
}