From 78133bda9c752e761ae00b6a43d2ddf5c67af53f Mon Sep 17 00:00:00 2001 From: joeyh Date: Sat, 25 Mar 2006 17:52:25 +0000 Subject: [PATCH] * ifdata: robustness patch from Adam Lackorzynski, in particular deal with /proc not mounted. Closes: #358930 --- debian/changelog | 7 ++++++ ifdata.c | 62 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/debian/changelog b/debian/changelog index 0fd520c..ad82f51 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +moreutils (0.9) UNRELEASED; urgency=low + + * ifdata: robustness patch from Adam Lackorzynski, in particular deal with + /proc not mounted. Closes: #358930 + + -- Joey Hess Sat, 25 Mar 2006 12:51:26 -0500 + moreutils (0.8) unstable; urgency=low * Back to Mithandir's C sponge, now fixed. diff --git a/ifdata.c b/ifdata.c index 92b68a6..c9a4622 100644 --- a/ifdata.c +++ b/ifdata.c @@ -275,9 +275,15 @@ struct if_stat *get_stats(char *iface) { int state=START; int len; struct if_stat *res=malloc(sizeof(struct if_stat)); + + if (!res) { + perror("malloc"); + return NULL; + } + fd=open("/proc/net/dev",O_RDONLY); if (fd==-1) { - perror("open"); + perror("open(\"/proc/net/dev\")"); return NULL; } while ((len=read(fd,buffer,4096))) { @@ -442,39 +448,48 @@ void please_do(int ndo, int *todo, char *ifname) { break; case DO_SINPACKETS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_packets); + if (stats) + printf("%llu",stats->in_packets); break; case DO_SINBYTES: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_bytes); + if (stats) + printf("%llu",stats->in_bytes); break; case DO_SINERRORS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_errors); + if (stats) + printf("%llu",stats->in_errors); break; case DO_SINDROPS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_drops); + if (stats) + printf("%llu",stats->in_drops); break; case DO_SINFIFO: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_fifo); + if (stats) + printf("%llu",stats->in_fifo); break; case DO_SINFRAME: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_frame); + if (stats) + printf("%llu",stats->in_frame); break; case DO_SINCOMPRESSES: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_compress); + if (stats) + printf("%llu",stats->in_compress); break; case DO_SINMULTICAST: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->in_multicast); + if (stats) + printf("%llu",stats->in_multicast); break; case DO_SINALL: if (!stats) stats=get_stats(ifname); - printf("%llu %llu %llu %llu %llu %llu %llu %llu", + if (stats) + printf("%llu %llu %llu %llu %llu %llu %llu %llu", stats->in_packets, stats->in_bytes, stats->in_errors, @@ -486,39 +501,48 @@ void please_do(int ndo, int *todo, char *ifname) { break; case DO_SOUTBYTES: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_bytes); + if (stats) + printf("%llu",stats->out_bytes); break; case DO_SOUTPACKETS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_packets); + if (stats) + printf("%llu",stats->out_packets); break; case DO_SOUTERRORS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_errors); + if (stats) + printf("%llu",stats->out_errors); break; case DO_SOUTDROPS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_drops); + if (stats) + printf("%llu",stats->out_drops); break; case DO_SOUTFIFO: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_fifo); + if (stats) + printf("%llu",stats->out_fifo); break; case DO_SOUTCOLLS: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_colls); + if (stats) + printf("%llu",stats->out_colls); break; case DO_SOUTCARRIER: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_carrier); + if (stats) + printf("%llu",stats->out_carrier); break; case DO_SOUTMULTICAST: if (!stats) stats=get_stats(ifname); - printf("%llu",stats->out_multicast); + if (stats) + printf("%llu",stats->out_multicast); break; case DO_SOUTALL: if (!stats) stats=get_stats(ifname); - printf("%llu %llu %llu %llu %llu %llu %llu %llu", + if (stats) + printf("%llu %llu %llu %llu %llu %llu %llu %llu", stats->out_packets, stats->out_bytes, stats->out_errors, -- 2.39.5