more advanced Nginx settings in GUI
[tomato.git] / release / src-rt-6.x.4708 / router / rc / rc.c
blob4c1cf615a29bdd490a86e95d8d1f7b41cadde29f
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 { "preinit", init_main },
85 { "init", init_main },
86 { "console", console_main },
87 { "rc", rc_main },
88 { "ip-up", ipup_main },
89 { "ip-down", ipdown_main },
90 /* KDB - these functions do nothing why are they here?
91 #ifdef TCONFIG_IPV6
92 { "ipv6-up", ip6up_main },
93 { "ipv6-down", ip6down_main },
94 #endif
96 { "ppp_event", pppevent_main },
97 { "hotplug", hotplug_main },
98 { "redial", redial_main },
99 { "listen", listen_main },
100 { "service", service_main },
101 { "sched", sched_main },
102 #ifdef TCONFIG_BCMARM
103 { "mtd-write", mtd_write_main_old },
104 { "mtd-erase", mtd_unlock_erase_main_old },
105 { "mtd-unlock", mtd_unlock_erase_main_old },
106 #else
107 { "mtd-write", mtd_write_main },
108 { "mtd-erase", mtd_unlock_erase_main },
109 { "mtd-unlock", mtd_unlock_erase_main },
110 #endif
111 { "buttons", buttons_main },
112 #ifdef CONFIG_BCMWL6
113 { "blink_5g", blink_5g_main },
114 #endif
115 { "rcheck", rcheck_main },
116 { "dhcpc-event", dhcpc_event_main },
117 { "dhcpc-release", dhcpc_release_main },
118 { "dhcpc-renew", dhcpc_renew_main },
119 #ifdef TCONFIG_IPV6
120 { "dhcp6c-state", dhcp6c_state_main },
121 #endif
122 { "radio", radio_main },
123 { "led", led_main },
124 { "halt", reboothalt_main },
125 { "reboot", reboothalt_main },
126 { "gpio", gpio_main },
127 { "wldist", wldist_main },
128 #ifdef TCONFIG_CIFS
129 { "mount-cifs", mount_cifs_main },
130 #endif
131 #ifdef TCONFIG_DDNS
132 { "ddns-update", ddns_update_main },
133 #endif
134 #ifdef DEBUG_RCTEST
135 { "rctest", rctest_main },
136 #endif
137 {NULL, NULL}
140 #ifdef TCONFIG_BCMARM
141 void erase_nvram(void)
143 eval("mtd-erase2", "nvram");
145 #endif
147 int main(int argc, char **argv)
149 char *base;
150 int f;
153 Make sure std* are valid since several functions attempt to close these
154 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
156 if ((f = open("/dev/null", O_RDWR)) < 3) {
157 dup(f);
158 dup(f);
160 else {
161 close(f);
164 base = strrchr(argv[0], '/');
165 base = base ? base + 1 : argv[0];
167 #if 1
168 if (strcmp(base, "rc") == 0) {
169 if (argc < 2) return 1;
170 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
171 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
172 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
173 ++argv;
174 --argc;
175 base = argv[0];
177 #endif
181 #if defined(DEBUG_NOISY)
182 if (nvram_match("debug_logrc", "1")) {
183 int i;
185 cprintf("[rc %d] ", getpid());
186 for (i = 0; i < argc; ++i) {
187 cprintf("%s ", argv[i]);
189 cprintf("\n");
192 #endif
194 #if defined(DEBUG_NOISY)
195 if (nvram_match("debug_ovrc", "1")) {
196 char tmp[256];
197 char *a[32];
199 realpath(argv[0], tmp);
200 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
201 sprintf(tmp, "%s%s", "/tmp/", base);
202 if (f_exists(tmp)) {
203 cprintf("[rc] override: %s\n", tmp);
204 memcpy(a, argv, argc * sizeof(a[0]));
205 a[argc] = 0;
206 a[0] = tmp;
207 execvp(tmp, a);
208 exit(0);
212 #endif
214 const applets_t *a;
215 for (a = applets; a->name; ++a) {
216 if (strcmp(base, a->name) == 0) {
217 openlog(base, LOG_PID, LOG_USER);
218 return a->main(argc, argv);
222 #ifdef TCONFIG_BCMARM
223 if (!strcmp(base, "nvram_erase")){
224 erase_nvram();
225 return 0;
227 /* mtd-erase2 [device] */
228 else if (!strcmp(base, "mtd-erase2")) {
229 if (argv[1] && ((!strcmp(argv[1], "boot")) ||
230 (!strcmp(argv[1], "linux")) ||
231 (!strcmp(argv[1], "linux2")) ||
232 (!strcmp(argv[1], "rootfs")) ||
233 (!strcmp(argv[1], "rootfs2")) ||
234 (!strcmp(argv[1], "nvram")))) {
236 return mtd_erase(argv[1]);
237 } else {
238 fprintf(stderr, "usage: mtd-erase2 [device]\n");
239 return EINVAL;
242 /* mtd-write2 [path] [device] */
243 else if (!strcmp(base, "mtd-write2")) {
244 if (argc >= 3)
245 return mtd_write(argv[1], argv[2]);
246 else {
247 fprintf(stderr, "usage: mtd-write2 [path] [device]\n");
248 return EINVAL;
251 #endif
253 printf("Unknown applet: %s\n", base);
254 return 0;