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;
}
......
This diff is collapsed.
......@@ -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");
......
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