]> git.proxmox.com Git - mirror_iproute2.git/blob - misc/rtacct.c
(Logical change 1.3)
[mirror_iproute2.git] / misc / rtacct.c
1 /*
2 * rtacct.c Applet to display contents of /proc/net/rt_acct.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #include <sys/time.h>
21 #include <fnmatch.h>
22 #include <sys/file.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sys/poll.h>
26 #include <sys/wait.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <signal.h>
30 #include <math.h>
31
32 #include "rt_names.h"
33
34 #include <SNAPSHOT.h>
35
36 int reset_history = 0;
37 int ignore_history = 0;
38 int no_output = 0;
39 int no_update = 0;
40 int scan_interval = 0;
41 int time_constant = 0;
42 int dump_zeros = 0;
43 unsigned long magic_number = 0;
44 double W;
45
46 int generic_proc_open(char *env, char *name)
47 {
48 char store[1024];
49 char *p = getenv(env);
50 if (!p) {
51 p = getenv("PROC_ROOT") ? : "/proc";
52 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
53 p = store;
54 }
55 return open(store, O_RDONLY);
56 }
57
58 int net_rtacct_open(void)
59 {
60 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
61 }
62
63 __u32 rmap[256/4];
64
65 struct rtacct_data
66 {
67 __u32 ival[256*4];
68
69 unsigned long long val[256*4];
70 double rate[256*4];
71 __u8 signature[128];
72 };
73
74 struct rtacct_data kern_db_static;
75
76 struct rtacct_data *kern_db = &kern_db_static;
77 struct rtacct_data *hist_db;
78
79 void nread(int fd, char *buf, int tot)
80 {
81 int count = 0;
82
83 while (count < tot) {
84 int n = read(fd, buf+count, tot-count);
85 if (n < 0) {
86 if (errno == EINTR)
87 continue;
88 exit(-1);
89 }
90 if (n == 0)
91 exit(-1);
92 count += n;
93 }
94 }
95
96
97 __u32 *read_kern_table(__u32 *tbl)
98 {
99 static __u32 *tbl_ptr;
100 int fd;
101
102 if (magic_number) {
103 if (tbl_ptr != NULL)
104 return tbl_ptr;
105
106 fd = open("/dev/mem", O_RDONLY);
107 if (fd < 0) {
108 perror("magic open");
109 exit(-1);
110 }
111 tbl_ptr = mmap(NULL, 4096,
112 PROT_READ,
113 MAP_SHARED,
114 fd, magic_number);
115 if ((unsigned long)tbl_ptr == ~0UL) {
116 perror("magic mmap");
117 exit(-1);
118 }
119 close(fd);
120 return tbl_ptr;
121 }
122
123 fd = net_rtacct_open();
124 if (fd >= 0) {
125 nread(fd, (char*)tbl, 256*16);
126 close(fd);
127 } else {
128 memset(tbl, 0, 256*16);
129 }
130 return tbl;
131 }
132
133 void format_rate(FILE *fp, double rate)
134 {
135 char temp[64];
136
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);
143 } else
144 fprintf(fp, " %-10u", (unsigned)rate);
145 }
146
147 void format_count(FILE *fp, unsigned long long val)
148 {
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);
153 else
154 fprintf(fp, " %10llu", val);
155 }
156
157 void dump_abs_db(FILE *fp)
158 {
159 int realm;
160 char b1[16];
161
162 if (!no_output) {
163 fprintf(fp, "#%s\n", kern_db->signature);
164 fprintf(fp,
165 "%-10s "
166 "%-10s "
167 "%-10s "
168 "%-10s "
169 "%-10s "
170 "\n"
171 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
172 fprintf(fp,
173 "%-10s "
174 "%-10s "
175 "%-10s "
176 "%-10s "
177 "%-10s "
178 "\n"
179 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
180
181 }
182
183 for (realm=0; realm<256; realm++) {
184 int i;
185 unsigned long long *val;
186 double *rate;
187
188 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
189 continue;
190
191 val = &kern_db->val[realm*4];
192 rate = &kern_db->rate[realm*4];
193
194 if (!dump_zeros &&
195 !val[0] && !rate[0] &&
196 !val[1] && !rate[1] &&
197 !val[2] && !rate[2] &&
198 !val[3] && !rate[3])
199 continue;
200
201 if (hist_db) {
202 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
203 }
204
205 if (no_output)
206 continue;
207
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]);
214 fprintf(fp, "\n");
215 }
216 }
217
218
219 void dump_incr_db(FILE *fp)
220 {
221 int k, realm;
222 char b1[16];
223
224 if (!no_output) {
225 fprintf(fp, "#%s\n", kern_db->signature);
226 fprintf(fp,
227 "%-10s "
228 "%-10s "
229 "%-10s "
230 "%-10s "
231 "%-10s "
232 "\n"
233 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
234 fprintf(fp,
235 "%-10s "
236 "%-10s "
237 "%-10s "
238 "%-10s "
239 "%-10s "
240 "\n"
241 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
242 }
243
244 for (realm=0; realm<256; realm++) {
245 int ovfl = 0;
246 int i;
247 unsigned long long *val;
248 double *rate;
249 unsigned long long rval[4];
250
251 if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
252 continue;
253
254 val = &kern_db->val[realm*4];
255 rate = &kern_db->rate[realm*4];
256
257 for (k=0; k<4; k++) {
258 rval[k] = val[k];
259 if (rval[k] < hist_db->val[realm*4+k])
260 ovfl = 1;
261 else
262 rval[k] -= hist_db->val[realm*4+k];
263 }
264 if (ovfl) {
265 for (k=0; k<4; k++)
266 rval[k] = val[k];
267 }
268 if (hist_db) {
269 memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
270 }
271
272 if (no_output)
273 continue;
274
275 if (!dump_zeros &&
276 !rval[0] && !rate[0] &&
277 !rval[1] && !rate[1] &&
278 !rval[2] && !rate[2] &&
279 !rval[3] && !rate[3])
280 continue;
281
282
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]);
289 fprintf(fp, "\n");
290 }
291 }
292
293
294 static int children;
295
296 void sigchild(int signo)
297 {
298 }
299
300 /* Server side only: read kernel data, update tables, calculate rates. */
301
302 void update_db(int interval)
303 {
304 int i;
305 __u32 *ival;
306 __u32 _ival[256*4];
307
308 ival = read_kern_table(_ival);
309
310 for (i=0; i<256*4; i++) {
311 double sample;
312 __u32 incr = ival[i] - kern_db->ival[i];
313
314 if (ival[i] == 0 && incr == 0 &&
315 kern_db->val[i] == 0 && kern_db->rate[i] == 0)
316 continue;
317
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;
326 } else {
327 double w = W*(double)interval/scan_interval;
328 kern_db->rate[i] += w*(sample-kern_db->rate[i]);
329 }
330 }
331 }
332 }
333
334 void send_db(int fd)
335 {
336 int tot = 0;
337
338 while (tot < sizeof(*kern_db)) {
339 int n = write(fd, ((char*)kern_db) + tot, sizeof(*kern_db)-tot);
340 if (n < 0) {
341 if (errno == EINTR)
342 continue;
343 return;
344 }
345 tot += n;
346 }
347 }
348
349
350
351 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
352
353
354 void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
355 {
356 int i;
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];
362 }
363
364 void server_loop(int fd)
365 {
366 struct timeval snaptime;
367 struct pollfd p;
368 p.fd = fd;
369 p.events = p.revents = POLLIN;
370
371 sprintf(kern_db->signature, "%d.%lu sampling_interval=%d time_const=%d",
372 getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
373
374 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
375
376 for (;;) {
377 int status;
378 int tdiff;
379 struct timeval now;
380 gettimeofday(&now, NULL);
381 tdiff = T_DIFF(now, snaptime);
382 if (tdiff >= scan_interval) {
383 update_db(tdiff);
384 snaptime = now;
385 tdiff = 0;
386 }
387 if (poll(&p, 1, tdiff + scan_interval) > 0
388 && (p.revents&POLLIN)) {
389 int clnt = accept(fd, NULL, NULL);
390 if (clnt >= 0) {
391 pid_t pid;
392 if (children >= 5) {
393 close(clnt);
394 } else if ((pid = fork()) != 0) {
395 if (pid>0)
396 children++;
397 close(clnt);
398 } else {
399 if (tdiff > 0)
400 update_db(tdiff);
401 send_db(clnt);
402 exit(0);
403 }
404 }
405 }
406 while (children && waitpid(-1, &status, WNOHANG) > 0)
407 children--;
408 }
409 }
410
411 int verify_forging(int fd)
412 {
413 struct ucred cred;
414 int olen = sizeof(cred);
415 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
416 olen < sizeof(cred))
417 return -1;
418 if (cred.uid == getuid() || cred.uid == 0)
419 return 0;
420 return -1;
421 }
422
423 static void usage(void) __attribute__((noreturn));
424
425 static void usage(void)
426 {
427 fprintf(stderr,
428 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
429 );
430 exit(-1);
431 }
432
433 int main(int argc, char *argv[])
434 {
435 char hist_name[128];
436 struct sockaddr_un sun;
437 int ch;
438 int fd;
439
440 while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
441 switch(ch) {
442 case 'z':
443 dump_zeros = 1;
444 break;
445 case 'r':
446 reset_history = 1;
447 break;
448 case 'a':
449 ignore_history = 1;
450 break;
451 case 's':
452 no_update = 1;
453 break;
454 case 'n':
455 no_output = 1;
456 break;
457 case 'd':
458 scan_interval = 1000*atoi(optarg);
459 break;
460 case 't':
461 if (sscanf(optarg, "%d", &time_constant) != 1 ||
462 time_constant <= 0) {
463 fprintf(stderr, "rtacct: invalid time constant divisor\n");
464 exit(-1);
465 }
466 break;
467 case 'v':
468 case 'V':
469 printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT);
470 exit(0);
471 case 'M':
472 /* Some secret undocumented option, nobody
473 * is expected to ask about its sense. See?
474 */
475 sscanf(optarg, "%lx", &magic_number);
476 break;
477 case 'h':
478 case '?':
479 default:
480 usage();
481 }
482 }
483
484 argc -= optind;
485 argv += optind;
486
487 if (argc) {
488 while (argc > 0) {
489 __u32 realm;
490 if (rtnl_rtrealm_a2n(&realm, argv[0])) {
491 fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
492 exit(-1);
493 }
494 rmap[realm>>5] |= (1<<(realm&0x1f));
495 argc--; argv++;
496 }
497 } else {
498 memset(rmap, ~0, sizeof(rmap));
499 /* Always suppress zeros. */
500 dump_zeros = 0;
501 }
502
503 sun.sun_family = AF_UNIX;
504 sun.sun_path[0] = 0;
505 sprintf(sun.sun_path+1, "rtacct%d", getuid());
506
507 if (scan_interval > 0) {
508 if (time_constant == 0)
509 time_constant = 60;
510 time_constant *= 1000;
511 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
512 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
513 perror("rtacct: socket");
514 exit(-1);
515 }
516 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
517 perror("rtacct: bind");
518 exit(-1);
519 }
520 if (listen(fd, 5) < 0) {
521 perror("rtacct: listen");
522 exit(-1);
523 }
524 if (fork())
525 exit(0);
526 chdir("/");
527 close(0); close(1); close(2); setsid();
528 signal(SIGPIPE, SIG_IGN);
529 signal(SIGCHLD, sigchild);
530 server_loop(fd);
531 exit(0);
532 }
533
534 if (getenv("RTACCT_HISTORY"))
535 snprintf(hist_name, sizeof(hist_name), getenv("RTACCT_HISTORY"));
536 else
537 sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
538
539 if (reset_history)
540 unlink(hist_name);
541
542 if (!ignore_history || !no_update) {
543 struct stat stb;
544
545 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
546 if (fd < 0) {
547 perror("rtacct: open history file");
548 exit(-1);
549 }
550 if (flock(fd, LOCK_EX)) {
551 perror("rtacct: flock history file");
552 exit(-1);
553 }
554 if (fstat(fd, &stb) != 0) {
555 perror("rtacct: fstat history file");
556 exit(-1);
557 }
558 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
559 fprintf(stderr, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
560 exit(-1);
561 }
562 if (stb.st_size != sizeof(*hist_db))
563 write(fd, kern_db, sizeof(*hist_db));
564
565 hist_db = mmap(NULL, sizeof(*hist_db),
566 PROT_READ|PROT_WRITE,
567 no_update ? MAP_PRIVATE : MAP_SHARED,
568 fd, 0);
569
570 if ((unsigned long)hist_db == ~0UL) {
571 perror("mmap");
572 exit(-1);
573 }
574
575 if (!ignore_history) {
576 FILE *tfp;
577 long uptime;
578 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
579 if (fscanf(tfp, "%ld", &uptime) != 1)
580 uptime = -1;
581 fclose(tfp);
582 }
583
584 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
585 fprintf(stderr, "rtacct: history is aged out, resetting\n");
586 memset(hist_db, 0, sizeof(*hist_db));
587 }
588 }
589
590 close(fd);
591 }
592
593 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
594 (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
595 || (strcpy(sun.sun_path+1, "rtacct0"),
596 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
597 && verify_forging(fd) == 0) {
598 nread(fd, (char*)kern_db, sizeof(*kern_db));
599 if (hist_db && hist_db->signature[0] &&
600 strcmp(kern_db->signature, hist_db->signature)) {
601 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
602 hist_db = NULL;
603 }
604 close(fd);
605 } else {
606 if (fd >= 0)
607 close(fd);
608
609 if (hist_db && hist_db->signature[0] &&
610 strcmp(hist_db->signature, "kernel")) {
611 fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
612 hist_db = NULL;
613 }
614
615 pad_kern_table(kern_db, read_kern_table(kern_db->ival));
616 strcpy(kern_db->signature, "kernel");
617 }
618
619 if (ignore_history || hist_db == NULL)
620 dump_abs_db(stdout);
621 else
622 dump_incr_db(stdout);
623
624 exit(0);
625 }