]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/nstat.c
ss: do not emit warn while dumping MPTCP on old kernels
[mirror_iproute2.git] / misc / nstat.c
index 6143719e3a07a7d7f01f714323bdf84b513f590c..ecdd4ce8266db0aa3825f2bd39986bc186ff846e 100644 (file)
@@ -29,7 +29,7 @@
 #include <getopt.h>
 
 #include <json_writer.h>
-#include <SNAPSHOT.h>
+#include "version.h"
 #include "utils.h"
 
 int dump_zeros;
@@ -37,7 +37,6 @@ int reset_history;
 int ignore_history;
 int no_output;
 int json_output;
-int pretty;
 int no_update;
 int scan_interval;
 int time_constant;
@@ -76,6 +75,11 @@ static int net_snmp6_open(void)
        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;
@@ -110,7 +114,7 @@ static int match(const 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;
@@ -132,20 +136,24 @@ static void load_good_table(FILE *fp)
                        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;
@@ -173,19 +181,24 @@ static int count_spaces(const char *line)
 
 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;
@@ -205,8 +218,10 @@ static void load_ugly_table(FILE *fp)
                                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;
@@ -214,18 +229,28 @@ static void load_ugly_table(FILE *fp)
                        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--;
@@ -233,6 +258,7 @@ static void load_ugly_table(FILE *fp)
                                n = n->next;
                } while (p > buf + off + 2);
        }
+       free(buf);
 
        while (db) {
                n = db;
@@ -247,6 +273,16 @@ static void load_ugly_table(FILE *fp)
        }
 }
 
+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");
@@ -391,6 +427,7 @@ static void update_db(int interval)
        load_netstat();
        load_snmp6();
        load_snmp();
+       load_sctp_snmp();
 
        h = kern_db;
        kern_db = n;
@@ -450,6 +487,7 @@ static void server_loop(int fd)
        load_netstat();
        load_snmp6();
        load_snmp();
+       load_sctp_snmp();
 
        for (;;) {
                int status;
@@ -508,18 +546,18 @@ static void usage(void) __attribute__((noreturn));
 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);
 }
 
@@ -582,7 +620,7 @@ int main(int argc, char *argv[])
                        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 '?':
@@ -689,12 +727,18 @@ int main(int argc, char *argv[])
            && 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);
@@ -706,6 +750,7 @@ int main(int argc, char *argv[])
                load_netstat();
                load_snmp6();
                load_snmp();
+               load_sctp_snmp();
                if (info_source[0] == 0)
                        strcpy(info_source, "kernel");
        }