Commit acd1e437 authored by Stephen Hemminger's avatar Stephen Hemminger

misc: fix style issues

More checkpatch spring cleaning
parent df4b043f
......@@ -47,17 +47,16 @@ int ifnum;
int *ifvec;
char **ifnames;
struct dbkey
{
struct dbkey {
__u32 iface;
__u32 addr;
};
#define IS_NEG(x) (((__u8*)(x))[0] == 0xFF)
#define IS_NEG(x) (((__u8 *)(x))[0] == 0xFF)
#define NEG_TIME(x) (((x)[2]<<24)|((x)[3]<<16)|((x)[4]<<8)|(x)[5])
#define NEG_AGE(x) ((__u32)time(NULL) - NEG_TIME((__u8*)x))
#define NEG_AGE(x) ((__u32)time(NULL) - NEG_TIME((__u8 *)x))
#define NEG_VALID(x) (NEG_AGE(x) < negative_timeout)
#define NEG_CNT(x) (((__u8*)(x))[1])
#define NEG_CNT(x) (((__u8 *)(x))[1])
struct rtnl_handle rth;
......@@ -96,8 +95,7 @@ int poll_timeout = 30000;
static void usage(void)
{
fprintf(stderr,
"Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ]"
" [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
"Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ] [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
exit(1);
}
......@@ -108,7 +106,7 @@ static int handle_if(int ifindex)
if (ifnum == 0)
return 1;
for (i=0; i<ifnum; i++)
for (i = 0; i < ifnum; i++)
if (ifvec[i] == ifindex)
return 1;
return 0;
......@@ -123,7 +121,7 @@ static void do_sysctl_adjustments(void)
if (!ifnum)
return;
for (i=0; i<ifnum; i++) {
for (i = 0; i < ifnum; i++) {
char buf[128];
FILE *fp;
......@@ -133,7 +131,7 @@ static void do_sysctl_adjustments(void)
if (no_kernel_broadcasts)
strcpy(buf, "0\n");
else
sprintf(buf, "%d\n", active_probing>=2 ? 1 : 3-active_probing);
sprintf(buf, "%d\n", active_probing >= 2 ? 1 : 3-active_probing);
fputs(buf, fp);
fclose(fp);
}
......@@ -141,7 +139,7 @@ static void do_sysctl_adjustments(void)
sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]);
if ((fp = fopen(buf, "w")) != NULL) {
sprintf(buf, "%d\n", active_probing<=1 ? 1 : active_probing);
sprintf(buf, "%d\n", active_probing <= 1 ? 1 : active_probing);
fputs(buf, fp);
fclose(fp);
}
......@@ -156,7 +154,7 @@ static void undo_sysctl_adjustments(void)
if (!sysctl_adjusted)
return;
for (i=0; i<ifnum; i++) {
for (i = 0; i < ifnum; i++) {
char buf[128];
FILE *fp;
......@@ -185,7 +183,7 @@ static int send_probe(int ifindex, __u32 addr)
struct sockaddr_in dst;
socklen_t len;
unsigned char buf[256];
struct arphdr *ah = (struct arphdr*)buf;
struct arphdr *ah = (struct arphdr *)buf;
unsigned char *p = (unsigned char *)(ah+1);
struct sockaddr_ll sll;
......@@ -203,10 +201,10 @@ static int send_probe(int ifindex, __u32 addr)
dst.sin_family = AF_INET;
dst.sin_port = htons(1025);
dst.sin_addr.s_addr = addr;
if (connect(udp_sock, (struct sockaddr*)&dst, sizeof(dst)) < 0)
if (connect(udp_sock, (struct sockaddr *)&dst, sizeof(dst)) < 0)
return -1;
len = sizeof(dst);
if (getsockname(udp_sock, (struct sockaddr*)&dst, &len) < 0)
if (getsockname(udp_sock, (struct sockaddr *)&dst, &len) < 0)
return -1;
ah->ar_hrd = htons(ifr.ifr_hwaddr.sa_family);
......@@ -219,19 +217,19 @@ static int send_probe(int ifindex, __u32 addr)
p += ah->ar_hln;
memcpy(p, &dst.sin_addr, 4);
p+=4;
p += 4;
sll.sll_family = AF_PACKET;
memset(sll.sll_addr, 0xFF, sizeof(sll.sll_addr));
sll.sll_ifindex = ifindex;
sll.sll_protocol = htons(ETH_P_ARP);
memcpy(p, &sll.sll_addr, ah->ar_hln);
p+=ah->ar_hln;
p += ah->ar_hln;
memcpy(p, &addr, 4);
p+=4;
p += 4;
if (sendto(pset[0].fd, buf, p-buf, 0, (struct sockaddr*)&sll, sizeof(sll)) < 0)
if (sendto(pset[0].fd, buf, p-buf, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
return -1;
stats.probes_sent++;
return 0;
......@@ -248,6 +246,7 @@ static int queue_active_probe(int ifindex, __u32 addr)
gettimeofday(&now, NULL);
if (prev.tv_sec) {
int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000;
buckets += diff;
} else {
buckets = broadcast_burst;
......@@ -266,9 +265,9 @@ static int queue_active_probe(int ifindex, __u32 addr)
static int respond_to_kernel(int ifindex, __u32 addr, char *lla, int llalen)
{
struct {
struct nlmsghdr n;
struct ndmsg ndm;
char buf[256];
struct nlmsghdr n;
struct ndmsg ndm;
char buf[256];
} req;
memset(&req.n, 0, sizeof(req.n));
......@@ -302,7 +301,7 @@ static int do_one_request(struct nlmsghdr *n)
{
struct ndmsg *ndm = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr * tb[NDA_MAX+1];
struct rtattr *tb[NDA_MAX+1];
struct dbkey key;
DBT dbkey, dbdat;
int do_acct = 0;
......@@ -405,6 +404,7 @@ static int do_one_request(struct nlmsghdr *n)
!IS_NEG(dbdat.data) ||
!NEG_VALID(dbdat.data)) {
__u8 ndata[6];
stats.kern_neg++;
prepare_neg_entry(ndata, time(NULL));
dbdat.data = ndata;
......@@ -444,7 +444,7 @@ static void get_kern_msg(void)
struct iovec iov;
char buf[8192];
struct msghdr msg = {
(void*)&nladdr, sizeof(nladdr),
(void *)&nladdr, sizeof(nladdr),
&iov, 1,
NULL, 0,
0
......@@ -466,7 +466,7 @@ static void get_kern_msg(void)
if (nladdr.nl_pid)
return;
for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
int len = h->nlmsg_len;
int l = len - sizeof(*h);
......@@ -477,7 +477,7 @@ static void get_kern_msg(void)
return;
status -= NLMSG_ALIGN(len);
h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
}
}
......@@ -487,13 +487,13 @@ static void get_arp_pkt(void)
unsigned char buf[1024];
struct sockaddr_ll sll;
socklen_t sll_len = sizeof(sll);
struct arphdr *a = (struct arphdr*)buf;
struct arphdr *a = (struct arphdr *)buf;
struct dbkey key;
DBT dbkey, dbdat;
int n;
n = recvfrom(pset[0].fd, buf, sizeof(buf), MSG_DONTWAIT,
(struct sockaddr*)&sll, &sll_len);
(struct sockaddr *)&sll, &sll_len);
if (n < 0) {
if (errno != EINTR && errno != EAGAIN)
syslog(LOG_ERR, "recvfrom: %m");
......@@ -515,7 +515,7 @@ static void get_arp_pkt(void)
return;
key.iface = sll.sll_ifindex;
memcpy(&key.addr, (char*)(a+1) + a->ar_hln, 4);
memcpy(&key.addr, (char *)(a+1) + a->ar_hln, 4);
/* DAD message, ignore. */
if (key.addr == 0)
......@@ -600,7 +600,7 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "h?b:lf:a:n:p:kR:B:")) != EOF) {
switch (opt) {
case 'b':
case 'b':
dbname = optarg;
break;
case 'f':
......@@ -624,7 +624,7 @@ int main(int argc, char **argv)
break;
case 'p':
if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) {
fprintf(stderr,"Invalid poll timeout\n");
fprintf(stderr, "Invalid poll timeout\n");
exit(-1);
}
break;
......@@ -666,15 +666,16 @@ int main(int argc, char **argv)
exit(-1);
}
if (ifnum) {
if (ifnum) {
int i;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
for (i=0; i<ifnum; i++) {
for (i = 0; i < ifnum; i++) {
strncpy(ifr.ifr_name, ifnames[i], IFNAMSIZ);
if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) {
perror("ioctl(SIOCGIFINDEX)");
exit(-1);;
exit(-1);
}
ifvec[i] = ifr.ifr_ifindex;
}
......@@ -717,7 +718,7 @@ int main(int argc, char **argv)
}
if (strncmp(macbuf, "FAILED:", 7) == 0)
continue;
if (!inet_aton(ipbuf, (struct in_addr*)&k.addr)) {
if (!inet_aton(ipbuf, (struct in_addr *)&k.addr)) {
fprintf(stderr, "Invalid IP address: \"%s\"\n", ipbuf);
goto do_abort;
}
......@@ -738,20 +739,23 @@ int main(int argc, char **argv)
if (do_list) {
DBT dbkey, dbdat;
printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC");
while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) {
struct dbkey *key = dbkey.data;
if (handle_if(key->iface)) {
if (!IS_NEG(dbdat.data)) {
char b1[18];
printf("%-8d %-15s %s\n",
key->iface,
inet_ntoa(*(struct in_addr*)&key->addr),
inet_ntoa(*(struct in_addr *)&key->addr),
ll_addr_n2a(dbdat.data, 6, ARPHRD_ETHER, b1, 18));
} else {
printf("%-8d %-15s FAILED: %dsec ago\n",
key->iface,
inet_ntoa(*(struct in_addr*)&key->addr),
inet_ntoa(*(struct in_addr *)&key->addr),
NEG_AGE(dbdat.data));
}
}
......@@ -769,11 +773,12 @@ int main(int argc, char **argv)
if (1) {
struct sockaddr_ll sll;
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_ARP);
sll.sll_ifindex = (ifnum == 1 ? ifvec[0] : 0);
if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
if (bind(pset[0].fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
perror("bind");
goto do_abort;
}
......
......@@ -35,15 +35,15 @@
#include <SNAPSHOT.h>
int dump_zeros = 0;
int reset_history = 0;
int ignore_history = 0;
int no_output = 0;
int json_output = 0;
int no_update = 0;
int scan_interval = 0;
int time_constant = 0;
int show_errors = 0;
int dump_zeros;
int reset_history;
int ignore_history;
int no_output;
int json_output;
int no_update;
int scan_interval;
int time_constant;
int show_errors;
int pretty;
double W;
char **patterns;
......@@ -54,8 +54,7 @@ int source_mismatch;
#define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
struct ifstat_ent
{
struct ifstat_ent {
struct ifstat_ent *next;
char *name;
int ifindex;
......@@ -100,7 +99,7 @@ static int match(const char *id)
if (npatterns == 0)
return 1;
for (i=0; i<npatterns; i++) {
for (i = 0; i < npatterns; i++) {
if (!fnmatch(patterns[i], id, 0))
return 1;
}
......@@ -111,7 +110,7 @@ static int get_nlmsg(const struct sockaddr_nl *who,
struct nlmsghdr *m, void *arg)
{
struct ifinfomsg *ifi = NLMSG_DATA(m);
struct rtattr * tb[IFLA_MAX+1];
struct rtattr *tb[IFLA_MAX+1];
int len = m->nlmsg_len;
struct ifstat_ent *n;
int i;
......@@ -137,7 +136,7 @@ static int get_nlmsg(const struct sockaddr_nl *who,
n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
memset(&n->rate, 0, sizeof(n->rate));
for (i=0; i<MAXS; i++)
for (i = 0; i < MAXS; i++)
n->val[i] = n->ival[i];
n->next = kern_db;
kern_db = n;
......@@ -209,8 +208,9 @@ static void load_raw_table(FILE *fp)
n->name = strdup(p);
p = next;
for (i=0; i<MAXS; i++) {
unsigned rate;
for (i = 0; i < MAXS; i++) {
unsigned int rate;
if (!(next = strchr(p, ' ')))
abort();
*next++ = 0;
......@@ -251,12 +251,14 @@ static void dump_raw_db(FILE *fp, int to_hist)
} else
fprintf(fp, "#%s\n", info_source);
for (n=kern_db; n; n=n->next) {
for (n = kern_db; n; n = n->next) {
int i;
unsigned long long *vals = n->val;
double *rates = n->rate;
if (!match(n->name)) {
struct ifstat_ent *h1;
if (!to_hist)
continue;
for (h1 = h; h1; h1 = h1->next) {
......@@ -273,14 +275,14 @@ static void dump_raw_db(FILE *fp, int to_hist)
jsonw_name(jw, n->name);
jsonw_start_object(jw);
for (i=0; i<MAXS && stats[i]; i++)
for (i = 0; i < MAXS && stats[i]; i++)
jsonw_uint_field(jw, stats[i], vals[i]);
jsonw_end_object(jw);
} else {
fprintf(fp, "%d %s ", n->ifindex, n->name);
for (i=0; i<MAXS; i++)
for (i = 0; i < MAXS; i++)
fprintf(fp, "%llu %u ", vals[i],
(unsigned)rates[i]);
(unsigned int)rates[i]);
fprintf(fp, "\n");
}
}
......@@ -308,18 +310,19 @@ static void format_rate(FILE *fp, const unsigned long long *vals,
fprintf(fp, "%8llu ", vals[i]);
if (rates[i] > mega) {
sprintf(temp, "%uM", (unsigned)(rates[i]/mega));
sprintf(temp, "%uM", (unsigned int)(rates[i]/mega));
fprintf(fp, "%-6s ", temp);
} else if (rates[i] > kilo) {
sprintf(temp, "%uK", (unsigned)(rates[i]/kilo));
sprintf(temp, "%uK", (unsigned int)(rates[i]/kilo));
fprintf(fp, "%-6s ", temp);
} else
fprintf(fp, "%-6u ", (unsigned)rates[i]);
fprintf(fp, "%-6u ", (unsigned int)rates[i]);
}
static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k)
{
char temp[64];
if (vals[i] > giga)
fprintf(fp, "%7lluM ", vals[i]/mega);
else if (vals[i] > mega)
......@@ -328,13 +331,13 @@ static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k)
fprintf(fp, "%8llu ", vals[i]);
if (vals[k] > giga) {
sprintf(temp, "%uM", (unsigned)(vals[k]/mega));
sprintf(temp, "%uM", (unsigned int)(vals[k]/mega));
fprintf(fp, "%-6s ", temp);
} else if (vals[k] > mega) {
sprintf(temp, "%uK", (unsigned)(vals[k]/kilo));
sprintf(temp, "%uK", (unsigned int)(vals[k]/kilo));
fprintf(fp, "%-6s ", temp);
} else
fprintf(fp, "%-6u ", (unsigned)vals[k]);
fprintf(fp, "%-6u ", (unsigned int)vals[k]);
}
static void print_head(FILE *fp)
......@@ -345,38 +348,38 @@ static void print_head(FILE *fp)
fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate");
fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate");
fprintf(fp, "%8s/%-6s ", "RX Data", "Rate");
fprintf(fp, "%8s/%-6s\n","TX Data", "Rate");
fprintf(fp, "%8s/%-6s\n", "TX Data", "Rate");
if (!show_errors) {
fprintf(fp, "%-15s ", "");
fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop");
fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop");
fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
fprintf(fp, "%8s/%-6s\n","TX Coll", "Rate");
fprintf(fp, "%8s/%-6s\n", "TX Coll", "Rate");
} else {
fprintf(fp, "%-15s ", "");
fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate");
fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate");
fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
fprintf(fp, "%8s/%-6s\n","RX Leng", "Rate");
fprintf(fp, "%8s/%-6s\n", "RX Leng", "Rate");
fprintf(fp, "%-15s ", "");
fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate");
fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate");
fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate");
fprintf(fp, "%8s/%-6s\n","RX Miss", "Rate");
fprintf(fp, "%8s/%-6s\n", "RX Miss", "Rate");
fprintf(fp, "%-15s ", "");
fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate");
fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate");
fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate");
fprintf(fp, "%8s/%-6s\n","TX Carr", "Rate");
fprintf(fp, "%8s/%-6s\n", "TX Carr", "Rate");
fprintf(fp, "%-15s ", "");
fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate");
fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate");
fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate");
fprintf(fp, "%8s/%-6s\n","TX Wind", "Rate");
fprintf(fp, "%8s/%-6s\n", "TX Wind", "Rate");
}
}
......@@ -388,7 +391,7 @@ static void print_one_json(json_writer_t *jw, const struct ifstat_ent *n,
jsonw_name(jw, n->name);
jsonw_start_object(jw);
for (i=0; i < m && stats[i]; i++)
for (i = 0; i < m && stats[i]; i++)
jsonw_uint_field(jw, stats[i], vals[i]);
jsonw_end_object(jw);
......@@ -400,7 +403,7 @@ static void print_one_if(FILE *fp, const struct ifstat_ent *n,
int i;
fprintf(fp, "%-15s ", n->name);
for (i=0; i<4; i++)
for (i = 0; i < 4; i++)
format_rate(fp, vals, n->rate, i);
fprintf(fp, "\n");
......@@ -454,7 +457,7 @@ static void dump_kern_db(FILE *fp)
} else
print_head(fp);
for (n=kern_db; n; n=n->next) {
for (n = kern_db; n; n = n->next) {
if (!match(n->name))
continue;
......@@ -480,7 +483,7 @@ static void dump_incr_db(FILE *fp)
} else
print_head(fp);
for (n=kern_db; n; n=n->next) {
for (n = kern_db; n; n = n->next) {
int i;
unsigned long long vals[MAXS];
struct ifstat_ent *h1;
......@@ -530,9 +533,11 @@ static void update_db(int interval)
for (n = kern_db; n; n = n->next) {
struct ifstat_ent *h1;
for (h1 = h; h1; h1 = h1->next) {
if (h1->ifindex == n->ifindex) {
int i;
for (i = 0; i < MAXS; i++) {
if ((long)(h1->ival[i] - n->ival[i]) < 0) {
memset(n->ival, 0, sizeof(n->ival));
......@@ -542,6 +547,7 @@ static void update_db(int interval)
for (i = 0; i < MAXS; i++) {
double sample;
unsigned long incr = h1->ival[i] - n->ival[i];
n->val[i] += incr;
n->ival[i] = h1->ival[i];
sample = (double)(incr*1000)/interval;
......@@ -552,6 +558,7 @@ static void update_db(int interval)
n->rate[i] = sample;
} else {
double w = W*(double)interval/scan_interval;
n->rate[i] += w*(sample-n->rate[i]);
}
}
......@@ -559,6 +566,7 @@ static void update_db(int interval)
while (h != h1) {
struct ifstat_ent *tmp = h;
h = h->next;
free(tmp->name);
free(tmp);
......@@ -572,13 +580,14 @@ static void update_db(int interval)
}
}
#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
static void server_loop(int fd)
{
struct timeval snaptime = { 0 };
struct pollfd p;
p.fd = fd;
p.events = p.revents = POLLIN;
......@@ -603,16 +612,19 @@ static void server_loop(int fd)
if (poll(&p, 1, scan_interval - tdiff) > 0
&& (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) {
pid_t pid;
if (children >= 5) {
close(clnt);
} else if ((pid = fork()) != 0) {
if (pid>0)
if (pid > 0)
children++;
close(clnt);
} else {
FILE *fp = fdopen(clnt, "w");
if (fp)
dump_raw_db(fp, 0);
exit(0);
......@@ -629,7 +641,7 @@ static int verify_forging(int fd)
struct ucred cred;
socklen_t olen = sizeof(cred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) ||
olen < sizeof(cred))
return -1;
if (cred.uid == getuid() || cred.uid == 0)
......@@ -685,7 +697,7 @@ int main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, "hjpvVzrnasd:t:e",
longopts, NULL)) != EOF) {
switch(ch) {
switch (ch) {
case 'z':
dump_zeros = 1;
break;
......@@ -751,7 +763,7 @@ int main(int argc, char *argv[])
perror("ifstat: socket");
exit(-1);
}
if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
perror("ifstat: bind");
exit(-1);
}
......@@ -809,6 +821,7 @@ int main(int argc, char *argv[])
if (!ignore_history) {
FILE *tfp;
long uptime = -1;
if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
if (fscanf(tfp, "%ld", &uptime) != 1)
uptime = -1;
......@@ -828,11 +841,12 @@ int main(int argc, char *argv[])
}
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
(connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
(connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0
|| (strcpy(sun.sun_path+1, "ifstat0"),
connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
&& verify_forging(fd) == 0) {
FILE *sfp = fdopen(fd, "r");
load_raw_table(sfp);
if (hist_db && source_mismatch) {
fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
......
......@@ -56,10 +56,8 @@ static struct option opts[] = {
static int usage(char *name, int exit_code)
{
fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
"<laforge@gnumonks.org>\n");
fprintf(stderr, "This program is free software licensed under GNU GPLv2"
"\nwith ABSOLUTELY NO WARRANTY.\n\n");
fprintf(stderr, "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n");
fprintf(stderr, "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n\n");
fprintf(stderr, "Parameters:\n");
fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
fprintf(stderr, "\t-c --count <count>\t"
......@@ -145,14 +143,13 @@ static int map_field_params(struct lnstat_file *lnstat_files,
if (++j >= MAX_FIELDS - 1) {
fprintf(stderr,
"WARN: MAX_FIELDS (%d) reached,"
" truncating number of keys\n",
"WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
MAX_FIELDS);
goto full;
}
}
}
full:
full:
fps->num = j;
return 1;
}
......@@ -181,7 +178,7 @@ static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
struct field_params *fps,
int linewidth)
{
int h,i;
int h, i;
static struct table_hdr th;
int ofs = 0;
......@@ -269,7 +266,7 @@ int main(int argc, char **argv)
num_req_files = 1;
}
while ((c = getopt_long(argc, argv,"Vc:djpf:h?i:k:s:w:",
while ((c = getopt_long(argc, argv, "Vc:djpf:h?i:k:s:w:",
opts, NULL)) != -1) {
int len = 0;
char *tmp, *tok;
......@@ -303,8 +300,7 @@ int main(int argc, char **argv)
tok = strtok(NULL, ",")) {
if (fp.num >= MAX_FIELDS) {
fprintf(stderr,
"WARN: too many keys"
" requested: (%d max)\n",
"WARN: too many keys requested: (%d max)\n",
MAX_FIELDS);
break;
}
......@@ -356,7 +352,7 @@ int main(int argc, char **argv)
if (!header)
exit(1);
if (interval < 1 )
if (interval < 1)
interval = 1;
for (i = 0; i < count || !count; i++) {
......
......@@ -49,7 +49,7 @@ static int scan_lines(struct lnstat_file *lf, int i)
if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
return -1;
while(!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
while (!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
char *ptr = buf;
num_lines++;
......@@ -58,6 +58,7 @@ static int scan_lines(struct lnstat_file *lf, int i)
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
......@@ -102,7 +103,7 @@ 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;
}
scan_lines(lf, 0);
......@@ -158,6 +159,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;
......
......@@ -31,15 +31,15 @@
#include <json_writer.h>
#include <SNAPSHOT.h>
int dump_zeros = 0;
int reset_history = 0;
int ignore_history = 0;
int no_output = 0;
int json_output = 0;
int pretty = 0;
int no_update = 0;
int scan_interval = 0;
int time_constant = 0;
int dump_zeros;
int reset_history;
int ignore_history;
int no_output;
int json_output;
int pretty;
int no_update;
int scan_interval;
int time_constant;
double W;
char **patterns;
int npatterns;
......@@ -51,6 +51,7 @@ static int generic_proc_open(const char *env, char *name)
{
char store[128];
char *p = getenv(env);
if (!p) {
p = getenv("PROC_ROOT") ? : "/proc";
snprintf(store, sizeof(store)-1, "%s/%s", p, name);
......@@ -74,8 +75,7 @@ static int net_snmp6_open(void)
return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
}
struct nstat_ent
{
struct nstat_ent {
struct nstat_ent *next;
char *id;
unsigned long long val;
......@@ -94,7 +94,8 @@ static const char *useless_numbers[] = {
static int useless_number(const char *id)
{
int i;
for (i=0; i<sizeof(useless_numbers)/sizeof(*useless_numbers); i++)
for (i = 0; i < sizeof(useless_numbers)/sizeof(*useless_numbers); i++)
if (strcmp(id, useless_numbers[i]) == 0)
return 1;
return 0;
......@@ -107,7 +108,7 @@ static int match(const char *id)
if (npatterns == 0)
return 1;
for (i=0; i<npatterns; i++) {
for (i = 0; i < npatterns; i++) {
if (!fnmatch(patterns[i], id, 0))
return 1;
}
......@@ -125,6 +126,7 @@ static void load_good_table(FILE *fp)
unsigned long long val;
double rate;
char idbuf[sizeof(buf)];
if (buf[0] == '#') {
buf[strlen(buf)-1] = 0;
if (info_source[0] && strcmp(info_source, buf+1))
......@@ -192,6 +194,7 @@ static void load_ugly_table(FILE *fp)
while (*p) {
char *next;
if ((next = strchr(p, ' ')) != NULL)
*next++ = 0;
else if ((next = strchr(p, '\n')) != NULL)
......@@ -246,6 +249,7 @@ static void load_ugly_table(FILE *fp)
static void load_snmp(void)
{
FILE *fp = fdopen(net_snmp_open(), "r");
if (fp) {
load_ugly_table(fp);
fclose(fp);
......@@ -255,6 +259,7 @@ static void load_snmp(void)
static void load_snmp6(void)
{
FILE *fp = fdopen(net_snmp6_open(), "r");
if (fp) {
load_good_table(fp);
fclose(fp);
......@@ -264,6 +269,7 @@ static void load_snmp6(void)
static void load_netstat(void)
{
FILE *fp = fdopen(net_netstat_open(), "r");
if (fp) {
load_ugly_table(fp);
fclose(fp);
......@@ -284,12 +290,14 @@ static void dump_kern_db(FILE *fp, int to_hist)
} else
fprintf(fp, "#%s\n", info_source);
for (n=kern_db; n; n=n->next) {
for (n = kern_db; n; n = n->next) {
unsigned long long val = n->val;
if (!dump_zeros && !val && !n->rate)
continue;
if (!match(n->id)) {
struct nstat_ent *h1;
if (!to_hist)
continue;
for (h1 = h; h1; h1 = h1->next) {
......@@ -326,10 +334,11 @@ static void dump_incr_db(FILE *fp)
} else
fprintf(fp, "#%s\n", info_source);
for (n=kern_db; n; n=n->next) {
for (n = kern_db; n; n = n->next) {
int ovfl = 0;
unsigned long long val = n->val;
struct nstat_ent *h1;
for (h1 = h; h1; h1 = h1->next) {
if (strcmp(h1->id, n->id) == 0) {
if (val < h1->val) {
......@@ -381,6 +390,7 @@ static void update_db(int interval)
for (n = kern_db; n; n = n->next) {
struct nstat_ent *h1;
for (h1 = h; h1; h1 = h1->next) {
if (strcmp(h1->id, n->id) == 0) {
double sample;
......@@ -395,12 +405,14 @@ static void update_db(int interval)
n->rate = sample;
} else {
double w = W*(double)interval/scan_interval;
n->rate += w*(sample-n->rate);
}
}
while (h != h1) {
struct nstat_ent *tmp = h;
h = h->next;
free(tmp->id);
free(tmp);
......@@ -414,13 +426,14 @@ static void update_db(int interval)
}
}
#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
static void server_loop(int fd)
{
struct timeval snaptime = { 0 };
struct pollfd p;
p.fd = fd;
p.events = p.revents = POLLIN;
......@@ -435,6 +448,7 @@ static void server_loop(int fd)
int status;
time_t tdiff;
struct timeval now;
gettimeofday(&now, NULL);
tdiff = T_DIFF(now, snaptime);
if (tdiff >= scan_interval) {
......@@ -445,16 +459,19 @@ static void server_loop(int fd)
if (poll(&p, 1, scan_interval - tdiff) > 0
&& (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) {
pid_t pid;
if (children >= 5) {
close(clnt);
} else if ((pid = fork()) != 0) {
if (pid>0)
if (pid > 0)
children++;
close(clnt);
} else {
FILE *fp = fdopen(clnt, "w");
if (fp)
dump_kern_db(fp, 0);
exit(0);
......@@ -471,7 +488,7 @@ static int verify_forging(int fd)
struct ucred cred;
socklen_t olen = sizeof(cred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) ||
olen < sizeof(cred))
return -1;
if (cred.uid == getuid() || cred.uid == 0)
......@@ -524,7 +541,7 @@ int main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp",
longopts, NULL)) != EOF) {
switch(ch) {
switch (ch) {
case 'z':
dump_zeros = 1;
break;
......@@ -583,7 +600,7 @@ int main(int argc, char *argv[])
perror("nstat: socket");
exit(-1);
}
if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
perror("nstat: bind");
exit(-1);
}
......@@ -639,6 +656,7 @@ int main(int argc, char *argv[])
if (!ignore_history) {
FILE *tfp;
long uptime = -1;
if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
if (fscanf(tfp, "%ld", &uptime) != 1)
uptime = -1;
......@@ -658,11 +676,12 @@ int main(int argc, char *argv[])
}
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
(connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
(connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0
|| (strcpy(sun.sun_path+1, "nstat0"),
connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
&& 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");
......
......@@ -33,20 +33,21 @@
#include <SNAPSHOT.h>
int reset_history = 0;
int ignore_history = 0;
int no_output = 0;
int no_update = 0;
int scan_interval = 0;
int time_constant = 0;
int dump_zeros = 0;
unsigned long magic_number = 0;
int reset_history;
int ignore_history;
int no_output;
int no_update;
int scan_interval;
int time_constant;
int dump_zeros;
unsigned long magic_number;
double W;
static int generic_proc_open(const char *env, const char *name)
{
char store[1024];
char *p = getenv(env);
if (!p) {
p = getenv("PROC_ROOT") ? : "/proc";
snprintf(store, sizeof(store)-1, "%s/%s", p, name);
......@@ -62,8 +63,7 @@ static int net_rtacct_open(void)
static __u32 rmap[256/4];
struct rtacct_data
{
struct rtacct_data {
__u32 ival[256*4];
unsigned long long val[256*4];
......@@ -82,6 +82,7 @@ static void nread(int fd, char *buf, int tot)
while (count < tot) {
int n = read(fd, buf+count, tot-count);
if (n < 0) {
if (errno == EINTR)
continue;
......@@ -121,7 +122,7 @@ static __u32 *read_kern_table(__u32 *tbl)
fd = net_rtacct_open();
if (fd >= 0) {
nread(fd, (char*)tbl, 256*16);
nread(fd, (char *)tbl, 256*16);
close(fd);
} else {
memset(tbl, 0, 256*16);
......@@ -134,13 +135,13 @@ static void format_rate(FILE *fp, double rate)
char temp[64];
if (rate > 1024*1024) {
sprintf(temp, "%uM", (unsigned)rint(rate/(1024*1024)));
sprintf(temp, "%uM", (unsigned int)rint(rate/(1024*1024)));
fprintf(fp, " %-10s", temp);
} else if (rate > 1024) {
sprintf(temp, "%uK", (unsigned)rint(rate/1024));
sprintf(temp, "%uK", (unsigned int)rint(rate/1024));
fprintf(fp, " %-10s", temp);
} else
fprintf(fp, " %-10u", (unsigned)rate);
fprintf(fp, " %-10u", (unsigned int)rate);
}
static void format_count(FILE *fp, unsigned long long val)
......@@ -161,25 +162,19 @@ static void dump_abs_db(FILE *fp)
if (!no_output) {
fprintf(fp, "#%s\n", kern_db->signature);
fprintf(fp,
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"\n"
"%-10s %-10s "
"%-10s %-10s "
"%-10s \n"
, "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
fprintf(fp,
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"\n"
"%-10s %-10s "
"%-10s %-10s "
"%-10s \n"
, "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
}
for (realm=0; realm<256; realm++) {
for (realm = 0; realm < 256; realm++) {
int i;
unsigned long long *val;
double *rate;
......@@ -223,24 +218,18 @@ static void dump_incr_db(FILE *fp)
if (!no_output) {
fprintf(fp, "#%s\n", kern_db->signature);
fprintf(fp,
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"\n"
"%-10s %-10s "
"%-10s %-10s "
"%-10s \n"
, "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
fprintf(fp,
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"%-10s "
"\n"
"%-10s %-10s "
"%-10s %-10s "
"%-10s \n"
, "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
}
for (realm=0; realm<256; realm++) {
for (realm = 0; realm < 256; realm++) {
int ovfl = 0;
int i;
unsigned long long *val;
......@@ -253,7 +242,7 @@ static void dump_incr_db(FILE *fp)
val = &kern_db->val[realm*4];
rate = &kern_db->rate[realm*4];
for (k=0; k<4; k++) {
for (k = 0; k < 4; k++) {
rval[k] = val[k];
if (rval[k] < hist_db->val[realm*4+k])
ovfl = 1;
......@@ -261,7 +250,7 @@ static void dump_incr_db(FILE *fp)
rval[k] -= hist_db->val[realm*4+k];
}
if (ovfl) {
for (k=0; k<4; k++)
for (k = 0; k < 4; k++)
rval[k] = val[k];
}
if (hist_db) {
......@@ -306,7 +295,7 @@ static void update_db(int interval)
ival = read_kern_table(_ival);
for (i=0; i<256*4; i++) {
for (i = 0; i < 256*4; i++) {
double sample;
__u32 incr = ival[i] - kern_db->ival[i];
......@@ -324,6 +313,7 @@ static void update_db(int interval)
kern_db->rate[i] = sample;
} else {
double w = W*(double)interval/scan_interval;
kern_db->rate[i] += w*(sample-kern_db->rate[i]);
}
}
......@@ -335,7 +325,8 @@ static void send_db(int fd)
int tot = 0;
while (tot < sizeof(*kern_db)) {
int n = write(fd, ((char*)kern_db) + tot, sizeof(*kern_db)-tot);
int n = write(fd, ((char *)kern_db) + tot, sizeof(*kern_db)-tot);
if (n < 0) {
if (errno == EINTR)
continue;
......@@ -347,16 +338,17 @@ static void send_db(int fd)
#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
#define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
static void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
{
int i;
memset(dat->rate, 0, sizeof(dat->rate));
if (dat->ival != ival)
memcpy(dat->ival, ival, sizeof(dat->ival));
for (i=0; i<256*4; i++)
for (i = 0; i < 256*4; i++)
dat->val[i] = ival[i];
}
......@@ -364,12 +356,13 @@ static void server_loop(int fd)
{
struct timeval snaptime = { 0 };
struct pollfd p;
p.fd = fd;
p.events = p.revents = POLLIN;
sprintf(kern_db->signature,
"%u.%lu sampling_interval=%d time_const=%d",
(unsigned) getpid(), (unsigned long)random(),
(unsigned int) getpid(), (unsigned long)random(),
scan_interval/1000, time_constant/1000);
pad_kern_table(kern_db, read_kern_table(kern_db->ival));
......@@ -378,6 +371,7 @@ static void server_loop(int fd)
int status;
int tdiff;
struct timeval now;
gettimeofday(&now, NULL);
tdiff = T_DIFF(now, snaptime);
if (tdiff >= scan_interval) {
......@@ -388,12 +382,14 @@ static void server_loop(int fd)
if (poll(&p, 1, tdiff + scan_interval) > 0
&& (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) {
pid_t pid;
if (children >= 5) {
close(clnt);
} else if ((pid = fork()) != 0) {
if (pid>0)
if (pid > 0)
children++;
close(clnt);
} else {
......@@ -414,7 +410,7 @@ static int verify_forging(int fd)
struct ucred cred;
socklen_t olen = sizeof(cred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &olen) ||
olen < sizeof(cred))
return -1;
if (cred.uid == getuid() || cred.uid == 0)
......@@ -440,7 +436,7 @@ int main(int argc, char *argv[])
int fd;
while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
switch(ch) {
switch (ch) {
case 'z':
dump_zeros = 1;
break;
......@@ -489,6 +485,7 @@ int main(int argc, char *argv[])
if (argc) {
while (argc > 0) {
__u32 realm;
if (rtnl_rtrealm_a2n(&realm, argv[0])) {
fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
exit(-1);
......@@ -515,7 +512,7 @@ int main(int argc, char *argv[])
perror("rtacct: socket");
exit(-1);
}
if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
if (bind(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
perror("rtacct: bind");
exit(-1);
}
......@@ -580,6 +577,7 @@ int main(int argc, char *argv[])
if (!ignore_history) {
FILE *tfp;
long uptime = -1;
if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
if (fscanf(tfp, "%ld", &uptime) != 1)
uptime = -1;
......@@ -596,11 +594,11 @@ int main(int argc, char *argv[])
}
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
(connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
(connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0
|| (strcpy(sun.sun_path+1, "rtacct0"),
connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
connect(fd, (struct sockaddr *)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
&& verify_forging(fd) == 0) {
nread(fd, (char*)kern_db, sizeof(*kern_db));
nread(fd, (char *)kern_db, sizeof(*kern_db));
if (hist_db && hist_db->signature[0] &&
strcmp(kern_db->signature, hist_db->signature)) {
fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
......
......@@ -86,20 +86,20 @@ static int security_get_initial_context(char *name, char **context)
}
#endif
int resolve_hosts = 0;
int resolve_hosts;
int resolve_services = 1;
int preferred_family = AF_UNSPEC;
int show_options = 0;
int show_details = 0;
int show_users = 0;
int show_mem = 0;
int show_tcpinfo = 0;
int show_bpf = 0;
int show_proc_ctx = 0;
int show_sock_ctx = 0;
int show_options;
int show_details;
int show_users;
int show_mem;
int show_tcpinfo;
int show_bpf;
int show_proc_ctx;
int show_sock_ctx;
/* If show_users & show_proc_ctx only do user_ent_hash_build() once */
int user_ent_hash_build_init = 0;
int follow_events = 0;
int user_ent_hash_build_init;
int follow_events;
int netid_width;
int state_width;
......@@ -111,10 +111,9 @@ int screen_width;
static const char *TCP_PROTO = "tcp";
static const char *UDP_PROTO = "udp";
static const char *RAW_PROTO = "raw";
static const char *dg_proto = NULL;
static const char *dg_proto;
enum
{
enum {
TCP_DB,
DCCP_DB,
UDP_DB,
......@@ -154,8 +153,7 @@ enum {
#include "ssfilter.h"
struct filter
{
struct filter {
int dbs;
int states;
int families;
......@@ -551,7 +549,7 @@ enum entry_types {
};
#define ENTRY_BUF_SIZE 512
static int find_entry(unsigned ino, char **buf, int type)
static int find_entry(unsigned int ino, char **buf, int type)
{
struct user_ent *p;
int cnt = 0;
......@@ -624,8 +622,7 @@ next:
/* Get stats from slab */
struct slabstat
{
struct slabstat {
int socks;
int tcp_ports;
int tcp_tws;
......@@ -635,8 +632,8 @@ struct slabstat
static struct slabstat slabstat;
static const char *slabstat_ids[] =
{
static const char *slabstat_ids[] = {
"sock",
"tcp_bind_bucket",
"tcp_tw_bucket",
......@@ -666,9 +663,10 @@ static int get_slabstat(struct slabstat *s)
fclose(fp);
return -1;
}
while(fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
int i;
for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
for (i = 0; i < sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
sscanf(buf, "%*s%d", ((int *)s) + i);
cnt--;
......@@ -701,7 +699,7 @@ static const char *sstate_name[] = {
[SS_CLOSE] = "UNCONN",
[SS_CLOSE_WAIT] = "CLOSE-WAIT",
[SS_LAST_ACK] = "LAST-ACK",
[SS_LISTEN] = "LISTEN",
[SS_LISTEN] = "LISTEN",
[SS_CLOSING] = "CLOSING",
};
......@@ -716,12 +714,11 @@ static const char *sstate_namel[] = {
[SS_CLOSE] = "unconnected",
[SS_CLOSE_WAIT] = "close-wait",
[SS_LAST_ACK] = "last-ack",
[SS_LISTEN] = "listening",
[SS_LISTEN] = "listening",
[SS_CLOSING] = "closing",
};
struct sockstat
{
struct sockstat {
struct sockstat *next;
unsigned int type;
uint16_t prot;
......@@ -731,8 +728,8 @@ struct sockstat
int rport;
int state;
int rq, wq;
unsigned ino;
unsigned uid;
unsigned int ino;
unsigned int uid;
int refcnt;
unsigned int iface;
unsigned long long sk;
......@@ -740,8 +737,7 @@ struct sockstat
char *peer_name;
};
struct dctcpstat
{
struct dctcpstat {
unsigned int ce_state;
unsigned int alpha;
unsigned int ab_ecn;
......@@ -749,8 +745,7 @@ struct dctcpstat
bool enabled;
};
struct tcpstat
{
struct tcpstat {
struct sockstat ss;
int timer;
int timeout;
......@@ -816,8 +811,7 @@ static void sock_addr_print_width(int addr_len, const char *addr, char *delim,
if (ifname) {
printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim,
port_len, port);
}
else {
} else {
printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port);
}
}
......@@ -841,6 +835,7 @@ static const char *print_ms_timer(int timeout)
{
static char buf[64];
int secs, msecs, minutes;
if (timeout < 0)
timeout = 0;
secs = timeout/1000;
......@@ -891,7 +886,7 @@ static void init_service_resolver(void)
struct scache *c;
if (sscanf(buf, "%u %*d %s %u %s",
&progn, proto, &port, prog+4) != 4)
&progn, proto, &port, prog+4) != 4)
continue;
if (!(c = malloc(sizeof(*c))))
......@@ -919,10 +914,11 @@ static void init_service_resolver(void)
*/
static int is_ephemeral(int port)
{
static int min = 0, max = 0;
static int min = 0, max;
if (!min) {
FILE *f = ephemeral_ports_open();
if (!f || fscanf(f, "%d %d", &min, &max) < 2) {
min = 1024;
max = 4999;
......@@ -946,6 +942,7 @@ static const char *__resolve_service(int port)
if (!is_ephemeral(port)) {
static int notfirst;
struct servent *se;
if (!notfirst) {
setservent(1);
notfirst = 1;
......@@ -1041,8 +1038,7 @@ static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex
ifname);
}
struct aafilter
{
struct aafilter {
inet_prefix addr;
int port;
struct aafilter *next;
......@@ -1061,6 +1057,7 @@ static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
if (a->data[0] == 0 && a->data[1] == 0 &&
a->data[2] == htonl(0xffff)) {
inet_prefix tmp = *a;
tmp.data[0] = a->data[3];
return inet_addr_match(&tmp, p, plen);
}
......@@ -1071,6 +1068,7 @@ static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
static int unix_match(const inet_prefix *a, const inet_prefix *p)
{
char *addr, *pattern;
memcpy(&addr, a->data, sizeof(addr));
memcpy(&pattern, p->data, sizeof(pattern));
if (pattern == NULL)
......@@ -1087,6 +1085,7 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
{
if (s->local.family == AF_UNIX) {
char *p;
memcpy(&p, s->local.data, sizeof(p));
return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
strspn(p+1, "0123456789abcdef") == 5);
......@@ -1100,7 +1099,8 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
}
case SSF_DCOND:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
if (a->addr.family == AF_UNIX)
return unix_match(&s->remote, &a->addr);
if (a->port != -1 && a->port != s->rport)
......@@ -1116,7 +1116,8 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
}
case SSF_SCOND:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
if (a->addr.family == AF_UNIX)
return unix_match(&s->local, &a->addr);
if (a->port != -1 && a->port != s->lport)
......@@ -1132,22 +1133,26 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
}
case SSF_D_GE:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
return s->rport >= a->port;
}
case SSF_D_LE:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
return s->rport <= a->port;
}
case SSF_S_GE:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
return s->lport >= a->port;
}
case SSF_S_LE:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
return s->lport <= a->port;
}
......@@ -1167,7 +1172,8 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
static void ssfilter_patch(char *a, int len, int reloc)
{
while (len > 0) {
struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a;
struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)a;
if (op->no == len+4)
op->no += reloc;
len -= op->yes;
......@@ -1182,20 +1188,20 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
switch (f->type) {
case SSF_S_AUTO:
{
if (!(*bytecode=malloc(4))) abort();
((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
if (!(*bytecode = malloc(4))) abort();
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
return 4;
}
case SSF_DCOND:
case SSF_SCOND:
{
struct aafilter *a = (void*)f->pred;
struct aafilter *a = (void *)f->pred;
struct aafilter *b;
char *ptr;
int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
int len = 0;
for (b=a; b; b=b->next) {
for (b = a; b; b = b->next) {
len += 4 + sizeof(struct inet_diag_hostcond);
if (a->addr.family == AF_INET6)
len += 16;
......@@ -1206,11 +1212,11 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
}
if (!(ptr = malloc(len))) abort();
*bytecode = ptr;
for (b=a; b; b=b->next) {
for (b = a; b; b = b->next) {
struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
int alen = (a->addr.family == AF_INET6 ? 16 : 4);
int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4);
struct inet_diag_hostcond *cond = (struct inet_diag_hostcond *)(ptr+4);
*op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
cond->family = a->addr.family;
......@@ -1228,34 +1234,38 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
}
case SSF_D_GE:
{
struct aafilter *x = (void*)f->pred;
if (!(*bytecode=malloc(8))) abort();
((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
struct aafilter *x = (void *)f->pred;
if (!(*bytecode = malloc(8))) abort();
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
return 8;
}
case SSF_D_LE:
{
struct aafilter *x = (void*)f->pred;
if (!(*bytecode=malloc(8))) abort();
((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
struct aafilter *x = (void *)f->pred;
if (!(*bytecode = malloc(8))) abort();
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
return 8;
}
case SSF_S_GE:
{
struct aafilter *x = (void*)f->pred;
if (!(*bytecode=malloc(8))) abort();
((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
struct aafilter *x = (void *)f->pred;
if (!(*bytecode = malloc(8))) abort();
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
return 8;
}
case SSF_S_LE:
{
struct aafilter *x = (void*)f->pred;
if (!(*bytecode=malloc(8))) abort();
((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
struct aafilter *x = (void *)f->pred;
if (!(*bytecode = malloc(8))) abort();
((struct inet_diag_bc_op *)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
((struct inet_diag_bc_op *)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
return 8;
}
......@@ -1263,6 +1273,7 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
{
char *a1, *a2, *a;
int l1, l2;
l1 = ssfilter_bytecompile(f->pred, &a1);
l2 = ssfilter_bytecompile(f->post, &a2);
if (!(a = malloc(l1+l2))) abort();
......@@ -1277,13 +1288,14 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
{
char *a1, *a2, *a;
int l1, l2;
l1 = ssfilter_bytecompile(f->pred, &a1);
l2 = ssfilter_bytecompile(f->post, &a2);
if (!(a = malloc(l1+l2+4))) abort();
memcpy(a, a1, l1);
memcpy(a+l1+4, a2, l2);
free(a1); free(a2);
*(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
*bytecode = a;
return l1+l2+4;
}
......@@ -1291,11 +1303,12 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
{
char *a1, *a;
int l1;
l1 = ssfilter_bytecompile(f->pred, &a1);
if (!(a = malloc(l1+4))) abort();
memcpy(a, a1, l1);
free(a1);
*(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
*(struct inet_diag_bc_op *)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
*bytecode = a;
return l1+4;
}
......@@ -1319,6 +1332,7 @@ static int remember_he(struct aafilter *a, struct hostent *he)
while (*ptr) {
struct aafilter *b = a;
if (a->addr.bitlen) {
if ((b = malloc(sizeof(*b))) == NULL)
return cnt;
......@@ -1358,11 +1372,12 @@ static int get_dns_host(struct aafilter *a, const char *addr, int fam)
return !cnt;
}
static int xll_initted = 0;
static int xll_initted;
static void xll_init(void)
{
struct rtnl_handle rth;
if (rtnl_open(&rth, 0) < 0)
exit(1);
......@@ -1395,9 +1410,10 @@ void *parse_hostcond(char *addr, bool is_port)
if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
char *p;
a.addr.family = AF_UNIX;
if (strncmp(addr, "unix:", 5) == 0)
addr+=5;
addr += 5;
p = strdup(addr);
a.addr.bitlen = 8*strlen(p);
memcpy(a.addr.data, &p, sizeof(p));
......@@ -1409,7 +1425,7 @@ void *parse_hostcond(char *addr, bool is_port)
a.addr.family = AF_PACKET;
a.addr.bitlen = 0;
if (strncmp(addr, "link:", 5) == 0)
addr+=5;
addr += 5;
port = strchr(addr, ':');
if (port) {
*port = 0;
......@@ -1422,6 +1438,7 @@ void *parse_hostcond(char *addr, bool is_port)
}
if (addr[0] && strcmp(addr, "*")) {
unsigned short tmp;
a.addr.bitlen = 32;
if (ll_proto_a2n(&tmp, addr))
return NULL;
......@@ -1435,7 +1452,7 @@ void *parse_hostcond(char *addr, bool is_port)
a.addr.family = AF_NETLINK;
a.addr.bitlen = 0;
if (strncmp(addr, "netlink:", 8) == 0)
addr+=8;
addr += 8;
port = strchr(addr, ':');
if (port) {
*port = 0;
......@@ -1490,6 +1507,7 @@ void *parse_hostcond(char *addr, bool is_port)
if (get_integer(&a.port, port, 0)) {
struct servent *se1 = NULL;
struct servent *se2 = NULL;
if (current_filter.dbs&(1<<UDP_DB))
se1 = getservbyname(port, UDP_PROTO);
if (current_filter.dbs&(1<<TCP_DB))
......@@ -1504,6 +1522,7 @@ void *parse_hostcond(char *addr, bool is_port)
a.port = ntohs(se1->s_port);
} else {
struct scache *s;
for (s = rlist; s; s = s->next) {
if ((s->proto == UDP_PROTO &&
(current_filter.dbs&(1<<UDP_DB))) ||
......@@ -1589,12 +1608,12 @@ static void inet_stats_print(struct sockstat *s, int protocol)
}
static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
sockstat *s)
sockstat * s)
{
s->local.family = s->remote.family = family;
if (family == AF_INET) {
sscanf(loc, "%x:%x", s->local.data, (unsigned*)&s->lport);
sscanf(rem, "%x:%x", s->remote.data, (unsigned*)&s->rport);
sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
s->local.bytelen = s->remote.bytelen = 4;
return 0;
} else {
......@@ -1640,9 +1659,9 @@ static int proc_inet_split_line(char *line, char **loc, char **rem, char **data)
static char *sprint_bw(char *buf, double bw)
{
if (bw > 1000000.)
sprintf(buf,"%.1fM", bw / 1000000.);
sprintf(buf, "%.1fM", bw / 1000000.);
else if (bw > 1000.)
sprintf(buf,"%.1fK", bw / 1000.);
sprintf(buf, "%.1fK", bw / 1000.);
else
sprintf(buf, "%g", bw);
......@@ -1770,6 +1789,7 @@ static int tcp_show_line(char *line, const struct filter *f, int family)
return -1;
int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
if (!(f->states & (1 << state)))
return 0;
......@@ -1833,6 +1853,7 @@ static int generic_record_read(FILE *fp,
while (fgets(line, sizeof(line), fp) != NULL) {
int n = strlen(line);
if (n == 0 || line[n-1] != '\n') {
errno = -EINVAL;
return -1;
......@@ -2005,11 +2026,11 @@ static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r,
static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol)
{
struct rtattr * tb[INET_DIAG_MAX+1];
struct rtattr *tb[INET_DIAG_MAX+1];
struct inet_diag_msg *r = NLMSG_DATA(nlh);
struct sockstat s = {};
parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
s.state = r->idiag_state;
......@@ -2053,11 +2074,13 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol)
sock_details_print(&s);
if (s.local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) {
unsigned char v6only;
v6only = *(__u8 *)RTA_DATA(tb[INET_DIAG_SKV6ONLY]);
printf(" v6only:%u", v6only);
}
if (tb[INET_DIAG_SHUTDOWN]) {
unsigned char mask;
mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]);
printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
}
......@@ -2127,7 +2150,7 @@ static int tcpdiag_send(int fd, int protocol, struct filter *f)
}
msg = (struct msghdr) {
.msg_name = (void*)&nladdr,
.msg_name = (void *)&nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov,
.msg_iovlen = f->f ? 3 : 1,
......@@ -2144,6 +2167,7 @@ static int tcpdiag_send(int fd, int protocol, struct filter *f)
static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
{
struct sockaddr_nl nladdr;
DIAG_REQUEST(req, struct inet_diag_req_v2 r);
char *bc = NULL;
int bclen;
......@@ -2186,7 +2210,7 @@ static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
}
msg = (struct msghdr) {
.msg_name = (void*)&nladdr,
.msg_name = (void *)&nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov,
.msg_iovlen = f->f ? 3 : 1,
......@@ -2212,6 +2236,7 @@ static int kill_inet_sock(const struct sockaddr_nl *addr,
struct inet_diag_msg *d = NLMSG_DATA(h);
struct inet_diag_arg *diag_arg = arg;
struct rtnl_handle *rth = diag_arg->rth;
DIAG_REQUEST(req, struct inet_diag_req_v2 r);
req.nlh.nlmsg_type = SOCK_DESTROY;
......@@ -2306,7 +2331,7 @@ static int tcp_show_netlink_file(struct filter *f)
while (1) {
int status, err;
struct nlmsghdr *h = (struct nlmsghdr*)buf;
struct nlmsghdr *h = (struct nlmsghdr *)buf;
status = fread(buf, 1, sizeof(*h), fp);
if (status < 0) {
......@@ -2334,7 +2359,8 @@ static int tcp_show_netlink_file(struct filter *f)
return 0;
if (h->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
fprintf(stderr, "ERROR truncated\n");
} else {
......@@ -2381,6 +2407,7 @@ static int tcp_show(struct filter *f, int socktype)
get_slabstat(&slabstat);
int guess = slabstat.socks+slabstat.tcp_syns;
if (f->states&(1<<SS_TIME_WAIT))
guess += slabstat.tcp_tws;
if (guess > (16*1024*1024)/128)
......@@ -2423,6 +2450,7 @@ static int tcp_show(struct filter *f, int socktype)
outerr:
do {
int saved_errno = errno;
free(buf);
if (fp)
fclose(fp);
......@@ -2443,6 +2471,7 @@ static int dgram_show_line(char *line, const struct filter *f, int family)
return -1;
int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
if (!(f->states & (1 << state)))
return 0;
......@@ -2501,6 +2530,7 @@ static int udp_show(struct filter *f)
outerr:
do {
int saved_errno = errno;
if (fp)
fclose(fp);
errno = saved_errno;
......@@ -2536,6 +2566,7 @@ static int raw_show(struct filter *f)
outerr:
do {
int saved_errno = errno;
if (fp)
fclose(fp);
errno = saved_errno;
......@@ -2629,6 +2660,7 @@ static void unix_stats_print(struct sockstat *list, struct filter *f)
if (use_proc && f->f) {
struct sockstat st;
st.local.family = AF_UNIX;
st.remote.family = AF_UNIX;
memcpy(st.local.data, &s->name, sizeof(s->name));
......@@ -2673,7 +2705,7 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
char name[128];
struct sockstat stat = { .name = "*", .peer_name = "*" };
parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr *)(r+1),
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
stat.type = r->udiag_type;
......@@ -2686,6 +2718,7 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
if (tb[UNIX_DIAG_RQLEN]) {
struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
stat.rq = rql->udiag_rqueue;
stat.wq = rql->udiag_wqueue;
}
......@@ -2714,6 +2747,7 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
if (show_details) {
if (tb[UNIX_DIAG_SHUTDOWN]) {
unsigned char mask;
mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]);
printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
}
......@@ -2922,7 +2956,7 @@ static int packet_show_sock(const struct sockaddr_nl *addr,
uint32_t fanout = 0;
bool has_fanout = false;
parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1),
parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr *)(r+1),
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
/* use /proc/net/packet if all info are not available */
......@@ -2937,6 +2971,7 @@ static int packet_show_sock(const struct sockaddr_nl *addr,
if (tb[PACKET_DIAG_MEMINFO]) {
__u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
}
......@@ -3100,12 +3135,13 @@ static int packet_show(struct filter *f)
}
static int netlink_show_one(struct filter *f,
int prot, int pid, unsigned groups,
int state, int dst_pid, unsigned dst_group,
int prot, int pid, unsigned int groups,
int state, int dst_pid, unsigned int dst_group,
int rq, int wq,
unsigned long long sk, unsigned long long cb)
{
struct sockstat st;
SPRINT_BUF(prot_buf) = {};
const char *prot_name;
char procname[64] = {};
......@@ -3135,11 +3171,13 @@ static int netlink_show_one(struct filter *f,
procname[0] = '*';
} else if (resolve_services) {
int done = 0;
if (!pid) {
done = 1;
strncpy(procname, "kernel", 6);
} else if (pid > 0) {
FILE *fp;
snprintf(procname, sizeof(procname), "%s/%d/stat",
getenv("PROC_ROOT") ? : "/proc", pid);
if ((fp = fopen(procname, "r")) != NULL) {
......@@ -3163,6 +3201,7 @@ static int netlink_show_one(struct filter *f,
if (state == NETLINK_CONNECTED) {
char dst_group_buf[30];
char dst_pid_buf[30];
sock_addr_print(int_to_str(dst_group, dst_group_buf), ":",
int_to_str(dst_pid, dst_pid_buf), NULL);
} else {
......@@ -3170,6 +3209,7 @@ static int netlink_show_one(struct filter *f,
}
char *pid_context = NULL;
if (show_proc_ctx) {
/* The pid value will either be:
* 0 if destination kernel - show kernel initial context.
......@@ -3207,7 +3247,7 @@ static int netlink_show_sock(const struct sockaddr_nl *addr,
int rq = 0, wq = 0;
unsigned long groups = 0;
parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr*)(r+1),
parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr *)(r+1),
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS]))
......@@ -3215,6 +3255,7 @@ static int netlink_show_sock(const struct sockaddr_nl *addr,
if (tb[NETLINK_DIAG_MEMINFO]) {
const __u32 *skmeminfo;
skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]);
rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC];
......@@ -3252,7 +3293,7 @@ static int netlink_show(struct filter *f)
FILE *fp;
char buf[256];
int prot, pid;
unsigned groups;
unsigned int groups;
int rq, wq, rc;
unsigned long long sk, cb;
......@@ -3340,8 +3381,7 @@ Exit:
return ret;
}
struct snmpstat
{
struct snmpstat {
int tcp_estab;
};
......@@ -3360,6 +3400,7 @@ static int get_snmp_int(char *proto, char *key, int *result)
while (fgets(buf, sizeof(buf), fp) != NULL) {
char *p = buf;
int pos = 0;
if (memcmp(buf, proto, protolen))
continue;
while ((p = strchr(p, ' ')) != NULL) {
......@@ -3392,8 +3433,7 @@ static int get_snmp_int(char *proto, char *key, int *result)
/* Get stats from sockstat */
struct ssummary
{
struct ssummary {
int socks;
int tcp_mem;
int tcp_total;
......@@ -3449,13 +3489,13 @@ static int get_sockstat(struct ssummary *s)
if ((fp = net_sockstat_open()) == NULL)
return -1;
while(fgets(buf, sizeof(buf), fp) != NULL)
while (fgets(buf, sizeof(buf), fp) != NULL)
get_sockstat_line(buf, s);
fclose(fp);
if ((fp = net_sockstat6_open()) == NULL)
return 0;
while(fgets(buf, sizeof(buf), fp) != NULL)
while (fgets(buf, sizeof(buf), fp) != NULL)
get_sockstat_line(buf, s);
fclose(fp);
......@@ -3572,6 +3612,7 @@ static void usage(void)
static int scan_state(const char *state)
{
int i;
if (strcasecmp(state, "close") == 0 ||
strcasecmp(state, "closed") == 0)
return (1<<SS_CLOSE);
......@@ -3589,7 +3630,7 @@ static int scan_state(const char *state)
return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
if (strcasecmp(state, "big") == 0)
return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
for (i=0; i<SS_MAX; i++) {
for (i = 0; i < SS_MAX; i++) {
if (strcasecmp(state, sstate_namel[i]) == 0)
return (1<<i);
}
......@@ -3646,7 +3687,7 @@ int main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:K",
long_opts, NULL)) != EOF) {
switch(ch) {
switch (ch) {
case 'n':
resolve_services = 0;
break;
......@@ -3729,10 +3770,11 @@ int main(int argc, char *argv[])
case 'A':
{
char *p, *p1;
if (!saw_query) {
current_filter.dbs = 0;
state_filter = state_filter ?
state_filter : SS_CONN;
state_filter : SS_CONN;
saw_query = 1;
do_default = 0;
}
......@@ -3893,6 +3935,7 @@ int main(int argc, char *argv[])
if (dump_tcpdiag) {
FILE *dump_fp = stdout;
if (!(current_filter.dbs & (1<<TCP_DB))) {
fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
exit(0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment