VPN GUI backend
[tomato.git] / release / src / router / rc / services.c
blobc4f9050f846bcb62082f0ab84a0203bc975cf9d0
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-2008 Jonathan Zarate
37 #include "rc.h"
39 #include <arpa/inet.h>
40 #include <time.h>
41 #include <sys/time.h>
42 #include <errno.h>
44 #define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
45 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
49 // -----------------------------------------------------------------------------
51 static const char dmhosts[] = "/etc/hosts.dnsmasq";
52 static const char dmresolv[] = "/etc/resolv.dnsmasq";
53 static const char dmpid[] = "/var/run/dnsmasq.pid";
55 static pid_t pid_dnsmasq = -1;
58 void start_dnsmasq()
60 FILE *f;
61 const char *nv;
62 char buf[512];
63 char lan[24];
64 const char *router_ip;
65 const char *lan_ifname;
66 char sdhcp_lease[32];
67 char *e;
68 int n;
69 char *mac, *ip, *name;
70 char *p;
71 int ipn;
72 char ipbuf[32];
73 FILE *hf;
74 int dhcp_start;
75 int dhcp_count;
76 int dhcp_lease;
77 int do_dhcpd;
78 int do_dns;
80 if (getpid() != 1) {
81 start_service("dnsmasq");
82 return;
85 stop_dnsmasq();
87 if (nvram_match("wl_mode", "wet")) return;
88 /*
89 if (nvram_get_int("dnsmasq_norw")) {
90 if (f_exists("/etc/dnsmasq.conf")) {
91 syslog(LOG_INFO, "%s exists. Not rewriting.", "/etc/dnsmasq.conf");
92 goto RUN;
96 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
98 lan_ifname = nvram_safe_get("lan_ifname");
99 router_ip = nvram_safe_get("lan_ipaddr");
100 strlcpy(lan, router_ip, sizeof(lan));
101 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
103 fprintf(f,
104 "pid-file=%s\n"
105 "interface=%s\n",
106 dmpid, lan_ifname);
107 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
108 if (*nv) fprintf(f, "domain=%s\n", nv);
111 // dns
112 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
113 else n = 4096;
114 fprintf(f,
115 "resolv-file=%s\n" // the real stuff is here
116 "addn-hosts=%s\n" // "
117 "expand-hosts\n" // expand hostnames in hosts file
118 "min-port=%u\n", // min port used for random src port
119 dmresolv, dmhosts, n);
120 do_dns = nvram_match("dhcpd_dmdns", "1");
123 // dhcp
124 do_dhcpd = nvram_match("lan_proto", "dhcp");
125 if (do_dhcpd) {
126 dhcp_lease = nvram_get_int("dhcp_lease");
127 if (dhcp_lease <= 0) dhcp_lease = 1440;
129 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
130 if (n < 0) strcpy(sdhcp_lease, "infinite");
131 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
133 if (!do_dns) {
134 // if not using dnsmasq for dns
136 const dns_list_t *dns = get_dns(); // this always points to a static buffer
137 if ((dns->count == 0) && (nvram_match("dhcpd_llndns", "1"))) {
138 // no DNS might be temporary. use a low lease time to force clients to update.
139 dhcp_lease = 2;
140 strcpy(sdhcp_lease, "2m");
141 do_dns = 1;
143 else {
144 // pass the dns directly
145 buf[0] = 0;
146 for (n = 0 ; n < dns->count; ++n) {
147 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n]));
149 fprintf(f, "dhcp-option=6%s\n", buf);
153 dhcp_start = nvram_get_int("dhcp_start");
154 dhcp_count = nvram_get_int("dhcp_num");
155 fprintf(f,
156 "dhcp-range=%s%d,%s%d,%s,%dm\n" // lease config
157 "dhcp-option=3,%s\n" // gateway
158 "dhcp-authoritative\n", // this is the authoritative server - handle all requests
159 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease,
160 router_ip);
162 if (check_wanup()) {
163 // avoid leasing wan ip incase the modem gives an ip in our range
164 fprintf(f, "dhcp-host=01:02:03:04:05:06,%s\n", nvram_safe_get("wan_ipaddr"));
167 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
168 fprintf(f, "dhcp-option=44,%s\n", nv);
171 else {
172 fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
175 // write static lease entries & create hosts file
177 if ((hf = fopen(dmhosts, "w")) != NULL) {
178 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
179 fprintf(hf, "%s %s\n", router_ip, nv);
182 p = nvram_safe_get("dhcpd_static"); // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
183 while ((e = strchr(p, '>')) != NULL) {
184 n = (e - p);
185 if (n > 52) {
186 p = e + 1;
187 continue;
190 strncpy(buf, p, n);
191 buf[n] = 0;
192 p = e + 1;
194 if ((e = strchr(buf, '<')) == NULL) continue;
195 *e = 0;
196 mac = buf;
198 ip = e + 1;
199 if ((e = strchr(ip, '<')) == NULL) continue;
200 *e = 0;
201 if (strchr(ip, '.') == NULL) {
202 ipn = atoi(ip);
203 if ((ipn <= 0) || (ipn > 255)) continue;
204 sprintf(ipbuf, "%s%d", lan, ipn);
205 ip = ipbuf;
207 else {
208 if (inet_addr(ip) == INADDR_NONE) continue;
211 name = e + 1;
213 if ((hf) && (*name != 0)) {
214 fprintf(hf, "%s %s\n", ip, name);
217 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
218 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
222 if (hf) fclose(hf);
226 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
230 FILE *af;
232 fflush(f);
233 if ((af = fopen("/etc/dnsmasq.custom", "r")) != NULL) {
234 while ((n = fread(buf, 1, sizeof(buf), af)) > 0) {
235 fwrite(buf, 1, n, f);
237 fclose(af);
242 fclose(f);
244 if (do_dns) {
245 unlink("/etc/resolv.conf");
246 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
249 // RUN:
250 eval("dnsmasq");
252 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
253 f_read_string(dmpid, buf, sizeof(buf));
254 pid_dnsmasq = atol(buf);
258 void stop_dnsmasq(void)
260 if (getpid() != 1) {
261 stop_service("dnsmasq");
262 return;
265 pid_dnsmasq = -1;
267 unlink("/etc/resolv.conf");
268 symlink(dmresolv, "/etc/resolv.conf");
270 killall_tk("dnsmasq");
273 void clear_resolv(void)
275 _dprintf("%s\n", __FUNCTION__);
277 f_write(dmresolv, NULL, 0, 0, 0); // blank
280 void dns_to_resolv(void)
282 FILE *f;
283 const dns_list_t *dns;
284 int i;
285 mode_t m;
287 _dprintf("%s\n", __FUNCTION__);
289 m = umask(022); // 077 from pppoecd
290 if ((f = fopen(dmresolv, "w")) != NULL) {
291 dns = get_dns(); // static buffer
292 if (dns->count == 0) {
293 // Put a pseudo DNS IP to trigger Connect On Demand
294 if ((nvram_match("ppp_demand", "1")) &&
295 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
296 fprintf(f, "nameserver 1.1.1.1\n");
299 else {
300 for (i = 0; i < dns->count; i++) {
301 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i]));
304 fclose(f);
306 umask(m);
309 // -----------------------------------------------------------------------------
311 void start_httpd(void)
313 chdir("/www");
314 if (!nvram_match("http_enable", "0")) {
315 xstart("httpd");
317 if (!nvram_match("https_enable", "0")) {
318 xstart("httpd", "-s");
320 chdir("/");
323 void stop_httpd(void)
325 killall_tk("httpd");
328 // -----------------------------------------------------------------------------
330 void start_upnp(void)
332 if ((nvram_match("upnp_enable", "1")) && (get_wan_proto() != WP_DISABLED)) {
333 #ifdef TEST_MINIUPNP
334 xstart("miniupnpd",
335 "-i", nvram_safe_get("wan_iface"),
336 "-a", nvram_safe_get("lan_ipaddr"),
337 "-p", "5555",
338 "-U");
339 #else
340 xstart("upnp",
341 "-D",
342 "-L", nvram_safe_get("lan_ifname"),
343 "-W", nvram_safe_get("wan_iface"),
344 "-I", nvram_safe_get("upnp_ssdp_interval"),
345 "-A", nvram_safe_get("upnp_max_age"));
346 #endif
350 void stop_upnp(void)
352 #ifdef TEST_MINIUPNP
353 killall_tk("miniupnpd");
354 #else
355 killall_tk("upnp");
356 #endif
359 // -----------------------------------------------------------------------------
361 static pid_t pid_crond = -1;
363 void start_cron(void)
365 _dprintf("%s\n", __FUNCTION__);
367 stop_cron();
369 char *argv[] = { "crond", "-l", "9", NULL };
371 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
372 _eval(argv, NULL, 0, NULL);
374 if (!nvram_contains_word("debug_norestart", "crond")) {
375 pid_crond = -2; // intentionally fail test_cron()
380 void stop_cron(void)
382 _dprintf("%s\n", __FUNCTION__);
384 pid_crond = -1;
385 killall_tk("crond");
388 // -----------------------------------------------------------------------------
390 // Written by Sparq in 2002/07/16
391 void start_zebra(void)
393 #ifdef TCONFIG_ZEBRA
394 FILE *fp;
396 char *lan_tx = nvram_safe_get("dr_lan_tx");
397 char *lan_rx = nvram_safe_get("dr_lan_rx");
398 char *wan_tx = nvram_safe_get("dr_wan_tx");
399 char *wan_rx = nvram_safe_get("dr_wan_rx");
401 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
402 return;
405 // empty
406 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
407 fclose(fp);
411 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
412 char *lan_ifname = nvram_safe_get("lan_ifname");
413 char *wan_ifname = nvram_safe_get("wan_ifname");
415 fprintf(fp, "router rip\n");
416 fprintf(fp, "network %s\n", lan_ifname);
417 fprintf(fp, "network %s\n", wan_ifname);
418 fprintf(fp, "redistribute connected\n");
419 //fprintf(fp, "redistribute static\n");
421 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
422 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
424 fprintf(fp, "interface %s\n", lan_ifname);
425 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
426 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
428 fprintf(fp, "interface %s\n", wan_ifname);
429 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
430 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
432 fprintf(fp, "router rip\n");
433 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
434 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
435 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
436 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
437 fprintf(fp, "access-list private deny any\n");
439 //fprintf(fp, "debug rip events\n");
440 //fprintf(fp, "log file /etc/ripd.log\n");
441 fclose(fp);
444 xstart("zebra", "-d", "-f", "/etc/zebra.conf");
445 xstart("ripd", "-d", "-f", "/etc/ripd.conf");
446 #endif
449 void stop_zebra(void)
451 #ifdef TCONFIG_ZEBRA
452 killall("zebra", SIGTERM);
453 killall("ripd", SIGTERM);
455 unlink("/etc/zebra.conf");
456 unlink("/etc/ripd.conf");
457 #endif
460 // -----------------------------------------------------------------------------
462 void start_syslog(void)
464 char *argv[12];
465 int argc;
466 char *nv;
467 char rem[256];
469 argv[0] = "syslogd";
470 argv[1] = "-m";
471 argv[2] = nvram_get("log_mark");
472 argc = 3;
474 if (nvram_match("log_remote", "1")) {
475 nv = nvram_safe_get("log_remoteip");
476 if (*nv) {
477 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
478 argv[argc++] = "-R";
479 argv[argc++] = rem;
483 if (nvram_match("log_file", "1")) {
484 argv[argc++] = "-L";
485 argv[argc++] = "-s";
486 argv[argc++] = "50";
489 if (argc > 3) {
490 argv[argc] = NULL;
491 _eval(argv, NULL, 0, NULL);
492 usleep(500000);
494 argv[0] = "klogd";
495 argv[1] = NULL;
496 _eval(argv, NULL, 0, NULL);
497 usleep(500000);
501 void stop_syslog(void)
503 killall("klogd", SIGTERM);
504 killall("syslogd", SIGTERM);
507 // -----------------------------------------------------------------------------
509 static pid_t pid_igmp = -1;
511 void start_igmp_proxy(void)
513 char *p;
515 pid_igmp = -1;
516 if (nvram_match("multicast_pass", "1")) {
517 switch (get_wan_proto()) {
518 case WP_PPPOE:
519 case WP_PPTP:
520 case WP_L2TP:
521 p = "wan_iface";
522 break;
523 default:
524 p = "wan_ifname";
525 break;
527 xstart("igmprt", "-f", "-i", nvram_safe_get(p));
529 if (!nvram_contains_word("debug_norestart", "igmprt")) {
530 pid_igmp = -2;
535 void stop_igmp_proxy(void)
537 pid_igmp = -1;
538 killall("igmprt", SIGTERM);
542 // -----------------------------------------------------------------------------
544 void set_tz(void)
546 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
549 void start_ntpc(void)
551 set_tz();
553 stop_ntpc();
555 if (nvram_get_int("ntp_updates") >= 0) {
556 xstart("ntpsync", "--init");
560 void stop_ntpc(void)
562 killall("ntpsync", SIGTERM);
565 // -----------------------------------------------------------------------------
567 static void stop_rstats(void)
569 int n;
570 int pid;
572 n = 60;
573 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
574 if (kill(pid, SIGTERM) != 0) break;
575 sleep(1);
579 static void start_rstats(int new)
581 if (nvram_match("rstats_enable", "1")) {
582 stop_rstats();
583 if (new) xstart("rstats", "--new");
584 else xstart("rstats");
588 // -----------------------------------------------------------------------------
590 static void _check(pid_t *pid, const char *name, void (*func)(void) )
592 if (*pid != -1) {
593 if (kill(*pid, 0) != 0) {
594 if ((*pid = pidof(name)) == -1) func();
599 void check_services(void)
601 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
602 _check(&pid_crond, "crond", start_cron);
603 _check(&pid_igmp, "igmprt", start_igmp_proxy);
606 // -----------------------------------------------------------------------------
608 void start_services(void)
610 static int once = 1;
612 if (once) {
613 once = 0;
615 create_passwd();
616 if (nvram_match("telnetd_eas", "1")) start_telnetd();
617 if (nvram_match("sshd_eas", "1")) start_sshd();
620 start_syslog();
621 #if TOMATO_SL
622 start_usbevent();
623 #endif
624 start_nas();
625 start_zebra();
626 start_dnsmasq();
627 start_cifs();
628 start_httpd();
629 start_cron();
630 start_upnp();
631 start_rstats(0);
632 start_sched();
633 #ifdef TCONFIG_SAMBA
634 start_smbd();
635 #endif
638 void stop_services(void)
640 clear_resolv();
642 #ifdef TCONFIG_SAMBA
643 stop_smbd();
644 #endif
645 stop_sched();
646 stop_rstats();
647 stop_upnp();
648 stop_cron();
649 stop_httpd();
650 stop_cifs();
651 stop_dnsmasq();
652 stop_zebra();
653 stop_nas();
654 #if TOMATO_SL
655 stop_usbevent();
656 #endif
657 stop_syslog();
660 // -----------------------------------------------------------------------------
662 void exec_service(void)
664 const int A_START = 1;
665 const int A_STOP = 2;
666 const int A_RESTART = 1|2;
667 char buffer[128];
668 char *service;
669 char *act;
670 char *next;
671 int action;
672 int i;
674 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
675 next = buffer;
677 TOP:
678 act = strsep(&next, ",");
679 service = strsep(&act, "-");
680 if (act == NULL) {
681 next = NULL;
682 goto CLEAR;
685 if (strcmp(act, "start") == 0) action = A_START;
686 else if (strcmp(act, "stop") == 0) action = A_STOP;
687 else if (strcmp(act, "restart") == 0) action = A_RESTART;
688 else action = 0;
690 _dprintf("%s %s service=%s action=%s\n", __FILE__, __FUNCTION__, service, act);
693 if (strcmp(service, "dhcpc") == 0) {
694 if (action & A_STOP) stop_dhcpc();
695 if (action & A_START) start_dhcpc();
696 goto CLEAR;
699 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
700 if (action & A_STOP) stop_dnsmasq();
701 if (action & A_START) {
702 dns_to_resolv();
703 start_dnsmasq();
705 goto CLEAR;
708 if (strcmp(service, "firewall") == 0) {
709 if (action & A_STOP) {
710 stop_firewall();
711 stop_igmp_proxy();
713 if (action & A_START) {
714 start_firewall();
715 start_igmp_proxy();
717 goto CLEAR;
720 if (strcmp(service, "restrict") == 0) {
721 if (action & A_STOP) {
722 stop_firewall();
724 if (action & A_START) {
725 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
727 start_firewall();
729 // if radio was disabled by access restriction, but no rule is handling it now, enable it
730 if (i == 1) {
731 if (nvram_get_int("rrules_radio") < 0) {
732 if (!get_radio()) eval("radio", "on");
736 goto CLEAR;
739 if (strcmp(service, "qos") == 0) {
740 if (action & A_STOP) {
741 stop_qos();
743 stop_firewall(); start_firewall(); // always restarted
744 if (action & A_START) {
745 start_qos();
746 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
748 goto CLEAR;
751 if (strcmp(service, "upnp") == 0) {
752 if (action & A_STOP) {
753 stop_upnp();
755 stop_firewall(); start_firewall(); // always restarted
756 if (action & A_START) {
757 start_upnp();
759 goto CLEAR;
762 if (strcmp(service, "telnetd") == 0) {
763 if (action & A_STOP) stop_telnetd();
764 if (action & A_START) start_telnetd();
765 goto CLEAR;
768 if (strcmp(service, "sshd") == 0) {
769 if (action & A_STOP) stop_sshd();
770 if (action & A_START) start_sshd();
771 goto CLEAR;
774 if (strcmp(service, "admin") == 0) {
775 if (action & A_STOP) {
776 stop_sshd();
777 stop_telnetd();
778 stop_httpd();
780 stop_firewall(); start_firewall(); // always restarted
781 if (action & A_START) {
782 start_httpd();
783 create_passwd();
784 if (nvram_match("telnetd_eas", "1")) start_telnetd();
785 if (nvram_match("sshd_eas", "1")) start_sshd();
787 goto CLEAR;
790 if (strcmp(service, "ddns") == 0) {
791 if (action & A_STOP) stop_ddns();
792 if (action & A_START) start_ddns();
793 goto CLEAR;
796 if (strcmp(service, "ntpc") == 0) {
797 if (action & A_STOP) stop_ntpc();
798 if (action & A_START) start_ntpc();
799 goto CLEAR;
802 if (strcmp(service, "logging") == 0) {
803 if (action & A_STOP) {
804 stop_syslog();
805 stop_cron();
807 stop_firewall(); start_firewall(); // always restarted
808 if (action & A_START) {
809 start_cron();
810 start_syslog();
812 goto CLEAR;
815 if (strcmp(service, "crond") == 0) {
816 if (action & A_STOP) {
817 stop_cron();
819 if (action & A_START) {
820 start_cron();
822 goto CLEAR;
825 if (strcmp(service, "upgrade") == 0) {
826 if (action & A_START) {
827 #if TOMATO_SL
828 stop_usbevent();
829 stop_smbd();
830 #endif
831 stop_jffs2();
832 // stop_cifs();
833 stop_zebra();
834 stop_cron();
835 stop_ntpc();
836 stop_upnp();
837 // stop_dhcpc();
838 killall("rstats", SIGTERM);
839 killall("buttons", SIGTERM);
840 stop_syslog();
842 goto CLEAR;
845 #ifdef TCONFIG_CIFS
846 if (strcmp(service, "cifs") == 0) {
847 if (action & A_STOP) stop_cifs();
848 if (action & A_START) start_cifs();
849 goto CLEAR;
851 #endif
853 #ifdef TCONFIG_JFFS2
854 if (strcmp(service, "jffs2") == 0) {
855 if (action & A_STOP) stop_jffs2();
856 if (action & A_START) start_jffs2();
857 goto CLEAR;
859 #endif
861 if (strcmp(service, "routing") == 0) {
862 if (action & A_STOP) {
863 stop_zebra();
864 do_static_routes(0); // remove old '_saved'
865 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
867 stop_firewall();
868 start_firewall();
869 if (action & A_START) {
870 do_static_routes(1); // add new
871 start_zebra();
872 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
874 goto CLEAR;
877 if (strcmp(service, "ctnf") == 0) {
878 if (action & A_START) {
879 setup_conntrack();
880 stop_firewall();
881 start_firewall();
883 goto CLEAR;
886 if (strcmp(service, "wan") == 0) {
887 if (action & A_STOP) {
888 if (get_wan_proto() == WP_PPPOE) {
889 stop_dnsmasq();
890 stop_redial();
891 stop_singe_pppoe(PPPOE0);
892 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
893 sleep(1);
894 start_pppoe(PPPOE0);
896 start_dnsmasq();
898 else {
899 stop_wan();
903 if (action & A_START) {
904 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
906 if (get_wan_proto() == WP_PPPOE) {
907 stop_singe_pppoe(PPPOE0);
908 start_pppoe(PPPOE0);
909 if (nvram_invmatch("ppp_demand", "1")) {
910 start_redial();
913 else {
914 start_wan(BOOT);
916 sleep(2);
917 force_to_dial();
919 goto CLEAR;
922 if (strcmp(service, "net") == 0) {
923 if (action & A_STOP) {
924 stop_wan();
925 stop_lan();
926 stop_vlan();
928 if (action & A_START) {
929 start_vlan();
930 start_lan();
931 start_wan(BOOT);
933 goto CLEAR;
936 if (strcmp(service, "rstats") == 0) {
937 if (action & A_STOP) stop_rstats();
938 if (action & A_START) start_rstats(0);
939 goto CLEAR;
942 if (strcmp(service, "rstatsnew") == 0) {
943 if (action & A_STOP) stop_rstats();
944 if (action & A_START) start_rstats(1);
945 goto CLEAR;
948 if (strcmp(service, "sched") == 0) {
949 if (action & A_STOP) stop_sched();
950 if (action & A_START) start_sched();
951 goto CLEAR;
954 #if TOMATO_SL
955 if (strcmp(service, "smbd") == 0) {
956 if (action & A_STOP) stop_smbd();
957 if (action & A_START) start_smbd();
958 goto CLEAR;
961 if (strcmp(service, "test1") == 0) {
962 if (action & A_STOP) stop_test_1();
963 if (action & A_START) start_test_1();
964 goto CLEAR;
966 #endif
968 if (strncmp(service, "vpnclient", 9) == 0) {
969 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
970 if (action & A_START) start_vpnclient(atoi(&service[9]));
971 goto CLEAR;
974 if (strncmp(service, "vpnserver", 9) == 0) {
975 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
976 if (action & A_START) start_vpnserver(atoi(&service[9]));
977 goto CLEAR;
981 CLEAR:
982 if (next) goto TOP;
984 // some functions check action_service and must be cleared at end -- zzz
985 nvram_set("action_service", "");
988 static void do_service(const char *name, const char *action, int user)
990 int n;
991 char s[64];
993 n = 15;
994 while (!nvram_match("action_service", "")) {
995 if (user) {
996 putchar('*');
997 fflush(stdout);
999 else if (--n < 0) break;
1000 sleep(1);
1003 snprintf(s, sizeof(s), "%s-%s", name, action);
1004 nvram_set("action_service", s);
1005 kill(1, SIGUSR1);
1007 n = 15;
1008 while (nvram_match("action_service", s)) {
1009 if (user) {
1010 putchar('.');
1011 fflush(stdout);
1013 else if (--n < 0) break;
1014 sleep(1);
1018 int service_main(int argc, char *argv[])
1020 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1021 do_service(argv[1], argv[2], 1);
1022 printf("\nDone.\n");
1023 return 0;
1026 void start_service(const char *name)
1028 do_service(name, "start", 0);
1031 void stop_service(const char *name)
1033 do_service(name, "stop", 0);