Add new Netgear and Tenda router support
[tomato.git] / release / src / router / rc / rc.c
blob8be73f92176cc0fdd53251e60ff9380a779e99df
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "rc.h"
11 #ifdef DEBUG_RCTEST
12 // used for various testing
13 static int rctest_main(int argc, char *argv[])
15 if (argc < 2) {
16 printf("test what?\n");
18 else {
19 printf("%s\n", argv[1]);
21 if (strcmp(argv[1], "foo") == 0) {
23 else if (strcmp(argv[1], "qos") == 0) {
24 start_qos();
26 #ifdef TCONFIG_IPV6
27 if (strcmp(argv[1], "6rd") == 0) {
28 stop_6rd_tunnel();
29 start_6rd_tunnel();
31 #endif
32 #ifdef DEBUG_IPTFILE
33 else if (strcmp(argv[1], "iptfile") == 0) {
34 create_test_iptfile();
36 #endif
37 else {
38 printf("what?\n");
41 return 0;
43 #endif
46 static int hotplug_main(int argc, char *argv[])
48 if (argc >= 2) {
49 if (strcmp(argv[1], "net") == 0) {
50 hotplug_net();
52 #ifdef TCONFIG_USB
53 // !!TB - USB Support
54 else if (strcmp(argv[1], "usb") == 0) {
55 hotplug_usb();
57 #ifdef LINUX26
58 else if (strcmp(argv[1], "block") == 0) {
59 hotplug_usb();
61 #endif
62 #endif
64 return 0;
67 static int rc_main(int argc, char *argv[])
69 if (argc < 2) return 0;
70 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
71 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
72 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
73 return 0;
78 typedef struct {
79 const char *name;
80 int (*main)(int argc, char *argv[]);
81 } applets_t;
83 static const applets_t applets[] = {
84 { "init", init_main },
85 { "console", console_main },
86 { "rc", rc_main },
87 { "ip-up", ipup_main },
88 { "ip-down", ipdown_main },
89 /* KDB - these functions do nothing why are they here?
90 #ifdef TCONFIG_IPV6
91 { "ipv6-up", ip6up_main },
92 { "ipv6-down", ip6down_main },
93 #endif
95 { "ppp_event", pppevent_main },
96 { "hotplug", hotplug_main },
97 { "redial", redial_main },
98 { "listen", listen_main },
99 { "service", service_main },
100 { "sched", sched_main },
101 { "mtd-write", mtd_write_main },
102 { "mtd-erase", mtd_unlock_erase_main },
103 { "mtd-unlock", mtd_unlock_erase_main },
104 { "buttons", buttons_main },
105 { "blink", blink_main },
106 #ifdef CONFIG_BCMWL6
107 { "blink_5g", blink_5g_main },
108 #endif
109 { "rcheck", rcheck_main },
110 { "dhcpc-event", dhcpc_event_main },
111 { "dhcpc-release", dhcpc_release_main },
112 { "dhcpc-renew", dhcpc_renew_main },
113 #ifdef TCONFIG_IPV6
114 { "dhcp6c-state", dhcp6c_state_main },
115 #endif
116 { "radio", radio_main },
117 { "led", led_main },
118 { "halt", reboothalt_main },
119 { "reboot", reboothalt_main },
120 { "gpio", gpio_main },
121 { "wldist", wldist_main },
122 #ifdef TCONFIG_CIFS
123 { "mount-cifs", mount_cifs_main },
124 #endif
125 #ifdef TCONFIG_DDNS
126 { "ddns-update", ddns_update_main },
127 #endif
128 #ifdef DEBUG_RCTEST
129 { "rctest", rctest_main },
130 #endif
131 {NULL, NULL}
134 int main(int argc, char **argv)
136 char *base;
137 int f;
140 Make sure std* are valid since several functions attempt to close these
141 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
143 if ((f = open("/dev/null", O_RDWR)) < 3) {
144 dup(f);
145 dup(f);
147 else {
148 close(f);
151 base = strrchr(argv[0], '/');
152 base = base ? base + 1 : argv[0];
154 #if 1
155 if (strcmp(base, "rc") == 0) {
156 if (argc < 2) return 1;
157 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
158 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
159 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
160 ++argv;
161 --argc;
162 base = argv[0];
164 #endif
166 #if defined(DEBUG_NOISY)
167 if (nvram_match("debug_logrc", "1")) {
168 int i;
170 cprintf("[rc %d] ", getpid());
171 for (i = 0; i < argc; ++i) {
172 cprintf("%s ", argv[i]);
174 cprintf("\n");
177 #endif
179 #if defined(DEBUG_NOISY)
180 if (nvram_match("debug_ovrc", "1")) {
181 char tmp[256];
182 char *a[32];
184 realpath(argv[0], tmp);
185 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
186 sprintf(tmp, "%s%s", "/tmp/", base);
187 if (f_exists(tmp)) {
188 cprintf("[rc] override: %s\n", tmp);
189 memcpy(a, argv, argc * sizeof(a[0]));
190 a[argc] = 0;
191 a[0] = tmp;
192 execvp(tmp, a);
193 exit(0);
197 #endif
199 const applets_t *a;
200 for (a = applets; a->name; ++a) {
201 if (strcmp(base, a->name) == 0) {
202 openlog(base, LOG_PID, LOG_USER);
203 return a->main(argc, argv);
207 printf("Unknown applet: %s\n", base);
208 return 0;