• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
generic_proc_open(const char * env,const char * name)46 static int generic_proc_open(const char *env, const 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(p, O_RDONLY);
56 }
57 
net_rtacct_open(void)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 	char			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 
nread(int fd,char * buf,int tot)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 
read_kern_table(__u32 * tbl)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 
format_rate(FILE * fp,double rate)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 
format_count(FILE * fp,unsigned long long val)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 
dump_abs_db(FILE * fp)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 
dump_incr_db(FILE * fp)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 
sigchild(int signo)296 void sigchild(int signo)
297 {
298 }
299 
300 /* Server side only: read kernel data, update tables, calculate rates. */
301 
update_db(int interval)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 
send_db(int fd)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 
pad_kern_table(struct rtacct_data * dat,__u32 * ival)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 
server_loop(int fd)364 void server_loop(int fd)
365 {
366 	struct timeval snaptime = { 0 };
367 	struct pollfd p;
368 	p.fd = fd;
369 	p.events = p.revents = POLLIN;
370 
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);
375 
376 	pad_kern_table(kern_db, read_kern_table(kern_db->ival));
377 
378 	for (;;) {
379 		int status;
380 		int tdiff;
381 		struct timeval now;
382 		gettimeofday(&now, NULL);
383 		tdiff = T_DIFF(now, snaptime);
384 		if (tdiff >= scan_interval) {
385 			update_db(tdiff);
386 			snaptime = now;
387 			tdiff = 0;
388 		}
389 		if (poll(&p, 1, tdiff + scan_interval) > 0
390 		    && (p.revents&POLLIN)) {
391 			int clnt = accept(fd, NULL, NULL);
392 			if (clnt >= 0) {
393 				pid_t pid;
394 				if (children >= 5) {
395 					close(clnt);
396 				} else if ((pid = fork()) != 0) {
397 					if (pid>0)
398 						children++;
399 					close(clnt);
400 				} else {
401 					if (tdiff > 0)
402 						update_db(tdiff);
403 					send_db(clnt);
404 					exit(0);
405 				}
406 			}
407 		}
408 		while (children && waitpid(-1, &status, WNOHANG) > 0)
409 			children--;
410 	}
411 }
412 
verify_forging(int fd)413 int verify_forging(int fd)
414 {
415 	struct ucred cred;
416 	socklen_t olen = sizeof(cred);
417 
418 	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
419 	    olen < sizeof(cred))
420 		return -1;
421 	if (cred.uid == getuid() || cred.uid == 0)
422 		return 0;
423 	return -1;
424 }
425 
426 static void usage(void) __attribute__((noreturn));
427 
usage(void)428 static void usage(void)
429 {
430 	fprintf(stderr,
431 "Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
432 		);
433 	exit(-1);
434 }
435 
main(int argc,char * argv[])436 int main(int argc, char *argv[])
437 {
438 	char hist_name[128];
439 	struct sockaddr_un sun;
440 	int ch;
441 	int fd;
442 
443 	while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
444 		switch(ch) {
445 		case 'z':
446 			dump_zeros = 1;
447 			break;
448 		case 'r':
449 			reset_history = 1;
450 			break;
451 		case 'a':
452 			ignore_history = 1;
453 			break;
454 		case 's':
455 			no_update = 1;
456 			break;
457 		case 'n':
458 			no_output = 1;
459 			break;
460 		case 'd':
461 			scan_interval = 1000*atoi(optarg);
462 			break;
463 		case 't':
464 			if (sscanf(optarg, "%d", &time_constant) != 1 ||
465 			    time_constant <= 0) {
466 				fprintf(stderr, "rtacct: invalid time constant divisor\n");
467 				exit(-1);
468 			}
469 			break;
470 		case 'v':
471 		case 'V':
472 			printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT);
473 			exit(0);
474 		case 'M':
475 			/* Some secret undocumented option, nobody
476 			 * is expected to ask about its sense. See?
477 			 */
478 			sscanf(optarg, "%lx", &magic_number);
479 			break;
480 		case 'h':
481 		case '?':
482 		default:
483 			usage();
484 		}
485 	}
486 
487 	argc -= optind;
488 	argv += optind;
489 
490 	if (argc) {
491 		while (argc > 0) {
492 			__u32 realm;
493 			if (rtnl_rtrealm_a2n(&realm, argv[0])) {
494 				fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
495 				exit(-1);
496 			}
497 			rmap[realm>>5] |= (1<<(realm&0x1f));
498 			argc--; argv++;
499 		}
500 	} else {
501 		memset(rmap, ~0, sizeof(rmap));
502 		/* Always suppress zeros. */
503 		dump_zeros = 0;
504 	}
505 
506 	sun.sun_family = AF_UNIX;
507 	sun.sun_path[0] = 0;
508 	sprintf(sun.sun_path+1, "rtacct%d", getuid());
509 
510 	if (scan_interval > 0) {
511 		if (time_constant == 0)
512 			time_constant = 60;
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");
517 			exit(-1);
518 		}
519 		if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
520 			perror("rtacct: bind");
521 			exit(-1);
522 		}
523 		if (listen(fd, 5) < 0) {
524 			perror("rtacct: listen");
525 			exit(-1);
526 		}
527 		if (daemon(0, 0)) {
528 			perror("rtacct: daemon");
529 			exit(-1);
530 		}
531 		signal(SIGPIPE, SIG_IGN);
532 		signal(SIGCHLD, sigchild);
533 		server_loop(fd);
534 		exit(0);
535 	}
536 
537 	if (getenv("RTACCT_HISTORY"))
538 		snprintf(hist_name, sizeof(hist_name), "%s", getenv("RTACCT_HISTORY"));
539 	else
540 		sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
541 
542 	if (reset_history)
543 		unlink(hist_name);
544 
545 	if (!ignore_history || !no_update) {
546 		struct stat stb;
547 
548 		fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
549 		if (fd < 0) {
550 			perror("rtacct: open history file");
551 			exit(-1);
552 		}
553 		if (flock(fd, LOCK_EX)) {
554 			perror("rtacct: flock history file");
555 			exit(-1);
556 		}
557 		if (fstat(fd, &stb) != 0) {
558 			perror("rtacct: fstat history file");
559 			exit(-1);
560 		}
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");
563 			exit(-1);
564 		}
565 		if (stb.st_size != sizeof(*hist_db))
566 			if (write(fd, kern_db, sizeof(*hist_db)) < 0) {
567 				perror("rtacct: write history file");
568 				exit(-1);
569 			}
570 
571 		hist_db = mmap(NULL, sizeof(*hist_db),
572 			       PROT_READ|PROT_WRITE,
573 			       no_update ? MAP_PRIVATE : MAP_SHARED,
574 			       fd, 0);
575 
576 		if ((unsigned long)hist_db == ~0UL) {
577 			perror("mmap");
578 			exit(-1);
579 		}
580 
581 		if (!ignore_history) {
582 			FILE *tfp;
583 			long uptime = -1;
584 			if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
585 				if (fscanf(tfp, "%ld", &uptime) != 1)
586 					uptime = -1;
587 				fclose(tfp);
588 			}
589 
590 			if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
591 				fprintf(stderr, "rtacct: history is aged out, resetting\n");
592 				memset(hist_db, 0, sizeof(*hist_db));
593 			}
594 		}
595 
596 		close(fd);
597 	}
598 
599 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
600 	    (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
601 	     || (strcpy(sun.sun_path+1, "rtacct0"),
602 		 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
603 	    && verify_forging(fd) == 0) {
604 		nread(fd, (char*)kern_db, sizeof(*kern_db));
605 		if (hist_db && hist_db->signature[0] &&
606 		    strcmp(kern_db->signature, hist_db->signature)) {
607 			fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
608 			hist_db = NULL;
609 		}
610 		close(fd);
611 	} else {
612 		if (fd >= 0)
613 			close(fd);
614 
615 		if (hist_db && hist_db->signature[0] &&
616 		    strcmp(hist_db->signature, "kernel")) {
617 			fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
618 			hist_db = NULL;
619 		}
620 
621 		pad_kern_table(kern_db, read_kern_table(kern_db->ival));
622 		strcpy(kern_db->signature, "kernel");
623 	}
624 
625 	if (ignore_history || hist_db == NULL)
626 		dump_abs_db(stdout);
627 	else
628 		dump_incr_db(stdout);
629 
630 	exit(0);
631 }
632