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>
43 unsigned long magic_number
;
46 static int generic_proc_open(const char *env
, const char *name
)
49 char *p
= getenv(env
);
52 p
= getenv("PROC_ROOT") ? : "/proc";
53 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
56 return open(p
, O_RDONLY
);
59 static int net_rtacct_open(void)
61 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
64 static __u32 rmap
[256/4];
69 unsigned long long val
[256*4];
74 static struct rtacct_data kern_db_static
;
76 static struct rtacct_data
*kern_db
= &kern_db_static
;
77 static struct rtacct_data
*hist_db
;
79 static void nread(int fd
, char *buf
, int tot
)
84 int n
= read(fd
, buf
+count
, tot
-count
);
97 static __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 static void format_rate(FILE *fp
, double rate
)
137 if (rate
> 1024*1024) {
138 sprintf(temp
, "%uM", (unsigned int)rint(rate
/(1024*1024)));
139 fprintf(fp
, " %-10s", temp
);
140 } else if (rate
> 1024) {
141 sprintf(temp
, "%uK", (unsigned int)rint(rate
/1024));
142 fprintf(fp
, " %-10s", temp
);
144 fprintf(fp
, " %-10u", (unsigned int)rate
);
147 static 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 static void dump_abs_db(FILE *fp
)
163 fprintf(fp
, "#%s\n", kern_db
->signature
);
168 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
173 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
177 for (realm
= 0; realm
< 256; realm
++) {
179 unsigned long long *val
;
182 if (!(rmap
[realm
>>5] & (1<<(realm
&0x1f))))
185 val
= &kern_db
->val
[realm
*4];
186 rate
= &kern_db
->rate
[realm
*4];
189 !val
[0] && !rate
[0] &&
190 !val
[1] && !rate
[1] &&
191 !val
[2] && !rate
[2] &&
196 memcpy(&hist_db
->val
[realm
*4], val
, sizeof(*val
)*4);
202 fprintf(fp
, "%-10s", rtnl_rtrealm_n2a(realm
, b1
, sizeof(b1
)));
203 for (i
= 0; i
< 4; i
++)
204 format_count(fp
, val
[i
]);
205 fprintf(fp
, "\n%-10s", "");
206 for (i
= 0; i
< 4; i
++)
207 format_rate(fp
, rate
[i
]);
213 static void dump_incr_db(FILE *fp
)
219 fprintf(fp
, "#%s\n", kern_db
->signature
);
224 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
229 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
232 for (realm
= 0; realm
< 256; realm
++) {
235 unsigned long long *val
;
237 unsigned long long rval
[4];
239 if (!(rmap
[realm
>>5] & (1<<(realm
&0x1f))))
242 val
= &kern_db
->val
[realm
*4];
243 rate
= &kern_db
->rate
[realm
*4];
245 for (k
= 0; k
< 4; k
++) {
247 if (rval
[k
] < hist_db
->val
[realm
*4+k
])
250 rval
[k
] -= hist_db
->val
[realm
*4+k
];
253 for (k
= 0; k
< 4; k
++)
257 memcpy(&hist_db
->val
[realm
*4], val
, sizeof(*val
)*4);
264 !rval
[0] && !rate
[0] &&
265 !rval
[1] && !rate
[1] &&
266 !rval
[2] && !rate
[2] &&
267 !rval
[3] && !rate
[3])
271 fprintf(fp
, "%-10s", rtnl_rtrealm_n2a(realm
, b1
, sizeof(b1
)));
272 for (i
= 0; i
< 4; i
++)
273 format_count(fp
, rval
[i
]);
274 fprintf(fp
, "\n%-10s", "");
275 for (i
= 0; i
< 4; i
++)
276 format_rate(fp
, rate
[i
]);
284 static void sigchild(int signo
)
288 /* Server side only: read kernel data, update tables, calculate rates. */
290 static void update_db(int interval
)
296 ival
= read_kern_table(_ival
);
298 for (i
= 0; i
< 256*4; i
++) {
300 __u32 incr
= ival
[i
] - kern_db
->ival
[i
];
302 if (ival
[i
] == 0 && incr
== 0 &&
303 kern_db
->val
[i
] == 0 && kern_db
->rate
[i
] == 0)
306 kern_db
->val
[i
] += incr
;
307 kern_db
->ival
[i
] = ival
[i
];
308 sample
= (double)(incr
*1000)/interval
;
309 if (interval
>= scan_interval
) {
310 kern_db
->rate
[i
] += W
*(sample
-kern_db
->rate
[i
]);
311 } else if (interval
>= 1000) {
312 if (interval
>= time_constant
) {
313 kern_db
->rate
[i
] = sample
;
315 double w
= W
*(double)interval
/scan_interval
;
317 kern_db
->rate
[i
] += w
*(sample
-kern_db
->rate
[i
]);
323 static void send_db(int fd
)
327 while (tot
< sizeof(*kern_db
)) {
328 int n
= write(fd
, ((char *)kern_db
) + tot
, sizeof(*kern_db
)-tot
);
341 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
344 static void pad_kern_table(struct rtacct_data
*dat
, __u32
*ival
)
348 memset(dat
->rate
, 0, sizeof(dat
->rate
));
349 if (dat
->ival
!= ival
)
350 memcpy(dat
->ival
, ival
, sizeof(dat
->ival
));
351 for (i
= 0; i
< 256*4; i
++)
352 dat
->val
[i
] = ival
[i
];
355 static void server_loop(int fd
)
357 struct timeval snaptime
= { 0 };
361 p
.events
= p
.revents
= POLLIN
;
363 sprintf(kern_db
->signature
,
364 "%u.%lu sampling_interval=%d time_const=%d",
365 (unsigned int) getpid(), (unsigned long)random(),
366 scan_interval
/1000, time_constant
/1000);
368 pad_kern_table(kern_db
, read_kern_table(kern_db
->ival
));
375 gettimeofday(&now
, NULL
);
376 tdiff
= T_DIFF(now
, snaptime
);
377 if (tdiff
>= scan_interval
) {
382 if (poll(&p
, 1, tdiff
+ scan_interval
) > 0
383 && (p
.revents
&POLLIN
)) {
384 int clnt
= accept(fd
, NULL
, NULL
);
391 } else if ((pid
= fork()) != 0) {
403 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
408 static int verify_forging(int fd
)
411 socklen_t olen
= sizeof(cred
);
413 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
416 if (cred
.uid
== getuid() || cred
.uid
== 0)
421 static void usage(void) __attribute__((noreturn
));
423 static void usage(void)
426 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
431 int main(int argc
, char *argv
[])
434 struct sockaddr_un sun
;
438 while ((ch
= getopt(argc
, argv
, "h?vVzrM:nasd:t:")) != EOF
) {
456 scan_interval
= 1000*atoi(optarg
);
459 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
460 time_constant
<= 0) {
461 fprintf(stderr
, "rtacct: invalid time constant divisor\n");
467 printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT
);
470 /* Some secret undocumented option, nobody
471 * is expected to ask about its sense. See?
473 sscanf(optarg
, "%lx", &magic_number
);
489 if (rtnl_rtrealm_a2n(&realm
, argv
[0])) {
490 fprintf(stderr
, "Warning: realm \"%s\" does not exist.\n", argv
[0]);
493 rmap
[realm
>>5] |= (1<<(realm
&0x1f));
497 memset(rmap
, ~0, sizeof(rmap
));
498 /* Always suppress zeros. */
502 sun
.sun_family
= AF_UNIX
;
504 sprintf(sun
.sun_path
+1, "rtacct%d", getuid());
506 if (scan_interval
> 0) {
507 if (time_constant
== 0)
509 time_constant
*= 1000;
510 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
511 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
512 perror("rtacct: socket");
515 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
516 perror("rtacct: bind");
519 if (listen(fd
, 5) < 0) {
520 perror("rtacct: listen");
524 perror("rtacct: daemon");
527 signal(SIGPIPE
, SIG_IGN
);
528 signal(SIGCHLD
, sigchild
);
533 if (getenv("RTACCT_HISTORY"))
534 snprintf(hist_name
, sizeof(hist_name
), "%s", getenv("RTACCT_HISTORY"));
536 sprintf(hist_name
, "/tmp/.rtacct.u%d", getuid());
541 if (!ignore_history
|| !no_update
) {
544 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
546 perror("rtacct: open history file");
549 if (flock(fd
, LOCK_EX
)) {
550 perror("rtacct: flock history file");
553 if (fstat(fd
, &stb
) != 0) {
554 perror("rtacct: fstat history file");
557 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
558 fprintf(stderr
, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
561 if (stb
.st_size
!= sizeof(*hist_db
))
562 if (write(fd
, kern_db
, sizeof(*hist_db
)) < 0) {
563 perror("rtacct: write history file");
567 hist_db
= mmap(NULL
, sizeof(*hist_db
),
568 PROT_READ
|PROT_WRITE
,
569 no_update
? MAP_PRIVATE
: MAP_SHARED
,
572 if ((unsigned long)hist_db
== ~0UL) {
577 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
);