Tomato 1.26 beta(1766)
[tomato.git] / release / src / router / httpd / bwm.c
blobb7e1732cc3d731b7538c0890fc061946be1402c7
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/wait.h>
16 #include <typedefs.h>
19 static const char *hfn = "/var/lib/misc/rstats-history.gz";
21 void wo_bwmbackup(char *url)
23 struct stat st;
24 time_t t;
25 int i;
27 if (stat(hfn, &st) == 0) {
28 t = st.st_mtime;
29 sleep(1);
31 else {
32 t = 0;
34 killall("rstats", SIGHUP);
35 for (i = 10; i > 0; --i) {
36 if ((stat(hfn, &st) == 0) && (st.st_mtime != t)) break;
37 sleep(1);
39 if (i == 0) {
40 send_error(500, NULL, NULL);
41 return;
43 send_header(200, NULL, mime_binary, 0);
44 do_file((char *)hfn);
47 void wi_bwmrestore(char *url, int len, char *boundary)
49 char *buf;
50 const char *error;
51 int ok;
52 int n;
53 char tmp[64];
55 check_id(url);
57 tmp[0] = 0;
58 buf = NULL;
59 error = "Error reading file";
60 ok = 0;
62 if (!skip_header(&len)) {
63 goto ERROR;
66 if ((len < 64) || (len > 10240)) {
67 goto ERROR;
70 if ((buf = malloc(len)) == NULL) {
71 error = "Not enough memory";
72 goto ERROR;
75 n = web_read(buf, len);
76 len -= n;
78 sprintf(tmp, "%s.new", hfn);
79 if (f_write(tmp, buf, n, 0, 0600) != n) {
80 unlink(tmp);
81 error = "Error writing temporary file";
82 goto ERROR;
84 f_write("/var/tmp/rstats-load", NULL, 0, 0, 0600);
85 killall("rstats", SIGHUP);
86 sleep(1);
88 error = NULL;
89 rboot = 1; // used as "ok"
91 ERROR:
92 free(buf);
93 web_eat(len);
94 if (error != NULL) resmsg_set(error);
97 void wo_bwmrestore(char *url)
99 if (rboot) {
100 redirect("/bwm-daily.asp");
102 else {
103 parse_asp("error.asp");
107 void asp_netdev(int argc, char **argv)
109 FILE *f;
110 char buf[256];
111 unsigned long rx, tx;
112 char *p;
113 char *ifname;
114 char comma;
115 char *exclude;
117 exclude = nvram_safe_get("rstats_exclude");
118 web_puts("\n\nnetdev={");
119 if ((f = fopen("/proc/net/dev", "r")) != NULL) {
120 fgets(buf, sizeof(buf), f); // header
121 fgets(buf, sizeof(buf), f); // "
122 comma = ' ';
123 while (fgets(buf, sizeof(buf), f)) {
124 if ((p = strchr(buf, ':')) == NULL) continue;
125 *p = 0;
126 if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
127 else ++ifname;
128 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
129 if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue;
131 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
132 if (sscanf(p + 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx, &tx) != 2) continue;
133 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ifname, rx, tx);
134 comma = ',';
136 fclose(f);
138 web_puts("};\n");
141 void asp_bandwidth(int argc, char **argv)
143 char *name;
144 int sig;
146 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
147 if (strcmp(argv[0], "speed") == 0) {
148 sig = SIGUSR1;
149 name = "/var/spool/rstats-speed.js";
151 else {
152 sig = SIGUSR2;
153 name = "/var/spool/rstats-history.js";
155 unlink(name);
156 killall("rstats", sig);
157 f_wait_exists(name, 5);
158 do_file(name);
159 unlink(name);