4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <sys/sysinfo.h>
13 #include <sys/ioctl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <sys/types.h>
22 #include <sys/statfs.h>
24 #include <net/route.h>
28 //#include <sys/mount.h>
35 // to javascript-safe string
36 char *js_string(const char *s
)
42 if ((buffer
= malloc((strlen(s
) * 4) + 1)) != NULL
) {
44 while ((c
= *s
++) != 0) {
45 if ((c
== '"') || (c
== '\'') || (c
== '\\') || (!isprint(c
))) {
46 b
+= sprintf(b
, "\\x%02x", c
);
57 // to html-safe string
58 char *html_string(const char *s
)
64 if ((buffer
= malloc((strlen(s
) * 6) + 1)) != NULL
) {
66 while ((c
= *s
++) != 0) {
67 if ((c
== '&') || (c
== '<') || (c
== '>') || (c
== '"') || (c
== '\'') || (!isprint(c
))) {
68 b
+= sprintf(b
, "&#%d;", c
);
80 char *unix_string(const char *s
)
86 if ((buffer
= malloc(strlen(s
) + 1)) != NULL
) {
88 while ((c
= *s
++) != 0)
89 if (c
!= '\r') *b
++ = c
;
96 char *reltime(char *buf
, time_t t
)
104 sprintf(buf
, "%d day%s, %02d:%02d:%02d", days
, ((days
==1) ? "" : "s"), ((m
/ 60) % 24), (m
% 60), (int)(t
% 60));
108 int get_client_info(char *mac
, char *ifname
)
116 IP address HW type Flags HW address Mask Device
117 192.168.0.1 0x1 0x2 00:01:02:03:04:05 * vlan1
118 192.168.1.5 0x1 0x2 00:05:06:07:08:09 * br0
121 if ((f
= fopen("/proc/net/arp", "r")) != NULL
) {
122 while (fgets(s
, sizeof(s
), f
)) {
123 if (sscanf(s
, "%15s %*s %*s %17s %*s %16s", ips
, mac
, ifname
) == 3) {
124 if (inet_addr(ips
) == clientsai
.sin_addr
.s_addr
) {
139 // <% lanip(mode); %>
141 // 1 return first 3 octets (192.168.1)
142 // 2 return last octet (1)
143 // else return full (192.168.1.1)
145 void asp_lanip(int argc
, char **argv
)
151 mode
= argc
? *argv
[0] : 0;
153 if ((nv
= nvram_get("lan_ipaddr")) != NULL
) {
155 if ((p
= strrchr(s
, '.')) != NULL
) {
157 web_puts((mode
== '1') ? s
: (mode
== '2') ? (p
+ 1) : nv
);
162 void asp_lipp(int argc
, char **argv
)
168 // <% psup(process); %>
169 // returns 1 if process is running
171 void asp_psup(int argc
, char **argv
)
173 if (argc
== 1) web_printf("%d", pidof(argv
[0]) > 0);
178 total: used: free: shared: buffers: cached:
179 Mem: 14872576 12877824 1994752 0 1236992 4837376
201 unsigned long shared
;
202 unsigned long buffers
;
203 unsigned long cached
;
204 unsigned long swaptotal
;
205 unsigned long swapfree
;
206 unsigned long maxfreeram
;
209 static int get_memory(meminfo_t
*m
)
215 if ((f
= fopen("/proc/meminfo", "r")) != NULL
) {
216 while (fgets(s
, sizeof(s
), f
)) {
217 if (strncmp(s
, "Mem:", 4) == 0) {
218 if (sscanf(s
+ 6, "%ld %*d %ld %ld %ld %ld", &m
->total
, &m
->free
, &m
->shared
, &m
->buffers
, &m
->cached
) == 5)
221 else if (strncmp(s
, "SwapTotal:", 10) == 0) {
222 m
->swaptotal
= strtoul(s
+ 12, NULL
, 10) * 1024;
225 else if (strncmp(s
, "SwapFree:", 9) == 0) {
226 m
->swapfree
= strtoul(s
+ 11, NULL
, 10) * 1024;
234 memset(m
, 0, sizeof(*m
));
237 m
->maxfreeram
= m
->free
;
238 if (nvram_match("t_cafree", "1")) m
->maxfreeram
+= m
->cached
;
242 void asp_sysinfo(int argc
, char **argv
)
248 web_puts("\nsysinfo = {\n");
253 "\tuptime_s: '%s',\n"
254 "\tloads: [%ld, %ld, %ld],\n"
258 "\tbufferram: %ld,\n"
260 "\ttotalswap: %ld,\n"
262 "\ttotalfreeram: %ld,\n"
265 reltime(s
, si
.uptime
),
266 si
.loads
[0], si
.loads
[1], si
.loads
[2],
268 mem
.shared
, mem
.buffers
, mem
.cached
,
269 mem
.swaptotal
, mem
.swapfree
,
275 void asp_activeroutes(int argc
, char **argv
)
281 unsigned long gateway
;
291 web_puts("\nactiveroutes = [");
293 if ((f
= fopen("/proc/net/route", "r")) != NULL
) {
294 while (fgets(s
, sizeof(s
), f
)) {
295 if (sscanf(s
, "%16s%lx%lx%lx%*s%*s%u%lx", dev
, &dest
, &gateway
, &flags
, &metric
, &mask
) != 6) continue;
296 if ((flags
& RTF_UP
) == 0) continue;
299 strcpy(s_dest
, inet_ntoa(ia
));
302 strcpy(s_dest
, "default");
306 strcpy(s_gateway
, inet_ntoa(ia
));
309 strcpy(s_gateway
, "*");
312 strcpy(s_mask
, inet_ntoa(ia
));
313 web_printf("%s['%s','%s','%s','%s',%u]", n
? "," : "", dev
, s_dest
, s_gateway
, s_mask
, metric
);
321 void asp_cgi_get(int argc
, char **argv
)
326 for (i
= 0; i
< argc
; ++i
) {
327 v
= webcgi_get(argv
[i
]);
332 void asp_time(int argc
, char **argv
)
339 web_puts("Not Available");
342 strftime(s
, sizeof(s
), "%a, %d %b %Y %H:%M:%S %z", localtime(&t
));
347 void asp_wanup(int argc
, char **argv
)
349 web_puts(check_wanup() ? "1" : "0");
352 void asp_wanstatus(int argc
, char **argv
)
356 if ((using_dhcpc()) && (f_exists("/var/lib/misc/dhcpc.renewing"))) {
359 else if (check_wanup()) {
362 else if (f_exists("/var/lib/misc/wan.connecting")) {
371 void asp_link_uptime(int argc
, char **argv
)
381 if (f_read("/var/lib/misc/wantime", &uptime
, sizeof(uptime
)) == sizeof(uptime
)) {
382 reltime(buf
, si
.uptime
- uptime
);
388 void asp_rrule(int argc
, char **argv
)
393 i
= nvram_get_int("rruleN");
394 sprintf(s
, "rrule%d", i
);
395 web_puts("\nrrule = '");
396 web_putj(nvram_safe_get(s
));
397 web_printf("';\nrruleN = %d;\n", i
);
400 void asp_compmac(int argc
, char **argv
)
405 if (get_client_info(mac
, ifname
)) {
410 void asp_ident(int argc
, char **argv
)
412 web_puth(nvram_safe_get("router_name"));
415 void asp_statfs(int argc
, char **argv
)
419 if (argc
!= 2) return;
421 // used for /cifs/, /jffs/... if it returns squashfs type, assume it's not mounted
422 if ((statfs(argv
[0], &sf
) != 0) || (sf
.f_type
== 0x73717368))
423 memset(&sf
, 0, sizeof(sf
));
431 ((uint64_t)sf
.f_bsize
* sf
.f_blocks
),
432 ((uint64_t)sf
.f_bsize
* sf
.f_bfree
));
435 void asp_notice(int argc
, char **argv
)
440 if (argc
!= 1) return;
441 snprintf(s
, sizeof(s
), "/var/notice/%s", argv
[0]);
442 if (f_read_string(s
, buf
, sizeof(buf
)) <= 0) return;
446 void wo_wakeup(char *url
)
452 if ((mac
= webcgi_get("mac")) != NULL
) {
453 end
= mac
+ strlen(mac
);
455 while ((*mac
== ' ') || (*mac
== '\t') || (*mac
== '\r') || (*mac
== '\n')) ++mac
;
456 if (*mac
== 0) break;
459 while ((*p
!= 0) && (*p
!= ' ') && (*p
!= '\r') && (*p
!= '\n')) ++p
;
462 eval("ether-wake", "-i", nvram_safe_get("lan_ifname"), mac
);
469 void asp_dns(int argc
, char **argv
)
473 const dns_list_t
*dns
;
475 dns
= get_dns(); // static buffer
476 strcpy(s
, "\ndns = [");
477 for (i
= 0 ; i
< dns
->count
; ++i
) {
478 sprintf(s
+ strlen(s
), "%s'%s:%u'", i
? "," : "", inet_ntoa(dns
->dns
[i
].addr
), dns
->dns
[i
].port
);
484 void wo_resolve(char *url
)
494 web_puts("\nresolve_data = [\n");
495 if ((p
= webcgi_get("ip")) != NULL
) {
496 while ((ip
= strsep(&p
, ",")) != NULL
) {
497 ia
.s_addr
= inet_addr(ip
);
498 he
= gethostbyaddr(&ia
, sizeof(ia
), AF_INET
);
499 js
= js_string(he
? he
->h_name
: "");
500 web_printf("%c['%s','%s']", comma
, inet_ntoa(ia
), js
);
513 #define PROC_SCSI_ROOT "/proc/scsi"
514 #define USB_STORAGE "usb-storage"
516 /* this value should not match any of the existing EFH_ values in shared.h */
517 #define EFH_PRINT 0x00000080 /* output partition list to the web response */
519 int is_partition_mounted(char *dev_name
, int host_num
, int disc_num
, int part_num
, uint flags
)
526 unsigned long psize
= 0;
528 if (!find_label_or_uuid(dev_name
, the_label
, NULL
)) {
529 sprintf(the_label
, "disc%d_%d", disc_num
, part_num
);
532 if (flags
& EFH_PRINT
) {
533 if (flags
& EFH_1ST_DISC
) {
534 // [disc_no, [partitions array]],...
535 web_printf("]],[%d,[", disc_num
);
537 // [partition_name, is_mounted, mount_point, type, opts, size],...
538 web_printf("%s['%s',", (flags
& EFH_1ST_DISC
) ? "" : ",", the_label
);
541 if ((mnt
= findmntents(dev_name
, 0, 0, 0))) {
543 if (flags
& EFH_PRINT
) {
544 if (statfs(mnt
->mnt_dir
, &s
) == 0) {
545 psize
= (s
.f_blocks
* (unsigned long long) s
.f_bsize
+ 1024*1024/2) / (1024*1024);
547 web_printf("1,'%s','%s','%s','%ld']",
548 mnt
->mnt_dir
, mnt
->mnt_type
, mnt
->mnt_opts
, psize
);
551 else if ((mnt
= findmntents(dev_name
, 1, 0, 0))) {
553 if (flags
& EFH_PRINT
) {
554 web_printf("2,'','%s','','%ld']",
555 "swap", (atoi(mnt
->mnt_type
) + 1023) / 1024);
559 if (flags
& EFH_PRINT
) {
560 type
= detect_fs_type(dev_name
);
561 web_printf("0,'','%s','','0']", type
? type
: "");
568 int is_host_mounted(int host_no
, int print_parts
)
570 if (print_parts
) web_puts("[-1,[");
572 int fd
= nvram_get_int("usb_nolock") ? -1 : file_lock("usb");
573 int mounted
= exec_for_host(
576 print_parts
? EFH_PRINT
: 0,
577 is_partition_mounted
);
580 if (print_parts
) web_puts("]]");
586 * The disc # doesn't correspond to the host#, since there may be more than
587 * one partition on a disk.
588 * Nor does either correspond to the scsi host number.
589 * And if the user plugs or unplugs a usb storage device after bringing up the
590 * NAS:USB support page, the numbers won't match anymore, since "part#"s
591 * may be added or deleted to the /dev/discs* or /dev/scsi**.
593 * But since we only need to support the devices list and mount/unmount
594 * functionality on the host level, the host# shoudl work ok. Just make sure
595 * to always pass and use the _host#_, and not the disc#.
597 void asp_usbdevices(int argc
, char **argv
)
599 DIR *scsi_dir
=NULL
, *usb_dir
=NULL
;
600 struct dirent
*dp
, *scsi_dirent
;
602 int i
= 0, attached
, mounted
;
605 char *tmp
=NULL
, g_usb_vendor
[30], g_usb_product
[30], g_usb_serial
[30];
607 web_puts("\nusbdev = [");
609 if (!nvram_match("usb_enable", "1")) {
614 /* find all attached USB storage devices */
615 scsi_dir
= opendir(PROC_SCSI_ROOT
);
616 while (scsi_dir
&& (scsi_dirent
= readdir(scsi_dir
)))
618 if (!strncmp(USB_STORAGE
, scsi_dirent
->d_name
, strlen(USB_STORAGE
)))
620 sprintf(line
, "%s/%s", PROC_SCSI_ROOT
, scsi_dirent
->d_name
);
621 usb_dir
= opendir(line
);
622 while (usb_dir
&& (dp
= readdir(usb_dir
)))
624 if (!strcmp(dp
->d_name
, "..") || !strcmp(dp
->d_name
, "."))
626 sprintf(line
, "%s/%s/%s", PROC_SCSI_ROOT
, scsi_dirent
->d_name
, dp
->d_name
);
628 fp
= fopen(line
, "r");
632 g_usb_product
[0] = 0;
636 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
637 if (strstr(line
, "Attached: Yes")) {
640 else if (strstr(line
, "Vendor")) {
641 tmp
= strtok(line
, " ");
642 tmp
= strtok(NULL
, "\n");
643 strncpy(g_usb_vendor
, tmp
, sizeof(g_usb_vendor
) - 1);
646 else if (strstr(line
, "Product")) {
647 tmp
= strtok(line
, " ");
648 tmp
= strtok(NULL
, "\n");
649 strncpy(g_usb_product
, tmp
, sizeof(g_usb_product
) - 1);
652 else if (strstr(line
, "Serial Number")) {
653 tmp
= strtok(line
, " ");
654 tmp
= strtok(NULL
, " ");
655 tmp
= strtok(NULL
, "\n");
656 strncpy(g_usb_serial
, tmp
, sizeof(g_usb_serial
) - 1);
662 /* Host no. assigned by scsi driver for this UFD */
663 host_no
= atoi(dp
->d_name
);
664 web_printf("%s['Storage','%d','%s','%s','%s', [", i
? "," : "",
665 host_no
, g_usb_vendor
, g_usb_product
, g_usb_serial
);
666 mounted
= is_host_mounted(host_no
, 1);
667 web_printf("], %d]", mounted
);
680 /* now look for printers */
681 usb_dir
= opendir("/proc/usblp");
682 while (usb_dir
&& (dp
= readdir(usb_dir
)))
684 if (!strcmp(dp
->d_name
, "..") || !strcmp(dp
->d_name
, "."))
688 sprintf(line, "/dev/usb/%s", dp->d_name);
692 sprintf(line
, "/proc/usblp/%s", dp
->d_name
);
693 if ((fp
= fopen(line
, "r"))) {
695 g_usb_product
[0] = 0;
698 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
699 if (strstr(line
, "Manufacturer")) {
700 tmp
= strtok(line
, "=");
701 tmp
= strtok(NULL
, "\n");
702 strncpy(g_usb_vendor
, tmp
, sizeof(g_usb_vendor
) - 1);
705 else if (strstr(line
, "Model")) {
706 tmp
= strtok(line
, "=");
707 tmp
= strtok(NULL
, "\n");
708 strncpy(g_usb_product
, tmp
, sizeof(g_usb_product
) - 1);
712 if ((strlen(g_usb_product
) > 0) || (strlen(g_usb_vendor
) > 0)) {
713 web_printf("%s['Printer','%s','%s','%s','']", i
? "," : "",
714 dp
->d_name
, g_usb_vendor
, g_usb_product
);
727 void wo_usbcommand(char *url
)
732 web_puts("\nusb = [\n");
733 if ((p
= webcgi_get("remove")) != NULL
) {
736 else if ((p
= webcgi_get("mount")) != NULL
) {
740 add_remove_usbhost(p
, add
);
741 web_printf("%d", is_host_mounted(atoi(p
), 0));
746 #endif // TCONFIG_USB