Merge branch 'tomato-ND-usbmod-mixvpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / rc.c
blobd1c10b94365a18947534d847b5d2c2fc6267be12
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 DEBUG_IPTFILE
27 else if (strcmp(argv[1], "iptfile") == 0) {
28 create_test_iptfile();
30 #endif
31 else {
32 printf("what?\n");
35 return 0;
37 #endif
40 static int hotplug_main(int argc, char *argv[])
42 if (argc >= 2) {
43 if (strcmp(argv[1], "net") == 0) {
44 hotplug_net();
46 #ifdef TCONFIG_USB
47 // !!TB - USB Support
48 else if (strcmp(argv[1], "usb") == 0) {
49 hotplug_usb();
51 #ifdef LINUX26
52 else if (strcmp(argv[1], "block") == 0) {
53 hotplug_usb();
55 #endif
56 #endif
58 return 0;
61 static int rc_main(int argc, char *argv[])
63 if (argc < 2) return 0;
64 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
65 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
66 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
67 return 0;
72 typedef struct {
73 const char *name;
74 int (*main)(int argc, char *argv[]);
75 } applets_t;
77 static const applets_t applets[] = {
78 { "init", init_main },
79 { "rc", rc_main },
80 { "ip-up", ipup_main },
81 { "ip-down", ipdown_main },
82 { "set-pppoepid", set_pppoepid_main },
83 { "pppoe_down", pppoe_down_main },
84 { "ppp_event", pppevent_main },
85 { "hotplug", hotplug_main },
86 { "redial", redial_main },
87 { "listen", listen_main },
88 { "service", service_main },
89 { "sched", sched_main },
90 { "mtd-write", mtd_write_main },
91 { "mtd-erase", mtd_unlock_erase_main },
92 { "mtd-unlock", mtd_unlock_erase_main },
93 { "buttons", buttons_main },
94 { "rcheck", rcheck_main },
95 { "dhcpc-event", dhcpc_event_main },
96 { "dhcpc-release", dhcpc_release_main },
97 { "dhcpc-renew", dhcpc_renew_main },
98 { "radio", radio_main },
99 { "led", led_main },
100 { "halt", reboothalt_main },
101 { "reboot", reboothalt_main },
102 { "gpio", gpio_main },
103 { "wldist", wldist_main },
104 #ifdef TCONFIG_CIFS
105 { "mount-cifs", mount_cifs_main },
106 #endif
107 #ifdef TCONFIG_DDNS
108 { "ddns-update", ddns_update_main },
109 #endif
110 #ifdef DEBUG_RCTEST
111 { "rctest", rctest_main },
112 #endif
113 {NULL, NULL}
116 int main(int argc, char **argv)
118 char *base;
119 int f;
122 Make sure std* are valid since several functions attempt to close these
123 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
125 if ((f = open("/dev/null", O_RDWR)) < 3) {
126 dup(f);
127 dup(f);
129 else {
130 close(f);
133 base = strrchr(argv[0], '/');
134 base = base ? base + 1 : argv[0];
136 #if 0
137 if (strcmp(base, "rc") == 0) {
138 if (argc < 2) return 1;
139 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
140 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
141 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
142 ++argv;
143 --argc;
144 base = argv[0];
146 #endif
148 #if defined(DEBUG_NOISY)
149 if (nvram_match("debug_logrc", "1")) {
150 int i;
152 cprintf("[rc %d] ", getpid());
153 for (i = 0; i < argc; ++i) {
154 cprintf("%s ", argv[i]);
156 cprintf("\n");
159 #endif
161 #if defined(DEBUG_NOISY)
162 if (nvram_match("debug_ovrc", "1")) {
163 char tmp[256];
164 char *a[32];
166 realpath(argv[0], tmp);
167 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
168 sprintf(tmp, "%s%s", "/tmp/", base);
169 if (f_exists(tmp)) {
170 cprintf("[rc] override: %s\n", tmp);
171 memcpy(a, argv, argc * sizeof(a[0]));
172 a[argc] = 0;
173 a[0] = tmp;
174 execvp(tmp, a);
175 exit(0);
179 #endif
181 const applets_t *a;
182 for (a = applets; a->name; ++a) {
183 if (strcmp(base, a->name) == 0) {
184 openlog(base, LOG_PID, LOG_USER);
185 return a->main(argc, argv);
189 printf("Unknown applet: %s\n", base);
190 return 0;