revised QOS/BWLimiter
[tomato.git] / release / src / router / rc / services.c
blob399dad4597f07e300d3fc3831baa93f70cb23dd4
1 /*
3 Copyright 2003, CyberTAN Inc. All Rights Reserved
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
6 the contents of this file may not be disclosed to third parties,
7 copied or duplicated in any form without the prior written
8 permission of CyberTAN Inc.
10 This software should be used as a reference only, and it not
11 intended for production use!
13 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
14 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. CYBERTAN
15 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
21 Copyright 2005, Broadcom Corporation
22 All Rights Reserved.
24 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
25 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
26 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
27 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
32 Modified for Tomato Firmware
33 Portions, Copyright (C) 2006-2009 Jonathan Zarate
36 #include "rc.h"
38 #include <arpa/inet.h>
39 #include <time.h>
40 #include <sys/time.h>
41 #include <errno.h>
43 // !!TB
44 #include <sys/mount.h>
45 #include <mntent.h>
46 #include <dirent.h>
48 // Pop an alarm to recheck pids in 500 msec.
49 static const struct itimerval pop_tv = { {0,0}, {0, 500 * 1000} };
51 // Pop an alarm to reap zombies.
52 static const struct itimerval zombie_tv = { {0,0}, {307, 0} };
54 // -----------------------------------------------------------------------------
56 static const char dmhosts[] = "/etc/dnsmasq/hosts";
57 static const char dmdhcp[] = "/etc/dnsmasq/dhcp";
58 static const char dmresolv[] = "/etc/resolv.dnsmasq";
60 static pid_t pid_dnsmasq = -1;
62 static int is_wet(int idx, int unit, int subunit, void *param)
64 return nvram_match(wl_nvname("mode", unit, subunit), "wet");
67 void start_dnsmasq()
69 FILE *f;
70 const char *nv;
71 char buf[512];
72 char lan[24];
73 const char *router_ip;
74 char sdhcp_lease[32];
75 char *e;
76 int n;
77 char *mac, *ip, *name;
78 char *p;
79 int ipn;
80 char ipbuf[32];
81 FILE *hf, *df;
82 int dhcp_start;
83 int dhcp_count;
84 int dhcp_lease;
85 int do_dhcpd;
86 int do_dns;
87 int do_dhcpd_hosts;
89 TRACE_PT("begin\n");
91 if (getpid() != 1) {
92 start_service("dnsmasq");
93 return;
96 stop_dnsmasq();
98 if (foreach_wif(1, NULL, is_wet)) return;
100 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
102 router_ip = nvram_safe_get("lan_ipaddr");
104 fprintf(f,
105 "pid-file=/var/run/dnsmasq.pid\n");
106 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
107 if (*nv) fprintf(f, "domain=%s\n", nv);
110 // dns
111 const dns_list_t *dns = get_dns(); // this always points to a static buffer
113 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
114 else n = 4096;
115 fprintf(f,
116 "resolv-file=%s\n" // the real stuff is here
117 "addn-hosts=%s\n" // directory with additional hosts files
118 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv, dmhosts, dmdhcp, n);
122 do_dns = nvram_match("dhcpd_dmdns", "1");
124 // DNS rebinding protection, will discard upstream RFC1918 responses
125 if (nvram_get_int("dns_norebind")) {
126 fprintf(f,
127 "stop-dns-rebind\n"
128 "rebind-localhost-ok\n");
129 // allow RFC1918 responses for server domain
130 switch (get_wan_proto()) {
131 case WP_PPTP:
132 nv = nvram_get("pptp_server_ip");
133 break;
134 case WP_L2TP:
135 nv = nvram_get("l2tp_server_ip");
136 break;
137 default:
138 nv = NULL;
139 break;
141 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
144 for (n = 0 ; n < dns->count; ++n) {
145 if (dns->dns[n].port != 53) {
146 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
150 if (nvram_get_int("dhcpd_static_only")) {
151 fprintf(f, "dhcp-ignore=tag:!known\n");
154 // dhcp
155 do_dhcpd_hosts=0;
156 char lanN_proto[] = "lanXX_proto";
157 char lanN_ifname[] = "lanXX_ifname";
158 char lanN_ipaddr[] = "lanXX_ipaddr";
159 char lanN_netmask[] = "lanXX_netmask";
160 char dhcpdN_startip[] = "dhcpdXX_startip";
161 char dhcpdN_endip[] = "dhcpdXX_endip";
162 char dhcpN_start[] = "dhcpXX_start";
163 char dhcpN_num[] = "dhcpXX_num";
164 char dhcpN_lease[] = "dhcpXX_lease";
165 char br;
166 for(br=0 ; br<=3 ; br++) {
167 char bridge[2] = "0";
168 if (br!=0)
169 bridge[0]+=br;
170 else
171 strcpy(bridge, "");
173 sprintf(lanN_proto, "lan%s_proto", bridge);
174 sprintf(lanN_ifname, "lan%s_ifname", bridge);
175 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
176 do_dhcpd = nvram_match(lanN_proto, "dhcp");
177 if (do_dhcpd) {
178 do_dhcpd_hosts++;
180 router_ip = nvram_safe_get(lanN_ipaddr);
181 strlcpy(lan, router_ip, sizeof(lan));
182 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
184 fprintf(f,
185 "interface=%s\n",
186 nvram_safe_get(lanN_ifname));
188 sprintf(dhcpN_lease, "dhcp%s_lease", bridge);
189 dhcp_lease = nvram_get_int(dhcpN_lease);
191 if (dhcp_lease <= 0) dhcp_lease = 1440;
193 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
194 if (n < 0) strcpy(sdhcp_lease, "infinite");
195 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
197 if (!do_dns) {
198 // if not using dnsmasq for dns
200 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
201 // no DNS might be temporary. use a low lease time to force clients to update.
202 dhcp_lease = 2;
203 strcpy(sdhcp_lease, "2m");
204 do_dns = 1;
206 else {
207 // pass the dns directly
208 buf[0] = 0;
209 for (n = 0 ; n < dns->count; ++n) {
210 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
211 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
214 fprintf(f, "dhcp-option=%s,6%s\n", nvram_safe_get(lanN_ifname), buf);
218 sprintf(dhcpdN_startip, "dhcpd%s_startip", bridge);
219 sprintf(dhcpdN_endip, "dhcpd%s_endip", bridge);
220 sprintf(lanN_netmask, "lan%s_netmask", bridge);
222 if ((p = nvram_get(dhcpdN_startip)) && (*p) && (e = nvram_get(dhcpdN_endip)) && (*e)) {
223 fprintf(f, "dhcp-range=%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname), p, e, nvram_safe_get(lanN_netmask), dhcp_lease);
225 else {
226 // for compatibility
227 sprintf(dhcpN_start, "dhcp%s_start", bridge);
228 sprintf(dhcpN_num, "dhcp%s_num", bridge);
229 sprintf(lanN_netmask, "lan%s_netmask", bridge);
230 dhcp_start = nvram_get_int(dhcpN_start);
231 dhcp_count = nvram_get_int(dhcpN_num);
232 fprintf(f, "dhcp-range=%s,%s%d,%s%d,%s,%dm\n",
233 nvram_safe_get(lanN_ifname), lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(lanN_netmask), dhcp_lease);
236 nv = nvram_safe_get(lanN_ipaddr);
237 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
238 p = nvram_safe_get("lan_gateway");
239 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
241 #ifdef TCONFIG_VLAN
242 fprintf(f,
243 "dhcp-option=%s,3,%s\n", // gateway
244 nvram_safe_get(lanN_ifname), nv);
245 #endif
246 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
247 fprintf(f, "dhcp-option=%s,44,%s\n", nvram_safe_get(lanN_ifname), nv);
249 #ifdef TCONFIG_SAMBASRV
250 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
251 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
252 // Samba will serve as a WINS server
253 fprintf(f, "dhcp-option=%s,44,0.0.0.0\n", nvram_safe_get(lanN_ifname));
256 #endif
257 } else {
258 if (strcmp(nvram_safe_get(lanN_ifname),"")!=0) {
259 fprintf(f, "interface=%s\n", nvram_safe_get(lanN_ifname));
260 fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname));
264 // write static lease entries & create hosts file
266 mkdir_if_none(dmhosts);
267 snprintf(buf, sizeof(buf), "%s/hosts", dmhosts);
268 if ((hf = fopen(buf, "w")) != NULL) {
269 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
270 fprintf(hf, "%s %s\n", router_ip, nv);
271 #ifdef TCONFIG_SAMBASRV
272 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
273 fprintf(hf, "%s %s\n", router_ip, nv);
274 #endif
275 p = (char *)get_wanip();
276 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
277 p = "127.0.0.1";
278 fprintf(hf, "%s wan-ip\n", p);
279 if (nv && (*nv))
280 fprintf(hf, "%s %s-wan\n", p, nv);
284 mkdir_if_none(dmdhcp);
285 snprintf(buf, sizeof(buf), "%s/dhcp-hosts", dmdhcp);
286 df = fopen(buf, "w");
288 // PREVIOUS/OLD FORMAT:
289 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 73 w/ delim
290 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
291 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 103 w/ delim
293 // NEW FORMAT (+static ARP binding flag after hostname):
294 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 75 w/ delim
295 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
296 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 105 w/ delim
298 p = nvram_safe_get("dhcpd_static");
299 while ((e = strchr(p, '>')) != NULL) {
300 n = (e - p);
301 if (n > 104) {
302 p = e + 1;
303 continue;
306 strncpy(buf, p, n);
307 buf[n] = 0;
308 p = e + 1;
310 if ((e = strchr(buf, '<')) == NULL) continue;
311 *e = 0;
312 mac = buf;
314 ip = e + 1;
315 if ((e = strchr(ip, '<')) == NULL) continue;
316 *e = 0;
317 if (strchr(ip, '.') == NULL) {
318 ipn = atoi(ip);
319 if ((ipn <= 0) || (ipn > 255)) continue;
320 sprintf(ipbuf, "%s%d", lan, ipn);
321 ip = ipbuf;
323 else {
324 if (inet_addr(ip) == INADDR_NONE) continue;
327 name = e + 1;
329 if ((e = strchr(name, '<')) != NULL) {
330 *e = 0;
333 if ((hf) && (*name != 0)) {
334 fprintf(hf, "%s %s\n", ip, name);
337 if ((do_dhcpd_hosts > 0) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
338 char static_dhcp_lease[32];
339 strcpy(static_dhcp_lease, "");
340 if (nvram_get_int("dhcpd_slt") != 0)
341 sprintf(static_dhcp_lease, ",%s", sdhcp_lease);
342 if (df)
343 fprintf(df, "%s,%s%s\n", mac, ip, static_dhcp_lease);
344 else
345 fprintf(f, "dhcp-host=%s,%s%s\n", mac, ip, static_dhcp_lease);
349 if (df) fclose(df);
350 if (hf) fclose(hf);
352 n = nvram_get_int("dhcpd_lmax");
353 fprintf(f,
354 "dhcp-lease-max=%d\n",
355 (n > 0) ? n : 255);
356 if (nvram_get_int("dhcpd_auth") >= 0) {
357 fprintf(f, "dhcp-authoritative\n");
362 #ifdef TCONFIG_OPENVPN
363 write_vpn_dnsmasq_config(f);
364 #endif
366 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
368 fappend(f, "/etc/dnsmasq.custom");
372 fclose(f);
374 if (do_dns) {
375 unlink("/etc/resolv.conf");
376 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
379 TRACE_PT("run dnsmasq\n");
381 // Default to some values we like, but allow the user to override them.
382 eval("dnsmasq", "-c", "1500", "--log-async");
384 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
385 pid_dnsmasq = -2;
388 TRACE_PT("end\n");
391 void stop_dnsmasq(void)
393 TRACE_PT("begin\n");
395 if (getpid() != 1) {
396 stop_service("dnsmasq");
397 return;
400 pid_dnsmasq = -1;
402 unlink("/etc/resolv.conf");
403 symlink(dmresolv, "/etc/resolv.conf");
405 killall_tk("dnsmasq");
407 TRACE_PT("end\n");
410 void clear_resolv(void)
412 f_write(dmresolv, NULL, 0, 0, 0); // blank
415 #ifdef TCONFIG_IPV6
416 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
418 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
419 struct in6_addr addr;
420 int cnt = 0;
422 foreach(p, dns, next) {
423 // verify that this is a valid IPv6 address
424 if (inet_pton(AF_INET6, p, &addr) == 1) {
425 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
426 ++cnt;
430 return cnt;
432 #endif
434 void dns_to_resolv(void)
436 FILE *f;
437 const dns_list_t *dns;
438 int i;
439 mode_t m;
441 m = umask(022); // 077 from pppoecd
442 if ((f = fopen(dmresolv, "w")) != NULL) {
443 // Check for VPN DNS entries
444 if (!write_pptpvpn_resolv(f) && !write_vpn_resolv(f)) {
445 #ifdef TCONFIG_IPV6
446 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
447 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
448 #endif
449 dns = get_dns(); // static buffer
450 if (dns->count == 0) {
451 // Put a pseudo DNS IP to trigger Connect On Demand
452 if (nvram_match("ppp_demand", "1")) {
453 switch (get_wan_proto()) {
454 case WP_PPPOE:
455 case WP_PPP3G:
456 case WP_PPTP:
457 case WP_L2TP:
458 fprintf(f, "nameserver 1.1.1.1\n");
459 break;
463 else {
464 for (i = 0; i < dns->count; i++) {
465 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
466 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
471 fclose(f);
473 umask(m);
476 // -----------------------------------------------------------------------------
478 void start_httpd(void)
480 if (getpid() != 1) {
481 start_service("httpd");
482 return;
485 stop_httpd();
486 chdir("/www");
487 eval("httpd");
488 chdir("/");
491 void stop_httpd(void)
493 if (getpid() != 1) {
494 stop_service("httpd");
495 return;
498 killall_tk("httpd");
501 // -----------------------------------------------------------------------------
502 #ifdef TCONFIG_IPV6
504 static void add_ip6_lanaddr(void)
506 char ip[INET6_ADDRSTRLEN + 4];
507 const char *p;
509 p = ipv6_router_address(NULL);
510 if (*p) {
511 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
512 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
516 void start_ipv6_tunnel(void)
518 char ip[INET6_ADDRSTRLEN + 4];
519 struct in_addr addr4;
520 struct in6_addr addr;
521 const char *wanip, *mtu, *tun_dev;
522 int service;
524 service = get_ipv6_service();
525 tun_dev = get_wan6face();
526 wanip = get_wanip();
527 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
528 modprobe("sit");
530 if (service == IPV6_ANYCAST_6TO4)
531 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
532 else
533 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
534 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
535 "remote", ip,
536 "local", (char *)wanip,
537 "ttl", nvram_safe_get("ipv6_tun_ttl"));
539 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
540 nvram_set("ipv6_ifname", (char *)tun_dev);
542 if (service == IPV6_ANYCAST_6TO4) {
543 add_ip6_lanaddr();
544 addr4.s_addr = 0;
545 memset(&addr, 0, sizeof(addr));
546 inet_aton(wanip, &addr4);
547 addr.s6_addr16[0] = htons(0x2002);
548 ipv6_mapaddr4(&addr, 16, &addr4, 0);
549 addr.s6_addr16[7] = htons(0x0001);
550 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
551 strncat(ip, "/16", sizeof(ip));
553 else {
554 snprintf(ip, sizeof(ip), "%s/%d",
555 nvram_safe_get("ipv6_tun_addr"),
556 nvram_get_int("ipv6_tun_addrlen") ? : 64);
558 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
559 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
561 // (re)start radvd
562 if (service == IPV6_ANYCAST_6TO4)
563 start_radvd();
566 void stop_ipv6_tunnel(void)
568 eval("ip", "tunnel", "del", (char *)get_wan6face());
569 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
570 // get rid of old IPv6 address from lan iface
571 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
573 modprobe_r("sit");
576 static pid_t pid_radvd = -1;
578 void start_radvd(void)
580 FILE *f;
581 char *prefix, *ip, *mtu;
582 int do_dns, do_6to4;
583 char *argv[] = { "radvd", NULL, NULL, NULL };
584 int pid, argc, service, cnt;
586 if (getpid() != 1) {
587 start_service("radvd");
588 return;
591 stop_radvd();
593 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
594 service = get_ipv6_service();
595 do_6to4 = (service == IPV6_ANYCAST_6TO4);
596 mtu = NULL;
598 switch (service) {
599 case IPV6_NATIVE_DHCP:
600 prefix = "::";
601 break;
602 case IPV6_ANYCAST_6TO4:
603 case IPV6_6IN4:
604 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
605 // fall through
606 default:
607 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
608 break;
610 if (!(*prefix)) prefix = "::";
612 // Create radvd.conf
613 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
615 ip = (char *)ipv6_router_address(NULL);
616 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
618 fprintf(f,
619 "interface %s\n"
620 "{\n"
621 " IgnoreIfMissing on;\n"
622 " AdvSendAdvert on;\n"
623 " MaxRtrAdvInterval 60;\n"
624 " AdvHomeAgentFlag off;\n"
625 " AdvManagedFlag off;\n"
626 "%s%s%s"
627 " prefix %s/64 \n"
628 " {\n"
629 " AdvOnLink on;\n"
630 " AdvAutonomous on;\n"
631 "%s"
632 "%s%s%s"
633 " };\n",
634 nvram_safe_get("lan_ifname"),
635 mtu ? " AdvLinkMTU " : "", mtu ? : "", mtu ? ";\n" : "",
636 prefix,
637 do_6to4 ? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
638 do_6to4 ? " Base6to4Interface " : "",
639 do_6to4 ? get_wanface() : "",
640 do_6to4 ? ";\n" : "");
642 if (do_dns) {
643 fprintf(f, " RDNSS %s {};\n", ip);
645 else {
646 cnt = write_ipv6_dns_servers(f, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
647 if (cnt == 0 || nvram_get_int("dns_addget"))
648 cnt += write_ipv6_dns_servers(f, (cnt) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
649 if (cnt) fprintf(f, "{};\n");
652 fprintf(f,
653 "};\n"); // close "interface" section
654 fclose(f);
656 // Start radvd
657 argc = 1;
658 if (nvram_get_int("debug_ipv6")) {
659 argv[argc++] = "-d";
660 argv[argc++] = "10";
662 argv[argc] = NULL;
663 _eval(argv, NULL, 0, &pid);
665 if (!nvram_contains_word("debug_norestart", "radvd")) {
666 pid_radvd = -2;
671 void stop_radvd(void)
673 if (getpid() != 1) {
674 stop_service("radvd");
675 return;
678 pid_radvd = -1;
679 killall_tk("radvd");
682 void start_ipv6(void)
684 int service;
686 service = get_ipv6_service();
687 enable_ip_forward();
689 // Check if turned on
690 switch (service) {
691 case IPV6_NATIVE:
692 case IPV6_6IN4:
693 case IPV6_MANUAL:
694 add_ip6_lanaddr();
695 break;
696 case IPV6_NATIVE_DHCP:
697 case IPV6_ANYCAST_6TO4:
698 nvram_set("ipv6_rtr_addr", "");
699 nvram_set("ipv6_prefix", "");
700 break;
703 if (service != IPV6_DISABLED) {
704 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
705 accept_ra(nvram_safe_get("lan_ifname"));
709 void stop_ipv6(void)
711 stop_ipv6_tunnel();
712 stop_dhcp6c();
713 eval("ip", "-6", "addr", "flush", "scope", "global");
716 #endif
718 // -----------------------------------------------------------------------------
720 void start_upnp(void)
722 if (getpid() != 1) {
723 start_service("upnp");
724 return;
727 if (get_wan_proto() == WP_DISABLED) return;
729 int enable;
730 FILE *f;
731 int upnp_port;
733 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
734 mkdir("/etc/upnp", 0777);
735 if (f_exists("/etc/upnp/config.alt")) {
736 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
738 else {
739 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
740 upnp_port = nvram_get_int("upnp_port");
741 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
744 fprintf(f,
745 "ext_ifname=%s\n"
746 "port=%d\n"
747 "enable_upnp=%s\n"
748 "enable_natpmp=%s\n"
749 "secure_mode=%s\n"
750 "upnp_forward_chain=upnp\n"
751 "upnp_nat_chain=upnp\n"
752 "notify_interval=%d\n"
753 "system_uptime=yes\n"
754 "\n"
756 get_wanface(),
757 upnp_port,
758 (enable & 1) ? "yes" : "no", // upnp enable
759 (enable & 2) ? "yes" : "no", // natpmp enable
760 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
761 nvram_get_int("upnp_ssdp_interval")
764 if (nvram_get_int("upnp_clean")) {
765 int interval = nvram_get_int("upnp_clean_interval");
766 if (interval < 60) interval = 60;
767 fprintf(f,
768 "clean_ruleset_interval=%d\n"
769 "clean_ruleset_threshold=%d\n",
770 interval,
771 nvram_get_int("upnp_clean_threshold")
774 else
775 fprintf(f,"clean_ruleset_interval=0\n");
777 if (nvram_match("upnp_mnp", "1")) {
778 int https = nvram_get_int("https_enable");
779 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
780 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
781 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
783 else {
784 // Empty parameters are not included into XML service description
785 fprintf(f, "presentation_url=\n");
788 char uuid[45];
789 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
790 fprintf(f, "uuid=%s\n", uuid);
792 #ifdef TCONFIG_VLAN
793 char lanN_ipaddr[] = "lanXX_ipaddr";
794 char lanN_netmask[] = "lanXX_netmask";
795 char upnp_lanN[] = "upnp_lanXX";
796 char br;
798 for(br=0 ; br<4 ; br++) {
799 char bridge[2] = "0";
800 if (br!=0)
801 bridge[0]+=br;
802 else
803 strcpy(bridge, "");
805 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
806 sprintf(lanN_netmask, "lan%s_netmask", bridge);
807 sprintf(upnp_lanN, "upnp_lan%s", bridge);
809 char *lanip = nvram_safe_get(lanN_ipaddr);
810 char *lanmask = nvram_safe_get(lanN_netmask);
811 char *lanlisten = nvram_safe_get(upnp_lanN);
812 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
813 #else
814 char *lanip = nvram_safe_get("lan_ipaddr");
815 char *lanmask = nvram_safe_get("lan_netmask");
816 #endif
817 fprintf(f,
818 "listening_ip=%s/%s\n",
819 lanip, lanmask);
820 int ports[4];
821 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
822 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
823 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
824 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
825 fprintf(f,
826 "allow %d-%d %s/%s %d-%d\n",
827 ports[0], ports[1],
828 lanip, lanmask,
829 ports[2], ports[3]
832 else {
833 // by default allow only redirection of ports above 1024
834 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
836 #ifdef TCONFIG_VLAN
839 #endif
841 fappend(f, "/jffs/upnpconfig.custom");
842 fappend(f, "/etc/upnp/config.custom");
843 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
844 fclose(f);
846 xstart("miniupnpd", "-f", "/etc/upnp/config");
852 void stop_upnp(void)
854 if (getpid() != 1) {
855 stop_service("upnp");
856 return;
859 killall_tk("miniupnpd");
862 // -----------------------------------------------------------------------------
864 static pid_t pid_crond = -1;
866 void start_cron(void)
868 stop_cron();
870 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
871 if (!nvram_contains_word("debug_norestart", "crond")) {
872 pid_crond = -2;
876 void stop_cron(void)
878 pid_crond = -1;
879 killall_tk("crond");
882 // -----------------------------------------------------------------------------
883 #ifdef LINUX26
885 static pid_t pid_hotplug2 = -1;
887 void start_hotplug2()
889 stop_hotplug2();
891 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
892 xstart("hotplug2", "--persistent", "--no-coldplug");
893 // FIXME: Don't remember exactly why I put "sleep" here -
894 // but it was not for a race with check_services()... - TB
895 sleep(1);
897 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
898 pid_hotplug2 = -2;
902 void stop_hotplug2(void)
904 pid_hotplug2 = -1;
905 killall_tk("hotplug2");
908 #endif /* LINUX26 */
909 // -----------------------------------------------------------------------------
911 // Written by Sparq in 2002/07/16
912 void start_zebra(void)
914 #ifdef TCONFIG_ZEBRA
915 if (getpid() != 1) {
916 start_service("zebra");
917 return;
920 FILE *fp;
922 char *lan_tx = nvram_safe_get("dr_lan_tx");
923 char *lan_rx = nvram_safe_get("dr_lan_rx");
924 #ifdef TCONFIG_VLAN
925 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
926 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
927 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
928 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
929 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
930 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
931 #endif
932 char *wan_tx = nvram_safe_get("dr_wan_tx");
933 char *wan_rx = nvram_safe_get("dr_wan_rx");
935 #ifdef TCONFIG_VLAN
936 if ((*lan_tx == '0') && (*lan_rx == '0') &&
937 (*lan1_tx == '0') && (*lan1_rx == '0') &&
938 (*lan2_tx == '0') && (*lan2_rx == '0') &&
939 (*lan3_tx == '0') && (*lan3_rx == '0') &&
940 (*wan_tx == '0') && (*wan_rx == '0')) {
941 #else
942 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
943 #endif
944 return;
947 // empty
948 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
949 fclose(fp);
953 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
954 char *lan_ifname = nvram_safe_get("lan_ifname");
955 #ifdef TCONFIG_VLAN
956 char *lan1_ifname = nvram_safe_get("lan1_ifname");
957 char *lan2_ifname = nvram_safe_get("lan2_ifname");
958 char *lan3_ifname = nvram_safe_get("lan3_ifname");
959 #endif
960 char *wan_ifname = nvram_safe_get("wan_ifname");
962 fprintf(fp, "router rip\n");
963 if(strcmp(lan_ifname,"")!=0)
964 fprintf(fp, "network %s\n", lan_ifname);
965 #ifdef TCONFIG_VLAN
966 if(strcmp(lan1_ifname,"")!=0)
967 fprintf(fp, "network %s\n", lan1_ifname);
968 if(strcmp(lan2_ifname,"")!=0)
969 fprintf(fp, "network %s\n", lan2_ifname);
970 if(strcmp(lan3_ifname,"")!=0)
971 fprintf(fp, "network %s\n", lan3_ifname);
972 #endif
973 fprintf(fp, "network %s\n", wan_ifname);
974 fprintf(fp, "redistribute connected\n");
975 //fprintf(fp, "redistribute static\n");
977 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
978 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
980 if(strcmp(lan_ifname,"")!=0) {
981 fprintf(fp, "interface %s\n", lan_ifname);
982 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
983 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
985 #ifdef TCONFIG_VLAN
986 if(strcmp(lan1_ifname,"")!=0) {
987 fprintf(fp, "interface %s\n", lan1_ifname);
988 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
989 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
991 if(strcmp(lan2_ifname,"")!=0) {
992 fprintf(fp, "interface %s\n", lan2_ifname);
993 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
994 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
996 if(strcmp(lan3_ifname,"")!=0) {
997 fprintf(fp, "interface %s\n", lan3_ifname);
998 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
999 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
1001 #endif
1002 fprintf(fp, "interface %s\n", wan_ifname);
1003 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
1004 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
1006 fprintf(fp, "router rip\n");
1007 if(strcmp(lan_ifname,"")!=0) {
1008 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
1009 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
1011 #ifdef TCONFIG_VLAN
1012 if(strcmp(lan1_ifname,"")!=0) {
1013 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
1014 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
1016 if(strcmp(lan2_ifname,"")!=0) {
1017 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
1018 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
1020 if(strcmp(lan3_ifname,"")!=0) {
1021 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
1022 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
1024 #endif
1025 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
1026 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
1027 fprintf(fp, "access-list private deny any\n");
1029 //fprintf(fp, "debug rip events\n");
1030 //fprintf(fp, "log file /etc/ripd.log\n");
1031 fclose(fp);
1034 xstart("zebra", "-d");
1035 xstart("ripd", "-d");
1036 #endif
1039 void stop_zebra(void)
1041 #ifdef TCONFIG_ZEBRA
1042 if (getpid() != 1) {
1043 stop_service("zebra");
1044 return;
1047 killall("zebra", SIGTERM);
1048 killall("ripd", SIGTERM);
1050 unlink("/etc/zebra.conf");
1051 unlink("/etc/ripd.conf");
1052 #endif
1055 // -----------------------------------------------------------------------------
1057 void start_syslog(void)
1059 char *argv[16];
1060 int argc;
1061 char *nv;
1062 char *b_opt = "";
1063 char rem[256];
1064 int n;
1065 char s[64];
1066 char cfg[256];
1067 char *rot_siz = "50";
1068 char *rot_keep = "1";
1069 char *log_file_path;
1071 argv[0] = "syslogd";
1072 argc = 1;
1074 if (nvram_match("log_remote", "1")) {
1075 nv = nvram_safe_get("log_remoteip");
1076 if (*nv) {
1077 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1078 argv[argc++] = "-R";
1079 argv[argc++] = rem;
1083 if (nvram_match("log_file", "1")) {
1084 argv[argc++] = "-L";
1086 if (strcmp(nvram_safe_get("log_file_size"), "") != 0) {
1087 rot_siz = nvram_safe_get("log_file_size");
1089 if (nvram_get_int("log_file_size") > 0) {
1090 rot_keep = nvram_safe_get("log_file_keep");
1093 // log to custom path - shibby
1094 if (nvram_match("log_file_custom", "1")) {
1095 log_file_path = nvram_safe_get("log_file_path");
1096 argv[argc++] = "-s";
1097 argv[argc++] = rot_siz;
1098 argv[argc++] = "-O";
1099 argv[argc++] = log_file_path;
1100 if (strcmp(nvram_safe_get("log_file_path"), "/var/log/messages") != 0) {
1101 remove("/var/log/messages");
1102 symlink(log_file_path, "/var/log/messages");
1105 else
1107 /* Read options: rotate_size(kb) num_backups logfilename.
1108 * Ignore these settings and use defaults if the logfile cannot be written to.
1110 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1111 if ((nv = strchr(cfg, '\n')))
1112 *nv = 0;
1114 if ((nv = strtok(cfg, " \t"))) {
1115 if (isdigit(*nv))
1116 rot_siz = nv;
1119 if ((nv = strtok(NULL, " \t")))
1120 b_opt = nv;
1122 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1123 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1124 argv[argc++] = "-O";
1125 argv[argc++] = nv;
1127 else {
1128 rot_siz = "50";
1129 b_opt = "";
1134 if (nvram_match("log_file_custom", "0")) {
1135 argv[argc++] = "-s";
1136 argv[argc++] = rot_siz;
1137 struct stat sb;
1138 if (lstat("/var/log/messages", &sb) != -1)
1139 if (S_ISLNK(sb.st_mode))
1140 remove("/var/log/messages");
1143 if (isdigit(*b_opt)) {
1144 argv[argc++] = "-b";
1145 argv[argc++] = b_opt;
1146 } else
1147 if (nvram_get_int("log_file_size") > 0) {
1148 argv[argc++] = "-b";
1149 argv[argc++] = rot_keep;
1153 if (argc > 1) {
1154 argv[argc] = NULL;
1155 _eval(argv, NULL, 0, NULL);
1157 argv[0] = "klogd";
1158 argv[1] = NULL;
1159 _eval(argv, NULL, 0, NULL);
1161 // used to be available in syslogd -m
1162 n = nvram_get_int("log_mark");
1163 if (n > 0) {
1164 // n is in minutes
1165 if (n < 60)
1166 sprintf(rem, "*/%d * * * *", n);
1167 else if (n < 60 * 24)
1168 sprintf(rem, "0 */%d * * *", n / 60);
1169 else
1170 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1171 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1172 eval("cru", "a", "syslogdmark", s);
1174 else {
1175 eval("cru", "d", "syslogdmark");
1180 void stop_syslog(void)
1182 killall("klogd", SIGTERM);
1183 killall("syslogd", SIGTERM);
1186 // -----------------------------------------------------------------------------
1188 static pid_t pid_igmp = -1;
1190 void start_igmp_proxy(void)
1192 FILE *fp;
1194 pid_igmp = -1;
1195 if (nvram_match("multicast_pass", "1")) {
1196 if (get_wan_proto() == WP_DISABLED)
1197 return;
1199 if (f_exists("/etc/igmp.alt")) {
1200 eval("igmpproxy", "/etc/igmp.alt");
1202 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1203 fprintf(fp,
1204 "quickleave\n"
1205 "phyint %s upstream\n"
1206 "\taltnet %s\n",
1207 // "phyint %s downstream ratelimit 0\n",
1208 // get_wanface(),
1209 nvram_safe_get("wan_ifname"),
1210 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1211 // nvram_safe_get("lan_ifname"));
1213 #ifdef TCONFIG_VLAN
1214 char lanN_ifname[] = "lanXX_ifname";
1215 char multicast_lanN[] = "multicast_lanXX";
1216 char br;
1218 for(br=0 ; br<4 ; br++) {
1219 char bridge[2] = "0";
1220 if (br!=0)
1221 bridge[0]+=br;
1222 else
1223 strcpy(bridge, "");
1225 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1226 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1228 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1229 fprintf(fp,
1230 "phyint %s downstream ratelimit 0\n",
1231 nvram_safe_get(lanN_ifname));
1234 #else
1235 fprintf(fp,
1236 "phyint %s downstream ratelimit 0\n",
1237 nvram_safe_get("lan_ifname"));
1238 #endif
1239 fclose(fp);
1240 eval("igmpproxy", "/etc/igmp.conf");
1242 else {
1243 return;
1245 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1246 pid_igmp = -2;
1251 void stop_igmp_proxy(void)
1253 pid_igmp = -1;
1254 killall_tk("igmpproxy");
1257 // -----------------------------------------------------------------------------
1259 void start_udpxy(void)
1261 if (nvram_match("udpxy_enable", "1")) {
1262 if (get_wan_proto() == WP_DISABLED)
1263 return;
1264 eval("udpxy", (nvram_get_int("udpxy_stats") ? "-S" : ""), "-p", nvram_safe_get("udpxy_port"), "-c", nvram_safe_get("udpxy_clients"), "-m", nvram_safe_get("wan_ifname") );
1268 void stop_udpxy(void)
1270 killall_tk("udpxy");
1273 // -----------------------------------------------------------------------------
1275 #ifdef TCONFIG_NOCAT
1277 static pid_t pid_splashd = -1;
1278 void start_splashd(void)
1280 pid_splashd = -1;
1281 start_nocat();
1282 if (!nvram_contains_word("debug_norestart", "splashd")) {
1283 pid_splashd = -2;
1287 void stop_splashd(void)
1289 pid_splashd = -1;
1290 stop_nocat();
1292 #endif
1294 // -----------------------------------------------------------------------------
1296 void set_tz(void)
1298 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1301 void start_ntpc(void)
1303 set_tz();
1305 stop_ntpc();
1307 if (nvram_get_int("ntp_updates") >= 0) {
1308 xstart("ntpsync", "--init");
1312 void stop_ntpc(void)
1314 killall("ntpsync", SIGTERM);
1317 // -----------------------------------------------------------------------------
1319 static void stop_rstats(void)
1321 int n, m;
1322 int pid;
1323 int pidz;
1324 int ppidz;
1325 int w = 0;
1327 n = 60;
1328 m = 15;
1329 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1330 w = 1;
1331 pidz = pidof("gzip");
1332 if (pidz < 1) pidz = pidof("cp");
1333 ppidz = ppid(ppid(pidz));
1334 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1335 syslog(LOG_DEBUG, "rstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1336 --m;
1337 } else {
1338 kill(pid, SIGTERM);
1340 sleep(1);
1342 if ((w == 1) && (n > 0))
1343 syslog(LOG_DEBUG, "rstats stopped.\n");
1346 static void start_rstats(int new)
1348 if (nvram_match("rstats_enable", "1")) {
1349 stop_rstats();
1350 if (new) {
1351 syslog(LOG_DEBUG, "starting rstats (new datafile).\n");
1352 xstart("rstats", "--new");
1353 } else {
1354 syslog(LOG_DEBUG, "starting rstats.\n");
1355 xstart("rstats");
1360 static void stop_cstats(void)
1362 int n, m;
1363 int pid;
1364 int pidz;
1365 int ppidz;
1366 int w = 0;
1368 n = 60;
1369 m = 15;
1370 while ((n-- > 0) && ((pid = pidof("cstats")) > 0)) {
1371 w = 1;
1372 pidz = pidof("gzip");
1373 if (pidz < 1) pidz = pidof("cp");
1374 ppidz = ppid(ppid(pidz));
1375 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1376 syslog(LOG_DEBUG, "cstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1377 --m;
1378 } else {
1379 kill(pid, SIGTERM);
1381 sleep(1);
1383 if ((w == 1) && (n > 0))
1384 syslog(LOG_DEBUG, "cstats stopped.\n");
1387 static void start_cstats(int new)
1389 if (nvram_match("cstats_enable", "1")) {
1390 stop_cstats();
1391 if (new) {
1392 syslog(LOG_DEBUG, "starting cstats (new datafile).\n");
1393 xstart("cstats", "--new");
1394 } else {
1395 syslog(LOG_DEBUG, "starting cstats.\n");
1396 xstart("cstats");
1401 // -----------------------------------------------------------------------------
1403 // !!TB - FTP Server
1405 #ifdef TCONFIG_FTP
1406 static char *get_full_storage_path(char *val)
1408 static char buf[128];
1409 int len;
1411 if (val[0] == '/')
1412 len = sprintf(buf, "%s", val);
1413 else
1414 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1416 if (len > 1 && buf[len - 1] == '/')
1417 buf[len - 1] = 0;
1419 return buf;
1422 static char *nvram_storage_path(char *var)
1424 char *val = nvram_safe_get(var);
1425 return get_full_storage_path(val);
1428 char vsftpd_conf[] = "/etc/vsftpd.conf";
1429 char vsftpd_users[] = "/etc/vsftpd.users";
1430 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1432 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1434 static void start_ftpd(void)
1436 char tmp[256];
1437 FILE *fp, *f;
1438 char *buf;
1439 char *p, *q;
1440 char *user, *pass, *rights, *root_dir;
1441 int i;
1443 if (getpid() != 1) {
1444 start_service("ftpd");
1445 return;
1448 if (!nvram_get_int("ftp_enable")) return;
1450 mkdir_if_none(vsftpd_users);
1451 mkdir_if_none("/var/run/vsftpd");
1453 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1454 return;
1456 if (nvram_get_int("ftp_super"))
1458 /* rights */
1459 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1460 if ((f = fopen(tmp, "w")))
1462 fprintf(f,
1463 "dirlist_enable=yes\n"
1464 "write_enable=yes\n"
1465 "download_enable=yes\n");
1466 fclose(f);
1470 #ifdef TCONFIG_SAMBASRV
1471 if (nvram_match("smbd_cset", "utf8"))
1472 fprintf(fp, "utf8=yes\n");
1473 #endif
1475 if (nvram_invmatch("ftp_anonymous", "0"))
1477 fprintf(fp,
1478 "anon_allow_writable_root=yes\n"
1479 "anon_world_readable_only=no\n"
1480 "anon_umask=022\n");
1482 /* rights */
1483 sprintf(tmp, "%s/ftp", vsftpd_users);
1484 if ((f = fopen(tmp, "w")))
1486 if (nvram_match("ftp_dirlist", "0"))
1487 fprintf(f, "dirlist_enable=yes\n");
1488 if (nvram_match("ftp_anonymous", "1") ||
1489 nvram_match("ftp_anonymous", "3"))
1490 fprintf(f, "write_enable=yes\n");
1491 if (nvram_match("ftp_anonymous", "1") ||
1492 nvram_match("ftp_anonymous", "2"))
1493 fprintf(f, "download_enable=yes\n");
1494 fclose(f);
1496 if (nvram_match("ftp_anonymous", "1") ||
1497 nvram_match("ftp_anonymous", "3"))
1498 fprintf(fp,
1499 "anon_upload_enable=yes\n"
1500 "anon_mkdir_write_enable=yes\n"
1501 "anon_other_write_enable=yes\n");
1502 } else {
1503 fprintf(fp, "anonymous_enable=no\n");
1506 fprintf(fp,
1507 "dirmessage_enable=yes\n"
1508 "download_enable=no\n"
1509 "dirlist_enable=no\n"
1510 "hide_ids=yes\n"
1511 "syslog_enable=yes\n"
1512 "local_enable=yes\n"
1513 "local_umask=022\n"
1514 "chmod_enable=no\n"
1515 "chroot_local_user=yes\n"
1516 "check_shell=no\n"
1517 "log_ftp_protocol=%s\n"
1518 "user_config_dir=%s\n"
1519 "passwd_file=%s\n"
1520 "listen%s=yes\n"
1521 "listen_port=%s\n"
1522 "background=yes\n"
1523 "isolate=no\n"
1524 "max_clients=%d\n"
1525 "max_per_ip=%d\n"
1526 "max_login_fails=1\n"
1527 "idle_session_timeout=%s\n"
1528 "use_sendfile=no\n"
1529 "anon_max_rate=%d\n"
1530 "local_max_rate=%d\n"
1531 "%s\n",
1532 nvram_get_int("log_ftp") ? "yes" : "no",
1533 vsftpd_users, vsftpd_passwd,
1534 #ifdef TCONFIG_IPV6
1535 ipv6_enabled() ? "_ipv6" : "",
1536 #else
1538 #endif
1539 nvram_get("ftp_port") ? : "21",
1540 nvram_get_int("ftp_max"),
1541 nvram_get_int("ftp_ipmax"),
1542 nvram_get("ftp_staytimeout") ? : "300",
1543 nvram_get_int("ftp_anonrate") * 1024,
1544 nvram_get_int("ftp_rate") * 1024,
1545 nvram_safe_get("ftp_custom"));
1547 fclose(fp);
1549 /* prepare passwd file and default users */
1550 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1551 return;
1553 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1554 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1556 fprintf(fp, /* anonymous, admin, nobody */
1557 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1558 "%s:%s:0:0:root:/:/sbin/nologin\n"
1559 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1560 nvram_storage_path("ftp_anonroot"), user,
1561 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1562 MOUNT_ROOT);
1564 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1567 username<password<rights[<root_dir]
1568 rights:
1569 Read/Write
1570 Read Only
1571 View Only
1572 Private
1574 p = buf;
1575 while ((q = strsep(&p, ">")) != NULL) {
1576 i = vstrsep(q, "<", &user, &pass, &rights, &root_dir);
1577 if (i < 3 || i > 4) continue;
1578 if (!user || !pass) continue;
1580 if (i == 3 || !root_dir || !(*root_dir))
1581 root_dir = nvram_safe_get("ftp_pubroot");
1583 /* directory */
1584 if (strncmp(rights, "Private", 7) == 0)
1586 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1587 mkdir_if_none(tmp);
1589 else
1590 sprintf(tmp, "%s", get_full_storage_path(root_dir));
1592 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1593 user, crypt(pass, "$1$"), user, tmp);
1595 /* rights */
1596 sprintf(tmp, "%s/%s", vsftpd_users, user);
1597 if ((f = fopen(tmp, "w")))
1599 tmp[0] = 0;
1600 if (nvram_invmatch("ftp_dirlist", "1"))
1601 strcat(tmp, "dirlist_enable=yes\n");
1602 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1603 strcat(tmp, "download_enable=yes\n");
1604 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1605 strcat(tmp, "write_enable=yes\n");
1607 fputs(tmp, f);
1608 fclose(f);
1611 free(buf);
1614 fclose(fp);
1615 killall("vsftpd", SIGHUP);
1617 /* start vsftpd if it's not already running */
1618 if (pidof("vsftpd") <= 0)
1619 xstart("vsftpd");
1622 static void stop_ftpd(void)
1624 if (getpid() != 1) {
1625 stop_service("ftpd");
1626 return;
1629 killall_tk("vsftpd");
1630 unlink(vsftpd_passwd);
1631 unlink(vsftpd_conf);
1632 eval("rm", "-rf", vsftpd_users);
1634 #endif // TCONFIG_FTP
1636 // -----------------------------------------------------------------------------
1638 // !!TB - Samba
1640 #ifdef TCONFIG_SAMBASRV
1641 static void kill_samba(int sig)
1643 if (sig == SIGTERM) {
1644 killall_tk("smbd");
1645 killall_tk("nmbd");
1647 else {
1648 killall("smbd", sig);
1649 killall("nmbd", sig);
1653 static void start_samba(void)
1655 FILE *fp;
1656 DIR *dir = NULL;
1657 struct dirent *dp;
1658 char nlsmod[15];
1659 int mode;
1660 char *nv;
1662 if (getpid() != 1) {
1663 start_service("smbd");
1664 return;
1667 mode = nvram_get_int("smbd_enable");
1668 if (!mode || !nvram_invmatch("lan_hostname", ""))
1669 return;
1671 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1672 return;
1674 fprintf(fp, "[global]\n"
1675 " interfaces = %s\n"
1676 " bind interfaces only = yes\n"
1677 " workgroup = %s\n"
1678 " netbios name = %s\n"
1679 " server string = %s\n"
1680 " guest account = nobody\n"
1681 " security = user\n"
1682 " %s\n"
1683 " guest ok = %s\n"
1684 " guest only = no\n"
1685 " browseable = yes\n"
1686 " syslog only = yes\n"
1687 " timestamp logs = no\n"
1688 " syslog = 1\n"
1689 " encrypt passwords = yes\n"
1690 " preserve case = yes\n"
1691 " short preserve case = yes\n",
1692 nvram_safe_get("lan_ifname"),
1693 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1694 nvram_safe_get("lan_hostname"),
1695 nvram_get("router_name") ? : "Tomato",
1696 mode == 2 ? "" : "map to guest = Bad User",
1697 mode == 2 ? "no" : "yes" // guest ok
1700 if (nvram_get_int("smbd_wins")) {
1701 nv = nvram_safe_get("wan_wins");
1702 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1703 fprintf(fp, " wins support = yes\n");
1707 if (nvram_get_int("smbd_master")) {
1708 fprintf(fp,
1709 " domain master = yes\n"
1710 " local master = yes\n"
1711 " preferred master = yes\n"
1712 " os level = 65\n");
1715 nv = nvram_safe_get("smbd_cpage");
1716 if (*nv) {
1717 #ifndef TCONFIG_SAMBA3
1718 fprintf(fp, " client code page = %s\n", nv);
1719 #endif
1720 sprintf(nlsmod, "nls_cp%s", nv);
1722 nv = nvram_safe_get("smbd_nlsmod");
1723 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1724 modprobe_r(nv);
1726 modprobe(nlsmod);
1727 nvram_set("smbd_nlsmod", nlsmod);
1730 #ifndef TCONFIG_SAMBA3
1731 if (nvram_match("smbd_cset", "utf8"))
1732 fprintf(fp, " coding system = utf8\n");
1733 else if (nvram_invmatch("smbd_cset", ""))
1734 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1735 #endif
1737 nv = nvram_safe_get("smbd_custom");
1738 /* add socket options unless overriden by the user */
1739 if (strstr(nv, "socket options") == NULL) {
1740 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1742 fprintf(fp, "%s\n\n", nv);
1744 /* configure shares */
1746 char *buf;
1747 char *p, *q;
1748 char *name, *path, *comment, *writeable, *hidden;
1749 int cnt = 0;
1751 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1753 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1755 p = buf;
1756 while ((q = strsep(&p, ">")) != NULL) {
1757 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1758 if (!path || !name) continue;
1760 /* share name */
1761 fprintf(fp, "\n[%s]\n", name);
1763 /* path */
1764 fprintf(fp, " path = %s\n", path);
1766 /* access level */
1767 if (!strcmp(writeable, "1"))
1768 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1769 if (!strcmp(hidden, "1"))
1770 fprintf(fp, " browseable = no\n");
1772 /* comment */
1773 if (comment)
1774 fprintf(fp, " comment = %s\n", comment);
1776 cnt++;
1778 free(buf);
1781 /* Share every mountpoint below MOUNT_ROOT */
1782 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1783 while ((dp = readdir(dir))) {
1784 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1786 /* Only if is a directory and is mounted */
1787 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
1788 continue;
1790 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1791 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1792 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1793 if (nvram_match("smbd_autoshare", "3")) // Hidden
1794 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1795 dp->d_name, MOUNT_ROOT, dp->d_name);
1796 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1797 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1799 cnt++;
1803 if (dir) closedir(dir);
1805 if (cnt == 0) {
1806 /* by default share MOUNT_ROOT as read-only */
1807 fprintf(fp, "\n[share]\n"
1808 " path = %s\n"
1809 " writable = no\n",
1810 MOUNT_ROOT);
1813 fclose(fp);
1815 mkdir_if_none("/var/run/samba");
1816 mkdir_if_none("/etc/samba");
1818 /* write smbpasswd */
1819 #ifdef TCONFIG_SAMBA3
1820 eval("smbpasswd", "nobody", "\"\"");
1821 #else
1822 eval("smbpasswd", "-a", "nobody", "\"\"");
1823 #endif
1824 if (mode == 2) {
1825 char *smbd_user;
1826 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1827 smbd_user = "nas";
1828 #ifdef TCONFIG_SAMBA3
1829 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1830 #else
1831 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1832 #endif
1835 kill_samba(SIGHUP);
1836 int ret1 = 0, ret2 = 0;
1837 /* start samba if it's not already running */
1838 if (pidof("nmbd") <= 0)
1839 ret1 = xstart("nmbd", "-D");
1840 if (pidof("smbd") <= 0)
1841 ret2 = xstart("smbd", "-D");
1843 if (ret1 || ret2) kill_samba(SIGTERM);
1846 static void stop_samba(void)
1848 if (getpid() != 1) {
1849 stop_service("smbd");
1850 return;
1853 kill_samba(SIGTERM);
1854 /* clean up */
1855 unlink("/var/log/smb");
1856 unlink("/var/log/nmb");
1857 eval("rm", "-rf", "/var/run/samba");
1859 #endif // TCONFIG_SAMBASRV
1861 #ifdef TCONFIG_MEDIA_SERVER
1862 #define MEDIA_SERVER_APP "minidlna"
1864 static void start_media_server(void)
1866 FILE *f;
1867 int port, pid, https;
1868 char *dbdir;
1869 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1870 static int once = 1;
1872 if (getpid() != 1) {
1873 start_service("media");
1874 return;
1877 if (nvram_get_int("ms_sas") == 0)
1878 once = 0;
1880 if (nvram_get_int("ms_enable") != 0) {
1881 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1882 // no forced rescan
1883 argv[3] = NULL;
1885 nvram_unset("ms_rescan");
1887 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1888 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1890 else {
1891 if ((f = fopen(argv[2], "w")) != NULL) {
1892 port = nvram_get_int("ms_port");
1893 https = nvram_get_int("https_enable");
1894 dbdir = nvram_safe_get("ms_dbdir");
1895 if (!(*dbdir)) dbdir = NULL;
1896 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1898 fprintf(f,
1899 "network_interface=%s\n"
1900 "port=%d\n"
1901 "friendly_name=%s\n"
1902 "db_dir=%s/.db\n"
1903 "enable_tivo=%s\n"
1904 "strict_dlna=%s\n"
1905 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1906 "inotify=yes\n"
1907 "notify_interval=600\n"
1908 "album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg\n"
1909 "\n",
1910 nvram_safe_get("lan_ifname"),
1911 (port < 0) || (port >= 0xffff) ? 0 : port,
1912 nvram_get("router_name") ? : "Tomato",
1913 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1914 nvram_get_int("ms_tivo") ? "yes" : "no",
1915 nvram_get_int("ms_stdlna") ? "yes" : "no",
1916 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1919 // media directories
1920 char *buf, *p, *q;
1921 char *path, *restrict;
1923 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1924 /* path<restrict[A|V|P|] */
1926 p = buf;
1927 while ((q = strsep(&p, ">")) != NULL) {
1928 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1929 continue;
1930 fprintf(f, "media_dir=%s%s%s\n",
1931 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1933 free(buf);
1936 fclose(f);
1940 /* start media server if it's not already running */
1941 if (pidof(MEDIA_SERVER_APP) <= 0) {
1942 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1943 /* If we started the media server successfully, wait 1 sec
1944 * to let it die if it can't open the database file.
1945 * If it's still alive after that, assume it's running and
1946 * disable forced once-after-reboot rescan.
1948 sleep(1);
1949 if (pidof(MEDIA_SERVER_APP) > 0)
1950 once = 0;
1956 static void stop_media_server(void)
1958 if (getpid() != 1) {
1959 stop_service("media");
1960 return;
1963 killall_tk(MEDIA_SERVER_APP);
1965 #endif // TCONFIG_MEDIA_SERVER
1967 #ifdef TCONFIG_USB
1968 static void start_nas_services(void)
1970 if (getpid() != 1) {
1971 start_service("usbapps");
1972 return;
1975 #ifdef TCONFIG_SAMBASRV
1976 start_samba();
1977 #endif
1978 #ifdef TCONFIG_FTP
1979 start_ftpd();
1980 #endif
1981 #ifdef TCONFIG_MEDIA_SERVER
1982 start_media_server();
1983 #endif
1986 static void stop_nas_services(void)
1988 if (getpid() != 1) {
1989 stop_service("usbapps");
1990 return;
1993 #ifdef TCONFIG_MEDIA_SERVER
1994 stop_media_server();
1995 #endif
1996 #ifdef TCONFIG_FTP
1997 stop_ftpd();
1998 #endif
1999 #ifdef TCONFIG_SAMBASRV
2000 stop_samba();
2001 #endif
2004 void restart_nas_services(int stop, int start)
2006 int fd = file_lock("usb");
2007 /* restart all NAS applications */
2008 if (stop)
2009 stop_nas_services();
2010 if (start)
2011 start_nas_services();
2012 file_unlock(fd);
2014 #endif // TCONFIG_USB
2016 // -----------------------------------------------------------------------------
2018 /* -1 = Don't check for this program, it is not expected to be running.
2019 * Other = This program has been started and should be kept running. If no
2020 * process with the name is running, call func to restart it.
2021 * Note: At startup, dnsmasq forks a short-lived child which forks a
2022 * long-lived (grand)child. The parents terminate.
2023 * Many daemons use this technique.
2025 static void _check(pid_t pid, const char *name, void (*func)(void))
2027 if (pid == -1) return;
2029 if (pidof(name) > 0) return;
2031 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
2032 func();
2034 // Force recheck in 500 msec
2035 setitimer(ITIMER_REAL, &pop_tv, NULL);
2038 void check_services(void)
2040 TRACE_PT("keep alive\n");
2042 // Periodically reap any zombies
2043 setitimer(ITIMER_REAL, &zombie_tv, NULL);
2045 #ifdef LINUX26
2046 _check(pid_hotplug2, "hotplug2", start_hotplug2);
2047 #endif
2048 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
2049 _check(pid_crond, "crond", start_cron);
2050 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
2051 #ifdef TCONFIG_IPV6
2052 _check(pid_radvd, "radvd", start_radvd);
2053 #endif
2055 // #ifdef TCONFIG_NOCAT
2056 // if (nvram_get_int("NC_enable"))
2057 // _check(&pid_splashd, "splashd", start_splashd);
2058 // #endif
2062 // -----------------------------------------------------------------------------
2064 void start_services(void)
2066 static int once = 1;
2068 if (once) {
2069 once = 0;
2071 if (nvram_get_int("telnetd_eas")) start_telnetd();
2072 if (nvram_get_int("sshd_eas")) start_sshd();
2075 // start_syslog();
2076 start_nas();
2077 start_zebra();
2078 start_dnsmasq();
2079 start_cifs();
2080 start_httpd();
2081 start_cron();
2082 // start_upnp();
2083 start_rstats(0);
2084 start_cstats(0);
2085 start_sched();
2086 #ifdef TCONFIG_PPTPD
2087 start_pptpd();
2088 #endif
2089 #ifdef TCONFIG_IPV6
2090 /* note: starting radvd here might be too early in case of
2091 * DHCPv6 or 6to4 because we won't have received a prefix and
2092 * so it will disable advertisements. To restart them, we have
2093 * to send radvd a SIGHUP, or restart it.
2095 start_radvd();
2096 #endif
2097 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2099 #ifdef TCONFIG_SNMP
2100 start_snmp();
2101 #endif
2105 void stop_services(void)
2107 clear_resolv();
2108 // restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2112 #ifdef TCONFIG_SNMP
2113 stop_snmp();
2114 #endif
2117 #ifdef TCONFIG_IPV6
2118 stop_radvd();
2119 #endif
2120 #ifdef TCONFIG_PPTPD
2121 stop_pptpd();
2122 #endif
2123 stop_sched();
2124 stop_rstats();
2125 stop_cstats();
2126 // stop_upnp();
2127 stop_cron();
2128 stop_httpd();
2129 stop_cifs();
2130 stop_dnsmasq();
2131 stop_zebra();
2132 stop_nas();
2133 // stop_syslog();
2136 // -----------------------------------------------------------------------------
2138 /* nvram "action_service" is: "service-action[-modifier]"
2139 * action is something like "stop" or "start" or "restart"
2140 * optional modifier is "c" for the "service" command-line command
2142 void exec_service(void)
2144 const int A_START = 1;
2145 const int A_STOP = 2;
2146 const int A_RESTART = 1|2;
2147 char buffer[128];
2148 char *service;
2149 char *act;
2150 char *next;
2151 char *modifier;
2152 int action, user;
2153 int i;
2155 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2156 next = buffer;
2158 TOP:
2159 act = strsep(&next, ",");
2160 service = strsep(&act, "-");
2161 if (act == NULL) {
2162 next = NULL;
2163 goto CLEAR;
2165 modifier = act;
2166 strsep(&modifier, "-");
2168 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2170 if (strcmp(act, "start") == 0) action = A_START;
2171 else if (strcmp(act, "stop") == 0) action = A_STOP;
2172 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2173 else action = 0;
2174 user = (modifier != NULL && *modifier == 'c');
2176 if (strcmp(service, "dhcpc") == 0) {
2177 if (action & A_STOP) stop_dhcpc();
2178 if (action & A_START) start_dhcpc();
2179 goto CLEAR;
2182 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2183 if (action & A_STOP) stop_dnsmasq();
2184 if (action & A_START) {
2185 dns_to_resolv();
2186 start_dnsmasq();
2188 goto CLEAR;
2191 if (strcmp(service, "firewall") == 0) {
2192 if (action & A_STOP) {
2193 stop_firewall();
2194 stop_igmp_proxy();
2195 stop_udpxy();
2197 if (action & A_START) {
2198 start_firewall();
2199 start_igmp_proxy();
2200 start_udpxy();
2202 goto CLEAR;
2205 if (strcmp(service, "restrict") == 0) {
2206 if (action & A_STOP) {
2207 stop_firewall();
2209 if (action & A_START) {
2210 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2212 start_firewall();
2214 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2215 if (i == 1) {
2216 if (nvram_get_int("rrules_radio") < 0) {
2217 eval("radio", "on");
2221 goto CLEAR;
2224 if (strcmp(service, "arpbind") == 0) {
2225 if (action & A_STOP) stop_arpbind();
2226 if (action & A_START) start_arpbind();
2227 goto CLEAR;
2230 if (strcmp(service, "qos") == 0) {
2231 if (action & A_STOP) {
2232 stop_qos();
2234 stop_firewall(); start_firewall(); // always restarted
2235 if (action & A_START) {
2236 start_qos();
2237 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2239 goto CLEAR;
2242 if (strcmp(service, "qoslimit") == 0) {
2243 if (action & A_STOP) {
2244 stop_qoslimit();
2246 stop_firewall(); start_firewall(); // always restarted
2247 if (action & A_START) {
2248 start_qoslimit();
2250 goto CLEAR;
2253 if (strcmp(service, "upnp") == 0) {
2254 if (action & A_STOP) {
2255 stop_upnp();
2257 stop_firewall(); start_firewall(); // always restarted
2258 if (action & A_START) {
2259 start_upnp();
2261 goto CLEAR;
2264 if (strcmp(service, "telnetd") == 0) {
2265 if (action & A_STOP) stop_telnetd();
2266 if (action & A_START) start_telnetd();
2267 goto CLEAR;
2270 if (strcmp(service, "sshd") == 0) {
2271 if (action & A_STOP) stop_sshd();
2272 if (action & A_START) start_sshd();
2273 goto CLEAR;
2276 if (strcmp(service, "httpd") == 0) {
2277 if (action & A_STOP) stop_httpd();
2278 if (action & A_START) start_httpd();
2279 goto CLEAR;
2282 #ifdef TCONFIG_IPV6
2283 if (strcmp(service, "ipv6") == 0) {
2284 if (action & A_STOP) {
2285 stop_radvd();
2286 stop_ipv6();
2288 if (action & A_START) {
2289 start_ipv6();
2290 start_radvd();
2292 goto CLEAR;
2295 if (strcmp(service, "radvd") == 0) {
2296 if (action & A_STOP) {
2297 stop_radvd();
2299 if (action & A_START) {
2300 start_radvd();
2302 goto CLEAR;
2305 if (strncmp(service, "dhcp6", 5) == 0) {
2306 if (action & A_STOP) {
2307 stop_dhcp6c();
2309 if (action & A_START) {
2310 start_dhcp6c();
2312 goto CLEAR;
2314 #endif
2316 if (strcmp(service, "admin") == 0) {
2317 if (action & A_STOP) {
2318 stop_sshd();
2319 stop_telnetd();
2320 stop_httpd();
2322 stop_firewall(); start_firewall(); // always restarted
2323 if (action & A_START) {
2324 start_httpd();
2325 create_passwd();
2326 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2327 if (nvram_match("sshd_eas", "1")) start_sshd();
2329 goto CLEAR;
2332 if (strcmp(service, "ddns") == 0) {
2333 if (action & A_STOP) stop_ddns();
2334 if (action & A_START) start_ddns();
2335 goto CLEAR;
2338 if (strcmp(service, "ntpc") == 0) {
2339 if (action & A_STOP) stop_ntpc();
2340 if (action & A_START) start_ntpc();
2341 goto CLEAR;
2344 if (strcmp(service, "logging") == 0) {
2345 if (action & A_STOP) {
2346 stop_syslog();
2348 if (action & A_START) {
2349 start_syslog();
2351 if (!user) {
2352 // always restarted except from "service" command
2353 stop_cron(); start_cron();
2354 stop_firewall(); start_firewall();
2356 goto CLEAR;
2359 if (strcmp(service, "crond") == 0) {
2360 if (action & A_STOP) {
2361 stop_cron();
2363 if (action & A_START) {
2364 start_cron();
2366 goto CLEAR;
2369 #ifdef LINUX26
2370 if (strncmp(service, "hotplug", 7) == 0) {
2371 if (action & A_STOP) {
2372 stop_hotplug2();
2374 if (action & A_START) {
2375 start_hotplug2(1);
2377 goto CLEAR;
2379 #endif
2381 if (strcmp(service, "upgrade") == 0) {
2382 if (action & A_START) {
2383 #if TOMATO_SL
2384 stop_usbevent();
2385 stop_smbd();
2386 #endif
2387 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2388 stop_jffs2();
2389 // stop_cifs();
2390 stop_zebra();
2391 stop_cron();
2392 stop_ntpc();
2393 stop_upnp();
2394 // stop_dhcpc();
2395 killall("rstats", SIGTERM);
2396 killall("cstats", SIGTERM);
2397 killall("buttons", SIGTERM);
2398 stop_syslog();
2399 remove_storage_main(1); // !!TB - USB Support
2400 stop_usb(); // !!TB - USB Support
2402 goto CLEAR;
2405 #ifdef TCONFIG_CIFS
2406 if (strcmp(service, "cifs") == 0) {
2407 if (action & A_STOP) stop_cifs();
2408 if (action & A_START) start_cifs();
2409 goto CLEAR;
2411 #endif
2413 #ifdef TCONFIG_JFFS2
2414 if (strncmp(service, "jffs", 4) == 0) {
2415 if (action & A_STOP) stop_jffs2();
2416 if (action & A_START) start_jffs2();
2417 goto CLEAR;
2419 #endif
2421 if (strcmp(service, "zebra") == 0) {
2422 if (action & A_STOP) stop_zebra();
2423 if (action & A_START) start_zebra();
2424 goto CLEAR;
2427 if (strcmp(service, "routing") == 0) {
2428 if (action & A_STOP) {
2429 stop_zebra();
2430 do_static_routes(0); // remove old '_saved'
2431 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2432 #ifdef TCONFIG_VLAN
2433 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2434 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2435 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2436 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2437 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2438 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2439 #endif
2441 stop_firewall();
2442 start_firewall();
2443 if (action & A_START) {
2444 do_static_routes(1); // add new
2445 start_zebra();
2446 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2447 #ifdef TCONFIG_VLAN
2448 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2449 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2450 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2451 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2452 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2453 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2454 #endif
2456 goto CLEAR;
2459 if (strcmp(service, "ctnf") == 0) {
2460 if (action & A_START) {
2461 setup_conntrack();
2462 stop_firewall();
2463 start_firewall();
2465 goto CLEAR;
2468 if (strcmp(service, "wan") == 0) {
2469 if (action & A_STOP) {
2470 stop_wan();
2473 if (action & A_START) {
2474 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2475 start_wan(BOOT);
2476 sleep(2);
2477 force_to_dial();
2479 goto CLEAR;
2482 if (strcmp(service, "net") == 0) {
2483 if (action & A_STOP) {
2484 #ifdef TCONFIG_USB
2485 stop_nas_services();
2486 #endif
2487 #ifdef TCONFIG_IPV6
2488 stop_radvd();
2489 #endif
2490 stop_httpd();
2491 stop_dnsmasq();
2492 stop_nas();
2493 stop_wan();
2494 stop_arpbind();
2495 stop_lan();
2496 stop_vlan();
2498 if (action & A_START) {
2499 start_vlan();
2500 start_lan();
2501 start_wan(BOOT);
2502 start_arpbind();
2503 start_nas();
2504 start_dnsmasq();
2505 start_httpd();
2506 #ifdef TCONFIG_IPV6
2507 start_radvd();
2508 #endif
2509 start_wl();
2510 #ifdef TCONFIG_USB
2511 start_nas_services();
2512 #endif
2514 goto CLEAR;
2517 if (strcmp(service, "nas") == 0) {
2518 if (action & A_STOP) {
2519 stop_nas();
2521 if (action & A_START) {
2522 start_nas();
2523 start_wl();
2525 goto CLEAR;
2528 if (strcmp(service, "rstats") == 0) {
2529 if (action & A_STOP) stop_rstats();
2530 if (action & A_START) start_rstats(0);
2531 goto CLEAR;
2534 if (strcmp(service, "rstatsnew") == 0) {
2535 if (action & A_STOP) stop_rstats();
2536 if (action & A_START) start_rstats(1);
2537 goto CLEAR;
2540 if (strcmp(service, "cstats") == 0) {
2541 if (action & A_STOP) stop_cstats();
2542 if (action & A_START) start_cstats(0);
2543 goto CLEAR;
2546 if (strcmp(service, "cstatsnew") == 0) {
2547 if (action & A_STOP) stop_cstats();
2548 if (action & A_START) start_cstats(1);
2549 goto CLEAR;
2552 if (strcmp(service, "sched") == 0) {
2553 if (action & A_STOP) stop_sched();
2554 if (action & A_START) start_sched();
2555 goto CLEAR;
2559 #ifdef TCONFIG_SNMP
2560 if (strcmp(service, "snmp") == 0) {
2561 if (action & A_STOP) stop_snmp();
2562 if (action & A_START) start_snmp();
2563 goto CLEAR;
2565 #endif
2568 #ifdef TCONFIG_USB
2569 // !!TB - USB Support
2570 if (strcmp(service, "usb") == 0) {
2571 if (action & A_STOP) stop_usb();
2572 if (action & A_START) {
2573 start_usb();
2574 // restart Samba and ftp since they may be killed by stop_usb()
2575 restart_nas_services(0, 1);
2576 // remount all partitions by simulating hotplug event
2577 add_remove_usbhost("-1", 1);
2579 goto CLEAR;
2582 if (strcmp(service, "usbapps") == 0) {
2583 if (action & A_STOP) stop_nas_services();
2584 if (action & A_START) start_nas_services();
2585 goto CLEAR;
2587 #endif
2589 #ifdef TCONFIG_FTP
2590 // !!TB - FTP Server
2591 if (strcmp(service, "ftpd") == 0) {
2592 if (action & A_STOP) stop_ftpd();
2593 setup_conntrack();
2594 stop_firewall();
2595 start_firewall();
2596 if (action & A_START) start_ftpd();
2597 goto CLEAR;
2599 #endif
2601 #ifdef TCONFIG_MEDIA_SERVER
2602 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2603 if (action & A_STOP) stop_media_server();
2604 if (action & A_START) start_media_server();
2605 goto CLEAR;
2607 #endif
2609 #ifdef TCONFIG_SAMBASRV
2610 // !!TB - Samba
2611 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2612 if (action & A_STOP) stop_samba();
2613 if (action & A_START) {
2614 create_passwd();
2615 stop_dnsmasq();
2616 start_dnsmasq();
2617 start_samba();
2619 goto CLEAR;
2621 #endif
2623 #ifdef TCONFIG_OPENVPN
2624 if (strncmp(service, "vpnclient", 9) == 0) {
2625 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2626 if (action & A_START) start_vpnclient(atoi(&service[9]));
2627 goto CLEAR;
2630 if (strncmp(service, "vpnserver", 9) == 0) {
2631 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2632 if (action & A_START) start_vpnserver(atoi(&service[9]));
2633 goto CLEAR;
2635 #endif
2637 #ifdef TCONFIG_NOCAT
2638 if (strcmp(service, "splashd") == 0) {
2639 if (action & A_STOP) stop_splashd();
2640 if (action & A_START) start_splashd();
2641 goto CLEAR;
2643 #endif
2645 #ifdef TCONFIG_PPTPD
2646 if (strcmp(service, "pptpd") == 0) {
2647 if (action & A_STOP) stop_pptpd();
2648 if (action & A_START) start_pptpd();
2649 goto CLEAR;
2651 #endif
2653 #ifdef TCONFIG_USERPPTP
2654 if (strcmp(service, "pptpclient") == 0) {
2655 if (action & A_STOP) stop_pptp_client();
2656 if (action & A_START) start_pptp_client();
2657 if (action & (A_START | A_STOP))
2659 stop_dnsmasq();
2660 dns_to_resolv();
2661 start_dnsmasq();
2662 if ((action & A_START) == 0)
2663 clear_pptp_route();
2665 goto CLEAR;
2667 #endif
2669 CLEAR:
2670 if (next) goto TOP;
2672 // some functions check action_service and must be cleared at end -- zzz
2673 nvram_set("action_service", "");
2675 // Force recheck in 500 msec
2676 setitimer(ITIMER_REAL, &pop_tv, NULL);
2679 static void do_service(const char *name, const char *action, int user)
2681 int n;
2682 char s[64];
2684 n = 150;
2685 while (!nvram_match("action_service", "")) {
2686 if (user) {
2687 putchar('*');
2688 fflush(stdout);
2690 else if (--n < 0) break;
2691 usleep(100 * 1000);
2694 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2695 nvram_set("action_service", s);
2697 if (nvram_match("debug_rc_svc", "1")) {
2698 nvram_unset("debug_rc_svc");
2699 exec_service();
2700 } else {
2701 kill(1, SIGUSR1);
2704 n = 150;
2705 while (nvram_match("action_service", s)) {
2706 if (user) {
2707 putchar('.');
2708 fflush(stdout);
2710 else if (--n < 0) {
2711 break;
2713 usleep(100 * 1000);
2717 int service_main(int argc, char *argv[])
2719 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2720 do_service(argv[1], argv[2], 1);
2721 printf("\nDone.\n");
2722 return 0;
2725 void start_service(const char *name)
2727 do_service(name, "start", 0);
2730 void stop_service(const char *name)
2732 do_service(name, "stop", 0);
2736 void restart_service(const char *name)
2738 do_service(name, "restart", 0);