usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / rc / rc.c
blob9e9f438d6c8e1fcb92f93a29f60a99e376a9a6e9
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 #ifdef TCONFIG_IPV6
90 { "ipv6-up", ip6up_main },
91 { "ipv6-down", ip6down_main },
92 #endif
93 { "ppp_event", pppevent_main },
94 { "hotplug", hotplug_main },
95 { "redial", redial_main },
96 { "listen", listen_main },
97 { "service", service_main },
98 { "sched", sched_main },
99 { "mtd-write", mtd_write_main },
100 { "mtd-erase", mtd_unlock_erase_main },
101 { "mtd-unlock", mtd_unlock_erase_main },
102 { "buttons", buttons_main },
103 { "rcheck", rcheck_main },
104 { "dhcpc-event", dhcpc_event_main },
105 { "dhcpc-release", dhcpc_release_main },
106 { "dhcpc-renew", dhcpc_renew_main },
107 #ifdef TCONFIG_IPV6
108 { "dhcp6c-state", dhcp6c_state_main },
109 #endif
110 { "radio", radio_main },
111 { "led", led_main },
112 { "halt", reboothalt_main },
113 { "reboot", reboothalt_main },
114 { "gpio", gpio_main },
115 { "wldist", wldist_main },
116 #ifdef TCONFIG_CIFS
117 { "mount-cifs", mount_cifs_main },
118 #endif
119 #ifdef TCONFIG_DDNS
120 { "ddns-update", ddns_update_main },
121 #endif
122 #ifdef DEBUG_RCTEST
123 { "rctest", rctest_main },
124 #endif
125 {NULL, NULL}
128 int main(int argc, char **argv)
130 char *base;
131 int f;
134 Make sure std* are valid since several functions attempt to close these
135 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
137 if ((f = open("/dev/null", O_RDWR)) < 3) {
138 dup(f);
139 dup(f);
141 else {
142 close(f);
145 base = strrchr(argv[0], '/');
146 base = base ? base + 1 : argv[0];
148 #if 1
149 if (strcmp(base, "rc") == 0) {
150 if (argc < 2) return 1;
151 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
152 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
153 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
154 ++argv;
155 --argc;
156 base = argv[0];
158 #endif
160 #if defined(DEBUG_NOISY)
161 if (nvram_match("debug_logrc", "1")) {
162 int i;
164 cprintf("[rc %d] ", getpid());
165 for (i = 0; i < argc; ++i) {
166 cprintf("%s ", argv[i]);
168 cprintf("\n");
171 #endif
173 #if defined(DEBUG_NOISY)
174 if (nvram_match("debug_ovrc", "1")) {
175 char tmp[256];
176 char *a[32];
178 realpath(argv[0], tmp);
179 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
180 sprintf(tmp, "%s%s", "/tmp/", base);
181 if (f_exists(tmp)) {
182 cprintf("[rc] override: %s\n", tmp);
183 memcpy(a, argv, argc * sizeof(a[0]));
184 a[argc] = 0;
185 a[0] = tmp;
186 execvp(tmp, a);
187 exit(0);
191 #endif
193 const applets_t *a;
194 for (a = applets; a->name; ++a) {
195 if (strcmp(base, a->name) == 0) {
196 openlog(base, LOG_PID, LOG_USER);
197 return a->main(argc, argv);
201 printf("Unknown applet: %s\n", base);
202 return 0;