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; ...@@ -47,17 +47,16 @@ int ifnum;
int *ifvec; int *ifvec;
char **ifnames; char **ifnames;
struct dbkey struct dbkey {
{
__u32 iface; __u32 iface;
__u32 addr; __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_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_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; struct rtnl_handle rth;
...@@ -96,8 +95,7 @@ int poll_timeout = 30000; ...@@ -96,8 +95,7 @@ int poll_timeout = 30000;
static void usage(void) static void usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ]" "Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ] [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
" [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
exit(1); exit(1);
} }
...@@ -108,7 +106,7 @@ static int handle_if(int ifindex) ...@@ -108,7 +106,7 @@ static int handle_if(int ifindex)
if (ifnum == 0) if (ifnum == 0)
return 1; return 1;
for (i=0; i<ifnum; i++) for (i = 0; i < ifnum; i++)
if (ifvec[i] == ifindex) if (ifvec[i] == ifindex)
return 1; return 1;
return 0; return 0;
...@@ -123,7 +121,7 @@ static void do_sysctl_adjustments(void) ...@@ -123,7 +121,7 @@ static void do_sysctl_adjustments(void)
if (!ifnum) if (!ifnum)
return; return;
for (i=0; i<ifnum; i++) { for (i = 0; i < ifnum; i++) {
char buf[128]; char buf[128];
FILE *fp; FILE *fp;
...@@ -133,7 +131,7 @@ static void do_sysctl_adjustments(void) ...@@ -133,7 +131,7 @@ static void do_sysctl_adjustments(void)
if (no_kernel_broadcasts) if (no_kernel_broadcasts)
strcpy(buf, "0\n"); strcpy(buf, "0\n");
else 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); fputs(buf, fp);
fclose(fp); fclose(fp);
} }
...@@ -141,7 +139,7 @@ static void do_sysctl_adjustments(void) ...@@ -141,7 +139,7 @@ static void do_sysctl_adjustments(void)
sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]); sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]);
if ((fp = fopen(buf, "w")) != NULL) { 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); fputs(buf, fp);
fclose(fp); fclose(fp);
} }
...@@ -156,7 +154,7 @@ static void undo_sysctl_adjustments(void) ...@@ -156,7 +154,7 @@ static void undo_sysctl_adjustments(void)
if (!sysctl_adjusted) if (!sysctl_adjusted)
return; return;
for (i=0; i<ifnum; i++) { for (i = 0; i < ifnum; i++) {
char buf[128]; char buf[128];
FILE *fp; FILE *fp;
...@@ -185,7 +183,7 @@ static int send_probe(int ifindex, __u32 addr) ...@@ -185,7 +183,7 @@ static int send_probe(int ifindex, __u32 addr)
struct sockaddr_in dst; struct sockaddr_in dst;
socklen_t len; socklen_t len;
unsigned char buf[256]; unsigned char buf[256];
struct arphdr *ah = (struct arphdr*)buf; struct arphdr *ah = (struct arphdr *)buf;
unsigned char *p = (unsigned char *)(ah+1); unsigned char *p = (unsigned char *)(ah+1);
struct sockaddr_ll sll; struct sockaddr_ll sll;
...@@ -203,10 +201,10 @@ static int send_probe(int ifindex, __u32 addr) ...@@ -203,10 +201,10 @@ static int send_probe(int ifindex, __u32 addr)
dst.sin_family = AF_INET; dst.sin_family = AF_INET;
dst.sin_port = htons(1025); dst.sin_port = htons(1025);
dst.sin_addr.s_addr = addr; 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; return -1;
len = sizeof(dst); len = sizeof(dst);
if (getsockname(udp_sock, (struct sockaddr*)&dst, &len) < 0) if (getsockname(udp_sock, (struct sockaddr *)&dst, &len) < 0)
return -1; return -1;
ah->ar_hrd = htons(ifr.ifr_hwaddr.sa_family); ah->ar_hrd = htons(ifr.ifr_hwaddr.sa_family);
...@@ -219,19 +217,19 @@ static int send_probe(int ifindex, __u32 addr) ...@@ -219,19 +217,19 @@ static int send_probe(int ifindex, __u32 addr)
p += ah->ar_hln; p += ah->ar_hln;
memcpy(p, &dst.sin_addr, 4); memcpy(p, &dst.sin_addr, 4);
p+=4; p += 4;
sll.sll_family = AF_PACKET; sll.sll_family = AF_PACKET;
memset(sll.sll_addr, 0xFF, sizeof(sll.sll_addr)); memset(sll.sll_addr, 0xFF, sizeof(sll.sll_addr));
sll.sll_ifindex = ifindex; sll.sll_ifindex = ifindex;
sll.sll_protocol = htons(ETH_P_ARP); sll.sll_protocol = htons(ETH_P_ARP);
memcpy(p, &sll.sll_addr, ah->ar_hln); memcpy(p, &sll.sll_addr, ah->ar_hln);
p+=ah->ar_hln; p += ah->ar_hln;
memcpy(p, &addr, 4); 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; return -1;
stats.probes_sent++; stats.probes_sent++;
return 0; return 0;
...@@ -248,6 +246,7 @@ static int queue_active_probe(int ifindex, __u32 addr) ...@@ -248,6 +246,7 @@ static int queue_active_probe(int ifindex, __u32 addr)
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
if (prev.tv_sec) { if (prev.tv_sec) {
int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000; int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000;
buckets += diff; buckets += diff;
} else { } else {
buckets = broadcast_burst; buckets = broadcast_burst;
...@@ -266,9 +265,9 @@ static int queue_active_probe(int ifindex, __u32 addr) ...@@ -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) static int respond_to_kernel(int ifindex, __u32 addr, char *lla, int llalen)
{ {
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct ndmsg ndm; struct ndmsg ndm;
char buf[256]; char buf[256];
} req; } req;
memset(&req.n, 0, sizeof(req.n)); memset(&req.n, 0, sizeof(req.n));
...@@ -302,7 +301,7 @@ static int do_one_request(struct nlmsghdr *n) ...@@ -302,7 +301,7 @@ static int do_one_request(struct nlmsghdr *n)
{ {
struct ndmsg *ndm = NLMSG_DATA(n); struct ndmsg *ndm = NLMSG_DATA(n);
int len = n->nlmsg_len; int len = n->nlmsg_len;
struct rtattr * tb[NDA_MAX+1]; struct rtattr *tb[NDA_MAX+1];
struct dbkey key; struct dbkey key;
DBT dbkey, dbdat; DBT dbkey, dbdat;
int do_acct = 0; int do_acct = 0;
...@@ -405,6 +404,7 @@ static int do_one_request(struct nlmsghdr *n) ...@@ -405,6 +404,7 @@ static int do_one_request(struct nlmsghdr *n)
!IS_NEG(dbdat.data) || !IS_NEG(dbdat.data) ||
!NEG_VALID(dbdat.data)) { !NEG_VALID(dbdat.data)) {
__u8 ndata[6]; __u8 ndata[6];
stats.kern_neg++; stats.kern_neg++;
prepare_neg_entry(ndata, time(NULL)); prepare_neg_entry(ndata, time(NULL));
dbdat.data = ndata; dbdat.data = ndata;
...@@ -444,7 +444,7 @@ static void get_kern_msg(void) ...@@ -444,7 +444,7 @@ static void get_kern_msg(void)
struct iovec iov; struct iovec iov;
char buf[8192]; char buf[8192];
struct msghdr msg = { struct msghdr msg = {
(void*)&nladdr, sizeof(nladdr), (void *)&nladdr, sizeof(nladdr),
&iov, 1, &iov, 1,
NULL, 0, NULL, 0,
0 0
...@@ -466,7 +466,7 @@ static void get_kern_msg(void) ...@@ -466,7 +466,7 @@ static void get_kern_msg(void)
if (nladdr.nl_pid) if (nladdr.nl_pid)
return; return;
for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
int len = h->nlmsg_len; int len = h->nlmsg_len;
int l = len - sizeof(*h); int l = len - sizeof(*h);
...@@ -477,7 +477,7 @@ static void get_kern_msg(void) ...@@ -477,7 +477,7 @@ static void get_kern_msg(void)
return; return;
status -= NLMSG_ALIGN(len); 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) ...@@ -487,13 +487,13 @@ static void get_arp_pkt(void)
unsigned char buf[1024]; unsigned char buf[1024];
struct sockaddr_ll sll; struct sockaddr_ll sll;
socklen_t sll_len = sizeof(sll); socklen_t sll_len = sizeof(sll);
struct arphdr *a = (struct arphdr*)buf; struct arphdr *a = (struct arphdr *)buf;
struct dbkey key; struct dbkey key;
DBT dbkey, dbdat; DBT dbkey, dbdat;
int n; int n;
n = recvfrom(pset[0].fd, buf, sizeof(buf), MSG_DONTWAIT, n = recvfrom(pset[0].fd, buf, sizeof(buf), MSG_DONTWAIT,
(struct sockaddr*)&sll, &sll_len); (struct sockaddr *)&sll, &sll_len);
if (n < 0) { if (n < 0) {
if (errno != EINTR && errno != EAGAIN) if (errno != EINTR && errno != EAGAIN)
syslog(LOG_ERR, "recvfrom: %m"); syslog(LOG_ERR, "recvfrom: %m");
...@@ -515,7 +515,7 @@ static void get_arp_pkt(void) ...@@ -515,7 +515,7 @@ static void get_arp_pkt(void)
return; return;
key.iface = sll.sll_ifindex; 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. */ /* DAD message, ignore. */
if (key.addr == 0) if (key.addr == 0)
...@@ -600,7 +600,7 @@ int main(int argc, char **argv) ...@@ -600,7 +600,7 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "h?b:lf:a:n:p:kR:B:")) != EOF) { while ((opt = getopt(argc, argv, "h?b:lf:a:n:p:kR:B:")) != EOF) {
switch (opt) { switch (opt) {
case 'b': case 'b':
dbname = optarg; dbname = optarg;
break; break;
case 'f': case 'f':
...@@ -624,7 +624,7 @@ int main(int argc, char **argv) ...@@ -624,7 +624,7 @@ int main(int argc, char **argv)
break; break;
case 'p': case 'p':
if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) { if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) {
fprintf(stderr,"Invalid poll timeout\n"); fprintf(stderr, "Invalid poll timeout\n");
exit(-1); exit(-1);
} }
break; break;
...@@ -666,15 +666,16 @@ int main(int argc, char **argv) ...@@ -666,15 +666,16 @@ int main(int argc, char **argv)
exit(-1); exit(-1);
} }
if (ifnum) { if (ifnum) {
int i; int i;
struct ifreq ifr; struct ifreq ifr;
memset(&ifr, 0, sizeof(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); strncpy(ifr.ifr_name, ifnames[i], IFNAMSIZ);
if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) { if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) {
perror("ioctl(SIOCGIFINDEX)"); perror("ioctl(SIOCGIFINDEX)");
exit(-1);; exit(-1);
} }
ifvec[i] = ifr.ifr_ifindex; ifvec[i] = ifr.ifr_ifindex;
} }
...@@ -717,7 +718,7 @@ int main(int argc, char **argv) ...@@ -717,7 +718,7 @@ int main(int argc, char **argv)
} }
if (strncmp(macbuf, "FAILED:", 7) == 0) if (strncmp(macbuf, "FAILED:", 7) == 0)
continue; 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); fprintf(stderr, "Invalid IP address: \"%s\"\n", ipbuf);
goto do_abort; goto do_abort;
} }
...@@ -738,20 +739,23 @@ int main(int argc, char **argv) ...@@ -738,20 +739,23 @@ int main(int argc, char **argv)
if (do_list) { if (do_list) {
DBT dbkey, dbdat; DBT dbkey, dbdat;
printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC"); printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC");
while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) { while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) {
struct dbkey *key = dbkey.data; struct dbkey *key = dbkey.data;
if (handle_if(key->iface)) { if (handle_if(key->iface)) {
if (!IS_NEG(dbdat.data)) { if (!IS_NEG(dbdat.data)) {
char b1[18]; char b1[18];
printf("%-8d %-15s %s\n", printf("%-8d %-15s %s\n",
key->iface, 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)); ll_addr_n2a(dbdat.data, 6, ARPHRD_ETHER, b1, 18));
} else { } else {
printf("%-8d %-15s FAILED: %dsec ago\n", printf("%-8d %-15s FAILED: %dsec ago\n",
key->iface, key->iface,
inet_ntoa(*(struct in_addr*)&key->addr), inet_ntoa(*(struct in_addr *)&key->addr),
NEG_AGE(dbdat.data)); NEG_AGE(dbdat.data));
} }
} }
...@@ -769,11 +773,12 @@ int main(int argc, char **argv) ...@@ -769,11 +773,12 @@ int main(int argc, char **argv)
if (1) { if (1) {
struct sockaddr_ll sll; struct sockaddr_ll sll;
memset(&sll, 0, sizeof(sll)); memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET; sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_ARP); sll.sll_protocol = htons(ETH_P_ARP);
sll.sll_ifindex = (ifnum == 1 ? ifvec[0] : 0); 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"); perror("bind");
goto do_abort; goto do_abort;
} }
......
This diff is collapsed.
...@@ -56,10 +56,8 @@ static struct option opts[] = { ...@@ -56,10 +56,8 @@ static struct option opts[] = {
static int usage(char *name, int exit_code) static int usage(char *name, int exit_code)
{ {
fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION); fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
fprintf(stderr, "Copyright (C) 2004 by Harald Welte " fprintf(stderr, "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n");
"<laforge@gnumonks.org>\n"); fprintf(stderr, "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n\n");
fprintf(stderr, "This program is free software licensed under GNU GPLv2"
"\nwith ABSOLUTELY NO WARRANTY.\n\n");
fprintf(stderr, "Parameters:\n"); fprintf(stderr, "Parameters:\n");
fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n"); fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
fprintf(stderr, "\t-c --count <count>\t" fprintf(stderr, "\t-c --count <count>\t"
...@@ -145,14 +143,13 @@ static int map_field_params(struct lnstat_file *lnstat_files, ...@@ -145,14 +143,13 @@ static int map_field_params(struct lnstat_file *lnstat_files,
if (++j >= MAX_FIELDS - 1) { if (++j >= MAX_FIELDS - 1) {
fprintf(stderr, fprintf(stderr,
"WARN: MAX_FIELDS (%d) reached," "WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
" truncating number of keys\n",
MAX_FIELDS); MAX_FIELDS);
goto full; goto full;
} }
} }
} }
full: full:
fps->num = j; fps->num = j;
return 1; return 1;
} }
...@@ -181,7 +178,7 @@ static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files, ...@@ -181,7 +178,7 @@ static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
struct field_params *fps, struct field_params *fps,
int linewidth) int linewidth)
{ {
int h,i; int h, i;
static struct table_hdr th; static struct table_hdr th;
int ofs = 0; int ofs = 0;
...@@ -269,7 +266,7 @@ int main(int argc, char **argv) ...@@ -269,7 +266,7 @@ int main(int argc, char **argv)
num_req_files = 1; 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) { opts, NULL)) != -1) {
int len = 0; int len = 0;
char *tmp, *tok; char *tmp, *tok;
...@@ -303,8 +300,7 @@ int main(int argc, char **argv) ...@@ -303,8 +300,7 @@ int main(int argc, char **argv)
tok = strtok(NULL, ",")) { tok = strtok(NULL, ",")) {
if (fp.num >= MAX_FIELDS) { if (fp.num >= MAX_FIELDS) {
fprintf(stderr, fprintf(stderr,
"WARN: too many keys" "WARN: too many keys requested: (%d max)\n",
" requested: (%d max)\n",
MAX_FIELDS); MAX_FIELDS);
break; break;
} }
...@@ -356,7 +352,7 @@ int main(int argc, char **argv) ...@@ -356,7 +352,7 @@ int main(int argc, char **argv)
if (!header) if (!header)
exit(1); exit(1);
if (interval < 1 ) if (interval < 1)
interval = 1; interval = 1;
for (i = 0; i < count || !count; i++) { for (i = 0; i < count || !count; i++) {
......
...@@ -49,7 +49,7 @@ static int scan_lines(struct lnstat_file *lf, int 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)) if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
return -1; 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; char *ptr = buf;
num_lines++; num_lines++;
...@@ -58,6 +58,7 @@ static int scan_lines(struct lnstat_file *lf, int i) ...@@ -58,6 +58,7 @@ static int scan_lines(struct lnstat_file *lf, int i)
for (j = 0; j < lf->num_fields; j++) { for (j = 0; j < lf->num_fields; j++) {
unsigned long f = strtoul(ptr, &ptr, 16); unsigned long f = strtoul(ptr, &ptr, 16);
if (j == 0) if (j == 0)
lf->fields[j].values[i] = f; lf->fields[j].values[i] = f;
else else
...@@ -102,7 +103,7 @@ int lnstat_update(struct lnstat_file *lnstat_files) ...@@ -102,7 +103,7 @@ int lnstat_update(struct lnstat_file *lnstat_files)
lfi->result = lfi->values[1]; lfi->result = lfi->values[1];
else else
lfi->result = (lfi->values[1]-lfi->values[0]) lfi->result = (lfi->values[1]-lfi->values[0])
/ lf->interval.tv_sec; / lf->interval.tv_sec;
} }
scan_lines(lf, 0); scan_lines(lf, 0);
...@@ -158,6 +159,7 @@ static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf) ...@@ -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) static int name_in_array(const int num, const char **arr, const char *name)
{ {
int i; int i;
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (!strcmp(arr[i], name)) if (!strcmp(arr[i], name))
return 1; return 1;
......
...@@ -31,15 +31,15 @@ ...@@ -31,15 +31,15 @@
#include <json_writer.h> #include <json_writer.h>
#include <SNAPSHOT.h> #include <SNAPSHOT.h>
int dump_zeros = 0; int dump_zeros;
int reset_history = 0; int reset_history;
int ignore_history = 0; int ignore_history;
int no_output = 0; int no_output;
int json_output = 0; int json_output;
int pretty = 0; int pretty;
int no_update = 0; int no_update;
int scan_interval = 0; int scan_interval;
int time_constant = 0; int time_constant;
double W; double W;
char **patterns; char **patterns;
int npatterns; int npatterns;
...@@ -51,6 +51,7 @@ static int generic_proc_open(const char *env, char *name) ...@@ -51,6 +51,7 @@ static int generic_proc_open(const char *env, char *name)
{ {
char store[128]; char store[128];
char *p = getenv(env); char *p = getenv(env);
if (!p) { if (!p) {
p = getenv("PROC_ROOT") ? : "/proc"; p = getenv("PROC_ROOT") ? : "/proc";
snprintf(store, sizeof(store)-1, "%s/%s", p, name); snprintf(store, sizeof(store)-1, "%s/%s", p, name);
...@@ -74,8 +75,7 @@ static int net_snmp6_open(void) ...@@ -74,8 +75,7 @@ static int net_snmp6_open(void)
return generic_proc_open("PROC_NET_SNMP6", "net/snmp6"); return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
} }
struct nstat_ent struct nstat_ent {
{
struct nstat_ent *next; struct nstat_ent *next;
char *id; char *id;
unsigned long long val; unsigned long long val;
...@@ -94,7 +94,8 @@ static const char *useless_numbers[] = { ...@@ -94,7 +94,8 @@ static const char *useless_numbers[] = {
static int useless_number(const char *id) static int useless_number(const char *id)
{ {
int i; 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) if (strcmp(id, useless_numbers[i]) == 0)
return 1; return 1;
return 0; return 0;
...@@ -107,7 +108,7 @@ static int match(const char *id) ...@@ -107,7 +108,7 @@ static int match(const char *id)
if (npatterns == 0) if (npatterns == 0)
return 1; return 1;
for (i=0; i<npatterns; i++) { for (i = 0; i < npatterns; i++) {
if (!fnmatch(patterns[i], id, 0)) if (!fnmatch(patterns[i], id, 0))
return 1; return 1;
} }
...@@ -125,6 +126,7 @@ static void load_good_table(FILE *fp) ...@@ -125,6 +126,7 @@ static void load_good_table(FILE *fp)
unsigned long long val; unsigned long long val;
double rate; double rate;
char idbuf[sizeof(buf)]; char idbuf[sizeof(buf)];
if (buf[0] == '#') { if (buf[0] == '#') {
buf[strlen(buf)-1] = 0; buf[strlen(buf)-1] = 0;
if (info_source[0] && strcmp(info_source, buf+1)) if (info_source[0] && strcmp(info_source, buf+1))
...@@ -192,6 +194,7 @@ static void load_ugly_table(FILE *fp) ...@@ -192,6 +194,7 @@ static void load_ugly_table(FILE *fp)
while (*p) { while (*p) {
char *next; char *next;
if ((next = strchr(p, ' ')) != NULL) if ((next = strchr(p, ' ')) != NULL)
*next++ = 0; *next++ = 0;
else if ((next = strchr(p, '\n')) != NULL) else if ((next = strchr(p, '\n')) != NULL)
...@@ -246,6 +249,7 @@ static void load_ugly_table(FILE *fp) ...@@ -246,6 +249,7 @@ static void load_ugly_table(FILE *fp)
static void load_snmp(void) static void load_snmp(void)
{ {
FILE *fp = fdopen(net_snmp_open(), "r"); FILE *fp = fdopen(net_snmp_open(), "r");
if (fp) { if (fp) {
load_ugly_table(fp); load_ugly_table(fp);
fclose(fp); fclose(fp);
...@@ -255,6 +259,7 @@ static void load_snmp(void) ...@@ -255,6 +259,7 @@ static void load_snmp(void)
static void load_snmp6(void) static void load_snmp6(void)
{ {
FILE *fp = fdopen(net_snmp6_open(), "r"); FILE *fp = fdopen(net_snmp6_open(), "r");
if (fp) { if (fp) {
load_good_table(fp); load_good_table(fp);
fclose(fp); fclose(fp);
...@@ -264,6 +269,7 @@ static void load_snmp6(void) ...@@ -264,6 +269,7 @@ static void load_snmp6(void)
static void load_netstat(void) static void load_netstat(void)
{ {
FILE *fp = fdopen(net_netstat_open(), "r"); FILE *fp = fdopen(net_netstat_open(), "r");
if (fp) { if (fp) {
load_ugly_table(fp); load_ugly_table(fp);
fclose(fp); fclose(fp);
...@@ -284,12 +290,14 @@ static void dump_kern_db(FILE *fp, int to_hist) ...@@ -284,12 +290,14 @@ static void dump_kern_db(FILE *fp, int to_hist)
} else } else
fprintf(fp, "#%s\n", info_source); 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; unsigned long long val = n->val;
if (!dump_zeros && !val && !n->rate) if (!dump_zeros && !val && !n->rate)
continue; continue;
if (!match(n->id)) { if (!match(n->id)) {
struct nstat_ent *h1; struct nstat_ent *h1;
if (!to_hist) if (!to_hist)
continue; continue;
for (h1 = h; h1; h1 = h1->next) { for (h1 = h; h1; h1 = h1->next) {
...@@ -326,10 +334,11 @@ static void dump_incr_db(FILE *fp) ...@@ -326,10 +334,11 @@ static void dump_incr_db(FILE *fp)
} else } else
fprintf(fp, "#%s\n", info_source); 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; int ovfl = 0;
unsigned long long val = n->val; unsigned long long val = n->val;
struct nstat_ent *h1; struct nstat_ent *h1;
for (h1 = h; h1; h1 = h1->next) { for (h1 = h; h1; h1 = h1->next) {
if (strcmp(h1->id, n->id) == 0) { if (strcmp(h1->id, n->id) == 0) {
if (val < h1->val) { if (val < h1->val) {
...@@ -381,6 +390,7 @@ static void update_db(int interval) ...@@ -381,6 +390,7 @@ static void update_db(int interval)
for (n = kern_db; n; n = n->next) { for (n = kern_db; n; n = n->next) {
struct nstat_ent *h1; struct nstat_ent *h1;
for (h1 = h; h1; h1 = h1->next) { for (h1 = h; h1; h1 = h1->next) {
if (strcmp(h1->id, n->id) == 0) { if (strcmp(h1->id, n->id) == 0) {
double sample; double sample;
...@@ -395,12 +405,14 @@ static void update_db(int interval) ...@@ -395,12 +405,14 @@ static void update_db(int interval)
n->rate = sample; n->rate = sample;
} else { } else {
double w = W*(double)interval/scan_interval; double w = W*(double)interval/scan_interval;
n->rate += w*(sample-n->rate); n->rate += w*(sample-n->rate);
} }
} }
while (h != h1) { while (h != h1) {
struct nstat_ent *tmp = h; struct nstat_ent *tmp = h;
h = h->next; h = h->next;
free(tmp->id); free(tmp->id);
free(tmp); free(tmp);
...@@ -414,13 +426,14 @@ static void update_db(int interval) ...@@ -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) static void server_loop(int fd)
{ {
struct timeval snaptime = { 0 }; struct timeval snaptime = { 0 };
struct pollfd p; struct pollfd p;
p.fd = fd; p.fd = fd;
p.events = p.revents = POLLIN; p.events = p.revents = POLLIN;
...@@ -435,6 +448,7 @@ static void server_loop(int fd) ...@@ -435,6 +448,7 @@ static void server_loop(int fd)
int status; int status;
time_t tdiff; time_t tdiff;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
tdiff = T_DIFF(now, snaptime); tdiff = T_DIFF(now, snaptime);
if (tdiff >= scan_interval) { if (tdiff >= scan_interval) {
...@@ -445,16 +459,19 @@ static void server_loop(int fd) ...@@ -445,16 +459,19 @@ static void server_loop(int fd)
if (poll(&p, 1, scan_interval - tdiff) > 0 if (poll(&p, 1, scan_interval - tdiff) > 0
&& (p.revents&POLLIN)) { && (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL); int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) { if (clnt >= 0) {
pid_t pid; pid_t pid;
if (children >= 5) { if (children >= 5) {
close(clnt); close(clnt);
} else if ((pid = fork()) != 0) { } else if ((pid = fork()) != 0) {
if (pid>0) if (pid > 0)
children++; children++;
close(clnt); close(clnt);
} else { } else {
FILE *fp = fdopen(clnt, "w"); FILE *fp = fdopen(clnt, "w");
if (fp) if (fp)
dump_kern_db(fp, 0); dump_kern_db(fp, 0);
exit(0); exit(0);
...@@ -471,7 +488,7 @@ static int verify_forging(int fd) ...@@ -471,7 +488,7 @@ static int verify_forging(int fd)
struct ucred cred; struct ucred cred;
socklen_t olen = sizeof(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)) olen < sizeof(cred))
return -1; return -1;
if (cred.uid == getuid() || cred.uid == 0) if (cred.uid == getuid() || cred.uid == 0)
...@@ -524,7 +541,7 @@ int main(int argc, char *argv[]) ...@@ -524,7 +541,7 @@ int main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp", while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp",
longopts, NULL)) != EOF) { longopts, NULL)) != EOF) {
switch(ch) { switch (ch) {
case 'z': case 'z':
dump_zeros = 1; dump_zeros = 1;
break; break;
...@@ -583,7 +600,7 @@ int main(int argc, char *argv[]) ...@@ -583,7 +600,7 @@ int main(int argc, char *argv[])
perror("nstat: socket"); perror("nstat: socket");
exit(-1); 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"); perror("nstat: bind");
exit(-1); exit(-1);
} }
...@@ -639,6 +656,7 @@ int main(int argc, char *argv[]) ...@@ -639,6 +656,7 @@ int main(int argc, char *argv[])
if (!ignore_history) { if (!ignore_history) {
FILE *tfp; FILE *tfp;
long uptime = -1; long uptime = -1;
if ((tfp = fopen("/proc/uptime", "r")) != NULL) { if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
if (fscanf(tfp, "%ld", &uptime) != 1) if (fscanf(tfp, "%ld", &uptime) != 1)
uptime = -1; uptime = -1;
...@@ -658,11 +676,12 @@ int main(int argc, char *argv[]) ...@@ -658,11 +676,12 @@ int main(int argc, char *argv[])
} }
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && 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"), || (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) { && verify_forging(fd) == 0) {
FILE *sfp = fdopen(fd, "r"); FILE *sfp = fdopen(fd, "r");
load_good_table(sfp); load_good_table(sfp);
if (hist_db && source_mismatch) { if (hist_db && source_mismatch) {
fprintf(stderr, "nstat: history is stale, ignoring it.\n"); fprintf(stderr, "nstat: history is stale, ignoring it.\n");
......
...@@ -33,20 +33,21 @@ ...@@ -33,20 +33,21 @@
#include <SNAPSHOT.h> #include <SNAPSHOT.h>
int reset_history = 0; int reset_history;
int ignore_history = 0; int ignore_history;
int no_output = 0; int no_output;
int no_update = 0; int no_update;
int scan_interval = 0; int scan_interval;
int time_constant = 0; int time_constant;
int dump_zeros = 0; int dump_zeros;
unsigned long magic_number = 0; unsigned long magic_number;
double W; double W;
static int generic_proc_open(const char *env, const char *name) static int generic_proc_open(const char *env, const char *name)
{ {
char store[1024]; char store[1024];
char *p = getenv(env); char *p = getenv(env);
if (!p) { if (!p) {
p = getenv("PROC_ROOT") ? : "/proc"; p = getenv("PROC_ROOT") ? : "/proc";
snprintf(store, sizeof(store)-1, "%s/%s", p, name); snprintf(store, sizeof(store)-1, "%s/%s", p, name);
...@@ -62,8 +63,7 @@ static int net_rtacct_open(void) ...@@ -62,8 +63,7 @@ static int net_rtacct_open(void)
static __u32 rmap[256/4]; static __u32 rmap[256/4];
struct rtacct_data struct rtacct_data {
{
__u32 ival[256*4]; __u32 ival[256*4];
unsigned long long val[256*4]; unsigned long long val[256*4];
...@@ -82,6 +82,7 @@ static void nread(int fd, char *buf, int tot) ...@@ -82,6 +82,7 @@ static void nread(int fd, char *buf, int tot)
while (count < tot) { while (count < tot) {
int n = read(fd, buf+count, tot-count); int n = read(fd, buf+count, tot-count);
if (n < 0) { if (n < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
...@@ -121,7 +122,7 @@ static __u32 *read_kern_table(__u32 *tbl) ...@@ -121,7 +122,7 @@ static __u32 *read_kern_table(__u32 *tbl)
fd = net_rtacct_open(); fd = net_rtacct_open();
if (fd >= 0) { if (fd >= 0) {
nread(fd, (char*)tbl, 256*16); nread(fd, (char *)tbl, 256*16);
close(fd); close(fd);
} else { } else {
memset(tbl, 0, 256*16); memset(tbl, 0, 256*16);
...@@ -134,13 +135,13 @@ static void format_rate(FILE *fp, double rate) ...@@ -134,13 +135,13 @@ static void format_rate(FILE *fp, double rate)
char temp[64]; char temp[64];
if (rate > 1024*1024) { 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); fprintf(fp, " %-10s", temp);
} else if (rate > 1024) { } else if (rate > 1024) {
sprintf(temp, "%uK", (unsigned)rint(rate/1024)); sprintf(temp, "%uK", (unsigned int)rint(rate/1024));
fprintf(fp, " %-10s", temp); fprintf(fp, " %-10s", temp);
} else } else
fprintf(fp, " %-10u", (unsigned)rate); fprintf(fp, " %-10u", (unsigned int)rate);
} }
static void format_count(FILE *fp, unsigned long long val) static void format_count(FILE *fp, unsigned long long val)
...@@ -161,25 +162,19 @@ static void dump_abs_db(FILE *fp) ...@@ -161,25 +162,19 @@ static void dump_abs_db(FILE *fp)
if (!no_output) { if (!no_output) {
fprintf(fp, "#%s\n", kern_db->signature); fprintf(fp, "#%s\n", kern_db->signature);
fprintf(fp, fprintf(fp,
"%-10s " "%-10s %-10s "
"%-10s " "%-10s %-10s "
"%-10s " "%-10s \n"
"%-10s "
"%-10s "
"\n"
, "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom"); , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
fprintf(fp, fprintf(fp,
"%-10s " "%-10s %-10s "
"%-10s " "%-10s %-10s "
"%-10s " "%-10s \n"
"%-10s "
"%-10s "
"\n"
, "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom"); , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
} }
for (realm=0; realm<256; realm++) { for (realm = 0; realm < 256; realm++) {
int i; int i;
unsigned long long *val; unsigned long long *val;
double *rate; double *rate;
...@@ -223,24 +218,18 @@ static void dump_incr_db(FILE *fp) ...@@ -223,24 +218,18 @@ static void dump_incr_db(FILE *fp)
if (!no_output) { if (!no_output) {
fprintf(fp, "#%s\n", kern_db->signature); fprintf(fp, "#%s\n", kern_db->signature);
fprintf(fp, fprintf(fp,
"%-10s " "%-10s %-10s "
"%-10s " "%-10s %-10s "
"%-10s " "%-10s \n"
"%-10s "
"%-10s "
"\n"
, "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom"); , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
fprintf(fp, fprintf(fp,
"%-10s " "%-10s %-10s "
"%-10s " "%-10s %-10s "
"%-10s " "%-10s \n"
"%-10s "
"%-10s "
"\n"
, "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom"); , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
} }
for (realm=0; realm<256; realm++) { for (realm = 0; realm < 256; realm++) {
int ovfl = 0; int ovfl = 0;
int i; int i;
unsigned long long *val; unsigned long long *val;
...@@ -253,7 +242,7 @@ static void dump_incr_db(FILE *fp) ...@@ -253,7 +242,7 @@ static void dump_incr_db(FILE *fp)
val = &kern_db->val[realm*4]; val = &kern_db->val[realm*4];
rate = &kern_db->rate[realm*4]; rate = &kern_db->rate[realm*4];
for (k=0; k<4; k++) { for (k = 0; k < 4; k++) {
rval[k] = val[k]; rval[k] = val[k];
if (rval[k] < hist_db->val[realm*4+k]) if (rval[k] < hist_db->val[realm*4+k])
ovfl = 1; ovfl = 1;
...@@ -261,7 +250,7 @@ static void dump_incr_db(FILE *fp) ...@@ -261,7 +250,7 @@ static void dump_incr_db(FILE *fp)
rval[k] -= hist_db->val[realm*4+k]; rval[k] -= hist_db->val[realm*4+k];
} }
if (ovfl) { if (ovfl) {
for (k=0; k<4; k++) for (k = 0; k < 4; k++)
rval[k] = val[k]; rval[k] = val[k];
} }
if (hist_db) { if (hist_db) {
...@@ -306,7 +295,7 @@ static void update_db(int interval) ...@@ -306,7 +295,7 @@ static void update_db(int interval)
ival = read_kern_table(_ival); ival = read_kern_table(_ival);
for (i=0; i<256*4; i++) { for (i = 0; i < 256*4; i++) {
double sample; double sample;
__u32 incr = ival[i] - kern_db->ival[i]; __u32 incr = ival[i] - kern_db->ival[i];
...@@ -324,6 +313,7 @@ static void update_db(int interval) ...@@ -324,6 +313,7 @@ static void update_db(int interval)
kern_db->rate[i] = sample; kern_db->rate[i] = sample;
} else { } else {
double w = W*(double)interval/scan_interval; double w = W*(double)interval/scan_interval;
kern_db->rate[i] += w*(sample-kern_db->rate[i]); kern_db->rate[i] += w*(sample-kern_db->rate[i]);
} }
} }
...@@ -335,7 +325,8 @@ static void send_db(int fd) ...@@ -335,7 +325,8 @@ static void send_db(int fd)
int tot = 0; int tot = 0;
while (tot < sizeof(*kern_db)) { 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 (n < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
...@@ -347,16 +338,17 @@ static void send_db(int fd) ...@@ -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) static void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
{ {
int i; int i;
memset(dat->rate, 0, sizeof(dat->rate)); memset(dat->rate, 0, sizeof(dat->rate));
if (dat->ival != ival) if (dat->ival != ival)
memcpy(dat->ival, ival, sizeof(dat->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]; dat->val[i] = ival[i];
} }
...@@ -364,12 +356,13 @@ static void server_loop(int fd) ...@@ -364,12 +356,13 @@ static void server_loop(int fd)
{ {
struct timeval snaptime = { 0 }; struct timeval snaptime = { 0 };
struct pollfd p; struct pollfd p;
p.fd = fd; p.fd = fd;
p.events = p.revents = POLLIN; p.events = p.revents = POLLIN;
sprintf(kern_db->signature, sprintf(kern_db->signature,
"%u.%lu sampling_interval=%d time_const=%d", "%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); scan_interval/1000, time_constant/1000);
pad_kern_table(kern_db, read_kern_table(kern_db->ival)); pad_kern_table(kern_db, read_kern_table(kern_db->ival));
...@@ -378,6 +371,7 @@ static void server_loop(int fd) ...@@ -378,6 +371,7 @@ static void server_loop(int fd)
int status; int status;
int tdiff; int tdiff;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
tdiff = T_DIFF(now, snaptime); tdiff = T_DIFF(now, snaptime);
if (tdiff >= scan_interval) { if (tdiff >= scan_interval) {
...@@ -388,12 +382,14 @@ static void server_loop(int fd) ...@@ -388,12 +382,14 @@ static void server_loop(int fd)
if (poll(&p, 1, tdiff + scan_interval) > 0 if (poll(&p, 1, tdiff + scan_interval) > 0
&& (p.revents&POLLIN)) { && (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL); int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) { if (clnt >= 0) {
pid_t pid; pid_t pid;
if (children >= 5) { if (children >= 5) {
close(clnt); close(clnt);
} else if ((pid = fork()) != 0) { } else if ((pid = fork()) != 0) {
if (pid>0) if (pid > 0)
children++; children++;
close(clnt); close(clnt);
} else { } else {
...@@ -414,7 +410,7 @@ static int verify_forging(int fd) ...@@ -414,7 +410,7 @@ static int verify_forging(int fd)
struct ucred cred; struct ucred cred;
socklen_t olen = sizeof(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)) olen < sizeof(cred))
return -1; return -1;
if (cred.uid == getuid() || cred.uid == 0) if (cred.uid == getuid() || cred.uid == 0)
...@@ -440,7 +436,7 @@ int main(int argc, char *argv[]) ...@@ -440,7 +436,7 @@ int main(int argc, char *argv[])
int fd; int fd;
while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) { while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
switch(ch) { switch (ch) {
case 'z': case 'z':
dump_zeros = 1; dump_zeros = 1;
break; break;
...@@ -489,6 +485,7 @@ int main(int argc, char *argv[]) ...@@ -489,6 +485,7 @@ int main(int argc, char *argv[])
if (argc) { if (argc) {
while (argc > 0) { while (argc > 0) {
__u32 realm; __u32 realm;
if (rtnl_rtrealm_a2n(&realm, argv[0])) { if (rtnl_rtrealm_a2n(&realm, argv[0])) {
fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]); fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
exit(-1); exit(-1);
...@@ -515,7 +512,7 @@ int main(int argc, char *argv[]) ...@@ -515,7 +512,7 @@ int main(int argc, char *argv[])
perror("rtacct: socket"); perror("rtacct: socket");
exit(-1); 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"); perror("rtacct: bind");
exit(-1); exit(-1);
} }
...@@ -580,6 +577,7 @@ int main(int argc, char *argv[]) ...@@ -580,6 +577,7 @@ int main(int argc, char *argv[])
if (!ignore_history) { if (!ignore_history) {
FILE *tfp; FILE *tfp;
long uptime = -1; long uptime = -1;
if ((tfp = fopen("/proc/uptime", "r")) != NULL) { if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
if (fscanf(tfp, "%ld", &uptime) != 1) if (fscanf(tfp, "%ld", &uptime) != 1)
uptime = -1; uptime = -1;
...@@ -596,11 +594,11 @@ int main(int argc, char *argv[]) ...@@ -596,11 +594,11 @@ int main(int argc, char *argv[])
} }
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && 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"), || (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) { && 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] && if (hist_db && hist_db->signature[0] &&
strcmp(kern_db->signature, hist_db->signature)) { strcmp(kern_db->signature, hist_db->signature)) {
fprintf(stderr, "rtacct: history is stale, ignoring it.\n"); fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
......
This diff is collapsed.
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