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>
22 #include <sys/socket.h>
42 unsigned long magic_number
;
45 static int generic_proc_open(const char *env
, const char *name
)
48 char *p
= getenv(env
);
51 p
= getenv("PROC_ROOT") ? : "/proc";
52 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
55 return open(p
, O_RDONLY
);
58 static int net_rtacct_open(void)
60 return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
63 static __u32 rmap
[256/4];
68 unsigned long long val
[256*4];
73 static struct rtacct_data kern_db_static
;
75 static struct rtacct_data
*kern_db
= &kern_db_static
;
76 static struct rtacct_data
*hist_db
;
78 static void nread(int fd
, char *buf
, int tot
)
83 int n
= read(fd
, buf
+count
, tot
-count
);
96 static __u32
*read_kern_table(__u32
*tbl
)
98 static __u32
*tbl_ptr
;
105 fd
= open("/dev/mem", O_RDONLY
);
107 perror("magic open");
110 tbl_ptr
= mmap(NULL
, 4096,
114 if ((unsigned long)tbl_ptr
== ~0UL) {
115 perror("magic mmap");
122 fd
= net_rtacct_open();
124 nread(fd
, (char *)tbl
, 256*16);
127 memset(tbl
, 0, 256*16);
132 static void format_rate(FILE *fp
, double rate
)
136 if (rate
> 1024*1024) {
137 sprintf(temp
, "%uM", (unsigned int)rint(rate
/(1024*1024)));
138 fprintf(fp
, " %-10s", temp
);
139 } else if (rate
> 1024) {
140 sprintf(temp
, "%uK", (unsigned int)rint(rate
/1024));
141 fprintf(fp
, " %-10s", temp
);
143 fprintf(fp
, " %-10u", (unsigned int)rate
);
146 static void format_count(FILE *fp
, unsigned long long val
)
148 if (val
> 1024*1024*1024)
149 fprintf(fp
, " %10lluM", val
/(1024*1024));
150 else if (val
> 1024*1024)
151 fprintf(fp
, " %10lluK", val
/1024);
153 fprintf(fp
, " %10llu", val
);
156 static void dump_abs_db(FILE *fp
)
162 fprintf(fp
, "#%s\n", kern_db
->signature
);
167 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
172 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
176 for (realm
= 0; realm
< 256; realm
++) {
178 unsigned long long *val
;
181 if (!(rmap
[realm
>>5] & (1<<(realm
&0x1f))))
184 val
= &kern_db
->val
[realm
*4];
185 rate
= &kern_db
->rate
[realm
*4];
188 !val
[0] && !rate
[0] &&
189 !val
[1] && !rate
[1] &&
190 !val
[2] && !rate
[2] &&
195 memcpy(&hist_db
->val
[realm
*4], val
, sizeof(*val
)*4);
201 fprintf(fp
, "%-10s", rtnl_rtrealm_n2a(realm
, b1
, sizeof(b1
)));
202 for (i
= 0; i
< 4; i
++)
203 format_count(fp
, val
[i
]);
204 fprintf(fp
, "\n%-10s", "");
205 for (i
= 0; i
< 4; i
++)
206 format_rate(fp
, rate
[i
]);
212 static void dump_incr_db(FILE *fp
)
218 fprintf(fp
, "#%s\n", kern_db
->signature
);
223 , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
228 , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
231 for (realm
= 0; realm
< 256; realm
++) {
234 unsigned long long *val
;
236 unsigned long long rval
[4];
238 if (!(rmap
[realm
>>5] & (1<<(realm
&0x1f))))
241 val
= &kern_db
->val
[realm
*4];
242 rate
= &kern_db
->rate
[realm
*4];
244 for (k
= 0; k
< 4; k
++) {
246 if (rval
[k
] < hist_db
->val
[realm
*4+k
])
249 rval
[k
] -= hist_db
->val
[realm
*4+k
];
252 for (k
= 0; k
< 4; k
++)
256 memcpy(&hist_db
->val
[realm
*4], val
, sizeof(*val
)*4);
263 !rval
[0] && !rate
[0] &&
264 !rval
[1] && !rate
[1] &&
265 !rval
[2] && !rate
[2] &&
266 !rval
[3] && !rate
[3])
270 fprintf(fp
, "%-10s", rtnl_rtrealm_n2a(realm
, b1
, sizeof(b1
)));
271 for (i
= 0; i
< 4; i
++)
272 format_count(fp
, rval
[i
]);
273 fprintf(fp
, "\n%-10s", "");
274 for (i
= 0; i
< 4; i
++)
275 format_rate(fp
, rate
[i
]);
283 static void sigchild(int signo
)
287 /* Server side only: read kernel data, update tables, calculate rates. */
289 static void update_db(int interval
)
295 ival
= read_kern_table(_ival
);
297 for (i
= 0; i
< 256*4; i
++) {
299 __u32 incr
= ival
[i
] - kern_db
->ival
[i
];
301 if (ival
[i
] == 0 && incr
== 0 &&
302 kern_db
->val
[i
] == 0 && kern_db
->rate
[i
] == 0)
305 kern_db
->val
[i
] += incr
;
306 kern_db
->ival
[i
] = ival
[i
];
307 sample
= (double)(incr
*1000)/interval
;
308 if (interval
>= scan_interval
) {
309 kern_db
->rate
[i
] += W
*(sample
-kern_db
->rate
[i
]);
310 } else if (interval
>= 1000) {
311 if (interval
>= time_constant
) {
312 kern_db
->rate
[i
] = sample
;
314 double w
= W
*(double)interval
/scan_interval
;
316 kern_db
->rate
[i
] += w
*(sample
-kern_db
->rate
[i
]);
322 static void send_db(int fd
)
326 while (tot
< sizeof(*kern_db
)) {
327 int n
= write(fd
, ((char *)kern_db
) + tot
, sizeof(*kern_db
)-tot
);
340 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
343 static void pad_kern_table(struct rtacct_data
*dat
, __u32
*ival
)
347 memset(dat
->rate
, 0, sizeof(dat
->rate
));
348 if (dat
->ival
!= ival
)
349 memcpy(dat
->ival
, ival
, sizeof(dat
->ival
));
350 for (i
= 0; i
< 256*4; i
++)
351 dat
->val
[i
] = ival
[i
];
354 static void server_loop(int fd
)
356 struct timeval snaptime
= { 0 };
360 p
.events
= p
.revents
= POLLIN
;
362 sprintf(kern_db
->signature
,
363 "%u.%lu sampling_interval=%d time_const=%d",
364 (unsigned int) getpid(), (unsigned long)random(),
365 scan_interval
/1000, time_constant
/1000);
367 pad_kern_table(kern_db
, read_kern_table(kern_db
->ival
));
374 gettimeofday(&now
, NULL
);
375 tdiff
= T_DIFF(now
, snaptime
);
376 if (tdiff
>= scan_interval
) {
381 if (poll(&p
, 1, tdiff
+ scan_interval
) > 0
382 && (p
.revents
&POLLIN
)) {
383 int clnt
= accept(fd
, NULL
, NULL
);
390 } else if ((pid
= fork()) != 0) {
402 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
407 static int verify_forging(int fd
)
410 socklen_t olen
= sizeof(cred
);
412 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
415 if (cred
.uid
== getuid() || cred
.uid
== 0)
420 static void usage(void) __attribute__((noreturn
));
422 static void usage(void)
425 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
430 int main(int argc
, char *argv
[])
433 struct sockaddr_un sun
;
437 while ((ch
= getopt(argc
, argv
, "h?vVzrM:nasd:t:")) != EOF
) {
455 scan_interval
= 1000*atoi(optarg
);
458 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
459 time_constant
<= 0) {
460 fprintf(stderr
, "rtacct: invalid time constant divisor\n");
466 printf("rtacct utility, iproute2-%s\n", version
);
469 /* Some secret undocumented option, nobody
470 * is expected to ask about its sense. See?
472 sscanf(optarg
, "%lx", &magic_number
);
488 if (rtnl_rtrealm_a2n(&realm
, argv
[0])) {
489 fprintf(stderr
, "Warning: realm \"%s\" does not exist.\n", argv
[0]);
492 rmap
[realm
>>5] |= (1<<(realm
&0x1f));
496 memset(rmap
, ~0, sizeof(rmap
));
497 /* Always suppress zeros. */
501 sun
.sun_family
= AF_UNIX
;
503 sprintf(sun
.sun_path
+1, "rtacct%d", getuid());
505 if (scan_interval
> 0) {
506 if (time_constant
== 0)
508 time_constant
*= 1000;
509 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
510 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
511 perror("rtacct: socket");
514 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
515 perror("rtacct: bind");
518 if (listen(fd
, 5) < 0) {
519 perror("rtacct: listen");
523 perror("rtacct: daemon");
526 signal(SIGPIPE
, SIG_IGN
);
527 signal(SIGCHLD
, sigchild
);
532 if (getenv("RTACCT_HISTORY"))
533 snprintf(hist_name
, sizeof(hist_name
), "%s", getenv("RTACCT_HISTORY"));
535 sprintf(hist_name
, "/tmp/.rtacct.u%d", getuid());
540 if (!ignore_history
|| !no_update
) {
543 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
545 perror("rtacct: open history file");
548 if (flock(fd
, LOCK_EX
)) {
549 perror("rtacct: flock history file");
552 if (fstat(fd
, &stb
) != 0) {
553 perror("rtacct: fstat history file");
556 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
557 fprintf(stderr
, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
560 if (stb
.st_size
!= sizeof(*hist_db
))
561 if (write(fd
, kern_db
, sizeof(*hist_db
)) < 0) {
562 perror("rtacct: write history file");
566 hist_db
= mmap(NULL
, sizeof(*hist_db
),
567 PROT_READ
|PROT_WRITE
,
568 no_update
? MAP_PRIVATE
: MAP_SHARED
,
571 if ((unsigned long)hist_db
== ~0UL) {
576 if (!ignore_history
) {
580 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
581 if (fscanf(tfp
, "%ld", &uptime
) != 1)
586 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
587 fprintf(stderr
, "rtacct: history is aged out, resetting\n");
588 memset(hist_db
, 0, sizeof(*hist_db
));
595 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
596 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
597 || (strcpy(sun
.sun_path
+1, "rtacct0"),
598 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
599 && verify_forging(fd
) == 0) {
600 nread(fd
, (char *)kern_db
, sizeof(*kern_db
));
601 if (hist_db
&& hist_db
->signature
[0] &&
602 strcmp(kern_db
->signature
, hist_db
->signature
)) {
603 fprintf(stderr
, "rtacct: history is stale, ignoring it.\n");
611 if (hist_db
&& hist_db
->signature
[0] &&
612 strcmp(hist_db
->signature
, "kernel")) {
613 fprintf(stderr
, "rtacct: history is stale, ignoring it.\n");
617 pad_kern_table(kern_db
, read_kern_table(kern_db
->ival
));
618 strcpy(kern_db
->signature
, "kernel");
621 if (ignore_history
|| hist_db
== NULL
)
624 dump_incr_db(stdout
);