2 * rtacct.c Applet to display contents of /proc/net/rt_acct.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
23 #include <sys/socket.h>
36 int reset_history = 0;
37 int ignore_history = 0;
40 int scan_interval = 0;
41 int time_constant = 0;
43 unsigned long magic_number = 0;
46 int generic_proc_open(char *env, char *name)
49 char *p = getenv(env);
51 p = getenv("PROC_ROOT") ? : "/proc";
52 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
55 return open(store, O_RDONLY);
58 int net_rtacct_open(void)
60 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
69 unsigned long long val[256*4];
74 struct rtacct_data kern_db_static;
76 struct rtacct_data *kern_db = &kern_db_static;
77 struct rtacct_data *hist_db;
79 void nread(int fd, char *buf, int tot)
84 int n = read(fd, buf+count, tot-count);
97 __u32 *read_kern_table(__u32 *tbl)
99 static __u32 *tbl_ptr;
106 fd = open("/dev/mem", O_RDONLY);
108 perror("magic open");
111 tbl_ptr = mmap(NULL, 4096,
115 if ((unsigned long)tbl_ptr == ~0UL) {
116 perror("magic mmap");
123 fd = net_rtacct_open();
125 nread(fd, (char*)tbl, 256*16);
128 memset(tbl, 0, 256*16);
133 void format_rate(FILE *fp, double rate)
137 if (rate > 1024*1024) {
138 sprintf(temp, "%uM", (unsigned)rint(rate/(1024*1024)));
139 fprintf(fp, " %-10s", temp);
140 } else if (rate > 1024) {
141 sprintf(temp, "%uK", (unsigned)rint(rate/1024));
142 fprintf(fp, " %-10s", temp);
144 fprintf(fp, " %-10u", (unsigned)rate);
147 void format_count(FILE *fp, unsigned long long val)
149 if (val > 1024*1024*1024)
150 fprintf(fp, " %10lluM", val/(1024*1024));
151 else if (val > 1024*1024)
152 fprintf(fp, " %10lluK", val/1024);
154 fprintf(fp, " %10llu", val);
157 void dump_abs_db(FILE *fp)
163 fprintf(fp, "#%s\n", kern_db->signature);
171 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
179 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
183 for (realm=0; realm<256; realm++) {
185 unsigned long long *val;
188 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
191 val = &kern_db->val[realm*4];
192 rate = &kern_db->rate[realm*4];
195 !val[0] && !rate[0] &&
196 !val[1] && !rate[1] &&
197 !val[2] && !rate[2] &&
202 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
208 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
209 for (i = 0; i < 4; i++)
210 format_count(fp, val[i]);
211 fprintf(fp, "\n%-10s", "");
212 for (i = 0; i < 4; i++)
213 format_rate(fp, rate[i]);
219 void dump_incr_db(FILE *fp)
225 fprintf(fp, "#%s\n", kern_db->signature);
233 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
241 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
244 for (realm=0; realm<256; realm++) {
247 unsigned long long *val;
249 unsigned long long rval[4];
251 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
254 val = &kern_db->val[realm*4];
255 rate = &kern_db->rate[realm*4];
257 for (k=0; k<4; k++) {
259 if (rval[k] < hist_db->val[realm*4+k])
262 rval[k] -= hist_db->val[realm*4+k];
269 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
276 !rval[0] && !rate[0] &&
277 !rval[1] && !rate[1] &&
278 !rval[2] && !rate[2] &&
279 !rval[3] && !rate[3])
283 fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
284 for (i = 0; i < 4; i++)
285 format_count(fp, rval[i]);
286 fprintf(fp, "\n%-10s", "");
287 for (i = 0; i < 4; i++)
288 format_rate(fp, rate[i]);
296 void sigchild(int signo)
300 /* Server side only: read kernel data, update tables, calculate rates. */
302 void update_db(int interval)
308 ival = read_kern_table(_ival);
310 for (i=0; i<256*4; i++) {
312 __u32 incr = ival[i] - kern_db->ival[i];
314 if (ival[i] == 0 && incr == 0 &&
315 kern_db->val[i] == 0 && kern_db->rate[i] == 0)
318 kern_db->val[i] += incr;
319 kern_db->ival[i] = ival[i];
320 sample = (double)(incr*1000)/interval;
321 if (interval >= scan_interval) {
322 kern_db->rate[i] += W*(sample-kern_db->rate[i]);
323 } else if (interval >= 1000) {
324 if (interval >= time_constant) {
325 kern_db->rate[i] = sample;
327 double w = W*(double)interval/scan_interval;
328 kern_db->rate[i] += w*(sample-kern_db->rate[i]);
338 while (tot < sizeof(*kern_db)) {
339 int n = write(fd, ((char*)kern_db) + tot, sizeof(*kern_db)-tot);
351 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
354 void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
357 memset(dat->rate, 0, sizeof(dat->rate));
358 if (dat->ival != ival)
359 memcpy(dat->ival, ival, sizeof(dat->ival));
360 for (i=0; i<256*4; i++)
361 dat->val[i] = ival[i];
364 void server_loop(int fd)
366 struct timeval snaptime = { 0 };
369 p.events = p.revents = POLLIN;
371 sprintf(kern_db->signature,
372 "%u.%lu sampling_interval=%d time_const=%d",
373 (unsigned) getpid(), (unsigned long)random(),
374 scan_interval/1000, time_constant/1000);
376 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
382 gettimeofday(&now, NULL);
383 tdiff = T_DIFF(now, snaptime);
384 if (tdiff >= scan_interval) {
389 if (poll(&p, 1, tdiff + scan_interval) > 0
390 && (p.revents&POLLIN)) {
391 int clnt = accept(fd, NULL, NULL);
396 } else if ((pid = fork()) != 0) {
408 while (children && waitpid(-1, &status, WNOHANG) > 0)
413 int verify_forging(int fd)
416 socklen_t olen = sizeof(cred);
418 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
421 if (cred.uid == getuid() || cred.uid == 0)
426 static void usage(void) __attribute__((noreturn));
428 static void usage(void)
431 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
436 int main(int argc, char *argv[])
439 struct sockaddr_un sun;
443 while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
461 scan_interval = 1000*atoi(optarg);
464 if (sscanf(optarg, "%d", &time_constant) != 1 ||
465 time_constant <= 0) {
466 fprintf(stderr, "rtacct: invalid time constant divisor\n");
472 printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT);
475 /* Some secret undocumented option, nobody
476 * is expected to ask about its sense. See?
478 sscanf(optarg, "%lx", &magic_number);
493 if (rtnl_rtrealm_a2n(&realm, argv[0])) {
494 fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
497 rmap[realm>>5] |= (1<<(realm&0x1f));
501 memset(rmap, ~0, sizeof(rmap));
502 /* Always suppress zeros. */
506 sun.sun_family = AF_UNIX;
508 sprintf(sun.sun_path+1, "rtacct%d", getuid());
510 if (scan_interval > 0) {
511 if (time_constant == 0)
513 time_constant *= 1000;
514 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
515 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
516 perror("rtacct: socket");
519 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
520 perror("rtacct: bind");
523 if (listen(fd, 5) < 0) {
524 perror("rtacct: listen");
530 close(0); close(1); close(2); setsid();
531 signal(SIGPIPE, SIG_IGN);
532 signal(SIGCHLD, sigchild);
537 if (getenv("RTACCT_HISTORY"))
538 snprintf(hist_name, sizeof(hist_name), getenv("RTACCT_HISTORY"));
540 sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
545 if (!ignore_history || !no_update) {
548 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
550 perror("rtacct: open history file");
553 if (flock(fd, LOCK_EX)) {
554 perror("rtacct: flock history file");
557 if (fstat(fd, &stb) != 0) {
558 perror("rtacct: fstat history file");
561 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
562 fprintf(stderr, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
565 if (stb.st_size != sizeof(*hist_db))
566 write(fd, kern_db, sizeof(*hist_db));
568 hist_db = mmap(NULL, sizeof(*hist_db),
569 PROT_READ|PROT_WRITE,
570 no_update ? MAP_PRIVATE : MAP_SHARED,
573 if ((unsigned long)hist_db == ~0UL) {
578 if (!ignore_history) {
581 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
582 if (fscanf(tfp, "%ld", &uptime) != 1)
587 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
588 fprintf(stderr, "rtacct: history is aged out, resetting\n");
589 memset(hist_db, 0, sizeof(*hist_db));
596 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
597 (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
598 || (strcpy(sun.sun_path+1, "rtacct0"),
599 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
600 && verify_forging(fd) == 0) {
601 nread(fd, (char*)kern_db, sizeof(*kern_db));
602 if (hist_db && hist_db->signature[0] &&
603 strcmp(kern_db->signature, hist_db->signature)) {
604 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
612 if (hist_db && hist_db->signature[0] &&
613 strcmp(hist_db->signature, "kernel")) {
614 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
618 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
619 strcpy(kern_db->signature, "kernel");
622 if (ignore_history || hist_db == NULL)
625 dump_incr_db(stdout);